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