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