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 static ir_type *ir_type_const_char;
47 static ir_type *ir_type_wchar_t;
48 static ir_type *ir_type_void;
49 static ir_type *ir_type_int;
51 static type_t *type_const_char;
53 static int next_value_number_function;
54 static ir_node *continue_label;
55 static ir_node *break_label;
56 static ir_node *current_switch_cond;
57 static bool saw_default_label;
58 static ir_node **imature_blocks;
60 static const declaration_t *current_function_decl;
61 static ir_node *current_function_name;
62 static ir_node *current_funcsig;
64 static struct obstack asm_obst;
66 typedef enum declaration_kind_t {
67 DECLARATION_KIND_UNKNOWN,
68 DECLARATION_KIND_FUNCTION,
69 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
70 DECLARATION_KIND_GLOBAL_VARIABLE,
71 DECLARATION_KIND_LOCAL_VARIABLE,
72 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
73 DECLARATION_KIND_COMPOUND_MEMBER,
74 DECLARATION_KIND_LABEL_BLOCK,
75 DECLARATION_KIND_ENUM_ENTRY
78 static ir_type *get_ir_type(type_t *type);
79 static int count_decls_in_stmts(const statement_t *stmt);
81 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
83 const declaration_t *declaration = get_irg_loc_description(irg, pos);
85 warningf(&declaration->source_position,
86 "variable '%#T' might be used uninitialized",
87 declaration->type, declaration->symbol);
88 return new_r_Unknown(irg, mode);
91 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
93 const source_position_t *pos = (const source_position_t*) dbg;
96 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
100 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
102 const source_position_t *pos = (const source_position_t*) dbg;
107 return pos->input_name;
110 static dbg_info *get_dbg_info(const source_position_t *pos)
112 return (dbg_info*) pos;
115 static unsigned unique_id = 0;
117 static ident *unique_ident(const char *tag)
121 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
123 return new_id_from_str(buf);
126 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST+1];
128 static ir_mode *mode_int, *mode_uint;
130 static ir_node *expression_to_firm(const expression_t *expression);
131 static inline ir_mode *get_ir_mode(type_t *type);
133 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
135 unsigned flags = get_atomic_type_flags(kind);
136 unsigned size = get_atomic_type_size(kind);
137 if( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
138 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
141 unsigned bit_size = size * 8;
142 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
143 mode_arithmetic arithmetic;
144 unsigned modulo_shift;
146 if(flags & ATOMIC_TYPE_FLAG_INTEGER) {
147 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
148 snprintf(name, sizeof(name), "i%s%d", is_signed?"":"u", bit_size);
149 sort = irms_int_number;
150 arithmetic = irma_twos_complement;
151 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
153 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
154 snprintf(name, sizeof(name), "f%d", bit_size);
155 sort = irms_float_number;
156 arithmetic = irma_ieee754;
159 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
167 * Initialises the atomic modes depending on the machine size.
169 static void init_atomic_modes(void)
171 for(int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
172 _atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
174 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
175 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
177 /* there's no real void type in firm */
178 _atomic_modes[ATOMIC_TYPE_VOID] = mode_int;
180 /* initialize pointer modes */
182 mode_sort sort = irms_reference;
183 unsigned bit_size = machine_size;
185 mode_arithmetic arithmetic = irma_twos_complement;
186 unsigned modulo_shift
187 = bit_size < machine_size ? machine_size : bit_size;
189 snprintf(name, sizeof(name), "p%d", machine_size);
190 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
193 set_reference_mode_signed_eq(ptr_mode, _atomic_modes[get_intptr_kind()]);
194 set_reference_mode_unsigned_eq(ptr_mode, _atomic_modes[get_uintptr_kind()]);
196 /* Hmm, pointers should be machine size */
197 set_modeP_data(ptr_mode);
198 set_modeP_code(ptr_mode);
201 static unsigned get_compound_type_size(compound_type_t *type)
203 ir_type *irtype = get_ir_type((type_t*) type);
204 return get_type_size_bytes(irtype);
207 static unsigned get_array_type_size(array_type_t *type)
209 assert(!type->is_vla);
210 ir_type *irtype = get_ir_type((type_t*) type);
211 return get_type_size_bytes(irtype);
215 static unsigned get_type_size_const(type_t *type)
217 type = skip_typeref(type);
221 panic("error type occured");
223 return get_atomic_type_size(type->atomic.akind);
225 return 2 * get_atomic_type_size(type->complex.akind);
227 return get_atomic_type_size(type->imaginary.akind);
229 return get_mode_size_bytes(mode_int);
230 case TYPE_COMPOUND_UNION:
231 case TYPE_COMPOUND_STRUCT:
232 return get_compound_type_size(&type->compound);
234 /* just a pointer to the function */
235 return get_mode_size_bytes(mode_P_code);
237 return get_mode_size_bytes(mode_P_data);
239 return get_array_type_size(&type->array);
241 return get_type_size_const(type->builtin.real_type);
243 panic("type size of bitfield request");
249 panic("Trying to determine size of invalid type");
252 static ir_node *get_type_size(type_t *type)
254 type = skip_typeref(type);
256 if(is_type_array(type) && type->array.is_vla) {
257 ir_node *size_node = type->array.size_node;
258 if(size_node == NULL) {
259 size_node = expression_to_firm(type->array.size_expression);
260 assert(!is_Const(size_node));
261 type->array.size_node = size_node;
264 ir_node *elem_size = get_type_size(type->array.element_type);
265 ir_mode *mode = get_irn_mode(size_node);
266 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
270 ir_mode *mode = get_ir_mode(type_size_t);
272 sym.type_p = get_ir_type(type);
273 return new_SymConst(mode, sym, symconst_type_size);
276 static unsigned count_parameters(const function_type_t *function_type)
280 function_parameter_t *parameter = function_type->parameters;
281 for ( ; parameter != NULL; parameter = parameter->next) {
289 * Creates a Firm type for an atomic type
291 static ir_type *create_atomic_type(const atomic_type_t *type)
293 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
294 atomic_type_kind_t kind = type->akind;
295 ir_mode *mode = _atomic_modes[kind];
296 ident *id = get_mode_ident(mode);
297 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
299 set_type_alignment_bytes(irtype, type->base.alignment);
305 * Creates a Firm type for a complex type
307 static ir_type *create_complex_type(const complex_type_t *type)
310 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
311 atomic_type_kind_t kind = type->akind;
312 ir_mode *mode = _atomic_modes[kind];
313 ident *id = get_mode_ident(mode);
317 /* FIXME: finish the array */
322 * Creates a Firm type for an imaginary type
324 static ir_type *create_imaginary_type(const imaginary_type_t *type)
326 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
327 atomic_type_kind_t kind = type->akind;
328 ir_mode *mode = _atomic_modes[kind];
329 ident *id = get_mode_ident(mode);
330 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
332 set_type_alignment_bytes(irtype, type->base.alignment);
337 static ir_type *create_method_type(const function_type_t *function_type)
339 type_t *return_type = function_type->return_type;
341 ident *id = unique_ident("functiontype");
342 int n_parameters = count_parameters(function_type);
343 int n_results = return_type == type_void ? 0 : 1;
344 dbg_info *dbgi = get_dbg_info(&function_type->base.source_position);
345 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
347 if(return_type != type_void) {
348 ir_type *restype = get_ir_type(return_type);
349 set_method_res_type(irtype, 0, restype);
352 function_parameter_t *parameter = function_type->parameters;
354 for( ; parameter != NULL; parameter = parameter->next) {
355 ir_type *p_irtype = get_ir_type(parameter->type);
356 set_method_param_type(irtype, n, p_irtype);
360 if(function_type->variadic || function_type->unspecified_parameters) {
361 set_method_variadicity(irtype, variadicity_variadic);
367 static ir_type *create_pointer_type(pointer_type_t *type)
369 type_t *points_to = type->points_to;
370 ir_type *ir_points_to;
371 /* Avoid endless recursion if the points_to type contains this poiner type
372 * again (might be a struct). We therefore first create a void* pointer
373 * and then set the real points_to type
375 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
376 ir_type *ir_type = new_d_type_pointer(unique_ident("pointer"),
377 ir_type_void, mode_P_data, dbgi);
378 type->base.firm_type = ir_type;
380 ir_points_to = get_ir_type(points_to);
381 set_pointer_points_to_type(ir_type, ir_points_to);
386 static ir_type *create_array_type(array_type_t *type)
388 type_t *element_type = type->element_type;
389 ir_type *ir_element_type = get_ir_type(element_type);
391 ident *id = unique_ident("array");
392 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
393 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
395 const int align = get_type_alignment_bytes(ir_element_type);
396 set_type_alignment_bytes(ir_type, align);
398 if(type->size_constant) {
399 int n_elements = type->size;
401 set_array_bounds_int(ir_type, 0, 0, n_elements);
403 size_t elemsize = get_type_size_bytes(ir_element_type);
404 if(elemsize % align > 0) {
405 elemsize += align - (elemsize % align);
407 set_type_size_bytes(ir_type, n_elements * elemsize);
409 set_array_lower_bound_int(ir_type, 0, 0);
411 set_type_state(ir_type, layout_fixed);
417 * Return the signed integer type of size bits.
419 * @param size the size
421 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
424 static ir_mode *s_modes[64 + 1] = {NULL, };
428 if (size <= 0 || size > 64)
431 mode = s_modes[size];
435 snprintf(name, sizeof(name), "bf_I%u", size);
436 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
437 size <= 32 ? 32 : size );
438 s_modes[size] = mode;
442 snprintf(name, sizeof(name), "I%u", size);
443 ident *id = new_id_from_str(name);
444 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
445 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
446 set_primitive_base_type(res, base_tp);
452 * Return the unsigned integer type of size bits.
454 * @param size the size
456 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
459 static ir_mode *u_modes[64 + 1] = {NULL, };
463 if (size <= 0 || size > 64)
466 mode = u_modes[size];
470 snprintf(name, sizeof(name), "bf_U%u", size);
471 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
472 size <= 32 ? 32 : size );
473 u_modes[size] = mode;
478 snprintf(name, sizeof(name), "U%u", size);
479 ident *id = new_id_from_str(name);
480 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
481 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
482 set_primitive_base_type(res, base_tp);
487 static ir_type *create_bitfield_type(bitfield_type_t *const type)
489 type_t *base = skip_typeref(type->base_type);
490 assert(base->kind == TYPE_ATOMIC);
491 ir_type *irbase = get_ir_type(base);
493 unsigned size = fold_constant(type->size);
495 assert(!is_type_float(base));
496 if(is_type_signed(base)) {
497 return get_signed_int_type_for_bit_size(irbase, size);
499 return get_unsigned_int_type_for_bit_size(irbase, size);
503 #define INVALID_TYPE ((ir_type_ptr)-1)
505 static ir_type *create_union_type(compound_type_t *type, ir_type *irtype,
506 size_t *outer_offset, size_t *outer_align);
508 static ir_type *create_struct_type(compound_type_t *type, ir_type *irtype,
509 size_t *outer_offset, size_t *outer_align)
511 declaration_t *declaration = type->declaration;
512 if(declaration->v.irtype != NULL) {
513 return declaration->v.irtype;
516 size_t align_all = 1;
518 size_t bit_offset = 0;
520 symbol_t *symbol = declaration->symbol;
523 id = unique_ident(symbol->string);
525 id = unique_ident("__anonymous_struct");
527 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
529 irtype = new_d_type_struct(id, dbgi);
531 declaration->v.irtype = irtype;
532 type->base.firm_type = irtype;
534 offset = *outer_offset;
535 align_all = *outer_align;
538 declaration_t *entry = declaration->scope.declarations;
539 for( ; entry != NULL; entry = entry->next) {
540 if(entry->namespc != NAMESPACE_NORMAL)
543 symbol_t *symbol = entry->symbol;
544 type_t *entry_type = skip_typeref(entry->type);
545 dbg_info *dbgi = get_dbg_info(&entry->source_position);
548 ident = new_id_from_str(symbol->string);
550 if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
551 create_struct_type(&entry_type->compound, irtype, &offset,
554 } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
555 create_union_type(&entry_type->compound, irtype, &offset,
559 assert(entry_type->kind == TYPE_BITFIELD);
561 ident = unique_ident("anon");
564 ir_type *base_irtype;
565 if(entry_type->kind == TYPE_BITFIELD) {
566 base_irtype = get_ir_type(entry_type->bitfield.base_type);
568 base_irtype = get_ir_type(entry_type);
571 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
572 size_t misalign = offset % entry_alignment;
574 ir_type *entry_irtype = get_ir_type(entry_type);
575 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
578 size_t bits_remainder;
579 if(entry_type->kind == TYPE_BITFIELD) {
580 size_t size_bits = fold_constant(entry_type->bitfield.size);
581 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
583 if(size_bits > rest_size_bits) {
584 /* start a new bucket */
585 offset += entry_alignment - misalign;
591 /* put into current bucket */
592 base = offset - misalign;
593 bits_remainder = misalign * 8 + bit_offset;
596 offset += size_bits / 8;
597 bit_offset = bit_offset + (size_bits % 8);
599 size_t entry_size = get_type_size_bytes(base_irtype);
600 if(misalign > 0 || bit_offset > 0)
601 offset += entry_alignment - misalign;
605 offset += entry_size;
609 if(entry_alignment > align_all) {
610 if(entry_alignment % align_all != 0) {
611 panic("uneven alignments not supported yet");
613 align_all = entry_alignment;
616 set_entity_offset(entity, base);
617 set_entity_offset_bits_remainder(entity,
618 (unsigned char) bits_remainder);
619 //add_struct_member(irtype, entity);
620 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
621 assert(entry->v.entity == NULL);
622 entry->v.entity = entity;
625 size_t misalign = offset % align_all;
626 if(misalign > 0 || bit_offset > 0) {
627 offset += align_all - misalign;
630 if(outer_offset != NULL) {
631 *outer_offset = offset;
632 *outer_align = align_all;
634 set_type_alignment_bytes(irtype, align_all);
635 set_type_size_bytes(irtype, offset);
636 set_type_state(irtype, layout_fixed);
642 static ir_type *create_union_type(compound_type_t *type, ir_type *irtype,
643 size_t *outer_offset, size_t *outer_align)
645 declaration_t *declaration = type->declaration;
646 if(declaration->v.irtype != NULL) {
647 return declaration->v.irtype;
650 size_t align_all = 1;
655 symbol_t *symbol = declaration->symbol;
658 id = unique_ident(symbol->string);
660 id = unique_ident("__anonymous_union");
662 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
664 irtype = new_d_type_union(id, dbgi);
666 declaration->v.irtype = irtype;
667 type->base.firm_type = irtype;
669 offset = *outer_offset;
670 align_all = *outer_align;
673 type->base.firm_type = irtype;
675 declaration_t *entry = declaration->scope.declarations;
676 for( ; entry != NULL; entry = entry->next) {
677 if(entry->namespc != NAMESPACE_NORMAL)
680 symbol_t *symbol = entry->symbol;
681 type_t *entry_type = skip_typeref(entry->type);
682 ir_type *entry_ir_type = get_ir_type(entry_type);
686 ident = new_id_from_str(entry->symbol->string);
688 size_t offs = offset;
689 if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
690 create_struct_type(&entry_type->compound, irtype, &offs,
693 } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
694 create_union_type(&entry_type->compound, irtype, &offs,
698 panic("anonymous union member must be struct or union");
700 size_t entry_size = offs - offset;
701 if(entry_size > size) {
704 ident = unique_ident("anon");
707 size_t entry_size = get_type_size_bytes(entry_ir_type);
708 size_t entry_alignment = get_type_alignment_bytes(entry_ir_type);
710 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
711 ir_entity *const entity = new_d_entity(irtype, ident, entry_ir_type,
713 //add_union_member(irtype, entity);
714 set_entity_offset(entity, 0);
715 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
716 assert(entry->v.entity == NULL);
717 entry->v.entity = entity;
719 if(entry_size > size) {
722 if(entry_alignment > align_all) {
723 if(entry_alignment % align_all != 0) {
724 panic("Uneven alignments not supported yet");
726 align_all = entry_alignment;
730 if(outer_offset != NULL) {
731 assert(*outer_offset == offset);
733 size_t misalign = offset % align_all;
735 size += align_all - misalign;
736 *outer_offset += size;
738 if(align_all > *outer_align) {
739 if(align_all % *outer_align != 0) {
740 panic("uneven alignments not supported yet");
742 *outer_align = align_all;
745 set_type_alignment_bytes(irtype, align_all);
746 set_type_size_bytes(irtype, size);
747 set_type_state(irtype, layout_fixed);
748 declaration->v.irtype = irtype;
754 static ir_type *create_enum_type(enum_type_t *const type)
756 type->base.firm_type = ir_type_int;
758 ir_mode *const mode = mode_int;
759 tarval *const one = get_mode_one(mode);
760 tarval * tv_next = get_tarval_null(mode);
762 declaration_t *declaration = type->declaration->next;
763 for (; declaration != NULL; declaration = declaration->next) {
764 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
767 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
769 expression_t *const init = declaration->init.enum_value;
771 ir_node *const cnst = expression_to_firm(init);
772 if (!is_Const(cnst)) {
773 panic("couldn't fold constant");
775 tv_next = get_Const_tarval(cnst);
777 declaration->v.enum_val = tv_next;
778 tv_next = tarval_add(tv_next, one);
784 static ir_type *get_ir_type(type_t *type)
786 assert(type != NULL);
788 type = skip_typeref(type);
790 if(type->base.firm_type != NULL) {
791 assert(type->base.firm_type != INVALID_TYPE);
792 return type->base.firm_type;
795 ir_type *firm_type = NULL;
798 panic("error type occurred");
800 firm_type = create_atomic_type(&type->atomic);
803 firm_type = create_complex_type(&type->complex);
806 firm_type = create_imaginary_type(&type->imaginary);
809 firm_type = create_method_type(&type->function);
812 firm_type = create_pointer_type(&type->pointer);
815 firm_type = create_array_type(&type->array);
817 case TYPE_COMPOUND_STRUCT:
818 firm_type = create_struct_type(&type->compound, NULL, NULL, NULL);
820 case TYPE_COMPOUND_UNION:
821 firm_type = create_union_type(&type->compound, NULL, NULL, NULL);
824 firm_type = create_enum_type(&type->enumt);
827 firm_type = get_ir_type(type->builtin.real_type);
830 firm_type = create_bitfield_type(&type->bitfield);
838 if(firm_type == NULL)
839 panic("unknown type found");
841 type->base.firm_type = firm_type;
845 static inline ir_mode *get_ir_mode(type_t *type)
847 ir_type *irtype = get_ir_type(type);
849 /* firm doesn't report a mode for arrays somehow... */
850 if(is_Array_type(irtype)) {
854 ir_mode *mode = get_type_mode(irtype);
855 assert(mode != NULL);
859 static ident *predef_idents[rts_max];
861 /** Names of the runtime functions. */
862 static const struct {
863 int id; /**< the rts id */
864 int n_res; /**< number of return values */
865 const char *name; /**< the name of the rts function */
866 int n_params; /**< number of parameters */
867 unsigned flags; /**< language flags */
869 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
870 { rts_abort, 0, "abort", 0, _C89 },
871 { rts_abs, 1, "abs", 1, _C89 },
872 { rts_labs, 1, "labs", 1, _C89 },
873 { rts_llabs, 1, "llabs", 1, _C99 },
874 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
876 { rts_fabs, 1, "fabs", 1, _C89 },
877 { rts_sqrt, 1, "sqrt", 1, _C89 },
878 { rts_cbrt, 1, "cbrt", 1, _C99 },
879 { rts_exp, 1, "exp", 1, _C89 },
880 { rts_exp2, 1, "exp2", 1, _C89 },
881 { rts_exp10, 1, "exp10", 1, _GNUC },
882 { rts_log, 1, "log", 1, _C89 },
883 { rts_log2, 1, "log2", 1, _C89 },
884 { rts_log10, 1, "log10", 1, _C89 },
885 { rts_pow, 1, "pow", 2, _C89 },
886 { rts_sin, 1, "sin", 1, _C89 },
887 { rts_cos, 1, "cos", 1, _C89 },
888 { rts_tan, 1, "tan", 1, _C89 },
889 { rts_asin, 1, "asin", 1, _C89 },
890 { rts_acos, 1, "acos", 1, _C89 },
891 { rts_atan, 1, "atan", 1, _C89 },
892 { rts_sinh, 1, "sinh", 1, _C89 },
893 { rts_cosh, 1, "cosh", 1, _C89 },
894 { rts_tanh, 1, "tanh", 1, _C89 },
896 { rts_fabsf, 1, "fabsf", 1, _C99 },
897 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
898 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
899 { rts_expf, 1, "expf", 1, _C99 },
900 { rts_exp2f, 1, "exp2f", 1, _C99 },
901 { rts_exp10f, 1, "exp10f", 1, _GNUC },
902 { rts_logf, 1, "logf", 1, _C99 },
903 { rts_log2f, 1, "log2f", 1, _C99 },
904 { rts_log10f, 1, "log10f", 1, _C99 },
905 { rts_powf, 1, "powf", 2, _C99 },
906 { rts_sinf, 1, "sinf", 1, _C99 },
907 { rts_cosf, 1, "cosf", 1, _C99 },
908 { rts_tanf, 1, "tanf", 1, _C99 },
909 { rts_asinf, 1, "asinf", 1, _C99 },
910 { rts_acosf, 1, "acosf", 1, _C99 },
911 { rts_atanf, 1, "atanf", 1, _C99 },
912 { rts_sinhf, 1, "sinhf", 1, _C99 },
913 { rts_coshf, 1, "coshf", 1, _C99 },
914 { rts_tanhf, 1, "tanhf", 1, _C99 },
916 { rts_fabsl, 1, "fabsl", 1, _C99 },
917 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
918 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
919 { rts_expl, 1, "expl", 1, _C99 },
920 { rts_exp2l, 1, "exp2l", 1, _C99 },
921 { rts_exp10l, 1, "exp10l", 1, _GNUC },
922 { rts_logl, 1, "logl", 1, _C99 },
923 { rts_log2l, 1, "log2l", 1, _C99 },
924 { rts_log10l, 1, "log10l", 1, _C99 },
925 { rts_powl, 1, "powl", 2, _C99 },
926 { rts_sinl, 1, "sinl", 1, _C99 },
927 { rts_cosl, 1, "cosl", 1, _C99 },
928 { rts_tanl, 1, "tanl", 1, _C99 },
929 { rts_asinl, 1, "asinl", 1, _C99 },
930 { rts_acosl, 1, "acosl", 1, _C99 },
931 { rts_atanl, 1, "atanl", 1, _C99 },
932 { rts_sinhl, 1, "sinhl", 1, _C99 },
933 { rts_coshl, 1, "coshl", 1, _C99 },
934 { rts_tanhl, 1, "tanhl", 1, _C99 },
936 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
937 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
938 { rts_strcmp, 1, "strcmp", 2, _C89 },
939 { rts_strncmp, 1, "strncmp", 3, _C89 }
943 * Mangles an entity linker (ld) name for win32 usage.
945 * @param ent the entity to be mangled
946 * @param decl_modifiers the set of modifiers for this entity
948 static ir_ident_ptr decorate_win32(ir_entity_ptr ent, decl_modifiers_t decl_modifiers) {
951 if (is_Method_type(get_entity_type(ent)))
952 id = decorate_win32_c_fkt(ent, get_entity_ident(ent));
954 /* always add an underscore in win32 */
955 id = mangle(new_id_from_chars("_", 1), get_entity_ident(ent));
958 if (decl_modifiers & DM_DLLIMPORT) {
959 /* add prefix for imported symbols */
960 id = mangle(new_id_from_chars("__imp_", 6), id);
966 * Mangles an entity linker (ld) name from a declaration.
968 * @param ent the entity to be mangled
969 * @param declaration the declaration
971 static void mangle_ent_from_decl(ir_entity *ent, declaration_t *declaration)
975 if (firm_opt.os_support == OS_SUPPORT_MINGW)
976 id = decorate_win32(ent, declaration->decl_modifiers);
978 id = get_entity_ident(ent);
980 set_entity_ld_ident(ent, id);
983 static ir_entity* get_function_entity(declaration_t *declaration)
985 if(declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
986 return declaration->v.entity;
987 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
989 symbol_t *symbol = declaration->symbol;
990 ident *id = new_id_from_str(symbol->string);
992 ir_type *global_type = get_glob_type();
993 ir_type *ir_type_method = get_ir_type(declaration->type);
994 assert(is_Method_type(ir_type_method));
996 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
997 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
998 mangle_ent_from_decl(entity, declaration);
999 if(declaration->storage_class == STORAGE_CLASS_STATIC
1000 || declaration->is_inline) {
1001 set_entity_visibility(entity, visibility_local);
1002 } else if(declaration->init.statement != NULL) {
1003 set_entity_visibility(entity, visibility_external_visible);
1005 set_entity_visibility(entity, visibility_external_allocated);
1007 /* We should check for file scope here, but as long as we compile C only
1008 this is not needed. */
1009 int n_params = get_method_n_params(ir_type_method);
1010 int n_res = get_method_n_ress(ir_type_method);
1013 if (n_params == 0 && n_res == 0 && id == predef_idents[rts_abort]) {
1014 /* found abort(), store for later */
1018 if (! firm_opt.freestanding) {
1019 /* check for a known runtime function */
1020 for (i = 0; i < rts_max; ++i) {
1021 /* ignore those rts functions not necessary needed for current mode */
1022 if ((c_mode & rts_data[i].flags) == 0)
1024 if (n_params == rts_data[i].n_params && n_res == rts_data[i].n_res &&
1025 id == predef_idents[rts_data[i].id])
1026 rts_entities[rts_data[i].id] = entity;
1031 set_entity_allocation(entity, allocation_static);
1033 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1034 declaration->v.entity = entity;
1039 static ir_node *const_to_firm(const const_expression_t *cnst)
1041 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1042 ir_mode *mode = get_ir_mode(cnst->base.type);
1047 if(mode_is_float(mode)) {
1048 tv = new_tarval_from_double(cnst->v.float_value, mode);
1050 if(mode_is_signed(mode)) {
1051 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1053 len = snprintf(buf, sizeof(buf), "%llu",
1054 (unsigned long long) cnst->v.int_value);
1056 tv = new_tarval_from_str(buf, len, mode);
1059 return new_d_Const(dbgi, mode, tv);
1062 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1064 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1065 ir_mode *mode = get_ir_mode(cnst->base.type);
1067 long long int v = 0;
1068 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1069 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1072 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1073 tarval *tv = new_tarval_from_str(buf, len, mode);
1075 return new_d_Const(dbgi, mode, tv);
1078 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1080 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1081 ir_mode *mode = get_ir_mode(cnst->base.type);
1083 long long int v = cnst->v.wide_character.begin[0];
1086 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1087 tarval *tv = new_tarval_from_str(buf, len, mode);
1089 return new_d_Const(dbgi, mode, tv);
1092 static ir_node *create_global(dbg_info *dbgi, ir_mode *mode,
1095 assert(entity != NULL);
1097 ir_graph *irg = current_ir_graph;
1098 return new_d_simpleSel(dbgi, get_irg_no_mem(irg),
1099 get_irg_globals(irg), entity);
1101 symconst_symbol sym;
1102 sym.entity_p = entity;
1103 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1107 static ir_node *string_to_firm(const source_position_t *const src_pos,
1108 const char *const id_prefix,
1109 const string_t *const value)
1111 ir_type *const global_type = get_glob_type();
1112 dbg_info *const dbgi = get_dbg_info(src_pos);
1113 ir_type *const type = new_d_type_array(unique_ident("strtype"), 1,
1114 ir_type_const_char, dbgi);
1116 ident *const id = unique_ident(id_prefix);
1117 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1118 set_entity_ld_ident(entity, id);
1119 set_entity_variability(entity, variability_constant);
1120 set_entity_allocation(entity, allocation_static);
1122 ir_type *const elem_type = ir_type_const_char;
1123 ir_mode *const mode = get_type_mode(elem_type);
1125 const char* const string = value->begin;
1126 const size_t slen = value->size;
1128 set_array_lower_bound_int(type, 0, 0);
1129 set_array_upper_bound_int(type, 0, slen);
1130 set_type_size_bytes(type, slen);
1131 set_type_state(type, layout_fixed);
1133 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1134 for(size_t i = 0; i < slen; ++i) {
1135 tvs[i] = new_tarval_from_long(string[i], mode);
1138 set_array_entity_values(entity, tvs, slen);
1141 return create_global(dbgi, mode_P_data, entity);
1144 static ir_node *string_literal_to_firm(
1145 const string_literal_expression_t* literal)
1147 return string_to_firm(&literal->base.source_position, "Lstr",
1151 static ir_node *wide_string_literal_to_firm(
1152 const wide_string_literal_expression_t* const literal)
1154 ir_type *const global_type = get_glob_type();
1155 ir_type *const elem_type = ir_type_wchar_t;
1156 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1157 ir_type *const type = new_d_type_array(unique_ident("strtype"), 1,
1160 ident *const id = unique_ident("Lstr");
1161 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1162 set_entity_ld_ident(entity, id);
1163 set_entity_variability(entity, variability_constant);
1164 set_entity_allocation(entity, allocation_static);
1166 ir_mode *const mode = get_type_mode(elem_type);
1168 const wchar_rep_t *const string = literal->value.begin;
1169 const size_t slen = literal->value.size;
1171 set_array_lower_bound_int(type, 0, 0);
1172 set_array_upper_bound_int(type, 0, slen);
1173 set_type_size_bytes(type, slen);
1174 set_type_state(type, layout_fixed);
1176 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1177 for(size_t i = 0; i < slen; ++i) {
1178 tvs[i] = new_tarval_from_long(string[i], mode);
1181 set_array_entity_values(entity, tvs, slen);
1184 return create_global(dbgi, mode_P_data, entity);
1187 static ir_node *deref_address(type_t *const type, ir_node *const addr,
1188 dbg_info *const dbgi)
1190 ir_type *irtype = get_ir_type(type);
1191 if (is_compound_type(irtype)
1192 || is_Method_type(irtype)
1193 || is_Array_type(irtype)) {
1197 ir_mode *const mode = get_type_mode(irtype);
1198 ir_node *const memory = get_store();
1199 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1200 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1201 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1203 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1204 set_Load_volatility(load, volatility_is_volatile);
1207 set_store(load_mem);
1211 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1213 ir_mode *mode = get_irn_mode(node);
1215 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1217 if(!mode_is_float(mode))
1220 /* check if there is already a Conv */
1221 if (get_irn_op(node) == op_Conv) {
1222 /* convert it into a strict Conv */
1223 set_Conv_strict(node, 1);
1227 /* otherwise create a new one */
1228 return new_d_strictConv(dbgi, node, mode);
1231 static ir_node *get_global_var_address(dbg_info *const dbgi,
1232 const declaration_t *const decl)
1234 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1236 ir_entity *const entity = decl->v.entity;
1237 switch ((storage_class_tag_t)decl->storage_class) {
1238 case STORAGE_CLASS_THREAD:
1239 case STORAGE_CLASS_THREAD_EXTERN:
1240 case STORAGE_CLASS_THREAD_STATIC: {
1241 ir_node *const no_mem = new_NoMem();
1242 ir_node *const tls = get_irg_tls(current_ir_graph);
1243 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1247 return create_global(dbgi, mode_P_data, entity);
1251 /* Returns the correct base address depending on whether it is a parameter or a
1252 * normal local variable */
1253 static ir_node *get_local_frame(ir_entity *const ent)
1255 ir_graph *const irg = current_ir_graph;
1256 const ir_type *const owner = get_entity_owner(ent);
1257 if (owner == get_irg_frame_type(irg)) {
1258 return get_irg_frame(irg);
1260 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1261 return get_irg_value_param_base(irg);
1265 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1267 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1268 declaration_t *declaration = ref->declaration;
1269 type_t *type = skip_typeref(declaration->type);
1271 switch((declaration_kind_t) declaration->declaration_kind) {
1272 case DECLARATION_KIND_UNKNOWN:
1273 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
1279 case DECLARATION_KIND_ENUM_ENTRY: {
1280 ir_mode *const mode = get_ir_mode(type);
1281 return new_Const(mode, declaration->v.enum_val);
1284 case DECLARATION_KIND_LOCAL_VARIABLE: {
1285 ir_mode *const mode = get_ir_mode(type);
1286 return get_value(declaration->v.value_number, mode);
1288 case DECLARATION_KIND_FUNCTION: {
1289 ir_mode *const mode = get_ir_mode(type);
1290 return create_global(dbgi, mode, declaration->v.entity);
1292 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1293 ir_node *const addr = get_global_var_address(dbgi, declaration);
1294 return deref_address(declaration->type, addr, dbgi);
1297 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1298 ir_entity *entity = declaration->v.entity;
1299 ir_node *frame = get_local_frame(entity);
1300 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1301 return deref_address(declaration->type, sel, dbgi);
1304 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1305 return declaration->v.vla_base;
1307 case DECLARATION_KIND_COMPOUND_MEMBER:
1308 case DECLARATION_KIND_LABEL_BLOCK:
1309 panic("not implemented reference type");
1312 panic("reference to declaration with unknown type found");
1315 static ir_node *reference_addr(const reference_expression_t *ref)
1317 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1318 declaration_t *declaration = ref->declaration;
1320 switch((declaration_kind_t) declaration->declaration_kind) {
1321 case DECLARATION_KIND_UNKNOWN:
1323 case DECLARATION_KIND_LOCAL_VARIABLE:
1324 panic("local variable without entity has no address");
1325 case DECLARATION_KIND_FUNCTION: {
1326 type_t *const type = skip_typeref(ref->base.type);
1327 ir_mode *const mode = get_ir_mode(type);
1328 return create_global(dbgi, mode, declaration->v.entity);
1330 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1331 ir_node *const addr = get_global_var_address(dbgi, declaration);
1334 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1335 ir_entity *entity = declaration->v.entity;
1336 ir_node *frame = get_local_frame(entity);
1337 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1342 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1343 return declaration->v.vla_base;
1345 case DECLARATION_KIND_ENUM_ENTRY:
1346 panic("trying to reference enum entry");
1348 case DECLARATION_KIND_COMPOUND_MEMBER:
1349 case DECLARATION_KIND_LABEL_BLOCK:
1350 panic("not implemented reference type");
1353 panic("reference to declaration with unknown type found");
1356 static ir_node *process_builtin_call(const call_expression_t *call)
1358 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1360 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1361 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1363 type_t *type = skip_typeref(builtin->base.type);
1364 assert(is_type_pointer(type));
1366 type_t *function_type = skip_typeref(type->pointer.points_to);
1367 symbol_t *symbol = builtin->symbol;
1369 switch(symbol->ID) {
1370 case T___builtin_alloca: {
1371 if(call->arguments == NULL || call->arguments->next != NULL) {
1372 panic("invalid number of parameters on __builtin_alloca");
1374 expression_t *argument = call->arguments->expression;
1375 ir_node *size = expression_to_firm(argument);
1377 ir_node *store = get_store();
1378 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1380 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1382 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1386 case T___builtin_nan:
1387 case T___builtin_nanf:
1388 case T___builtin_nand: {
1389 /* Ignore string for now... */
1390 assert(is_type_function(function_type));
1391 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1392 tarval *tv = get_mode_NAN(mode);
1393 ir_node *res = new_d_Const(dbgi, mode, tv);
1396 case T___builtin_va_end:
1399 panic("Unsupported builtin found\n");
1403 static ir_node *call_expression_to_firm(const call_expression_t *call)
1405 assert(get_cur_block() != NULL);
1407 expression_t *function = call->function;
1408 if(function->kind == EXPR_BUILTIN_SYMBOL) {
1409 return process_builtin_call(call);
1411 ir_node *callee = expression_to_firm(function);
1413 type_t *type = skip_typeref(function->base.type);
1414 assert(is_type_pointer(type));
1415 pointer_type_t *pointer_type = &type->pointer;
1416 type_t *points_to = skip_typeref(pointer_type->points_to);
1417 assert(is_type_function(points_to));
1418 function_type_t *function_type = &points_to->function;
1420 int n_parameters = 0;
1421 call_argument_t *argument = call->arguments;
1422 for( ; argument != NULL; argument = argument->next) {
1426 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1428 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1429 ir_type *new_method_type = NULL;
1430 if(function_type->variadic || function_type->unspecified_parameters) {
1431 /* we need to construct a new method type matching the call
1433 int n_res = get_method_n_ress(ir_method_type);
1434 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1435 new_method_type = new_d_type_method(unique_ident("calltype"),
1436 n_parameters, n_res, dbgi);
1437 set_method_calling_convention(new_method_type,
1438 get_method_calling_convention(ir_method_type));
1439 set_method_additional_properties(new_method_type,
1440 get_method_additional_properties(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));
1447 ir_node *in[n_parameters];
1449 argument = call->arguments;
1451 for( ; argument != NULL; argument = argument->next) {
1452 expression_t *expression = argument->expression;
1453 ir_node *arg_node = expression_to_firm(expression);
1455 arg_node = do_strict_conv(dbgi, arg_node);
1458 if(new_method_type != NULL) {
1459 ir_type *irtype = get_ir_type(expression->base.type);
1460 set_method_param_type(new_method_type, n, irtype);
1465 assert(n == n_parameters);
1467 if(new_method_type != NULL)
1468 ir_method_type = new_method_type;
1470 ir_node *store = get_store();
1471 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1473 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1476 type_t *return_type = skip_typeref(function_type->return_type);
1477 ir_node *result = NULL;
1479 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1481 if(is_type_scalar(return_type)) {
1482 mode = get_ir_mode(return_type);
1486 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1487 result = new_d_Proj(dbgi, resproj, mode, 0);
1493 static void statement_to_firm(statement_t *statement);
1494 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1496 static ir_node *expression_to_addr(const expression_t *expression);
1497 static void create_condition_evaluation(const expression_t *expression,
1498 ir_node *true_block,
1499 ir_node *false_block);
1501 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1504 value = do_strict_conv(dbgi, value);
1506 ir_node *memory = get_store();
1508 if(is_type_scalar(type)) {
1509 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1510 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1511 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
1512 set_Store_volatility(store, volatility_is_volatile);
1513 set_store(store_mem);
1515 ir_type *irtype = get_ir_type(type);
1516 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1517 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1518 set_store(copyb_mem);
1522 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1524 tarval *all_one = get_mode_all_one(mode);
1525 int mode_size = get_mode_size_bits(mode);
1527 assert(offset >= 0 && size >= 0);
1528 assert(offset + size <= mode_size);
1529 if(size == mode_size) {
1533 long shiftr = get_mode_size_bits(mode) - size;
1534 long shiftl = offset;
1535 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1536 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1537 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1538 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1543 static void bitfield_store_to_firm(const unary_expression_t *expression,
1546 expression_t *select = expression->value;
1547 assert(select->kind == EXPR_SELECT);
1548 type_t *type = select->base.type;
1549 assert(type->kind == TYPE_BITFIELD);
1550 ir_mode *mode = get_ir_mode(type->bitfield.base_type);
1551 ir_node *addr = expression_to_addr(select);
1553 assert(get_irn_mode(value) == mode);
1555 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1557 /* kill upper bits of value and shift to right position */
1558 ir_entity *entity = select->select.compound_entry->v.entity;
1559 int bitoffset = get_entity_offset_bits_remainder(entity);
1560 ir_type *entity_type = get_entity_type(entity);
1561 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1563 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1564 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1565 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1566 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1567 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1568 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1570 /* load current value */
1571 ir_node *mem = get_store();
1572 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1573 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1574 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1575 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1576 tarval *inv_mask = tarval_not(shift_mask);
1577 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1578 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1580 /* construct new value and store */
1581 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1582 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1583 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1584 set_store(store_mem);
1586 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1587 set_Load_volatility(load, volatility_is_volatile);
1588 set_Store_volatility(store, volatility_is_volatile);
1592 static void set_value_for_expression(const expression_t *expression,
1595 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1596 value = do_strict_conv(dbgi, value);
1598 if(expression->kind == EXPR_REFERENCE) {
1599 const reference_expression_t *ref = &expression->reference;
1601 declaration_t *declaration = ref->declaration;
1602 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1603 if(declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1604 set_value(declaration->v.value_number, value);
1609 if(expression->kind == EXPR_UNARY_BITFIELD_EXTRACT) {
1610 bitfield_store_to_firm(&expression->unary, value);
1614 ir_node *addr = expression_to_addr(expression);
1615 type_t *type = skip_typeref(expression->base.type);
1616 assign_value(dbgi, addr, type, value);
1619 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1621 ir_mode *value_mode = get_irn_mode(value);
1623 if (value_mode == dest_mode || is_Bad(value))
1626 if(dest_mode == mode_b) {
1627 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1628 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1629 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1633 return new_d_Conv(dbgi, value, dest_mode);
1636 static ir_node *create_incdec(const unary_expression_t *expression)
1638 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1639 type_t *type = skip_typeref(expression->base.type);
1640 ir_mode *mode = get_ir_mode(type);
1641 expression_t *value = expression->value;
1643 ir_node *value_node = expression_to_firm(value);
1646 if(is_type_pointer(type)) {
1647 pointer_type_t *pointer_type = &type->pointer;
1648 offset = get_type_size(pointer_type->points_to);
1650 assert(is_type_arithmetic(type));
1651 offset = new_Const(mode, get_mode_one(mode));
1654 switch(expression->base.kind) {
1655 case EXPR_UNARY_POSTFIX_INCREMENT: {
1656 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1657 set_value_for_expression(value, new_value);
1660 case EXPR_UNARY_POSTFIX_DECREMENT: {
1661 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1662 set_value_for_expression(value, new_value);
1665 case EXPR_UNARY_PREFIX_INCREMENT: {
1666 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1667 set_value_for_expression(value, new_value);
1670 case EXPR_UNARY_PREFIX_DECREMENT: {
1671 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1672 set_value_for_expression(value, new_value);
1676 panic("no incdec expr in create_incdec");
1681 static bool is_local_variable(expression_t *expression)
1683 if (expression->kind != EXPR_REFERENCE)
1685 reference_expression_t *ref_expr = &expression->reference;
1686 declaration_t *declaration = ref_expr->declaration;
1687 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1690 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1693 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1694 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1695 case EXPR_BINARY_NOTEQUAL:
1696 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1697 case EXPR_BINARY_ISLESS:
1698 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1699 case EXPR_BINARY_ISLESSEQUAL:
1700 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1701 case EXPR_BINARY_ISGREATER:
1702 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1703 case EXPR_BINARY_ISGREATEREQUAL:
1704 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1705 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1710 panic("trying to get pn_Cmp from non-comparison binexpr type");
1714 * Handle the assume optimizer hint: check if a Confirm
1715 * node can be created.
1717 * @param dbi debug info
1718 * @param expr the IL assume expression
1720 * we support here only some simple cases:
1725 static ir_node *handle_assume_compare(dbg_info *dbi,
1726 const binary_expression_t *expression)
1728 expression_t *op1 = expression->left;
1729 expression_t *op2 = expression->right;
1730 declaration_t *var2, *var = NULL;
1731 ir_node *res = NULL;
1734 cmp_val = get_pnc(expression->base.kind, op1->base.type);
1736 if (is_local_variable(op1) && is_local_variable(op2)) {
1737 var = op1->reference.declaration;
1738 var2 = op2->reference.declaration;
1740 type_t *const type = skip_typeref(var->type);
1741 ir_mode *const mode = get_ir_mode(type);
1743 ir_node *const irn1 = get_value(var->v.value_number, mode);
1744 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1746 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1747 set_value(var2->v.value_number, res);
1749 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1750 set_value(var->v.value_number, res);
1756 if (is_local_variable(op1) && is_constant_expression(op2)) {
1757 var = op1->reference.declaration;
1759 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1760 cmp_val = get_inversed_pnc(cmp_val);
1761 var = op2->reference.declaration;
1766 type_t *const type = skip_typeref(var->type);
1767 ir_mode *const mode = get_ir_mode(type);
1769 res = get_value(var->v.value_number, mode);
1770 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1771 set_value(var->v.value_number, res);
1777 * Handle the assume optimizer hint.
1779 * @param dbi debug info
1780 * @param expr the IL assume expression
1782 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1783 switch(expression->kind) {
1784 case EXPR_BINARY_EQUAL:
1785 case EXPR_BINARY_NOTEQUAL:
1786 case EXPR_BINARY_LESS:
1787 case EXPR_BINARY_LESSEQUAL:
1788 case EXPR_BINARY_GREATER:
1789 case EXPR_BINARY_GREATEREQUAL:
1790 return handle_assume_compare(dbi, &expression->binary);
1796 static ir_node *bitfield_extract_to_firm(const unary_expression_t *expression)
1798 expression_t *select = expression->value;
1799 assert(select->kind == EXPR_SELECT);
1801 type_t *type = select->base.type;
1802 assert(type->kind == TYPE_BITFIELD);
1803 ir_mode *mode = get_ir_mode(type->bitfield.base_type);
1804 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1805 ir_node *addr = expression_to_addr(select);
1806 ir_node *mem = get_store();
1807 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1808 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1809 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1811 load_res = create_conv(dbgi, load_res, mode_int);
1813 set_store(load_mem);
1815 /* kill upper bits */
1816 ir_entity *entity = select->select.compound_entry->v.entity;
1817 int bitoffset = get_entity_offset_bits_remainder(entity);
1818 ir_type *entity_type = get_entity_type(entity);
1819 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1820 long shift_bitsl = machine_size - bitoffset - bitsize;
1821 assert(shift_bitsl >= 0);
1822 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1823 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1824 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1826 long shift_bitsr = bitoffset + shift_bitsl;
1827 assert(shift_bitsr <= (long) machine_size);
1828 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1829 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1831 if(mode_is_signed(mode)) {
1832 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1834 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1837 return create_conv(dbgi, shiftr, mode);
1840 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1842 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1843 type_t *type = skip_typeref(expression->base.type);
1845 if(expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
1846 return expression_to_addr(expression->value);
1848 const expression_t *value = expression->value;
1850 switch(expression->base.kind) {
1851 case EXPR_UNARY_NEGATE: {
1852 ir_node *value_node = expression_to_firm(value);
1853 ir_mode *mode = get_ir_mode(type);
1854 return new_d_Minus(dbgi, value_node, mode);
1856 case EXPR_UNARY_PLUS:
1857 return expression_to_firm(value);
1858 case EXPR_UNARY_BITWISE_NEGATE: {
1859 ir_node *value_node = expression_to_firm(value);
1860 ir_mode *mode = get_ir_mode(type);
1861 return new_d_Not(dbgi, value_node, mode);
1863 case EXPR_UNARY_NOT: {
1864 ir_node *value_node = expression_to_firm(value);
1865 ir_mode *mode = get_ir_mode(type);
1866 if(get_irn_mode(value_node) != mode_b) {
1867 value_node = create_conv(dbgi, value_node, mode_b);
1869 value_node = new_d_Not(dbgi, value_node, mode_b);
1870 if(mode != mode_b) {
1871 value_node = create_conv(dbgi, value_node, mode);
1875 case EXPR_UNARY_DEREFERENCE: {
1876 ir_node *value_node = expression_to_firm(value);
1877 type_t *value_type = skip_typeref(value->base.type);
1878 assert(is_type_pointer(value_type));
1879 type_t *points_to = value_type->pointer.points_to;
1880 return deref_address(points_to, value_node, dbgi);
1882 case EXPR_UNARY_POSTFIX_INCREMENT:
1883 case EXPR_UNARY_POSTFIX_DECREMENT:
1884 case EXPR_UNARY_PREFIX_INCREMENT:
1885 case EXPR_UNARY_PREFIX_DECREMENT:
1886 return create_incdec(expression);
1887 case EXPR_UNARY_CAST: {
1888 ir_node *value_node = expression_to_firm(value);
1889 if(is_type_scalar(type)) {
1890 ir_mode *mode = get_ir_mode(type);
1891 ir_node *node = create_conv(dbgi, value_node, mode);
1892 node = do_strict_conv(dbgi, node);
1895 /* make sure firm type is constructed */
1896 (void) get_ir_type(type);
1900 case EXPR_UNARY_CAST_IMPLICIT: {
1901 ir_node *value_node = expression_to_firm(value);
1902 if(is_type_scalar(type)) {
1903 ir_mode *mode = get_ir_mode(type);
1904 return create_conv(dbgi, value_node, mode);
1909 case EXPR_UNARY_ASSUME:
1910 if(firm_opt.confirm)
1911 return handle_assume(dbgi, value);
1914 case EXPR_UNARY_BITFIELD_EXTRACT:
1915 return bitfield_extract_to_firm(expression);
1920 panic("invalid UNEXPR type found");
1923 static ir_node *produce_condition_result(const expression_t *expression,
1926 ir_mode *mode = get_ir_mode(expression->base.type);
1927 ir_node *cur_block = get_cur_block();
1929 ir_node *one_block = new_immBlock();
1930 ir_node *one = new_Const(mode, get_mode_one(mode));
1931 ir_node *jmp_one = new_d_Jmp(dbgi);
1933 ir_node *zero_block = new_immBlock();
1934 ir_node *zero = new_Const(mode, get_mode_null(mode));
1935 ir_node *jmp_zero = new_d_Jmp(dbgi);
1937 set_cur_block(cur_block);
1938 create_condition_evaluation(expression, one_block, zero_block);
1939 mature_immBlock(one_block);
1940 mature_immBlock(zero_block);
1942 ir_node *common_block = new_immBlock();
1943 add_immBlock_pred(common_block, jmp_one);
1944 add_immBlock_pred(common_block, jmp_zero);
1945 mature_immBlock(common_block);
1947 ir_node *in[2] = { one, zero };
1948 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1953 static ir_node *create_lazy_op(const binary_expression_t *expression)
1955 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1956 type_t *type = expression->base.type;
1957 ir_mode *mode = get_ir_mode(type);
1959 if(is_constant_expression(expression->left)) {
1960 long val = fold_constant(expression->left);
1961 expression_kind_t ekind = expression->base.kind;
1962 if((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
1963 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1964 return expression_to_firm(expression->right);
1966 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
1967 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
1968 return new_Const(mode, get_mode_one(mode));
1972 return produce_condition_result((const expression_t*) expression, dbgi);
1975 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1976 ir_node *right, ir_mode *mode);
1978 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1979 create_arithmetic_func func)
1981 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1982 ir_node *left = expression_to_firm(expression->left);
1983 ir_node *right = expression_to_firm(expression->right);
1984 type_t *type = expression->right->base.type;
1985 /* be careful with the modes, because in arithmetic assign nodes only
1986 * the right operand has the mode of the arithmetic already */
1987 ir_mode *mode = get_ir_mode(type);
1988 left = create_conv(dbgi, left, mode);
1989 ir_node *res = func(dbgi, left, right, mode);
1994 static ir_node *pointer_arithmetic(ir_node *const pointer,
1997 dbg_info *const dbgi,
1998 const create_arithmetic_func func)
2000 pointer_type_t *const pointer_type = &type->pointer;
2001 type_t *const points_to = pointer_type->points_to;
2002 const unsigned elem_size = get_type_size_const(points_to);
2004 assert(elem_size >= 1);
2005 if (elem_size > 1) {
2006 integer = create_conv(dbgi, integer, mode_int);
2007 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
2008 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
2012 ir_mode *const mode = get_ir_mode(type);
2013 return func(dbgi, pointer, integer, mode);
2016 static ir_node *create_arithmetic_assign_binop(
2017 const binary_expression_t *expression, create_arithmetic_func func)
2019 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2020 type_t *const type = skip_typeref(expression->base.type);
2023 if (is_type_pointer(type)) {
2024 ir_node *const pointer = expression_to_firm(expression->left);
2025 ir_node * integer = expression_to_firm(expression->right);
2026 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
2028 value = create_arithmetic_binop(expression, func);
2031 ir_mode *const mode = get_ir_mode(type);
2032 value = create_conv(dbgi, value, mode);
2033 set_value_for_expression(expression->left, value);
2038 static ir_node *create_add(const binary_expression_t *expression)
2040 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2041 ir_node *left = expression_to_firm(expression->left);
2042 ir_node *right = expression_to_firm(expression->right);
2043 type_t *type = expression->base.type;
2045 expression_t *expr_left = expression->left;
2046 expression_t *expr_right = expression->right;
2047 type_t *type_left = skip_typeref(expr_left->base.type);
2048 type_t *type_right = skip_typeref(expr_right->base.type);
2050 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2051 ir_mode *const mode = get_ir_mode(type);
2052 return new_d_Add(dbgi, left, right, mode);
2055 if (is_type_pointer(type_left)) {
2056 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
2058 assert(is_type_pointer(type_right));
2059 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
2063 static ir_node *create_sub(const binary_expression_t *expression)
2065 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2066 expression_t *const expr_left = expression->left;
2067 expression_t *const expr_right = expression->right;
2068 ir_node *const left = expression_to_firm(expr_left);
2069 ir_node *const right = expression_to_firm(expr_right);
2070 type_t *const type = expression->base.type;
2071 type_t *const type_left = skip_typeref(expr_left->base.type);
2072 type_t *const type_right = skip_typeref(expr_right->base.type);
2074 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2075 ir_mode *const mode = get_ir_mode(type);
2076 return new_d_Sub(dbgi, left, right, mode);
2077 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2078 const pointer_type_t *const ptr_type = &type_left->pointer;
2080 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2081 ir_mode *const mode = get_ir_mode(type);
2082 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2083 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2084 ir_node *const no_mem = new_NoMem();
2085 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size, mode,
2086 op_pin_state_floats);
2087 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2090 assert(is_type_pointer(type_left));
2091 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
2094 static ir_node *create_shift(const binary_expression_t *expression)
2096 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2097 ir_node *left = expression_to_firm(expression->left);
2098 ir_node *right = expression_to_firm(expression->right);
2099 type_t *type = expression->base.type;
2100 ir_mode *mode = get_ir_mode(type);
2102 /* firm always wants the shift count to be unsigned */
2103 right = create_conv(dbgi, right, mode_uint);
2107 switch(expression->base.kind) {
2108 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2109 case EXPR_BINARY_SHIFTLEFT:
2110 res = new_d_Shl(dbgi, left, right, mode);
2112 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2113 case EXPR_BINARY_SHIFTRIGHT: {
2114 expression_t *expr_left = expression->left;
2115 type_t *type_left = skip_typeref(expr_left->base.type);
2117 if(is_type_signed(type_left)) {
2118 res = new_d_Shrs(dbgi, left, right, mode);
2120 res = new_d_Shr(dbgi, left, right, mode);
2125 panic("create shift op called for non-shift op");
2132 static ir_node *create_divmod(const binary_expression_t *expression)
2134 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2135 ir_node *left = expression_to_firm(expression->left);
2136 ir_node *right = expression_to_firm(expression->right);
2137 ir_node *pin = new_Pin(new_NoMem());
2138 /* be careful with the modes, because in arithmetic assign nodes only
2139 * the right operand has the mode of the arithmetic already */
2140 type_t *type = expression->right->base.type;
2141 ir_mode *mode = get_ir_mode(type);
2142 left = create_conv(dbgi, left, mode);
2146 switch (expression->base.kind) {
2147 case EXPR_BINARY_DIV:
2148 case EXPR_BINARY_DIV_ASSIGN:
2149 if(mode_is_float(mode)) {
2150 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2151 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2153 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2154 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2158 case EXPR_BINARY_MOD:
2159 case EXPR_BINARY_MOD_ASSIGN:
2160 assert(!mode_is_float(mode));
2161 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
2162 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2165 default: panic("unexpected binary expression type in create_divmod()");
2171 static ir_node *create_arithmetic_assign_divmod(
2172 const binary_expression_t *expression)
2174 ir_node * value = create_divmod(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 assert(type->kind != TYPE_POINTER);
2181 value = create_conv(dbgi, value, mode);
2182 set_value_for_expression(expression->left, value);
2187 static ir_node *create_arithmetic_assign_shift(
2188 const binary_expression_t *expression)
2190 ir_node * value = create_shift(expression);
2191 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2192 type_t *const type = expression->base.type;
2193 ir_mode *const mode = get_ir_mode(type);
2195 value = create_conv(dbgi, value, mode);
2196 set_value_for_expression(expression->left, value);
2201 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2203 expression_kind_t kind = expression->base.kind;
2206 case EXPR_BINARY_EQUAL:
2207 case EXPR_BINARY_NOTEQUAL:
2208 case EXPR_BINARY_LESS:
2209 case EXPR_BINARY_LESSEQUAL:
2210 case EXPR_BINARY_GREATER:
2211 case EXPR_BINARY_GREATEREQUAL:
2212 case EXPR_BINARY_ISGREATER:
2213 case EXPR_BINARY_ISGREATEREQUAL:
2214 case EXPR_BINARY_ISLESS:
2215 case EXPR_BINARY_ISLESSEQUAL:
2216 case EXPR_BINARY_ISLESSGREATER:
2217 case EXPR_BINARY_ISUNORDERED: {
2218 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2219 ir_node *left = expression_to_firm(expression->left);
2220 ir_node *right = expression_to_firm(expression->right);
2221 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2222 long pnc = get_pnc(kind, expression->left->base.type);
2223 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2226 case EXPR_BINARY_ASSIGN: {
2227 ir_node *right = expression_to_firm(expression->right);
2228 set_value_for_expression(expression->left, right);
2232 case EXPR_BINARY_ADD:
2233 return create_add(expression);
2234 case EXPR_BINARY_SUB:
2235 return create_sub(expression);
2236 case EXPR_BINARY_MUL:
2237 return create_arithmetic_binop(expression, new_d_Mul);
2238 case EXPR_BINARY_BITWISE_AND:
2239 return create_arithmetic_binop(expression, new_d_And);
2240 case EXPR_BINARY_BITWISE_OR:
2241 return create_arithmetic_binop(expression, new_d_Or);
2242 case EXPR_BINARY_BITWISE_XOR:
2243 return create_arithmetic_binop(expression, new_d_Eor);
2244 case EXPR_BINARY_SHIFTLEFT:
2245 case EXPR_BINARY_SHIFTRIGHT:
2246 return create_shift(expression);
2247 case EXPR_BINARY_DIV:
2248 case EXPR_BINARY_MOD:
2249 return create_divmod(expression);
2250 case EXPR_BINARY_LOGICAL_AND:
2251 case EXPR_BINARY_LOGICAL_OR:
2252 return create_lazy_op(expression);
2253 case EXPR_BINARY_COMMA:
2254 expression_to_firm(expression->left);
2255 return expression_to_firm(expression->right);
2256 case EXPR_BINARY_ADD_ASSIGN:
2257 return create_arithmetic_assign_binop(expression, new_d_Add);
2258 case EXPR_BINARY_SUB_ASSIGN:
2259 return create_arithmetic_assign_binop(expression, new_d_Sub);
2260 case EXPR_BINARY_MUL_ASSIGN:
2261 return create_arithmetic_assign_binop(expression, new_d_Mul);
2262 case EXPR_BINARY_MOD_ASSIGN:
2263 case EXPR_BINARY_DIV_ASSIGN:
2264 return create_arithmetic_assign_divmod(expression);
2265 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2266 return create_arithmetic_assign_binop(expression, new_d_And);
2267 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2268 return create_arithmetic_assign_binop(expression, new_d_Or);
2269 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2270 return create_arithmetic_assign_binop(expression, new_d_Eor);
2271 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2272 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2273 return create_arithmetic_assign_shift(expression);
2274 case EXPR_BINARY_BUILTIN_EXPECT:
2275 return expression_to_firm(expression->left);
2277 panic("TODO binexpr type");
2281 static ir_node *array_access_addr(const array_access_expression_t *expression)
2283 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2284 ir_node *base_addr = expression_to_firm(expression->array_ref);
2285 ir_node *offset = expression_to_firm(expression->index);
2286 offset = create_conv(dbgi, offset, mode_uint);
2288 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2289 assert(is_type_pointer(ref_type));
2290 pointer_type_t *pointer_type = &ref_type->pointer;
2292 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2293 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2295 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2300 static ir_node *array_access_to_firm(
2301 const array_access_expression_t *expression)
2303 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2304 ir_node *addr = array_access_addr(expression);
2305 type_t *type = revert_automatic_type_conversion(
2306 (const expression_t*) expression);
2307 type = skip_typeref(type);
2309 return deref_address(type, addr, dbgi);
2312 static long get_offsetof_offset(const offsetof_expression_t *expression)
2314 type_t *orig_type = expression->type;
2317 designator_t *designator = expression->designator;
2318 for( ; designator != NULL; designator = designator->next) {
2319 type_t *type = skip_typeref(orig_type);
2320 /* be sure the type is constructed */
2321 (void) get_ir_type(type);
2323 if(designator->symbol != NULL) {
2324 assert(is_type_compound(type));
2325 symbol_t *symbol = designator->symbol;
2327 declaration_t *declaration = type->compound.declaration;
2328 declaration_t *iter = declaration->scope.declarations;
2329 for( ; iter != NULL; iter = iter->next) {
2330 if(iter->symbol == symbol) {
2334 assert(iter != NULL);
2336 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2337 offset += get_entity_offset(iter->v.entity);
2339 orig_type = iter->type;
2341 expression_t *array_index = designator->array_index;
2342 assert(designator->array_index != NULL);
2343 assert(is_type_array(type));
2344 assert(is_type_valid(array_index->base.type));
2346 long index = fold_constant(array_index);
2347 ir_type *arr_type = get_ir_type(type);
2348 ir_type *elem_type = get_array_element_type(arr_type);
2349 long elem_size = get_type_size_bytes(elem_type);
2351 offset += index * elem_size;
2353 orig_type = type->array.element_type;
2360 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2362 ir_mode *mode = get_ir_mode(expression->base.type);
2363 long offset = get_offsetof_offset(expression);
2364 tarval *tv = new_tarval_from_long(offset, mode);
2365 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2367 return new_d_Const(dbgi, mode, tv);
2370 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2371 ir_entity *entity, type_t *type);
2373 static ir_node *compound_literal_to_firm(
2374 const compound_literal_expression_t *expression)
2376 type_t *type = expression->type;
2378 /* create an entity on the stack */
2379 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2381 ident *const id = unique_ident("CompLit");
2382 ir_type *const irtype = get_ir_type(type);
2383 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2384 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2385 set_entity_ld_ident(entity, id);
2387 set_entity_variability(entity, variability_uninitialized);
2389 /* create initialisation code */
2390 initializer_t *initializer = expression->initializer;
2391 create_local_initializer(initializer, dbgi, entity, type);
2393 /* create a sel for the compound literal address */
2394 ir_node *frame = get_local_frame(entity);
2395 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2400 * Transform a sizeof expression into Firm code.
2402 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2404 type_t *type = expression->type;
2406 type = expression->tp_expression->base.type;
2407 assert(type != NULL);
2410 type = skip_typeref(type);
2411 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2412 if(is_type_array(type) && type->array.is_vla
2413 && expression->tp_expression != NULL) {
2414 expression_to_firm(expression->tp_expression);
2417 return get_type_size(type);
2421 * Transform an alignof expression into Firm code.
2423 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2425 type_t *type = expression->type;
2427 /* beware: if expression is a variable reference, return the
2428 alignment of the variable. */
2429 const expression_t *tp_expression = expression->tp_expression;
2430 const declaration_t *declaration = expr_is_variable(tp_expression);
2431 if (declaration != NULL) {
2432 /* TODO: get the alignment of this variable. */
2434 type = tp_expression->base.type;
2435 assert(type != NULL);
2438 ir_mode *const mode = get_ir_mode(expression->base.type);
2439 symconst_symbol sym;
2440 sym.type_p = get_ir_type(type);
2441 return new_SymConst(mode, sym, symconst_type_align);
2444 static void init_ir_types(void);
2445 long fold_constant(const expression_t *expression)
2449 assert(is_constant_expression(expression));
2451 ir_graph *old_current_ir_graph = current_ir_graph;
2452 if(current_ir_graph == NULL) {
2453 current_ir_graph = get_const_code_irg();
2456 ir_node *cnst = expression_to_firm(expression);
2457 current_ir_graph = old_current_ir_graph;
2459 if(!is_Const(cnst)) {
2460 panic("couldn't fold constant\n");
2463 tarval *tv = get_Const_tarval(cnst);
2464 if(!tarval_is_long(tv)) {
2465 panic("result of constant folding is not integer\n");
2468 return get_tarval_long(tv);
2471 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2473 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2475 /* first try to fold a constant condition */
2476 if(is_constant_expression(expression->condition)) {
2477 long val = fold_constant(expression->condition);
2479 return expression_to_firm(expression->true_expression);
2481 return expression_to_firm(expression->false_expression);
2485 ir_node *cur_block = get_cur_block();
2487 /* create the true block */
2488 ir_node *true_block = new_immBlock();
2490 ir_node *true_val = expression_to_firm(expression->true_expression);
2491 ir_node *true_jmp = new_Jmp();
2493 /* create the false block */
2494 ir_node *false_block = new_immBlock();
2496 ir_node *false_val = expression_to_firm(expression->false_expression);
2497 ir_node *false_jmp = new_Jmp();
2499 /* create the condition evaluation */
2500 set_cur_block(cur_block);
2501 create_condition_evaluation(expression->condition, true_block, false_block);
2502 mature_immBlock(true_block);
2503 mature_immBlock(false_block);
2505 /* create the common block */
2506 ir_node *common_block = new_immBlock();
2507 add_immBlock_pred(common_block, true_jmp);
2508 add_immBlock_pred(common_block, false_jmp);
2509 mature_immBlock(common_block);
2511 /* TODO improve static semantics, so either both or no values are NULL */
2512 if (true_val == NULL || false_val == NULL)
2515 ir_node *in[2] = { true_val, false_val };
2516 ir_mode *mode = get_irn_mode(true_val);
2517 assert(get_irn_mode(false_val) == mode);
2518 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2523 static ir_node *select_addr(const select_expression_t *expression)
2525 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2527 ir_node *compound_addr = expression_to_firm(expression->compound);
2529 declaration_t *entry = expression->compound_entry;
2530 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2531 ir_entity *entity = entry->v.entity;
2533 assert(entity != NULL);
2535 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2540 static ir_node *select_to_firm(const select_expression_t *expression)
2542 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2543 ir_node *addr = select_addr(expression);
2544 type_t *type = revert_automatic_type_conversion(
2545 (const expression_t*) expression);
2546 type = skip_typeref(type);
2548 return deref_address(type, addr, dbgi);
2551 /* Values returned by __builtin_classify_type. */
2552 typedef enum gcc_type_class
2558 enumeral_type_class,
2561 reference_type_class,
2565 function_type_class,
2576 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2578 const type_t *const type = skip_typeref(expr->type_expression->base.type);
2584 const atomic_type_t *const atomic_type = &type->atomic;
2585 switch (atomic_type->akind) {
2586 /* should not be reached */
2587 case ATOMIC_TYPE_INVALID:
2591 /* gcc cannot do that */
2592 case ATOMIC_TYPE_VOID:
2593 tc = void_type_class;
2596 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2597 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2598 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2599 case ATOMIC_TYPE_SHORT:
2600 case ATOMIC_TYPE_USHORT:
2601 case ATOMIC_TYPE_INT:
2602 case ATOMIC_TYPE_UINT:
2603 case ATOMIC_TYPE_LONG:
2604 case ATOMIC_TYPE_ULONG:
2605 case ATOMIC_TYPE_LONGLONG:
2606 case ATOMIC_TYPE_ULONGLONG:
2607 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2608 tc = integer_type_class;
2611 case ATOMIC_TYPE_FLOAT:
2612 case ATOMIC_TYPE_DOUBLE:
2613 case ATOMIC_TYPE_LONG_DOUBLE:
2614 tc = real_type_class;
2617 panic("Unexpected atomic type in classify_type_to_firm().");
2620 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
2621 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
2622 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
2623 case TYPE_ARRAY: /* gcc handles this as pointer */
2624 case TYPE_FUNCTION: /* gcc handles this as pointer */
2625 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
2626 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
2627 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
2629 /* gcc handles this as integer */
2630 case TYPE_ENUM: tc = integer_type_class; goto make_const;
2633 /* typedef/typeof should be skipped already */
2640 panic("unexpected TYPE classify_type_to_firm().");
2643 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2644 ir_mode *const mode = mode_int;
2645 tarval *const tv = new_tarval_from_long(tc, mode);
2646 return new_d_Const(dbgi, mode, tv);
2649 static ir_node *function_name_to_firm(
2650 const funcname_expression_t *const expr)
2652 switch(expr->kind) {
2653 case FUNCNAME_FUNCTION:
2654 case FUNCNAME_PRETTY_FUNCTION:
2655 case FUNCNAME_FUNCDNAME:
2656 if (current_function_name == NULL) {
2657 const source_position_t *const src_pos = &expr->base.source_position;
2658 const char *const name = current_function_decl->symbol->string;
2659 const string_t string = { name, strlen(name) + 1 };
2660 current_function_name = string_to_firm(src_pos, "__func__", &string);
2662 return current_function_name;
2663 case FUNCNAME_FUNCSIG:
2664 if (current_funcsig == NULL) {
2665 const source_position_t *const src_pos = &expr->base.source_position;
2666 ir_entity *ent = get_irg_entity(current_ir_graph);
2667 const char *const name = get_entity_ld_name(ent);
2668 const string_t string = { name, strlen(name) + 1 };
2669 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__", &string);
2671 return current_funcsig;
2673 panic("Unsupported function name");
2676 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2678 statement_t *statement = expr->statement;
2680 assert(statement->kind == STATEMENT_COMPOUND);
2681 return compound_statement_to_firm(&statement->compound);
2684 static ir_node *va_start_expression_to_firm(
2685 const va_start_expression_t *const expr)
2687 ir_type *const method_type = get_ir_type(current_function_decl->type);
2688 int const n = get_method_n_params(method_type) - 1;
2689 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2690 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2691 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2692 ir_node *const no_mem = new_NoMem();
2693 ir_node *const arg_sel =
2694 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2696 ir_node *const cnst = get_type_size(expr->parameter->type);
2697 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2698 set_value_for_expression(expr->ap, add);
2703 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2705 type_t *const type = expr->base.type;
2706 ir_node *const ap = expression_to_firm(expr->ap);
2707 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2708 ir_node *const res = deref_address(type, ap, dbgi);
2710 ir_node *const cnst = get_type_size(expr->base.type);
2711 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2712 set_value_for_expression(expr->ap, add);
2717 static ir_node *dereference_addr(const unary_expression_t *const expression)
2719 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2720 return expression_to_firm(expression->value);
2723 static ir_node *expression_to_addr(const expression_t *expression)
2725 switch(expression->kind) {
2726 case EXPR_REFERENCE:
2727 return reference_addr(&expression->reference);
2728 case EXPR_ARRAY_ACCESS:
2729 return array_access_addr(&expression->array_access);
2731 return select_addr(&expression->select);
2733 return call_expression_to_firm(&expression->call);
2734 case EXPR_UNARY_DEREFERENCE: {
2735 return dereference_addr(&expression->unary);
2740 panic("trying to get address of non-lvalue");
2743 static ir_node *builtin_constant_to_firm(
2744 const builtin_constant_expression_t *expression)
2746 ir_mode *mode = get_ir_mode(expression->base.type);
2749 if (is_constant_expression(expression->value)) {
2754 return new_Const_long(mode, v);
2757 static ir_node *builtin_prefetch_to_firm(
2758 const builtin_prefetch_expression_t *expression)
2760 ir_node *adr = expression_to_firm(expression->adr);
2761 /* no Firm support for prefetch yet */
2766 static ir_node *_expression_to_firm(const expression_t *expression)
2768 switch(expression->kind) {
2769 case EXPR_CHARACTER_CONSTANT:
2770 return character_constant_to_firm(&expression->conste);
2771 case EXPR_WIDE_CHARACTER_CONSTANT:
2772 return wide_character_constant_to_firm(&expression->conste);
2774 return const_to_firm(&expression->conste);
2775 case EXPR_STRING_LITERAL:
2776 return string_literal_to_firm(&expression->string);
2777 case EXPR_WIDE_STRING_LITERAL:
2778 return wide_string_literal_to_firm(&expression->wide_string);
2779 case EXPR_REFERENCE:
2780 return reference_expression_to_firm(&expression->reference);
2782 return call_expression_to_firm(&expression->call);
2784 return unary_expression_to_firm(&expression->unary);
2786 return binary_expression_to_firm(&expression->binary);
2787 case EXPR_ARRAY_ACCESS:
2788 return array_access_to_firm(&expression->array_access);
2790 return sizeof_to_firm(&expression->typeprop);
2792 return alignof_to_firm(&expression->typeprop);
2793 case EXPR_CONDITIONAL:
2794 return conditional_to_firm(&expression->conditional);
2796 return select_to_firm(&expression->select);
2797 case EXPR_CLASSIFY_TYPE:
2798 return classify_type_to_firm(&expression->classify_type);
2800 return function_name_to_firm(&expression->funcname);
2801 case EXPR_STATEMENT:
2802 return statement_expression_to_firm(&expression->statement);
2804 return va_start_expression_to_firm(&expression->va_starte);
2806 return va_arg_expression_to_firm(&expression->va_arge);
2807 case EXPR_BUILTIN_SYMBOL:
2808 panic("unimplemented expression found");
2809 case EXPR_BUILTIN_CONSTANT_P:
2810 return builtin_constant_to_firm(&expression->builtin_constant);
2811 case EXPR_BUILTIN_PREFETCH:
2812 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2814 return offsetof_to_firm(&expression->offsetofe);
2815 case EXPR_COMPOUND_LITERAL:
2816 return compound_literal_to_firm(&expression->compound_literal);
2822 panic("invalid expression found");
2825 static ir_node *expression_to_firm(const expression_t *expression)
2827 ir_node *res = _expression_to_firm(expression);
2829 if(res != NULL && get_irn_mode(res) == mode_b) {
2830 ir_mode *mode = get_ir_mode(expression->base.type);
2832 if(is_Const_null(res)) {
2833 return new_Const_long(mode, 0);
2835 assert(is_Const_one(res));
2836 return new_Const_long(mode, 1);
2840 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2841 return produce_condition_result(expression, dbgi);
2847 static ir_node *expression_to_modeb(const expression_t *expression)
2849 ir_node *res = _expression_to_firm(expression);
2850 res = create_conv(NULL, res, mode_b);
2856 * create a short-circuit expression evaluation that tries to construct
2857 * efficient control flow structures for &&, || and ! expressions
2859 static void create_condition_evaluation(const expression_t *expression,
2860 ir_node *true_block,
2861 ir_node *false_block)
2863 switch(expression->kind) {
2864 case EXPR_UNARY_NOT: {
2865 const unary_expression_t *unary_expression = &expression->unary;
2866 create_condition_evaluation(unary_expression->value, false_block,
2870 case EXPR_BINARY_LOGICAL_AND: {
2871 const binary_expression_t *binary_expression = &expression->binary;
2873 ir_node *cur_block = get_cur_block();
2874 ir_node *extra_block = new_immBlock();
2875 set_cur_block(cur_block);
2876 create_condition_evaluation(binary_expression->left, extra_block,
2878 mature_immBlock(extra_block);
2879 set_cur_block(extra_block);
2880 create_condition_evaluation(binary_expression->right, true_block,
2884 case EXPR_BINARY_LOGICAL_OR: {
2885 const binary_expression_t *binary_expression = &expression->binary;
2887 ir_node *cur_block = get_cur_block();
2888 ir_node *extra_block = new_immBlock();
2889 set_cur_block(cur_block);
2890 create_condition_evaluation(binary_expression->left, true_block,
2892 mature_immBlock(extra_block);
2893 set_cur_block(extra_block);
2894 create_condition_evaluation(binary_expression->right, true_block,
2902 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2903 ir_node *condition = expression_to_modeb(expression);
2904 ir_node *cond = new_d_Cond(dbgi, condition);
2905 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2906 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2908 /* set branch prediction info based on __builtin_expect */
2909 if(expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
2910 long cnst = fold_constant(expression->binary.right);
2911 cond_jmp_predicate pred;
2914 pred = COND_JMP_PRED_FALSE;
2916 pred = COND_JMP_PRED_TRUE;
2918 set_Cond_jmp_pred(cond, pred);
2921 add_immBlock_pred(true_block, true_proj);
2922 add_immBlock_pred(false_block, false_proj);
2924 set_cur_block(NULL);
2929 static void create_declaration_entity(declaration_t *declaration,
2930 declaration_kind_t declaration_kind,
2931 ir_type *parent_type)
2933 ident *const id = new_id_from_str(declaration->symbol->string);
2934 type_t *const type = skip_typeref(declaration->type);
2935 ir_type *const irtype = get_ir_type(type);
2936 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2937 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2938 mangle_ent_from_decl(entity, declaration);
2940 declaration->declaration_kind = (unsigned char) declaration_kind;
2941 declaration->v.entity = entity;
2942 set_entity_variability(entity, variability_uninitialized);
2943 if(parent_type == get_tls_type())
2944 set_entity_allocation(entity, allocation_automatic);
2945 else if(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
2946 set_entity_allocation(entity, allocation_static);
2948 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
2949 set_entity_volatility(entity, volatility_is_volatile);
2954 typedef struct type_path_entry_t type_path_entry_t;
2955 struct type_path_entry_t {
2957 ir_initializer_t *initializer;
2959 declaration_t *compound_entry;
2962 typedef struct type_path_t type_path_t;
2963 struct type_path_t {
2964 type_path_entry_t *path;
2969 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
2971 size_t len = ARR_LEN(path->path);
2973 for(size_t i = 0; i < len; ++i) {
2974 const type_path_entry_t *entry = & path->path[i];
2976 type_t *type = skip_typeref(entry->type);
2977 if(is_type_compound(type)) {
2978 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
2979 } else if(is_type_array(type)) {
2980 fprintf(stderr, "[%zd]", entry->index);
2982 fprintf(stderr, "-INVALID-");
2985 fprintf(stderr, " (");
2986 print_type(path->top_type);
2987 fprintf(stderr, ")");
2990 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2992 size_t len = ARR_LEN(path->path);
2994 return & path->path[len-1];
2997 static type_path_entry_t *append_to_type_path(type_path_t *path)
2999 size_t len = ARR_LEN(path->path);
3000 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3002 type_path_entry_t *result = & path->path[len];
3003 memset(result, 0, sizeof(result[0]));
3007 static size_t get_compound_size(const compound_type_t *type)
3009 declaration_t *declaration = type->declaration;
3010 declaration_t *member = declaration->scope.declarations;
3012 for( ; member != NULL; member = member->next) {
3015 /* TODO: cache results? */
3020 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3022 type_t *orig_top_type = path->top_type;
3023 type_t *top_type = skip_typeref(orig_top_type);
3025 assert(is_type_compound(top_type) || is_type_array(top_type));
3027 if(ARR_LEN(path->path) == 0) {
3030 type_path_entry_t *top = get_type_path_top(path);
3031 ir_initializer_t *initializer = top->initializer;
3032 return get_initializer_compound_value(initializer, top->index);
3036 static void descend_into_subtype(type_path_t *path)
3038 type_t *orig_top_type = path->top_type;
3039 type_t *top_type = skip_typeref(orig_top_type);
3041 assert(is_type_compound(top_type) || is_type_array(top_type));
3043 ir_initializer_t *initializer = get_initializer_entry(path);
3045 type_path_entry_t *top = append_to_type_path(path);
3046 top->type = top_type;
3050 if(is_type_compound(top_type)) {
3051 declaration_t *declaration = top_type->compound.declaration;
3052 declaration_t *entry = declaration->scope.declarations;
3054 top->compound_entry = entry;
3056 len = get_compound_size(&top_type->compound);
3058 path->top_type = entry->type;
3060 assert(is_type_array(top_type));
3061 assert(top_type->array.size > 0);
3064 path->top_type = top_type->array.element_type;
3065 len = top_type->array.size;
3067 if(initializer == NULL
3068 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3069 initializer = create_initializer_compound(len);
3070 /* we have to set the entry at the 2nd latest path entry... */
3071 size_t path_len = ARR_LEN(path->path);
3072 assert(path_len >= 1);
3074 type_path_entry_t *entry = & path->path[path_len-2];
3075 ir_initializer_t *tinitializer = entry->initializer;
3076 set_initializer_compound_value(tinitializer, entry->index,
3080 top->initializer = initializer;
3083 static void ascend_from_subtype(type_path_t *path)
3085 type_path_entry_t *top = get_type_path_top(path);
3087 path->top_type = top->type;
3089 size_t len = ARR_LEN(path->path);
3090 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3093 static void walk_designator(type_path_t *path, const designator_t *designator)
3095 /* designators start at current object type */
3096 ARR_RESIZE(type_path_entry_t, path->path, 1);
3098 for( ; designator != NULL; designator = designator->next) {
3099 type_path_entry_t *top = get_type_path_top(path);
3100 type_t *orig_type = top->type;
3101 type_t *type = skip_typeref(orig_type);
3103 if(designator->symbol != NULL) {
3104 assert(is_type_compound(type));
3106 symbol_t *symbol = designator->symbol;
3108 declaration_t *declaration = type->compound.declaration;
3109 declaration_t *iter = declaration->scope.declarations;
3110 for( ; iter != NULL; iter = iter->next, ++index) {
3111 if(iter->symbol == symbol) {
3115 assert(iter != NULL);
3117 top->type = orig_type;
3118 top->compound_entry = iter;
3120 orig_type = iter->type;
3122 expression_t *array_index = designator->array_index;
3123 assert(designator->array_index != NULL);
3124 assert(is_type_array(type));
3125 assert(is_type_valid(array_index->base.type));
3127 long index = fold_constant(array_index);
3130 if(type->array.size_constant == 1) {
3131 long array_size = type->array.size;
3132 assert(index < array_size);
3136 top->type = orig_type;
3137 top->index = (size_t) index;
3138 orig_type = type->array.element_type;
3140 path->top_type = orig_type;
3142 if(designator->next != NULL) {
3143 descend_into_subtype(path);
3147 path->invalid = false;
3150 static void advance_current_object(type_path_t *path)
3153 /* TODO: handle this... */
3154 panic("invalid initializer in ast2firm (excessive elements)");
3158 type_path_entry_t *top = get_type_path_top(path);
3160 type_t *type = skip_typeref(top->type);
3161 if(is_type_union(type)) {
3162 top->compound_entry = NULL;
3163 } else if(is_type_struct(type)) {
3164 declaration_t *entry = top->compound_entry;
3167 entry = entry->next;
3168 top->compound_entry = entry;
3170 path->top_type = entry->type;
3174 assert(is_type_array(type));
3177 if(!type->array.size_constant || top->index < type->array.size) {
3182 /* we're past the last member of the current sub-aggregate, try if we
3183 * can ascend in the type hierarchy and continue with another subobject */
3184 size_t len = ARR_LEN(path->path);
3187 ascend_from_subtype(path);
3188 advance_current_object(path);
3190 path->invalid = true;
3195 static ir_initializer_t *create_ir_initializer(
3196 const initializer_t *initializer, type_t *type);
3198 static ir_initializer_t *create_ir_initializer_value(
3199 const initializer_value_t *initializer)
3201 ir_node *value = expression_to_firm(initializer->value);
3202 return create_initializer_const(value);
3205 static ir_initializer_t *create_ir_initializer_list(
3206 const initializer_list_t *initializer, type_t *type)
3209 memset(&path, 0, sizeof(path));
3210 path.top_type = type;
3211 path.path = NEW_ARR_F(type_path_entry_t, 0);
3213 descend_into_subtype(&path);
3215 for(size_t i = 0; i < initializer->len; ++i) {
3216 const initializer_t *sub_initializer = initializer->initializers[i];
3218 if(sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3219 walk_designator(&path, sub_initializer->designator.designator);
3223 if(sub_initializer->kind == INITIALIZER_VALUE) {
3224 /* we might have to descend into types until we're at a scalar
3227 type_t *orig_top_type = path.top_type;
3228 type_t *top_type = skip_typeref(orig_top_type);
3230 if(is_type_scalar(top_type))
3232 descend_into_subtype(&path);
3236 ir_initializer_t *sub_irinitializer
3237 = create_ir_initializer(sub_initializer, path.top_type);
3239 size_t path_len = ARR_LEN(path.path);
3240 assert(path_len >= 1);
3241 type_path_entry_t *entry = & path.path[path_len-1];
3242 ir_initializer_t *tinitializer = entry->initializer;
3243 set_initializer_compound_value(tinitializer, entry->index,
3246 advance_current_object(&path);
3249 assert(ARR_LEN(path.path) >= 1);
3250 ir_initializer_t *result = path.path[0].initializer;
3251 DEL_ARR_F(path.path);
3256 static ir_initializer_t *create_ir_initializer_string(
3257 const initializer_string_t *initializer, type_t *type)
3259 type = skip_typeref(type);
3261 size_t string_len = initializer->string.size;
3262 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3263 size_t len = type->array.size;
3264 ir_initializer_t *irinitializer = create_initializer_compound(len);
3266 const char *string = initializer->string.begin;
3267 ir_mode *mode = get_type_mode(ir_type_const_char);
3269 for(size_t i = 0; i < len; ++i) {
3274 tarval *tv = new_tarval_from_long(string[i], mode);
3275 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3277 set_initializer_compound_value(irinitializer, i, char_initializer);
3280 return irinitializer;
3283 static ir_initializer_t *create_ir_initializer_wide_string(
3284 const initializer_wide_string_t *initializer, type_t *type)
3286 size_t string_len = initializer->string.size;
3287 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3288 size_t len = type->array.size;
3289 ir_initializer_t *irinitializer = create_initializer_compound(len);
3291 const wchar_rep_t *string = initializer->string.begin;
3292 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3294 for(size_t i = 0; i < len; ++i) {
3296 if(i < string_len) {
3299 tarval *tv = new_tarval_from_long(string[i], mode);
3300 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3302 set_initializer_compound_value(irinitializer, i, char_initializer);
3305 return irinitializer;
3308 static ir_initializer_t *create_ir_initializer(
3309 const initializer_t *initializer, type_t *type)
3311 switch(initializer->kind) {
3312 case INITIALIZER_STRING:
3313 return create_ir_initializer_string(&initializer->string, type);
3315 case INITIALIZER_WIDE_STRING:
3316 return create_ir_initializer_wide_string(&initializer->wide_string,
3319 case INITIALIZER_LIST:
3320 return create_ir_initializer_list(&initializer->list, type);
3322 case INITIALIZER_VALUE:
3323 return create_ir_initializer_value(&initializer->value);
3325 case INITIALIZER_DESIGNATOR:
3326 panic("unexpected designator initializer found");
3328 panic("unknown initializer");
3331 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
3334 ir_mode *mode = get_type_mode(type);
3335 tarval *zero = get_mode_null(mode);
3336 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3338 /* TODO: bitfields */
3339 ir_node *mem = get_store();
3340 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3341 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3345 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3346 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3348 switch(get_initializer_kind(initializer)) {
3349 case IR_INITIALIZER_NULL: {
3350 /* TODO: implement this for compound types... */
3351 assert(type != NULL);
3353 ir_mode *mode = get_type_mode(type);
3354 tarval *zero = get_mode_null(mode);
3355 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3357 /* TODO: bitfields */
3358 ir_node *mem = get_store();
3359 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3360 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3364 case IR_INITIALIZER_CONST: {
3365 ir_node *node = get_initializer_const_value(initializer);
3366 ir_mode *mode = get_irn_mode(node);
3367 assert(get_type_mode(type) == mode);
3369 /* TODO: bitfields... */
3370 ir_node *mem = get_store();
3371 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3372 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3376 case IR_INITIALIZER_TARVAL: {
3377 tarval *tv = get_initializer_tarval_value(initializer);
3378 ir_mode *mode = get_tarval_mode(tv);
3379 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3380 assert(get_type_mode(type) == mode);
3382 /* TODO: bitfields... */
3383 ir_node *mem = get_store();
3384 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3385 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3389 case IR_INITIALIZER_COMPOUND: {
3390 assert(is_compound_type(type));
3392 if(is_Array_type(type)) {
3393 assert(has_array_upper_bound(type, 0));
3394 n_members = get_array_upper_bound_int(type, 0);
3396 n_members = get_compound_n_members(type);
3399 if(get_initializer_compound_n_entries(initializer)
3400 != (unsigned) n_members)
3401 panic("initializer doesn't match compound type");
3403 for(int i = 0; i < n_members; ++i) {
3406 if(is_Array_type(type)) {
3407 ir_entity *entity = get_array_element_entity(type);
3408 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3409 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3410 ir_node *in[1] = { cnst };
3411 irtype = get_array_element_type(type);
3412 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3414 ir_entity *member = get_compound_member(type, i);
3416 irtype = get_entity_type(member);
3417 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3420 ir_initializer_t *sub_init
3421 = get_initializer_compound_value(initializer, i);
3423 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3429 panic("invalid IR_INITIALIZER found");
3432 static void create_dynamic_initializer(ir_initializer_t *initializer,
3433 dbg_info *dbgi, ir_entity *entity)
3435 ir_node *frame = get_local_frame(entity);
3436 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3437 ir_type *type = get_entity_type(entity);
3439 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3442 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3443 ir_entity *entity, type_t *type)
3445 ir_node *memory = get_store();
3446 ir_node *nomem = new_NoMem();
3447 ir_node *frame = get_irg_frame(current_ir_graph);
3448 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3450 if(initializer->kind == INITIALIZER_VALUE) {
3451 initializer_value_t *initializer_value = &initializer->value;
3453 ir_node *value = expression_to_firm(initializer_value->value);
3454 type = skip_typeref(type);
3455 assign_value(dbgi, addr, type, value);
3459 if(!is_constant_initializer(initializer)) {
3460 ir_initializer_t *irinitializer
3461 = create_ir_initializer(initializer, type);
3463 create_dynamic_initializer(irinitializer, dbgi, entity);
3467 /* create the ir_initializer */
3468 ir_graph *const old_current_ir_graph = current_ir_graph;
3469 current_ir_graph = get_const_code_irg();
3471 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3473 assert(current_ir_graph == get_const_code_irg());
3474 current_ir_graph = old_current_ir_graph;
3476 /* create a "template" entity which is copied to the entity on the stack */
3477 ident *const id = unique_ident("initializer");
3478 ir_type *const irtype = get_ir_type(type);
3479 ir_type *const global_type = get_glob_type();
3480 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3481 set_entity_ld_ident(init_entity, id);
3483 set_entity_variability(init_entity, variability_initialized);
3484 set_entity_visibility(init_entity, visibility_local);
3485 set_entity_allocation(init_entity, allocation_static);
3487 set_entity_initializer(init_entity, irinitializer);
3489 ir_node *const src_addr = create_global(dbgi, mode_P_data, init_entity);
3490 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3492 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3493 set_store(copyb_mem);
3496 static void create_initializer_local_variable_entity(declaration_t *declaration)
3498 initializer_t *initializer = declaration->init.initializer;
3499 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3500 ir_entity *entity = declaration->v.entity;
3501 type_t *type = declaration->type;
3502 create_local_initializer(initializer, dbgi, entity, type);
3505 static void create_declaration_initializer(declaration_t *declaration)
3507 initializer_t *initializer = declaration->init.initializer;
3508 if(initializer == NULL)
3511 declaration_kind_t declaration_kind
3512 = (declaration_kind_t) declaration->declaration_kind;
3513 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3514 create_initializer_local_variable_entity(declaration);
3518 if(initializer->kind == INITIALIZER_VALUE) {
3519 initializer_value_t *initializer_value = &initializer->value;
3521 ir_node *value = expression_to_firm(initializer_value->value);
3523 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3524 set_value(declaration->v.value_number, value);
3526 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3528 ir_entity *entity = declaration->v.entity;
3530 set_entity_variability(entity, variability_initialized);
3531 set_atomic_ent_value(entity, value);
3534 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3535 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3537 ir_entity *entity = declaration->v.entity;
3538 ir_initializer_t *irinitializer
3539 = create_ir_initializer(initializer, declaration->type);
3541 set_entity_variability(entity, variability_initialized);
3542 set_entity_initializer(entity, irinitializer);
3546 static void create_variable_length_array(declaration_t *declaration)
3548 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3549 type_t *type = declaration->type;
3550 ir_node *mem = get_store();
3551 ir_type *el_type = get_ir_type(type->array.element_type);
3553 /* make sure size_node is calculated */
3554 get_type_size(type);
3555 ir_node *elems = type->array.size_node;
3556 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3558 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3559 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3562 /* initializers are not allowed for VLAs */
3563 assert(declaration->init.initializer == NULL);
3565 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3566 declaration->v.vla_base = addr;
3568 /* TODO: record VLA somewhere so we create the free node when we leave
3573 * Creates a Firm local variable from a declaration.
3575 static void create_local_variable(declaration_t *declaration)
3577 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3579 bool needs_entity = declaration->address_taken;
3580 type_t *type = skip_typeref(declaration->type);
3582 /* is it a variable length array? */
3583 if(is_type_array(type) && !type->array.size_constant) {
3584 create_variable_length_array(declaration);
3586 } else if(is_type_array(type) || is_type_compound(type)) {
3587 needs_entity = true;
3588 } else if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3589 needs_entity = true;
3593 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3594 create_declaration_entity(declaration,
3595 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3598 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3599 declaration->v.value_number = next_value_number_function;
3600 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3601 ++next_value_number_function;
3604 create_declaration_initializer(declaration);
3607 static void create_local_static_variable(declaration_t *declaration)
3609 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3611 type_t *const type = skip_typeref(declaration->type);
3612 ir_type *const global_type = get_glob_type();
3613 ident *const id = unique_ident(declaration->symbol->string);
3614 ir_type *const irtype = get_ir_type(type);
3615 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3616 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3617 mangle_ent_from_decl(entity, declaration);
3619 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3620 set_entity_volatility(entity, volatility_is_volatile);
3623 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3624 declaration->v.entity = entity;
3625 set_entity_variability(entity, variability_uninitialized);
3626 set_entity_visibility(entity, visibility_local);
3627 set_entity_allocation(entity, allocation_static);
3629 ir_graph *const old_current_ir_graph = current_ir_graph;
3630 current_ir_graph = get_const_code_irg();
3632 create_declaration_initializer(declaration);
3634 assert(current_ir_graph == get_const_code_irg());
3635 current_ir_graph = old_current_ir_graph;
3640 static void return_statement_to_firm(return_statement_t *statement)
3642 if(get_cur_block() == NULL)
3645 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3646 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3651 if(get_method_n_ress(func_irtype) > 0) {
3652 ir_type *res_type = get_method_res_type(func_irtype, 0);
3654 if(statement->value != NULL) {
3655 ir_node *node = expression_to_firm(statement->value);
3656 node = do_strict_conv(dbgi, node);
3660 if(is_compound_type(res_type)) {
3663 mode = get_type_mode(res_type);
3665 in[0] = new_Unknown(mode);
3669 /* build return_value for its side effects */
3670 if(statement->value != NULL) {
3671 expression_to_firm(statement->value);
3676 ir_node *store = get_store();
3677 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3679 ir_node *end_block = get_irg_end_block(current_ir_graph);
3680 add_immBlock_pred(end_block, ret);
3682 set_cur_block(NULL);
3685 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3687 if(get_cur_block() == NULL)
3690 return expression_to_firm(statement->expression);
3693 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3695 ir_node *result = NULL;
3696 statement_t *statement = compound->statements;
3697 for( ; statement != NULL; statement = statement->base.next) {
3698 //context2firm(&statement->scope);
3700 if(statement->base.next == NULL
3701 && statement->kind == STATEMENT_EXPRESSION) {
3702 result = expression_statement_to_firm(
3703 &statement->expression);
3706 statement_to_firm(statement);
3712 static void create_global_variable(declaration_t *declaration)
3716 switch ((storage_class_tag_t)declaration->storage_class) {
3717 case STORAGE_CLASS_STATIC:
3718 vis = visibility_local;
3721 case STORAGE_CLASS_EXTERN:
3722 vis = visibility_external_allocated;
3725 case STORAGE_CLASS_NONE:
3726 vis = visibility_external_visible;
3729 case STORAGE_CLASS_THREAD:
3730 vis = visibility_external_visible;
3733 case STORAGE_CLASS_THREAD_EXTERN:
3734 vis = visibility_external_allocated;
3737 case STORAGE_CLASS_THREAD_STATIC:
3738 vis = visibility_local;
3742 var_type = get_tls_type();
3746 var_type = get_glob_type();
3750 create_declaration_entity(declaration,
3751 DECLARATION_KIND_GLOBAL_VARIABLE,
3753 set_entity_visibility(declaration->v.entity, vis);
3757 case STORAGE_CLASS_TYPEDEF:
3758 case STORAGE_CLASS_AUTO:
3759 case STORAGE_CLASS_REGISTER:
3760 case STORAGE_CLASS_ENUM_ENTRY:
3763 panic("Invalid storage class for global variable");
3766 static void create_local_declaration(declaration_t *declaration)
3768 if(declaration->symbol == NULL)
3771 type_t *type = skip_typeref(declaration->type);
3773 switch ((storage_class_tag_t) declaration->storage_class) {
3774 case STORAGE_CLASS_STATIC:
3775 create_local_static_variable(declaration);
3777 case STORAGE_CLASS_EXTERN:
3778 create_global_variable(declaration);
3779 create_declaration_initializer(declaration);
3781 case STORAGE_CLASS_NONE:
3782 case STORAGE_CLASS_AUTO:
3783 case STORAGE_CLASS_REGISTER:
3784 if(is_type_function(type)) {
3785 if(declaration->init.statement != NULL) {
3786 panic("nested functions not supported yet");
3788 get_function_entity(declaration);
3791 create_local_variable(declaration);
3794 case STORAGE_CLASS_ENUM_ENTRY:
3795 case STORAGE_CLASS_TYPEDEF:
3796 case STORAGE_CLASS_THREAD:
3797 case STORAGE_CLASS_THREAD_EXTERN:
3798 case STORAGE_CLASS_THREAD_STATIC:
3801 panic("invalid storage class found");
3804 static void declaration_statement_to_firm(declaration_statement_t *statement)
3806 declaration_t *declaration = statement->declarations_begin;
3807 declaration_t *end = statement->declarations_end->next;
3808 for( ; declaration != end; declaration = declaration->next) {
3809 if(declaration->namespc != NAMESPACE_NORMAL)
3811 create_local_declaration(declaration);
3815 static void if_statement_to_firm(if_statement_t *statement)
3817 ir_node *cur_block = get_cur_block();
3819 ir_node *fallthrough_block = new_immBlock();
3821 /* the true (blocks) */
3822 ir_node *true_block;
3823 if (statement->true_statement != NULL) {
3824 true_block = new_immBlock();
3825 statement_to_firm(statement->true_statement);
3826 if(get_cur_block() != NULL) {
3827 ir_node *jmp = new_Jmp();
3828 add_immBlock_pred(fallthrough_block, jmp);
3831 true_block = fallthrough_block;
3834 /* the false (blocks) */
3835 ir_node *false_block;
3836 if(statement->false_statement != NULL) {
3837 false_block = new_immBlock();
3839 statement_to_firm(statement->false_statement);
3840 if(get_cur_block() != NULL) {
3841 ir_node *jmp = new_Jmp();
3842 add_immBlock_pred(fallthrough_block, jmp);
3845 false_block = fallthrough_block;
3848 /* create the condition */
3849 if(cur_block != NULL) {
3850 set_cur_block(cur_block);
3851 create_condition_evaluation(statement->condition, true_block,
3855 mature_immBlock(true_block);
3856 if(false_block != fallthrough_block) {
3857 mature_immBlock(false_block);
3859 mature_immBlock(fallthrough_block);
3861 set_cur_block(fallthrough_block);
3864 static void while_statement_to_firm(while_statement_t *statement)
3866 ir_node *jmp = NULL;
3867 if(get_cur_block() != NULL) {
3871 /* create the header block */
3872 ir_node *header_block = new_immBlock();
3874 add_immBlock_pred(header_block, jmp);
3877 /* the false block */
3878 ir_node *false_block = new_immBlock();
3881 ir_node *body_block;
3882 if (statement->body != NULL) {
3883 ir_node *old_continue_label = continue_label;
3884 ir_node *old_break_label = break_label;
3885 continue_label = header_block;
3886 break_label = false_block;
3888 body_block = new_immBlock();
3889 statement_to_firm(statement->body);
3891 assert(continue_label == header_block);
3892 assert(break_label == false_block);
3893 continue_label = old_continue_label;
3894 break_label = old_break_label;
3896 if(get_cur_block() != NULL) {
3898 add_immBlock_pred(header_block, jmp);
3901 body_block = header_block;
3904 /* create the condition */
3905 set_cur_block(header_block);
3907 create_condition_evaluation(statement->condition, body_block, false_block);
3908 mature_immBlock(body_block);
3909 mature_immBlock(false_block);
3910 mature_immBlock(header_block);
3912 set_cur_block(false_block);
3915 static void do_while_statement_to_firm(do_while_statement_t *statement)
3917 ir_node *jmp = NULL;
3918 if(get_cur_block() != NULL) {
3922 /* create the header block */
3923 ir_node *header_block = new_immBlock();
3925 /* the false block */
3926 ir_node *false_block = new_immBlock();
3929 ir_node *body_block = new_immBlock();
3931 add_immBlock_pred(body_block, jmp);
3934 if (statement->body != NULL) {
3935 ir_node *old_continue_label = continue_label;
3936 ir_node *old_break_label = break_label;
3937 continue_label = header_block;
3938 break_label = false_block;
3940 statement_to_firm(statement->body);
3942 assert(continue_label == header_block);
3943 assert(break_label == false_block);
3944 continue_label = old_continue_label;
3945 break_label = old_break_label;
3947 if (get_cur_block() == NULL) {
3948 mature_immBlock(header_block);
3949 mature_immBlock(body_block);
3950 mature_immBlock(false_block);
3955 ir_node *body_jmp = new_Jmp();
3956 add_immBlock_pred(header_block, body_jmp);
3957 mature_immBlock(header_block);
3959 /* create the condition */
3960 set_cur_block(header_block);
3962 create_condition_evaluation(statement->condition, body_block, false_block);
3963 mature_immBlock(body_block);
3964 mature_immBlock(false_block);
3965 mature_immBlock(header_block);
3967 set_cur_block(false_block);
3970 static void for_statement_to_firm(for_statement_t *statement)
3972 ir_node *jmp = NULL;
3973 if (get_cur_block() != NULL) {
3974 if(statement->initialisation != NULL) {
3975 expression_to_firm(statement->initialisation);
3978 /* create declarations */
3979 declaration_t *declaration = statement->scope.declarations;
3980 for( ; declaration != NULL; declaration = declaration->next) {
3981 create_local_declaration(declaration);
3988 /* create the step block */
3989 ir_node *const step_block = new_immBlock();
3990 if (statement->step != NULL) {
3991 expression_to_firm(statement->step);
3993 ir_node *const step_jmp = new_Jmp();
3995 /* create the header block */
3996 ir_node *const header_block = new_immBlock();
3998 add_immBlock_pred(header_block, jmp);
4000 add_immBlock_pred(header_block, step_jmp);
4002 /* the false block */
4003 ir_node *const false_block = new_immBlock();
4006 ir_node * body_block;
4007 if (statement->body != NULL) {
4008 ir_node *const old_continue_label = continue_label;
4009 ir_node *const old_break_label = break_label;
4010 continue_label = step_block;
4011 break_label = false_block;
4013 body_block = new_immBlock();
4014 statement_to_firm(statement->body);
4016 assert(continue_label == step_block);
4017 assert(break_label == false_block);
4018 continue_label = old_continue_label;
4019 break_label = old_break_label;
4021 if (get_cur_block() != NULL) {
4023 add_immBlock_pred(step_block, jmp);
4026 body_block = step_block;
4029 /* create the condition */
4030 set_cur_block(header_block);
4031 if (statement->condition != NULL) {
4032 create_condition_evaluation(statement->condition, body_block,
4035 keep_alive(header_block);
4037 add_immBlock_pred(body_block, jmp);
4040 mature_immBlock(body_block);
4041 mature_immBlock(false_block);
4042 mature_immBlock(step_block);
4043 mature_immBlock(header_block);
4044 mature_immBlock(false_block);
4046 set_cur_block(false_block);
4049 static void create_jump_statement(const statement_t *statement,
4050 ir_node *target_block)
4052 if(get_cur_block() == NULL)
4055 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4056 ir_node *jump = new_d_Jmp(dbgi);
4057 add_immBlock_pred(target_block, jump);
4059 set_cur_block(NULL);
4062 static void switch_statement_to_firm(const switch_statement_t *statement)
4064 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4066 ir_node *expression = expression_to_firm(statement->expression);
4067 ir_node *cond = new_d_Cond(dbgi, expression);
4068 ir_node *break_block = new_immBlock();
4070 set_cur_block(NULL);
4072 ir_node *const old_switch_cond = current_switch_cond;
4073 ir_node *const old_break_label = break_label;
4074 const bool old_saw_default_label = saw_default_label;
4075 current_switch_cond = cond;
4076 break_label = break_block;
4078 if (statement->body != NULL) {
4079 statement_to_firm(statement->body);
4082 if(get_cur_block() != NULL) {
4083 ir_node *jmp = new_Jmp();
4084 add_immBlock_pred(break_block, jmp);
4087 if (!saw_default_label) {
4088 set_cur_block(get_nodes_block(cond));
4089 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4090 MAGIC_DEFAULT_PN_NUMBER);
4091 add_immBlock_pred(break_block, proj);
4094 assert(current_switch_cond == cond);
4095 assert(break_label == break_block);
4096 current_switch_cond = old_switch_cond;
4097 break_label = old_break_label;
4098 saw_default_label = old_saw_default_label;
4100 mature_immBlock(break_block);
4101 set_cur_block(break_block);
4104 static void case_label_to_firm(const case_label_statement_t *statement)
4106 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4108 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4110 /* let's create a node and hope firm constant folding creates a Const
4113 ir_node *old_block = get_nodes_block(current_switch_cond);
4114 ir_node *block = new_immBlock();
4116 set_cur_block(old_block);
4117 if(statement->expression != NULL) {
4118 long start_pn = fold_constant(statement->expression);
4119 long end_pn = start_pn;
4120 if (statement->end_range != NULL) {
4121 end_pn = fold_constant(statement->end_range);
4123 assert(start_pn <= end_pn);
4124 /* create jumps for all cases in the given range */
4125 for (long pn = start_pn; pn <= end_pn; ++pn) {
4126 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
4127 /* oops someone detected our cheating... */
4128 panic("magic default pn used");
4130 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4131 add_immBlock_pred(block, proj);
4134 saw_default_label = true;
4135 proj = new_d_defaultProj(dbgi, current_switch_cond,
4136 MAGIC_DEFAULT_PN_NUMBER);
4138 add_immBlock_pred(block, proj);
4141 if (fallthrough != NULL) {
4142 add_immBlock_pred(block, fallthrough);
4144 mature_immBlock(block);
4145 set_cur_block(block);
4147 if(statement->statement != NULL) {
4148 statement_to_firm(statement->statement);
4152 static ir_node *get_label_block(declaration_t *label)
4154 assert(label->namespc == NAMESPACE_LABEL);
4156 if(label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
4157 return label->v.block;
4159 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
4161 ir_node *old_cur_block = get_cur_block();
4162 ir_node *block = new_immBlock();
4163 set_cur_block(old_cur_block);
4165 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
4166 label->v.block = block;
4168 ARR_APP1(ir_node *, imature_blocks, block);
4173 static void label_to_firm(const label_statement_t *statement)
4175 ir_node *block = get_label_block(statement->label);
4177 if(get_cur_block() != NULL) {
4178 ir_node *jmp = new_Jmp();
4179 add_immBlock_pred(block, jmp);
4182 set_cur_block(block);
4185 if(statement->statement != NULL) {
4186 statement_to_firm(statement->statement);
4190 static void goto_to_firm(const goto_statement_t *statement)
4192 if(get_cur_block() == NULL)
4195 ir_node *block = get_label_block(statement->label);
4196 ir_node *jmp = new_Jmp();
4197 add_immBlock_pred(block, jmp);
4199 set_cur_block(NULL);
4202 typedef enum modifier_t {
4203 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
4204 ASM_MODIFIER_READ_WRITE = 1 << 1,
4205 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
4206 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
4209 static void asm_statement_to_firm(const asm_statement_t *statement)
4212 fprintf(stderr, "WARNING asm not implemented yet!\n");
4214 bool needs_memory = false;
4216 size_t n_clobbers = 0;
4217 asm_clobber_t *clobber = statement->clobbers;
4218 for( ; clobber != NULL; clobber = clobber->next) {
4219 if(strcmp(clobber->clobber, "memory") == 0) {
4220 needs_memory = true;
4224 ident *id = new_id_from_str(clobber->clobber);
4225 obstack_ptr_grow(&asm_obst, id);
4228 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4229 ident **clobbers = NULL;
4230 if(n_clobbers > 0) {
4231 clobbers = obstack_finish(&asm_obst);
4234 /* find and count input and output constraints */
4235 asm_constraint_t *constraint = statement->inputs;
4236 for( ; constraint != NULL; constraint = constraint->next) {
4238 bool supports_memop = false;
4239 for(const char *c = constraint->constraints; *c != 0; ++c) {
4240 /* TODO: improve error messages */
4244 panic("multiple alternative assembler constraints not "
4252 supports_memop = true;
4253 obstack_1grow(&asm_obst, *c);
4256 if(modifiers & ASM_MODIFIER_READ_WRITE)
4257 panic("inconsistent register constraints");
4258 modifiers |= ASM_MODIFIER_WRITE_ONLY;
4261 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
4262 panic("inconsistent register constraints");
4263 modifiers |= ASM_MODIFIER_READ_WRITE;
4266 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
4267 panic("early clobber assembler constraint not supported yet");
4270 modifiers |= ASM_MODIFIER_COMMUTATIVE;
4271 panic("commutative assembler constraint not supported yet");
4274 /* skip register preferences stuff... */
4275 while(*c != 0 && *c != ',')
4279 /* skip register preferences stuff... */
4283 obstack_1grow(&asm_obst, *c);
4287 obstack_1grow(&asm_obst, '\0');
4288 const char *constraint_string = obstack_finish(&asm_obst);
4290 needs_memory |= supports_memop;
4291 if(supports_memop) {
4298 static void ms_try_statement_to_firm(ms_try_statement_t *statement) {
4299 statement_to_firm(statement->try_statement);
4300 warningf(&statement->base.source_position, "structured exception handling ignored");
4303 static void leave_statement_to_firm(leave_statement_t *statement) {
4304 errorf(&statement->base.source_position, "__leave not supported yet");
4307 static void statement_to_firm(statement_t *statement)
4309 switch(statement->kind) {
4310 case STATEMENT_INVALID:
4311 panic("invalid statement found");
4313 case STATEMENT_EMPTY:
4316 case STATEMENT_COMPOUND:
4317 compound_statement_to_firm(&statement->compound);
4319 case STATEMENT_RETURN:
4320 return_statement_to_firm(&statement->returns);
4322 case STATEMENT_EXPRESSION:
4323 expression_statement_to_firm(&statement->expression);
4326 if_statement_to_firm(&statement->ifs);
4328 case STATEMENT_WHILE:
4329 while_statement_to_firm(&statement->whiles);
4331 case STATEMENT_DO_WHILE:
4332 do_while_statement_to_firm(&statement->do_while);
4334 case STATEMENT_DECLARATION:
4335 declaration_statement_to_firm(&statement->declaration);
4337 case STATEMENT_BREAK:
4338 create_jump_statement(statement, break_label);
4340 case STATEMENT_CONTINUE:
4341 create_jump_statement(statement, continue_label);
4343 case STATEMENT_SWITCH:
4344 switch_statement_to_firm(&statement->switchs);
4346 case STATEMENT_CASE_LABEL:
4347 case_label_to_firm(&statement->case_label);
4350 for_statement_to_firm(&statement->fors);
4352 case STATEMENT_LABEL:
4353 label_to_firm(&statement->label);
4355 case STATEMENT_GOTO:
4356 goto_to_firm(&statement->gotos);
4359 asm_statement_to_firm(&statement->asms);
4361 case STATEMENT_MS_TRY:
4362 ms_try_statement_to_firm(&statement->ms_try);
4364 case STATEMENT_LEAVE:
4365 leave_statement_to_firm(&statement->leave);
4368 panic("Statement not implemented\n");
4371 static int count_decls_in_expression(const expression_t *expression);
4373 static int count_local_declarations(const declaration_t * decl,
4374 const declaration_t *const end)
4377 for (; decl != end; decl = decl->next) {
4378 if(decl->namespc != NAMESPACE_NORMAL)
4380 const type_t *type = skip_typeref(decl->type);
4381 if (!decl->address_taken && is_type_scalar(type))
4383 const initializer_t *initializer = decl->init.initializer;
4384 /* FIXME: should walk initializer hierarchies... */
4385 if(initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4386 count += count_decls_in_expression(initializer->value.value);
4392 static int count_decls_in_expression(const expression_t *expression) {
4395 if(expression == NULL)
4398 switch((expression_kind_t) expression->base.kind) {
4399 case EXPR_STATEMENT:
4400 return count_decls_in_stmts(expression->statement.statement);
4402 int count_left = count_decls_in_expression(expression->binary.left);
4403 int count_right = count_decls_in_expression(expression->binary.right);
4404 return count_left + count_right;
4407 return count_decls_in_expression(expression->unary.value);
4409 call_argument_t *argument = expression->call.arguments;
4410 for( ; argument != NULL; argument = argument->next) {
4411 count += count_decls_in_expression(argument->expression);
4418 panic("unexpected expression kind");
4420 case EXPR_COMPOUND_LITERAL:
4424 case EXPR_CONDITIONAL:
4425 count += count_decls_in_expression(expression->conditional.condition);
4426 count += count_decls_in_expression(expression->conditional.true_expression);
4427 count += count_decls_in_expression(expression->conditional.false_expression);
4430 case EXPR_BUILTIN_PREFETCH:
4431 count += count_decls_in_expression(expression->builtin_prefetch.adr);
4432 count += count_decls_in_expression(expression->builtin_prefetch.rw);
4433 count += count_decls_in_expression(expression->builtin_prefetch.locality);
4436 case EXPR_BUILTIN_CONSTANT_P:
4437 count += count_decls_in_expression(expression->builtin_constant.value);
4441 count += count_decls_in_expression(expression->select.compound);
4444 case EXPR_ARRAY_ACCESS:
4445 count += count_decls_in_expression(expression->array_access.array_ref);
4446 count += count_decls_in_expression(expression->array_access.index);
4449 case EXPR_CLASSIFY_TYPE:
4450 count += count_decls_in_expression(expression->classify_type.type_expression);
4454 case EXPR_ALIGNOF: {
4455 expression_t *tp_expression = expression->typeprop.tp_expression;
4456 if (tp_expression != NULL) {
4457 count += count_decls_in_expression(tp_expression);
4463 case EXPR_REFERENCE:
4465 case EXPR_CHARACTER_CONSTANT:
4466 case EXPR_WIDE_CHARACTER_CONSTANT:
4467 case EXPR_STRING_LITERAL:
4468 case EXPR_WIDE_STRING_LITERAL:
4470 case EXPR_BUILTIN_SYMBOL:
4476 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
4477 * (or implement all the missing expressions here/implement a walker)
4483 static int count_decls_in_stmts(const statement_t *stmt)
4486 for (; stmt != NULL; stmt = stmt->base.next) {
4487 switch (stmt->kind) {
4488 case STATEMENT_EMPTY:
4491 case STATEMENT_DECLARATION: {
4492 const declaration_statement_t *const decl_stmt = &stmt->declaration;
4493 count += count_local_declarations(decl_stmt->declarations_begin,
4494 decl_stmt->declarations_end->next);
4498 case STATEMENT_COMPOUND: {
4499 const compound_statement_t *const comp =
4501 count += count_decls_in_stmts(comp->statements);
4505 case STATEMENT_IF: {
4506 const if_statement_t *const if_stmt = &stmt->ifs;
4507 count += count_decls_in_expression(if_stmt->condition);
4508 count += count_decls_in_stmts(if_stmt->true_statement);
4509 count += count_decls_in_stmts(if_stmt->false_statement);
4513 case STATEMENT_SWITCH: {
4514 const switch_statement_t *const switch_stmt = &stmt->switchs;
4515 count += count_decls_in_expression(switch_stmt->expression);
4516 count += count_decls_in_stmts(switch_stmt->body);
4520 case STATEMENT_LABEL: {
4521 const label_statement_t *const label_stmt = &stmt->label;
4522 if(label_stmt->statement != NULL) {
4523 count += count_decls_in_stmts(label_stmt->statement);
4528 case STATEMENT_WHILE: {
4529 const while_statement_t *const while_stmt = &stmt->whiles;
4530 count += count_decls_in_expression(while_stmt->condition);
4531 count += count_decls_in_stmts(while_stmt->body);
4535 case STATEMENT_DO_WHILE: {
4536 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
4537 count += count_decls_in_expression(do_while_stmt->condition);
4538 count += count_decls_in_stmts(do_while_stmt->body);
4542 case STATEMENT_FOR: {
4543 const for_statement_t *const for_stmt = &stmt->fors;
4544 count += count_local_declarations(for_stmt->scope.declarations, NULL);
4545 count += count_decls_in_expression(for_stmt->initialisation);
4546 count += count_decls_in_expression(for_stmt->condition);
4547 count += count_decls_in_expression(for_stmt->step);
4548 count += count_decls_in_stmts(for_stmt->body);
4552 case STATEMENT_CASE_LABEL: {
4553 const case_label_statement_t *label = &stmt->case_label;
4554 count += count_decls_in_expression(label->expression);
4555 if(label->statement != NULL) {
4556 count += count_decls_in_stmts(label->statement);
4562 case STATEMENT_BREAK:
4563 case STATEMENT_CONTINUE:
4566 case STATEMENT_EXPRESSION: {
4567 const expression_statement_t *expr_stmt = &stmt->expression;
4568 count += count_decls_in_expression(expr_stmt->expression);
4572 case STATEMENT_GOTO:
4573 case STATEMENT_LEAVE:
4574 case STATEMENT_INVALID:
4577 case STATEMENT_RETURN: {
4578 const return_statement_t *ret_stmt = &stmt->returns;
4579 count += count_decls_in_expression(ret_stmt->value);
4583 case STATEMENT_MS_TRY: {
4584 const ms_try_statement_t *const try_stmt = &stmt->ms_try;
4585 count += count_decls_in_stmts(try_stmt->try_statement);
4586 if(try_stmt->except_expression != NULL)
4587 count += count_decls_in_expression(try_stmt->except_expression);
4588 count += count_decls_in_stmts(try_stmt->final_statement);
4596 static int get_function_n_local_vars(declaration_t *declaration)
4600 /* count parameters */
4601 count += count_local_declarations(declaration->scope.declarations, NULL);
4603 /* count local variables declared in body */
4604 count += count_decls_in_stmts(declaration->init.statement);
4609 static void initialize_function_parameters(declaration_t *declaration)
4611 ir_graph *irg = current_ir_graph;
4612 ir_node *args = get_irg_args(irg);
4613 ir_node *start_block = get_irg_start_block(irg);
4614 ir_type *function_irtype = get_ir_type(declaration->type);
4617 declaration_t *parameter = declaration->scope.declarations;
4618 for( ; parameter != NULL; parameter = parameter->next, ++n) {
4619 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
4620 type_t *type = skip_typeref(parameter->type);
4622 bool needs_entity = parameter->address_taken;
4623 assert(!is_type_array(type));
4624 if(is_type_compound(type)) {
4625 needs_entity = true;
4629 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
4630 ident *id = new_id_from_str(parameter->symbol->string);
4631 set_entity_ident(entity, id);
4633 parameter->declaration_kind
4634 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
4635 parameter->v.entity = entity;
4639 ir_mode *mode = get_ir_mode(parameter->type);
4641 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
4643 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
4644 parameter->v.value_number = next_value_number_function;
4645 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
4646 ++next_value_number_function;
4648 set_value(parameter->v.value_number, proj);
4653 * Handle additional decl modifiers for IR-graphs
4655 * @param irg the IR-graph
4656 * @param dec_modifiers additional modifiers
4658 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
4660 if (decl_modifiers & DM_NORETURN) {
4661 /* TRUE if the declaration includes the Microsoft
4662 __declspec(noreturn) specifier. */
4663 set_irg_additional_property(irg, mtp_property_noreturn);
4665 if (decl_modifiers & DM_NOTHROW) {
4666 /* TRUE if the declaration includes the Microsoft
4667 __declspec(nothrow) specifier. */
4668 set_irg_additional_property(irg, mtp_property_nothrow);
4670 if (decl_modifiers & DM_NAKED) {
4671 /* TRUE if the declaration includes the Microsoft
4672 __declspec(naked) specifier. */
4673 set_irg_additional_property(irg, mtp_property_naked);
4675 if (decl_modifiers & DM_FORCEINLINE) {
4676 /* TRUE if the declaration includes the
4677 Microsoft __forceinline specifier. */
4678 set_irg_inline_property(irg, irg_inline_forced);
4680 if (decl_modifiers & DM_NOINLINE) {
4681 /* TRUE if the declaration includes the Microsoft
4682 __declspec(noinline) specifier. */
4683 set_irg_inline_property(irg, irg_inline_forbidden);
4687 static void create_function(declaration_t *declaration)
4689 ir_entity *function_entity = get_function_entity(declaration);
4691 if(declaration->init.statement == NULL)
4694 current_function_decl = declaration;
4695 current_function_name = NULL;
4696 current_funcsig = NULL;
4698 assert(imature_blocks == NULL);
4699 imature_blocks = NEW_ARR_F(ir_node*, 0);
4701 int n_local_vars = get_function_n_local_vars(declaration);
4702 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
4703 ir_node *first_block = get_cur_block();
4705 /* set inline flags */
4706 if (declaration->is_inline)
4707 set_irg_inline_property(irg, irg_inline_recomended);
4708 handle_decl_modifier_irg(irg, declaration->decl_modifiers);
4710 next_value_number_function = 0;
4711 initialize_function_parameters(declaration);
4713 statement_to_firm(declaration->init.statement);
4715 ir_node *end_block = get_irg_end_block(irg);
4717 /* do we have a return statement yet? */
4718 if(get_cur_block() != NULL) {
4719 type_t *type = skip_typeref(declaration->type);
4720 assert(is_type_function(type));
4721 const function_type_t *func_type = &type->function;
4722 const type_t *return_type
4723 = skip_typeref(func_type->return_type);
4726 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4727 ret = new_Return(get_store(), 0, NULL);
4730 if(is_type_scalar(return_type)) {
4731 mode = get_ir_mode(func_type->return_type);
4737 /* ยง5.1.2.2.3 main implicitly returns 0 */
4738 if (strcmp(declaration->symbol->string, "main") == 0) {
4739 in[0] = new_Const(mode, get_mode_null(mode));
4741 in[0] = new_Unknown(mode);
4743 ret = new_Return(get_store(), 1, in);
4745 add_immBlock_pred(end_block, ret);
4748 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
4749 mature_immBlock(imature_blocks[i]);
4751 DEL_ARR_F(imature_blocks);
4752 imature_blocks = NULL;
4754 mature_immBlock(first_block);
4755 mature_immBlock(end_block);
4757 irg_finalize_cons(irg);
4759 /* finalize the frame type */
4760 ir_type *frame_type = get_irg_frame_type(irg);
4761 int n = get_compound_n_members(frame_type);
4764 for(int i = 0; i < n; ++i) {
4765 ir_entity *entity = get_compound_member(frame_type, i);
4766 ir_type *entity_type = get_entity_type(entity);
4768 int align = get_type_alignment_bytes(entity_type);
4769 if(align > align_all)
4773 misalign = offset % align;
4775 offset += align - misalign;
4779 set_entity_offset(entity, offset);
4780 offset += get_type_size_bytes(entity_type);
4782 set_type_size_bytes(frame_type, offset);
4783 set_type_alignment_bytes(frame_type, align_all);
4788 static void scope_to_firm(scope_t *scope)
4790 /* first pass: create declarations */
4791 declaration_t *declaration = scope->declarations;
4792 for( ; declaration != NULL; declaration = declaration->next) {
4793 if(declaration->namespc != NAMESPACE_NORMAL)
4795 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4796 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4798 if(declaration->symbol == NULL)
4801 type_t *type = skip_typeref(declaration->type);
4802 if(is_type_function(type)) {
4803 get_function_entity(declaration);
4805 create_global_variable(declaration);
4809 /* second pass: create code/initializers */
4810 declaration = scope->declarations;
4811 for( ; declaration != NULL; declaration = declaration->next) {
4812 if(declaration->namespc != NAMESPACE_NORMAL)
4814 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4815 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4817 if(declaration->symbol == NULL)
4820 type_t *type = declaration->type;
4821 if(type->kind == TYPE_FUNCTION) {
4822 create_function(declaration);
4824 assert(declaration->declaration_kind
4825 == DECLARATION_KIND_GLOBAL_VARIABLE);
4826 current_ir_graph = get_const_code_irg();
4827 create_declaration_initializer(declaration);
4832 void init_ast2firm(void)
4834 obstack_init(&asm_obst);
4835 init_atomic_modes();
4837 /* create idents for all known runtime functions */
4838 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
4839 predef_idents[rts_data[i].id] = new_id_from_str(rts_data[i].name);
4843 static void init_ir_types(void)
4845 static int ir_types_initialized = 0;
4846 if(ir_types_initialized)
4848 ir_types_initialized = 1;
4850 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4851 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4852 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4854 ir_type_int = get_ir_type(type_int);
4855 ir_type_const_char = get_ir_type(type_const_char);
4856 ir_type_wchar_t = get_ir_type(type_wchar_t);
4857 ir_type_void = get_ir_type(type_void);
4860 void exit_ast2firm(void)
4862 obstack_free(&asm_obst, NULL);
4865 void translation_unit_to_firm(translation_unit_t *unit)
4867 /* just to be sure */
4868 continue_label = NULL;
4870 current_switch_cond = NULL;
4874 scope_to_firm(&unit->scope);