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