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