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