avoid even more casts of type structs
[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 #include "parser.h"
20
21 #define MAGIC_DEFAULT_PN_NUMBER     (long) -314159265
22
23 static ir_type *ir_type_const_char;
24 static ir_type *ir_type_void;
25 static ir_type *ir_type_int;
26 static ir_type *ir_type_void_ptr;
27
28 static type_t *type_const_char;
29 static type_t *type_void;
30 static type_t *type_int;
31
32 static symbol_t *symbol_alloca;
33
34 static int       next_value_number_function;
35 static ir_node  *continue_label;
36 static ir_node  *break_label;
37 static ir_node  *current_switch_cond;
38 static bool      saw_default_label;
39 static ir_node **imature_blocks;
40
41 static const declaration_t *current_function_decl;
42 static ir_node             *current_function_name;
43
44 typedef enum declaration_type_t {
45         DECLARATION_TYPE_UNKNOWN,
46         DECLARATION_TYPE_FUNCTION,
47         DECLARATION_TYPE_GLOBAL_VARIABLE,
48         DECLARATION_TYPE_LOCAL_VARIABLE,
49         DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
50         DECLARATION_TYPE_COMPOUND_MEMBER,
51         DECLARATION_TYPE_LABEL_BLOCK,
52         DECLARATION_TYPE_ENUM_ENTRY
53 } declaration_type_t;
54
55 static ir_type *get_ir_type(type_t *type);
56
57 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
58 {
59         (void) pos;
60 #if 0
61         const declaration_t *declaration = & value_numbers[pos]->declaration;
62
63         print_warning_prefix(declaration->source_position);
64         fprintf(stderr, "variable '%s' might be used uninitialized\n",
65                         declaration->symbol->string);
66 #endif
67         fprintf(stderr, "Some variable might be used uninitialized\n");
68         return new_r_Unknown(irg, mode);
69 }
70
71 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
72 {
73         const source_position_t *pos = (const source_position_t*) dbg;
74         if(pos == NULL)
75                 return 0;
76         return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
77                                    pos->linenr);
78 }
79
80 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
81 {
82         const source_position_t *pos = (const source_position_t*) dbg;
83         if(pos == NULL)
84                 return NULL;
85         if(line != NULL)
86                 *line = pos->linenr;
87         return pos->input_name;
88 }
89
90 void init_ast2firm(void)
91 {
92         type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
93         type_void       = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
94         type_int        = make_atomic_type(ATOMIC_TYPE_INT,  TYPE_QUALIFIER_NONE);
95
96         ir_type_int        = get_ir_type(type_int);
97         ir_type_const_char = get_ir_type(type_const_char);
98         ir_type_void       = get_ir_type(type_int); /* we don't have a real void
99                                                        type in firm */
100         ir_type_void_ptr   = new_type_pointer(new_id_from_str("void_ptr"),
101                                               ir_type_void, mode_P_data);
102
103         type_void->base.firm_type = ir_type_void;
104
105         symbol_alloca = symbol_table_insert("__builtin_alloca");
106 }
107
108 void exit_ast2firm(void)
109 {
110 }
111
112 static unsigned unique_id = 0;
113
114 static ident *unique_ident(const char *tag)
115 {
116         char buf[256];
117
118         snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
119         unique_id++;
120         return new_id_from_str(buf);
121 }
122
123 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
124 {
125         switch(atomic_type->atype) {
126         case ATOMIC_TYPE_SCHAR:
127         case ATOMIC_TYPE_CHAR:
128                 return mode_Bs;
129         case ATOMIC_TYPE_UCHAR:
130                 return mode_Bu;
131         case ATOMIC_TYPE_SHORT:
132                 return mode_Hs;
133         case ATOMIC_TYPE_USHORT:
134                 return mode_Hu;
135         case ATOMIC_TYPE_LONG:
136         case ATOMIC_TYPE_INT:
137                 return mode_Is;
138         case ATOMIC_TYPE_ULONG:
139         case ATOMIC_TYPE_UINT:
140                 return mode_Iu;
141         case ATOMIC_TYPE_LONGLONG:
142                 return mode_Ls;
143         case ATOMIC_TYPE_ULONGLONG:
144                 return mode_Lu;
145         case ATOMIC_TYPE_FLOAT:
146                 return mode_F;
147         case ATOMIC_TYPE_DOUBLE:
148                 return mode_D;
149         case ATOMIC_TYPE_LONG_DOUBLE:
150                 return mode_E;
151         case ATOMIC_TYPE_BOOL:
152                 return mode_b;
153 #ifdef PROVIDE_COMPLEX
154         case ATOMIC_TYPE_FLOAT_COMPLEX:
155         case ATOMIC_TYPE_DOUBLE_COMPLEX:
156         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
157                 panic("complex lowering not implemented yet");
158                 break;
159         case ATOMIC_TYPE_FLOAT_IMAGINARY:
160         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
161         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
162                 panic("imaginary lowering not implemented yet");
163                 break;
164 #endif
165         case ATOMIC_TYPE_VOID:
166                 /* firm has no real void... */
167                 return mode_Is;
168         case ATOMIC_TYPE_INVALID:
169                 break;
170         }
171         panic("Encountered unknown atomic type");
172 }
173
174
175 static unsigned get_type_size(type_t *type);
176
177 static unsigned get_atomic_type_size(const atomic_type_t *type)
178 {
179         switch(type->atype) {
180         case ATOMIC_TYPE_CHAR:
181         case ATOMIC_TYPE_SCHAR:
182         case ATOMIC_TYPE_UCHAR:
183                 return 1;
184
185         case ATOMIC_TYPE_SHORT:
186         case ATOMIC_TYPE_USHORT:
187                 return 2;
188
189         case ATOMIC_TYPE_BOOL:
190         case ATOMIC_TYPE_INT:
191         case ATOMIC_TYPE_UINT:
192         case ATOMIC_TYPE_LONG:
193         case ATOMIC_TYPE_ULONG:
194         case ATOMIC_TYPE_FLOAT:
195                 return 4;
196
197         case ATOMIC_TYPE_LONGLONG:
198         case ATOMIC_TYPE_ULONGLONG:
199         case ATOMIC_TYPE_DOUBLE:
200                 return 8;
201
202         case ATOMIC_TYPE_LONG_DOUBLE:
203                 return 12;
204
205         case ATOMIC_TYPE_VOID:
206                 return 1;
207
208         case ATOMIC_TYPE_INVALID:
209                 break;
210         }
211         panic("Trying to determine size of invalid atomic type");
212 }
213
214 static unsigned get_compound_type_size(compound_type_t *type)
215 {
216         ir_type *irtype = get_ir_type((type_t*) type);
217         return get_type_size_bytes(irtype);
218 }
219
220 static unsigned get_array_type_size(array_type_t *type)
221 {
222         ir_type *irtype = get_ir_type((type_t*) type);
223         return get_type_size_bytes(irtype);
224 }
225
226 static unsigned get_type_size(type_t *type)
227 {
228         type = skip_typeref(type);
229
230         switch(type->type) {
231         case TYPE_ATOMIC:
232                 return get_atomic_type_size(&type->atomic);
233         case TYPE_ENUM:
234                 return get_mode_size_bytes(mode_Is);
235         case TYPE_COMPOUND_UNION:
236         case TYPE_COMPOUND_STRUCT:
237                 return get_compound_type_size(&type->compound);
238         case TYPE_FUNCTION:
239                 /* just a pointer to the function */
240                 return get_mode_size_bytes(mode_P_code);
241         case TYPE_POINTER:
242                 return get_mode_size_bytes(mode_P_data);
243         case TYPE_ARRAY:
244                 return get_array_type_size(&type->array);
245         case TYPE_BUILTIN:
246         case TYPE_TYPEDEF:
247         case TYPE_TYPEOF:
248         case TYPE_COUNT:
249         case TYPE_INVALID:
250                 break;
251         }
252         panic("Trying to determine size of invalid type");
253 }
254
255 static unsigned count_parameters(const function_type_t *function_type)
256 {
257         unsigned count = 0;
258
259         function_parameter_t *parameter = function_type->parameters;
260         for ( ; parameter != NULL; parameter = parameter->next) {
261                 ++count;
262         }
263
264         return count;
265 }
266
267
268
269
270 static long fold_constant(const expression_t *expression);
271
272 static ir_type *create_atomic_type(const atomic_type_t *type)
273 {
274         ir_mode *mode   = get_atomic_mode(type);
275         ident   *id     = get_mode_ident(mode);
276         ir_type *irtype = new_type_primitive(id, mode);
277
278         return irtype;
279 }
280
281 static ir_type *create_method_type(const function_type_t *function_type)
282 {
283         type_t  *result_type  = function_type->result_type;
284
285         ident   *id           = unique_ident("functiontype");
286         int      n_parameters = count_parameters(function_type);
287         int      n_results    = result_type == type_void ? 0 : 1;
288         ir_type *irtype       = new_type_method(id, n_parameters, n_results);
289
290         if(result_type != type_void) {
291                 ir_type *restype = get_ir_type(result_type);
292                 set_method_res_type(irtype, 0, restype);
293         }
294
295         function_parameter_t *parameter = function_type->parameters;
296         int                   n         = 0;
297         for( ; parameter != NULL; parameter = parameter->next) {
298                 ir_type *p_irtype = get_ir_type(parameter->type);
299                 set_method_param_type(irtype, n, p_irtype);
300                 ++n;
301         }
302
303         if(function_type->variadic || function_type->unspecified_parameters) {
304                 set_method_variadicity(irtype, variadicity_variadic);
305         }
306
307         return irtype;
308 }
309
310 static ir_type *create_pointer_type(pointer_type_t *type)
311 {
312         type_t  *points_to = type->points_to;
313         ir_type *ir_points_to;
314         /* Avoid endless recursion if the points_to type contains this poiner type
315          * again (might be a struct). We therefore first create a void* pointer
316          * and then set the real points_to type
317          */
318         ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
319                                             ir_type_void, mode_P_data);
320         type->type.firm_type  = ir_type;
321
322         ir_points_to = get_ir_type(points_to);
323         set_pointer_points_to_type(ir_type, ir_points_to);
324
325         return ir_type;
326 }
327
328 static ir_type *create_array_type(array_type_t *type)
329 {
330         type_t  *element_type    = type->element_type;
331         ir_type *ir_element_type = get_ir_type(element_type);
332
333         ident   *id      = unique_ident("array");
334         ir_type *ir_type = new_type_array(id, 1, ir_element_type);
335
336         if(type->size != NULL) {
337                 int n_elements = fold_constant(type->size);
338
339                 set_array_bounds_int(ir_type, 0, 0, n_elements);
340
341                 size_t elemsize = get_type_size_bytes(ir_element_type);
342                 int align = get_type_alignment_bytes(ir_element_type);
343                 if(elemsize % align > 0) {
344                         elemsize += align - (elemsize % align);
345                 }
346                 set_type_size_bytes(ir_type, n_elements * elemsize);
347                 set_type_alignment_bytes(ir_type, align);
348                 set_type_state(ir_type, layout_fixed);
349         }
350
351         return ir_type;
352 }
353
354 #define INVALID_TYPE ((ir_type_ptr)-1)
355
356 static ir_type *create_struct_type(compound_type_t *type)
357 {
358         symbol_t *symbol = type->declaration->symbol;
359         ident    *id;
360         if(symbol != NULL) {
361                 id = unique_ident(symbol->string);
362         } else {
363                 id = unique_ident("__anonymous_struct");
364         }
365         ir_type *ir_type = new_type_struct(id);
366
367         type->type.firm_type = ir_type;
368
369         int align_all = 1;
370         int offset    = 0;
371         declaration_t *entry = type->declaration->context.declarations;
372         for( ; entry != NULL; entry = entry->next) {
373                 if(entry->namespc != NAMESPACE_NORMAL)
374                         continue;
375
376                 ident       *ident         = new_id_from_str(entry->symbol->string);
377                 ir_type_ptr  entry_ir_type = get_ir_type(entry->type);
378
379                 int entry_size      = get_type_size_bytes(entry_ir_type);
380                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
381                 int misalign        = offset % entry_alignment;
382                 if (misalign != 0)
383                         offset += entry_alignment - misalign;
384
385                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
386                 set_entity_offset(entity, offset);
387                 add_struct_member(ir_type, entity);
388                 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
389                 entry->v.entity         = entity;
390
391                 offset += entry_size;
392                 if(entry_alignment > align_all) {
393                         if(entry_alignment % align_all != 0) {
394                                 panic("Uneven alignments not supported yet");
395                         }
396                         align_all = entry_alignment;
397                 }
398         }
399
400         int misalign = offset % align_all;
401         offset += misalign;
402         set_type_alignment_bytes(ir_type, align_all);
403         set_type_size_bytes(ir_type, offset);
404         set_type_state(ir_type, layout_fixed);
405
406         return ir_type;
407 }
408
409 static ir_type *create_union_type(compound_type_t *type)
410 {
411         declaration_t *declaration = type->declaration;
412         symbol_t      *symbol      = declaration->symbol;
413         ident         *id;
414         if(symbol != NULL) {
415                 id = unique_ident(symbol->string);
416         } else {
417                 id = unique_ident("__anonymous_union");
418         }
419         ir_type  *ir_type = new_type_union(id);
420
421         type->type.firm_type = ir_type;
422
423         int align_all = 1;
424         int size      = 0;
425         declaration_t *entry = declaration->context.declarations;
426         for( ; entry != NULL; entry = entry->next) {
427                 if(entry->namespc != NAMESPACE_NORMAL)
428                         continue;
429
430                 ident       *ident         = new_id_from_str(entry->symbol->string);
431                 ir_type_ptr  entry_ir_type = get_ir_type(entry->type);
432
433                 int entry_size      = get_type_size_bytes(entry_ir_type);
434                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
435
436                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
437                 add_union_member(ir_type, entity);
438                 set_entity_offset(entity, 0);
439                 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
440                 entry->v.entity         = entity;
441
442                 if(entry_size > size) {
443                         size = entry_size;
444                 }
445                 if(entry_alignment > align_all) {
446                         if(entry_alignment % align_all != 0) {
447                                 panic("Uneven alignments not supported yet");
448                         }
449                         align_all = entry_alignment;
450                 }
451         }
452
453         set_type_alignment_bytes(ir_type, align_all);
454         set_type_size_bytes(ir_type, size);
455         set_type_state(ir_type, layout_fixed);
456
457         return ir_type;
458 }
459
460 static ir_node *expression_to_firm(const expression_t *expression);
461 static inline ir_mode *get_ir_mode(type_t *type);
462
463 static ir_type *create_enum_type(enum_type_t *const type)
464 {
465         type->type.firm_type = ir_type_int;
466
467         ir_mode *const mode    = get_ir_mode((type_t*) type);
468         tarval  *const one     = get_mode_one(mode);
469         tarval  *      tv_next = get_tarval_null(mode);
470
471         declaration_t *declaration = type->declaration->next;
472         for (; declaration != NULL; declaration = declaration->next) {
473                 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
474                         break;
475
476                 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
477
478                 expression_t *const init = declaration->init.enum_value;
479                 if (init != NULL) {
480                         ir_node *const cnst = expression_to_firm(init);
481                         if (!is_Const(cnst)) {
482                                 panic("couldn't fold constant");
483                         }
484                         tv_next = get_Const_tarval(cnst);
485                 }
486                 declaration->v.enum_val = tv_next;
487                 tv_next = tarval_add(tv_next, one);
488         }
489
490         return ir_type_int;
491 }
492
493 static ir_type *get_ir_type(type_t *type)
494 {
495         assert(type != NULL);
496
497         type = skip_typeref(type);
498
499         if(type->base.firm_type != NULL) {
500                 assert(type->base.firm_type != INVALID_TYPE);
501                 return type->base.firm_type;
502         }
503
504         ir_type *firm_type = NULL;
505         switch(type->type) {
506         case TYPE_ATOMIC:
507                 firm_type = create_atomic_type(&type->atomic);
508                 break;
509         case TYPE_FUNCTION:
510                 firm_type = create_method_type(&type->function);
511                 break;
512         case TYPE_POINTER:
513                 firm_type = create_pointer_type(&type->pointer);
514                 break;
515         case TYPE_ARRAY:
516                 firm_type = create_array_type(&type->array);
517                 break;
518         case TYPE_COMPOUND_STRUCT:
519                 firm_type = create_struct_type(&type->compound);
520                 break;
521         case TYPE_COMPOUND_UNION:
522                 firm_type = create_union_type(&type->compound);
523                 break;
524         case TYPE_ENUM:
525                 firm_type = create_enum_type(&type->enumt);
526                 break;
527         case TYPE_BUILTIN:
528         case TYPE_TYPEOF:
529         case TYPE_TYPEDEF:
530         case TYPE_COUNT:
531         case TYPE_INVALID:
532                 break;
533         }
534         if(firm_type == NULL)
535                 panic("unknown type found");
536
537         type->base.firm_type = firm_type;
538         return firm_type;
539 }
540
541 static inline ir_mode *get_ir_mode(type_t *type)
542 {
543         ir_type *irtype = get_ir_type(type);
544
545         /* firm doesn't report a mode for arrays somehow... */
546         if(is_Array_type(irtype)) {
547                 return mode_P;
548         }
549
550         ir_mode *mode = get_type_mode(irtype);
551         assert(mode != NULL);
552         return mode;
553 }
554
555 static ir_entity* get_function_entity(declaration_t *declaration)
556 {
557         if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
558                 return declaration->v.entity;
559         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
560
561         symbol_t *symbol = declaration->symbol;
562         ident    *id     = new_id_from_str(symbol->string);
563
564         ir_type  *global_type    = get_glob_type();
565         ir_type  *ir_type_method = get_ir_type(declaration->type);
566         assert(is_Method_type(ir_type_method));
567
568         ir_entity *entity = new_entity(global_type, id, ir_type_method);
569         set_entity_ld_ident(entity, id);
570         if(declaration->storage_class == STORAGE_CLASS_STATIC
571                         || declaration->is_inline) {
572                 set_entity_visibility(entity, visibility_local);
573         } else if(declaration->init.statement != NULL) {
574                 set_entity_visibility(entity, visibility_external_visible);
575         } else {
576                 set_entity_visibility(entity, visibility_external_allocated);
577         }
578
579         declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
580         declaration->v.entity         = entity;
581
582         return entity;
583 }
584
585 static dbg_info *get_dbg_info(const source_position_t *pos)
586 {
587         return (dbg_info*) pos;
588 }
589
590 static ir_node *const_to_firm(const const_t *cnst)
591 {
592         dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
593         ir_mode  *mode = get_ir_mode(cnst->expression.datatype);
594
595         char    buf[128];
596         tarval *tv;
597         size_t  len;
598         if(mode_is_float(mode)) {
599                 tv = new_tarval_from_double(cnst->v.float_value, mode);
600         } else {
601                 if(mode_is_signed(mode)) {
602                         len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
603                 } else {
604                         len = snprintf(buf, sizeof(buf), "%llu", cnst->v.int_value);
605                 }
606                 tv = new_tarval_from_str(buf, len, mode);
607         }
608
609         return new_d_Const(dbgi, mode, tv);
610 }
611
612 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
613 {
614         assert(entity != NULL);
615         union symconst_symbol sym;
616         sym.entity_p = entity;
617         return new_d_SymConst(dbgi, sym, symconst_addr_ent);
618 }
619
620 static ir_node *string_to_firm(const source_position_t *const src_pos,
621                                const char *const id_prefix,
622                                const char *const string)
623 {
624         ir_type *const global_type = get_glob_type();
625         ir_type *const type        = new_type_array(unique_ident("strtype"), 1,
626                                                     ir_type_const_char);
627
628         ident     *const id     = unique_ident(id_prefix);
629         ir_entity *const entity = new_entity(global_type, id, type);
630         set_entity_ld_ident(entity, id);
631         set_entity_variability(entity, variability_constant);
632
633         ir_type *const elem_type = ir_type_const_char;
634         ir_mode *const mode      = get_type_mode(elem_type);
635
636         const size_t slen = strlen(string) + 1;
637
638         set_array_lower_bound_int(type, 0, 0);
639         set_array_upper_bound_int(type, 0, slen);
640         set_type_size_bytes(type, slen);
641         set_type_state(type, layout_fixed);
642
643         tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
644         for(size_t i = 0; i < slen; ++i) {
645                 tvs[i] = new_tarval_from_long(string[i], mode);
646         }
647
648         set_array_entity_values(entity, tvs, slen);
649         free(tvs);
650
651         dbg_info *const dbgi = get_dbg_info(src_pos);
652
653         return create_symconst(dbgi, entity);
654 }
655
656 static ir_node *string_literal_to_firm(const string_literal_t* literal)
657 {
658         return string_to_firm(&literal->expression.source_position, "Lstr",
659                               literal->value);
660 }
661
662 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
663                               dbg_info *const dbgi)
664 {
665         if(is_compound_type(irtype) || is_Array_type(irtype)) {
666                 return addr;
667         }
668
669         ir_mode *const mode     = get_type_mode(irtype);
670         ir_node *const memory   = get_store();
671         ir_node *const load     = new_d_Load(dbgi, memory, addr, mode);
672         ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
673         ir_node *const load_res = new_d_Proj(dbgi, load, mode,   pn_Load_res);
674         set_store(load_mem);
675         return load_res;
676 }
677
678 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
679 {
680         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
681         declaration_t *declaration = ref->declaration;
682         type_t        *type        = skip_typeref(declaration->type);
683
684         switch((declaration_type_t) declaration->declaration_type) {
685         case DECLARATION_TYPE_UNKNOWN:
686                 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
687                         break;
688                 }
689                 get_ir_type(type);
690                 /* FALLTHROUGH */
691
692         case DECLARATION_TYPE_ENUM_ENTRY: {
693                 ir_mode *const mode = get_ir_mode(type);
694                 return new_Const(mode, declaration->v.enum_val);
695         }
696
697         case DECLARATION_TYPE_LOCAL_VARIABLE: {
698                 ir_mode *mode = get_ir_mode(type);
699                 return get_value(declaration->v.value_number, mode);
700         }
701         case DECLARATION_TYPE_FUNCTION: {
702                 return create_symconst(dbgi, declaration->v.entity);
703         }
704         case DECLARATION_TYPE_GLOBAL_VARIABLE: {
705                 ir_entity *entity   = declaration->v.entity;
706                 ir_node   *symconst = create_symconst(dbgi, entity);
707                 ir_type   *irtype   = get_entity_type(entity);
708                 return deref_address(irtype, symconst, dbgi);
709         }
710         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
711                 ir_entity *entity = declaration->v.entity;
712                 ir_node   *frame  = get_irg_frame(current_ir_graph);
713                 ir_node   *sel    = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
714                 ir_type   *irtype = get_entity_type(entity);
715                 return deref_address(irtype, sel, dbgi);
716         }
717
718         case DECLARATION_TYPE_COMPOUND_MEMBER:
719         case DECLARATION_TYPE_LABEL_BLOCK:
720                 panic("not implemented reference type");
721         }
722
723         panic("reference to declaration with unknown type found");
724 }
725
726 static ir_node *reference_addr(const reference_expression_t *ref)
727 {
728         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
729         declaration_t *declaration = ref->declaration;
730
731         switch((declaration_type_t) declaration->declaration_type) {
732         case DECLARATION_TYPE_UNKNOWN:
733                 break;
734         case DECLARATION_TYPE_LOCAL_VARIABLE:
735                 panic("local variable without entity has no address");
736         case DECLARATION_TYPE_FUNCTION: {
737                 return create_symconst(dbgi, declaration->v.entity);
738         }
739         case DECLARATION_TYPE_GLOBAL_VARIABLE: {
740                 ir_entity *entity   = declaration->v.entity;
741                 ir_node   *symconst = create_symconst(dbgi, entity);
742                 return symconst;
743         }
744         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
745                 ir_entity *entity = declaration->v.entity;
746                 ir_node   *frame  = get_irg_frame(current_ir_graph);
747                 ir_node   *sel    = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
748
749                 return sel;
750         }
751
752         case DECLARATION_TYPE_ENUM_ENTRY:
753                 panic("trying to reference enum entry");
754
755         case DECLARATION_TYPE_COMPOUND_MEMBER:
756         case DECLARATION_TYPE_LABEL_BLOCK:
757                 panic("not implemented reference type");
758         }
759
760         panic("reference to declaration with unknown type found");
761 }
762
763 static ir_node *process_builtin_call(const call_expression_t *call)
764 {
765         dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
766
767         assert(call->function->type == EXPR_BUILTIN_SYMBOL);
768         builtin_symbol_expression_t *builtin
769                 = (builtin_symbol_expression_t*) call->function;
770         symbol_t *symbol = builtin->symbol;
771
772         if(symbol == symbol_alloca) {
773                 if(call->arguments == NULL || call->arguments->next != NULL) {
774                         panic("invalid number of parameters on __builtin_alloca");
775                 }
776                 expression_t *argument = call->arguments->expression;
777                 ir_node      *size     = expression_to_firm(argument);
778
779                 ir_node *store  = get_store();
780                 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
781                                               stack_alloc);
782                 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
783                 set_store(proj_m);
784                 ir_node *res    = new_Proj(alloca, mode_P_data, pn_Alloc_res);
785
786                 return res;
787         } else {
788                 panic("Unsupported builtin found\n");
789         }
790 }
791
792 static ir_node *call_expression_to_firm(const call_expression_t *call)
793 {
794         assert(get_cur_block() != NULL);
795
796         expression_t  *function = call->function;
797         if(function->type == EXPR_BUILTIN_SYMBOL) {
798                 return process_builtin_call(call);
799         }
800         ir_node       *callee   = expression_to_firm(function);
801
802         function_type_t *function_type;
803         assert(function->datatype->type == TYPE_POINTER);
804         pointer_type_t *const ptr_type = &function->datatype->pointer;
805         assert(ptr_type->points_to->type == TYPE_FUNCTION);
806         function_type = &ptr_type->points_to->function;
807
808         int              n_parameters = 0;
809         call_argument_t *argument     = call->arguments;
810         for( ; argument != NULL; argument = argument->next) {
811                 ++n_parameters;
812         }
813
814         ir_type *ir_method_type  = get_ir_type((type_t*) function_type);
815         ir_type *new_method_type = NULL;
816         if(function_type->variadic || function_type->unspecified_parameters) {
817                 /* we need to construct a new method type matching the call
818                  * arguments... */
819                 int n_res       = get_method_n_ress(ir_method_type);
820                 new_method_type = new_type_method(unique_ident("calltype"),
821                                                   n_parameters, n_res);
822                 set_method_calling_convention(new_method_type,
823                                get_method_calling_convention(ir_method_type));
824                 set_method_additional_properties(new_method_type,
825                                get_method_additional_properties(ir_method_type));
826
827                 for(int i = 0; i < n_res; ++i) {
828                         set_method_res_type(new_method_type, i,
829                                             get_method_res_type(ir_method_type, i));
830                 }
831         }
832         ir_node *in[n_parameters];
833
834         argument = call->arguments;
835         int n = 0;
836         for( ; argument != NULL; argument = argument->next) {
837                 expression_t *expression = argument->expression;
838                 ir_node      *arg_node   = expression_to_firm(expression);
839
840                 in[n] = arg_node;
841                 if(new_method_type != NULL) {
842                         ir_type *irtype = get_ir_type(expression->datatype);
843                         set_method_param_type(new_method_type, n, irtype);
844                 }
845
846                 n++;
847         }
848         assert(n == n_parameters);
849
850         if(new_method_type != NULL)
851                 ir_method_type = new_method_type;
852
853         dbg_info *dbgi  = get_dbg_info(&call->expression.source_position);
854         ir_node  *store = get_store();
855         ir_node  *node  = new_d_Call(dbgi, store, callee, n_parameters, in,
856                                      ir_method_type);
857         ir_node  *mem   = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
858         set_store(mem);
859
860         type_t  *result_type = function_type->result_type;
861         ir_node *result      = NULL;
862         if(result_type != type_void) {
863                 ir_mode *mode    = get_ir_mode(result_type);
864                 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
865                 result           = new_d_Proj(dbgi, resproj, mode, 0);
866         }
867
868         return result;
869 }
870
871 static void statement_to_firm(statement_t *statement);
872 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
873
874 static ir_node *expression_to_addr(const expression_t *expression);
875 static void create_condition_evaluation(const expression_t *expression,
876                                         ir_node *true_block,
877                                         ir_node *false_block);
878
879 static void set_value_for_expression(const expression_t *expression,
880                                      ir_node *value)
881 {
882         if(expression->type == EXPR_REFERENCE) {
883                 reference_expression_t *ref = (reference_expression_t*) expression;
884
885                 declaration_t *declaration = ref->declaration;
886                 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
887                 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
888                         set_value(declaration->v.value_number, value);
889                         return;
890                 }
891         }
892
893         dbg_info *dbgi      = get_dbg_info(&expression->source_position);
894         ir_node  *addr      = expression_to_addr(expression);
895         assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
896         ir_node  *memory    = get_store();
897         ir_node  *store     = new_d_Store(dbgi, memory, addr, value);
898         ir_node  *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
899         set_store(store_mem);
900 }
901
902 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
903 {
904         ir_mode *value_mode = get_irn_mode(value);
905
906         if (value_mode == dest_mode || is_Bad(value))
907                 return value;
908
909         if(dest_mode == mode_b) {
910                 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
911                 ir_node *cmp  = new_d_Cmp(dbgi, value, zero);
912                 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
913                 return proj;
914         }
915
916         return new_d_Conv(dbgi, value, dest_mode);
917 }
918
919 static ir_node *create_incdec(const unary_expression_t *expression)
920 {
921         dbg_info     *dbgi  = get_dbg_info(&expression->expression.source_position);
922         type_t       *type  = expression->expression.datatype;
923         ir_mode      *mode  = get_ir_mode(type);
924         expression_t *value = expression->value;
925
926         ir_node *value_node = expression_to_firm(value);
927
928         ir_node *offset;
929         if(type->type == TYPE_POINTER) {
930                 pointer_type_t *pointer_type = &type->pointer;
931                 unsigned        elem_size    = get_type_size(pointer_type->points_to);
932                 offset = new_Const_long(mode_Is, elem_size);
933         } else {
934                 assert(is_type_arithmetic(type));
935                 offset = new_Const(mode, get_mode_one(mode));
936         }
937
938         switch(expression->type) {
939         case UNEXPR_POSTFIX_INCREMENT: {
940                 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
941                 set_value_for_expression(value, new_value);
942                 return value_node;
943         }
944         case UNEXPR_POSTFIX_DECREMENT: {
945                 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
946                 set_value_for_expression(value, new_value);
947                 return value_node;
948         }
949         case UNEXPR_PREFIX_INCREMENT: {
950                 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
951                 set_value_for_expression(value, new_value);
952                 return new_value;
953         }
954         case UNEXPR_PREFIX_DECREMENT: {
955                 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
956                 set_value_for_expression(value, new_value);
957                 return new_value;
958         }
959         default:
960                 panic("no incdec expr in create_incdec");
961                 return NULL;
962         }
963 }
964
965 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
966 {
967         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
968         type_t   *type = skip_typeref(expression->expression.datatype);
969
970         if(expression->type == UNEXPR_TAKE_ADDRESS)
971                 return expression_to_addr(expression->value);
972
973         const expression_t *value      = expression->value;
974         ir_node            *value_node = expression_to_firm(value);
975
976         switch(expression->type) {
977         case UNEXPR_NEGATE:
978                 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
979         case UNEXPR_PLUS:
980                 return value_node;
981         case UNEXPR_BITWISE_NEGATE:
982                 return new_d_Not(dbgi, value_node, get_ir_mode(type));
983         case UNEXPR_NOT: {
984                 if(get_irn_mode(value_node) != mode_b) {
985                         value_node = create_conv(dbgi, value_node, mode_b);
986                 }
987                 value_node = new_d_Not(dbgi, value_node, mode_b);
988                 ir_mode *const mode = get_ir_mode(type);
989                 if(mode != mode_b) {
990                         value_node = create_conv(dbgi, value_node, mode);
991                 }
992                 return value_node;
993         }
994         case UNEXPR_DEREFERENCE: {
995                 ir_type *irtype = get_ir_type(type);
996                 return deref_address(irtype, value_node, dbgi);
997         }
998         case UNEXPR_POSTFIX_INCREMENT:
999         case UNEXPR_POSTFIX_DECREMENT:
1000         case UNEXPR_PREFIX_INCREMENT:
1001         case UNEXPR_PREFIX_DECREMENT:
1002                 return create_incdec(expression);
1003         case UNEXPR_CAST:
1004                 return create_conv(dbgi, value_node, get_ir_mode(type));
1005
1006         case UNEXPR_TAKE_ADDRESS:
1007         case UNEXPR_INVALID:
1008                 break;
1009         }
1010         panic("invalid UNEXPR type found");
1011 }
1012
1013 static long get_pnc(binary_expression_type_t type)
1014 {
1015         switch(type) {
1016         case BINEXPR_EQUAL:        return pn_Cmp_Eq;
1017         case BINEXPR_NOTEQUAL:     return pn_Cmp_Lg;
1018         case BINEXPR_LESS:         return pn_Cmp_Lt;
1019         case BINEXPR_LESSEQUAL:    return pn_Cmp_Le;
1020         case BINEXPR_GREATER:      return pn_Cmp_Gt;
1021         case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1022         default:
1023                 break;
1024         }
1025         panic("trying to get pn_Cmp from non-comparison binexpr type");
1026 }
1027
1028 static ir_node *create_lazy_op(const binary_expression_t *expression)
1029 {
1030         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1031         type_t   *type = expression->expression.datatype;
1032         ir_mode  *mode = get_ir_mode(type);
1033
1034         ir_node *cur_block = get_cur_block();
1035
1036         ir_node *one_block = new_immBlock();
1037         ir_node *one       = new_Const(mode, get_mode_one(mode));
1038         ir_node *jmp_one   = new_d_Jmp(dbgi);
1039
1040         ir_node *zero_block = new_immBlock();
1041         ir_node *zero       = new_Const(mode, get_mode_null(mode));
1042         ir_node *jmp_zero   = new_d_Jmp(dbgi);
1043
1044         set_cur_block(cur_block);
1045         create_condition_evaluation((const expression_t*) expression,
1046                                     one_block, zero_block);
1047         mature_immBlock(one_block);
1048         mature_immBlock(zero_block);
1049
1050         ir_node *common_block = new_immBlock();
1051         add_immBlock_pred(common_block, jmp_one);
1052         add_immBlock_pred(common_block, jmp_zero);
1053         mature_immBlock(common_block);
1054
1055         ir_node *in[2] = { one, zero };
1056         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
1057
1058         return val;
1059 }
1060
1061 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1062                                             ir_node *right, ir_mode *mode);
1063
1064 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1065                                         create_arithmetic_func func)
1066 {
1067         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1068         ir_node  *left  = expression_to_firm(expression->left);
1069         ir_node  *right = expression_to_firm(expression->right);
1070         type_t   *type  = expression->right->datatype;
1071         /* be careful with the modes, because in arithmetic assign nodes only
1072          * the right operand has the mode of the arithmetic already */
1073         ir_mode  *mode  = get_ir_mode(type);
1074         left            = create_conv(dbgi, left, mode);
1075         ir_node  *res   = func(dbgi, left, right, mode);
1076
1077         return res;
1078 }
1079
1080 static ir_node *pointer_arithmetic(ir_node  *const pointer,
1081                                    ir_node  *      integer,
1082                                    type_t   *const type,
1083                                    dbg_info *const dbgi,
1084                                    const create_arithmetic_func func)
1085 {
1086         pointer_type_t *const pointer_type = (pointer_type_t*)type;
1087         type_t         *const points_to    = pointer_type->points_to;
1088         const unsigned        elem_size    = get_type_size(points_to);
1089
1090         assert(elem_size >= 1);
1091         if (elem_size > 1) {
1092                 integer             = create_conv(dbgi, integer, mode_Is);
1093                 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1094                 ir_node *const mul  = new_d_Mul(dbgi, integer, cnst, mode_Is);
1095                 integer = mul;
1096         }
1097
1098         ir_mode *const mode = get_ir_mode(type);
1099         return func(dbgi, pointer, integer, mode);
1100 }
1101
1102 static ir_node *create_arithmetic_assign_binop(
1103                 const binary_expression_t *expression, create_arithmetic_func func)
1104 {
1105         dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1106         type_t   *const type = expression->expression.datatype;
1107         ir_node  *value;
1108
1109         if (type->type == TYPE_POINTER) {
1110                 ir_node        *const pointer = expression_to_firm(expression->left);
1111                 ir_node        *      integer = expression_to_firm(expression->right);
1112                 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1113         } else {
1114                 value = create_arithmetic_binop(expression, func);
1115         }
1116
1117         ir_mode  *const mode = get_ir_mode(type);
1118         value = create_conv(dbgi, value, mode);
1119         set_value_for_expression(expression->left, value);
1120
1121         return value;
1122 }
1123
1124 static ir_node *create_add(const binary_expression_t *expression)
1125 {
1126         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1127         ir_node  *left  = expression_to_firm(expression->left);
1128         ir_node  *right = expression_to_firm(expression->right);
1129         type_t   *type  = expression->expression.datatype;
1130
1131         expression_t *expr_left  = expression->left;
1132         expression_t *expr_right = expression->right;
1133         type_t       *type_left  = skip_typeref(expr_left->datatype);
1134         type_t       *type_right = skip_typeref(expr_right->datatype);
1135
1136         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1137                 ir_mode *const mode = get_ir_mode(type);
1138                 return new_d_Add(dbgi, left, right, mode);
1139         }
1140
1141         if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1142                 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1143         } else {
1144                 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1145                 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1146         }
1147 }
1148
1149 static ir_node *create_sub(const binary_expression_t *expression)
1150 {
1151         dbg_info *const dbgi  = get_dbg_info(&expression->expression.source_position);
1152         expression_t *const expr_left  = expression->left;
1153         expression_t *const expr_right = expression->right;
1154         ir_node      *const left       = expression_to_firm(expr_left);
1155         ir_node      *const right      = expression_to_firm(expr_right);
1156         type_t       *const type       = expression->expression.datatype;
1157         type_t       *const type_left  = skip_typeref(expr_left->datatype);
1158         type_t       *const type_right = skip_typeref(expr_right->datatype);
1159
1160         if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1161                 ir_mode *const mode = get_ir_mode(type);
1162                 return new_d_Sub(dbgi, left, right, mode);
1163         } else if (type_left->type == TYPE_POINTER
1164                         && type_right->type == TYPE_POINTER) {
1165                 const pointer_type_t *const ptr_type = &type_left->pointer;
1166                 const unsigned elem_size             = get_type_size(ptr_type->points_to);
1167                 ir_mode *const mode   = get_ir_mode(type);
1168                 ir_node *const sub    = new_d_Sub(dbgi, left, right, mode);
1169                 ir_node *const cnst   = new_Const_long(mode_Is, (long)elem_size);
1170                 ir_node *const no_mem = new_NoMem();
1171                 ir_node *const div    = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1172                                                   op_pin_state_floats);
1173                 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1174         }
1175
1176         assert(type_left->type == TYPE_POINTER);
1177         return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1178 }
1179
1180 static ir_node *create_shift(const binary_expression_t *expression)
1181 {
1182         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1183         ir_node  *left  = expression_to_firm(expression->left);
1184         ir_node  *right = expression_to_firm(expression->right);
1185         type_t   *type  = expression->expression.datatype;
1186         ir_mode  *mode  = get_ir_mode(type);
1187
1188         /* firm always wants the shift count to be unsigned */
1189         right = create_conv(dbgi, right, mode_Iu);
1190
1191         ir_node *res;
1192
1193         switch(expression->type) {
1194         case BINEXPR_SHIFTLEFT_ASSIGN:
1195         case BINEXPR_SHIFTLEFT:
1196                 res = new_d_Shl(dbgi, left, right, mode);
1197                 break;
1198         case BINEXPR_SHIFTRIGHT_ASSIGN:
1199         case BINEXPR_SHIFTRIGHT: {
1200                  expression_t *expr_left = expression->left;
1201                  type_t       *type_left = skip_typeref(expr_left->datatype);
1202
1203                  if(is_type_signed(type_left)) {
1204                         res = new_d_Shrs(dbgi, left, right, mode);
1205                  } else {
1206                          res = new_d_Shr(dbgi, left, right, mode);
1207                  }
1208                  break;
1209         }
1210         default:
1211                 panic("create shift op called for non-shift op");
1212         }
1213
1214         return res;
1215 }
1216
1217
1218 static ir_node *create_divmod(const binary_expression_t *expression)
1219 {
1220         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1221         ir_node  *left  = expression_to_firm(expression->left);
1222         ir_node  *right = expression_to_firm(expression->right);
1223         ir_node  *pin   = new_Pin(new_NoMem());
1224         /* be careful with the modes, because in arithmetic assign nodes only
1225          * the right operand has the mode of the arithmetic already */
1226         type_t   *type  = expression->right->datatype;
1227         ir_mode  *mode  = get_ir_mode(type);
1228         left            = create_conv(dbgi, left, mode);
1229         ir_node  *op;
1230         ir_node  *res;
1231
1232         switch (expression->type)  {
1233                 case BINEXPR_DIV:
1234                 case BINEXPR_DIV_ASSIGN:
1235                         if(mode_is_float(mode)) {
1236                                 op  = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1237                                 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1238                         } else {
1239                                 op  = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1240                                 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1241                         }
1242                         break;
1243
1244                 case BINEXPR_MOD:
1245                 case BINEXPR_MOD_ASSIGN:
1246                         assert(!mode_is_float(mode));
1247                         op  = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1248                         res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1249                         break;
1250
1251                 default: panic("unexpected binary expression type in create_divmod()");
1252         }
1253
1254         return res;
1255 }
1256
1257 static ir_node *create_arithmetic_assign_divmod(
1258                 const binary_expression_t *expression)
1259 {
1260         ir_node  *      value = create_divmod(expression);
1261         dbg_info *const dbgi  = get_dbg_info(&expression->expression.source_position);
1262         type_t   *const type  = expression->expression.datatype;
1263         ir_mode  *const mode  = get_ir_mode(type);
1264
1265         assert(type->type != TYPE_POINTER);
1266
1267         value = create_conv(dbgi, value, mode);
1268         set_value_for_expression(expression->left, value);
1269
1270         return value;
1271 }
1272
1273 static ir_node *create_arithmetic_assign_shift(
1274                 const binary_expression_t *expression)
1275 {
1276         ir_node  *      value = create_shift(expression);
1277         dbg_info *const dbgi  = get_dbg_info(&expression->expression.source_position);
1278         type_t   *const type  = expression->expression.datatype;
1279         ir_mode  *const mode  = get_ir_mode(type);
1280
1281         value = create_conv(dbgi, value, mode);
1282         set_value_for_expression(expression->left, value);
1283
1284         return value;
1285 }
1286
1287 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1288 {
1289         binary_expression_type_t type = expression->type;
1290         switch(type) {
1291         case BINEXPR_EQUAL:
1292         case BINEXPR_NOTEQUAL:
1293         case BINEXPR_LESS:
1294         case BINEXPR_LESSEQUAL:
1295         case BINEXPR_GREATER:
1296         case BINEXPR_GREATEREQUAL: {
1297                 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1298                 ir_node *left  = expression_to_firm(expression->left);
1299                 ir_node *right = expression_to_firm(expression->right);
1300                 ir_node *cmp   = new_d_Cmp(dbgi, left, right);
1301                 long     pnc   = get_pnc(type);
1302                 ir_node *proj  = new_d_Proj(dbgi, cmp, mode_b, pnc);
1303                 return proj;
1304         }
1305         case BINEXPR_ASSIGN: {
1306                 ir_node *right = expression_to_firm(expression->right);
1307                 set_value_for_expression(expression->left, right);
1308                 return right;
1309         }
1310         case BINEXPR_ADD:
1311                 return create_add(expression);
1312         case BINEXPR_SUB:
1313                 return create_sub(expression);
1314         case BINEXPR_MUL:
1315                 return create_arithmetic_binop(expression, new_d_Mul);
1316         case BINEXPR_BITWISE_AND:
1317                 return create_arithmetic_binop(expression, new_d_And);
1318         case BINEXPR_BITWISE_OR:
1319                 return create_arithmetic_binop(expression, new_d_Or);
1320         case BINEXPR_BITWISE_XOR:
1321                 return create_arithmetic_binop(expression, new_d_Eor);
1322         case BINEXPR_SHIFTLEFT:
1323         case BINEXPR_SHIFTRIGHT:
1324                 return create_shift(expression);
1325         case BINEXPR_DIV:
1326         case BINEXPR_MOD:
1327                 return create_divmod(expression);
1328         case BINEXPR_LOGICAL_AND:
1329         case BINEXPR_LOGICAL_OR:
1330                 return create_lazy_op(expression);
1331         case BINEXPR_COMMA:
1332                 expression_to_firm(expression->left);
1333                 return expression_to_firm(expression->right);
1334         case BINEXPR_ADD_ASSIGN:
1335                 return create_arithmetic_assign_binop(expression, new_d_Add);
1336         case BINEXPR_SUB_ASSIGN:
1337                 return create_arithmetic_assign_binop(expression, new_d_Sub);
1338         case BINEXPR_MUL_ASSIGN:
1339                 return create_arithmetic_assign_binop(expression, new_d_Mul);
1340         case BINEXPR_DIV_ASSIGN:
1341                 return create_arithmetic_assign_divmod(expression);
1342         case BINEXPR_BITWISE_AND_ASSIGN:
1343                 return create_arithmetic_assign_binop(expression, new_d_And);
1344         case BINEXPR_BITWISE_OR_ASSIGN:
1345                 return create_arithmetic_assign_binop(expression, new_d_Or);
1346         case BINEXPR_BITWISE_XOR_ASSIGN:
1347                 return create_arithmetic_assign_binop(expression, new_d_Eor);
1348         case BINEXPR_SHIFTLEFT_ASSIGN:
1349         case BINEXPR_SHIFTRIGHT_ASSIGN:
1350                 return create_arithmetic_assign_shift(expression);
1351         default:
1352                 panic("TODO binexpr type");
1353         }
1354 }
1355
1356 static ir_node *array_access_addr(const array_access_expression_t *expression)
1357 {
1358         dbg_info *dbgi      = get_dbg_info(&expression->expression.source_position);
1359         ir_node  *base_addr = expression_to_firm(expression->array_ref);
1360         ir_node  *offset    = expression_to_firm(expression->index);
1361         offset              = create_conv(dbgi, offset, mode_Iu);
1362
1363         type_t *ref_type = skip_typeref(expression->array_ref->datatype);
1364         assert(is_type_pointer(ref_type));
1365         pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1366
1367         unsigned elem_size       = get_type_size(pointer_type->points_to);
1368         ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1369         ir_node *real_offset     = new_d_Mul(dbgi, offset, elem_size_const,
1370                                              mode_Iu);
1371         ir_node *result          = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1372
1373         return result;
1374 }
1375
1376 static ir_node *array_access_to_firm(
1377                 const array_access_expression_t *expression)
1378 {
1379         dbg_info *dbgi   = get_dbg_info(&expression->expression.source_position);
1380         ir_node  *addr   = array_access_addr(expression);
1381         type_t   *type   = revert_automatic_type_conversion(
1382                         (const expression_t*) expression);
1383         type             = skip_typeref(type);
1384         ir_type  *irtype = get_ir_type(type);
1385
1386         return deref_address(irtype, addr, dbgi);
1387 }
1388
1389 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1390 {
1391         type_t *type = expression->type;
1392         if(type == NULL) {
1393                 type = expression->size_expression->datatype;
1394                 assert(type != NULL);
1395         }
1396
1397         ir_mode  *mode      = get_ir_mode(expression->expression.datatype);
1398         unsigned  size      = get_type_size(type);
1399         ir_node  *size_node = new_Const_long(mode, size);
1400
1401         return size_node;
1402 }
1403
1404 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1405 {
1406         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1407
1408         ir_node *cur_block   = get_cur_block();
1409
1410         /* create the true block */
1411         ir_node *true_block  = new_immBlock();
1412
1413         ir_node *true_val = expression_to_firm(expression->true_expression);
1414         ir_node *true_jmp = new_Jmp();
1415
1416         /* create the false block */
1417         ir_node *false_block = new_immBlock();
1418
1419         ir_node *false_val = expression_to_firm(expression->false_expression);
1420         ir_node *false_jmp = new_Jmp();
1421
1422         /* create the condition evaluation */
1423         set_cur_block(cur_block);
1424         create_condition_evaluation(expression->condition, true_block, false_block);
1425         mature_immBlock(true_block);
1426         mature_immBlock(false_block);
1427
1428         /* create the common block */
1429         ir_node *common_block = new_immBlock();
1430         add_immBlock_pred(common_block, true_jmp);
1431         add_immBlock_pred(common_block, false_jmp);
1432         mature_immBlock(common_block);
1433
1434         /* TODO improve static semantics, so either both or no values are NULL */
1435         if (true_val == NULL || false_val == NULL) return NULL;
1436
1437         ir_node *in[2] = { true_val, false_val };
1438         ir_mode *mode  = get_irn_mode(true_val);
1439         assert(get_irn_mode(false_val) == mode);
1440         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
1441
1442         return val;
1443 }
1444
1445 static ir_node *select_addr(const select_expression_t *expression)
1446 {
1447         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1448
1449         ir_node *compound_addr = expression_to_firm(expression->compound);
1450
1451         declaration_t *entry = expression->compound_entry;
1452         assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1453         ir_entity     *entity = entry->v.entity;
1454
1455         assert(entity != NULL);
1456
1457         ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1458
1459         return sel;
1460 }
1461
1462 static ir_node *select_to_firm(const select_expression_t *expression)
1463 {
1464         dbg_info *dbgi   = get_dbg_info(&expression->expression.source_position);
1465         ir_node  *addr   = select_addr(expression);
1466         type_t   *type   = revert_automatic_type_conversion(
1467                         (const expression_t*) expression);
1468         type             = skip_typeref(type);
1469         ir_type  *irtype = get_ir_type(type);
1470
1471         return deref_address(irtype, addr, dbgi);
1472 }
1473
1474 /* Values returned by __builtin_classify_type. */
1475 typedef enum gcc_type_class
1476 {
1477         no_type_class = -1,
1478         void_type_class,
1479         integer_type_class,
1480         char_type_class,
1481         enumeral_type_class,
1482         boolean_type_class,
1483         pointer_type_class,
1484         reference_type_class,
1485         offset_type_class,
1486         real_type_class,
1487         complex_type_class,
1488         function_type_class,
1489         method_type_class,
1490         record_type_class,
1491         union_type_class,
1492         array_type_class,
1493         string_type_class,
1494         set_type_class,
1495         file_type_class,
1496         lang_type_class
1497 } gcc_type_class;
1498
1499 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1500 {
1501         const type_t *const type = expr->type_expression->datatype;
1502
1503         gcc_type_class tc;
1504         switch (type->type)
1505         {
1506                 case TYPE_ATOMIC: {
1507                         const atomic_type_t *const atomic_type = &type->atomic;
1508                         switch (atomic_type->atype) {
1509                                 // should not be reached
1510                                 case ATOMIC_TYPE_INVALID:
1511                                         tc = no_type_class;
1512                                         break;
1513
1514                                 // gcc cannot do that
1515                                 case ATOMIC_TYPE_VOID:
1516                                         tc = void_type_class;
1517                                         break;
1518
1519                                 case ATOMIC_TYPE_CHAR:      // gcc handles this as integer
1520                                 case ATOMIC_TYPE_SCHAR:     // gcc handles this as integer
1521                                 case ATOMIC_TYPE_UCHAR:     // gcc handles this as integer
1522                                 case ATOMIC_TYPE_SHORT:
1523                                 case ATOMIC_TYPE_USHORT:
1524                                 case ATOMIC_TYPE_INT:
1525                                 case ATOMIC_TYPE_UINT:
1526                                 case ATOMIC_TYPE_LONG:
1527                                 case ATOMIC_TYPE_ULONG:
1528                                 case ATOMIC_TYPE_LONGLONG:
1529                                 case ATOMIC_TYPE_ULONGLONG:
1530                                 case ATOMIC_TYPE_BOOL:      // gcc handles this as integer
1531                                         tc = integer_type_class;
1532                                         break;
1533
1534                                 case ATOMIC_TYPE_FLOAT:
1535                                 case ATOMIC_TYPE_DOUBLE:
1536                                 case ATOMIC_TYPE_LONG_DOUBLE:
1537                                         tc = real_type_class;
1538                                         break;
1539
1540 #ifdef PROVIDE_COMPLEX
1541                                 case ATOMIC_TYPE_FLOAT_COMPLEX:
1542                                 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1543                                 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1544                                         tc = complex_type_class;
1545                                         break;
1546                                 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1547                                 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1548                                 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1549                                         tc = complex_type_class;
1550                                         break;
1551 #endif
1552
1553                                 default:
1554                                         panic("Unimplemented case in classify_type_to_firm().");
1555                         }
1556                         break;
1557                 }
1558
1559                 case TYPE_ARRAY:           // gcc handles this as pointer
1560                 case TYPE_FUNCTION:        // gcc handles this as pointer
1561                 case TYPE_POINTER:         tc = pointer_type_class; break;
1562                 case TYPE_COMPOUND_STRUCT: tc = record_type_class;  break;
1563                 case TYPE_COMPOUND_UNION:  tc = union_type_class;   break;
1564
1565                 // gcc handles this as integer
1566                 case TYPE_ENUM:            tc = integer_type_class; break;
1567
1568                 default:
1569                         panic("Unimplemented case in classify_type_to_firm().");
1570         }
1571
1572         dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1573         ir_mode  *const mode = mode_Is;
1574         tarval   *const tv   = new_tarval_from_long(tc, mode);
1575         return new_d_Const(dbgi, mode, tv);
1576 }
1577
1578 static ir_node *function_name_to_firm(const string_literal_t *const expr)
1579 {
1580         if (current_function_name == NULL) {
1581                 const source_position_t *const src_pos =
1582                         &expr->expression.source_position;
1583                 const char *const name = current_function_decl->symbol->string;
1584                 current_function_name = string_to_firm(src_pos, "__func__", name);
1585         }
1586
1587         return current_function_name;
1588 }
1589
1590 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1591 {
1592         statement_t *statement = expr->statement;
1593
1594         assert(statement->type == STATEMENT_COMPOUND);
1595         return compound_statement_to_firm((compound_statement_t*) statement);
1596 }
1597
1598 static ir_node *dereference_addr(const unary_expression_t *const expression)
1599 {
1600         assert(expression->type == UNEXPR_DEREFERENCE);
1601         return expression_to_firm(expression->value);
1602 }
1603
1604 static ir_node *expression_to_addr(const expression_t *expression)
1605 {
1606         switch(expression->type) {
1607         case EXPR_REFERENCE:
1608                 return reference_addr((const reference_expression_t*) expression);
1609         case EXPR_ARRAY_ACCESS:
1610                 return array_access_addr((const array_access_expression_t*) expression);
1611         case EXPR_SELECT:
1612                 return select_addr((const select_expression_t*) expression);
1613         case EXPR_UNARY: {
1614                 const unary_expression_t *const unary_expr =
1615                         (const unary_expression_t*)expression;
1616                 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1617                         return dereference_addr(unary_expr);
1618                 }
1619                 break;
1620         }
1621         default:
1622                 break;
1623         }
1624         panic("trying to get address of non-lvalue");
1625 }
1626
1627 static ir_node *_expression_to_firm(const expression_t *expression)
1628 {
1629         switch(expression->type) {
1630         case EXPR_CONST:
1631                 return const_to_firm((const const_t*) expression);
1632         case EXPR_STRING_LITERAL:
1633                 return string_literal_to_firm((const string_literal_t*) expression);
1634         case EXPR_REFERENCE:
1635                 return reference_expression_to_firm(
1636                                 (const reference_expression_t*) expression);
1637         case EXPR_CALL:
1638                 return call_expression_to_firm((const call_expression_t*) expression);
1639         case EXPR_UNARY:
1640                 return unary_expression_to_firm((const unary_expression_t*) expression);
1641         case EXPR_BINARY:
1642                 return binary_expression_to_firm(
1643                                 (const binary_expression_t*) expression);
1644         case EXPR_ARRAY_ACCESS:
1645                 return array_access_to_firm(
1646                                 (const array_access_expression_t*) expression);
1647         case EXPR_SIZEOF:
1648                 return sizeof_to_firm((const sizeof_expression_t*) expression);
1649         case EXPR_CONDITIONAL:
1650                 return conditional_to_firm((const conditional_expression_t*)expression);
1651         case EXPR_SELECT:
1652                 return select_to_firm((const select_expression_t*) expression);
1653         case EXPR_CLASSIFY_TYPE:
1654                 return classify_type_to_firm((const classify_type_expression_t*)expression);
1655         case EXPR_FUNCTION:
1656         case EXPR_PRETTY_FUNCTION:
1657                 return function_name_to_firm((const string_literal_t*)expression);
1658         case EXPR_STATEMENT:
1659                 return statement_expression_to_firm(
1660                                 (const statement_expression_t*) expression);
1661         case EXPR_OFFSETOF:
1662         case EXPR_VA_ARG:
1663         case EXPR_BUILTIN_SYMBOL:
1664                 panic("unimplemented expression found");
1665
1666         case EXPR_UNKNOWN:
1667         case EXPR_INVALID:
1668                 break;
1669         }
1670         panic("invalid expression found");
1671 }
1672
1673 static ir_node *expression_to_firm(const expression_t *expression)
1674 {
1675         ir_node *res = _expression_to_firm(expression);
1676
1677         if(res != NULL && get_irn_mode(res) == mode_b) {
1678                 ir_mode *mode = get_ir_mode(expression->datatype);
1679                 res           = create_conv(NULL, res, mode);
1680         }
1681
1682         return res;
1683 }
1684
1685 static ir_node *expression_to_modeb(const expression_t *expression)
1686 {
1687         ir_node *res = _expression_to_firm(expression);
1688         res          = create_conv(NULL, res, mode_b);
1689
1690         return res;
1691 }
1692
1693 /**
1694  * create a short-circuit expression evaluation that tries to construct
1695  * efficient control flow structures for &&, || and ! expressions
1696  */
1697 static void create_condition_evaluation(const expression_t *expression,
1698                                         ir_node *true_block,
1699                                         ir_node *false_block)
1700 {
1701         switch(expression->type) {
1702         case EXPR_UNARY: {
1703                 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1704                 if(unary_expression->type == UNEXPR_NOT) {
1705                         create_condition_evaluation(unary_expression->value, false_block,
1706                                                     true_block);
1707                         return;
1708                 }
1709                 break;
1710         }
1711         case EXPR_BINARY: {
1712                 binary_expression_t *binary_expression
1713                         = (binary_expression_t*) expression;
1714                 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1715                         ir_node *cur_block   = get_cur_block();
1716                         ir_node *extra_block = new_immBlock();
1717                         set_cur_block(cur_block);
1718                         create_condition_evaluation(binary_expression->left, extra_block,
1719                                                     false_block);
1720                         mature_immBlock(extra_block);
1721                         set_cur_block(extra_block);
1722                         create_condition_evaluation(binary_expression->right, true_block,
1723                                                     false_block);
1724                         return;
1725                 }
1726                 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1727                         ir_node *cur_block   = get_cur_block();
1728                         ir_node *extra_block = new_immBlock();
1729                         set_cur_block(cur_block);
1730                         create_condition_evaluation(binary_expression->left, true_block,
1731                                                     extra_block);
1732                         mature_immBlock(extra_block);
1733                         set_cur_block(extra_block);
1734                         create_condition_evaluation(binary_expression->right, true_block,
1735                                                     false_block);
1736                         return;
1737                 }
1738                 break;
1739         }
1740         default:
1741                 break;
1742         }
1743
1744         dbg_info *dbgi       = get_dbg_info(&expression->source_position);
1745         ir_node  *condition  = expression_to_modeb(expression);
1746         ir_node  *cond       = new_d_Cond(dbgi, condition);
1747         ir_node  *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1748         ir_node  *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1749
1750         add_immBlock_pred(true_block, true_proj);
1751         add_immBlock_pred(false_block, false_proj);
1752
1753         set_cur_block(NULL);
1754 }
1755
1756
1757 static void return_statement_to_firm(return_statement_t *statement)
1758 {
1759         if(get_cur_block() == NULL)
1760                 return;
1761
1762
1763         ir_type *func_irtype = get_ir_type(current_function_decl->type);
1764
1765         ir_node *value = NULL;
1766         if(statement->return_value != NULL) {
1767                 value = expression_to_firm(statement->return_value);
1768         }
1769
1770         ir_node *in[1];
1771         int      in_len;
1772         if(get_method_n_ress(func_irtype) > 0) {
1773                 if(value == NULL) {
1774                         ir_type *res_type = get_method_res_type(func_irtype, 0);
1775                         ir_mode *mode     = get_type_mode(res_type);
1776                         value             = new_Unknown(mode);
1777                 }
1778
1779                 in[0]  = value;
1780                 in_len = 1;
1781         } else {
1782                 in_len = 0;
1783         }
1784
1785         dbg_info *dbgi  = get_dbg_info(&statement->statement.source_position);
1786         ir_node  *store = get_store();
1787         ir_node  *ret   = new_d_Return(dbgi, store, in_len, in);
1788
1789         ir_node *end_block = get_irg_end_block(current_ir_graph);
1790         add_immBlock_pred(end_block, ret);
1791
1792         set_cur_block(NULL);
1793 }
1794
1795 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1796 {
1797         if(get_cur_block() == NULL)
1798                 return NULL;
1799
1800         return expression_to_firm(statement->expression);
1801 }
1802
1803 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1804 {
1805         ir_node     *result    = NULL;
1806         statement_t *statement = compound->statements;
1807         for( ; statement != NULL; statement = statement->next) {
1808                 //context2firm(&statement->context);
1809
1810                 if(statement->next == NULL && statement->type == STATEMENT_EXPRESSION) {
1811                         result = expression_statement_to_firm(
1812                                         (expression_statement_t*) statement);
1813                         break;
1814                 }
1815                 statement_to_firm(statement);
1816         }
1817
1818         return result;
1819 }
1820
1821 static void if_statement_to_firm(if_statement_t *statement)
1822 {
1823         ir_node *cur_block = get_cur_block();
1824
1825         ir_node *fallthrough_block = new_immBlock();
1826
1827         /* the true (blocks) */
1828         ir_node *true_block;
1829         if (statement->true_statement != NULL) {
1830                 true_block = new_immBlock();
1831                 statement_to_firm(statement->true_statement);
1832                 if(get_cur_block() != NULL) {
1833                         ir_node *jmp = new_Jmp();
1834                         add_immBlock_pred(fallthrough_block, jmp);
1835                 }
1836         } else {
1837                 true_block = fallthrough_block;
1838         }
1839
1840         /* the false (blocks) */
1841         ir_node *false_block;
1842         if(statement->false_statement != NULL) {
1843                 false_block = new_immBlock();
1844
1845                 statement_to_firm(statement->false_statement);
1846                 if(get_cur_block() != NULL) {
1847                         ir_node *jmp = new_Jmp();
1848                         add_immBlock_pred(fallthrough_block, jmp);
1849                 }
1850         } else {
1851                 false_block = fallthrough_block;
1852         }
1853
1854         /* create the condition */
1855         if(cur_block != NULL) {
1856                 set_cur_block(cur_block);
1857                 create_condition_evaluation(statement->condition, true_block,
1858                                             false_block);
1859         }
1860
1861         mature_immBlock(true_block);
1862         if(false_block != fallthrough_block) {
1863                 mature_immBlock(false_block);
1864         }
1865         mature_immBlock(fallthrough_block);
1866
1867         set_cur_block(fallthrough_block);
1868 }
1869
1870 static void while_statement_to_firm(while_statement_t *statement)
1871 {
1872         ir_node *jmp = NULL;
1873         if(get_cur_block() != NULL) {
1874                 jmp = new_Jmp();
1875         }
1876
1877         /* create the header block */
1878         ir_node *header_block = new_immBlock();
1879         if(jmp != NULL) {
1880                 add_immBlock_pred(header_block, jmp);
1881         }
1882
1883         /* the false block */
1884         ir_node *false_block = new_immBlock();
1885
1886         /* the loop body */
1887         ir_node *body_block;
1888         if (statement->body != NULL) {
1889                 ir_node *old_continue_label = continue_label;
1890                 ir_node *old_break_label    = break_label;
1891                 continue_label              = header_block;
1892                 break_label                 = false_block;
1893
1894                 body_block = new_immBlock();
1895                 statement_to_firm(statement->body);
1896
1897                 assert(continue_label == header_block);
1898                 assert(break_label    == false_block);
1899                 continue_label = old_continue_label;
1900                 break_label    = old_break_label;
1901
1902                 if(get_cur_block() != NULL) {
1903                         ir_node *jmp = new_Jmp();
1904                         add_immBlock_pred(header_block, jmp);
1905                 }
1906         } else {
1907                 body_block = header_block;
1908         }
1909
1910         /* create the condition */
1911         set_cur_block(header_block);
1912
1913         create_condition_evaluation(statement->condition, body_block, false_block);
1914         mature_immBlock(body_block);
1915         mature_immBlock(false_block);
1916         mature_immBlock(header_block);
1917
1918         set_cur_block(false_block);
1919 }
1920
1921 static void do_while_statement_to_firm(do_while_statement_t *statement)
1922 {
1923         ir_node *jmp = NULL;
1924         if(get_cur_block() != NULL) {
1925                 jmp = new_Jmp();
1926         }
1927
1928         /* create the header block */
1929         ir_node *header_block = new_immBlock();
1930
1931         /* the false block */
1932         ir_node *false_block = new_immBlock();
1933
1934         /* the loop body */
1935         ir_node *body_block = new_immBlock();
1936         if(jmp != NULL) {
1937                 add_immBlock_pred(body_block, jmp);
1938         }
1939
1940         if (statement->body != NULL) {
1941                 ir_node *old_continue_label = continue_label;
1942                 ir_node *old_break_label    = break_label;
1943                 continue_label              = header_block;
1944                 break_label                 = false_block;
1945
1946                 statement_to_firm(statement->body);
1947
1948                 assert(continue_label == header_block);
1949                 assert(break_label    == false_block);
1950                 continue_label = old_continue_label;
1951                 break_label    = old_break_label;
1952
1953                 if (get_cur_block() == NULL) {
1954                         mature_immBlock(header_block);
1955                         mature_immBlock(body_block);
1956                         mature_immBlock(false_block);
1957                         return;
1958                 }
1959         }
1960
1961         ir_node *body_jmp = new_Jmp();
1962         add_immBlock_pred(header_block, body_jmp);
1963         mature_immBlock(header_block);
1964
1965         /* create the condition */
1966         set_cur_block(header_block);
1967
1968         create_condition_evaluation(statement->condition, body_block, false_block);
1969         mature_immBlock(body_block);
1970         mature_immBlock(false_block);
1971         mature_immBlock(header_block);
1972
1973         set_cur_block(false_block);
1974 }
1975
1976 static void for_statement_to_firm(for_statement_t *statement)
1977 {
1978         ir_node *jmp = NULL;
1979         if (get_cur_block() != NULL) {
1980                 if(statement->initialisation != NULL) {
1981                         expression_to_firm(statement->initialisation);
1982                 }
1983                 jmp = new_Jmp();
1984         }
1985
1986         /* create the step block */
1987         ir_node *const step_block = new_immBlock();
1988         if (statement->step != NULL) {
1989                 expression_to_firm(statement->step);
1990         }
1991         ir_node *const step_jmp = new_Jmp();
1992
1993         /* create the header block */
1994         ir_node *const header_block = new_immBlock();
1995         if (jmp != NULL) {
1996                 add_immBlock_pred(header_block, jmp);
1997         }
1998         add_immBlock_pred(header_block, step_jmp);
1999
2000         /* the false block */
2001         ir_node *const false_block = new_immBlock();
2002
2003         /* the loop body */
2004         ir_node * body_block;
2005         if (statement->body != NULL) {
2006                 ir_node *const old_continue_label = continue_label;
2007                 ir_node *const old_break_label    = break_label;
2008                 continue_label = step_block;
2009                 break_label    = false_block;
2010
2011                 body_block = new_immBlock();
2012                 statement_to_firm(statement->body);
2013
2014                 assert(continue_label == step_block);
2015                 assert(break_label    == false_block);
2016                 continue_label = old_continue_label;
2017                 break_label    = old_break_label;
2018
2019                 if (get_cur_block() != NULL) {
2020                         ir_node *const jmp = new_Jmp();
2021                         add_immBlock_pred(step_block, jmp);
2022                 }
2023         } else {
2024                 body_block = step_block;
2025         }
2026
2027         /* create the condition */
2028         set_cur_block(header_block);
2029         if (statement->condition != NULL) {
2030                 create_condition_evaluation(statement->condition, body_block,
2031                                             false_block);
2032         } else {
2033                 keep_alive(header_block);
2034                 ir_node *jmp = new_Jmp();
2035                 add_immBlock_pred(body_block, jmp);
2036         }
2037
2038         mature_immBlock(body_block);
2039         mature_immBlock(false_block);
2040         mature_immBlock(step_block);
2041         mature_immBlock(header_block);
2042         mature_immBlock(false_block);
2043
2044         set_cur_block(false_block);
2045 }
2046
2047 static void create_declaration_entity(declaration_t *declaration,
2048                                       declaration_type_t declaration_type,
2049                                       ir_type *parent_type)
2050 {
2051         ident     *id     = new_id_from_str(declaration->symbol->string);
2052         ir_type   *irtype = get_ir_type(declaration->type);
2053         ir_entity *entity = new_entity(parent_type, id, irtype);
2054         set_entity_ld_ident(entity, id);
2055
2056         declaration->declaration_type = declaration_type;
2057         declaration->v.entity         = entity;
2058         set_entity_variability(entity, variability_uninitialized);
2059         /* TODO: visibility? */
2060 }
2061
2062 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2063
2064 enum compound_graph_entry_type_t {
2065         COMPOUND_GRAPH_ENTRY_ARRAY,
2066         COMPOUND_GRAPH_ENTRY_COMPOUND
2067 };
2068
2069 struct compound_graph_path_entry_t {
2070         int type;
2071         union {
2072                 ir_entity *entity;
2073                 int        array_index;
2074         } v;
2075         compound_graph_path_entry_t *prev;
2076 };
2077
2078 static void create_initializer_object(initializer_t *initializer, type_t *type,
2079                 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2080
2081 static compound_graph_path *create_compound_path(ir_type *type,
2082                 compound_graph_path_entry_t *entry, int len)
2083 {
2084         compound_graph_path *path = new_compound_graph_path(type, len);
2085
2086         int i = len - 1;
2087         for( ; entry != NULL; entry = entry->prev, --i) {
2088                 assert(i >= 0);
2089                 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2090                         set_compound_graph_path_node(path, i, entry->v.entity);
2091                 } else {
2092                         assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2093                         set_compound_graph_path_array_index(path, i, entry->v.array_index);
2094                 }
2095         }
2096         assert(i == -1);
2097
2098         return path;
2099 }
2100
2101 static void create_initializer_value(initializer_value_t *initializer,
2102                                      ir_entity *entity,
2103                                      compound_graph_path_entry_t *entry,
2104                                      int len)
2105 {
2106         ir_node             *node = expression_to_firm(initializer->value);
2107         ir_type             *type = get_entity_type(entity);
2108         compound_graph_path *path = create_compound_path(type, entry, len);
2109         add_compound_ent_value_w_path(entity, node, path);
2110 }
2111
2112 static void create_initializer_compound(initializer_list_t *initializer,
2113                                         compound_type_t *type,
2114                                         ir_entity *entity,
2115                                         compound_graph_path_entry_t *last_entry,
2116                                         int len)
2117 {
2118         declaration_t *compound_declaration = type->declaration;
2119
2120         declaration_t *compound_entry = compound_declaration->context.declarations;
2121
2122         compound_graph_path_entry_t entry;
2123         entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2124         entry.prev = last_entry;
2125         ++len;
2126
2127         size_t i = 0;
2128         for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2129                 if(compound_entry->symbol == NULL)
2130                         continue;
2131                 if(compound_entry->namespc != NAMESPACE_NORMAL)
2132                         continue;
2133
2134                 if(i >= initializer->len)
2135                         break;
2136
2137                 entry.v.entity = compound_entry->v.entity;
2138
2139                 initializer_t *sub_initializer = initializer->initializers[i];
2140
2141                 assert(compound_entry != NULL);
2142                 assert(compound_entry->declaration_type
2143                                 == DECLARATION_TYPE_COMPOUND_MEMBER);
2144
2145                 if(sub_initializer->type == INITIALIZER_VALUE) {
2146                         create_initializer_value(&sub_initializer->value,
2147                                                  entity, &entry, len);
2148                 } else {
2149                         type_t *type = skip_typeref(compound_entry->type);
2150                         create_initializer_object(sub_initializer, type, entity, &entry,
2151                                                   len);
2152                 }
2153
2154                 ++i;
2155         }
2156 }
2157
2158 static void create_initializer_array(initializer_list_t *initializer,
2159                                      array_type_t *type, ir_entity *entity,
2160                                      compound_graph_path_entry_t *last_entry,
2161                                      int len)
2162 {
2163         type_t *element_type = type->element_type;
2164         element_type         = skip_typeref(element_type);
2165
2166         compound_graph_path_entry_t entry;
2167         entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2168         entry.prev = last_entry;
2169         ++len;
2170
2171         size_t i;
2172         for(i = 0; i < initializer->len; ++i) {
2173                 entry.v.array_index = i;
2174
2175                 initializer_t *sub_initializer = initializer->initializers[i];
2176
2177                 if(sub_initializer->type == INITIALIZER_VALUE) {
2178                         create_initializer_value(&sub_initializer->value,
2179                                                  entity, &entry, len);
2180                 } else {
2181                         create_initializer_object(sub_initializer, element_type, entity,
2182                                                   &entry, len);
2183                 }
2184         }
2185
2186 #if 0
2187         /* TODO: initializer rest... */
2188         if(type->size_expression != NULL) {
2189                 size_t array_len = fold_constant(type->size_expression);
2190                 for( ; i < array_len; ++i) {
2191
2192                 }
2193         }
2194 #endif
2195 }
2196
2197 static void create_initializer_string(initializer_string_t *initializer,
2198                                       array_type_t *type, ir_entity *entity,
2199                                       compound_graph_path_entry_t *last_entry,
2200                                       int len)
2201 {
2202         type_t *element_type = type->element_type;
2203         element_type         = skip_typeref(element_type);
2204
2205         compound_graph_path_entry_t entry;
2206         entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2207         entry.prev = last_entry;
2208         ++len;
2209
2210         ir_type    *irtype  = get_entity_type(entity);
2211         size_t      arr_len = get_array_type_size(type);
2212         const char *p       = initializer->string;
2213         size_t      i       = 0;
2214         for(i = 0; i < arr_len; ++i, ++p) {
2215                 entry.v.array_index = i;
2216
2217                 ir_node             *node = new_Const_long(mode_Bs, *p);
2218                 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2219                 add_compound_ent_value_w_path(entity, node, path);
2220
2221                 if(*p == '\0')
2222                         break;
2223         }
2224 }
2225
2226 static void create_initializer_object(initializer_t *initializer, type_t *type,
2227                 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2228 {
2229         if(type->type == TYPE_ARRAY) {
2230                 array_type_t *array_type = &type->array;
2231
2232                 if(initializer->type == INITIALIZER_STRING) {
2233                         initializer_string_t *string = &initializer->string;
2234                         create_initializer_string(string, array_type, entity, entry, len);
2235                 } else {
2236                         assert(initializer->type == INITIALIZER_LIST);
2237                         initializer_list_t *list = &initializer->list;
2238                         create_initializer_array(list, array_type, entity, entry, len);
2239                 }
2240         } else {
2241                 assert(initializer->type == INITIALIZER_LIST);
2242                 initializer_list_t *list = &initializer->list;
2243
2244                 assert(type->type == TYPE_COMPOUND_STRUCT
2245                                 || type->type == TYPE_COMPOUND_UNION);
2246                 compound_type_t *compound_type = &type->compound;
2247                 create_initializer_compound(list, compound_type, entity, entry, len);
2248         }
2249 }
2250
2251 static void create_initializer_local_variable_entity(declaration_t *declaration)
2252 {
2253         initializer_t *initializer = declaration->init.initializer;
2254         dbg_info      *dbgi        = get_dbg_info(&declaration->source_position);
2255         ir_entity     *entity      = declaration->v.entity;
2256         ir_node       *memory      = get_store();
2257         ir_node       *nomem       = new_NoMem();
2258         ir_node       *frame       = get_irg_frame(current_ir_graph);
2259         ir_node       *addr        = new_d_simpleSel(dbgi, nomem, frame, entity);
2260
2261         if(is_atomic_entity(entity)) {
2262                 assert(initializer->type == INITIALIZER_VALUE);
2263                 initializer_value_t *initializer_value = &initializer->value;
2264
2265                 ir_node *value     = expression_to_firm(initializer_value->value);
2266                 ir_node *store     = new_d_Store(dbgi, memory, addr, value);
2267                 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2268                 set_store(store_mem);
2269                 return;
2270         }
2271
2272         /* create a "template" entity which is copied to the entity on the stack */
2273         ident     *id          = unique_ident("initializer");
2274         ir_type   *irtype      = get_ir_type(declaration->type);
2275         ir_type   *global_type = get_glob_type();
2276         ir_entity *init_entity = new_entity(global_type, id, irtype);
2277         set_entity_ld_ident(init_entity, id);
2278
2279         set_entity_variability(init_entity, variability_initialized);
2280         set_entity_visibility(init_entity, visibility_local);
2281
2282         ir_graph *old_current_ir_graph = current_ir_graph;
2283         current_ir_graph = get_const_code_irg();
2284
2285         type_t *type = skip_typeref(declaration->type);
2286         create_initializer_object(initializer, type, init_entity, NULL, 0);
2287
2288         assert(current_ir_graph == get_const_code_irg());
2289         current_ir_graph = old_current_ir_graph;
2290
2291         ir_node *src_addr  = create_symconst(dbgi, init_entity);
2292         ir_node *copyb     = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2293
2294         ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2295         set_store(copyb_mem);
2296 }
2297
2298 static void create_initializer(declaration_t *declaration)
2299 {
2300         initializer_t *initializer = declaration->init.initializer;
2301         if(initializer == NULL)
2302                 return;
2303
2304         declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2305         if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2306                 create_initializer_local_variable_entity(declaration);
2307                 return;
2308         }
2309
2310         if(initializer->type == INITIALIZER_VALUE) {
2311                 initializer_value_t *initializer_value = &initializer->value;
2312
2313                 ir_node *value = expression_to_firm(initializer_value->value);
2314
2315                 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2316                         set_value(declaration->v.value_number, value);
2317                 } else {
2318                         assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2319
2320                         ir_entity *entity = declaration->v.entity;
2321
2322                         set_entity_variability(entity, variability_initialized);
2323                         set_atomic_ent_value(entity, value);
2324                 }
2325         } else {
2326                 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2327                 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2328                                 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2329
2330                 ir_entity *entity = declaration->v.entity;
2331                 set_entity_variability(entity, variability_initialized);
2332
2333                 type_t *type = skip_typeref(declaration->type);
2334                 create_initializer_object(initializer, type, entity, NULL, 0);
2335         }
2336 }
2337
2338 static void create_local_variable(declaration_t *declaration)
2339 {
2340         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2341
2342         bool needs_entity = declaration->address_taken;
2343         type_t *type = skip_typeref(declaration->type);
2344
2345         if(type->type == TYPE_ARRAY
2346                         || type->type == TYPE_COMPOUND_STRUCT
2347                         || type->type == TYPE_COMPOUND_UNION) {
2348                 needs_entity = true;
2349         }
2350
2351         if(needs_entity) {
2352                 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2353                 create_declaration_entity(declaration,
2354                                           DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2355                                           frame_type);
2356         } else {
2357                 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2358                 declaration->v.value_number   = next_value_number_function;
2359                 ++next_value_number_function;
2360         }
2361
2362         create_initializer(declaration);
2363 }
2364
2365 static void create_local_static_variable(declaration_t *declaration)
2366 {
2367         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2368
2369         type_t    *type        = skip_typeref(declaration->type);
2370         ir_type   *global_type = get_glob_type();
2371         ident     *id          = unique_ident(declaration->symbol->string);
2372         ir_type   *irtype      = get_ir_type(type);
2373         ir_entity *entity      = new_entity(global_type, id, irtype);
2374         set_entity_ld_ident(entity, id);
2375
2376         declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2377         declaration->v.entity         = entity;
2378         set_entity_variability(entity, variability_uninitialized);
2379         set_entity_visibility(entity, visibility_local);
2380
2381         ir_graph *old_current_ir_graph = current_ir_graph;
2382         current_ir_graph = get_const_code_irg();
2383
2384         create_initializer(declaration);
2385
2386         assert(current_ir_graph == get_const_code_irg());
2387         current_ir_graph = old_current_ir_graph;
2388 }
2389
2390 static void declaration_statement_to_firm(declaration_statement_t *statement)
2391 {
2392         declaration_t *declaration = statement->declarations_begin;
2393         declaration_t *end         = statement->declarations_end->next;
2394         for( ; declaration != end; declaration = declaration->next) {
2395                 type_t *type = declaration->type;
2396
2397                 switch(declaration->storage_class) {
2398                 case STORAGE_CLASS_TYPEDEF:
2399                         continue;
2400                 case STORAGE_CLASS_STATIC:
2401                         create_local_static_variable(declaration);
2402                         continue;
2403                 case STORAGE_CLASS_ENUM_ENTRY:
2404                         panic("enum entry declaration in local block found");
2405                 case STORAGE_CLASS_EXTERN:
2406                         panic("extern declaration in local block found");
2407                 case STORAGE_CLASS_NONE:
2408                 case STORAGE_CLASS_AUTO:
2409                 case STORAGE_CLASS_REGISTER:
2410                         if(type->type == TYPE_FUNCTION) {
2411                                 panic("nested functions not supported yet");
2412                         } else {
2413                                 create_local_variable(declaration);
2414                         }
2415                         continue;
2416                 }
2417                 panic("invalid storage class found");
2418         }
2419 }
2420
2421 static void create_jump_statement(const statement_t *statement,
2422                                   ir_node *target_block)
2423 {
2424         if(get_cur_block() == NULL)
2425                 return;
2426
2427         dbg_info *dbgi = get_dbg_info(&statement->source_position);
2428         ir_node  *jump = new_d_Jmp(dbgi);
2429         add_immBlock_pred(target_block, jump);
2430
2431         set_cur_block(NULL);
2432 }
2433
2434 static void switch_statement_to_firm(const switch_statement_t *statement)
2435 {
2436         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2437
2438         ir_node *expression  = expression_to_firm(statement->expression);
2439         ir_node *cond        = new_d_Cond(dbgi, expression);
2440         ir_node *break_block = new_immBlock();
2441
2442         set_cur_block(NULL);
2443
2444         ir_node *const old_switch_cond       = current_switch_cond;
2445         ir_node *const old_break_label       = break_label;
2446         const bool     old_saw_default_label = saw_default_label;
2447         current_switch_cond                  = cond;
2448         break_label                          = break_block;
2449
2450         statement_to_firm(statement->body);
2451
2452         if(get_cur_block() != NULL) {
2453                 ir_node *jmp = new_Jmp();
2454                 add_immBlock_pred(break_block, jmp);
2455         }
2456
2457         if (!saw_default_label) {
2458                 set_cur_block(get_nodes_block(cond));
2459                 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2460                                                         MAGIC_DEFAULT_PN_NUMBER);
2461                 add_immBlock_pred(break_block, proj);
2462         }
2463
2464         assert(current_switch_cond == cond);
2465         assert(break_label         == break_block);
2466         current_switch_cond = old_switch_cond;
2467         break_label         = old_break_label;
2468         saw_default_label   = old_saw_default_label;
2469
2470         mature_immBlock(break_block);
2471         set_cur_block(break_block);
2472 }
2473
2474 static long fold_constant(const expression_t *expression)
2475 {
2476         ir_graph *old_current_ir_graph = current_ir_graph;
2477         current_ir_graph = get_const_code_irg();
2478
2479         ir_node *cnst = expression_to_firm(expression);
2480         if(!is_Const(cnst)) {
2481                 panic("couldn't fold constantl");
2482         }
2483         tarval *tv = get_Const_tarval(cnst);
2484         if(!tarval_is_long(tv)) {
2485                 panic("folded constant not an integer");
2486         }
2487
2488         long res = get_tarval_long(tv);
2489
2490         current_ir_graph = old_current_ir_graph;
2491         return res;
2492 }
2493
2494 static void case_label_to_firm(const case_label_statement_t *statement)
2495 {
2496         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2497
2498         ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2499
2500         /* let's create a node and hope firm constant folding creates a Const
2501          * node... */
2502         ir_node *proj;
2503         set_cur_block(get_nodes_block(current_switch_cond));
2504         if(statement->expression) {
2505                 long pn = fold_constant(statement->expression);
2506                 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2507                         /* oops someone detected our cheating... */
2508                         panic("magic default pn used");
2509                 }
2510                 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2511         } else {
2512                 saw_default_label = true;
2513                 proj = new_d_defaultProj(dbgi, current_switch_cond,
2514                                          MAGIC_DEFAULT_PN_NUMBER);
2515         }
2516
2517         ir_node *block = new_immBlock();
2518         if (fallthrough != NULL) {
2519                 add_immBlock_pred(block, fallthrough);
2520         }
2521         add_immBlock_pred(block, proj);
2522         mature_immBlock(block);
2523
2524         statement_to_firm(statement->label_statement);
2525 }
2526
2527 static ir_node *get_label_block(declaration_t *label)
2528 {
2529         assert(label->namespc == NAMESPACE_LABEL);
2530
2531         if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2532                 return label->v.block;
2533         }
2534         assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2535
2536         ir_node *old_cur_block = get_cur_block();
2537         ir_node *block         = new_immBlock();
2538         set_cur_block(old_cur_block);
2539
2540         label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2541         label->v.block          = block;
2542
2543         ARR_APP1(ir_node *, imature_blocks, block);
2544
2545         return block;
2546 }
2547
2548 static void label_to_firm(const label_statement_t *statement)
2549 {
2550         ir_node *block = get_label_block(statement->label);
2551
2552         if(get_cur_block() != NULL) {
2553                 ir_node *jmp = new_Jmp();
2554                 add_immBlock_pred(block, jmp);
2555         }
2556
2557         set_cur_block(block);
2558         keep_alive(block);
2559
2560         statement_to_firm(statement->label_statement);
2561 }
2562
2563 static void goto_to_firm(const goto_statement_t *statement)
2564 {
2565         if(get_cur_block() == NULL)
2566                 return;
2567
2568         ir_node *block = get_label_block(statement->label);
2569         ir_node *jmp   = new_Jmp();
2570         add_immBlock_pred(block, jmp);
2571
2572         set_cur_block(NULL);
2573 }
2574
2575 static void statement_to_firm(statement_t *statement)
2576 {
2577         switch(statement->type) {
2578         case STATEMENT_COMPOUND:
2579                 compound_statement_to_firm((compound_statement_t*) statement);
2580                 return;
2581         case STATEMENT_RETURN:
2582                 return_statement_to_firm((return_statement_t*) statement);
2583                 return;
2584         case STATEMENT_EXPRESSION:
2585                 expression_statement_to_firm((expression_statement_t*) statement);
2586                 return;
2587         case STATEMENT_IF:
2588                 if_statement_to_firm((if_statement_t*) statement);
2589                 return;
2590         case STATEMENT_WHILE:
2591                 while_statement_to_firm((while_statement_t*) statement);
2592                 return;
2593         case STATEMENT_DO_WHILE:
2594                 do_while_statement_to_firm((do_while_statement_t*) statement);
2595                 return;
2596         case STATEMENT_DECLARATION:
2597                 declaration_statement_to_firm((declaration_statement_t*) statement);
2598                 return;
2599         case STATEMENT_BREAK:
2600                 create_jump_statement(statement, break_label);
2601                 return;
2602         case STATEMENT_CONTINUE:
2603                 create_jump_statement(statement, continue_label);
2604                 return;
2605         case STATEMENT_SWITCH:
2606                 switch_statement_to_firm((switch_statement_t*) statement);
2607                 return;
2608         case STATEMENT_CASE_LABEL:
2609                 case_label_to_firm((case_label_statement_t*) statement);
2610                 return;
2611         case STATEMENT_FOR:
2612                 for_statement_to_firm((for_statement_t*) statement);
2613                 return;
2614         case STATEMENT_LABEL:
2615                 label_to_firm((label_statement_t*) statement);
2616                 return;
2617         case STATEMENT_GOTO:
2618                 goto_to_firm((goto_statement_t*) statement);
2619                 return;
2620         default:
2621                 break;
2622         }
2623         panic("Statement not implemented\n");
2624 }
2625
2626 static int count_local_declarations(const declaration_t *      decl,
2627                                     const declaration_t *const end)
2628 {
2629         int count = 0;
2630         for (; decl != end; decl = decl->next) {
2631                 const type_t *type = skip_typeref(decl->type);
2632                 switch (type->type) {
2633                         case TYPE_ATOMIC:
2634                         case TYPE_ENUM:
2635                         case TYPE_POINTER:
2636                                 if (!decl->address_taken)
2637                                         ++count;
2638                                 break;
2639
2640                         default: break;
2641                 }
2642         }
2643         return count;
2644 }
2645
2646 static int count_decls_in_stmts(const statement_t *stmt)
2647 {
2648         int count = 0;
2649         for (; stmt != NULL; stmt = stmt->next) {
2650                 switch (stmt->type) {
2651                         case STATEMENT_DECLARATION: {
2652                                 const declaration_statement_t *const decl_stmt =
2653                                         (const declaration_statement_t*)stmt;
2654                                 count += count_local_declarations(decl_stmt->declarations_begin,
2655                                                                   decl_stmt->declarations_end->next);
2656                                 break;
2657                         }
2658
2659                         case STATEMENT_COMPOUND: {
2660                                 const compound_statement_t *const comp =
2661                                         (const compound_statement_t*)stmt;
2662                                 count += count_decls_in_stmts(comp->statements);
2663                                 break;
2664                         }
2665
2666                         case STATEMENT_IF: {
2667                                 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2668                                 count += count_decls_in_stmts(if_stmt->true_statement);
2669                                 count += count_decls_in_stmts(if_stmt->false_statement);
2670                                 break;
2671                         }
2672
2673                         case STATEMENT_SWITCH: {
2674                                 const switch_statement_t *const switch_stmt =
2675                                         (const switch_statement_t*)stmt;
2676                                 count += count_decls_in_stmts(switch_stmt->body);
2677                                 break;
2678                         }
2679
2680                         case STATEMENT_LABEL: {
2681                                 const label_statement_t *const label_stmt =
2682                                         (const label_statement_t*)stmt;
2683                                 count += count_decls_in_stmts(label_stmt->label_statement);
2684                                 break;
2685                         }
2686
2687                         case STATEMENT_WHILE: {
2688                                 const while_statement_t *const while_stmt =
2689                                         (const while_statement_t*)stmt;
2690                                 count += count_decls_in_stmts(while_stmt->body);
2691                                 break;
2692                         }
2693
2694                         case STATEMENT_DO_WHILE: {
2695                                 const do_while_statement_t *const do_while_stmt =
2696                                         (const do_while_statement_t*)stmt;
2697                                 count += count_decls_in_stmts(do_while_stmt->body);
2698                                 break;
2699                         }
2700
2701                         case STATEMENT_FOR: {
2702                                 const for_statement_t *const for_stmt =
2703                                         (const for_statement_t*)stmt;
2704                                 /* TODO initialisation */
2705                                 count += count_decls_in_stmts(for_stmt->body);
2706                                 break;
2707                         }
2708
2709                         case STATEMENT_BREAK:
2710                         case STATEMENT_CASE_LABEL:
2711                         case STATEMENT_CONTINUE:
2712                         case STATEMENT_EXPRESSION:
2713                         case STATEMENT_GOTO:
2714                         case STATEMENT_INVALID:
2715                         case STATEMENT_RETURN:
2716                                 break;
2717                 }
2718         }
2719         return count;
2720 }
2721
2722 static int get_function_n_local_vars(declaration_t *declaration)
2723 {
2724         int count = 0;
2725
2726         /* count parameters */
2727         count += count_local_declarations(declaration->context.declarations, NULL);
2728
2729         /* count local variables declared in body */
2730         count += count_decls_in_stmts(declaration->init.statement);
2731
2732         /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2733          * for expression statements... */
2734         count += 10;
2735
2736         return count;
2737 }
2738
2739 static void initialize_function_parameters(declaration_t *declaration)
2740 {
2741         ir_graph        *irg             = current_ir_graph;
2742         ir_node         *args            = get_irg_args(irg);
2743         ir_node         *start_block     = get_irg_start_block(irg);
2744         ir_type         *function_irtype = get_ir_type(declaration->type);
2745
2746         int            n         = 0;
2747         declaration_t *parameter = declaration->context.declarations;
2748         for( ; parameter != NULL; parameter = parameter->next, ++n) {
2749                 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2750                 type_t *type = skip_typeref(parameter->type);
2751
2752                 bool needs_entity = parameter->address_taken;
2753                 if(type->type == TYPE_COMPOUND_STRUCT
2754                                 || type->type == TYPE_COMPOUND_UNION) {
2755                         needs_entity = true;
2756                 }
2757
2758                 if(needs_entity) {
2759                         ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2760                         ident     *id     = new_id_from_str(parameter->symbol->string);
2761                         set_entity_ident(entity, id);
2762
2763                         parameter->declaration_type
2764                                 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2765                         parameter->v.entity = entity;
2766                         continue;
2767                 }
2768
2769                 ir_mode *mode = get_ir_mode(parameter->type);
2770                 long     pn   = n;
2771                 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2772
2773                 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2774                 parameter->v.value_number   = next_value_number_function;
2775                 ++next_value_number_function;
2776
2777                 set_value(parameter->v.value_number, proj);
2778         }
2779 }
2780
2781 static void create_function(declaration_t *declaration)
2782 {
2783         ir_entity *entity = get_function_entity(declaration);
2784
2785         if(declaration->init.statement == NULL)
2786                 return;
2787
2788         current_function_decl = declaration;
2789         current_function_name = NULL;
2790
2791         assert(imature_blocks == NULL);
2792         imature_blocks = NEW_ARR_F(ir_node*, 0);
2793
2794         int       n_local_vars = get_function_n_local_vars(declaration);
2795         ir_graph *irg          = new_ir_graph(entity, n_local_vars);
2796         ir_node  *first_block  = get_cur_block();
2797
2798         next_value_number_function = 0;
2799         initialize_function_parameters(declaration);
2800
2801         statement_to_firm(declaration->init.statement);
2802
2803         ir_node *end_block = get_irg_end_block(irg);
2804
2805         /* do we have a return statement yet? */
2806         if(get_cur_block() != NULL) {
2807                 assert(declaration->type->type == TYPE_FUNCTION);
2808                 const function_type_t* const func_type = &declaration->type->function;
2809
2810                 ir_node *ret;
2811                 if (func_type->result_type == type_void) {
2812                         ret = new_Return(get_store(), 0, NULL);
2813                 } else {
2814                         ir_mode *const mode = get_ir_mode(func_type->result_type);
2815                         ir_node *      in[1];
2816                         // ยง5.1.2.2.3 main implicitly returns 0
2817                         if (strcmp(declaration->symbol->string, "main") == 0) {
2818                                 in[0] = new_Const(mode, get_mode_null(mode));
2819                         } else {
2820                                 in[0] = new_Unknown(mode);
2821                         }
2822                         ret = new_Return(get_store(), 1, in);
2823                 }
2824                 add_immBlock_pred(end_block, ret);
2825         }
2826
2827         for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2828                 mature_immBlock(imature_blocks[i]);
2829         }
2830         DEL_ARR_F(imature_blocks);
2831         imature_blocks = NULL;
2832
2833         mature_immBlock(first_block);
2834         mature_immBlock(end_block);
2835
2836         irg_finalize_cons(irg);
2837
2838         /* finalize the frame type */
2839         ir_type *frame_type = get_irg_frame_type(irg);
2840         int      n          = get_compound_n_members(frame_type);
2841         int      align_all  = 4;
2842         int      offset     = 0;
2843         for(int i = 0; i < n; ++i) {
2844                 ir_entity *entity      = get_compound_member(frame_type, i);
2845                 ir_type   *entity_type = get_entity_type(entity);
2846
2847                 int align = get_type_alignment_bytes(entity_type);
2848                 if(align > align_all)
2849                         align_all = align;
2850                 int misalign = 0;
2851                 if(align > 0) {
2852                         misalign  = offset % align;
2853                         if(misalign > 0) {
2854                                 offset += align - misalign;
2855                         }
2856                 }
2857
2858                 set_entity_offset(entity, offset);
2859                 offset += get_type_size_bytes(entity_type);
2860         }
2861         set_type_size_bytes(frame_type, offset);
2862         set_type_alignment_bytes(frame_type, align_all);
2863         set_type_state(frame_type, layout_fixed);
2864
2865         irg_vrfy(irg);
2866 }
2867
2868 static void create_global_variable(declaration_t *declaration)
2869 {
2870         ir_type   *global_type = get_glob_type();
2871         create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2872                                   global_type);
2873
2874         ir_entity *entity = declaration->v.entity;
2875         if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2876                 set_entity_visibility(entity, visibility_local);
2877         } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2878                 set_entity_visibility(entity, visibility_external_allocated);
2879         } else {
2880                 set_entity_visibility(entity, visibility_external_visible);
2881         }
2882         current_ir_graph = get_const_code_irg();
2883         create_initializer(declaration);
2884 }
2885
2886 static void context_to_firm(context_t *context)
2887 {
2888         /* first pass: create declarations */
2889         declaration_t *declaration = context->declarations;
2890         for( ; declaration != NULL; declaration = declaration->next) {
2891                 if(declaration->namespc != NAMESPACE_NORMAL)
2892                         continue;
2893                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2894                                 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2895                         continue;
2896                 if(declaration->symbol == NULL)
2897                         continue;
2898
2899                 type_t *type = declaration->type;
2900                 if(type->type == TYPE_FUNCTION) {
2901                         get_function_entity(declaration);
2902                 } else {
2903                         create_global_variable(declaration);
2904                 }
2905         }
2906
2907         /* second pass: create code */
2908         declaration = context->declarations;
2909         for( ; declaration != NULL; declaration = declaration->next) {
2910                 if(declaration->namespc != NAMESPACE_NORMAL)
2911                         continue;
2912                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2913                                 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2914                         continue;
2915                 if(declaration->symbol == NULL)
2916                         continue;
2917
2918                 type_t *type = declaration->type;
2919                 if(type->type != TYPE_FUNCTION)
2920                         continue;
2921
2922                 create_function(declaration);
2923         }
2924 }
2925
2926 void translation_unit_to_firm(translation_unit_t *unit)
2927 {
2928         /* just to be sure */
2929         continue_label      = NULL;
2930         break_label         = NULL;
2931         current_switch_cond = NULL;
2932
2933         context_to_firm(& unit->context);
2934 }