8 #include <libfirm/firm.h>
9 #include <libfirm/adt/obst.h>
13 #include "adt/error.h"
18 static ir_type *ir_type_const_char;
19 static ir_type *ir_type_void;
20 static ir_type *ir_type_int;
21 static ir_type *ir_type_void_ptr;
23 static type_t *type_const_char;
24 static type_t *type_void;
25 static type_t *type_int;
27 typedef enum declaration_type_t {
28 DECLARATION_TYPE_UNKNOWN,
29 DECLARATION_TYPE_FUNCTION,
30 DECLARATION_TYPE_GLOBAL_VARIABLE,
31 DECLARATION_TYPE_PARAMETER,
32 DECLARATION_TYPE_PARAMETER_ENTITY,
33 DECLARATION_TYPE_LOCAL_VARIABLE,
34 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
35 DECLARATION_TYPE_COMPOUND_MEMBER
38 typedef struct type2firm_env_t type2firm_env_t;
39 struct type2firm_env_t {
40 int can_cache; /* nonzero if type can safely be cached because
41 no typevariables are in the hierarchy */
44 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type);
45 static ir_type *get_ir_type(type_t *type);
47 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
51 const declaration_t *declaration = & value_numbers[pos]->declaration;
53 print_warning_prefix(declaration->source_position);
54 fprintf(stderr, "variable '%s' might be used uninitialized\n",
55 declaration->symbol->string);
57 fprintf(stderr, "Some variable might be used uninitialized\n");
58 return new_r_Unknown(irg, mode);
61 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
63 const source_position_t *pos = (const source_position_t*) dbg;
66 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
70 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
72 const source_position_t *pos = (const source_position_t*) dbg;
77 return pos->input_name;
80 void init_ast2firm(void)
82 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
83 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
84 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
86 ir_type_int = get_ir_type(type_int);
87 ir_type_const_char = get_ir_type(type_const_char);
88 ir_type_void = get_ir_type(type_int); /* we don't have a real void
90 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
91 ir_type_void, mode_P_data);
93 type_void->firm_type = ir_type_void;
96 void exit_ast2firm(void)
100 static unsigned unique_id = 0;
102 static ident *unique_ident(const char *tag)
106 snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
108 return new_id_from_str(buf);
111 static type_t *skip_typeref(type_t *type)
116 const typedef_type_t *typedef_type = (const typedef_type_t*) type;
117 type = typedef_type->declaration->type;
121 const typeof_type_t *typeof_type = (const typeof_type_t *) type;
122 if(typeof_type->typeof_type != NULL) {
123 type = typeof_type->typeof_type;
125 type = typeof_type->expression->datatype;
138 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
140 switch(atomic_type->atype) {
141 case ATOMIC_TYPE_SCHAR:
142 case ATOMIC_TYPE_CHAR:
144 case ATOMIC_TYPE_UCHAR:
146 case ATOMIC_TYPE_SHORT:
148 case ATOMIC_TYPE_USHORT:
150 case ATOMIC_TYPE_LONG:
151 case ATOMIC_TYPE_INT:
153 case ATOMIC_TYPE_ULONG:
154 case ATOMIC_TYPE_UINT:
156 case ATOMIC_TYPE_LONGLONG:
158 case ATOMIC_TYPE_ULONGLONG:
160 case ATOMIC_TYPE_FLOAT:
162 case ATOMIC_TYPE_DOUBLE:
164 case ATOMIC_TYPE_LONG_DOUBLE:
166 case ATOMIC_TYPE_BOOL:
168 #ifdef PROVIDE_COMPLEX
169 case ATOMIC_TYPE_FLOAT_COMPLEX:
170 case ATOMIC_TYPE_DOUBLE_COMPLEX:
171 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
172 panic("complex lowering not implemented yet");
174 case ATOMIC_TYPE_FLOAT_IMAGINARY:
175 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
176 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
177 panic("imaginary lowering not implemented yet");
180 case ATOMIC_TYPE_VOID:
181 panic("tried to get mode from void type");
183 case ATOMIC_TYPE_INVALID:
186 panic("Encountered unknown atomic type");
190 static unsigned get_type_size(type_t *type);
192 static unsigned get_atomic_type_size(const atomic_type_t *type)
194 switch(type->atype) {
195 case ATOMIC_TYPE_CHAR:
196 case ATOMIC_TYPE_SCHAR:
197 case ATOMIC_TYPE_UCHAR:
200 case ATOMIC_TYPE_SHORT:
201 case ATOMIC_TYPE_USHORT:
204 case ATOMIC_TYPE_BOOL:
205 case ATOMIC_TYPE_INT:
206 case ATOMIC_TYPE_UINT:
207 case ATOMIC_TYPE_LONG:
208 case ATOMIC_TYPE_ULONG:
209 case ATOMIC_TYPE_FLOAT:
212 case ATOMIC_TYPE_LONGLONG:
213 case ATOMIC_TYPE_ULONGLONG:
214 case ATOMIC_TYPE_DOUBLE:
217 case ATOMIC_TYPE_LONG_DOUBLE:
220 case ATOMIC_TYPE_VOID:
223 case ATOMIC_TYPE_INVALID:
226 panic("Trying to determine size of invalid atomic type");
229 static unsigned get_compound_type_size(compound_type_t *type)
231 ir_type *irtype = get_ir_type(&type->type);
232 return get_type_size_bytes(irtype);
235 static unsigned get_array_type_size(array_type_t *type)
237 ir_type *irtype = get_ir_type(&type->type);
238 return get_type_size_bytes(irtype);
241 static unsigned get_type_size(type_t *type)
243 type = skip_typeref(type);
247 return get_atomic_type_size((const atomic_type_t*) type);
249 return get_mode_size_bytes(mode_Is);
250 case TYPE_COMPOUND_UNION:
251 case TYPE_COMPOUND_STRUCT:
252 return get_compound_type_size((compound_type_t*) type);
254 /* just a pointer to the function */
255 return get_mode_size_bytes(mode_P_code);
257 return get_mode_size_bytes(mode_P_data);
259 return get_array_type_size((array_type_t*) type);
266 panic("Trying to determine size of invalid type");
269 static unsigned count_parameters(const function_type_t *function_type)
273 function_parameter_t *parameter = function_type->parameters;
274 for ( ; parameter != NULL; parameter = parameter->next) {
284 static ir_type *create_atomic_type(type2firm_env_t *env,
285 const atomic_type_t *type)
288 ir_mode *mode = get_atomic_mode(type);
289 ident *id = get_mode_ident(mode);
290 ir_type *irtype = new_type_primitive(id, mode);
295 static ir_type *create_method_type(type2firm_env_t *env,
296 const function_type_t *function_type)
298 type_t *result_type = function_type->result_type;
300 ident *id = unique_ident("functiontype");
301 int n_parameters = count_parameters(function_type);
302 int n_results = result_type == type_void ? 0 : 1;
303 ir_type *irtype = new_type_method(id, n_parameters, n_results);
305 if(result_type != type_void) {
306 ir_type *restype = _get_ir_type(env, result_type);
307 set_method_res_type(irtype, 0, restype);
310 function_parameter_t *parameter = function_type->parameters;
312 for( ; parameter != NULL; parameter = parameter->next) {
313 ir_type *p_irtype = _get_ir_type(env, parameter->type);
314 set_method_param_type(irtype, n, p_irtype);
318 if(function_type->variadic) {
319 set_method_variadicity(irtype, variadicity_variadic);
325 static ir_type *create_pointer_type(type2firm_env_t *env, pointer_type_t *type)
327 type_t *points_to = type->points_to;
328 ir_type *ir_points_to;
329 /* Avoid endless recursion if the points_to type contains this poiner type
330 * again (might be a struct). We therefore first create a void* pointer
331 * and then set the real points_to type
333 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
334 ir_type_void, mode_P_data);
335 type->type.firm_type = ir_type;
337 ir_points_to = _get_ir_type(env, points_to);
338 set_pointer_points_to_type(ir_type, ir_points_to);
343 static ir_type *create_array_type(type2firm_env_t *env, array_type_t *type)
345 type_t *element_type = type->element_type;
346 ir_type *ir_element_type = _get_ir_type(env, element_type);
350 panic("TODO arraytpye size not implemented yet");
352 ir_type *ir_type = new_type_array(unique_ident("array"), 1, ir_element_type);
353 set_array_bounds_int(ir_type, 0, 0, n_elements);
355 size_t elemsize = get_type_size_bytes(ir_element_type);
356 int align = get_type_alignment_bytes(ir_element_type);
357 if(elemsize % align > 0) {
358 elemsize += align - (elemsize % align);
360 set_type_size_bytes(ir_type, n_elements * elemsize);
361 set_type_alignment_bytes(ir_type, align);
362 set_type_state(ir_type, layout_fixed);
367 #define INVALID_TYPE ((ir_type_ptr)-1)
369 static ir_type *create_struct_type(type2firm_env_t *env, compound_type_t *type)
371 symbol_t *symbol = type->declaration->symbol;
374 id = unique_ident(symbol->string);
376 id = unique_ident("__anonymous_struct");
378 ir_type *ir_type = new_type_struct(id);
380 type->type.firm_type = ir_type;
384 declaration_t *entry = type->declaration->context.declarations;
385 for( ; entry != NULL; entry = entry->next) {
386 ident *ident = new_id_from_str(entry->symbol->string);
387 ir_type_ptr entry_ir_type = _get_ir_type(env, entry->type);
389 int entry_size = get_type_size_bytes(entry_ir_type);
390 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
391 int misalign = offset % entry_alignment;
394 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
395 set_entity_offset(entity, offset);
396 add_struct_member(ir_type, entity);
397 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
398 entry->v.entity = entity;
400 offset += entry_size;
401 if(entry_alignment > align_all) {
402 if(entry_alignment % align_all != 0) {
403 panic("Uneven alignments not supported yet");
405 align_all = entry_alignment;
409 int misalign = offset % align_all;
411 set_type_alignment_bytes(ir_type, align_all);
412 set_type_size_bytes(ir_type, offset);
413 set_type_state(ir_type, layout_fixed);
418 static ir_type *create_union_type(type2firm_env_t *env, compound_type_t *type)
420 declaration_t *declaration = type->declaration;
421 symbol_t *symbol = declaration->symbol;
424 id = unique_ident(symbol->string);
426 id = unique_ident("__anonymous_union");
428 ir_type *ir_type = new_type_union(id);
430 type->type.firm_type = ir_type;
434 declaration_t *entry = declaration->context.declarations;
435 for( ; entry != NULL; entry = entry->next) {
436 ident *ident = new_id_from_str(entry->symbol->string);
437 ir_type_ptr entry_ir_type = _get_ir_type(env, entry->type);
439 int entry_size = get_type_size_bytes(entry_ir_type);
440 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
442 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
443 add_union_member(ir_type, entity);
444 set_entity_offset(entity, 0);
445 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
446 entry->v.entity = entity;
448 if(entry_size > size) {
451 if(entry_alignment > align_all) {
452 if(entry_alignment % align_all != 0) {
453 panic("Uneven alignments not supported yet");
455 align_all = entry_alignment;
459 set_type_alignment_bytes(ir_type, align_all);
460 set_type_size_bytes(ir_type, size);
461 set_type_state(ir_type, layout_fixed);
466 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type)
468 assert(type != NULL);
470 type = skip_typeref(type);
472 if(type->firm_type != NULL) {
473 assert(type->firm_type != INVALID_TYPE);
474 return type->firm_type;
477 ir_type *firm_type = NULL;
480 firm_type = create_atomic_type(env, (atomic_type_t*) type);
483 firm_type = create_method_type(env, (function_type_t*) type);
486 firm_type = create_pointer_type(env, (pointer_type_t*) type);
489 firm_type = create_array_type(env, (array_type_t*) type);
491 case TYPE_COMPOUND_STRUCT:
492 firm_type = create_struct_type(env, (compound_type_t*) type);
494 case TYPE_COMPOUND_UNION:
495 firm_type = create_union_type(env, (compound_type_t*) type);
498 firm_type = ir_type_int;
506 if(firm_type == NULL)
507 panic("unknown type found");
510 type->firm_type = firm_type;
516 static ir_type *get_ir_type(type_t *type)
521 return _get_ir_type(&env, type);
524 static inline ir_mode *get_ir_mode(type_t *type)
526 ir_type *irtype = get_ir_type(type);
527 ir_mode *mode = get_type_mode(irtype);
528 assert(mode != NULL);
532 static ir_entity* get_function_entity(declaration_t *declaration)
534 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
535 return declaration->v.entity;
536 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
538 symbol_t *symbol = declaration->symbol;
539 ident *id = new_id_from_str(symbol->string);
541 ir_type *global_type = get_glob_type();
542 ir_type *ir_type_method = get_ir_type(declaration->type);
543 assert(is_Method_type(ir_type_method));
545 type_t *type = declaration->type;
546 ir_entity *entity = new_entity(global_type, id, ir_type_method);
547 set_entity_ld_ident(entity, id);
548 if(declaration->storage_class & STORAGE_CLASS_STATIC
549 || type->qualifiers & TYPE_QUALIFIER_INLINE) {
550 set_entity_visibility(entity, visibility_local);
551 } else if(declaration->init.statement != NULL) {
552 set_entity_visibility(entity, visibility_external_visible);
554 set_entity_visibility(entity, visibility_external_allocated);
557 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
558 declaration->v.entity = entity;
565 static ir_node *expression_to_firm(const expression_t *expression);
567 static dbg_info *get_dbg_info(const source_position_t *pos)
569 return (dbg_info*) pos;
572 static ir_node *const_to_firm(const const_t *cnst)
574 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
575 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
578 if(mode_is_float(mode)) {
579 tv = new_tarval_from_double(cnst->v.float_value, mode);
581 tv = new_tarval_from_long(cnst->v.int_value, mode);
584 return new_d_Const(dbgi, mode, tv);
587 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
589 assert(entity != NULL);
590 union symconst_symbol sym;
591 sym.entity_p = entity;
592 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
595 static ir_node *string_literal_to_firm(const string_literal_t* literal)
597 ir_type *global_type = get_glob_type();
598 ir_type *type = new_type_array(unique_ident("strtype"), 1,
601 ident *id = unique_ident("Lstr");
602 ir_entity *entity = new_entity(global_type, id, type);
603 set_entity_ld_ident(entity, id);
604 set_entity_variability(entity, variability_constant);
606 ir_type *elem_type = ir_type_const_char;
607 ir_mode *mode = get_type_mode(elem_type);
609 const char *string = literal->value;
610 size_t slen = strlen(string) + 1;
612 set_array_lower_bound_int(type, 0, 0);
613 set_array_upper_bound_int(type, 0, slen);
614 set_type_size_bytes(type, slen);
615 set_type_state(type, layout_fixed);
617 tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
618 for(size_t i = 0; i < slen; ++i) {
619 tvs[i] = new_tarval_from_long(string[i], mode);
622 set_array_entity_values(entity, tvs, slen);
625 dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
627 return create_symconst(dbgi, entity);
630 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
632 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
633 declaration_t *declaration = ref->declaration;
635 switch((declaration_type_t) declaration->declaration_type) {
636 case DECLARATION_TYPE_UNKNOWN:
638 case DECLARATION_TYPE_PARAMETER: {
639 ir_node *args = get_irg_args(current_ir_graph);
640 ir_mode *mode = get_ir_mode(declaration->type);
641 ir_node *block = get_irg_start_block(current_ir_graph);
642 long pn = declaration->v.value_number;
644 return new_r_Proj(current_ir_graph, block, args, mode, pn);
646 case DECLARATION_TYPE_FUNCTION: {
647 return create_symconst(dbgi, declaration->v.entity);
649 case DECLARATION_TYPE_PARAMETER_ENTITY:
650 case DECLARATION_TYPE_LOCAL_VARIABLE:
651 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
652 case DECLARATION_TYPE_GLOBAL_VARIABLE:
653 case DECLARATION_TYPE_COMPOUND_MEMBER:
654 panic("not implemented reference type");
657 panic("reference to declaration with unknown type found");
660 static ir_node *call_expression_to_firm(const call_expression_t *call)
662 expression_t *function = call->function;
663 ir_node *callee = expression_to_firm(function);
665 assert(function->datatype->type == TYPE_FUNCTION);
666 function_type_t *function_type = (function_type_t*) function->datatype;
668 int n_parameters = 0;
669 call_argument_t *argument = call->arguments;
670 for( ; argument != NULL; argument = argument->next) {
674 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
675 ir_type *new_method_type = NULL;
676 if(function_type->variadic) {
677 /* we need to construct a new method type matching the call
679 int n_res = get_method_n_ress(ir_method_type);
680 new_method_type = new_type_method(unique_ident("calltype"),
681 n_parameters, n_res);
682 set_method_calling_convention(new_method_type,
683 get_method_calling_convention(ir_method_type));
684 set_method_additional_properties(new_method_type,
685 get_method_additional_properties(ir_method_type));
687 for(int i = 0; i < n_res; ++i) {
688 set_method_res_type(new_method_type, i,
689 get_method_res_type(ir_method_type, i));
692 ir_node *in[n_parameters];
694 argument = call->arguments;
696 for( ; argument != NULL; argument = argument->next) {
697 expression_t *expression = argument->expression;
698 ir_node *arg_node = expression_to_firm(expression);
701 if(new_method_type != NULL) {
702 ir_type *irtype = get_ir_type(expression->datatype);
703 set_method_param_type(new_method_type, n, irtype);
708 assert(n == n_parameters);
710 if(new_method_type != NULL)
711 ir_method_type = new_method_type;
713 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
714 ir_node *store = get_store();
715 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
717 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
720 type_t *result_type = function_type->result_type;
721 ir_node *result = NULL;
722 if(result_type != type_void) {
723 ir_mode *mode = get_ir_mode(result_type);
724 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
725 result = new_d_Proj(dbgi, resproj, mode, 0);
731 static ir_node *load_from_expression_addr(type_t *type, ir_node *addr,
734 ir_mode *mode = get_ir_mode(type);
735 ir_node *memory = get_store();
736 ir_node *load = new_d_Load(dbgi, memory, addr, mode);
737 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
738 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
744 static ir_node *expression_addr(const expression_t *expression)
747 panic("expression_addr not implemented yet");
751 static void set_value_for_expression(const expression_t *expression,
756 panic("set_value_for_expression not implemented yet");
759 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
761 ir_mode *value_mode = get_irn_mode(value);
763 if(value_mode == dest_mode)
766 if(dest_mode == mode_b) {
767 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
768 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
769 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
773 return new_d_Conv(dbgi, value, dest_mode);
776 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
778 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
779 type_t *type = expression->expression.datatype;
780 ir_mode *mode = get_ir_mode(type);
782 if(expression->type == UNEXPR_TAKE_ADDRESS)
783 return expression_addr(expression->value);
785 const expression_t *value = expression->value;
786 ir_node *value_node = expression_to_firm(value);
788 switch(expression->type) {
790 return new_d_Minus(dbgi, value_node, mode);
793 case UNEXPR_BITWISE_NEGATE:
794 return new_d_Not(dbgi, value_node, mode);
796 if(get_irn_mode(value_node) != mode_b) {
797 value_node = create_conv(dbgi, value_node, mode_b);
799 value_node = new_d_Not(dbgi, value_node, mode_b);
801 value_node = create_conv(dbgi, value_node, mode);
804 case UNEXPR_DEREFERENCE:
805 return load_from_expression_addr(type, value_node, dbgi);
806 case UNEXPR_POSTFIX_INCREMENT: {
807 ir_node *one = new_Const(mode, get_mode_one(mode));
808 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
809 set_value_for_expression(value, new_value);
812 case UNEXPR_POSTFIX_DECREMENT: {
813 ir_node *one = new_Const(mode, get_mode_one(mode));
814 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
815 set_value_for_expression(value, new_value);
818 case UNEXPR_PREFIX_INCREMENT: {
819 ir_node *one = new_Const(mode, get_mode_one(mode));
820 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
821 set_value_for_expression(value, new_value);
824 case UNEXPR_PREFIX_DECREMENT: {
825 ir_node *one = new_Const(mode, get_mode_one(mode));
826 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
827 set_value_for_expression(value, new_value);
831 return create_conv(dbgi, value_node, mode);
833 case UNEXPR_TAKE_ADDRESS:
837 panic("invalid UNEXPR type found");
840 static ir_node *expression_to_firm(const expression_t *expression)
842 switch(expression->type) {
844 return const_to_firm((const const_t*) expression);
845 case EXPR_STRING_LITERAL:
846 return string_literal_to_firm((const string_literal_t*) expression);
848 return reference_expression_to_firm((const reference_expression_t*)
851 return call_expression_to_firm((const call_expression_t*) expression);
853 return unary_expression_to_firm((const unary_expression_t*) expression);
857 panic("unsupported expression found");
862 static void statement_to_firm(statement_t *statement);
864 static void return_statement_to_firm(return_statement_t *statement)
866 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
869 if(statement->return_value != NULL) {
870 ir_node *retval = expression_to_firm(statement->return_value);
874 ret = new_d_Return(dbgi, get_store(), 1, in);
876 ret = new_d_Return(dbgi, get_store(), 0, NULL);
878 ir_node *end_block = get_irg_end_block(current_ir_graph);
879 add_immBlock_pred(end_block, ret);
884 static void compound_statement_to_firm(compound_statement_t *compound)
886 statement_t *statement = compound->statements;
887 for( ; statement != NULL; statement = statement->next) {
888 //context2firm(&statement->context);
889 statement_to_firm(statement);
893 static void expression_statement_to_firm(expression_statement_t *statement)
895 expression_to_firm(statement->expression);
898 static void if_statement_to_firm(if_statement_t *statement)
900 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
901 ir_node *condition = expression_to_firm(statement->condition);
902 assert(condition != NULL);
903 assert(get_irn_mode(condition) == mode_b);
905 ir_node *cond = new_d_Cond(dbgi, condition);
906 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
907 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
909 ir_node *fallthrough_block = new_immBlock();
911 /* the true (blocks) */
912 ir_node *true_block = new_immBlock();
913 add_immBlock_pred(true_block, true_proj);
914 mature_immBlock(true_block);
916 statement_to_firm(statement->true_statement);
917 if(get_cur_block() != NULL) {
918 ir_node *jmp = new_Jmp();
919 add_immBlock_pred(fallthrough_block, jmp);
922 /* the false (blocks) */
923 if(statement->false_statement != NULL) {
924 ir_node *false_block = new_immBlock();
925 add_immBlock_pred(false_block, false_proj);
926 mature_immBlock(false_block);
928 statement_to_firm(statement->false_statement);
929 if(get_cur_block() != NULL) {
930 ir_node *jmp = new_Jmp();
931 add_immBlock_pred(fallthrough_block, jmp);
934 add_immBlock_pred(fallthrough_block, false_proj);
936 mature_immBlock(fallthrough_block);
938 set_cur_block(fallthrough_block);
941 static void statement_to_firm(statement_t *statement)
943 switch(statement->type) {
944 case STATEMENT_COMPOUND:
945 compound_statement_to_firm((compound_statement_t*) statement);
947 case STATEMENT_RETURN:
948 return_statement_to_firm((return_statement_t*) statement);
950 case STATEMENT_EXPRESSION:
951 expression_statement_to_firm((expression_statement_t*) statement);
954 if_statement_to_firm((if_statement_t*) statement);
959 panic("Statement not implemented\n");
962 static int get_function_n_local_vars(declaration_t *declaration)
969 static void initialize_function_parameters(declaration_t *declaration)
972 declaration_t *parameter = declaration->context.declarations;
973 for( ; parameter != NULL; parameter = parameter->next) {
974 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
976 parameter->declaration_type = DECLARATION_TYPE_PARAMETER;
977 parameter->v.value_number = n;
982 static void create_function(declaration_t *declaration)
984 ir_entity *entity = get_function_entity(declaration);
986 if(declaration->init.statement == NULL)
989 initialize_function_parameters(declaration);
991 int n_local_vars = get_function_n_local_vars(declaration);
992 ir_graph *irg = new_ir_graph(entity, n_local_vars);
993 ir_node *first_block = get_cur_block();
995 statement_to_firm(declaration->init.statement);
997 ir_node *end_block = get_irg_end_block(irg);
999 /* do we have a return statement yet? */
1000 if(get_cur_block() != NULL) {
1001 ir_node *ret = new_Return(get_store(), 0, NULL);
1002 add_immBlock_pred(end_block, ret);
1005 mature_immBlock(first_block);
1006 mature_immBlock(end_block);
1008 irg_finalize_cons(irg);
1010 /* finalize the frame type */
1011 ir_type *frame_type = get_irg_frame_type(irg);
1012 int n = get_compound_n_members(frame_type);
1015 for(int i = 0; i < n; ++i) {
1016 ir_entity *entity = get_compound_member(frame_type, i);
1017 ir_type *entity_type = get_entity_type(entity);
1019 int align = get_type_alignment_bytes(entity_type);
1020 if(align > align_all)
1024 misalign = offset % align;
1028 set_entity_offset(entity, offset);
1029 offset += get_type_size_bytes(entity_type);
1031 set_type_size_bytes(frame_type, offset);
1032 set_type_alignment_bytes(frame_type, align_all);
1033 set_type_state(frame_type, layout_fixed);
1038 static void context_to_firm(context_t *context)
1040 declaration_t *declaration = context->declarations;
1041 for( ; declaration != NULL; declaration = declaration->next) {
1042 type_t *type = declaration->type;
1043 if(type->type == TYPE_FUNCTION) {
1044 create_function(declaration);
1051 void translation_unit_to_firm(translation_unit_t *unit)
1053 /* remove me later TODO FIXME */
1054 (void) get_type_size;
1056 context_to_firm(& unit->context);