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