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