9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
19 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
21 static ir_type *ir_type_const_char;
22 static ir_type *ir_type_void;
23 static ir_type *ir_type_int;
24 static ir_type *ir_type_void_ptr;
26 static type_t *type_const_char;
27 static type_t *type_void;
28 static type_t *type_int;
30 static int next_value_number_function;
31 static ir_node *continue_label;
32 static ir_node *break_label;
33 static ir_node *current_switch_cond;
35 typedef enum declaration_type_t {
36 DECLARATION_TYPE_UNKNOWN,
37 DECLARATION_TYPE_FUNCTION,
38 DECLARATION_TYPE_GLOBAL_VARIABLE,
39 DECLARATION_TYPE_LOCAL_VARIABLE,
40 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
41 DECLARATION_TYPE_COMPOUND_MEMBER
44 static ir_type *get_ir_type(type_t *type);
46 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
50 const declaration_t *declaration = & value_numbers[pos]->declaration;
52 print_warning_prefix(declaration->source_position);
53 fprintf(stderr, "variable '%s' might be used uninitialized\n",
54 declaration->symbol->string);
56 fprintf(stderr, "Some variable might be used uninitialized\n");
57 return new_r_Unknown(irg, mode);
60 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
62 const source_position_t *pos = (const source_position_t*) dbg;
65 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
69 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
71 const source_position_t *pos = (const source_position_t*) dbg;
76 return pos->input_name;
79 void init_ast2firm(void)
81 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
82 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
83 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
85 ir_type_int = get_ir_type(type_int);
86 ir_type_const_char = get_ir_type(type_const_char);
87 ir_type_void = get_ir_type(type_int); /* we don't have a real void
89 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
90 ir_type_void, mode_P_data);
92 type_void->firm_type = ir_type_void;
95 void exit_ast2firm(void)
99 static unsigned unique_id = 0;
101 static ident *unique_ident(const char *tag)
105 snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
107 return new_id_from_str(buf);
110 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
112 switch(atomic_type->atype) {
113 case ATOMIC_TYPE_SCHAR:
114 case ATOMIC_TYPE_CHAR:
116 case ATOMIC_TYPE_UCHAR:
118 case ATOMIC_TYPE_SHORT:
120 case ATOMIC_TYPE_USHORT:
122 case ATOMIC_TYPE_LONG:
123 case ATOMIC_TYPE_INT:
125 case ATOMIC_TYPE_ULONG:
126 case ATOMIC_TYPE_UINT:
128 case ATOMIC_TYPE_LONGLONG:
130 case ATOMIC_TYPE_ULONGLONG:
132 case ATOMIC_TYPE_FLOAT:
134 case ATOMIC_TYPE_DOUBLE:
136 case ATOMIC_TYPE_LONG_DOUBLE:
138 case ATOMIC_TYPE_BOOL:
140 #ifdef PROVIDE_COMPLEX
141 case ATOMIC_TYPE_FLOAT_COMPLEX:
142 case ATOMIC_TYPE_DOUBLE_COMPLEX:
143 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
144 panic("complex lowering not implemented yet");
146 case ATOMIC_TYPE_FLOAT_IMAGINARY:
147 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
148 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
149 panic("imaginary lowering not implemented yet");
152 case ATOMIC_TYPE_VOID:
153 /* firm has no real void... */
155 case ATOMIC_TYPE_INVALID:
158 panic("Encountered unknown atomic type");
162 static unsigned get_type_size(type_t *type);
164 static unsigned get_atomic_type_size(const atomic_type_t *type)
166 switch(type->atype) {
167 case ATOMIC_TYPE_CHAR:
168 case ATOMIC_TYPE_SCHAR:
169 case ATOMIC_TYPE_UCHAR:
172 case ATOMIC_TYPE_SHORT:
173 case ATOMIC_TYPE_USHORT:
176 case ATOMIC_TYPE_BOOL:
177 case ATOMIC_TYPE_INT:
178 case ATOMIC_TYPE_UINT:
179 case ATOMIC_TYPE_LONG:
180 case ATOMIC_TYPE_ULONG:
181 case ATOMIC_TYPE_FLOAT:
184 case ATOMIC_TYPE_LONGLONG:
185 case ATOMIC_TYPE_ULONGLONG:
186 case ATOMIC_TYPE_DOUBLE:
189 case ATOMIC_TYPE_LONG_DOUBLE:
192 case ATOMIC_TYPE_VOID:
195 case ATOMIC_TYPE_INVALID:
198 panic("Trying to determine size of invalid atomic type");
201 static unsigned get_compound_type_size(compound_type_t *type)
203 ir_type *irtype = get_ir_type(&type->type);
204 return get_type_size_bytes(irtype);
207 static unsigned get_array_type_size(array_type_t *type)
209 ir_type *irtype = get_ir_type(&type->type);
210 return get_type_size_bytes(irtype);
213 static unsigned get_type_size(type_t *type)
215 type = skip_typeref(type);
219 return get_atomic_type_size((const atomic_type_t*) type);
221 return get_mode_size_bytes(mode_Is);
222 case TYPE_COMPOUND_UNION:
223 case TYPE_COMPOUND_STRUCT:
224 return get_compound_type_size((compound_type_t*) type);
226 /* just a pointer to the function */
227 return get_mode_size_bytes(mode_P_code);
229 return get_mode_size_bytes(mode_P_data);
231 return get_array_type_size((array_type_t*) type);
238 panic("Trying to determine size of invalid type");
241 static unsigned count_parameters(const function_type_t *function_type)
245 function_parameter_t *parameter = function_type->parameters;
246 for ( ; parameter != NULL; parameter = parameter->next) {
256 static ir_type *create_atomic_type(const atomic_type_t *type)
258 ir_mode *mode = get_atomic_mode(type);
259 ident *id = get_mode_ident(mode);
260 ir_type *irtype = new_type_primitive(id, mode);
265 static ir_type *create_method_type(const function_type_t *function_type)
267 type_t *result_type = function_type->result_type;
269 ident *id = unique_ident("functiontype");
270 int n_parameters = count_parameters(function_type);
271 int n_results = result_type == type_void ? 0 : 1;
272 ir_type *irtype = new_type_method(id, n_parameters, n_results);
274 if(result_type != type_void) {
275 ir_type *restype = get_ir_type(result_type);
276 set_method_res_type(irtype, 0, restype);
279 function_parameter_t *parameter = function_type->parameters;
281 for( ; parameter != NULL; parameter = parameter->next) {
282 ir_type *p_irtype = get_ir_type(parameter->type);
283 set_method_param_type(irtype, n, p_irtype);
287 if(function_type->variadic || function_type->unspecified_parameters) {
288 set_method_variadicity(irtype, variadicity_variadic);
294 static ir_type *create_pointer_type(pointer_type_t *type)
296 type_t *points_to = type->points_to;
297 ir_type *ir_points_to;
298 /* Avoid endless recursion if the points_to type contains this poiner type
299 * again (might be a struct). We therefore first create a void* pointer
300 * and then set the real points_to type
302 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
303 ir_type_void, mode_P_data);
304 type->type.firm_type = ir_type;
306 ir_points_to = get_ir_type(points_to);
307 set_pointer_points_to_type(ir_type, ir_points_to);
312 static ir_type *create_array_type(array_type_t *type)
314 type_t *element_type = type->element_type;
315 ir_type *ir_element_type = get_ir_type(element_type);
319 panic("TODO arraytpye size not implemented yet");
321 ir_type *ir_type = new_type_array(unique_ident("array"), 1, ir_element_type);
322 set_array_bounds_int(ir_type, 0, 0, n_elements);
324 size_t elemsize = get_type_size_bytes(ir_element_type);
325 int align = get_type_alignment_bytes(ir_element_type);
326 if(elemsize % align > 0) {
327 elemsize += align - (elemsize % align);
329 set_type_size_bytes(ir_type, n_elements * elemsize);
330 set_type_alignment_bytes(ir_type, align);
331 set_type_state(ir_type, layout_fixed);
336 #define INVALID_TYPE ((ir_type_ptr)-1)
338 static ir_type *create_struct_type(compound_type_t *type)
340 symbol_t *symbol = type->declaration->symbol;
343 id = unique_ident(symbol->string);
345 id = unique_ident("__anonymous_struct");
347 ir_type *ir_type = new_type_struct(id);
349 type->type.firm_type = ir_type;
353 declaration_t *entry = type->declaration->context.declarations;
354 for( ; entry != NULL; entry = entry->next) {
355 if(entry->namespace != NAMESPACE_NORMAL)
358 ident *ident = new_id_from_str(entry->symbol->string);
359 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
361 int entry_size = get_type_size_bytes(entry_ir_type);
362 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
363 int misalign = offset % entry_alignment;
366 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
367 set_entity_offset(entity, offset);
368 add_struct_member(ir_type, entity);
369 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
370 entry->v.entity = entity;
372 offset += entry_size;
373 if(entry_alignment > align_all) {
374 if(entry_alignment % align_all != 0) {
375 panic("Uneven alignments not supported yet");
377 align_all = entry_alignment;
381 int misalign = offset % align_all;
383 set_type_alignment_bytes(ir_type, align_all);
384 set_type_size_bytes(ir_type, offset);
385 set_type_state(ir_type, layout_fixed);
390 static ir_type *create_union_type(compound_type_t *type)
392 declaration_t *declaration = type->declaration;
393 symbol_t *symbol = declaration->symbol;
396 id = unique_ident(symbol->string);
398 id = unique_ident("__anonymous_union");
400 ir_type *ir_type = new_type_union(id);
402 type->type.firm_type = ir_type;
406 declaration_t *entry = declaration->context.declarations;
407 for( ; entry != NULL; entry = entry->next) {
408 if(entry->namespace != NAMESPACE_NORMAL)
411 ident *ident = new_id_from_str(entry->symbol->string);
412 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
414 int entry_size = get_type_size_bytes(entry_ir_type);
415 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
417 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
418 add_union_member(ir_type, entity);
419 set_entity_offset(entity, 0);
420 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
421 entry->v.entity = entity;
423 if(entry_size > size) {
426 if(entry_alignment > align_all) {
427 if(entry_alignment % align_all != 0) {
428 panic("Uneven alignments not supported yet");
430 align_all = entry_alignment;
434 set_type_alignment_bytes(ir_type, align_all);
435 set_type_size_bytes(ir_type, size);
436 set_type_state(ir_type, layout_fixed);
441 static ir_type *get_ir_type(type_t *type)
443 assert(type != NULL);
445 type = skip_typeref(type);
447 if(type->firm_type != NULL) {
448 assert(type->firm_type != INVALID_TYPE);
449 return type->firm_type;
452 ir_type *firm_type = NULL;
455 firm_type = create_atomic_type((atomic_type_t*) type);
458 firm_type = create_method_type((function_type_t*) type);
461 firm_type = create_pointer_type((pointer_type_t*) type);
464 firm_type = create_array_type((array_type_t*) type);
466 case TYPE_COMPOUND_STRUCT:
467 firm_type = create_struct_type((compound_type_t*) type);
469 case TYPE_COMPOUND_UNION:
470 firm_type = create_union_type((compound_type_t*) type);
473 firm_type = ir_type_int;
481 if(firm_type == NULL)
482 panic("unknown type found");
484 type->firm_type = firm_type;
488 static inline ir_mode *get_ir_mode(type_t *type)
490 ir_type *irtype = get_ir_type(type);
491 ir_mode *mode = get_type_mode(irtype);
492 assert(mode != NULL);
496 static ir_entity* get_function_entity(declaration_t *declaration)
498 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
499 return declaration->v.entity;
500 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
502 symbol_t *symbol = declaration->symbol;
503 ident *id = new_id_from_str(symbol->string);
505 ir_type *global_type = get_glob_type();
506 ir_type *ir_type_method = get_ir_type(declaration->type);
507 assert(is_Method_type(ir_type_method));
509 type_t *type = declaration->type;
510 ir_entity *entity = new_entity(global_type, id, ir_type_method);
511 set_entity_ld_ident(entity, id);
512 if(declaration->storage_class & STORAGE_CLASS_STATIC
513 || type->qualifiers & TYPE_QUALIFIER_INLINE) {
514 set_entity_visibility(entity, visibility_local);
515 } else if(declaration->init.statement != NULL) {
516 set_entity_visibility(entity, visibility_external_visible);
518 set_entity_visibility(entity, visibility_external_allocated);
521 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
522 declaration->v.entity = entity;
529 static ir_node *expression_to_firm(const expression_t *expression);
530 static ir_node *_expression_to_firm(const expression_t *expression);
532 static dbg_info *get_dbg_info(const source_position_t *pos)
534 return (dbg_info*) pos;
537 static ir_node *const_to_firm(const const_t *cnst)
539 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
540 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
543 if(mode_is_float(mode)) {
544 tv = new_tarval_from_double(cnst->v.float_value, mode);
546 tv = new_tarval_from_long(cnst->v.int_value, mode);
549 return new_d_Const(dbgi, mode, tv);
552 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
554 assert(entity != NULL);
555 union symconst_symbol sym;
556 sym.entity_p = entity;
557 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
560 static ir_node *string_literal_to_firm(const string_literal_t* literal)
562 ir_type *global_type = get_glob_type();
563 ir_type *type = new_type_array(unique_ident("strtype"), 1,
566 ident *id = unique_ident("Lstr");
567 ir_entity *entity = new_entity(global_type, id, type);
568 set_entity_ld_ident(entity, id);
569 set_entity_variability(entity, variability_constant);
571 ir_type *elem_type = ir_type_const_char;
572 ir_mode *mode = get_type_mode(elem_type);
574 const char *string = literal->value;
575 size_t slen = strlen(string) + 1;
577 set_array_lower_bound_int(type, 0, 0);
578 set_array_upper_bound_int(type, 0, slen);
579 set_type_size_bytes(type, slen);
580 set_type_state(type, layout_fixed);
582 tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
583 for(size_t i = 0; i < slen; ++i) {
584 tvs[i] = new_tarval_from_long(string[i], mode);
587 set_array_entity_values(entity, tvs, slen);
590 dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
592 return create_symconst(dbgi, entity);
595 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
597 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
598 declaration_t *declaration = ref->declaration;
599 type_t *type = declaration->type;
600 ir_mode *mode = get_ir_mode(type);
602 switch((declaration_type_t) declaration->declaration_type) {
603 case DECLARATION_TYPE_UNKNOWN:
605 case DECLARATION_TYPE_LOCAL_VARIABLE:
606 return get_value(declaration->v.value_number, mode);
607 case DECLARATION_TYPE_FUNCTION: {
608 return create_symconst(dbgi, declaration->v.entity);
610 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
611 case DECLARATION_TYPE_GLOBAL_VARIABLE:
612 case DECLARATION_TYPE_COMPOUND_MEMBER:
613 panic("not implemented reference type");
616 panic("reference to declaration with unknown type found");
619 static ir_node *call_expression_to_firm(const call_expression_t *call)
621 assert(get_cur_block() != NULL);
623 expression_t *function = call->function;
624 ir_node *callee = expression_to_firm(function);
626 assert(function->datatype->type == TYPE_FUNCTION);
627 function_type_t *function_type = (function_type_t*) function->datatype;
629 int n_parameters = 0;
630 call_argument_t *argument = call->arguments;
631 for( ; argument != NULL; argument = argument->next) {
635 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
636 ir_type *new_method_type = NULL;
637 if(function_type->variadic || function_type->unspecified_parameters) {
638 /* we need to construct a new method type matching the call
640 int n_res = get_method_n_ress(ir_method_type);
641 new_method_type = new_type_method(unique_ident("calltype"),
642 n_parameters, n_res);
643 set_method_calling_convention(new_method_type,
644 get_method_calling_convention(ir_method_type));
645 set_method_additional_properties(new_method_type,
646 get_method_additional_properties(ir_method_type));
648 for(int i = 0; i < n_res; ++i) {
649 set_method_res_type(new_method_type, i,
650 get_method_res_type(ir_method_type, i));
653 ir_node *in[n_parameters];
655 argument = call->arguments;
657 for( ; argument != NULL; argument = argument->next) {
658 expression_t *expression = argument->expression;
659 ir_node *arg_node = expression_to_firm(expression);
662 if(new_method_type != NULL) {
663 ir_type *irtype = get_ir_type(expression->datatype);
664 set_method_param_type(new_method_type, n, irtype);
669 assert(n == n_parameters);
671 if(new_method_type != NULL)
672 ir_method_type = new_method_type;
674 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
675 ir_node *store = get_store();
676 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
678 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
681 type_t *result_type = function_type->result_type;
682 ir_node *result = NULL;
683 if(result_type != type_void) {
684 ir_mode *mode = get_ir_mode(result_type);
685 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
686 result = new_d_Proj(dbgi, resproj, mode, 0);
692 static ir_node *load_from_expression_addr(type_t *type, ir_node *addr,
695 ir_mode *mode = get_ir_mode(type);
696 ir_node *memory = get_store();
697 ir_node *load = new_d_Load(dbgi, memory, addr, mode);
698 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
699 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
705 static ir_node *expression_to_addr(const expression_t *expression);
707 static void set_value_for_expression(const expression_t *expression,
710 if(expression->type == EXPR_REFERENCE) {
711 reference_expression_t *ref = (reference_expression_t*) expression;
713 declaration_t *declaration = ref->declaration;
714 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
715 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
716 set_value(declaration->v.value_number, value);
721 dbg_info *dbgi = get_dbg_info(&expression->source_position);
722 ir_node *addr = expression_to_addr(expression);
723 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
724 ir_node *memory = get_store();
725 ir_node *store = new_d_Store(dbgi, memory, addr, value);
726 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
727 set_store(store_mem);
730 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
732 ir_mode *value_mode = get_irn_mode(value);
734 if(value_mode == dest_mode)
737 if(dest_mode == mode_b) {
738 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
739 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
740 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
744 return new_d_Conv(dbgi, value, dest_mode);
747 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
749 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
750 type_t *type = expression->expression.datatype;
751 ir_mode *mode = get_ir_mode(type);
753 if(expression->type == UNEXPR_TAKE_ADDRESS)
754 return expression_to_addr(expression->value);
756 const expression_t *value = expression->value;
757 ir_node *value_node = expression_to_firm(value);
759 switch(expression->type) {
761 return new_d_Minus(dbgi, value_node, mode);
764 case UNEXPR_BITWISE_NEGATE:
765 return new_d_Not(dbgi, value_node, mode);
767 if(get_irn_mode(value_node) != mode_b) {
768 value_node = create_conv(dbgi, value_node, mode_b);
770 value_node = new_d_Not(dbgi, value_node, mode_b);
772 value_node = create_conv(dbgi, value_node, mode);
775 case UNEXPR_DEREFERENCE:
776 return load_from_expression_addr(type, value_node, dbgi);
777 case UNEXPR_POSTFIX_INCREMENT: {
778 ir_node *one = new_Const(mode, get_mode_one(mode));
779 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
780 set_value_for_expression(value, new_value);
783 case UNEXPR_POSTFIX_DECREMENT: {
784 ir_node *one = new_Const(mode, get_mode_one(mode));
785 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
786 set_value_for_expression(value, new_value);
789 case UNEXPR_PREFIX_INCREMENT: {
790 ir_node *one = new_Const(mode, get_mode_one(mode));
791 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
792 set_value_for_expression(value, new_value);
795 case UNEXPR_PREFIX_DECREMENT: {
796 ir_node *one = new_Const(mode, get_mode_one(mode));
797 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
798 set_value_for_expression(value, new_value);
802 return create_conv(dbgi, value_node, mode);
804 case UNEXPR_TAKE_ADDRESS:
808 panic("invalid UNEXPR type found");
811 static long get_pnc(binary_expression_type_t type)
814 case BINEXPR_EQUAL: return pn_Cmp_Eq;
815 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
816 case BINEXPR_LESS: return pn_Cmp_Lt;
817 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
818 case BINEXPR_GREATER: return pn_Cmp_Gt;
819 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
823 panic("trying to get pn_Cmp from non-comparison binexpr type");
826 static ir_node *create_lazy_op(const binary_expression_t *expression)
828 bool is_or = (expression->type == BINEXPR_LOGICAL_OR);
829 assert(is_or || expression->type == BINEXPR_LOGICAL_AND);
831 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
832 ir_node *val1 = expression_to_firm(expression->left);
833 val1 = create_conv(dbgi, val1, mode_b);
835 ir_node *cond = new_d_Cond(dbgi, val1);
836 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
837 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
839 ir_node *fallthrough_block = new_immBlock();
842 ir_node *calc_val2_block = new_immBlock();
844 add_immBlock_pred(calc_val2_block, false_proj);
846 add_immBlock_pred(calc_val2_block, true_proj);
849 mature_immBlock(calc_val2_block);
851 ir_node *val2 = expression_to_firm(expression->right);
852 val2 = create_conv(dbgi, val2, mode_b);
853 if(get_cur_block() != NULL) {
854 ir_node *jmp = new_d_Jmp(dbgi);
855 add_immBlock_pred(fallthrough_block, jmp);
861 constb = new_d_Const(dbgi, mode_b, get_tarval_b_true());
862 add_immBlock_pred(fallthrough_block, true_proj);
864 constb = new_d_Const(dbgi, mode_b, get_tarval_b_false());
865 add_immBlock_pred(fallthrough_block, false_proj);
867 mature_immBlock(fallthrough_block);
869 set_cur_block(fallthrough_block);
871 ir_node *in[2] = { val2, constb };
872 ir_node *val = new_d_Phi(dbgi, 2, in, mode_b);
877 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
878 ir_node *right, ir_mode *mode);
880 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
881 create_arithmetic_func func)
883 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
884 ir_node *left = expression_to_firm(expression->left);
885 ir_node *right = expression_to_firm(expression->right);
886 type_t *type = expression->expression.datatype;
887 ir_mode *mode = get_ir_mode(type);
888 ir_node *res = func(dbgi, left, right, mode);
893 static ir_node *create_add(const binary_expression_t *expression)
895 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
896 ir_node *left = expression_to_firm(expression->left);
897 ir_node *right = expression_to_firm(expression->right);
898 type_t *type = expression->expression.datatype;
899 ir_mode *mode = get_ir_mode(type);
901 expression_t *expr_left = expression->left;
902 expression_t *expr_right = expression->right;
903 type_t *type_left = skip_typeref(expr_left->datatype);
904 type_t *type_right = skip_typeref(expr_right->datatype);
906 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
907 return new_d_Add(dbgi, left, right, mode);
912 pointer_type_t *pointer_type;
913 if(type_left->type == TYPE_POINTER) {
916 pointer_type = (pointer_type_t*) type_left;
918 assert(type_right->type == TYPE_POINTER);
921 pointer_type = (pointer_type_t*) type_right;
924 type_t *points_to = pointer_type->points_to;
925 unsigned elem_size = get_type_size(points_to);
927 assert(elem_size >= 1);
929 ir_node *cnst = new_Const_long(mode_Iu, elem_size);
930 ir_node *mul = new_d_Mul(dbgi, integer, cnst, mode_Iu);
934 ir_node *res = new_d_Add(dbgi, pointer, integer, mode);
939 static ir_node *create_sub(const binary_expression_t *expression)
941 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
942 ir_node *left = expression_to_firm(expression->left);
943 ir_node *right = expression_to_firm(expression->right);
944 type_t *type = expression->expression.datatype;
945 ir_mode *mode = get_ir_mode(type);
947 expression_t *expr_left = expression->left;
948 expression_t *expr_right = expression->right;
949 type_t *type_left = skip_typeref(expr_left->datatype);
950 type_t *type_right = skip_typeref(expr_right->datatype);
952 if((is_type_arithmetic(type_left) && is_type_arithmetic(type_right))
953 || (type_left->type == TYPE_POINTER
954 && type_right->type == TYPE_POINTER)) {
955 return new_d_Sub(dbgi, left, right, mode);
958 assert(type_right->type == TYPE_POINTER);
959 ir_node *pointer = left;
960 ir_node *integer = right;
961 pointer_type_t *pointer_type = (pointer_type_t*) type_right;
963 type_t *points_to = pointer_type->points_to;
964 unsigned elem_size = get_type_size(points_to);
966 assert(elem_size >= 1);
968 ir_node *cnst = new_Const_long(mode_Iu, elem_size);
969 ir_node *mul = new_d_Mul(dbgi, integer, cnst, mode_Iu);
973 ir_node *res = new_d_Sub(dbgi, pointer, integer, mode);
980 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
982 binary_expression_type_t type = expression->type;
985 case BINEXPR_NOTEQUAL:
987 case BINEXPR_LESSEQUAL:
988 case BINEXPR_GREATER:
989 case BINEXPR_GREATEREQUAL: {
990 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
991 ir_node *left = expression_to_firm(expression->left);
992 ir_node *right = expression_to_firm(expression->right);
993 ir_node *cmp = new_d_Cmp(dbgi, left, right);
994 long pnc = get_pnc(type);
995 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
998 case BINEXPR_ASSIGN: {
999 ir_node *right = expression_to_firm(expression->right);
1000 set_value_for_expression(expression->left, right);
1004 return create_add(expression);
1006 return create_sub(expression);
1008 return create_arithmetic_binop(expression, new_d_Mul);
1009 case BINEXPR_BITWISE_AND:
1010 return create_arithmetic_binop(expression, new_d_And);
1011 case BINEXPR_BITWISE_OR:
1012 return create_arithmetic_binop(expression, new_d_Or);
1013 case BINEXPR_BITWISE_XOR:
1014 return create_arithmetic_binop(expression, new_d_Eor);
1015 case BINEXPR_SHIFTLEFT:
1016 return create_arithmetic_binop(expression, new_d_Shl);
1017 case BINEXPR_SHIFTRIGHT:
1018 return create_arithmetic_binop(expression, new_d_Shr);
1019 case BINEXPR_LOGICAL_AND:
1020 case BINEXPR_LOGICAL_OR:
1021 return create_lazy_op(expression);
1023 panic("TODO binexpr type");
1027 static ir_node *array_access_addr(const array_access_expression_t *expression)
1029 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1031 ir_node *base_addr = expression_to_firm(expression->array_ref);
1032 ir_node *offset = expression_to_firm(expression->index);
1033 offset = create_conv(dbgi, offset, mode_Iu);
1035 unsigned elem_size = get_type_size(expression->expression.datatype);
1036 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1037 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1039 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1044 static ir_node *array_access_to_firm(
1045 const array_access_expression_t *expression)
1048 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1049 ir_node *addr = array_access_addr(expression);
1050 type_t *type = expression->expression.datatype;
1052 return load_from_expression_addr(type, addr, dbgi);
1055 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1057 type_t *type = expression->type;
1059 type = expression->size_expression->datatype;
1060 assert(type != NULL);
1063 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1064 unsigned size = get_type_size(type);
1065 ir_node *size_node = new_Const_long(mode, size);
1070 static ir_node *expression_to_addr(const expression_t *expression)
1072 switch(expression->type) {
1073 case EXPR_ARRAY_ACCESS:
1074 return array_access_addr((const array_access_expression_t*) expression);
1078 panic("trying to get address of non-lvalue");
1081 static ir_node *_expression_to_firm(const expression_t *expression)
1083 switch(expression->type) {
1085 return const_to_firm((const const_t*) expression);
1086 case EXPR_STRING_LITERAL:
1087 return string_literal_to_firm((const string_literal_t*) expression);
1088 case EXPR_REFERENCE:
1089 return reference_expression_to_firm(
1090 (const reference_expression_t*) expression);
1092 return call_expression_to_firm((const call_expression_t*) expression);
1094 return unary_expression_to_firm((const unary_expression_t*) expression);
1096 return binary_expression_to_firm(
1097 (const binary_expression_t*) expression);
1098 case EXPR_ARRAY_ACCESS:
1099 return array_access_to_firm(
1100 (const array_access_expression_t*) expression);
1102 return sizeof_to_firm((const sizeof_expression_t*) expression);
1106 panic("unsupported expression found");
1109 static ir_node *expression_to_firm(const expression_t *expression)
1111 ir_node *res = _expression_to_firm(expression);
1113 if(expression->datatype == type_void)
1116 ir_mode *mode = get_ir_mode(expression->datatype);
1117 res = create_conv(NULL, res, mode);
1122 static void statement_to_firm(statement_t *statement);
1124 static void return_statement_to_firm(return_statement_t *statement)
1126 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1129 if(statement->return_value != NULL) {
1130 ir_node *retval = expression_to_firm(statement->return_value);
1134 ret = new_d_Return(dbgi, get_store(), 1, in);
1136 ret = new_d_Return(dbgi, get_store(), 0, NULL);
1138 ir_node *end_block = get_irg_end_block(current_ir_graph);
1139 add_immBlock_pred(end_block, ret);
1141 set_cur_block(NULL);
1144 static void compound_statement_to_firm(compound_statement_t *compound)
1146 statement_t *statement = compound->statements;
1147 for( ; statement != NULL; statement = statement->next) {
1148 //context2firm(&statement->context);
1149 statement_to_firm(statement);
1153 static void expression_statement_to_firm(expression_statement_t *statement)
1155 expression_to_firm(statement->expression);
1158 static void if_statement_to_firm(if_statement_t *statement)
1160 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1161 ir_node *condition = _expression_to_firm(statement->condition);
1162 assert(condition != NULL);
1164 /* make sure we have a mode_b condition */
1165 condition = create_conv(dbgi, condition, mode_b);
1167 ir_node *cond = new_d_Cond(dbgi, condition);
1168 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1169 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1171 ir_node *fallthrough_block = new_immBlock();
1173 /* the true (blocks) */
1174 ir_node *true_block = new_immBlock();
1175 add_immBlock_pred(true_block, true_proj);
1176 mature_immBlock(true_block);
1178 statement_to_firm(statement->true_statement);
1179 if(get_cur_block() != NULL) {
1180 ir_node *jmp = new_Jmp();
1181 add_immBlock_pred(fallthrough_block, jmp);
1184 /* the false (blocks) */
1185 if(statement->false_statement != NULL) {
1186 ir_node *false_block = new_immBlock();
1187 add_immBlock_pred(false_block, false_proj);
1188 mature_immBlock(false_block);
1190 statement_to_firm(statement->false_statement);
1191 if(get_cur_block() != NULL) {
1192 ir_node *jmp = new_Jmp();
1193 add_immBlock_pred(fallthrough_block, jmp);
1196 add_immBlock_pred(fallthrough_block, false_proj);
1198 mature_immBlock(fallthrough_block);
1200 set_cur_block(fallthrough_block);
1203 static void while_statement_to_firm(while_statement_t *statement)
1205 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1207 ir_node *jmp = NULL;
1208 if(get_cur_block() != NULL) {
1212 /* create the header block */
1213 ir_node *header_block = new_immBlock();
1215 add_immBlock_pred(header_block, jmp);
1218 /* create the condition */
1219 ir_node *condition = _expression_to_firm(statement->condition);
1220 condition = create_conv(dbgi, condition, mode_b);
1222 ir_node *cond = new_d_Cond(dbgi, condition);
1223 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1224 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1226 /* the false block */
1227 ir_node *false_block = new_immBlock();
1228 add_immBlock_pred(false_block, false_proj);
1231 ir_node *body_block = new_immBlock();
1232 add_immBlock_pred(body_block, true_proj);
1233 mature_immBlock(body_block);
1235 ir_node *old_continue_label = continue_label;
1236 ir_node *old_break_label = break_label;
1237 continue_label = header_block;
1238 break_label = false_block;
1240 statement_to_firm(statement->body);
1242 assert(continue_label == header_block);
1243 assert(break_label == false_block);
1244 continue_label = old_continue_label;
1245 break_label = old_break_label;
1247 if(get_cur_block() != NULL) {
1248 ir_node *jmp = new_Jmp();
1249 add_immBlock_pred(header_block, jmp);
1252 mature_immBlock(header_block);
1253 mature_immBlock(false_block);
1255 set_cur_block(false_block);
1258 static void do_while_statement_to_firm(do_while_statement_t *statement)
1260 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1262 ir_node *jmp = NULL;
1263 if(get_cur_block() != NULL) {
1267 /* create the header block */
1268 ir_node *header_block = new_immBlock();
1270 /* the false block */
1271 ir_node *false_block = new_immBlock();
1274 ir_node *body_block = new_immBlock();
1276 add_immBlock_pred(body_block, jmp);
1279 ir_node *old_continue_label = continue_label;
1280 ir_node *old_break_label = break_label;
1281 continue_label = header_block;
1282 break_label = false_block;
1284 statement_to_firm(statement->body);
1286 assert(continue_label == header_block);
1287 assert(break_label == false_block);
1288 continue_label = old_continue_label;
1289 break_label = old_break_label;
1291 if(get_cur_block() == NULL) {
1292 mature_immBlock(header_block);
1293 mature_immBlock(body_block);
1297 ir_node *body_jmp = new_Jmp();
1298 add_immBlock_pred(header_block, body_jmp);
1299 mature_immBlock(header_block);
1301 /* create the condition */
1302 ir_node *condition = _expression_to_firm(statement->condition);
1303 condition = create_conv(dbgi, condition, mode_b);
1305 ir_node *cond = new_d_Cond(dbgi, condition);
1306 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1307 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1309 add_immBlock_pred(body_block, true_proj);
1310 mature_immBlock(body_block);
1312 add_immBlock_pred(false_block, false_proj);
1313 mature_immBlock(false_block);
1315 set_cur_block(false_block);
1318 static void for_statement_to_firm(for_statement_t *statement)
1320 dbg_info *const dbgi = get_dbg_info(&statement->statement.source_position);
1322 ir_node *jmp = NULL;
1323 if (get_cur_block() != NULL) {
1324 if(statement->initialisation != NULL) {
1325 expression_to_firm(statement->initialisation);
1330 /* create the step block */
1331 ir_node *const step_block = new_immBlock();
1332 expression_to_firm(statement->step);
1333 ir_node *const step_jmp = new_Jmp();
1335 /* create the header block */
1336 ir_node *const header_block = new_immBlock();
1338 add_immBlock_pred(header_block, jmp);
1340 add_immBlock_pred(header_block, step_jmp);
1342 /* create the condition */
1343 ir_node *const cond_expr = _expression_to_firm(statement->condition);
1344 ir_node *const condition = create_conv(dbgi, cond_expr, mode_b);
1346 ir_node *const cond = new_d_Cond(dbgi, condition);
1347 ir_node *const true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1348 ir_node *const false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1350 /* the false block */
1351 ir_node *const false_block = new_immBlock();
1352 add_immBlock_pred(false_block, false_proj);
1355 ir_node *const body_block = new_immBlock();
1356 add_immBlock_pred(body_block, true_proj);
1357 mature_immBlock(body_block);
1359 ir_node *const old_continue_label = continue_label;
1360 ir_node *const old_break_label = break_label;
1361 continue_label = step_block;
1362 break_label = false_block;
1364 statement_to_firm(statement->body);
1366 assert(continue_label == step_block);
1367 assert(break_label == false_block);
1368 continue_label = old_continue_label;
1369 break_label = old_break_label;
1371 if (get_cur_block() != NULL) {
1372 ir_node *const jmp = new_Jmp();
1373 add_immBlock_pred(step_block, jmp);
1376 mature_immBlock(step_block);
1377 mature_immBlock(header_block);
1378 mature_immBlock(false_block);
1380 set_cur_block(false_block);
1383 static void create_declaration_entity(declaration_t *declaration,
1384 declaration_type_t declaration_type,
1385 ir_type *parent_type)
1387 ident *id = new_id_from_str(declaration->symbol->string);
1388 ir_type *irtype = get_ir_type(declaration->type);
1389 ir_entity *entity = new_entity(parent_type, id, irtype);
1390 set_entity_ld_ident(entity, id);
1392 declaration->declaration_type = declaration_type;
1393 declaration->v.entity = entity;
1394 set_entity_variability(entity, variability_uninitialized);
1395 /* TODO: visibility? */
1398 static void create_initializer(declaration_t *declaration)
1400 initializer_t *initializer = declaration->init.initializer;
1401 if(initializer == NULL)
1404 if(initializer->type == INITIALIZER_VALUE) {
1405 assert(initializer->designator == NULL);
1406 assert(initializer->next == NULL);
1407 ir_node *init_node = expression_to_firm(initializer->v.value);
1409 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1410 set_value(declaration->v.value_number, init_node);
1412 panic("initializer not completely implemented yet");
1415 assert(initializer->type == INITIALIZER_LIST);
1416 panic("list initializer not supported yet");
1420 static void create_local_variable(declaration_t *declaration)
1422 bool needs_entity = declaration->address_taken;
1424 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
1427 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
1428 create_declaration_entity(declaration,
1429 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
1432 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1433 declaration->v.value_number = next_value_number_function;
1434 ++next_value_number_function;
1437 create_initializer(declaration);
1440 static void declaration_statement_to_firm(declaration_statement_t *statement)
1442 declaration_t *declaration = statement->declarations_begin;
1443 declaration_t *end = statement->declarations_end->next;
1444 for( ; declaration != end; declaration = declaration->next) {
1445 type_t *type = declaration->type;
1447 switch(declaration->storage_class) {
1448 case STORAGE_CLASS_TYPEDEF:
1450 case STORAGE_CLASS_STATIC:
1451 panic("static local vars not implemented yet");
1452 case STORAGE_CLASS_ENUM_ENTRY:
1453 panic("enum entry declaration in local block found");
1454 case STORAGE_CLASS_EXTERN:
1455 panic("extern declaration in local block found");
1456 case STORAGE_CLASS_NONE:
1457 case STORAGE_CLASS_AUTO:
1458 case STORAGE_CLASS_REGISTER:
1459 if(type->type == TYPE_FUNCTION) {
1460 panic("nested functions not supported yet");
1462 create_local_variable(declaration);
1466 panic("invalid storage class found");
1470 static void create_jump_statement(const statement_t *statement,
1471 ir_node *target_block)
1473 dbg_info *dbgi = get_dbg_info(&statement->source_position);
1474 ir_node *jump = new_d_Jmp(dbgi);
1475 add_immBlock_pred(target_block, jump);
1477 set_cur_block(NULL);
1480 static void switch_statement_to_firm(const switch_statement_t *statement)
1482 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1484 ir_node *expression = expression_to_firm(statement->expression);
1485 ir_node *cond = new_d_Cond(dbgi, expression);
1486 ir_node *break_block = new_immBlock();
1488 set_cur_block(NULL);
1490 ir_node *old_switch_cond = current_switch_cond;
1491 ir_node *old_break_label = break_label;
1492 current_switch_cond = cond;
1493 break_label = break_block;
1495 statement_to_firm(statement->body);
1497 if(get_cur_block() != NULL) {
1498 ir_node *jmp = new_Jmp();
1499 add_immBlock_pred(break_block, jmp);
1502 assert(current_switch_cond == cond);
1503 assert(break_label == break_block);
1504 current_switch_cond = old_switch_cond;
1505 break_label = old_break_label;
1507 mature_immBlock(break_block);
1508 set_cur_block(break_block);
1511 static void case_label_to_firm(const case_label_statement_t *statement)
1513 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1515 /* let's create a node and hope firm constant folding creates a Const
1518 set_cur_block(get_nodes_block(current_switch_cond));
1519 if(statement->expression) {
1520 ir_node *cnst = expression_to_firm(statement->expression);
1521 if(!is_Const(cnst)) {
1522 panic("couldn't fold constant for case label");
1524 tarval *tv = get_Const_tarval(cnst);
1525 if(!mode_is_int(get_tarval_mode(tv))) {
1526 panic("case label not an integer");
1529 long pn = get_tarval_long(tv);
1530 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
1531 /* oops someone detected our cheating... */
1532 panic("magic default pn used");
1534 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
1536 proj = new_d_defaultProj(dbgi, current_switch_cond,
1537 MAGIC_DEFAULT_PN_NUMBER);
1540 ir_node *block = new_immBlock();
1541 add_immBlock_pred(block, proj);
1542 mature_immBlock(block);
1545 static void statement_to_firm(statement_t *statement)
1547 switch(statement->type) {
1548 case STATEMENT_COMPOUND:
1549 compound_statement_to_firm((compound_statement_t*) statement);
1551 case STATEMENT_RETURN:
1552 return_statement_to_firm((return_statement_t*) statement);
1554 case STATEMENT_EXPRESSION:
1555 expression_statement_to_firm((expression_statement_t*) statement);
1558 if_statement_to_firm((if_statement_t*) statement);
1560 case STATEMENT_WHILE:
1561 while_statement_to_firm((while_statement_t*) statement);
1563 case STATEMENT_DO_WHILE:
1564 do_while_statement_to_firm((do_while_statement_t*) statement);
1566 case STATEMENT_DECLARATION:
1567 declaration_statement_to_firm((declaration_statement_t*) statement);
1569 case STATEMENT_BREAK:
1570 create_jump_statement(statement, break_label);
1572 case STATEMENT_CONTINUE:
1573 create_jump_statement(statement, continue_label);
1575 case STATEMENT_SWITCH:
1576 switch_statement_to_firm((switch_statement_t*) statement);
1578 case STATEMENT_CASE_LABEL:
1579 case_label_to_firm((case_label_statement_t*) statement);
1582 for_statement_to_firm((for_statement_t*) statement);
1587 panic("Statement not implemented\n");
1590 static int get_function_n_local_vars(declaration_t *declaration)
1597 static void initialize_function_parameters(declaration_t *declaration)
1599 ir_graph *irg = current_ir_graph;
1600 ir_node *args = get_irg_args(irg);
1601 ir_node *start_block = get_irg_start_block(irg);
1604 declaration_t *parameter = declaration->context.declarations;
1605 for( ; parameter != NULL; parameter = parameter->next) {
1606 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
1608 if(parameter->address_taken) {
1609 panic("address take from parameter not implemented yet");
1612 ir_mode *mode = get_ir_mode(parameter->type);
1614 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
1617 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1618 parameter->v.value_number = next_value_number_function;
1619 ++next_value_number_function;
1621 set_value(parameter->v.value_number, proj);
1625 static void create_function(declaration_t *declaration)
1627 ir_entity *entity = get_function_entity(declaration);
1629 if(declaration->init.statement == NULL)
1632 int n_local_vars = get_function_n_local_vars(declaration);
1633 ir_graph *irg = new_ir_graph(entity, n_local_vars);
1634 ir_node *first_block = get_cur_block();
1636 next_value_number_function = 0;
1637 initialize_function_parameters(declaration);
1639 statement_to_firm(declaration->init.statement);
1641 ir_node *end_block = get_irg_end_block(irg);
1643 /* do we have a return statement yet? */
1644 if(get_cur_block() != NULL) {
1645 assert(declaration->type->type == TYPE_FUNCTION);
1646 const function_type_t* const func_type = (const function_type_t*)declaration->type;
1648 if (func_type->result_type == type_void) {
1649 ret = new_Return(get_store(), 0, NULL);
1651 ir_mode *const mode = get_ir_mode(func_type->result_type);
1653 // ยง5.1.2.2.3 main implicitly returns 0
1654 if (strcmp(declaration->symbol->string, "main") == 0) {
1655 in[0] = new_Const(mode, get_mode_null(mode));
1657 in[0] = new_Unknown(mode);
1659 ret = new_Return(get_store(), 1, in);
1661 add_immBlock_pred(end_block, ret);
1664 mature_immBlock(first_block);
1665 mature_immBlock(end_block);
1667 irg_finalize_cons(irg);
1669 /* finalize the frame type */
1670 ir_type *frame_type = get_irg_frame_type(irg);
1671 int n = get_compound_n_members(frame_type);
1674 for(int i = 0; i < n; ++i) {
1675 ir_entity *entity = get_compound_member(frame_type, i);
1676 ir_type *entity_type = get_entity_type(entity);
1678 int align = get_type_alignment_bytes(entity_type);
1679 if(align > align_all)
1683 misalign = offset % align;
1687 set_entity_offset(entity, offset);
1688 offset += get_type_size_bytes(entity_type);
1690 set_type_size_bytes(frame_type, offset);
1691 set_type_alignment_bytes(frame_type, align_all);
1692 set_type_state(frame_type, layout_fixed);
1697 static void context_to_firm(context_t *context)
1699 declaration_t *declaration = context->declarations;
1700 for( ; declaration != NULL; declaration = declaration->next) {
1701 if(declaration->namespace != NAMESPACE_NORMAL)
1703 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
1704 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
1707 type_t *type = declaration->type;
1708 if(type->type == TYPE_FUNCTION) {
1709 create_function(declaration);
1716 void translation_unit_to_firm(translation_unit_t *unit)
1718 /* remove me later TODO FIXME */
1719 (void) get_type_size;
1721 /* just to be sure */
1722 continue_label = NULL;
1724 current_switch_cond = NULL;
1726 context_to_firm(& unit->context);