fix some goto/label bugs
[cparser] / ast2firm.c
1 #include <config.h>
2
3 #define _GNU_SOURCE
4
5 #include <assert.h>
6 #include <string.h>
7 #include <stdbool.h>
8
9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
11
12 #include "ast2firm.h"
13
14 #include "adt/error.h"
15 #include "adt/array.h"
16 #include "token_t.h"
17 #include "type_t.h"
18 #include "ast_t.h"
19
20 #define MAGIC_DEFAULT_PN_NUMBER     (long) -314159265
21
22 static ir_type *ir_type_const_char;
23 static ir_type *ir_type_void;
24 static ir_type *ir_type_int;
25 static ir_type *ir_type_void_ptr;
26
27 static type_t *type_const_char;
28 static type_t *type_void;
29 static type_t *type_int;
30
31 static int       next_value_number_function;
32 static ir_node  *continue_label;
33 static ir_node  *break_label;
34 static ir_node  *current_switch_cond;
35 static ir_node **imature_blocks;
36
37 typedef enum declaration_type_t {
38         DECLARATION_TYPE_UNKNOWN,
39         DECLARATION_TYPE_FUNCTION,
40         DECLARATION_TYPE_GLOBAL_VARIABLE,
41         DECLARATION_TYPE_LOCAL_VARIABLE,
42         DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
43         DECLARATION_TYPE_COMPOUND_MEMBER,
44         DECLARATION_TYPE_LABEL_BLOCK,
45 } declaration_type_t;
46
47 static ir_type *get_ir_type(type_t *type);
48
49 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
50 {
51         (void) pos;
52 #if 0
53         const declaration_t *declaration = & value_numbers[pos]->declaration;
54
55         print_warning_prefix(declaration->source_position);
56         fprintf(stderr, "variable '%s' might be used uninitialized\n",
57                         declaration->symbol->string);
58 #endif
59         fprintf(stderr, "Some variable might be used uninitialized\n");
60         return new_r_Unknown(irg, mode);
61 }
62
63 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
64 {
65         const source_position_t *pos = (const source_position_t*) dbg;
66         if(pos == NULL)
67                 return 0;
68         return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
69                                    pos->linenr);
70 }
71
72 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
73 {
74         const source_position_t *pos = (const source_position_t*) dbg;
75         if(pos == NULL)
76                 return NULL;
77         if(line != NULL)
78                 *line = pos->linenr;
79         return pos->input_name;
80 }
81
82 void init_ast2firm(void)
83 {
84         type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
85         type_void       = make_atomic_type(ATOMIC_TYPE_VOID, 0);
86         type_int        = make_atomic_type(ATOMIC_TYPE_INT, 0);
87
88         ir_type_int        = get_ir_type(type_int);
89         ir_type_const_char = get_ir_type(type_const_char);
90         ir_type_void       = get_ir_type(type_int); /* we don't have a real void
91                                                        type in firm */
92         ir_type_void_ptr   = new_type_pointer(new_id_from_str("void_ptr"),
93                                               ir_type_void, mode_P_data);
94
95         type_void->firm_type = ir_type_void;
96 }
97
98 void exit_ast2firm(void)
99 {
100 }
101
102 static unsigned unique_id = 0;
103
104 static ident *unique_ident(const char *tag)
105 {
106         char buf[256];
107
108         snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
109         unique_id++;
110         return new_id_from_str(buf);
111 }
112
113 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
114 {
115         switch(atomic_type->atype) {
116         case ATOMIC_TYPE_SCHAR:
117         case ATOMIC_TYPE_CHAR:
118                 return mode_Bs;
119         case ATOMIC_TYPE_UCHAR:
120                 return mode_Bu;
121         case ATOMIC_TYPE_SHORT:
122                 return mode_Hs;
123         case ATOMIC_TYPE_USHORT:
124                 return mode_Hu;
125         case ATOMIC_TYPE_LONG:
126         case ATOMIC_TYPE_INT:
127                 return mode_Is;
128         case ATOMIC_TYPE_ULONG:
129         case ATOMIC_TYPE_UINT:
130                 return mode_Iu;
131         case ATOMIC_TYPE_LONGLONG:
132                 return mode_Ls;
133         case ATOMIC_TYPE_ULONGLONG:
134                 return mode_Lu;
135         case ATOMIC_TYPE_FLOAT:
136                 return mode_F;
137         case ATOMIC_TYPE_DOUBLE:
138                 return mode_D;
139         case ATOMIC_TYPE_LONG_DOUBLE:
140                 return mode_E;
141         case ATOMIC_TYPE_BOOL:
142                 return mode_b;
143 #ifdef PROVIDE_COMPLEX
144         case ATOMIC_TYPE_FLOAT_COMPLEX:
145         case ATOMIC_TYPE_DOUBLE_COMPLEX:
146         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
147                 panic("complex lowering not implemented yet");
148                 break;
149         case ATOMIC_TYPE_FLOAT_IMAGINARY:
150         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
151         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
152                 panic("imaginary lowering not implemented yet");
153                 break;
154 #endif
155         case ATOMIC_TYPE_VOID:
156                 /* firm has no real void... */
157                 return mode_Is;
158         case ATOMIC_TYPE_INVALID:
159                 break;
160         }
161         panic("Encountered unknown atomic type");
162 }
163
164
165 static unsigned get_type_size(type_t *type);
166
167 static unsigned get_atomic_type_size(const atomic_type_t *type)
168 {
169         switch(type->atype) {
170         case ATOMIC_TYPE_CHAR:
171         case ATOMIC_TYPE_SCHAR:
172         case ATOMIC_TYPE_UCHAR:
173                 return 1;
174
175         case ATOMIC_TYPE_SHORT:
176         case ATOMIC_TYPE_USHORT:
177                 return 2;
178
179         case ATOMIC_TYPE_BOOL:
180         case ATOMIC_TYPE_INT:
181         case ATOMIC_TYPE_UINT:
182         case ATOMIC_TYPE_LONG:
183         case ATOMIC_TYPE_ULONG:
184         case ATOMIC_TYPE_FLOAT:
185                 return 4;
186
187         case ATOMIC_TYPE_LONGLONG:
188         case ATOMIC_TYPE_ULONGLONG:
189         case ATOMIC_TYPE_DOUBLE:
190                 return 8;
191
192         case ATOMIC_TYPE_LONG_DOUBLE:
193                 return 12;
194
195         case ATOMIC_TYPE_VOID:
196                 return 1;
197
198         case ATOMIC_TYPE_INVALID:
199                 break;
200         }
201         panic("Trying to determine size of invalid atomic type");
202 }
203
204 static unsigned get_compound_type_size(compound_type_t *type)
205 {
206         ir_type *irtype = get_ir_type(&type->type);
207         return get_type_size_bytes(irtype);
208 }
209
210 static unsigned get_array_type_size(array_type_t *type)
211 {
212         ir_type *irtype = get_ir_type(&type->type);
213         return get_type_size_bytes(irtype);
214 }
215
216 static unsigned get_type_size(type_t *type)
217 {
218         type = skip_typeref(type);
219
220         switch(type->type) {
221         case TYPE_ATOMIC:
222                 return get_atomic_type_size((const atomic_type_t*) type);
223         case TYPE_ENUM:
224                 return get_mode_size_bytes(mode_Is);
225         case TYPE_COMPOUND_UNION:
226         case TYPE_COMPOUND_STRUCT:
227                 return get_compound_type_size((compound_type_t*) type);
228         case TYPE_FUNCTION:
229                 /* just a pointer to the function */
230                 return get_mode_size_bytes(mode_P_code);
231         case TYPE_POINTER:
232                 return get_mode_size_bytes(mode_P_data);
233         case TYPE_ARRAY:
234                 return get_array_type_size((array_type_t*) type);
235         case TYPE_BUILTIN:
236         case TYPE_TYPEDEF:
237         case TYPE_TYPEOF:
238         case TYPE_INVALID:
239                 break;
240         }
241         panic("Trying to determine size of invalid type");
242 }
243
244 static unsigned count_parameters(const function_type_t *function_type)
245 {
246         unsigned count = 0;
247
248         function_parameter_t *parameter = function_type->parameters;
249         for ( ; parameter != NULL; parameter = parameter->next) {
250                 ++count;
251         }
252
253         return count;
254 }
255
256
257
258
259 static ir_type *create_atomic_type(const atomic_type_t *type)
260 {
261         ir_mode *mode   = get_atomic_mode(type);
262         ident   *id     = get_mode_ident(mode);
263         ir_type *irtype = new_type_primitive(id, mode);
264
265         return irtype;
266 }
267
268 static ir_type *create_method_type(const function_type_t *function_type)
269 {
270         type_t  *result_type  = function_type->result_type;
271
272         ident   *id           = unique_ident("functiontype");
273         int      n_parameters = count_parameters(function_type);
274         int      n_results    = result_type == type_void ? 0 : 1;
275         ir_type *irtype       = new_type_method(id, n_parameters, n_results);
276
277         if(result_type != type_void) {
278                 ir_type *restype = get_ir_type(result_type);
279                 set_method_res_type(irtype, 0, restype);
280         }
281
282         function_parameter_t *parameter = function_type->parameters;
283         int                   n         = 0;
284         for( ; parameter != NULL; parameter = parameter->next) {
285                 ir_type *p_irtype = get_ir_type(parameter->type);
286                 set_method_param_type(irtype, n, p_irtype);
287                 ++n;
288         }
289
290         if(function_type->variadic || function_type->unspecified_parameters) {
291                 set_method_variadicity(irtype, variadicity_variadic);
292         }
293
294         return irtype;
295 }
296
297 static ir_type *create_pointer_type(pointer_type_t *type)
298 {
299         type_t  *points_to = type->points_to;
300         ir_type *ir_points_to;
301         /* Avoid endless recursion if the points_to type contains this poiner type
302          * again (might be a struct). We therefore first create a void* pointer
303          * and then set the real points_to type
304          */
305         ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
306                                             ir_type_void, mode_P_data);
307         type->type.firm_type  = ir_type;
308
309         ir_points_to = get_ir_type(points_to);
310         set_pointer_points_to_type(ir_type, ir_points_to);
311
312         return ir_type;
313 }
314
315 static ir_type *create_array_type(array_type_t *type)
316 {
317         type_t  *element_type    = type->element_type;
318         ir_type *ir_element_type = get_ir_type(element_type);
319
320         /* TODO... */
321         int n_elements = 0;
322         panic("TODO arraytpye size not implemented yet");
323
324         ir_type *ir_type = new_type_array(unique_ident("array"), 1, ir_element_type);
325         set_array_bounds_int(ir_type, 0, 0, n_elements);
326
327         size_t elemsize = get_type_size_bytes(ir_element_type);
328         int align = get_type_alignment_bytes(ir_element_type);
329         if(elemsize % align > 0) {
330                 elemsize += align - (elemsize % align);
331         }
332         set_type_size_bytes(ir_type, n_elements * elemsize);
333         set_type_alignment_bytes(ir_type, align);
334         set_type_state(ir_type, layout_fixed);
335
336         return ir_type;
337 }
338
339 #define INVALID_TYPE ((ir_type_ptr)-1)
340
341 static ir_type *create_struct_type(compound_type_t *type)
342 {
343         symbol_t *symbol = type->declaration->symbol;
344         ident    *id;
345         if(symbol != NULL) {
346                 id = unique_ident(symbol->string);
347         } else {
348                 id = unique_ident("__anonymous_struct");
349         }
350         ir_type *ir_type = new_type_struct(id);
351
352         type->type.firm_type = ir_type;
353
354         int align_all = 1;
355         int offset    = 0;
356         declaration_t *entry = type->declaration->context.declarations;
357         for( ; entry != NULL; entry = entry->next) {
358                 if(entry->namespace != NAMESPACE_NORMAL)
359                         continue;
360
361                 ident       *ident         = new_id_from_str(entry->symbol->string);
362                 ir_type_ptr  entry_ir_type = get_ir_type(entry->type);
363
364                 int entry_size      = get_type_size_bytes(entry_ir_type);
365                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
366                 int misalign = offset % entry_alignment;
367                 offset += misalign;
368
369                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
370                 set_entity_offset(entity, offset);
371                 add_struct_member(ir_type, entity);
372                 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
373                 entry->v.entity         = entity;
374
375                 offset += entry_size;
376                 if(entry_alignment > align_all) {
377                         if(entry_alignment % align_all != 0) {
378                                 panic("Uneven alignments not supported yet");
379                         }
380                         align_all = entry_alignment;
381                 }
382         }
383
384         int misalign = offset % align_all;
385         offset += misalign;
386         set_type_alignment_bytes(ir_type, align_all);
387         set_type_size_bytes(ir_type, offset);
388         set_type_state(ir_type, layout_fixed);
389
390         return ir_type;
391 }
392
393 static ir_type *create_union_type(compound_type_t *type)
394 {
395         declaration_t *declaration = type->declaration;
396         symbol_t      *symbol      = declaration->symbol;
397         ident         *id;
398         if(symbol != NULL) {
399                 id = unique_ident(symbol->string);
400         } else {
401                 id = unique_ident("__anonymous_union");
402         }
403         ir_type  *ir_type = new_type_union(id);
404
405         type->type.firm_type = ir_type;
406
407         int align_all = 1;
408         int size      = 0;
409         declaration_t *entry = declaration->context.declarations;
410         for( ; entry != NULL; entry = entry->next) {
411                 if(entry->namespace != NAMESPACE_NORMAL)
412                         continue;
413
414                 ident       *ident         = new_id_from_str(entry->symbol->string);
415                 ir_type_ptr  entry_ir_type = get_ir_type(entry->type);
416
417                 int entry_size      = get_type_size_bytes(entry_ir_type);
418                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
419
420                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
421                 add_union_member(ir_type, entity);
422                 set_entity_offset(entity, 0);
423                 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
424                 entry->v.entity         = entity;
425
426                 if(entry_size > size) {
427                         size = entry_size;
428                 }
429                 if(entry_alignment > align_all) {
430                         if(entry_alignment % align_all != 0) {
431                                 panic("Uneven alignments not supported yet");
432                         }
433                         align_all = entry_alignment;
434                 }
435         }
436
437         set_type_alignment_bytes(ir_type, align_all);
438         set_type_size_bytes(ir_type, size);
439         set_type_state(ir_type, layout_fixed);
440
441         return ir_type;
442 }
443
444 static ir_type *get_ir_type(type_t *type)
445 {
446         assert(type != NULL);
447
448         type = skip_typeref(type);
449
450         if(type->firm_type != NULL) {
451                 assert(type->firm_type != INVALID_TYPE);
452                 return type->firm_type;
453         }
454
455         ir_type *firm_type = NULL;
456         switch(type->type) {
457         case TYPE_ATOMIC:
458                 firm_type = create_atomic_type((atomic_type_t*) type);
459                 break;
460         case TYPE_FUNCTION:
461                 firm_type = create_method_type((function_type_t*) type);
462                 break;
463         case TYPE_POINTER:
464                 firm_type = create_pointer_type((pointer_type_t*) type);
465                 break;
466         case TYPE_ARRAY:
467                 firm_type = create_array_type((array_type_t*) type);
468                 break;
469         case TYPE_COMPOUND_STRUCT:
470                 firm_type = create_struct_type((compound_type_t*) type);
471                 break;
472         case TYPE_COMPOUND_UNION:
473                 firm_type = create_union_type((compound_type_t*) type);
474                 break;
475         case TYPE_ENUM:
476                 firm_type = ir_type_int;
477                 break;
478         case TYPE_BUILTIN:
479         case TYPE_TYPEOF:
480         case TYPE_TYPEDEF:
481         case TYPE_INVALID:
482                 break;
483         }
484         if(firm_type == NULL)
485                 panic("unknown type found");
486
487         type->firm_type = firm_type;
488         return firm_type;
489 }
490
491 static inline ir_mode *get_ir_mode(type_t *type)
492 {
493         ir_type *irtype = get_ir_type(type);
494         ir_mode *mode   = get_type_mode(irtype);
495         assert(mode != NULL);
496         return mode;
497 }
498
499 static ir_entity* get_function_entity(declaration_t *declaration)
500 {
501         if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
502                 return declaration->v.entity;
503         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
504
505         symbol_t *symbol = declaration->symbol;
506         ident    *id     = new_id_from_str(symbol->string);
507
508         ir_type  *global_type    = get_glob_type();
509         ir_type  *ir_type_method = get_ir_type(declaration->type);
510         assert(is_Method_type(ir_type_method));
511
512         type_t    *type   = declaration->type;
513         ir_entity *entity = new_entity(global_type, id, ir_type_method);
514         set_entity_ld_ident(entity, id);
515         if(declaration->storage_class & STORAGE_CLASS_STATIC
516                         || type->qualifiers & TYPE_QUALIFIER_INLINE) {
517                 set_entity_visibility(entity, visibility_local);
518         } else if(declaration->init.statement != NULL) {
519                 set_entity_visibility(entity, visibility_external_visible);
520         } else {
521                 set_entity_visibility(entity, visibility_external_allocated);
522         }
523
524         declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
525         declaration->v.entity         = entity;
526
527         return entity;
528 }
529
530
531
532 static ir_node *expression_to_firm(const expression_t *expression);
533 static ir_node *expression_to_modeb(const expression_t *expression);
534
535 static dbg_info *get_dbg_info(const source_position_t *pos)
536 {
537         return (dbg_info*) pos;
538 }
539
540 static ir_node *const_to_firm(const const_t *cnst)
541 {
542         dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
543         ir_mode  *mode = get_ir_mode(cnst->expression.datatype);
544
545         tarval   *tv;
546         if(mode_is_float(mode)) {
547                 tv = new_tarval_from_double(cnst->v.float_value, mode);
548         } else {
549                 tv = new_tarval_from_long(cnst->v.int_value, mode);
550         }
551
552         return new_d_Const(dbgi, mode, tv);
553 }
554
555 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
556 {
557         assert(entity != NULL);
558         union symconst_symbol sym;
559         sym.entity_p = entity;
560         return new_d_SymConst(dbgi, sym, symconst_addr_ent);
561 }
562
563 static ir_node *string_literal_to_firm(const string_literal_t* literal)
564 {
565         ir_type   *global_type = get_glob_type();
566         ir_type   *type        = new_type_array(unique_ident("strtype"), 1,
567                                                 ir_type_const_char);
568
569         ident     *id     = unique_ident("Lstr");
570         ir_entity *entity = new_entity(global_type, id, type);
571         set_entity_ld_ident(entity, id);
572         set_entity_variability(entity, variability_constant);
573
574         ir_type    *elem_type = ir_type_const_char;
575         ir_mode    *mode      = get_type_mode(elem_type);
576
577         const char *string = literal->value;
578         size_t      slen   = strlen(string) + 1;
579
580         set_array_lower_bound_int(type, 0, 0);
581         set_array_upper_bound_int(type, 0, slen);
582         set_type_size_bytes(type, slen);
583         set_type_state(type, layout_fixed);
584
585         tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
586         for(size_t i = 0; i < slen; ++i) {
587                 tvs[i] = new_tarval_from_long(string[i], mode);
588         }
589
590         set_array_entity_values(entity, tvs, slen);
591         free(tvs);
592
593         dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
594
595         return create_symconst(dbgi, entity);
596 }
597
598 static ir_node *load_from_expression_addr(type_t *type, ir_node *addr,
599                                           dbg_info *dbgi)
600 {
601         ir_mode *mode     = get_ir_mode(type);
602         ir_node *memory   = get_store();
603         ir_node *load     = new_d_Load(dbgi, memory, addr, mode);
604         ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
605         ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
606         set_store(load_mem);
607
608         return load_res;
609 }
610
611 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
612 {
613         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
614         declaration_t *declaration = ref->declaration;
615         type_t        *type        = declaration->type;
616         ir_mode       *mode        = get_ir_mode(type);
617
618         switch((declaration_type_t) declaration->declaration_type) {
619         case DECLARATION_TYPE_UNKNOWN:
620                 break;
621         case DECLARATION_TYPE_LOCAL_VARIABLE:
622                 return get_value(declaration->v.value_number, mode);
623         case DECLARATION_TYPE_FUNCTION: {
624                 return create_symconst(dbgi, declaration->v.entity);
625         }
626         case DECLARATION_TYPE_GLOBAL_VARIABLE: {
627                 ir_entity *entity   = declaration->v.entity;
628                 ir_node   *symconst = create_symconst(dbgi, entity);
629                 return load_from_expression_addr(type, symconst, dbgi);
630         }
631         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
632         case DECLARATION_TYPE_COMPOUND_MEMBER:
633         case DECLARATION_TYPE_LABEL_BLOCK:
634                 panic("not implemented reference type");
635         }
636
637         panic("reference to declaration with unknown type found");
638 }
639
640 static ir_node *reference_addr(const reference_expression_t *ref)
641 {
642         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
643         declaration_t *declaration = ref->declaration;
644
645         switch((declaration_type_t) declaration->declaration_type) {
646         case DECLARATION_TYPE_UNKNOWN:
647                 break;
648         case DECLARATION_TYPE_LOCAL_VARIABLE:
649                 panic("local variable without entity has no address");
650         case DECLARATION_TYPE_FUNCTION: {
651                 return create_symconst(dbgi, declaration->v.entity);
652         }
653         case DECLARATION_TYPE_GLOBAL_VARIABLE: {
654                 ir_entity *entity   = declaration->v.entity;
655                 ir_node   *symconst = create_symconst(dbgi, entity);
656                 return symconst;
657         }
658         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
659         case DECLARATION_TYPE_COMPOUND_MEMBER:
660         case DECLARATION_TYPE_LABEL_BLOCK:
661                 panic("not implemented reference type");
662         }
663
664         panic("reference to declaration with unknown type found");
665 }
666
667 static ir_node *call_expression_to_firm(const call_expression_t *call)
668 {
669         assert(get_cur_block() != NULL);
670
671         expression_t  *function = call->function;
672         ir_node       *callee   = expression_to_firm(function);
673
674         assert(function->datatype->type == TYPE_FUNCTION);
675         function_type_t *function_type = (function_type_t*) function->datatype;
676
677         int              n_parameters = 0;
678         call_argument_t *argument     = call->arguments;
679         for( ; argument != NULL; argument = argument->next) {
680                 ++n_parameters;
681         }
682
683         ir_type *ir_method_type  = get_ir_type((type_t*) function_type);
684         ir_type *new_method_type = NULL;
685         if(function_type->variadic || function_type->unspecified_parameters) {
686                 /* we need to construct a new method type matching the call
687                  * arguments... */
688                 int n_res       = get_method_n_ress(ir_method_type);
689                 new_method_type = new_type_method(unique_ident("calltype"),
690                                                   n_parameters, n_res);
691                 set_method_calling_convention(new_method_type,
692                                get_method_calling_convention(ir_method_type));
693                 set_method_additional_properties(new_method_type,
694                                get_method_additional_properties(ir_method_type));
695
696                 for(int i = 0; i < n_res; ++i) {
697                         set_method_res_type(new_method_type, i,
698                                             get_method_res_type(ir_method_type, i));
699                 }
700         }
701         ir_node *in[n_parameters];
702
703         argument = call->arguments;
704         int n = 0;
705         for( ; argument != NULL; argument = argument->next) {
706                 expression_t *expression = argument->expression;
707                 ir_node      *arg_node   = expression_to_firm(expression);
708
709                 in[n] = arg_node;
710                 if(new_method_type != NULL) {
711                         ir_type *irtype = get_ir_type(expression->datatype);
712                         set_method_param_type(new_method_type, n, irtype);
713                 }
714
715                 n++;
716         }
717         assert(n == n_parameters);
718
719         if(new_method_type != NULL)
720                 ir_method_type = new_method_type;
721
722         dbg_info *dbgi  = get_dbg_info(&call->expression.source_position);
723         ir_node  *store = get_store();
724         ir_node  *node  = new_d_Call(dbgi, store, callee, n_parameters, in,
725                                      ir_method_type);
726         ir_node  *mem   = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
727         set_store(mem);
728
729         type_t  *result_type = function_type->result_type;
730         ir_node *result      = NULL;
731         if(result_type != type_void) {
732                 ir_mode *mode    = get_ir_mode(result_type);
733                 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
734                 result           = new_d_Proj(dbgi, resproj, mode, 0);
735         }
736
737         return result;
738 }
739
740 static ir_node *expression_to_addr(const expression_t *expression);
741
742 static void set_value_for_expression(const expression_t *expression,
743                                      ir_node *value)
744 {
745         if(expression->type == EXPR_REFERENCE) {
746                 reference_expression_t *ref = (reference_expression_t*) expression;
747
748                 declaration_t *declaration = ref->declaration;
749                 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
750                 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
751                         set_value(declaration->v.value_number, value);
752                         return;
753                 }
754         }
755
756         dbg_info *dbgi      = get_dbg_info(&expression->source_position);
757         ir_node  *addr      = expression_to_addr(expression);
758         assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
759         ir_node  *memory    = get_store();
760         ir_node  *store     = new_d_Store(dbgi, memory, addr, value);
761         ir_node  *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
762         set_store(store_mem);
763 }
764
765 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
766 {
767         ir_mode *value_mode = get_irn_mode(value);
768
769         if(value_mode == dest_mode)
770                 return value;
771
772         if(dest_mode == mode_b) {
773                 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
774                 ir_node *cmp  = new_d_Cmp(dbgi, value, zero);
775                 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
776                 return proj;
777         }
778
779         return new_d_Conv(dbgi, value, dest_mode);
780 }
781
782 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
783 {
784         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
785         type_t   *type = expression->expression.datatype;
786         ir_mode  *mode = get_ir_mode(type);
787
788         if(expression->type == UNEXPR_TAKE_ADDRESS)
789                 return expression_to_addr(expression->value);
790
791         const expression_t *value      = expression->value;
792         ir_node            *value_node = expression_to_firm(value);
793
794         switch(expression->type) {
795         case UNEXPR_NEGATE:
796                 return new_d_Minus(dbgi, value_node, mode);
797         case UNEXPR_PLUS:
798                 return value_node;
799         case UNEXPR_BITWISE_NEGATE:
800                 return new_d_Not(dbgi, value_node, mode);
801         case UNEXPR_NOT:
802                 if(get_irn_mode(value_node) != mode_b) {
803                         value_node = create_conv(dbgi, value_node, mode_b);
804                 }
805                 value_node = new_d_Not(dbgi, value_node, mode_b);
806                 if(mode != mode_b) {
807                         value_node = create_conv(dbgi, value_node, mode);
808                 }
809                 return value_node;
810         case UNEXPR_DEREFERENCE:
811                 return load_from_expression_addr(type, value_node, dbgi);
812         case UNEXPR_POSTFIX_INCREMENT: {
813                 ir_node *one       = new_Const(mode, get_mode_one(mode));
814                 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
815                 set_value_for_expression(value, new_value);
816                 return value_node;
817         }
818         case UNEXPR_POSTFIX_DECREMENT: {
819                 ir_node *one       = new_Const(mode, get_mode_one(mode));
820                 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
821                 set_value_for_expression(value, new_value);
822                 return value_node;
823         }
824         case UNEXPR_PREFIX_INCREMENT: {
825                 ir_node *one       = new_Const(mode, get_mode_one(mode));
826                 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
827                 set_value_for_expression(value, new_value);
828                 return new_value;
829         }
830         case UNEXPR_PREFIX_DECREMENT: {
831                 ir_node *one       = new_Const(mode, get_mode_one(mode));
832                 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
833                 set_value_for_expression(value, new_value);
834                 return new_value;
835         }
836         case UNEXPR_CAST:
837                 return create_conv(dbgi, value_node, mode);
838
839         case UNEXPR_TAKE_ADDRESS:
840         case UNEXPR_INVALID:
841                 break;
842         }
843         panic("invalid UNEXPR type found");
844 }
845
846 static long get_pnc(binary_expression_type_t type)
847 {
848         switch(type) {
849         case BINEXPR_EQUAL:        return pn_Cmp_Eq;
850         case BINEXPR_NOTEQUAL:     return pn_Cmp_Lg;
851         case BINEXPR_LESS:         return pn_Cmp_Lt;
852         case BINEXPR_LESSEQUAL:    return pn_Cmp_Le;
853         case BINEXPR_GREATER:      return pn_Cmp_Gt;
854         case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
855         default:
856                 break;
857         }
858         panic("trying to get pn_Cmp from non-comparison binexpr type");
859 }
860
861 static ir_node *create_lazy_op(const binary_expression_t *expression)
862 {
863         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
864
865         bool is_or = (expression->type == BINEXPR_LOGICAL_OR);
866         assert(is_or || expression->type == BINEXPR_LOGICAL_AND);
867
868         ir_node  *val1       = expression_to_modeb(expression->left);
869         ir_node  *cond       = new_d_Cond(dbgi, val1);
870         ir_node  *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
871         ir_node  *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
872
873         ir_node *fallthrough_block = new_immBlock();
874
875         /* the true case */
876         ir_node *calc_val2_block = new_immBlock();
877         if(is_or) {
878                 add_immBlock_pred(calc_val2_block, false_proj);
879         } else {
880                 add_immBlock_pred(calc_val2_block, true_proj);
881         }
882
883         mature_immBlock(calc_val2_block);
884
885         ir_node *val2 = expression_to_modeb(expression->right);
886         if(get_cur_block() != NULL) {
887                 ir_node *jmp = new_d_Jmp(dbgi);
888                 add_immBlock_pred(fallthrough_block, jmp);
889         }
890
891         /* fallthrough */
892         ir_node *constb;
893         if(is_or) {
894                 constb = new_d_Const(dbgi, mode_b, get_tarval_b_true());
895                 add_immBlock_pred(fallthrough_block, true_proj);
896         } else {
897                 constb = new_d_Const(dbgi, mode_b, get_tarval_b_false());
898                 add_immBlock_pred(fallthrough_block, false_proj);
899         }
900         mature_immBlock(fallthrough_block);
901
902         set_cur_block(fallthrough_block);
903
904         ir_node *in[2] = { val2, constb };
905         ir_node *val   = new_d_Phi(dbgi, 2, in, mode_b);
906
907         return val;
908 }
909
910 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
911                                             ir_node *right, ir_mode *mode);
912
913 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
914                                         create_arithmetic_func func)
915 {
916         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
917         ir_node  *left  = expression_to_firm(expression->left);
918         ir_node  *right = expression_to_firm(expression->right);
919         type_t   *type  = expression->right->datatype;
920         /* be careful with the modes, because in asithmetic assign nodes only
921          * the right operand has the mode of the arithmetic alread */
922         ir_mode  *mode  = get_ir_mode(type);
923         left            = create_conv(dbgi, left, mode);
924         ir_node  *res   = func(dbgi, left, right, mode);
925
926         return res;
927 }
928
929 static ir_node *create_arithmetic_assign_binop(
930                 const binary_expression_t *expression, create_arithmetic_func func)
931 {
932         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
933         ir_node  *value = create_arithmetic_binop(expression, func);
934         type_t   *type  = expression->expression.datatype;
935         ir_mode  *mode  = get_ir_mode(type);
936
937         assert(type->type != TYPE_POINTER);
938
939         value = create_conv(dbgi, value, mode);
940         set_value_for_expression(expression->left, value);
941
942         return value;
943 }
944
945 static ir_node *create_add(const binary_expression_t *expression)
946 {
947         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
948         ir_node  *left  = expression_to_firm(expression->left);
949         ir_node  *right = expression_to_firm(expression->right);
950         type_t   *type  = expression->expression.datatype;
951         ir_mode  *mode  = get_ir_mode(type);
952
953         expression_t *expr_left  = expression->left;
954         expression_t *expr_right = expression->right;
955         type_t       *type_left  = skip_typeref(expr_left->datatype);
956         type_t       *type_right = skip_typeref(expr_right->datatype);
957
958         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
959                 return new_d_Add(dbgi, left, right, mode);
960         }
961
962         ir_node        *pointer;
963         ir_node        *integer;
964         pointer_type_t *pointer_type;
965         if(type_left->type == TYPE_POINTER) {
966                 pointer      = left;
967                 integer      = right;
968                 pointer_type = (pointer_type_t*) type_left;
969         } else {
970                 assert(type_right->type == TYPE_POINTER);
971                 pointer      = right;
972                 integer      = left;
973                 pointer_type = (pointer_type_t*) type_right;
974         }
975
976         type_t   *points_to = pointer_type->points_to;
977         unsigned  elem_size = get_type_size(points_to);
978
979         assert(elem_size >= 1);
980         if(elem_size > 1) {
981                 integer       = create_conv(dbgi, integer, mode_Is);
982                 ir_node *cnst = new_Const_long(mode_Is, (int) elem_size);
983                 ir_node *mul  = new_d_Mul(dbgi, integer, cnst, mode_Is);
984                 integer = mul;
985         }
986
987         ir_node *res = new_d_Add(dbgi, pointer, integer, mode);
988
989         return res;
990 }
991
992 static ir_node *create_sub(const binary_expression_t *expression)
993 {
994         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
995         ir_node  *left  = expression_to_firm(expression->left);
996         ir_node  *right = expression_to_firm(expression->right);
997         type_t   *type  = expression->expression.datatype;
998         ir_mode  *mode  = get_ir_mode(type);
999
1000         expression_t *expr_left  = expression->left;
1001         expression_t *expr_right = expression->right;
1002         type_t       *type_left  = skip_typeref(expr_left->datatype);
1003         type_t       *type_right = skip_typeref(expr_right->datatype);
1004
1005         if((is_type_arithmetic(type_left) && is_type_arithmetic(type_right))
1006                         || (type_left->type == TYPE_POINTER
1007                                 && type_right->type == TYPE_POINTER)) {
1008                 return new_d_Sub(dbgi, left, right, mode);
1009         }
1010
1011         assert(type_right->type == TYPE_POINTER);
1012         ir_node        *pointer      = left;
1013         ir_node        *integer      = right;
1014         pointer_type_t *pointer_type = (pointer_type_t*) type_right;
1015
1016         type_t   *points_to = pointer_type->points_to;
1017         unsigned  elem_size = get_type_size(points_to);
1018
1019         assert(elem_size >= 1);
1020         if(elem_size > 1) {
1021                 ir_node *cnst = new_Const_long(mode_Iu, elem_size);
1022                 ir_node *mul  = new_d_Mul(dbgi, integer, cnst, mode_Iu);
1023                 integer = mul;
1024         }
1025
1026         ir_node *res = new_d_Sub(dbgi, pointer, integer, mode);
1027
1028         return res;
1029 }
1030
1031 static ir_node *create_divmod(const binary_expression_t *expression)
1032 {
1033         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1034         ir_node  *left  = expression_to_firm(expression->left);
1035         ir_node  *right = expression_to_firm(expression->right);
1036         ir_node  *pin   = new_Pin(new_NoMem());
1037         type_t   *type  = expression->expression.datatype;
1038         ir_mode  *mode  = get_ir_mode(type);
1039         ir_node  *op;
1040         ir_node  *res;
1041
1042         if(expression->type == BINEXPR_DIV) {
1043                 if(mode_is_float(mode)) {
1044                         op  = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1045                         res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1046                 } else {
1047                         op  = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1048                         res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1049                 }
1050         } else {
1051                 assert(expression->type == BINEXPR_MOD);
1052                 assert(!mode_is_float(mode));
1053                 op  = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1054                 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1055         }
1056
1057         return res;
1058 }
1059
1060
1061
1062 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1063 {
1064         binary_expression_type_t type = expression->type;
1065         switch(type) {
1066         case BINEXPR_EQUAL:
1067         case BINEXPR_NOTEQUAL:
1068         case BINEXPR_LESS:
1069         case BINEXPR_LESSEQUAL:
1070         case BINEXPR_GREATER:
1071         case BINEXPR_GREATEREQUAL: {
1072                 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1073                 ir_node *left  = expression_to_firm(expression->left);
1074                 ir_node *right = expression_to_firm(expression->right);
1075                 ir_node *cmp   = new_d_Cmp(dbgi, left, right);
1076                 long     pnc   = get_pnc(type);
1077                 ir_node *proj  = new_d_Proj(dbgi, cmp, mode_b, pnc);
1078                 return proj;
1079         }
1080         case BINEXPR_ASSIGN: {
1081                 ir_node *right = expression_to_firm(expression->right);
1082                 set_value_for_expression(expression->left, right);
1083                 return right;
1084         }
1085         case BINEXPR_ADD:
1086                 return create_add(expression);
1087         case BINEXPR_SUB:
1088                 return create_sub(expression);
1089         case BINEXPR_MUL:
1090                 return create_arithmetic_binop(expression, new_d_Mul);
1091         case BINEXPR_BITWISE_AND:
1092                 return create_arithmetic_binop(expression, new_d_And);
1093         case BINEXPR_BITWISE_OR:
1094                 return create_arithmetic_binop(expression, new_d_Or);
1095         case BINEXPR_BITWISE_XOR:
1096                 return create_arithmetic_binop(expression, new_d_Eor);
1097         case BINEXPR_SHIFTLEFT:
1098                 return create_arithmetic_binop(expression, new_d_Shl);
1099         case BINEXPR_SHIFTRIGHT:
1100                 return create_arithmetic_binop(expression, new_d_Shr);
1101         case BINEXPR_DIV:
1102         case BINEXPR_MOD:
1103                 return create_divmod(expression);
1104         case BINEXPR_LOGICAL_AND:
1105         case BINEXPR_LOGICAL_OR:
1106                 return create_lazy_op(expression);
1107         case BINEXPR_COMMA:
1108                 expression_to_firm(expression->left);
1109                 return expression_to_firm(expression->right);
1110         case BINEXPR_ADD_ASSIGN:
1111                 return create_arithmetic_assign_binop(expression, new_d_Add);
1112         case BINEXPR_SUB_ASSIGN:
1113                 return create_arithmetic_assign_binop(expression, new_d_Sub);
1114         case BINEXPR_MUL_ASSIGN:
1115                 return create_arithmetic_assign_binop(expression, new_d_Mul);
1116         case BINEXPR_BITWISE_AND_ASSIGN:
1117                 return create_arithmetic_assign_binop(expression, new_d_And);
1118         case BINEXPR_BITWISE_OR_ASSIGN:
1119                 return create_arithmetic_assign_binop(expression, new_d_Or);
1120         case BINEXPR_BITWISE_XOR_ASSIGN:
1121                 return create_arithmetic_assign_binop(expression, new_d_Eor);
1122         case BINEXPR_SHIFTLEFT_ASSIGN:
1123                 return create_arithmetic_assign_binop(expression, new_d_Shl);
1124         case BINEXPR_SHIFTRIGHT_ASSIGN:
1125                 return create_arithmetic_assign_binop(expression, new_d_Shr);
1126         default:
1127                 panic("TODO binexpr type");
1128         }
1129 }
1130
1131 static ir_node *array_access_addr(const array_access_expression_t *expression)
1132 {
1133         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1134
1135         ir_node *base_addr = expression_to_firm(expression->array_ref);
1136         ir_node *offset    = expression_to_firm(expression->index);
1137         offset             = create_conv(dbgi, offset, mode_Iu);
1138
1139         unsigned elem_size       = get_type_size(expression->expression.datatype);
1140         ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1141         ir_node *real_offset     = new_d_Mul(dbgi, offset, elem_size_const,
1142                                              mode_Iu);
1143         ir_node *result          = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1144
1145         return result;
1146 }
1147
1148 static ir_node *array_access_to_firm(
1149                 const array_access_expression_t *expression)
1150 {
1151
1152         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1153         ir_node  *addr = array_access_addr(expression);
1154         type_t   *type = expression->expression.datatype;
1155
1156         return load_from_expression_addr(type, addr, dbgi);
1157 }
1158
1159 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1160 {
1161         type_t *type = expression->type;
1162         if(type == NULL) {
1163                 type = expression->size_expression->datatype;
1164                 assert(type != NULL);
1165         }
1166
1167         ir_mode  *mode      = get_ir_mode(expression->expression.datatype);
1168         unsigned  size      = get_type_size(type);
1169         ir_node  *size_node = new_Const_long(mode, size);
1170
1171         return size_node;
1172 }
1173
1174 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1175 {
1176         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1177
1178         ir_node *condition  = expression_to_modeb(expression->condition);
1179         ir_node *cond       = new_d_Cond(dbgi, condition);
1180         ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1181         ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1182
1183         /* create the true block */
1184         ir_node *true_block = new_immBlock();
1185         add_immBlock_pred(true_block, true_proj);
1186         mature_immBlock(true_block);
1187
1188         ir_node *true_val = expression_to_firm(expression->true_expression);
1189         ir_node *true_jmp = new_Jmp();
1190
1191         /* create the false block */
1192         ir_node *false_block = new_immBlock();
1193         add_immBlock_pred(false_block, false_proj);
1194         mature_immBlock(false_block);
1195
1196         ir_node *false_val = expression_to_firm(expression->false_expression);
1197         ir_node *false_jmp = new_Jmp();
1198
1199         /* create the common block */
1200         ir_node *common_block = new_immBlock();
1201         add_immBlock_pred(common_block, true_jmp);
1202         add_immBlock_pred(common_block, false_jmp);
1203         mature_immBlock(common_block);
1204
1205         ir_node *in[2] = { true_val, false_val };
1206         ir_mode *mode  = get_irn_mode(true_val);
1207         assert(get_irn_mode(false_val) == mode);
1208         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
1209
1210         return val;
1211 }
1212
1213 static ir_node *expression_to_addr(const expression_t *expression)
1214 {
1215         switch(expression->type) {
1216         case EXPR_REFERENCE:
1217                 return reference_addr((const reference_expression_t*) expression);
1218         case EXPR_ARRAY_ACCESS:
1219                 return array_access_addr((const array_access_expression_t*) expression);
1220         default:
1221                 break;
1222         }
1223         panic("trying to get address of non-lvalue");
1224 }
1225
1226 static ir_node *_expression_to_firm(const expression_t *expression)
1227 {
1228         switch(expression->type) {
1229         case EXPR_CONST:
1230                 return const_to_firm((const const_t*) expression);
1231         case EXPR_STRING_LITERAL:
1232                 return string_literal_to_firm((const string_literal_t*) expression);
1233         case EXPR_REFERENCE:
1234                 return reference_expression_to_firm(
1235                                 (const reference_expression_t*) expression);
1236         case EXPR_CALL:
1237                 return call_expression_to_firm((const call_expression_t*) expression);
1238         case EXPR_UNARY:
1239                 return unary_expression_to_firm((const unary_expression_t*) expression);
1240         case EXPR_BINARY:
1241                 return binary_expression_to_firm(
1242                                 (const binary_expression_t*) expression);
1243         case EXPR_ARRAY_ACCESS:
1244                 return array_access_to_firm(
1245                                 (const array_access_expression_t*) expression);
1246         case EXPR_SIZEOF:
1247                 return sizeof_to_firm((const sizeof_expression_t*) expression);
1248         case EXPR_CONDITIONAL:
1249                 return conditional_to_firm((const conditional_expression_t*)expression);
1250         default:
1251                 break;
1252         }
1253         panic("unsupported expression found");
1254 }
1255
1256 static ir_node *expression_to_firm(const expression_t *expression)
1257 {
1258         ir_node *res  = _expression_to_firm(expression);
1259
1260         if(expression->datatype == type_void)
1261                 return NULL;
1262
1263         ir_mode *mode = get_ir_mode(expression->datatype);
1264         res           = create_conv(NULL, res, mode);
1265         return res;
1266 }
1267
1268 static ir_node *expression_to_modeb(const expression_t *expression)
1269 {
1270         ir_node *res = _expression_to_firm(expression);
1271         res          = create_conv(NULL, res, mode_b);
1272
1273         return res;
1274 }
1275
1276 static void statement_to_firm(statement_t *statement);
1277
1278 static void return_statement_to_firm(return_statement_t *statement)
1279 {
1280         if(get_cur_block() == NULL)
1281                 return;
1282
1283         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1284         ir_node  *ret;
1285
1286         if(statement->return_value != NULL) {
1287                 ir_node *retval = expression_to_firm(statement->return_value);
1288                 ir_node *in[1];
1289
1290                 in[0] = retval;
1291                 ret   = new_d_Return(dbgi, get_store(), 1, in);
1292         } else {
1293                 ret   = new_d_Return(dbgi, get_store(), 0, NULL);
1294         }
1295         ir_node *end_block = get_irg_end_block(current_ir_graph);
1296         add_immBlock_pred(end_block, ret);
1297
1298         set_cur_block(NULL);
1299 }
1300
1301 static void compound_statement_to_firm(compound_statement_t *compound)
1302 {
1303         statement_t *statement = compound->statements;
1304         for( ; statement != NULL; statement = statement->next) {
1305                 //context2firm(&statement->context);
1306                 statement_to_firm(statement);
1307         }
1308 }
1309
1310 static void expression_statement_to_firm(expression_statement_t *statement)
1311 {
1312         if(get_cur_block() == NULL)
1313                 return;
1314
1315         expression_to_firm(statement->expression);
1316 }
1317
1318 static void if_statement_to_firm(if_statement_t *statement)
1319 {
1320         dbg_info *dbgi      = get_dbg_info(&statement->statement.source_position);
1321         ir_node  *condition = expression_to_modeb(statement->condition);
1322
1323         /* make sure we have a mode_b condition */
1324         ir_node *cond       = new_d_Cond(dbgi, condition);
1325         ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1326         ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1327
1328         ir_node *fallthrough_block = new_immBlock();
1329
1330         /* the true (blocks) */
1331         ir_node *true_block = new_immBlock();
1332         add_immBlock_pred(true_block, true_proj);
1333         mature_immBlock(true_block);
1334
1335         statement_to_firm(statement->true_statement);
1336         if(get_cur_block() != NULL) {
1337                 ir_node *jmp = new_Jmp();
1338                 add_immBlock_pred(fallthrough_block, jmp);
1339         }
1340
1341         /* the false (blocks) */
1342         if(statement->false_statement != NULL) {
1343                 ir_node *false_block = new_immBlock();
1344                 add_immBlock_pred(false_block, false_proj);
1345                 mature_immBlock(false_block);
1346
1347                 statement_to_firm(statement->false_statement);
1348                 if(get_cur_block() != NULL) {
1349                         ir_node *jmp = new_Jmp();
1350                         add_immBlock_pred(fallthrough_block, jmp);
1351                 }
1352         } else {
1353                 add_immBlock_pred(fallthrough_block, false_proj);
1354         }
1355         mature_immBlock(fallthrough_block);
1356
1357         set_cur_block(fallthrough_block);
1358 }
1359
1360 static void while_statement_to_firm(while_statement_t *statement)
1361 {
1362         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1363
1364         ir_node *jmp = NULL;
1365         if(get_cur_block() != NULL) {
1366                 jmp = new_Jmp();
1367         }
1368
1369         /* create the header block */
1370         ir_node *header_block = new_immBlock();
1371         if(jmp != NULL) {
1372                 add_immBlock_pred(header_block, jmp);
1373         }
1374
1375         /* create the condition */
1376         ir_node *condition  = expression_to_modeb(statement->condition);
1377         ir_node *cond       = new_d_Cond(dbgi, condition);
1378         ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1379         ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1380
1381         /* the false block */
1382         ir_node *false_block = new_immBlock();
1383         add_immBlock_pred(false_block, false_proj);
1384
1385         /* the loop body */
1386         ir_node *body_block = new_immBlock();
1387         add_immBlock_pred(body_block, true_proj);
1388         mature_immBlock(body_block);
1389
1390         ir_node *old_continue_label = continue_label;
1391         ir_node *old_break_label    = break_label;
1392         continue_label              = header_block;
1393         break_label                 = false_block;
1394
1395         statement_to_firm(statement->body);
1396
1397         assert(continue_label == header_block);
1398         assert(break_label    == false_block);
1399         continue_label = old_continue_label;
1400         break_label    = old_break_label;
1401
1402         if(get_cur_block() != NULL) {
1403                 ir_node *jmp = new_Jmp();
1404                 add_immBlock_pred(header_block, jmp);
1405         }
1406
1407         mature_immBlock(header_block);
1408         mature_immBlock(false_block);
1409
1410         set_cur_block(false_block);
1411 }
1412
1413 static void do_while_statement_to_firm(do_while_statement_t *statement)
1414 {
1415         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1416
1417         ir_node *jmp = NULL;
1418         if(get_cur_block() != NULL) {
1419                 jmp = new_Jmp();
1420         }
1421
1422         /* create the header block */
1423         ir_node *header_block = new_immBlock();
1424
1425         /* the false block */
1426         ir_node *false_block = new_immBlock();
1427
1428         /* the loop body */
1429         ir_node *body_block = new_immBlock();
1430         if(jmp != NULL) {
1431                 add_immBlock_pred(body_block, jmp);
1432         }
1433
1434         ir_node *old_continue_label = continue_label;
1435         ir_node *old_break_label    = break_label;
1436         continue_label              = header_block;
1437         break_label                 = false_block;
1438
1439         statement_to_firm(statement->body);
1440
1441         assert(continue_label == header_block);
1442         assert(break_label    == false_block);
1443         continue_label = old_continue_label;
1444         break_label    = old_break_label;
1445
1446         if(get_cur_block() == NULL) {
1447                 mature_immBlock(header_block);
1448                 mature_immBlock(body_block);
1449                 return;
1450         }
1451
1452         ir_node *body_jmp = new_Jmp();
1453         add_immBlock_pred(header_block, body_jmp);
1454         mature_immBlock(header_block);
1455
1456         /* create the condition */
1457         ir_node *condition  = expression_to_modeb(statement->condition);
1458         ir_node *cond       = new_d_Cond(dbgi, condition);
1459         ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1460         ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1461
1462         add_immBlock_pred(body_block, true_proj);
1463         mature_immBlock(body_block);
1464
1465         add_immBlock_pred(false_block, false_proj);
1466         mature_immBlock(false_block);
1467
1468         set_cur_block(false_block);
1469 }
1470
1471 static void for_statement_to_firm(for_statement_t *statement)
1472 {
1473         dbg_info *const dbgi = get_dbg_info(&statement->statement.source_position);
1474
1475         ir_node *jmp = NULL;
1476         if (get_cur_block() != NULL) {
1477                 if(statement->initialisation != NULL) {
1478                         expression_to_firm(statement->initialisation);
1479                 }
1480                 jmp = new_Jmp();
1481         }
1482
1483         /* create the step block */
1484         ir_node *const step_block = new_immBlock();
1485         if (statement->step != NULL) {
1486                 expression_to_firm(statement->step);
1487         }
1488         ir_node *const step_jmp   = new_Jmp();
1489
1490         /* create the header block */
1491         ir_node *const header_block = new_immBlock();
1492         if (jmp != NULL) {
1493                 add_immBlock_pred(header_block, jmp);
1494         }
1495         add_immBlock_pred(header_block, step_jmp);
1496
1497         /* create the condition */
1498         ir_node *true_proj;
1499         ir_node *false_proj;
1500         if (statement->condition != NULL) {
1501                 ir_node *const condition  = expression_to_modeb(statement->condition);
1502                 ir_node *const cond       = new_d_Cond(dbgi, condition);
1503                 true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1504                 false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1505         } else {
1506                 keep_alive(header_block);
1507                 true_proj  = new_Jmp();
1508                 false_proj = NULL;
1509         }
1510
1511         /* the false block */
1512         ir_node *const false_block = new_immBlock();
1513         if (false_proj != NULL) {
1514                 add_immBlock_pred(false_block, false_proj);
1515         }
1516
1517         /* the loop body */
1518         ir_node *const body_block = new_immBlock();
1519         add_immBlock_pred(body_block, true_proj);
1520         mature_immBlock(body_block);
1521
1522         ir_node *const old_continue_label = continue_label;
1523         ir_node *const old_break_label    = break_label;
1524         continue_label = step_block;
1525         break_label    = false_block;
1526
1527         statement_to_firm(statement->body);
1528
1529         assert(continue_label == step_block);
1530         assert(break_label    == false_block);
1531         continue_label = old_continue_label;
1532         break_label    = old_break_label;
1533
1534         if (get_cur_block() != NULL) {
1535                 ir_node *const jmp = new_Jmp();
1536                 add_immBlock_pred(step_block, jmp);
1537         }
1538
1539         mature_immBlock(step_block);
1540         mature_immBlock(header_block);
1541         mature_immBlock(false_block);
1542
1543         set_cur_block(false_block);
1544 }
1545
1546 static void create_declaration_entity(declaration_t *declaration,
1547                                       declaration_type_t declaration_type,
1548                                       ir_type *parent_type)
1549 {
1550         ident     *id     = new_id_from_str(declaration->symbol->string);
1551         ir_type   *irtype = get_ir_type(declaration->type);
1552         ir_entity *entity = new_entity(parent_type, id, irtype);
1553         set_entity_ld_ident(entity, id);
1554
1555         declaration->declaration_type = declaration_type;
1556         declaration->v.entity         = entity;
1557         set_entity_variability(entity, variability_uninitialized);
1558         /* TODO: visibility? */
1559 }
1560
1561 static void create_initializer(declaration_t *declaration)
1562 {
1563         initializer_t *initializer = declaration->init.initializer;
1564         if(initializer == NULL)
1565                 return;
1566
1567         if(initializer->type == INITIALIZER_VALUE) {
1568                 assert(initializer->designator == NULL);
1569                 assert(initializer->next == NULL);
1570                 ir_node *init_node = expression_to_firm(initializer->v.value);
1571
1572                 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1573                         set_value(declaration->v.value_number, init_node);
1574                 } else {
1575                         ir_entity *entity = declaration->v.entity;
1576
1577                         set_entity_variability(entity, variability_initialized);
1578                         set_atomic_ent_value(entity, init_node);
1579                 }
1580         } else {
1581                 assert(initializer->type == INITIALIZER_LIST);
1582                 panic("list initializer not supported yet");
1583         }
1584 }
1585
1586 static void create_local_variable(declaration_t *declaration)
1587 {
1588         bool needs_entity = declaration->address_taken;
1589
1590         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
1591
1592         if(needs_entity) {
1593                 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
1594                 create_declaration_entity(declaration,
1595                                           DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
1596                                           frame_type);
1597         } else {
1598                 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1599                 declaration->v.value_number   = next_value_number_function;
1600                 ++next_value_number_function;
1601         }
1602
1603         create_initializer(declaration);
1604 }
1605
1606 static void declaration_statement_to_firm(declaration_statement_t *statement)
1607 {
1608         declaration_t *declaration = statement->declarations_begin;
1609         declaration_t *end         = statement->declarations_end->next;
1610         for( ; declaration != end; declaration = declaration->next) {
1611                 type_t *type = declaration->type;
1612
1613                 switch(declaration->storage_class) {
1614                 case STORAGE_CLASS_TYPEDEF:
1615                         continue;
1616                 case STORAGE_CLASS_STATIC:
1617                         panic("static local vars not implemented yet");
1618                 case STORAGE_CLASS_ENUM_ENTRY:
1619                         panic("enum entry declaration in local block found");
1620                 case STORAGE_CLASS_EXTERN:
1621                         panic("extern declaration in local block found");
1622                 case STORAGE_CLASS_NONE:
1623                 case STORAGE_CLASS_AUTO:
1624                 case STORAGE_CLASS_REGISTER:
1625                         if(type->type == TYPE_FUNCTION) {
1626                                 panic("nested functions not supported yet");
1627                         } else {
1628                                 create_local_variable(declaration);
1629                         }
1630                         continue;
1631                 }
1632                 panic("invalid storage class found");
1633         }
1634 }
1635
1636 static void create_jump_statement(const statement_t *statement,
1637                                   ir_node *target_block)
1638 {
1639         dbg_info *dbgi = get_dbg_info(&statement->source_position);
1640         ir_node  *jump = new_d_Jmp(dbgi);
1641         add_immBlock_pred(target_block, jump);
1642
1643         set_cur_block(NULL);
1644 }
1645
1646 static void switch_statement_to_firm(const switch_statement_t *statement)
1647 {
1648         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1649
1650         ir_node *expression  = expression_to_firm(statement->expression);
1651         ir_node *cond        = new_d_Cond(dbgi, expression);
1652         ir_node *break_block = new_immBlock();
1653
1654         set_cur_block(NULL);
1655
1656         ir_node *old_switch_cond = current_switch_cond;
1657         ir_node *old_break_label = break_label;
1658         current_switch_cond      = cond;
1659         break_label              = break_block;
1660
1661         statement_to_firm(statement->body);
1662
1663         if(get_cur_block() != NULL) {
1664                 ir_node *jmp = new_Jmp();
1665                 add_immBlock_pred(break_block, jmp);
1666         }
1667
1668         assert(current_switch_cond == cond);
1669         assert(break_label         == break_block);
1670         current_switch_cond = old_switch_cond;
1671         break_label         = old_break_label;
1672
1673         mature_immBlock(break_block);
1674         set_cur_block(break_block);
1675 }
1676
1677 static void case_label_to_firm(const case_label_statement_t *statement)
1678 {
1679         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1680
1681         /* let's create a node and hope firm constant folding creates a Const
1682          * node... */
1683         ir_node *proj;
1684         set_cur_block(get_nodes_block(current_switch_cond));
1685         if(statement->expression) {
1686                 ir_node *cnst = expression_to_firm(statement->expression);
1687                 if(!is_Const(cnst)) {
1688                         panic("couldn't fold constant for case label");
1689                 }
1690                 tarval *tv = get_Const_tarval(cnst);
1691                 if(!mode_is_int(get_tarval_mode(tv))) {
1692                         panic("case label not an integer");
1693                 }
1694
1695                 long pn = get_tarval_long(tv);
1696                 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
1697                         /* oops someone detected our cheating... */
1698                         panic("magic default pn used");
1699                 }
1700                 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
1701         } else {
1702                 proj = new_d_defaultProj(dbgi, current_switch_cond,
1703                                          MAGIC_DEFAULT_PN_NUMBER);
1704         }
1705
1706         ir_node *block = new_immBlock();
1707         add_immBlock_pred(block, proj);
1708         mature_immBlock(block);
1709 }
1710
1711 static ir_node *get_label_block(declaration_t *label)
1712 {
1713         assert(label->namespace == NAMESPACE_LABEL);
1714
1715         if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
1716                 return label->v.block;
1717         }
1718         assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
1719
1720         ir_node *old_cur_block = get_cur_block();
1721         ir_node *block         = new_immBlock();
1722         set_cur_block(old_cur_block);
1723
1724         label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
1725         label->v.block          = block;
1726
1727         ARR_APP1(imature_blocks, block);
1728
1729         return block;
1730 }
1731
1732 static void label_to_firm(const label_statement_t *statement)
1733 {
1734         ir_node *block = get_label_block(statement->label);
1735
1736         if(get_cur_block() != NULL) {
1737                 ir_node *jmp = new_Jmp();
1738                 add_immBlock_pred(block, jmp);
1739         }
1740
1741         set_cur_block(block);
1742         keep_alive(block);
1743
1744         statement_to_firm(statement->label_statement);
1745 }
1746
1747 static void goto_to_firm(const goto_statement_t *statement)
1748 {
1749         if(get_cur_block() == NULL)
1750                 return;
1751
1752         ir_node *block = get_label_block(statement->label);
1753         ir_node *jmp   = new_Jmp();
1754         add_immBlock_pred(block, jmp);
1755
1756         set_cur_block(NULL);
1757 }
1758
1759 static void statement_to_firm(statement_t *statement)
1760 {
1761         switch(statement->type) {
1762         case STATEMENT_COMPOUND:
1763                 compound_statement_to_firm((compound_statement_t*) statement);
1764                 return;
1765         case STATEMENT_RETURN:
1766                 return_statement_to_firm((return_statement_t*) statement);
1767                 return;
1768         case STATEMENT_EXPRESSION:
1769                 expression_statement_to_firm((expression_statement_t*) statement);
1770                 return;
1771         case STATEMENT_IF:
1772                 if_statement_to_firm((if_statement_t*) statement);
1773                 return;
1774         case STATEMENT_WHILE:
1775                 while_statement_to_firm((while_statement_t*) statement);
1776                 return;
1777         case STATEMENT_DO_WHILE:
1778                 do_while_statement_to_firm((do_while_statement_t*) statement);
1779                 return;
1780         case STATEMENT_DECLARATION:
1781                 declaration_statement_to_firm((declaration_statement_t*) statement);
1782                 return;
1783         case STATEMENT_BREAK:
1784                 create_jump_statement(statement, break_label);
1785                 return;
1786         case STATEMENT_CONTINUE:
1787                 create_jump_statement(statement, continue_label);
1788                 return;
1789         case STATEMENT_SWITCH:
1790                 switch_statement_to_firm((switch_statement_t*) statement);
1791                 return;
1792         case STATEMENT_CASE_LABEL:
1793                 case_label_to_firm((case_label_statement_t*) statement);
1794                 return;
1795         case STATEMENT_FOR:
1796                 for_statement_to_firm((for_statement_t*) statement);
1797                 return;
1798         case STATEMENT_LABEL:
1799                 label_to_firm((label_statement_t*) statement);
1800                 return;
1801         case STATEMENT_GOTO:
1802                 goto_to_firm((goto_statement_t*) statement);
1803                 return;
1804         default:
1805                 break;
1806         }
1807         panic("Statement not implemented\n");
1808 }
1809
1810 static int get_function_n_local_vars(declaration_t *declaration)
1811 {
1812         (void) declaration;
1813         /* TODO */
1814         return 30;
1815 }
1816
1817 static void initialize_function_parameters(declaration_t *declaration)
1818 {
1819         ir_graph *irg         = current_ir_graph;
1820         ir_node  *args        = get_irg_args(irg);
1821         ir_node  *start_block = get_irg_start_block(irg);
1822
1823         int            n         = 0;
1824         declaration_t *parameter = declaration->context.declarations;
1825         for( ; parameter != NULL; parameter = parameter->next) {
1826                 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
1827
1828                 if(parameter->address_taken) {
1829                         panic("address take from parameter not implemented yet");
1830                 }
1831
1832                 ir_mode *mode = get_ir_mode(parameter->type);
1833                 long     pn   = n;
1834                 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
1835                 ++n;
1836
1837                 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1838                 parameter->v.value_number   = next_value_number_function;
1839                 ++next_value_number_function;
1840
1841                 set_value(parameter->v.value_number, proj);
1842         }
1843 }
1844
1845 static void create_function(declaration_t *declaration)
1846 {
1847         ir_entity *entity = get_function_entity(declaration);
1848
1849         if(declaration->init.statement == NULL)
1850                 return;
1851
1852         assert(imature_blocks == NULL);
1853         imature_blocks = NEW_ARR_F(ir_node*, 0);
1854
1855         int       n_local_vars = get_function_n_local_vars(declaration);
1856         ir_graph *irg          = new_ir_graph(entity, n_local_vars);
1857         ir_node  *first_block  = get_cur_block();
1858
1859         next_value_number_function = 0;
1860         initialize_function_parameters(declaration);
1861
1862         statement_to_firm(declaration->init.statement);
1863
1864         ir_node *end_block = get_irg_end_block(irg);
1865
1866         /* do we have a return statement yet? */
1867         if(get_cur_block() != NULL) {
1868                 assert(declaration->type->type == TYPE_FUNCTION);
1869                 const function_type_t* const func_type
1870                         = (const function_type_t*) declaration->type;
1871                 ir_node *ret;
1872                 if (func_type->result_type == type_void) {
1873                         ret = new_Return(get_store(), 0, NULL);
1874                 } else {
1875                         ir_mode *const mode = get_ir_mode(func_type->result_type);
1876                         ir_node *      in[1];
1877                         // ยง5.1.2.2.3 main implicitly returns 0
1878                         if (strcmp(declaration->symbol->string, "main") == 0) {
1879                                 in[0] = new_Const(mode, get_mode_null(mode));
1880                         } else {
1881                                 in[0] = new_Unknown(mode);
1882                         }
1883                         ret = new_Return(get_store(), 1, in);
1884                 }
1885                 add_immBlock_pred(end_block, ret);
1886         }
1887
1888         for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
1889                 mature_immBlock(imature_blocks[i]);
1890         }
1891         DEL_ARR_F(imature_blocks);
1892         imature_blocks = NULL;
1893
1894         mature_immBlock(first_block);
1895         mature_immBlock(end_block);
1896
1897         irg_finalize_cons(irg);
1898
1899         /* finalize the frame type */
1900         ir_type *frame_type = get_irg_frame_type(irg);
1901         int      n          = get_compound_n_members(frame_type);
1902         int      align_all  = 4;
1903         int      offset     = 0;
1904         for(int i = 0; i < n; ++i) {
1905                 ir_entity *entity      = get_compound_member(frame_type, i);
1906                 ir_type   *entity_type = get_entity_type(entity);
1907
1908                 int align = get_type_alignment_bytes(entity_type);
1909                 if(align > align_all)
1910                         align_all = align;
1911                 int misalign = 0;
1912                 if(align > 0) {
1913                         misalign  = offset % align;
1914                         offset   += misalign;
1915                 }
1916
1917                 set_entity_offset(entity, offset);
1918                 offset += get_type_size_bytes(entity_type);
1919         }
1920         set_type_size_bytes(frame_type, offset);
1921         set_type_alignment_bytes(frame_type, align_all);
1922         set_type_state(frame_type, layout_fixed);
1923
1924         irg_vrfy(irg);
1925 }
1926
1927 static void create_global_variable(declaration_t *declaration)
1928 {
1929         ir_type   *global_type = get_glob_type();
1930         create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
1931                                   global_type);
1932
1933         ir_entity *entity = declaration->v.entity;
1934         if(declaration->storage_class == STORAGE_CLASS_STATIC) {
1935                 set_entity_visibility(entity, visibility_local);
1936         } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
1937                 set_entity_visibility(entity, visibility_external_allocated);
1938         } else {
1939                 set_entity_visibility(entity, visibility_external_visible);
1940         }
1941         current_ir_graph = get_const_code_irg();
1942         create_initializer(declaration);
1943 }
1944
1945 static void context_to_firm(context_t *context)
1946 {
1947         declaration_t *declaration = context->declarations;
1948         for( ; declaration != NULL; declaration = declaration->next) {
1949                 if(declaration->namespace != NAMESPACE_NORMAL)
1950                         continue;
1951                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
1952                                 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
1953                         continue;
1954                 if(declaration->symbol == NULL)
1955                         continue;
1956
1957                 type_t *type = declaration->type;
1958                 if(type->type == TYPE_FUNCTION) {
1959                         create_function(declaration);
1960                 } else {
1961                         create_global_variable(declaration);
1962                 }
1963         }
1964 }
1965
1966 void translation_unit_to_firm(translation_unit_t *unit)
1967 {
1968         /* remove me later TODO FIXME */
1969         (void) get_type_size;
1970
1971         /* just to be sure */
1972         continue_label      = NULL;
1973         break_label         = NULL;
1974         current_switch_cond = NULL;
1975
1976         context_to_firm(& unit->context);
1977 }