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>
31 #include "adt/error.h"
32 #include "adt/array.h"
38 #include "diagnostic.h"
39 #include "lang_features.h"
41 #include "driver/firm_opt.h"
42 #include "driver/firm_cmdline.h"
44 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
46 /* some idents needed for name mangling */
47 static ident *id_underscore;
50 static ir_type *ir_type_const_char;
51 static ir_type *ir_type_wchar_t;
52 static ir_type *ir_type_void;
53 static ir_type *ir_type_int;
55 static type_t *type_const_char;
57 static int next_value_number_function;
58 static ir_node *continue_label;
59 static ir_node *break_label;
60 static ir_node *current_switch_cond;
61 static bool saw_default_label;
62 static ir_node **imature_blocks;
64 static const declaration_t *current_function_decl;
65 static ir_node *current_function_name;
66 static ir_node *current_funcsig;
68 static struct obstack asm_obst;
70 typedef enum declaration_kind_t {
71 DECLARATION_KIND_UNKNOWN,
72 DECLARATION_KIND_FUNCTION,
73 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
74 DECLARATION_KIND_GLOBAL_VARIABLE,
75 DECLARATION_KIND_LOCAL_VARIABLE,
76 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
77 DECLARATION_KIND_COMPOUND_MEMBER,
78 DECLARATION_KIND_LABEL_BLOCK,
79 DECLARATION_KIND_ENUM_ENTRY,
80 DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE,
81 DECLARATION_KIND_COMPOUND_TYPE_COMPLETE,
85 static ir_type *get_ir_type(type_t *type);
86 static ir_type *get_ir_type_incomplete(type_t *type);
87 static int count_decls_in_stmts(const statement_t *stmt);
89 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
91 const declaration_t *declaration = get_irg_loc_description(irg, pos);
93 warningf(&declaration->source_position,
94 "variable '%#T' might be used uninitialized",
95 declaration->type, declaration->symbol);
96 return new_r_Unknown(irg, mode);
99 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
101 const source_position_t *pos = (const source_position_t*) dbg;
104 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
108 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
110 const source_position_t *pos = (const source_position_t*) dbg;
115 return pos->input_name;
118 static dbg_info *get_dbg_info(const source_position_t *pos)
120 return (dbg_info*) pos;
123 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST+1];
125 static ir_mode *mode_int, *mode_uint;
127 static ir_node *expression_to_firm(const expression_t *expression);
128 static inline ir_mode *get_ir_mode(type_t *type);
129 static void create_local_declaration(declaration_t *declaration);
131 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
133 unsigned flags = get_atomic_type_flags(kind);
134 unsigned size = get_atomic_type_size(kind);
135 if( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
136 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
139 unsigned bit_size = size * 8;
140 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
141 ir_mode_arithmetic arithmetic;
142 unsigned modulo_shift;
144 if(flags & ATOMIC_TYPE_FLAG_INTEGER) {
145 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
146 snprintf(name, sizeof(name), "i%s%d", is_signed?"":"u", bit_size);
147 sort = irms_int_number;
148 arithmetic = irma_twos_complement;
149 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
151 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
152 snprintf(name, sizeof(name), "f%d", bit_size);
153 sort = irms_float_number;
154 arithmetic = irma_ieee754;
157 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
165 * Initialises the atomic modes depending on the machine size.
167 static void init_atomic_modes(void)
169 for(int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
170 _atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
172 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
173 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
175 /* there's no real void type in firm */
176 _atomic_modes[ATOMIC_TYPE_VOID] = mode_int;
178 /* initialize pointer modes */
180 ir_mode_sort sort = irms_reference;
181 unsigned bit_size = machine_size;
183 ir_mode_arithmetic arithmetic = irma_twos_complement;
184 unsigned modulo_shift
185 = bit_size < machine_size ? machine_size : bit_size;
187 snprintf(name, sizeof(name), "p%d", machine_size);
188 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
191 set_reference_mode_signed_eq(ptr_mode, _atomic_modes[get_intptr_kind()]);
192 set_reference_mode_unsigned_eq(ptr_mode, _atomic_modes[get_uintptr_kind()]);
194 /* Hmm, pointers should be machine size */
195 set_modeP_data(ptr_mode);
196 set_modeP_code(ptr_mode);
199 static unsigned get_compound_type_size(compound_type_t *type)
201 ir_type *irtype = get_ir_type((type_t*) type);
202 return get_type_size_bytes(irtype);
205 static unsigned get_array_type_size(array_type_t *type)
207 assert(!type->is_vla);
208 ir_type *irtype = get_ir_type((type_t*) type);
209 return get_type_size_bytes(irtype);
213 static unsigned get_type_size_const(type_t *type)
215 type = skip_typeref(type);
219 panic("error type occured");
221 return get_atomic_type_size(type->atomic.akind);
223 return 2 * get_atomic_type_size(type->complex.akind);
225 return get_atomic_type_size(type->imaginary.akind);
227 return get_mode_size_bytes(mode_int);
228 case TYPE_COMPOUND_UNION:
229 case TYPE_COMPOUND_STRUCT:
230 return get_compound_type_size(&type->compound);
232 /* just a pointer to the function */
233 return get_mode_size_bytes(mode_P_code);
235 return get_mode_size_bytes(mode_P_data);
237 return get_array_type_size(&type->array);
239 return get_type_size_const(type->builtin.real_type);
241 panic("type size of bitfield request");
247 panic("Trying to determine size of invalid type");
250 static ir_node *get_type_size(type_t *type)
252 type = skip_typeref(type);
254 if(is_type_array(type) && type->array.is_vla) {
255 ir_node *size_node = type->array.size_node;
256 if(size_node == NULL) {
257 size_node = expression_to_firm(type->array.size_expression);
258 assert(!is_Const(size_node));
259 type->array.size_node = size_node;
262 ir_node *elem_size = get_type_size(type->array.element_type);
263 ir_mode *mode = get_irn_mode(size_node);
264 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
268 ir_mode *mode = get_ir_mode(type_size_t);
270 sym.type_p = get_ir_type(type);
271 return new_SymConst(mode, sym, symconst_type_size);
274 static unsigned count_parameters(const function_type_t *function_type)
278 function_parameter_t *parameter = function_type->parameters;
279 for ( ; parameter != NULL; parameter = parameter->next) {
287 * Creates a Firm type for an atomic type
289 static ir_type *create_atomic_type(const atomic_type_t *type)
291 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
292 atomic_type_kind_t kind = type->akind;
293 ir_mode *mode = _atomic_modes[kind];
294 ident *id = get_mode_ident(mode);
295 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
297 set_type_alignment_bytes(irtype, type->base.alignment);
303 * Creates a Firm type for a complex type
305 static ir_type *create_complex_type(const complex_type_t *type)
307 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
308 atomic_type_kind_t kind = type->akind;
309 ir_mode *mode = _atomic_modes[kind];
310 ident *id = get_mode_ident(mode);
315 /* FIXME: finish the array */
320 * Creates a Firm type for an imaginary type
322 static ir_type *create_imaginary_type(const imaginary_type_t *type)
324 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
325 atomic_type_kind_t kind = type->akind;
326 ir_mode *mode = _atomic_modes[kind];
327 ident *id = get_mode_ident(mode);
328 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
330 set_type_alignment_bytes(irtype, type->base.alignment);
335 static ir_type *create_method_type(const function_type_t *function_type)
337 type_t *return_type = function_type->return_type;
339 ident *id = id_unique("functiontype.%u");
340 int n_parameters = count_parameters(function_type);
341 int n_results = return_type == type_void ? 0 : 1;
342 dbg_info *dbgi = get_dbg_info(&function_type->base.source_position);
343 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
345 if(return_type != type_void) {
346 ir_type *restype = get_ir_type(return_type);
347 set_method_res_type(irtype, 0, restype);
350 function_parameter_t *parameter = function_type->parameters;
352 for( ; parameter != NULL; parameter = parameter->next) {
353 ir_type *p_irtype = get_ir_type(parameter->type);
354 set_method_param_type(irtype, n, p_irtype);
358 if(function_type->variadic || function_type->unspecified_parameters) {
359 set_method_variadicity(irtype, variadicity_variadic);
365 static ir_type *create_pointer_type(pointer_type_t *type)
367 type_t *points_to = type->points_to;
368 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
369 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
370 ir_type *ir_type = new_d_type_pointer(id_unique("pointer.%u"),
371 ir_points_to, mode_P_data, dbgi);
376 static ir_type *create_array_type(array_type_t *type)
378 type_t *element_type = type->element_type;
379 ir_type *ir_element_type = get_ir_type(element_type);
381 ident *id = id_unique("array.%u");
382 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
383 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
385 const int align = get_type_alignment_bytes(ir_element_type);
386 set_type_alignment_bytes(ir_type, align);
388 if(type->size_constant) {
389 int n_elements = type->size;
391 set_array_bounds_int(ir_type, 0, 0, n_elements);
393 size_t elemsize = get_type_size_bytes(ir_element_type);
394 if(elemsize % align > 0) {
395 elemsize += align - (elemsize % align);
397 set_type_size_bytes(ir_type, n_elements * elemsize);
399 set_array_lower_bound_int(ir_type, 0, 0);
401 set_type_state(ir_type, layout_fixed);
407 * Return the signed integer type of size bits.
409 * @param size the size
411 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
414 static ir_mode *s_modes[64 + 1] = {NULL, };
418 if (size <= 0 || size > 64)
421 mode = s_modes[size];
425 snprintf(name, sizeof(name), "bf_I%u", size);
426 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
427 size <= 32 ? 32 : size );
428 s_modes[size] = mode;
432 snprintf(name, sizeof(name), "I%u", size);
433 ident *id = new_id_from_str(name);
434 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
435 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
436 set_primitive_base_type(res, base_tp);
442 * Return the unsigned integer type of size bits.
444 * @param size the size
446 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
449 static ir_mode *u_modes[64 + 1] = {NULL, };
453 if (size <= 0 || size > 64)
456 mode = u_modes[size];
460 snprintf(name, sizeof(name), "bf_U%u", size);
461 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
462 size <= 32 ? 32 : size );
463 u_modes[size] = mode;
468 snprintf(name, sizeof(name), "U%u", size);
469 ident *id = new_id_from_str(name);
470 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
471 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
472 set_primitive_base_type(res, base_tp);
477 static ir_type *create_bitfield_type(bitfield_type_t *const type)
479 type_t *base = skip_typeref(type->base_type);
480 assert(base->kind == TYPE_ATOMIC);
481 ir_type *irbase = get_ir_type(base);
483 unsigned size = fold_constant(type->size);
485 assert(!is_type_float(base));
486 if(is_type_signed(base)) {
487 return get_signed_int_type_for_bit_size(irbase, size);
489 return get_unsigned_int_type_for_bit_size(irbase, size);
493 #define INVALID_TYPE ((ir_type_ptr)-1)
496 COMPOUND_IS_STRUCT = false,
497 COMPOUND_IS_UNION = true
501 * Construct firm type from ast struct tyep.
503 * As anonymous inner structs get flattened to a single firm type, we might get
504 * irtype, outer_offset and out_align passed (they represent the position of
505 * the anonymous inner struct inside the resulting firm struct)
507 static ir_type *create_compound_type(compound_type_t *type, ir_type *irtype,
508 size_t *outer_offset, size_t *outer_align,
509 bool incomplete, bool is_union)
511 declaration_t *declaration = type->declaration;
512 declaration_kind_t kind = declaration->declaration_kind;
514 if(kind == DECLARATION_KIND_COMPOUND_TYPE_COMPLETE
515 || (kind == DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE
517 return declaration->v.irtype;
519 size_t align_all = 1;
521 size_t bit_offset = 0;
525 symbol_t *symbol = declaration->symbol;
528 id = new_id_from_str(symbol->string);
531 id = id_unique("__anonymous_union.%u");
533 id = id_unique("__anonymous_struct.%u");
536 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
539 irtype = new_d_type_union(id, dbgi);
541 irtype = new_d_type_struct(id, dbgi);
544 declaration->declaration_kind
545 = DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE;
546 declaration->v.irtype = irtype;
547 //type->base.firm_type = irtype;
549 offset = *outer_offset;
550 align_all = *outer_align;
556 declaration->declaration_kind = DECLARATION_KIND_COMPOUND_TYPE_COMPLETE;
558 declaration_t *entry = declaration->scope.declarations;
559 for( ; entry != NULL; entry = entry->next) {
560 if(entry->namespc != NAMESPACE_NORMAL)
563 size_t prev_offset = offset;
565 symbol_t *symbol = entry->symbol;
566 type_t *entry_type = skip_typeref(entry->type);
567 dbg_info *dbgi = get_dbg_info(&entry->source_position);
571 ident = new_id_from_str(symbol->string);
573 if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
574 create_compound_type(&entry_type->compound, irtype, &offset,
575 &align_all, false, COMPOUND_IS_STRUCT);
576 goto finished_member;
577 } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
578 create_compound_type(&entry_type->compound, irtype, &offset,
579 &align_all, false, COMPOUND_IS_UNION);
580 goto finished_member;
582 assert(entry_type->kind == TYPE_BITFIELD);
584 ident = id_unique("anon.%u");
587 ir_type *base_irtype;
588 if(entry_type->kind == TYPE_BITFIELD) {
589 base_irtype = get_ir_type(entry_type->bitfield.base_type);
591 base_irtype = get_ir_type(entry_type);
594 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
595 size_t misalign = offset % entry_alignment;
597 ir_type *entry_irtype = get_ir_type(entry_type);
598 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
601 size_t bits_remainder;
602 if(entry_type->kind == TYPE_BITFIELD) {
603 size_t size_bits = fold_constant(entry_type->bitfield.size);
604 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
606 if(size_bits > rest_size_bits) {
607 /* start a new bucket */
608 offset += entry_alignment - misalign;
614 /* put into current bucket */
615 base = offset - misalign;
616 bits_remainder = misalign * 8 + bit_offset;
619 offset += size_bits / 8;
620 bit_offset = bit_offset + (size_bits % 8);
622 size_t entry_size = get_type_size_bytes(base_irtype);
623 if(misalign > 0 || bit_offset > 0)
624 offset += entry_alignment - misalign;
628 offset += entry_size;
632 if(entry_alignment > align_all) {
633 if(entry_alignment % align_all != 0) {
634 panic("uneven alignments not supported yet");
636 align_all = entry_alignment;
639 set_entity_offset(entity, base);
640 set_entity_offset_bits_remainder(entity,
641 (unsigned char) bits_remainder);
642 //add_struct_member(irtype, entity);
643 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
644 assert(entry->v.entity == NULL);
645 entry->v.entity = entity;
649 size_t entry_size = offset - prev_offset;
650 if (entry_size > size) {
662 size_t misalign = offset % align_all;
663 if(misalign > 0 || bit_offset > 0) {
664 size += align_all - misalign;
667 if(outer_offset != NULL) {
669 *outer_offset = offset;
671 *outer_offset += size;
674 if (align_all > *outer_align) {
675 if(align_all % *outer_align != 0) {
676 panic("uneven alignments not supported yet");
678 *outer_align = align_all;
681 set_type_alignment_bytes(irtype, align_all);
682 set_type_size_bytes(irtype, size);
683 set_type_state(irtype, layout_fixed);
689 static ir_type *create_enum_type(enum_type_t *const type)
691 type->base.firm_type = ir_type_int;
693 ir_mode *const mode = mode_int;
694 tarval *const one = get_mode_one(mode);
695 tarval * tv_next = get_tarval_null(mode);
697 declaration_t *declaration = type->declaration->next;
698 for (; declaration != NULL; declaration = declaration->next) {
699 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
702 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
704 expression_t *const init = declaration->init.enum_value;
706 ir_node *const cnst = expression_to_firm(init);
707 if (!is_Const(cnst)) {
708 panic("couldn't fold constant");
710 tv_next = get_Const_tarval(cnst);
712 declaration->v.enum_val = tv_next;
713 tv_next = tarval_add(tv_next, one);
719 static ir_type *get_ir_type_incomplete(type_t *type)
721 assert(type != NULL);
722 type = skip_typeref(type);
724 if (type->base.firm_type != NULL) {
725 assert(type->base.firm_type != INVALID_TYPE);
726 return type->base.firm_type;
729 switch (type->kind) {
730 case TYPE_COMPOUND_STRUCT:
731 return create_compound_type(&type->compound, NULL, NULL, NULL,
732 true, COMPOUND_IS_STRUCT);
734 case TYPE_COMPOUND_UNION:
735 return create_compound_type(&type->compound, NULL, NULL, NULL,
736 true, COMPOUND_IS_UNION);
738 return get_ir_type(type);
742 static ir_type *get_ir_type(type_t *type)
744 assert(type != NULL);
746 type = skip_typeref(type);
748 if (type->base.firm_type != NULL) {
749 assert(type->base.firm_type != INVALID_TYPE);
750 return type->base.firm_type;
753 ir_type *firm_type = NULL;
754 switch (type->kind) {
756 panic("error type occurred");
758 firm_type = create_atomic_type(&type->atomic);
761 firm_type = create_complex_type(&type->complex);
764 firm_type = create_imaginary_type(&type->imaginary);
767 firm_type = create_method_type(&type->function);
770 firm_type = create_pointer_type(&type->pointer);
773 firm_type = create_array_type(&type->array);
775 case TYPE_COMPOUND_STRUCT:
776 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
777 false, COMPOUND_IS_STRUCT);
779 case TYPE_COMPOUND_UNION:
780 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
781 false, COMPOUND_IS_UNION);
784 firm_type = create_enum_type(&type->enumt);
787 firm_type = get_ir_type(type->builtin.real_type);
790 firm_type = create_bitfield_type(&type->bitfield);
798 if(firm_type == NULL)
799 panic("unknown type found");
801 type->base.firm_type = firm_type;
805 static inline ir_mode *get_ir_mode(type_t *type)
807 ir_type *irtype = get_ir_type(type);
809 /* firm doesn't report a mode for arrays somehow... */
810 if(is_Array_type(irtype)) {
814 ir_mode *mode = get_type_mode(irtype);
815 assert(mode != NULL);
819 /** Names of the runtime functions. */
820 static const struct {
821 int id; /**< the rts id */
822 int n_res; /**< number of return values */
823 const char *name; /**< the name of the rts function */
824 int n_params; /**< number of parameters */
825 unsigned flags; /**< language flags */
827 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
828 { rts_abort, 0, "abort", 0, _C89 },
829 { rts_alloca, 1, "alloca", 1, _GNUC },
830 { rts_abs, 1, "abs", 1, _C89 },
831 { rts_labs, 1, "labs", 1, _C89 },
832 { rts_llabs, 1, "llabs", 1, _C99 },
833 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
835 { rts_fabs, 1, "fabs", 1, _C89 },
836 { rts_sqrt, 1, "sqrt", 1, _C89 },
837 { rts_cbrt, 1, "cbrt", 1, _C99 },
838 { rts_exp, 1, "exp", 1, _C89 },
839 { rts_exp2, 1, "exp2", 1, _C89 },
840 { rts_exp10, 1, "exp10", 1, _GNUC },
841 { rts_log, 1, "log", 1, _C89 },
842 { rts_log2, 1, "log2", 1, _C89 },
843 { rts_log10, 1, "log10", 1, _C89 },
844 { rts_pow, 1, "pow", 2, _C89 },
845 { rts_sin, 1, "sin", 1, _C89 },
846 { rts_cos, 1, "cos", 1, _C89 },
847 { rts_tan, 1, "tan", 1, _C89 },
848 { rts_asin, 1, "asin", 1, _C89 },
849 { rts_acos, 1, "acos", 1, _C89 },
850 { rts_atan, 1, "atan", 1, _C89 },
851 { rts_sinh, 1, "sinh", 1, _C89 },
852 { rts_cosh, 1, "cosh", 1, _C89 },
853 { rts_tanh, 1, "tanh", 1, _C89 },
855 { rts_fabsf, 1, "fabsf", 1, _C99 },
856 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
857 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
858 { rts_expf, 1, "expf", 1, _C99 },
859 { rts_exp2f, 1, "exp2f", 1, _C99 },
860 { rts_exp10f, 1, "exp10f", 1, _GNUC },
861 { rts_logf, 1, "logf", 1, _C99 },
862 { rts_log2f, 1, "log2f", 1, _C99 },
863 { rts_log10f, 1, "log10f", 1, _C99 },
864 { rts_powf, 1, "powf", 2, _C99 },
865 { rts_sinf, 1, "sinf", 1, _C99 },
866 { rts_cosf, 1, "cosf", 1, _C99 },
867 { rts_tanf, 1, "tanf", 1, _C99 },
868 { rts_asinf, 1, "asinf", 1, _C99 },
869 { rts_acosf, 1, "acosf", 1, _C99 },
870 { rts_atanf, 1, "atanf", 1, _C99 },
871 { rts_sinhf, 1, "sinhf", 1, _C99 },
872 { rts_coshf, 1, "coshf", 1, _C99 },
873 { rts_tanhf, 1, "tanhf", 1, _C99 },
875 { rts_fabsl, 1, "fabsl", 1, _C99 },
876 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
877 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
878 { rts_expl, 1, "expl", 1, _C99 },
879 { rts_exp2l, 1, "exp2l", 1, _C99 },
880 { rts_exp10l, 1, "exp10l", 1, _GNUC },
881 { rts_logl, 1, "logl", 1, _C99 },
882 { rts_log2l, 1, "log2l", 1, _C99 },
883 { rts_log10l, 1, "log10l", 1, _C99 },
884 { rts_powl, 1, "powl", 2, _C99 },
885 { rts_sinl, 1, "sinl", 1, _C99 },
886 { rts_cosl, 1, "cosl", 1, _C99 },
887 { rts_tanl, 1, "tanl", 1, _C99 },
888 { rts_asinl, 1, "asinl", 1, _C99 },
889 { rts_acosl, 1, "acosl", 1, _C99 },
890 { rts_atanl, 1, "atanl", 1, _C99 },
891 { rts_sinhl, 1, "sinhl", 1, _C99 },
892 { rts_coshl, 1, "coshl", 1, _C99 },
893 { rts_tanhl, 1, "tanhl", 1, _C99 },
895 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
896 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
897 { rts_strcmp, 1, "strcmp", 2, _C89 },
898 { rts_strncmp, 1, "strncmp", 3, _C89 }
901 static ident *rts_idents[sizeof(rts_data) / sizeof(rts_data[0])];
904 * Mangles an entity linker (ld) name for win32 usage.
906 * @param ent the entity to be mangled
907 * @param declaration the declaration
909 static ident *create_ld_ident_win32(ir_entity *ent, declaration_t *declaration)
913 if (is_Method_type(get_entity_type(ent)))
914 id = decorate_win32_c_fkt(ent, get_entity_ident(ent));
916 /* always add an underscore in win32 */
917 id = mangle(id_underscore, get_entity_ident(ent));
920 decl_modifiers_t decl_modifiers = declaration->decl_modifiers;
921 if (decl_modifiers & DM_DLLIMPORT) {
922 /* add prefix for imported symbols */
923 id = mangle(id_imp, id);
929 * Mangles an entity linker (ld) name for Linux ELF usage.
931 * @param ent the entity to be mangled
932 * @param declaration the declaration
934 static ident *create_ld_ident_linux_elf(ir_entity *entity,
935 declaration_t *declaration)
938 return get_entity_ident(entity);
942 * Mangles an entity linker (ld) name for Mach-O usage.
944 * @param ent the entity to be mangled
945 * @param declaration the declaration
947 static ident *create_ld_ident_macho(ir_entity *ent, declaration_t *declaration)
950 ident *id = mangle(id_underscore, get_entity_ident(ent));
954 typedef ident* (*create_ld_ident_func)(ir_entity *entity,
955 declaration_t *declaration);
956 create_ld_ident_func create_ld_ident = create_ld_ident_linux_elf;
958 static ir_entity* get_function_entity(declaration_t *declaration)
960 if(declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
961 return declaration->v.entity;
962 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
964 symbol_t *symbol = declaration->symbol;
965 ident *id = new_id_from_str(symbol->string);
967 ir_type *global_type = get_glob_type();
968 ir_type *ir_type_method = get_ir_type(declaration->type);
969 assert(is_Method_type(ir_type_method));
971 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
972 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
973 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
974 if(declaration->storage_class == STORAGE_CLASS_STATIC &&
975 declaration->init.statement == NULL) {
976 /* this entity was declared, but never defined */
977 set_entity_peculiarity(entity, peculiarity_description);
979 if(declaration->storage_class == STORAGE_CLASS_STATIC
980 || declaration->is_inline) {
981 set_entity_visibility(entity, visibility_local);
982 } else if(declaration->init.statement != NULL) {
983 set_entity_visibility(entity, visibility_external_visible);
985 set_entity_visibility(entity, visibility_external_allocated);
987 set_entity_allocation(entity, allocation_static);
989 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
990 declaration->v.entity = entity;
992 /* We should check for file scope here, but as long as we compile C only
993 this is not needed. */
994 if (! firm_opt.freestanding) {
995 /* check for a known runtime function */
996 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
997 if (id != rts_idents[i])
1000 /* ignore those rts functions not necessary needed for current mode */
1001 if ((c_mode & rts_data[i].flags) == 0)
1003 assert(rts_entities[rts_data[i].id] == NULL);
1004 rts_entities[rts_data[i].id] = entity;
1011 static ir_node *const_to_firm(const const_expression_t *cnst)
1013 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1014 ir_mode *mode = get_ir_mode(cnst->base.type);
1019 if(mode_is_float(mode)) {
1020 tv = new_tarval_from_double(cnst->v.float_value, mode);
1022 if(mode_is_signed(mode)) {
1023 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1025 len = snprintf(buf, sizeof(buf), "%llu",
1026 (unsigned long long) cnst->v.int_value);
1028 tv = new_tarval_from_str(buf, len, mode);
1031 return new_d_Const(dbgi, mode, tv);
1034 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1036 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1037 ir_mode *mode = get_ir_mode(cnst->base.type);
1039 long long int v = 0;
1040 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1041 if (char_is_signed) {
1042 v = (v << 8) | ((signed char)cnst->v.character.begin[i]);
1044 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1048 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1049 tarval *tv = new_tarval_from_str(buf, len, mode);
1051 return new_d_Const(dbgi, mode, tv);
1054 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1056 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1057 ir_mode *mode = get_ir_mode(cnst->base.type);
1059 long long int v = cnst->v.wide_character.begin[0];
1062 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1063 tarval *tv = new_tarval_from_str(buf, len, mode);
1065 return new_d_Const(dbgi, mode, tv);
1068 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1071 assert(entity != NULL);
1072 union symconst_symbol sym;
1073 sym.entity_p = entity;
1074 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1077 static ir_node *string_to_firm(const source_position_t *const src_pos,
1078 const char *const id_prefix,
1079 const string_t *const value)
1081 ir_type *const global_type = get_glob_type();
1082 dbg_info *const dbgi = get_dbg_info(src_pos);
1083 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1084 ir_type_const_char, dbgi);
1086 ident *const id = id_unique(id_prefix);
1087 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1088 set_entity_ld_ident(entity, id);
1089 set_entity_variability(entity, variability_constant);
1090 set_entity_allocation(entity, allocation_static);
1092 ir_type *const elem_type = ir_type_const_char;
1093 ir_mode *const mode = get_type_mode(elem_type);
1095 const char* const string = value->begin;
1096 const size_t slen = value->size;
1098 set_array_lower_bound_int(type, 0, 0);
1099 set_array_upper_bound_int(type, 0, slen);
1100 set_type_size_bytes(type, slen);
1101 set_type_state(type, layout_fixed);
1103 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1104 for(size_t i = 0; i < slen; ++i) {
1105 tvs[i] = new_tarval_from_long(string[i], mode);
1108 set_array_entity_values(entity, tvs, slen);
1111 return create_symconst(dbgi, mode_P_data, entity);
1114 static ir_node *string_literal_to_firm(
1115 const string_literal_expression_t* literal)
1117 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1121 static ir_node *wide_string_literal_to_firm(
1122 const wide_string_literal_expression_t* const literal)
1124 ir_type *const global_type = get_glob_type();
1125 ir_type *const elem_type = ir_type_wchar_t;
1126 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1127 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1130 ident *const id = id_unique("Lstr.%u");
1131 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1132 set_entity_ld_ident(entity, id);
1133 set_entity_variability(entity, variability_constant);
1134 set_entity_allocation(entity, allocation_static);
1136 ir_mode *const mode = get_type_mode(elem_type);
1138 const wchar_rep_t *const string = literal->value.begin;
1139 const size_t slen = literal->value.size;
1141 set_array_lower_bound_int(type, 0, 0);
1142 set_array_upper_bound_int(type, 0, slen);
1143 set_type_size_bytes(type, slen);
1144 set_type_state(type, layout_fixed);
1146 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1147 for(size_t i = 0; i < slen; ++i) {
1148 tvs[i] = new_tarval_from_long(string[i], mode);
1151 set_array_entity_values(entity, tvs, slen);
1154 return create_symconst(dbgi, mode_P_data, entity);
1157 static ir_node *deref_address(type_t *const type, ir_node *const addr,
1158 dbg_info *const dbgi)
1160 ir_type *irtype = get_ir_type(type);
1161 if (is_compound_type(irtype)
1162 || is_Method_type(irtype)
1163 || is_Array_type(irtype)) {
1167 ir_mode *const mode = get_type_mode(irtype);
1168 ir_node *const memory = get_store();
1169 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1170 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1171 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1173 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1174 set_Load_volatility(load, volatility_is_volatile);
1177 set_store(load_mem);
1181 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1183 ir_mode *mode = get_irn_mode(node);
1185 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1187 if (!mode_is_float(mode))
1190 /* check if there is already a Conv */
1191 if (get_irn_op(node) == op_Conv) {
1192 /* convert it into a strict Conv */
1193 set_Conv_strict(node, 1);
1197 /* otherwise create a new one */
1198 return new_d_strictConv(dbgi, node, mode);
1201 static ir_node *get_global_var_address(dbg_info *const dbgi,
1202 const declaration_t *const decl)
1204 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1206 ir_entity *const entity = decl->v.entity;
1207 switch ((storage_class_tag_t)decl->storage_class) {
1208 case STORAGE_CLASS_THREAD:
1209 case STORAGE_CLASS_THREAD_EXTERN:
1210 case STORAGE_CLASS_THREAD_STATIC: {
1211 ir_node *const no_mem = new_NoMem();
1212 ir_node *const tls = get_irg_tls(current_ir_graph);
1213 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1217 return create_symconst(dbgi, mode_P_data, entity);
1221 /* Returns the correct base address depending on whether it is a parameter or a
1222 * normal local variable */
1223 static ir_node *get_local_frame(ir_entity *const ent)
1225 ir_graph *const irg = current_ir_graph;
1226 const ir_type *const owner = get_entity_owner(ent);
1227 if (owner == get_irg_frame_type(irg)) {
1228 return get_irg_frame(irg);
1230 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1231 return get_irg_value_param_base(irg);
1235 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1237 ir_mode *value_mode = get_irn_mode(value);
1239 if (value_mode == dest_mode || is_Bad(value))
1242 if(dest_mode == mode_b) {
1243 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1244 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1245 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1249 return new_d_Conv(dbgi, value, dest_mode);
1252 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1254 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1255 declaration_t *declaration = ref->declaration;
1256 type_t *type = skip_typeref(declaration->type);
1258 /* make sure the type is constructed */
1259 (void) get_ir_type(type);
1261 switch((declaration_kind_t) declaration->declaration_kind) {
1262 case DECLARATION_KIND_TYPE:
1263 case DECLARATION_KIND_UNKNOWN:
1266 case DECLARATION_KIND_ENUM_ENTRY: {
1267 ir_mode *const mode = get_ir_mode(type);
1268 return new_Const(mode, declaration->v.enum_val);
1271 case DECLARATION_KIND_LOCAL_VARIABLE: {
1272 ir_mode *const mode = get_ir_mode(type);
1273 return get_value(declaration->v.value_number, mode);
1275 case DECLARATION_KIND_FUNCTION: {
1276 ir_mode *const mode = get_ir_mode(type);
1277 return create_symconst(dbgi, mode, declaration->v.entity);
1279 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1280 ir_node *const addr = get_global_var_address(dbgi, declaration);
1281 return deref_address(declaration->type, addr, dbgi);
1284 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1285 ir_entity *entity = declaration->v.entity;
1286 ir_node *frame = get_local_frame(entity);
1287 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1288 return deref_address(declaration->type, sel, dbgi);
1291 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1292 return declaration->v.vla_base;
1294 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1295 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1296 case DECLARATION_KIND_COMPOUND_MEMBER:
1297 case DECLARATION_KIND_LABEL_BLOCK:
1298 panic("not implemented reference type");
1301 panic("reference to declaration with unknown type found");
1304 static ir_node *reference_addr(const reference_expression_t *ref)
1306 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1307 declaration_t *declaration = ref->declaration;
1309 switch((declaration_kind_t) declaration->declaration_kind) {
1310 case DECLARATION_KIND_TYPE:
1311 case DECLARATION_KIND_UNKNOWN:
1313 case DECLARATION_KIND_LOCAL_VARIABLE:
1314 panic("local variable without entity has no address");
1315 case DECLARATION_KIND_FUNCTION: {
1316 type_t *const type = skip_typeref(ref->base.type);
1317 ir_mode *const mode = get_ir_mode(type);
1318 return create_symconst(dbgi, mode, declaration->v.entity);
1320 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1321 ir_node *const addr = get_global_var_address(dbgi, declaration);
1324 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1325 ir_entity *entity = declaration->v.entity;
1326 ir_node *frame = get_local_frame(entity);
1327 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1332 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1333 return declaration->v.vla_base;
1335 case DECLARATION_KIND_ENUM_ENTRY:
1336 panic("trying to reference enum entry");
1338 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1339 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1340 case DECLARATION_KIND_COMPOUND_MEMBER:
1341 case DECLARATION_KIND_LABEL_BLOCK:
1342 panic("not implemented reference type");
1345 panic("reference to declaration with unknown type found");
1348 static ir_node *process_builtin_call(const call_expression_t *call)
1350 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1352 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1353 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1355 type_t *type = skip_typeref(builtin->base.type);
1356 assert(is_type_pointer(type));
1358 type_t *function_type = skip_typeref(type->pointer.points_to);
1359 symbol_t *symbol = builtin->symbol;
1361 switch(symbol->ID) {
1362 case T___builtin_alloca: {
1363 if(call->arguments == NULL || call->arguments->next != NULL) {
1364 panic("invalid number of parameters on __builtin_alloca");
1366 expression_t *argument = call->arguments->expression;
1367 ir_node *size = expression_to_firm(argument);
1369 ir_node *store = get_store();
1370 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1372 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1374 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1378 case T___builtin_huge_val: {
1379 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1380 tarval *tv = get_mode_infinite(mode);
1381 ir_node *res = new_d_Const(dbgi, mode, tv);
1384 case T___builtin_nan:
1385 case T___builtin_nanf:
1386 case T___builtin_nand: {
1387 /* Ignore string for now... */
1388 assert(is_type_function(function_type));
1389 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1390 tarval *tv = get_mode_NAN(mode);
1391 ir_node *res = new_d_Const(dbgi, mode, tv);
1394 case T___builtin_va_end:
1397 panic("Unsupported builtin found\n");
1401 static ir_node *call_expression_to_firm(const call_expression_t *call)
1403 assert(get_cur_block() != NULL);
1405 expression_t *function = call->function;
1406 if(function->kind == EXPR_BUILTIN_SYMBOL) {
1407 return process_builtin_call(call);
1409 ir_node *callee = expression_to_firm(function);
1411 type_t *type = skip_typeref(function->base.type);
1412 assert(is_type_pointer(type));
1413 pointer_type_t *pointer_type = &type->pointer;
1414 type_t *points_to = skip_typeref(pointer_type->points_to);
1415 assert(is_type_function(points_to));
1416 function_type_t *function_type = &points_to->function;
1418 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1420 int n_parameters = 0;
1421 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1422 ir_type *new_method_type = NULL;
1423 if(function_type->variadic || function_type->unspecified_parameters) {
1424 const call_argument_t *argument = call->arguments;
1425 for( ; argument != NULL; argument = argument->next) {
1429 /* we need to construct a new method type matching the call
1431 int n_res = get_method_n_ress(ir_method_type);
1432 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1433 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1434 n_parameters, n_res, dbgi);
1435 set_method_calling_convention(new_method_type,
1436 get_method_calling_convention(ir_method_type));
1437 set_method_additional_properties(new_method_type,
1438 get_method_additional_properties(ir_method_type));
1439 set_method_variadicity(new_method_type,
1440 get_method_variadicity(ir_method_type));
1442 for(int i = 0; i < n_res; ++i) {
1443 set_method_res_type(new_method_type, i,
1444 get_method_res_type(ir_method_type, i));
1446 argument = call->arguments;
1447 for(int i = 0; i < n_parameters; ++i, argument = argument->next) {
1448 expression_t *expression = argument->expression;
1449 ir_type *irtype = get_ir_type(expression->base.type);
1450 set_method_param_type(new_method_type, i, irtype);
1452 ir_method_type = new_method_type;
1454 n_parameters = get_method_n_params(ir_method_type);
1457 ir_node *in[n_parameters];
1459 const call_argument_t *argument = call->arguments;
1460 for(int n = 0; n < n_parameters; ++n) {
1461 expression_t *expression = argument->expression;
1462 ir_node *arg_node = expression_to_firm(expression);
1464 arg_node = do_strict_conv(dbgi, arg_node);
1468 argument = argument->next;
1471 ir_node *store = get_store();
1472 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1474 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1477 type_t *return_type = skip_typeref(function_type->return_type);
1478 ir_node *result = NULL;
1480 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1482 if(is_type_scalar(return_type)) {
1483 mode = get_ir_mode(return_type);
1487 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1488 result = new_d_Proj(dbgi, resproj, mode, 0);
1494 static void statement_to_firm(statement_t *statement);
1495 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1497 static ir_node *expression_to_addr(const expression_t *expression);
1498 static void create_condition_evaluation(const expression_t *expression,
1499 ir_node *true_block,
1500 ir_node *false_block);
1502 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1505 value = do_strict_conv(dbgi, value);
1507 ir_node *memory = get_store();
1509 if(is_type_scalar(type)) {
1510 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1511 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1512 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
1513 set_Store_volatility(store, volatility_is_volatile);
1514 set_store(store_mem);
1516 ir_type *irtype = get_ir_type(type);
1517 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1518 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1519 set_store(copyb_mem);
1523 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1525 tarval *all_one = get_mode_all_one(mode);
1526 int mode_size = get_mode_size_bits(mode);
1528 assert(offset >= 0 && size >= 0);
1529 assert(offset + size <= mode_size);
1530 if(size == mode_size) {
1534 long shiftr = get_mode_size_bits(mode) - size;
1535 long shiftl = offset;
1536 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1537 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1538 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1539 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1544 static void bitfield_store_to_firm(const unary_expression_t *expression,
1547 expression_t *select = expression->value;
1548 assert(select->kind == EXPR_SELECT);
1549 type_t *type = select->base.type;
1550 assert(type->kind == TYPE_BITFIELD);
1551 ir_mode *mode = get_ir_mode(type->bitfield.base_type);
1552 ir_node *addr = expression_to_addr(select);
1554 assert(get_irn_mode(value) == mode || is_Bad(value));
1556 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1558 /* kill upper bits of value and shift to right position */
1559 ir_entity *entity = select->select.compound_entry->v.entity;
1560 int bitoffset = get_entity_offset_bits_remainder(entity);
1561 ir_type *entity_type = get_entity_type(entity);
1562 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1564 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1565 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1566 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1567 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1568 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1569 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1571 /* load current value */
1572 ir_node *mem = get_store();
1573 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1574 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1575 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1576 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1577 tarval *inv_mask = tarval_not(shift_mask);
1578 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1579 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1581 /* construct new value and store */
1582 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1583 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1584 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1585 set_store(store_mem);
1587 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1588 set_Load_volatility(load, volatility_is_volatile);
1589 set_Store_volatility(store, volatility_is_volatile);
1593 static void set_value_for_expression(const expression_t *expression,
1596 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1597 value = do_strict_conv(dbgi, value);
1599 if(expression->kind == EXPR_REFERENCE) {
1600 const reference_expression_t *ref = &expression->reference;
1602 declaration_t *declaration = ref->declaration;
1603 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1604 if(declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1605 set_value(declaration->v.value_number, value);
1610 if(expression->kind == EXPR_UNARY_BITFIELD_EXTRACT) {
1611 bitfield_store_to_firm(&expression->unary, value);
1615 ir_node *addr = expression_to_addr(expression);
1616 type_t *type = skip_typeref(expression->base.type);
1617 assign_value(dbgi, addr, type, value);
1620 static ir_node *create_incdec(const unary_expression_t *expression)
1622 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1623 type_t *type = skip_typeref(expression->base.type);
1624 ir_mode *mode = get_ir_mode(type);
1625 expression_t *value = expression->value;
1627 ir_node *value_node = expression_to_firm(value);
1630 if(is_type_pointer(type)) {
1631 pointer_type_t *pointer_type = &type->pointer;
1632 offset = get_type_size(pointer_type->points_to);
1634 assert(is_type_arithmetic(type));
1635 offset = new_Const(mode, get_mode_one(mode));
1638 switch(expression->base.kind) {
1639 case EXPR_UNARY_POSTFIX_INCREMENT: {
1640 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1641 set_value_for_expression(value, new_value);
1644 case EXPR_UNARY_POSTFIX_DECREMENT: {
1645 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1646 set_value_for_expression(value, new_value);
1649 case EXPR_UNARY_PREFIX_INCREMENT: {
1650 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1651 set_value_for_expression(value, new_value);
1654 case EXPR_UNARY_PREFIX_DECREMENT: {
1655 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1656 set_value_for_expression(value, new_value);
1660 panic("no incdec expr in create_incdec");
1665 static bool is_local_variable(expression_t *expression)
1667 if (expression->kind != EXPR_REFERENCE)
1669 reference_expression_t *ref_expr = &expression->reference;
1670 declaration_t *declaration = ref_expr->declaration;
1671 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1674 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1677 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1678 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1679 case EXPR_BINARY_NOTEQUAL:
1680 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1681 case EXPR_BINARY_ISLESS:
1682 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1683 case EXPR_BINARY_ISLESSEQUAL:
1684 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1685 case EXPR_BINARY_ISGREATER:
1686 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1687 case EXPR_BINARY_ISGREATEREQUAL:
1688 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1689 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1694 panic("trying to get pn_Cmp from non-comparison binexpr type");
1698 * Handle the assume optimizer hint: check if a Confirm
1699 * node can be created.
1701 * @param dbi debug info
1702 * @param expr the IL assume expression
1704 * we support here only some simple cases:
1709 static ir_node *handle_assume_compare(dbg_info *dbi,
1710 const binary_expression_t *expression)
1712 expression_t *op1 = expression->left;
1713 expression_t *op2 = expression->right;
1714 declaration_t *var2, *var = NULL;
1715 ir_node *res = NULL;
1718 cmp_val = get_pnc(expression->base.kind, op1->base.type);
1720 if (is_local_variable(op1) && is_local_variable(op2)) {
1721 var = op1->reference.declaration;
1722 var2 = op2->reference.declaration;
1724 type_t *const type = skip_typeref(var->type);
1725 ir_mode *const mode = get_ir_mode(type);
1727 ir_node *const irn1 = get_value(var->v.value_number, mode);
1728 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1730 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1731 set_value(var2->v.value_number, res);
1733 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1734 set_value(var->v.value_number, res);
1740 if (is_local_variable(op1) && is_constant_expression(op2)) {
1741 var = op1->reference.declaration;
1743 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1744 cmp_val = get_inversed_pnc(cmp_val);
1745 var = op2->reference.declaration;
1750 type_t *const type = skip_typeref(var->type);
1751 ir_mode *const mode = get_ir_mode(type);
1753 res = get_value(var->v.value_number, mode);
1754 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1755 set_value(var->v.value_number, res);
1761 * Handle the assume optimizer hint.
1763 * @param dbi debug info
1764 * @param expr the IL assume expression
1766 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1767 switch(expression->kind) {
1768 case EXPR_BINARY_EQUAL:
1769 case EXPR_BINARY_NOTEQUAL:
1770 case EXPR_BINARY_LESS:
1771 case EXPR_BINARY_LESSEQUAL:
1772 case EXPR_BINARY_GREATER:
1773 case EXPR_BINARY_GREATEREQUAL:
1774 return handle_assume_compare(dbi, &expression->binary);
1780 static ir_node *bitfield_extract_to_firm(const unary_expression_t *expression)
1782 expression_t *select = expression->value;
1783 assert(select->kind == EXPR_SELECT);
1785 type_t *type = select->base.type;
1786 assert(type->kind == TYPE_BITFIELD);
1787 ir_mode *mode = get_ir_mode(type->bitfield.base_type);
1788 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1789 ir_node *addr = expression_to_addr(select);
1790 ir_node *mem = get_store();
1791 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1792 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1793 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1795 load_res = create_conv(dbgi, load_res, mode_int);
1797 set_store(load_mem);
1799 /* kill upper bits */
1800 ir_entity *entity = select->select.compound_entry->v.entity;
1801 int bitoffset = get_entity_offset_bits_remainder(entity);
1802 ir_type *entity_type = get_entity_type(entity);
1803 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1804 long shift_bitsl = machine_size - bitoffset - bitsize;
1805 assert(shift_bitsl >= 0);
1806 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1807 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1808 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1810 long shift_bitsr = bitoffset + shift_bitsl;
1811 assert(shift_bitsr <= (long) machine_size);
1812 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1813 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1815 if(mode_is_signed(mode)) {
1816 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1818 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1821 return create_conv(dbgi, shiftr, mode);
1824 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1826 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1827 type_t *type = skip_typeref(expression->base.type);
1829 if(expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
1830 return expression_to_addr(expression->value);
1832 const expression_t *value = expression->value;
1834 switch(expression->base.kind) {
1835 case EXPR_UNARY_NEGATE: {
1836 ir_node *value_node = expression_to_firm(value);
1837 ir_mode *mode = get_ir_mode(type);
1838 return new_d_Minus(dbgi, value_node, mode);
1840 case EXPR_UNARY_PLUS:
1841 return expression_to_firm(value);
1842 case EXPR_UNARY_BITWISE_NEGATE: {
1843 ir_node *value_node = expression_to_firm(value);
1844 ir_mode *mode = get_ir_mode(type);
1845 return new_d_Not(dbgi, value_node, mode);
1847 case EXPR_UNARY_NOT: {
1848 ir_node *value_node = expression_to_firm(value);
1849 ir_mode *mode = get_ir_mode(type);
1850 if(get_irn_mode(value_node) != mode_b) {
1851 value_node = create_conv(dbgi, value_node, mode_b);
1853 value_node = new_d_Not(dbgi, value_node, mode_b);
1854 if(mode != mode_b) {
1855 value_node = create_conv(dbgi, value_node, mode);
1859 case EXPR_UNARY_DEREFERENCE: {
1860 ir_node *value_node = expression_to_firm(value);
1861 type_t *value_type = skip_typeref(value->base.type);
1862 assert(is_type_pointer(value_type));
1863 type_t *points_to = value_type->pointer.points_to;
1864 return deref_address(points_to, value_node, dbgi);
1866 case EXPR_UNARY_POSTFIX_INCREMENT:
1867 case EXPR_UNARY_POSTFIX_DECREMENT:
1868 case EXPR_UNARY_PREFIX_INCREMENT:
1869 case EXPR_UNARY_PREFIX_DECREMENT:
1870 return create_incdec(expression);
1871 case EXPR_UNARY_CAST: {
1872 ir_node *value_node = expression_to_firm(value);
1873 if(is_type_scalar(type)) {
1874 ir_mode *mode = get_ir_mode(type);
1875 ir_node *node = create_conv(dbgi, value_node, mode);
1876 node = do_strict_conv(dbgi, node);
1879 /* make sure firm type is constructed */
1880 (void) get_ir_type(type);
1884 case EXPR_UNARY_CAST_IMPLICIT: {
1885 ir_node *value_node = expression_to_firm(value);
1886 if(is_type_scalar(type)) {
1887 ir_mode *mode = get_ir_mode(type);
1888 return create_conv(dbgi, value_node, mode);
1893 case EXPR_UNARY_ASSUME:
1894 if(firm_opt.confirm)
1895 return handle_assume(dbgi, value);
1898 case EXPR_UNARY_BITFIELD_EXTRACT:
1899 return bitfield_extract_to_firm(expression);
1904 panic("invalid UNEXPR type found");
1907 static ir_node *produce_condition_result(const expression_t *expression,
1910 ir_mode *mode = get_ir_mode(expression->base.type);
1911 ir_node *cur_block = get_cur_block();
1913 ir_node *one_block = new_immBlock();
1914 ir_node *one = new_Const(mode, get_mode_one(mode));
1915 ir_node *jmp_one = new_d_Jmp(dbgi);
1917 ir_node *zero_block = new_immBlock();
1918 ir_node *zero = new_Const(mode, get_mode_null(mode));
1919 ir_node *jmp_zero = new_d_Jmp(dbgi);
1921 set_cur_block(cur_block);
1922 create_condition_evaluation(expression, one_block, zero_block);
1923 mature_immBlock(one_block);
1924 mature_immBlock(zero_block);
1926 ir_node *common_block = new_immBlock();
1927 add_immBlock_pred(common_block, jmp_one);
1928 add_immBlock_pred(common_block, jmp_zero);
1929 mature_immBlock(common_block);
1931 ir_node *in[2] = { one, zero };
1932 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1937 static ir_node *create_lazy_op(const binary_expression_t *expression)
1939 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1940 type_t *type = expression->base.type;
1941 ir_mode *mode = get_ir_mode(type);
1943 if(is_constant_expression(expression->left)) {
1944 long val = fold_constant(expression->left);
1945 expression_kind_t ekind = expression->base.kind;
1946 if((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
1947 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1948 return expression_to_firm(expression->right);
1950 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
1951 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
1952 return new_Const(mode, get_mode_one(mode));
1956 return produce_condition_result((const expression_t*) expression, dbgi);
1959 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1960 ir_node *right, ir_mode *mode);
1962 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1963 create_arithmetic_func func)
1965 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1966 ir_node *left = expression_to_firm(expression->left);
1967 ir_node *right = expression_to_firm(expression->right);
1968 type_t *type = expression->right->base.type;
1969 /* be careful with the modes, because in arithmetic assign nodes only
1970 * the right operand has the mode of the arithmetic already */
1971 ir_mode *mode = get_ir_mode(type);
1972 left = create_conv(dbgi, left, mode);
1973 ir_node *res = func(dbgi, left, right, mode);
1978 static ir_node *pointer_arithmetic(ir_node *const pointer,
1981 dbg_info *const dbgi,
1982 const create_arithmetic_func func)
1984 pointer_type_t *const pointer_type = &type->pointer;
1985 type_t *const points_to = pointer_type->points_to;
1986 const unsigned elem_size = get_type_size_const(points_to);
1988 assert(elem_size >= 1);
1989 if (elem_size > 1) {
1990 integer = create_conv(dbgi, integer, mode_int);
1991 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1992 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
1996 ir_mode *const mode = get_ir_mode(type);
1997 return func(dbgi, pointer, integer, mode);
2000 static ir_node *create_arithmetic_assign_binop(
2001 const binary_expression_t *expression, create_arithmetic_func func)
2003 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2004 type_t *const type = skip_typeref(expression->base.type);
2007 if (is_type_pointer(type)) {
2008 ir_node *const pointer = expression_to_firm(expression->left);
2009 ir_node * integer = expression_to_firm(expression->right);
2010 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
2012 value = create_arithmetic_binop(expression, func);
2015 ir_mode *const mode = get_ir_mode(type);
2016 value = create_conv(dbgi, value, mode);
2017 set_value_for_expression(expression->left, value);
2022 static ir_node *create_add(const binary_expression_t *expression)
2024 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2025 ir_node *left = expression_to_firm(expression->left);
2026 ir_node *right = expression_to_firm(expression->right);
2027 type_t *type = expression->base.type;
2029 expression_t *expr_left = expression->left;
2030 expression_t *expr_right = expression->right;
2031 type_t *type_left = skip_typeref(expr_left->base.type);
2032 type_t *type_right = skip_typeref(expr_right->base.type);
2034 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2035 ir_mode *const mode = get_ir_mode(type);
2036 return new_d_Add(dbgi, left, right, mode);
2039 if (is_type_pointer(type_left)) {
2040 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
2042 assert(is_type_pointer(type_right));
2043 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
2047 static ir_node *create_sub(const binary_expression_t *expression)
2049 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2050 expression_t *const expr_left = expression->left;
2051 expression_t *const expr_right = expression->right;
2052 ir_node *const left = expression_to_firm(expr_left);
2053 ir_node *const right = expression_to_firm(expr_right);
2054 type_t *const type = expression->base.type;
2055 type_t *const type_left = skip_typeref(expr_left->base.type);
2056 type_t *const type_right = skip_typeref(expr_right->base.type);
2058 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2059 ir_mode *const mode = get_ir_mode(type);
2060 return new_d_Sub(dbgi, left, right, mode);
2061 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2062 const pointer_type_t *const ptr_type = &type_left->pointer;
2064 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2065 ir_mode *const mode = get_ir_mode(type);
2066 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2067 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2068 ir_node *const no_mem = new_NoMem();
2069 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size, mode,
2070 op_pin_state_floats);
2071 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2074 assert(is_type_pointer(type_left));
2075 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
2078 static ir_node *create_shift(const binary_expression_t *expression)
2080 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2081 ir_node *left = expression_to_firm(expression->left);
2082 ir_node *right = expression_to_firm(expression->right);
2083 type_t *type = expression->base.type;
2084 ir_mode *mode = get_ir_mode(type);
2086 /* firm always wants the shift count to be unsigned */
2087 right = create_conv(dbgi, right, mode_uint);
2091 switch(expression->base.kind) {
2092 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2093 case EXPR_BINARY_SHIFTLEFT:
2094 res = new_d_Shl(dbgi, left, right, mode);
2096 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2097 case EXPR_BINARY_SHIFTRIGHT: {
2098 expression_t *expr_left = expression->left;
2099 type_t *type_left = skip_typeref(expr_left->base.type);
2101 if(is_type_signed(type_left)) {
2102 res = new_d_Shrs(dbgi, left, right, mode);
2104 res = new_d_Shr(dbgi, left, right, mode);
2109 panic("create shift op called for non-shift op");
2116 static ir_node *create_divmod(const binary_expression_t *expression)
2118 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2119 ir_node *left = expression_to_firm(expression->left);
2120 ir_node *right = expression_to_firm(expression->right);
2121 ir_node *pin = new_Pin(new_NoMem());
2122 /* be careful with the modes, because in arithmetic assign nodes only
2123 * the right operand has the mode of the arithmetic already */
2124 type_t *type = expression->right->base.type;
2125 ir_mode *mode = get_ir_mode(type);
2126 left = create_conv(dbgi, left, mode);
2130 switch (expression->base.kind) {
2131 case EXPR_BINARY_DIV:
2132 case EXPR_BINARY_DIV_ASSIGN:
2133 if(mode_is_float(mode)) {
2134 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2135 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2137 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2138 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2142 case EXPR_BINARY_MOD:
2143 case EXPR_BINARY_MOD_ASSIGN:
2144 assert(!mode_is_float(mode));
2145 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
2146 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2149 default: panic("unexpected binary expression type in create_divmod()");
2155 static ir_node *create_arithmetic_assign_divmod(
2156 const binary_expression_t *expression)
2158 ir_node * value = create_divmod(expression);
2159 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2160 type_t *const type = expression->base.type;
2161 ir_mode *const mode = get_ir_mode(type);
2163 assert(type->kind != TYPE_POINTER);
2165 value = create_conv(dbgi, value, mode);
2166 set_value_for_expression(expression->left, value);
2171 static ir_node *create_arithmetic_assign_shift(
2172 const binary_expression_t *expression)
2174 ir_node * value = create_shift(expression);
2175 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2176 type_t *const type = expression->base.type;
2177 ir_mode *const mode = get_ir_mode(type);
2179 value = create_conv(dbgi, value, mode);
2180 set_value_for_expression(expression->left, value);
2185 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2187 expression_kind_t kind = expression->base.kind;
2190 case EXPR_BINARY_EQUAL:
2191 case EXPR_BINARY_NOTEQUAL:
2192 case EXPR_BINARY_LESS:
2193 case EXPR_BINARY_LESSEQUAL:
2194 case EXPR_BINARY_GREATER:
2195 case EXPR_BINARY_GREATEREQUAL:
2196 case EXPR_BINARY_ISGREATER:
2197 case EXPR_BINARY_ISGREATEREQUAL:
2198 case EXPR_BINARY_ISLESS:
2199 case EXPR_BINARY_ISLESSEQUAL:
2200 case EXPR_BINARY_ISLESSGREATER:
2201 case EXPR_BINARY_ISUNORDERED: {
2202 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2203 ir_node *left = expression_to_firm(expression->left);
2204 ir_node *right = expression_to_firm(expression->right);
2205 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2206 long pnc = get_pnc(kind, expression->left->base.type);
2207 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2210 case EXPR_BINARY_ASSIGN: {
2211 ir_node *right = expression_to_firm(expression->right);
2212 set_value_for_expression(expression->left, right);
2216 case EXPR_BINARY_ADD:
2217 return create_add(expression);
2218 case EXPR_BINARY_SUB:
2219 return create_sub(expression);
2220 case EXPR_BINARY_MUL:
2221 return create_arithmetic_binop(expression, new_d_Mul);
2222 case EXPR_BINARY_BITWISE_AND:
2223 return create_arithmetic_binop(expression, new_d_And);
2224 case EXPR_BINARY_BITWISE_OR:
2225 return create_arithmetic_binop(expression, new_d_Or);
2226 case EXPR_BINARY_BITWISE_XOR:
2227 return create_arithmetic_binop(expression, new_d_Eor);
2228 case EXPR_BINARY_SHIFTLEFT:
2229 case EXPR_BINARY_SHIFTRIGHT:
2230 return create_shift(expression);
2231 case EXPR_BINARY_DIV:
2232 case EXPR_BINARY_MOD:
2233 return create_divmod(expression);
2234 case EXPR_BINARY_LOGICAL_AND:
2235 case EXPR_BINARY_LOGICAL_OR:
2236 return create_lazy_op(expression);
2237 case EXPR_BINARY_COMMA:
2238 expression_to_firm(expression->left);
2239 return expression_to_firm(expression->right);
2240 case EXPR_BINARY_ADD_ASSIGN:
2241 return create_arithmetic_assign_binop(expression, new_d_Add);
2242 case EXPR_BINARY_SUB_ASSIGN:
2243 return create_arithmetic_assign_binop(expression, new_d_Sub);
2244 case EXPR_BINARY_MUL_ASSIGN:
2245 return create_arithmetic_assign_binop(expression, new_d_Mul);
2246 case EXPR_BINARY_MOD_ASSIGN:
2247 case EXPR_BINARY_DIV_ASSIGN:
2248 return create_arithmetic_assign_divmod(expression);
2249 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2250 return create_arithmetic_assign_binop(expression, new_d_And);
2251 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2252 return create_arithmetic_assign_binop(expression, new_d_Or);
2253 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2254 return create_arithmetic_assign_binop(expression, new_d_Eor);
2255 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2256 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2257 return create_arithmetic_assign_shift(expression);
2258 case EXPR_BINARY_BUILTIN_EXPECT:
2259 return expression_to_firm(expression->left);
2261 panic("TODO binexpr type");
2265 static ir_node *array_access_addr(const array_access_expression_t *expression)
2267 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2268 ir_node *base_addr = expression_to_firm(expression->array_ref);
2269 ir_node *offset = expression_to_firm(expression->index);
2271 /* Matze: it would be better to force mode to mode_uint as this creates more
2272 * opportunities for CSE. Unforunately we still have some optimisations that
2273 * are too conservative in the presence of convs. So we better go with the
2274 * mode of offset and avoid the conv */
2275 ir_mode *mode = get_irn_mode(offset);
2276 offset = create_conv(dbgi, offset, mode);
2278 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2279 assert(is_type_pointer(ref_type));
2280 pointer_type_t *pointer_type = &ref_type->pointer;
2282 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2283 elem_size_const = create_conv(dbgi, elem_size_const, mode);
2284 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2286 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2291 static ir_node *array_access_to_firm(
2292 const array_access_expression_t *expression)
2294 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2295 ir_node *addr = array_access_addr(expression);
2296 type_t *type = revert_automatic_type_conversion(
2297 (const expression_t*) expression);
2298 type = skip_typeref(type);
2300 return deref_address(type, addr, dbgi);
2303 static long get_offsetof_offset(const offsetof_expression_t *expression)
2305 type_t *orig_type = expression->type;
2308 designator_t *designator = expression->designator;
2309 for( ; designator != NULL; designator = designator->next) {
2310 type_t *type = skip_typeref(orig_type);
2311 /* be sure the type is constructed */
2312 (void) get_ir_type(type);
2314 if(designator->symbol != NULL) {
2315 assert(is_type_compound(type));
2316 symbol_t *symbol = designator->symbol;
2318 declaration_t *declaration = type->compound.declaration;
2319 declaration_t *iter = declaration->scope.declarations;
2320 for( ; iter != NULL; iter = iter->next) {
2321 if(iter->symbol == symbol) {
2325 assert(iter != NULL);
2327 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2328 offset += get_entity_offset(iter->v.entity);
2330 orig_type = iter->type;
2332 expression_t *array_index = designator->array_index;
2333 assert(designator->array_index != NULL);
2334 assert(is_type_array(type));
2335 assert(is_type_valid(array_index->base.type));
2337 long index = fold_constant(array_index);
2338 ir_type *arr_type = get_ir_type(type);
2339 ir_type *elem_type = get_array_element_type(arr_type);
2340 long elem_size = get_type_size_bytes(elem_type);
2342 offset += index * elem_size;
2344 orig_type = type->array.element_type;
2351 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2353 ir_mode *mode = get_ir_mode(expression->base.type);
2354 long offset = get_offsetof_offset(expression);
2355 tarval *tv = new_tarval_from_long(offset, mode);
2356 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2358 return new_d_Const(dbgi, mode, tv);
2361 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2362 ir_entity *entity, type_t *type);
2364 static ir_node *compound_literal_to_firm(
2365 const compound_literal_expression_t *expression)
2367 type_t *type = expression->type;
2369 /* create an entity on the stack */
2370 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2372 ident *const id = id_unique("CompLit.%u");
2373 ir_type *const irtype = get_ir_type(type);
2374 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2375 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2376 set_entity_ld_ident(entity, id);
2378 set_entity_variability(entity, variability_uninitialized);
2380 /* create initialisation code */
2381 initializer_t *initializer = expression->initializer;
2382 create_local_initializer(initializer, dbgi, entity, type);
2384 /* create a sel for the compound literal address */
2385 ir_node *frame = get_local_frame(entity);
2386 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2391 * Transform a sizeof expression into Firm code.
2393 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2395 type_t *type = expression->type;
2397 type = expression->tp_expression->base.type;
2398 assert(type != NULL);
2401 type = skip_typeref(type);
2402 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2403 if(is_type_array(type) && type->array.is_vla
2404 && expression->tp_expression != NULL) {
2405 expression_to_firm(expression->tp_expression);
2408 return get_type_size(type);
2412 * Transform an alignof expression into Firm code.
2414 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2416 type_t *type = expression->type;
2418 /* beware: if expression is a variable reference, return the
2419 alignment of the variable. */
2420 const expression_t *tp_expression = expression->tp_expression;
2421 const declaration_t *declaration = expr_is_variable(tp_expression);
2422 if (declaration != NULL) {
2423 /* TODO: get the alignment of this variable. */
2425 type = tp_expression->base.type;
2426 assert(type != NULL);
2429 ir_mode *const mode = get_ir_mode(expression->base.type);
2430 symconst_symbol sym;
2431 sym.type_p = get_ir_type(type);
2432 return new_SymConst(mode, sym, symconst_type_align);
2435 static void init_ir_types(void);
2436 long fold_constant(const expression_t *expression)
2440 assert(is_constant_expression(expression));
2442 ir_graph *old_current_ir_graph = current_ir_graph;
2443 if(current_ir_graph == NULL) {
2444 current_ir_graph = get_const_code_irg();
2447 ir_node *cnst = expression_to_firm(expression);
2448 current_ir_graph = old_current_ir_graph;
2450 if(!is_Const(cnst)) {
2451 panic("couldn't fold constant\n");
2454 tarval *tv = get_Const_tarval(cnst);
2455 if(!tarval_is_long(tv)) {
2456 panic("result of constant folding is not integer\n");
2459 return get_tarval_long(tv);
2462 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2464 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2466 /* first try to fold a constant condition */
2467 if(is_constant_expression(expression->condition)) {
2468 long val = fold_constant(expression->condition);
2470 return expression_to_firm(expression->true_expression);
2472 return expression_to_firm(expression->false_expression);
2476 ir_node *cur_block = get_cur_block();
2478 /* create the true block */
2479 ir_node *true_block = new_immBlock();
2481 ir_node *true_val = expression_to_firm(expression->true_expression);
2482 ir_node *true_jmp = new_Jmp();
2484 /* create the false block */
2485 ir_node *false_block = new_immBlock();
2487 ir_node *false_val = expression_to_firm(expression->false_expression);
2488 ir_node *false_jmp = new_Jmp();
2490 /* create the condition evaluation */
2491 set_cur_block(cur_block);
2492 create_condition_evaluation(expression->condition, true_block, false_block);
2493 mature_immBlock(true_block);
2494 mature_immBlock(false_block);
2496 /* create the common block */
2497 ir_node *common_block = new_immBlock();
2498 add_immBlock_pred(common_block, true_jmp);
2499 add_immBlock_pred(common_block, false_jmp);
2500 mature_immBlock(common_block);
2502 /* TODO improve static semantics, so either both or no values are NULL */
2503 if (true_val == NULL || false_val == NULL)
2506 ir_node *in[2] = { true_val, false_val };
2507 ir_mode *mode = get_irn_mode(true_val);
2508 assert(get_irn_mode(false_val) == mode);
2509 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2514 static ir_node *select_addr(const select_expression_t *expression)
2516 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2518 ir_node *compound_addr = expression_to_firm(expression->compound);
2520 /* make sure the type is constructed */
2521 type_t *type = skip_typeref(expression->compound->base.type);
2522 if (is_type_pointer(type)) {
2523 type = type->pointer.points_to;
2525 (void) get_ir_type(type);
2527 declaration_t *entry = expression->compound_entry;
2528 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2529 ir_entity *entity = entry->v.entity;
2531 assert(entity != NULL);
2533 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2538 static ir_node *select_to_firm(const select_expression_t *expression)
2540 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2541 ir_node *addr = select_addr(expression);
2542 type_t *type = revert_automatic_type_conversion(
2543 (const expression_t*) expression);
2544 type = skip_typeref(type);
2546 return deref_address(type, addr, dbgi);
2549 /* Values returned by __builtin_classify_type. */
2550 typedef enum gcc_type_class
2556 enumeral_type_class,
2559 reference_type_class,
2563 function_type_class,
2574 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2576 const type_t *const type = skip_typeref(expr->type_expression->base.type);
2582 const atomic_type_t *const atomic_type = &type->atomic;
2583 switch (atomic_type->akind) {
2584 /* should not be reached */
2585 case ATOMIC_TYPE_INVALID:
2589 /* gcc cannot do that */
2590 case ATOMIC_TYPE_VOID:
2591 tc = void_type_class;
2594 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2595 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2596 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2597 case ATOMIC_TYPE_SHORT:
2598 case ATOMIC_TYPE_USHORT:
2599 case ATOMIC_TYPE_INT:
2600 case ATOMIC_TYPE_UINT:
2601 case ATOMIC_TYPE_LONG:
2602 case ATOMIC_TYPE_ULONG:
2603 case ATOMIC_TYPE_LONGLONG:
2604 case ATOMIC_TYPE_ULONGLONG:
2605 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2606 tc = integer_type_class;
2609 case ATOMIC_TYPE_FLOAT:
2610 case ATOMIC_TYPE_DOUBLE:
2611 case ATOMIC_TYPE_LONG_DOUBLE:
2612 tc = real_type_class;
2615 panic("Unexpected atomic type in classify_type_to_firm().");
2618 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
2619 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
2620 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
2621 case TYPE_ARRAY: /* gcc handles this as pointer */
2622 case TYPE_FUNCTION: /* gcc handles this as pointer */
2623 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
2624 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
2625 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
2627 /* gcc handles this as integer */
2628 case TYPE_ENUM: tc = integer_type_class; goto make_const;
2631 /* typedef/typeof should be skipped already */
2638 panic("unexpected TYPE classify_type_to_firm().");
2641 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2642 ir_mode *const mode = mode_int;
2643 tarval *const tv = new_tarval_from_long(tc, mode);
2644 return new_d_Const(dbgi, mode, tv);
2647 static ir_node *function_name_to_firm(
2648 const funcname_expression_t *const expr)
2650 switch(expr->kind) {
2651 case FUNCNAME_FUNCTION:
2652 case FUNCNAME_PRETTY_FUNCTION:
2653 case FUNCNAME_FUNCDNAME:
2654 if (current_function_name == NULL) {
2655 const source_position_t *const src_pos = &expr->base.source_position;
2656 const char *const name = current_function_decl->symbol->string;
2657 const string_t string = { name, strlen(name) + 1 };
2658 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
2660 return current_function_name;
2661 case FUNCNAME_FUNCSIG:
2662 if (current_funcsig == NULL) {
2663 const source_position_t *const src_pos = &expr->base.source_position;
2664 ir_entity *ent = get_irg_entity(current_ir_graph);
2665 const char *const name = get_entity_ld_name(ent);
2666 const string_t string = { name, strlen(name) + 1 };
2667 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
2669 return current_funcsig;
2671 panic("Unsupported function name");
2674 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2676 statement_t *statement = expr->statement;
2678 assert(statement->kind == STATEMENT_COMPOUND);
2679 return compound_statement_to_firm(&statement->compound);
2682 static ir_node *va_start_expression_to_firm(
2683 const va_start_expression_t *const expr)
2685 ir_type *const method_type = get_ir_type(current_function_decl->type);
2686 int const n = get_method_n_params(method_type) - 1;
2687 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2688 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2689 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2690 ir_node *const no_mem = new_NoMem();
2691 ir_node *const arg_sel =
2692 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2694 ir_node *const cnst = get_type_size(expr->parameter->type);
2695 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2696 set_value_for_expression(expr->ap, add);
2701 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2703 type_t *const type = expr->base.type;
2704 ir_node *const ap = expression_to_firm(expr->ap);
2705 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2706 ir_node *const res = deref_address(type, ap, dbgi);
2708 ir_node *const cnst = get_type_size(expr->base.type);
2709 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2710 set_value_for_expression(expr->ap, add);
2715 static ir_node *dereference_addr(const unary_expression_t *const expression)
2717 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2718 return expression_to_firm(expression->value);
2721 static ir_node *expression_to_addr(const expression_t *expression)
2723 switch(expression->kind) {
2724 case EXPR_REFERENCE:
2725 return reference_addr(&expression->reference);
2726 case EXPR_ARRAY_ACCESS:
2727 return array_access_addr(&expression->array_access);
2729 return select_addr(&expression->select);
2731 return call_expression_to_firm(&expression->call);
2732 case EXPR_UNARY_DEREFERENCE: {
2733 return dereference_addr(&expression->unary);
2738 panic("trying to get address of non-lvalue");
2741 static ir_node *builtin_constant_to_firm(
2742 const builtin_constant_expression_t *expression)
2744 ir_mode *mode = get_ir_mode(expression->base.type);
2747 if (is_constant_expression(expression->value)) {
2752 return new_Const_long(mode, v);
2755 static ir_node *builtin_prefetch_to_firm(
2756 const builtin_prefetch_expression_t *expression)
2758 ir_node *adr = expression_to_firm(expression->adr);
2759 /* no Firm support for prefetch yet */
2764 static ir_node *_expression_to_firm(const expression_t *expression)
2766 switch(expression->kind) {
2767 case EXPR_CHARACTER_CONSTANT:
2768 return character_constant_to_firm(&expression->conste);
2769 case EXPR_WIDE_CHARACTER_CONSTANT:
2770 return wide_character_constant_to_firm(&expression->conste);
2772 return const_to_firm(&expression->conste);
2773 case EXPR_STRING_LITERAL:
2774 return string_literal_to_firm(&expression->string);
2775 case EXPR_WIDE_STRING_LITERAL:
2776 return wide_string_literal_to_firm(&expression->wide_string);
2777 case EXPR_REFERENCE:
2778 return reference_expression_to_firm(&expression->reference);
2780 return call_expression_to_firm(&expression->call);
2782 return unary_expression_to_firm(&expression->unary);
2784 return binary_expression_to_firm(&expression->binary);
2785 case EXPR_ARRAY_ACCESS:
2786 return array_access_to_firm(&expression->array_access);
2788 return sizeof_to_firm(&expression->typeprop);
2790 return alignof_to_firm(&expression->typeprop);
2791 case EXPR_CONDITIONAL:
2792 return conditional_to_firm(&expression->conditional);
2794 return select_to_firm(&expression->select);
2795 case EXPR_CLASSIFY_TYPE:
2796 return classify_type_to_firm(&expression->classify_type);
2798 return function_name_to_firm(&expression->funcname);
2799 case EXPR_STATEMENT:
2800 return statement_expression_to_firm(&expression->statement);
2802 return va_start_expression_to_firm(&expression->va_starte);
2804 return va_arg_expression_to_firm(&expression->va_arge);
2805 case EXPR_BUILTIN_SYMBOL:
2806 panic("unimplemented expression found");
2807 case EXPR_BUILTIN_CONSTANT_P:
2808 return builtin_constant_to_firm(&expression->builtin_constant);
2809 case EXPR_BUILTIN_PREFETCH:
2810 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2812 return offsetof_to_firm(&expression->offsetofe);
2813 case EXPR_COMPOUND_LITERAL:
2814 return compound_literal_to_firm(&expression->compound_literal);
2820 panic("invalid expression found");
2823 static ir_node *expression_to_firm(const expression_t *expression)
2825 ir_node *res = _expression_to_firm(expression);
2827 if(res != NULL && get_irn_mode(res) == mode_b) {
2828 ir_mode *mode = get_ir_mode(expression->base.type);
2830 if(is_Const_null(res)) {
2831 return new_Const_long(mode, 0);
2833 assert(is_Const_one(res));
2834 return new_Const_long(mode, 1);
2838 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2839 return produce_condition_result(expression, dbgi);
2845 static ir_node *expression_to_modeb(const expression_t *expression)
2847 ir_node *res = _expression_to_firm(expression);
2848 res = create_conv(NULL, res, mode_b);
2854 * create a short-circuit expression evaluation that tries to construct
2855 * efficient control flow structures for &&, || and ! expressions
2857 static void create_condition_evaluation(const expression_t *expression,
2858 ir_node *true_block,
2859 ir_node *false_block)
2861 switch(expression->kind) {
2862 case EXPR_UNARY_NOT: {
2863 const unary_expression_t *unary_expression = &expression->unary;
2864 create_condition_evaluation(unary_expression->value, false_block,
2868 case EXPR_BINARY_LOGICAL_AND: {
2869 const binary_expression_t *binary_expression = &expression->binary;
2871 ir_node *cur_block = get_cur_block();
2872 ir_node *extra_block = new_immBlock();
2873 set_cur_block(cur_block);
2874 create_condition_evaluation(binary_expression->left, extra_block,
2876 mature_immBlock(extra_block);
2877 set_cur_block(extra_block);
2878 create_condition_evaluation(binary_expression->right, true_block,
2882 case EXPR_BINARY_LOGICAL_OR: {
2883 const binary_expression_t *binary_expression = &expression->binary;
2885 ir_node *cur_block = get_cur_block();
2886 ir_node *extra_block = new_immBlock();
2887 set_cur_block(cur_block);
2888 create_condition_evaluation(binary_expression->left, true_block,
2890 mature_immBlock(extra_block);
2891 set_cur_block(extra_block);
2892 create_condition_evaluation(binary_expression->right, true_block,
2900 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2901 ir_node *condition = expression_to_modeb(expression);
2902 ir_node *cond = new_d_Cond(dbgi, condition);
2903 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2904 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2906 /* set branch prediction info based on __builtin_expect */
2907 if(expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
2908 long cnst = fold_constant(expression->binary.right);
2909 cond_jmp_predicate pred;
2912 pred = COND_JMP_PRED_FALSE;
2914 pred = COND_JMP_PRED_TRUE;
2916 set_Cond_jmp_pred(cond, pred);
2919 add_immBlock_pred(true_block, true_proj);
2920 add_immBlock_pred(false_block, false_proj);
2922 set_cur_block(NULL);
2927 static void create_declaration_entity(declaration_t *declaration,
2928 declaration_kind_t declaration_kind,
2929 ir_type *parent_type)
2931 ident *const id = new_id_from_str(declaration->symbol->string);
2932 type_t *const type = skip_typeref(declaration->type);
2933 ir_type *const irtype = get_ir_type(type);
2934 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2935 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2937 declaration->declaration_kind = (unsigned char) declaration_kind;
2938 declaration->v.entity = entity;
2939 set_entity_variability(entity, variability_uninitialized);
2940 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
2941 if(parent_type == get_tls_type())
2942 set_entity_allocation(entity, allocation_automatic);
2943 else if(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
2944 set_entity_allocation(entity, allocation_static);
2946 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
2947 set_entity_volatility(entity, volatility_is_volatile);
2952 typedef struct type_path_entry_t type_path_entry_t;
2953 struct type_path_entry_t {
2955 ir_initializer_t *initializer;
2957 declaration_t *compound_entry;
2960 typedef struct type_path_t type_path_t;
2961 struct type_path_t {
2962 type_path_entry_t *path;
2967 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
2969 size_t len = ARR_LEN(path->path);
2971 for(size_t i = 0; i < len; ++i) {
2972 const type_path_entry_t *entry = & path->path[i];
2974 type_t *type = skip_typeref(entry->type);
2975 if(is_type_compound(type)) {
2976 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
2977 } else if(is_type_array(type)) {
2978 fprintf(stderr, "[%zd]", entry->index);
2980 fprintf(stderr, "-INVALID-");
2983 fprintf(stderr, " (");
2984 print_type(path->top_type);
2985 fprintf(stderr, ")");
2988 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2990 size_t len = ARR_LEN(path->path);
2992 return & path->path[len-1];
2995 static type_path_entry_t *append_to_type_path(type_path_t *path)
2997 size_t len = ARR_LEN(path->path);
2998 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3000 type_path_entry_t *result = & path->path[len];
3001 memset(result, 0, sizeof(result[0]));
3005 static size_t get_compound_size(const compound_type_t *type)
3007 declaration_t *declaration = type->declaration;
3008 declaration_t *member = declaration->scope.declarations;
3010 for( ; member != NULL; member = member->next) {
3013 /* TODO: cache results? */
3018 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3020 type_t *orig_top_type = path->top_type;
3021 type_t *top_type = skip_typeref(orig_top_type);
3023 assert(is_type_compound(top_type) || is_type_array(top_type));
3025 if(ARR_LEN(path->path) == 0) {
3028 type_path_entry_t *top = get_type_path_top(path);
3029 ir_initializer_t *initializer = top->initializer;
3030 return get_initializer_compound_value(initializer, top->index);
3034 static void descend_into_subtype(type_path_t *path)
3036 type_t *orig_top_type = path->top_type;
3037 type_t *top_type = skip_typeref(orig_top_type);
3039 assert(is_type_compound(top_type) || is_type_array(top_type));
3041 ir_initializer_t *initializer = get_initializer_entry(path);
3043 type_path_entry_t *top = append_to_type_path(path);
3044 top->type = top_type;
3048 if(is_type_compound(top_type)) {
3049 declaration_t *declaration = top_type->compound.declaration;
3050 declaration_t *entry = declaration->scope.declarations;
3052 top->compound_entry = entry;
3054 len = get_compound_size(&top_type->compound);
3056 path->top_type = entry->type;
3058 assert(is_type_array(top_type));
3059 assert(top_type->array.size > 0);
3062 path->top_type = top_type->array.element_type;
3063 len = top_type->array.size;
3065 if(initializer == NULL
3066 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3067 initializer = create_initializer_compound(len);
3068 /* we have to set the entry at the 2nd latest path entry... */
3069 size_t path_len = ARR_LEN(path->path);
3070 assert(path_len >= 1);
3072 type_path_entry_t *entry = & path->path[path_len-2];
3073 ir_initializer_t *tinitializer = entry->initializer;
3074 set_initializer_compound_value(tinitializer, entry->index,
3078 top->initializer = initializer;
3081 static void ascend_from_subtype(type_path_t *path)
3083 type_path_entry_t *top = get_type_path_top(path);
3085 path->top_type = top->type;
3087 size_t len = ARR_LEN(path->path);
3088 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3091 static void walk_designator(type_path_t *path, const designator_t *designator)
3093 /* designators start at current object type */
3094 ARR_RESIZE(type_path_entry_t, path->path, 1);
3096 for( ; designator != NULL; designator = designator->next) {
3097 type_path_entry_t *top = get_type_path_top(path);
3098 type_t *orig_type = top->type;
3099 type_t *type = skip_typeref(orig_type);
3101 if(designator->symbol != NULL) {
3102 assert(is_type_compound(type));
3104 symbol_t *symbol = designator->symbol;
3106 declaration_t *declaration = type->compound.declaration;
3107 declaration_t *iter = declaration->scope.declarations;
3108 for( ; iter != NULL; iter = iter->next, ++index) {
3109 if(iter->symbol == symbol) {
3113 assert(iter != NULL);
3115 top->type = orig_type;
3116 top->compound_entry = iter;
3118 orig_type = iter->type;
3120 expression_t *array_index = designator->array_index;
3121 assert(designator->array_index != NULL);
3122 assert(is_type_array(type));
3123 assert(is_type_valid(array_index->base.type));
3125 long index = fold_constant(array_index);
3128 if(type->array.size_constant == 1) {
3129 long array_size = type->array.size;
3130 assert(index < array_size);
3134 top->type = orig_type;
3135 top->index = (size_t) index;
3136 orig_type = type->array.element_type;
3138 path->top_type = orig_type;
3140 if(designator->next != NULL) {
3141 descend_into_subtype(path);
3145 path->invalid = false;
3148 static void advance_current_object(type_path_t *path)
3151 /* TODO: handle this... */
3152 panic("invalid initializer in ast2firm (excessive elements)");
3156 type_path_entry_t *top = get_type_path_top(path);
3158 type_t *type = skip_typeref(top->type);
3159 if(is_type_union(type)) {
3160 top->compound_entry = NULL;
3161 } else if(is_type_struct(type)) {
3162 declaration_t *entry = top->compound_entry;
3165 entry = entry->next;
3166 top->compound_entry = entry;
3168 path->top_type = entry->type;
3172 assert(is_type_array(type));
3175 if(!type->array.size_constant || top->index < type->array.size) {
3180 /* we're past the last member of the current sub-aggregate, try if we
3181 * can ascend in the type hierarchy and continue with another subobject */
3182 size_t len = ARR_LEN(path->path);
3185 ascend_from_subtype(path);
3186 advance_current_object(path);
3188 path->invalid = true;
3193 static ir_initializer_t *create_ir_initializer(
3194 const initializer_t *initializer, type_t *type);
3196 static ir_initializer_t *create_ir_initializer_value(
3197 const initializer_value_t *initializer)
3199 if (is_type_compound(initializer->value->base.type)) {
3200 panic("initializer creation for compounds not implemented yet");
3202 ir_node *value = expression_to_firm(initializer->value);
3203 return create_initializer_const(value);
3206 static ir_initializer_t *create_ir_initializer_list(
3207 const initializer_list_t *initializer, type_t *type)
3210 memset(&path, 0, sizeof(path));
3211 path.top_type = type;
3212 path.path = NEW_ARR_F(type_path_entry_t, 0);
3214 descend_into_subtype(&path);
3216 for(size_t i = 0; i < initializer->len; ++i) {
3217 const initializer_t *sub_initializer = initializer->initializers[i];
3219 if(sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3220 walk_designator(&path, sub_initializer->designator.designator);
3224 if(sub_initializer->kind == INITIALIZER_VALUE) {
3225 /* we might have to descend into types until we're at a scalar
3228 type_t *orig_top_type = path.top_type;
3229 type_t *top_type = skip_typeref(orig_top_type);
3231 if(is_type_scalar(top_type))
3233 descend_into_subtype(&path);
3237 ir_initializer_t *sub_irinitializer
3238 = create_ir_initializer(sub_initializer, path.top_type);
3240 size_t path_len = ARR_LEN(path.path);
3241 assert(path_len >= 1);
3242 type_path_entry_t *entry = & path.path[path_len-1];
3243 ir_initializer_t *tinitializer = entry->initializer;
3244 set_initializer_compound_value(tinitializer, entry->index,
3247 advance_current_object(&path);
3250 assert(ARR_LEN(path.path) >= 1);
3251 ir_initializer_t *result = path.path[0].initializer;
3252 DEL_ARR_F(path.path);
3257 static ir_initializer_t *create_ir_initializer_string(
3258 const initializer_string_t *initializer, type_t *type)
3260 type = skip_typeref(type);
3262 size_t string_len = initializer->string.size;
3263 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3264 size_t len = type->array.size;
3265 ir_initializer_t *irinitializer = create_initializer_compound(len);
3267 const char *string = initializer->string.begin;
3268 ir_mode *mode = get_type_mode(ir_type_const_char);
3270 for(size_t i = 0; i < len; ++i) {
3275 tarval *tv = new_tarval_from_long(c, mode);
3276 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3278 set_initializer_compound_value(irinitializer, i, char_initializer);
3281 return irinitializer;
3284 static ir_initializer_t *create_ir_initializer_wide_string(
3285 const initializer_wide_string_t *initializer, type_t *type)
3287 size_t string_len = initializer->string.size;
3288 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3289 size_t len = type->array.size;
3290 ir_initializer_t *irinitializer = create_initializer_compound(len);
3292 const wchar_rep_t *string = initializer->string.begin;
3293 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3295 for(size_t i = 0; i < len; ++i) {
3297 if(i < string_len) {
3300 tarval *tv = new_tarval_from_long(c, mode);
3301 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3303 set_initializer_compound_value(irinitializer, i, char_initializer);
3306 return irinitializer;
3309 static ir_initializer_t *create_ir_initializer(
3310 const initializer_t *initializer, type_t *type)
3312 switch(initializer->kind) {
3313 case INITIALIZER_STRING:
3314 return create_ir_initializer_string(&initializer->string, type);
3316 case INITIALIZER_WIDE_STRING:
3317 return create_ir_initializer_wide_string(&initializer->wide_string,
3320 case INITIALIZER_LIST:
3321 return create_ir_initializer_list(&initializer->list, type);
3323 case INITIALIZER_VALUE:
3324 return create_ir_initializer_value(&initializer->value);
3326 case INITIALIZER_DESIGNATOR:
3327 panic("unexpected designator initializer found");
3329 panic("unknown initializer");
3332 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
3335 if (is_atomic_type(type)) {
3336 ir_mode *mode = get_type_mode(type);
3337 tarval *zero = get_mode_null(mode);
3338 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3340 /* TODO: bitfields */
3341 ir_node *mem = get_store();
3342 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3343 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3346 assert(is_compound_type(type));
3349 if(is_Array_type(type)) {
3350 assert(has_array_upper_bound(type, 0));
3351 n_members = get_array_upper_bound_int(type, 0);
3353 n_members = get_compound_n_members(type);
3356 for(int i = 0; i < n_members; ++i) {
3359 if(is_Array_type(type)) {
3360 ir_entity *entity = get_array_element_entity(type);
3361 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3362 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3363 ir_node *in[1] = { cnst };
3364 irtype = get_array_element_type(type);
3365 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3367 ir_entity *member = get_compound_member(type, i);
3369 irtype = get_entity_type(member);
3370 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3373 create_dynamic_null_initializer(irtype, dbgi, addr);
3378 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3379 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3381 switch(get_initializer_kind(initializer)) {
3382 case IR_INITIALIZER_NULL: {
3383 create_dynamic_null_initializer(type, dbgi, base_addr);
3386 case IR_INITIALIZER_CONST: {
3387 ir_node *node = get_initializer_const_value(initializer);
3388 ir_mode *mode = get_irn_mode(node);
3389 assert(get_type_mode(type) == mode);
3391 /* TODO: bitfields... */
3392 ir_node *mem = get_store();
3393 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3394 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3398 case IR_INITIALIZER_TARVAL: {
3399 tarval *tv = get_initializer_tarval_value(initializer);
3400 ir_mode *mode = get_tarval_mode(tv);
3401 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3402 assert(get_type_mode(type) == mode);
3404 /* TODO: bitfields... */
3405 ir_node *mem = get_store();
3406 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3407 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3411 case IR_INITIALIZER_COMPOUND: {
3412 assert(is_compound_type(type));
3414 if(is_Array_type(type)) {
3415 assert(has_array_upper_bound(type, 0));
3416 n_members = get_array_upper_bound_int(type, 0);
3418 n_members = get_compound_n_members(type);
3421 if(get_initializer_compound_n_entries(initializer)
3422 != (unsigned) n_members)
3423 panic("initializer doesn't match compound type");
3425 for(int i = 0; i < n_members; ++i) {
3428 if(is_Array_type(type)) {
3429 ir_entity *entity = get_array_element_entity(type);
3430 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3431 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3432 ir_node *in[1] = { cnst };
3433 irtype = get_array_element_type(type);
3434 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3436 ir_entity *member = get_compound_member(type, i);
3438 irtype = get_entity_type(member);
3439 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3442 ir_initializer_t *sub_init
3443 = get_initializer_compound_value(initializer, i);
3445 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3451 panic("invalid IR_INITIALIZER found");
3454 static void create_dynamic_initializer(ir_initializer_t *initializer,
3455 dbg_info *dbgi, ir_entity *entity)
3457 ir_node *frame = get_local_frame(entity);
3458 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3459 ir_type *type = get_entity_type(entity);
3461 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3464 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3465 ir_entity *entity, type_t *type)
3467 ir_node *memory = get_store();
3468 ir_node *nomem = new_NoMem();
3469 ir_node *frame = get_irg_frame(current_ir_graph);
3470 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3472 if(initializer->kind == INITIALIZER_VALUE) {
3473 initializer_value_t *initializer_value = &initializer->value;
3475 ir_node *value = expression_to_firm(initializer_value->value);
3476 type = skip_typeref(type);
3477 assign_value(dbgi, addr, type, value);
3481 if(!is_constant_initializer(initializer)) {
3482 ir_initializer_t *irinitializer
3483 = create_ir_initializer(initializer, type);
3485 create_dynamic_initializer(irinitializer, dbgi, entity);
3489 /* create the ir_initializer */
3490 ir_graph *const old_current_ir_graph = current_ir_graph;
3491 current_ir_graph = get_const_code_irg();
3493 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3495 assert(current_ir_graph == get_const_code_irg());
3496 current_ir_graph = old_current_ir_graph;
3498 /* create a "template" entity which is copied to the entity on the stack */
3499 ident *const id = id_unique("initializer.%u");
3500 ir_type *const irtype = get_ir_type(type);
3501 ir_type *const global_type = get_glob_type();
3502 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3503 set_entity_ld_ident(init_entity, id);
3505 set_entity_variability(init_entity, variability_initialized);
3506 set_entity_visibility(init_entity, visibility_local);
3507 set_entity_allocation(init_entity, allocation_static);
3509 set_entity_initializer(init_entity, irinitializer);
3511 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3512 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3514 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3515 set_store(copyb_mem);
3518 static void create_initializer_local_variable_entity(declaration_t *declaration)
3520 initializer_t *initializer = declaration->init.initializer;
3521 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3522 ir_entity *entity = declaration->v.entity;
3523 type_t *type = declaration->type;
3524 create_local_initializer(initializer, dbgi, entity, type);
3527 static void create_declaration_initializer(declaration_t *declaration)
3529 initializer_t *initializer = declaration->init.initializer;
3530 if(initializer == NULL)
3533 declaration_kind_t declaration_kind
3534 = (declaration_kind_t) declaration->declaration_kind;
3535 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3536 create_initializer_local_variable_entity(declaration);
3540 if(initializer->kind == INITIALIZER_VALUE) {
3541 initializer_value_t *initializer_value = &initializer->value;
3543 = get_dbg_info(&declaration->source_position);
3545 ir_node *value = expression_to_firm(initializer_value->value);
3546 value = do_strict_conv(dbgi, value);
3548 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3549 set_value(declaration->v.value_number, value);
3551 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3553 ir_entity *entity = declaration->v.entity;
3555 set_entity_variability(entity, variability_initialized);
3556 set_atomic_ent_value(entity, value);
3559 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3560 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3562 ir_entity *entity = declaration->v.entity;
3563 ir_initializer_t *irinitializer
3564 = create_ir_initializer(initializer, declaration->type);
3566 set_entity_variability(entity, variability_initialized);
3567 set_entity_initializer(entity, irinitializer);
3571 static void create_variable_length_array(declaration_t *declaration)
3573 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3574 type_t *type = declaration->type;
3575 ir_node *mem = get_store();
3576 ir_type *el_type = get_ir_type(type->array.element_type);
3578 /* make sure size_node is calculated */
3579 get_type_size(type);
3580 ir_node *elems = type->array.size_node;
3581 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3583 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3584 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3587 /* initializers are not allowed for VLAs */
3588 assert(declaration->init.initializer == NULL);
3590 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3591 declaration->v.vla_base = addr;
3593 /* TODO: record VLA somewhere so we create the free node when we leave
3598 * Creates a Firm local variable from a declaration.
3600 static void create_local_variable(declaration_t *declaration)
3602 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3604 bool needs_entity = declaration->address_taken;
3605 type_t *type = skip_typeref(declaration->type);
3607 /* is it a variable length array? */
3608 if(is_type_array(type) && !type->array.size_constant) {
3609 create_variable_length_array(declaration);
3611 } else if(is_type_array(type) || is_type_compound(type)) {
3612 needs_entity = true;
3613 } else if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3614 needs_entity = true;
3618 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3619 create_declaration_entity(declaration,
3620 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3623 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3624 declaration->v.value_number = next_value_number_function;
3625 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3626 ++next_value_number_function;
3630 static void create_local_static_variable(declaration_t *declaration)
3632 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3634 type_t *const type = skip_typeref(declaration->type);
3635 ir_type *const global_type = get_glob_type();
3636 ir_type *const irtype = get_ir_type(type);
3637 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3639 size_t l = strlen(declaration->symbol->string);
3640 char buf[l + sizeof(".%u")];
3641 snprintf(buf, sizeof(buf), "%s.%%u", declaration->symbol->string);
3642 ident *const id = id_unique(buf);
3644 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3646 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3647 set_entity_volatility(entity, volatility_is_volatile);
3650 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3651 declaration->v.entity = entity;
3652 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3653 set_entity_variability(entity, variability_uninitialized);
3654 set_entity_visibility(entity, visibility_local);
3655 set_entity_allocation(entity, allocation_static);
3657 ir_graph *const old_current_ir_graph = current_ir_graph;
3658 current_ir_graph = get_const_code_irg();
3660 create_declaration_initializer(declaration);
3662 assert(current_ir_graph == get_const_code_irg());
3663 current_ir_graph = old_current_ir_graph;
3668 static void return_statement_to_firm(return_statement_t *statement)
3670 if(get_cur_block() == NULL)
3673 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3674 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3679 if(get_method_n_ress(func_irtype) > 0) {
3680 ir_type *res_type = get_method_res_type(func_irtype, 0);
3682 if(statement->value != NULL) {
3683 ir_node *node = expression_to_firm(statement->value);
3684 node = do_strict_conv(dbgi, node);
3688 if(is_compound_type(res_type)) {
3691 mode = get_type_mode(res_type);
3693 in[0] = new_Unknown(mode);
3697 /* build return_value for its side effects */
3698 if(statement->value != NULL) {
3699 expression_to_firm(statement->value);
3704 ir_node *store = get_store();
3705 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3707 ir_node *end_block = get_irg_end_block(current_ir_graph);
3708 add_immBlock_pred(end_block, ret);
3710 set_cur_block(NULL);
3713 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3715 if(get_cur_block() == NULL)
3718 return expression_to_firm(statement->expression);
3721 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3723 declaration_t *declaration = compound->scope.declarations;
3724 for( ; declaration != NULL; declaration = declaration->next) {
3725 create_local_declaration(declaration);
3728 ir_node *result = NULL;
3729 statement_t *statement = compound->statements;
3730 for( ; statement != NULL; statement = statement->base.next) {
3731 if(statement->base.next == NULL
3732 && statement->kind == STATEMENT_EXPRESSION) {
3733 result = expression_statement_to_firm(
3734 &statement->expression);
3737 statement_to_firm(statement);
3743 static void create_global_variable(declaration_t *declaration)
3747 switch ((storage_class_tag_t)declaration->storage_class) {
3748 case STORAGE_CLASS_STATIC:
3749 vis = visibility_local;
3752 case STORAGE_CLASS_EXTERN:
3753 vis = visibility_external_allocated;
3756 case STORAGE_CLASS_NONE:
3757 vis = visibility_external_visible;
3760 case STORAGE_CLASS_THREAD:
3761 vis = visibility_external_visible;
3764 case STORAGE_CLASS_THREAD_EXTERN:
3765 vis = visibility_external_allocated;
3768 case STORAGE_CLASS_THREAD_STATIC:
3769 vis = visibility_local;
3773 var_type = get_tls_type();
3777 var_type = get_glob_type();
3781 create_declaration_entity(declaration,
3782 DECLARATION_KIND_GLOBAL_VARIABLE,
3784 set_entity_visibility(declaration->v.entity, vis);
3788 case STORAGE_CLASS_TYPEDEF:
3789 case STORAGE_CLASS_AUTO:
3790 case STORAGE_CLASS_REGISTER:
3791 case STORAGE_CLASS_ENUM_ENTRY:
3794 panic("Invalid storage class for global variable");
3797 static void create_local_declaration(declaration_t *declaration)
3799 if (declaration->namespc != NAMESPACE_NORMAL)
3801 /* construct type */
3802 (void) get_ir_type(declaration->type);
3803 if (declaration->symbol == NULL) {
3807 type_t *type = skip_typeref(declaration->type);
3809 switch ((storage_class_tag_t) declaration->storage_class) {
3810 case STORAGE_CLASS_STATIC:
3811 create_local_static_variable(declaration);
3813 case STORAGE_CLASS_EXTERN:
3814 create_global_variable(declaration);
3815 create_declaration_initializer(declaration);
3817 case STORAGE_CLASS_NONE:
3818 case STORAGE_CLASS_AUTO:
3819 case STORAGE_CLASS_REGISTER:
3820 if(is_type_function(type)) {
3821 if(declaration->init.statement != NULL) {
3822 panic("nested functions not supported yet");
3824 get_function_entity(declaration);
3827 create_local_variable(declaration);
3830 case STORAGE_CLASS_ENUM_ENTRY:
3831 /* should already be handled */
3832 assert(declaration->declaration_kind == DECLARATION_KIND_ENUM_ENTRY);
3834 case STORAGE_CLASS_TYPEDEF:
3835 declaration->declaration_kind = DECLARATION_KIND_TYPE;
3837 case STORAGE_CLASS_THREAD:
3838 case STORAGE_CLASS_THREAD_EXTERN:
3839 case STORAGE_CLASS_THREAD_STATIC:
3842 panic("invalid storage class found");
3845 static void initialize_local_declaration(declaration_t *declaration)
3847 if(declaration->symbol == NULL || declaration->namespc != NAMESPACE_NORMAL)
3850 switch ((declaration_kind_t) declaration->declaration_kind) {
3851 case DECLARATION_KIND_LOCAL_VARIABLE:
3852 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
3853 create_declaration_initializer(declaration);
3856 case DECLARATION_KIND_LABEL_BLOCK:
3857 case DECLARATION_KIND_COMPOUND_MEMBER:
3858 case DECLARATION_KIND_GLOBAL_VARIABLE:
3859 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
3860 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
3861 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
3862 case DECLARATION_KIND_FUNCTION:
3863 case DECLARATION_KIND_TYPE:
3864 case DECLARATION_KIND_ENUM_ENTRY:
3867 case DECLARATION_KIND_UNKNOWN:
3868 panic("can't initialize unknwon declaration");
3870 panic("invalid declaration kind");
3873 static void declaration_statement_to_firm(declaration_statement_t *statement)
3875 declaration_t *declaration = statement->declarations_begin;
3876 declaration_t *end = statement->declarations_end->next;
3877 for( ; declaration != end; declaration = declaration->next) {
3878 if(declaration->namespc != NAMESPACE_NORMAL)
3880 initialize_local_declaration(declaration);
3884 static void if_statement_to_firm(if_statement_t *statement)
3886 ir_node *cur_block = get_cur_block();
3888 ir_node *fallthrough_block = new_immBlock();
3890 /* the true (blocks) */
3891 ir_node *true_block;
3892 if (statement->true_statement != NULL) {
3893 true_block = new_immBlock();
3894 statement_to_firm(statement->true_statement);
3895 if(get_cur_block() != NULL) {
3896 ir_node *jmp = new_Jmp();
3897 add_immBlock_pred(fallthrough_block, jmp);
3900 true_block = fallthrough_block;
3903 /* the false (blocks) */
3904 ir_node *false_block;
3905 if(statement->false_statement != NULL) {
3906 false_block = new_immBlock();
3908 statement_to_firm(statement->false_statement);
3909 if(get_cur_block() != NULL) {
3910 ir_node *jmp = new_Jmp();
3911 add_immBlock_pred(fallthrough_block, jmp);
3914 false_block = fallthrough_block;
3917 /* create the condition */
3918 if(cur_block != NULL) {
3919 set_cur_block(cur_block);
3920 create_condition_evaluation(statement->condition, true_block,
3924 mature_immBlock(true_block);
3925 if(false_block != fallthrough_block) {
3926 mature_immBlock(false_block);
3928 mature_immBlock(fallthrough_block);
3930 set_cur_block(fallthrough_block);
3933 static void while_statement_to_firm(while_statement_t *statement)
3935 ir_node *jmp = NULL;
3936 if(get_cur_block() != NULL) {
3940 /* create the header block */
3941 ir_node *header_block = new_immBlock();
3943 add_immBlock_pred(header_block, jmp);
3946 /* the false block */
3947 ir_node *false_block = new_immBlock();
3950 ir_node *body_block;
3951 if (statement->body != NULL) {
3952 ir_node *old_continue_label = continue_label;
3953 ir_node *old_break_label = break_label;
3954 continue_label = header_block;
3955 break_label = false_block;
3957 body_block = new_immBlock();
3958 statement_to_firm(statement->body);
3960 assert(continue_label == header_block);
3961 assert(break_label == false_block);
3962 continue_label = old_continue_label;
3963 break_label = old_break_label;
3965 if(get_cur_block() != NULL) {
3967 add_immBlock_pred(header_block, jmp);
3970 body_block = header_block;
3973 /* create the condition */
3974 set_cur_block(header_block);
3976 create_condition_evaluation(statement->condition, body_block, false_block);
3977 mature_immBlock(body_block);
3978 mature_immBlock(false_block);
3979 mature_immBlock(header_block);
3981 set_cur_block(false_block);
3984 static void do_while_statement_to_firm(do_while_statement_t *statement)
3986 ir_node *jmp = NULL;
3987 if(get_cur_block() != NULL) {
3991 /* create the header block */
3992 ir_node *header_block = new_immBlock();
3994 /* the false block */
3995 ir_node *false_block = new_immBlock();
3998 ir_node *body_block = new_immBlock();
4000 add_immBlock_pred(body_block, jmp);
4003 if (statement->body != NULL) {
4004 ir_node *old_continue_label = continue_label;
4005 ir_node *old_break_label = break_label;
4006 continue_label = header_block;
4007 break_label = false_block;
4009 statement_to_firm(statement->body);
4011 assert(continue_label == header_block);
4012 assert(break_label == false_block);
4013 continue_label = old_continue_label;
4014 break_label = old_break_label;
4016 if (get_cur_block() == NULL) {
4017 mature_immBlock(header_block);
4018 mature_immBlock(body_block);
4019 mature_immBlock(false_block);
4024 ir_node *body_jmp = new_Jmp();
4025 add_immBlock_pred(header_block, body_jmp);
4026 mature_immBlock(header_block);
4028 /* create the condition */
4029 set_cur_block(header_block);
4031 create_condition_evaluation(statement->condition, body_block, false_block);
4032 mature_immBlock(body_block);
4033 mature_immBlock(false_block);
4034 mature_immBlock(header_block);
4036 set_cur_block(false_block);
4039 static void for_statement_to_firm(for_statement_t *statement)
4041 ir_node *jmp = NULL;
4043 /* create declarations */
4044 declaration_t *declaration = statement->scope.declarations;
4045 for( ; declaration != NULL; declaration = declaration->next) {
4046 create_local_declaration(declaration);
4048 declaration = statement->scope.declarations;
4049 for( ; declaration != NULL; declaration = declaration->next) {
4050 initialize_local_declaration(declaration);
4053 if (get_cur_block() != NULL) {
4054 if(statement->initialisation != NULL) {
4055 expression_to_firm(statement->initialisation);
4062 /* create the step block */
4063 ir_node *const step_block = new_immBlock();
4064 if (statement->step != NULL) {
4065 expression_to_firm(statement->step);
4067 ir_node *const step_jmp = new_Jmp();
4069 /* create the header block */
4070 ir_node *const header_block = new_immBlock();
4072 add_immBlock_pred(header_block, jmp);
4074 add_immBlock_pred(header_block, step_jmp);
4076 /* the false block */
4077 ir_node *const false_block = new_immBlock();
4080 ir_node * body_block;
4081 if (statement->body != NULL) {
4082 ir_node *const old_continue_label = continue_label;
4083 ir_node *const old_break_label = break_label;
4084 continue_label = step_block;
4085 break_label = false_block;
4087 body_block = new_immBlock();
4088 statement_to_firm(statement->body);
4090 assert(continue_label == step_block);
4091 assert(break_label == false_block);
4092 continue_label = old_continue_label;
4093 break_label = old_break_label;
4095 if (get_cur_block() != NULL) {
4097 add_immBlock_pred(step_block, jmp);
4100 body_block = step_block;
4103 /* create the condition */
4104 set_cur_block(header_block);
4105 if (statement->condition != NULL) {
4106 create_condition_evaluation(statement->condition, body_block,
4109 keep_alive(header_block);
4111 add_immBlock_pred(body_block, jmp);
4114 mature_immBlock(body_block);
4115 mature_immBlock(false_block);
4116 mature_immBlock(step_block);
4117 mature_immBlock(header_block);
4118 mature_immBlock(false_block);
4120 set_cur_block(false_block);
4123 static void create_jump_statement(const statement_t *statement,
4124 ir_node *target_block)
4126 if(get_cur_block() == NULL)
4129 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4130 ir_node *jump = new_d_Jmp(dbgi);
4131 add_immBlock_pred(target_block, jump);
4133 set_cur_block(NULL);
4136 static void switch_statement_to_firm(const switch_statement_t *statement)
4138 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4140 ir_node *expression = expression_to_firm(statement->expression);
4141 ir_node *cond = new_d_Cond(dbgi, expression);
4142 ir_node *break_block = new_immBlock();
4144 set_cur_block(NULL);
4146 ir_node *const old_switch_cond = current_switch_cond;
4147 ir_node *const old_break_label = break_label;
4148 const bool old_saw_default_label = saw_default_label;
4149 saw_default_label = false;
4150 current_switch_cond = cond;
4151 break_label = break_block;
4153 if (statement->body != NULL) {
4154 statement_to_firm(statement->body);
4157 if(get_cur_block() != NULL) {
4158 ir_node *jmp = new_Jmp();
4159 add_immBlock_pred(break_block, jmp);
4162 if (!saw_default_label) {
4163 set_cur_block(get_nodes_block(cond));
4164 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4165 MAGIC_DEFAULT_PN_NUMBER);
4166 add_immBlock_pred(break_block, proj);
4169 assert(current_switch_cond == cond);
4170 assert(break_label == break_block);
4171 current_switch_cond = old_switch_cond;
4172 break_label = old_break_label;
4173 saw_default_label = old_saw_default_label;
4175 mature_immBlock(break_block);
4176 set_cur_block(break_block);
4179 static void case_label_to_firm(const case_label_statement_t *statement)
4181 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4183 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4185 /* let's create a node and hope firm constant folding creates a Const
4188 ir_node *old_block = get_nodes_block(current_switch_cond);
4189 ir_node *block = new_immBlock();
4191 set_cur_block(old_block);
4192 if(statement->expression != NULL) {
4193 long start_pn = fold_constant(statement->expression);
4194 long end_pn = start_pn;
4195 if (statement->end_range != NULL) {
4196 end_pn = fold_constant(statement->end_range);
4198 assert(start_pn <= end_pn);
4199 /* create jumps for all cases in the given range */
4200 for (long pn = start_pn; pn <= end_pn; ++pn) {
4201 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
4202 /* oops someone detected our cheating... */
4203 panic("magic default pn used");
4205 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4206 add_immBlock_pred(block, proj);
4209 saw_default_label = true;
4210 proj = new_d_defaultProj(dbgi, current_switch_cond,
4211 MAGIC_DEFAULT_PN_NUMBER);
4213 add_immBlock_pred(block, proj);
4216 if (fallthrough != NULL) {
4217 add_immBlock_pred(block, fallthrough);
4219 mature_immBlock(block);
4220 set_cur_block(block);
4222 if(statement->statement != NULL) {
4223 statement_to_firm(statement->statement);
4227 static ir_node *get_label_block(declaration_t *label)
4229 assert(label->namespc == NAMESPACE_LABEL);
4231 if(label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
4232 return label->v.block;
4234 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
4236 ir_node *old_cur_block = get_cur_block();
4237 ir_node *block = new_immBlock();
4238 set_cur_block(old_cur_block);
4240 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
4241 label->v.block = block;
4243 ARR_APP1(ir_node *, imature_blocks, block);
4248 static void label_to_firm(const label_statement_t *statement)
4250 ir_node *block = get_label_block(statement->label);
4252 if(get_cur_block() != NULL) {
4253 ir_node *jmp = new_Jmp();
4254 add_immBlock_pred(block, jmp);
4257 set_cur_block(block);
4260 if(statement->statement != NULL) {
4261 statement_to_firm(statement->statement);
4265 static void goto_to_firm(const goto_statement_t *statement)
4267 if(get_cur_block() == NULL)
4270 ir_node *block = get_label_block(statement->label);
4271 ir_node *jmp = new_Jmp();
4272 add_immBlock_pred(block, jmp);
4274 set_cur_block(NULL);
4277 typedef enum modifier_t {
4278 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
4279 ASM_MODIFIER_READ_WRITE = 1 << 1,
4280 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
4281 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
4284 static void asm_statement_to_firm(const asm_statement_t *statement)
4287 fprintf(stderr, "WARNING asm not implemented yet!\n");
4289 bool needs_memory = false;
4291 size_t n_clobbers = 0;
4292 asm_clobber_t *clobber = statement->clobbers;
4293 for( ; clobber != NULL; clobber = clobber->next) {
4294 if(strcmp(clobber->clobber, "memory") == 0) {
4295 needs_memory = true;
4299 ident *id = new_id_from_str(clobber->clobber);
4300 obstack_ptr_grow(&asm_obst, id);
4303 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4304 ident **clobbers = NULL;
4305 if(n_clobbers > 0) {
4306 clobbers = obstack_finish(&asm_obst);
4309 /* find and count input and output constraints */
4310 asm_constraint_t *constraint = statement->inputs;
4311 for( ; constraint != NULL; constraint = constraint->next) {
4313 bool supports_memop = false;
4314 for(const char *c = constraint->constraints; *c != 0; ++c) {
4315 /* TODO: improve error messages */
4319 panic("multiple alternative assembler constraints not "
4327 supports_memop = true;
4328 obstack_1grow(&asm_obst, *c);
4331 if(modifiers & ASM_MODIFIER_READ_WRITE)
4332 panic("inconsistent register constraints");
4333 modifiers |= ASM_MODIFIER_WRITE_ONLY;
4336 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
4337 panic("inconsistent register constraints");
4338 modifiers |= ASM_MODIFIER_READ_WRITE;
4341 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
4342 panic("early clobber assembler constraint not supported yet");
4345 modifiers |= ASM_MODIFIER_COMMUTATIVE;
4346 panic("commutative assembler constraint not supported yet");
4349 /* skip register preferences stuff... */
4350 while(*c != 0 && *c != ',')
4354 /* skip register preferences stuff... */
4358 obstack_1grow(&asm_obst, *c);
4362 obstack_1grow(&asm_obst, '\0');
4363 const char *constraint_string = obstack_finish(&asm_obst);
4365 needs_memory |= supports_memop;
4366 if(supports_memop) {
4373 static void ms_try_statement_to_firm(ms_try_statement_t *statement) {
4374 statement_to_firm(statement->try_statement);
4375 warningf(&statement->base.source_position, "structured exception handling ignored");
4378 static void leave_statement_to_firm(leave_statement_t *statement) {
4379 errorf(&statement->base.source_position, "__leave not supported yet");
4382 static void statement_to_firm(statement_t *statement)
4384 switch(statement->kind) {
4385 case STATEMENT_INVALID:
4386 panic("invalid statement found");
4388 case STATEMENT_EMPTY:
4391 case STATEMENT_COMPOUND:
4392 compound_statement_to_firm(&statement->compound);
4394 case STATEMENT_RETURN:
4395 return_statement_to_firm(&statement->returns);
4397 case STATEMENT_EXPRESSION:
4398 expression_statement_to_firm(&statement->expression);
4401 if_statement_to_firm(&statement->ifs);
4403 case STATEMENT_WHILE:
4404 while_statement_to_firm(&statement->whiles);
4406 case STATEMENT_DO_WHILE:
4407 do_while_statement_to_firm(&statement->do_while);
4409 case STATEMENT_DECLARATION:
4410 declaration_statement_to_firm(&statement->declaration);
4412 case STATEMENT_BREAK:
4413 create_jump_statement(statement, break_label);
4415 case STATEMENT_CONTINUE:
4416 create_jump_statement(statement, continue_label);
4418 case STATEMENT_SWITCH:
4419 switch_statement_to_firm(&statement->switchs);
4421 case STATEMENT_CASE_LABEL:
4422 case_label_to_firm(&statement->case_label);
4425 for_statement_to_firm(&statement->fors);
4427 case STATEMENT_LABEL:
4428 label_to_firm(&statement->label);
4430 case STATEMENT_GOTO:
4431 goto_to_firm(&statement->gotos);
4434 asm_statement_to_firm(&statement->asms);
4436 case STATEMENT_MS_TRY:
4437 ms_try_statement_to_firm(&statement->ms_try);
4439 case STATEMENT_LEAVE:
4440 leave_statement_to_firm(&statement->leave);
4443 panic("Statement not implemented\n");
4446 static int count_decls_in_expression(const expression_t *expression);
4448 static int count_local_declarations(const declaration_t * decl,
4449 const declaration_t *const end)
4452 for (; decl != end; decl = decl->next) {
4453 if(decl->namespc != NAMESPACE_NORMAL)
4455 const type_t *type = skip_typeref(decl->type);
4456 if (!decl->address_taken && is_type_scalar(type))
4458 const initializer_t *initializer = decl->init.initializer;
4459 /* FIXME: should walk initializer hierarchies... */
4460 if(initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4461 count += count_decls_in_expression(initializer->value.value);
4467 static int count_decls_in_expression(const expression_t *expression) {
4470 if(expression == NULL)
4473 switch((expression_kind_t) expression->base.kind) {
4474 case EXPR_STATEMENT:
4475 return count_decls_in_stmts(expression->statement.statement);
4477 int count_left = count_decls_in_expression(expression->binary.left);
4478 int count_right = count_decls_in_expression(expression->binary.right);
4479 return count_left + count_right;
4482 return count_decls_in_expression(expression->unary.value);
4484 call_argument_t *argument = expression->call.arguments;
4485 for( ; argument != NULL; argument = argument->next) {
4486 count += count_decls_in_expression(argument->expression);
4493 panic("unexpected expression kind");
4495 case EXPR_COMPOUND_LITERAL:
4499 case EXPR_CONDITIONAL:
4500 count += count_decls_in_expression(expression->conditional.condition);
4501 count += count_decls_in_expression(expression->conditional.true_expression);
4502 count += count_decls_in_expression(expression->conditional.false_expression);
4505 case EXPR_BUILTIN_PREFETCH:
4506 count += count_decls_in_expression(expression->builtin_prefetch.adr);
4507 count += count_decls_in_expression(expression->builtin_prefetch.rw);
4508 count += count_decls_in_expression(expression->builtin_prefetch.locality);
4511 case EXPR_BUILTIN_CONSTANT_P:
4512 count += count_decls_in_expression(expression->builtin_constant.value);
4516 count += count_decls_in_expression(expression->select.compound);
4519 case EXPR_ARRAY_ACCESS:
4520 count += count_decls_in_expression(expression->array_access.array_ref);
4521 count += count_decls_in_expression(expression->array_access.index);
4524 case EXPR_CLASSIFY_TYPE:
4525 count += count_decls_in_expression(expression->classify_type.type_expression);
4529 case EXPR_ALIGNOF: {
4530 expression_t *tp_expression = expression->typeprop.tp_expression;
4531 if (tp_expression != NULL) {
4532 count += count_decls_in_expression(tp_expression);
4538 case EXPR_REFERENCE:
4540 case EXPR_CHARACTER_CONSTANT:
4541 case EXPR_WIDE_CHARACTER_CONSTANT:
4542 case EXPR_STRING_LITERAL:
4543 case EXPR_WIDE_STRING_LITERAL:
4545 case EXPR_BUILTIN_SYMBOL:
4551 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
4552 * (or implement all the missing expressions here/implement a walker)
4558 static int count_decls_in_stmts(const statement_t *stmt)
4561 for (; stmt != NULL; stmt = stmt->base.next) {
4562 switch (stmt->kind) {
4563 case STATEMENT_EMPTY:
4566 case STATEMENT_DECLARATION: {
4567 const declaration_statement_t *const decl_stmt = &stmt->declaration;
4568 count += count_local_declarations(decl_stmt->declarations_begin,
4569 decl_stmt->declarations_end->next);
4573 case STATEMENT_COMPOUND: {
4574 const compound_statement_t *const comp =
4576 count += count_decls_in_stmts(comp->statements);
4580 case STATEMENT_IF: {
4581 const if_statement_t *const if_stmt = &stmt->ifs;
4582 count += count_decls_in_expression(if_stmt->condition);
4583 count += count_decls_in_stmts(if_stmt->true_statement);
4584 count += count_decls_in_stmts(if_stmt->false_statement);
4588 case STATEMENT_SWITCH: {
4589 const switch_statement_t *const switch_stmt = &stmt->switchs;
4590 count += count_decls_in_expression(switch_stmt->expression);
4591 count += count_decls_in_stmts(switch_stmt->body);
4595 case STATEMENT_LABEL: {
4596 const label_statement_t *const label_stmt = &stmt->label;
4597 if(label_stmt->statement != NULL) {
4598 count += count_decls_in_stmts(label_stmt->statement);
4603 case STATEMENT_WHILE: {
4604 const while_statement_t *const while_stmt = &stmt->whiles;
4605 count += count_decls_in_expression(while_stmt->condition);
4606 count += count_decls_in_stmts(while_stmt->body);
4610 case STATEMENT_DO_WHILE: {
4611 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
4612 count += count_decls_in_expression(do_while_stmt->condition);
4613 count += count_decls_in_stmts(do_while_stmt->body);
4617 case STATEMENT_FOR: {
4618 const for_statement_t *const for_stmt = &stmt->fors;
4619 count += count_local_declarations(for_stmt->scope.declarations, NULL);
4620 count += count_decls_in_expression(for_stmt->initialisation);
4621 count += count_decls_in_expression(for_stmt->condition);
4622 count += count_decls_in_expression(for_stmt->step);
4623 count += count_decls_in_stmts(for_stmt->body);
4627 case STATEMENT_CASE_LABEL: {
4628 const case_label_statement_t *label = &stmt->case_label;
4629 count += count_decls_in_expression(label->expression);
4630 if(label->statement != NULL) {
4631 count += count_decls_in_stmts(label->statement);
4637 case STATEMENT_BREAK:
4638 case STATEMENT_CONTINUE:
4641 case STATEMENT_EXPRESSION: {
4642 const expression_statement_t *expr_stmt = &stmt->expression;
4643 count += count_decls_in_expression(expr_stmt->expression);
4647 case STATEMENT_GOTO:
4648 case STATEMENT_LEAVE:
4649 case STATEMENT_INVALID:
4652 case STATEMENT_RETURN: {
4653 const return_statement_t *ret_stmt = &stmt->returns;
4654 count += count_decls_in_expression(ret_stmt->value);
4658 case STATEMENT_MS_TRY: {
4659 const ms_try_statement_t *const try_stmt = &stmt->ms_try;
4660 count += count_decls_in_stmts(try_stmt->try_statement);
4661 if(try_stmt->except_expression != NULL)
4662 count += count_decls_in_expression(try_stmt->except_expression);
4663 count += count_decls_in_stmts(try_stmt->final_statement);
4671 static int get_function_n_local_vars(declaration_t *declaration)
4675 /* count parameters */
4676 count += count_local_declarations(declaration->scope.declarations, NULL);
4678 /* count local variables declared in body */
4679 count += count_decls_in_stmts(declaration->init.statement);
4684 static void initialize_function_parameters(declaration_t *declaration)
4686 ir_graph *irg = current_ir_graph;
4687 ir_node *args = get_irg_args(irg);
4688 ir_node *start_block = get_irg_start_block(irg);
4689 ir_type *function_irtype = get_ir_type(declaration->type);
4692 declaration_t *parameter = declaration->scope.declarations;
4693 for( ; parameter != NULL; parameter = parameter->next, ++n) {
4694 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
4695 type_t *type = skip_typeref(parameter->type);
4697 bool needs_entity = parameter->address_taken;
4698 assert(!is_type_array(type));
4699 if(is_type_compound(type)) {
4700 needs_entity = true;
4704 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
4705 ident *id = new_id_from_str(parameter->symbol->string);
4706 set_entity_ident(entity, id);
4708 parameter->declaration_kind
4709 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
4710 parameter->v.entity = entity;
4714 ir_mode *mode = get_ir_mode(parameter->type);
4716 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
4718 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
4719 parameter->v.value_number = next_value_number_function;
4720 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
4721 ++next_value_number_function;
4723 set_value(parameter->v.value_number, proj);
4728 * Handle additional decl modifiers for IR-graphs
4730 * @param irg the IR-graph
4731 * @param dec_modifiers additional modifiers
4733 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
4735 if (decl_modifiers & DM_NORETURN) {
4736 /* TRUE if the declaration includes the Microsoft
4737 __declspec(noreturn) specifier. */
4738 set_irg_additional_property(irg, mtp_property_noreturn);
4740 if (decl_modifiers & DM_NOTHROW) {
4741 /* TRUE if the declaration includes the Microsoft
4742 __declspec(nothrow) specifier. */
4743 set_irg_additional_property(irg, mtp_property_nothrow);
4745 if (decl_modifiers & DM_NAKED) {
4746 /* TRUE if the declaration includes the Microsoft
4747 __declspec(naked) specifier. */
4748 set_irg_additional_property(irg, mtp_property_naked);
4750 if (decl_modifiers & DM_FORCEINLINE) {
4751 /* TRUE if the declaration includes the
4752 Microsoft __forceinline specifier. */
4753 set_irg_inline_property(irg, irg_inline_forced);
4755 if (decl_modifiers & DM_NOINLINE) {
4756 /* TRUE if the declaration includes the Microsoft
4757 __declspec(noinline) specifier. */
4758 set_irg_inline_property(irg, irg_inline_forbidden);
4762 static void create_function(declaration_t *declaration)
4764 ir_entity *function_entity = get_function_entity(declaration);
4766 if(declaration->init.statement == NULL)
4769 current_function_decl = declaration;
4770 current_function_name = NULL;
4771 current_funcsig = NULL;
4773 assert(imature_blocks == NULL);
4774 imature_blocks = NEW_ARR_F(ir_node*, 0);
4776 int n_local_vars = get_function_n_local_vars(declaration);
4777 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
4779 set_irg_fp_model(irg, firm_opt.fp_model);
4780 tarval_enable_fp_ops((firm_opt.fp_model & fp_strict_algebraic) == 0);
4781 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
4783 ir_node *first_block = get_cur_block();
4785 /* set inline flags */
4786 if (declaration->is_inline)
4787 set_irg_inline_property(irg, irg_inline_recomended);
4788 handle_decl_modifier_irg(irg, declaration->decl_modifiers);
4790 next_value_number_function = 0;
4791 initialize_function_parameters(declaration);
4793 statement_to_firm(declaration->init.statement);
4795 ir_node *end_block = get_irg_end_block(irg);
4797 /* do we have a return statement yet? */
4798 if(get_cur_block() != NULL) {
4799 type_t *type = skip_typeref(declaration->type);
4800 assert(is_type_function(type));
4801 const function_type_t *func_type = &type->function;
4802 const type_t *return_type
4803 = skip_typeref(func_type->return_type);
4806 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4807 ret = new_Return(get_store(), 0, NULL);
4810 if(is_type_scalar(return_type)) {
4811 mode = get_ir_mode(func_type->return_type);
4817 /* ยง5.1.2.2.3 main implicitly returns 0 */
4818 if (strcmp(declaration->symbol->string, "main") == 0) {
4819 in[0] = new_Const(mode, get_mode_null(mode));
4821 in[0] = new_Unknown(mode);
4823 ret = new_Return(get_store(), 1, in);
4825 add_immBlock_pred(end_block, ret);
4828 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
4829 mature_immBlock(imature_blocks[i]);
4831 DEL_ARR_F(imature_blocks);
4832 imature_blocks = NULL;
4834 mature_immBlock(first_block);
4835 mature_immBlock(end_block);
4837 irg_finalize_cons(irg);
4839 /* finalize the frame type */
4840 ir_type *frame_type = get_irg_frame_type(irg);
4841 int n = get_compound_n_members(frame_type);
4844 for(int i = 0; i < n; ++i) {
4845 ir_entity *entity = get_compound_member(frame_type, i);
4846 ir_type *entity_type = get_entity_type(entity);
4848 int align = get_type_alignment_bytes(entity_type);
4849 if(align > align_all)
4853 misalign = offset % align;
4855 offset += align - misalign;
4859 set_entity_offset(entity, offset);
4860 offset += get_type_size_bytes(entity_type);
4862 set_type_size_bytes(frame_type, offset);
4863 set_type_alignment_bytes(frame_type, align_all);
4868 static void scope_to_firm(scope_t *scope)
4870 /* first pass: create declarations */
4871 declaration_t *declaration = scope->declarations;
4872 for( ; declaration != NULL; declaration = declaration->next) {
4873 if(declaration->namespc != NAMESPACE_NORMAL)
4875 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4876 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4878 if(declaration->symbol == NULL)
4881 type_t *type = skip_typeref(declaration->type);
4882 if(is_type_function(type)) {
4883 get_function_entity(declaration);
4885 create_global_variable(declaration);
4889 /* second pass: create code/initializers */
4890 declaration = scope->declarations;
4891 for( ; declaration != NULL; declaration = declaration->next) {
4892 if(declaration->namespc != NAMESPACE_NORMAL)
4894 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4895 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4897 if(declaration->symbol == NULL)
4900 type_t *type = declaration->type;
4901 if(type->kind == TYPE_FUNCTION) {
4902 create_function(declaration);
4904 assert(declaration->declaration_kind
4905 == DECLARATION_KIND_GLOBAL_VARIABLE);
4906 current_ir_graph = get_const_code_irg();
4907 create_declaration_initializer(declaration);
4912 void init_ast2firm(void)
4914 obstack_init(&asm_obst);
4915 init_atomic_modes();
4917 id_underscore = new_id_from_chars("_", 1);
4918 id_imp = new_id_from_chars("__imp_", 6);
4920 /* OS option must be set to the backend */
4921 const char *s = "ia32-gasmode=linux";
4922 switch (firm_opt.os_support) {
4923 case OS_SUPPORT_WIN32:
4924 create_ld_ident = create_ld_ident_win32;
4925 s = "ia32-gasmode=mingw";
4927 case OS_SUPPORT_LINUX:
4928 create_ld_ident = create_ld_ident_linux_elf;
4929 s = "ia32-gasmode=linux"; break;
4931 case OS_SUPPORT_MACHO:
4932 create_ld_ident = create_ld_ident_macho;
4933 s = "ia32-gasmode=macho"; break;
4938 /* create idents for all known runtime functions */
4939 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
4940 rts_idents[i] = new_id_from_str(rts_data[i].name);
4944 static void init_ir_types(void)
4946 static int ir_types_initialized = 0;
4947 if(ir_types_initialized)
4949 ir_types_initialized = 1;
4951 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4952 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4953 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4955 ir_type_int = get_ir_type(type_int);
4956 ir_type_const_char = get_ir_type(type_const_char);
4957 ir_type_wchar_t = get_ir_type(type_wchar_t);
4958 ir_type_void = get_ir_type(type_void);
4961 void exit_ast2firm(void)
4963 obstack_free(&asm_obst, NULL);
4966 void translation_unit_to_firm(translation_unit_t *unit)
4968 /* just to be sure */
4969 continue_label = NULL;
4971 current_switch_cond = NULL;
4975 scope_to_firm(&unit->scope);