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