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