9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
15 #include "adt/array.h"
20 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
22 static ir_type *ir_type_const_char;
23 static ir_type *ir_type_void;
24 static ir_type *ir_type_int;
25 static ir_type *ir_type_void_ptr;
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 ir_node **imature_blocks;
37 typedef enum declaration_type_t {
38 DECLARATION_TYPE_UNKNOWN,
39 DECLARATION_TYPE_FUNCTION,
40 DECLARATION_TYPE_GLOBAL_VARIABLE,
41 DECLARATION_TYPE_LOCAL_VARIABLE,
42 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
43 DECLARATION_TYPE_COMPOUND_MEMBER,
44 DECLARATION_TYPE_LABEL_BLOCK,
47 static ir_type *get_ir_type(type_t *type);
49 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
53 const declaration_t *declaration = & value_numbers[pos]->declaration;
55 print_warning_prefix(declaration->source_position);
56 fprintf(stderr, "variable '%s' might be used uninitialized\n",
57 declaration->symbol->string);
59 fprintf(stderr, "Some variable might be used uninitialized\n");
60 return new_r_Unknown(irg, mode);
63 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
65 const source_position_t *pos = (const source_position_t*) dbg;
68 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
72 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
74 const source_position_t *pos = (const source_position_t*) dbg;
79 return pos->input_name;
82 void init_ast2firm(void)
84 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
85 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
86 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
88 ir_type_int = get_ir_type(type_int);
89 ir_type_const_char = get_ir_type(type_const_char);
90 ir_type_void = get_ir_type(type_int); /* we don't have a real void
92 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
93 ir_type_void, mode_P_data);
95 type_void->firm_type = ir_type_void;
98 void exit_ast2firm(void)
102 static unsigned unique_id = 0;
104 static ident *unique_ident(const char *tag)
108 snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
110 return new_id_from_str(buf);
113 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
115 switch(atomic_type->atype) {
116 case ATOMIC_TYPE_SCHAR:
117 case ATOMIC_TYPE_CHAR:
119 case ATOMIC_TYPE_UCHAR:
121 case ATOMIC_TYPE_SHORT:
123 case ATOMIC_TYPE_USHORT:
125 case ATOMIC_TYPE_LONG:
126 case ATOMIC_TYPE_INT:
128 case ATOMIC_TYPE_ULONG:
129 case ATOMIC_TYPE_UINT:
131 case ATOMIC_TYPE_LONGLONG:
133 case ATOMIC_TYPE_ULONGLONG:
135 case ATOMIC_TYPE_FLOAT:
137 case ATOMIC_TYPE_DOUBLE:
139 case ATOMIC_TYPE_LONG_DOUBLE:
141 case ATOMIC_TYPE_BOOL:
143 #ifdef PROVIDE_COMPLEX
144 case ATOMIC_TYPE_FLOAT_COMPLEX:
145 case ATOMIC_TYPE_DOUBLE_COMPLEX:
146 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
147 panic("complex lowering not implemented yet");
149 case ATOMIC_TYPE_FLOAT_IMAGINARY:
150 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
151 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
152 panic("imaginary lowering not implemented yet");
155 case ATOMIC_TYPE_VOID:
156 /* firm has no real void... */
158 case ATOMIC_TYPE_INVALID:
161 panic("Encountered unknown atomic type");
165 static unsigned get_type_size(type_t *type);
167 static unsigned get_atomic_type_size(const atomic_type_t *type)
169 switch(type->atype) {
170 case ATOMIC_TYPE_CHAR:
171 case ATOMIC_TYPE_SCHAR:
172 case ATOMIC_TYPE_UCHAR:
175 case ATOMIC_TYPE_SHORT:
176 case ATOMIC_TYPE_USHORT:
179 case ATOMIC_TYPE_BOOL:
180 case ATOMIC_TYPE_INT:
181 case ATOMIC_TYPE_UINT:
182 case ATOMIC_TYPE_LONG:
183 case ATOMIC_TYPE_ULONG:
184 case ATOMIC_TYPE_FLOAT:
187 case ATOMIC_TYPE_LONGLONG:
188 case ATOMIC_TYPE_ULONGLONG:
189 case ATOMIC_TYPE_DOUBLE:
192 case ATOMIC_TYPE_LONG_DOUBLE:
195 case ATOMIC_TYPE_VOID:
198 case ATOMIC_TYPE_INVALID:
201 panic("Trying to determine size of invalid atomic type");
204 static unsigned get_compound_type_size(compound_type_t *type)
206 ir_type *irtype = get_ir_type(&type->type);
207 return get_type_size_bytes(irtype);
210 static unsigned get_array_type_size(array_type_t *type)
212 ir_type *irtype = get_ir_type(&type->type);
213 return get_type_size_bytes(irtype);
216 static unsigned get_type_size(type_t *type)
218 type = skip_typeref(type);
222 return get_atomic_type_size((const atomic_type_t*) type);
224 return get_mode_size_bytes(mode_Is);
225 case TYPE_COMPOUND_UNION:
226 case TYPE_COMPOUND_STRUCT:
227 return get_compound_type_size((compound_type_t*) type);
229 /* just a pointer to the function */
230 return get_mode_size_bytes(mode_P_code);
232 return get_mode_size_bytes(mode_P_data);
234 return get_array_type_size((array_type_t*) type);
241 panic("Trying to determine size of invalid type");
244 static unsigned count_parameters(const function_type_t *function_type)
248 function_parameter_t *parameter = function_type->parameters;
249 for ( ; parameter != NULL; parameter = parameter->next) {
259 static ir_type *create_atomic_type(const atomic_type_t *type)
261 ir_mode *mode = get_atomic_mode(type);
262 ident *id = get_mode_ident(mode);
263 ir_type *irtype = new_type_primitive(id, mode);
268 static ir_type *create_method_type(const function_type_t *function_type)
270 type_t *result_type = function_type->result_type;
272 ident *id = unique_ident("functiontype");
273 int n_parameters = count_parameters(function_type);
274 int n_results = result_type == type_void ? 0 : 1;
275 ir_type *irtype = new_type_method(id, n_parameters, n_results);
277 if(result_type != type_void) {
278 ir_type *restype = get_ir_type(result_type);
279 set_method_res_type(irtype, 0, restype);
282 function_parameter_t *parameter = function_type->parameters;
284 for( ; parameter != NULL; parameter = parameter->next) {
285 ir_type *p_irtype = get_ir_type(parameter->type);
286 set_method_param_type(irtype, n, p_irtype);
290 if(function_type->variadic || function_type->unspecified_parameters) {
291 set_method_variadicity(irtype, variadicity_variadic);
297 static ir_type *create_pointer_type(pointer_type_t *type)
299 type_t *points_to = type->points_to;
300 ir_type *ir_points_to;
301 /* Avoid endless recursion if the points_to type contains this poiner type
302 * again (might be a struct). We therefore first create a void* pointer
303 * and then set the real points_to type
305 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
306 ir_type_void, mode_P_data);
307 type->type.firm_type = ir_type;
309 ir_points_to = get_ir_type(points_to);
310 set_pointer_points_to_type(ir_type, ir_points_to);
315 static ir_type *create_array_type(array_type_t *type)
317 type_t *element_type = type->element_type;
318 ir_type *ir_element_type = get_ir_type(element_type);
322 panic("TODO arraytpye size not implemented yet");
324 ir_type *ir_type = new_type_array(unique_ident("array"), 1, ir_element_type);
325 set_array_bounds_int(ir_type, 0, 0, n_elements);
327 size_t elemsize = get_type_size_bytes(ir_element_type);
328 int align = get_type_alignment_bytes(ir_element_type);
329 if(elemsize % align > 0) {
330 elemsize += align - (elemsize % align);
332 set_type_size_bytes(ir_type, n_elements * elemsize);
333 set_type_alignment_bytes(ir_type, align);
334 set_type_state(ir_type, layout_fixed);
339 #define INVALID_TYPE ((ir_type_ptr)-1)
341 static ir_type *create_struct_type(compound_type_t *type)
343 symbol_t *symbol = type->declaration->symbol;
346 id = unique_ident(symbol->string);
348 id = unique_ident("__anonymous_struct");
350 ir_type *ir_type = new_type_struct(id);
352 type->type.firm_type = ir_type;
356 declaration_t *entry = type->declaration->context.declarations;
357 for( ; entry != NULL; entry = entry->next) {
358 if(entry->namespace != NAMESPACE_NORMAL)
361 ident *ident = new_id_from_str(entry->symbol->string);
362 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
364 int entry_size = get_type_size_bytes(entry_ir_type);
365 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
366 int misalign = offset % entry_alignment;
369 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
370 set_entity_offset(entity, offset);
371 add_struct_member(ir_type, entity);
372 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
373 entry->v.entity = entity;
375 offset += entry_size;
376 if(entry_alignment > align_all) {
377 if(entry_alignment % align_all != 0) {
378 panic("Uneven alignments not supported yet");
380 align_all = entry_alignment;
384 int misalign = offset % align_all;
386 set_type_alignment_bytes(ir_type, align_all);
387 set_type_size_bytes(ir_type, offset);
388 set_type_state(ir_type, layout_fixed);
393 static ir_type *create_union_type(compound_type_t *type)
395 declaration_t *declaration = type->declaration;
396 symbol_t *symbol = declaration->symbol;
399 id = unique_ident(symbol->string);
401 id = unique_ident("__anonymous_union");
403 ir_type *ir_type = new_type_union(id);
405 type->type.firm_type = ir_type;
409 declaration_t *entry = declaration->context.declarations;
410 for( ; entry != NULL; entry = entry->next) {
411 if(entry->namespace != NAMESPACE_NORMAL)
414 ident *ident = new_id_from_str(entry->symbol->string);
415 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
417 int entry_size = get_type_size_bytes(entry_ir_type);
418 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
420 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
421 add_union_member(ir_type, entity);
422 set_entity_offset(entity, 0);
423 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
424 entry->v.entity = entity;
426 if(entry_size > size) {
429 if(entry_alignment > align_all) {
430 if(entry_alignment % align_all != 0) {
431 panic("Uneven alignments not supported yet");
433 align_all = entry_alignment;
437 set_type_alignment_bytes(ir_type, align_all);
438 set_type_size_bytes(ir_type, size);
439 set_type_state(ir_type, layout_fixed);
444 static ir_type *get_ir_type(type_t *type)
446 assert(type != NULL);
448 type = skip_typeref(type);
450 if(type->firm_type != NULL) {
451 assert(type->firm_type != INVALID_TYPE);
452 return type->firm_type;
455 ir_type *firm_type = NULL;
458 firm_type = create_atomic_type((atomic_type_t*) type);
461 firm_type = create_method_type((function_type_t*) type);
464 firm_type = create_pointer_type((pointer_type_t*) type);
467 firm_type = create_array_type((array_type_t*) type);
469 case TYPE_COMPOUND_STRUCT:
470 firm_type = create_struct_type((compound_type_t*) type);
472 case TYPE_COMPOUND_UNION:
473 firm_type = create_union_type((compound_type_t*) type);
476 firm_type = ir_type_int;
484 if(firm_type == NULL)
485 panic("unknown type found");
487 type->firm_type = firm_type;
491 static inline ir_mode *get_ir_mode(type_t *type)
493 ir_type *irtype = get_ir_type(type);
494 ir_mode *mode = get_type_mode(irtype);
495 assert(mode != NULL);
499 static ir_entity* get_function_entity(declaration_t *declaration)
501 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
502 return declaration->v.entity;
503 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
505 symbol_t *symbol = declaration->symbol;
506 ident *id = new_id_from_str(symbol->string);
508 ir_type *global_type = get_glob_type();
509 ir_type *ir_type_method = get_ir_type(declaration->type);
510 assert(is_Method_type(ir_type_method));
512 type_t *type = declaration->type;
513 ir_entity *entity = new_entity(global_type, id, ir_type_method);
514 set_entity_ld_ident(entity, id);
515 if(declaration->storage_class & STORAGE_CLASS_STATIC
516 || type->qualifiers & TYPE_QUALIFIER_INLINE) {
517 set_entity_visibility(entity, visibility_local);
518 } else if(declaration->init.statement != NULL) {
519 set_entity_visibility(entity, visibility_external_visible);
521 set_entity_visibility(entity, visibility_external_allocated);
524 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
525 declaration->v.entity = entity;
532 static ir_node *expression_to_firm(const expression_t *expression);
533 static ir_node *expression_to_modeb(const expression_t *expression);
535 static dbg_info *get_dbg_info(const source_position_t *pos)
537 return (dbg_info*) pos;
540 static ir_node *const_to_firm(const const_t *cnst)
542 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
543 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
546 if(mode_is_float(mode)) {
547 tv = new_tarval_from_double(cnst->v.float_value, mode);
549 tv = new_tarval_from_long(cnst->v.int_value, mode);
552 return new_d_Const(dbgi, mode, tv);
555 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
557 assert(entity != NULL);
558 union symconst_symbol sym;
559 sym.entity_p = entity;
560 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
563 static ir_node *string_literal_to_firm(const string_literal_t* literal)
565 ir_type *global_type = get_glob_type();
566 ir_type *type = new_type_array(unique_ident("strtype"), 1,
569 ident *id = unique_ident("Lstr");
570 ir_entity *entity = new_entity(global_type, id, type);
571 set_entity_ld_ident(entity, id);
572 set_entity_variability(entity, variability_constant);
574 ir_type *elem_type = ir_type_const_char;
575 ir_mode *mode = get_type_mode(elem_type);
577 const char *string = literal->value;
578 size_t slen = strlen(string) + 1;
580 set_array_lower_bound_int(type, 0, 0);
581 set_array_upper_bound_int(type, 0, slen);
582 set_type_size_bytes(type, slen);
583 set_type_state(type, layout_fixed);
585 tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
586 for(size_t i = 0; i < slen; ++i) {
587 tvs[i] = new_tarval_from_long(string[i], mode);
590 set_array_entity_values(entity, tvs, slen);
593 dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
595 return create_symconst(dbgi, entity);
598 static ir_node *load_from_expression_addr(type_t *type, ir_node *addr,
601 ir_mode *mode = get_ir_mode(type);
602 ir_node *memory = get_store();
603 ir_node *load = new_d_Load(dbgi, memory, addr, mode);
604 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
605 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
611 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
613 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
614 declaration_t *declaration = ref->declaration;
615 type_t *type = declaration->type;
616 ir_mode *mode = get_ir_mode(type);
618 switch((declaration_type_t) declaration->declaration_type) {
619 case DECLARATION_TYPE_UNKNOWN:
621 case DECLARATION_TYPE_LOCAL_VARIABLE:
622 return get_value(declaration->v.value_number, mode);
623 case DECLARATION_TYPE_FUNCTION: {
624 return create_symconst(dbgi, declaration->v.entity);
626 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
627 ir_entity *entity = declaration->v.entity;
628 ir_node *symconst = create_symconst(dbgi, entity);
629 return load_from_expression_addr(type, symconst, dbgi);
631 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
632 case DECLARATION_TYPE_COMPOUND_MEMBER:
633 case DECLARATION_TYPE_LABEL_BLOCK:
634 panic("not implemented reference type");
637 panic("reference to declaration with unknown type found");
640 static ir_node *reference_addr(const reference_expression_t *ref)
642 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
643 declaration_t *declaration = ref->declaration;
645 switch((declaration_type_t) declaration->declaration_type) {
646 case DECLARATION_TYPE_UNKNOWN:
648 case DECLARATION_TYPE_LOCAL_VARIABLE:
649 panic("local variable without entity has no address");
650 case DECLARATION_TYPE_FUNCTION: {
651 return create_symconst(dbgi, declaration->v.entity);
653 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
654 ir_entity *entity = declaration->v.entity;
655 ir_node *symconst = create_symconst(dbgi, entity);
658 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
659 case DECLARATION_TYPE_COMPOUND_MEMBER:
660 case DECLARATION_TYPE_LABEL_BLOCK:
661 panic("not implemented reference type");
664 panic("reference to declaration with unknown type found");
667 static ir_node *call_expression_to_firm(const call_expression_t *call)
669 assert(get_cur_block() != NULL);
671 expression_t *function = call->function;
672 ir_node *callee = expression_to_firm(function);
674 assert(function->datatype->type == TYPE_FUNCTION);
675 function_type_t *function_type = (function_type_t*) function->datatype;
677 int n_parameters = 0;
678 call_argument_t *argument = call->arguments;
679 for( ; argument != NULL; argument = argument->next) {
683 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
684 ir_type *new_method_type = NULL;
685 if(function_type->variadic || function_type->unspecified_parameters) {
686 /* we need to construct a new method type matching the call
688 int n_res = get_method_n_ress(ir_method_type);
689 new_method_type = new_type_method(unique_ident("calltype"),
690 n_parameters, n_res);
691 set_method_calling_convention(new_method_type,
692 get_method_calling_convention(ir_method_type));
693 set_method_additional_properties(new_method_type,
694 get_method_additional_properties(ir_method_type));
696 for(int i = 0; i < n_res; ++i) {
697 set_method_res_type(new_method_type, i,
698 get_method_res_type(ir_method_type, i));
701 ir_node *in[n_parameters];
703 argument = call->arguments;
705 for( ; argument != NULL; argument = argument->next) {
706 expression_t *expression = argument->expression;
707 ir_node *arg_node = expression_to_firm(expression);
710 if(new_method_type != NULL) {
711 ir_type *irtype = get_ir_type(expression->datatype);
712 set_method_param_type(new_method_type, n, irtype);
717 assert(n == n_parameters);
719 if(new_method_type != NULL)
720 ir_method_type = new_method_type;
722 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
723 ir_node *store = get_store();
724 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
726 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
729 type_t *result_type = function_type->result_type;
730 ir_node *result = NULL;
731 if(result_type != type_void) {
732 ir_mode *mode = get_ir_mode(result_type);
733 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
734 result = new_d_Proj(dbgi, resproj, mode, 0);
740 static ir_node *expression_to_addr(const expression_t *expression);
742 static void set_value_for_expression(const expression_t *expression,
745 if(expression->type == EXPR_REFERENCE) {
746 reference_expression_t *ref = (reference_expression_t*) expression;
748 declaration_t *declaration = ref->declaration;
749 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
750 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
751 set_value(declaration->v.value_number, value);
756 dbg_info *dbgi = get_dbg_info(&expression->source_position);
757 ir_node *addr = expression_to_addr(expression);
758 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
759 ir_node *memory = get_store();
760 ir_node *store = new_d_Store(dbgi, memory, addr, value);
761 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
762 set_store(store_mem);
765 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
767 ir_mode *value_mode = get_irn_mode(value);
769 if(value_mode == dest_mode)
772 if(dest_mode == mode_b) {
773 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
774 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
775 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
779 return new_d_Conv(dbgi, value, dest_mode);
782 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
784 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
785 type_t *type = expression->expression.datatype;
786 ir_mode *mode = get_ir_mode(type);
788 if(expression->type == UNEXPR_TAKE_ADDRESS)
789 return expression_to_addr(expression->value);
791 const expression_t *value = expression->value;
792 ir_node *value_node = expression_to_firm(value);
794 switch(expression->type) {
796 return new_d_Minus(dbgi, value_node, mode);
799 case UNEXPR_BITWISE_NEGATE:
800 return new_d_Not(dbgi, value_node, mode);
802 if(get_irn_mode(value_node) != mode_b) {
803 value_node = create_conv(dbgi, value_node, mode_b);
805 value_node = new_d_Not(dbgi, value_node, mode_b);
807 value_node = create_conv(dbgi, value_node, mode);
810 case UNEXPR_DEREFERENCE:
811 return load_from_expression_addr(type, value_node, dbgi);
812 case UNEXPR_POSTFIX_INCREMENT: {
813 ir_node *one = new_Const(mode, get_mode_one(mode));
814 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
815 set_value_for_expression(value, new_value);
818 case UNEXPR_POSTFIX_DECREMENT: {
819 ir_node *one = new_Const(mode, get_mode_one(mode));
820 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
821 set_value_for_expression(value, new_value);
824 case UNEXPR_PREFIX_INCREMENT: {
825 ir_node *one = new_Const(mode, get_mode_one(mode));
826 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
827 set_value_for_expression(value, new_value);
830 case UNEXPR_PREFIX_DECREMENT: {
831 ir_node *one = new_Const(mode, get_mode_one(mode));
832 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
833 set_value_for_expression(value, new_value);
837 return create_conv(dbgi, value_node, mode);
839 case UNEXPR_TAKE_ADDRESS:
843 panic("invalid UNEXPR type found");
846 static long get_pnc(binary_expression_type_t type)
849 case BINEXPR_EQUAL: return pn_Cmp_Eq;
850 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
851 case BINEXPR_LESS: return pn_Cmp_Lt;
852 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
853 case BINEXPR_GREATER: return pn_Cmp_Gt;
854 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
858 panic("trying to get pn_Cmp from non-comparison binexpr type");
861 static ir_node *create_lazy_op(const binary_expression_t *expression)
863 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
865 bool is_or = (expression->type == BINEXPR_LOGICAL_OR);
866 assert(is_or || expression->type == BINEXPR_LOGICAL_AND);
868 ir_node *val1 = expression_to_modeb(expression->left);
869 ir_node *cond = new_d_Cond(dbgi, val1);
870 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
871 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
873 ir_node *fallthrough_block = new_immBlock();
876 ir_node *calc_val2_block = new_immBlock();
878 add_immBlock_pred(calc_val2_block, false_proj);
880 add_immBlock_pred(calc_val2_block, true_proj);
883 mature_immBlock(calc_val2_block);
885 ir_node *val2 = expression_to_modeb(expression->right);
886 if(get_cur_block() != NULL) {
887 ir_node *jmp = new_d_Jmp(dbgi);
888 add_immBlock_pred(fallthrough_block, jmp);
894 constb = new_d_Const(dbgi, mode_b, get_tarval_b_true());
895 add_immBlock_pred(fallthrough_block, true_proj);
897 constb = new_d_Const(dbgi, mode_b, get_tarval_b_false());
898 add_immBlock_pred(fallthrough_block, false_proj);
900 mature_immBlock(fallthrough_block);
902 set_cur_block(fallthrough_block);
904 ir_node *in[2] = { val2, constb };
905 ir_node *val = new_d_Phi(dbgi, 2, in, mode_b);
910 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
911 ir_node *right, ir_mode *mode);
913 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
914 create_arithmetic_func func)
916 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
917 ir_node *left = expression_to_firm(expression->left);
918 ir_node *right = expression_to_firm(expression->right);
919 type_t *type = expression->right->datatype;
920 /* be careful with the modes, because in asithmetic assign nodes only
921 * the right operand has the mode of the arithmetic alread */
922 ir_mode *mode = get_ir_mode(type);
923 left = create_conv(dbgi, left, mode);
924 ir_node *res = func(dbgi, left, right, mode);
929 static ir_node *create_arithmetic_assign_binop(
930 const binary_expression_t *expression, create_arithmetic_func func)
932 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
933 ir_node *value = create_arithmetic_binop(expression, func);
934 type_t *type = expression->expression.datatype;
935 ir_mode *mode = get_ir_mode(type);
937 assert(type->type != TYPE_POINTER);
939 value = create_conv(dbgi, value, mode);
940 set_value_for_expression(expression->left, value);
945 static ir_node *create_add(const binary_expression_t *expression)
947 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
948 ir_node *left = expression_to_firm(expression->left);
949 ir_node *right = expression_to_firm(expression->right);
950 type_t *type = expression->expression.datatype;
951 ir_mode *mode = get_ir_mode(type);
953 expression_t *expr_left = expression->left;
954 expression_t *expr_right = expression->right;
955 type_t *type_left = skip_typeref(expr_left->datatype);
956 type_t *type_right = skip_typeref(expr_right->datatype);
958 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
959 return new_d_Add(dbgi, left, right, mode);
964 pointer_type_t *pointer_type;
965 if(type_left->type == TYPE_POINTER) {
968 pointer_type = (pointer_type_t*) type_left;
970 assert(type_right->type == TYPE_POINTER);
973 pointer_type = (pointer_type_t*) type_right;
976 type_t *points_to = pointer_type->points_to;
977 unsigned elem_size = get_type_size(points_to);
979 assert(elem_size >= 1);
981 integer = create_conv(dbgi, integer, mode_Is);
982 ir_node *cnst = new_Const_long(mode_Is, (int) elem_size);
983 ir_node *mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
987 ir_node *res = new_d_Add(dbgi, pointer, integer, mode);
992 static ir_node *create_sub(const binary_expression_t *expression)
994 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
995 ir_node *left = expression_to_firm(expression->left);
996 ir_node *right = expression_to_firm(expression->right);
997 type_t *type = expression->expression.datatype;
998 ir_mode *mode = get_ir_mode(type);
1000 expression_t *expr_left = expression->left;
1001 expression_t *expr_right = expression->right;
1002 type_t *type_left = skip_typeref(expr_left->datatype);
1003 type_t *type_right = skip_typeref(expr_right->datatype);
1005 if((is_type_arithmetic(type_left) && is_type_arithmetic(type_right))
1006 || (type_left->type == TYPE_POINTER
1007 && type_right->type == TYPE_POINTER)) {
1008 return new_d_Sub(dbgi, left, right, mode);
1011 assert(type_right->type == TYPE_POINTER);
1012 ir_node *pointer = left;
1013 ir_node *integer = right;
1014 pointer_type_t *pointer_type = (pointer_type_t*) type_right;
1016 type_t *points_to = pointer_type->points_to;
1017 unsigned elem_size = get_type_size(points_to);
1019 assert(elem_size >= 1);
1021 ir_node *cnst = new_Const_long(mode_Iu, elem_size);
1022 ir_node *mul = new_d_Mul(dbgi, integer, cnst, mode_Iu);
1026 ir_node *res = new_d_Sub(dbgi, pointer, integer, mode);
1031 static ir_node *create_divmod(const binary_expression_t *expression)
1033 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1034 ir_node *left = expression_to_firm(expression->left);
1035 ir_node *right = expression_to_firm(expression->right);
1036 ir_node *pin = new_Pin(new_NoMem());
1037 type_t *type = expression->expression.datatype;
1038 ir_mode *mode = get_ir_mode(type);
1042 if(expression->type == BINEXPR_DIV) {
1043 if(mode_is_float(mode)) {
1044 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1045 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1047 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1048 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1051 assert(expression->type == BINEXPR_MOD);
1052 assert(!mode_is_float(mode));
1053 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1054 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1062 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1064 binary_expression_type_t type = expression->type;
1067 case BINEXPR_NOTEQUAL:
1069 case BINEXPR_LESSEQUAL:
1070 case BINEXPR_GREATER:
1071 case BINEXPR_GREATEREQUAL: {
1072 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1073 ir_node *left = expression_to_firm(expression->left);
1074 ir_node *right = expression_to_firm(expression->right);
1075 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1076 long pnc = get_pnc(type);
1077 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1080 case BINEXPR_ASSIGN: {
1081 ir_node *right = expression_to_firm(expression->right);
1082 set_value_for_expression(expression->left, right);
1086 return create_add(expression);
1088 return create_sub(expression);
1090 return create_arithmetic_binop(expression, new_d_Mul);
1091 case BINEXPR_BITWISE_AND:
1092 return create_arithmetic_binop(expression, new_d_And);
1093 case BINEXPR_BITWISE_OR:
1094 return create_arithmetic_binop(expression, new_d_Or);
1095 case BINEXPR_BITWISE_XOR:
1096 return create_arithmetic_binop(expression, new_d_Eor);
1097 case BINEXPR_SHIFTLEFT:
1098 return create_arithmetic_binop(expression, new_d_Shl);
1099 case BINEXPR_SHIFTRIGHT:
1100 return create_arithmetic_binop(expression, new_d_Shr);
1103 return create_divmod(expression);
1104 case BINEXPR_LOGICAL_AND:
1105 case BINEXPR_LOGICAL_OR:
1106 return create_lazy_op(expression);
1108 expression_to_firm(expression->left);
1109 return expression_to_firm(expression->right);
1110 case BINEXPR_ADD_ASSIGN:
1111 return create_arithmetic_assign_binop(expression, new_d_Add);
1112 case BINEXPR_SUB_ASSIGN:
1113 return create_arithmetic_assign_binop(expression, new_d_Sub);
1114 case BINEXPR_MUL_ASSIGN:
1115 return create_arithmetic_assign_binop(expression, new_d_Mul);
1116 case BINEXPR_BITWISE_AND_ASSIGN:
1117 return create_arithmetic_assign_binop(expression, new_d_And);
1118 case BINEXPR_BITWISE_OR_ASSIGN:
1119 return create_arithmetic_assign_binop(expression, new_d_Or);
1120 case BINEXPR_BITWISE_XOR_ASSIGN:
1121 return create_arithmetic_assign_binop(expression, new_d_Eor);
1122 case BINEXPR_SHIFTLEFT_ASSIGN:
1123 return create_arithmetic_assign_binop(expression, new_d_Shl);
1124 case BINEXPR_SHIFTRIGHT_ASSIGN:
1125 return create_arithmetic_assign_binop(expression, new_d_Shr);
1127 panic("TODO binexpr type");
1131 static ir_node *array_access_addr(const array_access_expression_t *expression)
1133 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1135 ir_node *base_addr = expression_to_firm(expression->array_ref);
1136 ir_node *offset = expression_to_firm(expression->index);
1137 offset = create_conv(dbgi, offset, mode_Iu);
1139 unsigned elem_size = get_type_size(expression->expression.datatype);
1140 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1141 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1143 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1148 static ir_node *array_access_to_firm(
1149 const array_access_expression_t *expression)
1152 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1153 ir_node *addr = array_access_addr(expression);
1154 type_t *type = expression->expression.datatype;
1156 return load_from_expression_addr(type, addr, dbgi);
1159 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1161 type_t *type = expression->type;
1163 type = expression->size_expression->datatype;
1164 assert(type != NULL);
1167 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1168 unsigned size = get_type_size(type);
1169 ir_node *size_node = new_Const_long(mode, size);
1174 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1176 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1178 ir_node *condition = expression_to_modeb(expression->condition);
1179 ir_node *cond = new_d_Cond(dbgi, condition);
1180 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1181 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1183 /* create the true block */
1184 ir_node *true_block = new_immBlock();
1185 add_immBlock_pred(true_block, true_proj);
1186 mature_immBlock(true_block);
1188 ir_node *true_val = expression_to_firm(expression->true_expression);
1189 ir_node *true_jmp = new_Jmp();
1191 /* create the false block */
1192 ir_node *false_block = new_immBlock();
1193 add_immBlock_pred(false_block, false_proj);
1194 mature_immBlock(false_block);
1196 ir_node *false_val = expression_to_firm(expression->false_expression);
1197 ir_node *false_jmp = new_Jmp();
1199 /* create the common block */
1200 ir_node *common_block = new_immBlock();
1201 add_immBlock_pred(common_block, true_jmp);
1202 add_immBlock_pred(common_block, false_jmp);
1203 mature_immBlock(common_block);
1205 ir_node *in[2] = { true_val, false_val };
1206 ir_mode *mode = get_irn_mode(true_val);
1207 assert(get_irn_mode(false_val) == mode);
1208 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1213 static ir_node *expression_to_addr(const expression_t *expression)
1215 switch(expression->type) {
1216 case EXPR_REFERENCE:
1217 return reference_addr((const reference_expression_t*) expression);
1218 case EXPR_ARRAY_ACCESS:
1219 return array_access_addr((const array_access_expression_t*) expression);
1223 panic("trying to get address of non-lvalue");
1226 static ir_node *_expression_to_firm(const expression_t *expression)
1228 switch(expression->type) {
1230 return const_to_firm((const const_t*) expression);
1231 case EXPR_STRING_LITERAL:
1232 return string_literal_to_firm((const string_literal_t*) expression);
1233 case EXPR_REFERENCE:
1234 return reference_expression_to_firm(
1235 (const reference_expression_t*) expression);
1237 return call_expression_to_firm((const call_expression_t*) expression);
1239 return unary_expression_to_firm((const unary_expression_t*) expression);
1241 return binary_expression_to_firm(
1242 (const binary_expression_t*) expression);
1243 case EXPR_ARRAY_ACCESS:
1244 return array_access_to_firm(
1245 (const array_access_expression_t*) expression);
1247 return sizeof_to_firm((const sizeof_expression_t*) expression);
1248 case EXPR_CONDITIONAL:
1249 return conditional_to_firm((const conditional_expression_t*)expression);
1253 panic("unsupported expression found");
1256 static ir_node *expression_to_firm(const expression_t *expression)
1258 ir_node *res = _expression_to_firm(expression);
1260 if(expression->datatype == type_void)
1263 ir_mode *mode = get_ir_mode(expression->datatype);
1264 res = create_conv(NULL, res, mode);
1268 static ir_node *expression_to_modeb(const expression_t *expression)
1270 ir_node *res = _expression_to_firm(expression);
1271 res = create_conv(NULL, res, mode_b);
1276 static void statement_to_firm(statement_t *statement);
1278 static void return_statement_to_firm(return_statement_t *statement)
1280 if(get_cur_block() == NULL)
1283 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1286 if(statement->return_value != NULL) {
1287 ir_node *retval = expression_to_firm(statement->return_value);
1291 ret = new_d_Return(dbgi, get_store(), 1, in);
1293 ret = new_d_Return(dbgi, get_store(), 0, NULL);
1295 ir_node *end_block = get_irg_end_block(current_ir_graph);
1296 add_immBlock_pred(end_block, ret);
1298 set_cur_block(NULL);
1301 static void compound_statement_to_firm(compound_statement_t *compound)
1303 statement_t *statement = compound->statements;
1304 for( ; statement != NULL; statement = statement->next) {
1305 //context2firm(&statement->context);
1306 statement_to_firm(statement);
1310 static void expression_statement_to_firm(expression_statement_t *statement)
1312 if(get_cur_block() == NULL)
1315 expression_to_firm(statement->expression);
1318 static void if_statement_to_firm(if_statement_t *statement)
1320 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1321 ir_node *condition = expression_to_modeb(statement->condition);
1323 /* make sure we have a mode_b condition */
1324 ir_node *cond = new_d_Cond(dbgi, condition);
1325 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1326 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1328 ir_node *fallthrough_block = new_immBlock();
1330 /* the true (blocks) */
1331 ir_node *true_block = new_immBlock();
1332 add_immBlock_pred(true_block, true_proj);
1333 mature_immBlock(true_block);
1335 statement_to_firm(statement->true_statement);
1336 if(get_cur_block() != NULL) {
1337 ir_node *jmp = new_Jmp();
1338 add_immBlock_pred(fallthrough_block, jmp);
1341 /* the false (blocks) */
1342 if(statement->false_statement != NULL) {
1343 ir_node *false_block = new_immBlock();
1344 add_immBlock_pred(false_block, false_proj);
1345 mature_immBlock(false_block);
1347 statement_to_firm(statement->false_statement);
1348 if(get_cur_block() != NULL) {
1349 ir_node *jmp = new_Jmp();
1350 add_immBlock_pred(fallthrough_block, jmp);
1353 add_immBlock_pred(fallthrough_block, false_proj);
1355 mature_immBlock(fallthrough_block);
1357 set_cur_block(fallthrough_block);
1360 static void while_statement_to_firm(while_statement_t *statement)
1362 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1364 ir_node *jmp = NULL;
1365 if(get_cur_block() != NULL) {
1369 /* create the header block */
1370 ir_node *header_block = new_immBlock();
1372 add_immBlock_pred(header_block, jmp);
1375 /* create the condition */
1376 ir_node *condition = expression_to_modeb(statement->condition);
1377 ir_node *cond = new_d_Cond(dbgi, condition);
1378 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1379 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1381 /* the false block */
1382 ir_node *false_block = new_immBlock();
1383 add_immBlock_pred(false_block, false_proj);
1386 ir_node *body_block = new_immBlock();
1387 add_immBlock_pred(body_block, true_proj);
1388 mature_immBlock(body_block);
1390 ir_node *old_continue_label = continue_label;
1391 ir_node *old_break_label = break_label;
1392 continue_label = header_block;
1393 break_label = false_block;
1395 statement_to_firm(statement->body);
1397 assert(continue_label == header_block);
1398 assert(break_label == false_block);
1399 continue_label = old_continue_label;
1400 break_label = old_break_label;
1402 if(get_cur_block() != NULL) {
1403 ir_node *jmp = new_Jmp();
1404 add_immBlock_pred(header_block, jmp);
1407 mature_immBlock(header_block);
1408 mature_immBlock(false_block);
1410 set_cur_block(false_block);
1413 static void do_while_statement_to_firm(do_while_statement_t *statement)
1415 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1417 ir_node *jmp = NULL;
1418 if(get_cur_block() != NULL) {
1422 /* create the header block */
1423 ir_node *header_block = new_immBlock();
1425 /* the false block */
1426 ir_node *false_block = new_immBlock();
1429 ir_node *body_block = new_immBlock();
1431 add_immBlock_pred(body_block, jmp);
1434 ir_node *old_continue_label = continue_label;
1435 ir_node *old_break_label = break_label;
1436 continue_label = header_block;
1437 break_label = false_block;
1439 statement_to_firm(statement->body);
1441 assert(continue_label == header_block);
1442 assert(break_label == false_block);
1443 continue_label = old_continue_label;
1444 break_label = old_break_label;
1446 if(get_cur_block() == NULL) {
1447 mature_immBlock(header_block);
1448 mature_immBlock(body_block);
1452 ir_node *body_jmp = new_Jmp();
1453 add_immBlock_pred(header_block, body_jmp);
1454 mature_immBlock(header_block);
1456 /* create the condition */
1457 ir_node *condition = expression_to_modeb(statement->condition);
1458 ir_node *cond = new_d_Cond(dbgi, condition);
1459 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1460 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1462 add_immBlock_pred(body_block, true_proj);
1463 mature_immBlock(body_block);
1465 add_immBlock_pred(false_block, false_proj);
1466 mature_immBlock(false_block);
1468 set_cur_block(false_block);
1471 static void for_statement_to_firm(for_statement_t *statement)
1473 dbg_info *const dbgi = get_dbg_info(&statement->statement.source_position);
1475 ir_node *jmp = NULL;
1476 if (get_cur_block() != NULL) {
1477 if(statement->initialisation != NULL) {
1478 expression_to_firm(statement->initialisation);
1483 /* create the step block */
1484 ir_node *const step_block = new_immBlock();
1485 if (statement->step != NULL) {
1486 expression_to_firm(statement->step);
1488 ir_node *const step_jmp = new_Jmp();
1490 /* create the header block */
1491 ir_node *const header_block = new_immBlock();
1493 add_immBlock_pred(header_block, jmp);
1495 add_immBlock_pred(header_block, step_jmp);
1497 /* create the condition */
1499 ir_node *false_proj;
1500 if (statement->condition != NULL) {
1501 ir_node *const condition = expression_to_modeb(statement->condition);
1502 ir_node *const cond = new_d_Cond(dbgi, condition);
1503 true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1504 false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1506 keep_alive(header_block);
1507 true_proj = new_Jmp();
1511 /* the false block */
1512 ir_node *const false_block = new_immBlock();
1513 if (false_proj != NULL) {
1514 add_immBlock_pred(false_block, false_proj);
1518 ir_node *const body_block = new_immBlock();
1519 add_immBlock_pred(body_block, true_proj);
1520 mature_immBlock(body_block);
1522 ir_node *const old_continue_label = continue_label;
1523 ir_node *const old_break_label = break_label;
1524 continue_label = step_block;
1525 break_label = false_block;
1527 statement_to_firm(statement->body);
1529 assert(continue_label == step_block);
1530 assert(break_label == false_block);
1531 continue_label = old_continue_label;
1532 break_label = old_break_label;
1534 if (get_cur_block() != NULL) {
1535 ir_node *const jmp = new_Jmp();
1536 add_immBlock_pred(step_block, jmp);
1539 mature_immBlock(step_block);
1540 mature_immBlock(header_block);
1541 mature_immBlock(false_block);
1543 set_cur_block(false_block);
1546 static void create_declaration_entity(declaration_t *declaration,
1547 declaration_type_t declaration_type,
1548 ir_type *parent_type)
1550 ident *id = new_id_from_str(declaration->symbol->string);
1551 ir_type *irtype = get_ir_type(declaration->type);
1552 ir_entity *entity = new_entity(parent_type, id, irtype);
1553 set_entity_ld_ident(entity, id);
1555 declaration->declaration_type = declaration_type;
1556 declaration->v.entity = entity;
1557 set_entity_variability(entity, variability_uninitialized);
1558 /* TODO: visibility? */
1561 static void create_initializer(declaration_t *declaration)
1563 initializer_t *initializer = declaration->init.initializer;
1564 if(initializer == NULL)
1567 if(initializer->type == INITIALIZER_VALUE) {
1568 assert(initializer->designator == NULL);
1569 assert(initializer->next == NULL);
1570 ir_node *init_node = expression_to_firm(initializer->v.value);
1572 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1573 set_value(declaration->v.value_number, init_node);
1575 ir_entity *entity = declaration->v.entity;
1577 set_entity_variability(entity, variability_initialized);
1578 set_atomic_ent_value(entity, init_node);
1581 assert(initializer->type == INITIALIZER_LIST);
1582 panic("list initializer not supported yet");
1586 static void create_local_variable(declaration_t *declaration)
1588 bool needs_entity = declaration->address_taken;
1590 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
1593 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
1594 create_declaration_entity(declaration,
1595 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
1598 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1599 declaration->v.value_number = next_value_number_function;
1600 ++next_value_number_function;
1603 create_initializer(declaration);
1606 static void declaration_statement_to_firm(declaration_statement_t *statement)
1608 declaration_t *declaration = statement->declarations_begin;
1609 declaration_t *end = statement->declarations_end->next;
1610 for( ; declaration != end; declaration = declaration->next) {
1611 type_t *type = declaration->type;
1613 switch(declaration->storage_class) {
1614 case STORAGE_CLASS_TYPEDEF:
1616 case STORAGE_CLASS_STATIC:
1617 panic("static local vars not implemented yet");
1618 case STORAGE_CLASS_ENUM_ENTRY:
1619 panic("enum entry declaration in local block found");
1620 case STORAGE_CLASS_EXTERN:
1621 panic("extern declaration in local block found");
1622 case STORAGE_CLASS_NONE:
1623 case STORAGE_CLASS_AUTO:
1624 case STORAGE_CLASS_REGISTER:
1625 if(type->type == TYPE_FUNCTION) {
1626 panic("nested functions not supported yet");
1628 create_local_variable(declaration);
1632 panic("invalid storage class found");
1636 static void create_jump_statement(const statement_t *statement,
1637 ir_node *target_block)
1639 dbg_info *dbgi = get_dbg_info(&statement->source_position);
1640 ir_node *jump = new_d_Jmp(dbgi);
1641 add_immBlock_pred(target_block, jump);
1643 set_cur_block(NULL);
1646 static void switch_statement_to_firm(const switch_statement_t *statement)
1648 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1650 ir_node *expression = expression_to_firm(statement->expression);
1651 ir_node *cond = new_d_Cond(dbgi, expression);
1652 ir_node *break_block = new_immBlock();
1654 set_cur_block(NULL);
1656 ir_node *old_switch_cond = current_switch_cond;
1657 ir_node *old_break_label = break_label;
1658 current_switch_cond = cond;
1659 break_label = break_block;
1661 statement_to_firm(statement->body);
1663 if(get_cur_block() != NULL) {
1664 ir_node *jmp = new_Jmp();
1665 add_immBlock_pred(break_block, jmp);
1668 assert(current_switch_cond == cond);
1669 assert(break_label == break_block);
1670 current_switch_cond = old_switch_cond;
1671 break_label = old_break_label;
1673 mature_immBlock(break_block);
1674 set_cur_block(break_block);
1677 static void case_label_to_firm(const case_label_statement_t *statement)
1679 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1681 /* let's create a node and hope firm constant folding creates a Const
1684 set_cur_block(get_nodes_block(current_switch_cond));
1685 if(statement->expression) {
1686 ir_node *cnst = expression_to_firm(statement->expression);
1687 if(!is_Const(cnst)) {
1688 panic("couldn't fold constant for case label");
1690 tarval *tv = get_Const_tarval(cnst);
1691 if(!mode_is_int(get_tarval_mode(tv))) {
1692 panic("case label not an integer");
1695 long pn = get_tarval_long(tv);
1696 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
1697 /* oops someone detected our cheating... */
1698 panic("magic default pn used");
1700 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
1702 proj = new_d_defaultProj(dbgi, current_switch_cond,
1703 MAGIC_DEFAULT_PN_NUMBER);
1706 ir_node *block = new_immBlock();
1707 add_immBlock_pred(block, proj);
1708 mature_immBlock(block);
1711 static ir_node *get_label_block(declaration_t *label)
1713 assert(label->namespace == NAMESPACE_LABEL);
1715 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
1716 return label->v.block;
1718 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
1720 ir_node *old_cur_block = get_cur_block();
1721 ir_node *block = new_immBlock();
1722 set_cur_block(old_cur_block);
1724 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
1725 label->v.block = block;
1727 ARR_APP1(imature_blocks, block);
1732 static void label_to_firm(const label_statement_t *statement)
1734 ir_node *block = get_label_block(statement->label);
1736 if(get_cur_block() != NULL) {
1737 ir_node *jmp = new_Jmp();
1738 add_immBlock_pred(block, jmp);
1741 set_cur_block(block);
1744 statement_to_firm(statement->label_statement);
1747 static void goto_to_firm(const goto_statement_t *statement)
1749 if(get_cur_block() == NULL)
1752 ir_node *block = get_label_block(statement->label);
1753 ir_node *jmp = new_Jmp();
1754 add_immBlock_pred(block, jmp);
1756 set_cur_block(NULL);
1759 static void statement_to_firm(statement_t *statement)
1761 switch(statement->type) {
1762 case STATEMENT_COMPOUND:
1763 compound_statement_to_firm((compound_statement_t*) statement);
1765 case STATEMENT_RETURN:
1766 return_statement_to_firm((return_statement_t*) statement);
1768 case STATEMENT_EXPRESSION:
1769 expression_statement_to_firm((expression_statement_t*) statement);
1772 if_statement_to_firm((if_statement_t*) statement);
1774 case STATEMENT_WHILE:
1775 while_statement_to_firm((while_statement_t*) statement);
1777 case STATEMENT_DO_WHILE:
1778 do_while_statement_to_firm((do_while_statement_t*) statement);
1780 case STATEMENT_DECLARATION:
1781 declaration_statement_to_firm((declaration_statement_t*) statement);
1783 case STATEMENT_BREAK:
1784 create_jump_statement(statement, break_label);
1786 case STATEMENT_CONTINUE:
1787 create_jump_statement(statement, continue_label);
1789 case STATEMENT_SWITCH:
1790 switch_statement_to_firm((switch_statement_t*) statement);
1792 case STATEMENT_CASE_LABEL:
1793 case_label_to_firm((case_label_statement_t*) statement);
1796 for_statement_to_firm((for_statement_t*) statement);
1798 case STATEMENT_LABEL:
1799 label_to_firm((label_statement_t*) statement);
1801 case STATEMENT_GOTO:
1802 goto_to_firm((goto_statement_t*) statement);
1807 panic("Statement not implemented\n");
1810 static int get_function_n_local_vars(declaration_t *declaration)
1817 static void initialize_function_parameters(declaration_t *declaration)
1819 ir_graph *irg = current_ir_graph;
1820 ir_node *args = get_irg_args(irg);
1821 ir_node *start_block = get_irg_start_block(irg);
1824 declaration_t *parameter = declaration->context.declarations;
1825 for( ; parameter != NULL; parameter = parameter->next) {
1826 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
1828 if(parameter->address_taken) {
1829 panic("address take from parameter not implemented yet");
1832 ir_mode *mode = get_ir_mode(parameter->type);
1834 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
1837 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1838 parameter->v.value_number = next_value_number_function;
1839 ++next_value_number_function;
1841 set_value(parameter->v.value_number, proj);
1845 static void create_function(declaration_t *declaration)
1847 ir_entity *entity = get_function_entity(declaration);
1849 if(declaration->init.statement == NULL)
1852 assert(imature_blocks == NULL);
1853 imature_blocks = NEW_ARR_F(ir_node*, 0);
1855 int n_local_vars = get_function_n_local_vars(declaration);
1856 ir_graph *irg = new_ir_graph(entity, n_local_vars);
1857 ir_node *first_block = get_cur_block();
1859 next_value_number_function = 0;
1860 initialize_function_parameters(declaration);
1862 statement_to_firm(declaration->init.statement);
1864 ir_node *end_block = get_irg_end_block(irg);
1866 /* do we have a return statement yet? */
1867 if(get_cur_block() != NULL) {
1868 assert(declaration->type->type == TYPE_FUNCTION);
1869 const function_type_t* const func_type
1870 = (const function_type_t*) declaration->type;
1872 if (func_type->result_type == type_void) {
1873 ret = new_Return(get_store(), 0, NULL);
1875 ir_mode *const mode = get_ir_mode(func_type->result_type);
1877 // ยง5.1.2.2.3 main implicitly returns 0
1878 if (strcmp(declaration->symbol->string, "main") == 0) {
1879 in[0] = new_Const(mode, get_mode_null(mode));
1881 in[0] = new_Unknown(mode);
1883 ret = new_Return(get_store(), 1, in);
1885 add_immBlock_pred(end_block, ret);
1888 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
1889 mature_immBlock(imature_blocks[i]);
1891 DEL_ARR_F(imature_blocks);
1892 imature_blocks = NULL;
1894 mature_immBlock(first_block);
1895 mature_immBlock(end_block);
1897 irg_finalize_cons(irg);
1899 /* finalize the frame type */
1900 ir_type *frame_type = get_irg_frame_type(irg);
1901 int n = get_compound_n_members(frame_type);
1904 for(int i = 0; i < n; ++i) {
1905 ir_entity *entity = get_compound_member(frame_type, i);
1906 ir_type *entity_type = get_entity_type(entity);
1908 int align = get_type_alignment_bytes(entity_type);
1909 if(align > align_all)
1913 misalign = offset % align;
1917 set_entity_offset(entity, offset);
1918 offset += get_type_size_bytes(entity_type);
1920 set_type_size_bytes(frame_type, offset);
1921 set_type_alignment_bytes(frame_type, align_all);
1922 set_type_state(frame_type, layout_fixed);
1927 static void create_global_variable(declaration_t *declaration)
1929 ir_type *global_type = get_glob_type();
1930 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
1933 ir_entity *entity = declaration->v.entity;
1934 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
1935 set_entity_visibility(entity, visibility_local);
1936 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
1937 set_entity_visibility(entity, visibility_external_allocated);
1939 set_entity_visibility(entity, visibility_external_visible);
1941 current_ir_graph = get_const_code_irg();
1942 create_initializer(declaration);
1945 static void context_to_firm(context_t *context)
1947 declaration_t *declaration = context->declarations;
1948 for( ; declaration != NULL; declaration = declaration->next) {
1949 if(declaration->namespace != NAMESPACE_NORMAL)
1951 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
1952 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
1954 if(declaration->symbol == NULL)
1957 type_t *type = declaration->type;
1958 if(type->type == TYPE_FUNCTION) {
1959 create_function(declaration);
1961 create_global_variable(declaration);
1966 void translation_unit_to_firm(translation_unit_t *unit)
1968 /* remove me later TODO FIXME */
1969 (void) get_type_size;
1971 /* just to be sure */
1972 continue_label = NULL;
1974 current_switch_cond = NULL;
1976 context_to_firm(& unit->context);