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