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