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;
63 static struct obstack asm_obst;
65 typedef enum declaration_kind_t {
66 DECLARATION_KIND_UNKNOWN,
67 DECLARATION_KIND_FUNCTION,
68 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
69 DECLARATION_KIND_GLOBAL_VARIABLE,
70 DECLARATION_KIND_LOCAL_VARIABLE,
71 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
72 DECLARATION_KIND_COMPOUND_MEMBER,
73 DECLARATION_KIND_LABEL_BLOCK,
74 DECLARATION_KIND_ENUM_ENTRY
77 static ir_type *get_ir_type(type_t *type);
78 static int count_decls_in_stmts(const statement_t *stmt);
80 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
82 const declaration_t *declaration = get_irg_loc_description(irg, pos);
84 warningf(declaration->source_position,
85 "variable '%#T' might be used uninitialized",
86 declaration->type, declaration->symbol);
87 return new_r_Unknown(irg, mode);
90 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
92 const source_position_t *pos = (const source_position_t*) dbg;
95 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
99 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
101 const source_position_t *pos = (const source_position_t*) dbg;
106 return pos->input_name;
109 static dbg_info *get_dbg_info(const source_position_t *pos)
111 return (dbg_info*) pos;
114 static unsigned unique_id = 0;
116 static ident *unique_ident(const char *tag)
120 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
122 return new_id_from_str(buf);
126 * Return the signed integer mode of size bytes.
128 * @param size the size
130 static ir_mode *get_smode(unsigned size)
132 static ir_mode *s_modes[16 + 1] = {0, };
135 if (size <= 0 || size > 16)
144 snprintf(name, sizeof(name), "i%u", bits);
145 res = new_ir_mode(name, irms_int_number, bits, 1, irma_twos_complement,
146 bits <= machine_size ? machine_size : bits );
154 * Return the unsigned integer mode of size bytes.
156 * @param size the size
158 static ir_mode *get_umode(unsigned size)
160 static ir_mode *u_modes[16 + 1] = {0, };
163 if (size <= 0 || size > 16)
172 snprintf(name, sizeof(name), "u%u", bits);
173 res = new_ir_mode(name, irms_int_number, bits, 0, irma_twos_complement,
174 bits <= machine_size ? machine_size : bits );
182 * Return the pointer mode of size bytes.
184 * @param size the size
186 static ir_mode *get_ptrmode(unsigned size, char *name)
188 static ir_mode *p_modes[16 + 1] = {0, };
191 if (size <= 0 || size > 16)
201 snprintf(buf, sizeof(buf), "p%u", bits);
204 res = new_ir_mode(name, irms_reference, bits, 0, irma_twos_complement,
205 bits <= machine_size ? machine_size : bits);
209 set_reference_mode_signed_eq(res, get_smode(size));
210 set_reference_mode_unsigned_eq(res, get_umode(size));
215 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST];
217 static ir_mode *mode_int, *mode_uint;
219 static ir_node *expression_to_firm(const expression_t *expression);
220 static inline ir_mode *get_ir_mode(type_t *type);
223 * Initialises the atomic modes depending on the machine size.
225 static void init_atomic_modes(void) {
226 unsigned int_size = machine_size < 32 ? 2 : 4;
227 unsigned long_size = machine_size < 64 ? 4 : 8;
228 unsigned llong_size = machine_size < 32 ? 4 : 8;
230 /* firm has no real void... */
231 _atomic_modes[ATOMIC_TYPE_VOID] = mode_T;
232 _atomic_modes[ATOMIC_TYPE_CHAR] = char_is_signed ? get_smode(1) : get_umode(1);
233 _atomic_modes[ATOMIC_TYPE_SCHAR] = get_smode(1);
234 _atomic_modes[ATOMIC_TYPE_UCHAR] = get_umode(1);
235 _atomic_modes[ATOMIC_TYPE_SHORT] = get_smode(2);
236 _atomic_modes[ATOMIC_TYPE_USHORT] = get_umode(2);
237 _atomic_modes[ATOMIC_TYPE_INT] = get_smode(int_size);
238 _atomic_modes[ATOMIC_TYPE_UINT] = get_umode(int_size);
239 _atomic_modes[ATOMIC_TYPE_LONG] = get_smode(long_size);
240 _atomic_modes[ATOMIC_TYPE_ULONG] = get_umode(long_size);
241 _atomic_modes[ATOMIC_TYPE_LONGLONG] = get_smode(llong_size);
242 _atomic_modes[ATOMIC_TYPE_ULONGLONG] = get_umode(llong_size);
243 _atomic_modes[ATOMIC_TYPE_FLOAT] = mode_F;
244 _atomic_modes[ATOMIC_TYPE_DOUBLE] = mode_D;
245 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = mode_E;
246 _atomic_modes[ATOMIC_TYPE_BOOL] = get_umode(int_size);
248 #ifdef PROVIDE_COMPLEX
249 _atomic_modes[ATOMIC_TYPE_BOOL] = _atomic_modes[ATOMIC_TYPE_INT];
250 _atomic_modes[ATOMIC_TYPE_FLOAT_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_FLOAT];
251 _atomic_modes[ATOMIC_TYPE_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_DOUBLE];
252 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE];
255 /* Hmm, pointers should be machine size */
256 set_modeP_data(get_ptrmode(machine_size >> 3, NULL));
257 set_modeP_code(get_ptrmode(machine_size >> 3, NULL));
259 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
260 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
263 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
266 if ((unsigned)atomic_type->akind < (unsigned)ATOMIC_TYPE_LAST)
267 res = _atomic_modes[(unsigned)atomic_type->akind];
269 panic("Encountered unknown atomic type");
273 static unsigned get_atomic_type_size(const atomic_type_t *type)
275 switch(type->akind) {
276 case ATOMIC_TYPE_CHAR:
277 case ATOMIC_TYPE_SCHAR:
278 case ATOMIC_TYPE_UCHAR:
281 case ATOMIC_TYPE_SHORT:
282 case ATOMIC_TYPE_USHORT:
285 case ATOMIC_TYPE_BOOL:
286 case ATOMIC_TYPE_INT:
287 case ATOMIC_TYPE_UINT:
288 return machine_size >> 3;
290 case ATOMIC_TYPE_LONG:
291 case ATOMIC_TYPE_ULONG:
292 return machine_size > 16 ? machine_size >> 3 : 4;
294 case ATOMIC_TYPE_LONGLONG:
295 case ATOMIC_TYPE_ULONGLONG:
296 return machine_size > 16 ? 8 : 4;
298 case ATOMIC_TYPE_FLOAT:
301 case ATOMIC_TYPE_DOUBLE:
304 case ATOMIC_TYPE_LONG_DOUBLE:
307 case ATOMIC_TYPE_VOID:
310 case ATOMIC_TYPE_INVALID:
311 case ATOMIC_TYPE_LAST:
314 panic("Trying to determine size of invalid atomic type");
317 static unsigned get_compound_type_size(compound_type_t *type)
319 ir_type *irtype = get_ir_type((type_t*) type);
320 return get_type_size_bytes(irtype);
323 static unsigned get_array_type_size(array_type_t *type)
325 assert(!type->is_vla);
326 ir_type *irtype = get_ir_type((type_t*) type);
327 return get_type_size_bytes(irtype);
331 static unsigned get_type_size_const(type_t *type)
333 type = skip_typeref(type);
337 panic("error type occured");
339 return get_atomic_type_size(&type->atomic);
341 return get_mode_size_bytes(mode_int);
342 case TYPE_COMPOUND_UNION:
343 case TYPE_COMPOUND_STRUCT:
344 return get_compound_type_size(&type->compound);
346 /* just a pointer to the function */
347 return get_mode_size_bytes(mode_P_code);
349 return get_mode_size_bytes(mode_P_data);
351 return get_array_type_size(&type->array);
353 return get_type_size_const(type->builtin.real_type);
355 panic("type size of bitfield request");
361 panic("Trying to determine size of invalid type");
364 static ir_node *get_type_size(type_t *type)
366 type = skip_typeref(type);
368 if(is_type_array(type) && type->array.is_vla) {
369 ir_node *size_node = type->array.size_node;
370 if(size_node == NULL) {
371 size_node = expression_to_firm(type->array.size_expression);
372 assert(!is_Const(size_node));
373 type->array.size_node = size_node;
376 ir_node *elem_size = get_type_size(type->array.element_type);
377 ir_mode *mode = get_irn_mode(size_node);
378 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
382 ir_mode *mode = get_ir_mode(type_size_t);
384 sym.type_p = get_ir_type(type);
385 return new_SymConst(mode, sym, symconst_type_size);
388 static unsigned count_parameters(const function_type_t *function_type)
392 function_parameter_t *parameter = function_type->parameters;
393 for ( ; parameter != NULL; parameter = parameter->next) {
401 static ir_type *create_atomic_type(const atomic_type_t *type)
403 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
404 ir_mode *mode = get_atomic_mode(type);
405 ident *id = get_mode_ident(mode);
406 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
408 /* TODO: this is x86 specific, we should fiddle this into
409 * lang_features.h somehow... */
410 if(type->akind == ATOMIC_TYPE_LONG_DOUBLE
411 || type->akind == ATOMIC_TYPE_DOUBLE
412 || type->akind == ATOMIC_TYPE_LONGLONG
413 || type->akind == ATOMIC_TYPE_ULONGLONG) {
414 set_type_alignment_bytes(irtype, 4);
420 static ir_type *create_method_type(const function_type_t *function_type)
422 type_t *return_type = function_type->return_type;
424 ident *id = unique_ident("functiontype");
425 int n_parameters = count_parameters(function_type);
426 int n_results = return_type == type_void ? 0 : 1;
427 dbg_info *dbgi = get_dbg_info(&function_type->type.source_position);
428 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
430 if(return_type != type_void) {
431 ir_type *restype = get_ir_type(return_type);
432 set_method_res_type(irtype, 0, restype);
435 function_parameter_t *parameter = function_type->parameters;
437 for( ; parameter != NULL; parameter = parameter->next) {
438 ir_type *p_irtype = get_ir_type(parameter->type);
439 set_method_param_type(irtype, n, p_irtype);
443 if(function_type->variadic || function_type->unspecified_parameters) {
444 set_method_variadicity(irtype, variadicity_variadic);
450 static ir_type *create_pointer_type(pointer_type_t *type)
452 type_t *points_to = type->points_to;
453 ir_type *ir_points_to;
454 /* Avoid endless recursion if the points_to type contains this poiner type
455 * again (might be a struct). We therefore first create a void* pointer
456 * and then set the real points_to type
458 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
459 ir_type *ir_type = new_d_type_pointer(unique_ident("pointer"),
460 ir_type_void, mode_P_data, dbgi);
461 type->type.firm_type = ir_type;
463 ir_points_to = get_ir_type(points_to);
464 set_pointer_points_to_type(ir_type, ir_points_to);
469 static ir_type *create_array_type(array_type_t *type)
471 type_t *element_type = type->element_type;
472 ir_type *ir_element_type = get_ir_type(element_type);
474 ident *id = unique_ident("array");
475 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
476 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
478 const int align = get_type_alignment_bytes(ir_element_type);
479 set_type_alignment_bytes(ir_type, align);
481 if(type->size_constant) {
482 int n_elements = type->size;
484 set_array_bounds_int(ir_type, 0, 0, n_elements);
486 size_t elemsize = get_type_size_bytes(ir_element_type);
487 if(elemsize % align > 0) {
488 elemsize += align - (elemsize % align);
490 set_type_size_bytes(ir_type, n_elements * elemsize);
492 set_array_lower_bound_int(ir_type, 0, 0);
494 set_type_state(ir_type, layout_fixed);
500 * Return the signed integer type of size bits.
502 * @param size the size
504 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
507 static ir_mode *s_modes[64 + 1] = {NULL, };
511 if (size <= 0 || size > 64)
514 mode = s_modes[size];
518 snprintf(name, sizeof(name), "bf_I%u", size);
519 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
520 size <= 32 ? 32 : size );
521 s_modes[size] = mode;
525 snprintf(name, sizeof(name), "I%u", size);
526 ident *id = new_id_from_str(name);
527 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
528 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
529 set_primitive_base_type(res, base_tp);
535 * Return the unsigned integer type of size bits.
537 * @param size the size
539 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
542 static ir_mode *u_modes[64 + 1] = {NULL, };
546 if (size <= 0 || size > 64)
549 mode = u_modes[size];
553 snprintf(name, sizeof(name), "bf_U%u", size);
554 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
555 size <= 32 ? 32 : size );
556 u_modes[size] = mode;
561 snprintf(name, sizeof(name), "U%u", size);
562 ident *id = new_id_from_str(name);
563 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
564 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
565 set_primitive_base_type(res, base_tp);
570 static ir_type *create_bitfield_type(bitfield_type_t *const type)
572 type_t *base = skip_typeref(type->base);
573 assert(base->kind == TYPE_ATOMIC);
574 ir_type *irbase = get_ir_type(base);
576 unsigned size = fold_constant(type->size);
578 assert(!is_type_float(base));
579 if(is_type_signed(base)) {
580 return get_signed_int_type_for_bit_size(irbase, size);
582 return get_unsigned_int_type_for_bit_size(irbase, size);
586 #define INVALID_TYPE ((ir_type_ptr)-1)
588 static ir_type *create_union_type(compound_type_t *type, ir_type *irtype,
589 size_t *outer_offset, size_t *outer_align);
591 static ir_type *create_struct_type(compound_type_t *type, ir_type *irtype,
592 size_t *outer_offset, size_t *outer_align)
594 declaration_t *declaration = type->declaration;
595 if(declaration->v.irtype != NULL) {
596 return declaration->v.irtype;
599 size_t align_all = 1;
601 size_t bit_offset = 0;
603 symbol_t *symbol = declaration->symbol;
606 id = unique_ident(symbol->string);
608 id = unique_ident("__anonymous_struct");
610 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
612 irtype = new_d_type_struct(id, dbgi);
614 declaration->v.irtype = irtype;
615 type->type.firm_type = irtype;
617 offset = *outer_offset;
618 align_all = *outer_align;
621 declaration_t *entry = declaration->scope.declarations;
622 for( ; entry != NULL; entry = entry->next) {
623 if(entry->namespc != NAMESPACE_NORMAL)
626 symbol_t *symbol = entry->symbol;
627 type_t *entry_type = skip_typeref(entry->type);
628 dbg_info *dbgi = get_dbg_info(&entry->source_position);
631 ident = new_id_from_str(symbol->string);
633 if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
634 create_struct_type(&entry_type->compound, irtype, &offset,
637 } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
638 create_union_type(&entry_type->compound, irtype, &offset,
642 assert(entry_type->kind == TYPE_BITFIELD);
644 ident = unique_ident("anon");
647 ir_type *base_irtype;
648 if(entry_type->kind == TYPE_BITFIELD) {
649 base_irtype = get_ir_type(entry_type->bitfield.base);
651 base_irtype = get_ir_type(entry_type);
654 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
655 size_t misalign = offset % entry_alignment;
657 ir_type *entry_irtype = get_ir_type(entry_type);
658 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
661 size_t bits_remainder;
662 if(entry_type->kind == TYPE_BITFIELD) {
663 size_t size_bits = fold_constant(entry_type->bitfield.size);
664 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
666 if(size_bits > rest_size_bits) {
667 /* start a new bucket */
668 offset += entry_alignment - misalign;
674 /* put into current bucket */
675 base = offset - misalign;
676 bits_remainder = misalign * 8 + bit_offset;
679 offset += size_bits / 8;
680 bit_offset = bit_offset + (size_bits % 8);
682 size_t entry_size = get_type_size_bytes(base_irtype);
683 if(misalign > 0 || bit_offset > 0)
684 offset += entry_alignment - misalign;
688 offset += entry_size;
692 if(entry_alignment > align_all) {
693 if(entry_alignment % align_all != 0) {
694 panic("uneven alignments not supported yet");
696 align_all = entry_alignment;
699 set_entity_offset(entity, base);
700 set_entity_offset_bits_remainder(entity,
701 (unsigned char) bits_remainder);
702 //add_struct_member(irtype, entity);
703 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
704 assert(entry->v.entity == NULL);
705 entry->v.entity = entity;
708 size_t misalign = offset % align_all;
709 if(misalign > 0 || bit_offset > 0) {
710 offset += align_all - misalign;
713 if(outer_offset != NULL) {
714 *outer_offset = offset;
715 *outer_align = align_all;
717 set_type_alignment_bytes(irtype, align_all);
718 set_type_size_bytes(irtype, offset);
719 set_type_state(irtype, layout_fixed);
725 static ir_type *create_union_type(compound_type_t *type, ir_type *irtype,
726 size_t *outer_offset, size_t *outer_align)
728 declaration_t *declaration = type->declaration;
729 if(declaration->v.irtype != NULL) {
730 return declaration->v.irtype;
733 size_t align_all = 1;
738 symbol_t *symbol = declaration->symbol;
741 id = unique_ident(symbol->string);
743 id = unique_ident("__anonymous_union");
745 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
747 irtype = new_d_type_union(id, dbgi);
749 declaration->v.irtype = irtype;
750 type->type.firm_type = irtype;
752 offset = *outer_offset;
753 align_all = *outer_align;
756 type->type.firm_type = irtype;
758 declaration_t *entry = declaration->scope.declarations;
759 for( ; entry != NULL; entry = entry->next) {
760 if(entry->namespc != NAMESPACE_NORMAL)
763 symbol_t *symbol = entry->symbol;
764 type_t *entry_type = skip_typeref(entry->type);
765 ir_type *entry_ir_type = get_ir_type(entry_type);
769 ident = new_id_from_str(entry->symbol->string);
771 size_t offs = offset;
772 if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
773 create_struct_type(&entry_type->compound, irtype, &offs,
776 } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
777 create_union_type(&entry_type->compound, irtype, &offs,
781 panic("anonymous union member must be struct or union");
783 size_t entry_size = offs - offset;
784 if(entry_size > size) {
787 ident = unique_ident("anon");
790 size_t entry_size = get_type_size_bytes(entry_ir_type);
791 size_t entry_alignment = get_type_alignment_bytes(entry_ir_type);
793 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
794 ir_entity *const entity = new_d_entity(irtype, ident, entry_ir_type,
796 //add_union_member(irtype, entity);
797 set_entity_offset(entity, 0);
798 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
799 assert(entry->v.entity == NULL);
800 entry->v.entity = entity;
802 if(entry_size > size) {
805 if(entry_alignment > align_all) {
806 if(entry_alignment % align_all != 0) {
807 panic("Uneven alignments not supported yet");
809 align_all = entry_alignment;
813 if(outer_offset != NULL) {
814 assert(*outer_offset == offset);
816 size_t misalign = offset % align_all;
818 size += align_all - misalign;
819 *outer_offset += size;
821 if(align_all > *outer_align) {
822 if(align_all % *outer_align != 0) {
823 panic("uneven alignments not supported yet");
825 *outer_align = align_all;
828 set_type_alignment_bytes(irtype, align_all);
829 set_type_size_bytes(irtype, size);
830 set_type_state(irtype, layout_fixed);
831 declaration->v.irtype = irtype;
837 static ir_type *create_enum_type(enum_type_t *const type)
839 type->type.firm_type = ir_type_int;
841 ir_mode *const mode = mode_int;
842 tarval *const one = get_mode_one(mode);
843 tarval * tv_next = get_tarval_null(mode);
845 declaration_t *declaration = type->declaration->next;
846 for (; declaration != NULL; declaration = declaration->next) {
847 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
850 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
852 expression_t *const init = declaration->init.enum_value;
854 ir_node *const cnst = expression_to_firm(init);
855 if (!is_Const(cnst)) {
856 panic("couldn't fold constant");
858 tv_next = get_Const_tarval(cnst);
860 declaration->v.enum_val = tv_next;
861 tv_next = tarval_add(tv_next, one);
867 static ir_type *get_ir_type(type_t *type)
869 assert(type != NULL);
871 type = skip_typeref(type);
873 if(type->base.firm_type != NULL) {
874 assert(type->base.firm_type != INVALID_TYPE);
875 return type->base.firm_type;
878 ir_type *firm_type = NULL;
881 panic("error type occured");
883 firm_type = create_atomic_type(&type->atomic);
886 firm_type = create_method_type(&type->function);
889 firm_type = create_pointer_type(&type->pointer);
892 firm_type = create_array_type(&type->array);
894 case TYPE_COMPOUND_STRUCT:
895 firm_type = create_struct_type(&type->compound, NULL, NULL, NULL);
897 case TYPE_COMPOUND_UNION:
898 firm_type = create_union_type(&type->compound, NULL, NULL, NULL);
901 firm_type = create_enum_type(&type->enumt);
904 firm_type = get_ir_type(type->builtin.real_type);
907 firm_type = create_bitfield_type(&type->bitfield);
915 if(firm_type == NULL)
916 panic("unknown type found");
918 type->base.firm_type = firm_type;
922 static inline ir_mode *get_ir_mode(type_t *type)
924 ir_type *irtype = get_ir_type(type);
926 /* firm doesn't report a mode for arrays somehow... */
927 if(is_Array_type(irtype)) {
931 ir_mode *mode = get_type_mode(irtype);
932 assert(mode != NULL);
936 static ident *predef_idents[rts_max];
938 /** Names of the runtime functions. */
939 static const struct {
940 int id; /**< the rts id */
941 int n_res; /**< number of return values */
942 const char *name; /**< the name of the rts function */
943 int n_params; /**< number of parameters */
944 unsigned flags; /**< language flags */
946 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
947 { rts_abort, 0, "abort", 0, _C89 },
948 { rts_abs, 1, "abs", 1, _C89 },
949 { rts_labs, 1, "labs", 1, _C89 },
950 { rts_llabs, 1, "llabs", 1, _C99 },
951 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
953 { rts_fabs, 1, "fabs", 1, _C89 },
954 { rts_sqrt, 1, "sqrt", 1, _C89 },
955 { rts_cbrt, 1, "cbrt", 1, _C99 },
956 { rts_exp, 1, "exp", 1, _C89 },
957 { rts_exp2, 1, "exp2", 1, _C89 },
958 { rts_exp10, 1, "exp10", 1, _GNUC },
959 { rts_log, 1, "log", 1, _C89 },
960 { rts_log2, 1, "log2", 1, _C89 },
961 { rts_log10, 1, "log10", 1, _C89 },
962 { rts_pow, 1, "pow", 2, _C89 },
963 { rts_sin, 1, "sin", 1, _C89 },
964 { rts_cos, 1, "cos", 1, _C89 },
965 { rts_tan, 1, "tan", 1, _C89 },
966 { rts_asin, 1, "asin", 1, _C89 },
967 { rts_acos, 1, "acos", 1, _C89 },
968 { rts_atan, 1, "atan", 1, _C89 },
969 { rts_sinh, 1, "sinh", 1, _C89 },
970 { rts_cosh, 1, "cosh", 1, _C89 },
971 { rts_tanh, 1, "tanh", 1, _C89 },
973 { rts_fabsf, 1, "fabsf", 1, _C99 },
974 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
975 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
976 { rts_expf, 1, "expf", 1, _C99 },
977 { rts_exp2f, 1, "exp2f", 1, _C99 },
978 { rts_exp10f, 1, "exp10f", 1, _GNUC },
979 { rts_logf, 1, "logf", 1, _C99 },
980 { rts_log2f, 1, "log2f", 1, _C99 },
981 { rts_log10f, 1, "log10f", 1, _C99 },
982 { rts_powf, 1, "powf", 2, _C99 },
983 { rts_sinf, 1, "sinf", 1, _C99 },
984 { rts_cosf, 1, "cosf", 1, _C99 },
985 { rts_tanf, 1, "tanf", 1, _C99 },
986 { rts_asinf, 1, "asinf", 1, _C99 },
987 { rts_acosf, 1, "acosf", 1, _C99 },
988 { rts_atanf, 1, "atanf", 1, _C99 },
989 { rts_sinhf, 1, "sinhf", 1, _C99 },
990 { rts_coshf, 1, "coshf", 1, _C99 },
991 { rts_tanhf, 1, "tanhf", 1, _C99 },
993 { rts_fabsl, 1, "fabsl", 1, _C99 },
994 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
995 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
996 { rts_expl, 1, "expl", 1, _C99 },
997 { rts_exp2l, 1, "exp2l", 1, _C99 },
998 { rts_exp10l, 1, "exp10l", 1, _GNUC },
999 { rts_logl, 1, "logl", 1, _C99 },
1000 { rts_log2l, 1, "log2l", 1, _C99 },
1001 { rts_log10l, 1, "log10l", 1, _C99 },
1002 { rts_powl, 1, "powl", 2, _C99 },
1003 { rts_sinl, 1, "sinl", 1, _C99 },
1004 { rts_cosl, 1, "cosl", 1, _C99 },
1005 { rts_tanl, 1, "tanl", 1, _C99 },
1006 { rts_asinl, 1, "asinl", 1, _C99 },
1007 { rts_acosl, 1, "acosl", 1, _C99 },
1008 { rts_atanl, 1, "atanl", 1, _C99 },
1009 { rts_sinhl, 1, "sinhl", 1, _C99 },
1010 { rts_coshl, 1, "coshl", 1, _C99 },
1011 { rts_tanhl, 1, "tanhl", 1, _C99 },
1013 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
1014 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
1015 { rts_strcmp, 1, "strcmp", 2, _C89 },
1016 { rts_strncmp, 1, "strncmp", 3, _C89 }
1019 static ir_entity* get_function_entity(declaration_t *declaration)
1021 if(declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
1022 return declaration->v.entity;
1023 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
1025 symbol_t *symbol = declaration->symbol;
1026 ident *id = new_id_from_str(symbol->string);
1028 ir_type *global_type = get_glob_type();
1029 ir_type *ir_type_method = get_ir_type(declaration->type);
1030 assert(is_Method_type(ir_type_method));
1032 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
1033 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
1034 set_entity_ld_ident(entity, id);
1035 if(declaration->storage_class == STORAGE_CLASS_STATIC
1036 || declaration->is_inline) {
1037 set_entity_visibility(entity, visibility_local);
1038 } else if(declaration->init.statement != NULL) {
1039 set_entity_visibility(entity, visibility_external_visible);
1041 set_entity_visibility(entity, visibility_external_allocated);
1043 /* We should check for file scope here, but as long as we compile C only
1044 this is not needed. */
1045 int n_params = get_method_n_params(ir_type_method);
1046 int n_res = get_method_n_ress(ir_type_method);
1049 if (n_params == 0 && n_res == 0 && id == predef_idents[rts_abort]) {
1050 /* found abort(), store for later */
1054 if (! firm_opt.freestanding) {
1055 /* check for a known runtime function */
1056 for (i = 0; i < rts_max; ++i) {
1057 /* ignore those rts functions not necessary needed for current mode */
1058 if ((c_mode & rts_data[i].flags) == 0)
1060 if (n_params == rts_data[i].n_params && n_res == rts_data[i].n_res &&
1061 id == predef_idents[rts_data[i].id])
1062 rts_entities[rts_data[i].id] = entity;
1067 set_entity_allocation(entity, allocation_static);
1069 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1070 declaration->v.entity = entity;
1075 static ir_node *const_to_firm(const const_expression_t *cnst)
1077 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1078 ir_mode *mode = get_ir_mode(cnst->base.type);
1083 if(mode_is_float(mode)) {
1084 tv = new_tarval_from_double(cnst->v.float_value, mode);
1086 if(mode_is_signed(mode)) {
1087 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1089 len = snprintf(buf, sizeof(buf), "%llu",
1090 (unsigned long long) cnst->v.int_value);
1092 tv = new_tarval_from_str(buf, len, mode);
1095 return new_d_Const(dbgi, mode, tv);
1098 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1100 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1101 ir_mode *mode = get_ir_mode(cnst->base.type);
1103 long long int v = 0;
1104 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1105 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1108 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1109 tarval *tv = new_tarval_from_str(buf, len, mode);
1111 return new_d_Const(dbgi, mode, tv);
1114 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1116 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1117 ir_mode *mode = get_ir_mode(cnst->base.type);
1119 long long int v = cnst->v.wide_character.begin[0];
1122 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1123 tarval *tv = new_tarval_from_str(buf, len, mode);
1125 return new_d_Const(dbgi, mode, tv);
1128 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1131 assert(entity != NULL);
1132 union symconst_symbol sym;
1133 sym.entity_p = entity;
1134 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1137 static ir_node *string_to_firm(const source_position_t *const src_pos,
1138 const char *const id_prefix,
1139 const string_t *const value)
1141 ir_type *const global_type = get_glob_type();
1142 dbg_info *const dbgi = get_dbg_info(src_pos);
1143 ir_type *const type = new_d_type_array(unique_ident("strtype"), 1,
1144 ir_type_const_char, dbgi);
1146 ident *const id = unique_ident(id_prefix);
1147 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1148 set_entity_ld_ident(entity, id);
1149 set_entity_variability(entity, variability_constant);
1150 set_entity_allocation(entity, allocation_static);
1152 ir_type *const elem_type = ir_type_const_char;
1153 ir_mode *const mode = get_type_mode(elem_type);
1155 const char* const string = value->begin;
1156 const size_t slen = value->size;
1158 set_array_lower_bound_int(type, 0, 0);
1159 set_array_upper_bound_int(type, 0, slen);
1160 set_type_size_bytes(type, slen);
1161 set_type_state(type, layout_fixed);
1163 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1164 for(size_t i = 0; i < slen; ++i) {
1165 tvs[i] = new_tarval_from_long(string[i], mode);
1168 set_array_entity_values(entity, tvs, slen);
1171 return create_symconst(dbgi, mode_P_data, entity);
1174 static ir_node *string_literal_to_firm(
1175 const string_literal_expression_t* literal)
1177 return string_to_firm(&literal->base.source_position, "Lstr",
1181 static ir_node *wide_string_literal_to_firm(
1182 const wide_string_literal_expression_t* const literal)
1184 ir_type *const global_type = get_glob_type();
1185 ir_type *const elem_type = ir_type_wchar_t;
1186 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1187 ir_type *const type = new_d_type_array(unique_ident("strtype"), 1,
1190 ident *const id = unique_ident("Lstr");
1191 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1192 set_entity_ld_ident(entity, id);
1193 set_entity_variability(entity, variability_constant);
1194 set_entity_allocation(entity, allocation_static);
1196 ir_mode *const mode = get_type_mode(elem_type);
1198 const wchar_rep_t *const string = literal->value.begin;
1199 const size_t slen = literal->value.size;
1201 set_array_lower_bound_int(type, 0, 0);
1202 set_array_upper_bound_int(type, 0, slen);
1203 set_type_size_bytes(type, slen);
1204 set_type_state(type, layout_fixed);
1206 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1207 for(size_t i = 0; i < slen; ++i) {
1208 tvs[i] = new_tarval_from_long(string[i], mode);
1211 set_array_entity_values(entity, tvs, slen);
1214 return create_symconst(dbgi, mode_P_data, entity);
1217 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
1218 dbg_info *const dbgi)
1220 if (is_compound_type(irtype) ||
1221 is_Method_type(irtype) ||
1222 is_Array_type(irtype)) {
1226 ir_mode *const mode = get_type_mode(irtype);
1227 ir_node *const memory = get_store();
1228 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1229 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1230 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1231 set_store(load_mem);
1235 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1237 ir_mode *mode = get_irn_mode(node);
1239 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1241 if(!mode_is_float(mode))
1244 /* check if there is already a Conv */
1245 if (get_irn_op(node) == op_Conv) {
1246 /* convert it into a strict Conv */
1247 set_Conv_strict(node, 1);
1251 /* otherwise create a new one */
1252 return new_d_strictConv(dbgi, node, mode);
1255 static ir_node *get_global_var_address(dbg_info *const dbgi,
1256 const declaration_t *const decl)
1258 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1260 ir_entity *const entity = decl->v.entity;
1261 switch ((storage_class_tag_t)decl->storage_class) {
1262 case STORAGE_CLASS_THREAD:
1263 case STORAGE_CLASS_THREAD_EXTERN:
1264 case STORAGE_CLASS_THREAD_STATIC: {
1265 ir_node *const no_mem = new_NoMem();
1266 ir_node *const tls = get_irg_tls(current_ir_graph);
1267 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1271 return create_symconst(dbgi, mode_P_data, entity);
1275 /* Returns the correct base address depending on whether it is a parameter or a
1276 * normal local variable */
1277 static ir_node *get_local_frame(ir_entity *const ent)
1279 ir_graph *const irg = current_ir_graph;
1280 const ir_type *const owner = get_entity_owner(ent);
1281 if (owner == get_irg_frame_type(irg)) {
1282 return get_irg_frame(irg);
1284 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1285 return get_irg_value_param_base(irg);
1289 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1291 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1292 declaration_t *declaration = ref->declaration;
1293 type_t *type = skip_typeref(declaration->type);
1295 switch((declaration_kind_t) declaration->declaration_kind) {
1296 case DECLARATION_KIND_UNKNOWN:
1297 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
1303 case DECLARATION_KIND_ENUM_ENTRY: {
1304 ir_mode *const mode = get_ir_mode(type);
1305 return new_Const(mode, declaration->v.enum_val);
1308 case DECLARATION_KIND_LOCAL_VARIABLE: {
1309 ir_mode *const mode = get_ir_mode(type);
1310 return get_value(declaration->v.value_number, mode);
1312 case DECLARATION_KIND_FUNCTION: {
1313 ir_mode *const mode = get_ir_mode(type);
1314 return create_symconst(dbgi, mode, declaration->v.entity);
1316 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1317 ir_node *const addr = get_global_var_address(dbgi, declaration);
1318 ir_type *const irtype = get_entity_type(declaration->v.entity);
1319 return deref_address(irtype, addr, dbgi);
1322 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1323 ir_entity *entity = declaration->v.entity;
1324 ir_node *frame = get_local_frame(entity);
1325 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1326 ir_type *irtype = get_entity_type(entity);
1327 return deref_address(irtype, sel, dbgi);
1330 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1331 return declaration->v.vla_base;
1333 case DECLARATION_KIND_COMPOUND_MEMBER:
1334 case DECLARATION_KIND_LABEL_BLOCK:
1335 panic("not implemented reference type");
1338 panic("reference to declaration with unknown type found");
1341 static ir_node *reference_addr(const reference_expression_t *ref)
1343 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1344 declaration_t *declaration = ref->declaration;
1346 switch((declaration_kind_t) declaration->declaration_kind) {
1347 case DECLARATION_KIND_UNKNOWN:
1349 case DECLARATION_KIND_LOCAL_VARIABLE:
1350 panic("local variable without entity has no address");
1351 case DECLARATION_KIND_FUNCTION: {
1352 type_t *const type = skip_typeref(ref->base.type);
1353 ir_mode *const mode = get_ir_mode(type);
1354 return create_symconst(dbgi, mode, declaration->v.entity);
1356 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1357 ir_node *const addr = get_global_var_address(dbgi, declaration);
1360 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1361 ir_entity *entity = declaration->v.entity;
1362 ir_node *frame = get_local_frame(entity);
1363 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1368 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1369 return declaration->v.vla_base;
1371 case DECLARATION_KIND_ENUM_ENTRY:
1372 panic("trying to reference enum entry");
1374 case DECLARATION_KIND_COMPOUND_MEMBER:
1375 case DECLARATION_KIND_LABEL_BLOCK:
1376 panic("not implemented reference type");
1379 panic("reference to declaration with unknown type found");
1382 static ir_node *process_builtin_call(const call_expression_t *call)
1384 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1386 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1387 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1389 type_t *type = skip_typeref(builtin->base.type);
1390 assert(is_type_pointer(type));
1392 type_t *function_type = skip_typeref(type->pointer.points_to);
1393 symbol_t *symbol = builtin->symbol;
1395 switch(symbol->ID) {
1396 case T___builtin_alloca: {
1397 if(call->arguments == NULL || call->arguments->next != NULL) {
1398 panic("invalid number of parameters on __builtin_alloca");
1400 expression_t *argument = call->arguments->expression;
1401 ir_node *size = expression_to_firm(argument);
1403 ir_node *store = get_store();
1404 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1406 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1408 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1412 case T___builtin_nan:
1413 case T___builtin_nanf:
1414 case T___builtin_nand: {
1415 /* Ignore string for now... */
1416 assert(is_type_function(function_type));
1417 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1418 tarval *tv = get_mode_NAN(mode);
1419 ir_node *res = new_d_Const(dbgi, mode, tv);
1422 case T___builtin_va_end:
1425 panic("Unsupported builtin found\n");
1429 static ir_node *call_expression_to_firm(const call_expression_t *call)
1431 assert(get_cur_block() != NULL);
1433 expression_t *function = call->function;
1434 if(function->kind == EXPR_BUILTIN_SYMBOL) {
1435 return process_builtin_call(call);
1437 ir_node *callee = expression_to_firm(function);
1439 type_t *type = skip_typeref(function->base.type);
1440 assert(is_type_pointer(type));
1441 pointer_type_t *pointer_type = &type->pointer;
1442 type_t *points_to = skip_typeref(pointer_type->points_to);
1443 assert(is_type_function(points_to));
1444 function_type_t *function_type = &points_to->function;
1446 int n_parameters = 0;
1447 call_argument_t *argument = call->arguments;
1448 for( ; argument != NULL; argument = argument->next) {
1452 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1454 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1455 ir_type *new_method_type = NULL;
1456 if(function_type->variadic || function_type->unspecified_parameters) {
1457 /* we need to construct a new method type matching the call
1459 int n_res = get_method_n_ress(ir_method_type);
1460 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1461 new_method_type = new_d_type_method(unique_ident("calltype"),
1462 n_parameters, n_res, dbgi);
1463 set_method_calling_convention(new_method_type,
1464 get_method_calling_convention(ir_method_type));
1465 set_method_additional_properties(new_method_type,
1466 get_method_additional_properties(ir_method_type));
1468 for(int i = 0; i < n_res; ++i) {
1469 set_method_res_type(new_method_type, i,
1470 get_method_res_type(ir_method_type, i));
1473 ir_node *in[n_parameters];
1475 argument = call->arguments;
1477 for( ; argument != NULL; argument = argument->next) {
1478 expression_t *expression = argument->expression;
1479 ir_node *arg_node = expression_to_firm(expression);
1481 arg_node = do_strict_conv(dbgi, arg_node);
1484 if(new_method_type != NULL) {
1485 ir_type *irtype = get_ir_type(expression->base.type);
1486 set_method_param_type(new_method_type, n, irtype);
1491 assert(n == n_parameters);
1493 if(new_method_type != NULL)
1494 ir_method_type = new_method_type;
1496 ir_node *store = get_store();
1497 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1499 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1502 type_t *return_type = skip_typeref(function_type->return_type);
1503 ir_node *result = NULL;
1505 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1507 if(is_type_scalar(return_type)) {
1508 mode = get_ir_mode(return_type);
1512 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1513 result = new_d_Proj(dbgi, resproj, mode, 0);
1519 static void statement_to_firm(statement_t *statement);
1520 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1522 static ir_node *expression_to_addr(const expression_t *expression);
1523 static void create_condition_evaluation(const expression_t *expression,
1524 ir_node *true_block,
1525 ir_node *false_block);
1527 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1530 value = do_strict_conv(dbgi, value);
1532 ir_node *memory = get_store();
1534 if(is_type_scalar(type)) {
1535 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1536 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1537 set_store(store_mem);
1539 ir_type *irtype = get_ir_type(type);
1540 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1541 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1542 set_store(copyb_mem);
1546 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1548 tarval *all_one = get_mode_all_one(mode);
1549 int mode_size = get_mode_size_bits(mode);
1551 assert(offset >= 0 && size >= 0);
1552 assert(offset + size <= mode_size);
1553 if(size == mode_size) {
1557 long shiftr = get_mode_size_bits(mode) - size;
1558 long shiftl = offset;
1559 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1560 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1561 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1562 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1567 static void bitfield_store_to_firm(const unary_expression_t *expression,
1570 expression_t *select = expression->value;
1571 assert(select->kind == EXPR_SELECT);
1572 type_t *type = select->base.type;
1573 assert(type->kind == TYPE_BITFIELD);
1574 ir_mode *mode = get_ir_mode(type->bitfield.base);
1575 ir_node *addr = expression_to_addr(select);
1577 assert(get_irn_mode(value) == mode);
1579 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1581 /* kill upper bits of value and shift to right position */
1582 ir_entity *entity = select->select.compound_entry->v.entity;
1583 int bitoffset = get_entity_offset_bits_remainder(entity);
1584 ir_type *entity_type = get_entity_type(entity);
1585 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1587 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1588 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1589 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1590 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1591 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1592 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1594 /* load current value */
1595 ir_node *mem = get_store();
1596 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1597 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1598 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1599 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1600 tarval *inv_mask = tarval_not(shift_mask);
1601 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1602 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1604 /* construct new value and store */
1605 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1606 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1607 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1608 set_store(store_mem);
1611 static void set_value_for_expression(const expression_t *expression,
1614 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1615 value = do_strict_conv(dbgi, value);
1617 if(expression->kind == EXPR_REFERENCE) {
1618 const reference_expression_t *ref = &expression->reference;
1620 declaration_t *declaration = ref->declaration;
1621 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1622 if(declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1623 set_value(declaration->v.value_number, value);
1628 if(expression->kind == EXPR_UNARY_BITFIELD_EXTRACT) {
1629 bitfield_store_to_firm(&expression->unary, value);
1633 ir_node *addr = expression_to_addr(expression);
1634 type_t *type = skip_typeref(expression->base.type);
1635 assign_value(dbgi, addr, type, value);
1638 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1640 ir_mode *value_mode = get_irn_mode(value);
1642 if (value_mode == dest_mode || is_Bad(value))
1645 if(dest_mode == mode_b) {
1646 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1647 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1648 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1652 return new_d_Conv(dbgi, value, dest_mode);
1655 static ir_node *create_incdec(const unary_expression_t *expression)
1657 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1658 type_t *type = skip_typeref(expression->base.type);
1659 ir_mode *mode = get_ir_mode(type);
1660 expression_t *value = expression->value;
1662 ir_node *value_node = expression_to_firm(value);
1665 if(is_type_pointer(type)) {
1666 pointer_type_t *pointer_type = &type->pointer;
1667 offset = get_type_size(pointer_type->points_to);
1669 assert(is_type_arithmetic(type));
1670 offset = new_Const(mode, get_mode_one(mode));
1673 switch(expression->base.kind) {
1674 case EXPR_UNARY_POSTFIX_INCREMENT: {
1675 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1676 set_value_for_expression(value, new_value);
1679 case EXPR_UNARY_POSTFIX_DECREMENT: {
1680 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1681 set_value_for_expression(value, new_value);
1684 case EXPR_UNARY_PREFIX_INCREMENT: {
1685 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1686 set_value_for_expression(value, new_value);
1689 case EXPR_UNARY_PREFIX_DECREMENT: {
1690 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1691 set_value_for_expression(value, new_value);
1695 panic("no incdec expr in create_incdec");
1700 static bool is_local_variable(expression_t *expression)
1702 if (expression->kind != EXPR_REFERENCE)
1704 reference_expression_t *ref_expr = &expression->reference;
1705 declaration_t *declaration = ref_expr->declaration;
1706 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1709 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1712 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1713 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1714 case EXPR_BINARY_NOTEQUAL:
1715 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1716 case EXPR_BINARY_ISLESS:
1717 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1718 case EXPR_BINARY_ISLESSEQUAL:
1719 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1720 case EXPR_BINARY_ISGREATER:
1721 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1722 case EXPR_BINARY_ISGREATEREQUAL:
1723 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1724 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1729 panic("trying to get pn_Cmp from non-comparison binexpr type");
1733 * Handle the assume optimizer hint: check if a Confirm
1734 * node can be created.
1736 * @param dbi debug info
1737 * @param expr the IL assume expression
1739 * we support here only some simple cases:
1744 static ir_node *handle_assume_compare(dbg_info *dbi,
1745 const binary_expression_t *expression)
1747 expression_t *op1 = expression->left;
1748 expression_t *op2 = expression->right;
1749 declaration_t *var2, *var = NULL;
1750 ir_node *res = NULL;
1753 cmp_val = get_pnc(expression->base.kind, op1->base.type);
1755 if (is_local_variable(op1) && is_local_variable(op2)) {
1756 var = op1->reference.declaration;
1757 var2 = op2->reference.declaration;
1759 type_t *const type = skip_typeref(var->type);
1760 ir_mode *const mode = get_ir_mode(type);
1762 ir_node *const irn1 = get_value(var->v.value_number, mode);
1763 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1765 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1766 set_value(var2->v.value_number, res);
1768 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1769 set_value(var->v.value_number, res);
1775 if (is_local_variable(op1) && is_constant_expression(op2)) {
1776 var = op1->reference.declaration;
1778 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1779 cmp_val = get_inversed_pnc(cmp_val);
1780 var = op2->reference.declaration;
1785 type_t *const type = skip_typeref(var->type);
1786 ir_mode *const mode = get_ir_mode(type);
1788 res = get_value(var->v.value_number, mode);
1789 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1790 set_value(var->v.value_number, res);
1796 * Handle the assume optimizer hint.
1798 * @param dbi debug info
1799 * @param expr the IL assume expression
1801 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1802 switch(expression->kind) {
1803 case EXPR_BINARY_EQUAL:
1804 case EXPR_BINARY_NOTEQUAL:
1805 case EXPR_BINARY_LESS:
1806 case EXPR_BINARY_LESSEQUAL:
1807 case EXPR_BINARY_GREATER:
1808 case EXPR_BINARY_GREATEREQUAL:
1809 return handle_assume_compare(dbi, &expression->binary);
1815 static ir_node *bitfield_extract_to_firm(const unary_expression_t *expression)
1817 expression_t *select = expression->value;
1818 assert(select->kind == EXPR_SELECT);
1820 type_t *type = select->base.type;
1821 assert(type->kind == TYPE_BITFIELD);
1822 ir_mode *mode = get_ir_mode(type->bitfield.base);
1823 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1824 ir_node *addr = expression_to_addr(select);
1825 ir_node *mem = get_store();
1826 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1827 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1828 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1830 load_res = create_conv(dbgi, load_res, mode_int);
1832 set_store(load_mem);
1834 /* kill upper bits */
1835 ir_entity *entity = select->select.compound_entry->v.entity;
1836 int bitoffset = get_entity_offset_bits_remainder(entity);
1837 ir_type *entity_type = get_entity_type(entity);
1838 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1839 long shift_bitsl = machine_size - bitoffset - bitsize;
1840 assert(shift_bitsl >= 0);
1841 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1842 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1843 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1845 long shift_bitsr = bitoffset + shift_bitsl;
1846 assert(shift_bitsr <= (long) machine_size);
1847 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1848 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1850 if(mode_is_signed(mode)) {
1851 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1853 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1856 return create_conv(dbgi, shiftr, mode);
1859 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1861 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1862 type_t *type = skip_typeref(expression->base.type);
1864 if(expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
1865 return expression_to_addr(expression->value);
1867 const expression_t *value = expression->value;
1869 switch(expression->base.kind) {
1870 case EXPR_UNARY_NEGATE: {
1871 ir_node *value_node = expression_to_firm(value);
1872 ir_mode *mode = get_ir_mode(type);
1873 return new_d_Minus(dbgi, value_node, mode);
1875 case EXPR_UNARY_PLUS:
1876 return expression_to_firm(value);
1877 case EXPR_UNARY_BITWISE_NEGATE: {
1878 ir_node *value_node = expression_to_firm(value);
1879 ir_mode *mode = get_ir_mode(type);
1880 return new_d_Not(dbgi, value_node, mode);
1882 case EXPR_UNARY_NOT: {
1883 ir_node *value_node = expression_to_firm(value);
1884 ir_mode *mode = get_ir_mode(type);
1885 if(get_irn_mode(value_node) != mode_b) {
1886 value_node = create_conv(dbgi, value_node, mode_b);
1888 value_node = new_d_Not(dbgi, value_node, mode_b);
1889 if(mode != mode_b) {
1890 value_node = create_conv(dbgi, value_node, mode);
1894 case EXPR_UNARY_DEREFERENCE: {
1895 ir_node *value_node = expression_to_firm(value);
1896 type_t *value_type = skip_typeref(value->base.type);
1897 ir_type *irtype = get_ir_type(value_type);
1898 assert(is_Pointer_type(irtype));
1899 ir_type *points_to = get_pointer_points_to_type(irtype);
1900 return deref_address(points_to, value_node, dbgi);
1902 case EXPR_UNARY_POSTFIX_INCREMENT:
1903 case EXPR_UNARY_POSTFIX_DECREMENT:
1904 case EXPR_UNARY_PREFIX_INCREMENT:
1905 case EXPR_UNARY_PREFIX_DECREMENT:
1906 return create_incdec(expression);
1907 case EXPR_UNARY_CAST: {
1908 ir_node *value_node = expression_to_firm(value);
1909 if(is_type_scalar(type)) {
1910 ir_mode *mode = get_ir_mode(type);
1911 ir_node *node = create_conv(dbgi, value_node, mode);
1912 node = do_strict_conv(dbgi, node);
1918 case EXPR_UNARY_CAST_IMPLICIT: {
1919 ir_node *value_node = expression_to_firm(value);
1920 if(is_type_scalar(type)) {
1921 ir_mode *mode = get_ir_mode(type);
1922 return create_conv(dbgi, value_node, mode);
1927 case EXPR_UNARY_ASSUME:
1928 if(firm_opt.confirm)
1929 return handle_assume(dbgi, value);
1932 case EXPR_UNARY_BITFIELD_EXTRACT:
1933 return bitfield_extract_to_firm(expression);
1938 panic("invalid UNEXPR type found");
1941 static ir_node *produce_condition_result(const expression_t *expression,
1944 ir_mode *mode = get_ir_mode(expression->base.type);
1945 ir_node *cur_block = get_cur_block();
1947 ir_node *one_block = new_immBlock();
1948 ir_node *one = new_Const(mode, get_mode_one(mode));
1949 ir_node *jmp_one = new_d_Jmp(dbgi);
1951 ir_node *zero_block = new_immBlock();
1952 ir_node *zero = new_Const(mode, get_mode_null(mode));
1953 ir_node *jmp_zero = new_d_Jmp(dbgi);
1955 set_cur_block(cur_block);
1956 create_condition_evaluation(expression, one_block, zero_block);
1957 mature_immBlock(one_block);
1958 mature_immBlock(zero_block);
1960 ir_node *common_block = new_immBlock();
1961 add_immBlock_pred(common_block, jmp_one);
1962 add_immBlock_pred(common_block, jmp_zero);
1963 mature_immBlock(common_block);
1965 ir_node *in[2] = { one, zero };
1966 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1971 static ir_node *create_lazy_op(const binary_expression_t *expression)
1973 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1974 type_t *type = expression->base.type;
1975 ir_mode *mode = get_ir_mode(type);
1977 if(is_constant_expression(expression->left)) {
1978 long val = fold_constant(expression->left);
1979 expression_kind_t ekind = expression->base.kind;
1980 if((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
1981 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1982 return expression_to_firm(expression->right);
1984 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
1985 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
1986 return new_Const(mode, get_mode_one(mode));
1990 return produce_condition_result((const expression_t*) expression, dbgi);
1993 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1994 ir_node *right, ir_mode *mode);
1996 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1997 create_arithmetic_func func)
1999 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2000 ir_node *left = expression_to_firm(expression->left);
2001 ir_node *right = expression_to_firm(expression->right);
2002 type_t *type = expression->right->base.type;
2003 /* be careful with the modes, because in arithmetic assign nodes only
2004 * the right operand has the mode of the arithmetic already */
2005 ir_mode *mode = get_ir_mode(type);
2006 left = create_conv(dbgi, left, mode);
2007 ir_node *res = func(dbgi, left, right, mode);
2012 static ir_node *pointer_arithmetic(ir_node *const pointer,
2015 dbg_info *const dbgi,
2016 const create_arithmetic_func func)
2018 pointer_type_t *const pointer_type = &type->pointer;
2019 type_t *const points_to = pointer_type->points_to;
2020 const unsigned elem_size = get_type_size_const(points_to);
2022 assert(elem_size >= 1);
2023 if (elem_size > 1) {
2024 integer = create_conv(dbgi, integer, mode_int);
2025 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
2026 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
2030 ir_mode *const mode = get_ir_mode(type);
2031 return func(dbgi, pointer, integer, mode);
2034 static ir_node *create_arithmetic_assign_binop(
2035 const binary_expression_t *expression, create_arithmetic_func func)
2037 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2038 type_t *const type = skip_typeref(expression->base.type);
2041 if (is_type_pointer(type)) {
2042 ir_node *const pointer = expression_to_firm(expression->left);
2043 ir_node * integer = expression_to_firm(expression->right);
2044 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
2046 value = create_arithmetic_binop(expression, func);
2049 ir_mode *const mode = get_ir_mode(type);
2050 value = create_conv(dbgi, value, mode);
2051 set_value_for_expression(expression->left, value);
2056 static ir_node *create_add(const binary_expression_t *expression)
2058 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2059 ir_node *left = expression_to_firm(expression->left);
2060 ir_node *right = expression_to_firm(expression->right);
2061 type_t *type = expression->base.type;
2063 expression_t *expr_left = expression->left;
2064 expression_t *expr_right = expression->right;
2065 type_t *type_left = skip_typeref(expr_left->base.type);
2066 type_t *type_right = skip_typeref(expr_right->base.type);
2068 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2069 ir_mode *const mode = get_ir_mode(type);
2070 return new_d_Add(dbgi, left, right, mode);
2073 if (is_type_pointer(type_left)) {
2074 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
2076 assert(is_type_pointer(type_right));
2077 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
2081 static ir_node *create_sub(const binary_expression_t *expression)
2083 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2084 expression_t *const expr_left = expression->left;
2085 expression_t *const expr_right = expression->right;
2086 ir_node *const left = expression_to_firm(expr_left);
2087 ir_node *const right = expression_to_firm(expr_right);
2088 type_t *const type = expression->base.type;
2089 type_t *const type_left = skip_typeref(expr_left->base.type);
2090 type_t *const type_right = skip_typeref(expr_right->base.type);
2092 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2093 ir_mode *const mode = get_ir_mode(type);
2094 return new_d_Sub(dbgi, left, right, mode);
2095 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2096 const pointer_type_t *const ptr_type = &type_left->pointer;
2098 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2099 ir_mode *const mode = get_ir_mode(type);
2100 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2101 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2102 ir_node *const no_mem = new_NoMem();
2103 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size, mode,
2104 op_pin_state_floats);
2105 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2108 assert(is_type_pointer(type_left));
2109 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
2112 static ir_node *create_shift(const binary_expression_t *expression)
2114 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2115 ir_node *left = expression_to_firm(expression->left);
2116 ir_node *right = expression_to_firm(expression->right);
2117 type_t *type = expression->base.type;
2118 ir_mode *mode = get_ir_mode(type);
2120 /* firm always wants the shift count to be unsigned */
2121 right = create_conv(dbgi, right, mode_uint);
2125 switch(expression->base.kind) {
2126 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2127 case EXPR_BINARY_SHIFTLEFT:
2128 res = new_d_Shl(dbgi, left, right, mode);
2130 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2131 case EXPR_BINARY_SHIFTRIGHT: {
2132 expression_t *expr_left = expression->left;
2133 type_t *type_left = skip_typeref(expr_left->base.type);
2135 if(is_type_signed(type_left)) {
2136 res = new_d_Shrs(dbgi, left, right, mode);
2138 res = new_d_Shr(dbgi, left, right, mode);
2143 panic("create shift op called for non-shift op");
2150 static ir_node *create_divmod(const binary_expression_t *expression)
2152 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2153 ir_node *left = expression_to_firm(expression->left);
2154 ir_node *right = expression_to_firm(expression->right);
2155 ir_node *pin = new_Pin(new_NoMem());
2156 /* be careful with the modes, because in arithmetic assign nodes only
2157 * the right operand has the mode of the arithmetic already */
2158 type_t *type = expression->right->base.type;
2159 ir_mode *mode = get_ir_mode(type);
2160 left = create_conv(dbgi, left, mode);
2164 switch (expression->base.kind) {
2165 case EXPR_BINARY_DIV:
2166 case EXPR_BINARY_DIV_ASSIGN:
2167 if(mode_is_float(mode)) {
2168 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2169 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2171 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2172 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2176 case EXPR_BINARY_MOD:
2177 case EXPR_BINARY_MOD_ASSIGN:
2178 assert(!mode_is_float(mode));
2179 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
2180 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2183 default: panic("unexpected binary expression type in create_divmod()");
2189 static ir_node *create_arithmetic_assign_divmod(
2190 const binary_expression_t *expression)
2192 ir_node * value = create_divmod(expression);
2193 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2194 type_t *const type = expression->base.type;
2195 ir_mode *const mode = get_ir_mode(type);
2197 assert(type->kind != TYPE_POINTER);
2199 value = create_conv(dbgi, value, mode);
2200 set_value_for_expression(expression->left, value);
2205 static ir_node *create_arithmetic_assign_shift(
2206 const binary_expression_t *expression)
2208 ir_node * value = create_shift(expression);
2209 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2210 type_t *const type = expression->base.type;
2211 ir_mode *const mode = get_ir_mode(type);
2213 value = create_conv(dbgi, value, mode);
2214 set_value_for_expression(expression->left, value);
2219 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2221 expression_kind_t kind = expression->base.kind;
2224 case EXPR_BINARY_EQUAL:
2225 case EXPR_BINARY_NOTEQUAL:
2226 case EXPR_BINARY_LESS:
2227 case EXPR_BINARY_LESSEQUAL:
2228 case EXPR_BINARY_GREATER:
2229 case EXPR_BINARY_GREATEREQUAL:
2230 case EXPR_BINARY_ISGREATER:
2231 case EXPR_BINARY_ISGREATEREQUAL:
2232 case EXPR_BINARY_ISLESS:
2233 case EXPR_BINARY_ISLESSEQUAL:
2234 case EXPR_BINARY_ISLESSGREATER:
2235 case EXPR_BINARY_ISUNORDERED: {
2236 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2237 ir_node *left = expression_to_firm(expression->left);
2238 ir_node *right = expression_to_firm(expression->right);
2239 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2240 long pnc = get_pnc(kind, expression->left->base.type);
2241 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2244 case EXPR_BINARY_ASSIGN: {
2245 ir_node *right = expression_to_firm(expression->right);
2246 set_value_for_expression(expression->left, right);
2250 case EXPR_BINARY_ADD:
2251 return create_add(expression);
2252 case EXPR_BINARY_SUB:
2253 return create_sub(expression);
2254 case EXPR_BINARY_MUL:
2255 return create_arithmetic_binop(expression, new_d_Mul);
2256 case EXPR_BINARY_BITWISE_AND:
2257 return create_arithmetic_binop(expression, new_d_And);
2258 case EXPR_BINARY_BITWISE_OR:
2259 return create_arithmetic_binop(expression, new_d_Or);
2260 case EXPR_BINARY_BITWISE_XOR:
2261 return create_arithmetic_binop(expression, new_d_Eor);
2262 case EXPR_BINARY_SHIFTLEFT:
2263 case EXPR_BINARY_SHIFTRIGHT:
2264 return create_shift(expression);
2265 case EXPR_BINARY_DIV:
2266 case EXPR_BINARY_MOD:
2267 return create_divmod(expression);
2268 case EXPR_BINARY_LOGICAL_AND:
2269 case EXPR_BINARY_LOGICAL_OR:
2270 return create_lazy_op(expression);
2271 case EXPR_BINARY_COMMA:
2272 expression_to_firm(expression->left);
2273 return expression_to_firm(expression->right);
2274 case EXPR_BINARY_ADD_ASSIGN:
2275 return create_arithmetic_assign_binop(expression, new_d_Add);
2276 case EXPR_BINARY_SUB_ASSIGN:
2277 return create_arithmetic_assign_binop(expression, new_d_Sub);
2278 case EXPR_BINARY_MUL_ASSIGN:
2279 return create_arithmetic_assign_binop(expression, new_d_Mul);
2280 case EXPR_BINARY_MOD_ASSIGN:
2281 case EXPR_BINARY_DIV_ASSIGN:
2282 return create_arithmetic_assign_divmod(expression);
2283 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2284 return create_arithmetic_assign_binop(expression, new_d_And);
2285 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2286 return create_arithmetic_assign_binop(expression, new_d_Or);
2287 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2288 return create_arithmetic_assign_binop(expression, new_d_Eor);
2289 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2290 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2291 return create_arithmetic_assign_shift(expression);
2292 case EXPR_BINARY_BUILTIN_EXPECT:
2293 return expression_to_firm(expression->left);
2295 panic("TODO binexpr type");
2299 static ir_node *array_access_addr(const array_access_expression_t *expression)
2301 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2302 ir_node *base_addr = expression_to_firm(expression->array_ref);
2303 ir_node *offset = expression_to_firm(expression->index);
2304 offset = create_conv(dbgi, offset, mode_uint);
2306 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2307 assert(is_type_pointer(ref_type));
2308 pointer_type_t *pointer_type = &ref_type->pointer;
2310 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2311 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2313 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2318 static ir_node *array_access_to_firm(
2319 const array_access_expression_t *expression)
2321 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2322 ir_node *addr = array_access_addr(expression);
2323 type_t *type = revert_automatic_type_conversion(
2324 (const expression_t*) expression);
2325 type = skip_typeref(type);
2326 ir_type *irtype = get_ir_type(type);
2328 return deref_address(irtype, addr, dbgi);
2331 static long get_offsetof_offset(const offsetof_expression_t *expression)
2333 type_t *orig_type = expression->type;
2336 designator_t *designator = expression->designator;
2337 for( ; designator != NULL; designator = designator->next) {
2338 type_t *type = skip_typeref(orig_type);
2339 /* be sure the type is constructed */
2340 (void) get_ir_type(type);
2342 if(designator->symbol != NULL) {
2343 assert(is_type_compound(type));
2344 symbol_t *symbol = designator->symbol;
2346 declaration_t *declaration = type->compound.declaration;
2347 declaration_t *iter = declaration->scope.declarations;
2348 for( ; iter != NULL; iter = iter->next) {
2349 if(iter->symbol == symbol) {
2353 assert(iter != NULL);
2355 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2356 offset += get_entity_offset(iter->v.entity);
2358 orig_type = iter->type;
2360 expression_t *array_index = designator->array_index;
2361 assert(designator->array_index != NULL);
2362 assert(is_type_array(type));
2363 assert(is_type_valid(array_index->base.type));
2365 long index = fold_constant(array_index);
2366 ir_type *arr_type = get_ir_type(type);
2367 ir_type *elem_type = get_array_element_type(arr_type);
2368 long elem_size = get_type_size_bytes(elem_type);
2370 offset += index * elem_size;
2372 orig_type = type->array.element_type;
2379 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2381 ir_mode *mode = get_ir_mode(expression->base.type);
2382 long offset = get_offsetof_offset(expression);
2383 tarval *tv = new_tarval_from_long(offset, mode);
2384 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2386 return new_d_Const(dbgi, mode, tv);
2389 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2390 ir_entity *entity, type_t *type);
2392 static ir_node *compound_literal_to_firm(
2393 const compound_literal_expression_t *expression)
2395 type_t *type = expression->type;
2397 /* create an entity on the stack */
2398 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2400 ident *const id = unique_ident("CompLit");
2401 ir_type *const irtype = get_ir_type(type);
2402 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2403 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2404 set_entity_ld_ident(entity, id);
2406 set_entity_variability(entity, variability_uninitialized);
2408 /* create initialisation code */
2409 initializer_t *initializer = expression->initializer;
2410 create_local_initializer(initializer, dbgi, entity, type);
2412 /* create a sel for the compound literal address */
2413 ir_node *frame = get_local_frame(entity);
2414 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2419 * Transform a sizeof expression into Firm code.
2421 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2423 type_t *type = expression->type;
2425 type = expression->tp_expression->base.type;
2426 assert(type != NULL);
2429 type = skip_typeref(type);
2430 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2431 if(is_type_array(type) && type->array.is_vla
2432 && expression->tp_expression != NULL) {
2433 expression_to_firm(expression->tp_expression);
2436 return get_type_size(type);
2440 * Transform an alignof expression into Firm code.
2442 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2444 type_t *type = expression->type;
2446 /* beware: if expression is a variable reference, return the
2447 alignment of the variable. */
2448 const expression_t *tp_expression = expression->tp_expression;
2449 const declaration_t *declaration = expr_is_variable(tp_expression);
2450 if (declaration != NULL) {
2451 /* TODO: get the alignment of this variable. */
2453 type = tp_expression->base.type;
2454 assert(type != NULL);
2457 ir_mode *const mode = get_ir_mode(expression->base.type);
2458 symconst_symbol sym;
2459 sym.type_p = get_ir_type(type);
2460 return new_SymConst(mode, sym, symconst_type_align);
2463 static void init_ir_types(void);
2464 long fold_constant(const expression_t *expression)
2468 assert(is_constant_expression(expression));
2470 ir_graph *old_current_ir_graph = current_ir_graph;
2471 if(current_ir_graph == NULL) {
2472 current_ir_graph = get_const_code_irg();
2475 ir_node *cnst = expression_to_firm(expression);
2476 current_ir_graph = old_current_ir_graph;
2478 if(!is_Const(cnst)) {
2479 panic("couldn't fold constant\n");
2482 tarval *tv = get_Const_tarval(cnst);
2483 if(!tarval_is_long(tv)) {
2484 panic("result of constant folding is not integer\n");
2487 return get_tarval_long(tv);
2490 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2492 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2494 /* first try to fold a constant condition */
2495 if(is_constant_expression(expression->condition)) {
2496 long val = fold_constant(expression->condition);
2498 return expression_to_firm(expression->true_expression);
2500 return expression_to_firm(expression->false_expression);
2504 ir_node *cur_block = get_cur_block();
2506 /* create the true block */
2507 ir_node *true_block = new_immBlock();
2509 ir_node *true_val = expression_to_firm(expression->true_expression);
2510 ir_node *true_jmp = new_Jmp();
2512 /* create the false block */
2513 ir_node *false_block = new_immBlock();
2515 ir_node *false_val = expression_to_firm(expression->false_expression);
2516 ir_node *false_jmp = new_Jmp();
2518 /* create the condition evaluation */
2519 set_cur_block(cur_block);
2520 create_condition_evaluation(expression->condition, true_block, false_block);
2521 mature_immBlock(true_block);
2522 mature_immBlock(false_block);
2524 /* create the common block */
2525 ir_node *common_block = new_immBlock();
2526 add_immBlock_pred(common_block, true_jmp);
2527 add_immBlock_pred(common_block, false_jmp);
2528 mature_immBlock(common_block);
2530 /* TODO improve static semantics, so either both or no values are NULL */
2531 if (true_val == NULL || false_val == NULL)
2534 ir_node *in[2] = { true_val, false_val };
2535 ir_mode *mode = get_irn_mode(true_val);
2536 assert(get_irn_mode(false_val) == mode);
2537 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2542 static ir_node *select_addr(const select_expression_t *expression)
2544 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2546 ir_node *compound_addr = expression_to_firm(expression->compound);
2548 declaration_t *entry = expression->compound_entry;
2549 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2550 ir_entity *entity = entry->v.entity;
2552 assert(entity != NULL);
2554 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2559 static ir_node *select_to_firm(const select_expression_t *expression)
2561 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2562 ir_node *addr = select_addr(expression);
2563 type_t *type = revert_automatic_type_conversion(
2564 (const expression_t*) expression);
2565 type = skip_typeref(type);
2566 ir_type *irtype = get_ir_type(type);
2568 return deref_address(irtype, addr, dbgi);
2571 /* Values returned by __builtin_classify_type. */
2572 typedef enum gcc_type_class
2578 enumeral_type_class,
2581 reference_type_class,
2585 function_type_class,
2596 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2598 const type_t *const type = expr->type_expression->base.type;
2604 const atomic_type_t *const atomic_type = &type->atomic;
2605 switch (atomic_type->akind) {
2606 /* should not be reached */
2607 case ATOMIC_TYPE_INVALID:
2611 /* gcc cannot do that */
2612 case ATOMIC_TYPE_VOID:
2613 tc = void_type_class;
2616 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2617 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2618 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2619 case ATOMIC_TYPE_SHORT:
2620 case ATOMIC_TYPE_USHORT:
2621 case ATOMIC_TYPE_INT:
2622 case ATOMIC_TYPE_UINT:
2623 case ATOMIC_TYPE_LONG:
2624 case ATOMIC_TYPE_ULONG:
2625 case ATOMIC_TYPE_LONGLONG:
2626 case ATOMIC_TYPE_ULONGLONG:
2627 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2628 tc = integer_type_class;
2631 case ATOMIC_TYPE_FLOAT:
2632 case ATOMIC_TYPE_DOUBLE:
2633 case ATOMIC_TYPE_LONG_DOUBLE:
2634 tc = real_type_class;
2637 #ifdef PROVIDE_COMPLEX
2638 case ATOMIC_TYPE_FLOAT_COMPLEX:
2639 case ATOMIC_TYPE_DOUBLE_COMPLEX:
2640 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
2641 tc = complex_type_class;
2643 case ATOMIC_TYPE_FLOAT_IMAGINARY:
2644 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
2645 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
2646 tc = complex_type_class;
2651 panic("Unimplemented case in classify_type_to_firm().");
2656 case TYPE_ARRAY: /* gcc handles this as pointer */
2657 case TYPE_FUNCTION: /* gcc handles this as pointer */
2658 case TYPE_POINTER: tc = pointer_type_class; break;
2659 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
2660 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
2662 /* gcc handles this as integer */
2663 case TYPE_ENUM: tc = integer_type_class; break;
2666 panic("Unimplemented case in classify_type_to_firm().");
2669 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2670 ir_mode *const mode = mode_int;
2671 tarval *const tv = new_tarval_from_long(tc, mode);
2672 return new_d_Const(dbgi, mode, tv);
2675 static ir_node *function_name_to_firm(
2676 const string_literal_expression_t *const expr)
2678 if (current_function_name == NULL) {
2679 const source_position_t *const src_pos = &expr->base.source_position;
2680 const char *const name = current_function_decl->symbol->string;
2681 const string_t string = { name, strlen(name) + 1 };
2682 current_function_name = string_to_firm(src_pos, "__func__", &string);
2685 return current_function_name;
2688 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2690 statement_t *statement = expr->statement;
2692 assert(statement->kind == STATEMENT_COMPOUND);
2693 return compound_statement_to_firm(&statement->compound);
2696 static ir_node *va_start_expression_to_firm(
2697 const va_start_expression_t *const expr)
2699 ir_type *const method_type = get_ir_type(current_function_decl->type);
2700 int const n = get_method_n_params(method_type) - 1;
2701 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2702 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2703 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2704 ir_node *const no_mem = new_NoMem();
2705 ir_node *const arg_sel =
2706 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2708 ir_node *const cnst = get_type_size(expr->parameter->type);
2709 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2710 set_value_for_expression(expr->ap, add);
2715 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2717 ir_type *const irtype = get_ir_type(expr->base.type);
2718 ir_node *const ap = expression_to_firm(expr->ap);
2719 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2720 ir_node *const res = deref_address(irtype, ap, dbgi);
2722 ir_node *const cnst = get_type_size(expr->base.type);
2723 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2724 set_value_for_expression(expr->ap, add);
2729 static ir_node *dereference_addr(const unary_expression_t *const expression)
2731 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2732 return expression_to_firm(expression->value);
2735 static ir_node *expression_to_addr(const expression_t *expression)
2737 switch(expression->kind) {
2738 case EXPR_REFERENCE:
2739 return reference_addr(&expression->reference);
2740 case EXPR_ARRAY_ACCESS:
2741 return array_access_addr(&expression->array_access);
2743 return select_addr(&expression->select);
2745 return call_expression_to_firm(&expression->call);
2746 case EXPR_UNARY_DEREFERENCE: {
2747 return dereference_addr(&expression->unary);
2752 panic("trying to get address of non-lvalue");
2755 static ir_node *builtin_constant_to_firm(
2756 const builtin_constant_expression_t *expression)
2758 ir_mode *mode = get_ir_mode(expression->base.type);
2761 if (is_constant_expression(expression->value)) {
2766 return new_Const_long(mode, v);
2769 static ir_node *builtin_prefetch_to_firm(
2770 const builtin_prefetch_expression_t *expression)
2772 ir_node *adr = expression_to_firm(expression->adr);
2773 /* no Firm support for prefetch yet */
2778 static ir_node *_expression_to_firm(const expression_t *expression)
2780 switch(expression->kind) {
2781 case EXPR_CHARACTER_CONSTANT:
2782 return character_constant_to_firm(&expression->conste);
2783 case EXPR_WIDE_CHARACTER_CONSTANT:
2784 return wide_character_constant_to_firm(&expression->conste);
2786 return const_to_firm(&expression->conste);
2787 case EXPR_STRING_LITERAL:
2788 return string_literal_to_firm(&expression->string);
2789 case EXPR_WIDE_STRING_LITERAL:
2790 return wide_string_literal_to_firm(&expression->wide_string);
2791 case EXPR_REFERENCE:
2792 return reference_expression_to_firm(&expression->reference);
2794 return call_expression_to_firm(&expression->call);
2796 return unary_expression_to_firm(&expression->unary);
2798 return binary_expression_to_firm(&expression->binary);
2799 case EXPR_ARRAY_ACCESS:
2800 return array_access_to_firm(&expression->array_access);
2802 return sizeof_to_firm(&expression->typeprop);
2804 return alignof_to_firm(&expression->typeprop);
2805 case EXPR_CONDITIONAL:
2806 return conditional_to_firm(&expression->conditional);
2808 return select_to_firm(&expression->select);
2809 case EXPR_CLASSIFY_TYPE:
2810 return classify_type_to_firm(&expression->classify_type);
2812 case EXPR_PRETTY_FUNCTION:
2813 return function_name_to_firm(&expression->string);
2814 case EXPR_STATEMENT:
2815 return statement_expression_to_firm(&expression->statement);
2817 return va_start_expression_to_firm(&expression->va_starte);
2819 return va_arg_expression_to_firm(&expression->va_arge);
2820 case EXPR_BUILTIN_SYMBOL:
2821 panic("unimplemented expression found");
2822 case EXPR_BUILTIN_CONSTANT_P:
2823 return builtin_constant_to_firm(&expression->builtin_constant);
2824 case EXPR_BUILTIN_PREFETCH:
2825 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2827 return offsetof_to_firm(&expression->offsetofe);
2828 case EXPR_COMPOUND_LITERAL:
2829 return compound_literal_to_firm(&expression->compound_literal);
2835 panic("invalid expression found");
2838 static ir_node *expression_to_firm(const expression_t *expression)
2840 ir_node *res = _expression_to_firm(expression);
2842 if(res != NULL && get_irn_mode(res) == mode_b) {
2843 ir_mode *mode = get_ir_mode(expression->base.type);
2845 if(is_Const_null(res)) {
2846 return new_Const_long(mode, 0);
2848 assert(is_Const_one(res));
2849 return new_Const_long(mode, 1);
2853 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2854 return produce_condition_result(expression, dbgi);
2860 static ir_node *expression_to_modeb(const expression_t *expression)
2862 ir_node *res = _expression_to_firm(expression);
2863 res = create_conv(NULL, res, mode_b);
2869 * create a short-circuit expression evaluation that tries to construct
2870 * efficient control flow structures for &&, || and ! expressions
2872 static void create_condition_evaluation(const expression_t *expression,
2873 ir_node *true_block,
2874 ir_node *false_block)
2876 switch(expression->kind) {
2877 case EXPR_UNARY_NOT: {
2878 const unary_expression_t *unary_expression = &expression->unary;
2879 create_condition_evaluation(unary_expression->value, false_block,
2883 case EXPR_BINARY_LOGICAL_AND: {
2884 const binary_expression_t *binary_expression = &expression->binary;
2886 ir_node *cur_block = get_cur_block();
2887 ir_node *extra_block = new_immBlock();
2888 set_cur_block(cur_block);
2889 create_condition_evaluation(binary_expression->left, extra_block,
2891 mature_immBlock(extra_block);
2892 set_cur_block(extra_block);
2893 create_condition_evaluation(binary_expression->right, true_block,
2897 case EXPR_BINARY_LOGICAL_OR: {
2898 const binary_expression_t *binary_expression = &expression->binary;
2900 ir_node *cur_block = get_cur_block();
2901 ir_node *extra_block = new_immBlock();
2902 set_cur_block(cur_block);
2903 create_condition_evaluation(binary_expression->left, true_block,
2905 mature_immBlock(extra_block);
2906 set_cur_block(extra_block);
2907 create_condition_evaluation(binary_expression->right, true_block,
2915 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2916 ir_node *condition = expression_to_modeb(expression);
2917 ir_node *cond = new_d_Cond(dbgi, condition);
2918 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2919 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2921 /* set branch prediction info based on __builtin_expect */
2922 if(expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
2923 long cnst = fold_constant(expression->binary.right);
2924 cond_jmp_predicate pred;
2927 pred = COND_JMP_PRED_FALSE;
2929 pred = COND_JMP_PRED_TRUE;
2931 set_Cond_jmp_pred(cond, pred);
2934 add_immBlock_pred(true_block, true_proj);
2935 add_immBlock_pred(false_block, false_proj);
2937 set_cur_block(NULL);
2942 static void create_declaration_entity(declaration_t *declaration,
2943 declaration_kind_t declaration_kind,
2944 ir_type *parent_type)
2946 ident *const id = new_id_from_str(declaration->symbol->string);
2947 ir_type *const irtype = get_ir_type(declaration->type);
2948 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2949 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2950 set_entity_ld_ident(entity, id);
2952 declaration->declaration_kind = (unsigned char) declaration_kind;
2953 declaration->v.entity = entity;
2954 set_entity_variability(entity, variability_uninitialized);
2955 if(parent_type == get_tls_type())
2956 set_entity_allocation(entity, allocation_automatic);
2957 else if(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
2958 set_entity_allocation(entity, allocation_static);
2959 /* TODO: visibility? */
2963 typedef struct type_path_entry_t type_path_entry_t;
2964 struct type_path_entry_t {
2966 ir_initializer_t *initializer;
2968 declaration_t *compound_entry;
2971 typedef struct type_path_t type_path_t;
2972 struct type_path_t {
2973 type_path_entry_t *path;
2978 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
2980 size_t len = ARR_LEN(path->path);
2982 for(size_t i = 0; i < len; ++i) {
2983 const type_path_entry_t *entry = & path->path[i];
2985 type_t *type = skip_typeref(entry->type);
2986 if(is_type_compound(type)) {
2987 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
2988 } else if(is_type_array(type)) {
2989 fprintf(stderr, "[%u]", entry->index);
2991 fprintf(stderr, "-INVALID-");
2994 fprintf(stderr, " (");
2995 print_type(path->top_type);
2996 fprintf(stderr, ")");
2999 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3001 size_t len = ARR_LEN(path->path);
3003 return & path->path[len-1];
3006 static type_path_entry_t *append_to_type_path(type_path_t *path)
3008 size_t len = ARR_LEN(path->path);
3009 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3011 type_path_entry_t *result = & path->path[len];
3012 memset(result, 0, sizeof(result[0]));
3016 static size_t get_compound_size(const compound_type_t *type)
3018 declaration_t *declaration = type->declaration;
3019 declaration_t *member = declaration->scope.declarations;
3021 for( ; member != NULL; member = member->next) {
3024 /* TODO: cache results? */
3029 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3031 type_t *orig_top_type = path->top_type;
3032 type_t *top_type = skip_typeref(orig_top_type);
3034 assert(is_type_compound(top_type) || is_type_array(top_type));
3036 if(ARR_LEN(path->path) == 0) {
3039 type_path_entry_t *top = get_type_path_top(path);
3040 ir_initializer_t *initializer = top->initializer;
3041 return get_initializer_compound_value(initializer, top->index);
3045 static void descend_into_subtype(type_path_t *path)
3047 type_t *orig_top_type = path->top_type;
3048 type_t *top_type = skip_typeref(orig_top_type);
3050 assert(is_type_compound(top_type) || is_type_array(top_type));
3052 ir_initializer_t *initializer = get_initializer_entry(path);
3054 type_path_entry_t *top = append_to_type_path(path);
3055 top->type = top_type;
3059 if(is_type_compound(top_type)) {
3060 declaration_t *declaration = top_type->compound.declaration;
3061 declaration_t *entry = declaration->scope.declarations;
3063 top->compound_entry = entry;
3065 len = get_compound_size(&top_type->compound);
3067 path->top_type = entry->type;
3069 assert(is_type_array(top_type));
3070 assert(top_type->array.size > 0);
3073 path->top_type = top_type->array.element_type;
3074 len = top_type->array.size;
3076 if(initializer == NULL
3077 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3078 initializer = create_initializer_compound(len);
3079 /* we have to set the entry at the 2nd latest path entry... */
3080 size_t path_len = ARR_LEN(path->path);
3081 assert(path_len >= 1);
3083 type_path_entry_t *entry = & path->path[path_len-2];
3084 ir_initializer_t *tinitializer = entry->initializer;
3085 set_initializer_compound_value(tinitializer, entry->index,
3089 top->initializer = initializer;
3092 static void ascend_from_subtype(type_path_t *path)
3094 type_path_entry_t *top = get_type_path_top(path);
3096 path->top_type = top->type;
3098 size_t len = ARR_LEN(path->path);
3099 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3102 static void walk_designator(type_path_t *path, const designator_t *designator)
3104 /* designators start at current object type */
3105 ARR_RESIZE(type_path_entry_t, path->path, 1);
3107 for( ; designator != NULL; designator = designator->next) {
3108 type_path_entry_t *top = get_type_path_top(path);
3109 type_t *orig_type = top->type;
3110 type_t *type = skip_typeref(orig_type);
3112 if(designator->symbol != NULL) {
3113 assert(is_type_compound(type));
3115 symbol_t *symbol = designator->symbol;
3117 declaration_t *declaration = type->compound.declaration;
3118 declaration_t *iter = declaration->scope.declarations;
3119 for( ; iter != NULL; iter = iter->next, ++index) {
3120 if(iter->symbol == symbol) {
3124 assert(iter != NULL);
3126 top->type = orig_type;
3127 top->compound_entry = iter;
3129 orig_type = iter->type;
3131 expression_t *array_index = designator->array_index;
3132 assert(designator->array_index != NULL);
3133 assert(is_type_array(type));
3134 assert(is_type_valid(array_index->base.type));
3136 long index = fold_constant(array_index);
3139 if(type->array.size_constant == 1) {
3140 long array_size = type->array.size;
3141 assert(index < array_size);
3145 top->type = orig_type;
3146 top->index = (size_t) index;
3147 orig_type = type->array.element_type;
3149 path->top_type = orig_type;
3151 if(designator->next != NULL) {
3152 descend_into_subtype(path);
3156 path->invalid = false;
3159 static void advance_current_object(type_path_t *path)
3162 /* TODO: handle this... */
3163 panic("invalid initializer in ast2firm (excessive elements)");
3167 type_path_entry_t *top = get_type_path_top(path);
3169 type_t *type = skip_typeref(top->type);
3170 if(is_type_union(type)) {
3171 top->compound_entry = NULL;
3172 } else if(is_type_struct(type)) {
3173 declaration_t *entry = top->compound_entry;
3176 entry = entry->next;
3177 top->compound_entry = entry;
3179 path->top_type = entry->type;
3183 assert(is_type_array(type));
3186 if(!type->array.size_constant || top->index < type->array.size) {
3191 /* we're past the last member of the current sub-aggregate, try if we
3192 * can ascend in the type hierarchy and continue with another subobject */
3193 size_t len = ARR_LEN(path->path);
3196 ascend_from_subtype(path);
3197 advance_current_object(path);
3199 path->invalid = true;
3204 static ir_initializer_t *create_ir_initializer(
3205 const initializer_t *initializer, type_t *type);
3207 static ir_initializer_t *create_ir_initializer_value(
3208 const initializer_value_t *initializer)
3210 ir_node *value = expression_to_firm(initializer->value);
3211 return create_initializer_const(value);
3214 static ir_initializer_t *create_ir_initializer_list(
3215 const initializer_list_t *initializer, type_t *type)
3218 memset(&path, 0, sizeof(path));
3219 path.top_type = type;
3220 path.path = NEW_ARR_F(type_path_entry_t, 0);
3222 descend_into_subtype(&path);
3224 for(size_t i = 0; i < initializer->len; ++i) {
3225 const initializer_t *sub_initializer = initializer->initializers[i];
3227 if(sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3228 walk_designator(&path, sub_initializer->designator.designator);
3232 if(sub_initializer->kind == INITIALIZER_VALUE) {
3233 /* we might have to descend into types until we're at a scalar
3236 type_t *orig_top_type = path.top_type;
3237 type_t *top_type = skip_typeref(orig_top_type);
3239 if(is_type_scalar(top_type))
3241 descend_into_subtype(&path);
3245 ir_initializer_t *sub_irinitializer
3246 = create_ir_initializer(sub_initializer, path.top_type);
3248 size_t path_len = ARR_LEN(path.path);
3249 assert(path_len >= 1);
3250 type_path_entry_t *entry = & path.path[path_len-1];
3251 ir_initializer_t *tinitializer = entry->initializer;
3252 set_initializer_compound_value(tinitializer, entry->index,
3255 advance_current_object(&path);
3258 assert(ARR_LEN(path.path) >= 1);
3259 ir_initializer_t *result = path.path[0].initializer;
3260 DEL_ARR_F(path.path);
3265 static ir_initializer_t *create_ir_initializer_string(
3266 const initializer_string_t *initializer, type_t *type)
3268 size_t string_len = initializer->string.size;
3269 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3270 size_t len = type->array.size;
3271 ir_initializer_t *irinitializer = create_initializer_compound(len);
3273 const char *string = initializer->string.begin;
3274 ir_mode *mode = get_type_mode(ir_type_const_char);
3276 for(size_t i = 0; i < len; ++i) {
3281 tarval *tv = new_tarval_from_long(string[i], mode);
3282 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3284 set_initializer_compound_value(irinitializer, i, char_initializer);
3287 return irinitializer;
3290 static ir_initializer_t *create_ir_initializer_wide_string(
3291 const initializer_wide_string_t *initializer, type_t *type)
3293 size_t string_len = initializer->string.size;
3294 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3295 size_t len = type->array.size;
3296 ir_initializer_t *irinitializer = create_initializer_compound(len);
3298 const wchar_rep_t *string = initializer->string.begin;
3299 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3301 for(size_t i = 0; i < len; ++i) {
3303 if(i < string_len) {
3306 tarval *tv = new_tarval_from_long(string[i], mode);
3307 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3309 set_initializer_compound_value(irinitializer, i, char_initializer);
3312 return irinitializer;
3315 static ir_initializer_t *create_ir_initializer(
3316 const initializer_t *initializer, type_t *type)
3318 switch(initializer->kind) {
3319 case INITIALIZER_STRING:
3320 return create_ir_initializer_string(&initializer->string, type);
3322 case INITIALIZER_WIDE_STRING:
3323 return create_ir_initializer_wide_string(&initializer->wide_string,
3326 case INITIALIZER_LIST:
3327 return create_ir_initializer_list(&initializer->list, type);
3329 case INITIALIZER_VALUE:
3330 return create_ir_initializer_value(&initializer->value);
3332 case INITIALIZER_DESIGNATOR:
3333 panic("unexpected designator initializer found");
3335 panic("unknown initializer");
3338 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3339 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3341 switch(get_initializer_kind(initializer)) {
3342 case IR_INITIALIZER_NULL: {
3343 /* TODO: implement this for compound types... */
3344 assert(type != NULL);
3346 ir_mode *mode = get_type_mode(type);
3347 tarval *zero = get_mode_null(mode);
3348 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3350 /* TODO: bitfields */
3351 ir_node *mem = get_store();
3352 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3353 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3357 case IR_INITIALIZER_CONST: {
3358 ir_node *node = get_initializer_const_value(initializer);
3359 ir_mode *mode = get_irn_mode(node);
3360 assert(get_type_mode(type) == mode);
3362 /* TODO: bitfields... */
3363 ir_node *mem = get_store();
3364 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3365 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3369 case IR_INITIALIZER_TARVAL: {
3370 tarval *tv = get_initializer_tarval_value(initializer);
3371 ir_mode *mode = get_tarval_mode(tv);
3372 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3373 assert(get_type_mode(type) == mode);
3375 /* TODO: bitfields... */
3376 ir_node *mem = get_store();
3377 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3378 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3382 case IR_INITIALIZER_COMPOUND: {
3383 assert(is_compound_type(type));
3385 if(is_Array_type(type)) {
3386 assert(has_array_upper_bound(type, 0));
3387 n_members = get_array_upper_bound_int(type, 0);
3389 n_members = get_compound_n_members(type);
3392 if(get_initializer_compound_n_entries(initializer)
3393 != (unsigned) n_members)
3394 panic("initializer doesn't match compound type");
3396 for(int i = 0; i < n_members; ++i) {
3399 if(is_Array_type(type)) {
3400 ir_entity *entity = get_array_element_entity(type);
3401 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3402 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3403 ir_node *in[1] = { cnst };
3404 irtype = get_array_element_type(type);
3405 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3407 ir_entity *member = get_compound_member(type, i);
3409 irtype = get_entity_type(member);
3410 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3413 ir_initializer_t *sub_init
3414 = get_initializer_compound_value(initializer, i);
3416 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3422 panic("invalid IR_INITIALIZER found");
3425 static void create_dynamic_initializer(ir_initializer_t *initializer,
3426 dbg_info *dbgi, ir_entity *entity)
3428 ir_node *frame = get_local_frame(entity);
3429 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3430 ir_type *type = get_entity_type(entity);
3432 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3435 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3436 ir_entity *entity, type_t *type)
3438 ir_node *memory = get_store();
3439 ir_node *nomem = new_NoMem();
3440 ir_node *frame = get_irg_frame(current_ir_graph);
3441 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3443 if(initializer->kind == INITIALIZER_VALUE) {
3444 initializer_value_t *initializer_value = &initializer->value;
3446 ir_node *value = expression_to_firm(initializer_value->value);
3447 type = skip_typeref(type);
3448 assign_value(dbgi, addr, type, value);
3452 if(!is_constant_initializer(initializer)) {
3453 ir_initializer_t *irinitializer
3454 = create_ir_initializer(initializer, type);
3456 create_dynamic_initializer(irinitializer, dbgi, entity);
3460 /* create the ir_initializer */
3461 ir_graph *const old_current_ir_graph = current_ir_graph;
3462 current_ir_graph = get_const_code_irg();
3464 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3466 assert(current_ir_graph == get_const_code_irg());
3467 current_ir_graph = old_current_ir_graph;
3469 /* create a "template" entity which is copied to the entity on the stack */
3470 ident *const id = unique_ident("initializer");
3471 ir_type *const irtype = get_ir_type(type);
3472 ir_type *const global_type = get_glob_type();
3473 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3474 set_entity_ld_ident(init_entity, id);
3476 set_entity_variability(init_entity, variability_initialized);
3477 set_entity_visibility(init_entity, visibility_local);
3478 set_entity_allocation(init_entity, allocation_static);
3480 set_entity_initializer(init_entity, irinitializer);
3482 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3483 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3485 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3486 set_store(copyb_mem);
3489 static void create_initializer_local_variable_entity(declaration_t *declaration)
3491 initializer_t *initializer = declaration->init.initializer;
3492 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3493 ir_entity *entity = declaration->v.entity;
3494 type_t *type = declaration->type;
3495 create_local_initializer(initializer, dbgi, entity, type);
3498 static void create_declaration_initializer(declaration_t *declaration)
3500 initializer_t *initializer = declaration->init.initializer;
3501 if(initializer == NULL)
3504 declaration_kind_t declaration_kind
3505 = (declaration_kind_t) declaration->declaration_kind;
3506 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3507 create_initializer_local_variable_entity(declaration);
3511 if(initializer->kind == INITIALIZER_VALUE) {
3512 initializer_value_t *initializer_value = &initializer->value;
3514 ir_node *value = expression_to_firm(initializer_value->value);
3516 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3517 set_value(declaration->v.value_number, value);
3519 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3521 ir_entity *entity = declaration->v.entity;
3523 set_entity_variability(entity, variability_initialized);
3524 set_atomic_ent_value(entity, value);
3527 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3528 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3530 ir_entity *entity = declaration->v.entity;
3531 ir_initializer_t *irinitializer
3532 = create_ir_initializer(initializer, declaration->type);
3534 set_entity_variability(entity, variability_initialized);
3535 set_entity_initializer(entity, irinitializer);
3539 static void create_variable_length_array(declaration_t *declaration)
3541 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3542 type_t *type = declaration->type;
3543 ir_node *mem = get_store();
3544 ir_type *el_type = get_ir_type(type->array.element_type);
3546 /* make sure size_node is calculated */
3547 get_type_size(type);
3548 ir_node *elems = type->array.size_node;
3549 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3551 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3552 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3555 /* initializers are not allowed for VLAs */
3556 assert(declaration->init.initializer == NULL);
3558 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3559 declaration->v.vla_base = addr;
3561 /* TODO: record VLA somewhere so we create the free node when we leave
3566 * Creates a Firm local variable from a declaration.
3568 static void create_local_variable(declaration_t *declaration)
3570 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3572 bool needs_entity = declaration->address_taken;
3573 type_t *type = skip_typeref(declaration->type);
3575 /* is it a variable length array? */
3576 if(is_type_array(type) && !type->array.size_constant) {
3577 create_variable_length_array(declaration);
3579 } else if(is_type_array(type) || is_type_compound(type)) {
3580 needs_entity = true;
3584 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3585 create_declaration_entity(declaration,
3586 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3589 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3590 declaration->v.value_number = next_value_number_function;
3591 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3592 ++next_value_number_function;
3595 create_declaration_initializer(declaration);
3598 static void create_local_static_variable(declaration_t *declaration)
3600 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3602 type_t *const type = skip_typeref(declaration->type);
3603 ir_type *const global_type = get_glob_type();
3604 ident *const id = unique_ident(declaration->symbol->string);
3605 ir_type *const irtype = get_ir_type(type);
3606 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3607 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3608 set_entity_ld_ident(entity, id);
3610 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3611 declaration->v.entity = entity;
3612 set_entity_variability(entity, variability_uninitialized);
3613 set_entity_visibility(entity, visibility_local);
3614 set_entity_allocation(entity, allocation_static);
3616 ir_graph *const old_current_ir_graph = current_ir_graph;
3617 current_ir_graph = get_const_code_irg();
3619 create_declaration_initializer(declaration);
3621 assert(current_ir_graph == get_const_code_irg());
3622 current_ir_graph = old_current_ir_graph;
3627 static void return_statement_to_firm(return_statement_t *statement)
3629 if(get_cur_block() == NULL)
3632 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3633 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3638 if(get_method_n_ress(func_irtype) > 0) {
3639 ir_type *res_type = get_method_res_type(func_irtype, 0);
3641 if(statement->value != NULL) {
3642 ir_node *node = expression_to_firm(statement->value);
3643 node = do_strict_conv(dbgi, node);
3647 if(is_compound_type(res_type)) {
3650 mode = get_type_mode(res_type);
3652 in[0] = new_Unknown(mode);
3656 /* build return_value for its side effects */
3657 if(statement->value != NULL) {
3658 expression_to_firm(statement->value);
3663 ir_node *store = get_store();
3664 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3666 ir_node *end_block = get_irg_end_block(current_ir_graph);
3667 add_immBlock_pred(end_block, ret);
3669 set_cur_block(NULL);
3672 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3674 if(get_cur_block() == NULL)
3677 return expression_to_firm(statement->expression);
3680 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3682 ir_node *result = NULL;
3683 statement_t *statement = compound->statements;
3684 for( ; statement != NULL; statement = statement->base.next) {
3685 //context2firm(&statement->scope);
3687 if(statement->base.next == NULL
3688 && statement->kind == STATEMENT_EXPRESSION) {
3689 result = expression_statement_to_firm(
3690 &statement->expression);
3693 statement_to_firm(statement);
3699 static void create_global_variable(declaration_t *declaration)
3703 switch ((storage_class_tag_t)declaration->storage_class) {
3704 case STORAGE_CLASS_STATIC:
3705 vis = visibility_local;
3708 case STORAGE_CLASS_EXTERN:
3709 vis = visibility_external_allocated;
3712 case STORAGE_CLASS_NONE:
3713 vis = visibility_external_visible;
3716 case STORAGE_CLASS_THREAD:
3717 vis = visibility_external_visible;
3720 case STORAGE_CLASS_THREAD_EXTERN:
3721 vis = visibility_external_allocated;
3724 case STORAGE_CLASS_THREAD_STATIC:
3725 vis = visibility_local;
3729 var_type = get_tls_type();
3733 var_type = get_glob_type();
3737 create_declaration_entity(declaration,
3738 DECLARATION_KIND_GLOBAL_VARIABLE,
3740 set_entity_visibility(declaration->v.entity, vis);
3744 case STORAGE_CLASS_TYPEDEF:
3745 case STORAGE_CLASS_AUTO:
3746 case STORAGE_CLASS_REGISTER:
3747 case STORAGE_CLASS_ENUM_ENTRY:
3750 panic("Invalid storage class for global variable");
3753 static void create_local_declaration(declaration_t *declaration)
3755 if(declaration->symbol == NULL)
3758 type_t *type = skip_typeref(declaration->type);
3760 switch ((storage_class_tag_t) declaration->storage_class) {
3761 case STORAGE_CLASS_STATIC:
3762 create_local_static_variable(declaration);
3764 case STORAGE_CLASS_EXTERN:
3765 create_global_variable(declaration);
3766 create_declaration_initializer(declaration);
3768 case STORAGE_CLASS_NONE:
3769 case STORAGE_CLASS_AUTO:
3770 case STORAGE_CLASS_REGISTER:
3771 if(is_type_function(type)) {
3772 if(declaration->init.statement != NULL) {
3773 panic("nested functions not supported yet");
3775 get_function_entity(declaration);
3778 create_local_variable(declaration);
3781 case STORAGE_CLASS_ENUM_ENTRY:
3782 case STORAGE_CLASS_TYPEDEF:
3783 case STORAGE_CLASS_THREAD:
3784 case STORAGE_CLASS_THREAD_EXTERN:
3785 case STORAGE_CLASS_THREAD_STATIC:
3788 panic("invalid storage class found");
3791 static void declaration_statement_to_firm(declaration_statement_t *statement)
3793 declaration_t *declaration = statement->declarations_begin;
3794 declaration_t *end = statement->declarations_end->next;
3795 for( ; declaration != end; declaration = declaration->next) {
3796 if(declaration->namespc != NAMESPACE_NORMAL)
3798 create_local_declaration(declaration);
3802 static void if_statement_to_firm(if_statement_t *statement)
3804 ir_node *cur_block = get_cur_block();
3806 ir_node *fallthrough_block = new_immBlock();
3808 /* the true (blocks) */
3809 ir_node *true_block;
3810 if (statement->true_statement != NULL) {
3811 true_block = new_immBlock();
3812 statement_to_firm(statement->true_statement);
3813 if(get_cur_block() != NULL) {
3814 ir_node *jmp = new_Jmp();
3815 add_immBlock_pred(fallthrough_block, jmp);
3818 true_block = fallthrough_block;
3821 /* the false (blocks) */
3822 ir_node *false_block;
3823 if(statement->false_statement != NULL) {
3824 false_block = new_immBlock();
3826 statement_to_firm(statement->false_statement);
3827 if(get_cur_block() != NULL) {
3828 ir_node *jmp = new_Jmp();
3829 add_immBlock_pred(fallthrough_block, jmp);
3832 false_block = fallthrough_block;
3835 /* create the condition */
3836 if(cur_block != NULL) {
3837 set_cur_block(cur_block);
3838 create_condition_evaluation(statement->condition, true_block,
3842 mature_immBlock(true_block);
3843 if(false_block != fallthrough_block) {
3844 mature_immBlock(false_block);
3846 mature_immBlock(fallthrough_block);
3848 set_cur_block(fallthrough_block);
3851 static void while_statement_to_firm(while_statement_t *statement)
3853 ir_node *jmp = NULL;
3854 if(get_cur_block() != NULL) {
3858 /* create the header block */
3859 ir_node *header_block = new_immBlock();
3861 add_immBlock_pred(header_block, jmp);
3864 /* the false block */
3865 ir_node *false_block = new_immBlock();
3868 ir_node *body_block;
3869 if (statement->body != NULL) {
3870 ir_node *old_continue_label = continue_label;
3871 ir_node *old_break_label = break_label;
3872 continue_label = header_block;
3873 break_label = false_block;
3875 body_block = new_immBlock();
3876 statement_to_firm(statement->body);
3878 assert(continue_label == header_block);
3879 assert(break_label == false_block);
3880 continue_label = old_continue_label;
3881 break_label = old_break_label;
3883 if(get_cur_block() != NULL) {
3885 add_immBlock_pred(header_block, jmp);
3888 body_block = header_block;
3891 /* create the condition */
3892 set_cur_block(header_block);
3894 create_condition_evaluation(statement->condition, body_block, false_block);
3895 mature_immBlock(body_block);
3896 mature_immBlock(false_block);
3897 mature_immBlock(header_block);
3899 set_cur_block(false_block);
3902 static void do_while_statement_to_firm(do_while_statement_t *statement)
3904 ir_node *jmp = NULL;
3905 if(get_cur_block() != NULL) {
3909 /* create the header block */
3910 ir_node *header_block = new_immBlock();
3912 /* the false block */
3913 ir_node *false_block = new_immBlock();
3916 ir_node *body_block = new_immBlock();
3918 add_immBlock_pred(body_block, jmp);
3921 if (statement->body != NULL) {
3922 ir_node *old_continue_label = continue_label;
3923 ir_node *old_break_label = break_label;
3924 continue_label = header_block;
3925 break_label = false_block;
3927 statement_to_firm(statement->body);
3929 assert(continue_label == header_block);
3930 assert(break_label == false_block);
3931 continue_label = old_continue_label;
3932 break_label = old_break_label;
3934 if (get_cur_block() == NULL) {
3935 mature_immBlock(header_block);
3936 mature_immBlock(body_block);
3937 mature_immBlock(false_block);
3942 ir_node *body_jmp = new_Jmp();
3943 add_immBlock_pred(header_block, body_jmp);
3944 mature_immBlock(header_block);
3946 /* create the condition */
3947 set_cur_block(header_block);
3949 create_condition_evaluation(statement->condition, body_block, false_block);
3950 mature_immBlock(body_block);
3951 mature_immBlock(false_block);
3952 mature_immBlock(header_block);
3954 set_cur_block(false_block);
3957 static void for_statement_to_firm(for_statement_t *statement)
3959 ir_node *jmp = NULL;
3960 if (get_cur_block() != NULL) {
3961 if(statement->initialisation != NULL) {
3962 expression_to_firm(statement->initialisation);
3965 /* create declarations */
3966 declaration_t *declaration = statement->scope.declarations;
3967 for( ; declaration != NULL; declaration = declaration->next) {
3968 create_local_declaration(declaration);
3975 /* create the step block */
3976 ir_node *const step_block = new_immBlock();
3977 if (statement->step != NULL) {
3978 expression_to_firm(statement->step);
3980 ir_node *const step_jmp = new_Jmp();
3982 /* create the header block */
3983 ir_node *const header_block = new_immBlock();
3985 add_immBlock_pred(header_block, jmp);
3987 add_immBlock_pred(header_block, step_jmp);
3989 /* the false block */
3990 ir_node *const false_block = new_immBlock();
3993 ir_node * body_block;
3994 if (statement->body != NULL) {
3995 ir_node *const old_continue_label = continue_label;
3996 ir_node *const old_break_label = break_label;
3997 continue_label = step_block;
3998 break_label = false_block;
4000 body_block = new_immBlock();
4001 statement_to_firm(statement->body);
4003 assert(continue_label == step_block);
4004 assert(break_label == false_block);
4005 continue_label = old_continue_label;
4006 break_label = old_break_label;
4008 if (get_cur_block() != NULL) {
4010 add_immBlock_pred(step_block, jmp);
4013 body_block = step_block;
4016 /* create the condition */
4017 set_cur_block(header_block);
4018 if (statement->condition != NULL) {
4019 create_condition_evaluation(statement->condition, body_block,
4022 keep_alive(header_block);
4024 add_immBlock_pred(body_block, jmp);
4027 mature_immBlock(body_block);
4028 mature_immBlock(false_block);
4029 mature_immBlock(step_block);
4030 mature_immBlock(header_block);
4031 mature_immBlock(false_block);
4033 set_cur_block(false_block);
4036 static void create_jump_statement(const statement_t *statement,
4037 ir_node *target_block)
4039 if(get_cur_block() == NULL)
4042 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4043 ir_node *jump = new_d_Jmp(dbgi);
4044 add_immBlock_pred(target_block, jump);
4046 set_cur_block(NULL);
4049 static void switch_statement_to_firm(const switch_statement_t *statement)
4051 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4053 ir_node *expression = expression_to_firm(statement->expression);
4054 ir_node *cond = new_d_Cond(dbgi, expression);
4055 ir_node *break_block = new_immBlock();
4057 set_cur_block(NULL);
4059 ir_node *const old_switch_cond = current_switch_cond;
4060 ir_node *const old_break_label = break_label;
4061 const bool old_saw_default_label = saw_default_label;
4062 current_switch_cond = cond;
4063 break_label = break_block;
4065 if (statement->body != NULL) {
4066 statement_to_firm(statement->body);
4069 if(get_cur_block() != NULL) {
4070 ir_node *jmp = new_Jmp();
4071 add_immBlock_pred(break_block, jmp);
4074 if (!saw_default_label) {
4075 set_cur_block(get_nodes_block(cond));
4076 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4077 MAGIC_DEFAULT_PN_NUMBER);
4078 add_immBlock_pred(break_block, proj);
4081 assert(current_switch_cond == cond);
4082 assert(break_label == break_block);
4083 current_switch_cond = old_switch_cond;
4084 break_label = old_break_label;
4085 saw_default_label = old_saw_default_label;
4087 mature_immBlock(break_block);
4088 set_cur_block(break_block);
4091 static void case_label_to_firm(const case_label_statement_t *statement)
4093 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4095 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4097 /* let's create a node and hope firm constant folding creates a Const
4100 ir_node *old_block = get_nodes_block(current_switch_cond);
4101 ir_node *block = new_immBlock();
4103 set_cur_block(old_block);
4104 if(statement->expression != NULL) {
4105 long start_pn = fold_constant(statement->expression);
4106 long end_pn = start_pn;
4107 if (statement->end_range != NULL) {
4108 end_pn = fold_constant(statement->end_range);
4110 assert(start_pn <= end_pn);
4111 /* create jumps for all cases in the given range */
4112 for (long pn = start_pn; pn <= end_pn; ++pn) {
4113 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
4114 /* oops someone detected our cheating... */
4115 panic("magic default pn used");
4117 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4118 add_immBlock_pred(block, proj);
4121 saw_default_label = true;
4122 proj = new_d_defaultProj(dbgi, current_switch_cond,
4123 MAGIC_DEFAULT_PN_NUMBER);
4125 add_immBlock_pred(block, proj);
4128 if (fallthrough != NULL) {
4129 add_immBlock_pred(block, fallthrough);
4131 mature_immBlock(block);
4132 set_cur_block(block);
4134 if(statement->statement != NULL) {
4135 statement_to_firm(statement->statement);
4139 static ir_node *get_label_block(declaration_t *label)
4141 assert(label->namespc == NAMESPACE_LABEL);
4143 if(label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
4144 return label->v.block;
4146 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
4148 ir_node *old_cur_block = get_cur_block();
4149 ir_node *block = new_immBlock();
4150 set_cur_block(old_cur_block);
4152 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
4153 label->v.block = block;
4155 ARR_APP1(ir_node *, imature_blocks, block);
4160 static void label_to_firm(const label_statement_t *statement)
4162 ir_node *block = get_label_block(statement->label);
4164 if(get_cur_block() != NULL) {
4165 ir_node *jmp = new_Jmp();
4166 add_immBlock_pred(block, jmp);
4169 set_cur_block(block);
4172 if(statement->statement != NULL) {
4173 statement_to_firm(statement->statement);
4177 static void goto_to_firm(const goto_statement_t *statement)
4179 if(get_cur_block() == NULL)
4182 ir_node *block = get_label_block(statement->label);
4183 ir_node *jmp = new_Jmp();
4184 add_immBlock_pred(block, jmp);
4186 set_cur_block(NULL);
4189 typedef enum modifier_t {
4190 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
4191 ASM_MODIFIER_READ_WRITE = 1 << 1,
4192 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
4193 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
4196 static void asm_statement_to_firm(const asm_statement_t *statement)
4199 fprintf(stderr, "WARNING asm not implemented yet!\n");
4201 bool needs_memory = false;
4203 size_t n_clobbers = 0;
4204 asm_clobber_t *clobber = statement->clobbers;
4205 for( ; clobber != NULL; clobber = clobber->next) {
4206 if(strcmp(clobber->clobber, "memory") == 0) {
4207 needs_memory = true;
4211 ident *id = new_id_from_str(clobber->clobber);
4212 obstack_ptr_grow(&asm_obst, id);
4215 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4216 ident **clobbers = NULL;
4217 if(n_clobbers > 0) {
4218 clobbers = obstack_finish(&asm_obst);
4221 /* find and count input and output constraints */
4222 asm_constraint_t *constraint = statement->inputs;
4223 for( ; constraint != NULL; constraint = constraint->next) {
4225 bool supports_memop = false;
4226 for(const char *c = constraint->constraints; *c != 0; ++c) {
4227 /* TODO: improve error messages */
4231 panic("multiple alternative assembler constraints not "
4239 supports_memop = true;
4240 obstack_1grow(&asm_obst, *c);
4243 if(modifiers & ASM_MODIFIER_READ_WRITE)
4244 panic("inconsistent register constraints");
4245 modifiers |= ASM_MODIFIER_WRITE_ONLY;
4248 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
4249 panic("inconsistent register constraints");
4250 modifiers |= ASM_MODIFIER_READ_WRITE;
4253 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
4254 panic("early clobber assembler constraint not supported yet");
4257 modifiers |= ASM_MODIFIER_COMMUTATIVE;
4258 panic("commutative assembler constraint not supported yet");
4261 /* skip register preferences stuff... */
4262 while(*c != 0 && *c != ',')
4266 /* skip register preferences stuff... */
4270 obstack_1grow(&asm_obst, *c);
4274 obstack_1grow(&asm_obst, '\0');
4275 const char *constraint_string = obstack_finish(&asm_obst);
4277 needs_memory |= supports_memop;
4278 if(supports_memop) {
4285 static void statement_to_firm(statement_t *statement)
4287 switch(statement->kind) {
4288 case STATEMENT_INVALID:
4289 panic("invalid statement found");
4290 case STATEMENT_COMPOUND:
4291 compound_statement_to_firm(&statement->compound);
4293 case STATEMENT_RETURN:
4294 return_statement_to_firm(&statement->returns);
4296 case STATEMENT_EXPRESSION:
4297 expression_statement_to_firm(&statement->expression);
4300 if_statement_to_firm(&statement->ifs);
4302 case STATEMENT_WHILE:
4303 while_statement_to_firm(&statement->whiles);
4305 case STATEMENT_DO_WHILE:
4306 do_while_statement_to_firm(&statement->do_while);
4308 case STATEMENT_DECLARATION:
4309 declaration_statement_to_firm(&statement->declaration);
4311 case STATEMENT_BREAK:
4312 create_jump_statement(statement, break_label);
4314 case STATEMENT_CONTINUE:
4315 create_jump_statement(statement, continue_label);
4317 case STATEMENT_SWITCH:
4318 switch_statement_to_firm(&statement->switchs);
4320 case STATEMENT_CASE_LABEL:
4321 case_label_to_firm(&statement->case_label);
4324 for_statement_to_firm(&statement->fors);
4326 case STATEMENT_LABEL:
4327 label_to_firm(&statement->label);
4329 case STATEMENT_GOTO:
4330 goto_to_firm(&statement->gotos);
4333 asm_statement_to_firm(&statement->asms);
4336 panic("Statement not implemented\n");
4339 static int count_decls_in_expression(const expression_t *expression);
4341 static int count_local_declarations(const declaration_t * decl,
4342 const declaration_t *const end)
4345 for (; decl != end; decl = decl->next) {
4346 if(decl->namespc != NAMESPACE_NORMAL)
4348 const type_t *type = skip_typeref(decl->type);
4349 if (!decl->address_taken && is_type_scalar(type))
4351 const initializer_t *initializer = decl->init.initializer;
4352 /* FIXME: should walk initializer hierarchies... */
4353 if(initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4354 count += count_decls_in_expression(initializer->value.value);
4360 static int count_decls_in_expression(const expression_t *expression) {
4361 if(expression == NULL)
4364 switch(expression->base.kind) {
4365 case EXPR_STATEMENT:
4366 return count_decls_in_stmts(expression->statement.statement);
4368 int count_left = count_decls_in_expression(expression->binary.left);
4369 int count_right = count_decls_in_expression(expression->binary.right);
4370 return count_left + count_right;
4373 return count_decls_in_expression(expression->unary.value);
4376 call_argument_t *argument = expression->call.arguments;
4377 for( ; argument != NULL; argument = argument->next) {
4378 count += count_decls_in_expression(argument->expression);
4387 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
4388 * (or implement all the missing expressions here/implement a walker)
4394 static int count_decls_in_stmts(const statement_t *stmt)
4397 for (; stmt != NULL; stmt = stmt->base.next) {
4398 switch (stmt->kind) {
4399 case STATEMENT_DECLARATION: {
4400 const declaration_statement_t *const decl_stmt = &stmt->declaration;
4401 count += count_local_declarations(decl_stmt->declarations_begin,
4402 decl_stmt->declarations_end->next);
4406 case STATEMENT_COMPOUND: {
4407 const compound_statement_t *const comp =
4409 count += count_decls_in_stmts(comp->statements);
4413 case STATEMENT_IF: {
4414 const if_statement_t *const if_stmt = &stmt->ifs;
4415 count += count_decls_in_expression(if_stmt->condition);
4416 count += count_decls_in_stmts(if_stmt->true_statement);
4417 count += count_decls_in_stmts(if_stmt->false_statement);
4421 case STATEMENT_SWITCH: {
4422 const switch_statement_t *const switch_stmt = &stmt->switchs;
4423 count += count_decls_in_expression(switch_stmt->expression);
4424 count += count_decls_in_stmts(switch_stmt->body);
4428 case STATEMENT_LABEL: {
4429 const label_statement_t *const label_stmt = &stmt->label;
4430 if(label_stmt->statement != NULL) {
4431 count += count_decls_in_stmts(label_stmt->statement);
4436 case STATEMENT_WHILE: {
4437 const while_statement_t *const while_stmt = &stmt->whiles;
4438 count += count_decls_in_expression(while_stmt->condition);
4439 count += count_decls_in_stmts(while_stmt->body);
4443 case STATEMENT_DO_WHILE: {
4444 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
4445 count += count_decls_in_expression(do_while_stmt->condition);
4446 count += count_decls_in_stmts(do_while_stmt->body);
4450 case STATEMENT_FOR: {
4451 const for_statement_t *const for_stmt = &stmt->fors;
4452 count += count_local_declarations(for_stmt->scope.declarations, NULL);
4453 count += count_decls_in_expression(for_stmt->initialisation);
4454 count += count_decls_in_expression(for_stmt->condition);
4455 count += count_decls_in_expression(for_stmt->step);
4456 count += count_decls_in_stmts(for_stmt->body);
4460 case STATEMENT_CASE_LABEL: {
4461 const case_label_statement_t *label = &stmt->case_label;
4462 count += count_decls_in_expression(label->expression);
4463 if(label->statement != NULL) {
4464 count += count_decls_in_stmts(label->statement);
4470 case STATEMENT_BREAK:
4471 case STATEMENT_CONTINUE:
4474 case STATEMENT_EXPRESSION: {
4475 const expression_statement_t *expr_stmt = &stmt->expression;
4476 count += count_decls_in_expression(expr_stmt->expression);
4480 case STATEMENT_GOTO:
4481 case STATEMENT_INVALID:
4484 case STATEMENT_RETURN: {
4485 const return_statement_t *ret_stmt = &stmt->returns;
4486 count += count_decls_in_expression(ret_stmt->value);
4494 static int get_function_n_local_vars(declaration_t *declaration)
4498 /* count parameters */
4499 count += count_local_declarations(declaration->scope.declarations, NULL);
4501 /* count local variables declared in body */
4502 count += count_decls_in_stmts(declaration->init.statement);
4507 static void initialize_function_parameters(declaration_t *declaration)
4509 ir_graph *irg = current_ir_graph;
4510 ir_node *args = get_irg_args(irg);
4511 ir_node *start_block = get_irg_start_block(irg);
4512 ir_type *function_irtype = get_ir_type(declaration->type);
4515 declaration_t *parameter = declaration->scope.declarations;
4516 for( ; parameter != NULL; parameter = parameter->next, ++n) {
4517 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
4518 type_t *type = skip_typeref(parameter->type);
4520 bool needs_entity = parameter->address_taken;
4521 assert(!is_type_array(type));
4522 if(is_type_compound(type)) {
4523 needs_entity = true;
4527 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
4528 ident *id = new_id_from_str(parameter->symbol->string);
4529 set_entity_ident(entity, id);
4531 parameter->declaration_kind
4532 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
4533 parameter->v.entity = entity;
4537 ir_mode *mode = get_ir_mode(parameter->type);
4539 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
4541 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
4542 parameter->v.value_number = next_value_number_function;
4543 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
4544 ++next_value_number_function;
4546 set_value(parameter->v.value_number, proj);
4551 * Handle additional decl modifiers for IR-graphs
4553 * @param irg the IR-graph
4554 * @param dec_modifiers additional modifiers
4556 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
4558 if (decl_modifiers & DM_NORETURN) {
4559 /* TRUE if the declaration includes the Microsoft
4560 __declspec(noreturn) specifier. */
4561 set_irg_additional_property(irg, mtp_property_noreturn);
4563 if (decl_modifiers & DM_NOTHROW) {
4564 /* TRUE if the declaration includes the Microsoft
4565 __declspec(nothrow) specifier. */
4566 set_irg_additional_property(irg, mtp_property_nothrow);
4568 if (decl_modifiers & DM_NAKED) {
4569 /* TRUE if the declaration includes the Microsoft
4570 __declspec(naked) specifier. */
4571 set_irg_additional_property(irg, mtp_property_naked);
4573 if (decl_modifiers & DM_FORCEINLINE) {
4574 /* TRUE if the declaration includes the
4575 Microsoft __forceinline specifier. */
4576 set_irg_inline_property(irg, irg_inline_forced);
4578 if (decl_modifiers & DM_NOINLINE) {
4579 /* TRUE if the declaration includes the Microsoft
4580 __declspec(noinline) specifier. */
4581 set_irg_inline_property(irg, irg_inline_forbidden);
4585 static void create_function(declaration_t *declaration)
4587 ir_entity *function_entity = get_function_entity(declaration);
4589 if(declaration->init.statement == NULL)
4592 current_function_decl = declaration;
4593 current_function_name = NULL;
4595 assert(imature_blocks == NULL);
4596 imature_blocks = NEW_ARR_F(ir_node*, 0);
4598 int n_local_vars = get_function_n_local_vars(declaration);
4599 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
4600 ir_node *first_block = get_cur_block();
4602 /* set inline flags */
4603 if (declaration->is_inline)
4604 set_irg_inline_property(irg, irg_inline_recomended);
4605 handle_decl_modifier_irg(irg, declaration->modifiers);
4607 next_value_number_function = 0;
4608 initialize_function_parameters(declaration);
4610 statement_to_firm(declaration->init.statement);
4612 ir_node *end_block = get_irg_end_block(irg);
4614 /* do we have a return statement yet? */
4615 if(get_cur_block() != NULL) {
4616 type_t *type = skip_typeref(declaration->type);
4617 assert(is_type_function(type));
4618 const function_type_t *func_type = &type->function;
4619 const type_t *return_type
4620 = skip_typeref(func_type->return_type);
4623 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4624 ret = new_Return(get_store(), 0, NULL);
4627 if(is_type_scalar(return_type)) {
4628 mode = get_ir_mode(func_type->return_type);
4634 /* ยง5.1.2.2.3 main implicitly returns 0 */
4635 if (strcmp(declaration->symbol->string, "main") == 0) {
4636 in[0] = new_Const(mode, get_mode_null(mode));
4638 in[0] = new_Unknown(mode);
4640 ret = new_Return(get_store(), 1, in);
4642 add_immBlock_pred(end_block, ret);
4645 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
4646 mature_immBlock(imature_blocks[i]);
4648 DEL_ARR_F(imature_blocks);
4649 imature_blocks = NULL;
4651 mature_immBlock(first_block);
4652 mature_immBlock(end_block);
4654 irg_finalize_cons(irg);
4656 /* finalize the frame type */
4657 ir_type *frame_type = get_irg_frame_type(irg);
4658 int n = get_compound_n_members(frame_type);
4661 for(int i = 0; i < n; ++i) {
4662 ir_entity *entity = get_compound_member(frame_type, i);
4663 ir_type *entity_type = get_entity_type(entity);
4665 int align = get_type_alignment_bytes(entity_type);
4666 if(align > align_all)
4670 misalign = offset % align;
4672 offset += align - misalign;
4676 set_entity_offset(entity, offset);
4677 offset += get_type_size_bytes(entity_type);
4679 set_type_size_bytes(frame_type, offset);
4680 set_type_alignment_bytes(frame_type, align_all);
4685 static void scope_to_firm(scope_t *scope)
4687 /* first pass: create declarations */
4688 declaration_t *declaration = scope->declarations;
4689 for( ; declaration != NULL; declaration = declaration->next) {
4690 if(declaration->namespc != NAMESPACE_NORMAL)
4692 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4693 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4695 if(declaration->symbol == NULL)
4698 type_t *type = skip_typeref(declaration->type);
4699 if(is_type_function(type)) {
4700 get_function_entity(declaration);
4702 create_global_variable(declaration);
4706 /* second pass: create code/initializers */
4707 declaration = scope->declarations;
4708 for( ; declaration != NULL; declaration = declaration->next) {
4709 if(declaration->namespc != NAMESPACE_NORMAL)
4711 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4712 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4714 if(declaration->symbol == NULL)
4717 type_t *type = declaration->type;
4718 if(type->kind == TYPE_FUNCTION) {
4719 create_function(declaration);
4721 assert(declaration->declaration_kind
4722 == DECLARATION_KIND_GLOBAL_VARIABLE);
4723 current_ir_graph = get_const_code_irg();
4724 create_declaration_initializer(declaration);
4729 void init_ast2firm(void)
4731 obstack_init(&asm_obst);
4732 init_atomic_modes();
4734 /* create idents for all known runtime functions */
4735 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
4736 predef_idents[rts_data[i].id] = new_id_from_str(rts_data[i].name);
4740 static void init_ir_types(void)
4742 static int ir_types_initialized = 0;
4743 if(ir_types_initialized)
4745 ir_types_initialized = 1;
4747 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4748 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4749 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4751 ir_type_int = get_ir_type(type_int);
4752 ir_type_const_char = get_ir_type(type_const_char);
4753 ir_type_wchar_t = get_ir_type(type_wchar_t);
4754 ir_type_void = get_ir_type(type_int); /* we don't have a real void
4757 type_void->base.firm_type = ir_type_void;
4760 void exit_ast2firm(void)
4762 obstack_free(&asm_obst, NULL);
4765 void translation_unit_to_firm(translation_unit_t *unit)
4767 /* just to be sure */
4768 continue_label = NULL;
4770 current_switch_cond = NULL;
4774 scope_to_firm(&unit->scope);