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