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