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