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