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;
801 symbol_t *symbol = builtin->symbol;
802 type_t *type = builtin->expression.datatype;
803 type = skip_typeref(type);
806 case T___builtin_alloca: {
807 if(call->arguments == NULL || call->arguments->next != NULL) {
808 panic("invalid number of parameters on __builtin_alloca");
810 expression_t *argument = call->arguments->expression;
811 ir_node *size = expression_to_firm(argument);
813 ir_node *store = get_store();
814 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
816 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
818 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
822 case T___builtin_nan:
823 case T___builtin_nanf:
824 case T___builtin_nand: {
825 /* Ignore string for now... */
826 assert(type->type == TYPE_FUNCTION);
827 ir_mode *mode = get_ir_mode(type->function.result_type);
828 tarval *tv = get_mode_NAN(mode);
829 ir_node *res = new_d_Const(dbgi, mode, tv);
833 panic("Unsupported builtin found\n");
837 static ir_node *call_expression_to_firm(const call_expression_t *call)
839 assert(get_cur_block() != NULL);
841 expression_t *function = call->function;
842 if(function->type == EXPR_BUILTIN_SYMBOL) {
843 return process_builtin_call(call);
845 ir_node *callee = expression_to_firm(function);
847 type_t *type = function->base.datatype;
848 assert(type->type == TYPE_POINTER);
849 pointer_type_t *const ptr_type = &type->pointer;
850 assert(ptr_type->points_to->type == TYPE_FUNCTION);
851 function_type_t *function_type = &ptr_type->points_to->function;
853 int n_parameters = 0;
854 call_argument_t *argument = call->arguments;
855 for( ; argument != NULL; argument = argument->next) {
859 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
861 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
862 ir_type *new_method_type = NULL;
863 if(function_type->variadic || function_type->unspecified_parameters) {
864 /* we need to construct a new method type matching the call
866 int n_res = get_method_n_ress(ir_method_type);
867 new_method_type = new_type_method(unique_ident("calltype"),
868 n_parameters, n_res);
869 set_method_calling_convention(new_method_type,
870 get_method_calling_convention(ir_method_type));
871 set_method_additional_properties(new_method_type,
872 get_method_additional_properties(ir_method_type));
874 for(int i = 0; i < n_res; ++i) {
875 set_method_res_type(new_method_type, i,
876 get_method_res_type(ir_method_type, i));
879 ir_node *in[n_parameters];
881 argument = call->arguments;
883 for( ; argument != NULL; argument = argument->next) {
884 expression_t *expression = argument->expression;
885 ir_node *arg_node = expression_to_firm(expression);
887 arg_node = do_strict_conv(dbgi, arg_node);
890 if(new_method_type != NULL) {
891 ir_type *irtype = get_ir_type(expression->base.datatype);
892 set_method_param_type(new_method_type, n, irtype);
897 assert(n == n_parameters);
899 if(new_method_type != NULL)
900 ir_method_type = new_method_type;
902 ir_node *store = get_store();
903 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
905 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
908 type_t *result_type = skip_typeref(function_type->result_type);
909 ir_node *result = NULL;
911 if(!is_type_atomic(result_type, ATOMIC_TYPE_VOID)) {
913 if(is_type_scalar(result_type)) {
914 mode = get_ir_mode(result_type);
918 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
919 result = new_d_Proj(dbgi, resproj, mode, 0);
925 static void statement_to_firm(statement_t *statement);
926 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
928 static ir_node *expression_to_addr(const expression_t *expression);
929 static void create_condition_evaluation(const expression_t *expression,
931 ir_node *false_block);
933 static void set_value_for_expression(const expression_t *expression,
936 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
937 value = do_strict_conv(dbgi, value);
939 if(expression->type == EXPR_REFERENCE) {
940 reference_expression_t *ref = (reference_expression_t*) expression;
942 declaration_t *declaration = ref->declaration;
943 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
944 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
945 set_value(declaration->v.value_number, value);
950 ir_node *addr = expression_to_addr(expression);
951 ir_node *memory = get_store();
953 type_t *type = skip_typeref(expression->base.datatype);
954 if(is_type_scalar(type)) {
955 ir_node *store = new_d_Store(dbgi, memory, addr, value);
956 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
957 set_store(store_mem);
959 ir_type *irtype = get_ir_type(type);
960 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
961 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
962 set_store(copyb_mem);
966 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
968 ir_mode *value_mode = get_irn_mode(value);
970 if (value_mode == dest_mode || is_Bad(value))
973 if(dest_mode == mode_b) {
974 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
975 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
976 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
980 return new_d_Conv(dbgi, value, dest_mode);
983 static ir_node *create_incdec(const unary_expression_t *expression)
985 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
986 type_t *type = expression->expression.datatype;
987 ir_mode *mode = get_ir_mode(type);
988 expression_t *value = expression->value;
990 ir_node *value_node = expression_to_firm(value);
993 if(type->type == TYPE_POINTER) {
994 pointer_type_t *pointer_type = &type->pointer;
995 unsigned elem_size = get_type_size(pointer_type->points_to);
996 offset = new_Const_long(mode_Is, elem_size);
998 assert(is_type_arithmetic(type));
999 offset = new_Const(mode, get_mode_one(mode));
1002 switch(expression->type) {
1003 case UNEXPR_POSTFIX_INCREMENT: {
1004 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1005 set_value_for_expression(value, new_value);
1008 case UNEXPR_POSTFIX_DECREMENT: {
1009 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1010 set_value_for_expression(value, new_value);
1013 case UNEXPR_PREFIX_INCREMENT: {
1014 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1015 set_value_for_expression(value, new_value);
1018 case UNEXPR_PREFIX_DECREMENT: {
1019 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1020 set_value_for_expression(value, new_value);
1024 panic("no incdec expr in create_incdec");
1029 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1031 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1032 type_t *type = skip_typeref(expression->expression.datatype);
1034 if(expression->type == UNEXPR_TAKE_ADDRESS)
1035 return expression_to_addr(expression->value);
1037 const expression_t *value = expression->value;
1038 ir_node *value_node = expression_to_firm(value);
1040 switch(expression->type) {
1042 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
1045 case UNEXPR_BITWISE_NEGATE:
1046 return new_d_Not(dbgi, value_node, get_ir_mode(type));
1048 if(get_irn_mode(value_node) != mode_b) {
1049 value_node = create_conv(dbgi, value_node, mode_b);
1051 value_node = new_d_Not(dbgi, value_node, mode_b);
1052 ir_mode *const mode = get_ir_mode(type);
1053 if(mode != mode_b) {
1054 value_node = create_conv(dbgi, value_node, mode);
1058 case UNEXPR_DEREFERENCE: {
1059 ir_type *irtype = get_ir_type(type);
1060 return deref_address(irtype, value_node, dbgi);
1062 case UNEXPR_POSTFIX_INCREMENT:
1063 case UNEXPR_POSTFIX_DECREMENT:
1064 case UNEXPR_PREFIX_INCREMENT:
1065 case UNEXPR_PREFIX_DECREMENT:
1066 return create_incdec(expression);
1068 ir_node *node = create_conv(dbgi, value_node, get_ir_mode(type));
1069 node = do_strict_conv(dbgi, node);
1072 case UNEXPR_CAST_IMPLICIT:
1073 return create_conv(dbgi, value_node, get_ir_mode(type));
1075 case UNEXPR_TAKE_ADDRESS:
1076 case UNEXPR_INVALID:
1079 panic("invalid UNEXPR type found");
1082 static long get_pnc(binary_expression_type_t type)
1085 case BINEXPR_EQUAL: return pn_Cmp_Eq;
1086 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
1087 case BINEXPR_LESS: return pn_Cmp_Lt;
1088 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
1089 case BINEXPR_GREATER: return pn_Cmp_Gt;
1090 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1094 panic("trying to get pn_Cmp from non-comparison binexpr type");
1097 static ir_node *create_lazy_op(const binary_expression_t *expression)
1099 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1100 type_t *type = expression->expression.datatype;
1101 ir_mode *mode = get_ir_mode(type);
1103 ir_node *cur_block = get_cur_block();
1105 ir_node *one_block = new_immBlock();
1106 ir_node *one = new_Const(mode, get_mode_one(mode));
1107 ir_node *jmp_one = new_d_Jmp(dbgi);
1109 ir_node *zero_block = new_immBlock();
1110 ir_node *zero = new_Const(mode, get_mode_null(mode));
1111 ir_node *jmp_zero = new_d_Jmp(dbgi);
1113 set_cur_block(cur_block);
1114 create_condition_evaluation((const expression_t*) expression,
1115 one_block, zero_block);
1116 mature_immBlock(one_block);
1117 mature_immBlock(zero_block);
1119 ir_node *common_block = new_immBlock();
1120 add_immBlock_pred(common_block, jmp_one);
1121 add_immBlock_pred(common_block, jmp_zero);
1122 mature_immBlock(common_block);
1124 ir_node *in[2] = { one, zero };
1125 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1130 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1131 ir_node *right, ir_mode *mode);
1133 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1134 create_arithmetic_func func)
1136 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1137 ir_node *left = expression_to_firm(expression->left);
1138 ir_node *right = expression_to_firm(expression->right);
1139 type_t *type = expression->right->base.datatype;
1140 /* be careful with the modes, because in arithmetic assign nodes only
1141 * the right operand has the mode of the arithmetic already */
1142 ir_mode *mode = get_ir_mode(type);
1143 left = create_conv(dbgi, left, mode);
1144 ir_node *res = func(dbgi, left, right, mode);
1149 static ir_node *pointer_arithmetic(ir_node *const pointer,
1152 dbg_info *const dbgi,
1153 const create_arithmetic_func func)
1155 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1156 type_t *const points_to = pointer_type->points_to;
1157 const unsigned elem_size = get_type_size(points_to);
1159 assert(elem_size >= 1);
1160 if (elem_size > 1) {
1161 integer = create_conv(dbgi, integer, mode_Is);
1162 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1163 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1167 ir_mode *const mode = get_ir_mode(type);
1168 return func(dbgi, pointer, integer, mode);
1171 static ir_node *create_arithmetic_assign_binop(
1172 const binary_expression_t *expression, create_arithmetic_func func)
1174 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1175 type_t *const type = expression->expression.datatype;
1178 if (type->type == TYPE_POINTER) {
1179 ir_node *const pointer = expression_to_firm(expression->left);
1180 ir_node * integer = expression_to_firm(expression->right);
1181 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1183 value = create_arithmetic_binop(expression, func);
1186 ir_mode *const mode = get_ir_mode(type);
1187 value = create_conv(dbgi, value, mode);
1188 set_value_for_expression(expression->left, value);
1193 static ir_node *create_add(const binary_expression_t *expression)
1195 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1196 ir_node *left = expression_to_firm(expression->left);
1197 ir_node *right = expression_to_firm(expression->right);
1198 type_t *type = expression->expression.datatype;
1200 expression_t *expr_left = expression->left;
1201 expression_t *expr_right = expression->right;
1202 type_t *type_left = skip_typeref(expr_left->base.datatype);
1203 type_t *type_right = skip_typeref(expr_right->base.datatype);
1205 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1206 ir_mode *const mode = get_ir_mode(type);
1207 return new_d_Add(dbgi, left, right, mode);
1210 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1211 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1213 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1214 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1218 static ir_node *create_sub(const binary_expression_t *expression)
1220 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1221 expression_t *const expr_left = expression->left;
1222 expression_t *const expr_right = expression->right;
1223 ir_node *const left = expression_to_firm(expr_left);
1224 ir_node *const right = expression_to_firm(expr_right);
1225 type_t *const type = expression->expression.datatype;
1226 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1227 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1229 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1230 ir_mode *const mode = get_ir_mode(type);
1231 return new_d_Sub(dbgi, left, right, mode);
1232 } else if (type_left->type == TYPE_POINTER
1233 && type_right->type == TYPE_POINTER) {
1234 const pointer_type_t *const ptr_type = &type_left->pointer;
1235 const unsigned elem_size = get_type_size(ptr_type->points_to);
1236 ir_mode *const mode = get_ir_mode(type);
1237 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1238 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1239 ir_node *const no_mem = new_NoMem();
1240 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1241 op_pin_state_floats);
1242 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1245 assert(type_left->type == TYPE_POINTER);
1246 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1249 static ir_node *create_shift(const binary_expression_t *expression)
1251 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1252 ir_node *left = expression_to_firm(expression->left);
1253 ir_node *right = expression_to_firm(expression->right);
1254 type_t *type = expression->expression.datatype;
1255 ir_mode *mode = get_ir_mode(type);
1257 /* firm always wants the shift count to be unsigned */
1258 right = create_conv(dbgi, right, mode_Iu);
1262 switch(expression->type) {
1263 case BINEXPR_SHIFTLEFT_ASSIGN:
1264 case BINEXPR_SHIFTLEFT:
1265 res = new_d_Shl(dbgi, left, right, mode);
1267 case BINEXPR_SHIFTRIGHT_ASSIGN:
1268 case BINEXPR_SHIFTRIGHT: {
1269 expression_t *expr_left = expression->left;
1270 type_t *type_left = skip_typeref(expr_left->base.datatype);
1272 if(is_type_signed(type_left)) {
1273 res = new_d_Shrs(dbgi, left, right, mode);
1275 res = new_d_Shr(dbgi, left, right, mode);
1280 panic("create shift op called for non-shift op");
1287 static ir_node *create_divmod(const binary_expression_t *expression)
1289 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1290 ir_node *left = expression_to_firm(expression->left);
1291 ir_node *right = expression_to_firm(expression->right);
1292 ir_node *pin = new_Pin(new_NoMem());
1293 /* be careful with the modes, because in arithmetic assign nodes only
1294 * the right operand has the mode of the arithmetic already */
1295 type_t *type = expression->right->base.datatype;
1296 ir_mode *mode = get_ir_mode(type);
1297 left = create_conv(dbgi, left, mode);
1301 switch (expression->type) {
1303 case BINEXPR_DIV_ASSIGN:
1304 if(mode_is_float(mode)) {
1305 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1306 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1308 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1309 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1314 case BINEXPR_MOD_ASSIGN:
1315 assert(!mode_is_float(mode));
1316 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1317 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1320 default: panic("unexpected binary expression type in create_divmod()");
1326 static ir_node *create_arithmetic_assign_divmod(
1327 const binary_expression_t *expression)
1329 ir_node * value = create_divmod(expression);
1330 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1331 type_t *const type = expression->expression.datatype;
1332 ir_mode *const mode = get_ir_mode(type);
1334 assert(type->type != TYPE_POINTER);
1336 value = create_conv(dbgi, value, mode);
1337 set_value_for_expression(expression->left, value);
1342 static ir_node *create_arithmetic_assign_shift(
1343 const binary_expression_t *expression)
1345 ir_node * value = create_shift(expression);
1346 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1347 type_t *const type = expression->expression.datatype;
1348 ir_mode *const mode = get_ir_mode(type);
1350 value = create_conv(dbgi, value, mode);
1351 set_value_for_expression(expression->left, value);
1356 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1358 binary_expression_type_t type = expression->type;
1361 case BINEXPR_NOTEQUAL:
1363 case BINEXPR_LESSEQUAL:
1364 case BINEXPR_GREATER:
1365 case BINEXPR_GREATEREQUAL: {
1366 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1367 ir_node *left = expression_to_firm(expression->left);
1368 ir_node *right = expression_to_firm(expression->right);
1369 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1370 long pnc = get_pnc(type);
1371 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1374 case BINEXPR_ASSIGN: {
1375 ir_node *right = expression_to_firm(expression->right);
1376 set_value_for_expression(expression->left, right);
1381 return create_add(expression);
1383 return create_sub(expression);
1385 return create_arithmetic_binop(expression, new_d_Mul);
1386 case BINEXPR_BITWISE_AND:
1387 return create_arithmetic_binop(expression, new_d_And);
1388 case BINEXPR_BITWISE_OR:
1389 return create_arithmetic_binop(expression, new_d_Or);
1390 case BINEXPR_BITWISE_XOR:
1391 return create_arithmetic_binop(expression, new_d_Eor);
1392 case BINEXPR_SHIFTLEFT:
1393 case BINEXPR_SHIFTRIGHT:
1394 return create_shift(expression);
1397 return create_divmod(expression);
1398 case BINEXPR_LOGICAL_AND:
1399 case BINEXPR_LOGICAL_OR:
1400 return create_lazy_op(expression);
1402 expression_to_firm(expression->left);
1403 return expression_to_firm(expression->right);
1404 case BINEXPR_ADD_ASSIGN:
1405 return create_arithmetic_assign_binop(expression, new_d_Add);
1406 case BINEXPR_SUB_ASSIGN:
1407 return create_arithmetic_assign_binop(expression, new_d_Sub);
1408 case BINEXPR_MUL_ASSIGN:
1409 return create_arithmetic_assign_binop(expression, new_d_Mul);
1410 case BINEXPR_DIV_ASSIGN:
1411 return create_arithmetic_assign_divmod(expression);
1412 case BINEXPR_BITWISE_AND_ASSIGN:
1413 return create_arithmetic_assign_binop(expression, new_d_And);
1414 case BINEXPR_BITWISE_OR_ASSIGN:
1415 return create_arithmetic_assign_binop(expression, new_d_Or);
1416 case BINEXPR_BITWISE_XOR_ASSIGN:
1417 return create_arithmetic_assign_binop(expression, new_d_Eor);
1418 case BINEXPR_SHIFTLEFT_ASSIGN:
1419 case BINEXPR_SHIFTRIGHT_ASSIGN:
1420 return create_arithmetic_assign_shift(expression);
1422 panic("TODO binexpr type");
1426 static ir_node *array_access_addr(const array_access_expression_t *expression)
1428 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1429 ir_node *base_addr = expression_to_firm(expression->array_ref);
1430 ir_node *offset = expression_to_firm(expression->index);
1431 offset = create_conv(dbgi, offset, mode_Iu);
1433 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1434 assert(is_type_pointer(ref_type));
1435 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1437 unsigned elem_size = get_type_size(pointer_type->points_to);
1438 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1439 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1441 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1446 static ir_node *array_access_to_firm(
1447 const array_access_expression_t *expression)
1449 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1450 ir_node *addr = array_access_addr(expression);
1451 type_t *type = revert_automatic_type_conversion(
1452 (const expression_t*) expression);
1453 type = skip_typeref(type);
1454 ir_type *irtype = get_ir_type(type);
1456 return deref_address(irtype, addr, dbgi);
1459 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1461 type_t *type = expression->type;
1463 type = expression->size_expression->base.datatype;
1464 assert(type != NULL);
1467 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1468 unsigned size = get_type_size(type);
1469 ir_node *size_node = new_Const_long(mode, size);
1474 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1476 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1478 ir_node *cur_block = get_cur_block();
1480 /* create the true block */
1481 ir_node *true_block = new_immBlock();
1483 ir_node *true_val = expression_to_firm(expression->true_expression);
1484 ir_node *true_jmp = new_Jmp();
1486 /* create the false block */
1487 ir_node *false_block = new_immBlock();
1489 ir_node *false_val = expression_to_firm(expression->false_expression);
1490 ir_node *false_jmp = new_Jmp();
1492 /* create the condition evaluation */
1493 set_cur_block(cur_block);
1494 create_condition_evaluation(expression->condition, true_block, false_block);
1495 mature_immBlock(true_block);
1496 mature_immBlock(false_block);
1498 /* create the common block */
1499 ir_node *common_block = new_immBlock();
1500 add_immBlock_pred(common_block, true_jmp);
1501 add_immBlock_pred(common_block, false_jmp);
1502 mature_immBlock(common_block);
1504 /* TODO improve static semantics, so either both or no values are NULL */
1505 if (true_val == NULL || false_val == NULL) return NULL;
1507 ir_node *in[2] = { true_val, false_val };
1508 ir_mode *mode = get_irn_mode(true_val);
1509 assert(get_irn_mode(false_val) == mode);
1510 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1515 static ir_node *select_addr(const select_expression_t *expression)
1517 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1519 ir_node *compound_addr = expression_to_firm(expression->compound);
1521 declaration_t *entry = expression->compound_entry;
1522 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1523 ir_entity *entity = entry->v.entity;
1525 assert(entity != NULL);
1527 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1532 static ir_node *select_to_firm(const select_expression_t *expression)
1534 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1535 ir_node *addr = select_addr(expression);
1536 type_t *type = revert_automatic_type_conversion(
1537 (const expression_t*) expression);
1538 type = skip_typeref(type);
1539 ir_type *irtype = get_ir_type(type);
1541 return deref_address(irtype, addr, dbgi);
1544 /* Values returned by __builtin_classify_type. */
1545 typedef enum gcc_type_class
1551 enumeral_type_class,
1554 reference_type_class,
1558 function_type_class,
1569 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1571 const type_t *const type = expr->type_expression->base.datatype;
1577 const atomic_type_t *const atomic_type = &type->atomic;
1578 switch (atomic_type->atype) {
1579 // should not be reached
1580 case ATOMIC_TYPE_INVALID:
1584 // gcc cannot do that
1585 case ATOMIC_TYPE_VOID:
1586 tc = void_type_class;
1589 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1590 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1591 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1592 case ATOMIC_TYPE_SHORT:
1593 case ATOMIC_TYPE_USHORT:
1594 case ATOMIC_TYPE_INT:
1595 case ATOMIC_TYPE_UINT:
1596 case ATOMIC_TYPE_LONG:
1597 case ATOMIC_TYPE_ULONG:
1598 case ATOMIC_TYPE_LONGLONG:
1599 case ATOMIC_TYPE_ULONGLONG:
1600 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1601 tc = integer_type_class;
1604 case ATOMIC_TYPE_FLOAT:
1605 case ATOMIC_TYPE_DOUBLE:
1606 case ATOMIC_TYPE_LONG_DOUBLE:
1607 tc = real_type_class;
1610 #ifdef PROVIDE_COMPLEX
1611 case ATOMIC_TYPE_FLOAT_COMPLEX:
1612 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1613 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1614 tc = complex_type_class;
1616 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1617 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1618 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1619 tc = complex_type_class;
1624 panic("Unimplemented case in classify_type_to_firm().");
1629 case TYPE_ARRAY: // gcc handles this as pointer
1630 case TYPE_FUNCTION: // gcc handles this as pointer
1631 case TYPE_POINTER: tc = pointer_type_class; break;
1632 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1633 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1635 // gcc handles this as integer
1636 case TYPE_ENUM: tc = integer_type_class; break;
1639 panic("Unimplemented case in classify_type_to_firm().");
1642 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1643 ir_mode *const mode = mode_Is;
1644 tarval *const tv = new_tarval_from_long(tc, mode);
1645 return new_d_Const(dbgi, mode, tv);
1648 static ir_node *function_name_to_firm(
1649 const string_literal_expression_t *const expr)
1651 if (current_function_name == NULL) {
1652 const source_position_t *const src_pos =
1653 &expr->expression.source_position;
1654 const char *const name = current_function_decl->symbol->string;
1655 current_function_name = string_to_firm(src_pos, "__func__", name);
1658 return current_function_name;
1661 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1663 statement_t *statement = expr->statement;
1665 assert(statement->type == STATEMENT_COMPOUND);
1666 return compound_statement_to_firm((compound_statement_t*) statement);
1669 static ir_node *dereference_addr(const unary_expression_t *const expression)
1671 assert(expression->type == UNEXPR_DEREFERENCE);
1672 return expression_to_firm(expression->value);
1675 static ir_node *expression_to_addr(const expression_t *expression)
1677 switch(expression->type) {
1678 case EXPR_REFERENCE:
1679 return reference_addr(&expression->reference);
1680 case EXPR_ARRAY_ACCESS:
1681 return array_access_addr(&expression->array_access);
1683 return select_addr(&expression->select);
1685 return call_expression_to_firm(&expression->call);
1687 const unary_expression_t *const unary_expr = &expression->unary;
1688 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1689 return dereference_addr(unary_expr);
1696 panic("trying to get address of non-lvalue");
1699 static ir_node *_expression_to_firm(const expression_t *expression)
1701 switch(expression->type) {
1703 return const_to_firm(&expression->conste);
1704 case EXPR_STRING_LITERAL:
1705 return string_literal_to_firm(&expression->string);
1706 case EXPR_REFERENCE:
1707 return reference_expression_to_firm(&expression->reference);
1709 return call_expression_to_firm(&expression->call);
1711 return unary_expression_to_firm(&expression->unary);
1713 return binary_expression_to_firm(&expression->binary);
1714 case EXPR_ARRAY_ACCESS:
1715 return array_access_to_firm(&expression->array_access);
1717 return sizeof_to_firm(&expression->sizeofe);
1718 case EXPR_CONDITIONAL:
1719 return conditional_to_firm(&expression->conditional);
1721 return select_to_firm(&expression->select);
1722 case EXPR_CLASSIFY_TYPE:
1723 return classify_type_to_firm(&expression->classify_type);
1725 case EXPR_PRETTY_FUNCTION:
1726 return function_name_to_firm(&expression->string);
1727 case EXPR_STATEMENT:
1728 return statement_expression_to_firm(&expression->statement);
1731 case EXPR_BUILTIN_SYMBOL:
1732 panic("unimplemented expression found");
1738 panic("invalid expression found");
1741 static ir_node *expression_to_firm(const expression_t *expression)
1743 ir_node *res = _expression_to_firm(expression);
1745 if(res != NULL && get_irn_mode(res) == mode_b) {
1746 ir_mode *mode = get_ir_mode(expression->base.datatype);
1747 res = create_conv(NULL, res, mode);
1753 static ir_node *expression_to_modeb(const expression_t *expression)
1755 ir_node *res = _expression_to_firm(expression);
1756 res = create_conv(NULL, res, mode_b);
1762 * create a short-circuit expression evaluation that tries to construct
1763 * efficient control flow structures for &&, || and ! expressions
1765 static void create_condition_evaluation(const expression_t *expression,
1766 ir_node *true_block,
1767 ir_node *false_block)
1769 switch(expression->type) {
1771 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1772 if(unary_expression->type == UNEXPR_NOT) {
1773 create_condition_evaluation(unary_expression->value, false_block,
1780 binary_expression_t *binary_expression
1781 = (binary_expression_t*) expression;
1782 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1783 ir_node *cur_block = get_cur_block();
1784 ir_node *extra_block = new_immBlock();
1785 set_cur_block(cur_block);
1786 create_condition_evaluation(binary_expression->left, extra_block,
1788 mature_immBlock(extra_block);
1789 set_cur_block(extra_block);
1790 create_condition_evaluation(binary_expression->right, true_block,
1794 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1795 ir_node *cur_block = get_cur_block();
1796 ir_node *extra_block = new_immBlock();
1797 set_cur_block(cur_block);
1798 create_condition_evaluation(binary_expression->left, true_block,
1800 mature_immBlock(extra_block);
1801 set_cur_block(extra_block);
1802 create_condition_evaluation(binary_expression->right, true_block,
1812 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1813 ir_node *condition = expression_to_modeb(expression);
1814 ir_node *cond = new_d_Cond(dbgi, condition);
1815 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1816 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1818 add_immBlock_pred(true_block, true_proj);
1819 add_immBlock_pred(false_block, false_proj);
1821 set_cur_block(NULL);
1824 static void return_statement_to_firm(return_statement_t *statement)
1826 if(get_cur_block() == NULL)
1829 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1831 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1835 if(get_method_n_ress(func_irtype) > 0) {
1836 ir_type *res_type = get_method_res_type(func_irtype, 0);
1838 if(statement->return_value != NULL) {
1839 ir_node *node = expression_to_firm(statement->return_value);
1840 node = do_strict_conv(dbgi, node);
1844 if(is_compound_type(res_type)) {
1847 mode = get_type_mode(res_type);
1849 in[0] = new_Unknown(mode);
1853 /* build return_value for its side effects */
1854 if(statement->return_value != NULL) {
1855 expression_to_firm(statement->return_value);
1860 ir_node *store = get_store();
1861 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1863 ir_node *end_block = get_irg_end_block(current_ir_graph);
1864 add_immBlock_pred(end_block, ret);
1866 set_cur_block(NULL);
1869 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1871 if(get_cur_block() == NULL)
1874 return expression_to_firm(statement->expression);
1877 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1879 ir_node *result = NULL;
1880 statement_t *statement = compound->statements;
1881 for( ; statement != NULL; statement = statement->base.next) {
1882 //context2firm(&statement->context);
1884 if(statement->base.next == NULL
1885 && statement->type == STATEMENT_EXPRESSION) {
1886 result = expression_statement_to_firm(
1887 (expression_statement_t*) statement);
1890 statement_to_firm(statement);
1896 static void if_statement_to_firm(if_statement_t *statement)
1898 ir_node *cur_block = get_cur_block();
1900 ir_node *fallthrough_block = new_immBlock();
1902 /* the true (blocks) */
1903 ir_node *true_block;
1904 if (statement->true_statement != NULL) {
1905 true_block = new_immBlock();
1906 statement_to_firm(statement->true_statement);
1907 if(get_cur_block() != NULL) {
1908 ir_node *jmp = new_Jmp();
1909 add_immBlock_pred(fallthrough_block, jmp);
1912 true_block = fallthrough_block;
1915 /* the false (blocks) */
1916 ir_node *false_block;
1917 if(statement->false_statement != NULL) {
1918 false_block = new_immBlock();
1920 statement_to_firm(statement->false_statement);
1921 if(get_cur_block() != NULL) {
1922 ir_node *jmp = new_Jmp();
1923 add_immBlock_pred(fallthrough_block, jmp);
1926 false_block = fallthrough_block;
1929 /* create the condition */
1930 if(cur_block != NULL) {
1931 set_cur_block(cur_block);
1932 create_condition_evaluation(statement->condition, true_block,
1936 mature_immBlock(true_block);
1937 if(false_block != fallthrough_block) {
1938 mature_immBlock(false_block);
1940 mature_immBlock(fallthrough_block);
1942 set_cur_block(fallthrough_block);
1945 static void while_statement_to_firm(while_statement_t *statement)
1947 ir_node *jmp = NULL;
1948 if(get_cur_block() != NULL) {
1952 /* create the header block */
1953 ir_node *header_block = new_immBlock();
1955 add_immBlock_pred(header_block, jmp);
1958 /* the false block */
1959 ir_node *false_block = new_immBlock();
1962 ir_node *body_block;
1963 if (statement->body != NULL) {
1964 ir_node *old_continue_label = continue_label;
1965 ir_node *old_break_label = break_label;
1966 continue_label = header_block;
1967 break_label = false_block;
1969 body_block = new_immBlock();
1970 statement_to_firm(statement->body);
1972 assert(continue_label == header_block);
1973 assert(break_label == false_block);
1974 continue_label = old_continue_label;
1975 break_label = old_break_label;
1977 if(get_cur_block() != NULL) {
1979 add_immBlock_pred(header_block, jmp);
1982 body_block = header_block;
1985 /* create the condition */
1986 set_cur_block(header_block);
1988 create_condition_evaluation(statement->condition, body_block, false_block);
1989 mature_immBlock(body_block);
1990 mature_immBlock(false_block);
1991 mature_immBlock(header_block);
1993 set_cur_block(false_block);
1996 static void do_while_statement_to_firm(do_while_statement_t *statement)
1998 ir_node *jmp = NULL;
1999 if(get_cur_block() != NULL) {
2003 /* create the header block */
2004 ir_node *header_block = new_immBlock();
2006 /* the false block */
2007 ir_node *false_block = new_immBlock();
2010 ir_node *body_block = new_immBlock();
2012 add_immBlock_pred(body_block, jmp);
2015 if (statement->body != NULL) {
2016 ir_node *old_continue_label = continue_label;
2017 ir_node *old_break_label = break_label;
2018 continue_label = header_block;
2019 break_label = false_block;
2021 statement_to_firm(statement->body);
2023 assert(continue_label == header_block);
2024 assert(break_label == false_block);
2025 continue_label = old_continue_label;
2026 break_label = old_break_label;
2028 if (get_cur_block() == NULL) {
2029 mature_immBlock(header_block);
2030 mature_immBlock(body_block);
2031 mature_immBlock(false_block);
2036 ir_node *body_jmp = new_Jmp();
2037 add_immBlock_pred(header_block, body_jmp);
2038 mature_immBlock(header_block);
2040 /* create the condition */
2041 set_cur_block(header_block);
2043 create_condition_evaluation(statement->condition, body_block, false_block);
2044 mature_immBlock(body_block);
2045 mature_immBlock(false_block);
2046 mature_immBlock(header_block);
2048 set_cur_block(false_block);
2051 static void for_statement_to_firm(for_statement_t *statement)
2053 ir_node *jmp = NULL;
2054 if (get_cur_block() != NULL) {
2055 if(statement->initialisation != NULL) {
2056 expression_to_firm(statement->initialisation);
2061 /* create the step block */
2062 ir_node *const step_block = new_immBlock();
2063 if (statement->step != NULL) {
2064 expression_to_firm(statement->step);
2066 ir_node *const step_jmp = new_Jmp();
2068 /* create the header block */
2069 ir_node *const header_block = new_immBlock();
2071 add_immBlock_pred(header_block, jmp);
2073 add_immBlock_pred(header_block, step_jmp);
2075 /* the false block */
2076 ir_node *const false_block = new_immBlock();
2079 ir_node * body_block;
2080 if (statement->body != NULL) {
2081 ir_node *const old_continue_label = continue_label;
2082 ir_node *const old_break_label = break_label;
2083 continue_label = step_block;
2084 break_label = false_block;
2086 body_block = new_immBlock();
2087 statement_to_firm(statement->body);
2089 assert(continue_label == step_block);
2090 assert(break_label == false_block);
2091 continue_label = old_continue_label;
2092 break_label = old_break_label;
2094 if (get_cur_block() != NULL) {
2096 add_immBlock_pred(step_block, jmp);
2099 body_block = step_block;
2102 /* create the condition */
2103 set_cur_block(header_block);
2104 if (statement->condition != NULL) {
2105 create_condition_evaluation(statement->condition, body_block,
2108 keep_alive(header_block);
2110 add_immBlock_pred(body_block, jmp);
2113 mature_immBlock(body_block);
2114 mature_immBlock(false_block);
2115 mature_immBlock(step_block);
2116 mature_immBlock(header_block);
2117 mature_immBlock(false_block);
2119 set_cur_block(false_block);
2122 static void create_declaration_entity(declaration_t *declaration,
2123 declaration_type_t declaration_type,
2124 ir_type *parent_type)
2126 ident *id = new_id_from_str(declaration->symbol->string);
2127 ir_type *irtype = get_ir_type(declaration->type);
2128 ir_entity *entity = new_entity(parent_type, id, irtype);
2129 set_entity_ld_ident(entity, id);
2131 declaration->declaration_type = (unsigned char) declaration_type;
2132 declaration->v.entity = entity;
2133 set_entity_variability(entity, variability_uninitialized);
2134 /* TODO: visibility? */
2137 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2139 enum compound_graph_entry_type_t {
2140 COMPOUND_GRAPH_ENTRY_ARRAY,
2141 COMPOUND_GRAPH_ENTRY_COMPOUND
2144 struct compound_graph_path_entry_t {
2150 compound_graph_path_entry_t *prev;
2153 static void create_initializer_object(initializer_t *initializer, type_t *type,
2154 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2156 static compound_graph_path *create_compound_path(ir_type *type,
2157 compound_graph_path_entry_t *entry, int len)
2159 compound_graph_path *path = new_compound_graph_path(type, len);
2162 for( ; entry != NULL; entry = entry->prev, --i) {
2164 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2165 set_compound_graph_path_node(path, i, entry->v.entity);
2167 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2168 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2176 static void create_initializer_value(initializer_value_t *initializer,
2178 compound_graph_path_entry_t *entry,
2181 ir_node *node = expression_to_firm(initializer->value);
2182 ir_type *type = get_entity_type(entity);
2183 compound_graph_path *path = create_compound_path(type, entry, len);
2184 add_compound_ent_value_w_path(entity, node, path);
2187 static void create_initializer_compound(initializer_list_t *initializer,
2188 compound_type_t *type,
2190 compound_graph_path_entry_t *last_entry,
2193 declaration_t *compound_declaration = type->declaration;
2195 declaration_t *compound_entry = compound_declaration->context.declarations;
2197 compound_graph_path_entry_t entry;
2198 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2199 entry.prev = last_entry;
2203 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2204 if(compound_entry->symbol == NULL)
2206 if(compound_entry->namespc != NAMESPACE_NORMAL)
2209 if(i >= initializer->len)
2212 entry.v.entity = compound_entry->v.entity;
2214 initializer_t *sub_initializer = initializer->initializers[i];
2216 assert(compound_entry != NULL);
2217 assert(compound_entry->declaration_type
2218 == DECLARATION_TYPE_COMPOUND_MEMBER);
2220 if(sub_initializer->type == INITIALIZER_VALUE) {
2221 create_initializer_value(&sub_initializer->value,
2222 entity, &entry, len);
2224 type_t *entry_type = skip_typeref(compound_entry->type);
2225 create_initializer_object(sub_initializer, entry_type, entity,
2233 static void create_initializer_array(initializer_list_t *initializer,
2234 array_type_t *type, ir_entity *entity,
2235 compound_graph_path_entry_t *last_entry,
2238 type_t *element_type = type->element_type;
2239 element_type = skip_typeref(element_type);
2241 compound_graph_path_entry_t entry;
2242 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2243 entry.prev = last_entry;
2247 for(i = 0; i < initializer->len; ++i) {
2248 entry.v.array_index = i;
2250 initializer_t *sub_initializer = initializer->initializers[i];
2252 if(sub_initializer->type == INITIALIZER_VALUE) {
2253 create_initializer_value(&sub_initializer->value,
2254 entity, &entry, len);
2256 create_initializer_object(sub_initializer, element_type, entity,
2262 /* TODO: initialize rest... */
2263 if(type->size_expression != NULL) {
2264 size_t array_len = fold_constant(type->size_expression);
2265 for( ; i < array_len; ++i) {
2272 static void create_initializer_string(initializer_string_t *initializer,
2273 array_type_t *type, ir_entity *entity,
2274 compound_graph_path_entry_t *last_entry,
2277 type_t *element_type = type->element_type;
2278 element_type = skip_typeref(element_type);
2280 compound_graph_path_entry_t entry;
2281 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2282 entry.prev = last_entry;
2285 ir_type *irtype = get_entity_type(entity);
2286 size_t arr_len = get_array_type_size(type);
2287 const char *p = initializer->string;
2289 for(i = 0; i < arr_len; ++i, ++p) {
2290 entry.v.array_index = i;
2292 ir_node *node = new_Const_long(mode_Bs, *p);
2293 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2294 add_compound_ent_value_w_path(entity, node, path);
2301 static void create_initializer_object(initializer_t *initializer, type_t *type,
2302 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2304 if(type->type == TYPE_ARRAY) {
2305 array_type_t *array_type = &type->array;
2307 if(initializer->type == INITIALIZER_STRING) {
2308 initializer_string_t *string = &initializer->string;
2309 create_initializer_string(string, array_type, entity, entry, len);
2311 assert(initializer->type == INITIALIZER_LIST);
2312 initializer_list_t *list = &initializer->list;
2313 create_initializer_array(list, array_type, entity, entry, len);
2316 assert(initializer->type == INITIALIZER_LIST);
2317 initializer_list_t *list = &initializer->list;
2319 assert(type->type == TYPE_COMPOUND_STRUCT
2320 || type->type == TYPE_COMPOUND_UNION);
2321 compound_type_t *compound_type = &type->compound;
2322 create_initializer_compound(list, compound_type, entity, entry, len);
2326 static void create_initializer_local_variable_entity(declaration_t *declaration)
2328 initializer_t *initializer = declaration->init.initializer;
2329 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2330 ir_entity *entity = declaration->v.entity;
2331 ir_node *memory = get_store();
2332 ir_node *nomem = new_NoMem();
2333 ir_node *frame = get_irg_frame(current_ir_graph);
2334 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2336 if(is_atomic_entity(entity)) {
2337 assert(initializer->type == INITIALIZER_VALUE);
2338 initializer_value_t *initializer_value = &initializer->value;
2340 ir_node *value = expression_to_firm(initializer_value->value);
2341 ir_node *store = new_d_Store(dbgi, memory, addr, value);
2342 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2343 set_store(store_mem);
2347 /* create a "template" entity which is copied to the entity on the stack */
2348 ident *id = unique_ident("initializer");
2349 ir_type *irtype = get_ir_type(declaration->type);
2350 ir_type *global_type = get_glob_type();
2351 ir_entity *init_entity = new_entity(global_type, id, irtype);
2352 set_entity_ld_ident(init_entity, id);
2354 set_entity_variability(init_entity, variability_initialized);
2355 set_entity_visibility(init_entity, visibility_local);
2357 ir_graph *old_current_ir_graph = current_ir_graph;
2358 current_ir_graph = get_const_code_irg();
2360 type_t *type = skip_typeref(declaration->type);
2361 create_initializer_object(initializer, type, init_entity, NULL, 0);
2363 assert(current_ir_graph == get_const_code_irg());
2364 current_ir_graph = old_current_ir_graph;
2366 ir_node *src_addr = create_symconst(dbgi, init_entity);
2367 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2369 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2370 set_store(copyb_mem);
2373 static void create_initializer(declaration_t *declaration)
2375 initializer_t *initializer = declaration->init.initializer;
2376 if(initializer == NULL)
2379 declaration_type_t declaration_type
2380 = (declaration_type_t) declaration->declaration_type;
2381 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2382 create_initializer_local_variable_entity(declaration);
2386 if(initializer->type == INITIALIZER_VALUE) {
2387 initializer_value_t *initializer_value = &initializer->value;
2389 ir_node *value = expression_to_firm(initializer_value->value);
2391 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2392 set_value(declaration->v.value_number, value);
2394 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2396 ir_entity *entity = declaration->v.entity;
2398 set_entity_variability(entity, variability_initialized);
2399 set_atomic_ent_value(entity, value);
2402 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2403 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2405 ir_entity *entity = declaration->v.entity;
2406 set_entity_variability(entity, variability_initialized);
2408 type_t *type = skip_typeref(declaration->type);
2409 create_initializer_object(initializer, type, entity, NULL, 0);
2413 static void create_local_variable(declaration_t *declaration)
2415 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2417 bool needs_entity = declaration->address_taken;
2418 type_t *type = skip_typeref(declaration->type);
2420 if(type->type == TYPE_ARRAY
2421 || type->type == TYPE_COMPOUND_STRUCT
2422 || type->type == TYPE_COMPOUND_UNION) {
2423 needs_entity = true;
2427 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2428 create_declaration_entity(declaration,
2429 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2432 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2433 declaration->v.value_number = next_value_number_function;
2434 ++next_value_number_function;
2437 create_initializer(declaration);
2440 static void create_local_static_variable(declaration_t *declaration)
2442 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2444 type_t *type = skip_typeref(declaration->type);
2445 ir_type *global_type = get_glob_type();
2446 ident *id = unique_ident(declaration->symbol->string);
2447 ir_type *irtype = get_ir_type(type);
2448 ir_entity *entity = new_entity(global_type, id, irtype);
2449 set_entity_ld_ident(entity, id);
2451 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2452 declaration->v.entity = entity;
2453 set_entity_variability(entity, variability_uninitialized);
2454 set_entity_visibility(entity, visibility_local);
2456 ir_graph *old_current_ir_graph = current_ir_graph;
2457 current_ir_graph = get_const_code_irg();
2459 create_initializer(declaration);
2461 assert(current_ir_graph == get_const_code_irg());
2462 current_ir_graph = old_current_ir_graph;
2465 static void declaration_statement_to_firm(declaration_statement_t *statement)
2467 declaration_t *declaration = statement->declarations_begin;
2468 declaration_t *end = statement->declarations_end->next;
2469 for( ; declaration != end; declaration = declaration->next) {
2470 type_t *type = declaration->type;
2472 switch ((storage_class_tag_t)declaration->storage_class) {
2473 case STORAGE_CLASS_TYPEDEF:
2475 case STORAGE_CLASS_STATIC:
2476 create_local_static_variable(declaration);
2478 case STORAGE_CLASS_ENUM_ENTRY:
2479 panic("enum entry declaration in local block found");
2480 case STORAGE_CLASS_EXTERN:
2481 panic("extern declaration in local block found");
2482 case STORAGE_CLASS_NONE:
2483 case STORAGE_CLASS_AUTO:
2484 case STORAGE_CLASS_REGISTER:
2485 if(type->type == TYPE_FUNCTION) {
2486 panic("nested functions not supported yet");
2488 create_local_variable(declaration);
2491 case STORAGE_CLASS_THREAD:
2492 case STORAGE_CLASS_THREAD_EXTERN:
2493 case STORAGE_CLASS_THREAD_STATIC:
2496 panic("invalid storage class found");
2500 static void create_jump_statement(const statement_t *statement,
2501 ir_node *target_block)
2503 if(get_cur_block() == NULL)
2506 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2507 ir_node *jump = new_d_Jmp(dbgi);
2508 add_immBlock_pred(target_block, jump);
2510 set_cur_block(NULL);
2513 static void switch_statement_to_firm(const switch_statement_t *statement)
2515 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2517 ir_node *expression = expression_to_firm(statement->expression);
2518 ir_node *cond = new_d_Cond(dbgi, expression);
2519 ir_node *break_block = new_immBlock();
2521 set_cur_block(NULL);
2523 ir_node *const old_switch_cond = current_switch_cond;
2524 ir_node *const old_break_label = break_label;
2525 const bool old_saw_default_label = saw_default_label;
2526 current_switch_cond = cond;
2527 break_label = break_block;
2529 statement_to_firm(statement->body);
2531 if(get_cur_block() != NULL) {
2532 ir_node *jmp = new_Jmp();
2533 add_immBlock_pred(break_block, jmp);
2536 if (!saw_default_label) {
2537 set_cur_block(get_nodes_block(cond));
2538 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2539 MAGIC_DEFAULT_PN_NUMBER);
2540 add_immBlock_pred(break_block, proj);
2543 assert(current_switch_cond == cond);
2544 assert(break_label == break_block);
2545 current_switch_cond = old_switch_cond;
2546 break_label = old_break_label;
2547 saw_default_label = old_saw_default_label;
2549 mature_immBlock(break_block);
2550 set_cur_block(break_block);
2553 static long fold_constant(const expression_t *expression)
2555 ir_graph *old_current_ir_graph = current_ir_graph;
2556 current_ir_graph = get_const_code_irg();
2558 ir_node *cnst = expression_to_firm(expression);
2559 if(!is_Const(cnst)) {
2560 panic("couldn't fold constantl");
2562 tarval *tv = get_Const_tarval(cnst);
2563 if(!tarval_is_long(tv)) {
2564 panic("folded constant not an integer");
2567 long res = get_tarval_long(tv);
2569 current_ir_graph = old_current_ir_graph;
2573 static void case_label_to_firm(const case_label_statement_t *statement)
2575 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2577 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2579 /* let's create a node and hope firm constant folding creates a Const
2582 set_cur_block(get_nodes_block(current_switch_cond));
2583 if(statement->expression) {
2584 long pn = fold_constant(statement->expression);
2585 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2586 /* oops someone detected our cheating... */
2587 panic("magic default pn used");
2589 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2591 saw_default_label = true;
2592 proj = new_d_defaultProj(dbgi, current_switch_cond,
2593 MAGIC_DEFAULT_PN_NUMBER);
2596 ir_node *block = new_immBlock();
2597 if (fallthrough != NULL) {
2598 add_immBlock_pred(block, fallthrough);
2600 add_immBlock_pred(block, proj);
2601 mature_immBlock(block);
2603 statement_to_firm(statement->label_statement);
2606 static ir_node *get_label_block(declaration_t *label)
2608 assert(label->namespc == NAMESPACE_LABEL);
2610 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2611 return label->v.block;
2613 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2615 ir_node *old_cur_block = get_cur_block();
2616 ir_node *block = new_immBlock();
2617 set_cur_block(old_cur_block);
2619 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2620 label->v.block = block;
2622 ARR_APP1(ir_node *, imature_blocks, block);
2627 static void label_to_firm(const label_statement_t *statement)
2629 ir_node *block = get_label_block(statement->label);
2631 if(get_cur_block() != NULL) {
2632 ir_node *jmp = new_Jmp();
2633 add_immBlock_pred(block, jmp);
2636 set_cur_block(block);
2639 statement_to_firm(statement->label_statement);
2642 static void goto_to_firm(const goto_statement_t *statement)
2644 if(get_cur_block() == NULL)
2647 ir_node *block = get_label_block(statement->label);
2648 ir_node *jmp = new_Jmp();
2649 add_immBlock_pred(block, jmp);
2651 set_cur_block(NULL);
2654 static void statement_to_firm(statement_t *statement)
2656 switch(statement->type) {
2657 case STATEMENT_INVALID:
2658 panic("invalid statement found");
2659 case STATEMENT_COMPOUND:
2660 compound_statement_to_firm(&statement->compound);
2662 case STATEMENT_RETURN:
2663 return_statement_to_firm(&statement->returns);
2665 case STATEMENT_EXPRESSION:
2666 expression_statement_to_firm(&statement->expression);
2669 if_statement_to_firm(&statement->ifs);
2671 case STATEMENT_WHILE:
2672 while_statement_to_firm(&statement->whiles);
2674 case STATEMENT_DO_WHILE:
2675 do_while_statement_to_firm(&statement->do_while);
2677 case STATEMENT_DECLARATION:
2678 declaration_statement_to_firm(&statement->declaration);
2680 case STATEMENT_BREAK:
2681 create_jump_statement(statement, break_label);
2683 case STATEMENT_CONTINUE:
2684 create_jump_statement(statement, continue_label);
2686 case STATEMENT_SWITCH:
2687 switch_statement_to_firm(&statement->switchs);
2689 case STATEMENT_CASE_LABEL:
2690 case_label_to_firm(&statement->case_label);
2693 for_statement_to_firm(&statement->fors);
2695 case STATEMENT_LABEL:
2696 label_to_firm(&statement->label);
2698 case STATEMENT_GOTO:
2699 goto_to_firm(&statement->gotos);
2704 panic("Statement not implemented\n");
2707 static int count_local_declarations(const declaration_t * decl,
2708 const declaration_t *const end)
2711 for (; decl != end; decl = decl->next) {
2712 const type_t *type = skip_typeref(decl->type);
2713 switch (type->type) {
2717 if (!decl->address_taken)
2727 static int count_decls_in_stmts(const statement_t *stmt)
2730 for (; stmt != NULL; stmt = stmt->base.next) {
2731 switch (stmt->type) {
2732 case STATEMENT_DECLARATION: {
2733 const declaration_statement_t *const decl_stmt =
2734 (const declaration_statement_t*)stmt;
2735 count += count_local_declarations(decl_stmt->declarations_begin,
2736 decl_stmt->declarations_end->next);
2740 case STATEMENT_COMPOUND: {
2741 const compound_statement_t *const comp =
2742 (const compound_statement_t*)stmt;
2743 count += count_decls_in_stmts(comp->statements);
2747 case STATEMENT_IF: {
2748 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2749 count += count_decls_in_stmts(if_stmt->true_statement);
2750 count += count_decls_in_stmts(if_stmt->false_statement);
2754 case STATEMENT_SWITCH: {
2755 const switch_statement_t *const switch_stmt =
2756 (const switch_statement_t*)stmt;
2757 count += count_decls_in_stmts(switch_stmt->body);
2761 case STATEMENT_LABEL: {
2762 const label_statement_t *const label_stmt =
2763 (const label_statement_t*)stmt;
2764 count += count_decls_in_stmts(label_stmt->label_statement);
2768 case STATEMENT_WHILE: {
2769 const while_statement_t *const while_stmt =
2770 (const while_statement_t*)stmt;
2771 count += count_decls_in_stmts(while_stmt->body);
2775 case STATEMENT_DO_WHILE: {
2776 const do_while_statement_t *const do_while_stmt =
2777 (const do_while_statement_t*)stmt;
2778 count += count_decls_in_stmts(do_while_stmt->body);
2782 case STATEMENT_FOR: {
2783 const for_statement_t *const for_stmt =
2784 (const for_statement_t*)stmt;
2785 /* TODO initialisation */
2786 count += count_decls_in_stmts(for_stmt->body);
2791 case STATEMENT_BREAK:
2792 case STATEMENT_CASE_LABEL:
2793 case STATEMENT_CONTINUE:
2794 case STATEMENT_EXPRESSION:
2795 case STATEMENT_GOTO:
2796 case STATEMENT_INVALID:
2797 case STATEMENT_RETURN:
2804 static int get_function_n_local_vars(declaration_t *declaration)
2808 /* count parameters */
2809 count += count_local_declarations(declaration->context.declarations, NULL);
2811 /* count local variables declared in body */
2812 count += count_decls_in_stmts(declaration->init.statement);
2814 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2815 * for expression statements... */
2821 static void initialize_function_parameters(declaration_t *declaration)
2823 ir_graph *irg = current_ir_graph;
2824 ir_node *args = get_irg_args(irg);
2825 ir_node *start_block = get_irg_start_block(irg);
2826 ir_type *function_irtype = get_ir_type(declaration->type);
2829 declaration_t *parameter = declaration->context.declarations;
2830 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2831 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2832 type_t *type = skip_typeref(parameter->type);
2834 bool needs_entity = parameter->address_taken;
2835 if(type->type == TYPE_COMPOUND_STRUCT
2836 || type->type == TYPE_COMPOUND_UNION) {
2837 needs_entity = true;
2841 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2842 ident *id = new_id_from_str(parameter->symbol->string);
2843 set_entity_ident(entity, id);
2845 parameter->declaration_type
2846 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2847 parameter->v.entity = entity;
2851 ir_mode *mode = get_ir_mode(parameter->type);
2853 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2855 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2856 parameter->v.value_number = next_value_number_function;
2857 ++next_value_number_function;
2859 set_value(parameter->v.value_number, proj);
2863 static void create_function(declaration_t *declaration)
2865 ir_entity *function_entity = get_function_entity(declaration);
2867 if(declaration->init.statement == NULL)
2870 current_function_decl = declaration;
2871 current_function_name = NULL;
2873 assert(imature_blocks == NULL);
2874 imature_blocks = NEW_ARR_F(ir_node*, 0);
2876 int n_local_vars = get_function_n_local_vars(declaration);
2877 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
2878 ir_node *first_block = get_cur_block();
2880 next_value_number_function = 0;
2881 initialize_function_parameters(declaration);
2883 statement_to_firm(declaration->init.statement);
2885 ir_node *end_block = get_irg_end_block(irg);
2887 /* do we have a return statement yet? */
2888 if(get_cur_block() != NULL) {
2889 assert(declaration->type->type == TYPE_FUNCTION);
2890 const function_type_t* const func_type = &declaration->type->function;
2891 const type_t *result_type = skip_typeref(func_type->result_type);
2894 if (is_type_atomic(result_type, ATOMIC_TYPE_VOID)) {
2895 ret = new_Return(get_store(), 0, NULL);
2898 if(is_type_scalar(result_type)) {
2899 mode = get_ir_mode(func_type->result_type);
2905 // ยง5.1.2.2.3 main implicitly returns 0
2906 if (strcmp(declaration->symbol->string, "main") == 0) {
2907 in[0] = new_Const(mode, get_mode_null(mode));
2909 in[0] = new_Unknown(mode);
2911 ret = new_Return(get_store(), 1, in);
2913 add_immBlock_pred(end_block, ret);
2916 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2917 mature_immBlock(imature_blocks[i]);
2919 DEL_ARR_F(imature_blocks);
2920 imature_blocks = NULL;
2922 mature_immBlock(first_block);
2923 mature_immBlock(end_block);
2925 irg_finalize_cons(irg);
2927 /* finalize the frame type */
2928 ir_type *frame_type = get_irg_frame_type(irg);
2929 int n = get_compound_n_members(frame_type);
2932 for(int i = 0; i < n; ++i) {
2933 ir_entity *entity = get_compound_member(frame_type, i);
2934 ir_type *entity_type = get_entity_type(entity);
2936 int align = get_type_alignment_bytes(entity_type);
2937 if(align > align_all)
2941 misalign = offset % align;
2943 offset += align - misalign;
2947 set_entity_offset(entity, offset);
2948 offset += get_type_size_bytes(entity_type);
2950 set_type_size_bytes(frame_type, offset);
2951 set_type_alignment_bytes(frame_type, align_all);
2952 set_type_state(frame_type, layout_fixed);
2957 static void create_global_variable(declaration_t *declaration)
2961 switch ((storage_class_tag_t)declaration->storage_class) {
2962 case STORAGE_CLASS_STATIC:
2963 vis = visibility_local;
2966 case STORAGE_CLASS_EXTERN:
2967 vis = visibility_external_allocated;
2970 case STORAGE_CLASS_NONE:
2971 vis = visibility_external_visible;
2974 case STORAGE_CLASS_THREAD:
2975 vis = visibility_external_visible;
2978 case STORAGE_CLASS_THREAD_EXTERN:
2979 vis = visibility_external_allocated;
2982 case STORAGE_CLASS_THREAD_STATIC:
2983 vis = visibility_local;
2987 var_type = get_tls_type();
2991 var_type = get_glob_type();
2995 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2997 set_entity_visibility(declaration->v.entity, vis);
2999 current_ir_graph = get_const_code_irg();
3000 create_initializer(declaration);
3003 case STORAGE_CLASS_TYPEDEF:
3004 case STORAGE_CLASS_AUTO:
3005 case STORAGE_CLASS_REGISTER:
3006 case STORAGE_CLASS_ENUM_ENTRY:
3009 panic("Invalid storage class for global variable");
3012 static void context_to_firm(context_t *context)
3014 /* first pass: create declarations */
3015 declaration_t *declaration = context->declarations;
3016 for( ; declaration != NULL; declaration = declaration->next) {
3017 if(declaration->namespc != NAMESPACE_NORMAL)
3019 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3020 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3022 if(declaration->symbol == NULL)
3025 type_t *type = declaration->type;
3026 if(type->type == TYPE_FUNCTION) {
3027 get_function_entity(declaration);
3029 create_global_variable(declaration);
3033 /* second pass: create code */
3034 declaration = context->declarations;
3035 for( ; declaration != NULL; declaration = declaration->next) {
3036 if(declaration->namespc != NAMESPACE_NORMAL)
3038 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3039 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3041 if(declaration->symbol == NULL)
3044 type_t *type = declaration->type;
3045 if(type->type != TYPE_FUNCTION)
3048 create_function(declaration);
3052 void translation_unit_to_firm(translation_unit_t *unit)
3054 /* just to be sure */
3055 continue_label = NULL;
3057 current_switch_cond = NULL;
3059 context_to_firm(& unit->context);