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