rename method to function
[cparser] / ast2firm.c
1 #include <config.h>
2
3 #define _GNU_SOURCE
4
5 #include <assert.h>
6 #include <string.h>
7
8 #include <libfirm/firm.h>
9 #include <libfirm/adt/obst.h>
10
11 #include "adt/error.h"
12 #include "token_t.h"
13 #include "type_t.h"
14 #include "ast_t.h"
15
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;
20
21 static type_t *type_const_char;
22 static type_t *type_void;
23 static type_t *type_int;
24
25 typedef struct type2firm_env_t type2firm_env_t;
26 struct type2firm_env_t {
27         int can_cache;       /* nonzero if type can safely be cached because
28                                 no typevariables are in the hierarchy */
29 };
30
31 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type);
32 static ir_type *get_ir_type(type_t *type);
33
34 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
35 {
36         (void) pos;
37 #if 0
38         const declaration_t *declaration = & value_numbers[pos]->declaration;
39
40         print_warning_prefix(declaration->source_position);
41         fprintf(stderr, "variable '%s' might be used uninitialized\n",
42                         declaration->symbol->string);
43 #endif
44         fprintf(stderr, "Some variable might be used uninitialized\n");
45         return new_r_Unknown(irg, mode);
46 }
47
48 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
49 {
50         const source_position_t *pos = (const source_position_t*) dbg;
51         if(pos == NULL)
52                 return 0;
53         return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
54                                    pos->linenr);
55 }
56
57 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
58 {
59         const source_position_t *pos = (const source_position_t*) dbg;
60         if(pos == NULL)
61                 return NULL;
62         if(line != NULL)
63                 *line = pos->linenr;
64         return pos->input_name;
65 }
66
67 void init_ast2firm(void)
68 {
69         type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
70         type_void       = make_atomic_type(ATOMIC_TYPE_VOID, 0);
71         type_int        = make_atomic_type(ATOMIC_TYPE_INT, 0);
72
73         ir_type_int        = get_ir_type(type_int);
74         ir_type_const_char = get_ir_type(type_const_char);
75         ir_type_void       = get_ir_type(type_int); /* we don't have a real void
76                                                        type in firm */
77         ir_type_void_ptr   = new_type_pointer(new_id_from_str("void_ptr"),
78                                               ir_type_void, mode_P_data);
79 }
80
81 void exit_ast2firm(void)
82 {
83 }
84
85 static unsigned unique_id = 0;
86
87 static ident *unique_ident(const char *tag)
88 {
89         char buf[256];
90
91         snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
92         unique_id++;
93         return new_id_from_str(buf);
94 }
95
96 #if 0
97 static symbol_t *unique_symbol(const char *tag)
98 {
99         obstack_printf(&symbol_obstack, "%s.%d", tag, unique_id);
100         unique_id++;
101
102         const char *string = obstack_finish(&symbol_obstack);
103         symbol_t   *symbol = symbol_table_insert(string);
104
105         assert(symbol->string == string);
106
107         return symbol;
108 }
109 #endif
110
111 static type_t *skip_typeref(type_t *type)
112 {
113         while(1) {
114                 switch(type->type) {
115                 case TYPE_TYPEDEF: {
116                         const typedef_type_t *typedef_type = (const typedef_type_t*) type;
117                         type = typedef_type->declaration->type;
118                         continue;
119                 }
120                 case TYPE_TYPEOF: {
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;
124                         } else {
125                                 type = typeof_type->expression->datatype;
126                         }
127                         continue;
128                 }
129                 default:
130                         break;
131                 }
132                 break;
133         }
134
135         return type;
136 }
137
138 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
139 {
140         switch(atomic_type->atype) {
141         case ATOMIC_TYPE_SCHAR:
142         case ATOMIC_TYPE_CHAR:
143                 return mode_Bs;
144         case ATOMIC_TYPE_UCHAR:
145                 return mode_Bu;
146         case ATOMIC_TYPE_SHORT:
147                 return mode_Hs;
148         case ATOMIC_TYPE_USHORT:
149                 return mode_Hu;
150         case ATOMIC_TYPE_LONG:
151         case ATOMIC_TYPE_INT:
152                 return mode_Is;
153         case ATOMIC_TYPE_ULONG:
154         case ATOMIC_TYPE_UINT:
155                 return mode_Iu;
156         case ATOMIC_TYPE_LONGLONG:
157                 return mode_Ls;
158         case ATOMIC_TYPE_ULONGLONG:
159                 return mode_Lu;
160         case ATOMIC_TYPE_FLOAT:
161                 return mode_F;
162         case ATOMIC_TYPE_DOUBLE:
163                 return mode_D;
164         case ATOMIC_TYPE_LONG_DOUBLE:
165                 return mode_E;
166         case ATOMIC_TYPE_BOOL:
167                 return mode_b;
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");
173                 break;
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");
178                 break;
179 #endif
180         case ATOMIC_TYPE_VOID:
181                 panic("tried to get mode from void type");
182                 break;
183         case ATOMIC_TYPE_INVALID:
184                 break;
185         }
186         panic("Encountered unknown atomic type");
187 }
188
189
190 static unsigned get_type_size(type_t *type);
191
192 static unsigned get_atomic_type_size(const atomic_type_t *type)
193 {
194         switch(type->atype) {
195         case ATOMIC_TYPE_CHAR:
196         case ATOMIC_TYPE_SCHAR:
197         case ATOMIC_TYPE_UCHAR:
198                 return 1;
199
200         case ATOMIC_TYPE_SHORT:
201         case ATOMIC_TYPE_USHORT:
202                 return 2;
203
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:
210                 return 4;
211
212         case ATOMIC_TYPE_LONGLONG:
213         case ATOMIC_TYPE_ULONGLONG:
214         case ATOMIC_TYPE_DOUBLE:
215                 return 8;
216
217         case ATOMIC_TYPE_LONG_DOUBLE:
218                 return 12;
219
220         case ATOMIC_TYPE_VOID:
221                 return 1;
222
223         case ATOMIC_TYPE_INVALID:
224                 break;
225         }
226         panic("Trying to determine size of invalid atomic type");
227 }
228
229 static unsigned get_compound_type_size(compound_type_t *type)
230 {
231         ir_type *irtype = get_ir_type(&type->type);
232         return get_type_size_bytes(irtype);
233 }
234
235 static unsigned get_array_type_size(array_type_t *type)
236 {
237         ir_type *irtype = get_ir_type(&type->type);
238         return get_type_size_bytes(irtype);
239 }
240
241 static unsigned get_type_size(type_t *type)
242 {
243         type = skip_typeref(type);
244
245         switch(type->type) {
246         case TYPE_ATOMIC:
247                 return get_atomic_type_size((const atomic_type_t*) type);
248         case TYPE_ENUM:
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);
253         case TYPE_FUNCTION:
254                 /* just a pointer to the function */
255                 return get_mode_size_bytes(mode_P_code);
256         case TYPE_POINTER:
257                 return get_mode_size_bytes(mode_P_data);
258         case TYPE_ARRAY:
259                 return get_array_type_size((array_type_t*) type);
260         case TYPE_BUILTIN:
261         case TYPE_TYPEDEF:
262         case TYPE_TYPEOF:
263         case TYPE_INVALID:
264                 break;
265         }
266         panic("Trying to determine size of invalid type");
267 }
268
269 static unsigned count_parameters(const function_type_t *function_type)
270 {
271         unsigned count = 0;
272
273         function_parameter_t *parameter = function_type->parameters;
274         for ( ; parameter != NULL; parameter = parameter->next) {
275                 ++count;
276         }
277
278         return count;
279 }
280
281
282
283
284 static ir_type *get_atomic_type(type2firm_env_t *env, const atomic_type_t *type)
285 {
286         (void) env;
287         ir_mode *mode   = get_atomic_mode(type);
288         ident   *id     = get_mode_ident(mode);
289         ir_type *irtype = new_type_primitive(id, mode);
290
291         return irtype;
292 }
293
294 static ir_type *get_method_type(type2firm_env_t *env,
295                                 const function_type_t *function_type)
296 {
297         type_t  *result_type  = function_type->result_type;
298
299         ident   *id           = unique_ident("functiontype");
300         int      n_parameters = count_parameters(function_type);
301         int      n_results    = result_type == type_void ? 0 : 1;
302         ir_type *irtype       = new_type_method(id, n_parameters, n_results);
303
304         if(result_type != type_void) {
305                 ir_type *restype = _get_ir_type(env, result_type);
306                 set_method_res_type(irtype, 0, restype);
307         }
308
309         function_parameter_t *parameter = function_type->parameters;
310         int                   n         = 0;
311         for( ; parameter != NULL; parameter = parameter->next) {
312                 ir_type *p_irtype = _get_ir_type(env, parameter->type);
313                 set_method_param_type(irtype, n, p_irtype);
314                 ++n;
315         }
316
317         if(function_type->variadic) {
318                 set_method_variadicity(irtype, variadicity_variadic);
319         }
320
321         return irtype;
322 }
323
324 static ir_type *get_pointer_type(type2firm_env_t *env, pointer_type_t *type)
325 {
326         type_t  *points_to = type->points_to;
327         ir_type *ir_points_to;
328         /* Avoid endless recursion if the points_to type contains this poiner type
329          * again (might be a struct). We therefore first create a void* pointer
330          * and then set the real points_to type
331          */
332         ir_type *ir_type_void = get_ir_type(type_void);
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;
336
337         ir_points_to = _get_ir_type(env, points_to);
338         set_pointer_points_to_type(ir_type, ir_points_to);
339
340         return ir_type;
341 }
342
343 static ir_type *get_array_type(type2firm_env_t *env, array_type_t *type)
344 {
345         type_t  *element_type    = type->element_type;
346         ir_type *ir_element_type = _get_ir_type(env, element_type);
347
348         /* TODO... */
349         int n_elements = 0;
350         panic("TODO arraytpye size not implemented yet");
351
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);
354
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);
359         }
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);
363
364         return ir_type;
365 }
366
367 #define INVALID_TYPE ((ir_type_ptr)-1)
368
369 static ir_type *get_struct_type(type2firm_env_t *env, compound_type_t *type)
370 {
371         symbol_t *symbol = type->declaration->symbol;
372         ident    *id;
373         if(symbol != NULL) {
374                 id = unique_ident(symbol->string);
375         } else {
376                 id = unique_ident("__anonymous_struct");
377         }
378         ir_type *ir_type = new_type_struct(id);
379
380         type->type.firm_type = ir_type;
381
382         int align_all = 1;
383         int offset    = 0;
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);
388
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;
392                 offset += misalign;
393
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->entity = entity;
398
399                 offset += entry_size;
400                 if(entry_alignment > align_all) {
401                         if(entry_alignment % align_all != 0) {
402                                 panic("Uneven alignments not supported yet");
403                         }
404                         align_all = entry_alignment;
405                 }
406         }
407
408         int misalign = offset % align_all;
409         offset += misalign;
410         set_type_alignment_bytes(ir_type, align_all);
411         set_type_size_bytes(ir_type, offset);
412         set_type_state(ir_type, layout_fixed);
413
414         return ir_type;
415 }
416
417 static ir_type *get_union_type(type2firm_env_t *env, compound_type_t *type)
418 {
419         declaration_t *declaration = type->declaration;
420         symbol_t      *symbol      = declaration->symbol;
421         ident         *id;
422         if(symbol != NULL) {
423                 id = unique_ident(symbol->string);
424         } else {
425                 id = unique_ident("__anonymous_union");
426         }
427         ir_type  *ir_type = new_type_union(id);
428
429         type->type.firm_type = ir_type;
430
431         int align_all = 1;
432         int size      = 0;
433         declaration_t *entry = declaration->context.declarations;
434         for( ; entry != NULL; entry = entry->next) {
435                 ident       *ident         = new_id_from_str(entry->symbol->string);
436                 ir_type_ptr  entry_ir_type = _get_ir_type(env, entry->type);
437
438                 int entry_size      = get_type_size_bytes(entry_ir_type);
439                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
440
441                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
442                 add_union_member(ir_type, entity);
443                 set_entity_offset(entity, 0);
444                 entry->entity = entity;
445
446                 if(entry_size > size) {
447                         size = entry_size;
448                 }
449                 if(entry_alignment > align_all) {
450                         if(entry_alignment % align_all != 0) {
451                                 panic("Uneven alignments not supported yet");
452                         }
453                         align_all = entry_alignment;
454                 }
455         }
456
457         set_type_alignment_bytes(ir_type, align_all);
458         set_type_size_bytes(ir_type, size);
459         set_type_state(ir_type, layout_fixed);
460
461         return ir_type;
462 }
463
464 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type)
465 {
466         assert(type != NULL);
467
468         type = skip_typeref(type);
469
470         if(type->firm_type != NULL) {
471                 assert(type->firm_type != INVALID_TYPE);
472                 return type->firm_type;
473         }
474
475         ir_type *firm_type = NULL;
476         switch(type->type) {
477         case TYPE_ATOMIC:
478                 firm_type = get_atomic_type(env, (atomic_type_t*) type);
479                 break;
480         case TYPE_FUNCTION:
481                 firm_type = get_method_type(env, (function_type_t*) type);
482                 break;
483         case TYPE_POINTER:
484                 firm_type = get_pointer_type(env, (pointer_type_t*) type);
485                 break;
486         case TYPE_ARRAY:
487                 firm_type = get_array_type(env, (array_type_t*) type);
488                 break;
489         case TYPE_COMPOUND_STRUCT:
490                 firm_type = get_struct_type(env, (compound_type_t*) type);
491                 break;
492         case TYPE_COMPOUND_UNION:
493                 firm_type = get_union_type(env, (compound_type_t*) type);
494                 break;
495         case TYPE_ENUM:
496                 firm_type = ir_type_int;
497                 break;
498         case TYPE_BUILTIN:
499         case TYPE_TYPEOF:
500         case TYPE_TYPEDEF:
501         case TYPE_INVALID:
502                 break;
503         }
504         if(firm_type == NULL)
505                 panic("unknown type found");
506
507         if(env->can_cache) {
508                 type->firm_type = firm_type;
509         }
510         return firm_type;
511
512 }
513
514 static ir_type *get_ir_type(type_t *type)
515 {
516         type2firm_env_t env;
517         env.can_cache = 1;
518
519         return _get_ir_type(&env, type);
520 }
521
522 static inline ir_mode *get_ir_mode(type_t *type)
523 {
524         ir_type *irtype = get_ir_type(type);
525         ir_mode *mode   = get_type_mode(irtype);
526         assert(mode != NULL);
527         return mode;
528 }
529
530 static ir_entity* get_entity_function(declaration_t *declaration)
531 {
532         if(declaration->entity != NULL)
533                 return declaration->entity;
534
535         symbol_t *symbol = declaration->symbol;
536         ident    *id     = new_id_from_str(symbol->string);
537
538         ir_type  *global_type    = get_glob_type();
539         ir_type  *ir_type_method = get_ir_type(declaration->type);
540         assert(is_Method_type(ir_type_method));
541
542         type_t    *type   = declaration->type;
543         ir_entity *entity = new_entity(global_type, id, ir_type_method);
544         set_entity_ld_ident(entity, id);
545         if(declaration->storage_class & STORAGE_CLASS_STATIC
546                         || type->qualifiers & TYPE_QUALIFIER_INLINE) {
547                 set_entity_visibility(entity, visibility_local);
548         } else if(declaration->init.statement != NULL) {
549                 set_entity_visibility(entity, visibility_external_visible);
550         } else {
551                 set_entity_visibility(entity, visibility_external_allocated);
552         }
553
554         declaration->entity = entity;
555         return entity;
556 }
557
558
559
560 static dbg_info *get_dbg_info(const source_position_t *pos)
561 {
562         return (dbg_info*) pos;
563 }
564
565 static ir_node *const_to_firm(const const_t *cnst)
566 {
567         dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
568         ir_mode  *mode = get_ir_mode(cnst->expression.datatype);
569
570         tarval   *tv;
571         if(mode_is_float(mode)) {
572                 tv = new_tarval_from_double(cnst->v.float_value, mode);
573         } else {
574                 tv = new_tarval_from_long(cnst->v.int_value, mode);
575         }
576
577         return new_d_Const(dbgi, mode, tv);
578 }
579
580 static ir_node *string_literal_to_firm(const string_literal_t* literal)
581 {
582         ir_type   *global_type = get_glob_type();
583         ir_type   *type        = new_type_array(unique_ident("strtype"), 1,
584                                                 ir_type_const_char);
585
586         ir_entity *ent = new_entity(global_type, unique_ident("Lstr"), type);
587         set_entity_variability(ent, variability_constant);
588
589         ir_type    *elem_type = ir_type_const_char;
590         ir_mode    *mode      = get_type_mode(elem_type);
591
592         const char *string = literal->value;
593         size_t      slen   = strlen(string) + 1;
594
595         set_array_lower_bound_int(type, 0, 0);
596         set_array_upper_bound_int(type, 0, slen);
597         set_type_size_bytes(type, slen);
598         set_type_state(type, layout_fixed);
599
600         tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
601         for(size_t i = 0; i < slen; ++i) {
602                 tvs[i] = new_tarval_from_long(string[i], mode);
603         }
604
605         set_array_entity_values(ent, tvs, slen);
606         free(tvs);
607
608         dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
609
610         union symconst_symbol sym;
611         sym.entity_p = ent;
612         return new_d_SymConst(dbgi, sym, symconst_addr_ent);
613 }
614
615 #if 0
616 static ir_node *call_expression_to_firm(const call_expression_t *call)
617 {
618         expression_t  *function = call->function;
619         ir_node       *callee   = expression_to_firm(function);
620
621         assert(function->datatype->type == TYPE_FUNCTION);
622         pointer_type_t *pointer_type = (pointer_type_t*) function->datatype;
623         type_t         *points_to    = pointer_type->points_to;
624
625         assert(points_to->type == TYPE_FUNCTION);
626         method_type_t *method_type     = (method_type_t*) points_to;
627         ir_type       *ir_method_type  = get_ir_type((type_t*) method_type);
628         ir_type       *new_method_type = NULL;
629
630         int              n_parameters = 0;
631         call_argument_t *argument     = call->arguments;
632         while(argument != NULL) {
633                 n_parameters++;
634                 argument = argument->next;
635         }
636
637         if(method_type->variable_arguments) {
638                 /* we need to construct a new method type matching the call
639                  * arguments... */
640                 new_method_type = new_type_method(unique_ident("calltype"),
641                                                   n_parameters,
642                                                   get_method_n_ress(ir_method_type));
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));
647
648                 for(int i = 0; i < get_method_n_ress(ir_method_type); ++i) {
649                         set_method_res_type(new_method_type, i,
650                                             get_method_res_type(ir_method_type, i));
651                 }
652         }
653         ir_node *in[n_parameters];
654
655         argument = call->arguments;
656         int n = 0;
657         while(argument != NULL) {
658                 expression_t *expression = argument->expression;
659
660                 ir_node *arg_node = expression_to_firm(expression);
661
662                 in[n] = arg_node;
663                 if(new_method_type != NULL) {
664                         ir_type *irtype = get_ir_type(expression->datatype);
665                         set_method_param_type(new_method_type, n, irtype);
666                 }
667
668                 argument = argument->next;
669                 n++;
670         }
671
672         if(new_method_type != NULL)
673                 ir_method_type = new_method_type;
674
675         dbg_info *dbgi  = get_dbg_info(&call->expression.source_position);
676         ir_node  *store = get_store();
677         ir_node  *node  = new_d_Call(dbgi, store, callee, n_parameters, in,
678                                      ir_method_type);
679         ir_node  *mem   = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
680         set_store(mem);
681
682         type_t  *result_type = method_type->result_type;
683         ir_node *result      = NULL;
684         if(result_type->type != TYPE_VOID) {
685                 ir_mode *mode    = get_ir_mode(result_type);
686                 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
687                 result           = new_d_Proj(dbgi, resproj, mode, 0);
688         }
689
690         return result;
691 }
692 #endif
693
694 static ir_node *expression_to_firm(expression_t *expression)
695 {
696         switch(expression->type) {
697         case EXPR_CONST:
698                 return const_to_firm((const const_t*) expression);
699         case EXPR_STRING_LITERAL:
700                 return string_literal_to_firm((const string_literal_t*) expression);
701         default:
702                 break;
703         }
704         panic("unsupported expression found");
705 }
706
707
708
709 static void statement_to_firm(statement_t *statement);
710
711 static void return_statement_to_firm(return_statement_t *statement)
712 {
713         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
714         ir_node  *ret;
715
716         if(statement->return_value != NULL) {
717                 ir_node *retval = expression_to_firm(statement->return_value);
718                 ir_node *in[1];
719
720                 in[0] = retval;
721                 ret   = new_d_Return(dbgi, get_store(), 1, in);
722         } else {
723                 ret   = new_d_Return(dbgi, get_store(), 0, NULL);
724         }
725         ir_node *end_block = get_irg_end_block(current_ir_graph);
726         add_immBlock_pred(end_block, ret);
727
728         set_cur_block(NULL);
729 }
730
731 static void compound_statement_to_firm(compound_statement_t *compound)
732 {
733         statement_t *statement = compound->statements;
734         for( ; statement != NULL; statement = statement->next) {
735                 //context2firm(&statement->context);
736                 statement_to_firm(statement);
737         }
738 }
739
740 static void statement_to_firm(statement_t *statement)
741 {
742         switch(statement->type) {
743         case STATEMENT_COMPOUND:
744                 compound_statement_to_firm((compound_statement_t*) statement);
745                 return;
746         case STATEMENT_RETURN:
747                 return_statement_to_firm((return_statement_t*) statement);
748                 return;
749         default:
750                 break;
751         }
752         panic("Statement not implemented\n");
753 }
754
755 static int get_function_n_local_vars(declaration_t *declaration)
756 {
757         (void) declaration;
758         /* TODO */
759         return 30;
760 }
761
762 static void create_function(declaration_t *declaration)
763 {
764         ir_entity *entity = get_entity_function(declaration);
765
766         //context2firm(declaration->context);
767
768         if(declaration->init.statement == NULL)
769                 return;
770
771         int        n_local_vars = get_function_n_local_vars(declaration);
772         ir_graph  *irg          = new_ir_graph(entity, n_local_vars);
773         ir_node   *first_block  = get_cur_block();
774
775         statement_to_firm(declaration->init.statement);
776
777         ir_node *end_block = get_irg_end_block(irg);
778
779         /* do we have a return statement yet? */
780         if(get_cur_block() != NULL) {
781                 ir_node *ret = new_Return(get_store(), 0, NULL);
782                 add_immBlock_pred(end_block, ret);
783         }
784
785         mature_immBlock(first_block);
786         mature_immBlock(end_block);
787
788         irg_finalize_cons(irg);
789
790         /* finalize the frame type */
791         ir_type *frame_type = get_irg_frame_type(irg);
792         int      n          = get_compound_n_members(frame_type);
793         int      align_all  = 4;
794         int      offset     = 0;
795         for(int i = 0; i < n; ++i) {
796                 ir_entity *entity      = get_compound_member(frame_type, i);
797                 ir_type   *entity_type = get_entity_type(entity);
798
799                 int align = get_type_alignment_bytes(entity_type);
800                 if(align > align_all)
801                         align_all = align;
802                 int misalign = 0;
803                 if(align > 0) {
804                         misalign  = offset % align;
805                         offset   += misalign;
806                 }
807
808                 set_entity_offset(entity, offset);
809                 offset += get_type_size_bytes(entity_type);
810         }
811         set_type_size_bytes(frame_type, offset);
812         set_type_alignment_bytes(frame_type, align_all);
813         set_type_state(frame_type, layout_fixed);
814
815         irg_vrfy(irg);
816 }
817
818 static void context_to_firm(context_t *context)
819 {
820         declaration_t *declaration = context->declarations;
821         for( ; declaration != NULL; declaration = declaration->next) {
822                 type_t *type = declaration->type;
823                 if(type->type == TYPE_FUNCTION) {
824                         create_function(declaration);
825                 } else {
826                         /* TODO... */
827                 }
828         }
829 }
830
831 void translation_unit_to_firm(translation_unit_t *unit)
832 {
833         /* remove me later TODO FIXME */
834         (void) get_type_size;
835
836         context_to_firm(& unit->context);
837 }