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