9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
15 #include "adt/array.h"
21 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
23 static ir_type *ir_type_const_char;
24 static ir_type *ir_type_void;
25 static ir_type *ir_type_int;
27 static type_t *type_const_char;
28 static type_t *type_void;
29 static type_t *type_int;
31 static int next_value_number_function;
32 static ir_node *continue_label;
33 static ir_node *break_label;
34 static ir_node *current_switch_cond;
35 static bool saw_default_label;
36 static ir_node **imature_blocks;
38 static const declaration_t *current_function_decl;
39 static ir_node *current_function_name;
41 typedef enum declaration_type_t {
42 DECLARATION_TYPE_UNKNOWN,
43 DECLARATION_TYPE_FUNCTION,
44 DECLARATION_TYPE_GLOBAL_VARIABLE,
45 DECLARATION_TYPE_LOCAL_VARIABLE,
46 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
47 DECLARATION_TYPE_COMPOUND_MEMBER,
48 DECLARATION_TYPE_LABEL_BLOCK,
49 DECLARATION_TYPE_ENUM_ENTRY
52 static ir_type *get_ir_type(type_t *type);
54 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
58 const declaration_t *declaration = & value_numbers[pos]->declaration;
60 print_warning_prefix(declaration->source_position);
61 fprintf(stderr, "variable '%s' might be used uninitialized\n",
62 declaration->symbol->string);
64 fprintf(stderr, "Some variable might be used uninitialized\n");
65 return new_r_Unknown(irg, mode);
68 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
70 const source_position_t *pos = (const source_position_t*) dbg;
73 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
77 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
79 const source_position_t *pos = (const source_position_t*) dbg;
84 return pos->input_name;
87 void init_ast2firm(void)
89 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
90 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
91 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
93 ir_type_int = get_ir_type(type_int);
94 ir_type_const_char = get_ir_type(type_const_char);
95 ir_type_void = get_ir_type(type_int); /* we don't have a real void
98 type_void->base.firm_type = ir_type_void;
101 void exit_ast2firm(void)
105 static unsigned unique_id = 0;
107 static ident *unique_ident(const char *tag)
111 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
113 return new_id_from_str(buf);
116 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
118 switch(atomic_type->atype) {
119 case ATOMIC_TYPE_SCHAR:
120 case ATOMIC_TYPE_CHAR:
122 case ATOMIC_TYPE_UCHAR:
124 case ATOMIC_TYPE_SHORT:
126 case ATOMIC_TYPE_USHORT:
128 case ATOMIC_TYPE_LONG:
129 case ATOMIC_TYPE_INT:
131 case ATOMIC_TYPE_ULONG:
132 case ATOMIC_TYPE_UINT:
134 case ATOMIC_TYPE_LONGLONG:
136 case ATOMIC_TYPE_ULONGLONG:
138 case ATOMIC_TYPE_FLOAT:
140 case ATOMIC_TYPE_DOUBLE:
142 case ATOMIC_TYPE_LONG_DOUBLE:
144 case ATOMIC_TYPE_BOOL:
146 #ifdef PROVIDE_COMPLEX
147 case ATOMIC_TYPE_FLOAT_COMPLEX:
148 case ATOMIC_TYPE_DOUBLE_COMPLEX:
149 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
150 panic("complex lowering not implemented yet");
152 case ATOMIC_TYPE_FLOAT_IMAGINARY:
153 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
154 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
155 panic("imaginary lowering not implemented yet");
158 case ATOMIC_TYPE_VOID:
159 /* firm has no real void... */
161 case ATOMIC_TYPE_INVALID:
164 panic("Encountered unknown atomic type");
168 static unsigned get_type_size(type_t *type);
170 static unsigned get_atomic_type_size(const atomic_type_t *type)
172 switch(type->atype) {
173 case ATOMIC_TYPE_CHAR:
174 case ATOMIC_TYPE_SCHAR:
175 case ATOMIC_TYPE_UCHAR:
178 case ATOMIC_TYPE_SHORT:
179 case ATOMIC_TYPE_USHORT:
182 case ATOMIC_TYPE_BOOL:
183 case ATOMIC_TYPE_INT:
184 case ATOMIC_TYPE_UINT:
185 case ATOMIC_TYPE_LONG:
186 case ATOMIC_TYPE_ULONG:
187 case ATOMIC_TYPE_FLOAT:
190 case ATOMIC_TYPE_LONGLONG:
191 case ATOMIC_TYPE_ULONGLONG:
192 case ATOMIC_TYPE_DOUBLE:
195 case ATOMIC_TYPE_LONG_DOUBLE:
198 case ATOMIC_TYPE_VOID:
201 case ATOMIC_TYPE_INVALID:
204 panic("Trying to determine size of invalid atomic type");
207 static unsigned get_compound_type_size(compound_type_t *type)
209 ir_type *irtype = get_ir_type((type_t*) type);
210 return get_type_size_bytes(irtype);
213 static unsigned get_array_type_size(array_type_t *type)
215 ir_type *irtype = get_ir_type((type_t*) type);
216 return get_type_size_bytes(irtype);
219 static unsigned get_type_size(type_t *type)
221 type = skip_typeref(type);
225 return get_atomic_type_size(&type->atomic);
227 return get_mode_size_bytes(mode_Is);
228 case TYPE_COMPOUND_UNION:
229 case TYPE_COMPOUND_STRUCT:
230 return get_compound_type_size(&type->compound);
232 /* just a pointer to the function */
233 return get_mode_size_bytes(mode_P_code);
235 return get_mode_size_bytes(mode_P_data);
237 return get_array_type_size(&type->array);
244 panic("Trying to determine size of invalid type");
247 static unsigned count_parameters(const function_type_t *function_type)
251 function_parameter_t *parameter = function_type->parameters;
252 for ( ; parameter != NULL; parameter = parameter->next) {
262 static long fold_constant(const expression_t *expression);
264 static ir_type *create_atomic_type(const atomic_type_t *type)
266 ir_mode *mode = get_atomic_mode(type);
267 ident *id = get_mode_ident(mode);
268 ir_type *irtype = new_type_primitive(id, mode);
273 static ir_type *create_method_type(const function_type_t *function_type)
275 type_t *result_type = function_type->result_type;
277 ident *id = unique_ident("functiontype");
278 int n_parameters = count_parameters(function_type);
279 int n_results = result_type == type_void ? 0 : 1;
280 ir_type *irtype = new_type_method(id, n_parameters, n_results);
282 if(result_type != type_void) {
283 ir_type *restype = get_ir_type(result_type);
284 set_method_res_type(irtype, 0, restype);
287 function_parameter_t *parameter = function_type->parameters;
289 for( ; parameter != NULL; parameter = parameter->next) {
290 ir_type *p_irtype = get_ir_type(parameter->type);
291 set_method_param_type(irtype, n, p_irtype);
295 if(function_type->variadic || function_type->unspecified_parameters) {
296 set_method_variadicity(irtype, variadicity_variadic);
302 static ir_type *create_pointer_type(pointer_type_t *type)
304 type_t *points_to = type->points_to;
305 ir_type *ir_points_to;
306 /* Avoid endless recursion if the points_to type contains this poiner type
307 * again (might be a struct). We therefore first create a void* pointer
308 * and then set the real points_to type
310 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
311 ir_type_void, mode_P_data);
312 type->type.firm_type = ir_type;
314 ir_points_to = get_ir_type(points_to);
315 set_pointer_points_to_type(ir_type, ir_points_to);
320 static ir_type *create_array_type(array_type_t *type)
322 type_t *element_type = type->element_type;
323 ir_type *ir_element_type = get_ir_type(element_type);
325 ident *id = unique_ident("array");
326 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
328 if(type->size != NULL) {
329 int n_elements = fold_constant(type->size);
331 set_array_bounds_int(ir_type, 0, 0, n_elements);
333 size_t elemsize = get_type_size_bytes(ir_element_type);
334 int align = get_type_alignment_bytes(ir_element_type);
335 if(elemsize % align > 0) {
336 elemsize += align - (elemsize % align);
338 set_type_size_bytes(ir_type, n_elements * elemsize);
339 set_type_alignment_bytes(ir_type, align);
340 set_type_state(ir_type, layout_fixed);
346 #define INVALID_TYPE ((ir_type_ptr)-1)
348 static ir_type *create_struct_type(compound_type_t *type)
350 symbol_t *symbol = type->declaration->symbol;
353 id = unique_ident(symbol->string);
355 id = unique_ident("__anonymous_struct");
357 ir_type *ir_type = new_type_struct(id);
359 type->type.firm_type = ir_type;
363 declaration_t *entry = type->declaration->context.declarations;
364 for( ; entry != NULL; entry = entry->next) {
365 if(entry->namespc != NAMESPACE_NORMAL)
368 ident *ident = new_id_from_str(entry->symbol->string);
369 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
371 int entry_size = get_type_size_bytes(entry_ir_type);
372 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
373 int misalign = offset % entry_alignment;
375 offset += entry_alignment - misalign;
377 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
378 set_entity_offset(entity, offset);
379 add_struct_member(ir_type, entity);
380 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
381 entry->v.entity = entity;
383 offset += entry_size;
384 if(entry_alignment > align_all) {
385 if(entry_alignment % align_all != 0) {
386 panic("Uneven alignments not supported yet");
388 align_all = entry_alignment;
392 int misalign = offset % align_all;
394 set_type_alignment_bytes(ir_type, align_all);
395 set_type_size_bytes(ir_type, offset);
396 set_type_state(ir_type, layout_fixed);
401 static ir_type *create_union_type(compound_type_t *type)
403 declaration_t *declaration = type->declaration;
404 symbol_t *symbol = declaration->symbol;
407 id = unique_ident(symbol->string);
409 id = unique_ident("__anonymous_union");
411 ir_type *ir_type = new_type_union(id);
413 type->type.firm_type = ir_type;
417 declaration_t *entry = declaration->context.declarations;
418 for( ; entry != NULL; entry = entry->next) {
419 if(entry->namespc != NAMESPACE_NORMAL)
422 ident *ident = new_id_from_str(entry->symbol->string);
423 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
425 int entry_size = get_type_size_bytes(entry_ir_type);
426 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
428 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
429 add_union_member(ir_type, entity);
430 set_entity_offset(entity, 0);
431 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
432 entry->v.entity = entity;
434 if(entry_size > size) {
437 if(entry_alignment > align_all) {
438 if(entry_alignment % align_all != 0) {
439 panic("Uneven alignments not supported yet");
441 align_all = entry_alignment;
445 set_type_alignment_bytes(ir_type, align_all);
446 set_type_size_bytes(ir_type, size);
447 set_type_state(ir_type, layout_fixed);
452 static ir_node *expression_to_firm(const expression_t *expression);
453 static inline ir_mode *get_ir_mode(type_t *type);
455 static ir_type *create_enum_type(enum_type_t *const type)
457 type->type.firm_type = ir_type_int;
459 ir_mode *const mode = get_ir_mode((type_t*) type);
460 tarval *const one = get_mode_one(mode);
461 tarval * tv_next = get_tarval_null(mode);
463 declaration_t *declaration = type->declaration->next;
464 for (; declaration != NULL; declaration = declaration->next) {
465 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
468 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
470 expression_t *const init = declaration->init.enum_value;
472 ir_node *const cnst = expression_to_firm(init);
473 if (!is_Const(cnst)) {
474 panic("couldn't fold constant");
476 tv_next = get_Const_tarval(cnst);
478 declaration->v.enum_val = tv_next;
479 tv_next = tarval_add(tv_next, one);
485 static ir_type *get_ir_type(type_t *type)
487 assert(type != NULL);
489 type = skip_typeref(type);
491 if(type->base.firm_type != NULL) {
492 assert(type->base.firm_type != INVALID_TYPE);
493 return type->base.firm_type;
496 ir_type *firm_type = NULL;
499 firm_type = create_atomic_type(&type->atomic);
502 firm_type = create_method_type(&type->function);
505 firm_type = create_pointer_type(&type->pointer);
508 firm_type = create_array_type(&type->array);
510 case TYPE_COMPOUND_STRUCT:
511 firm_type = create_struct_type(&type->compound);
513 case TYPE_COMPOUND_UNION:
514 firm_type = create_union_type(&type->compound);
517 firm_type = create_enum_type(&type->enumt);
525 if(firm_type == NULL)
526 panic("unknown type found");
528 type->base.firm_type = firm_type;
532 static inline ir_mode *get_ir_mode(type_t *type)
534 ir_type *irtype = get_ir_type(type);
536 /* firm doesn't report a mode for arrays somehow... */
537 if(is_Array_type(irtype)) {
541 ir_mode *mode = get_type_mode(irtype);
542 assert(mode != NULL);
546 static ir_entity* get_function_entity(declaration_t *declaration)
548 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
549 return declaration->v.entity;
550 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
552 symbol_t *symbol = declaration->symbol;
553 ident *id = new_id_from_str(symbol->string);
555 ir_type *global_type = get_glob_type();
556 ir_type *ir_type_method = get_ir_type(declaration->type);
557 assert(is_Method_type(ir_type_method));
559 ir_entity *entity = new_entity(global_type, id, ir_type_method);
560 set_entity_ld_ident(entity, id);
561 if(declaration->storage_class == STORAGE_CLASS_STATIC
562 || declaration->is_inline) {
563 set_entity_visibility(entity, visibility_local);
564 } else if(declaration->init.statement != NULL) {
565 set_entity_visibility(entity, visibility_external_visible);
567 set_entity_visibility(entity, visibility_external_allocated);
570 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
571 declaration->v.entity = entity;
576 static dbg_info *get_dbg_info(const source_position_t *pos)
578 return (dbg_info*) pos;
581 static ir_node *const_to_firm(const const_expression_t *cnst)
583 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
584 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
589 if(mode_is_float(mode)) {
590 tv = new_tarval_from_double(cnst->v.float_value, mode);
592 if(mode_is_signed(mode)) {
593 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
595 len = snprintf(buf, sizeof(buf), "%llu",
596 (unsigned long long) cnst->v.int_value);
598 tv = new_tarval_from_str(buf, len, mode);
601 return new_d_Const(dbgi, mode, tv);
604 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
606 assert(entity != NULL);
607 union symconst_symbol sym;
608 sym.entity_p = entity;
609 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
612 static ir_node *string_to_firm(const source_position_t *const src_pos,
613 const char *const id_prefix,
614 const char *const string)
616 ir_type *const global_type = get_glob_type();
617 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
620 ident *const id = unique_ident(id_prefix);
621 ir_entity *const entity = new_entity(global_type, id, type);
622 set_entity_ld_ident(entity, id);
623 set_entity_variability(entity, variability_constant);
625 ir_type *const elem_type = ir_type_const_char;
626 ir_mode *const mode = get_type_mode(elem_type);
628 const size_t slen = strlen(string) + 1;
630 set_array_lower_bound_int(type, 0, 0);
631 set_array_upper_bound_int(type, 0, slen);
632 set_type_size_bytes(type, slen);
633 set_type_state(type, layout_fixed);
635 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
636 for(size_t i = 0; i < slen; ++i) {
637 tvs[i] = new_tarval_from_long(string[i], mode);
640 set_array_entity_values(entity, tvs, slen);
643 dbg_info *const dbgi = get_dbg_info(src_pos);
645 return create_symconst(dbgi, entity);
648 static ir_node *string_literal_to_firm(
649 const string_literal_expression_t* literal)
651 return string_to_firm(&literal->expression.source_position, "Lstr",
655 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
656 dbg_info *const dbgi)
658 if(is_compound_type(irtype) || is_Array_type(irtype)) {
662 ir_mode *const mode = get_type_mode(irtype);
663 ir_node *const memory = get_store();
664 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
665 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
666 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
671 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
673 ir_mode *mode = get_irn_mode(node);
675 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
677 if(!mode_is_float(mode))
680 /* check if there is already a Conv */
681 if (get_irn_op(node) == op_Conv) {
682 /* convert it into a strict Conv */
683 set_Conv_strict(node, 1);
687 /* otherwise create a new one */
688 return new_d_strictConv(dbgi, node, mode);
691 static ir_node *get_global_var_address(dbg_info *const dbgi,
692 const declaration_t *const decl)
694 assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
696 ir_entity *const entity = decl->v.entity;
697 switch ((storage_class_tag_t)decl->storage_class) {
698 case STORAGE_CLASS_THREAD:
699 case STORAGE_CLASS_THREAD_EXTERN:
700 case STORAGE_CLASS_THREAD_STATIC: {
701 ir_node *const no_mem = new_NoMem();
702 ir_node *const tls = get_irg_tls(current_ir_graph);
703 return new_d_simpleSel(dbgi, no_mem, tls, entity);
707 return create_symconst(dbgi, entity);
711 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
713 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
714 declaration_t *declaration = ref->declaration;
715 type_t *type = skip_typeref(declaration->type);
717 switch((declaration_type_t) declaration->declaration_type) {
718 case DECLARATION_TYPE_UNKNOWN:
719 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
725 case DECLARATION_TYPE_ENUM_ENTRY: {
726 ir_mode *const mode = get_ir_mode(type);
727 return new_Const(mode, declaration->v.enum_val);
730 case DECLARATION_TYPE_LOCAL_VARIABLE: {
731 ir_mode *mode = get_ir_mode(type);
732 return get_value(declaration->v.value_number, mode);
734 case DECLARATION_TYPE_FUNCTION: {
735 return create_symconst(dbgi, declaration->v.entity);
737 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
738 ir_node *const addr = get_global_var_address(dbgi, declaration);
739 ir_type *const irtype = get_entity_type(declaration->v.entity);
740 return deref_address(irtype, addr, dbgi);
743 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
744 ir_entity *entity = declaration->v.entity;
745 ir_node *frame = get_irg_frame(current_ir_graph);
746 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
747 ir_type *irtype = get_entity_type(entity);
748 return deref_address(irtype, sel, dbgi);
751 case DECLARATION_TYPE_COMPOUND_MEMBER:
752 case DECLARATION_TYPE_LABEL_BLOCK:
753 panic("not implemented reference type");
756 panic("reference to declaration with unknown type found");
759 static ir_node *reference_addr(const reference_expression_t *ref)
761 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
762 declaration_t *declaration = ref->declaration;
764 switch((declaration_type_t) declaration->declaration_type) {
765 case DECLARATION_TYPE_UNKNOWN:
767 case DECLARATION_TYPE_LOCAL_VARIABLE:
768 panic("local variable without entity has no address");
769 case DECLARATION_TYPE_FUNCTION: {
770 return create_symconst(dbgi, declaration->v.entity);
772 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
773 ir_node *const addr = get_global_var_address(dbgi, declaration);
776 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
777 ir_entity *entity = declaration->v.entity;
778 ir_node *frame = get_irg_frame(current_ir_graph);
779 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
784 case DECLARATION_TYPE_ENUM_ENTRY:
785 panic("trying to reference enum entry");
787 case DECLARATION_TYPE_COMPOUND_MEMBER:
788 case DECLARATION_TYPE_LABEL_BLOCK:
789 panic("not implemented reference type");
792 panic("reference to declaration with unknown type found");
795 static ir_node *process_builtin_call(const call_expression_t *call)
797 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
799 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
800 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
802 type_t *type = skip_typeref(builtin->expression.datatype);
803 assert(type->type == TYPE_POINTER);
805 type_t *function_type = skip_typeref(type->pointer.points_to);
806 symbol_t *symbol = builtin->symbol;
809 case T___builtin_alloca: {
810 if(call->arguments == NULL || call->arguments->next != NULL) {
811 panic("invalid number of parameters on __builtin_alloca");
813 expression_t *argument = call->arguments->expression;
814 ir_node *size = expression_to_firm(argument);
816 ir_node *store = get_store();
817 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
819 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
821 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
825 case T___builtin_nan:
826 case T___builtin_nanf:
827 case T___builtin_nand: {
828 /* Ignore string for now... */
829 assert(function_type->type == TYPE_FUNCTION);
830 ir_mode *mode = get_ir_mode(function_type->function.result_type);
831 tarval *tv = get_mode_NAN(mode);
832 ir_node *res = new_d_Const(dbgi, mode, tv);
836 panic("Unsupported builtin found\n");
840 static ir_node *call_expression_to_firm(const call_expression_t *call)
842 assert(get_cur_block() != NULL);
844 expression_t *function = call->function;
845 if(function->type == EXPR_BUILTIN_SYMBOL) {
846 return process_builtin_call(call);
848 ir_node *callee = expression_to_firm(function);
850 type_t *type = function->base.datatype;
851 assert(type->type == TYPE_POINTER);
852 pointer_type_t *const ptr_type = &type->pointer;
853 assert(ptr_type->points_to->type == TYPE_FUNCTION);
854 function_type_t *function_type = &ptr_type->points_to->function;
856 int n_parameters = 0;
857 call_argument_t *argument = call->arguments;
858 for( ; argument != NULL; argument = argument->next) {
862 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
864 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
865 ir_type *new_method_type = NULL;
866 if(function_type->variadic || function_type->unspecified_parameters) {
867 /* we need to construct a new method type matching the call
869 int n_res = get_method_n_ress(ir_method_type);
870 new_method_type = new_type_method(unique_ident("calltype"),
871 n_parameters, n_res);
872 set_method_calling_convention(new_method_type,
873 get_method_calling_convention(ir_method_type));
874 set_method_additional_properties(new_method_type,
875 get_method_additional_properties(ir_method_type));
877 for(int i = 0; i < n_res; ++i) {
878 set_method_res_type(new_method_type, i,
879 get_method_res_type(ir_method_type, i));
882 ir_node *in[n_parameters];
884 argument = call->arguments;
886 for( ; argument != NULL; argument = argument->next) {
887 expression_t *expression = argument->expression;
888 ir_node *arg_node = expression_to_firm(expression);
890 arg_node = do_strict_conv(dbgi, arg_node);
893 if(new_method_type != NULL) {
894 ir_type *irtype = get_ir_type(expression->base.datatype);
895 set_method_param_type(new_method_type, n, irtype);
900 assert(n == n_parameters);
902 if(new_method_type != NULL)
903 ir_method_type = new_method_type;
905 ir_node *store = get_store();
906 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
908 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
911 type_t *result_type = skip_typeref(function_type->result_type);
912 ir_node *result = NULL;
914 if(!is_type_atomic(result_type, ATOMIC_TYPE_VOID)) {
916 if(is_type_scalar(result_type)) {
917 mode = get_ir_mode(result_type);
921 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
922 result = new_d_Proj(dbgi, resproj, mode, 0);
928 static void statement_to_firm(statement_t *statement);
929 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
931 static ir_node *expression_to_addr(const expression_t *expression);
932 static void create_condition_evaluation(const expression_t *expression,
934 ir_node *false_block);
936 static void set_value_for_expression(const expression_t *expression,
939 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
940 value = do_strict_conv(dbgi, value);
942 if(expression->type == EXPR_REFERENCE) {
943 reference_expression_t *ref = (reference_expression_t*) expression;
945 declaration_t *declaration = ref->declaration;
946 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
947 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
948 set_value(declaration->v.value_number, value);
953 ir_node *addr = expression_to_addr(expression);
954 ir_node *memory = get_store();
956 type_t *type = skip_typeref(expression->base.datatype);
957 if(is_type_scalar(type)) {
958 ir_node *store = new_d_Store(dbgi, memory, addr, value);
959 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
960 set_store(store_mem);
962 ir_type *irtype = get_ir_type(type);
963 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
964 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
965 set_store(copyb_mem);
969 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
971 ir_mode *value_mode = get_irn_mode(value);
973 if (value_mode == dest_mode || is_Bad(value))
976 if(dest_mode == mode_b) {
977 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
978 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
979 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
983 return new_d_Conv(dbgi, value, dest_mode);
986 static ir_node *create_incdec(const unary_expression_t *expression)
988 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
989 type_t *type = expression->expression.datatype;
990 ir_mode *mode = get_ir_mode(type);
991 expression_t *value = expression->value;
993 ir_node *value_node = expression_to_firm(value);
996 if(type->type == TYPE_POINTER) {
997 pointer_type_t *pointer_type = &type->pointer;
998 unsigned elem_size = get_type_size(pointer_type->points_to);
999 offset = new_Const_long(mode_Is, elem_size);
1001 assert(is_type_arithmetic(type));
1002 offset = new_Const(mode, get_mode_one(mode));
1005 switch(expression->type) {
1006 case UNEXPR_POSTFIX_INCREMENT: {
1007 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1008 set_value_for_expression(value, new_value);
1011 case UNEXPR_POSTFIX_DECREMENT: {
1012 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1013 set_value_for_expression(value, new_value);
1016 case UNEXPR_PREFIX_INCREMENT: {
1017 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1018 set_value_for_expression(value, new_value);
1021 case UNEXPR_PREFIX_DECREMENT: {
1022 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1023 set_value_for_expression(value, new_value);
1027 panic("no incdec expr in create_incdec");
1032 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1034 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1035 type_t *type = skip_typeref(expression->expression.datatype);
1037 if(expression->type == UNEXPR_TAKE_ADDRESS)
1038 return expression_to_addr(expression->value);
1040 const expression_t *value = expression->value;
1041 ir_node *value_node = expression_to_firm(value);
1043 switch(expression->type) {
1045 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
1048 case UNEXPR_BITWISE_NEGATE:
1049 return new_d_Not(dbgi, value_node, get_ir_mode(type));
1051 if(get_irn_mode(value_node) != mode_b) {
1052 value_node = create_conv(dbgi, value_node, mode_b);
1054 value_node = new_d_Not(dbgi, value_node, mode_b);
1055 ir_mode *const mode = get_ir_mode(type);
1056 if(mode != mode_b) {
1057 value_node = create_conv(dbgi, value_node, mode);
1061 case UNEXPR_DEREFERENCE: {
1062 ir_type *irtype = get_ir_type(type);
1063 return deref_address(irtype, value_node, dbgi);
1065 case UNEXPR_POSTFIX_INCREMENT:
1066 case UNEXPR_POSTFIX_DECREMENT:
1067 case UNEXPR_PREFIX_INCREMENT:
1068 case UNEXPR_PREFIX_DECREMENT:
1069 return create_incdec(expression);
1071 ir_node *node = create_conv(dbgi, value_node, get_ir_mode(type));
1072 node = do_strict_conv(dbgi, node);
1075 case UNEXPR_CAST_IMPLICIT:
1076 return create_conv(dbgi, value_node, get_ir_mode(type));
1078 case UNEXPR_TAKE_ADDRESS:
1079 case UNEXPR_INVALID:
1082 panic("invalid UNEXPR type found");
1085 static long get_pnc(binary_expression_type_t type)
1088 case BINEXPR_EQUAL: return pn_Cmp_Eq;
1089 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
1090 case BINEXPR_LESS: return pn_Cmp_Lt;
1091 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
1092 case BINEXPR_GREATER: return pn_Cmp_Gt;
1093 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1097 panic("trying to get pn_Cmp from non-comparison binexpr type");
1100 static ir_node *create_lazy_op(const binary_expression_t *expression)
1102 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1103 type_t *type = expression->expression.datatype;
1104 ir_mode *mode = get_ir_mode(type);
1106 ir_node *cur_block = get_cur_block();
1108 ir_node *one_block = new_immBlock();
1109 ir_node *one = new_Const(mode, get_mode_one(mode));
1110 ir_node *jmp_one = new_d_Jmp(dbgi);
1112 ir_node *zero_block = new_immBlock();
1113 ir_node *zero = new_Const(mode, get_mode_null(mode));
1114 ir_node *jmp_zero = new_d_Jmp(dbgi);
1116 set_cur_block(cur_block);
1117 create_condition_evaluation((const expression_t*) expression,
1118 one_block, zero_block);
1119 mature_immBlock(one_block);
1120 mature_immBlock(zero_block);
1122 ir_node *common_block = new_immBlock();
1123 add_immBlock_pred(common_block, jmp_one);
1124 add_immBlock_pred(common_block, jmp_zero);
1125 mature_immBlock(common_block);
1127 ir_node *in[2] = { one, zero };
1128 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1133 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1134 ir_node *right, ir_mode *mode);
1136 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1137 create_arithmetic_func func)
1139 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1140 ir_node *left = expression_to_firm(expression->left);
1141 ir_node *right = expression_to_firm(expression->right);
1142 type_t *type = expression->right->base.datatype;
1143 /* be careful with the modes, because in arithmetic assign nodes only
1144 * the right operand has the mode of the arithmetic already */
1145 ir_mode *mode = get_ir_mode(type);
1146 left = create_conv(dbgi, left, mode);
1147 ir_node *res = func(dbgi, left, right, mode);
1152 static ir_node *pointer_arithmetic(ir_node *const pointer,
1155 dbg_info *const dbgi,
1156 const create_arithmetic_func func)
1158 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1159 type_t *const points_to = pointer_type->points_to;
1160 const unsigned elem_size = get_type_size(points_to);
1162 assert(elem_size >= 1);
1163 if (elem_size > 1) {
1164 integer = create_conv(dbgi, integer, mode_Is);
1165 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1166 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1170 ir_mode *const mode = get_ir_mode(type);
1171 return func(dbgi, pointer, integer, mode);
1174 static ir_node *create_arithmetic_assign_binop(
1175 const binary_expression_t *expression, create_arithmetic_func func)
1177 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1178 type_t *const type = expression->expression.datatype;
1181 if (type->type == TYPE_POINTER) {
1182 ir_node *const pointer = expression_to_firm(expression->left);
1183 ir_node * integer = expression_to_firm(expression->right);
1184 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1186 value = create_arithmetic_binop(expression, func);
1189 ir_mode *const mode = get_ir_mode(type);
1190 value = create_conv(dbgi, value, mode);
1191 set_value_for_expression(expression->left, value);
1196 static ir_node *create_add(const binary_expression_t *expression)
1198 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1199 ir_node *left = expression_to_firm(expression->left);
1200 ir_node *right = expression_to_firm(expression->right);
1201 type_t *type = expression->expression.datatype;
1203 expression_t *expr_left = expression->left;
1204 expression_t *expr_right = expression->right;
1205 type_t *type_left = skip_typeref(expr_left->base.datatype);
1206 type_t *type_right = skip_typeref(expr_right->base.datatype);
1208 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1209 ir_mode *const mode = get_ir_mode(type);
1210 return new_d_Add(dbgi, left, right, mode);
1213 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1214 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1216 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1217 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1221 static ir_node *create_sub(const binary_expression_t *expression)
1223 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1224 expression_t *const expr_left = expression->left;
1225 expression_t *const expr_right = expression->right;
1226 ir_node *const left = expression_to_firm(expr_left);
1227 ir_node *const right = expression_to_firm(expr_right);
1228 type_t *const type = expression->expression.datatype;
1229 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1230 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1232 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1233 ir_mode *const mode = get_ir_mode(type);
1234 return new_d_Sub(dbgi, left, right, mode);
1235 } else if (type_left->type == TYPE_POINTER
1236 && type_right->type == TYPE_POINTER) {
1237 const pointer_type_t *const ptr_type = &type_left->pointer;
1238 const unsigned elem_size = get_type_size(ptr_type->points_to);
1239 ir_mode *const mode = get_ir_mode(type);
1240 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1241 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1242 ir_node *const no_mem = new_NoMem();
1243 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1244 op_pin_state_floats);
1245 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1248 assert(type_left->type == TYPE_POINTER);
1249 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1252 static ir_node *create_shift(const binary_expression_t *expression)
1254 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1255 ir_node *left = expression_to_firm(expression->left);
1256 ir_node *right = expression_to_firm(expression->right);
1257 type_t *type = expression->expression.datatype;
1258 ir_mode *mode = get_ir_mode(type);
1260 /* firm always wants the shift count to be unsigned */
1261 right = create_conv(dbgi, right, mode_Iu);
1265 switch(expression->type) {
1266 case BINEXPR_SHIFTLEFT_ASSIGN:
1267 case BINEXPR_SHIFTLEFT:
1268 res = new_d_Shl(dbgi, left, right, mode);
1270 case BINEXPR_SHIFTRIGHT_ASSIGN:
1271 case BINEXPR_SHIFTRIGHT: {
1272 expression_t *expr_left = expression->left;
1273 type_t *type_left = skip_typeref(expr_left->base.datatype);
1275 if(is_type_signed(type_left)) {
1276 res = new_d_Shrs(dbgi, left, right, mode);
1278 res = new_d_Shr(dbgi, left, right, mode);
1283 panic("create shift op called for non-shift op");
1290 static ir_node *create_divmod(const binary_expression_t *expression)
1292 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1293 ir_node *left = expression_to_firm(expression->left);
1294 ir_node *right = expression_to_firm(expression->right);
1295 ir_node *pin = new_Pin(new_NoMem());
1296 /* be careful with the modes, because in arithmetic assign nodes only
1297 * the right operand has the mode of the arithmetic already */
1298 type_t *type = expression->right->base.datatype;
1299 ir_mode *mode = get_ir_mode(type);
1300 left = create_conv(dbgi, left, mode);
1304 switch (expression->type) {
1306 case BINEXPR_DIV_ASSIGN:
1307 if(mode_is_float(mode)) {
1308 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1309 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1311 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1312 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1317 case BINEXPR_MOD_ASSIGN:
1318 assert(!mode_is_float(mode));
1319 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1320 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1323 default: panic("unexpected binary expression type in create_divmod()");
1329 static ir_node *create_arithmetic_assign_divmod(
1330 const binary_expression_t *expression)
1332 ir_node * value = create_divmod(expression);
1333 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1334 type_t *const type = expression->expression.datatype;
1335 ir_mode *const mode = get_ir_mode(type);
1337 assert(type->type != TYPE_POINTER);
1339 value = create_conv(dbgi, value, mode);
1340 set_value_for_expression(expression->left, value);
1345 static ir_node *create_arithmetic_assign_shift(
1346 const binary_expression_t *expression)
1348 ir_node * value = create_shift(expression);
1349 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1350 type_t *const type = expression->expression.datatype;
1351 ir_mode *const mode = get_ir_mode(type);
1353 value = create_conv(dbgi, value, mode);
1354 set_value_for_expression(expression->left, value);
1359 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1361 binary_expression_type_t type = expression->type;
1364 case BINEXPR_NOTEQUAL:
1366 case BINEXPR_LESSEQUAL:
1367 case BINEXPR_GREATER:
1368 case BINEXPR_GREATEREQUAL: {
1369 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1370 ir_node *left = expression_to_firm(expression->left);
1371 ir_node *right = expression_to_firm(expression->right);
1372 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1373 long pnc = get_pnc(type);
1374 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1377 case BINEXPR_ASSIGN: {
1378 ir_node *right = expression_to_firm(expression->right);
1379 set_value_for_expression(expression->left, right);
1384 return create_add(expression);
1386 return create_sub(expression);
1388 return create_arithmetic_binop(expression, new_d_Mul);
1389 case BINEXPR_BITWISE_AND:
1390 return create_arithmetic_binop(expression, new_d_And);
1391 case BINEXPR_BITWISE_OR:
1392 return create_arithmetic_binop(expression, new_d_Or);
1393 case BINEXPR_BITWISE_XOR:
1394 return create_arithmetic_binop(expression, new_d_Eor);
1395 case BINEXPR_SHIFTLEFT:
1396 case BINEXPR_SHIFTRIGHT:
1397 return create_shift(expression);
1400 return create_divmod(expression);
1401 case BINEXPR_LOGICAL_AND:
1402 case BINEXPR_LOGICAL_OR:
1403 return create_lazy_op(expression);
1405 expression_to_firm(expression->left);
1406 return expression_to_firm(expression->right);
1407 case BINEXPR_ADD_ASSIGN:
1408 return create_arithmetic_assign_binop(expression, new_d_Add);
1409 case BINEXPR_SUB_ASSIGN:
1410 return create_arithmetic_assign_binop(expression, new_d_Sub);
1411 case BINEXPR_MUL_ASSIGN:
1412 return create_arithmetic_assign_binop(expression, new_d_Mul);
1413 case BINEXPR_DIV_ASSIGN:
1414 return create_arithmetic_assign_divmod(expression);
1415 case BINEXPR_BITWISE_AND_ASSIGN:
1416 return create_arithmetic_assign_binop(expression, new_d_And);
1417 case BINEXPR_BITWISE_OR_ASSIGN:
1418 return create_arithmetic_assign_binop(expression, new_d_Or);
1419 case BINEXPR_BITWISE_XOR_ASSIGN:
1420 return create_arithmetic_assign_binop(expression, new_d_Eor);
1421 case BINEXPR_SHIFTLEFT_ASSIGN:
1422 case BINEXPR_SHIFTRIGHT_ASSIGN:
1423 return create_arithmetic_assign_shift(expression);
1425 panic("TODO binexpr type");
1429 static ir_node *array_access_addr(const array_access_expression_t *expression)
1431 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1432 ir_node *base_addr = expression_to_firm(expression->array_ref);
1433 ir_node *offset = expression_to_firm(expression->index);
1434 offset = create_conv(dbgi, offset, mode_Iu);
1436 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1437 assert(is_type_pointer(ref_type));
1438 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1440 unsigned elem_size = get_type_size(pointer_type->points_to);
1441 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1442 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1444 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1449 static ir_node *array_access_to_firm(
1450 const array_access_expression_t *expression)
1452 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1453 ir_node *addr = array_access_addr(expression);
1454 type_t *type = revert_automatic_type_conversion(
1455 (const expression_t*) expression);
1456 type = skip_typeref(type);
1457 ir_type *irtype = get_ir_type(type);
1459 return deref_address(irtype, addr, dbgi);
1462 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1464 type_t *type = expression->type;
1466 type = expression->size_expression->base.datatype;
1467 assert(type != NULL);
1470 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1471 unsigned size = get_type_size(type);
1472 ir_node *size_node = new_Const_long(mode, size);
1477 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1479 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1481 ir_node *cur_block = get_cur_block();
1483 /* create the true block */
1484 ir_node *true_block = new_immBlock();
1486 ir_node *true_val = expression_to_firm(expression->true_expression);
1487 ir_node *true_jmp = new_Jmp();
1489 /* create the false block */
1490 ir_node *false_block = new_immBlock();
1492 ir_node *false_val = expression_to_firm(expression->false_expression);
1493 ir_node *false_jmp = new_Jmp();
1495 /* create the condition evaluation */
1496 set_cur_block(cur_block);
1497 create_condition_evaluation(expression->condition, true_block, false_block);
1498 mature_immBlock(true_block);
1499 mature_immBlock(false_block);
1501 /* create the common block */
1502 ir_node *common_block = new_immBlock();
1503 add_immBlock_pred(common_block, true_jmp);
1504 add_immBlock_pred(common_block, false_jmp);
1505 mature_immBlock(common_block);
1507 /* TODO improve static semantics, so either both or no values are NULL */
1508 if (true_val == NULL || false_val == NULL)
1511 ir_node *in[2] = { true_val, false_val };
1512 ir_mode *mode = get_irn_mode(true_val);
1513 assert(get_irn_mode(false_val) == mode);
1514 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1519 static ir_node *select_addr(const select_expression_t *expression)
1521 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1523 ir_node *compound_addr = expression_to_firm(expression->compound);
1525 declaration_t *entry = expression->compound_entry;
1526 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1527 ir_entity *entity = entry->v.entity;
1529 assert(entity != NULL);
1531 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1536 static ir_node *select_to_firm(const select_expression_t *expression)
1538 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1539 ir_node *addr = select_addr(expression);
1540 type_t *type = revert_automatic_type_conversion(
1541 (const expression_t*) expression);
1542 type = skip_typeref(type);
1543 ir_type *irtype = get_ir_type(type);
1545 return deref_address(irtype, addr, dbgi);
1548 /* Values returned by __builtin_classify_type. */
1549 typedef enum gcc_type_class
1555 enumeral_type_class,
1558 reference_type_class,
1562 function_type_class,
1573 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1575 const type_t *const type = expr->type_expression->base.datatype;
1581 const atomic_type_t *const atomic_type = &type->atomic;
1582 switch (atomic_type->atype) {
1583 // should not be reached
1584 case ATOMIC_TYPE_INVALID:
1588 // gcc cannot do that
1589 case ATOMIC_TYPE_VOID:
1590 tc = void_type_class;
1593 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1594 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1595 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1596 case ATOMIC_TYPE_SHORT:
1597 case ATOMIC_TYPE_USHORT:
1598 case ATOMIC_TYPE_INT:
1599 case ATOMIC_TYPE_UINT:
1600 case ATOMIC_TYPE_LONG:
1601 case ATOMIC_TYPE_ULONG:
1602 case ATOMIC_TYPE_LONGLONG:
1603 case ATOMIC_TYPE_ULONGLONG:
1604 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1605 tc = integer_type_class;
1608 case ATOMIC_TYPE_FLOAT:
1609 case ATOMIC_TYPE_DOUBLE:
1610 case ATOMIC_TYPE_LONG_DOUBLE:
1611 tc = real_type_class;
1614 #ifdef PROVIDE_COMPLEX
1615 case ATOMIC_TYPE_FLOAT_COMPLEX:
1616 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1617 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1618 tc = complex_type_class;
1620 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1621 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1622 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1623 tc = complex_type_class;
1628 panic("Unimplemented case in classify_type_to_firm().");
1633 case TYPE_ARRAY: // gcc handles this as pointer
1634 case TYPE_FUNCTION: // gcc handles this as pointer
1635 case TYPE_POINTER: tc = pointer_type_class; break;
1636 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1637 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1639 // gcc handles this as integer
1640 case TYPE_ENUM: tc = integer_type_class; break;
1643 panic("Unimplemented case in classify_type_to_firm().");
1646 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1647 ir_mode *const mode = mode_Is;
1648 tarval *const tv = new_tarval_from_long(tc, mode);
1649 return new_d_Const(dbgi, mode, tv);
1652 static ir_node *function_name_to_firm(
1653 const string_literal_expression_t *const expr)
1655 if (current_function_name == NULL) {
1656 const source_position_t *const src_pos =
1657 &expr->expression.source_position;
1658 const char *const name = current_function_decl->symbol->string;
1659 current_function_name = string_to_firm(src_pos, "__func__", name);
1662 return current_function_name;
1665 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1667 statement_t *statement = expr->statement;
1669 assert(statement->type == STATEMENT_COMPOUND);
1670 return compound_statement_to_firm((compound_statement_t*) statement);
1673 static ir_node *dereference_addr(const unary_expression_t *const expression)
1675 assert(expression->type == UNEXPR_DEREFERENCE);
1676 return expression_to_firm(expression->value);
1679 static ir_node *expression_to_addr(const expression_t *expression)
1681 switch(expression->type) {
1682 case EXPR_REFERENCE:
1683 return reference_addr(&expression->reference);
1684 case EXPR_ARRAY_ACCESS:
1685 return array_access_addr(&expression->array_access);
1687 return select_addr(&expression->select);
1689 return call_expression_to_firm(&expression->call);
1691 const unary_expression_t *const unary_expr = &expression->unary;
1692 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1693 return dereference_addr(unary_expr);
1700 panic("trying to get address of non-lvalue");
1703 static ir_node *_expression_to_firm(const expression_t *expression)
1705 switch(expression->type) {
1707 return const_to_firm(&expression->conste);
1708 case EXPR_STRING_LITERAL:
1709 return string_literal_to_firm(&expression->string);
1710 case EXPR_REFERENCE:
1711 return reference_expression_to_firm(&expression->reference);
1713 return call_expression_to_firm(&expression->call);
1715 return unary_expression_to_firm(&expression->unary);
1717 return binary_expression_to_firm(&expression->binary);
1718 case EXPR_ARRAY_ACCESS:
1719 return array_access_to_firm(&expression->array_access);
1721 return sizeof_to_firm(&expression->sizeofe);
1722 case EXPR_CONDITIONAL:
1723 return conditional_to_firm(&expression->conditional);
1725 return select_to_firm(&expression->select);
1726 case EXPR_CLASSIFY_TYPE:
1727 return classify_type_to_firm(&expression->classify_type);
1729 case EXPR_PRETTY_FUNCTION:
1730 return function_name_to_firm(&expression->string);
1731 case EXPR_STATEMENT:
1732 return statement_expression_to_firm(&expression->statement);
1735 case EXPR_BUILTIN_SYMBOL:
1736 panic("unimplemented expression found");
1742 panic("invalid expression found");
1745 static ir_node *expression_to_firm(const expression_t *expression)
1747 ir_node *res = _expression_to_firm(expression);
1749 if(res != NULL && get_irn_mode(res) == mode_b) {
1750 ir_mode *mode = get_ir_mode(expression->base.datatype);
1751 res = create_conv(NULL, res, mode);
1757 static ir_node *expression_to_modeb(const expression_t *expression)
1759 ir_node *res = _expression_to_firm(expression);
1760 res = create_conv(NULL, res, mode_b);
1766 * create a short-circuit expression evaluation that tries to construct
1767 * efficient control flow structures for &&, || and ! expressions
1769 static void create_condition_evaluation(const expression_t *expression,
1770 ir_node *true_block,
1771 ir_node *false_block)
1773 switch(expression->type) {
1775 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1776 if(unary_expression->type == UNEXPR_NOT) {
1777 create_condition_evaluation(unary_expression->value, false_block,
1784 binary_expression_t *binary_expression
1785 = (binary_expression_t*) expression;
1786 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1787 ir_node *cur_block = get_cur_block();
1788 ir_node *extra_block = new_immBlock();
1789 set_cur_block(cur_block);
1790 create_condition_evaluation(binary_expression->left, extra_block,
1792 mature_immBlock(extra_block);
1793 set_cur_block(extra_block);
1794 create_condition_evaluation(binary_expression->right, true_block,
1798 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1799 ir_node *cur_block = get_cur_block();
1800 ir_node *extra_block = new_immBlock();
1801 set_cur_block(cur_block);
1802 create_condition_evaluation(binary_expression->left, true_block,
1804 mature_immBlock(extra_block);
1805 set_cur_block(extra_block);
1806 create_condition_evaluation(binary_expression->right, true_block,
1816 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1817 ir_node *condition = expression_to_modeb(expression);
1818 ir_node *cond = new_d_Cond(dbgi, condition);
1819 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1820 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1822 add_immBlock_pred(true_block, true_proj);
1823 add_immBlock_pred(false_block, false_proj);
1825 set_cur_block(NULL);
1828 static void return_statement_to_firm(return_statement_t *statement)
1830 if(get_cur_block() == NULL)
1833 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1835 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1839 if(get_method_n_ress(func_irtype) > 0) {
1840 ir_type *res_type = get_method_res_type(func_irtype, 0);
1842 if(statement->return_value != NULL) {
1843 ir_node *node = expression_to_firm(statement->return_value);
1844 node = do_strict_conv(dbgi, node);
1848 if(is_compound_type(res_type)) {
1851 mode = get_type_mode(res_type);
1853 in[0] = new_Unknown(mode);
1857 /* build return_value for its side effects */
1858 if(statement->return_value != NULL) {
1859 expression_to_firm(statement->return_value);
1864 ir_node *store = get_store();
1865 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1867 ir_node *end_block = get_irg_end_block(current_ir_graph);
1868 add_immBlock_pred(end_block, ret);
1870 set_cur_block(NULL);
1873 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1875 if(get_cur_block() == NULL)
1878 return expression_to_firm(statement->expression);
1881 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1883 ir_node *result = NULL;
1884 statement_t *statement = compound->statements;
1885 for( ; statement != NULL; statement = statement->base.next) {
1886 //context2firm(&statement->context);
1888 if(statement->base.next == NULL
1889 && statement->type == STATEMENT_EXPRESSION) {
1890 result = expression_statement_to_firm(
1891 (expression_statement_t*) statement);
1894 statement_to_firm(statement);
1900 static void if_statement_to_firm(if_statement_t *statement)
1902 ir_node *cur_block = get_cur_block();
1904 ir_node *fallthrough_block = new_immBlock();
1906 /* the true (blocks) */
1907 ir_node *true_block;
1908 if (statement->true_statement != NULL) {
1909 true_block = new_immBlock();
1910 statement_to_firm(statement->true_statement);
1911 if(get_cur_block() != NULL) {
1912 ir_node *jmp = new_Jmp();
1913 add_immBlock_pred(fallthrough_block, jmp);
1916 true_block = fallthrough_block;
1919 /* the false (blocks) */
1920 ir_node *false_block;
1921 if(statement->false_statement != NULL) {
1922 false_block = new_immBlock();
1924 statement_to_firm(statement->false_statement);
1925 if(get_cur_block() != NULL) {
1926 ir_node *jmp = new_Jmp();
1927 add_immBlock_pred(fallthrough_block, jmp);
1930 false_block = fallthrough_block;
1933 /* create the condition */
1934 if(cur_block != NULL) {
1935 set_cur_block(cur_block);
1936 create_condition_evaluation(statement->condition, true_block,
1940 mature_immBlock(true_block);
1941 if(false_block != fallthrough_block) {
1942 mature_immBlock(false_block);
1944 mature_immBlock(fallthrough_block);
1946 set_cur_block(fallthrough_block);
1949 static void while_statement_to_firm(while_statement_t *statement)
1951 ir_node *jmp = NULL;
1952 if(get_cur_block() != NULL) {
1956 /* create the header block */
1957 ir_node *header_block = new_immBlock();
1959 add_immBlock_pred(header_block, jmp);
1962 /* the false block */
1963 ir_node *false_block = new_immBlock();
1966 ir_node *body_block;
1967 if (statement->body != NULL) {
1968 ir_node *old_continue_label = continue_label;
1969 ir_node *old_break_label = break_label;
1970 continue_label = header_block;
1971 break_label = false_block;
1973 body_block = new_immBlock();
1974 statement_to_firm(statement->body);
1976 assert(continue_label == header_block);
1977 assert(break_label == false_block);
1978 continue_label = old_continue_label;
1979 break_label = old_break_label;
1981 if(get_cur_block() != NULL) {
1983 add_immBlock_pred(header_block, jmp);
1986 body_block = header_block;
1989 /* create the condition */
1990 set_cur_block(header_block);
1992 create_condition_evaluation(statement->condition, body_block, false_block);
1993 mature_immBlock(body_block);
1994 mature_immBlock(false_block);
1995 mature_immBlock(header_block);
1997 set_cur_block(false_block);
2000 static void do_while_statement_to_firm(do_while_statement_t *statement)
2002 ir_node *jmp = NULL;
2003 if(get_cur_block() != NULL) {
2007 /* create the header block */
2008 ir_node *header_block = new_immBlock();
2010 /* the false block */
2011 ir_node *false_block = new_immBlock();
2014 ir_node *body_block = new_immBlock();
2016 add_immBlock_pred(body_block, jmp);
2019 if (statement->body != NULL) {
2020 ir_node *old_continue_label = continue_label;
2021 ir_node *old_break_label = break_label;
2022 continue_label = header_block;
2023 break_label = false_block;
2025 statement_to_firm(statement->body);
2027 assert(continue_label == header_block);
2028 assert(break_label == false_block);
2029 continue_label = old_continue_label;
2030 break_label = old_break_label;
2032 if (get_cur_block() == NULL) {
2033 mature_immBlock(header_block);
2034 mature_immBlock(body_block);
2035 mature_immBlock(false_block);
2040 ir_node *body_jmp = new_Jmp();
2041 add_immBlock_pred(header_block, body_jmp);
2042 mature_immBlock(header_block);
2044 /* create the condition */
2045 set_cur_block(header_block);
2047 create_condition_evaluation(statement->condition, body_block, false_block);
2048 mature_immBlock(body_block);
2049 mature_immBlock(false_block);
2050 mature_immBlock(header_block);
2052 set_cur_block(false_block);
2055 static void for_statement_to_firm(for_statement_t *statement)
2057 ir_node *jmp = NULL;
2058 if (get_cur_block() != NULL) {
2059 if(statement->initialisation != NULL) {
2060 expression_to_firm(statement->initialisation);
2065 /* create the step block */
2066 ir_node *const step_block = new_immBlock();
2067 if (statement->step != NULL) {
2068 expression_to_firm(statement->step);
2070 ir_node *const step_jmp = new_Jmp();
2072 /* create the header block */
2073 ir_node *const header_block = new_immBlock();
2075 add_immBlock_pred(header_block, jmp);
2077 add_immBlock_pred(header_block, step_jmp);
2079 /* the false block */
2080 ir_node *const false_block = new_immBlock();
2083 ir_node * body_block;
2084 if (statement->body != NULL) {
2085 ir_node *const old_continue_label = continue_label;
2086 ir_node *const old_break_label = break_label;
2087 continue_label = step_block;
2088 break_label = false_block;
2090 body_block = new_immBlock();
2091 statement_to_firm(statement->body);
2093 assert(continue_label == step_block);
2094 assert(break_label == false_block);
2095 continue_label = old_continue_label;
2096 break_label = old_break_label;
2098 if (get_cur_block() != NULL) {
2100 add_immBlock_pred(step_block, jmp);
2103 body_block = step_block;
2106 /* create the condition */
2107 set_cur_block(header_block);
2108 if (statement->condition != NULL) {
2109 create_condition_evaluation(statement->condition, body_block,
2112 keep_alive(header_block);
2114 add_immBlock_pred(body_block, jmp);
2117 mature_immBlock(body_block);
2118 mature_immBlock(false_block);
2119 mature_immBlock(step_block);
2120 mature_immBlock(header_block);
2121 mature_immBlock(false_block);
2123 set_cur_block(false_block);
2126 static void create_declaration_entity(declaration_t *declaration,
2127 declaration_type_t declaration_type,
2128 ir_type *parent_type)
2130 ident *id = new_id_from_str(declaration->symbol->string);
2131 ir_type *irtype = get_ir_type(declaration->type);
2132 ir_entity *entity = new_entity(parent_type, id, irtype);
2133 set_entity_ld_ident(entity, id);
2135 declaration->declaration_type = (unsigned char) declaration_type;
2136 declaration->v.entity = entity;
2137 set_entity_variability(entity, variability_uninitialized);
2138 /* TODO: visibility? */
2141 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2143 enum compound_graph_entry_type_t {
2144 COMPOUND_GRAPH_ENTRY_ARRAY,
2145 COMPOUND_GRAPH_ENTRY_COMPOUND
2148 struct compound_graph_path_entry_t {
2154 compound_graph_path_entry_t *prev;
2157 static void create_initializer_object(initializer_t *initializer, type_t *type,
2158 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2160 static compound_graph_path *create_compound_path(ir_type *type,
2161 compound_graph_path_entry_t *entry, int len)
2163 compound_graph_path *path = new_compound_graph_path(type, len);
2166 for( ; entry != NULL; entry = entry->prev, --i) {
2168 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2169 set_compound_graph_path_node(path, i, entry->v.entity);
2171 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2172 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2180 static void create_initializer_value(initializer_value_t *initializer,
2182 compound_graph_path_entry_t *entry,
2185 ir_node *node = expression_to_firm(initializer->value);
2186 ir_type *type = get_entity_type(entity);
2187 compound_graph_path *path = create_compound_path(type, entry, len);
2188 add_compound_ent_value_w_path(entity, node, path);
2191 static void create_initializer_compound(initializer_list_t *initializer,
2192 compound_type_t *type,
2194 compound_graph_path_entry_t *last_entry,
2197 declaration_t *compound_declaration = type->declaration;
2199 declaration_t *compound_entry = compound_declaration->context.declarations;
2201 compound_graph_path_entry_t entry;
2202 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2203 entry.prev = last_entry;
2207 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2208 if(compound_entry->symbol == NULL)
2210 if(compound_entry->namespc != NAMESPACE_NORMAL)
2213 if(i >= initializer->len)
2216 entry.v.entity = compound_entry->v.entity;
2218 initializer_t *sub_initializer = initializer->initializers[i];
2220 assert(compound_entry != NULL);
2221 assert(compound_entry->declaration_type
2222 == DECLARATION_TYPE_COMPOUND_MEMBER);
2224 if(sub_initializer->type == INITIALIZER_VALUE) {
2225 create_initializer_value(&sub_initializer->value,
2226 entity, &entry, len);
2228 type_t *entry_type = skip_typeref(compound_entry->type);
2229 create_initializer_object(sub_initializer, entry_type, entity,
2237 static void create_initializer_array(initializer_list_t *initializer,
2238 array_type_t *type, ir_entity *entity,
2239 compound_graph_path_entry_t *last_entry,
2242 type_t *element_type = type->element_type;
2243 element_type = skip_typeref(element_type);
2245 compound_graph_path_entry_t entry;
2246 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2247 entry.prev = last_entry;
2251 for(i = 0; i < initializer->len; ++i) {
2252 entry.v.array_index = i;
2254 initializer_t *sub_initializer = initializer->initializers[i];
2256 if(sub_initializer->type == INITIALIZER_VALUE) {
2257 create_initializer_value(&sub_initializer->value,
2258 entity, &entry, len);
2260 create_initializer_object(sub_initializer, element_type, entity,
2266 /* TODO: initialize rest... */
2267 if(type->size_expression != NULL) {
2268 size_t array_len = fold_constant(type->size_expression);
2269 for( ; i < array_len; ++i) {
2276 static void create_initializer_string(initializer_string_t *initializer,
2277 array_type_t *type, ir_entity *entity,
2278 compound_graph_path_entry_t *last_entry,
2281 type_t *element_type = type->element_type;
2282 element_type = skip_typeref(element_type);
2284 compound_graph_path_entry_t entry;
2285 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2286 entry.prev = last_entry;
2289 ir_type *irtype = get_entity_type(entity);
2290 size_t arr_len = get_array_type_size(type);
2291 const char *p = initializer->string;
2293 for(i = 0; i < arr_len; ++i, ++p) {
2294 entry.v.array_index = i;
2296 ir_node *node = new_Const_long(mode_Bs, *p);
2297 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2298 add_compound_ent_value_w_path(entity, node, path);
2305 static void create_initializer_object(initializer_t *initializer, type_t *type,
2306 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2308 if(type->type == TYPE_ARRAY) {
2309 array_type_t *array_type = &type->array;
2311 if(initializer->type == INITIALIZER_STRING) {
2312 initializer_string_t *string = &initializer->string;
2313 create_initializer_string(string, array_type, entity, entry, len);
2315 assert(initializer->type == INITIALIZER_LIST);
2316 initializer_list_t *list = &initializer->list;
2317 create_initializer_array(list, array_type, entity, entry, len);
2320 assert(initializer->type == INITIALIZER_LIST);
2321 initializer_list_t *list = &initializer->list;
2323 assert(type->type == TYPE_COMPOUND_STRUCT
2324 || type->type == TYPE_COMPOUND_UNION);
2325 compound_type_t *compound_type = &type->compound;
2326 create_initializer_compound(list, compound_type, entity, entry, len);
2330 static void create_initializer_local_variable_entity(declaration_t *declaration)
2332 initializer_t *initializer = declaration->init.initializer;
2333 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2334 ir_entity *entity = declaration->v.entity;
2335 ir_node *memory = get_store();
2336 ir_node *nomem = new_NoMem();
2337 ir_node *frame = get_irg_frame(current_ir_graph);
2338 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2340 if(is_atomic_entity(entity)) {
2341 assert(initializer->type == INITIALIZER_VALUE);
2342 initializer_value_t *initializer_value = &initializer->value;
2344 ir_node *value = expression_to_firm(initializer_value->value);
2345 ir_node *store = new_d_Store(dbgi, memory, addr, value);
2346 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2347 set_store(store_mem);
2351 /* create a "template" entity which is copied to the entity on the stack */
2352 ident *id = unique_ident("initializer");
2353 ir_type *irtype = get_ir_type(declaration->type);
2354 ir_type *global_type = get_glob_type();
2355 ir_entity *init_entity = new_entity(global_type, id, irtype);
2356 set_entity_ld_ident(init_entity, id);
2358 set_entity_variability(init_entity, variability_initialized);
2359 set_entity_visibility(init_entity, visibility_local);
2361 ir_graph *old_current_ir_graph = current_ir_graph;
2362 current_ir_graph = get_const_code_irg();
2364 type_t *type = skip_typeref(declaration->type);
2365 create_initializer_object(initializer, type, init_entity, NULL, 0);
2367 assert(current_ir_graph == get_const_code_irg());
2368 current_ir_graph = old_current_ir_graph;
2370 ir_node *src_addr = create_symconst(dbgi, init_entity);
2371 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2373 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2374 set_store(copyb_mem);
2377 static void create_initializer(declaration_t *declaration)
2379 initializer_t *initializer = declaration->init.initializer;
2380 if(initializer == NULL)
2383 declaration_type_t declaration_type
2384 = (declaration_type_t) declaration->declaration_type;
2385 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2386 create_initializer_local_variable_entity(declaration);
2390 if(initializer->type == INITIALIZER_VALUE) {
2391 initializer_value_t *initializer_value = &initializer->value;
2393 ir_node *value = expression_to_firm(initializer_value->value);
2395 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2396 set_value(declaration->v.value_number, value);
2398 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2400 ir_entity *entity = declaration->v.entity;
2402 set_entity_variability(entity, variability_initialized);
2403 set_atomic_ent_value(entity, value);
2406 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2407 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2409 ir_entity *entity = declaration->v.entity;
2410 set_entity_variability(entity, variability_initialized);
2412 type_t *type = skip_typeref(declaration->type);
2413 create_initializer_object(initializer, type, entity, NULL, 0);
2417 static void create_local_variable(declaration_t *declaration)
2419 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2421 bool needs_entity = declaration->address_taken;
2422 type_t *type = skip_typeref(declaration->type);
2424 if(type->type == TYPE_ARRAY
2425 || type->type == TYPE_COMPOUND_STRUCT
2426 || type->type == TYPE_COMPOUND_UNION) {
2427 needs_entity = true;
2431 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2432 create_declaration_entity(declaration,
2433 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2436 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2437 declaration->v.value_number = next_value_number_function;
2438 ++next_value_number_function;
2441 create_initializer(declaration);
2444 static void create_local_static_variable(declaration_t *declaration)
2446 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2448 type_t *type = skip_typeref(declaration->type);
2449 ir_type *global_type = get_glob_type();
2450 ident *id = unique_ident(declaration->symbol->string);
2451 ir_type *irtype = get_ir_type(type);
2452 ir_entity *entity = new_entity(global_type, id, irtype);
2453 set_entity_ld_ident(entity, id);
2455 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2456 declaration->v.entity = entity;
2457 set_entity_variability(entity, variability_uninitialized);
2458 set_entity_visibility(entity, visibility_local);
2460 ir_graph *old_current_ir_graph = current_ir_graph;
2461 current_ir_graph = get_const_code_irg();
2463 create_initializer(declaration);
2465 assert(current_ir_graph == get_const_code_irg());
2466 current_ir_graph = old_current_ir_graph;
2469 static void declaration_statement_to_firm(declaration_statement_t *statement)
2471 declaration_t *declaration = statement->declarations_begin;
2472 declaration_t *end = statement->declarations_end->next;
2473 for( ; declaration != end; declaration = declaration->next) {
2474 type_t *type = declaration->type;
2476 switch ((storage_class_tag_t)declaration->storage_class) {
2477 case STORAGE_CLASS_TYPEDEF:
2479 case STORAGE_CLASS_STATIC:
2480 create_local_static_variable(declaration);
2482 case STORAGE_CLASS_ENUM_ENTRY:
2483 panic("enum entry declaration in local block found");
2484 case STORAGE_CLASS_EXTERN:
2485 panic("extern declaration in local block found");
2486 case STORAGE_CLASS_NONE:
2487 case STORAGE_CLASS_AUTO:
2488 case STORAGE_CLASS_REGISTER:
2489 if(type->type == TYPE_FUNCTION) {
2490 panic("nested functions not supported yet");
2492 create_local_variable(declaration);
2495 case STORAGE_CLASS_THREAD:
2496 case STORAGE_CLASS_THREAD_EXTERN:
2497 case STORAGE_CLASS_THREAD_STATIC:
2500 panic("invalid storage class found");
2504 static void create_jump_statement(const statement_t *statement,
2505 ir_node *target_block)
2507 if(get_cur_block() == NULL)
2510 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2511 ir_node *jump = new_d_Jmp(dbgi);
2512 add_immBlock_pred(target_block, jump);
2514 set_cur_block(NULL);
2517 static void switch_statement_to_firm(const switch_statement_t *statement)
2519 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2521 ir_node *expression = expression_to_firm(statement->expression);
2522 ir_node *cond = new_d_Cond(dbgi, expression);
2523 ir_node *break_block = new_immBlock();
2525 set_cur_block(NULL);
2527 ir_node *const old_switch_cond = current_switch_cond;
2528 ir_node *const old_break_label = break_label;
2529 const bool old_saw_default_label = saw_default_label;
2530 current_switch_cond = cond;
2531 break_label = break_block;
2533 statement_to_firm(statement->body);
2535 if(get_cur_block() != NULL) {
2536 ir_node *jmp = new_Jmp();
2537 add_immBlock_pred(break_block, jmp);
2540 if (!saw_default_label) {
2541 set_cur_block(get_nodes_block(cond));
2542 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2543 MAGIC_DEFAULT_PN_NUMBER);
2544 add_immBlock_pred(break_block, proj);
2547 assert(current_switch_cond == cond);
2548 assert(break_label == break_block);
2549 current_switch_cond = old_switch_cond;
2550 break_label = old_break_label;
2551 saw_default_label = old_saw_default_label;
2553 mature_immBlock(break_block);
2554 set_cur_block(break_block);
2557 static long fold_constant(const expression_t *expression)
2559 ir_graph *old_current_ir_graph = current_ir_graph;
2560 current_ir_graph = get_const_code_irg();
2562 ir_node *cnst = expression_to_firm(expression);
2563 if(!is_Const(cnst)) {
2564 panic("couldn't fold constantl");
2566 tarval *tv = get_Const_tarval(cnst);
2567 if(!tarval_is_long(tv)) {
2568 panic("folded constant not an integer");
2571 long res = get_tarval_long(tv);
2573 current_ir_graph = old_current_ir_graph;
2577 static void case_label_to_firm(const case_label_statement_t *statement)
2579 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2581 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2583 /* let's create a node and hope firm constant folding creates a Const
2586 set_cur_block(get_nodes_block(current_switch_cond));
2587 if(statement->expression) {
2588 long pn = fold_constant(statement->expression);
2589 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2590 /* oops someone detected our cheating... */
2591 panic("magic default pn used");
2593 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2595 saw_default_label = true;
2596 proj = new_d_defaultProj(dbgi, current_switch_cond,
2597 MAGIC_DEFAULT_PN_NUMBER);
2600 ir_node *block = new_immBlock();
2601 if (fallthrough != NULL) {
2602 add_immBlock_pred(block, fallthrough);
2604 add_immBlock_pred(block, proj);
2605 mature_immBlock(block);
2607 statement_to_firm(statement->label_statement);
2610 static ir_node *get_label_block(declaration_t *label)
2612 assert(label->namespc == NAMESPACE_LABEL);
2614 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2615 return label->v.block;
2617 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2619 ir_node *old_cur_block = get_cur_block();
2620 ir_node *block = new_immBlock();
2621 set_cur_block(old_cur_block);
2623 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2624 label->v.block = block;
2626 ARR_APP1(ir_node *, imature_blocks, block);
2631 static void label_to_firm(const label_statement_t *statement)
2633 ir_node *block = get_label_block(statement->label);
2635 if(get_cur_block() != NULL) {
2636 ir_node *jmp = new_Jmp();
2637 add_immBlock_pred(block, jmp);
2640 set_cur_block(block);
2643 statement_to_firm(statement->label_statement);
2646 static void goto_to_firm(const goto_statement_t *statement)
2648 if(get_cur_block() == NULL)
2651 ir_node *block = get_label_block(statement->label);
2652 ir_node *jmp = new_Jmp();
2653 add_immBlock_pred(block, jmp);
2655 set_cur_block(NULL);
2658 static void statement_to_firm(statement_t *statement)
2660 switch(statement->type) {
2661 case STATEMENT_INVALID:
2662 panic("invalid statement found");
2663 case STATEMENT_COMPOUND:
2664 compound_statement_to_firm(&statement->compound);
2666 case STATEMENT_RETURN:
2667 return_statement_to_firm(&statement->returns);
2669 case STATEMENT_EXPRESSION:
2670 expression_statement_to_firm(&statement->expression);
2673 if_statement_to_firm(&statement->ifs);
2675 case STATEMENT_WHILE:
2676 while_statement_to_firm(&statement->whiles);
2678 case STATEMENT_DO_WHILE:
2679 do_while_statement_to_firm(&statement->do_while);
2681 case STATEMENT_DECLARATION:
2682 declaration_statement_to_firm(&statement->declaration);
2684 case STATEMENT_BREAK:
2685 create_jump_statement(statement, break_label);
2687 case STATEMENT_CONTINUE:
2688 create_jump_statement(statement, continue_label);
2690 case STATEMENT_SWITCH:
2691 switch_statement_to_firm(&statement->switchs);
2693 case STATEMENT_CASE_LABEL:
2694 case_label_to_firm(&statement->case_label);
2697 for_statement_to_firm(&statement->fors);
2699 case STATEMENT_LABEL:
2700 label_to_firm(&statement->label);
2702 case STATEMENT_GOTO:
2703 goto_to_firm(&statement->gotos);
2708 panic("Statement not implemented\n");
2711 static int count_local_declarations(const declaration_t * decl,
2712 const declaration_t *const end)
2715 for (; decl != end; decl = decl->next) {
2716 const type_t *type = skip_typeref(decl->type);
2717 switch (type->type) {
2721 if (!decl->address_taken)
2731 static int count_decls_in_stmts(const statement_t *stmt)
2734 for (; stmt != NULL; stmt = stmt->base.next) {
2735 switch (stmt->type) {
2736 case STATEMENT_DECLARATION: {
2737 const declaration_statement_t *const decl_stmt =
2738 (const declaration_statement_t*)stmt;
2739 count += count_local_declarations(decl_stmt->declarations_begin,
2740 decl_stmt->declarations_end->next);
2744 case STATEMENT_COMPOUND: {
2745 const compound_statement_t *const comp =
2746 (const compound_statement_t*)stmt;
2747 count += count_decls_in_stmts(comp->statements);
2751 case STATEMENT_IF: {
2752 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2753 count += count_decls_in_stmts(if_stmt->true_statement);
2754 count += count_decls_in_stmts(if_stmt->false_statement);
2758 case STATEMENT_SWITCH: {
2759 const switch_statement_t *const switch_stmt =
2760 (const switch_statement_t*)stmt;
2761 count += count_decls_in_stmts(switch_stmt->body);
2765 case STATEMENT_LABEL: {
2766 const label_statement_t *const label_stmt =
2767 (const label_statement_t*)stmt;
2768 count += count_decls_in_stmts(label_stmt->label_statement);
2772 case STATEMENT_WHILE: {
2773 const while_statement_t *const while_stmt =
2774 (const while_statement_t*)stmt;
2775 count += count_decls_in_stmts(while_stmt->body);
2779 case STATEMENT_DO_WHILE: {
2780 const do_while_statement_t *const do_while_stmt =
2781 (const do_while_statement_t*)stmt;
2782 count += count_decls_in_stmts(do_while_stmt->body);
2786 case STATEMENT_FOR: {
2787 const for_statement_t *const for_stmt =
2788 (const for_statement_t*)stmt;
2789 /* TODO initialisation */
2790 count += count_decls_in_stmts(for_stmt->body);
2795 case STATEMENT_BREAK:
2796 case STATEMENT_CASE_LABEL:
2797 case STATEMENT_CONTINUE:
2798 case STATEMENT_EXPRESSION:
2799 case STATEMENT_GOTO:
2800 case STATEMENT_INVALID:
2801 case STATEMENT_RETURN:
2808 static int get_function_n_local_vars(declaration_t *declaration)
2812 /* count parameters */
2813 count += count_local_declarations(declaration->context.declarations, NULL);
2815 /* count local variables declared in body */
2816 count += count_decls_in_stmts(declaration->init.statement);
2818 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2819 * for expression statements... */
2825 static void initialize_function_parameters(declaration_t *declaration)
2827 ir_graph *irg = current_ir_graph;
2828 ir_node *args = get_irg_args(irg);
2829 ir_node *start_block = get_irg_start_block(irg);
2830 ir_type *function_irtype = get_ir_type(declaration->type);
2833 declaration_t *parameter = declaration->context.declarations;
2834 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2835 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2836 type_t *type = skip_typeref(parameter->type);
2838 bool needs_entity = parameter->address_taken;
2839 if(type->type == TYPE_COMPOUND_STRUCT
2840 || type->type == TYPE_COMPOUND_UNION) {
2841 needs_entity = true;
2845 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2846 ident *id = new_id_from_str(parameter->symbol->string);
2847 set_entity_ident(entity, id);
2849 parameter->declaration_type
2850 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2851 parameter->v.entity = entity;
2855 ir_mode *mode = get_ir_mode(parameter->type);
2857 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2859 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2860 parameter->v.value_number = next_value_number_function;
2861 ++next_value_number_function;
2863 set_value(parameter->v.value_number, proj);
2867 static void create_function(declaration_t *declaration)
2869 ir_entity *function_entity = get_function_entity(declaration);
2871 if(declaration->init.statement == NULL)
2874 current_function_decl = declaration;
2875 current_function_name = NULL;
2877 assert(imature_blocks == NULL);
2878 imature_blocks = NEW_ARR_F(ir_node*, 0);
2880 int n_local_vars = get_function_n_local_vars(declaration);
2881 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
2882 ir_node *first_block = get_cur_block();
2884 next_value_number_function = 0;
2885 initialize_function_parameters(declaration);
2887 statement_to_firm(declaration->init.statement);
2889 ir_node *end_block = get_irg_end_block(irg);
2891 /* do we have a return statement yet? */
2892 if(get_cur_block() != NULL) {
2893 assert(declaration->type->type == TYPE_FUNCTION);
2894 const function_type_t* const func_type = &declaration->type->function;
2895 const type_t *result_type = skip_typeref(func_type->result_type);
2898 if (is_type_atomic(result_type, ATOMIC_TYPE_VOID)) {
2899 ret = new_Return(get_store(), 0, NULL);
2902 if(is_type_scalar(result_type)) {
2903 mode = get_ir_mode(func_type->result_type);
2909 // ยง5.1.2.2.3 main implicitly returns 0
2910 if (strcmp(declaration->symbol->string, "main") == 0) {
2911 in[0] = new_Const(mode, get_mode_null(mode));
2913 in[0] = new_Unknown(mode);
2915 ret = new_Return(get_store(), 1, in);
2917 add_immBlock_pred(end_block, ret);
2920 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2921 mature_immBlock(imature_blocks[i]);
2923 DEL_ARR_F(imature_blocks);
2924 imature_blocks = NULL;
2926 mature_immBlock(first_block);
2927 mature_immBlock(end_block);
2929 irg_finalize_cons(irg);
2931 /* finalize the frame type */
2932 ir_type *frame_type = get_irg_frame_type(irg);
2933 int n = get_compound_n_members(frame_type);
2936 for(int i = 0; i < n; ++i) {
2937 ir_entity *entity = get_compound_member(frame_type, i);
2938 ir_type *entity_type = get_entity_type(entity);
2940 int align = get_type_alignment_bytes(entity_type);
2941 if(align > align_all)
2945 misalign = offset % align;
2947 offset += align - misalign;
2951 set_entity_offset(entity, offset);
2952 offset += get_type_size_bytes(entity_type);
2954 set_type_size_bytes(frame_type, offset);
2955 set_type_alignment_bytes(frame_type, align_all);
2956 set_type_state(frame_type, layout_fixed);
2961 static void create_global_variable(declaration_t *declaration)
2965 switch ((storage_class_tag_t)declaration->storage_class) {
2966 case STORAGE_CLASS_STATIC:
2967 vis = visibility_local;
2970 case STORAGE_CLASS_EXTERN:
2971 vis = visibility_external_allocated;
2974 case STORAGE_CLASS_NONE:
2975 vis = visibility_external_visible;
2978 case STORAGE_CLASS_THREAD:
2979 vis = visibility_external_visible;
2982 case STORAGE_CLASS_THREAD_EXTERN:
2983 vis = visibility_external_allocated;
2986 case STORAGE_CLASS_THREAD_STATIC:
2987 vis = visibility_local;
2991 var_type = get_tls_type();
2995 var_type = get_glob_type();
2999 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3001 set_entity_visibility(declaration->v.entity, vis);
3003 current_ir_graph = get_const_code_irg();
3004 create_initializer(declaration);
3007 case STORAGE_CLASS_TYPEDEF:
3008 case STORAGE_CLASS_AUTO:
3009 case STORAGE_CLASS_REGISTER:
3010 case STORAGE_CLASS_ENUM_ENTRY:
3013 panic("Invalid storage class for global variable");
3016 static void context_to_firm(context_t *context)
3018 /* first pass: create declarations */
3019 declaration_t *declaration = context->declarations;
3020 for( ; declaration != NULL; declaration = declaration->next) {
3021 if(declaration->namespc != NAMESPACE_NORMAL)
3023 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3024 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3026 if(declaration->symbol == NULL)
3029 type_t *type = declaration->type;
3030 if(type->type == TYPE_FUNCTION) {
3031 get_function_entity(declaration);
3033 create_global_variable(declaration);
3037 /* second pass: create code */
3038 declaration = context->declarations;
3039 for( ; declaration != NULL; declaration = declaration->next) {
3040 if(declaration->namespc != NAMESPACE_NORMAL)
3042 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3043 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3045 if(declaration->symbol == NULL)
3048 type_t *type = declaration->type;
3049 if(type->type != TYPE_FUNCTION)
3052 create_function(declaration);
3056 void translation_unit_to_firm(translation_unit_t *unit)
3058 /* just to be sure */
3059 continue_label = NULL;
3061 current_switch_cond = NULL;
3063 context_to_firm(& unit->context);