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