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