8 #include <libfirm/firm.h>
9 #include <libfirm/adt/obst.h>
11 #include "adt/error.h"
16 static ir_type *ir_type_const_char;
17 static ir_type *ir_type_void;
18 static ir_type *ir_type_int;
19 static ir_type *ir_type_void_ptr;
21 static type_t *type_const_char;
22 static type_t *type_void;
23 static type_t *type_int;
25 typedef enum declaration_type_t {
26 DECLARATION_TYPE_UNKNOWN,
27 DECLARATION_TYPE_FUNCTION,
28 DECLARATION_TYPE_GLOBAL_VARIABLE,
29 DECLARATION_TYPE_PARAMETER,
30 DECLARATION_TYPE_PARAMETER_ENTITY,
31 DECLARATION_TYPE_LOCAL_VARIABLE,
32 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
33 DECLARATION_TYPE_COMPOUND_MEMBER
36 typedef struct type2firm_env_t type2firm_env_t;
37 struct type2firm_env_t {
38 int can_cache; /* nonzero if type can safely be cached because
39 no typevariables are in the hierarchy */
42 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type);
43 static ir_type *get_ir_type(type_t *type);
45 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
49 const declaration_t *declaration = & value_numbers[pos]->declaration;
51 print_warning_prefix(declaration->source_position);
52 fprintf(stderr, "variable '%s' might be used uninitialized\n",
53 declaration->symbol->string);
55 fprintf(stderr, "Some variable might be used uninitialized\n");
56 return new_r_Unknown(irg, mode);
59 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
61 const source_position_t *pos = (const source_position_t*) dbg;
64 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
68 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
70 const source_position_t *pos = (const source_position_t*) dbg;
75 return pos->input_name;
78 void init_ast2firm(void)
80 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
81 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
82 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
84 ir_type_int = get_ir_type(type_int);
85 ir_type_const_char = get_ir_type(type_const_char);
86 ir_type_void = get_ir_type(type_int); /* we don't have a real void
88 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
89 ir_type_void, mode_P_data);
91 type_void->firm_type = ir_type_void;
94 void exit_ast2firm(void)
98 static unsigned unique_id = 0;
100 static ident *unique_ident(const char *tag)
104 snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
106 return new_id_from_str(buf);
110 static symbol_t *unique_symbol(const char *tag)
112 obstack_printf(&symbol_obstack, "%s.%d", tag, unique_id);
115 const char *string = obstack_finish(&symbol_obstack);
116 symbol_t *symbol = symbol_table_insert(string);
118 assert(symbol->string == string);
124 static type_t *skip_typeref(type_t *type)
129 const typedef_type_t *typedef_type = (const typedef_type_t*) type;
130 type = typedef_type->declaration->type;
134 const typeof_type_t *typeof_type = (const typeof_type_t *) type;
135 if(typeof_type->typeof_type != NULL) {
136 type = typeof_type->typeof_type;
138 type = typeof_type->expression->datatype;
151 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
153 switch(atomic_type->atype) {
154 case ATOMIC_TYPE_SCHAR:
155 case ATOMIC_TYPE_CHAR:
157 case ATOMIC_TYPE_UCHAR:
159 case ATOMIC_TYPE_SHORT:
161 case ATOMIC_TYPE_USHORT:
163 case ATOMIC_TYPE_LONG:
164 case ATOMIC_TYPE_INT:
166 case ATOMIC_TYPE_ULONG:
167 case ATOMIC_TYPE_UINT:
169 case ATOMIC_TYPE_LONGLONG:
171 case ATOMIC_TYPE_ULONGLONG:
173 case ATOMIC_TYPE_FLOAT:
175 case ATOMIC_TYPE_DOUBLE:
177 case ATOMIC_TYPE_LONG_DOUBLE:
179 case ATOMIC_TYPE_BOOL:
181 #ifdef PROVIDE_COMPLEX
182 case ATOMIC_TYPE_FLOAT_COMPLEX:
183 case ATOMIC_TYPE_DOUBLE_COMPLEX:
184 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
185 panic("complex lowering not implemented yet");
187 case ATOMIC_TYPE_FLOAT_IMAGINARY:
188 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
189 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
190 panic("imaginary lowering not implemented yet");
193 case ATOMIC_TYPE_VOID:
194 panic("tried to get mode from void type");
196 case ATOMIC_TYPE_INVALID:
199 panic("Encountered unknown atomic type");
203 static unsigned get_type_size(type_t *type);
205 static unsigned get_atomic_type_size(const atomic_type_t *type)
207 switch(type->atype) {
208 case ATOMIC_TYPE_CHAR:
209 case ATOMIC_TYPE_SCHAR:
210 case ATOMIC_TYPE_UCHAR:
213 case ATOMIC_TYPE_SHORT:
214 case ATOMIC_TYPE_USHORT:
217 case ATOMIC_TYPE_BOOL:
218 case ATOMIC_TYPE_INT:
219 case ATOMIC_TYPE_UINT:
220 case ATOMIC_TYPE_LONG:
221 case ATOMIC_TYPE_ULONG:
222 case ATOMIC_TYPE_FLOAT:
225 case ATOMIC_TYPE_LONGLONG:
226 case ATOMIC_TYPE_ULONGLONG:
227 case ATOMIC_TYPE_DOUBLE:
230 case ATOMIC_TYPE_LONG_DOUBLE:
233 case ATOMIC_TYPE_VOID:
236 case ATOMIC_TYPE_INVALID:
239 panic("Trying to determine size of invalid atomic type");
242 static unsigned get_compound_type_size(compound_type_t *type)
244 ir_type *irtype = get_ir_type(&type->type);
245 return get_type_size_bytes(irtype);
248 static unsigned get_array_type_size(array_type_t *type)
250 ir_type *irtype = get_ir_type(&type->type);
251 return get_type_size_bytes(irtype);
254 static unsigned get_type_size(type_t *type)
256 type = skip_typeref(type);
260 return get_atomic_type_size((const atomic_type_t*) type);
262 return get_mode_size_bytes(mode_Is);
263 case TYPE_COMPOUND_UNION:
264 case TYPE_COMPOUND_STRUCT:
265 return get_compound_type_size((compound_type_t*) type);
267 /* just a pointer to the function */
268 return get_mode_size_bytes(mode_P_code);
270 return get_mode_size_bytes(mode_P_data);
272 return get_array_type_size((array_type_t*) type);
279 panic("Trying to determine size of invalid type");
282 static unsigned count_parameters(const function_type_t *function_type)
286 function_parameter_t *parameter = function_type->parameters;
287 for ( ; parameter != NULL; parameter = parameter->next) {
297 static ir_type *create_atomic_type(type2firm_env_t *env,
298 const atomic_type_t *type)
301 ir_mode *mode = get_atomic_mode(type);
302 ident *id = get_mode_ident(mode);
303 ir_type *irtype = new_type_primitive(id, mode);
308 static ir_type *create_method_type(type2firm_env_t *env,
309 const function_type_t *function_type)
311 type_t *result_type = function_type->result_type;
313 ident *id = unique_ident("functiontype");
314 int n_parameters = count_parameters(function_type);
315 int n_results = result_type == type_void ? 0 : 1;
316 ir_type *irtype = new_type_method(id, n_parameters, n_results);
318 if(result_type != type_void) {
319 ir_type *restype = _get_ir_type(env, result_type);
320 set_method_res_type(irtype, 0, restype);
323 function_parameter_t *parameter = function_type->parameters;
325 for( ; parameter != NULL; parameter = parameter->next) {
326 ir_type *p_irtype = _get_ir_type(env, parameter->type);
327 set_method_param_type(irtype, n, p_irtype);
331 if(function_type->variadic) {
332 set_method_variadicity(irtype, variadicity_variadic);
338 static ir_type *create_pointer_type(type2firm_env_t *env, pointer_type_t *type)
340 type_t *points_to = type->points_to;
341 ir_type *ir_points_to;
342 /* Avoid endless recursion if the points_to type contains this poiner type
343 * again (might be a struct). We therefore first create a void* pointer
344 * and then set the real points_to type
346 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
347 ir_type_void, mode_P_data);
348 type->type.firm_type = ir_type;
350 ir_points_to = _get_ir_type(env, points_to);
351 set_pointer_points_to_type(ir_type, ir_points_to);
356 static ir_type *create_array_type(type2firm_env_t *env, array_type_t *type)
358 type_t *element_type = type->element_type;
359 ir_type *ir_element_type = _get_ir_type(env, element_type);
363 panic("TODO arraytpye size not implemented yet");
365 ir_type *ir_type = new_type_array(unique_ident("array"), 1, ir_element_type);
366 set_array_bounds_int(ir_type, 0, 0, n_elements);
368 size_t elemsize = get_type_size_bytes(ir_element_type);
369 int align = get_type_alignment_bytes(ir_element_type);
370 if(elemsize % align > 0) {
371 elemsize += align - (elemsize % align);
373 set_type_size_bytes(ir_type, n_elements * elemsize);
374 set_type_alignment_bytes(ir_type, align);
375 set_type_state(ir_type, layout_fixed);
380 #define INVALID_TYPE ((ir_type_ptr)-1)
382 static ir_type *create_struct_type(type2firm_env_t *env, compound_type_t *type)
384 symbol_t *symbol = type->declaration->symbol;
387 id = unique_ident(symbol->string);
389 id = unique_ident("__anonymous_struct");
391 ir_type *ir_type = new_type_struct(id);
393 type->type.firm_type = ir_type;
397 declaration_t *entry = type->declaration->context.declarations;
398 for( ; entry != NULL; entry = entry->next) {
399 ident *ident = new_id_from_str(entry->symbol->string);
400 ir_type_ptr entry_ir_type = _get_ir_type(env, entry->type);
402 int entry_size = get_type_size_bytes(entry_ir_type);
403 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
404 int misalign = offset % entry_alignment;
407 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
408 set_entity_offset(entity, offset);
409 add_struct_member(ir_type, entity);
410 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
411 entry->v.entity = entity;
413 offset += entry_size;
414 if(entry_alignment > align_all) {
415 if(entry_alignment % align_all != 0) {
416 panic("Uneven alignments not supported yet");
418 align_all = entry_alignment;
422 int misalign = offset % align_all;
424 set_type_alignment_bytes(ir_type, align_all);
425 set_type_size_bytes(ir_type, offset);
426 set_type_state(ir_type, layout_fixed);
431 static ir_type *create_union_type(type2firm_env_t *env, compound_type_t *type)
433 declaration_t *declaration = type->declaration;
434 symbol_t *symbol = declaration->symbol;
437 id = unique_ident(symbol->string);
439 id = unique_ident("__anonymous_union");
441 ir_type *ir_type = new_type_union(id);
443 type->type.firm_type = ir_type;
447 declaration_t *entry = declaration->context.declarations;
448 for( ; entry != NULL; entry = entry->next) {
449 ident *ident = new_id_from_str(entry->symbol->string);
450 ir_type_ptr entry_ir_type = _get_ir_type(env, entry->type);
452 int entry_size = get_type_size_bytes(entry_ir_type);
453 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
455 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
456 add_union_member(ir_type, entity);
457 set_entity_offset(entity, 0);
458 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
459 entry->v.entity = entity;
461 if(entry_size > size) {
464 if(entry_alignment > align_all) {
465 if(entry_alignment % align_all != 0) {
466 panic("Uneven alignments not supported yet");
468 align_all = entry_alignment;
472 set_type_alignment_bytes(ir_type, align_all);
473 set_type_size_bytes(ir_type, size);
474 set_type_state(ir_type, layout_fixed);
479 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type)
481 assert(type != NULL);
483 type = skip_typeref(type);
485 if(type->firm_type != NULL) {
486 assert(type->firm_type != INVALID_TYPE);
487 return type->firm_type;
490 ir_type *firm_type = NULL;
493 firm_type = create_atomic_type(env, (atomic_type_t*) type);
496 firm_type = create_method_type(env, (function_type_t*) type);
499 firm_type = create_pointer_type(env, (pointer_type_t*) type);
502 firm_type = create_array_type(env, (array_type_t*) type);
504 case TYPE_COMPOUND_STRUCT:
505 firm_type = create_struct_type(env, (compound_type_t*) type);
507 case TYPE_COMPOUND_UNION:
508 firm_type = create_union_type(env, (compound_type_t*) type);
511 firm_type = ir_type_int;
519 if(firm_type == NULL)
520 panic("unknown type found");
523 type->firm_type = firm_type;
529 static ir_type *get_ir_type(type_t *type)
534 return _get_ir_type(&env, type);
537 static inline ir_mode *get_ir_mode(type_t *type)
539 ir_type *irtype = get_ir_type(type);
540 ir_mode *mode = get_type_mode(irtype);
541 assert(mode != NULL);
545 static ir_entity* get_function_entity(declaration_t *declaration)
547 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
548 return declaration->v.entity;
549 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
551 symbol_t *symbol = declaration->symbol;
552 ident *id = new_id_from_str(symbol->string);
554 ir_type *global_type = get_glob_type();
555 ir_type *ir_type_method = get_ir_type(declaration->type);
556 assert(is_Method_type(ir_type_method));
558 type_t *type = declaration->type;
559 ir_entity *entity = new_entity(global_type, id, ir_type_method);
560 set_entity_ld_ident(entity, id);
561 if(declaration->storage_class & STORAGE_CLASS_STATIC
562 || type->qualifiers & TYPE_QUALIFIER_INLINE) {
563 set_entity_visibility(entity, visibility_local);
564 } else if(declaration->init.statement != NULL) {
565 set_entity_visibility(entity, visibility_external_visible);
567 set_entity_visibility(entity, visibility_external_allocated);
570 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
571 declaration->v.entity = entity;
578 static ir_node *expression_to_firm(const expression_t *expression);
580 static dbg_info *get_dbg_info(const source_position_t *pos)
582 return (dbg_info*) pos;
585 static ir_node *const_to_firm(const const_t *cnst)
587 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
588 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
591 if(mode_is_float(mode)) {
592 tv = new_tarval_from_double(cnst->v.float_value, mode);
594 tv = new_tarval_from_long(cnst->v.int_value, mode);
597 return new_d_Const(dbgi, mode, tv);
600 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
602 assert(entity != NULL);
603 union symconst_symbol sym;
604 sym.entity_p = entity;
605 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
608 static ir_node *string_literal_to_firm(const string_literal_t* literal)
610 ir_type *global_type = get_glob_type();
611 ir_type *type = new_type_array(unique_ident("strtype"), 1,
614 ident *id = unique_ident("Lstr");
615 ir_entity *entity = new_entity(global_type, id, type);
616 set_entity_ld_ident(entity, id);
617 set_entity_variability(entity, variability_constant);
619 ir_type *elem_type = ir_type_const_char;
620 ir_mode *mode = get_type_mode(elem_type);
622 const char *string = literal->value;
623 size_t slen = strlen(string) + 1;
625 set_array_lower_bound_int(type, 0, 0);
626 set_array_upper_bound_int(type, 0, slen);
627 set_type_size_bytes(type, slen);
628 set_type_state(type, layout_fixed);
630 tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
631 for(size_t i = 0; i < slen; ++i) {
632 tvs[i] = new_tarval_from_long(string[i], mode);
635 set_array_entity_values(entity, tvs, slen);
638 dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
640 return create_symconst(dbgi, entity);
643 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
645 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
646 declaration_t *declaration = ref->declaration;
648 switch((declaration_type_t) declaration->declaration_type) {
649 case DECLARATION_TYPE_UNKNOWN:
651 case DECLARATION_TYPE_PARAMETER: {
652 ir_node *args = get_irg_args(current_ir_graph);
653 ir_mode *mode = get_ir_mode(declaration->type);
654 ir_node *block = get_irg_start_block(current_ir_graph);
655 long pn = declaration->v.value_number;
657 return new_r_Proj(current_ir_graph, block, args, mode, pn);
659 case DECLARATION_TYPE_FUNCTION: {
660 return create_symconst(dbgi, declaration->v.entity);
662 case DECLARATION_TYPE_PARAMETER_ENTITY:
663 case DECLARATION_TYPE_LOCAL_VARIABLE:
664 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
665 case DECLARATION_TYPE_GLOBAL_VARIABLE:
666 case DECLARATION_TYPE_COMPOUND_MEMBER:
667 panic("not implemented reference type");
670 panic("reference to declaration with unknown type found");
673 static ir_node *call_expression_to_firm(const call_expression_t *call)
675 expression_t *function = call->function;
676 ir_node *callee = expression_to_firm(function);
678 assert(function->datatype->type == TYPE_FUNCTION);
679 function_type_t *function_type = (function_type_t*) function->datatype;
681 int n_parameters = 0;
682 call_argument_t *argument = call->arguments;
683 for( ; argument != NULL; argument = argument->next) {
687 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
688 ir_type *new_method_type = NULL;
689 if(function_type->variadic) {
690 /* we need to construct a new method type matching the call
692 int n_res = get_method_n_ress(ir_method_type);
693 new_method_type = new_type_method(unique_ident("calltype"),
694 n_parameters, n_res);
695 set_method_calling_convention(new_method_type,
696 get_method_calling_convention(ir_method_type));
697 set_method_additional_properties(new_method_type,
698 get_method_additional_properties(ir_method_type));
700 for(int i = 0; i < n_res; ++i) {
701 set_method_res_type(new_method_type, i,
702 get_method_res_type(ir_method_type, i));
705 ir_node *in[n_parameters];
707 argument = call->arguments;
709 for( ; argument != NULL; argument = argument->next) {
710 expression_t *expression = argument->expression;
711 ir_node *arg_node = expression_to_firm(expression);
714 if(new_method_type != NULL) {
715 ir_type *irtype = get_ir_type(expression->datatype);
716 set_method_param_type(new_method_type, n, irtype);
721 assert(n == n_parameters);
723 if(new_method_type != NULL)
724 ir_method_type = new_method_type;
726 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
727 ir_node *store = get_store();
728 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
730 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
733 type_t *result_type = function_type->result_type;
734 ir_node *result = NULL;
735 if(result_type != type_void) {
736 ir_mode *mode = get_ir_mode(result_type);
737 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
738 result = new_d_Proj(dbgi, resproj, mode, 0);
744 static ir_node *load_from_expression_addr(type_t *type, ir_node *addr,
747 ir_mode *mode = get_ir_mode(type);
748 ir_node *memory = get_store();
749 ir_node *load = new_d_Load(dbgi, memory, addr, mode);
750 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
751 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
757 static ir_node *expression_addr(const expression_t *expression)
760 panic("expression_addr not implemented yet");
764 static void set_value_for_expression(const expression_t *expression,
769 panic("set_value_for_expression not implemented yet");
772 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
774 ir_mode *value_mode = get_irn_mode(value);
776 if(value_mode == dest_mode)
779 if(dest_mode == mode_b) {
780 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
781 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
782 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
786 return new_d_Conv(dbgi, value, dest_mode);
789 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
791 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
792 type_t *type = expression->expression.datatype;
793 ir_mode *mode = get_ir_mode(type);
795 if(expression->type == UNEXPR_TAKE_ADDRESS)
796 return expression_addr(expression->value);
798 const expression_t *value = expression->value;
799 ir_node *value_node = expression_to_firm(value);
801 switch(expression->type) {
803 return new_d_Minus(dbgi, value_node, mode);
806 case UNEXPR_BITWISE_NEGATE:
807 return new_d_Not(dbgi, value_node, mode);
809 if(get_irn_mode(value_node) != mode_b) {
810 value_node = create_conv(dbgi, value_node, mode_b);
812 value_node = new_d_Not(dbgi, value_node, mode_b);
814 value_node = create_conv(dbgi, value_node, mode);
817 case UNEXPR_DEREFERENCE:
818 return load_from_expression_addr(type, value_node, dbgi);
819 case UNEXPR_POSTFIX_INCREMENT: {
820 ir_node *one = new_Const(mode, get_mode_one(mode));
821 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
822 set_value_for_expression(value, new_value);
825 case UNEXPR_POSTFIX_DECREMENT: {
826 ir_node *one = new_Const(mode, get_mode_one(mode));
827 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
828 set_value_for_expression(value, new_value);
831 case UNEXPR_PREFIX_INCREMENT: {
832 ir_node *one = new_Const(mode, get_mode_one(mode));
833 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
834 set_value_for_expression(value, new_value);
837 case UNEXPR_PREFIX_DECREMENT: {
838 ir_node *one = new_Const(mode, get_mode_one(mode));
839 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
840 set_value_for_expression(value, new_value);
844 return create_conv(dbgi, value_node, mode);
846 case UNEXPR_TAKE_ADDRESS:
850 panic("invalid UNEXPR type found");
853 static ir_node *expression_to_firm(const expression_t *expression)
855 switch(expression->type) {
857 return const_to_firm((const const_t*) expression);
858 case EXPR_STRING_LITERAL:
859 return string_literal_to_firm((const string_literal_t*) expression);
861 return reference_expression_to_firm((const reference_expression_t*)
864 return call_expression_to_firm((const call_expression_t*) expression);
866 return unary_expression_to_firm((const unary_expression_t*) expression);
870 panic("unsupported expression found");
875 static void statement_to_firm(statement_t *statement);
877 static void return_statement_to_firm(return_statement_t *statement)
879 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
882 if(statement->return_value != NULL) {
883 ir_node *retval = expression_to_firm(statement->return_value);
887 ret = new_d_Return(dbgi, get_store(), 1, in);
889 ret = new_d_Return(dbgi, get_store(), 0, NULL);
891 ir_node *end_block = get_irg_end_block(current_ir_graph);
892 add_immBlock_pred(end_block, ret);
897 static void compound_statement_to_firm(compound_statement_t *compound)
899 statement_t *statement = compound->statements;
900 for( ; statement != NULL; statement = statement->next) {
901 //context2firm(&statement->context);
902 statement_to_firm(statement);
906 static void expression_statement_to_firm(expression_statement_t *statement)
908 expression_to_firm(statement->expression);
911 static void statement_to_firm(statement_t *statement)
913 switch(statement->type) {
914 case STATEMENT_COMPOUND:
915 compound_statement_to_firm((compound_statement_t*) statement);
917 case STATEMENT_RETURN:
918 return_statement_to_firm((return_statement_t*) statement);
920 case STATEMENT_EXPRESSION:
921 expression_statement_to_firm((expression_statement_t*) statement);
926 panic("Statement not implemented\n");
929 static int get_function_n_local_vars(declaration_t *declaration)
936 static void initialize_function_parameters(declaration_t *declaration)
939 declaration_t *parameter = declaration->context.declarations;
940 for( ; parameter != NULL; parameter = parameter->next) {
941 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
943 parameter->declaration_type = DECLARATION_TYPE_PARAMETER;
944 parameter->v.value_number = n;
949 static void create_function(declaration_t *declaration)
951 ir_entity *entity = get_function_entity(declaration);
953 if(declaration->init.statement == NULL)
956 initialize_function_parameters(declaration);
958 int n_local_vars = get_function_n_local_vars(declaration);
959 ir_graph *irg = new_ir_graph(entity, n_local_vars);
960 ir_node *first_block = get_cur_block();
962 statement_to_firm(declaration->init.statement);
964 ir_node *end_block = get_irg_end_block(irg);
966 /* do we have a return statement yet? */
967 if(get_cur_block() != NULL) {
968 ir_node *ret = new_Return(get_store(), 0, NULL);
969 add_immBlock_pred(end_block, ret);
972 mature_immBlock(first_block);
973 mature_immBlock(end_block);
975 irg_finalize_cons(irg);
977 /* finalize the frame type */
978 ir_type *frame_type = get_irg_frame_type(irg);
979 int n = get_compound_n_members(frame_type);
982 for(int i = 0; i < n; ++i) {
983 ir_entity *entity = get_compound_member(frame_type, i);
984 ir_type *entity_type = get_entity_type(entity);
986 int align = get_type_alignment_bytes(entity_type);
987 if(align > align_all)
991 misalign = offset % align;
995 set_entity_offset(entity, offset);
996 offset += get_type_size_bytes(entity_type);
998 set_type_size_bytes(frame_type, offset);
999 set_type_alignment_bytes(frame_type, align_all);
1000 set_type_state(frame_type, layout_fixed);
1005 static void context_to_firm(context_t *context)
1007 declaration_t *declaration = context->declarations;
1008 for( ; declaration != NULL; declaration = declaration->next) {
1009 type_t *type = declaration->type;
1010 if(type->type == TYPE_FUNCTION) {
1011 create_function(declaration);
1018 void translation_unit_to_firm(translation_unit_t *unit)
1020 /* remove me later TODO FIXME */
1021 (void) get_type_size;
1023 context_to_firm(& unit->context);