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