5912543808170e1408764072c0fdf2f18a82aa99
[cparser] / ast2firm.c
1 #include <config.h>
2
3 #define _GNU_SOURCE
4
5 #include <assert.h>
6 #include <string.h>
7 #include <stdbool.h>
8
9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
11
12 #include "ast2firm.h"
13
14 #include "adt/error.h"
15 #include "adt/array.h"
16 #include "token_t.h"
17 #include "type_t.h"
18 #include "ast_t.h"
19
20 #define MAGIC_DEFAULT_PN_NUMBER     (long) -314159265
21
22 static ir_type *ir_type_const_char;
23 static ir_type *ir_type_void;
24 static ir_type *ir_type_int;
25 static ir_type *ir_type_void_ptr;
26
27 static type_t *type_const_char;
28 static type_t *type_void;
29 static type_t *type_int;
30
31 static int       next_value_number_function;
32 static ir_node  *continue_label;
33 static ir_node  *break_label;
34 static ir_node  *current_switch_cond;
35 static bool      saw_default_label;
36 static ir_node **imature_blocks;
37
38 typedef enum declaration_type_t {
39         DECLARATION_TYPE_UNKNOWN,
40         DECLARATION_TYPE_FUNCTION,
41         DECLARATION_TYPE_GLOBAL_VARIABLE,
42         DECLARATION_TYPE_LOCAL_VARIABLE,
43         DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
44         DECLARATION_TYPE_COMPOUND_MEMBER,
45         DECLARATION_TYPE_LABEL_BLOCK,
46 } declaration_type_t;
47
48 static ir_type *get_ir_type(type_t *type);
49
50 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
51 {
52         (void) pos;
53 #if 0
54         const declaration_t *declaration = & value_numbers[pos]->declaration;
55
56         print_warning_prefix(declaration->source_position);
57         fprintf(stderr, "variable '%s' might be used uninitialized\n",
58                         declaration->symbol->string);
59 #endif
60         fprintf(stderr, "Some variable might be used uninitialized\n");
61         return new_r_Unknown(irg, mode);
62 }
63
64 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
65 {
66         const source_position_t *pos = (const source_position_t*) dbg;
67         if(pos == NULL)
68                 return 0;
69         return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
70                                    pos->linenr);
71 }
72
73 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
74 {
75         const source_position_t *pos = (const source_position_t*) dbg;
76         if(pos == NULL)
77                 return NULL;
78         if(line != NULL)
79                 *line = pos->linenr;
80         return pos->input_name;
81 }
82
83 void init_ast2firm(void)
84 {
85         type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
86         type_void       = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
87         type_int        = make_atomic_type(ATOMIC_TYPE_INT,  TYPE_QUALIFIER_NONE);
88
89         ir_type_int        = get_ir_type(type_int);
90         ir_type_const_char = get_ir_type(type_const_char);
91         ir_type_void       = get_ir_type(type_int); /* we don't have a real void
92                                                        type in firm */
93         ir_type_void_ptr   = new_type_pointer(new_id_from_str("void_ptr"),
94                                               ir_type_void, mode_P_data);
95
96         type_void->firm_type = ir_type_void;
97 }
98
99 void exit_ast2firm(void)
100 {
101 }
102
103 static unsigned unique_id = 0;
104
105 static ident *unique_ident(const char *tag)
106 {
107         char buf[256];
108
109         snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
110         unique_id++;
111         return new_id_from_str(buf);
112 }
113
114 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
115 {
116         switch(atomic_type->atype) {
117         case ATOMIC_TYPE_SCHAR:
118         case ATOMIC_TYPE_CHAR:
119                 return mode_Bs;
120         case ATOMIC_TYPE_UCHAR:
121                 return mode_Bu;
122         case ATOMIC_TYPE_SHORT:
123                 return mode_Hs;
124         case ATOMIC_TYPE_USHORT:
125                 return mode_Hu;
126         case ATOMIC_TYPE_LONG:
127         case ATOMIC_TYPE_INT:
128                 return mode_Is;
129         case ATOMIC_TYPE_ULONG:
130         case ATOMIC_TYPE_UINT:
131                 return mode_Iu;
132         case ATOMIC_TYPE_LONGLONG:
133                 return mode_Ls;
134         case ATOMIC_TYPE_ULONGLONG:
135                 return mode_Lu;
136         case ATOMIC_TYPE_FLOAT:
137                 return mode_F;
138         case ATOMIC_TYPE_DOUBLE:
139                 return mode_D;
140         case ATOMIC_TYPE_LONG_DOUBLE:
141                 return mode_E;
142         case ATOMIC_TYPE_BOOL:
143                 return mode_b;
144 #ifdef PROVIDE_COMPLEX
145         case ATOMIC_TYPE_FLOAT_COMPLEX:
146         case ATOMIC_TYPE_DOUBLE_COMPLEX:
147         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
148                 panic("complex lowering not implemented yet");
149                 break;
150         case ATOMIC_TYPE_FLOAT_IMAGINARY:
151         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
152         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
153                 panic("imaginary lowering not implemented yet");
154                 break;
155 #endif
156         case ATOMIC_TYPE_VOID:
157                 /* firm has no real void... */
158                 return mode_Is;
159         case ATOMIC_TYPE_INVALID:
160                 break;
161         }
162         panic("Encountered unknown atomic type");
163 }
164
165
166 static unsigned get_type_size(type_t *type);
167
168 static unsigned get_atomic_type_size(const atomic_type_t *type)
169 {
170         switch(type->atype) {
171         case ATOMIC_TYPE_CHAR:
172         case ATOMIC_TYPE_SCHAR:
173         case ATOMIC_TYPE_UCHAR:
174                 return 1;
175
176         case ATOMIC_TYPE_SHORT:
177         case ATOMIC_TYPE_USHORT:
178                 return 2;
179
180         case ATOMIC_TYPE_BOOL:
181         case ATOMIC_TYPE_INT:
182         case ATOMIC_TYPE_UINT:
183         case ATOMIC_TYPE_LONG:
184         case ATOMIC_TYPE_ULONG:
185         case ATOMIC_TYPE_FLOAT:
186                 return 4;
187
188         case ATOMIC_TYPE_LONGLONG:
189         case ATOMIC_TYPE_ULONGLONG:
190         case ATOMIC_TYPE_DOUBLE:
191                 return 8;
192
193         case ATOMIC_TYPE_LONG_DOUBLE:
194                 return 12;
195
196         case ATOMIC_TYPE_VOID:
197                 return 1;
198
199         case ATOMIC_TYPE_INVALID:
200                 break;
201         }
202         panic("Trying to determine size of invalid atomic type");
203 }
204
205 static unsigned get_compound_type_size(compound_type_t *type)
206 {
207         ir_type *irtype = get_ir_type(&type->type);
208         return get_type_size_bytes(irtype);
209 }
210
211 static unsigned get_array_type_size(array_type_t *type)
212 {
213         ir_type *irtype = get_ir_type(&type->type);
214         return get_type_size_bytes(irtype);
215 }
216
217 static unsigned get_type_size(type_t *type)
218 {
219         type = skip_typeref(type);
220
221         switch(type->type) {
222         case TYPE_ATOMIC:
223                 return get_atomic_type_size((const atomic_type_t*) type);
224         case TYPE_ENUM:
225                 return get_mode_size_bytes(mode_Is);
226         case TYPE_COMPOUND_UNION:
227         case TYPE_COMPOUND_STRUCT:
228                 return get_compound_type_size((compound_type_t*) type);
229         case TYPE_FUNCTION:
230                 /* just a pointer to the function */
231                 return get_mode_size_bytes(mode_P_code);
232         case TYPE_POINTER:
233                 return get_mode_size_bytes(mode_P_data);
234         case TYPE_ARRAY:
235                 return get_array_type_size((array_type_t*) type);
236         case TYPE_BUILTIN:
237         case TYPE_TYPEDEF:
238         case TYPE_TYPEOF:
239         case TYPE_INVALID:
240                 break;
241         }
242         panic("Trying to determine size of invalid type");
243 }
244
245 static unsigned count_parameters(const function_type_t *function_type)
246 {
247         unsigned count = 0;
248
249         function_parameter_t *parameter = function_type->parameters;
250         for ( ; parameter != NULL; parameter = parameter->next) {
251                 ++count;
252         }
253
254         return count;
255 }
256
257
258
259
260 static long fold_constant(const expression_t *expression);
261
262 static ir_type *create_atomic_type(const atomic_type_t *type)
263 {
264         ir_mode *mode   = get_atomic_mode(type);
265         ident   *id     = get_mode_ident(mode);
266         ir_type *irtype = new_type_primitive(id, mode);
267
268         return irtype;
269 }
270
271 static ir_type *create_method_type(const function_type_t *function_type)
272 {
273         type_t  *result_type  = function_type->result_type;
274
275         ident   *id           = unique_ident("functiontype");
276         int      n_parameters = count_parameters(function_type);
277         int      n_results    = result_type == type_void ? 0 : 1;
278         ir_type *irtype       = new_type_method(id, n_parameters, n_results);
279
280         if(result_type != type_void) {
281                 ir_type *restype = get_ir_type(result_type);
282                 set_method_res_type(irtype, 0, restype);
283         }
284
285         function_parameter_t *parameter = function_type->parameters;
286         int                   n         = 0;
287         for( ; parameter != NULL; parameter = parameter->next) {
288                 ir_type *p_irtype = get_ir_type(parameter->type);
289                 set_method_param_type(irtype, n, p_irtype);
290                 ++n;
291         }
292
293         if(function_type->variadic || function_type->unspecified_parameters) {
294                 set_method_variadicity(irtype, variadicity_variadic);
295         }
296
297         return irtype;
298 }
299
300 static ir_type *create_pointer_type(pointer_type_t *type)
301 {
302         type_t  *points_to = type->points_to;
303         ir_type *ir_points_to;
304         /* Avoid endless recursion if the points_to type contains this poiner type
305          * again (might be a struct). We therefore first create a void* pointer
306          * and then set the real points_to type
307          */
308         ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
309                                             ir_type_void, mode_P_data);
310         type->type.firm_type  = ir_type;
311
312         ir_points_to = get_ir_type(points_to);
313         set_pointer_points_to_type(ir_type, ir_points_to);
314
315         return ir_type;
316 }
317
318 static ir_type *create_array_type(array_type_t *type)
319 {
320         type_t  *element_type    = type->element_type;
321         ir_type *ir_element_type = get_ir_type(element_type);
322
323         ident   *id      = unique_ident("array");
324         ir_type *ir_type = new_type_array(id, 1, ir_element_type);
325
326         if(type->size != NULL) {
327                 int n_elements = fold_constant(type->size);
328
329                 set_array_bounds_int(ir_type, 0, 0, n_elements);
330
331                 size_t elemsize = get_type_size_bytes(ir_element_type);
332                 int align = get_type_alignment_bytes(ir_element_type);
333                 if(elemsize % align > 0) {
334                         elemsize += align - (elemsize % align);
335                 }
336                 set_type_size_bytes(ir_type, n_elements * elemsize);
337                 set_type_alignment_bytes(ir_type, align);
338                 set_type_state(ir_type, layout_fixed);
339         }
340
341         return ir_type;
342 }
343
344 #define INVALID_TYPE ((ir_type_ptr)-1)
345
346 static ir_type *create_struct_type(compound_type_t *type)
347 {
348         symbol_t *symbol = type->declaration->symbol;
349         ident    *id;
350         if(symbol != NULL) {
351                 id = unique_ident(symbol->string);
352         } else {
353                 id = unique_ident("__anonymous_struct");
354         }
355         ir_type *ir_type = new_type_struct(id);
356
357         type->type.firm_type = ir_type;
358
359         int align_all = 1;
360         int offset    = 0;
361         declaration_t *entry = type->declaration->context.declarations;
362         for( ; entry != NULL; entry = entry->next) {
363                 if(entry->namespace != NAMESPACE_NORMAL)
364                         continue;
365
366                 ident       *ident         = new_id_from_str(entry->symbol->string);
367                 ir_type_ptr  entry_ir_type = get_ir_type(entry->type);
368
369                 int entry_size      = get_type_size_bytes(entry_ir_type);
370                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
371                 int misalign = offset % entry_alignment;
372                 offset += misalign;
373
374                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
375                 set_entity_offset(entity, offset);
376                 add_struct_member(ir_type, entity);
377                 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
378                 entry->v.entity         = entity;
379
380                 offset += entry_size;
381                 if(entry_alignment > align_all) {
382                         if(entry_alignment % align_all != 0) {
383                                 panic("Uneven alignments not supported yet");
384                         }
385                         align_all = entry_alignment;
386                 }
387         }
388
389         int misalign = offset % align_all;
390         offset += misalign;
391         set_type_alignment_bytes(ir_type, align_all);
392         set_type_size_bytes(ir_type, offset);
393         set_type_state(ir_type, layout_fixed);
394
395         return ir_type;
396 }
397
398 static ir_type *create_union_type(compound_type_t *type)
399 {
400         declaration_t *declaration = type->declaration;
401         symbol_t      *symbol      = declaration->symbol;
402         ident         *id;
403         if(symbol != NULL) {
404                 id = unique_ident(symbol->string);
405         } else {
406                 id = unique_ident("__anonymous_union");
407         }
408         ir_type  *ir_type = new_type_union(id);
409
410         type->type.firm_type = ir_type;
411
412         int align_all = 1;
413         int size      = 0;
414         declaration_t *entry = declaration->context.declarations;
415         for( ; entry != NULL; entry = entry->next) {
416                 if(entry->namespace != NAMESPACE_NORMAL)
417                         continue;
418
419                 ident       *ident         = new_id_from_str(entry->symbol->string);
420                 ir_type_ptr  entry_ir_type = get_ir_type(entry->type);
421
422                 int entry_size      = get_type_size_bytes(entry_ir_type);
423                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
424
425                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
426                 add_union_member(ir_type, entity);
427                 set_entity_offset(entity, 0);
428                 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
429                 entry->v.entity         = entity;
430
431                 if(entry_size > size) {
432                         size = entry_size;
433                 }
434                 if(entry_alignment > align_all) {
435                         if(entry_alignment % align_all != 0) {
436                                 panic("Uneven alignments not supported yet");
437                         }
438                         align_all = entry_alignment;
439                 }
440         }
441
442         set_type_alignment_bytes(ir_type, align_all);
443         set_type_size_bytes(ir_type, size);
444         set_type_state(ir_type, layout_fixed);
445
446         return ir_type;
447 }
448
449 static ir_type *get_ir_type(type_t *type)
450 {
451         assert(type != NULL);
452
453         type = skip_typeref(type);
454
455         if(type->firm_type != NULL) {
456                 assert(type->firm_type != INVALID_TYPE);
457                 return type->firm_type;
458         }
459
460         ir_type *firm_type = NULL;
461         switch(type->type) {
462         case TYPE_ATOMIC:
463                 firm_type = create_atomic_type((atomic_type_t*) type);
464                 break;
465         case TYPE_FUNCTION:
466                 firm_type = create_method_type((function_type_t*) type);
467                 break;
468         case TYPE_POINTER:
469                 firm_type = create_pointer_type((pointer_type_t*) type);
470                 break;
471         case TYPE_ARRAY:
472                 firm_type = create_array_type((array_type_t*) type);
473                 break;
474         case TYPE_COMPOUND_STRUCT:
475                 firm_type = create_struct_type((compound_type_t*) type);
476                 break;
477         case TYPE_COMPOUND_UNION:
478                 firm_type = create_union_type((compound_type_t*) type);
479                 break;
480         case TYPE_ENUM:
481                 firm_type = ir_type_int;
482                 break;
483         case TYPE_BUILTIN:
484         case TYPE_TYPEOF:
485         case TYPE_TYPEDEF:
486         case TYPE_INVALID:
487                 break;
488         }
489         if(firm_type == NULL)
490                 panic("unknown type found");
491
492         type->firm_type = firm_type;
493         return firm_type;
494 }
495
496 static inline ir_mode *get_ir_mode(type_t *type)
497 {
498         ir_type *irtype = get_ir_type(type);
499
500         /* firm doesn't report a mode for arrays somehow... */
501         if(is_Array_type(irtype)) {
502                 return mode_P;
503         }
504
505         ir_mode *mode = get_type_mode(irtype);
506         assert(mode != NULL);
507         return mode;
508 }
509
510 static ir_entity* get_function_entity(declaration_t *declaration)
511 {
512         if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
513                 return declaration->v.entity;
514         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
515
516         symbol_t *symbol = declaration->symbol;
517         ident    *id     = new_id_from_str(symbol->string);
518
519         ir_type  *global_type    = get_glob_type();
520         ir_type  *ir_type_method = get_ir_type(declaration->type);
521         assert(is_Method_type(ir_type_method));
522
523         ir_entity *entity = new_entity(global_type, id, ir_type_method);
524         set_entity_ld_ident(entity, id);
525         if(declaration->storage_class == STORAGE_CLASS_STATIC
526                         || declaration->is_inline) {
527                 set_entity_visibility(entity, visibility_local);
528         } else if(declaration->init.statement != NULL) {
529                 set_entity_visibility(entity, visibility_external_visible);
530         } else {
531                 set_entity_visibility(entity, visibility_external_allocated);
532         }
533
534         declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
535         declaration->v.entity         = entity;
536
537         return entity;
538 }
539
540
541
542 static ir_node *expression_to_firm(const expression_t *expression);
543 static ir_node *expression_to_modeb(const expression_t *expression);
544
545 static dbg_info *get_dbg_info(const source_position_t *pos)
546 {
547         return (dbg_info*) pos;
548 }
549
550 static ir_node *const_to_firm(const const_t *cnst)
551 {
552         dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
553         ir_mode  *mode = get_ir_mode(cnst->expression.datatype);
554
555         tarval   *tv;
556         if(mode_is_float(mode)) {
557                 tv = new_tarval_from_double(cnst->v.float_value, mode);
558         } else {
559                 tv = new_tarval_from_long(cnst->v.int_value, mode);
560         }
561
562         return new_d_Const(dbgi, mode, tv);
563 }
564
565 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
566 {
567         assert(entity != NULL);
568         union symconst_symbol sym;
569         sym.entity_p = entity;
570         return new_d_SymConst(dbgi, sym, symconst_addr_ent);
571 }
572
573 static ir_node *string_literal_to_firm(const string_literal_t* literal)
574 {
575         ir_type   *global_type = get_glob_type();
576         ir_type   *type        = new_type_array(unique_ident("strtype"), 1,
577                                                 ir_type_const_char);
578
579         ident     *id     = unique_ident("Lstr");
580         ir_entity *entity = new_entity(global_type, id, type);
581         set_entity_ld_ident(entity, id);
582         set_entity_variability(entity, variability_constant);
583
584         ir_type    *elem_type = ir_type_const_char;
585         ir_mode    *mode      = get_type_mode(elem_type);
586
587         const char *string = literal->value;
588         size_t      slen   = strlen(string) + 1;
589
590         set_array_lower_bound_int(type, 0, 0);
591         set_array_upper_bound_int(type, 0, slen);
592         set_type_size_bytes(type, slen);
593         set_type_state(type, layout_fixed);
594
595         tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
596         for(size_t i = 0; i < slen; ++i) {
597                 tvs[i] = new_tarval_from_long(string[i], mode);
598         }
599
600         set_array_entity_values(entity, tvs, slen);
601         free(tvs);
602
603         dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
604
605         return create_symconst(dbgi, entity);
606 }
607
608 static ir_node *load_from_expression_addr(type_t *type, ir_node *addr,
609                                           dbg_info *dbgi)
610 {
611         ir_mode *mode     = get_ir_mode(type);
612         ir_node *memory   = get_store();
613         ir_node *load     = new_d_Load(dbgi, memory, addr, mode);
614         ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
615         ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
616         set_store(load_mem);
617
618         return load_res;
619 }
620
621 static ir_node *deref_address(type_t *const type, ir_node *const addr,
622                               dbg_info *const dbgi)
623 {
624         switch (type->type) {
625                 case TYPE_ARRAY:
626                 case TYPE_COMPOUND_STRUCT:
627                 case TYPE_COMPOUND_UNION:
628                         return addr;
629
630                 default:
631                         return load_from_expression_addr(type, addr, dbgi);
632         }
633 }
634
635 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
636 {
637         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
638         declaration_t *declaration = ref->declaration;
639         type_t        *type        = skip_typeref(declaration->type);
640
641         switch((declaration_type_t) declaration->declaration_type) {
642         case DECLARATION_TYPE_UNKNOWN:
643                 break;
644         case DECLARATION_TYPE_LOCAL_VARIABLE: {
645                 ir_mode *mode = get_ir_mode(type);
646                 return get_value(declaration->v.value_number, mode);
647         }
648         case DECLARATION_TYPE_FUNCTION: {
649                 return create_symconst(dbgi, declaration->v.entity);
650         }
651         case DECLARATION_TYPE_GLOBAL_VARIABLE: {
652                 ir_entity *entity   = declaration->v.entity;
653                 ir_node   *symconst = create_symconst(dbgi, entity);
654                 return deref_address(type, symconst, dbgi);
655         }
656         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
657                 ir_entity *entity = declaration->v.entity;
658                 ir_node   *frame  = get_irg_frame(current_ir_graph);
659                 ir_node   *sel    = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
660                 return deref_address(type, sel, dbgi);
661         }
662
663         case DECLARATION_TYPE_COMPOUND_MEMBER:
664         case DECLARATION_TYPE_LABEL_BLOCK:
665                 panic("not implemented reference type");
666         }
667
668         panic("reference to declaration with unknown type found");
669 }
670
671 static ir_node *reference_addr(const reference_expression_t *ref)
672 {
673         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
674         declaration_t *declaration = ref->declaration;
675
676         switch((declaration_type_t) declaration->declaration_type) {
677         case DECLARATION_TYPE_UNKNOWN:
678                 break;
679         case DECLARATION_TYPE_LOCAL_VARIABLE:
680                 panic("local variable without entity has no address");
681         case DECLARATION_TYPE_FUNCTION: {
682                 return create_symconst(dbgi, declaration->v.entity);
683         }
684         case DECLARATION_TYPE_GLOBAL_VARIABLE: {
685                 ir_entity *entity   = declaration->v.entity;
686                 ir_node   *symconst = create_symconst(dbgi, entity);
687                 return symconst;
688         }
689         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
690                 ir_entity *entity = declaration->v.entity;
691                 ir_node   *frame  = get_irg_frame(current_ir_graph);
692                 ir_node   *sel    = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
693
694                 return sel;
695         }
696         case DECLARATION_TYPE_COMPOUND_MEMBER:
697         case DECLARATION_TYPE_LABEL_BLOCK:
698                 panic("not implemented reference type");
699         }
700
701         panic("reference to declaration with unknown type found");
702 }
703
704 static ir_node *call_expression_to_firm(const call_expression_t *call)
705 {
706         assert(get_cur_block() != NULL);
707
708         expression_t  *function = call->function;
709         ir_node       *callee   = expression_to_firm(function);
710
711         assert(function->datatype->type == TYPE_FUNCTION);
712         function_type_t *function_type = (function_type_t*) function->datatype;
713
714         int              n_parameters = 0;
715         call_argument_t *argument     = call->arguments;
716         for( ; argument != NULL; argument = argument->next) {
717                 ++n_parameters;
718         }
719
720         ir_type *ir_method_type  = get_ir_type((type_t*) function_type);
721         ir_type *new_method_type = NULL;
722         if(function_type->variadic || function_type->unspecified_parameters) {
723                 /* we need to construct a new method type matching the call
724                  * arguments... */
725                 int n_res       = get_method_n_ress(ir_method_type);
726                 new_method_type = new_type_method(unique_ident("calltype"),
727                                                   n_parameters, n_res);
728                 set_method_calling_convention(new_method_type,
729                                get_method_calling_convention(ir_method_type));
730                 set_method_additional_properties(new_method_type,
731                                get_method_additional_properties(ir_method_type));
732
733                 for(int i = 0; i < n_res; ++i) {
734                         set_method_res_type(new_method_type, i,
735                                             get_method_res_type(ir_method_type, i));
736                 }
737         }
738         ir_node *in[n_parameters];
739
740         argument = call->arguments;
741         int n = 0;
742         for( ; argument != NULL; argument = argument->next) {
743                 expression_t *expression = argument->expression;
744                 ir_node      *arg_node   = expression_to_firm(expression);
745
746                 in[n] = arg_node;
747                 if(new_method_type != NULL) {
748                         ir_type *irtype = get_ir_type(expression->datatype);
749                         set_method_param_type(new_method_type, n, irtype);
750                 }
751
752                 n++;
753         }
754         assert(n == n_parameters);
755
756         if(new_method_type != NULL)
757                 ir_method_type = new_method_type;
758
759         dbg_info *dbgi  = get_dbg_info(&call->expression.source_position);
760         ir_node  *store = get_store();
761         ir_node  *node  = new_d_Call(dbgi, store, callee, n_parameters, in,
762                                      ir_method_type);
763         ir_node  *mem   = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
764         set_store(mem);
765
766         type_t  *result_type = function_type->result_type;
767         ir_node *result      = NULL;
768         if(result_type != type_void) {
769                 ir_mode *mode    = get_ir_mode(result_type);
770                 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
771                 result           = new_d_Proj(dbgi, resproj, mode, 0);
772         }
773
774         return result;
775 }
776
777 static ir_node *expression_to_addr(const expression_t *expression);
778
779 static void set_value_for_expression(const expression_t *expression,
780                                      ir_node *value)
781 {
782         if(expression->type == EXPR_REFERENCE) {
783                 reference_expression_t *ref = (reference_expression_t*) expression;
784
785                 declaration_t *declaration = ref->declaration;
786                 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
787                 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
788                         set_value(declaration->v.value_number, value);
789                         return;
790                 }
791         }
792
793         dbg_info *dbgi      = get_dbg_info(&expression->source_position);
794         ir_node  *addr      = expression_to_addr(expression);
795         assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
796         ir_node  *memory    = get_store();
797         ir_node  *store     = new_d_Store(dbgi, memory, addr, value);
798         ir_node  *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
799         set_store(store_mem);
800 }
801
802 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
803 {
804         ir_mode *value_mode = get_irn_mode(value);
805
806         if(value_mode == dest_mode)
807                 return value;
808
809         if(dest_mode == mode_b) {
810                 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
811                 ir_node *cmp  = new_d_Cmp(dbgi, value, zero);
812                 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
813                 return proj;
814         }
815
816         return new_d_Conv(dbgi, value, dest_mode);
817 }
818
819 static ir_node *create_incdec(const unary_expression_t *expression)
820 {
821         dbg_info     *dbgi  = get_dbg_info(&expression->expression.source_position);
822         type_t       *type  = expression->expression.datatype;
823         ir_mode      *mode  = get_ir_mode(type);
824         expression_t *value = expression->value;
825
826         ir_node *value_node = expression_to_firm(value);
827
828         ir_node *offset;
829         if(type->type == TYPE_POINTER) {
830                 pointer_type_t *pointer_type = (pointer_type_t*) type;
831                 unsigned        elem_size    = get_type_size(pointer_type->points_to);
832                 offset = new_Const_long(mode_Is, elem_size);
833         } else {
834                 assert(is_type_arithmetic(type));
835                 offset = new_Const(mode, get_mode_one(mode));
836         }
837
838         ir_node *new_value;
839         switch(expression->type) {
840         case UNEXPR_POSTFIX_INCREMENT: {
841                 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
842                 set_value_for_expression(value, new_value);
843                 return value_node;
844         }
845         case UNEXPR_POSTFIX_DECREMENT: {
846                 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
847                 set_value_for_expression(value, new_value);
848                 return value_node;
849         }
850         case UNEXPR_PREFIX_INCREMENT: {
851                 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
852                 set_value_for_expression(value, new_value);
853                 return new_value;
854         }
855         case UNEXPR_PREFIX_DECREMENT: {
856                 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
857                 set_value_for_expression(value, new_value);
858                 return new_value;
859         }
860         default:
861                 panic("no incdec expr in create_incdec");
862         }
863
864         return new_value;
865 }
866
867 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
868 {
869         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
870         type_t   *type = expression->expression.datatype;
871         ir_mode  *mode = get_ir_mode(type);
872
873         if(expression->type == UNEXPR_TAKE_ADDRESS)
874                 return expression_to_addr(expression->value);
875
876         const expression_t *value      = expression->value;
877         ir_node            *value_node = expression_to_firm(value);
878
879         switch(expression->type) {
880         case UNEXPR_NEGATE:
881                 return new_d_Minus(dbgi, value_node, mode);
882         case UNEXPR_PLUS:
883                 return value_node;
884         case UNEXPR_BITWISE_NEGATE:
885                 return new_d_Not(dbgi, value_node, mode);
886         case UNEXPR_NOT:
887                 if(get_irn_mode(value_node) != mode_b) {
888                         value_node = create_conv(dbgi, value_node, mode_b);
889                 }
890                 value_node = new_d_Not(dbgi, value_node, mode_b);
891                 if(mode != mode_b) {
892                         value_node = create_conv(dbgi, value_node, mode);
893                 }
894                 return value_node;
895         case UNEXPR_DEREFERENCE:
896                 return load_from_expression_addr(type, value_node, dbgi);
897         case UNEXPR_POSTFIX_INCREMENT:
898         case UNEXPR_POSTFIX_DECREMENT:
899         case UNEXPR_PREFIX_INCREMENT:
900         case UNEXPR_PREFIX_DECREMENT:
901                 return create_incdec(expression);
902         case UNEXPR_CAST:
903                 return create_conv(dbgi, value_node, mode);
904
905         case UNEXPR_TAKE_ADDRESS:
906         case UNEXPR_INVALID:
907                 break;
908         }
909         panic("invalid UNEXPR type found");
910 }
911
912 static long get_pnc(binary_expression_type_t type)
913 {
914         switch(type) {
915         case BINEXPR_EQUAL:        return pn_Cmp_Eq;
916         case BINEXPR_NOTEQUAL:     return pn_Cmp_Lg;
917         case BINEXPR_LESS:         return pn_Cmp_Lt;
918         case BINEXPR_LESSEQUAL:    return pn_Cmp_Le;
919         case BINEXPR_GREATER:      return pn_Cmp_Gt;
920         case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
921         default:
922                 break;
923         }
924         panic("trying to get pn_Cmp from non-comparison binexpr type");
925 }
926
927 static ir_node *create_lazy_op(const binary_expression_t *expression)
928 {
929         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
930
931         bool is_or = (expression->type == BINEXPR_LOGICAL_OR);
932         assert(is_or || expression->type == BINEXPR_LOGICAL_AND);
933
934         ir_node  *val1       = expression_to_modeb(expression->left);
935         ir_node  *cond       = new_d_Cond(dbgi, val1);
936         ir_node  *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
937         ir_node  *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
938
939         ir_node *fallthrough_block = new_immBlock();
940
941         /* the true case */
942         ir_node *calc_val2_block = new_immBlock();
943         if(is_or) {
944                 add_immBlock_pred(calc_val2_block, false_proj);
945         } else {
946                 add_immBlock_pred(calc_val2_block, true_proj);
947         }
948
949         mature_immBlock(calc_val2_block);
950
951         ir_node *val2 = expression_to_modeb(expression->right);
952         if(get_cur_block() != NULL) {
953                 ir_node *jmp = new_d_Jmp(dbgi);
954                 add_immBlock_pred(fallthrough_block, jmp);
955         }
956
957         /* fallthrough */
958         ir_node *constb;
959         if(is_or) {
960                 constb = new_d_Const(dbgi, mode_b, get_tarval_b_true());
961                 add_immBlock_pred(fallthrough_block, true_proj);
962         } else {
963                 constb = new_d_Const(dbgi, mode_b, get_tarval_b_false());
964                 add_immBlock_pred(fallthrough_block, false_proj);
965         }
966         mature_immBlock(fallthrough_block);
967
968         set_cur_block(fallthrough_block);
969
970         ir_node *in[2] = { val2, constb };
971         ir_node *val   = new_d_Phi(dbgi, 2, in, mode_b);
972
973         return val;
974 }
975
976 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
977                                             ir_node *right, ir_mode *mode);
978
979 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
980                                         create_arithmetic_func func)
981 {
982         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
983         ir_node  *left  = expression_to_firm(expression->left);
984         ir_node  *right = expression_to_firm(expression->right);
985         type_t   *type  = expression->right->datatype;
986         /* be careful with the modes, because in asithmetic assign nodes only
987          * the right operand has the mode of the arithmetic alread */
988         ir_mode  *mode  = get_ir_mode(type);
989         left            = create_conv(dbgi, left, mode);
990         ir_node  *res   = func(dbgi, left, right, mode);
991
992         return res;
993 }
994
995 static ir_node *create_arithmetic_assign_binop(
996                 const binary_expression_t *expression, create_arithmetic_func func)
997 {
998         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
999         ir_node  *value = create_arithmetic_binop(expression, func);
1000         type_t   *type  = expression->expression.datatype;
1001         ir_mode  *mode  = get_ir_mode(type);
1002
1003         assert(type->type != TYPE_POINTER);
1004
1005         value = create_conv(dbgi, value, mode);
1006         set_value_for_expression(expression->left, value);
1007
1008         return value;
1009 }
1010
1011 static ir_node *create_add(const binary_expression_t *expression)
1012 {
1013         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1014         ir_node  *left  = expression_to_firm(expression->left);
1015         ir_node  *right = expression_to_firm(expression->right);
1016         type_t   *type  = expression->expression.datatype;
1017         ir_mode  *mode  = get_ir_mode(type);
1018
1019         expression_t *expr_left  = expression->left;
1020         expression_t *expr_right = expression->right;
1021         type_t       *type_left  = skip_typeref(expr_left->datatype);
1022         type_t       *type_right = skip_typeref(expr_right->datatype);
1023
1024         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1025                 return new_d_Add(dbgi, left, right, mode);
1026         }
1027
1028         ir_node        *pointer;
1029         ir_node        *integer;
1030         pointer_type_t *pointer_type;
1031         if(type_left->type == TYPE_POINTER) {
1032                 pointer      = left;
1033                 integer      = right;
1034                 pointer_type = (pointer_type_t*) type_left;
1035         } else {
1036                 assert(type_right->type == TYPE_POINTER);
1037                 pointer      = right;
1038                 integer      = left;
1039                 pointer_type = (pointer_type_t*) type_right;
1040         }
1041
1042         type_t   *points_to = pointer_type->points_to;
1043         unsigned  elem_size = get_type_size(points_to);
1044
1045         assert(elem_size >= 1);
1046         if(elem_size > 1) {
1047                 integer       = create_conv(dbgi, integer, mode_Is);
1048                 ir_node *cnst = new_Const_long(mode_Is, (int) elem_size);
1049                 ir_node *mul  = new_d_Mul(dbgi, integer, cnst, mode_Is);
1050                 integer = mul;
1051         }
1052
1053         ir_node *res = new_d_Add(dbgi, pointer, integer, mode);
1054
1055         return res;
1056 }
1057
1058 static ir_node *create_sub(const binary_expression_t *expression)
1059 {
1060         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1061         ir_node  *left  = expression_to_firm(expression->left);
1062         ir_node  *right = expression_to_firm(expression->right);
1063         type_t   *type  = expression->expression.datatype;
1064         ir_mode  *mode  = get_ir_mode(type);
1065
1066         expression_t *expr_left  = expression->left;
1067         expression_t *expr_right = expression->right;
1068         type_t       *type_left  = skip_typeref(expr_left->datatype);
1069         type_t       *type_right = skip_typeref(expr_right->datatype);
1070
1071         if((is_type_arithmetic(type_left) && is_type_arithmetic(type_right))
1072                         || (type_left->type == TYPE_POINTER
1073                                 && type_right->type == TYPE_POINTER)) {
1074                 return new_d_Sub(dbgi, left, right, mode);
1075         }
1076
1077         assert(type_right->type == TYPE_POINTER);
1078         ir_node        *pointer      = left;
1079         ir_node        *integer      = right;
1080         pointer_type_t *pointer_type = (pointer_type_t*) type_right;
1081
1082         type_t   *points_to = pointer_type->points_to;
1083         unsigned  elem_size = get_type_size(points_to);
1084
1085         assert(elem_size >= 1);
1086         if(elem_size > 1) {
1087                 ir_node *cnst = new_Const_long(mode_Iu, elem_size);
1088                 ir_node *mul  = new_d_Mul(dbgi, integer, cnst, mode_Iu);
1089                 integer = mul;
1090         }
1091
1092         ir_node *res = new_d_Sub(dbgi, pointer, integer, mode);
1093
1094         return res;
1095 }
1096
1097 static ir_node *create_shift(const binary_expression_t *expression)
1098 {
1099         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1100         ir_node  *left  = expression_to_firm(expression->left);
1101         ir_node  *right = expression_to_firm(expression->right);
1102         type_t   *type  = expression->expression.datatype;
1103         ir_mode  *mode  = get_ir_mode(type);
1104
1105         /* firm always wants the shift count to be unsigned */
1106         right = create_conv(dbgi, right, mode_Iu);
1107
1108         ir_node *res;
1109
1110         switch(expression->type) {
1111         case BINEXPR_SHIFTLEFT:
1112                 res = new_d_Shl(dbgi, left, right, mode);
1113                 break;
1114         case BINEXPR_SHIFTRIGHT: {
1115                  expression_t *expr_left = expression->left;
1116                  type_t       *type_left = skip_typeref(expr_left->datatype);
1117
1118                  if(is_type_signed(type_left)) {
1119                         res = new_d_Shrs(dbgi, left, right, mode);
1120                  } else {
1121                          res = new_d_Shr(dbgi, left, right, mode);
1122                  }
1123                  break;
1124         }
1125         default:
1126                 panic("create shift op called for non-shift op");
1127         }
1128
1129         return res;
1130 }
1131
1132
1133 static ir_node *create_divmod(const binary_expression_t *expression)
1134 {
1135         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1136         ir_node  *left  = expression_to_firm(expression->left);
1137         ir_node  *right = expression_to_firm(expression->right);
1138         ir_node  *pin   = new_Pin(new_NoMem());
1139         type_t   *type  = expression->expression.datatype;
1140         ir_mode  *mode  = get_ir_mode(type);
1141         ir_node  *op;
1142         ir_node  *res;
1143
1144         switch (expression->type)  {
1145                 case BINEXPR_DIV:
1146                 case BINEXPR_DIV_ASSIGN:
1147                         if(mode_is_float(mode)) {
1148                                 op  = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1149                                 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1150                         } else {
1151                                 op  = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1152                                 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1153                         }
1154                         break;
1155
1156                 case BINEXPR_MOD:
1157                 case BINEXPR_MOD_ASSIGN:
1158                         assert(!mode_is_float(mode));
1159                         op  = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1160                         res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1161                         break;
1162
1163                 default: panic("unexpected binary expression type in create_divmod()");
1164         }
1165
1166         return res;
1167 }
1168
1169 static ir_node *create_arithmetic_assign_divmod(
1170                 const binary_expression_t *expression)
1171 {
1172         ir_node  *      value = create_divmod(expression);
1173         dbg_info *const dbgi  = get_dbg_info(&expression->expression.source_position);
1174         type_t   *const type  = expression->expression.datatype;
1175         ir_mode  *const mode  = get_ir_mode(type);
1176
1177         assert(type->type != TYPE_POINTER);
1178
1179         value = create_conv(dbgi, value, mode);
1180         set_value_for_expression(expression->left, value);
1181
1182         return value;
1183 }
1184
1185
1186 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1187 {
1188         binary_expression_type_t type = expression->type;
1189         switch(type) {
1190         case BINEXPR_EQUAL:
1191         case BINEXPR_NOTEQUAL:
1192         case BINEXPR_LESS:
1193         case BINEXPR_LESSEQUAL:
1194         case BINEXPR_GREATER:
1195         case BINEXPR_GREATEREQUAL: {
1196                 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1197                 ir_node *left  = expression_to_firm(expression->left);
1198                 ir_node *right = expression_to_firm(expression->right);
1199                 ir_node *cmp   = new_d_Cmp(dbgi, left, right);
1200                 long     pnc   = get_pnc(type);
1201                 ir_node *proj  = new_d_Proj(dbgi, cmp, mode_b, pnc);
1202                 return proj;
1203         }
1204         case BINEXPR_ASSIGN: {
1205                 ir_node *right = expression_to_firm(expression->right);
1206                 set_value_for_expression(expression->left, right);
1207                 return right;
1208         }
1209         case BINEXPR_ADD:
1210                 return create_add(expression);
1211         case BINEXPR_SUB:
1212                 return create_sub(expression);
1213         case BINEXPR_MUL:
1214                 return create_arithmetic_binop(expression, new_d_Mul);
1215         case BINEXPR_BITWISE_AND:
1216                 return create_arithmetic_binop(expression, new_d_And);
1217         case BINEXPR_BITWISE_OR:
1218                 return create_arithmetic_binop(expression, new_d_Or);
1219         case BINEXPR_BITWISE_XOR:
1220                 return create_arithmetic_binop(expression, new_d_Eor);
1221         case BINEXPR_SHIFTLEFT:
1222         case BINEXPR_SHIFTRIGHT:
1223                 return create_shift(expression);
1224         case BINEXPR_DIV:
1225         case BINEXPR_MOD:
1226                 return create_divmod(expression);
1227         case BINEXPR_LOGICAL_AND:
1228         case BINEXPR_LOGICAL_OR:
1229                 return create_lazy_op(expression);
1230         case BINEXPR_COMMA:
1231                 expression_to_firm(expression->left);
1232                 return expression_to_firm(expression->right);
1233         case BINEXPR_ADD_ASSIGN:
1234                 return create_arithmetic_assign_binop(expression, new_d_Add);
1235         case BINEXPR_SUB_ASSIGN:
1236                 return create_arithmetic_assign_binop(expression, new_d_Sub);
1237         case BINEXPR_MUL_ASSIGN:
1238                 return create_arithmetic_assign_binop(expression, new_d_Mul);
1239         case BINEXPR_DIV_ASSIGN:
1240                 return create_arithmetic_assign_divmod(expression);
1241         case BINEXPR_BITWISE_AND_ASSIGN:
1242                 return create_arithmetic_assign_binop(expression, new_d_And);
1243         case BINEXPR_BITWISE_OR_ASSIGN:
1244                 return create_arithmetic_assign_binop(expression, new_d_Or);
1245         case BINEXPR_BITWISE_XOR_ASSIGN:
1246                 return create_arithmetic_assign_binop(expression, new_d_Eor);
1247         case BINEXPR_SHIFTLEFT_ASSIGN:
1248                 return create_arithmetic_assign_binop(expression, new_d_Shl);
1249         case BINEXPR_SHIFTRIGHT_ASSIGN:
1250                 return create_arithmetic_assign_binop(expression, new_d_Shr);
1251         default:
1252                 panic("TODO binexpr type");
1253         }
1254 }
1255
1256 static ir_node *array_access_addr(const array_access_expression_t *expression)
1257 {
1258         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1259
1260         ir_node *base_addr = expression_to_firm(expression->array_ref);
1261         ir_node *offset    = expression_to_firm(expression->index);
1262         offset             = create_conv(dbgi, offset, mode_Iu);
1263
1264         unsigned elem_size       = get_type_size(expression->expression.datatype);
1265         ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1266         ir_node *real_offset     = new_d_Mul(dbgi, offset, elem_size_const,
1267                                              mode_Iu);
1268         ir_node *result          = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1269
1270         return result;
1271 }
1272
1273 static ir_node *array_access_to_firm(
1274                 const array_access_expression_t *expression)
1275 {
1276         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1277         ir_node  *addr = array_access_addr(expression);
1278         type_t   *type = expression->expression.datatype;
1279         return deref_address(type, addr, dbgi);
1280 }
1281
1282 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1283 {
1284         type_t *type = expression->type;
1285         if(type == NULL) {
1286                 type = expression->size_expression->datatype;
1287                 assert(type != NULL);
1288         }
1289
1290         ir_mode  *mode      = get_ir_mode(expression->expression.datatype);
1291         unsigned  size      = get_type_size(type);
1292         ir_node  *size_node = new_Const_long(mode, size);
1293
1294         return size_node;
1295 }
1296
1297 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1298 {
1299         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1300
1301         ir_node *condition  = expression_to_modeb(expression->condition);
1302         ir_node *cond       = new_d_Cond(dbgi, condition);
1303         ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1304         ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1305
1306         /* create the true block */
1307         ir_node *true_block = new_immBlock();
1308         add_immBlock_pred(true_block, true_proj);
1309         mature_immBlock(true_block);
1310
1311         ir_node *true_val = expression_to_firm(expression->true_expression);
1312         ir_node *true_jmp = new_Jmp();
1313
1314         /* create the false block */
1315         ir_node *false_block = new_immBlock();
1316         add_immBlock_pred(false_block, false_proj);
1317         mature_immBlock(false_block);
1318
1319         ir_node *false_val = expression_to_firm(expression->false_expression);
1320         ir_node *false_jmp = new_Jmp();
1321
1322         /* create the common block */
1323         ir_node *common_block = new_immBlock();
1324         add_immBlock_pred(common_block, true_jmp);
1325         add_immBlock_pred(common_block, false_jmp);
1326         mature_immBlock(common_block);
1327
1328         ir_node *in[2] = { true_val, false_val };
1329         ir_mode *mode  = get_irn_mode(true_val);
1330         assert(get_irn_mode(false_val) == mode);
1331         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
1332
1333         return val;
1334 }
1335
1336 static ir_node *select_addr(const select_expression_t *expression)
1337 {
1338         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1339
1340         ir_node *compound_addr = expression_to_firm(expression->compound);
1341
1342         declaration_t *entry = expression->compound_entry;
1343         assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1344         ir_entity     *entity = entry->v.entity;
1345
1346         assert(entity != NULL);
1347
1348         ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1349
1350         return sel;
1351 }
1352
1353 static ir_node *select_to_firm(const select_expression_t *expression)
1354 {
1355         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1356         ir_node  *addr = select_addr(expression);
1357         type_t   *type = expression->expression.datatype;
1358         return deref_address(type, addr, dbgi);
1359 }
1360
1361 static ir_node *dereference_addr(const unary_expression_t *const expression)
1362 {
1363         assert(expression->type == UNEXPR_DEREFERENCE);
1364         return expression_to_firm(expression->value);
1365 }
1366
1367 static ir_node *expression_to_addr(const expression_t *expression)
1368 {
1369         switch(expression->type) {
1370         case EXPR_REFERENCE:
1371                 return reference_addr((const reference_expression_t*) expression);
1372         case EXPR_ARRAY_ACCESS:
1373                 return array_access_addr((const array_access_expression_t*) expression);
1374         case EXPR_SELECT:
1375                 return select_addr((const select_expression_t*) expression);
1376         case EXPR_UNARY: {
1377                 const unary_expression_t *const unary_expr =
1378                         (const unary_expression_t*)expression;
1379                 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1380                         return dereference_addr(unary_expr);
1381                 }
1382                 break;
1383         }
1384         default:
1385                 break;
1386         }
1387         panic("trying to get address of non-lvalue");
1388 }
1389
1390 static ir_node *_expression_to_firm(const expression_t *expression)
1391 {
1392         switch(expression->type) {
1393         case EXPR_CONST:
1394                 return const_to_firm((const const_t*) expression);
1395         case EXPR_STRING_LITERAL:
1396                 return string_literal_to_firm((const string_literal_t*) expression);
1397         case EXPR_REFERENCE:
1398                 return reference_expression_to_firm(
1399                                 (const reference_expression_t*) expression);
1400         case EXPR_CALL:
1401                 return call_expression_to_firm((const call_expression_t*) expression);
1402         case EXPR_UNARY:
1403                 return unary_expression_to_firm((const unary_expression_t*) expression);
1404         case EXPR_BINARY:
1405                 return binary_expression_to_firm(
1406                                 (const binary_expression_t*) expression);
1407         case EXPR_ARRAY_ACCESS:
1408                 return array_access_to_firm(
1409                                 (const array_access_expression_t*) expression);
1410         case EXPR_SIZEOF:
1411                 return sizeof_to_firm((const sizeof_expression_t*) expression);
1412         case EXPR_CONDITIONAL:
1413                 return conditional_to_firm((const conditional_expression_t*)expression);
1414         case EXPR_SELECT:
1415                 return select_to_firm((const select_expression_t*) expression);
1416         case EXPR_FUNCTION:
1417         case EXPR_OFFSETOF:
1418         case EXPR_PRETTY_FUNCTION:
1419         case EXPR_VA_ARG:
1420         case EXPR_STATEMENT:
1421         case EXPR_BUILTIN_SYMBOL:
1422                 panic("unimplemented expression found");
1423
1424         case EXPR_UNKNOWN:
1425         case EXPR_INVALID:
1426                 break;
1427         }
1428         panic("invalid expression found");
1429 }
1430
1431 static ir_node *expression_to_firm(const expression_t *expression)
1432 {
1433         ir_node *res = _expression_to_firm(expression);
1434
1435         if(res != NULL && get_irn_mode(res) == mode_b) {
1436                 ir_mode *mode = get_ir_mode(expression->datatype);
1437                 res           = create_conv(NULL, res, mode);
1438         }
1439
1440         return res;
1441 }
1442
1443 static ir_node *expression_to_modeb(const expression_t *expression)
1444 {
1445         ir_node *res = _expression_to_firm(expression);
1446         res          = create_conv(NULL, res, mode_b);
1447
1448         return res;
1449 }
1450
1451 static void statement_to_firm(statement_t *statement);
1452
1453 static void return_statement_to_firm(return_statement_t *statement)
1454 {
1455         if(get_cur_block() == NULL)
1456                 return;
1457
1458         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1459         ir_node  *ret;
1460
1461         if(statement->return_value != NULL) {
1462                 ir_node *retval = expression_to_firm(statement->return_value);
1463                 ir_node *in[1];
1464
1465                 in[0] = retval;
1466                 ret   = new_d_Return(dbgi, get_store(), 1, in);
1467         } else {
1468                 ret   = new_d_Return(dbgi, get_store(), 0, NULL);
1469         }
1470         ir_node *end_block = get_irg_end_block(current_ir_graph);
1471         add_immBlock_pred(end_block, ret);
1472
1473         set_cur_block(NULL);
1474 }
1475
1476 static void compound_statement_to_firm(compound_statement_t *compound)
1477 {
1478         statement_t *statement = compound->statements;
1479         for( ; statement != NULL; statement = statement->next) {
1480                 //context2firm(&statement->context);
1481                 statement_to_firm(statement);
1482         }
1483 }
1484
1485 static void expression_statement_to_firm(expression_statement_t *statement)
1486 {
1487         if(get_cur_block() == NULL)
1488                 return;
1489
1490         expression_to_firm(statement->expression);
1491 }
1492
1493 static void if_statement_to_firm(if_statement_t *statement)
1494 {
1495         dbg_info *dbgi      = get_dbg_info(&statement->statement.source_position);
1496         ir_node  *condition = expression_to_modeb(statement->condition);
1497
1498         /* make sure we have a mode_b condition */
1499         ir_node *cond       = new_d_Cond(dbgi, condition);
1500         ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1501         ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1502
1503         ir_node *fallthrough_block = new_immBlock();
1504
1505         /* the true (blocks) */
1506         ir_node *true_block = new_immBlock();
1507         add_immBlock_pred(true_block, true_proj);
1508         mature_immBlock(true_block);
1509
1510         statement_to_firm(statement->true_statement);
1511         if(get_cur_block() != NULL) {
1512                 ir_node *jmp = new_Jmp();
1513                 add_immBlock_pred(fallthrough_block, jmp);
1514         }
1515
1516         /* the false (blocks) */
1517         if(statement->false_statement != NULL) {
1518                 ir_node *false_block = new_immBlock();
1519                 add_immBlock_pred(false_block, false_proj);
1520                 mature_immBlock(false_block);
1521
1522                 statement_to_firm(statement->false_statement);
1523                 if(get_cur_block() != NULL) {
1524                         ir_node *jmp = new_Jmp();
1525                         add_immBlock_pred(fallthrough_block, jmp);
1526                 }
1527         } else {
1528                 add_immBlock_pred(fallthrough_block, false_proj);
1529         }
1530         mature_immBlock(fallthrough_block);
1531
1532         set_cur_block(fallthrough_block);
1533 }
1534
1535 static void while_statement_to_firm(while_statement_t *statement)
1536 {
1537         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1538
1539         ir_node *jmp = NULL;
1540         if(get_cur_block() != NULL) {
1541                 jmp = new_Jmp();
1542         }
1543
1544         /* create the header block */
1545         ir_node *header_block = new_immBlock();
1546         if(jmp != NULL) {
1547                 add_immBlock_pred(header_block, jmp);
1548         }
1549
1550         /* create the condition */
1551         ir_node *condition  = expression_to_modeb(statement->condition);
1552         ir_node *cond       = new_d_Cond(dbgi, condition);
1553         ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1554         ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1555
1556         /* the false block */
1557         ir_node *false_block = new_immBlock();
1558         add_immBlock_pred(false_block, false_proj);
1559
1560         /* the loop body */
1561         ir_node *body_block = new_immBlock();
1562         add_immBlock_pred(body_block, true_proj);
1563         mature_immBlock(body_block);
1564
1565         ir_node *old_continue_label = continue_label;
1566         ir_node *old_break_label    = break_label;
1567         continue_label              = header_block;
1568         break_label                 = false_block;
1569
1570         statement_to_firm(statement->body);
1571
1572         assert(continue_label == header_block);
1573         assert(break_label    == false_block);
1574         continue_label = old_continue_label;
1575         break_label    = old_break_label;
1576
1577         if(get_cur_block() != NULL) {
1578                 ir_node *jmp = new_Jmp();
1579                 add_immBlock_pred(header_block, jmp);
1580         }
1581
1582         mature_immBlock(header_block);
1583         mature_immBlock(false_block);
1584
1585         set_cur_block(false_block);
1586 }
1587
1588 static void do_while_statement_to_firm(do_while_statement_t *statement)
1589 {
1590         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1591
1592         ir_node *jmp = NULL;
1593         if(get_cur_block() != NULL) {
1594                 jmp = new_Jmp();
1595         }
1596
1597         /* create the header block */
1598         ir_node *header_block = new_immBlock();
1599
1600         /* the false block */
1601         ir_node *false_block = new_immBlock();
1602
1603         /* the loop body */
1604         ir_node *body_block = new_immBlock();
1605         if(jmp != NULL) {
1606                 add_immBlock_pred(body_block, jmp);
1607         }
1608
1609         ir_node *old_continue_label = continue_label;
1610         ir_node *old_break_label    = break_label;
1611         continue_label              = header_block;
1612         break_label                 = false_block;
1613
1614         statement_to_firm(statement->body);
1615
1616         assert(continue_label == header_block);
1617         assert(break_label    == false_block);
1618         continue_label = old_continue_label;
1619         break_label    = old_break_label;
1620
1621         if(get_cur_block() == NULL) {
1622                 mature_immBlock(header_block);
1623                 mature_immBlock(body_block);
1624                 mature_immBlock(false_block);
1625                 return;
1626         }
1627
1628         ir_node *body_jmp = new_Jmp();
1629         add_immBlock_pred(header_block, body_jmp);
1630         mature_immBlock(header_block);
1631
1632         /* create the condition */
1633         set_cur_block(header_block);
1634         ir_node *condition  = expression_to_modeb(statement->condition);
1635         ir_node *cond       = new_d_Cond(dbgi, condition);
1636         ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1637         ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1638
1639         add_immBlock_pred(body_block, true_proj);
1640         mature_immBlock(body_block);
1641
1642         add_immBlock_pred(false_block, false_proj);
1643         mature_immBlock(false_block);
1644
1645         set_cur_block(false_block);
1646 }
1647
1648 static void for_statement_to_firm(for_statement_t *statement)
1649 {
1650         dbg_info *const dbgi = get_dbg_info(&statement->statement.source_position);
1651
1652         ir_node *jmp = NULL;
1653         if (get_cur_block() != NULL) {
1654                 if(statement->initialisation != NULL) {
1655                         expression_to_firm(statement->initialisation);
1656                 }
1657                 jmp = new_Jmp();
1658         }
1659
1660         /* create the step block */
1661         ir_node *const step_block = new_immBlock();
1662         if (statement->step != NULL) {
1663                 expression_to_firm(statement->step);
1664         }
1665         ir_node *const step_jmp   = new_Jmp();
1666
1667         /* create the header block */
1668         ir_node *const header_block = new_immBlock();
1669         if (jmp != NULL) {
1670                 add_immBlock_pred(header_block, jmp);
1671         }
1672         add_immBlock_pred(header_block, step_jmp);
1673
1674         /* create the condition */
1675         ir_node *true_proj;
1676         ir_node *false_proj;
1677         if (statement->condition != NULL) {
1678                 ir_node *const condition  = expression_to_modeb(statement->condition);
1679                 ir_node *const cond       = new_d_Cond(dbgi, condition);
1680                 true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1681                 false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1682         } else {
1683                 keep_alive(header_block);
1684                 true_proj  = new_Jmp();
1685                 false_proj = NULL;
1686         }
1687
1688         /* the false block */
1689         ir_node *const false_block = new_immBlock();
1690         if (false_proj != NULL) {
1691                 add_immBlock_pred(false_block, false_proj);
1692         }
1693
1694         /* the loop body */
1695         ir_node *const body_block = new_immBlock();
1696         add_immBlock_pred(body_block, true_proj);
1697         mature_immBlock(body_block);
1698
1699         ir_node *const old_continue_label = continue_label;
1700         ir_node *const old_break_label    = break_label;
1701         continue_label = step_block;
1702         break_label    = false_block;
1703
1704         statement_to_firm(statement->body);
1705
1706         assert(continue_label == step_block);
1707         assert(break_label    == false_block);
1708         continue_label = old_continue_label;
1709         break_label    = old_break_label;
1710
1711         if (get_cur_block() != NULL) {
1712                 ir_node *const jmp = new_Jmp();
1713                 add_immBlock_pred(step_block, jmp);
1714         }
1715
1716         mature_immBlock(step_block);
1717         mature_immBlock(header_block);
1718         mature_immBlock(false_block);
1719
1720         set_cur_block(false_block);
1721 }
1722
1723 static void create_declaration_entity(declaration_t *declaration,
1724                                       declaration_type_t declaration_type,
1725                                       ir_type *parent_type)
1726 {
1727         ident     *id     = new_id_from_str(declaration->symbol->string);
1728         ir_type   *irtype = get_ir_type(declaration->type);
1729         ir_entity *entity = new_entity(parent_type, id, irtype);
1730         set_entity_ld_ident(entity, id);
1731
1732         declaration->declaration_type = declaration_type;
1733         declaration->v.entity         = entity;
1734         set_entity_variability(entity, variability_uninitialized);
1735         /* TODO: visibility? */
1736 }
1737
1738 static void create_initializer(declaration_t *declaration)
1739 {
1740         initializer_t *initializer = declaration->init.initializer;
1741         if(initializer == NULL)
1742                 return;
1743
1744         if(initializer->type == INITIALIZER_VALUE) {
1745                 assert(initializer->designator == NULL);
1746                 assert(initializer->next == NULL);
1747                 ir_node *init_node = expression_to_firm(initializer->v.value);
1748
1749                 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1750                         set_value(declaration->v.value_number, init_node);
1751                 } else {
1752                         ir_entity *entity = declaration->v.entity;
1753
1754                         set_entity_variability(entity, variability_initialized);
1755                         set_atomic_ent_value(entity, init_node);
1756                 }
1757         } else {
1758                 assert(initializer->type == INITIALIZER_LIST);
1759                 panic("list initializer not supported yet");
1760         }
1761 }
1762
1763 static void create_local_variable(declaration_t *declaration)
1764 {
1765         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
1766
1767         bool needs_entity = declaration->address_taken;
1768         type_t *type = skip_typeref(declaration->type);
1769
1770         if(type->type == TYPE_ARRAY
1771                         || type->type == TYPE_COMPOUND_STRUCT
1772                         || type->type == TYPE_COMPOUND_UNION) {
1773                 needs_entity = true;
1774         }
1775
1776         if(needs_entity) {
1777                 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
1778                 create_declaration_entity(declaration,
1779                                           DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
1780                                           frame_type);
1781         } else {
1782                 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1783                 declaration->v.value_number   = next_value_number_function;
1784                 ++next_value_number_function;
1785         }
1786
1787         create_initializer(declaration);
1788 }
1789
1790 static void declaration_statement_to_firm(declaration_statement_t *statement)
1791 {
1792         declaration_t *declaration = statement->declarations_begin;
1793         declaration_t *end         = statement->declarations_end->next;
1794         for( ; declaration != end; declaration = declaration->next) {
1795                 type_t *type = declaration->type;
1796
1797                 switch(declaration->storage_class) {
1798                 case STORAGE_CLASS_TYPEDEF:
1799                         continue;
1800                 case STORAGE_CLASS_STATIC:
1801                         panic("static local vars not implemented yet");
1802                 case STORAGE_CLASS_ENUM_ENTRY:
1803                         panic("enum entry declaration in local block found");
1804                 case STORAGE_CLASS_EXTERN:
1805                         panic("extern declaration in local block found");
1806                 case STORAGE_CLASS_NONE:
1807                 case STORAGE_CLASS_AUTO:
1808                 case STORAGE_CLASS_REGISTER:
1809                         if(type->type == TYPE_FUNCTION) {
1810                                 panic("nested functions not supported yet");
1811                         } else {
1812                                 create_local_variable(declaration);
1813                         }
1814                         continue;
1815                 }
1816                 panic("invalid storage class found");
1817         }
1818 }
1819
1820 static void create_jump_statement(const statement_t *statement,
1821                                   ir_node *target_block)
1822 {
1823         if(get_cur_block() == NULL)
1824                 return;
1825
1826         dbg_info *dbgi = get_dbg_info(&statement->source_position);
1827         ir_node  *jump = new_d_Jmp(dbgi);
1828         add_immBlock_pred(target_block, jump);
1829
1830         set_cur_block(NULL);
1831 }
1832
1833 static void switch_statement_to_firm(const switch_statement_t *statement)
1834 {
1835         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1836
1837         ir_node *expression  = expression_to_firm(statement->expression);
1838         ir_node *cond        = new_d_Cond(dbgi, expression);
1839         ir_node *break_block = new_immBlock();
1840
1841         set_cur_block(NULL);
1842
1843         ir_node *const old_switch_cond       = current_switch_cond;
1844         ir_node *const old_break_label       = break_label;
1845         const bool     old_saw_default_label = saw_default_label;
1846         current_switch_cond                  = cond;
1847         break_label                          = break_block;
1848
1849         statement_to_firm(statement->body);
1850
1851         if(get_cur_block() != NULL) {
1852                 ir_node *jmp = new_Jmp();
1853                 add_immBlock_pred(break_block, jmp);
1854         }
1855
1856         if (!saw_default_label) {
1857                 set_cur_block(get_nodes_block(cond));
1858                 ir_node *const proj = new_d_defaultProj(dbgi, cond,
1859                                                         MAGIC_DEFAULT_PN_NUMBER);
1860                 add_immBlock_pred(break_block, proj);
1861         }
1862
1863         assert(current_switch_cond == cond);
1864         assert(break_label         == break_block);
1865         current_switch_cond = old_switch_cond;
1866         break_label         = old_break_label;
1867         saw_default_label   = old_saw_default_label;
1868
1869         mature_immBlock(break_block);
1870         set_cur_block(break_block);
1871 }
1872
1873 static long fold_constant(const expression_t *expression)
1874 {
1875         ir_graph *old_current_ir_graph = current_ir_graph;
1876         current_ir_graph = get_const_code_irg();
1877
1878         ir_node *cnst = expression_to_firm(expression);
1879         if(!is_Const(cnst)) {
1880                 panic("couldn't fold constantl");
1881         }
1882         tarval *tv = get_Const_tarval(cnst);
1883         if(!tarval_is_long(tv)) {
1884                 panic("folded constant not an integer");
1885         }
1886
1887         long res = get_tarval_long(tv);
1888
1889         current_ir_graph = old_current_ir_graph;
1890         return res;
1891 }
1892
1893 static void case_label_to_firm(const case_label_statement_t *statement)
1894 {
1895         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1896
1897         ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
1898
1899         /* let's create a node and hope firm constant folding creates a Const
1900          * node... */
1901         ir_node *proj;
1902         set_cur_block(get_nodes_block(current_switch_cond));
1903         if(statement->expression) {
1904                 long pn = fold_constant(statement->expression);
1905                 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
1906                         /* oops someone detected our cheating... */
1907                         panic("magic default pn used");
1908                 }
1909                 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
1910         } else {
1911                 saw_default_label = true;
1912                 proj = new_d_defaultProj(dbgi, current_switch_cond,
1913                                          MAGIC_DEFAULT_PN_NUMBER);
1914         }
1915
1916         ir_node *block = new_immBlock();
1917         if (fallthrough != NULL) {
1918                 add_immBlock_pred(block, fallthrough);
1919         }
1920         add_immBlock_pred(block, proj);
1921         mature_immBlock(block);
1922 }
1923
1924 static ir_node *get_label_block(declaration_t *label)
1925 {
1926         assert(label->namespace == NAMESPACE_LABEL);
1927
1928         if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
1929                 return label->v.block;
1930         }
1931         assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
1932
1933         ir_node *old_cur_block = get_cur_block();
1934         ir_node *block         = new_immBlock();
1935         set_cur_block(old_cur_block);
1936
1937         label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
1938         label->v.block          = block;
1939
1940         ARR_APP1(ir_node *, imature_blocks, block);
1941
1942         return block;
1943 }
1944
1945 static void label_to_firm(const label_statement_t *statement)
1946 {
1947         ir_node *block = get_label_block(statement->label);
1948
1949         if(get_cur_block() != NULL) {
1950                 ir_node *jmp = new_Jmp();
1951                 add_immBlock_pred(block, jmp);
1952         }
1953
1954         set_cur_block(block);
1955         keep_alive(block);
1956
1957         statement_to_firm(statement->label_statement);
1958 }
1959
1960 static void goto_to_firm(const goto_statement_t *statement)
1961 {
1962         if(get_cur_block() == NULL)
1963                 return;
1964
1965         ir_node *block = get_label_block(statement->label);
1966         ir_node *jmp   = new_Jmp();
1967         add_immBlock_pred(block, jmp);
1968
1969         set_cur_block(NULL);
1970 }
1971
1972 static void statement_to_firm(statement_t *statement)
1973 {
1974         switch(statement->type) {
1975         case STATEMENT_COMPOUND:
1976                 compound_statement_to_firm((compound_statement_t*) statement);
1977                 return;
1978         case STATEMENT_RETURN:
1979                 return_statement_to_firm((return_statement_t*) statement);
1980                 return;
1981         case STATEMENT_EXPRESSION:
1982                 expression_statement_to_firm((expression_statement_t*) statement);
1983                 return;
1984         case STATEMENT_IF:
1985                 if_statement_to_firm((if_statement_t*) statement);
1986                 return;
1987         case STATEMENT_WHILE:
1988                 while_statement_to_firm((while_statement_t*) statement);
1989                 return;
1990         case STATEMENT_DO_WHILE:
1991                 do_while_statement_to_firm((do_while_statement_t*) statement);
1992                 return;
1993         case STATEMENT_DECLARATION:
1994                 declaration_statement_to_firm((declaration_statement_t*) statement);
1995                 return;
1996         case STATEMENT_BREAK:
1997                 create_jump_statement(statement, break_label);
1998                 return;
1999         case STATEMENT_CONTINUE:
2000                 create_jump_statement(statement, continue_label);
2001                 return;
2002         case STATEMENT_SWITCH:
2003                 switch_statement_to_firm((switch_statement_t*) statement);
2004                 return;
2005         case STATEMENT_CASE_LABEL:
2006                 case_label_to_firm((case_label_statement_t*) statement);
2007                 return;
2008         case STATEMENT_FOR:
2009                 for_statement_to_firm((for_statement_t*) statement);
2010                 return;
2011         case STATEMENT_LABEL:
2012                 label_to_firm((label_statement_t*) statement);
2013                 return;
2014         case STATEMENT_GOTO:
2015                 goto_to_firm((goto_statement_t*) statement);
2016                 return;
2017         default:
2018                 break;
2019         }
2020         panic("Statement not implemented\n");
2021 }
2022
2023 static int get_function_n_local_vars(declaration_t *declaration)
2024 {
2025         (void) declaration;
2026         /* TODO */
2027         return 30;
2028 }
2029
2030 static void initialize_function_parameters(declaration_t *declaration)
2031 {
2032         ir_graph *irg         = current_ir_graph;
2033         ir_node  *args        = get_irg_args(irg);
2034         ir_node  *start_block = get_irg_start_block(irg);
2035
2036         int            n         = 0;
2037         declaration_t *parameter = declaration->context.declarations;
2038         for( ; parameter != NULL; parameter = parameter->next) {
2039                 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2040                 type_t *type = parameter->type;
2041
2042                 bool needs_entity = parameter->address_taken;
2043                 if(type->type == TYPE_COMPOUND_STRUCT
2044                                 || type->type == TYPE_COMPOUND_UNION) {
2045                         needs_entity = true;
2046                 }
2047
2048                 if(needs_entity) {
2049                         panic("entities for function parameters not implemented yet");
2050                 }
2051
2052                 ir_mode *mode = get_ir_mode(parameter->type);
2053                 long     pn   = n;
2054                 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2055                 ++n;
2056
2057                 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2058                 parameter->v.value_number   = next_value_number_function;
2059                 ++next_value_number_function;
2060
2061                 set_value(parameter->v.value_number, proj);
2062         }
2063 }
2064
2065 static void create_function(declaration_t *declaration)
2066 {
2067         ir_entity *entity = get_function_entity(declaration);
2068
2069         if(declaration->init.statement == NULL)
2070                 return;
2071
2072         assert(imature_blocks == NULL);
2073         imature_blocks = NEW_ARR_F(ir_node*, 0);
2074
2075         int       n_local_vars = get_function_n_local_vars(declaration);
2076         ir_graph *irg          = new_ir_graph(entity, n_local_vars);
2077         ir_node  *first_block  = get_cur_block();
2078
2079         next_value_number_function = 0;
2080         initialize_function_parameters(declaration);
2081
2082         statement_to_firm(declaration->init.statement);
2083
2084         ir_node *end_block = get_irg_end_block(irg);
2085
2086         /* do we have a return statement yet? */
2087         if(get_cur_block() != NULL) {
2088                 assert(declaration->type->type == TYPE_FUNCTION);
2089                 const function_type_t* const func_type
2090                         = (const function_type_t*) declaration->type;
2091                 ir_node *ret;
2092                 if (func_type->result_type == type_void) {
2093                         ret = new_Return(get_store(), 0, NULL);
2094                 } else {
2095                         ir_mode *const mode = get_ir_mode(func_type->result_type);
2096                         ir_node *      in[1];
2097                         // ยง5.1.2.2.3 main implicitly returns 0
2098                         if (strcmp(declaration->symbol->string, "main") == 0) {
2099                                 in[0] = new_Const(mode, get_mode_null(mode));
2100                         } else {
2101                                 in[0] = new_Unknown(mode);
2102                         }
2103                         ret = new_Return(get_store(), 1, in);
2104                 }
2105                 add_immBlock_pred(end_block, ret);
2106         }
2107
2108         for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2109                 mature_immBlock(imature_blocks[i]);
2110         }
2111         DEL_ARR_F(imature_blocks);
2112         imature_blocks = NULL;
2113
2114         mature_immBlock(first_block);
2115         mature_immBlock(end_block);
2116
2117         irg_finalize_cons(irg);
2118
2119         /* finalize the frame type */
2120         ir_type *frame_type = get_irg_frame_type(irg);
2121         int      n          = get_compound_n_members(frame_type);
2122         int      align_all  = 4;
2123         int      offset     = 0;
2124         for(int i = 0; i < n; ++i) {
2125                 ir_entity *entity      = get_compound_member(frame_type, i);
2126                 ir_type   *entity_type = get_entity_type(entity);
2127
2128                 int align = get_type_alignment_bytes(entity_type);
2129                 if(align > align_all)
2130                         align_all = align;
2131                 int misalign = 0;
2132                 if(align > 0) {
2133                         misalign  = offset % align;
2134                         offset   += misalign;
2135                 }
2136
2137                 set_entity_offset(entity, offset);
2138                 offset += get_type_size_bytes(entity_type);
2139         }
2140         set_type_size_bytes(frame_type, offset);
2141         set_type_alignment_bytes(frame_type, align_all);
2142         set_type_state(frame_type, layout_fixed);
2143
2144         irg_vrfy(irg);
2145 }
2146
2147 static void create_global_variable(declaration_t *declaration)
2148 {
2149         ir_type   *global_type = get_glob_type();
2150         create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2151                                   global_type);
2152
2153         ir_entity *entity = declaration->v.entity;
2154         if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2155                 set_entity_visibility(entity, visibility_local);
2156         } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2157                 set_entity_visibility(entity, visibility_external_allocated);
2158         } else {
2159                 set_entity_visibility(entity, visibility_external_visible);
2160         }
2161         current_ir_graph = get_const_code_irg();
2162         create_initializer(declaration);
2163 }
2164
2165 static void context_to_firm(context_t *context)
2166 {
2167         declaration_t *declaration = context->declarations;
2168         for( ; declaration != NULL; declaration = declaration->next) {
2169                 if(declaration->namespace != NAMESPACE_NORMAL)
2170                         continue;
2171                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2172                                 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2173                         continue;
2174                 if(declaration->symbol == NULL)
2175                         continue;
2176
2177                 type_t *type = declaration->type;
2178                 if(type->type == TYPE_FUNCTION) {
2179                         create_function(declaration);
2180                 } else {
2181                         create_global_variable(declaration);
2182                 }
2183         }
2184 }
2185
2186 void translation_unit_to_firm(translation_unit_t *unit)
2187 {
2188         /* remove me later TODO FIXME */
2189         (void) get_type_size;
2190
2191         /* just to be sure */
2192         continue_label      = NULL;
2193         break_label         = NULL;
2194         current_switch_cond = NULL;
2195
2196         context_to_firm(& unit->context);
2197 }