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