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