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