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) return NULL;
1510 ir_node *in[2] = { true_val, false_val };
1511 ir_mode *mode = get_irn_mode(true_val);
1512 assert(get_irn_mode(false_val) == mode);
1513 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1518 static ir_node *select_addr(const select_expression_t *expression)
1520 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1522 ir_node *compound_addr = expression_to_firm(expression->compound);
1524 declaration_t *entry = expression->compound_entry;
1525 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1526 ir_entity *entity = entry->v.entity;
1528 assert(entity != NULL);
1530 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1535 static ir_node *select_to_firm(const select_expression_t *expression)
1537 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1538 ir_node *addr = select_addr(expression);
1539 type_t *type = revert_automatic_type_conversion(
1540 (const expression_t*) expression);
1541 type = skip_typeref(type);
1542 ir_type *irtype = get_ir_type(type);
1544 return deref_address(irtype, addr, dbgi);
1547 /* Values returned by __builtin_classify_type. */
1548 typedef enum gcc_type_class
1554 enumeral_type_class,
1557 reference_type_class,
1561 function_type_class,
1572 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1574 const type_t *const type = expr->type_expression->base.datatype;
1580 const atomic_type_t *const atomic_type = &type->atomic;
1581 switch (atomic_type->atype) {
1582 // should not be reached
1583 case ATOMIC_TYPE_INVALID:
1587 // gcc cannot do that
1588 case ATOMIC_TYPE_VOID:
1589 tc = void_type_class;
1592 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1593 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1594 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1595 case ATOMIC_TYPE_SHORT:
1596 case ATOMIC_TYPE_USHORT:
1597 case ATOMIC_TYPE_INT:
1598 case ATOMIC_TYPE_UINT:
1599 case ATOMIC_TYPE_LONG:
1600 case ATOMIC_TYPE_ULONG:
1601 case ATOMIC_TYPE_LONGLONG:
1602 case ATOMIC_TYPE_ULONGLONG:
1603 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1604 tc = integer_type_class;
1607 case ATOMIC_TYPE_FLOAT:
1608 case ATOMIC_TYPE_DOUBLE:
1609 case ATOMIC_TYPE_LONG_DOUBLE:
1610 tc = real_type_class;
1613 #ifdef PROVIDE_COMPLEX
1614 case ATOMIC_TYPE_FLOAT_COMPLEX:
1615 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1616 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1617 tc = complex_type_class;
1619 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1620 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1621 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1622 tc = complex_type_class;
1627 panic("Unimplemented case in classify_type_to_firm().");
1632 case TYPE_ARRAY: // gcc handles this as pointer
1633 case TYPE_FUNCTION: // gcc handles this as pointer
1634 case TYPE_POINTER: tc = pointer_type_class; break;
1635 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1636 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1638 // gcc handles this as integer
1639 case TYPE_ENUM: tc = integer_type_class; break;
1642 panic("Unimplemented case in classify_type_to_firm().");
1645 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1646 ir_mode *const mode = mode_Is;
1647 tarval *const tv = new_tarval_from_long(tc, mode);
1648 return new_d_Const(dbgi, mode, tv);
1651 static ir_node *function_name_to_firm(
1652 const string_literal_expression_t *const expr)
1654 if (current_function_name == NULL) {
1655 const source_position_t *const src_pos =
1656 &expr->expression.source_position;
1657 const char *const name = current_function_decl->symbol->string;
1658 current_function_name = string_to_firm(src_pos, "__func__", name);
1661 return current_function_name;
1664 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1666 statement_t *statement = expr->statement;
1668 assert(statement->type == STATEMENT_COMPOUND);
1669 return compound_statement_to_firm((compound_statement_t*) statement);
1672 static ir_node *dereference_addr(const unary_expression_t *const expression)
1674 assert(expression->type == UNEXPR_DEREFERENCE);
1675 return expression_to_firm(expression->value);
1678 static ir_node *expression_to_addr(const expression_t *expression)
1680 switch(expression->type) {
1681 case EXPR_REFERENCE:
1682 return reference_addr(&expression->reference);
1683 case EXPR_ARRAY_ACCESS:
1684 return array_access_addr(&expression->array_access);
1686 return select_addr(&expression->select);
1688 return call_expression_to_firm(&expression->call);
1690 const unary_expression_t *const unary_expr = &expression->unary;
1691 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1692 return dereference_addr(unary_expr);
1699 panic("trying to get address of non-lvalue");
1702 static ir_node *_expression_to_firm(const expression_t *expression)
1704 switch(expression->type) {
1706 return const_to_firm(&expression->conste);
1707 case EXPR_STRING_LITERAL:
1708 return string_literal_to_firm(&expression->string);
1709 case EXPR_REFERENCE:
1710 return reference_expression_to_firm(&expression->reference);
1712 return call_expression_to_firm(&expression->call);
1714 return unary_expression_to_firm(&expression->unary);
1716 return binary_expression_to_firm(&expression->binary);
1717 case EXPR_ARRAY_ACCESS:
1718 return array_access_to_firm(&expression->array_access);
1720 return sizeof_to_firm(&expression->sizeofe);
1721 case EXPR_CONDITIONAL:
1722 return conditional_to_firm(&expression->conditional);
1724 return select_to_firm(&expression->select);
1725 case EXPR_CLASSIFY_TYPE:
1726 return classify_type_to_firm(&expression->classify_type);
1728 case EXPR_PRETTY_FUNCTION:
1729 return function_name_to_firm(&expression->string);
1730 case EXPR_STATEMENT:
1731 return statement_expression_to_firm(&expression->statement);
1734 case EXPR_BUILTIN_SYMBOL:
1735 panic("unimplemented expression found");
1741 panic("invalid expression found");
1744 static ir_node *expression_to_firm(const expression_t *expression)
1746 ir_node *res = _expression_to_firm(expression);
1748 if(res != NULL && get_irn_mode(res) == mode_b) {
1749 ir_mode *mode = get_ir_mode(expression->base.datatype);
1750 res = create_conv(NULL, res, mode);
1756 static ir_node *expression_to_modeb(const expression_t *expression)
1758 ir_node *res = _expression_to_firm(expression);
1759 res = create_conv(NULL, res, mode_b);
1765 * create a short-circuit expression evaluation that tries to construct
1766 * efficient control flow structures for &&, || and ! expressions
1768 static void create_condition_evaluation(const expression_t *expression,
1769 ir_node *true_block,
1770 ir_node *false_block)
1772 switch(expression->type) {
1774 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1775 if(unary_expression->type == UNEXPR_NOT) {
1776 create_condition_evaluation(unary_expression->value, false_block,
1783 binary_expression_t *binary_expression
1784 = (binary_expression_t*) expression;
1785 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1786 ir_node *cur_block = get_cur_block();
1787 ir_node *extra_block = new_immBlock();
1788 set_cur_block(cur_block);
1789 create_condition_evaluation(binary_expression->left, extra_block,
1791 mature_immBlock(extra_block);
1792 set_cur_block(extra_block);
1793 create_condition_evaluation(binary_expression->right, true_block,
1797 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1798 ir_node *cur_block = get_cur_block();
1799 ir_node *extra_block = new_immBlock();
1800 set_cur_block(cur_block);
1801 create_condition_evaluation(binary_expression->left, true_block,
1803 mature_immBlock(extra_block);
1804 set_cur_block(extra_block);
1805 create_condition_evaluation(binary_expression->right, true_block,
1815 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1816 ir_node *condition = expression_to_modeb(expression);
1817 ir_node *cond = new_d_Cond(dbgi, condition);
1818 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1819 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1821 add_immBlock_pred(true_block, true_proj);
1822 add_immBlock_pred(false_block, false_proj);
1824 set_cur_block(NULL);
1827 static void return_statement_to_firm(return_statement_t *statement)
1829 if(get_cur_block() == NULL)
1832 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1834 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1838 if(get_method_n_ress(func_irtype) > 0) {
1839 ir_type *res_type = get_method_res_type(func_irtype, 0);
1841 if(statement->return_value != NULL) {
1842 ir_node *node = expression_to_firm(statement->return_value);
1843 node = do_strict_conv(dbgi, node);
1847 if(is_compound_type(res_type)) {
1850 mode = get_type_mode(res_type);
1852 in[0] = new_Unknown(mode);
1856 /* build return_value for its side effects */
1857 if(statement->return_value != NULL) {
1858 expression_to_firm(statement->return_value);
1863 ir_node *store = get_store();
1864 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1866 ir_node *end_block = get_irg_end_block(current_ir_graph);
1867 add_immBlock_pred(end_block, ret);
1869 set_cur_block(NULL);
1872 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1874 if(get_cur_block() == NULL)
1877 return expression_to_firm(statement->expression);
1880 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1882 ir_node *result = NULL;
1883 statement_t *statement = compound->statements;
1884 for( ; statement != NULL; statement = statement->base.next) {
1885 //context2firm(&statement->context);
1887 if(statement->base.next == NULL
1888 && statement->type == STATEMENT_EXPRESSION) {
1889 result = expression_statement_to_firm(
1890 (expression_statement_t*) statement);
1893 statement_to_firm(statement);
1899 static void if_statement_to_firm(if_statement_t *statement)
1901 ir_node *cur_block = get_cur_block();
1903 ir_node *fallthrough_block = new_immBlock();
1905 /* the true (blocks) */
1906 ir_node *true_block;
1907 if (statement->true_statement != NULL) {
1908 true_block = new_immBlock();
1909 statement_to_firm(statement->true_statement);
1910 if(get_cur_block() != NULL) {
1911 ir_node *jmp = new_Jmp();
1912 add_immBlock_pred(fallthrough_block, jmp);
1915 true_block = fallthrough_block;
1918 /* the false (blocks) */
1919 ir_node *false_block;
1920 if(statement->false_statement != NULL) {
1921 false_block = new_immBlock();
1923 statement_to_firm(statement->false_statement);
1924 if(get_cur_block() != NULL) {
1925 ir_node *jmp = new_Jmp();
1926 add_immBlock_pred(fallthrough_block, jmp);
1929 false_block = fallthrough_block;
1932 /* create the condition */
1933 if(cur_block != NULL) {
1934 set_cur_block(cur_block);
1935 create_condition_evaluation(statement->condition, true_block,
1939 mature_immBlock(true_block);
1940 if(false_block != fallthrough_block) {
1941 mature_immBlock(false_block);
1943 mature_immBlock(fallthrough_block);
1945 set_cur_block(fallthrough_block);
1948 static void while_statement_to_firm(while_statement_t *statement)
1950 ir_node *jmp = NULL;
1951 if(get_cur_block() != NULL) {
1955 /* create the header block */
1956 ir_node *header_block = new_immBlock();
1958 add_immBlock_pred(header_block, jmp);
1961 /* the false block */
1962 ir_node *false_block = new_immBlock();
1965 ir_node *body_block;
1966 if (statement->body != NULL) {
1967 ir_node *old_continue_label = continue_label;
1968 ir_node *old_break_label = break_label;
1969 continue_label = header_block;
1970 break_label = false_block;
1972 body_block = new_immBlock();
1973 statement_to_firm(statement->body);
1975 assert(continue_label == header_block);
1976 assert(break_label == false_block);
1977 continue_label = old_continue_label;
1978 break_label = old_break_label;
1980 if(get_cur_block() != NULL) {
1982 add_immBlock_pred(header_block, jmp);
1985 body_block = header_block;
1988 /* create the condition */
1989 set_cur_block(header_block);
1991 create_condition_evaluation(statement->condition, body_block, false_block);
1992 mature_immBlock(body_block);
1993 mature_immBlock(false_block);
1994 mature_immBlock(header_block);
1996 set_cur_block(false_block);
1999 static void do_while_statement_to_firm(do_while_statement_t *statement)
2001 ir_node *jmp = NULL;
2002 if(get_cur_block() != NULL) {
2006 /* create the header block */
2007 ir_node *header_block = new_immBlock();
2009 /* the false block */
2010 ir_node *false_block = new_immBlock();
2013 ir_node *body_block = new_immBlock();
2015 add_immBlock_pred(body_block, jmp);
2018 if (statement->body != NULL) {
2019 ir_node *old_continue_label = continue_label;
2020 ir_node *old_break_label = break_label;
2021 continue_label = header_block;
2022 break_label = false_block;
2024 statement_to_firm(statement->body);
2026 assert(continue_label == header_block);
2027 assert(break_label == false_block);
2028 continue_label = old_continue_label;
2029 break_label = old_break_label;
2031 if (get_cur_block() == NULL) {
2032 mature_immBlock(header_block);
2033 mature_immBlock(body_block);
2034 mature_immBlock(false_block);
2039 ir_node *body_jmp = new_Jmp();
2040 add_immBlock_pred(header_block, body_jmp);
2041 mature_immBlock(header_block);
2043 /* create the condition */
2044 set_cur_block(header_block);
2046 create_condition_evaluation(statement->condition, body_block, false_block);
2047 mature_immBlock(body_block);
2048 mature_immBlock(false_block);
2049 mature_immBlock(header_block);
2051 set_cur_block(false_block);
2054 static void for_statement_to_firm(for_statement_t *statement)
2056 ir_node *jmp = NULL;
2057 if (get_cur_block() != NULL) {
2058 if(statement->initialisation != NULL) {
2059 expression_to_firm(statement->initialisation);
2064 /* create the step block */
2065 ir_node *const step_block = new_immBlock();
2066 if (statement->step != NULL) {
2067 expression_to_firm(statement->step);
2069 ir_node *const step_jmp = new_Jmp();
2071 /* create the header block */
2072 ir_node *const header_block = new_immBlock();
2074 add_immBlock_pred(header_block, jmp);
2076 add_immBlock_pred(header_block, step_jmp);
2078 /* the false block */
2079 ir_node *const false_block = new_immBlock();
2082 ir_node * body_block;
2083 if (statement->body != NULL) {
2084 ir_node *const old_continue_label = continue_label;
2085 ir_node *const old_break_label = break_label;
2086 continue_label = step_block;
2087 break_label = false_block;
2089 body_block = new_immBlock();
2090 statement_to_firm(statement->body);
2092 assert(continue_label == step_block);
2093 assert(break_label == false_block);
2094 continue_label = old_continue_label;
2095 break_label = old_break_label;
2097 if (get_cur_block() != NULL) {
2099 add_immBlock_pred(step_block, jmp);
2102 body_block = step_block;
2105 /* create the condition */
2106 set_cur_block(header_block);
2107 if (statement->condition != NULL) {
2108 create_condition_evaluation(statement->condition, body_block,
2111 keep_alive(header_block);
2113 add_immBlock_pred(body_block, jmp);
2116 mature_immBlock(body_block);
2117 mature_immBlock(false_block);
2118 mature_immBlock(step_block);
2119 mature_immBlock(header_block);
2120 mature_immBlock(false_block);
2122 set_cur_block(false_block);
2125 static void create_declaration_entity(declaration_t *declaration,
2126 declaration_type_t declaration_type,
2127 ir_type *parent_type)
2129 ident *id = new_id_from_str(declaration->symbol->string);
2130 ir_type *irtype = get_ir_type(declaration->type);
2131 ir_entity *entity = new_entity(parent_type, id, irtype);
2132 set_entity_ld_ident(entity, id);
2134 declaration->declaration_type = (unsigned char) declaration_type;
2135 declaration->v.entity = entity;
2136 set_entity_variability(entity, variability_uninitialized);
2137 /* TODO: visibility? */
2140 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2142 enum compound_graph_entry_type_t {
2143 COMPOUND_GRAPH_ENTRY_ARRAY,
2144 COMPOUND_GRAPH_ENTRY_COMPOUND
2147 struct compound_graph_path_entry_t {
2153 compound_graph_path_entry_t *prev;
2156 static void create_initializer_object(initializer_t *initializer, type_t *type,
2157 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2159 static compound_graph_path *create_compound_path(ir_type *type,
2160 compound_graph_path_entry_t *entry, int len)
2162 compound_graph_path *path = new_compound_graph_path(type, len);
2165 for( ; entry != NULL; entry = entry->prev, --i) {
2167 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2168 set_compound_graph_path_node(path, i, entry->v.entity);
2170 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2171 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2179 static void create_initializer_value(initializer_value_t *initializer,
2181 compound_graph_path_entry_t *entry,
2184 ir_node *node = expression_to_firm(initializer->value);
2185 ir_type *type = get_entity_type(entity);
2186 compound_graph_path *path = create_compound_path(type, entry, len);
2187 add_compound_ent_value_w_path(entity, node, path);
2190 static void create_initializer_compound(initializer_list_t *initializer,
2191 compound_type_t *type,
2193 compound_graph_path_entry_t *last_entry,
2196 declaration_t *compound_declaration = type->declaration;
2198 declaration_t *compound_entry = compound_declaration->context.declarations;
2200 compound_graph_path_entry_t entry;
2201 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2202 entry.prev = last_entry;
2206 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2207 if(compound_entry->symbol == NULL)
2209 if(compound_entry->namespc != NAMESPACE_NORMAL)
2212 if(i >= initializer->len)
2215 entry.v.entity = compound_entry->v.entity;
2217 initializer_t *sub_initializer = initializer->initializers[i];
2219 assert(compound_entry != NULL);
2220 assert(compound_entry->declaration_type
2221 == DECLARATION_TYPE_COMPOUND_MEMBER);
2223 if(sub_initializer->type == INITIALIZER_VALUE) {
2224 create_initializer_value(&sub_initializer->value,
2225 entity, &entry, len);
2227 type_t *entry_type = skip_typeref(compound_entry->type);
2228 create_initializer_object(sub_initializer, entry_type, entity,
2236 static void create_initializer_array(initializer_list_t *initializer,
2237 array_type_t *type, ir_entity *entity,
2238 compound_graph_path_entry_t *last_entry,
2241 type_t *element_type = type->element_type;
2242 element_type = skip_typeref(element_type);
2244 compound_graph_path_entry_t entry;
2245 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2246 entry.prev = last_entry;
2250 for(i = 0; i < initializer->len; ++i) {
2251 entry.v.array_index = i;
2253 initializer_t *sub_initializer = initializer->initializers[i];
2255 if(sub_initializer->type == INITIALIZER_VALUE) {
2256 create_initializer_value(&sub_initializer->value,
2257 entity, &entry, len);
2259 create_initializer_object(sub_initializer, element_type, entity,
2265 /* TODO: initialize rest... */
2266 if(type->size_expression != NULL) {
2267 size_t array_len = fold_constant(type->size_expression);
2268 for( ; i < array_len; ++i) {
2275 static void create_initializer_string(initializer_string_t *initializer,
2276 array_type_t *type, ir_entity *entity,
2277 compound_graph_path_entry_t *last_entry,
2280 type_t *element_type = type->element_type;
2281 element_type = skip_typeref(element_type);
2283 compound_graph_path_entry_t entry;
2284 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2285 entry.prev = last_entry;
2288 ir_type *irtype = get_entity_type(entity);
2289 size_t arr_len = get_array_type_size(type);
2290 const char *p = initializer->string;
2292 for(i = 0; i < arr_len; ++i, ++p) {
2293 entry.v.array_index = i;
2295 ir_node *node = new_Const_long(mode_Bs, *p);
2296 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2297 add_compound_ent_value_w_path(entity, node, path);
2304 static void create_initializer_object(initializer_t *initializer, type_t *type,
2305 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2307 if(type->type == TYPE_ARRAY) {
2308 array_type_t *array_type = &type->array;
2310 if(initializer->type == INITIALIZER_STRING) {
2311 initializer_string_t *string = &initializer->string;
2312 create_initializer_string(string, array_type, entity, entry, len);
2314 assert(initializer->type == INITIALIZER_LIST);
2315 initializer_list_t *list = &initializer->list;
2316 create_initializer_array(list, array_type, entity, entry, len);
2319 assert(initializer->type == INITIALIZER_LIST);
2320 initializer_list_t *list = &initializer->list;
2322 assert(type->type == TYPE_COMPOUND_STRUCT
2323 || type->type == TYPE_COMPOUND_UNION);
2324 compound_type_t *compound_type = &type->compound;
2325 create_initializer_compound(list, compound_type, entity, entry, len);
2329 static void create_initializer_local_variable_entity(declaration_t *declaration)
2331 initializer_t *initializer = declaration->init.initializer;
2332 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2333 ir_entity *entity = declaration->v.entity;
2334 ir_node *memory = get_store();
2335 ir_node *nomem = new_NoMem();
2336 ir_node *frame = get_irg_frame(current_ir_graph);
2337 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2339 if(is_atomic_entity(entity)) {
2340 assert(initializer->type == INITIALIZER_VALUE);
2341 initializer_value_t *initializer_value = &initializer->value;
2343 ir_node *value = expression_to_firm(initializer_value->value);
2344 ir_node *store = new_d_Store(dbgi, memory, addr, value);
2345 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2346 set_store(store_mem);
2350 /* create a "template" entity which is copied to the entity on the stack */
2351 ident *id = unique_ident("initializer");
2352 ir_type *irtype = get_ir_type(declaration->type);
2353 ir_type *global_type = get_glob_type();
2354 ir_entity *init_entity = new_entity(global_type, id, irtype);
2355 set_entity_ld_ident(init_entity, id);
2357 set_entity_variability(init_entity, variability_initialized);
2358 set_entity_visibility(init_entity, visibility_local);
2360 ir_graph *old_current_ir_graph = current_ir_graph;
2361 current_ir_graph = get_const_code_irg();
2363 type_t *type = skip_typeref(declaration->type);
2364 create_initializer_object(initializer, type, init_entity, NULL, 0);
2366 assert(current_ir_graph == get_const_code_irg());
2367 current_ir_graph = old_current_ir_graph;
2369 ir_node *src_addr = create_symconst(dbgi, init_entity);
2370 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2372 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2373 set_store(copyb_mem);
2376 static void create_initializer(declaration_t *declaration)
2378 initializer_t *initializer = declaration->init.initializer;
2379 if(initializer == NULL)
2382 declaration_type_t declaration_type
2383 = (declaration_type_t) declaration->declaration_type;
2384 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2385 create_initializer_local_variable_entity(declaration);
2389 if(initializer->type == INITIALIZER_VALUE) {
2390 initializer_value_t *initializer_value = &initializer->value;
2392 ir_node *value = expression_to_firm(initializer_value->value);
2394 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2395 set_value(declaration->v.value_number, value);
2397 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2399 ir_entity *entity = declaration->v.entity;
2401 set_entity_variability(entity, variability_initialized);
2402 set_atomic_ent_value(entity, value);
2405 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2406 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2408 ir_entity *entity = declaration->v.entity;
2409 set_entity_variability(entity, variability_initialized);
2411 type_t *type = skip_typeref(declaration->type);
2412 create_initializer_object(initializer, type, entity, NULL, 0);
2416 static void create_local_variable(declaration_t *declaration)
2418 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2420 bool needs_entity = declaration->address_taken;
2421 type_t *type = skip_typeref(declaration->type);
2423 if(type->type == TYPE_ARRAY
2424 || type->type == TYPE_COMPOUND_STRUCT
2425 || type->type == TYPE_COMPOUND_UNION) {
2426 needs_entity = true;
2430 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2431 create_declaration_entity(declaration,
2432 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2435 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2436 declaration->v.value_number = next_value_number_function;
2437 ++next_value_number_function;
2440 create_initializer(declaration);
2443 static void create_local_static_variable(declaration_t *declaration)
2445 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2447 type_t *type = skip_typeref(declaration->type);
2448 ir_type *global_type = get_glob_type();
2449 ident *id = unique_ident(declaration->symbol->string);
2450 ir_type *irtype = get_ir_type(type);
2451 ir_entity *entity = new_entity(global_type, id, irtype);
2452 set_entity_ld_ident(entity, id);
2454 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2455 declaration->v.entity = entity;
2456 set_entity_variability(entity, variability_uninitialized);
2457 set_entity_visibility(entity, visibility_local);
2459 ir_graph *old_current_ir_graph = current_ir_graph;
2460 current_ir_graph = get_const_code_irg();
2462 create_initializer(declaration);
2464 assert(current_ir_graph == get_const_code_irg());
2465 current_ir_graph = old_current_ir_graph;
2468 static void declaration_statement_to_firm(declaration_statement_t *statement)
2470 declaration_t *declaration = statement->declarations_begin;
2471 declaration_t *end = statement->declarations_end->next;
2472 for( ; declaration != end; declaration = declaration->next) {
2473 type_t *type = declaration->type;
2475 switch ((storage_class_tag_t)declaration->storage_class) {
2476 case STORAGE_CLASS_TYPEDEF:
2478 case STORAGE_CLASS_STATIC:
2479 create_local_static_variable(declaration);
2481 case STORAGE_CLASS_ENUM_ENTRY:
2482 panic("enum entry declaration in local block found");
2483 case STORAGE_CLASS_EXTERN:
2484 panic("extern declaration in local block found");
2485 case STORAGE_CLASS_NONE:
2486 case STORAGE_CLASS_AUTO:
2487 case STORAGE_CLASS_REGISTER:
2488 if(type->type == TYPE_FUNCTION) {
2489 panic("nested functions not supported yet");
2491 create_local_variable(declaration);
2494 case STORAGE_CLASS_THREAD:
2495 case STORAGE_CLASS_THREAD_EXTERN:
2496 case STORAGE_CLASS_THREAD_STATIC:
2499 panic("invalid storage class found");
2503 static void create_jump_statement(const statement_t *statement,
2504 ir_node *target_block)
2506 if(get_cur_block() == NULL)
2509 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2510 ir_node *jump = new_d_Jmp(dbgi);
2511 add_immBlock_pred(target_block, jump);
2513 set_cur_block(NULL);
2516 static void switch_statement_to_firm(const switch_statement_t *statement)
2518 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2520 ir_node *expression = expression_to_firm(statement->expression);
2521 ir_node *cond = new_d_Cond(dbgi, expression);
2522 ir_node *break_block = new_immBlock();
2524 set_cur_block(NULL);
2526 ir_node *const old_switch_cond = current_switch_cond;
2527 ir_node *const old_break_label = break_label;
2528 const bool old_saw_default_label = saw_default_label;
2529 current_switch_cond = cond;
2530 break_label = break_block;
2532 statement_to_firm(statement->body);
2534 if(get_cur_block() != NULL) {
2535 ir_node *jmp = new_Jmp();
2536 add_immBlock_pred(break_block, jmp);
2539 if (!saw_default_label) {
2540 set_cur_block(get_nodes_block(cond));
2541 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2542 MAGIC_DEFAULT_PN_NUMBER);
2543 add_immBlock_pred(break_block, proj);
2546 assert(current_switch_cond == cond);
2547 assert(break_label == break_block);
2548 current_switch_cond = old_switch_cond;
2549 break_label = old_break_label;
2550 saw_default_label = old_saw_default_label;
2552 mature_immBlock(break_block);
2553 set_cur_block(break_block);
2556 static long fold_constant(const expression_t *expression)
2558 ir_graph *old_current_ir_graph = current_ir_graph;
2559 current_ir_graph = get_const_code_irg();
2561 ir_node *cnst = expression_to_firm(expression);
2562 if(!is_Const(cnst)) {
2563 panic("couldn't fold constantl");
2565 tarval *tv = get_Const_tarval(cnst);
2566 if(!tarval_is_long(tv)) {
2567 panic("folded constant not an integer");
2570 long res = get_tarval_long(tv);
2572 current_ir_graph = old_current_ir_graph;
2576 static void case_label_to_firm(const case_label_statement_t *statement)
2578 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2580 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2582 /* let's create a node and hope firm constant folding creates a Const
2585 set_cur_block(get_nodes_block(current_switch_cond));
2586 if(statement->expression) {
2587 long pn = fold_constant(statement->expression);
2588 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2589 /* oops someone detected our cheating... */
2590 panic("magic default pn used");
2592 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2594 saw_default_label = true;
2595 proj = new_d_defaultProj(dbgi, current_switch_cond,
2596 MAGIC_DEFAULT_PN_NUMBER);
2599 ir_node *block = new_immBlock();
2600 if (fallthrough != NULL) {
2601 add_immBlock_pred(block, fallthrough);
2603 add_immBlock_pred(block, proj);
2604 mature_immBlock(block);
2606 statement_to_firm(statement->label_statement);
2609 static ir_node *get_label_block(declaration_t *label)
2611 assert(label->namespc == NAMESPACE_LABEL);
2613 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2614 return label->v.block;
2616 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2618 ir_node *old_cur_block = get_cur_block();
2619 ir_node *block = new_immBlock();
2620 set_cur_block(old_cur_block);
2622 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2623 label->v.block = block;
2625 ARR_APP1(ir_node *, imature_blocks, block);
2630 static void label_to_firm(const label_statement_t *statement)
2632 ir_node *block = get_label_block(statement->label);
2634 if(get_cur_block() != NULL) {
2635 ir_node *jmp = new_Jmp();
2636 add_immBlock_pred(block, jmp);
2639 set_cur_block(block);
2642 statement_to_firm(statement->label_statement);
2645 static void goto_to_firm(const goto_statement_t *statement)
2647 if(get_cur_block() == NULL)
2650 ir_node *block = get_label_block(statement->label);
2651 ir_node *jmp = new_Jmp();
2652 add_immBlock_pred(block, jmp);
2654 set_cur_block(NULL);
2657 static void statement_to_firm(statement_t *statement)
2659 switch(statement->type) {
2660 case STATEMENT_INVALID:
2661 panic("invalid statement found");
2662 case STATEMENT_COMPOUND:
2663 compound_statement_to_firm(&statement->compound);
2665 case STATEMENT_RETURN:
2666 return_statement_to_firm(&statement->returns);
2668 case STATEMENT_EXPRESSION:
2669 expression_statement_to_firm(&statement->expression);
2672 if_statement_to_firm(&statement->ifs);
2674 case STATEMENT_WHILE:
2675 while_statement_to_firm(&statement->whiles);
2677 case STATEMENT_DO_WHILE:
2678 do_while_statement_to_firm(&statement->do_while);
2680 case STATEMENT_DECLARATION:
2681 declaration_statement_to_firm(&statement->declaration);
2683 case STATEMENT_BREAK:
2684 create_jump_statement(statement, break_label);
2686 case STATEMENT_CONTINUE:
2687 create_jump_statement(statement, continue_label);
2689 case STATEMENT_SWITCH:
2690 switch_statement_to_firm(&statement->switchs);
2692 case STATEMENT_CASE_LABEL:
2693 case_label_to_firm(&statement->case_label);
2696 for_statement_to_firm(&statement->fors);
2698 case STATEMENT_LABEL:
2699 label_to_firm(&statement->label);
2701 case STATEMENT_GOTO:
2702 goto_to_firm(&statement->gotos);
2707 panic("Statement not implemented\n");
2710 static int count_local_declarations(const declaration_t * decl,
2711 const declaration_t *const end)
2714 for (; decl != end; decl = decl->next) {
2715 const type_t *type = skip_typeref(decl->type);
2716 switch (type->type) {
2720 if (!decl->address_taken)
2730 static int count_decls_in_stmts(const statement_t *stmt)
2733 for (; stmt != NULL; stmt = stmt->base.next) {
2734 switch (stmt->type) {
2735 case STATEMENT_DECLARATION: {
2736 const declaration_statement_t *const decl_stmt =
2737 (const declaration_statement_t*)stmt;
2738 count += count_local_declarations(decl_stmt->declarations_begin,
2739 decl_stmt->declarations_end->next);
2743 case STATEMENT_COMPOUND: {
2744 const compound_statement_t *const comp =
2745 (const compound_statement_t*)stmt;
2746 count += count_decls_in_stmts(comp->statements);
2750 case STATEMENT_IF: {
2751 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2752 count += count_decls_in_stmts(if_stmt->true_statement);
2753 count += count_decls_in_stmts(if_stmt->false_statement);
2757 case STATEMENT_SWITCH: {
2758 const switch_statement_t *const switch_stmt =
2759 (const switch_statement_t*)stmt;
2760 count += count_decls_in_stmts(switch_stmt->body);
2764 case STATEMENT_LABEL: {
2765 const label_statement_t *const label_stmt =
2766 (const label_statement_t*)stmt;
2767 count += count_decls_in_stmts(label_stmt->label_statement);
2771 case STATEMENT_WHILE: {
2772 const while_statement_t *const while_stmt =
2773 (const while_statement_t*)stmt;
2774 count += count_decls_in_stmts(while_stmt->body);
2778 case STATEMENT_DO_WHILE: {
2779 const do_while_statement_t *const do_while_stmt =
2780 (const do_while_statement_t*)stmt;
2781 count += count_decls_in_stmts(do_while_stmt->body);
2785 case STATEMENT_FOR: {
2786 const for_statement_t *const for_stmt =
2787 (const for_statement_t*)stmt;
2788 /* TODO initialisation */
2789 count += count_decls_in_stmts(for_stmt->body);
2794 case STATEMENT_BREAK:
2795 case STATEMENT_CASE_LABEL:
2796 case STATEMENT_CONTINUE:
2797 case STATEMENT_EXPRESSION:
2798 case STATEMENT_GOTO:
2799 case STATEMENT_INVALID:
2800 case STATEMENT_RETURN:
2807 static int get_function_n_local_vars(declaration_t *declaration)
2811 /* count parameters */
2812 count += count_local_declarations(declaration->context.declarations, NULL);
2814 /* count local variables declared in body */
2815 count += count_decls_in_stmts(declaration->init.statement);
2817 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2818 * for expression statements... */
2824 static void initialize_function_parameters(declaration_t *declaration)
2826 ir_graph *irg = current_ir_graph;
2827 ir_node *args = get_irg_args(irg);
2828 ir_node *start_block = get_irg_start_block(irg);
2829 ir_type *function_irtype = get_ir_type(declaration->type);
2832 declaration_t *parameter = declaration->context.declarations;
2833 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2834 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2835 type_t *type = skip_typeref(parameter->type);
2837 bool needs_entity = parameter->address_taken;
2838 if(type->type == TYPE_COMPOUND_STRUCT
2839 || type->type == TYPE_COMPOUND_UNION) {
2840 needs_entity = true;
2844 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2845 ident *id = new_id_from_str(parameter->symbol->string);
2846 set_entity_ident(entity, id);
2848 parameter->declaration_type
2849 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2850 parameter->v.entity = entity;
2854 ir_mode *mode = get_ir_mode(parameter->type);
2856 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2858 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2859 parameter->v.value_number = next_value_number_function;
2860 ++next_value_number_function;
2862 set_value(parameter->v.value_number, proj);
2866 static void create_function(declaration_t *declaration)
2868 ir_entity *function_entity = get_function_entity(declaration);
2870 if(declaration->init.statement == NULL)
2873 current_function_decl = declaration;
2874 current_function_name = NULL;
2876 assert(imature_blocks == NULL);
2877 imature_blocks = NEW_ARR_F(ir_node*, 0);
2879 int n_local_vars = get_function_n_local_vars(declaration);
2880 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
2881 ir_node *first_block = get_cur_block();
2883 next_value_number_function = 0;
2884 initialize_function_parameters(declaration);
2886 statement_to_firm(declaration->init.statement);
2888 ir_node *end_block = get_irg_end_block(irg);
2890 /* do we have a return statement yet? */
2891 if(get_cur_block() != NULL) {
2892 assert(declaration->type->type == TYPE_FUNCTION);
2893 const function_type_t* const func_type = &declaration->type->function;
2894 const type_t *result_type = skip_typeref(func_type->result_type);
2897 if (is_type_atomic(result_type, ATOMIC_TYPE_VOID)) {
2898 ret = new_Return(get_store(), 0, NULL);
2901 if(is_type_scalar(result_type)) {
2902 mode = get_ir_mode(func_type->result_type);
2908 // ยง5.1.2.2.3 main implicitly returns 0
2909 if (strcmp(declaration->symbol->string, "main") == 0) {
2910 in[0] = new_Const(mode, get_mode_null(mode));
2912 in[0] = new_Unknown(mode);
2914 ret = new_Return(get_store(), 1, in);
2916 add_immBlock_pred(end_block, ret);
2919 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2920 mature_immBlock(imature_blocks[i]);
2922 DEL_ARR_F(imature_blocks);
2923 imature_blocks = NULL;
2925 mature_immBlock(first_block);
2926 mature_immBlock(end_block);
2928 irg_finalize_cons(irg);
2930 /* finalize the frame type */
2931 ir_type *frame_type = get_irg_frame_type(irg);
2932 int n = get_compound_n_members(frame_type);
2935 for(int i = 0; i < n; ++i) {
2936 ir_entity *entity = get_compound_member(frame_type, i);
2937 ir_type *entity_type = get_entity_type(entity);
2939 int align = get_type_alignment_bytes(entity_type);
2940 if(align > align_all)
2944 misalign = offset % align;
2946 offset += align - misalign;
2950 set_entity_offset(entity, offset);
2951 offset += get_type_size_bytes(entity_type);
2953 set_type_size_bytes(frame_type, offset);
2954 set_type_alignment_bytes(frame_type, align_all);
2955 set_type_state(frame_type, layout_fixed);
2960 static void create_global_variable(declaration_t *declaration)
2964 switch ((storage_class_tag_t)declaration->storage_class) {
2965 case STORAGE_CLASS_STATIC:
2966 vis = visibility_local;
2969 case STORAGE_CLASS_EXTERN:
2970 vis = visibility_external_allocated;
2973 case STORAGE_CLASS_NONE:
2974 vis = visibility_external_visible;
2977 case STORAGE_CLASS_THREAD:
2978 vis = visibility_external_visible;
2981 case STORAGE_CLASS_THREAD_EXTERN:
2982 vis = visibility_external_allocated;
2985 case STORAGE_CLASS_THREAD_STATIC:
2986 vis = visibility_local;
2990 var_type = get_tls_type();
2994 var_type = get_glob_type();
2998 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3000 set_entity_visibility(declaration->v.entity, vis);
3002 current_ir_graph = get_const_code_irg();
3003 create_initializer(declaration);
3006 case STORAGE_CLASS_TYPEDEF:
3007 case STORAGE_CLASS_AUTO:
3008 case STORAGE_CLASS_REGISTER:
3009 case STORAGE_CLASS_ENUM_ENTRY:
3012 panic("Invalid storage class for global variable");
3015 static void context_to_firm(context_t *context)
3017 /* first pass: create declarations */
3018 declaration_t *declaration = context->declarations;
3019 for( ; declaration != NULL; declaration = declaration->next) {
3020 if(declaration->namespc != NAMESPACE_NORMAL)
3022 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3023 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3025 if(declaration->symbol == NULL)
3028 type_t *type = declaration->type;
3029 if(type->type == TYPE_FUNCTION) {
3030 get_function_entity(declaration);
3032 create_global_variable(declaration);
3036 /* second pass: create code */
3037 declaration = context->declarations;
3038 for( ; declaration != NULL; declaration = declaration->next) {
3039 if(declaration->namespc != NAMESPACE_NORMAL)
3041 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3042 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3044 if(declaration->symbol == NULL)
3047 type_t *type = declaration->type;
3048 if(type->type != TYPE_FUNCTION)
3051 create_function(declaration);
3055 void translation_unit_to_firm(translation_unit_t *unit)
3057 /* just to be sure */
3058 continue_label = NULL;
3060 current_switch_cond = NULL;
3062 context_to_firm(& unit->context);