use short-circuit evaluation like the master taught us
[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
544 static dbg_info *get_dbg_info(const source_position_t *pos)
545 {
546         return (dbg_info*) pos;
547 }
548
549 static ir_node *const_to_firm(const const_t *cnst)
550 {
551         dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
552         ir_mode  *mode = get_ir_mode(cnst->expression.datatype);
553
554         tarval   *tv;
555         if(mode_is_float(mode)) {
556                 tv = new_tarval_from_double(cnst->v.float_value, mode);
557         } else {
558                 tv = new_tarval_from_long(cnst->v.int_value, mode);
559         }
560
561         return new_d_Const(dbgi, mode, tv);
562 }
563
564 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
565 {
566         assert(entity != NULL);
567         union symconst_symbol sym;
568         sym.entity_p = entity;
569         return new_d_SymConst(dbgi, sym, symconst_addr_ent);
570 }
571
572 static ir_node *string_literal_to_firm(const string_literal_t* literal)
573 {
574         ir_type   *global_type = get_glob_type();
575         ir_type   *type        = new_type_array(unique_ident("strtype"), 1,
576                                                 ir_type_const_char);
577
578         ident     *id     = unique_ident("Lstr");
579         ir_entity *entity = new_entity(global_type, id, type);
580         set_entity_ld_ident(entity, id);
581         set_entity_variability(entity, variability_constant);
582
583         ir_type    *elem_type = ir_type_const_char;
584         ir_mode    *mode      = get_type_mode(elem_type);
585
586         const char *string = literal->value;
587         size_t      slen   = strlen(string) + 1;
588
589         set_array_lower_bound_int(type, 0, 0);
590         set_array_upper_bound_int(type, 0, slen);
591         set_type_size_bytes(type, slen);
592         set_type_state(type, layout_fixed);
593
594         tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
595         for(size_t i = 0; i < slen; ++i) {
596                 tvs[i] = new_tarval_from_long(string[i], mode);
597         }
598
599         set_array_entity_values(entity, tvs, slen);
600         free(tvs);
601
602         dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
603
604         return create_symconst(dbgi, entity);
605 }
606
607 static ir_node *load_from_expression_addr(type_t *type, ir_node *addr,
608                                           dbg_info *dbgi)
609 {
610         ir_mode *mode     = get_ir_mode(type);
611         ir_node *memory   = get_store();
612         ir_node *load     = new_d_Load(dbgi, memory, addr, mode);
613         ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
614         ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
615         set_store(load_mem);
616
617         return load_res;
618 }
619
620 static ir_node *deref_address(type_t *const type, ir_node *const addr,
621                               dbg_info *const dbgi)
622 {
623         switch (type->type) {
624                 case TYPE_ARRAY:
625                 case TYPE_COMPOUND_STRUCT:
626                 case TYPE_COMPOUND_UNION:
627                         return addr;
628
629                 default:
630                         return load_from_expression_addr(type, addr, dbgi);
631         }
632 }
633
634 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
635 {
636         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
637         declaration_t *declaration = ref->declaration;
638         type_t        *type        = skip_typeref(declaration->type);
639
640         switch((declaration_type_t) declaration->declaration_type) {
641         case DECLARATION_TYPE_UNKNOWN:
642                 break;
643         case DECLARATION_TYPE_LOCAL_VARIABLE: {
644                 ir_mode *mode = get_ir_mode(type);
645                 return get_value(declaration->v.value_number, mode);
646         }
647         case DECLARATION_TYPE_FUNCTION: {
648                 return create_symconst(dbgi, declaration->v.entity);
649         }
650         case DECLARATION_TYPE_GLOBAL_VARIABLE: {
651                 ir_entity *entity   = declaration->v.entity;
652                 ir_node   *symconst = create_symconst(dbgi, entity);
653                 return deref_address(type, symconst, dbgi);
654         }
655         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
656                 ir_entity *entity = declaration->v.entity;
657                 ir_node   *frame  = get_irg_frame(current_ir_graph);
658                 ir_node   *sel    = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
659                 return deref_address(type, sel, dbgi);
660         }
661
662         case DECLARATION_TYPE_COMPOUND_MEMBER:
663         case DECLARATION_TYPE_LABEL_BLOCK:
664                 panic("not implemented reference type");
665         }
666
667         panic("reference to declaration with unknown type found");
668 }
669
670 static ir_node *reference_addr(const reference_expression_t *ref)
671 {
672         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
673         declaration_t *declaration = ref->declaration;
674
675         switch((declaration_type_t) declaration->declaration_type) {
676         case DECLARATION_TYPE_UNKNOWN:
677                 break;
678         case DECLARATION_TYPE_LOCAL_VARIABLE:
679                 panic("local variable without entity has no address");
680         case DECLARATION_TYPE_FUNCTION: {
681                 return create_symconst(dbgi, declaration->v.entity);
682         }
683         case DECLARATION_TYPE_GLOBAL_VARIABLE: {
684                 ir_entity *entity   = declaration->v.entity;
685                 ir_node   *symconst = create_symconst(dbgi, entity);
686                 return symconst;
687         }
688         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
689                 ir_entity *entity = declaration->v.entity;
690                 ir_node   *frame  = get_irg_frame(current_ir_graph);
691                 ir_node   *sel    = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
692
693                 return sel;
694         }
695         case DECLARATION_TYPE_COMPOUND_MEMBER:
696         case DECLARATION_TYPE_LABEL_BLOCK:
697                 panic("not implemented reference type");
698         }
699
700         panic("reference to declaration with unknown type found");
701 }
702
703 static ir_node *call_expression_to_firm(const call_expression_t *call)
704 {
705         assert(get_cur_block() != NULL);
706
707         expression_t  *function = call->function;
708         ir_node       *callee   = expression_to_firm(function);
709
710         assert(function->datatype->type == TYPE_FUNCTION);
711         function_type_t *function_type = (function_type_t*) function->datatype;
712
713         int              n_parameters = 0;
714         call_argument_t *argument     = call->arguments;
715         for( ; argument != NULL; argument = argument->next) {
716                 ++n_parameters;
717         }
718
719         ir_type *ir_method_type  = get_ir_type((type_t*) function_type);
720         ir_type *new_method_type = NULL;
721         if(function_type->variadic || function_type->unspecified_parameters) {
722                 /* we need to construct a new method type matching the call
723                  * arguments... */
724                 int n_res       = get_method_n_ress(ir_method_type);
725                 new_method_type = new_type_method(unique_ident("calltype"),
726                                                   n_parameters, n_res);
727                 set_method_calling_convention(new_method_type,
728                                get_method_calling_convention(ir_method_type));
729                 set_method_additional_properties(new_method_type,
730                                get_method_additional_properties(ir_method_type));
731
732                 for(int i = 0; i < n_res; ++i) {
733                         set_method_res_type(new_method_type, i,
734                                             get_method_res_type(ir_method_type, i));
735                 }
736         }
737         ir_node *in[n_parameters];
738
739         argument = call->arguments;
740         int n = 0;
741         for( ; argument != NULL; argument = argument->next) {
742                 expression_t *expression = argument->expression;
743                 ir_node      *arg_node   = expression_to_firm(expression);
744
745                 in[n] = arg_node;
746                 if(new_method_type != NULL) {
747                         ir_type *irtype = get_ir_type(expression->datatype);
748                         set_method_param_type(new_method_type, n, irtype);
749                 }
750
751                 n++;
752         }
753         assert(n == n_parameters);
754
755         if(new_method_type != NULL)
756                 ir_method_type = new_method_type;
757
758         dbg_info *dbgi  = get_dbg_info(&call->expression.source_position);
759         ir_node  *store = get_store();
760         ir_node  *node  = new_d_Call(dbgi, store, callee, n_parameters, in,
761                                      ir_method_type);
762         ir_node  *mem   = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
763         set_store(mem);
764
765         type_t  *result_type = function_type->result_type;
766         ir_node *result      = NULL;
767         if(result_type != type_void) {
768                 ir_mode *mode    = get_ir_mode(result_type);
769                 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
770                 result           = new_d_Proj(dbgi, resproj, mode, 0);
771         }
772
773         return result;
774 }
775
776 static ir_node *expression_to_addr(const expression_t *expression);
777 static void create_condition_evaluation(const expression_t *expression,
778                                         ir_node *true_block,
779                                         ir_node *false_block);
780
781 static void set_value_for_expression(const expression_t *expression,
782                                      ir_node *value)
783 {
784         if(expression->type == EXPR_REFERENCE) {
785                 reference_expression_t *ref = (reference_expression_t*) expression;
786
787                 declaration_t *declaration = ref->declaration;
788                 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
789                 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
790                         set_value(declaration->v.value_number, value);
791                         return;
792                 }
793         }
794
795         dbg_info *dbgi      = get_dbg_info(&expression->source_position);
796         ir_node  *addr      = expression_to_addr(expression);
797         assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
798         ir_node  *memory    = get_store();
799         ir_node  *store     = new_d_Store(dbgi, memory, addr, value);
800         ir_node  *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
801         set_store(store_mem);
802 }
803
804 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
805 {
806         ir_mode *value_mode = get_irn_mode(value);
807
808         if(value_mode == dest_mode)
809                 return value;
810
811         if(dest_mode == mode_b) {
812                 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
813                 ir_node *cmp  = new_d_Cmp(dbgi, value, zero);
814                 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
815                 return proj;
816         }
817
818         return new_d_Conv(dbgi, value, dest_mode);
819 }
820
821 static ir_node *create_incdec(const unary_expression_t *expression)
822 {
823         dbg_info     *dbgi  = get_dbg_info(&expression->expression.source_position);
824         type_t       *type  = expression->expression.datatype;
825         ir_mode      *mode  = get_ir_mode(type);
826         expression_t *value = expression->value;
827
828         ir_node *value_node = expression_to_firm(value);
829
830         ir_node *offset;
831         if(type->type == TYPE_POINTER) {
832                 pointer_type_t *pointer_type = (pointer_type_t*) type;
833                 unsigned        elem_size    = get_type_size(pointer_type->points_to);
834                 offset = new_Const_long(mode_Is, elem_size);
835         } else {
836                 assert(is_type_arithmetic(type));
837                 offset = new_Const(mode, get_mode_one(mode));
838         }
839
840         ir_node *new_value;
841         switch(expression->type) {
842         case UNEXPR_POSTFIX_INCREMENT: {
843                 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
844                 set_value_for_expression(value, new_value);
845                 return value_node;
846         }
847         case UNEXPR_POSTFIX_DECREMENT: {
848                 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
849                 set_value_for_expression(value, new_value);
850                 return value_node;
851         }
852         case UNEXPR_PREFIX_INCREMENT: {
853                 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
854                 set_value_for_expression(value, new_value);
855                 return new_value;
856         }
857         case UNEXPR_PREFIX_DECREMENT: {
858                 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
859                 set_value_for_expression(value, new_value);
860                 return new_value;
861         }
862         default:
863                 panic("no incdec expr in create_incdec");
864         }
865
866         return new_value;
867 }
868
869 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
870 {
871         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
872         type_t   *type = expression->expression.datatype;
873         ir_mode  *mode = get_ir_mode(type);
874
875         if(expression->type == UNEXPR_TAKE_ADDRESS)
876                 return expression_to_addr(expression->value);
877
878         const expression_t *value      = expression->value;
879         ir_node            *value_node = expression_to_firm(value);
880
881         switch(expression->type) {
882         case UNEXPR_NEGATE:
883                 return new_d_Minus(dbgi, value_node, mode);
884         case UNEXPR_PLUS:
885                 return value_node;
886         case UNEXPR_BITWISE_NEGATE:
887                 return new_d_Not(dbgi, value_node, mode);
888         case UNEXPR_NOT:
889                 if(get_irn_mode(value_node) != mode_b) {
890                         value_node = create_conv(dbgi, value_node, mode_b);
891                 }
892                 value_node = new_d_Not(dbgi, value_node, mode_b);
893                 if(mode != mode_b) {
894                         value_node = create_conv(dbgi, value_node, mode);
895                 }
896                 return value_node;
897         case UNEXPR_DEREFERENCE:
898                 return load_from_expression_addr(type, value_node, dbgi);
899         case UNEXPR_POSTFIX_INCREMENT:
900         case UNEXPR_POSTFIX_DECREMENT:
901         case UNEXPR_PREFIX_INCREMENT:
902         case UNEXPR_PREFIX_DECREMENT:
903                 return create_incdec(expression);
904         case UNEXPR_CAST:
905                 return create_conv(dbgi, value_node, mode);
906
907         case UNEXPR_TAKE_ADDRESS:
908         case UNEXPR_INVALID:
909                 break;
910         }
911         panic("invalid UNEXPR type found");
912 }
913
914 static long get_pnc(binary_expression_type_t type)
915 {
916         switch(type) {
917         case BINEXPR_EQUAL:        return pn_Cmp_Eq;
918         case BINEXPR_NOTEQUAL:     return pn_Cmp_Lg;
919         case BINEXPR_LESS:         return pn_Cmp_Lt;
920         case BINEXPR_LESSEQUAL:    return pn_Cmp_Le;
921         case BINEXPR_GREATER:      return pn_Cmp_Gt;
922         case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
923         default:
924                 break;
925         }
926         panic("trying to get pn_Cmp from non-comparison binexpr type");
927 }
928
929 static ir_node *create_lazy_op(const binary_expression_t *expression)
930 {
931         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
932         type_t   *type = expression->expression.datatype;
933         ir_mode  *mode = get_ir_mode(type);
934
935         ir_node *cur_block = get_cur_block();
936
937         ir_node *one_block = new_immBlock();
938         ir_node *one       = new_Const(mode, get_mode_one(mode));
939         ir_node *jmp_one   = new_d_Jmp(dbgi);
940
941         ir_node *zero_block = new_immBlock();
942         ir_node *zero       = new_Const(mode, get_mode_null(mode));
943         ir_node *jmp_zero   = new_d_Jmp(dbgi);
944
945         set_cur_block(cur_block);
946         create_condition_evaluation((const expression_t*) expression,
947                                     one_block, zero_block);
948         mature_immBlock(one_block);
949         mature_immBlock(zero_block);
950
951         ir_node *common_block = new_immBlock();
952         add_immBlock_pred(common_block, jmp_one);
953         add_immBlock_pred(common_block, jmp_zero);
954         mature_immBlock(common_block);
955
956         ir_node *in[2] = { one, zero };
957         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
958
959         return val;
960 }
961
962 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
963                                             ir_node *right, ir_mode *mode);
964
965 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
966                                         create_arithmetic_func func)
967 {
968         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
969         ir_node  *left  = expression_to_firm(expression->left);
970         ir_node  *right = expression_to_firm(expression->right);
971         type_t   *type  = expression->right->datatype;
972         /* be careful with the modes, because in asithmetic assign nodes only
973          * the right operand has the mode of the arithmetic alread */
974         ir_mode  *mode  = get_ir_mode(type);
975         left            = create_conv(dbgi, left, mode);
976         ir_node  *res   = func(dbgi, left, right, mode);
977
978         return res;
979 }
980
981 static ir_node *create_arithmetic_assign_binop(
982                 const binary_expression_t *expression, create_arithmetic_func func)
983 {
984         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
985         ir_node  *value = create_arithmetic_binop(expression, func);
986         type_t   *type  = expression->expression.datatype;
987         ir_mode  *mode  = get_ir_mode(type);
988
989         assert(type->type != TYPE_POINTER);
990
991         value = create_conv(dbgi, value, mode);
992         set_value_for_expression(expression->left, value);
993
994         return value;
995 }
996
997 static ir_node *create_add(const binary_expression_t *expression)
998 {
999         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1000         ir_node  *left  = expression_to_firm(expression->left);
1001         ir_node  *right = expression_to_firm(expression->right);
1002         type_t   *type  = expression->expression.datatype;
1003         ir_mode  *mode  = get_ir_mode(type);
1004
1005         expression_t *expr_left  = expression->left;
1006         expression_t *expr_right = expression->right;
1007         type_t       *type_left  = skip_typeref(expr_left->datatype);
1008         type_t       *type_right = skip_typeref(expr_right->datatype);
1009
1010         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1011                 return new_d_Add(dbgi, left, right, mode);
1012         }
1013
1014         ir_node        *pointer;
1015         ir_node        *integer;
1016         pointer_type_t *pointer_type;
1017         if(type_left->type == TYPE_POINTER) {
1018                 pointer      = left;
1019                 integer      = right;
1020                 pointer_type = (pointer_type_t*) type_left;
1021         } else {
1022                 assert(type_right->type == TYPE_POINTER);
1023                 pointer      = right;
1024                 integer      = left;
1025                 pointer_type = (pointer_type_t*) type_right;
1026         }
1027
1028         type_t   *points_to = pointer_type->points_to;
1029         unsigned  elem_size = get_type_size(points_to);
1030
1031         assert(elem_size >= 1);
1032         if(elem_size > 1) {
1033                 integer       = create_conv(dbgi, integer, mode_Is);
1034                 ir_node *cnst = new_Const_long(mode_Is, (int) elem_size);
1035                 ir_node *mul  = new_d_Mul(dbgi, integer, cnst, mode_Is);
1036                 integer = mul;
1037         }
1038
1039         ir_node *res = new_d_Add(dbgi, pointer, integer, mode);
1040
1041         return res;
1042 }
1043
1044 static ir_node *create_sub(const binary_expression_t *expression)
1045 {
1046         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1047         ir_node  *left  = expression_to_firm(expression->left);
1048         ir_node  *right = expression_to_firm(expression->right);
1049         type_t   *type  = expression->expression.datatype;
1050         ir_mode  *mode  = get_ir_mode(type);
1051
1052         expression_t *expr_left  = expression->left;
1053         expression_t *expr_right = expression->right;
1054         type_t       *type_left  = skip_typeref(expr_left->datatype);
1055         type_t       *type_right = skip_typeref(expr_right->datatype);
1056
1057         if((is_type_arithmetic(type_left) && is_type_arithmetic(type_right))
1058                         || (type_left->type == TYPE_POINTER
1059                                 && type_right->type == TYPE_POINTER)) {
1060                 return new_d_Sub(dbgi, left, right, mode);
1061         }
1062
1063         assert(type_right->type == TYPE_POINTER);
1064         ir_node        *pointer      = left;
1065         ir_node        *integer      = right;
1066         pointer_type_t *pointer_type = (pointer_type_t*) type_right;
1067
1068         type_t   *points_to = pointer_type->points_to;
1069         unsigned  elem_size = get_type_size(points_to);
1070
1071         assert(elem_size >= 1);
1072         if(elem_size > 1) {
1073                 ir_node *cnst = new_Const_long(mode_Iu, elem_size);
1074                 ir_node *mul  = new_d_Mul(dbgi, integer, cnst, mode_Iu);
1075                 integer = mul;
1076         }
1077
1078         ir_node *res = new_d_Sub(dbgi, pointer, integer, mode);
1079
1080         return res;
1081 }
1082
1083 static ir_node *create_shift(const binary_expression_t *expression)
1084 {
1085         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1086         ir_node  *left  = expression_to_firm(expression->left);
1087         ir_node  *right = expression_to_firm(expression->right);
1088         type_t   *type  = expression->expression.datatype;
1089         ir_mode  *mode  = get_ir_mode(type);
1090
1091         /* firm always wants the shift count to be unsigned */
1092         right = create_conv(dbgi, right, mode_Iu);
1093
1094         ir_node *res;
1095
1096         switch(expression->type) {
1097         case BINEXPR_SHIFTLEFT:
1098                 res = new_d_Shl(dbgi, left, right, mode);
1099                 break;
1100         case BINEXPR_SHIFTRIGHT: {
1101                  expression_t *expr_left = expression->left;
1102                  type_t       *type_left = skip_typeref(expr_left->datatype);
1103
1104                  if(is_type_signed(type_left)) {
1105                         res = new_d_Shrs(dbgi, left, right, mode);
1106                  } else {
1107                          res = new_d_Shr(dbgi, left, right, mode);
1108                  }
1109                  break;
1110         }
1111         default:
1112                 panic("create shift op called for non-shift op");
1113         }
1114
1115         return res;
1116 }
1117
1118
1119 static ir_node *create_divmod(const binary_expression_t *expression)
1120 {
1121         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1122         ir_node  *left  = expression_to_firm(expression->left);
1123         ir_node  *right = expression_to_firm(expression->right);
1124         ir_node  *pin   = new_Pin(new_NoMem());
1125         type_t   *type  = expression->expression.datatype;
1126         ir_mode  *mode  = get_ir_mode(type);
1127         ir_node  *op;
1128         ir_node  *res;
1129
1130         switch (expression->type)  {
1131                 case BINEXPR_DIV:
1132                 case BINEXPR_DIV_ASSIGN:
1133                         if(mode_is_float(mode)) {
1134                                 op  = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1135                                 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1136                         } else {
1137                                 op  = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1138                                 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1139                         }
1140                         break;
1141
1142                 case BINEXPR_MOD:
1143                 case BINEXPR_MOD_ASSIGN:
1144                         assert(!mode_is_float(mode));
1145                         op  = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1146                         res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1147                         break;
1148
1149                 default: panic("unexpected binary expression type in create_divmod()");
1150         }
1151
1152         return res;
1153 }
1154
1155 static ir_node *create_arithmetic_assign_divmod(
1156                 const binary_expression_t *expression)
1157 {
1158         ir_node  *      value = create_divmod(expression);
1159         dbg_info *const dbgi  = get_dbg_info(&expression->expression.source_position);
1160         type_t   *const type  = expression->expression.datatype;
1161         ir_mode  *const mode  = get_ir_mode(type);
1162
1163         assert(type->type != TYPE_POINTER);
1164
1165         value = create_conv(dbgi, value, mode);
1166         set_value_for_expression(expression->left, value);
1167
1168         return value;
1169 }
1170
1171
1172 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1173 {
1174         binary_expression_type_t type = expression->type;
1175         switch(type) {
1176         case BINEXPR_EQUAL:
1177         case BINEXPR_NOTEQUAL:
1178         case BINEXPR_LESS:
1179         case BINEXPR_LESSEQUAL:
1180         case BINEXPR_GREATER:
1181         case BINEXPR_GREATEREQUAL: {
1182                 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1183                 ir_node *left  = expression_to_firm(expression->left);
1184                 ir_node *right = expression_to_firm(expression->right);
1185                 ir_node *cmp   = new_d_Cmp(dbgi, left, right);
1186                 long     pnc   = get_pnc(type);
1187                 ir_node *proj  = new_d_Proj(dbgi, cmp, mode_b, pnc);
1188                 return proj;
1189         }
1190         case BINEXPR_ASSIGN: {
1191                 ir_node *right = expression_to_firm(expression->right);
1192                 set_value_for_expression(expression->left, right);
1193                 return right;
1194         }
1195         case BINEXPR_ADD:
1196                 return create_add(expression);
1197         case BINEXPR_SUB:
1198                 return create_sub(expression);
1199         case BINEXPR_MUL:
1200                 return create_arithmetic_binop(expression, new_d_Mul);
1201         case BINEXPR_BITWISE_AND:
1202                 return create_arithmetic_binop(expression, new_d_And);
1203         case BINEXPR_BITWISE_OR:
1204                 return create_arithmetic_binop(expression, new_d_Or);
1205         case BINEXPR_BITWISE_XOR:
1206                 return create_arithmetic_binop(expression, new_d_Eor);
1207         case BINEXPR_SHIFTLEFT:
1208         case BINEXPR_SHIFTRIGHT:
1209                 return create_shift(expression);
1210         case BINEXPR_DIV:
1211         case BINEXPR_MOD:
1212                 return create_divmod(expression);
1213         case BINEXPR_LOGICAL_AND:
1214         case BINEXPR_LOGICAL_OR:
1215                 return create_lazy_op(expression);
1216         case BINEXPR_COMMA:
1217                 expression_to_firm(expression->left);
1218                 return expression_to_firm(expression->right);
1219         case BINEXPR_ADD_ASSIGN:
1220                 return create_arithmetic_assign_binop(expression, new_d_Add);
1221         case BINEXPR_SUB_ASSIGN:
1222                 return create_arithmetic_assign_binop(expression, new_d_Sub);
1223         case BINEXPR_MUL_ASSIGN:
1224                 return create_arithmetic_assign_binop(expression, new_d_Mul);
1225         case BINEXPR_DIV_ASSIGN:
1226                 return create_arithmetic_assign_divmod(expression);
1227         case BINEXPR_BITWISE_AND_ASSIGN:
1228                 return create_arithmetic_assign_binop(expression, new_d_And);
1229         case BINEXPR_BITWISE_OR_ASSIGN:
1230                 return create_arithmetic_assign_binop(expression, new_d_Or);
1231         case BINEXPR_BITWISE_XOR_ASSIGN:
1232                 return create_arithmetic_assign_binop(expression, new_d_Eor);
1233         case BINEXPR_SHIFTLEFT_ASSIGN:
1234                 return create_arithmetic_assign_binop(expression, new_d_Shl);
1235         case BINEXPR_SHIFTRIGHT_ASSIGN:
1236                 return create_arithmetic_assign_binop(expression, new_d_Shr);
1237         default:
1238                 panic("TODO binexpr type");
1239         }
1240 }
1241
1242 static ir_node *array_access_addr(const array_access_expression_t *expression)
1243 {
1244         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1245
1246         ir_node *base_addr = expression_to_firm(expression->array_ref);
1247         ir_node *offset    = expression_to_firm(expression->index);
1248         offset             = create_conv(dbgi, offset, mode_Iu);
1249
1250         unsigned elem_size       = get_type_size(expression->expression.datatype);
1251         ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1252         ir_node *real_offset     = new_d_Mul(dbgi, offset, elem_size_const,
1253                                              mode_Iu);
1254         ir_node *result          = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1255
1256         return result;
1257 }
1258
1259 static ir_node *array_access_to_firm(
1260                 const array_access_expression_t *expression)
1261 {
1262         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1263         ir_node  *addr = array_access_addr(expression);
1264         type_t   *type = expression->expression.datatype;
1265         return deref_address(type, addr, dbgi);
1266 }
1267
1268 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1269 {
1270         type_t *type = expression->type;
1271         if(type == NULL) {
1272                 type = expression->size_expression->datatype;
1273                 assert(type != NULL);
1274         }
1275
1276         ir_mode  *mode      = get_ir_mode(expression->expression.datatype);
1277         unsigned  size      = get_type_size(type);
1278         ir_node  *size_node = new_Const_long(mode, size);
1279
1280         return size_node;
1281 }
1282
1283 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1284 {
1285         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1286
1287         ir_node *cur_block   = get_cur_block();
1288
1289         /* create the true block */
1290         ir_node *true_block  = new_immBlock();
1291
1292         ir_node *true_val = expression_to_firm(expression->true_expression);
1293         ir_node *true_jmp = new_Jmp();
1294
1295         /* create the false block */
1296         ir_node *false_block = new_immBlock();
1297
1298         ir_node *false_val = expression_to_firm(expression->false_expression);
1299         ir_node *false_jmp = new_Jmp();
1300
1301         /* create the condition evaluation */
1302         set_cur_block(cur_block);
1303         create_condition_evaluation(expression->condition, true_block, false_block);
1304         mature_immBlock(true_block);
1305         mature_immBlock(false_block);
1306
1307         /* create the common block */
1308         ir_node *common_block = new_immBlock();
1309         add_immBlock_pred(common_block, true_jmp);
1310         add_immBlock_pred(common_block, false_jmp);
1311         mature_immBlock(common_block);
1312
1313         ir_node *in[2] = { true_val, false_val };
1314         ir_mode *mode  = get_irn_mode(true_val);
1315         assert(get_irn_mode(false_val) == mode);
1316         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
1317
1318         return val;
1319 }
1320
1321 static ir_node *select_addr(const select_expression_t *expression)
1322 {
1323         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1324
1325         ir_node *compound_addr = expression_to_firm(expression->compound);
1326
1327         declaration_t *entry = expression->compound_entry;
1328         assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1329         ir_entity     *entity = entry->v.entity;
1330
1331         assert(entity != NULL);
1332
1333         ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1334
1335         return sel;
1336 }
1337
1338 static ir_node *select_to_firm(const select_expression_t *expression)
1339 {
1340         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1341         ir_node  *addr = select_addr(expression);
1342         type_t   *type = expression->expression.datatype;
1343         return deref_address(type, addr, dbgi);
1344 }
1345
1346 static ir_node *dereference_addr(const unary_expression_t *const expression)
1347 {
1348         assert(expression->type == UNEXPR_DEREFERENCE);
1349         return expression_to_firm(expression->value);
1350 }
1351
1352 static ir_node *expression_to_addr(const expression_t *expression)
1353 {
1354         switch(expression->type) {
1355         case EXPR_REFERENCE:
1356                 return reference_addr((const reference_expression_t*) expression);
1357         case EXPR_ARRAY_ACCESS:
1358                 return array_access_addr((const array_access_expression_t*) expression);
1359         case EXPR_SELECT:
1360                 return select_addr((const select_expression_t*) expression);
1361         case EXPR_UNARY: {
1362                 const unary_expression_t *const unary_expr =
1363                         (const unary_expression_t*)expression;
1364                 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1365                         return dereference_addr(unary_expr);
1366                 }
1367                 break;
1368         }
1369         default:
1370                 break;
1371         }
1372         panic("trying to get address of non-lvalue");
1373 }
1374
1375 static ir_node *_expression_to_firm(const expression_t *expression)
1376 {
1377         switch(expression->type) {
1378         case EXPR_CONST:
1379                 return const_to_firm((const const_t*) expression);
1380         case EXPR_STRING_LITERAL:
1381                 return string_literal_to_firm((const string_literal_t*) expression);
1382         case EXPR_REFERENCE:
1383                 return reference_expression_to_firm(
1384                                 (const reference_expression_t*) expression);
1385         case EXPR_CALL:
1386                 return call_expression_to_firm((const call_expression_t*) expression);
1387         case EXPR_UNARY:
1388                 return unary_expression_to_firm((const unary_expression_t*) expression);
1389         case EXPR_BINARY:
1390                 return binary_expression_to_firm(
1391                                 (const binary_expression_t*) expression);
1392         case EXPR_ARRAY_ACCESS:
1393                 return array_access_to_firm(
1394                                 (const array_access_expression_t*) expression);
1395         case EXPR_SIZEOF:
1396                 return sizeof_to_firm((const sizeof_expression_t*) expression);
1397         case EXPR_CONDITIONAL:
1398                 return conditional_to_firm((const conditional_expression_t*)expression);
1399         case EXPR_SELECT:
1400                 return select_to_firm((const select_expression_t*) expression);
1401         case EXPR_FUNCTION:
1402         case EXPR_OFFSETOF:
1403         case EXPR_PRETTY_FUNCTION:
1404         case EXPR_VA_ARG:
1405         case EXPR_STATEMENT:
1406         case EXPR_BUILTIN_SYMBOL:
1407                 panic("unimplemented expression found");
1408
1409         case EXPR_UNKNOWN:
1410         case EXPR_INVALID:
1411                 break;
1412         }
1413         panic("invalid expression found");
1414 }
1415
1416 static ir_node *expression_to_firm(const expression_t *expression)
1417 {
1418         ir_node *res = _expression_to_firm(expression);
1419
1420         if(res != NULL && get_irn_mode(res) == mode_b) {
1421                 ir_mode *mode = get_ir_mode(expression->datatype);
1422                 res           = create_conv(NULL, res, mode);
1423         }
1424
1425         return res;
1426 }
1427
1428 static ir_node *expression_to_modeb(const expression_t *expression)
1429 {
1430         ir_node *res = _expression_to_firm(expression);
1431         res          = create_conv(NULL, res, mode_b);
1432
1433         return res;
1434 }
1435
1436 /**
1437  * create a short-circuit expression evaluation that tries to construct
1438  * efficient control flow structures for &&, || and ! expressions
1439  */
1440 static void create_condition_evaluation(const expression_t *expression,
1441                                         ir_node *true_block,
1442                                         ir_node *false_block)
1443 {
1444         switch(expression->type) {
1445         case EXPR_UNARY: {
1446                 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1447                 if(unary_expression->type == UNEXPR_NOT) {
1448                         create_condition_evaluation(unary_expression->value, false_block,
1449                                                     true_block);
1450                         return;
1451                 }
1452                 break;
1453         }
1454         case EXPR_BINARY: {
1455                 binary_expression_t *binary_expression
1456                         = (binary_expression_t*) expression;
1457                 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1458                         ir_node *cur_block   = get_cur_block();
1459                         ir_node *extra_block = new_immBlock();
1460                         set_cur_block(cur_block);
1461                         create_condition_evaluation(binary_expression->left, extra_block,
1462                                                     false_block);
1463                         mature_immBlock(extra_block);
1464                         set_cur_block(extra_block);
1465                         create_condition_evaluation(binary_expression->right, true_block,
1466                                                     false_block);
1467                         return;
1468                 }
1469                 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1470                         ir_node *cur_block   = get_cur_block();
1471                         ir_node *extra_block = new_immBlock();
1472                         set_cur_block(cur_block);
1473                         create_condition_evaluation(binary_expression->left, true_block,
1474                                                     extra_block);
1475                         mature_immBlock(extra_block);
1476                         set_cur_block(extra_block);
1477                         create_condition_evaluation(binary_expression->right, true_block,
1478                                                     false_block);
1479                         return;
1480                 }
1481                 break;
1482         }
1483         default:
1484                 break;
1485         }
1486
1487         dbg_info *dbgi       = get_dbg_info(&expression->source_position);
1488         ir_node  *condition  = expression_to_modeb(expression);
1489         ir_node  *cond       = new_d_Cond(dbgi, condition);
1490         ir_node  *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1491         ir_node  *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1492
1493         add_immBlock_pred(true_block, true_proj);
1494         add_immBlock_pred(false_block, false_proj);
1495
1496         set_cur_block(NULL);
1497 }
1498
1499 static void statement_to_firm(statement_t *statement);
1500
1501 static void return_statement_to_firm(return_statement_t *statement)
1502 {
1503         if(get_cur_block() == NULL)
1504                 return;
1505
1506         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1507         ir_node  *ret;
1508
1509         if(statement->return_value != NULL) {
1510                 ir_node *retval = expression_to_firm(statement->return_value);
1511                 ir_node *in[1];
1512
1513                 in[0] = retval;
1514                 ret   = new_d_Return(dbgi, get_store(), 1, in);
1515         } else {
1516                 ret   = new_d_Return(dbgi, get_store(), 0, NULL);
1517         }
1518         ir_node *end_block = get_irg_end_block(current_ir_graph);
1519         add_immBlock_pred(end_block, ret);
1520
1521         set_cur_block(NULL);
1522 }
1523
1524 static void compound_statement_to_firm(compound_statement_t *compound)
1525 {
1526         statement_t *statement = compound->statements;
1527         for( ; statement != NULL; statement = statement->next) {
1528                 //context2firm(&statement->context);
1529                 statement_to_firm(statement);
1530         }
1531 }
1532
1533 static void expression_statement_to_firm(expression_statement_t *statement)
1534 {
1535         if(get_cur_block() == NULL)
1536                 return;
1537
1538         expression_to_firm(statement->expression);
1539 }
1540
1541 static void if_statement_to_firm(if_statement_t *statement)
1542 {
1543         ir_node *cur_block = get_cur_block();
1544
1545         ir_node *fallthrough_block = new_immBlock();
1546
1547         /* the true (blocks) */
1548         ir_node *true_block = new_immBlock();
1549
1550         statement_to_firm(statement->true_statement);
1551         if(get_cur_block() != NULL) {
1552                 ir_node *jmp = new_Jmp();
1553                 add_immBlock_pred(fallthrough_block, jmp);
1554         }
1555
1556         /* the false (blocks) */
1557         ir_node *false_block;
1558         if(statement->false_statement != NULL) {
1559                 false_block = new_immBlock();
1560
1561                 statement_to_firm(statement->false_statement);
1562                 if(get_cur_block() != NULL) {
1563                         ir_node *jmp = new_Jmp();
1564                         add_immBlock_pred(fallthrough_block, jmp);
1565                 }
1566         } else {
1567                 false_block = fallthrough_block;
1568         }
1569
1570         /* create the condition */
1571         if(cur_block != NULL) {
1572                 set_cur_block(cur_block);
1573                 create_condition_evaluation(statement->condition, true_block,
1574                                             false_block);
1575         }
1576
1577         mature_immBlock(true_block);
1578         if(false_block != fallthrough_block) {
1579                 mature_immBlock(false_block);
1580         }
1581         mature_immBlock(fallthrough_block);
1582
1583         set_cur_block(fallthrough_block);
1584 }
1585
1586 static void while_statement_to_firm(while_statement_t *statement)
1587 {
1588         ir_node *jmp = NULL;
1589         if(get_cur_block() != NULL) {
1590                 jmp = new_Jmp();
1591         }
1592
1593         /* create the header block */
1594         ir_node *header_block = new_immBlock();
1595         if(jmp != NULL) {
1596                 add_immBlock_pred(header_block, jmp);
1597         }
1598
1599         /* the false block */
1600         ir_node *false_block = new_immBlock();
1601
1602         /* the loop body */
1603         ir_node *body_block = new_immBlock();
1604
1605         ir_node *old_continue_label = continue_label;
1606         ir_node *old_break_label    = break_label;
1607         continue_label              = header_block;
1608         break_label                 = false_block;
1609
1610         statement_to_firm(statement->body);
1611
1612         assert(continue_label == header_block);
1613         assert(break_label    == false_block);
1614         continue_label = old_continue_label;
1615         break_label    = old_break_label;
1616
1617         if(get_cur_block() != NULL) {
1618                 ir_node *jmp = new_Jmp();
1619                 add_immBlock_pred(header_block, jmp);
1620         }
1621
1622         /* create the condition */
1623         set_cur_block(header_block);
1624
1625         create_condition_evaluation(statement->condition, body_block, false_block);
1626         mature_immBlock(body_block);
1627         mature_immBlock(false_block);
1628         mature_immBlock(header_block);
1629
1630         set_cur_block(false_block);
1631 }
1632
1633 static void do_while_statement_to_firm(do_while_statement_t *statement)
1634 {
1635         ir_node *jmp = NULL;
1636         if(get_cur_block() != NULL) {
1637                 jmp = new_Jmp();
1638         }
1639
1640         /* create the header block */
1641         ir_node *header_block = new_immBlock();
1642
1643         /* the false block */
1644         ir_node *false_block = new_immBlock();
1645
1646         /* the loop body */
1647         ir_node *body_block = new_immBlock();
1648         if(jmp != NULL) {
1649                 add_immBlock_pred(body_block, jmp);
1650         }
1651
1652         ir_node *old_continue_label = continue_label;
1653         ir_node *old_break_label    = break_label;
1654         continue_label              = header_block;
1655         break_label                 = false_block;
1656
1657         statement_to_firm(statement->body);
1658
1659         assert(continue_label == header_block);
1660         assert(break_label    == false_block);
1661         continue_label = old_continue_label;
1662         break_label    = old_break_label;
1663
1664         if(get_cur_block() == NULL) {
1665                 mature_immBlock(header_block);
1666                 mature_immBlock(body_block);
1667                 mature_immBlock(false_block);
1668                 return;
1669         }
1670
1671         ir_node *body_jmp = new_Jmp();
1672         add_immBlock_pred(header_block, body_jmp);
1673         mature_immBlock(header_block);
1674
1675         /* create the condition */
1676         set_cur_block(header_block);
1677
1678         create_condition_evaluation(statement->condition, body_block, false_block);
1679         mature_immBlock(body_block);
1680         mature_immBlock(false_block);
1681         mature_immBlock(header_block);
1682
1683         set_cur_block(false_block);
1684 }
1685
1686 static void for_statement_to_firm(for_statement_t *statement)
1687 {
1688         ir_node *jmp = NULL;
1689         if (get_cur_block() != NULL) {
1690                 if(statement->initialisation != NULL) {
1691                         expression_to_firm(statement->initialisation);
1692                 }
1693                 jmp = new_Jmp();
1694         }
1695
1696         /* create the step block */
1697         ir_node *const step_block = new_immBlock();
1698         if (statement->step != NULL) {
1699                 expression_to_firm(statement->step);
1700         }
1701         ir_node *const step_jmp = new_Jmp();
1702
1703         /* create the header block */
1704         ir_node *const header_block = new_immBlock();
1705         if (jmp != NULL) {
1706                 add_immBlock_pred(header_block, jmp);
1707         }
1708         add_immBlock_pred(header_block, step_jmp);
1709
1710         /* the false block */
1711         ir_node *const false_block = new_immBlock();
1712
1713         /* the loop body */
1714         ir_node *const body_block = new_immBlock();
1715
1716         ir_node *const old_continue_label = continue_label;
1717         ir_node *const old_break_label    = break_label;
1718         continue_label = step_block;
1719         break_label    = false_block;
1720
1721         statement_to_firm(statement->body);
1722
1723         assert(continue_label == step_block);
1724         assert(break_label    == false_block);
1725         continue_label = old_continue_label;
1726         break_label    = old_break_label;
1727
1728         if (get_cur_block() != NULL) {
1729                 ir_node *const jmp = new_Jmp();
1730                 add_immBlock_pred(step_block, jmp);
1731         }
1732
1733         /* create the condition */
1734         set_cur_block(header_block);
1735         if (statement->condition != NULL) {
1736                 create_condition_evaluation(statement->condition, body_block,
1737                                             false_block);
1738         } else {
1739                 keep_alive(header_block);
1740                 ir_node *jmp = new_Jmp();
1741                 add_immBlock_pred(body_block, jmp);
1742         }
1743
1744         mature_immBlock(body_block);
1745         mature_immBlock(false_block);
1746         mature_immBlock(step_block);
1747         mature_immBlock(header_block);
1748         mature_immBlock(false_block);
1749
1750         set_cur_block(false_block);
1751 }
1752
1753 static void create_declaration_entity(declaration_t *declaration,
1754                                       declaration_type_t declaration_type,
1755                                       ir_type *parent_type)
1756 {
1757         ident     *id     = new_id_from_str(declaration->symbol->string);
1758         ir_type   *irtype = get_ir_type(declaration->type);
1759         ir_entity *entity = new_entity(parent_type, id, irtype);
1760         set_entity_ld_ident(entity, id);
1761
1762         declaration->declaration_type = declaration_type;
1763         declaration->v.entity         = entity;
1764         set_entity_variability(entity, variability_uninitialized);
1765         /* TODO: visibility? */
1766 }
1767
1768 static void create_initializer(declaration_t *declaration)
1769 {
1770         initializer_t *initializer = declaration->init.initializer;
1771         if(initializer == NULL)
1772                 return;
1773
1774         if(initializer->type == INITIALIZER_VALUE) {
1775                 assert(initializer->designator == NULL);
1776                 assert(initializer->next == NULL);
1777                 ir_node *init_node = expression_to_firm(initializer->v.value);
1778
1779                 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1780                         set_value(declaration->v.value_number, init_node);
1781                 } else {
1782                         ir_entity *entity = declaration->v.entity;
1783
1784                         set_entity_variability(entity, variability_initialized);
1785                         set_atomic_ent_value(entity, init_node);
1786                 }
1787         } else {
1788                 assert(initializer->type == INITIALIZER_LIST);
1789                 panic("list initializer not supported yet");
1790         }
1791 }
1792
1793 static void create_local_variable(declaration_t *declaration)
1794 {
1795         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
1796
1797         bool needs_entity = declaration->address_taken;
1798         type_t *type = skip_typeref(declaration->type);
1799
1800         if(type->type == TYPE_ARRAY
1801                         || type->type == TYPE_COMPOUND_STRUCT
1802                         || type->type == TYPE_COMPOUND_UNION) {
1803                 needs_entity = true;
1804         }
1805
1806         if(needs_entity) {
1807                 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
1808                 create_declaration_entity(declaration,
1809                                           DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
1810                                           frame_type);
1811         } else {
1812                 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1813                 declaration->v.value_number   = next_value_number_function;
1814                 ++next_value_number_function;
1815         }
1816
1817         create_initializer(declaration);
1818 }
1819
1820 static void declaration_statement_to_firm(declaration_statement_t *statement)
1821 {
1822         declaration_t *declaration = statement->declarations_begin;
1823         declaration_t *end         = statement->declarations_end->next;
1824         for( ; declaration != end; declaration = declaration->next) {
1825                 type_t *type = declaration->type;
1826
1827                 switch(declaration->storage_class) {
1828                 case STORAGE_CLASS_TYPEDEF:
1829                         continue;
1830                 case STORAGE_CLASS_STATIC:
1831                         panic("static local vars not implemented yet");
1832                 case STORAGE_CLASS_ENUM_ENTRY:
1833                         panic("enum entry declaration in local block found");
1834                 case STORAGE_CLASS_EXTERN:
1835                         panic("extern declaration in local block found");
1836                 case STORAGE_CLASS_NONE:
1837                 case STORAGE_CLASS_AUTO:
1838                 case STORAGE_CLASS_REGISTER:
1839                         if(type->type == TYPE_FUNCTION) {
1840                                 panic("nested functions not supported yet");
1841                         } else {
1842                                 create_local_variable(declaration);
1843                         }
1844                         continue;
1845                 }
1846                 panic("invalid storage class found");
1847         }
1848 }
1849
1850 static void create_jump_statement(const statement_t *statement,
1851                                   ir_node *target_block)
1852 {
1853         if(get_cur_block() == NULL)
1854                 return;
1855
1856         dbg_info *dbgi = get_dbg_info(&statement->source_position);
1857         ir_node  *jump = new_d_Jmp(dbgi);
1858         add_immBlock_pred(target_block, jump);
1859
1860         set_cur_block(NULL);
1861 }
1862
1863 static void switch_statement_to_firm(const switch_statement_t *statement)
1864 {
1865         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1866
1867         ir_node *expression  = expression_to_firm(statement->expression);
1868         ir_node *cond        = new_d_Cond(dbgi, expression);
1869         ir_node *break_block = new_immBlock();
1870
1871         set_cur_block(NULL);
1872
1873         ir_node *const old_switch_cond       = current_switch_cond;
1874         ir_node *const old_break_label       = break_label;
1875         const bool     old_saw_default_label = saw_default_label;
1876         current_switch_cond                  = cond;
1877         break_label                          = break_block;
1878
1879         statement_to_firm(statement->body);
1880
1881         if(get_cur_block() != NULL) {
1882                 ir_node *jmp = new_Jmp();
1883                 add_immBlock_pred(break_block, jmp);
1884         }
1885
1886         if (!saw_default_label) {
1887                 set_cur_block(get_nodes_block(cond));
1888                 ir_node *const proj = new_d_defaultProj(dbgi, cond,
1889                                                         MAGIC_DEFAULT_PN_NUMBER);
1890                 add_immBlock_pred(break_block, proj);
1891         }
1892
1893         assert(current_switch_cond == cond);
1894         assert(break_label         == break_block);
1895         current_switch_cond = old_switch_cond;
1896         break_label         = old_break_label;
1897         saw_default_label   = old_saw_default_label;
1898
1899         mature_immBlock(break_block);
1900         set_cur_block(break_block);
1901 }
1902
1903 static long fold_constant(const expression_t *expression)
1904 {
1905         ir_graph *old_current_ir_graph = current_ir_graph;
1906         current_ir_graph = get_const_code_irg();
1907
1908         ir_node *cnst = expression_to_firm(expression);
1909         if(!is_Const(cnst)) {
1910                 panic("couldn't fold constantl");
1911         }
1912         tarval *tv = get_Const_tarval(cnst);
1913         if(!tarval_is_long(tv)) {
1914                 panic("folded constant not an integer");
1915         }
1916
1917         long res = get_tarval_long(tv);
1918
1919         current_ir_graph = old_current_ir_graph;
1920         return res;
1921 }
1922
1923 static void case_label_to_firm(const case_label_statement_t *statement)
1924 {
1925         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1926
1927         ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
1928
1929         /* let's create a node and hope firm constant folding creates a Const
1930          * node... */
1931         ir_node *proj;
1932         set_cur_block(get_nodes_block(current_switch_cond));
1933         if(statement->expression) {
1934                 long pn = fold_constant(statement->expression);
1935                 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
1936                         /* oops someone detected our cheating... */
1937                         panic("magic default pn used");
1938                 }
1939                 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
1940         } else {
1941                 saw_default_label = true;
1942                 proj = new_d_defaultProj(dbgi, current_switch_cond,
1943                                          MAGIC_DEFAULT_PN_NUMBER);
1944         }
1945
1946         ir_node *block = new_immBlock();
1947         if (fallthrough != NULL) {
1948                 add_immBlock_pred(block, fallthrough);
1949         }
1950         add_immBlock_pred(block, proj);
1951         mature_immBlock(block);
1952 }
1953
1954 static ir_node *get_label_block(declaration_t *label)
1955 {
1956         assert(label->namespace == NAMESPACE_LABEL);
1957
1958         if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
1959                 return label->v.block;
1960         }
1961         assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
1962
1963         ir_node *old_cur_block = get_cur_block();
1964         ir_node *block         = new_immBlock();
1965         set_cur_block(old_cur_block);
1966
1967         label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
1968         label->v.block          = block;
1969
1970         ARR_APP1(ir_node *, imature_blocks, block);
1971
1972         return block;
1973 }
1974
1975 static void label_to_firm(const label_statement_t *statement)
1976 {
1977         ir_node *block = get_label_block(statement->label);
1978
1979         if(get_cur_block() != NULL) {
1980                 ir_node *jmp = new_Jmp();
1981                 add_immBlock_pred(block, jmp);
1982         }
1983
1984         set_cur_block(block);
1985         keep_alive(block);
1986
1987         statement_to_firm(statement->label_statement);
1988 }
1989
1990 static void goto_to_firm(const goto_statement_t *statement)
1991 {
1992         if(get_cur_block() == NULL)
1993                 return;
1994
1995         ir_node *block = get_label_block(statement->label);
1996         ir_node *jmp   = new_Jmp();
1997         add_immBlock_pred(block, jmp);
1998
1999         set_cur_block(NULL);
2000 }
2001
2002 static void statement_to_firm(statement_t *statement)
2003 {
2004         switch(statement->type) {
2005         case STATEMENT_COMPOUND:
2006                 compound_statement_to_firm((compound_statement_t*) statement);
2007                 return;
2008         case STATEMENT_RETURN:
2009                 return_statement_to_firm((return_statement_t*) statement);
2010                 return;
2011         case STATEMENT_EXPRESSION:
2012                 expression_statement_to_firm((expression_statement_t*) statement);
2013                 return;
2014         case STATEMENT_IF:
2015                 if_statement_to_firm((if_statement_t*) statement);
2016                 return;
2017         case STATEMENT_WHILE:
2018                 while_statement_to_firm((while_statement_t*) statement);
2019                 return;
2020         case STATEMENT_DO_WHILE:
2021                 do_while_statement_to_firm((do_while_statement_t*) statement);
2022                 return;
2023         case STATEMENT_DECLARATION:
2024                 declaration_statement_to_firm((declaration_statement_t*) statement);
2025                 return;
2026         case STATEMENT_BREAK:
2027                 create_jump_statement(statement, break_label);
2028                 return;
2029         case STATEMENT_CONTINUE:
2030                 create_jump_statement(statement, continue_label);
2031                 return;
2032         case STATEMENT_SWITCH:
2033                 switch_statement_to_firm((switch_statement_t*) statement);
2034                 return;
2035         case STATEMENT_CASE_LABEL:
2036                 case_label_to_firm((case_label_statement_t*) statement);
2037                 return;
2038         case STATEMENT_FOR:
2039                 for_statement_to_firm((for_statement_t*) statement);
2040                 return;
2041         case STATEMENT_LABEL:
2042                 label_to_firm((label_statement_t*) statement);
2043                 return;
2044         case STATEMENT_GOTO:
2045                 goto_to_firm((goto_statement_t*) statement);
2046                 return;
2047         default:
2048                 break;
2049         }
2050         panic("Statement not implemented\n");
2051 }
2052
2053 static int get_function_n_local_vars(declaration_t *declaration)
2054 {
2055         (void) declaration;
2056         /* TODO */
2057         return 30;
2058 }
2059
2060 static void initialize_function_parameters(declaration_t *declaration)
2061 {
2062         ir_graph *irg         = current_ir_graph;
2063         ir_node  *args        = get_irg_args(irg);
2064         ir_node  *start_block = get_irg_start_block(irg);
2065
2066         int            n         = 0;
2067         declaration_t *parameter = declaration->context.declarations;
2068         for( ; parameter != NULL; parameter = parameter->next) {
2069                 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2070                 type_t *type = parameter->type;
2071
2072                 bool needs_entity = parameter->address_taken;
2073                 if(type->type == TYPE_COMPOUND_STRUCT
2074                                 || type->type == TYPE_COMPOUND_UNION) {
2075                         needs_entity = true;
2076                 }
2077
2078                 if(needs_entity) {
2079                         panic("entities for function parameters not implemented yet");
2080                 }
2081
2082                 ir_mode *mode = get_ir_mode(parameter->type);
2083                 long     pn   = n;
2084                 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2085                 ++n;
2086
2087                 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2088                 parameter->v.value_number   = next_value_number_function;
2089                 ++next_value_number_function;
2090
2091                 set_value(parameter->v.value_number, proj);
2092         }
2093 }
2094
2095 static void create_function(declaration_t *declaration)
2096 {
2097         ir_entity *entity = get_function_entity(declaration);
2098
2099         if(declaration->init.statement == NULL)
2100                 return;
2101
2102         assert(imature_blocks == NULL);
2103         imature_blocks = NEW_ARR_F(ir_node*, 0);
2104
2105         int       n_local_vars = get_function_n_local_vars(declaration);
2106         ir_graph *irg          = new_ir_graph(entity, n_local_vars);
2107         ir_node  *first_block  = get_cur_block();
2108
2109         next_value_number_function = 0;
2110         initialize_function_parameters(declaration);
2111
2112         statement_to_firm(declaration->init.statement);
2113
2114         ir_node *end_block = get_irg_end_block(irg);
2115
2116         /* do we have a return statement yet? */
2117         if(get_cur_block() != NULL) {
2118                 assert(declaration->type->type == TYPE_FUNCTION);
2119                 const function_type_t* const func_type
2120                         = (const function_type_t*) declaration->type;
2121                 ir_node *ret;
2122                 if (func_type->result_type == type_void) {
2123                         ret = new_Return(get_store(), 0, NULL);
2124                 } else {
2125                         ir_mode *const mode = get_ir_mode(func_type->result_type);
2126                         ir_node *      in[1];
2127                         // ยง5.1.2.2.3 main implicitly returns 0
2128                         if (strcmp(declaration->symbol->string, "main") == 0) {
2129                                 in[0] = new_Const(mode, get_mode_null(mode));
2130                         } else {
2131                                 in[0] = new_Unknown(mode);
2132                         }
2133                         ret = new_Return(get_store(), 1, in);
2134                 }
2135                 add_immBlock_pred(end_block, ret);
2136         }
2137
2138         for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2139                 mature_immBlock(imature_blocks[i]);
2140         }
2141         DEL_ARR_F(imature_blocks);
2142         imature_blocks = NULL;
2143
2144         mature_immBlock(first_block);
2145         mature_immBlock(end_block);
2146
2147         irg_finalize_cons(irg);
2148
2149         /* finalize the frame type */
2150         ir_type *frame_type = get_irg_frame_type(irg);
2151         int      n          = get_compound_n_members(frame_type);
2152         int      align_all  = 4;
2153         int      offset     = 0;
2154         for(int i = 0; i < n; ++i) {
2155                 ir_entity *entity      = get_compound_member(frame_type, i);
2156                 ir_type   *entity_type = get_entity_type(entity);
2157
2158                 int align = get_type_alignment_bytes(entity_type);
2159                 if(align > align_all)
2160                         align_all = align;
2161                 int misalign = 0;
2162                 if(align > 0) {
2163                         misalign  = offset % align;
2164                         offset   += misalign;
2165                 }
2166
2167                 set_entity_offset(entity, offset);
2168                 offset += get_type_size_bytes(entity_type);
2169         }
2170         set_type_size_bytes(frame_type, offset);
2171         set_type_alignment_bytes(frame_type, align_all);
2172         set_type_state(frame_type, layout_fixed);
2173
2174         irg_vrfy(irg);
2175 }
2176
2177 static void create_global_variable(declaration_t *declaration)
2178 {
2179         ir_type   *global_type = get_glob_type();
2180         create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2181                                   global_type);
2182
2183         ir_entity *entity = declaration->v.entity;
2184         if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2185                 set_entity_visibility(entity, visibility_local);
2186         } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2187                 set_entity_visibility(entity, visibility_external_allocated);
2188         } else {
2189                 set_entity_visibility(entity, visibility_external_visible);
2190         }
2191         current_ir_graph = get_const_code_irg();
2192         create_initializer(declaration);
2193 }
2194
2195 static void context_to_firm(context_t *context)
2196 {
2197         declaration_t *declaration = context->declarations;
2198         for( ; declaration != NULL; declaration = declaration->next) {
2199                 if(declaration->namespace != NAMESPACE_NORMAL)
2200                         continue;
2201                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2202                                 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2203                         continue;
2204                 if(declaration->symbol == NULL)
2205                         continue;
2206
2207                 type_t *type = declaration->type;
2208                 if(type->type == TYPE_FUNCTION) {
2209                         create_function(declaration);
2210                 } else {
2211                         create_global_variable(declaration);
2212                 }
2213         }
2214 }
2215
2216 void translation_unit_to_firm(translation_unit_t *unit)
2217 {
2218         /* remove me later TODO FIXME */
2219         (void) get_type_size;
2220
2221         /* just to be sure */
2222         continue_label      = NULL;
2223         break_label         = NULL;
2224         current_switch_cond = NULL;
2225
2226         context_to_firm(& unit->context);
2227 }