- implemented non-constant initializers
[cparser] / ast2firm.c
1 #include <config.h>
2
3 #include <assert.h>
4 #include <string.h>
5 #include <stdbool.h>
6
7 #include <libfirm/firm.h>
8 #include <libfirm/adt/obst.h>
9
10 #include "ast2firm.h"
11
12 #include "adt/error.h"
13 #include "adt/array.h"
14 #include "token_t.h"
15 #include "type_t.h"
16 #include "ast_t.h"
17 #include "parser.h"
18 #include "diagnostic.h"
19 #include "lang_features.h"
20 #include "types.h"
21 #include "driver/firm_opt.h"
22 #include "driver/firm_cmdline.h"
23
24 #define MAGIC_DEFAULT_PN_NUMBER     (long) -314159265
25
26 static ir_type *ir_type_const_char;
27 static ir_type *ir_type_wchar_t;
28 static ir_type *ir_type_void;
29 static ir_type *ir_type_int;
30
31 static type_t *type_const_char;
32
33 static int       next_value_number_function;
34 static ir_node  *continue_label;
35 static ir_node  *break_label;
36 static ir_node  *current_switch_cond;
37 static bool      saw_default_label;
38 static ir_node **imature_blocks;
39
40 static const declaration_t *current_function_decl;
41 static ir_node             *current_function_name;
42
43 static struct obstack asm_obst;
44
45 typedef enum declaration_kind_t {
46         DECLARATION_KIND_UNKNOWN,
47         DECLARATION_KIND_FUNCTION,
48         DECLARATION_KIND_GLOBAL_VARIABLE,
49         DECLARATION_KIND_LOCAL_VARIABLE,
50         DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
51         DECLARATION_KIND_COMPOUND_MEMBER,
52         DECLARATION_KIND_LABEL_BLOCK,
53         DECLARATION_KIND_ENUM_ENTRY
54 } declaration_kind_t;
55
56 static ir_type *get_ir_type(type_t *type);
57 static int count_decls_in_stmts(const statement_t *stmt);
58
59 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
60 {
61         const declaration_t *declaration = get_irg_loc_description(irg, pos);
62
63         warningf(declaration->source_position,
64                  "variable '%#T' might be used uninitialized",
65                  declaration->type, declaration->symbol);
66         return new_r_Unknown(irg, mode);
67 }
68
69 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
70 {
71         const source_position_t *pos = (const source_position_t*) dbg;
72         if(pos == NULL)
73                 return 0;
74         return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
75                                    pos->linenr);
76 }
77
78 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
79 {
80         const source_position_t *pos = (const source_position_t*) dbg;
81         if(pos == NULL)
82                 return NULL;
83         if(line != NULL)
84                 *line = pos->linenr;
85         return pos->input_name;
86 }
87
88 static dbg_info *get_dbg_info(const source_position_t *pos)
89 {
90         return (dbg_info*) pos;
91 }
92
93 static unsigned unique_id = 0;
94
95 static ident *unique_ident(const char *tag)
96 {
97         char buf[256];
98
99         snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
100         unique_id++;
101         return new_id_from_str(buf);
102 }
103
104 /**
105  * Return the signed integer mode of size bytes.
106  *
107  * @param size   the size
108  */
109 static ir_mode *get_smode(unsigned size)
110 {
111         static ir_mode *s_modes[16 + 1] = {0, };
112         ir_mode *res;
113
114         if (size <= 0 || size > 16)
115                 return NULL;
116
117         res = s_modes[size];
118         if (res == NULL) {
119                 unsigned bits;
120         char name[32];
121
122         bits = size * 8;
123         snprintf(name, sizeof(name), "i%u", bits);
124         res = new_ir_mode(name, irms_int_number, bits, 1, irma_twos_complement,
125                                         bits <= machine_size ? machine_size : bits );
126
127                 s_modes[size] = res;
128         }
129         return res;
130 }
131
132 /**
133  * Return the unsigned integer mode of size bytes.
134  *
135  * @param size  the size
136  */
137 static ir_mode *get_umode(unsigned size)
138 {
139         static ir_mode *u_modes[16 + 1] = {0, };
140         ir_mode *res;
141
142         if (size <= 0 || size > 16)
143                 return NULL;
144
145         res = u_modes[size];
146         if (res == NULL) {
147                 unsigned bits;
148                 char name[32];
149
150                 bits = size * 8;
151                 snprintf(name, sizeof(name), "u%u", bits);
152                 res = new_ir_mode(name, irms_int_number, bits, 0, irma_twos_complement,
153                                                 bits <= machine_size ? machine_size : bits );
154
155                 u_modes[size] = res;
156         }
157         return res;
158 }
159
160 /**
161  * Return the pointer mode of size bytes.
162  *
163  * @param size  the size
164  */
165 static ir_mode *get_ptrmode(unsigned size, char *name)
166 {
167         static ir_mode *p_modes[16 + 1] = {0, };
168         ir_mode *res;
169
170         if (size <= 0 || size > 16)
171                 return NULL;
172
173         res = p_modes[size];
174         if (res == NULL) {
175                 unsigned bits;
176                 char buf[32];
177
178                 bits = size * 8;
179                 if (name == NULL) {
180                         snprintf(buf, sizeof(buf), "p%u", bits);
181                         name = buf;
182                 }
183                 res = new_ir_mode(name, irms_reference, bits, 0, irma_twos_complement,
184                                                 bits <= machine_size ? machine_size : bits);
185
186                 p_modes[size] = res;
187
188                 set_reference_mode_signed_eq(res, get_smode(size));
189                 set_reference_mode_unsigned_eq(res, get_umode(size));
190         }
191         return res;
192 }
193
194 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST];
195
196 static ir_mode *mode_int, *mode_uint;
197
198 /**
199  * Initialises the atomic modes depending on the machine size.
200  */
201 static void init_atomic_modes(void) {
202         unsigned int_size   = machine_size < 32 ? 2 : 4;
203         unsigned long_size  = machine_size < 64 ? 4 : 8;
204         unsigned llong_size = machine_size < 32 ? 4 : 8;
205
206         /* firm has no real void... */
207         _atomic_modes[ATOMIC_TYPE_VOID]        = mode_T;
208         _atomic_modes[ATOMIC_TYPE_CHAR]        = char_is_signed ? get_smode(1) : get_umode(1);
209         _atomic_modes[ATOMIC_TYPE_SCHAR]       = get_smode(1);
210         _atomic_modes[ATOMIC_TYPE_UCHAR]       = get_umode(1);
211         _atomic_modes[ATOMIC_TYPE_SHORT]       = get_smode(2);
212         _atomic_modes[ATOMIC_TYPE_USHORT]      = get_umode(2);
213         _atomic_modes[ATOMIC_TYPE_INT]         = get_smode(int_size);
214         _atomic_modes[ATOMIC_TYPE_UINT]        = get_umode(int_size);
215         _atomic_modes[ATOMIC_TYPE_LONG]        = get_smode(long_size);
216         _atomic_modes[ATOMIC_TYPE_ULONG]       = get_umode(long_size);
217         _atomic_modes[ATOMIC_TYPE_LONGLONG]    = get_smode(llong_size);
218         _atomic_modes[ATOMIC_TYPE_ULONGLONG]   = get_umode(llong_size);
219         _atomic_modes[ATOMIC_TYPE_FLOAT]       = mode_F;
220         _atomic_modes[ATOMIC_TYPE_DOUBLE]      = mode_D;
221         _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = mode_E;
222         _atomic_modes[ATOMIC_TYPE_BOOL]        = get_umode(int_size);
223
224 #ifdef PROVIDE_COMPLEX
225         _atomic_modes[ATOMIC_TYPE_BOOL]                  = _atomic_modes[ATOMIC_TYPE_INT];
226         _atomic_modes[ATOMIC_TYPE_FLOAT_IMAGINARY]       = _atomic_modes[ATOMIC_TYPE_FLOAT];
227         _atomic_modes[ATOMIC_TYPE_DOUBLE_IMAGINARY]      = _atomic_modes[ATOMIC_TYPE_DOUBLE];
228         _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE];
229 #endif
230
231         /* Hmm, pointers should be machine size */
232         set_modeP_data(get_ptrmode(machine_size >> 3, NULL));
233         set_modeP_code(get_ptrmode(machine_size >> 3, NULL));
234
235         mode_int  = _atomic_modes[ATOMIC_TYPE_INT];
236         mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
237 }
238
239 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
240 {
241         ir_mode *res = NULL;
242         if ((unsigned)atomic_type->akind < (unsigned)ATOMIC_TYPE_LAST)
243                 res = _atomic_modes[(unsigned)atomic_type->akind];
244         if (res == NULL)
245                 panic("Encountered unknown atomic type");
246         return res;
247 }
248
249 static unsigned get_type_size(type_t *type);
250
251 static unsigned get_atomic_type_size(const atomic_type_t *type)
252 {
253         switch(type->akind) {
254         case ATOMIC_TYPE_CHAR:
255         case ATOMIC_TYPE_SCHAR:
256         case ATOMIC_TYPE_UCHAR:
257                 return 1;
258
259         case ATOMIC_TYPE_SHORT:
260         case ATOMIC_TYPE_USHORT:
261                 return 2;
262
263         case ATOMIC_TYPE_BOOL:
264         case ATOMIC_TYPE_INT:
265         case ATOMIC_TYPE_UINT:
266                 return machine_size >> 3;
267
268         case ATOMIC_TYPE_LONG:
269         case ATOMIC_TYPE_ULONG:
270                 return machine_size > 16 ? machine_size >> 3 : 4;
271
272         case ATOMIC_TYPE_LONGLONG:
273         case ATOMIC_TYPE_ULONGLONG:
274                 return machine_size > 16 ? 8 : 4;
275
276         case ATOMIC_TYPE_FLOAT:
277                 return 4;
278
279         case ATOMIC_TYPE_DOUBLE:
280                 return 8;
281
282         case ATOMIC_TYPE_LONG_DOUBLE:
283                 return 12;
284
285         case ATOMIC_TYPE_VOID:
286                 return 1;
287
288         case ATOMIC_TYPE_INVALID:
289         case ATOMIC_TYPE_LAST:
290                 break;
291         }
292         panic("Trying to determine size of invalid atomic type");
293 }
294
295 static unsigned get_compound_type_size(compound_type_t *type)
296 {
297         ir_type *irtype = get_ir_type((type_t*) type);
298         return get_type_size_bytes(irtype);
299 }
300
301 static unsigned get_array_type_size(array_type_t *type)
302 {
303         ir_type *irtype = get_ir_type((type_t*) type);
304         return get_type_size_bytes(irtype);
305 }
306
307
308 static unsigned get_type_size(type_t *type)
309 {
310         type = skip_typeref(type);
311
312         switch(type->kind) {
313         case TYPE_ERROR:
314                 panic("error type occured");
315         case TYPE_ATOMIC:
316                 return get_atomic_type_size(&type->atomic);
317         case TYPE_ENUM:
318                 return get_mode_size_bytes(mode_int);
319         case TYPE_COMPOUND_UNION:
320         case TYPE_COMPOUND_STRUCT:
321                 return get_compound_type_size(&type->compound);
322         case TYPE_FUNCTION:
323                 /* just a pointer to the function */
324                 return get_mode_size_bytes(mode_P_code);
325         case TYPE_POINTER:
326                 return get_mode_size_bytes(mode_P_data);
327         case TYPE_ARRAY:
328                 return get_array_type_size(&type->array);
329         case TYPE_BUILTIN:
330                 return get_type_size(type->builtin.real_type);
331         case TYPE_BITFIELD:
332                 panic("type size of bitfield request");
333         case TYPE_TYPEDEF:
334         case TYPE_TYPEOF:
335         case TYPE_INVALID:
336                 break;
337         }
338         panic("Trying to determine size of invalid type");
339 }
340
341 static unsigned count_parameters(const function_type_t *function_type)
342 {
343         unsigned count = 0;
344
345         function_parameter_t *parameter = function_type->parameters;
346         for ( ; parameter != NULL; parameter = parameter->next) {
347                 ++count;
348         }
349
350         return count;
351 }
352
353
354 static ir_type *create_atomic_type(const atomic_type_t *type)
355 {
356         dbg_info *dbgi  = get_dbg_info(&type->type.source_position);
357         ir_mode *mode   = get_atomic_mode(type);
358         ident   *id     = get_mode_ident(mode);
359         ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
360
361         if(type->akind == ATOMIC_TYPE_LONG_DOUBLE
362                         || type->akind == ATOMIC_TYPE_DOUBLE) {
363                 set_type_alignment_bytes(irtype, 4);
364         }
365
366         return irtype;
367 }
368
369 static ir_type *create_method_type(const function_type_t *function_type)
370 {
371         type_t  *return_type  = function_type->return_type;
372
373         ident   *id           = unique_ident("functiontype");
374         int      n_parameters = count_parameters(function_type);
375         int      n_results    = return_type == type_void ? 0 : 1;
376         dbg_info *dbgi        = get_dbg_info(&function_type->type.source_position);
377         ir_type *irtype       = new_d_type_method(id, n_parameters, n_results, dbgi);
378
379         if(return_type != type_void) {
380                 ir_type *restype = get_ir_type(return_type);
381                 set_method_res_type(irtype, 0, restype);
382         }
383
384         function_parameter_t *parameter = function_type->parameters;
385         int                   n         = 0;
386         for( ; parameter != NULL; parameter = parameter->next) {
387                 ir_type *p_irtype = get_ir_type(parameter->type);
388                 set_method_param_type(irtype, n, p_irtype);
389                 ++n;
390         }
391
392         if(function_type->variadic || function_type->unspecified_parameters) {
393                 set_method_variadicity(irtype, variadicity_variadic);
394         }
395
396         return irtype;
397 }
398
399 static ir_type *create_pointer_type(pointer_type_t *type)
400 {
401         type_t  *points_to = type->points_to;
402         ir_type *ir_points_to;
403         /* Avoid endless recursion if the points_to type contains this poiner type
404          * again (might be a struct). We therefore first create a void* pointer
405          * and then set the real points_to type
406          */
407         dbg_info *dbgi   = get_dbg_info(&type->type.source_position);
408         ir_type *ir_type = new_d_type_pointer(unique_ident("pointer"),
409                                             ir_type_void, mode_P_data, dbgi);
410         type->type.firm_type  = ir_type;
411
412         ir_points_to = get_ir_type(points_to);
413         set_pointer_points_to_type(ir_type, ir_points_to);
414
415         return ir_type;
416 }
417
418 static ir_type *create_array_type(array_type_t *type)
419 {
420         type_t  *element_type    = type->element_type;
421         ir_type *ir_element_type = get_ir_type(element_type);
422
423         ident    *id      = unique_ident("array");
424         dbg_info *dbgi    = get_dbg_info(&type->type.source_position);
425         ir_type  *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
426
427         const int align = get_type_alignment_bytes(ir_element_type);
428         set_type_alignment_bytes(ir_type, align);
429
430         if(type->size_constant) {
431                 int n_elements = type->size;
432
433                 set_array_bounds_int(ir_type, 0, 0, n_elements);
434
435                 size_t elemsize = get_type_size_bytes(ir_element_type);
436                 if(elemsize % align > 0) {
437                         elemsize += align - (elemsize % align);
438                 }
439                 set_type_size_bytes(ir_type, n_elements * elemsize);
440         } else {
441                 set_array_lower_bound_int(ir_type, 0, 0);
442         }
443         set_type_state(ir_type, layout_fixed);
444
445         return ir_type;
446 }
447
448 /**
449  * Return the signed integer type of size bits.
450  *
451  * @param size   the size
452  */
453 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
454                                                  unsigned size)
455 {
456         static ir_mode *s_modes[64 + 1] = {NULL, };
457         ir_type *res;
458         ir_mode *mode;
459
460         if (size <= 0 || size > 64)
461                 return NULL;
462
463         mode = s_modes[size];
464         if (mode == NULL) {
465                 char name[32];
466
467                 snprintf(name, sizeof(name), "bf_I%u", size);
468                 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
469                                    size <= 32 ? 32 : size );
470                 s_modes[size] = mode;
471         }
472
473         char name[32];
474         snprintf(name, sizeof(name), "I%u", size);
475         ident *id = new_id_from_str(name);
476         dbg_info *dbgi = get_dbg_info(&builtin_source_position);
477         res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
478         set_primitive_base_type(res, base_tp);
479
480         return res;
481 }
482
483 /**
484  * Return the unsigned integer type of size bits.
485  *
486  * @param size   the size
487  */
488 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
489                                                    unsigned size)
490 {
491         static ir_mode *u_modes[64 + 1] = {NULL, };
492         ir_type *res;
493         ir_mode *mode;
494
495         if (size <= 0 || size > 64)
496                 return NULL;
497
498         mode = u_modes[size];
499         if (mode == NULL) {
500                 char name[32];
501
502                 snprintf(name, sizeof(name), "bf_U%u", size);
503                 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
504                                    size <= 32 ? 32 : size );
505                 u_modes[size] = mode;
506         }
507
508         char name[32];
509
510         snprintf(name, sizeof(name), "U%u", size);
511         ident *id = new_id_from_str(name);
512         dbg_info *dbgi = get_dbg_info(&builtin_source_position);
513         res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
514         set_primitive_base_type(res, base_tp);
515
516         return res;
517 }
518
519 static ir_type *create_bitfield_type(bitfield_type_t *const type)
520 {
521         type_t *base = skip_typeref(type->base);
522         assert(base->kind == TYPE_ATOMIC);
523         ir_type *irbase = get_ir_type(base);
524
525         unsigned size = fold_constant(type->size);
526
527         assert(!is_type_float(base));
528         if(is_type_signed(base)) {
529                 return get_signed_int_type_for_bit_size(irbase, size);
530         } else {
531                 return get_unsigned_int_type_for_bit_size(irbase, size);
532         }
533 }
534
535 #define INVALID_TYPE ((ir_type_ptr)-1)
536
537 static ir_type *create_union_type(compound_type_t *type, ir_type *irtype,
538                                   size_t *outer_offset, size_t *outer_align);
539
540 static ir_type *create_struct_type(compound_type_t *type, ir_type *irtype,
541                                    size_t *outer_offset, size_t *outer_align)
542 {
543         declaration_t *declaration = type->declaration;
544         if(declaration->v.irtype != NULL) {
545                 return declaration->v.irtype;
546         }
547
548         size_t align_all  = 1;
549         size_t offset     = 0;
550         size_t bit_offset = 0;
551         if(irtype == NULL) {
552                 symbol_t *symbol = declaration->symbol;
553                 ident    *id;
554                 if(symbol != NULL) {
555                         id = unique_ident(symbol->string);
556                 } else {
557                         id = unique_ident("__anonymous_struct");
558                 }
559                 dbg_info *dbgi  = get_dbg_info(&type->type.source_position);
560
561                 irtype = new_d_type_struct(id, dbgi);
562
563                 declaration->v.irtype = irtype;
564                 type->type.firm_type  = irtype;
565         } else {
566                 offset    = *outer_offset;
567                 align_all = *outer_align;
568         }
569
570         declaration_t *entry = declaration->scope.declarations;
571         for( ; entry != NULL; entry = entry->next) {
572                 if(entry->namespc != NAMESPACE_NORMAL)
573                         continue;
574
575                 symbol_t *symbol     = entry->symbol;
576                 type_t   *entry_type = skip_typeref(entry->type);
577                 dbg_info *dbgi       = get_dbg_info(&entry->source_position);
578                 ident    *ident;
579                 if(symbol != NULL) {
580                         ident = new_id_from_str(symbol->string);
581                 } else {
582                         if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
583                                 create_struct_type(&entry_type->compound, irtype, &offset,
584                                                    &align_all);
585                                 continue;
586                         } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
587                                 create_union_type(&entry_type->compound, irtype, &offset,
588                                                   &align_all);
589                                 continue;
590                         } else {
591                                 assert(entry_type->kind == TYPE_BITFIELD);
592                         }
593                         ident = unique_ident("anon");
594                 }
595
596                 ir_type *base_irtype;
597                 if(entry_type->kind == TYPE_BITFIELD) {
598                         base_irtype = get_ir_type(entry_type->bitfield.base);
599                 } else {
600                         base_irtype = get_ir_type(entry_type);
601                 }
602
603                 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
604                 size_t misalign        = offset % entry_alignment;
605
606                 ir_type   *entry_irtype = get_ir_type(entry_type);
607                 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
608
609                 size_t base;
610                 size_t bits_remainder;
611                 if(entry_type->kind == TYPE_BITFIELD) {
612                         size_t size_bits      = fold_constant(entry_type->bitfield.size);
613                         size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
614
615                         if(size_bits > rest_size_bits) {
616                                 /* start a new bucket */
617                                 offset     += entry_alignment - misalign;
618                                 bit_offset  = 0;
619
620                                 base           = offset;
621                                 bits_remainder = 0;
622                         } else {
623                                 /* put into current bucket */
624                                 base           = offset - misalign;
625                                 bits_remainder = misalign * 8 + bit_offset;
626                         }
627
628                         offset     += size_bits / 8;
629                         bit_offset  = bit_offset + (size_bits % 8);
630                 } else {
631                         size_t entry_size = get_type_size_bytes(base_irtype);
632                         if(misalign > 0 || bit_offset > 0)
633                                 offset += entry_alignment - misalign;
634
635                         base           = offset;
636                         bits_remainder = 0;
637                         offset        += entry_size;
638                         bit_offset     = 0;
639                 }
640
641                 if(entry_alignment > align_all) {
642                         if(entry_alignment % align_all != 0) {
643                                 panic("uneven alignments not supported yet");
644                         }
645                         align_all = entry_alignment;
646                 }
647
648                 set_entity_offset(entity, base);
649                 set_entity_offset_bits_remainder(entity,
650                                                  (unsigned char) bits_remainder);
651                 //add_struct_member(irtype, entity);
652                 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
653                 assert(entry->v.entity == NULL);
654                 entry->v.entity         = entity;
655         }
656
657         size_t misalign = offset % align_all;
658         if(misalign > 0 || bit_offset > 0) {
659                 offset += align_all - misalign;
660         }
661
662         if(outer_offset != NULL) {
663                 *outer_offset = offset;
664                 *outer_align  = align_all;
665         } else {
666                 set_type_alignment_bytes(irtype, align_all);
667                 set_type_size_bytes(irtype, offset);
668                 set_type_state(irtype, layout_fixed);
669         }
670
671         return irtype;
672 }
673
674 static ir_type *create_union_type(compound_type_t *type, ir_type *irtype,
675                                   size_t *outer_offset, size_t *outer_align)
676 {
677         declaration_t *declaration = type->declaration;
678         if(declaration->v.irtype != NULL) {
679                 return declaration->v.irtype;
680         }
681
682         size_t align_all = 1;
683         size_t offset    = 0;
684         size_t size      = 0;
685
686         if(irtype == NULL) {
687                 symbol_t      *symbol      = declaration->symbol;
688                 ident         *id;
689                 if(symbol != NULL) {
690                         id = unique_ident(symbol->string);
691                 } else {
692                         id = unique_ident("__anonymous_union");
693                 }
694                 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
695
696                 irtype = new_d_type_union(id, dbgi);
697
698                 declaration->v.irtype = irtype;
699                 type->type.firm_type  = irtype;
700         } else {
701                 offset    = *outer_offset;
702                 align_all = *outer_align;
703         }
704
705         type->type.firm_type = irtype;
706
707         declaration_t *entry = declaration->scope.declarations;
708         for( ; entry != NULL; entry = entry->next) {
709                 if(entry->namespc != NAMESPACE_NORMAL)
710                         continue;
711
712                 symbol_t *symbol        = entry->symbol;
713                 type_t   *entry_type    = skip_typeref(entry->type);
714                 ir_type  *entry_ir_type = get_ir_type(entry_type);
715
716                 ident *ident;
717                 if(symbol != NULL) {
718                         ident = new_id_from_str(entry->symbol->string);
719                 } else {
720                         size_t offs = offset;
721                         if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
722                                 create_struct_type(&entry_type->compound, irtype, &offs,
723                                                    &align_all);
724                                 continue;
725                         } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
726                                 create_union_type(&entry_type->compound, irtype, &offs,
727                                                   &align_all);
728                                 continue;
729                         } else {
730                                 panic("anonymous union member must be struct or union");
731                         }
732                         size_t entry_size = offs - offset;
733                         if(entry_size > size) {
734                                 size = entry_size;
735                         }
736                         ident = unique_ident("anon");
737                 }
738
739                 size_t entry_size      = get_type_size_bytes(entry_ir_type);
740                 size_t entry_alignment = get_type_alignment_bytes(entry_ir_type);
741
742                 dbg_info  *const dbgi   = get_dbg_info(&entry->source_position);
743                 ir_entity *const entity = new_d_entity(irtype, ident, entry_ir_type,
744                                                        dbgi);
745                 //add_union_member(irtype, entity);
746                 set_entity_offset(entity, 0);
747                 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
748                 assert(entry->v.entity == NULL);
749                 entry->v.entity         = entity;
750
751                 if(entry_size > size) {
752                         size = entry_size;
753                 }
754                 if(entry_alignment > align_all) {
755                         if(entry_alignment % align_all != 0) {
756                                 panic("Uneven alignments not supported yet");
757                         }
758                         align_all = entry_alignment;
759                 }
760         }
761
762         if(outer_offset != NULL) {
763                 assert(*outer_offset == offset);
764
765                 size_t misalign = offset % align_all;
766                 if (misalign != 0)
767                         size += align_all - misalign;
768                 *outer_offset += size;
769
770                 if(align_all > *outer_align) {
771                         if(align_all % *outer_align != 0) {
772                                 panic("uneven alignments not supported yet");
773                         }
774                         *outer_align = align_all;
775                 }
776         } else {
777                 set_type_alignment_bytes(irtype, align_all);
778                 set_type_size_bytes(irtype, size);
779                 set_type_state(irtype, layout_fixed);
780                 declaration->v.irtype = irtype;
781         }
782
783         return irtype;
784 }
785
786 static ir_node *expression_to_firm(const expression_t *expression);
787 static inline ir_mode *get_ir_mode(type_t *type);
788
789 static ir_type *create_enum_type(enum_type_t *const type)
790 {
791         type->type.firm_type = ir_type_int;
792
793         ir_mode *const mode    = mode_int;
794         tarval  *const one     = get_mode_one(mode);
795         tarval  *      tv_next = get_tarval_null(mode);
796
797         declaration_t *declaration = type->declaration->next;
798         for (; declaration != NULL; declaration = declaration->next) {
799                 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
800                         break;
801
802                 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
803
804                 expression_t *const init = declaration->init.enum_value;
805                 if (init != NULL) {
806                         ir_node *const cnst = expression_to_firm(init);
807                         if (!is_Const(cnst)) {
808                                 panic("couldn't fold constant");
809                         }
810                         tv_next = get_Const_tarval(cnst);
811                 }
812                 declaration->v.enum_val = tv_next;
813                 tv_next = tarval_add(tv_next, one);
814         }
815
816         return ir_type_int;
817 }
818
819 static ir_type *get_ir_type(type_t *type)
820 {
821         assert(type != NULL);
822
823         type = skip_typeref(type);
824
825         if(type->base.firm_type != NULL) {
826                 assert(type->base.firm_type != INVALID_TYPE);
827                 return type->base.firm_type;
828         }
829
830         ir_type *firm_type = NULL;
831         switch(type->kind) {
832         case TYPE_ERROR:
833                 panic("error type occured");
834         case TYPE_ATOMIC:
835                 firm_type = create_atomic_type(&type->atomic);
836                 break;
837         case TYPE_FUNCTION:
838                 firm_type = create_method_type(&type->function);
839                 break;
840         case TYPE_POINTER:
841                 firm_type = create_pointer_type(&type->pointer);
842                 break;
843         case TYPE_ARRAY:
844                 firm_type = create_array_type(&type->array);
845                 break;
846         case TYPE_COMPOUND_STRUCT:
847                 firm_type = create_struct_type(&type->compound, NULL, NULL, NULL);
848                 break;
849         case TYPE_COMPOUND_UNION:
850                 firm_type = create_union_type(&type->compound, NULL, NULL, NULL);
851                 break;
852         case TYPE_ENUM:
853                 firm_type = create_enum_type(&type->enumt);
854                 break;
855         case TYPE_BUILTIN:
856                 firm_type = get_ir_type(type->builtin.real_type);
857                 break;
858         case TYPE_BITFIELD:
859                 firm_type = create_bitfield_type(&type->bitfield);
860                 break;
861
862         case TYPE_TYPEOF:
863         case TYPE_TYPEDEF:
864         case TYPE_INVALID:
865                 break;
866         }
867         if(firm_type == NULL)
868                 panic("unknown type found");
869
870         type->base.firm_type = firm_type;
871         return firm_type;
872 }
873
874 static inline ir_mode *get_ir_mode(type_t *type)
875 {
876         ir_type *irtype = get_ir_type(type);
877
878         /* firm doesn't report a mode for arrays somehow... */
879         if(is_Array_type(irtype)) {
880                 return mode_P_data;
881         }
882
883         ir_mode *mode = get_type_mode(irtype);
884         assert(mode != NULL);
885         return mode;
886 }
887
888 static ident *predef_idents[rts_max];
889
890 /** Names of the runtime functions. */
891 static const struct {
892         int        id;           /**< the rts id */
893         int        n_res;        /**< number of return values */
894         const char *name;        /**< the name of the rts function */
895         int        n_params;     /**< number of parameters */
896         unsigned   flags;        /**< language flags */
897 } rts_data[] = {
898         { rts_debugbreak, 0, "__debugbreak", 0, _MS },
899         { rts_abort,      0, "abort",        0, _C89 },
900         { rts_abs,        1, "abs",          1, _C89 },
901         { rts_labs,       1, "labs",         1, _C89 },
902         { rts_llabs,      1, "llabs",        1, _C99 },
903         { rts_imaxabs,    1, "imaxabs",      1, _C99 },
904
905         { rts_fabs,       1, "fabs",         1, _C89 },
906         { rts_sqrt,       1, "sqrt",         1, _C89 },
907         { rts_cbrt,       1, "cbrt",         1, _C99 },
908         { rts_exp,        1, "exp",          1, _C89 },
909         { rts_exp2,       1, "exp2",         1, _C89 },
910         { rts_exp10,      1, "exp10",        1, _GNUC },
911         { rts_log,        1, "log",          1, _C89 },
912         { rts_log2,       1, "log2",         1, _C89 },
913         { rts_log10,      1, "log10",        1, _C89 },
914         { rts_pow,        1, "pow",          2, _C89 },
915         { rts_sin,        1, "sin",          1, _C89 },
916         { rts_cos,        1, "cos",          1, _C89 },
917         { rts_tan,        1, "tan",          1, _C89 },
918         { rts_asin,       1, "asin",         1, _C89 },
919         { rts_acos,       1, "acos",         1, _C89 },
920         { rts_atan,       1, "atan",         1, _C89 },
921         { rts_sinh,       1, "sinh",         1, _C89 },
922         { rts_cosh,       1, "cosh",         1, _C89 },
923         { rts_tanh,       1, "tanh",         1, _C89 },
924
925         { rts_fabsf,      1, "fabsf",        1, _C99 },
926         { rts_sqrtf,      1, "sqrtf",        1, _C99 },
927         { rts_cbrtf,      1, "cbrtf",        1, _C99 },
928         { rts_expf,       1, "expf",         1, _C99 },
929         { rts_exp2f,      1, "exp2f",        1, _C99 },
930         { rts_exp10f,     1, "exp10f",       1, _GNUC },
931         { rts_logf,       1, "logf",         1, _C99 },
932         { rts_log2f,      1, "log2f",        1, _C99 },
933         { rts_log10f,     1, "log10f",       1, _C99 },
934         { rts_powf,       1, "powf",         2, _C99 },
935         { rts_sinf,       1, "sinf",         1, _C99 },
936         { rts_cosf,       1, "cosf",         1, _C99 },
937         { rts_tanf,       1, "tanf",         1, _C99 },
938         { rts_asinf,      1, "asinf",        1, _C99 },
939         { rts_acosf,      1, "acosf",        1, _C99 },
940         { rts_atanf,      1, "atanf",        1, _C99 },
941         { rts_sinhf,      1, "sinhf",        1, _C99 },
942         { rts_coshf,      1, "coshf",        1, _C99 },
943         { rts_tanhf,      1, "tanhf",        1, _C99 },
944
945         { rts_fabsl,      1, "fabsl",        1, _C99 },
946         { rts_sqrtl,      1, "sqrtl",        1, _C99 },
947         { rts_cbrtl,      1, "cbrtl",        1, _C99 },
948         { rts_expl,       1, "expl",         1, _C99 },
949         { rts_exp2l,      1, "exp2l",        1, _C99 },
950         { rts_exp10l,     1, "exp10l",       1, _GNUC },
951         { rts_logl,       1, "logl",         1, _C99 },
952         { rts_log2l,      1, "log2l",        1, _C99 },
953         { rts_log10l,     1, "log10l",       1, _C99 },
954         { rts_powl,       1, "powl",         2, _C99 },
955         { rts_sinl,       1, "sinl",         1, _C99 },
956         { rts_cosl,       1, "cosl",         1, _C99 },
957         { rts_tanl,       1, "tanl",         1, _C99 },
958         { rts_asinl,      1, "asinl",        1, _C99 },
959         { rts_acosl,      1, "acosl",        1, _C99 },
960         { rts_atanl,      1, "atanl",        1, _C99 },
961         { rts_sinhl,      1, "sinhl",        1, _C99 },
962         { rts_coshl,      1, "coshl",        1, _C99 },
963         { rts_tanhl,      1, "tanhl",        1, _C99 },
964
965         { rts_memcpy,     1, "memcpy",       3, _C89 },  /* HMM, man say its C99 */
966         { rts_memset,     1, "memset",       3, _C89 },  /* HMM, man say its C99 */
967         { rts_strcmp,     1, "strcmp",       2, _C89 },
968         { rts_strncmp,    1, "strncmp",      3, _C89 }
969 };
970
971 static ir_entity* get_function_entity(declaration_t *declaration)
972 {
973         if(declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
974                 return declaration->v.entity;
975         assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
976
977         symbol_t *symbol = declaration->symbol;
978         ident    *id     = new_id_from_str(symbol->string);
979
980         ir_type  *global_type    = get_glob_type();
981         ir_type  *ir_type_method = get_ir_type(declaration->type);
982         assert(is_Method_type(ir_type_method));
983
984         dbg_info  *const dbgi   = get_dbg_info(&declaration->source_position);
985         ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
986         set_entity_ld_ident(entity, id);
987         if(declaration->storage_class == STORAGE_CLASS_STATIC
988                         || declaration->is_inline) {
989                 set_entity_visibility(entity, visibility_local);
990         } else if(declaration->init.statement != NULL) {
991                 set_entity_visibility(entity, visibility_external_visible);
992         } else {
993                 set_entity_visibility(entity, visibility_external_allocated);
994
995                 /* We should check for file scope here, but as long as we compile C only
996                    this is not needed. */
997                 int    n_params = get_method_n_params(ir_type_method);
998                 int    n_res    = get_method_n_ress(ir_type_method);
999                 int    i;
1000
1001                 if (n_params == 0 && n_res == 0 && id == predef_idents[rts_abort]) {
1002                         /* found abort(), store for later */
1003                         //abort_ent = ent;
1004                         //abort_tp  = ftype;
1005                 } else {
1006                         if (! firm_opt.freestanding) {
1007                                 /* check for a known runtime function */
1008                                 for (i = 0; i < rts_max; ++i) {
1009                                         /* ignore those rts functions not necessary needed for current mode */
1010                                         if ((c_mode & rts_data[i].flags) == 0)
1011                                                 continue;
1012                                         if (n_params == rts_data[i].n_params && n_res == rts_data[i].n_res &&
1013                                                 id == predef_idents[rts_data[i].id])
1014                                                 rts_entities[rts_data[i].id] = entity;
1015                                 }
1016                         }
1017                 }
1018         }
1019         set_entity_allocation(entity, allocation_static);
1020
1021         declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1022         declaration->v.entity         = entity;
1023
1024         return entity;
1025 }
1026
1027 static ir_node *const_to_firm(const const_expression_t *cnst)
1028 {
1029         dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1030         ir_mode  *mode = get_ir_mode(cnst->base.type);
1031
1032         char    buf[128];
1033         tarval *tv;
1034         size_t  len;
1035         if(mode_is_float(mode)) {
1036                 tv = new_tarval_from_double(cnst->v.float_value, mode);
1037         } else {
1038                 if(mode_is_signed(mode)) {
1039                         len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1040                 } else {
1041                         len = snprintf(buf, sizeof(buf), "%llu",
1042                                        (unsigned long long) cnst->v.int_value);
1043                 }
1044                 tv = new_tarval_from_str(buf, len, mode);
1045         }
1046
1047         return new_d_Const(dbgi, mode, tv);
1048 }
1049
1050 static ir_node *char_const_to_firm(const const_expression_t *cnst)
1051 {
1052         dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1053         ir_mode  *mode = get_ir_mode(cnst->base.type);
1054
1055         long long int v = 0;
1056         for (size_t i = 0; i < cnst->v.chars.size; ++i) {
1057                 v = (v << 8) | ((unsigned char)cnst->v.chars.begin[i]);
1058         }
1059         char    buf[128];
1060         size_t  len = snprintf(buf, sizeof(buf), "%lld", v);
1061         tarval *tv = new_tarval_from_str(buf, len, mode);
1062
1063         return new_d_Const(dbgi, mode, tv);
1064 }
1065
1066 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1067                                 ir_entity *entity)
1068 {
1069         assert(entity != NULL);
1070         union symconst_symbol sym;
1071         sym.entity_p = entity;
1072         return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1073 }
1074
1075 static ir_node *string_to_firm(const source_position_t *const src_pos,
1076                                const char *const id_prefix,
1077                                const string_t *const value)
1078 {
1079         ir_type  *const global_type = get_glob_type();
1080         dbg_info *const dbgi        = get_dbg_info(src_pos);
1081         ir_type  *const type        = new_d_type_array(unique_ident("strtype"), 1,
1082                                                        ir_type_const_char, dbgi);
1083
1084         ident     *const id     = unique_ident(id_prefix);
1085         ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1086         set_entity_ld_ident(entity, id);
1087         set_entity_variability(entity, variability_constant);
1088         set_entity_allocation(entity, allocation_static);
1089
1090         ir_type *const elem_type = ir_type_const_char;
1091         ir_mode *const mode      = get_type_mode(elem_type);
1092
1093         const char* const string = value->begin;
1094         const size_t      slen   = value->size;
1095
1096         set_array_lower_bound_int(type, 0, 0);
1097         set_array_upper_bound_int(type, 0, slen);
1098         set_type_size_bytes(type, slen);
1099         set_type_state(type, layout_fixed);
1100
1101         tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1102         for(size_t i = 0; i < slen; ++i) {
1103                 tvs[i] = new_tarval_from_long(string[i], mode);
1104         }
1105
1106         set_array_entity_values(entity, tvs, slen);
1107         free(tvs);
1108
1109         return create_symconst(dbgi, mode_P_data, entity);
1110 }
1111
1112 static ir_node *string_literal_to_firm(
1113                 const string_literal_expression_t* literal)
1114 {
1115         return string_to_firm(&literal->base.source_position, "Lstr",
1116                               &literal->value);
1117 }
1118
1119 static ir_node *wide_string_literal_to_firm(
1120         const wide_string_literal_expression_t* const literal)
1121 {
1122         ir_type *const global_type = get_glob_type();
1123         ir_type *const elem_type   = ir_type_wchar_t;
1124         dbg_info *const dbgi       = get_dbg_info(&literal->base.source_position);
1125         ir_type *const type        = new_d_type_array(unique_ident("strtype"), 1,
1126                                                     elem_type, dbgi);
1127
1128         ident     *const id     = unique_ident("Lstr");
1129         ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1130         set_entity_ld_ident(entity, id);
1131         set_entity_variability(entity, variability_constant);
1132         set_entity_allocation(entity, allocation_static);
1133
1134         ir_mode *const mode      = get_type_mode(elem_type);
1135
1136         const wchar_rep_t *const string = literal->value.begin;
1137         const size_t             slen   = literal->value.size;
1138
1139         set_array_lower_bound_int(type, 0, 0);
1140         set_array_upper_bound_int(type, 0, slen);
1141         set_type_size_bytes(type, slen);
1142         set_type_state(type, layout_fixed);
1143
1144         tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1145         for(size_t i = 0; i < slen; ++i) {
1146                 tvs[i] = new_tarval_from_long(string[i], mode);
1147         }
1148
1149         set_array_entity_values(entity, tvs, slen);
1150         free(tvs);
1151
1152         return create_symconst(dbgi, mode_P_data, entity);
1153 }
1154
1155 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
1156                               dbg_info *const dbgi)
1157 {
1158         if (is_compound_type(irtype) ||
1159                         is_Method_type(irtype)   ||
1160                         is_Array_type(irtype)) {
1161                 return addr;
1162         }
1163
1164         ir_mode *const mode     = get_type_mode(irtype);
1165         ir_node *const memory   = get_store();
1166         ir_node *const load     = new_d_Load(dbgi, memory, addr, mode);
1167         ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1168         ir_node *const load_res = new_d_Proj(dbgi, load, mode,   pn_Load_res);
1169         set_store(load_mem);
1170         return load_res;
1171 }
1172
1173 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1174 {
1175         ir_mode *mode = get_irn_mode(node);
1176
1177         if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1178                 return node;
1179         if(!mode_is_float(mode))
1180                 return node;
1181
1182         /* check if there is already a Conv */
1183         if (get_irn_op(node) == op_Conv) {
1184                 /* convert it into a strict Conv */
1185                 set_Conv_strict(node, 1);
1186                 return node;
1187         }
1188
1189         /* otherwise create a new one */
1190         return new_d_strictConv(dbgi, node, mode);
1191 }
1192
1193 static ir_node *get_global_var_address(dbg_info *const dbgi,
1194                                        const declaration_t *const decl)
1195 {
1196         assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1197
1198         ir_entity *const entity = decl->v.entity;
1199         switch ((storage_class_tag_t)decl->storage_class) {
1200                 case STORAGE_CLASS_THREAD:
1201                 case STORAGE_CLASS_THREAD_EXTERN:
1202                 case STORAGE_CLASS_THREAD_STATIC: {
1203                         ir_node *const no_mem = new_NoMem();
1204                         ir_node *const tls    = get_irg_tls(current_ir_graph);
1205                         return new_d_simpleSel(dbgi, no_mem, tls, entity);
1206                 }
1207
1208                 default:
1209                         return create_symconst(dbgi, mode_P_data, entity);
1210         }
1211 }
1212
1213 /* Returns the correct base address depending on whether it is a parameter or a
1214  * normal local variable */
1215 static ir_node *get_local_frame(ir_entity *const ent)
1216 {
1217         ir_graph      *const irg   = current_ir_graph;
1218         const ir_type *const owner = get_entity_owner(ent);
1219         if (owner == get_irg_frame_type(irg)) {
1220                 return get_irg_frame(irg);
1221         } else {
1222                 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1223                 return get_irg_value_param_base(irg);
1224         }
1225 }
1226
1227 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1228 {
1229         dbg_info      *dbgi        = get_dbg_info(&ref->base.source_position);
1230         declaration_t *declaration = ref->declaration;
1231         type_t        *type        = skip_typeref(declaration->type);
1232
1233         switch((declaration_kind_t) declaration->declaration_kind) {
1234         case DECLARATION_KIND_UNKNOWN:
1235                 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
1236                         break;
1237                 }
1238                 get_ir_type(type);
1239                 /* FALLTHROUGH */
1240
1241         case DECLARATION_KIND_ENUM_ENTRY: {
1242                 ir_mode *const mode = get_ir_mode(type);
1243                 return new_Const(mode, declaration->v.enum_val);
1244         }
1245
1246         case DECLARATION_KIND_LOCAL_VARIABLE: {
1247                 ir_mode *const mode = get_ir_mode(type);
1248                 return get_value(declaration->v.value_number, mode);
1249         }
1250         case DECLARATION_KIND_FUNCTION: {
1251                 ir_mode *const mode = get_ir_mode(type);
1252                 return create_symconst(dbgi, mode, declaration->v.entity);
1253         }
1254         case DECLARATION_KIND_GLOBAL_VARIABLE: {
1255                 ir_node *const addr   = get_global_var_address(dbgi, declaration);
1256                 ir_type *const irtype = get_entity_type(declaration->v.entity);
1257                 return deref_address(irtype, addr, dbgi);
1258         }
1259
1260         case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1261                 ir_entity *entity = declaration->v.entity;
1262                 ir_node   *frame  = get_local_frame(entity);
1263                 ir_node   *sel    = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1264                 ir_type   *irtype = get_entity_type(entity);
1265                 return deref_address(irtype, sel, dbgi);
1266         }
1267
1268         case DECLARATION_KIND_COMPOUND_MEMBER:
1269         case DECLARATION_KIND_LABEL_BLOCK:
1270                 panic("not implemented reference type");
1271         }
1272
1273         panic("reference to declaration with unknown type found");
1274 }
1275
1276 static ir_node *reference_addr(const reference_expression_t *ref)
1277 {
1278         dbg_info      *dbgi        = get_dbg_info(&ref->base.source_position);
1279         declaration_t *declaration = ref->declaration;
1280
1281         switch((declaration_kind_t) declaration->declaration_kind) {
1282         case DECLARATION_KIND_UNKNOWN:
1283                 break;
1284         case DECLARATION_KIND_LOCAL_VARIABLE:
1285                 panic("local variable without entity has no address");
1286         case DECLARATION_KIND_FUNCTION: {
1287                 type_t *const  type = skip_typeref(ref->base.type);
1288                 ir_mode *const mode = get_ir_mode(type);
1289                 return create_symconst(dbgi, mode, declaration->v.entity);
1290         }
1291         case DECLARATION_KIND_GLOBAL_VARIABLE: {
1292                 ir_node *const addr = get_global_var_address(dbgi, declaration);
1293                 return addr;
1294         }
1295         case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1296                 ir_entity *entity = declaration->v.entity;
1297                 ir_node   *frame  = get_local_frame(entity);
1298                 ir_node   *sel    = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1299
1300                 return sel;
1301         }
1302
1303         case DECLARATION_KIND_ENUM_ENTRY:
1304                 panic("trying to reference enum entry");
1305
1306         case DECLARATION_KIND_COMPOUND_MEMBER:
1307         case DECLARATION_KIND_LABEL_BLOCK:
1308                 panic("not implemented reference type");
1309         }
1310
1311         panic("reference to declaration with unknown type found");
1312 }
1313
1314 static ir_node *process_builtin_call(const call_expression_t *call)
1315 {
1316         dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1317
1318         assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1319         builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1320
1321         type_t *type = skip_typeref(builtin->base.type);
1322         assert(is_type_pointer(type));
1323
1324         type_t   *function_type = skip_typeref(type->pointer.points_to);
1325         symbol_t *symbol        = builtin->symbol;
1326
1327         switch(symbol->ID) {
1328         case T___builtin_alloca: {
1329                 if(call->arguments == NULL || call->arguments->next != NULL) {
1330                         panic("invalid number of parameters on __builtin_alloca");
1331                 }
1332                 expression_t *argument = call->arguments->expression;
1333                 ir_node      *size     = expression_to_firm(argument);
1334
1335                 ir_node *store  = get_store();
1336                 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1337                                               stack_alloc);
1338                 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1339                 set_store(proj_m);
1340                 ir_node *res    = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1341
1342                 return res;
1343         }
1344         case T___builtin_nan:
1345         case T___builtin_nanf:
1346         case T___builtin_nand: {
1347                 /* Ignore string for now... */
1348                 assert(is_type_function(function_type));
1349                 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1350                 tarval  *tv   = get_mode_NAN(mode);
1351                 ir_node *res  = new_d_Const(dbgi, mode, tv);
1352                 return res;
1353         }
1354         case T___builtin_va_end:
1355                 return NULL;
1356         default:
1357                 panic("Unsupported builtin found\n");
1358         }
1359 }
1360
1361 static ir_node *call_expression_to_firm(const call_expression_t *call)
1362 {
1363         assert(get_cur_block() != NULL);
1364
1365         expression_t *function = call->function;
1366         if(function->kind == EXPR_BUILTIN_SYMBOL) {
1367                 return process_builtin_call(call);
1368         }
1369         ir_node *callee = expression_to_firm(function);
1370
1371         type_t *type = skip_typeref(function->base.type);
1372         assert(is_type_pointer(type));
1373         pointer_type_t *pointer_type = &type->pointer;
1374         type_t         *points_to    = skip_typeref(pointer_type->points_to);
1375         assert(is_type_function(points_to));
1376         function_type_t *function_type = &points_to->function;
1377
1378         int              n_parameters = 0;
1379         call_argument_t *argument     = call->arguments;
1380         for( ; argument != NULL; argument = argument->next) {
1381                 ++n_parameters;
1382         }
1383
1384         dbg_info *dbgi  = get_dbg_info(&call->base.source_position);
1385
1386         ir_type *ir_method_type  = get_ir_type((type_t*) function_type);
1387         ir_type *new_method_type = NULL;
1388         if(function_type->variadic || function_type->unspecified_parameters) {
1389                 /* we need to construct a new method type matching the call
1390                  * arguments... */
1391                 int n_res       = get_method_n_ress(ir_method_type);
1392                 dbg_info *dbgi  = get_dbg_info(&call->base.source_position);
1393                 new_method_type = new_d_type_method(unique_ident("calltype"),
1394                                                   n_parameters, n_res, dbgi);
1395                 set_method_calling_convention(new_method_type,
1396                                get_method_calling_convention(ir_method_type));
1397                 set_method_additional_properties(new_method_type,
1398                                get_method_additional_properties(ir_method_type));
1399
1400                 for(int i = 0; i < n_res; ++i) {
1401                         set_method_res_type(new_method_type, i,
1402                                             get_method_res_type(ir_method_type, i));
1403                 }
1404         }
1405         ir_node *in[n_parameters];
1406
1407         argument = call->arguments;
1408         int n = 0;
1409         for( ; argument != NULL; argument = argument->next) {
1410                 expression_t *expression = argument->expression;
1411                 ir_node      *arg_node   = expression_to_firm(expression);
1412
1413                 arg_node = do_strict_conv(dbgi, arg_node);
1414
1415                 in[n] = arg_node;
1416                 if(new_method_type != NULL) {
1417                         ir_type *irtype = get_ir_type(expression->base.type);
1418                         set_method_param_type(new_method_type, n, irtype);
1419                 }
1420
1421                 n++;
1422         }
1423         assert(n == n_parameters);
1424
1425         if(new_method_type != NULL)
1426                 ir_method_type = new_method_type;
1427
1428         ir_node  *store = get_store();
1429         ir_node  *node  = new_d_Call(dbgi, store, callee, n_parameters, in,
1430                                      ir_method_type);
1431         ir_node  *mem   = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1432         set_store(mem);
1433
1434         type_t  *return_type = skip_typeref(function_type->return_type);
1435         ir_node *result      = NULL;
1436
1437         if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1438                 ir_mode *mode;
1439                 if(is_type_scalar(return_type)) {
1440                         mode = get_ir_mode(return_type);
1441                 } else {
1442                         mode = mode_P_data;
1443                 }
1444                 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1445                 result           = new_d_Proj(dbgi, resproj, mode, 0);
1446         }
1447
1448         return result;
1449 }
1450
1451 static void statement_to_firm(statement_t *statement);
1452 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1453
1454 static ir_node *expression_to_addr(const expression_t *expression);
1455 static void create_condition_evaluation(const expression_t *expression,
1456                                         ir_node *true_block,
1457                                         ir_node *false_block);
1458
1459 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1460                          ir_node *value)
1461 {
1462         value = do_strict_conv(dbgi, value);
1463
1464         ir_node *memory = get_store();
1465
1466         if(is_type_scalar(type)) {
1467                 ir_node  *store     = new_d_Store(dbgi, memory, addr, value);
1468                 ir_node  *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1469                 set_store(store_mem);
1470         } else {
1471                 ir_type *irtype    = get_ir_type(type);
1472                 ir_node *copyb     = new_d_CopyB(dbgi, memory, addr, value, irtype);
1473                 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1474                 set_store(copyb_mem);
1475         }
1476 }
1477
1478 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1479 {
1480         tarval *all_one   = get_mode_all_one(mode);
1481         int     mode_size = get_mode_size_bits(mode);
1482
1483         assert(offset >= 0 && size >= 0);
1484         assert(offset + size <= mode_size);
1485         if(size == mode_size) {
1486                 return all_one;
1487         }
1488
1489         long    shiftr    = get_mode_size_bits(mode) - size;
1490         long    shiftl    = offset;
1491         tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1492         tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1493         tarval *mask0     = tarval_shr(all_one, tv_shiftr);
1494         tarval *mask1     = tarval_shl(mask0, tv_shiftl);
1495
1496         return mask1;
1497 }
1498
1499 static void bitfield_store_to_firm(const unary_expression_t *expression,
1500                                    ir_node *value)
1501 {
1502         expression_t *select = expression->value;
1503         assert(select->kind == EXPR_SELECT);
1504         type_t       *type   = select->base.type;
1505         assert(type->kind == TYPE_BITFIELD);
1506         ir_mode      *mode   = get_ir_mode(type->bitfield.base);
1507         ir_node      *addr   = expression_to_addr(select);
1508
1509         assert(get_irn_mode(value) == mode);
1510
1511         dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1512
1513         /* kill upper bits of value and shift to right position */
1514         ir_entity *entity       = select->select.compound_entry->v.entity;
1515         int        bitoffset    = get_entity_offset_bits_remainder(entity);
1516         ir_type   *entity_type  = get_entity_type(entity);
1517         int        bitsize      = get_mode_size_bits(get_type_mode(entity_type));
1518
1519         tarval  *mask            = create_bitfield_mask(mode, 0, bitsize);
1520         ir_node *mask_node       = new_d_Const(dbgi, mode, mask);
1521         ir_node *value_masked    = new_d_And(dbgi, value, mask_node, mode);
1522         tarval  *shiftl          = new_tarval_from_long(bitoffset, mode_uint);
1523         ir_node *shiftcount      = new_d_Const(dbgi, mode_uint, shiftl);
1524         ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1525
1526         /* load current value */
1527         ir_node  *mem             = get_store();
1528         ir_node  *load            = new_d_Load(dbgi, mem, addr, mode);
1529         ir_node  *load_mem        = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1530         ir_node  *load_res        = new_d_Proj(dbgi, load, mode, pn_Load_res);
1531         tarval   *shift_mask      = create_bitfield_mask(mode, bitoffset, bitsize);
1532         tarval   *inv_mask        = tarval_not(shift_mask);
1533         ir_node  *inv_mask_node   = new_d_Const(dbgi, mode, inv_mask);
1534         ir_node  *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1535
1536         /* construct new value and store */
1537         ir_node *new_val   = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1538         ir_node *store     = new_d_Store(dbgi, load_mem, addr, new_val);
1539         ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1540         set_store(store_mem);
1541 }
1542
1543 static void set_value_for_expression(const expression_t *expression,
1544                                      ir_node *value)
1545 {
1546         dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1547         value          = do_strict_conv(dbgi, value);
1548
1549         if(expression->kind == EXPR_REFERENCE) {
1550                 const reference_expression_t *ref = &expression->reference;
1551
1552                 declaration_t *declaration = ref->declaration;
1553                 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1554                 if(declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1555                         set_value(declaration->v.value_number, value);
1556                         return;
1557                 }
1558         }
1559
1560         if(expression->kind == EXPR_UNARY_BITFIELD_EXTRACT) {
1561                 bitfield_store_to_firm(&expression->unary, value);
1562                 return;
1563         }
1564
1565         ir_node *addr = expression_to_addr(expression);
1566         type_t  *type = skip_typeref(expression->base.type);
1567         assign_value(dbgi, addr, type, value);
1568 }
1569
1570 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1571 {
1572         ir_mode *value_mode = get_irn_mode(value);
1573
1574         if (value_mode == dest_mode || is_Bad(value))
1575                 return value;
1576
1577         if(dest_mode == mode_b) {
1578                 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1579                 ir_node *cmp  = new_d_Cmp(dbgi, value, zero);
1580                 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1581                 return proj;
1582         }
1583
1584         return new_d_Conv(dbgi, value, dest_mode);
1585 }
1586
1587 static ir_node *create_incdec(const unary_expression_t *expression)
1588 {
1589         dbg_info     *dbgi  = get_dbg_info(&expression->base.source_position);
1590         type_t       *type  = skip_typeref(expression->base.type);
1591         ir_mode      *mode  = get_ir_mode(type);
1592         expression_t *value = expression->value;
1593
1594         ir_node *value_node = expression_to_firm(value);
1595
1596         ir_node *offset;
1597         if(is_type_pointer(type)) {
1598                 pointer_type_t *pointer_type = &type->pointer;
1599                 unsigned        elem_size    = get_type_size(pointer_type->points_to);
1600                 offset = new_Const_long(mode_int, elem_size);
1601         } else {
1602                 assert(is_type_arithmetic(type));
1603                 offset = new_Const(mode, get_mode_one(mode));
1604         }
1605
1606         switch(expression->base.kind) {
1607         case EXPR_UNARY_POSTFIX_INCREMENT: {
1608                 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1609                 set_value_for_expression(value, new_value);
1610                 return value_node;
1611         }
1612         case EXPR_UNARY_POSTFIX_DECREMENT: {
1613                 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1614                 set_value_for_expression(value, new_value);
1615                 return value_node;
1616         }
1617         case EXPR_UNARY_PREFIX_INCREMENT: {
1618                 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1619                 set_value_for_expression(value, new_value);
1620                 return new_value;
1621         }
1622         case EXPR_UNARY_PREFIX_DECREMENT: {
1623                 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1624                 set_value_for_expression(value, new_value);
1625                 return new_value;
1626         }
1627         default:
1628                 panic("no incdec expr in create_incdec");
1629                 return NULL;
1630         }
1631 }
1632
1633 static bool is_local_variable(expression_t *expression)
1634 {
1635         if (expression->kind != EXPR_REFERENCE)
1636                 return false;
1637         reference_expression_t *ref_expr    = &expression->reference;
1638         declaration_t          *declaration = ref_expr->declaration;
1639         return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1640 }
1641
1642 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1643 {
1644         switch(kind) {
1645         case EXPR_BINARY_EQUAL:         return pn_Cmp_Eq;
1646         case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1647         case EXPR_BINARY_NOTEQUAL:
1648                 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1649         case EXPR_BINARY_ISLESS:
1650         case EXPR_BINARY_LESS:          return pn_Cmp_Lt;
1651         case EXPR_BINARY_ISLESSEQUAL:
1652         case EXPR_BINARY_LESSEQUAL:     return pn_Cmp_Le;
1653         case EXPR_BINARY_ISGREATER:
1654         case EXPR_BINARY_GREATER:       return pn_Cmp_Gt;
1655         case EXPR_BINARY_ISGREATEREQUAL:
1656         case EXPR_BINARY_GREATEREQUAL:  return pn_Cmp_Ge;
1657         case EXPR_BINARY_ISUNORDERED:   return pn_Cmp_Uo;
1658
1659         default:
1660                 break;
1661         }
1662         panic("trying to get pn_Cmp from non-comparison binexpr type");
1663 }
1664
1665 /**
1666  * Handle the assume optimizer hint: check if a Confirm
1667  * node can be created.
1668  *
1669  * @param dbi    debug info
1670  * @param expr   the IL assume expression
1671  *
1672  * we support here only some simple cases:
1673  *  - var rel const
1674  *  - const rel val
1675  *  - var rel var
1676  */
1677 static ir_node *handle_assume_compare(dbg_info *dbi,
1678                                       const binary_expression_t *expression)
1679 {
1680         expression_t  *op1 = expression->left;
1681         expression_t  *op2 = expression->right;
1682         declaration_t *var2, *var = NULL;
1683         ir_node       *res = NULL;
1684         pn_Cmp         cmp_val;
1685
1686         cmp_val = get_pnc(expression->base.kind, op1->base.type);
1687
1688         if (is_local_variable(op1) && is_local_variable(op2)) {
1689         var  = op1->reference.declaration;
1690             var2 = op2->reference.declaration;
1691
1692                 type_t  *const type = skip_typeref(var->type);
1693                 ir_mode *const mode = get_ir_mode(type);
1694
1695                 ir_node *const irn1 = get_value(var->v.value_number, mode);
1696                 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1697
1698                 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1699                 set_value(var2->v.value_number, res);
1700
1701                 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1702                 set_value(var->v.value_number, res);
1703
1704                 return res;
1705         }
1706
1707         expression_t *con;
1708         if (is_local_variable(op1) && is_constant_expression(op2)) {
1709                 var = op1->reference.declaration;
1710                 con = op2;
1711         } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1712                 cmp_val = get_inversed_pnc(cmp_val);
1713                 var = op2->reference.declaration;
1714                 con = op1;
1715         }
1716
1717         if (var != NULL) {
1718                 type_t  *const type = skip_typeref(var->type);
1719                 ir_mode *const mode = get_ir_mode(type);
1720
1721                 res = get_value(var->v.value_number, mode);
1722                 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1723                 set_value(var->v.value_number, res);
1724         }
1725         return res;
1726 }
1727
1728 /**
1729  * Handle the assume optimizer hint.
1730  *
1731  * @param dbi    debug info
1732  * @param expr   the IL assume expression
1733  */
1734 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1735         switch(expression->kind) {
1736         case EXPR_BINARY_EQUAL:
1737         case EXPR_BINARY_NOTEQUAL:
1738         case EXPR_BINARY_LESS:
1739         case EXPR_BINARY_LESSEQUAL:
1740         case EXPR_BINARY_GREATER:
1741         case EXPR_BINARY_GREATEREQUAL:
1742                 return handle_assume_compare(dbi, &expression->binary);
1743         default:
1744                 return NULL;
1745         }
1746 }
1747
1748 static ir_node *bitfield_extract_to_firm(const unary_expression_t *expression)
1749 {
1750         expression_t *select = expression->value;
1751         assert(select->kind == EXPR_SELECT);
1752
1753         type_t   *type     = select->base.type;
1754         assert(type->kind == TYPE_BITFIELD);
1755         ir_mode  *mode     = get_ir_mode(type->bitfield.base);
1756         dbg_info *dbgi     = get_dbg_info(&expression->base.source_position);
1757         ir_node  *addr     = expression_to_addr(select);
1758         ir_node  *mem      = get_store();
1759         ir_node  *load     = new_d_Load(dbgi, mem, addr, mode);
1760         ir_node  *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1761         ir_node  *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1762
1763         load_res           = create_conv(dbgi, load_res, mode_int);
1764
1765         set_store(load_mem);
1766
1767         /* kill upper bits */
1768         ir_entity *entity       = select->select.compound_entry->v.entity;
1769         int        bitoffset    = get_entity_offset_bits_remainder(entity);
1770         ir_type   *entity_type  = get_entity_type(entity);
1771         int        bitsize      = get_mode_size_bits(get_type_mode(entity_type));
1772         long       shift_bitsl  = machine_size - bitoffset - bitsize;
1773         assert(shift_bitsl >= 0);
1774         tarval    *tvl          = new_tarval_from_long(shift_bitsl, mode_uint);
1775         ir_node   *countl       = new_d_Const(dbgi, mode_uint, tvl);
1776         ir_node   *shiftl       = new_d_Shl(dbgi, load_res, countl, mode_int);
1777
1778         long       shift_bitsr  = bitoffset + shift_bitsl;
1779         assert(shift_bitsr <= (long) machine_size);
1780         tarval    *tvr          = new_tarval_from_long(shift_bitsr, mode_uint);
1781         ir_node   *countr       = new_d_Const(dbgi, mode_uint, tvr);
1782         ir_node   *shiftr;
1783         if(mode_is_signed(mode)) {
1784                 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1785         } else {
1786                 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1787         }
1788
1789         return create_conv(dbgi, shiftr, mode);
1790 }
1791
1792 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1793 {
1794         dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1795         type_t   *type = skip_typeref(expression->base.type);
1796
1797         if(expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
1798                 return expression_to_addr(expression->value);
1799
1800         const expression_t *value = expression->value;
1801
1802         switch(expression->base.kind) {
1803         case EXPR_UNARY_NEGATE: {
1804                 ir_node *value_node = expression_to_firm(value);
1805                 ir_mode *mode = get_ir_mode(type);
1806                 return new_d_Minus(dbgi, value_node, mode);
1807         }
1808         case EXPR_UNARY_PLUS:
1809                 return expression_to_firm(value);
1810         case EXPR_UNARY_BITWISE_NEGATE: {
1811                 ir_node *value_node = expression_to_firm(value);
1812                 ir_mode *mode = get_ir_mode(type);
1813                 return new_d_Not(dbgi, value_node, mode);
1814         }
1815         case EXPR_UNARY_NOT: {
1816                 ir_node *value_node = expression_to_firm(value);
1817                 ir_mode *mode = get_ir_mode(type);
1818                 if(get_irn_mode(value_node) != mode_b) {
1819                         value_node = create_conv(dbgi, value_node, mode_b);
1820                 }
1821                 value_node = new_d_Not(dbgi, value_node, mode_b);
1822                 if(mode != mode_b) {
1823                         value_node = create_conv(dbgi, value_node, mode);
1824                 }
1825                 return value_node;
1826         }
1827         case EXPR_UNARY_DEREFERENCE: {
1828                 ir_node *value_node = expression_to_firm(value);
1829                 type_t  *value_type = skip_typeref(value->base.type);
1830                 ir_type *irtype     = get_ir_type(value_type);
1831                 assert(is_Pointer_type(irtype));
1832                 ir_type *points_to  = get_pointer_points_to_type(irtype);
1833                 return deref_address(points_to, value_node, dbgi);
1834         }
1835         case EXPR_UNARY_POSTFIX_INCREMENT:
1836         case EXPR_UNARY_POSTFIX_DECREMENT:
1837         case EXPR_UNARY_PREFIX_INCREMENT:
1838         case EXPR_UNARY_PREFIX_DECREMENT:
1839                 return create_incdec(expression);
1840         case EXPR_UNARY_CAST: {
1841                 ir_node *value_node = expression_to_firm(value);
1842                 if(is_type_scalar(type)) {
1843                         ir_mode *mode = get_ir_mode(type);
1844                         ir_node *node = create_conv(dbgi, value_node, mode);
1845                         node = do_strict_conv(dbgi, node);
1846                         return node;
1847                 } else {
1848                         return value_node;
1849                 }
1850         }
1851         case EXPR_UNARY_CAST_IMPLICIT: {
1852                 ir_node *value_node = expression_to_firm(value);
1853                 if(is_type_scalar(type)) {
1854                         ir_mode *mode = get_ir_mode(type);
1855                         return create_conv(dbgi, value_node, mode);
1856                 } else {
1857                         return value_node;
1858                 }
1859         }
1860         case EXPR_UNARY_ASSUME:
1861                 if(firm_opt.confirm)
1862                         return handle_assume(dbgi, value);
1863                 else
1864                         return NULL;
1865         case EXPR_UNARY_BITFIELD_EXTRACT:
1866                 return bitfield_extract_to_firm(expression);
1867
1868         default:
1869                 break;
1870         }
1871         panic("invalid UNEXPR type found");
1872 }
1873
1874 static ir_node *create_lazy_op(const binary_expression_t *expression)
1875 {
1876         dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1877         type_t   *type = expression->base.type;
1878         ir_mode  *mode = get_ir_mode(type);
1879
1880         if(is_constant_expression(expression->left)) {
1881                 long val = fold_constant(expression->left);
1882                 expression_kind_t ekind = expression->base.kind;
1883                 if((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
1884                                 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1885                         return expression_to_firm(expression->right);
1886                 } else {
1887                         assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
1888                                         || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
1889                         return new_Const(mode, get_mode_one(mode));
1890                 }
1891         }
1892
1893         ir_node *cur_block = get_cur_block();
1894
1895         ir_node *one_block = new_immBlock();
1896         ir_node *one       = new_Const(mode, get_mode_one(mode));
1897         ir_node *jmp_one   = new_d_Jmp(dbgi);
1898
1899         ir_node *zero_block = new_immBlock();
1900         ir_node *zero       = new_Const(mode, get_mode_null(mode));
1901         ir_node *jmp_zero   = new_d_Jmp(dbgi);
1902
1903         set_cur_block(cur_block);
1904         create_condition_evaluation((const expression_t*) expression,
1905                                     one_block, zero_block);
1906         mature_immBlock(one_block);
1907         mature_immBlock(zero_block);
1908
1909         ir_node *common_block = new_immBlock();
1910         add_immBlock_pred(common_block, jmp_one);
1911         add_immBlock_pred(common_block, jmp_zero);
1912         mature_immBlock(common_block);
1913
1914         ir_node *in[2] = { one, zero };
1915         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
1916
1917         return val;
1918 }
1919
1920 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1921                                             ir_node *right, ir_mode *mode);
1922
1923 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1924                                         create_arithmetic_func func)
1925 {
1926         dbg_info *dbgi  = get_dbg_info(&expression->base.source_position);
1927         ir_node  *left  = expression_to_firm(expression->left);
1928         ir_node  *right = expression_to_firm(expression->right);
1929         type_t   *type  = expression->right->base.type;
1930         /* be careful with the modes, because in arithmetic assign nodes only
1931          * the right operand has the mode of the arithmetic already */
1932         ir_mode  *mode  = get_ir_mode(type);
1933         left            = create_conv(dbgi, left, mode);
1934         ir_node  *res   = func(dbgi, left, right, mode);
1935
1936         return res;
1937 }
1938
1939 static ir_node *pointer_arithmetic(ir_node  *const pointer,
1940                                    ir_node  *      integer,
1941                                    type_t   *const type,
1942                                    dbg_info *const dbgi,
1943                                    const create_arithmetic_func func)
1944 {
1945         pointer_type_t *const pointer_type = &type->pointer;
1946         type_t         *const points_to    = pointer_type->points_to;
1947         const unsigned        elem_size    = get_type_size(points_to);
1948
1949         assert(elem_size >= 1);
1950         if (elem_size > 1) {
1951                 integer             = create_conv(dbgi, integer, mode_int);
1952                 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1953                 ir_node *const mul  = new_d_Mul(dbgi, integer, cnst, mode_int);
1954                 integer = mul;
1955         }
1956
1957         ir_mode *const mode = get_ir_mode(type);
1958         return func(dbgi, pointer, integer, mode);
1959 }
1960
1961 static ir_node *create_arithmetic_assign_binop(
1962                 const binary_expression_t *expression, create_arithmetic_func func)
1963 {
1964         dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
1965         type_t   *const type = skip_typeref(expression->base.type);
1966         ir_node  *value;
1967
1968         if (is_type_pointer(type)) {
1969                 ir_node *const pointer = expression_to_firm(expression->left);
1970                 ir_node *      integer = expression_to_firm(expression->right);
1971                 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1972         } else {
1973                 value = create_arithmetic_binop(expression, func);
1974         }
1975
1976         ir_mode *const mode = get_ir_mode(type);
1977         value = create_conv(dbgi, value, mode);
1978         set_value_for_expression(expression->left, value);
1979
1980         return value;
1981 }
1982
1983 static ir_node *create_add(const binary_expression_t *expression)
1984 {
1985         dbg_info *dbgi  = get_dbg_info(&expression->base.source_position);
1986         ir_node  *left  = expression_to_firm(expression->left);
1987         ir_node  *right = expression_to_firm(expression->right);
1988         type_t   *type  = expression->base.type;
1989
1990         expression_t *expr_left  = expression->left;
1991         expression_t *expr_right = expression->right;
1992         type_t       *type_left  = skip_typeref(expr_left->base.type);
1993         type_t       *type_right = skip_typeref(expr_right->base.type);
1994
1995         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1996                 ir_mode *const mode = get_ir_mode(type);
1997                 return new_d_Add(dbgi, left, right, mode);
1998         }
1999
2000         if (is_type_pointer(type_left)) {
2001                 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
2002         } else {
2003                 assert(is_type_pointer(type_right));
2004                 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
2005         }
2006 }
2007
2008 static ir_node *create_sub(const binary_expression_t *expression)
2009 {
2010         dbg_info *const dbgi  = get_dbg_info(&expression->base.source_position);
2011         expression_t *const expr_left  = expression->left;
2012         expression_t *const expr_right = expression->right;
2013         ir_node      *const left       = expression_to_firm(expr_left);
2014         ir_node      *const right      = expression_to_firm(expr_right);
2015         type_t       *const type       = expression->base.type;
2016         type_t       *const type_left  = skip_typeref(expr_left->base.type);
2017         type_t       *const type_right = skip_typeref(expr_right->base.type);
2018
2019         if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2020                 ir_mode *const mode = get_ir_mode(type);
2021                 return new_d_Sub(dbgi, left, right, mode);
2022         } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2023                 const pointer_type_t *const ptr_type = &type_left->pointer;
2024                 const unsigned elem_size             = get_type_size(ptr_type->points_to);
2025                 ir_mode *const mode   = get_ir_mode(type);
2026                 ir_node *const sub    = new_d_Sub(dbgi, left, right, mode);
2027                 ir_node *const cnst   = new_Const_long(mode_int, (long)elem_size);
2028                 ir_node *const no_mem = new_NoMem();
2029                 ir_node *const div    = new_d_Div(dbgi, no_mem, sub, cnst, mode,
2030                                                   op_pin_state_floats);
2031                 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2032         }
2033
2034         assert(is_type_pointer(type_left));
2035         return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
2036 }
2037
2038 static ir_node *create_shift(const binary_expression_t *expression)
2039 {
2040         dbg_info *dbgi  = get_dbg_info(&expression->base.source_position);
2041         ir_node  *left  = expression_to_firm(expression->left);
2042         ir_node  *right = expression_to_firm(expression->right);
2043         type_t   *type  = expression->base.type;
2044         ir_mode  *mode  = get_ir_mode(type);
2045
2046         /* firm always wants the shift count to be unsigned */
2047         right = create_conv(dbgi, right, mode_uint);
2048
2049         ir_node *res;
2050
2051         switch(expression->base.kind) {
2052         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2053         case EXPR_BINARY_SHIFTLEFT:
2054                 res = new_d_Shl(dbgi, left, right, mode);
2055                 break;
2056         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2057         case EXPR_BINARY_SHIFTRIGHT: {
2058                  expression_t *expr_left = expression->left;
2059                  type_t       *type_left = skip_typeref(expr_left->base.type);
2060
2061                  if(is_type_signed(type_left)) {
2062                         res = new_d_Shrs(dbgi, left, right, mode);
2063                  } else {
2064                          res = new_d_Shr(dbgi, left, right, mode);
2065                  }
2066                  break;
2067         }
2068         default:
2069                 panic("create shift op called for non-shift op");
2070         }
2071
2072         return res;
2073 }
2074
2075
2076 static ir_node *create_divmod(const binary_expression_t *expression)
2077 {
2078         dbg_info *dbgi  = get_dbg_info(&expression->base.source_position);
2079         ir_node  *left  = expression_to_firm(expression->left);
2080         ir_node  *right = expression_to_firm(expression->right);
2081         ir_node  *pin   = new_Pin(new_NoMem());
2082         /* be careful with the modes, because in arithmetic assign nodes only
2083          * the right operand has the mode of the arithmetic already */
2084         type_t   *type  = expression->right->base.type;
2085         ir_mode  *mode  = get_ir_mode(type);
2086         left            = create_conv(dbgi, left, mode);
2087         ir_node  *op;
2088         ir_node  *res;
2089
2090         switch (expression->base.kind) {
2091         case EXPR_BINARY_DIV:
2092         case EXPR_BINARY_DIV_ASSIGN:
2093                 if(mode_is_float(mode)) {
2094                         op  = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2095                         res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2096                 } else {
2097                         op  = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2098                         res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2099                 }
2100                 break;
2101
2102         case EXPR_BINARY_MOD:
2103         case EXPR_BINARY_MOD_ASSIGN:
2104                 assert(!mode_is_float(mode));
2105                 op  = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
2106                 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2107                 break;
2108
2109         default: panic("unexpected binary expression type in create_divmod()");
2110         }
2111
2112         return res;
2113 }
2114
2115 static ir_node *create_arithmetic_assign_divmod(
2116                 const binary_expression_t *expression)
2117 {
2118         ir_node  *      value = create_divmod(expression);
2119         dbg_info *const dbgi  = get_dbg_info(&expression->base.source_position);
2120         type_t   *const type  = expression->base.type;
2121         ir_mode  *const mode  = get_ir_mode(type);
2122
2123         assert(type->kind != TYPE_POINTER);
2124
2125         value = create_conv(dbgi, value, mode);
2126         set_value_for_expression(expression->left, value);
2127
2128         return value;
2129 }
2130
2131 static ir_node *create_arithmetic_assign_shift(
2132                 const binary_expression_t *expression)
2133 {
2134         ir_node  *      value = create_shift(expression);
2135         dbg_info *const dbgi  = get_dbg_info(&expression->base.source_position);
2136         type_t   *const type  = expression->base.type;
2137         ir_mode  *const mode  = get_ir_mode(type);
2138
2139         value = create_conv(dbgi, value, mode);
2140         set_value_for_expression(expression->left, value);
2141
2142         return value;
2143 }
2144
2145 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2146 {
2147         expression_kind_t kind = expression->base.kind;
2148
2149         switch(kind) {
2150         case EXPR_BINARY_EQUAL:
2151         case EXPR_BINARY_NOTEQUAL:
2152         case EXPR_BINARY_LESS:
2153         case EXPR_BINARY_LESSEQUAL:
2154         case EXPR_BINARY_GREATER:
2155         case EXPR_BINARY_GREATEREQUAL:
2156         case EXPR_BINARY_ISGREATER:
2157         case EXPR_BINARY_ISGREATEREQUAL:
2158         case EXPR_BINARY_ISLESS:
2159         case EXPR_BINARY_ISLESSEQUAL:
2160         case EXPR_BINARY_ISLESSGREATER:
2161         case EXPR_BINARY_ISUNORDERED: {
2162                 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2163                 ir_node *left  = expression_to_firm(expression->left);
2164                 ir_node *right = expression_to_firm(expression->right);
2165                 ir_node *cmp   = new_d_Cmp(dbgi, left, right);
2166                 long     pnc   = get_pnc(kind, expression->left->base.type);
2167                 ir_node *proj  = new_d_Proj(dbgi, cmp, mode_b, pnc);
2168                 return proj;
2169         }
2170         case EXPR_BINARY_ASSIGN: {
2171                 ir_node *right = expression_to_firm(expression->right);
2172                 set_value_for_expression(expression->left, right);
2173
2174                 return right;
2175         }
2176         case EXPR_BINARY_ADD:
2177                 return create_add(expression);
2178         case EXPR_BINARY_SUB:
2179                 return create_sub(expression);
2180         case EXPR_BINARY_MUL:
2181                 return create_arithmetic_binop(expression, new_d_Mul);
2182         case EXPR_BINARY_BITWISE_AND:
2183                 return create_arithmetic_binop(expression, new_d_And);
2184         case EXPR_BINARY_BITWISE_OR:
2185                 return create_arithmetic_binop(expression, new_d_Or);
2186         case EXPR_BINARY_BITWISE_XOR:
2187                 return create_arithmetic_binop(expression, new_d_Eor);
2188         case EXPR_BINARY_SHIFTLEFT:
2189         case EXPR_BINARY_SHIFTRIGHT:
2190                 return create_shift(expression);
2191         case EXPR_BINARY_DIV:
2192         case EXPR_BINARY_MOD:
2193                 return create_divmod(expression);
2194         case EXPR_BINARY_LOGICAL_AND:
2195         case EXPR_BINARY_LOGICAL_OR:
2196                 return create_lazy_op(expression);
2197         case EXPR_BINARY_COMMA:
2198                 expression_to_firm(expression->left);
2199                 return expression_to_firm(expression->right);
2200         case EXPR_BINARY_ADD_ASSIGN:
2201                 return create_arithmetic_assign_binop(expression, new_d_Add);
2202         case EXPR_BINARY_SUB_ASSIGN:
2203                 return create_arithmetic_assign_binop(expression, new_d_Sub);
2204         case EXPR_BINARY_MUL_ASSIGN:
2205                 return create_arithmetic_assign_binop(expression, new_d_Mul);
2206         case EXPR_BINARY_MOD_ASSIGN:
2207         case EXPR_BINARY_DIV_ASSIGN:
2208                 return create_arithmetic_assign_divmod(expression);
2209         case EXPR_BINARY_BITWISE_AND_ASSIGN:
2210                 return create_arithmetic_assign_binop(expression, new_d_And);
2211         case EXPR_BINARY_BITWISE_OR_ASSIGN:
2212                 return create_arithmetic_assign_binop(expression, new_d_Or);
2213         case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2214                 return create_arithmetic_assign_binop(expression, new_d_Eor);
2215         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2216         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2217                 return create_arithmetic_assign_shift(expression);
2218         case EXPR_BINARY_BUILTIN_EXPECT:
2219                 return expression_to_firm(expression->left);
2220         default:
2221                 panic("TODO binexpr type");
2222         }
2223 }
2224
2225 static ir_node *array_access_addr(const array_access_expression_t *expression)
2226 {
2227         dbg_info *dbgi      = get_dbg_info(&expression->base.source_position);
2228         ir_node  *base_addr = expression_to_firm(expression->array_ref);
2229         ir_node  *offset    = expression_to_firm(expression->index);
2230         offset              = create_conv(dbgi, offset, mode_uint);
2231
2232         type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2233         assert(is_type_pointer(ref_type));
2234         pointer_type_t *pointer_type = &ref_type->pointer;
2235
2236         unsigned elem_size       = get_type_size(pointer_type->points_to);
2237         ir_node *elem_size_const = new_Const_long(mode_uint, elem_size);
2238         ir_node *real_offset     = new_d_Mul(dbgi, offset, elem_size_const,
2239                                              mode_uint);
2240         ir_node *result          = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2241
2242         return result;
2243 }
2244
2245 static ir_node *array_access_to_firm(
2246                 const array_access_expression_t *expression)
2247 {
2248         dbg_info *dbgi   = get_dbg_info(&expression->base.source_position);
2249         ir_node  *addr   = array_access_addr(expression);
2250         type_t   *type   = revert_automatic_type_conversion(
2251                         (const expression_t*) expression);
2252         type             = skip_typeref(type);
2253         ir_type  *irtype = get_ir_type(type);
2254
2255         return deref_address(irtype, addr, dbgi);
2256 }
2257
2258 static long get_offsetof_offset(const offsetof_expression_t *expression)
2259 {
2260         type_t *orig_type = expression->type;
2261         long    offset    = 0;
2262
2263         designator_t *designator = expression->designator;
2264         for( ; designator != NULL; designator = designator->next) {
2265                 type_t *type = skip_typeref(orig_type);
2266                 /* be sure the type is constructed */
2267                 (void) get_ir_type(type);
2268
2269                 if(designator->symbol != NULL) {
2270                         assert(is_type_compound(type));
2271                         symbol_t *symbol = designator->symbol;
2272
2273                         declaration_t *declaration = type->compound.declaration;
2274                         declaration_t *iter        = declaration->scope.declarations;
2275                         for( ; iter != NULL; iter = iter->next) {
2276                                 if(iter->symbol == symbol) {
2277                                         break;
2278                                 }
2279                         }
2280                         assert(iter != NULL);
2281
2282                         assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2283                         offset += get_entity_offset(iter->v.entity);
2284
2285                         orig_type = iter->type;
2286                 } else {
2287                         expression_t *array_index = designator->array_index;
2288                         assert(designator->array_index != NULL);
2289                         assert(is_type_array(type));
2290                         assert(is_type_valid(array_index->base.type));
2291
2292                         long index         = fold_constant(array_index);
2293                         ir_type *arr_type  = get_ir_type(type);
2294                         ir_type *elem_type = get_array_element_type(arr_type);
2295                         long     elem_size = get_type_size_bytes(elem_type);
2296
2297                         offset += index * elem_size;
2298
2299                         orig_type = type->array.element_type;
2300                 }
2301         }
2302
2303         return offset;
2304 }
2305
2306 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2307 {
2308         ir_mode  *mode   = get_ir_mode(expression->base.type);
2309         long      offset = get_offsetof_offset(expression);
2310         tarval   *tv     = new_tarval_from_long(offset, mode);
2311         dbg_info *dbgi   = get_dbg_info(&expression->base.source_position);
2312
2313         return new_d_Const(dbgi, mode, tv);
2314 }
2315
2316 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2317                                      ir_entity *entity, type_t *type);
2318
2319 static ir_node *compound_literal_to_firm(
2320                 const compound_literal_expression_t *expression)
2321 {
2322         type_t *type = expression->type;
2323
2324         /* create an entity on the stack */
2325         ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2326
2327         ident     *const id     = unique_ident("CompLit");
2328         ir_type   *const irtype = get_ir_type(type);
2329         dbg_info  *const dbgi   = get_dbg_info(&expression->base.source_position);
2330         ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2331         set_entity_ld_ident(entity, id);
2332
2333         set_entity_variability(entity, variability_uninitialized);
2334
2335         /* create initialisation code */
2336         initializer_t *initializer = expression->initializer;
2337         create_local_initializer(initializer, dbgi, entity, type);
2338
2339         /* create a sel for the compound literal address */
2340         ir_node *frame = get_local_frame(entity);
2341         ir_node *sel   = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2342         return sel;
2343 }
2344
2345 /**
2346  * Transform a sizeof expression into Firm code.
2347  */
2348 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2349 {
2350         type_t *type = expression->type;
2351         if(type == NULL) {
2352                 type = expression->tp_expression->base.type;
2353                 assert(type != NULL);
2354         }
2355
2356         ir_mode *const mode = get_ir_mode(expression->base.type);
2357         symconst_symbol sym;
2358         sym.type_p = get_ir_type(type);
2359         return new_SymConst(mode, sym, symconst_type_size);
2360 }
2361
2362 /**
2363  * Transform an alignof expression into Firm code.
2364  */
2365 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2366 {
2367         type_t *type = expression->type;
2368         if(type == NULL) {
2369                 /* beware: if expression is a variable reference, return the
2370                    alignment of the variable. */
2371                 const expression_t *tp_expression = expression->tp_expression;
2372                 const declaration_t *declaration = expr_is_variable(tp_expression);
2373                 if (declaration != NULL) {
2374                         /* TODO: get the alignment of this variable. */
2375                 }
2376                 type = tp_expression->base.type;
2377                 assert(type != NULL);
2378         }
2379
2380         ir_mode *const mode = get_ir_mode(expression->base.type);
2381         symconst_symbol sym;
2382         sym.type_p = get_ir_type(type);
2383         return new_SymConst(mode, sym, symconst_type_align);
2384 }
2385
2386 static void init_ir_types(void);
2387 long fold_constant(const expression_t *expression)
2388 {
2389         init_ir_types();
2390
2391         assert(is_constant_expression(expression));
2392
2393         ir_graph *old_current_ir_graph = current_ir_graph;
2394         if(current_ir_graph == NULL) {
2395                 current_ir_graph = get_const_code_irg();
2396         }
2397
2398         ir_node *cnst = expression_to_firm(expression);
2399         current_ir_graph = old_current_ir_graph;
2400
2401         if(!is_Const(cnst)) {
2402                 panic("couldn't fold constant\n");
2403         }
2404
2405         tarval *tv = get_Const_tarval(cnst);
2406         if(!tarval_is_long(tv)) {
2407                 panic("result of constant folding is not integer\n");
2408         }
2409
2410         return get_tarval_long(tv);
2411 }
2412
2413 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2414 {
2415         dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2416
2417         /* first try to fold a constant condition */
2418         if(is_constant_expression(expression->condition)) {
2419                 long val = fold_constant(expression->condition);
2420                 if(val) {
2421                         return expression_to_firm(expression->true_expression);
2422                 } else {
2423                         return expression_to_firm(expression->false_expression);
2424                 }
2425         }
2426
2427         ir_node *cur_block   = get_cur_block();
2428
2429         /* create the true block */
2430         ir_node *true_block  = new_immBlock();
2431
2432         ir_node *true_val = expression_to_firm(expression->true_expression);
2433         ir_node *true_jmp = new_Jmp();
2434
2435         /* create the false block */
2436         ir_node *false_block = new_immBlock();
2437
2438         ir_node *false_val = expression_to_firm(expression->false_expression);
2439         ir_node *false_jmp = new_Jmp();
2440
2441         /* create the condition evaluation */
2442         set_cur_block(cur_block);
2443         create_condition_evaluation(expression->condition, true_block, false_block);
2444         mature_immBlock(true_block);
2445         mature_immBlock(false_block);
2446
2447         /* create the common block */
2448         ir_node *common_block = new_immBlock();
2449         add_immBlock_pred(common_block, true_jmp);
2450         add_immBlock_pred(common_block, false_jmp);
2451         mature_immBlock(common_block);
2452
2453         /* TODO improve static semantics, so either both or no values are NULL */
2454         if (true_val == NULL || false_val == NULL)
2455                 return NULL;
2456
2457         ir_node *in[2] = { true_val, false_val };
2458         ir_mode *mode  = get_irn_mode(true_val);
2459         assert(get_irn_mode(false_val) == mode);
2460         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
2461
2462         return val;
2463 }
2464
2465 static ir_node *select_addr(const select_expression_t *expression)
2466 {
2467         dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2468
2469         ir_node *compound_addr = expression_to_firm(expression->compound);
2470
2471         declaration_t *entry = expression->compound_entry;
2472         assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2473         ir_entity     *entity = entry->v.entity;
2474
2475         assert(entity != NULL);
2476
2477         ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2478
2479         return sel;
2480 }
2481
2482 static ir_node *select_to_firm(const select_expression_t *expression)
2483 {
2484         dbg_info *dbgi   = get_dbg_info(&expression->base.source_position);
2485         ir_node  *addr   = select_addr(expression);
2486         type_t   *type   = revert_automatic_type_conversion(
2487                         (const expression_t*) expression);
2488         type             = skip_typeref(type);
2489         ir_type  *irtype = get_ir_type(type);
2490
2491         return deref_address(irtype, addr, dbgi);
2492 }
2493
2494 /* Values returned by __builtin_classify_type. */
2495 typedef enum gcc_type_class
2496 {
2497         no_type_class = -1,
2498         void_type_class,
2499         integer_type_class,
2500         char_type_class,
2501         enumeral_type_class,
2502         boolean_type_class,
2503         pointer_type_class,
2504         reference_type_class,
2505         offset_type_class,
2506         real_type_class,
2507         complex_type_class,
2508         function_type_class,
2509         method_type_class,
2510         record_type_class,
2511         union_type_class,
2512         array_type_class,
2513         string_type_class,
2514         set_type_class,
2515         file_type_class,
2516         lang_type_class
2517 } gcc_type_class;
2518
2519 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2520 {
2521         const type_t *const type = expr->type_expression->base.type;
2522
2523         gcc_type_class tc;
2524         switch (type->kind)
2525         {
2526                 case TYPE_ATOMIC: {
2527                         const atomic_type_t *const atomic_type = &type->atomic;
2528                         switch (atomic_type->akind) {
2529                                 /* should not be reached */
2530                                 case ATOMIC_TYPE_INVALID:
2531                                         tc = no_type_class;
2532                                         break;
2533
2534                                 /* gcc cannot do that */
2535                                 case ATOMIC_TYPE_VOID:
2536                                         tc = void_type_class;
2537                                         break;
2538
2539                                 case ATOMIC_TYPE_CHAR:      /* gcc handles this as integer */
2540                                 case ATOMIC_TYPE_SCHAR:     /* gcc handles this as integer */
2541                                 case ATOMIC_TYPE_UCHAR:     /* gcc handles this as integer */
2542                                 case ATOMIC_TYPE_SHORT:
2543                                 case ATOMIC_TYPE_USHORT:
2544                                 case ATOMIC_TYPE_INT:
2545                                 case ATOMIC_TYPE_UINT:
2546                                 case ATOMIC_TYPE_LONG:
2547                                 case ATOMIC_TYPE_ULONG:
2548                                 case ATOMIC_TYPE_LONGLONG:
2549                                 case ATOMIC_TYPE_ULONGLONG:
2550                                 case ATOMIC_TYPE_BOOL:      /* gcc handles this as integer */
2551                                         tc = integer_type_class;
2552                                         break;
2553
2554                                 case ATOMIC_TYPE_FLOAT:
2555                                 case ATOMIC_TYPE_DOUBLE:
2556                                 case ATOMIC_TYPE_LONG_DOUBLE:
2557                                         tc = real_type_class;
2558                                         break;
2559
2560 #ifdef PROVIDE_COMPLEX
2561                                 case ATOMIC_TYPE_FLOAT_COMPLEX:
2562                                 case ATOMIC_TYPE_DOUBLE_COMPLEX:
2563                                 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
2564                                         tc = complex_type_class;
2565                                         break;
2566                                 case ATOMIC_TYPE_FLOAT_IMAGINARY:
2567                                 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
2568                                 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
2569                                         tc = complex_type_class;
2570                                         break;
2571 #endif
2572
2573                                 default:
2574                                         panic("Unimplemented case in classify_type_to_firm().");
2575                         }
2576                         break;
2577                 }
2578
2579                 case TYPE_ARRAY:           /* gcc handles this as pointer */
2580                 case TYPE_FUNCTION:        /* gcc handles this as pointer */
2581                 case TYPE_POINTER:         tc = pointer_type_class; break;
2582                 case TYPE_COMPOUND_STRUCT: tc = record_type_class;  break;
2583                 case TYPE_COMPOUND_UNION:  tc = union_type_class;   break;
2584
2585                 /* gcc handles this as integer */
2586                 case TYPE_ENUM:            tc = integer_type_class; break;
2587
2588                 default:
2589                         panic("Unimplemented case in classify_type_to_firm().");
2590         }
2591
2592         dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2593         ir_mode  *const mode = mode_int;
2594         tarval   *const tv   = new_tarval_from_long(tc, mode);
2595         return new_d_Const(dbgi, mode, tv);
2596 }
2597
2598 static ir_node *function_name_to_firm(
2599                 const string_literal_expression_t *const expr)
2600 {
2601         if (current_function_name == NULL) {
2602                 const source_position_t *const src_pos = &expr->base.source_position;
2603                 const char *const name = current_function_decl->symbol->string;
2604                 const string_t string = { name, strlen(name) + 1 };
2605                 current_function_name = string_to_firm(src_pos, "__func__", &string);
2606         }
2607
2608         return current_function_name;
2609 }
2610
2611 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2612 {
2613         statement_t *statement = expr->statement;
2614
2615         assert(statement->kind == STATEMENT_COMPOUND);
2616         return compound_statement_to_firm(&statement->compound);
2617 }
2618
2619 static ir_node *va_start_expression_to_firm(
2620         const va_start_expression_t *const expr)
2621 {
2622         ir_type   *const method_type = get_ir_type(current_function_decl->type);
2623         int        const n           = get_method_n_params(method_type) - 1;
2624         ir_entity *const parm_ent    = get_method_value_param_ent(method_type, n);
2625         ir_node   *const arg_base    = get_irg_value_param_base(current_ir_graph);
2626         dbg_info  *const dbgi        = get_dbg_info(&expr->base.source_position);
2627         ir_node   *const no_mem      = new_NoMem();
2628         ir_node   *const arg_sel     =
2629                 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2630
2631         size_t     const parm_size   = get_type_size(expr->parameter->type);
2632         ir_node   *const cnst        = new_Const_long(mode_uint, parm_size);
2633         ir_node   *const add         = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2634         set_value_for_expression(expr->ap, add);
2635
2636         return NULL;
2637 }
2638
2639 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2640 {
2641         ir_type  *const irtype = get_ir_type(expr->base.type);
2642         ir_node  *const ap     = expression_to_firm(expr->ap);
2643         dbg_info *const dbgi   = get_dbg_info(&expr->base.source_position);
2644         ir_node  *const res    = deref_address(irtype, ap, dbgi);
2645
2646         size_t    const parm_size = get_type_size(expr->base.type);
2647         ir_node  *const cnst      = new_Const_long(mode_uint, parm_size);
2648         ir_node  *const add       = new_d_Add(dbgi, ap, cnst, mode_P_data);
2649         set_value_for_expression(expr->ap, add);
2650
2651         return res;
2652 }
2653
2654 static ir_node *dereference_addr(const unary_expression_t *const expression)
2655 {
2656         assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2657         return expression_to_firm(expression->value);
2658 }
2659
2660 static ir_node *expression_to_addr(const expression_t *expression)
2661 {
2662         switch(expression->kind) {
2663         case EXPR_REFERENCE:
2664                 return reference_addr(&expression->reference);
2665         case EXPR_ARRAY_ACCESS:
2666                 return array_access_addr(&expression->array_access);
2667         case EXPR_SELECT:
2668                 return select_addr(&expression->select);
2669         case EXPR_CALL:
2670                 return call_expression_to_firm(&expression->call);
2671         case EXPR_UNARY_DEREFERENCE: {
2672                 return dereference_addr(&expression->unary);
2673         }
2674         default:
2675                 break;
2676         }
2677         panic("trying to get address of non-lvalue");
2678 }
2679
2680 static ir_node *builtin_constant_to_firm(
2681                 const builtin_constant_expression_t *expression)
2682 {
2683         ir_mode *mode = get_ir_mode(expression->base.type);
2684         long     v;
2685
2686         if (is_constant_expression(expression->value)) {
2687                 v = 1;
2688         } else {
2689                 v = 0;
2690         }
2691         return new_Const_long(mode, v);
2692 }
2693
2694 static ir_node *builtin_prefetch_to_firm(
2695                 const builtin_prefetch_expression_t *expression)
2696 {
2697         ir_node *adr = expression_to_firm(expression->adr);
2698         /* no Firm support for prefetch yet */
2699         (void) adr;
2700         return NULL;
2701 }
2702
2703 static ir_node *_expression_to_firm(const expression_t *expression)
2704 {
2705         switch(expression->kind) {
2706         case EXPR_CHAR_CONST:
2707                 return char_const_to_firm(&expression->conste);
2708         case EXPR_CONST:
2709                 return const_to_firm(&expression->conste);
2710         case EXPR_STRING_LITERAL:
2711                 return string_literal_to_firm(&expression->string);
2712         case EXPR_WIDE_STRING_LITERAL:
2713                 return wide_string_literal_to_firm(&expression->wide_string);
2714         case EXPR_REFERENCE:
2715                 return reference_expression_to_firm(&expression->reference);
2716         case EXPR_CALL:
2717                 return call_expression_to_firm(&expression->call);
2718         EXPR_UNARY_CASES
2719                 return unary_expression_to_firm(&expression->unary);
2720         EXPR_BINARY_CASES
2721                 return binary_expression_to_firm(&expression->binary);
2722         case EXPR_ARRAY_ACCESS:
2723                 return array_access_to_firm(&expression->array_access);
2724         case EXPR_SIZEOF:
2725                 return sizeof_to_firm(&expression->typeprop);
2726         case EXPR_ALIGNOF:
2727                 return alignof_to_firm(&expression->typeprop);
2728         case EXPR_CONDITIONAL:
2729                 return conditional_to_firm(&expression->conditional);
2730         case EXPR_SELECT:
2731                 return select_to_firm(&expression->select);
2732         case EXPR_CLASSIFY_TYPE:
2733                 return classify_type_to_firm(&expression->classify_type);
2734         case EXPR_FUNCTION:
2735         case EXPR_PRETTY_FUNCTION:
2736                 return function_name_to_firm(&expression->string);
2737         case EXPR_STATEMENT:
2738                 return statement_expression_to_firm(&expression->statement);
2739         case EXPR_VA_START:
2740                 return va_start_expression_to_firm(&expression->va_starte);
2741         case EXPR_VA_ARG:
2742                 return va_arg_expression_to_firm(&expression->va_arge);
2743         case EXPR_BUILTIN_SYMBOL:
2744                 panic("unimplemented expression found");
2745         case EXPR_BUILTIN_CONSTANT_P:
2746                 return builtin_constant_to_firm(&expression->builtin_constant);
2747         case EXPR_BUILTIN_PREFETCH:
2748                 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2749         case EXPR_OFFSETOF:
2750                 return offsetof_to_firm(&expression->offsetofe);
2751         case EXPR_COMPOUND_LITERAL:
2752                 return compound_literal_to_firm(&expression->compound_literal);
2753
2754         case EXPR_UNKNOWN:
2755         case EXPR_INVALID:
2756                 break;
2757         }
2758         panic("invalid expression found");
2759 }
2760
2761 static ir_node *expression_to_firm(const expression_t *expression)
2762 {
2763         ir_node *res = _expression_to_firm(expression);
2764
2765         if(res != NULL && get_irn_mode(res) == mode_b) {
2766                 ir_mode *mode = get_ir_mode(expression->base.type);
2767                 res           = create_conv(NULL, res, mode);
2768         }
2769
2770         return res;
2771 }
2772
2773 static ir_node *expression_to_modeb(const expression_t *expression)
2774 {
2775         ir_node *res = _expression_to_firm(expression);
2776         res          = create_conv(NULL, res, mode_b);
2777
2778         return res;
2779 }
2780
2781 /**
2782  * create a short-circuit expression evaluation that tries to construct
2783  * efficient control flow structures for &&, || and ! expressions
2784  */
2785 static void create_condition_evaluation(const expression_t *expression,
2786                                         ir_node *true_block,
2787                                         ir_node *false_block)
2788 {
2789         switch(expression->kind) {
2790         case EXPR_UNARY_NOT: {
2791                 const unary_expression_t *unary_expression = &expression->unary;
2792                 create_condition_evaluation(unary_expression->value, false_block,
2793                                             true_block);
2794                 return;
2795         }
2796         case EXPR_BINARY_LOGICAL_AND: {
2797                 const binary_expression_t *binary_expression = &expression->binary;
2798
2799                 ir_node *cur_block   = get_cur_block();
2800                 ir_node *extra_block = new_immBlock();
2801                 set_cur_block(cur_block);
2802                 create_condition_evaluation(binary_expression->left, extra_block,
2803                                             false_block);
2804                 mature_immBlock(extra_block);
2805                 set_cur_block(extra_block);
2806                 create_condition_evaluation(binary_expression->right, true_block,
2807                                             false_block);
2808                 return;
2809         }
2810         case EXPR_BINARY_LOGICAL_OR: {
2811                 const binary_expression_t *binary_expression = &expression->binary;
2812
2813                 ir_node *cur_block   = get_cur_block();
2814                 ir_node *extra_block = new_immBlock();
2815                 set_cur_block(cur_block);
2816                 create_condition_evaluation(binary_expression->left, true_block,
2817                                             extra_block);
2818                 mature_immBlock(extra_block);
2819                 set_cur_block(extra_block);
2820                 create_condition_evaluation(binary_expression->right, true_block,
2821                                             false_block);
2822                 return;
2823         }
2824         default:
2825                 break;
2826         }
2827
2828         dbg_info *dbgi       = get_dbg_info(&expression->base.source_position);
2829         ir_node  *condition  = expression_to_modeb(expression);
2830         ir_node  *cond       = new_d_Cond(dbgi, condition);
2831         ir_node  *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2832         ir_node  *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2833
2834         /* set branch prediction info based on __builtin_expect */
2835         if(expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
2836                 long               cnst = fold_constant(expression->binary.right);
2837                 cond_jmp_predicate pred;
2838
2839                 if(cnst == 0) {
2840                         pred = COND_JMP_PRED_FALSE;
2841                 } else {
2842                         pred = COND_JMP_PRED_TRUE;
2843                 }
2844                 set_Cond_jmp_pred(cond, pred);
2845         }
2846
2847         add_immBlock_pred(true_block, true_proj);
2848         add_immBlock_pred(false_block, false_proj);
2849
2850         set_cur_block(NULL);
2851 }
2852
2853
2854
2855 static void create_declaration_entity(declaration_t *declaration,
2856                                       declaration_kind_t declaration_kind,
2857                                       ir_type *parent_type)
2858 {
2859         ident     *const id     = new_id_from_str(declaration->symbol->string);
2860         ir_type   *const irtype = get_ir_type(declaration->type);
2861         dbg_info  *const dbgi   = get_dbg_info(&declaration->source_position);
2862         ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2863         set_entity_ld_ident(entity, id);
2864
2865         declaration->declaration_kind = (unsigned char) declaration_kind;
2866         declaration->v.entity         = entity;
2867         set_entity_variability(entity, variability_uninitialized);
2868         if(parent_type == get_tls_type())
2869                 set_entity_allocation(entity, allocation_automatic);
2870         else if(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
2871                 set_entity_allocation(entity, allocation_static);
2872         /* TODO: visibility? */
2873 }
2874
2875
2876 typedef struct type_path_entry_t type_path_entry_t;
2877 struct type_path_entry_t {
2878         type_t           *type;
2879         ir_initializer_t *initializer;
2880         size_t            index;
2881         declaration_t    *compound_entry;
2882 };
2883
2884 typedef struct type_path_t type_path_t;
2885 struct type_path_t {
2886         type_path_entry_t *path;
2887         type_t            *top_type;
2888         bool               invalid;
2889 };
2890
2891 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
2892 {
2893         size_t len = ARR_LEN(path->path);
2894
2895         for(size_t i = 0; i < len; ++i) {
2896                 const type_path_entry_t *entry = & path->path[i];
2897
2898                 type_t *type = skip_typeref(entry->type);
2899                 if(is_type_compound(type)) {
2900                         fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
2901                 } else if(is_type_array(type)) {
2902                         fprintf(stderr, "[%u]", entry->index);
2903                 } else {
2904                         fprintf(stderr, "-INVALID-");
2905                 }
2906         }
2907         fprintf(stderr, "  (");
2908         print_type(path->top_type);
2909         fprintf(stderr, ")");
2910 }
2911
2912 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2913 {
2914         size_t len = ARR_LEN(path->path);
2915         assert(len > 0);
2916         return & path->path[len-1];
2917 }
2918
2919 static type_path_entry_t *append_to_type_path(type_path_t *path)
2920 {
2921         size_t len = ARR_LEN(path->path);
2922         ARR_RESIZE(type_path_entry_t, path->path, len+1);
2923
2924         type_path_entry_t *result = & path->path[len];
2925         memset(result, 0, sizeof(result[0]));
2926         return result;
2927 }
2928
2929 static size_t get_compound_size(const compound_type_t *type)
2930 {
2931         declaration_t *declaration = type->declaration;
2932         declaration_t *member      = declaration->scope.declarations;
2933         size_t         size        = 0;
2934         for( ; member != NULL; member = member->next) {
2935                 ++size;
2936         }
2937         /* TODO: cache results? */
2938
2939         return size;
2940 }
2941
2942 static ir_initializer_t *get_initializer_entry(type_path_t *path)
2943 {
2944         type_t *orig_top_type = path->top_type;
2945         type_t *top_type      = skip_typeref(orig_top_type);
2946
2947         assert(is_type_compound(top_type) || is_type_array(top_type));
2948
2949         if(ARR_LEN(path->path) == 0) {
2950                 return NULL;
2951         } else {
2952                 type_path_entry_t *top         = get_type_path_top(path);
2953                 ir_initializer_t  *initializer = top->initializer;
2954                 return get_initializer_compound_value(initializer, top->index);
2955         }
2956 }
2957
2958 static void descend_into_subtype(type_path_t *path)
2959 {
2960         type_t *orig_top_type = path->top_type;
2961         type_t *top_type      = skip_typeref(orig_top_type);
2962
2963         assert(is_type_compound(top_type) || is_type_array(top_type));
2964
2965         ir_initializer_t *initializer = get_initializer_entry(path);
2966
2967         type_path_entry_t *top = append_to_type_path(path);
2968         top->type              = top_type;
2969
2970         size_t len;
2971
2972         if(is_type_compound(top_type)) {
2973                 declaration_t *declaration = top_type->compound.declaration;
2974                 declaration_t *entry       = declaration->scope.declarations;
2975
2976                 top->compound_entry = entry;
2977                 top->index          = 0;
2978                 path->top_type      = entry->type;
2979                 len                 = get_compound_size(&top_type->compound);
2980         } else {
2981                 assert(is_type_array(top_type));
2982                 assert(top_type->array.size > 0);
2983
2984                 top->index     = 0;
2985                 path->top_type = top_type->array.element_type;
2986                 len            = top_type->array.size;
2987         }
2988         if(initializer == NULL
2989                         || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
2990                 initializer = create_initializer_compound(len);
2991                 /* we have to set the entry at the 2nd latest path entry... */
2992                 size_t path_len = ARR_LEN(path->path);
2993                 assert(path_len >= 1);
2994                 if(path_len > 1) {
2995                         type_path_entry_t *entry        = & path->path[path_len-2];
2996                         ir_initializer_t  *tinitializer = entry->initializer;
2997                         set_initializer_compound_value(tinitializer, entry->index,
2998                                                        initializer);
2999                 }
3000         }
3001         top->initializer = initializer;
3002 }
3003
3004 static void ascend_from_subtype(type_path_t *path)
3005 {
3006         type_path_entry_t *top = get_type_path_top(path);
3007
3008         path->top_type = top->type;
3009
3010         size_t len = ARR_LEN(path->path);
3011         ARR_RESIZE(type_path_entry_t, path->path, len-1);
3012 }
3013
3014 static void walk_designator(type_path_t *path, const designator_t *designator)
3015 {
3016         /* designators start at current object type */
3017         ARR_RESIZE(type_path_entry_t, path->path, 1);
3018
3019         for( ; designator != NULL; designator = designator->next) {
3020                 type_path_entry_t *top         = get_type_path_top(path);
3021                 type_t            *orig_type   = top->type;
3022                 type_t            *type        = skip_typeref(orig_type);
3023
3024                 if(designator->symbol != NULL) {
3025                         assert(is_type_compound(type));
3026                         size_t    index  = 0;
3027                         symbol_t *symbol = designator->symbol;
3028
3029                         declaration_t *declaration = type->compound.declaration;
3030                         declaration_t *iter        = declaration->scope.declarations;
3031                         for( ; iter != NULL; iter = iter->next, ++index) {
3032                                 if(iter->symbol == symbol) {
3033                                         break;
3034                                 }
3035                         }
3036                         assert(iter != NULL);
3037
3038                         top->type           = orig_type;
3039                         top->compound_entry = iter;
3040                         top->index          = index;
3041                         orig_type           = iter->type;
3042                 } else {
3043                         expression_t *array_index = designator->array_index;
3044                         assert(designator->array_index != NULL);
3045                         assert(is_type_array(type));
3046                         assert(is_type_valid(array_index->base.type));
3047
3048                         long index = fold_constant(array_index);
3049                         assert(index >= 0);
3050 #ifndef NDEBUG
3051                         if(type->array.size_constant == 1) {
3052                                 long array_size = type->array.size;
3053                                 assert(index < array_size);
3054                         }
3055 #endif
3056
3057                         top->type  = orig_type;
3058                         top->index = (size_t) index;
3059                         orig_type  = type->array.element_type;
3060                 }
3061                 path->top_type = orig_type;
3062
3063                 if(designator->next != NULL) {
3064                         descend_into_subtype(path);
3065                 }
3066         }
3067
3068         path->invalid  = false;
3069 }
3070
3071 static void advance_current_object(type_path_t *path)
3072 {
3073         if(path->invalid) {
3074                 /* TODO: handle this... */
3075                 panic("invalid initializer in ast2firm (excessive elements)");
3076                 return;
3077         }
3078
3079         type_path_entry_t *top = get_type_path_top(path);
3080
3081         type_t *type = skip_typeref(top->type);
3082         if(is_type_union(type)) {
3083                 top->compound_entry = NULL;
3084         } else if(is_type_struct(type)) {
3085                 declaration_t *entry = top->compound_entry;
3086
3087                 top->index++;
3088                 entry               = entry->next;
3089                 top->compound_entry = entry;
3090                 if(entry != NULL) {
3091                         path->top_type = entry->type;
3092                         return;
3093                 }
3094         } else {
3095                 assert(is_type_array(type));
3096
3097                 top->index++;
3098                 if(!type->array.size_constant || top->index < type->array.size) {
3099                         return;
3100                 }
3101         }
3102
3103         /* we're past the last member of the current sub-aggregate, try if we
3104          * can ascend in the type hierarchy and continue with another subobject */
3105         size_t len = ARR_LEN(path->path);
3106
3107         if(len > 1) {
3108                 ascend_from_subtype(path);
3109                 advance_current_object(path);
3110         } else {
3111                 path->invalid = true;
3112         }
3113 }
3114
3115
3116 static ir_initializer_t *create_ir_initializer(
3117                 const initializer_t *initializer, type_t *type);
3118
3119 static ir_initializer_t *create_ir_initializer_value(
3120                 const initializer_value_t *initializer)
3121 {
3122         ir_node *value = expression_to_firm(initializer->value);
3123         return create_initializer_const(value);
3124 }
3125
3126 static ir_initializer_t *create_ir_initializer_list(
3127                 const initializer_list_t *initializer, type_t *type)
3128 {
3129         type_path_t path;
3130         memset(&path, 0, sizeof(path));
3131         path.top_type = type;
3132         path.path     = NEW_ARR_F(type_path_entry_t, 0);
3133
3134         descend_into_subtype(&path);
3135
3136         for(size_t i = 0; i < initializer->len; ++i) {
3137                 const initializer_t *sub_initializer = initializer->initializers[i];
3138
3139                 if(sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3140                         walk_designator(&path, sub_initializer->designator.designator);
3141                         continue;
3142                 }
3143
3144                 if(sub_initializer->kind == INITIALIZER_VALUE) {
3145                         /* we might have to descend into types until we're at a scalar
3146                          * type */
3147                         while(true) {
3148                                 type_t *orig_top_type = path.top_type;
3149                                 type_t *top_type      = skip_typeref(orig_top_type);
3150
3151                                 if(is_type_scalar(top_type))
3152                                         break;
3153                                 descend_into_subtype(&path);
3154                         }
3155                 }
3156
3157                 ir_initializer_t *sub_irinitializer
3158                         = create_ir_initializer(sub_initializer, path.top_type);
3159
3160                 size_t path_len = ARR_LEN(path.path);
3161                 assert(path_len >= 1);
3162                 type_path_entry_t *entry        = & path.path[path_len-1];
3163                 ir_initializer_t  *tinitializer = entry->initializer;
3164                 set_initializer_compound_value(tinitializer, entry->index,
3165                                                sub_irinitializer);
3166
3167                 advance_current_object(&path);
3168         }
3169
3170         assert(ARR_LEN(path.path) >= 1);
3171         ir_initializer_t *result = path.path[0].initializer;
3172         DEL_ARR_F(path.path);
3173
3174         return result;
3175 }
3176
3177 static ir_initializer_t *create_ir_initializer_string(
3178                 const initializer_string_t *initializer, type_t *type)
3179 {
3180         size_t            string_len    = initializer->string.size;
3181         assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3182         size_t            len           = type->array.size;
3183         ir_initializer_t *irinitializer = create_initializer_compound(len);
3184
3185         const char *string = initializer->string.begin;
3186         ir_mode    *mode   = get_type_mode(ir_type_const_char);
3187
3188         for(size_t i = 0; i < len; ++i) {
3189                 char c = 0;
3190                 if(i < string_len)
3191                         c = string[i];
3192
3193                 tarval           *tv = new_tarval_from_long(string[i], mode);
3194                 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3195
3196                 set_initializer_compound_value(irinitializer, i, char_initializer);
3197         }
3198
3199         return irinitializer;
3200 }
3201
3202 static ir_initializer_t *create_ir_initializer_wide_string(
3203                 const initializer_wide_string_t *initializer, type_t *type)
3204 {
3205         size_t            string_len    = initializer->string.size;
3206         assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3207         size_t            len           = type->array.size;
3208         ir_initializer_t *irinitializer = create_initializer_compound(len);
3209
3210         const wchar_rep_t *string = initializer->string.begin;
3211         ir_mode           *mode   = get_type_mode(ir_type_wchar_t);
3212
3213         for(size_t i = 0; i < len; ++i) {
3214                 wchar_rep_t c = 0;
3215                 if(i < string_len) {
3216                         c = string[i];
3217                 }
3218                 tarval *tv = new_tarval_from_long(string[i], mode);
3219                 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3220
3221                 set_initializer_compound_value(irinitializer, i, char_initializer);
3222         }
3223
3224         return irinitializer;
3225 }
3226
3227 static ir_initializer_t *create_ir_initializer(
3228                 const initializer_t *initializer, type_t *type)
3229 {
3230         switch(initializer->kind) {
3231                 case INITIALIZER_STRING:
3232                         return create_ir_initializer_string(&initializer->string, type);
3233
3234                 case INITIALIZER_WIDE_STRING:
3235                         return create_ir_initializer_wide_string(&initializer->wide_string,
3236                                                                  type);
3237
3238                 case INITIALIZER_LIST:
3239                         return create_ir_initializer_list(&initializer->list, type);
3240
3241                 case INITIALIZER_VALUE:
3242                         return create_ir_initializer_value(&initializer->value);
3243
3244                 case INITIALIZER_DESIGNATOR:
3245                         panic("unexpected designator initializer found");
3246         }
3247         panic("unknown initializer");
3248 }
3249
3250 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3251                 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3252 {
3253         switch(get_initializer_kind(initializer)) {
3254         case IR_INITIALIZER_NULL: {
3255                 ir_mode *mode = get_type_mode(type);
3256                 /* TODO: implement this for compound types... */
3257                 assert(type != NULL);
3258                 tarval  *zero = get_mode_null(mode);
3259                 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3260
3261                 /* TODO: bitfields */
3262                 ir_node *mem    = get_store();
3263                 ir_node *store  = new_d_Store(dbgi, mem, base_addr, cnst);
3264                 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3265                 set_store(proj_m);
3266                 return;
3267         }
3268         case IR_INITIALIZER_CONST: {
3269                 ir_node *node = get_initializer_const_value(initializer);
3270                 ir_mode *mode = get_irn_mode(node);
3271                 assert(get_type_mode(type) == mode);
3272
3273                 /* TODO: bitfields... */
3274                 ir_node *mem    = get_store();
3275                 ir_node *store  = new_d_Store(dbgi, mem, base_addr, node);
3276                 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3277                 set_store(proj_m);
3278                 return;
3279         }
3280         case IR_INITIALIZER_TARVAL: {
3281                 tarval  *tv   = get_initializer_tarval_value(initializer);
3282                 ir_mode *mode = get_tarval_mode(tv);
3283                 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3284                 assert(get_type_mode(type) == mode);
3285
3286                 /* TODO: bitfields... */
3287                 ir_node *mem    = get_store();
3288                 ir_node *store  = new_d_Store(dbgi, mem, base_addr, cnst);
3289                 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3290                 set_store(proj_m);
3291                 return;
3292         }
3293         case IR_INITIALIZER_COMPOUND: {
3294                 assert(is_compound_type(type));
3295                 int n_members;
3296                 if(is_Array_type(type)) {
3297                         assert(has_array_upper_bound(type, 0));
3298                         n_members = get_array_upper_bound_int(type, 0);
3299                 } else {
3300                         n_members = get_compound_n_members(type);
3301                 }
3302
3303                 if(get_initializer_compound_n_entries(initializer)
3304                                 != (unsigned) n_members)
3305                         panic("initializer doesn't match compound type");
3306
3307                 for(int i = 0; i < n_members; ++i) {
3308                         ir_node *addr;
3309                         ir_type *irtype;
3310                         if(is_Array_type(type)) {
3311                                 ir_entity *entity   = get_array_element_entity(type);
3312                                 tarval    *index_tv = new_tarval_from_long(i, mode_uint);
3313                                 ir_node   *cnst     = new_d_Const(dbgi, mode_uint, index_tv);
3314                                 ir_node   *in[1]    = { cnst };
3315                                 irtype = get_array_element_type(type);
3316                                 addr   = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3317                         } else {
3318                                 ir_entity *member = get_compound_member(type, i);
3319
3320                                 irtype = get_entity_type(member);
3321                                 addr   = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3322                         }
3323
3324                         ir_initializer_t *sub_init
3325                                 = get_initializer_compound_value(initializer, i);
3326
3327                         create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3328                 }
3329                 return;
3330         }
3331         }
3332
3333         panic("invalid IR_INITIALIZER found");
3334 }
3335
3336 static void create_dynamic_initializer(ir_initializer_t *initializer,
3337                 dbg_info *dbgi, ir_entity *entity)
3338 {
3339         ir_node *frame     = get_local_frame(entity);
3340         ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3341         ir_type *type      = get_entity_type(entity);
3342
3343         create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3344 }
3345
3346 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3347                                      ir_entity *entity, type_t *type)
3348 {
3349         ir_node *memory = get_store();
3350         ir_node *nomem  = new_NoMem();
3351         ir_node *frame  = get_irg_frame(current_ir_graph);
3352         ir_node *addr   = new_d_simpleSel(dbgi, nomem, frame, entity);
3353
3354         if(initializer->kind == INITIALIZER_VALUE) {
3355                 initializer_value_t *initializer_value = &initializer->value;
3356
3357                 ir_node *value = expression_to_firm(initializer_value->value);
3358                 type = skip_typeref(type);
3359                 assign_value(dbgi, addr, type, value);
3360                 return;
3361         }
3362
3363         if(!is_constant_initializer(initializer)) {
3364                 ir_initializer_t *irinitializer
3365                         = create_ir_initializer(initializer, type);
3366
3367                 create_dynamic_initializer(irinitializer, dbgi, entity);
3368                 return;
3369         }
3370
3371         /* create the ir_initializer */
3372         ir_graph *const old_current_ir_graph = current_ir_graph;
3373         current_ir_graph = get_const_code_irg();
3374
3375         ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3376
3377         assert(current_ir_graph == get_const_code_irg());
3378         current_ir_graph = old_current_ir_graph;
3379
3380         /* create a "template" entity which is copied to the entity on the stack */
3381         ident     *const id          = unique_ident("initializer");
3382         ir_type   *const irtype      = get_ir_type(type);
3383         ir_type   *const global_type = get_glob_type();
3384         ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3385         set_entity_ld_ident(init_entity, id);
3386
3387         set_entity_variability(init_entity, variability_initialized);
3388         set_entity_visibility(init_entity, visibility_local);
3389         set_entity_allocation(init_entity, allocation_static);
3390
3391         set_entity_initializer(init_entity, irinitializer);
3392
3393         ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3394         ir_node *const copyb    = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3395
3396         ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3397         set_store(copyb_mem);
3398 }
3399
3400 static void create_initializer_local_variable_entity(declaration_t *declaration)
3401 {
3402         initializer_t *initializer = declaration->init.initializer;
3403         dbg_info      *dbgi        = get_dbg_info(&declaration->source_position);
3404         ir_entity     *entity      = declaration->v.entity;
3405         type_t        *type        = declaration->type;
3406         create_local_initializer(initializer, dbgi, entity, type);
3407 }
3408
3409 static void create_declaration_initializer(declaration_t *declaration)
3410 {
3411         initializer_t *initializer = declaration->init.initializer;
3412         if(initializer == NULL)
3413                 return;
3414
3415         declaration_kind_t declaration_kind
3416                 = (declaration_kind_t) declaration->declaration_kind;
3417         if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3418                 create_initializer_local_variable_entity(declaration);
3419                 return;
3420         }
3421
3422         if(initializer->kind == INITIALIZER_VALUE) {
3423                 initializer_value_t *initializer_value = &initializer->value;
3424
3425                 ir_node *value = expression_to_firm(initializer_value->value);
3426
3427                 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3428                         set_value(declaration->v.value_number, value);
3429                 } else {
3430                         assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3431
3432                         ir_entity *entity = declaration->v.entity;
3433
3434                         set_entity_variability(entity, variability_initialized);
3435                         set_atomic_ent_value(entity, value);
3436                 }
3437         } else {
3438                 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3439                                 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3440
3441                 ir_entity        *entity        = declaration->v.entity;
3442                 ir_initializer_t *irinitializer
3443                         = create_ir_initializer(initializer, declaration->type);
3444
3445                 set_entity_variability(entity, variability_initialized);
3446                 set_entity_initializer(entity, irinitializer);
3447         }
3448 }
3449
3450 /**
3451  * Creates a Firm local variable from a declaration.
3452  */
3453 static void create_local_variable(declaration_t *declaration)
3454 {
3455         assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3456
3457         bool needs_entity = declaration->address_taken;
3458         type_t *type = skip_typeref(declaration->type);
3459
3460         if(is_type_array(type) || is_type_compound(type)) {
3461                 needs_entity = true;
3462         }
3463
3464         if(needs_entity) {
3465                 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3466                 create_declaration_entity(declaration,
3467                                           DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3468                                           frame_type);
3469         } else {
3470                 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3471                 declaration->v.value_number   = next_value_number_function;
3472                 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3473                 ++next_value_number_function;
3474         }
3475
3476         create_declaration_initializer(declaration);
3477 }
3478
3479 static void create_local_static_variable(declaration_t *declaration)
3480 {
3481         assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3482
3483         type_t    *const type        = skip_typeref(declaration->type);
3484         ir_type   *const global_type = get_glob_type();
3485         ident     *const id          = unique_ident(declaration->symbol->string);
3486         ir_type   *const irtype      = get_ir_type(type);
3487         dbg_info  *const dbgi        = get_dbg_info(&declaration->source_position);
3488         ir_entity *const entity      = new_d_entity(global_type, id, irtype, dbgi);
3489         set_entity_ld_ident(entity, id);
3490
3491         declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3492         declaration->v.entity         = entity;
3493         set_entity_variability(entity, variability_uninitialized);
3494         set_entity_visibility(entity, visibility_local);
3495         set_entity_allocation(entity, allocation_static);
3496
3497         ir_graph *const old_current_ir_graph = current_ir_graph;
3498         current_ir_graph = get_const_code_irg();
3499
3500         create_declaration_initializer(declaration);
3501
3502         assert(current_ir_graph == get_const_code_irg());
3503         current_ir_graph = old_current_ir_graph;
3504 }
3505
3506
3507
3508 static void return_statement_to_firm(return_statement_t *statement)
3509 {
3510         if(get_cur_block() == NULL)
3511                 return;
3512
3513         dbg_info *dbgi        = get_dbg_info(&statement->base.source_position);
3514         ir_type  *func_irtype = get_ir_type(current_function_decl->type);
3515
3516
3517         ir_node *in[1];
3518         int      in_len;
3519         if(get_method_n_ress(func_irtype) > 0) {
3520                 ir_type *res_type = get_method_res_type(func_irtype, 0);
3521
3522                 if(statement->value != NULL) {
3523                         ir_node *node = expression_to_firm(statement->value);
3524                         node  = do_strict_conv(dbgi, node);
3525                         in[0] = node;
3526                 } else {
3527                         ir_mode *mode;
3528                         if(is_compound_type(res_type)) {
3529                                 mode = mode_P_data;
3530                         } else {
3531                                 mode = get_type_mode(res_type);
3532                         }
3533                         in[0] = new_Unknown(mode);
3534                 }
3535                 in_len = 1;
3536         } else {
3537                 /* build return_value for its side effects */
3538                 if(statement->value != NULL) {
3539                         expression_to_firm(statement->value);
3540                 }
3541                 in_len = 0;
3542         }
3543
3544         ir_node  *store = get_store();
3545         ir_node  *ret   = new_d_Return(dbgi, store, in_len, in);
3546
3547         ir_node *end_block = get_irg_end_block(current_ir_graph);
3548         add_immBlock_pred(end_block, ret);
3549
3550         set_cur_block(NULL);
3551 }
3552
3553 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3554 {
3555         if(get_cur_block() == NULL)
3556                 return NULL;
3557
3558         return expression_to_firm(statement->expression);
3559 }
3560
3561 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3562 {
3563         ir_node     *result    = NULL;
3564         statement_t *statement = compound->statements;
3565         for( ; statement != NULL; statement = statement->base.next) {
3566                 //context2firm(&statement->scope);
3567
3568                 if(statement->base.next == NULL
3569                                 && statement->kind == STATEMENT_EXPRESSION) {
3570                         result = expression_statement_to_firm(
3571                                         &statement->expression);
3572                         break;
3573                 }
3574                 statement_to_firm(statement);
3575         }
3576
3577         return result;
3578 }
3579
3580 static void create_global_variable(declaration_t *declaration)
3581 {
3582         ir_visibility  vis;
3583         ir_type       *var_type;
3584         switch ((storage_class_tag_t)declaration->storage_class) {
3585                 case STORAGE_CLASS_STATIC:
3586                         vis = visibility_local;
3587                         goto global_var;
3588
3589                 case STORAGE_CLASS_EXTERN:
3590                         vis = visibility_external_allocated;
3591                         goto global_var;
3592
3593                 case STORAGE_CLASS_NONE:
3594                         vis = visibility_external_visible;
3595                         goto global_var;
3596
3597                 case STORAGE_CLASS_THREAD:
3598                         vis = visibility_external_visible;
3599                         goto tls_var;
3600
3601                 case STORAGE_CLASS_THREAD_EXTERN:
3602                         vis = visibility_external_allocated;
3603                         goto tls_var;
3604
3605                 case STORAGE_CLASS_THREAD_STATIC:
3606                         vis = visibility_local;
3607                         goto tls_var;
3608
3609 tls_var:
3610                         var_type = get_tls_type();
3611                         goto create_var;
3612
3613 global_var:
3614                         var_type = get_glob_type();
3615                         goto create_var;
3616
3617 create_var:
3618                         create_declaration_entity(declaration,
3619                                                   DECLARATION_KIND_GLOBAL_VARIABLE,
3620                                                   var_type);
3621                         set_entity_visibility(declaration->v.entity, vis);
3622
3623                         return;
3624
3625                 case STORAGE_CLASS_TYPEDEF:
3626                 case STORAGE_CLASS_AUTO:
3627                 case STORAGE_CLASS_REGISTER:
3628                 case STORAGE_CLASS_ENUM_ENTRY:
3629                         break;
3630         }
3631         panic("Invalid storage class for global variable");
3632 }
3633
3634 static void create_local_declaration(declaration_t *declaration)
3635 {
3636         if(declaration->symbol == NULL)
3637                 return;
3638
3639         type_t *type = skip_typeref(declaration->type);
3640
3641         switch ((storage_class_tag_t) declaration->storage_class) {
3642         case STORAGE_CLASS_STATIC:
3643                 create_local_static_variable(declaration);
3644                 return;
3645         case STORAGE_CLASS_EXTERN:
3646                 create_global_variable(declaration);
3647                 create_declaration_initializer(declaration);
3648                 return;
3649         case STORAGE_CLASS_NONE:
3650         case STORAGE_CLASS_AUTO:
3651         case STORAGE_CLASS_REGISTER:
3652                 if(is_type_function(type)) {
3653                         if(declaration->init.statement != NULL) {
3654                                 panic("nested functions not supported yet");
3655                         } else {
3656                                 get_function_entity(declaration);
3657                         }
3658                 } else {
3659                         create_local_variable(declaration);
3660                 }
3661                 return;
3662         case STORAGE_CLASS_ENUM_ENTRY:
3663         case STORAGE_CLASS_TYPEDEF:
3664         case STORAGE_CLASS_THREAD:
3665         case STORAGE_CLASS_THREAD_EXTERN:
3666         case STORAGE_CLASS_THREAD_STATIC:
3667                 return;
3668         }
3669         panic("invalid storage class found");
3670 }
3671
3672 static void declaration_statement_to_firm(declaration_statement_t *statement)
3673 {
3674         declaration_t *declaration = statement->declarations_begin;
3675         declaration_t *end         = statement->declarations_end->next;
3676         for( ; declaration != end; declaration = declaration->next) {
3677                 if(declaration->namespc != NAMESPACE_NORMAL)
3678                         continue;
3679                 create_local_declaration(declaration);
3680         }
3681 }
3682
3683 static void if_statement_to_firm(if_statement_t *statement)
3684 {
3685         ir_node *cur_block = get_cur_block();
3686
3687         ir_node *fallthrough_block = new_immBlock();
3688
3689         /* the true (blocks) */
3690         ir_node *true_block;
3691         if (statement->true_statement != NULL) {
3692                 true_block = new_immBlock();
3693                 statement_to_firm(statement->true_statement);
3694                 if(get_cur_block() != NULL) {
3695                         ir_node *jmp = new_Jmp();
3696                         add_immBlock_pred(fallthrough_block, jmp);
3697                 }
3698         } else {
3699                 true_block = fallthrough_block;
3700         }
3701
3702         /* the false (blocks) */
3703         ir_node *false_block;
3704         if(statement->false_statement != NULL) {
3705                 false_block = new_immBlock();
3706
3707                 statement_to_firm(statement->false_statement);
3708                 if(get_cur_block() != NULL) {
3709                         ir_node *jmp = new_Jmp();
3710                         add_immBlock_pred(fallthrough_block, jmp);
3711                 }
3712         } else {
3713                 false_block = fallthrough_block;
3714         }
3715
3716         /* create the condition */
3717         if(cur_block != NULL) {
3718                 set_cur_block(cur_block);
3719                 create_condition_evaluation(statement->condition, true_block,
3720                                             false_block);
3721         }
3722
3723         mature_immBlock(true_block);
3724         if(false_block != fallthrough_block) {
3725                 mature_immBlock(false_block);
3726         }
3727         mature_immBlock(fallthrough_block);
3728
3729         set_cur_block(fallthrough_block);
3730 }
3731
3732 static void while_statement_to_firm(while_statement_t *statement)
3733 {
3734         ir_node *jmp = NULL;
3735         if(get_cur_block() != NULL) {
3736                 jmp = new_Jmp();
3737         }
3738
3739         /* create the header block */
3740         ir_node *header_block = new_immBlock();
3741         if(jmp != NULL) {
3742                 add_immBlock_pred(header_block, jmp);
3743         }
3744
3745         /* the false block */
3746         ir_node *false_block = new_immBlock();
3747
3748         /* the loop body */
3749         ir_node *body_block;
3750         if (statement->body != NULL) {
3751                 ir_node *old_continue_label = continue_label;
3752                 ir_node *old_break_label    = break_label;
3753                 continue_label              = header_block;
3754                 break_label                 = false_block;
3755
3756                 body_block = new_immBlock();
3757                 statement_to_firm(statement->body);
3758
3759                 assert(continue_label == header_block);
3760                 assert(break_label    == false_block);
3761                 continue_label = old_continue_label;
3762                 break_label    = old_break_label;
3763
3764                 if(get_cur_block() != NULL) {
3765                         jmp = new_Jmp();
3766                         add_immBlock_pred(header_block, jmp);
3767                 }
3768         } else {
3769                 body_block = header_block;
3770         }
3771
3772         /* create the condition */
3773         set_cur_block(header_block);
3774
3775         create_condition_evaluation(statement->condition, body_block, false_block);
3776         mature_immBlock(body_block);
3777         mature_immBlock(false_block);
3778         mature_immBlock(header_block);
3779
3780         set_cur_block(false_block);
3781 }
3782
3783 static void do_while_statement_to_firm(do_while_statement_t *statement)
3784 {
3785         ir_node *jmp = NULL;
3786         if(get_cur_block() != NULL) {
3787                 jmp = new_Jmp();
3788         }
3789
3790         /* create the header block */
3791         ir_node *header_block = new_immBlock();
3792
3793         /* the false block */
3794         ir_node *false_block = new_immBlock();
3795
3796         /* the loop body */
3797         ir_node *body_block = new_immBlock();
3798         if(jmp != NULL) {
3799                 add_immBlock_pred(body_block, jmp);
3800         }
3801
3802         if (statement->body != NULL) {
3803                 ir_node *old_continue_label = continue_label;
3804                 ir_node *old_break_label    = break_label;
3805                 continue_label              = header_block;
3806                 break_label                 = false_block;
3807
3808                 statement_to_firm(statement->body);
3809
3810                 assert(continue_label == header_block);
3811                 assert(break_label    == false_block);
3812                 continue_label = old_continue_label;
3813                 break_label    = old_break_label;
3814
3815                 if (get_cur_block() == NULL) {
3816                         mature_immBlock(header_block);
3817                         mature_immBlock(body_block);
3818                         mature_immBlock(false_block);
3819                         return;
3820                 }
3821         }
3822
3823         ir_node *body_jmp = new_Jmp();
3824         add_immBlock_pred(header_block, body_jmp);
3825         mature_immBlock(header_block);
3826
3827         /* create the condition */
3828         set_cur_block(header_block);
3829
3830         create_condition_evaluation(statement->condition, body_block, false_block);
3831         mature_immBlock(body_block);
3832         mature_immBlock(false_block);
3833         mature_immBlock(header_block);
3834
3835         set_cur_block(false_block);
3836 }
3837
3838 static void for_statement_to_firm(for_statement_t *statement)
3839 {
3840         ir_node *jmp = NULL;
3841         if (get_cur_block() != NULL) {
3842                 if(statement->initialisation != NULL) {
3843                         expression_to_firm(statement->initialisation);
3844                 }
3845
3846                 /* create declarations */
3847                 declaration_t *declaration = statement->scope.declarations;
3848                 for( ; declaration != NULL; declaration = declaration->next) {
3849                         create_local_declaration(declaration);
3850                 }
3851
3852                 jmp = new_Jmp();
3853         }
3854
3855
3856         /* create the step block */
3857         ir_node *const step_block = new_immBlock();
3858         if (statement->step != NULL) {
3859                 expression_to_firm(statement->step);
3860         }
3861         ir_node *const step_jmp = new_Jmp();
3862
3863         /* create the header block */
3864         ir_node *const header_block = new_immBlock();
3865         if (jmp != NULL) {
3866                 add_immBlock_pred(header_block, jmp);
3867         }
3868         add_immBlock_pred(header_block, step_jmp);
3869
3870         /* the false block */
3871         ir_node *const false_block = new_immBlock();
3872
3873         /* the loop body */
3874         ir_node * body_block;
3875         if (statement->body != NULL) {
3876                 ir_node *const old_continue_label = continue_label;
3877                 ir_node *const old_break_label    = break_label;
3878                 continue_label = step_block;
3879                 break_label    = false_block;
3880
3881                 body_block = new_immBlock();
3882                 statement_to_firm(statement->body);
3883
3884                 assert(continue_label == step_block);
3885                 assert(break_label    == false_block);
3886                 continue_label = old_continue_label;
3887                 break_label    = old_break_label;
3888
3889                 if (get_cur_block() != NULL) {
3890                         jmp = new_Jmp();
3891                         add_immBlock_pred(step_block, jmp);
3892                 }
3893         } else {
3894                 body_block = step_block;
3895         }
3896
3897         /* create the condition */
3898         set_cur_block(header_block);
3899         if (statement->condition != NULL) {
3900                 create_condition_evaluation(statement->condition, body_block,
3901                                             false_block);
3902         } else {
3903                 keep_alive(header_block);
3904                 jmp = new_Jmp();
3905                 add_immBlock_pred(body_block, jmp);
3906         }
3907
3908         mature_immBlock(body_block);
3909         mature_immBlock(false_block);
3910         mature_immBlock(step_block);
3911         mature_immBlock(header_block);
3912         mature_immBlock(false_block);
3913
3914         set_cur_block(false_block);
3915 }
3916
3917 static void create_jump_statement(const statement_t *statement,
3918                                   ir_node *target_block)
3919 {
3920         if(get_cur_block() == NULL)
3921                 return;
3922
3923         dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3924         ir_node  *jump = new_d_Jmp(dbgi);
3925         add_immBlock_pred(target_block, jump);
3926
3927         set_cur_block(NULL);
3928 }
3929
3930 static void switch_statement_to_firm(const switch_statement_t *statement)
3931 {
3932         dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3933
3934         ir_node *expression  = expression_to_firm(statement->expression);
3935         ir_node *cond        = new_d_Cond(dbgi, expression);
3936         ir_node *break_block = new_immBlock();
3937
3938         set_cur_block(NULL);
3939
3940         ir_node *const old_switch_cond       = current_switch_cond;
3941         ir_node *const old_break_label       = break_label;
3942         const bool     old_saw_default_label = saw_default_label;
3943         current_switch_cond                  = cond;
3944         break_label                          = break_block;
3945
3946         if (statement->body != NULL) {
3947                 statement_to_firm(statement->body);
3948         }
3949
3950         if(get_cur_block() != NULL) {
3951                 ir_node *jmp = new_Jmp();
3952                 add_immBlock_pred(break_block, jmp);
3953         }
3954
3955         if (!saw_default_label) {
3956                 set_cur_block(get_nodes_block(cond));
3957                 ir_node *const proj = new_d_defaultProj(dbgi, cond,
3958                                                         MAGIC_DEFAULT_PN_NUMBER);
3959                 add_immBlock_pred(break_block, proj);
3960         }
3961
3962         assert(current_switch_cond == cond);
3963         assert(break_label         == break_block);
3964         current_switch_cond = old_switch_cond;
3965         break_label         = old_break_label;
3966         saw_default_label   = old_saw_default_label;
3967
3968         mature_immBlock(break_block);
3969         set_cur_block(break_block);
3970 }
3971
3972 static void case_label_to_firm(const case_label_statement_t *statement)
3973 {
3974         dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3975
3976         ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
3977
3978         /* let's create a node and hope firm constant folding creates a Const
3979          * node... */
3980         ir_node *proj;
3981         ir_node *old_block = get_nodes_block(current_switch_cond);
3982         ir_node *block     = new_immBlock();
3983
3984         set_cur_block(old_block);
3985         if(statement->expression != NULL) {
3986                 long start_pn = fold_constant(statement->expression);
3987                 long end_pn = start_pn;
3988                 if (statement->end_range != NULL) {
3989                         end_pn = fold_constant(statement->end_range);
3990                 }
3991                 assert(start_pn <= end_pn);
3992                 /* create jumps for all cases in the given range */
3993                 for (long pn = start_pn; pn <= end_pn; ++pn) {
3994                         if(pn == MAGIC_DEFAULT_PN_NUMBER) {
3995                                 /* oops someone detected our cheating... */
3996                                 panic("magic default pn used");
3997                         }
3998                         proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
3999                         add_immBlock_pred(block, proj);
4000                 }
4001         } else {
4002                 saw_default_label = true;
4003                 proj = new_d_defaultProj(dbgi, current_switch_cond,
4004                                          MAGIC_DEFAULT_PN_NUMBER);
4005
4006                 add_immBlock_pred(block, proj);
4007         }
4008
4009         if (fallthrough != NULL) {
4010                 add_immBlock_pred(block, fallthrough);
4011         }
4012         mature_immBlock(block);
4013         set_cur_block(block);
4014
4015         if(statement->statement != NULL) {
4016                 statement_to_firm(statement->statement);
4017         }
4018 }
4019
4020 static ir_node *get_label_block(declaration_t *label)
4021 {
4022         assert(label->namespc == NAMESPACE_LABEL);
4023
4024         if(label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
4025                 return label->v.block;
4026         }
4027         assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
4028
4029         ir_node *old_cur_block = get_cur_block();
4030         ir_node *block         = new_immBlock();
4031         set_cur_block(old_cur_block);
4032
4033         label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
4034         label->v.block          = block;
4035
4036         ARR_APP1(ir_node *, imature_blocks, block);
4037
4038         return block;
4039 }
4040
4041 static void label_to_firm(const label_statement_t *statement)
4042 {
4043         ir_node *block = get_label_block(statement->label);
4044
4045         if(get_cur_block() != NULL) {
4046                 ir_node *jmp = new_Jmp();
4047                 add_immBlock_pred(block, jmp);
4048         }
4049
4050         set_cur_block(block);
4051         keep_alive(block);
4052
4053         if(statement->statement != NULL) {
4054                 statement_to_firm(statement->statement);
4055         }
4056 }
4057
4058 static void goto_to_firm(const goto_statement_t *statement)
4059 {
4060         if(get_cur_block() == NULL)
4061                 return;
4062
4063         ir_node *block = get_label_block(statement->label);
4064         ir_node *jmp   = new_Jmp();
4065         add_immBlock_pred(block, jmp);
4066
4067         set_cur_block(NULL);
4068 }
4069
4070 typedef enum modifier_t {
4071         ASM_MODIFIER_WRITE_ONLY   = 1 << 0,
4072         ASM_MODIFIER_READ_WRITE   = 1 << 1,
4073         ASM_MODIFIER_COMMUTATIVE  = 1 << 2,
4074         ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
4075 } modifier_t;
4076
4077 static void asm_statement_to_firm(const asm_statement_t *statement)
4078 {
4079         (void) statement;
4080         fprintf(stderr, "WARNING asm not implemented yet!\n");
4081 #if 0
4082         bool needs_memory = false;
4083
4084         size_t         n_clobbers = 0;
4085         asm_clobber_t *clobber    = statement->clobbers;
4086         for( ; clobber != NULL; clobber = clobber->next) {
4087                 if(strcmp(clobber->clobber, "memory") == 0) {
4088                         needs_memory = true;
4089                         continue;
4090                 }
4091
4092                 ident *id = new_id_from_str(clobber->clobber);
4093                 obstack_ptr_grow(&asm_obst, id);
4094                 ++n_clobbers;
4095         }
4096         assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4097         ident **clobbers = NULL;
4098         if(n_clobbers > 0) {
4099                 clobbers = obstack_finish(&asm_obst);
4100         }
4101
4102         /* find and count input and output constraints */
4103         asm_constraint_t *constraint = statement->inputs;
4104         for( ; constraint != NULL; constraint = constraint->next) {
4105                 int  modifiers      = 0;
4106                 bool supports_memop = false;
4107                 for(const char *c = constraint->constraints; *c != 0; ++c) {
4108                         /* TODO: improve error messages */
4109                         switch(*c) {
4110                         case '?':
4111                         case '!':
4112                                 panic("multiple alternative assembler constraints not "
4113                                       "supported");
4114                         case 'm':
4115                         case 'o':
4116                         case 'V':
4117                         case '<':
4118                         case '>':
4119                         case 'X':
4120                                 supports_memop = true;
4121                                 obstack_1grow(&asm_obst, *c);
4122                                 break;
4123                         case '=':
4124                                 if(modifiers & ASM_MODIFIER_READ_WRITE)
4125                                         panic("inconsistent register constraints");
4126                                 modifiers |= ASM_MODIFIER_WRITE_ONLY;
4127                                 break;
4128                         case '+':
4129                                 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
4130                                         panic("inconsistent register constraints");
4131                                 modifiers |= ASM_MODIFIER_READ_WRITE;
4132                                 break;
4133                         case '&':
4134                                 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
4135                                 panic("early clobber assembler constraint not supported yet");
4136                                 break;
4137                         case '%':
4138                                 modifiers |= ASM_MODIFIER_COMMUTATIVE;
4139                                 panic("commutative assembler constraint not supported yet");
4140                                 break;
4141                         case '#':
4142                                 /* skip register preferences stuff... */
4143                                 while(*c != 0 && *c != ',')
4144                                         ++c;
4145                                 break;
4146                         case '*':
4147                                 /* skip register preferences stuff... */
4148                                 ++c;
4149                                 break;
4150                         default:
4151                                 obstack_1grow(&asm_obst, *c);
4152                                 break;
4153                         }
4154                 }
4155                 obstack_1grow(&asm_obst, '\0');
4156                 const char *constraint_string = obstack_finish(&asm_obst);
4157
4158                 needs_memory |= supports_memop;
4159                 if(supports_memop) {
4160
4161                 }
4162         }
4163 #endif
4164 }
4165
4166 static void statement_to_firm(statement_t *statement)
4167 {
4168         switch(statement->kind) {
4169         case STATEMENT_INVALID:
4170                 panic("invalid statement found");
4171         case STATEMENT_COMPOUND:
4172                 compound_statement_to_firm(&statement->compound);
4173                 return;
4174         case STATEMENT_RETURN:
4175                 return_statement_to_firm(&statement->returns);
4176                 return;
4177         case STATEMENT_EXPRESSION:
4178                 expression_statement_to_firm(&statement->expression);
4179                 return;
4180         case STATEMENT_IF:
4181                 if_statement_to_firm(&statement->ifs);
4182                 return;
4183         case STATEMENT_WHILE:
4184                 while_statement_to_firm(&statement->whiles);
4185                 return;
4186         case STATEMENT_DO_WHILE:
4187                 do_while_statement_to_firm(&statement->do_while);
4188                 return;
4189         case STATEMENT_DECLARATION:
4190                 declaration_statement_to_firm(&statement->declaration);
4191                 return;
4192         case STATEMENT_BREAK:
4193                 create_jump_statement(statement, break_label);
4194                 return;
4195         case STATEMENT_CONTINUE:
4196                 create_jump_statement(statement, continue_label);
4197                 return;
4198         case STATEMENT_SWITCH:
4199                 switch_statement_to_firm(&statement->switchs);
4200                 return;
4201         case STATEMENT_CASE_LABEL:
4202                 case_label_to_firm(&statement->case_label);
4203                 return;
4204         case STATEMENT_FOR:
4205                 for_statement_to_firm(&statement->fors);
4206                 return;
4207         case STATEMENT_LABEL:
4208                 label_to_firm(&statement->label);
4209                 return;
4210         case STATEMENT_GOTO:
4211                 goto_to_firm(&statement->gotos);
4212                 return;
4213         case STATEMENT_ASM:
4214                 asm_statement_to_firm(&statement->asms);
4215                 return;
4216         }
4217         panic("Statement not implemented\n");
4218 }
4219
4220 static int count_decls_in_expression(const expression_t *expression);
4221
4222 static int count_local_declarations(const declaration_t *      decl,
4223                                     const declaration_t *const end)
4224 {
4225         int count = 0;
4226         for (; decl != end; decl = decl->next) {
4227                 if(decl->namespc != NAMESPACE_NORMAL)
4228                         continue;
4229                 const type_t *type = skip_typeref(decl->type);
4230                 if (!decl->address_taken && is_type_scalar(type))
4231                         ++count;
4232                 const initializer_t *initializer = decl->init.initializer;
4233                 /* FIXME: should walk initializer hierarchies... */
4234                 if(initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4235                         count += count_decls_in_expression(initializer->value.value);
4236                 }
4237         }
4238         return count;
4239 }
4240
4241 static int count_decls_in_expression(const expression_t *expression) {
4242         if(expression == NULL)
4243                 return 0;
4244
4245         switch(expression->base.kind) {
4246         case EXPR_STATEMENT:
4247                 return count_decls_in_stmts(expression->statement.statement);
4248         EXPR_BINARY_CASES {
4249                 int count_left  = count_decls_in_expression(expression->binary.left);
4250                 int count_right = count_decls_in_expression(expression->binary.right);
4251                 return count_left + count_right;
4252         }
4253         EXPR_UNARY_CASES
4254                 return count_decls_in_expression(expression->unary.value);
4255         case EXPR_CALL: {
4256                 int count = 0;
4257                 call_argument_t *argument = expression->call.arguments;
4258                 for( ; argument != NULL; argument = argument->next) {
4259                         count += count_decls_in_expression(argument->expression);
4260                 }
4261                 return count;
4262         }
4263
4264         default:
4265                 break;
4266         }
4267
4268         /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
4269          * (or implement all the missing expressions here/implement a walker)
4270          */
4271
4272         return 0;
4273 }
4274
4275 static int count_decls_in_stmts(const statement_t *stmt)
4276 {
4277         int count = 0;
4278         for (; stmt != NULL; stmt = stmt->base.next) {
4279                 switch (stmt->kind) {
4280                         case STATEMENT_DECLARATION: {
4281                                 const declaration_statement_t *const decl_stmt = &stmt->declaration;
4282                                 count += count_local_declarations(decl_stmt->declarations_begin,
4283                                                                   decl_stmt->declarations_end->next);
4284                                 break;
4285                         }
4286
4287                         case STATEMENT_COMPOUND: {
4288                                 const compound_statement_t *const comp =
4289                                         &stmt->compound;
4290                                 count += count_decls_in_stmts(comp->statements);
4291                                 break;
4292                         }
4293
4294                         case STATEMENT_IF: {
4295                                 const if_statement_t *const if_stmt = &stmt->ifs;
4296                                 count += count_decls_in_expression(if_stmt->condition);
4297                                 count += count_decls_in_stmts(if_stmt->true_statement);
4298                                 count += count_decls_in_stmts(if_stmt->false_statement);
4299                                 break;
4300                         }
4301
4302                         case STATEMENT_SWITCH: {
4303                                 const switch_statement_t *const switch_stmt = &stmt->switchs;
4304                                 count += count_decls_in_expression(switch_stmt->expression);
4305                                 count += count_decls_in_stmts(switch_stmt->body);
4306                                 break;
4307                         }
4308
4309                         case STATEMENT_LABEL: {
4310                                 const label_statement_t *const label_stmt = &stmt->label;
4311                                 if(label_stmt->statement != NULL) {
4312                                         count += count_decls_in_stmts(label_stmt->statement);
4313                                 }
4314                                 break;
4315                         }
4316
4317                         case STATEMENT_WHILE: {
4318                                 const while_statement_t *const while_stmt = &stmt->whiles;
4319                                 count += count_decls_in_expression(while_stmt->condition);
4320                                 count += count_decls_in_stmts(while_stmt->body);
4321                                 break;
4322                         }
4323
4324                         case STATEMENT_DO_WHILE: {
4325                                 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
4326                                 count += count_decls_in_expression(do_while_stmt->condition);
4327                                 count += count_decls_in_stmts(do_while_stmt->body);
4328                                 break;
4329                         }
4330
4331                         case STATEMENT_FOR: {
4332                                 const for_statement_t *const for_stmt = &stmt->fors;
4333                                 count += count_local_declarations(for_stmt->scope.declarations, NULL);
4334                                 count += count_decls_in_expression(for_stmt->initialisation);
4335                                 count += count_decls_in_expression(for_stmt->condition);
4336                                 count += count_decls_in_expression(for_stmt->step);
4337                                 count += count_decls_in_stmts(for_stmt->body);
4338                                 break;
4339                         }
4340
4341                         case STATEMENT_CASE_LABEL: {
4342                                 const case_label_statement_t *label = &stmt->case_label;
4343                                 count += count_decls_in_expression(label->expression);
4344                                 if(label->statement != NULL) {
4345                                         count += count_decls_in_stmts(label->statement);
4346                                 }
4347                                 break;
4348                         }
4349
4350                         case STATEMENT_ASM:
4351                         case STATEMENT_BREAK:
4352                         case STATEMENT_CONTINUE:
4353                                 break;
4354
4355                         case STATEMENT_EXPRESSION: {
4356                                 const expression_statement_t *expr_stmt = &stmt->expression;
4357                                 count += count_decls_in_expression(expr_stmt->expression);
4358                                 break;
4359                         }
4360
4361                         case STATEMENT_GOTO:
4362                         case STATEMENT_INVALID:
4363                                 break;
4364
4365                         case STATEMENT_RETURN: {
4366                                 const return_statement_t *ret_stmt = &stmt->returns;
4367                                 count += count_decls_in_expression(ret_stmt->value);
4368                                 break;
4369                         }
4370                 }
4371         }
4372         return count;
4373 }
4374
4375 static int get_function_n_local_vars(declaration_t *declaration)
4376 {
4377         int count = 0;
4378
4379         /* count parameters */
4380         count += count_local_declarations(declaration->scope.declarations, NULL);
4381
4382         /* count local variables declared in body */
4383         count += count_decls_in_stmts(declaration->init.statement);
4384
4385         return count;
4386 }
4387
4388 static void initialize_function_parameters(declaration_t *declaration)
4389 {
4390         ir_graph        *irg             = current_ir_graph;
4391         ir_node         *args            = get_irg_args(irg);
4392         ir_node         *start_block     = get_irg_start_block(irg);
4393         ir_type         *function_irtype = get_ir_type(declaration->type);
4394
4395         int            n         = 0;
4396         declaration_t *parameter = declaration->scope.declarations;
4397         for( ; parameter != NULL; parameter = parameter->next, ++n) {
4398                 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
4399                 type_t *type = skip_typeref(parameter->type);
4400
4401                 bool needs_entity = parameter->address_taken;
4402                 assert(!is_type_array(type));
4403                 if(is_type_compound(type)) {
4404                         needs_entity = true;
4405                 }
4406
4407                 if(needs_entity) {
4408                         ir_entity *entity = get_method_value_param_ent(function_irtype, n);
4409                         ident     *id     = new_id_from_str(parameter->symbol->string);
4410                         set_entity_ident(entity, id);
4411
4412                         parameter->declaration_kind
4413                                 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
4414                         parameter->v.entity = entity;
4415                         continue;
4416                 }
4417
4418                 ir_mode *mode = get_ir_mode(parameter->type);
4419                 long     pn   = n;
4420                 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
4421
4422                 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
4423                 parameter->v.value_number   = next_value_number_function;
4424                 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
4425                 ++next_value_number_function;
4426
4427                 set_value(parameter->v.value_number, proj);
4428         }
4429 }
4430
4431 /**
4432  * Handle additional decl modifiers for IR-graphs
4433  *
4434  * @param irg            the IR-graph
4435  * @param dec_modifiers  additional modifiers
4436  */
4437 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
4438 {
4439         if (decl_modifiers & DM_NORETURN) {
4440                 /* TRUE if the declaration includes the Microsoft
4441                    __declspec(noreturn) specifier. */
4442                 set_irg_additional_property(irg, mtp_property_noreturn);
4443         }
4444         if (decl_modifiers & DM_NOTHROW) {
4445                 /* TRUE if the declaration includes the Microsoft
4446                    __declspec(nothrow) specifier. */
4447                 set_irg_additional_property(irg, mtp_property_nothrow);
4448         }
4449         if (decl_modifiers & DM_NAKED) {
4450                 /* TRUE if the declaration includes the Microsoft
4451                    __declspec(naked) specifier. */
4452                 set_irg_additional_property(irg, mtp_property_naked);
4453         }
4454         if (decl_modifiers & DM_FORCEINLINE) {
4455                 /* TRUE if the declaration includes the
4456                    Microsoft __forceinline specifier. */
4457                 set_irg_inline_property(irg, irg_inline_forced);
4458         }
4459         if (decl_modifiers & DM_NOINLINE) {
4460                 /* TRUE if the declaration includes the Microsoft
4461                    __declspec(noinline) specifier. */
4462                 set_irg_inline_property(irg, irg_inline_forbidden);
4463         }
4464 }
4465
4466 static void create_function(declaration_t *declaration)
4467 {
4468         ir_entity *function_entity = get_function_entity(declaration);
4469
4470         if(declaration->init.statement == NULL)
4471                 return;
4472
4473         current_function_decl = declaration;
4474         current_function_name = NULL;
4475
4476         assert(imature_blocks == NULL);
4477         imature_blocks = NEW_ARR_F(ir_node*, 0);
4478
4479         int       n_local_vars = get_function_n_local_vars(declaration);
4480         ir_graph *irg          = new_ir_graph(function_entity, n_local_vars);
4481         ir_node  *first_block  = get_cur_block();
4482
4483         /* set inline flags */
4484         if (declaration->is_inline)
4485         set_irg_inline_property(irg, irg_inline_recomended);
4486     handle_decl_modifier_irg(irg, declaration->modifiers);
4487
4488         next_value_number_function = 0;
4489         initialize_function_parameters(declaration);
4490
4491         statement_to_firm(declaration->init.statement);
4492
4493         ir_node *end_block = get_irg_end_block(irg);
4494
4495         /* do we have a return statement yet? */
4496         if(get_cur_block() != NULL) {
4497                 type_t *type = skip_typeref(declaration->type);
4498                 assert(is_type_function(type));
4499                 const function_type_t *func_type   = &type->function;
4500                 const type_t          *return_type
4501                         = skip_typeref(func_type->return_type);
4502
4503                 ir_node *ret;
4504                 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4505                         ret = new_Return(get_store(), 0, NULL);
4506                 } else {
4507                         ir_mode *mode;
4508                         if(is_type_scalar(return_type)) {
4509                                 mode = get_ir_mode(func_type->return_type);
4510                         } else {
4511                                 mode = mode_P_data;
4512                         }
4513
4514                         ir_node *in[1];
4515                         /* ยง5.1.2.2.3 main implicitly returns 0 */
4516                         if (strcmp(declaration->symbol->string, "main") == 0) {
4517                                 in[0] = new_Const(mode, get_mode_null(mode));
4518                         } else {
4519                                 in[0] = new_Unknown(mode);
4520                         }
4521                         ret = new_Return(get_store(), 1, in);
4522                 }
4523                 add_immBlock_pred(end_block, ret);
4524         }
4525
4526         for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
4527                 mature_immBlock(imature_blocks[i]);
4528         }
4529         DEL_ARR_F(imature_blocks);
4530         imature_blocks = NULL;
4531
4532         mature_immBlock(first_block);
4533         mature_immBlock(end_block);
4534
4535         irg_finalize_cons(irg);
4536
4537         /* finalize the frame type */
4538         ir_type *frame_type = get_irg_frame_type(irg);
4539         int      n          = get_compound_n_members(frame_type);
4540         int      align_all  = 4;
4541         int      offset     = 0;
4542         for(int i = 0; i < n; ++i) {
4543                 ir_entity *entity      = get_compound_member(frame_type, i);
4544                 ir_type   *entity_type = get_entity_type(entity);
4545
4546                 int align = get_type_alignment_bytes(entity_type);
4547                 if(align > align_all)
4548                         align_all = align;
4549                 int misalign = 0;
4550                 if(align > 0) {
4551                         misalign  = offset % align;
4552                         if(misalign > 0) {
4553                                 offset += align - misalign;
4554                         }
4555                 }
4556
4557                 set_entity_offset(entity, offset);
4558                 offset += get_type_size_bytes(entity_type);
4559         }
4560         set_type_size_bytes(frame_type, offset);
4561         set_type_alignment_bytes(frame_type, align_all);
4562         set_type_state(frame_type, layout_fixed);
4563
4564         irg_vrfy(irg);
4565 }
4566
4567 static void scope_to_firm(scope_t *scope)
4568 {
4569         /* first pass: create declarations */
4570         declaration_t *declaration = scope->declarations;
4571         for( ; declaration != NULL; declaration = declaration->next) {
4572                 if(declaration->namespc != NAMESPACE_NORMAL)
4573                         continue;
4574                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4575                                 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4576                         continue;
4577                 if(declaration->symbol == NULL)
4578                         continue;
4579
4580                 type_t *type = skip_typeref(declaration->type);
4581                 if(is_type_function(type)) {
4582                         get_function_entity(declaration);
4583                 } else {
4584                         create_global_variable(declaration);
4585                 }
4586         }
4587
4588         /* second pass: create code/initializers */
4589         declaration = scope->declarations;
4590         for( ; declaration != NULL; declaration = declaration->next) {
4591                 if(declaration->namespc != NAMESPACE_NORMAL)
4592                         continue;
4593                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4594                                 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4595                         continue;
4596                 if(declaration->symbol == NULL)
4597                         continue;
4598
4599                 type_t *type = declaration->type;
4600                 if(type->kind == TYPE_FUNCTION) {
4601                         create_function(declaration);
4602                 } else {
4603                         assert(declaration->declaration_kind
4604                                         == DECLARATION_KIND_GLOBAL_VARIABLE);
4605                         current_ir_graph = get_const_code_irg();
4606                         create_declaration_initializer(declaration);
4607                 }
4608         }
4609 }
4610
4611 void init_ast2firm(void)
4612 {
4613         obstack_init(&asm_obst);
4614         init_atomic_modes();
4615
4616         /* create idents for all known runtime functions */
4617         for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
4618                 predef_idents[rts_data[i].id] = new_id_from_str(rts_data[i].name);
4619         }
4620 }
4621
4622 static void init_ir_types(void)
4623 {
4624         static int ir_types_initialized = 0;
4625         if(ir_types_initialized)
4626                 return;
4627         ir_types_initialized = 1;
4628
4629         type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4630         type_void       = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4631         type_int        = make_atomic_type(ATOMIC_TYPE_INT,  TYPE_QUALIFIER_NONE);
4632
4633         ir_type_int        = get_ir_type(type_int);
4634         ir_type_const_char = get_ir_type(type_const_char);
4635         ir_type_wchar_t    = get_ir_type(type_wchar_t);
4636         ir_type_void       = get_ir_type(type_int); /* we don't have a real void
4637                                                        type in firm */
4638
4639         type_void->base.firm_type = ir_type_void;
4640 }
4641
4642 void exit_ast2firm(void)
4643 {
4644         obstack_free(&asm_obst, NULL);
4645 }
4646
4647 void translation_unit_to_firm(translation_unit_t *unit)
4648 {
4649         /* just to be sure */
4650         continue_label      = NULL;
4651         break_label         = NULL;
4652         current_switch_cond = NULL;
4653
4654         init_ir_types();
4655
4656         scope_to_firm(&unit->scope);
4657 }