a205b2722a7bedcdb8255a6f37daa4564c333ebc
[cparser] / ast2firm.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 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 #include <limits.h>
26
27 #include <libfirm/firm.h>
28 #include <libfirm/adt/obst.h>
29 #include <libfirm/be.h>
30
31 #include "ast2firm.h"
32
33 #include "adt/error.h"
34 #include "adt/array.h"
35 #include "adt/util.h"
36 #include "symbol_t.h"
37 #include "token_t.h"
38 #include "type_t.h"
39 #include "ast_t.h"
40 #include "entity_t.h"
41 #include "parser.h"
42 #include "diagnostic.h"
43 #include "lang_features.h"
44 #include "types.h"
45 #include "type_hash.h"
46 #include "mangle.h"
47 #include "walk_statements.h"
48 #include "warning.h"
49 #include "printer.h"
50 #include "entitymap_t.h"
51 #include "driver/firm_opt.h"
52 #include "driver/firm_cmdline.h"
53
54 typedef struct trampoline_region trampoline_region;
55 struct trampoline_region {
56         ir_entity        *function;    /**< The function that is called by this trampoline */
57         ir_entity        *region;      /**< created region for the trampoline */
58 };
59
60 static const backend_params *be_params;
61
62 static ir_type *ir_type_char;
63 static ir_type *ir_type_const_char;
64 static ir_type *ir_type_wchar_t;
65 static ir_type *ir_type_void;
66 static ir_type *ir_type_int;
67
68 /* architecture specific floating point arithmetic mode (if any) */
69 static ir_mode *mode_float_arithmetic;
70
71 /* alignment of stack parameters */
72 static unsigned stack_param_align;
73
74 static int        next_value_number_function;
75 static ir_node   *continue_label;
76 static ir_node   *break_label;
77 static ir_node   *current_switch_cond;
78 static bool       saw_default_label;
79 static label_t  **all_labels;
80 static entity_t **inner_functions;
81 static ir_node   *ijmp_list;
82 static bool       constant_folding;
83 static bool       initializer_use_bitfield_basetype;
84
85 extern bool       have_const_functions;
86
87 static const entity_t     *current_function_entity;
88 static ir_node            *current_function_name;
89 static ir_node            *current_funcsig;
90 static switch_statement_t *current_switch;
91 static ir_graph           *current_function;
92 static translation_unit_t *current_translation_unit;
93 static trampoline_region  *current_trampolines;
94 static ir_type            *current_outer_frame;
95 static ir_type            *current_outer_value_type;
96 static ir_node            *current_static_link;
97
98 static entitymap_t  entitymap;
99
100 static struct obstack asm_obst;
101
102 typedef enum declaration_kind_t {
103         DECLARATION_KIND_UNKNOWN,
104         DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
105         DECLARATION_KIND_GLOBAL_VARIABLE,
106         DECLARATION_KIND_LOCAL_VARIABLE,
107         DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
108         DECLARATION_KIND_PARAMETER,
109         DECLARATION_KIND_PARAMETER_ENTITY,
110         DECLARATION_KIND_FUNCTION,
111         DECLARATION_KIND_COMPOUND_MEMBER,
112         DECLARATION_KIND_INNER_FUNCTION
113 } declaration_kind_t;
114
115 static ir_mode *get_ir_mode_storage(type_t *type);
116 /*
117  * get arithmetic mode for a type. This is different from get_ir_mode_storage,
118  * int that it returns bigger modes for floating point on some platforms
119  * (x87 internally does arithemtic with 80bits)
120  */
121 static ir_mode *get_ir_mode_arithmetic(type_t *type);
122
123 static ir_type *get_ir_type_incomplete(type_t *type);
124
125 static void enqueue_inner_function(entity_t *entity)
126 {
127         if (inner_functions == NULL)
128                 inner_functions = NEW_ARR_F(entity_t *, 0);
129         ARR_APP1(entity_t*, inner_functions, entity);
130 }
131
132 static ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
133 {
134         const entity_t *entity = get_irg_loc_description(irg, pos);
135
136         if (entity != NULL) {
137                 warningf(&entity->base.source_position,
138                          "%s '%#T' might be used uninitialized",
139                          get_entity_kind_name(entity->kind),
140                          entity->declaration.type, entity->base.symbol);
141         }
142         return new_r_Unknown(irg, mode);
143 }
144
145 static const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
146 {
147         const source_position_t *pos = (const source_position_t*) dbg;
148         if (pos == NULL)
149                 return NULL;
150         if (line != NULL)
151                 *line = pos->linenr;
152         return pos->input_name;
153 }
154
155 static dbg_info *get_dbg_info(const source_position_t *pos)
156 {
157         return (dbg_info*) pos;
158 }
159
160 static void dbg_print_type_dbg_info(char *buffer, size_t buffer_size,
161                                     const type_dbg_info *dbg)
162 {
163         assert(dbg != NULL);
164         print_to_buffer(buffer, buffer_size);
165         const type_t *type = (const type_t*) dbg;
166         print_type(type);
167         finish_print_to_buffer();
168 }
169
170 static type_dbg_info *get_type_dbg_info_(const type_t *type)
171 {
172         return (type_dbg_info*) type;
173 }
174
175 static ir_mode *atomic_modes[ATOMIC_TYPE_LAST+1];
176
177 static ir_mode *mode_int, *mode_uint;
178
179 static ir_node *_expression_to_firm(const expression_t *expression);
180 static ir_node *expression_to_firm(const expression_t *expression);
181 static void create_local_declaration(entity_t *entity);
182
183 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
184 {
185         unsigned flags = get_atomic_type_flags(kind);
186         unsigned size  = get_atomic_type_size(kind);
187         if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
188                         && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
189                 char            name[64];
190                 ir_mode_sort    sort;
191                 unsigned        bit_size     = size * 8;
192                 bool            is_signed    = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
193                 unsigned        modulo_shift;
194                 ir_mode_arithmetic arithmetic;
195
196                 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
197                         assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
198                         snprintf(name, sizeof(name), "%s%u", is_signed ? "I" : "U",
199                                  bit_size);
200                         sort         = irms_int_number;
201                         arithmetic   = irma_twos_complement;
202                         modulo_shift = bit_size < machine_size ? machine_size : bit_size;
203                 } else {
204                         assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
205                         snprintf(name, sizeof(name), "F%u", bit_size);
206                         sort         = irms_float_number;
207                         arithmetic   = irma_ieee754;
208                         modulo_shift = 0;
209                 }
210                 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
211                                    modulo_shift);
212         }
213
214         return NULL;
215 }
216
217 /**
218  * Initialises the atomic modes depending on the machine size.
219  */
220 static void init_atomic_modes(void)
221 {
222         for (int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
223                 atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
224         }
225         mode_int  = atomic_modes[ATOMIC_TYPE_INT];
226         mode_uint = atomic_modes[ATOMIC_TYPE_UINT];
227
228         /* there's no real void type in firm */
229         atomic_modes[ATOMIC_TYPE_VOID] = atomic_modes[ATOMIC_TYPE_CHAR];
230
231         /* initialize pointer modes */
232         char            name[64];
233         ir_mode_sort    sort         = irms_reference;
234         unsigned        bit_size     = machine_size;
235         bool            is_signed    = 0;
236         ir_mode_arithmetic arithmetic   = irma_twos_complement;
237         unsigned        modulo_shift
238                 = bit_size < machine_size ? machine_size : bit_size;
239
240         snprintf(name, sizeof(name), "p%u", machine_size);
241         ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
242                                         modulo_shift);
243
244         set_reference_mode_signed_eq(ptr_mode, atomic_modes[get_intptr_kind()]);
245         set_reference_mode_unsigned_eq(ptr_mode, atomic_modes[get_uintptr_kind()]);
246
247         /* Hmm, pointers should be machine size */
248         set_modeP_data(ptr_mode);
249         set_modeP_code(ptr_mode);
250 }
251
252 ir_mode *get_atomic_mode(atomic_type_kind_t kind)
253 {
254         assert(kind <= ATOMIC_TYPE_LAST);
255         return atomic_modes[kind];
256 }
257
258 static ir_node *get_vla_size(array_type_t *const type)
259 {
260         ir_node *size_node = type->size_node;
261         if (size_node == NULL) {
262                 size_node = expression_to_firm(type->size_expression);
263                 type->size_node = size_node;
264         }
265         return size_node;
266 }
267
268 /**
269  * Return a node representing the size of a type.
270  */
271 static ir_node *get_type_size_node(type_t *type)
272 {
273         type = skip_typeref(type);
274
275         if (is_type_array(type) && type->array.is_vla) {
276                 ir_node *size_node = get_vla_size(&type->array);
277                 ir_node *elem_size = get_type_size_node(type->array.element_type);
278                 ir_mode *mode      = get_irn_mode(size_node);
279                 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
280                 return real_size;
281         }
282
283         ir_mode *mode = get_ir_mode_storage(type_size_t);
284         symconst_symbol sym;
285         sym.type_p = get_ir_type(type);
286         return new_SymConst(mode, sym, symconst_type_size);
287 }
288
289 static unsigned count_parameters(const function_type_t *function_type)
290 {
291         unsigned count = 0;
292
293         function_parameter_t *parameter = function_type->parameters;
294         for ( ; parameter != NULL; parameter = parameter->next) {
295                 ++count;
296         }
297
298         return count;
299 }
300
301 /**
302  * Creates a Firm type for an atomic type
303  */
304 static ir_type *create_atomic_type(atomic_type_kind_t akind, const type_t *type)
305 {
306         ir_mode        *mode      = atomic_modes[akind];
307         type_dbg_info  *dbgi      = get_type_dbg_info_(type);
308         ir_type        *irtype    = new_d_type_primitive(mode, dbgi);
309         il_alignment_t  alignment = get_atomic_type_alignment(akind);
310
311         set_type_alignment_bytes(irtype, alignment);
312
313         return irtype;
314 }
315
316 /**
317  * Creates a Firm type for a complex type
318  */
319 static ir_type *create_complex_type(const complex_type_t *type)
320 {
321         atomic_type_kind_t  kind = type->akind;
322         ir_mode            *mode = atomic_modes[kind];
323         ident              *id   = get_mode_ident(mode);
324
325         (void) id;
326
327         /* FIXME: finish the array */
328         return NULL;
329 }
330
331 /**
332  * Creates a Firm type for an imaginary type
333  */
334 static ir_type *create_imaginary_type(imaginary_type_t *type)
335 {
336         return create_atomic_type(type->akind, (const type_t*) type);
337 }
338
339 /**
340  * return type of a parameter (and take transparent union gnu extension into
341  * account)
342  */
343 static type_t *get_parameter_type(type_t *orig_type)
344 {
345         type_t *type = skip_typeref(orig_type);
346         if (is_type_union(type)
347                         && get_type_modifiers(orig_type) & DM_TRANSPARENT_UNION) {
348                 compound_t *compound = type->compound.compound;
349                 type                 = compound->members.entities->declaration.type;
350         }
351
352         return type;
353 }
354
355 static ir_type *create_method_type(const function_type_t *function_type, bool for_closure)
356 {
357         type_t        *return_type  = skip_typeref(function_type->return_type);
358
359         int            n_parameters = count_parameters(function_type)
360                                        + (for_closure ? 1 : 0);
361         int            n_results    = return_type == type_void ? 0 : 1;
362         type_dbg_info *dbgi         = get_type_dbg_info_((const type_t*) function_type);
363         ir_type       *irtype       = new_d_type_method(n_parameters, n_results, dbgi);
364
365         if (return_type != type_void) {
366                 ir_type *restype = get_ir_type(return_type);
367                 set_method_res_type(irtype, 0, restype);
368         }
369
370         function_parameter_t *parameter = function_type->parameters;
371         int                   n         = 0;
372         if (for_closure) {
373                 ir_type *p_irtype = get_ir_type(type_void_ptr);
374                 set_method_param_type(irtype, n, p_irtype);
375                 ++n;
376         }
377         for ( ; parameter != NULL; parameter = parameter->next) {
378                 type_t  *type     = get_parameter_type(parameter->type);
379                 ir_type *p_irtype = get_ir_type(type);
380                 set_method_param_type(irtype, n, p_irtype);
381                 ++n;
382         }
383
384         bool is_variadic = function_type->variadic;
385
386         if (is_variadic)
387                 set_method_variadicity(irtype, variadicity_variadic);
388
389         unsigned cc = get_method_calling_convention(irtype);
390         switch (function_type->calling_convention) {
391         case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typically cdecl */
392         case CC_CDECL:
393 is_cdecl:
394                 set_method_calling_convention(irtype, SET_CDECL(cc));
395                 break;
396
397         case CC_STDCALL:
398                 if (is_variadic)
399                         goto is_cdecl;
400
401                 /* only non-variadic function can use stdcall, else use cdecl */
402                 set_method_calling_convention(irtype, SET_STDCALL(cc));
403                 break;
404
405         case CC_FASTCALL:
406                 if (is_variadic)
407                         goto is_cdecl;
408                 /* only non-variadic function can use fastcall, else use cdecl */
409                 set_method_calling_convention(irtype, SET_FASTCALL(cc));
410                 break;
411
412         case CC_THISCALL:
413                 /* Hmm, leave default, not accepted by the parser yet. */
414                 break;
415         }
416
417         if (for_closure)
418                 set_method_calling_convention(irtype, get_method_calling_convention(irtype) | cc_this_call);
419
420         return irtype;
421 }
422
423 static ir_type *create_pointer_type(pointer_type_t *type)
424 {
425         type_dbg_info *dbgi         = get_type_dbg_info_((const type_t*) type);
426         type_t        *points_to    = type->points_to;
427         ir_type       *ir_points_to = get_ir_type_incomplete(points_to);
428         ir_type       *ir_type      = new_d_type_pointer(ir_points_to, dbgi);
429
430         return ir_type;
431 }
432
433 static ir_type *create_reference_type(reference_type_t *type)
434 {
435         type_dbg_info *dbgi         = get_type_dbg_info_((const type_t*) type);
436         type_t        *refers_to    = type->refers_to;
437         ir_type       *ir_refers_to = get_ir_type_incomplete(refers_to);
438         ir_type       *ir_type      = new_d_type_pointer(ir_refers_to, dbgi);
439
440         return ir_type;
441 }
442
443 static ir_type *create_array_type(array_type_t *type)
444 {
445         type_dbg_info *dbgi            = get_type_dbg_info_((const type_t*) type);
446         type_t        *element_type    = type->element_type;
447         ir_type       *ir_element_type = get_ir_type(element_type);
448         ir_type       *ir_type         = new_d_type_array(1, ir_element_type, dbgi);
449
450         const int align = get_type_alignment_bytes(ir_element_type);
451         set_type_alignment_bytes(ir_type, align);
452
453         if (type->size_constant) {
454                 int n_elements = type->size;
455
456                 set_array_bounds_int(ir_type, 0, 0, n_elements);
457
458                 size_t elemsize = get_type_size_bytes(ir_element_type);
459                 if (elemsize % align > 0) {
460                         elemsize += align - (elemsize % align);
461                 }
462                 set_type_size_bytes(ir_type, n_elements * elemsize);
463         } else {
464                 set_array_lower_bound_int(ir_type, 0, 0);
465         }
466         set_type_state(ir_type, layout_fixed);
467
468         return ir_type;
469 }
470
471 /**
472  * Return the signed integer type of size bits.
473  *
474  * @param size   the size
475  */
476 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
477                                                  unsigned size,
478                                                                                                  const type_t *type)
479 {
480         static ir_mode *s_modes[64 + 1] = {NULL, };
481         ir_type *res;
482         ir_mode *mode;
483
484         if (size <= 0 || size > 64)
485                 return NULL;
486
487         mode = s_modes[size];
488         if (mode == NULL) {
489                 char name[32];
490
491                 snprintf(name, sizeof(name), "bf_I%u", size);
492                 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
493                                    size <= 32 ? 32 : size );
494                 s_modes[size] = mode;
495         }
496
497         type_dbg_info *dbgi = get_type_dbg_info_(type);
498         res                 = new_d_type_primitive(mode, dbgi);
499         set_primitive_base_type(res, base_tp);
500
501         return res;
502 }
503
504 /**
505  * Return the unsigned integer type of size bits.
506  *
507  * @param size   the size
508  */
509 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
510                                                    unsigned size,
511                                                                                                    const type_t *type)
512 {
513         static ir_mode *u_modes[64 + 1] = {NULL, };
514         ir_type *res;
515         ir_mode *mode;
516
517         if (size <= 0 || size > 64)
518                 return NULL;
519
520         mode = u_modes[size];
521         if (mode == NULL) {
522                 char name[32];
523
524                 snprintf(name, sizeof(name), "bf_U%u", size);
525                 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
526                                    size <= 32 ? 32 : size );
527                 u_modes[size] = mode;
528         }
529
530         type_dbg_info *dbgi = get_type_dbg_info_(type);
531         res = new_d_type_primitive(mode, dbgi);
532         set_primitive_base_type(res, base_tp);
533
534         return res;
535 }
536
537 static ir_type *create_bitfield_type(bitfield_type_t *const type)
538 {
539         type_t *base = skip_typeref(type->base_type);
540         assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
541         ir_type *irbase = get_ir_type(base);
542
543         unsigned size = type->bit_size;
544
545         assert(!is_type_float(base));
546         if (is_type_signed(base)) {
547                 return get_signed_int_type_for_bit_size(irbase, size,
548                                                         (const type_t*) type);
549         } else {
550                 return get_unsigned_int_type_for_bit_size(irbase, size,
551                                                           (const type_t*) type);
552         }
553 }
554
555 #define INVALID_TYPE ((ir_type_ptr)-1)
556
557 enum {
558         COMPOUND_IS_STRUCT = false,
559         COMPOUND_IS_UNION  = true
560 };
561
562 /**
563  * Construct firm type from ast struct type.
564  */
565 static ir_type *create_compound_type(compound_type_t *type,
566                                      bool incomplete, bool is_union)
567 {
568         compound_t *compound = type->compound;
569
570         if (compound->irtype != NULL && (compound->irtype_complete || incomplete)) {
571                 return compound->irtype;
572         }
573
574         symbol_t *symbol = compound->base.symbol;
575         ident    *id;
576         if (symbol != NULL) {
577                 id = new_id_from_str(symbol->string);
578         } else {
579                 if (is_union) {
580                         id = id_unique("__anonymous_union.%u");
581                 } else {
582                         id = id_unique("__anonymous_struct.%u");
583                 }
584         }
585
586         ir_type *irtype;
587         if (is_union) {
588                 irtype = new_type_union(id);
589         } else {
590                 irtype = new_type_struct(id);
591         }
592
593         compound->irtype_complete = false;
594         compound->irtype          = irtype;
595
596         if (incomplete)
597                 return irtype;
598
599         if (is_union) {
600                 layout_union_type(type);
601         } else {
602                 layout_struct_type(type);
603         }
604
605         compound->irtype_complete = true;
606
607         entity_t *entry = compound->members.entities;
608         for ( ; entry != NULL; entry = entry->base.next) {
609                 if (entry->kind != ENTITY_COMPOUND_MEMBER)
610                         continue;
611
612                 symbol_t *symbol     = entry->base.symbol;
613                 type_t   *entry_type = entry->declaration.type;
614                 ident    *ident;
615                 if (symbol == NULL) {
616                         /* anonymous bitfield member, skip */
617                         if (entry_type->kind == TYPE_BITFIELD)
618                                 continue;
619                         assert(entry_type->kind == TYPE_COMPOUND_STRUCT
620                                         || entry_type->kind == TYPE_COMPOUND_UNION);
621                         ident = id_unique("anon.%u");
622                 } else {
623                         ident = new_id_from_str(symbol->string);
624                 }
625
626                 dbg_info *dbgi       = get_dbg_info(&entry->base.source_position);
627
628                 ir_type   *entry_irtype = get_ir_type(entry_type);
629                 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
630
631                 set_entity_offset(entity, entry->compound_member.offset);
632                 set_entity_offset_bits_remainder(entity,
633                                                  entry->compound_member.bit_offset);
634
635                 assert(entry->declaration.kind == DECLARATION_KIND_UNKNOWN);
636                 entry->declaration.kind       = DECLARATION_KIND_COMPOUND_MEMBER;
637                 entry->compound_member.entity = entity;
638         }
639
640         set_type_alignment_bytes(irtype, compound->alignment);
641         set_type_size_bytes(irtype, compound->size);
642         set_type_state(irtype, layout_fixed);
643
644         return irtype;
645 }
646
647 static ir_type *create_enum_type(enum_type_t *const type)
648 {
649         type->base.firm_type = ir_type_int;
650
651         ir_mode   *const mode    = mode_int;
652         ir_tarval *const one     = get_mode_one(mode);
653         ir_tarval *      tv_next = get_mode_null(mode);
654
655         bool constant_folding_old = constant_folding;
656         constant_folding = true;
657
658         enum_t   *enume = type->enume;
659         entity_t *entry = enume->base.next;
660         for (; entry != NULL; entry = entry->base.next) {
661                 if (entry->kind != ENTITY_ENUM_VALUE)
662                         break;
663
664                 expression_t *const init = entry->enum_value.value;
665                 if (init != NULL) {
666                         ir_node *const cnst = expression_to_firm(init);
667                         if (!is_Const(cnst)) {
668                                 panic("couldn't fold constant");
669                         }
670                         tv_next = get_Const_tarval(cnst);
671                 }
672                 entry->enum_value.tv = tv_next;
673                 tv_next = tarval_add(tv_next, one);
674         }
675
676         constant_folding = constant_folding_old;
677
678         return create_atomic_type(type->akind, (const type_t*) type);
679 }
680
681 static ir_type *get_ir_type_incomplete(type_t *type)
682 {
683         assert(type != NULL);
684         type = skip_typeref(type);
685
686         if (type->base.firm_type != NULL) {
687                 assert(type->base.firm_type != INVALID_TYPE);
688                 return type->base.firm_type;
689         }
690
691         switch (type->kind) {
692         case TYPE_COMPOUND_STRUCT:
693                 return create_compound_type(&type->compound, true, COMPOUND_IS_STRUCT);
694         case TYPE_COMPOUND_UNION:
695                 return create_compound_type(&type->compound, true, COMPOUND_IS_UNION);
696         default:
697                 return get_ir_type(type);
698         }
699 }
700
701 ir_type *get_ir_type(type_t *type)
702 {
703         assert(type != NULL);
704
705         type = skip_typeref(type);
706
707         if (type->base.firm_type != NULL) {
708                 assert(type->base.firm_type != INVALID_TYPE);
709                 return type->base.firm_type;
710         }
711
712         ir_type *firm_type = NULL;
713         switch (type->kind) {
714         case TYPE_ERROR:
715                 /* Happens while constant folding, when there was an error */
716                 return create_atomic_type(ATOMIC_TYPE_VOID, NULL);
717
718         case TYPE_ATOMIC:
719                 firm_type = create_atomic_type(type->atomic.akind, type);
720                 break;
721         case TYPE_COMPLEX:
722                 firm_type = create_complex_type(&type->complex);
723                 break;
724         case TYPE_IMAGINARY:
725                 firm_type = create_imaginary_type(&type->imaginary);
726                 break;
727         case TYPE_FUNCTION:
728                 firm_type = create_method_type(&type->function, false);
729                 break;
730         case TYPE_POINTER:
731                 firm_type = create_pointer_type(&type->pointer);
732                 break;
733         case TYPE_REFERENCE:
734                 firm_type = create_reference_type(&type->reference);
735                 break;
736         case TYPE_ARRAY:
737                 firm_type = create_array_type(&type->array);
738                 break;
739         case TYPE_COMPOUND_STRUCT:
740                 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_STRUCT);
741                 break;
742         case TYPE_COMPOUND_UNION:
743                 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_UNION);
744                 break;
745         case TYPE_ENUM:
746                 firm_type = create_enum_type(&type->enumt);
747                 break;
748         case TYPE_BUILTIN:
749                 firm_type = get_ir_type(type->builtin.real_type);
750                 break;
751         case TYPE_BITFIELD:
752                 firm_type = create_bitfield_type(&type->bitfield);
753                 break;
754
755         case TYPE_TYPEOF:
756         case TYPE_TYPEDEF:
757         case TYPE_INVALID:
758                 break;
759         }
760         if (firm_type == NULL)
761                 panic("unknown type found");
762
763         type->base.firm_type = firm_type;
764         return firm_type;
765 }
766
767 static ir_mode *get_ir_mode_storage(type_t *type)
768 {
769         ir_type *irtype = get_ir_type(type);
770
771         /* firm doesn't report a mode for arrays somehow... */
772         if (is_Array_type(irtype)) {
773                 return mode_P_data;
774         }
775
776         ir_mode *mode = get_type_mode(irtype);
777         assert(mode != NULL);
778         return mode;
779 }
780
781 static ir_mode *get_ir_mode_arithmetic(type_t *type)
782 {
783         ir_mode *mode = get_ir_mode_storage(type);
784         if (mode_is_float(mode) && mode_float_arithmetic != NULL) {
785                 return mode_float_arithmetic;
786         }
787
788         return mode;
789 }
790
791 /** Names of the runtime functions. */
792 static const struct {
793         int        id;           /**< the rts id */
794         int        n_res;        /**< number of return values */
795         const char *name;        /**< the name of the rts function */
796         int        n_params;     /**< number of parameters */
797         unsigned   flags;        /**< language flags */
798 } rts_data[] = {
799         { rts_debugbreak, 0, "__debugbreak", 0, _MS },
800         { rts_abort,      0, "abort",        0, _C89 },
801         { rts_alloca,     1, "alloca",       1, _ALL },
802         { rts_abs,        1, "abs",          1, _C89 },
803         { rts_labs,       1, "labs",         1, _C89 },
804         { rts_llabs,      1, "llabs",        1, _C99 },
805         { rts_imaxabs,    1, "imaxabs",      1, _C99 },
806
807         { rts_fabs,       1, "fabs",         1, _C89 },
808         { rts_sqrt,       1, "sqrt",         1, _C89 },
809         { rts_cbrt,       1, "cbrt",         1, _C99 },
810         { rts_exp,        1, "exp",          1, _C89 },
811         { rts_exp2,       1, "exp2",         1, _C89 },
812         { rts_exp10,      1, "exp10",        1, _GNUC },
813         { rts_log,        1, "log",          1, _C89 },
814         { rts_log2,       1, "log2",         1, _C89 },
815         { rts_log10,      1, "log10",        1, _C89 },
816         { rts_pow,        1, "pow",          2, _C89 },
817         { rts_sin,        1, "sin",          1, _C89 },
818         { rts_cos,        1, "cos",          1, _C89 },
819         { rts_tan,        1, "tan",          1, _C89 },
820         { rts_asin,       1, "asin",         1, _C89 },
821         { rts_acos,       1, "acos",         1, _C89 },
822         { rts_atan,       1, "atan",         1, _C89 },
823         { rts_sinh,       1, "sinh",         1, _C89 },
824         { rts_cosh,       1, "cosh",         1, _C89 },
825         { rts_tanh,       1, "tanh",         1, _C89 },
826
827         { rts_fabsf,      1, "fabsf",        1, _C99 },
828         { rts_sqrtf,      1, "sqrtf",        1, _C99 },
829         { rts_cbrtf,      1, "cbrtf",        1, _C99 },
830         { rts_expf,       1, "expf",         1, _C99 },
831         { rts_exp2f,      1, "exp2f",        1, _C99 },
832         { rts_exp10f,     1, "exp10f",       1, _GNUC },
833         { rts_logf,       1, "logf",         1, _C99 },
834         { rts_log2f,      1, "log2f",        1, _C99 },
835         { rts_log10f,     1, "log10f",       1, _C99 },
836         { rts_powf,       1, "powf",         2, _C99 },
837         { rts_sinf,       1, "sinf",         1, _C99 },
838         { rts_cosf,       1, "cosf",         1, _C99 },
839         { rts_tanf,       1, "tanf",         1, _C99 },
840         { rts_asinf,      1, "asinf",        1, _C99 },
841         { rts_acosf,      1, "acosf",        1, _C99 },
842         { rts_atanf,      1, "atanf",        1, _C99 },
843         { rts_sinhf,      1, "sinhf",        1, _C99 },
844         { rts_coshf,      1, "coshf",        1, _C99 },
845         { rts_tanhf,      1, "tanhf",        1, _C99 },
846
847         { rts_fabsl,      1, "fabsl",        1, _C99 },
848         { rts_sqrtl,      1, "sqrtl",        1, _C99 },
849         { rts_cbrtl,      1, "cbrtl",        1, _C99 },
850         { rts_expl,       1, "expl",         1, _C99 },
851         { rts_exp2l,      1, "exp2l",        1, _C99 },
852         { rts_exp10l,     1, "exp10l",       1, _GNUC },
853         { rts_logl,       1, "logl",         1, _C99 },
854         { rts_log2l,      1, "log2l",        1, _C99 },
855         { rts_log10l,     1, "log10l",       1, _C99 },
856         { rts_powl,       1, "powl",         2, _C99 },
857         { rts_sinl,       1, "sinl",         1, _C99 },
858         { rts_cosl,       1, "cosl",         1, _C99 },
859         { rts_tanl,       1, "tanl",         1, _C99 },
860         { rts_asinl,      1, "asinl",        1, _C99 },
861         { rts_acosl,      1, "acosl",        1, _C99 },
862         { rts_atanl,      1, "atanl",        1, _C99 },
863         { rts_sinhl,      1, "sinhl",        1, _C99 },
864         { rts_coshl,      1, "coshl",        1, _C99 },
865         { rts_tanhl,      1, "tanhl",        1, _C99 },
866
867         { rts_strcmp,     1, "strcmp",       2, _C89 },
868         { rts_strncmp,    1, "strncmp",      3, _C89 },
869         { rts_strcpy,     1, "strcpy",       2, _C89 },
870         { rts_strlen,     1, "strlen",       1, _C89 },
871         { rts_memcpy,     1, "memcpy",       3, _C89 },
872         { rts_mempcpy,    1, "mempcpy",      3, _GNUC },
873         { rts_memmove,    1, "memmove",      3, _C89 },
874         { rts_memset,     1, "memset",       3, _C89 },
875         { rts_memcmp,     1, "memcmp",       3, _C89 },
876 };
877
878 static ident *rts_idents[lengthof(rts_data)];
879
880 static create_ld_ident_func create_ld_ident = create_name_linux_elf;
881
882 void set_create_ld_ident(ident *(*func)(entity_t*))
883 {
884         create_ld_ident = func;
885 }
886
887 /**
888  * Handle GNU attributes for entities
889  *
890  * @param ent   the entity
891  * @param decl  the routine declaration
892  */
893 static void handle_decl_modifiers(ir_entity *irentity, entity_t *entity)
894 {
895         assert(is_declaration(entity));
896         decl_modifiers_t modifiers = entity->declaration.modifiers;
897
898         if (is_method_entity(irentity)) {
899                 if (modifiers & DM_PURE) {
900                         set_entity_additional_properties(irentity, mtp_property_pure);
901                 }
902                 if (modifiers & DM_CONST) {
903                         add_entity_additional_properties(irentity, mtp_property_const);
904                         have_const_functions = true;
905                 }
906         }
907         if (modifiers & DM_USED) {
908                 add_entity_linkage(irentity, IR_LINKAGE_HIDDEN_USER);
909         }
910         if (modifiers & DM_WEAK) {
911                 add_entity_linkage(irentity, IR_LINKAGE_WEAK);
912         }
913 }
914
915 static bool is_main(entity_t *entity)
916 {
917         static symbol_t *sym_main = NULL;
918         if (sym_main == NULL) {
919                 sym_main = symbol_table_insert("main");
920         }
921
922         if (entity->base.symbol != sym_main)
923                 return false;
924         /* must be in outermost scope */
925         if (entity->base.parent_scope != &current_translation_unit->scope)
926                 return false;
927
928         return true;
929 }
930
931 /**
932  * Creates an entity representing a function.
933  *
934  * @param entity       the function declaration/definition
935  * @param owner_type   the owner type of this function, NULL
936  *                     for global functions
937  */
938 static ir_entity *get_function_entity(entity_t *entity, ir_type *owner_type)
939 {
940         assert(entity->kind == ENTITY_FUNCTION);
941         if (entity->function.irentity != NULL) {
942                 return entity->function.irentity;
943         }
944
945         entity_t *original_entity = entity;
946         if (entity->function.btk != bk_none) {
947                 entity = get_builtin_replacement(entity);
948                 if (entity == NULL)
949                         return NULL;
950         }
951
952         if (is_main(entity)) {
953                 /* force main to C linkage */
954                 type_t *type = entity->declaration.type;
955                 assert(is_type_function(type));
956                 if (type->function.linkage != LINKAGE_C) {
957                         type_t *new_type           = duplicate_type(type);
958                         new_type->function.linkage = LINKAGE_C;
959                         type                       = identify_new_type(new_type);
960                         entity->declaration.type   = type;
961                 }
962         }
963
964         symbol_t *symbol = entity->base.symbol;
965         ident    *id     = new_id_from_str(symbol->string);
966
967         /* already an entity defined? */
968         ir_entity *irentity = entitymap_get(&entitymap, symbol);
969         bool const has_body = entity->function.statement != NULL;
970         if (irentity != NULL) {
971                 if (get_entity_visibility(irentity) == ir_visibility_external
972                                 && has_body) {
973                         set_entity_visibility(irentity, ir_visibility_default);
974                 }
975                 goto entity_created;
976         }
977
978         ir_type *ir_type_method;
979         if (entity->function.need_closure)
980                 ir_type_method = create_method_type(&entity->declaration.type->function, true);
981         else
982                 ir_type_method = get_ir_type(entity->declaration.type);
983
984         bool nested_function = false;
985         if (owner_type == NULL)
986                 owner_type = get_glob_type();
987         else
988                 nested_function = true;
989
990         dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
991         irentity             = new_d_entity(owner_type, id, ir_type_method, dbgi);
992
993         ident *ld_id;
994         if (nested_function)
995                 ld_id = id_unique("inner.%u");
996         else
997                 ld_id = create_ld_ident(entity);
998         set_entity_ld_ident(irentity, ld_id);
999
1000         handle_decl_modifiers(irentity, entity);
1001
1002         if (! nested_function) {
1003                 /* static inline             => local
1004                  * extern inline             => local
1005                  * inline without definition => local
1006                  * inline with definition    => external_visible */
1007                 storage_class_tag_t const storage_class
1008                         = (storage_class_tag_t) entity->declaration.storage_class;
1009                 bool                const is_inline     = entity->function.is_inline;
1010
1011                 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1012                     set_entity_visibility(irentity, ir_visibility_default);
1013                 } else if (storage_class == STORAGE_CLASS_STATIC ||
1014                            (is_inline && has_body)) {
1015                     set_entity_visibility(irentity, ir_visibility_local);
1016                 } else if (has_body) {
1017                     set_entity_visibility(irentity, ir_visibility_default);
1018                 } else {
1019                     set_entity_visibility(irentity, ir_visibility_external);
1020                 }
1021         } else {
1022                 /* nested functions are always local */
1023                 set_entity_visibility(irentity, ir_visibility_local);
1024         }
1025
1026         /* We should check for file scope here, but as long as we compile C only
1027            this is not needed. */
1028         if (!freestanding && !has_body) {
1029                 /* check for a known runtime function */
1030                 for (size_t i = 0; i < lengthof(rts_data); ++i) {
1031                         if (id != rts_idents[i])
1032                                 continue;
1033
1034                         function_type_t *function_type
1035                                 = &entity->declaration.type->function;
1036                         /* rts_entities code can't handle a "wrong" number of parameters */
1037                         if (function_type->unspecified_parameters)
1038                                 continue;
1039
1040                         /* check number of parameters */
1041                         int n_params = count_parameters(function_type);
1042                         if (n_params != rts_data[i].n_params)
1043                                 continue;
1044
1045                         type_t *return_type = skip_typeref(function_type->return_type);
1046                         int     n_res       = return_type != type_void ? 1 : 0;
1047                         if (n_res != rts_data[i].n_res)
1048                                 continue;
1049
1050                         /* ignore those rts functions not necessary needed for current mode */
1051                         if ((c_mode & rts_data[i].flags) == 0)
1052                                 continue;
1053                         assert(rts_entities[rts_data[i].id] == NULL);
1054                         rts_entities[rts_data[i].id] = irentity;
1055                 }
1056         }
1057
1058         entitymap_insert(&entitymap, symbol, irentity);
1059
1060 entity_created:
1061         original_entity->declaration.kind  = DECLARATION_KIND_FUNCTION;
1062         original_entity->function.irentity = irentity;
1063
1064         return irentity;
1065 }
1066
1067 /**
1068  * Creates a SymConst for a given entity.
1069  *
1070  * @param dbgi    debug info
1071  * @param mode    the (reference) mode for the SymConst
1072  * @param entity  the entity
1073  */
1074 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1075                                 ir_entity *entity)
1076 {
1077         assert(entity != NULL);
1078         union symconst_symbol sym;
1079         sym.entity_p = entity;
1080         return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1081 }
1082
1083 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1084 {
1085         ir_mode *value_mode = get_irn_mode(value);
1086
1087         if (value_mode == dest_mode || is_Bad(value))
1088                 return value;
1089
1090         if (dest_mode == mode_b) {
1091                 ir_node *zero = new_Const(get_mode_null(value_mode));
1092                 ir_node *cmp  = new_d_Cmp(dbgi, value, zero);
1093                 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1094                 return proj;
1095         }
1096
1097         return new_d_Conv(dbgi, value, dest_mode);
1098 }
1099
1100 /**
1101  * Creates a SymConst node representing a wide string literal.
1102  *
1103  * @param literal   the wide string literal
1104  */
1105 static ir_node *wide_string_literal_to_firm(
1106                 const string_literal_expression_t *literal)
1107 {
1108         ir_type  *const global_type = get_glob_type();
1109         ir_type  *const elem_type   = ir_type_wchar_t;
1110         dbg_info *const dbgi        = get_dbg_info(&literal->base.source_position);
1111         ir_type  *const type        = new_type_array(1, elem_type);
1112
1113         ident     *const id     = id_unique("str.%u");
1114         ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1115         set_entity_ld_ident(entity, id);
1116         set_entity_visibility(entity, ir_visibility_private);
1117         add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1118
1119         ir_mode      *const mode = get_type_mode(elem_type);
1120         const size_t        slen = wstrlen(&literal->value);
1121
1122         set_array_lower_bound_int(type, 0, 0);
1123         set_array_upper_bound_int(type, 0, slen);
1124         set_type_size_bytes(type, slen * get_mode_size_bytes(mode));
1125         set_type_state(type, layout_fixed);
1126
1127         ir_initializer_t *initializer = create_initializer_compound(slen);
1128         const char              *p    = literal->value.begin;
1129         for (size_t i = 0; i < slen; ++i) {
1130                 assert(p < literal->value.begin + literal->value.size);
1131                 utf32             v   = read_utf8_char(&p);
1132                 ir_tarval        *tv  = new_tarval_from_long(v, mode);
1133                 ir_initializer_t *val = create_initializer_tarval(tv);
1134                 set_initializer_compound_value(initializer, i, val);
1135         }
1136         set_entity_initializer(entity, initializer);
1137
1138         return create_symconst(dbgi, mode_P_data, entity);
1139 }
1140
1141 /**
1142  * Creates a SymConst node representing a string constant.
1143  *
1144  * @param src_pos    the source position of the string constant
1145  * @param id_prefix  a prefix for the name of the generated string constant
1146  * @param value      the value of the string constant
1147  */
1148 static ir_node *string_to_firm(const source_position_t *const src_pos,
1149                                const char *const id_prefix,
1150                                const string_t *const value)
1151 {
1152         ir_type  *const global_type = get_glob_type();
1153         dbg_info *const dbgi        = get_dbg_info(src_pos);
1154         ir_type  *const type        = new_type_array(1, ir_type_const_char);
1155
1156         ident     *const id     = id_unique(id_prefix);
1157         ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1158         set_entity_ld_ident(entity, id);
1159         set_entity_visibility(entity, ir_visibility_private);
1160         add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1161
1162         ir_type *const elem_type = ir_type_const_char;
1163         ir_mode *const mode      = get_type_mode(elem_type);
1164
1165         const char* const string = value->begin;
1166         const size_t      slen   = value->size;
1167
1168         set_array_lower_bound_int(type, 0, 0);
1169         set_array_upper_bound_int(type, 0, slen);
1170         set_type_size_bytes(type, slen);
1171         set_type_state(type, layout_fixed);
1172
1173         ir_initializer_t *initializer = create_initializer_compound(slen);
1174         for (size_t i = 0; i < slen; ++i) {
1175                 ir_tarval        *tv  = new_tarval_from_long(string[i], mode);
1176                 ir_initializer_t *val = create_initializer_tarval(tv);
1177                 set_initializer_compound_value(initializer, i, val);
1178         }
1179         set_entity_initializer(entity, initializer);
1180
1181         return create_symconst(dbgi, mode_P_data, entity);
1182 }
1183
1184 static bool try_create_integer(literal_expression_t *literal,
1185                                type_t *type, unsigned char base)
1186 {
1187         const char *string = literal->value.begin;
1188         size_t      size   = literal->value.size;
1189
1190         assert(type->kind == TYPE_ATOMIC);
1191         atomic_type_kind_t akind = type->atomic.akind;
1192
1193         ir_mode   *mode = atomic_modes[akind];
1194         ir_tarval *tv   = new_integer_tarval_from_str(string, size, 1, base, mode);
1195         if (tv == tarval_bad)
1196                 return false;
1197
1198         literal->base.type    = type;
1199         literal->target_value = tv;
1200         return true;
1201 }
1202
1203 static void create_integer_tarval(literal_expression_t *literal)
1204 {
1205         unsigned  us     = 0;
1206         unsigned  ls     = 0;
1207         symbol_t *suffix = literal->suffix;
1208         /* parse suffix */
1209         if (suffix != NULL) {
1210                 for (const char *c = suffix->string; *c != '\0'; ++c) {
1211                         if (*c == 'u' || *c == 'U') { ++us; }
1212                         if (*c == 'l' || *c == 'L') { ++ls; }
1213                 }
1214         }
1215
1216         unsigned base;
1217         switch (literal->base.kind) {
1218                 case EXPR_LITERAL_INTEGER_OCTAL:       base =  8; break;
1219                 case EXPR_LITERAL_INTEGER:             base = 10; break;
1220                 case EXPR_LITERAL_INTEGER_HEXADECIMAL: base = 16; break;
1221                 default: panic("invalid literal kind");
1222         }
1223
1224         tarval_int_overflow_mode_t old_mode = tarval_get_integer_overflow_mode();
1225
1226         /* now try if the constant is small enough for some types */
1227         tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
1228         if (ls < 1) {
1229                 if (us == 0 && try_create_integer(literal, type_int, base))
1230                         goto finished;
1231                 if ((us == 1 || base != 10)
1232                                 && try_create_integer(literal, type_unsigned_int, base))
1233                         goto finished;
1234         }
1235         if (ls < 2) {
1236                 if (us == 0 && try_create_integer(literal, type_long, base))
1237                         goto finished;
1238                 if ((us == 1 || base != 10)
1239                                 && try_create_integer(literal, type_unsigned_long, base))
1240                         goto finished;
1241         }
1242         /* last try? then we should not report tarval_bad */
1243         if (us != 1 && base == 10)
1244                 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1245         if (us == 0 && try_create_integer(literal, type_long_long, base))
1246                 goto finished;
1247
1248         /* last try */
1249         assert(us == 1 || base != 10);
1250         tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1251         bool res = try_create_integer(literal, type_unsigned_long_long, base);
1252         if (!res)
1253                 panic("internal error when parsing number literal");
1254
1255 finished:
1256         tarval_set_integer_overflow_mode(old_mode);
1257 }
1258
1259 void determine_literal_type(literal_expression_t *literal)
1260 {
1261         switch (literal->base.kind) {
1262         case EXPR_LITERAL_INTEGER:
1263         case EXPR_LITERAL_INTEGER_OCTAL:
1264         case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1265                 create_integer_tarval(literal);
1266                 return;
1267         default:
1268                 break;
1269         }
1270 }
1271
1272 /**
1273  * Creates a Const node representing a constant.
1274  */
1275 static ir_node *literal_to_firm(const literal_expression_t *literal)
1276 {
1277         type_t     *type   = skip_typeref(literal->base.type);
1278         ir_mode    *mode   = get_ir_mode_storage(type);
1279         const char *string = literal->value.begin;
1280         size_t      size   = literal->value.size;
1281         ir_tarval  *tv;
1282
1283         switch (literal->base.kind) {
1284         case EXPR_LITERAL_WIDE_CHARACTER: {
1285                 utf32  v = read_utf8_char(&string);
1286                 char   buf[128];
1287                 size_t len = snprintf(buf, sizeof(buf), UTF32_PRINTF_FORMAT, v);
1288
1289                 tv = new_tarval_from_str(buf, len, mode);
1290                 goto make_const;
1291         }
1292         case EXPR_LITERAL_CHARACTER: {
1293                 long long int v;
1294                 if (size == 1 && char_is_signed) {
1295                         v = (signed char)string[0];
1296                 } else {
1297                         v = 0;
1298                         for (size_t i = 0; i < size; ++i) {
1299                                 v = (v << 8) | ((unsigned char)string[i]);
1300                         }
1301                 }
1302                 char   buf[128];
1303                 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1304
1305                 tv = new_tarval_from_str(buf, len, mode);
1306                 goto make_const;
1307         }
1308         case EXPR_LITERAL_INTEGER:
1309         case EXPR_LITERAL_INTEGER_OCTAL:
1310         case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1311                 assert(literal->target_value != NULL);
1312                 tv = literal->target_value;
1313                 goto make_const;
1314         case EXPR_LITERAL_FLOATINGPOINT:
1315                 tv = new_tarval_from_str(string, size, mode);
1316                 goto make_const;
1317         case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL: {
1318                 char buffer[size + 2];
1319                 memcpy(buffer, "0x", 2);
1320                 memcpy(buffer+2, string, size);
1321                 tv = new_tarval_from_str(buffer, size+2, mode);
1322                 goto make_const;
1323         }
1324         case EXPR_LITERAL_BOOLEAN:
1325                 if (string[0] == 't') {
1326                         tv = get_mode_one(mode);
1327                 } else {
1328                         assert(string[0] == 'f');
1329                         tv = get_mode_null(mode);
1330                 }
1331                 goto make_const;
1332         case EXPR_LITERAL_MS_NOOP:
1333                 tv = get_mode_null(mode);
1334                 goto make_const;
1335         default:
1336                 break;
1337         }
1338         panic("Invalid literal kind found");
1339
1340 make_const: ;
1341         dbg_info *dbgi       = get_dbg_info(&literal->base.source_position);
1342         ir_node  *res        = new_d_Const(dbgi, tv);
1343         ir_mode  *mode_arith = get_ir_mode_arithmetic(type);
1344         return create_conv(dbgi, res, mode_arith);
1345 }
1346
1347 /*
1348  * Allocate an area of size bytes aligned at alignment
1349  * at a frame type.
1350  */
1351 static ir_entity *alloc_trampoline(ir_type *frame_type, int size, unsigned alignment)
1352 {
1353         static unsigned area_cnt = 0;
1354         char buf[32];
1355
1356         ir_type *tp = new_type_array(1, ir_type_char);
1357         set_array_bounds_int(tp, 0, 0, size);
1358         set_type_alignment_bytes(tp, alignment);
1359
1360         snprintf(buf, sizeof(buf), "trampolin%u", area_cnt++);
1361         ident *name = new_id_from_str(buf);
1362         ir_entity *area = new_entity(frame_type, name, tp);
1363
1364         /* mark this entity as compiler generated */
1365         set_entity_compiler_generated(area, 1);
1366         return area;
1367 }
1368
1369 /**
1370  * Return a node representing a trampoline region
1371  * for a given function entity.
1372  *
1373  * @param dbgi    debug info
1374  * @param entity  the function entity
1375  */
1376 static ir_node *get_trampoline_region(dbg_info *dbgi, ir_entity *entity)
1377 {
1378         ir_entity *region = NULL;
1379         int        i;
1380
1381         if (current_trampolines != NULL) {
1382                 for (i = ARR_LEN(current_trampolines) - 1; i >= 0; --i) {
1383                         if (current_trampolines[i].function == entity) {
1384                                 region = current_trampolines[i].region;
1385                                 break;
1386                         }
1387                 }
1388         } else {
1389                 current_trampolines = NEW_ARR_F(trampoline_region, 0);
1390         }
1391         ir_graph *irg = current_ir_graph;
1392         if (region == NULL) {
1393                 /* create a new region */
1394                 ir_type           *frame_tp = get_irg_frame_type(irg);
1395                 trampoline_region  reg;
1396                 reg.function = entity;
1397
1398                 reg.region   = alloc_trampoline(frame_tp,
1399                                                 be_params->trampoline_size,
1400                                                 be_params->trampoline_align);
1401                 ARR_APP1(trampoline_region, current_trampolines, reg);
1402                 region = reg.region;
1403         }
1404         return new_d_simpleSel(dbgi, get_irg_no_mem(irg), get_irg_frame(irg),
1405                                region);
1406 }
1407
1408 /**
1409  * Creates a trampoline for a function represented by an entity.
1410  *
1411  * @param dbgi    debug info
1412  * @param mode    the (reference) mode for the function address
1413  * @param entity  the function entity
1414  */
1415 static ir_node *create_trampoline(dbg_info *dbgi, ir_mode *mode,
1416                                   ir_entity *entity)
1417 {
1418         assert(entity != NULL);
1419         ir_node *in[3];
1420         in[0] = get_trampoline_region(dbgi, entity);
1421         in[1] = create_symconst(dbgi, mode, entity);
1422         in[2] = get_irg_frame(current_ir_graph);
1423
1424         ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_inner_trampoline, get_unknown_type());
1425         set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1426         return new_Proj(irn, mode, pn_Builtin_1_result);
1427 }
1428
1429 /**
1430  * Dereference an address.
1431  *
1432  * @param dbgi  debug info
1433  * @param type  the type of the dereferenced result (the points_to type)
1434  * @param addr  the address to dereference
1435  */
1436 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1437                                       ir_node *const addr)
1438 {
1439         ir_type *irtype = get_ir_type(type);
1440         if (is_compound_type(irtype)
1441                         || is_Method_type(irtype)
1442                         || is_Array_type(irtype)) {
1443                 return addr;
1444         }
1445
1446         ir_cons_flags  flags    = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
1447                                   ? cons_volatile : cons_none;
1448         ir_mode *const mode     = get_type_mode(irtype);
1449         ir_node *const memory   = get_store();
1450         ir_node *const load     = new_d_Load(dbgi, memory, addr, mode, flags);
1451         ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1452         ir_node *const load_res = new_d_Proj(dbgi, load, mode,   pn_Load_res);
1453
1454         set_store(load_mem);
1455
1456         ir_mode *const mode_arithmetic = get_ir_mode_arithmetic(type);
1457         return create_conv(dbgi, load_res, mode_arithmetic);
1458 }
1459
1460 /**
1461  * Creates a strict Conv (to the node's mode) if necessary.
1462  *
1463  * @param dbgi  debug info
1464  * @param node  the node to strict conv
1465  */
1466 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1467 {
1468         ir_mode *mode = get_irn_mode(node);
1469
1470         if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1471                 return node;
1472         if (!mode_is_float(mode))
1473                 return node;
1474
1475         /* check if there is already a Conv */
1476         if (is_Conv(node)) {
1477                 /* convert it into a strict Conv */
1478                 set_Conv_strict(node, 1);
1479                 return node;
1480         }
1481
1482         /* otherwise create a new one */
1483         return new_d_strictConv(dbgi, node, mode);
1484 }
1485
1486 /**
1487  * Returns the address of a global variable.
1488  *
1489  * @param dbgi      debug info
1490  * @param variable  the variable
1491  */
1492 static ir_node *get_global_var_address(dbg_info *const dbgi,
1493                                        const variable_t *const variable)
1494 {
1495         ir_entity *const irentity = variable->v.entity;
1496         if (variable->thread_local) {
1497                 ir_node *const no_mem = new_NoMem();
1498                 ir_node *const tls    = get_irg_tls(current_ir_graph);
1499                 return new_d_simpleSel(dbgi, no_mem, tls, irentity);
1500         } else {
1501                 return create_symconst(dbgi, mode_P_data, irentity);
1502         }
1503 }
1504
1505 /**
1506  * Returns the correct base address depending on whether it is a parameter or a
1507  * normal local variable.
1508  */
1509 static ir_node *get_local_frame(ir_entity *const ent)
1510 {
1511         ir_graph      *const irg   = current_ir_graph;
1512         const ir_type *const owner = get_entity_owner(ent);
1513         if (owner == current_outer_frame || owner == current_outer_value_type) {
1514                 assert(current_static_link != NULL);
1515                 return current_static_link;
1516         } else {
1517                 return get_irg_frame(irg);
1518         }
1519 }
1520
1521 /**
1522  * Keep all memory edges of the given block.
1523  */
1524 static void keep_all_memory(ir_node *block)
1525 {
1526         ir_node *old = get_cur_block();
1527
1528         set_cur_block(block);
1529         keep_alive(get_store());
1530         /* TODO: keep all memory edges from restricted pointers */
1531         set_cur_block(old);
1532 }
1533
1534 static ir_node *reference_expression_enum_value_to_firm(
1535                 const reference_expression_t *ref)
1536 {
1537         entity_t *entity = ref->entity;
1538         type_t   *type   = skip_typeref(entity->enum_value.enum_type);
1539         /* make sure the type is constructed */
1540         (void) get_ir_type(type);
1541
1542         return new_Const(entity->enum_value.tv);
1543 }
1544
1545 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1546 {
1547         dbg_info *dbgi   = get_dbg_info(&ref->base.source_position);
1548         entity_t *entity = ref->entity;
1549         assert(is_declaration(entity));
1550         type_t   *type   = skip_typeref(entity->declaration.type);
1551
1552         /* make sure the type is constructed */
1553         (void) get_ir_type(type);
1554
1555         if (entity->kind == ENTITY_FUNCTION && entity->function.btk != bk_none) {
1556                 ir_entity *irentity = get_function_entity(entity, NULL);
1557                 /* for gcc compatibility we have to produce (dummy) addresses for some
1558                  * builtins which don't have entities */
1559                 if (irentity == NULL) {
1560                         if (warning.other) {
1561                                 warningf(&ref->base.source_position,
1562                                                 "taking address of builtin '%Y'",
1563                                                 ref->entity->base.symbol);
1564                         }
1565
1566                         /* simply create a NULL pointer */
1567                         ir_mode  *mode = get_ir_mode_arithmetic(type_void_ptr);
1568                         ir_node  *res  = new_Const_long(mode, 0);
1569
1570                         return res;
1571                 }
1572         }
1573
1574         switch ((declaration_kind_t) entity->declaration.kind) {
1575         case DECLARATION_KIND_UNKNOWN:
1576                 break;
1577
1578         case DECLARATION_KIND_LOCAL_VARIABLE: {
1579                 ir_mode *const mode  = get_ir_mode_storage(type);
1580                 ir_node *const value = get_value(entity->variable.v.value_number, mode);
1581                 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1582         }
1583         case DECLARATION_KIND_PARAMETER: {
1584                 ir_mode *const mode  = get_ir_mode_storage(type);
1585                 ir_node *const value = get_value(entity->parameter.v.value_number,mode);
1586                 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1587         }
1588         case DECLARATION_KIND_FUNCTION: {
1589                 ir_mode *const mode = get_ir_mode_storage(type);
1590                 return create_symconst(dbgi, mode, entity->function.irentity);
1591         }
1592         case DECLARATION_KIND_INNER_FUNCTION: {
1593                 ir_mode *const mode = get_ir_mode_storage(type);
1594                 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1595                         /* inner function not using the closure */
1596                         return create_symconst(dbgi, mode, entity->function.irentity);
1597                 } else {
1598                         /* need trampoline here */
1599                         return create_trampoline(dbgi, mode, entity->function.irentity);
1600                 }
1601         }
1602         case DECLARATION_KIND_GLOBAL_VARIABLE: {
1603                 const variable_t *variable = &entity->variable;
1604                 ir_node *const addr = get_global_var_address(dbgi, variable);
1605                 return deref_address(dbgi, variable->base.type, addr);
1606         }
1607
1608         case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1609                 ir_entity *irentity = entity->variable.v.entity;
1610                 ir_node   *frame    = get_local_frame(irentity);
1611                 ir_node   *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1612                 return deref_address(dbgi, entity->declaration.type, sel);
1613         }
1614         case DECLARATION_KIND_PARAMETER_ENTITY: {
1615                 ir_entity *irentity = entity->parameter.v.entity;
1616                 ir_node   *frame    = get_local_frame(irentity);
1617                 ir_node   *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1618                 return deref_address(dbgi, entity->declaration.type, sel);
1619         }
1620
1621         case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1622                 return entity->variable.v.vla_base;
1623
1624         case DECLARATION_KIND_COMPOUND_MEMBER:
1625                 panic("not implemented reference type");
1626         }
1627
1628         panic("reference to declaration with unknown type found");
1629 }
1630
1631 static ir_node *reference_addr(const reference_expression_t *ref)
1632 {
1633         dbg_info *dbgi   = get_dbg_info(&ref->base.source_position);
1634         entity_t *entity = ref->entity;
1635         assert(is_declaration(entity));
1636
1637         switch((declaration_kind_t) entity->declaration.kind) {
1638         case DECLARATION_KIND_UNKNOWN:
1639                 break;
1640         case DECLARATION_KIND_PARAMETER:
1641         case DECLARATION_KIND_LOCAL_VARIABLE:
1642                 /* you can store to a local variable (so we don't panic but return NULL
1643                  * as an indicator for no real address) */
1644                 return NULL;
1645         case DECLARATION_KIND_GLOBAL_VARIABLE: {
1646                 ir_node *const addr = get_global_var_address(dbgi, &entity->variable);
1647                 return addr;
1648         }
1649         case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1650                 ir_entity *irentity = entity->variable.v.entity;
1651                 ir_node   *frame    = get_local_frame(irentity);
1652                 ir_node   *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1653
1654                 return sel;
1655         }
1656         case DECLARATION_KIND_PARAMETER_ENTITY: {
1657                 ir_entity *irentity = entity->parameter.v.entity;
1658                 ir_node   *frame    = get_local_frame(irentity);
1659                 ir_node   *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1660
1661                 return sel;
1662         }
1663
1664         case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1665                 return entity->variable.v.vla_base;
1666
1667         case DECLARATION_KIND_FUNCTION: {
1668                 type_t  *const type = skip_typeref(entity->declaration.type);
1669                 ir_mode *const mode = get_ir_mode_storage(type);
1670                 return create_symconst(dbgi, mode, entity->function.irentity);
1671         }
1672
1673         case DECLARATION_KIND_INNER_FUNCTION: {
1674                 type_t  *const type = skip_typeref(entity->declaration.type);
1675                 ir_mode *const mode = get_ir_mode_storage(type);
1676                 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1677                         /* inner function not using the closure */
1678                         return create_symconst(dbgi, mode, entity->function.irentity);
1679                 } else {
1680                         /* need trampoline here */
1681                         return create_trampoline(dbgi, mode, entity->function.irentity);
1682                 }
1683         }
1684
1685         case DECLARATION_KIND_COMPOUND_MEMBER:
1686                 panic("not implemented reference type");
1687         }
1688
1689         panic("reference to declaration with unknown type found");
1690 }
1691
1692 /**
1693  * Generate an unary builtin.
1694  *
1695  * @param kind           the builtin kind to generate
1696  * @param op             the operand
1697  * @param function_type  the function type for the GNU builtin routine
1698  * @param db             debug info
1699  */
1700 static ir_node *gen_unary_builtin(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
1701 {
1702         ir_node *in[1];
1703         in[0] = expression_to_firm(op);
1704
1705         ir_type *tp  = get_ir_type(function_type);
1706         ir_type *res = get_method_res_type(tp, 0);
1707         ir_node *irn = new_d_Builtin(db, get_irg_no_mem(current_ir_graph), 1, in, kind, tp);
1708         set_irn_pinned(irn, op_pin_state_floats);
1709         return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1710 }
1711
1712 /**
1713  * Generate a pinned unary builtin.
1714  *
1715  * @param kind           the builtin kind to generate
1716  * @param op             the operand
1717  * @param function_type  the function type for the GNU builtin routine
1718  * @param db             debug info
1719  */
1720 static ir_node *gen_unary_builtin_pinned(ir_builtin_kind kind, expression_t *op,
1721                                          type_t *function_type, dbg_info *db)
1722 {
1723         ir_node *in[1];
1724         in[0] = expression_to_firm(op);
1725
1726         ir_type *tp  = get_ir_type(function_type);
1727         ir_type *res = get_method_res_type(tp, 0);
1728         ir_node *mem = get_store();
1729         ir_node *irn = new_d_Builtin(db, mem, 1, in, kind, tp);
1730         set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1731         return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1732 }
1733
1734 /**
1735  * Generate an binary-void-return builtin.
1736  *
1737  * @param kind           the builtin kind to generate
1738  * @param op1            the first operand
1739  * @param op2            the second operand
1740  * @param function_type  the function type for the GNU builtin routine
1741  * @param db             debug info
1742  */
1743 static ir_node *gen_binary_builtin_mem(ir_builtin_kind kind, expression_t *op1,
1744                                        expression_t *op2, type_t *function_type,
1745                                                                            dbg_info *db)
1746 {
1747         ir_node *in[2];
1748         in[0] = expression_to_firm(op1);
1749         in[1] = expression_to_firm(op2);
1750
1751         ir_type *tp  = get_ir_type(function_type);
1752         ir_node *mem = get_store();
1753         ir_node *irn = new_d_Builtin(db, mem, 2, in, kind, tp);
1754         set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1755         return NULL;
1756 }
1757
1758 /**
1759  * Transform calls to builtin functions.
1760  */
1761 static ir_node *process_builtin_call(const call_expression_t *call)
1762 {
1763         dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1764
1765         assert(call->function->kind == EXPR_REFERENCE);
1766         reference_expression_t *builtin = &call->function->reference;
1767
1768         type_t *type = skip_typeref(builtin->base.type);
1769         assert(is_type_pointer(type));
1770
1771         type_t *function_type = skip_typeref(type->pointer.points_to);
1772
1773         switch (builtin->entity->function.btk) {
1774         case bk_gnu_builtin_alloca: {
1775                 if (call->arguments == NULL || call->arguments->next != NULL) {
1776                         panic("invalid number of parameters on __builtin_alloca");
1777                 }
1778                 expression_t *argument = call->arguments->expression;
1779                 ir_node      *size     = expression_to_firm(argument);
1780
1781                 ir_node *store  = get_store();
1782                 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1783                                               stack_alloc);
1784                 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1785                 set_store(proj_m);
1786                 ir_node *res    = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1787
1788                 return res;
1789         }
1790
1791         case bk_gnu_builtin_huge_val:
1792         case bk_gnu_builtin_huge_valf:
1793         case bk_gnu_builtin_huge_vall:
1794         case bk_gnu_builtin_inf:
1795         case bk_gnu_builtin_inff:
1796         case bk_gnu_builtin_infl: {
1797                 type_t    *type = function_type->function.return_type;
1798                 ir_mode   *mode = get_ir_mode_arithmetic(type);
1799                 ir_tarval *tv   = get_mode_infinite(mode);
1800                 ir_node   *res  = new_d_Const(dbgi, tv);
1801                 return res;
1802         }
1803         case bk_gnu_builtin_nan:
1804         case bk_gnu_builtin_nanf:
1805         case bk_gnu_builtin_nanl: {
1806                 /* Ignore string for now... */
1807                 assert(is_type_function(function_type));
1808                 type_t    *type = function_type->function.return_type;
1809                 ir_mode   *mode = get_ir_mode_arithmetic(type);
1810                 ir_tarval *tv   = get_mode_NAN(mode);
1811                 ir_node   *res  = new_d_Const(dbgi, tv);
1812                 return res;
1813         }
1814         case bk_gnu_builtin_expect: {
1815                 expression_t *argument = call->arguments->expression;
1816                 return _expression_to_firm(argument);
1817         }
1818         case bk_gnu_builtin_va_end:
1819                 /* evaluate the argument of va_end for its side effects */
1820                 _expression_to_firm(call->arguments->expression);
1821                 return NULL;
1822         case bk_gnu_builtin_frame_address: {
1823                 expression_t *const expression = call->arguments->expression;
1824                 bool val = fold_constant_to_bool(expression);
1825                 if (!val) {
1826                         /* the nice case */
1827                         return get_irg_frame(current_ir_graph);
1828                 } else {
1829                         /* get the argument */
1830                         ir_node *in[2];
1831
1832                         in[0] = expression_to_firm(expression);
1833                         in[1] = get_irg_frame(current_ir_graph);
1834                         ir_type *tp  = get_ir_type(function_type);
1835                         ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_frame_address, tp);
1836                         return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1837                 }
1838         }
1839         case bk_gnu_builtin_return_address: {
1840                 expression_t *const expression = call->arguments->expression;
1841                 ir_node *in[2];
1842
1843                 in[0] = expression_to_firm(expression);
1844                 in[1] = get_irg_frame(current_ir_graph);
1845                 ir_type *tp  = get_ir_type(function_type);
1846                 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1847                 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1848         }
1849         case bk_gnu_builtin_ffs:
1850                  return gen_unary_builtin(ir_bk_ffs,      call->arguments->expression, function_type, dbgi);
1851         case bk_gnu_builtin_clz:
1852                  return gen_unary_builtin(ir_bk_clz,      call->arguments->expression, function_type, dbgi);
1853         case bk_gnu_builtin_ctz:
1854                  return gen_unary_builtin(ir_bk_ctz,      call->arguments->expression, function_type, dbgi);
1855         case bk_gnu_builtin_popcount:
1856         case bk_ms__popcount:
1857                  return gen_unary_builtin(ir_bk_popcount, call->arguments->expression, function_type, dbgi);
1858         case bk_gnu_builtin_parity:
1859                  return gen_unary_builtin(ir_bk_parity,   call->arguments->expression, function_type, dbgi);
1860         case bk_gnu_builtin_prefetch: {
1861                 call_argument_t *const args = call->arguments;
1862                 expression_t *const addr    = args->expression;
1863                 ir_node *in[3];
1864
1865                 in[0] = _expression_to_firm(addr);
1866                 if (args->next != NULL) {
1867                         expression_t *const rw = args->next->expression;
1868
1869                         in[1] = _expression_to_firm(rw);
1870
1871                         if (args->next->next != NULL) {
1872                                 expression_t *const locality = args->next->next->expression;
1873
1874                                 in[2] = expression_to_firm(locality);
1875                         } else {
1876                                 in[2] = new_Const_long(mode_int, 3);
1877                         }
1878                 } else {
1879                         in[1] = new_Const_long(mode_int, 0);
1880                         in[2] = new_Const_long(mode_int, 3);
1881                 }
1882                 ir_type *tp  = get_ir_type(function_type);
1883                 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_prefetch, tp);
1884                 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1885                 return NULL;
1886         }
1887         case bk_gnu_builtin_object_size: {
1888                 /* determine value of "type" */
1889                 expression_t *type_expression = call->arguments->next->expression;
1890                 long          type_val        = fold_constant_to_int(type_expression);
1891                 type_t       *type            = function_type->function.return_type;
1892                 ir_mode      *mode            = get_ir_mode_arithmetic(type);
1893                 ir_tarval    *result;
1894
1895                 /* just produce a "I don't know" result */
1896                 if (type_val & 2)
1897                         result = new_tarval_from_long(0, mode);
1898                 else
1899                         result = new_tarval_from_long(-1, mode);
1900
1901                 return new_d_Const(dbgi, result);
1902         }
1903         case bk_gnu_builtin_trap:
1904         case bk_ms__ud2:
1905         {
1906                 ir_type *tp  = get_ir_type(function_type);
1907                 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_trap, tp);
1908                 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1909                 return NULL;
1910         }
1911         case bk_ms__debugbreak: {
1912                 ir_type *tp  = get_ir_type(function_type);
1913                 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_debugbreak, tp);
1914                 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1915                 return NULL;
1916         }
1917         case bk_ms_ReturnAddress: {
1918                 ir_node *in[2];
1919
1920                 in[0] = new_Const_long(mode_int, 0);
1921                 in[1] = get_irg_frame(current_ir_graph);
1922                 ir_type *tp  = get_ir_type(function_type);
1923                 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1924                 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1925         }
1926         case bk_ms_rotl:
1927         case bk_ms_rotl64: {
1928                 ir_node *val  = expression_to_firm(call->arguments->expression);
1929                 ir_node *shf  = expression_to_firm(call->arguments->next->expression);
1930                 ir_mode *mode = get_irn_mode(val);
1931                 return new_d_Rotl(dbgi, val, create_conv(dbgi, shf, mode_uint), mode);
1932         }
1933         case bk_ms_rotr:
1934         case bk_ms_rotr64: {
1935                 ir_node *val  = expression_to_firm(call->arguments->expression);
1936                 ir_node *shf  = expression_to_firm(call->arguments->next->expression);
1937                 ir_mode *mode = get_irn_mode(val);
1938                 ir_node *c    = new_Const_long(mode_uint, get_mode_size_bits(mode));
1939                 ir_node *sub  = new_d_Sub(dbgi, c, create_conv(dbgi, shf, mode_uint), mode_uint);
1940                 return new_d_Rotl(dbgi, val, sub, mode);
1941         }
1942         case bk_ms_byteswap_ushort:
1943         case bk_ms_byteswap_ulong:
1944         case bk_ms_byteswap_uint64:
1945                 return gen_unary_builtin(ir_bk_bswap, call->arguments->expression, function_type, dbgi);
1946         case bk_ms__inbyte:
1947         case bk_ms__inword:
1948         case bk_ms__indword:
1949                 return gen_unary_builtin_pinned(ir_bk_inport, call->arguments->expression, function_type, dbgi);
1950         case bk_ms__outbyte:
1951         case bk_ms__outword:
1952         case bk_ms__outdword:
1953                 return gen_binary_builtin_mem(ir_bk_outport, call->arguments->expression,
1954                         call->arguments->next->expression, function_type, dbgi);
1955         default:
1956                 panic("unsupported builtin found");
1957         }
1958 }
1959
1960 /**
1961  * Transform a call expression.
1962  * Handles some special cases, like alloca() calls, which must be resolved
1963  * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1964  * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1965  * handled right...
1966  */
1967 static ir_node *call_expression_to_firm(const call_expression_t *const call)
1968 {
1969         dbg_info *const dbgi = get_dbg_info(&call->base.source_position);
1970         assert(get_cur_block() != NULL);
1971
1972         expression_t *function = call->function;
1973         if (function->kind == EXPR_REFERENCE) {
1974                 const reference_expression_t *ref    = &function->reference;
1975                 entity_t                     *entity = ref->entity;
1976
1977                 if (entity->kind == ENTITY_FUNCTION) {
1978                         ir_entity *irentity = entity->function.irentity;
1979                         if (irentity == NULL)
1980                                 irentity = get_function_entity(entity, NULL);
1981
1982                         if (irentity == NULL && entity->function.btk != bk_none) {
1983                                 return process_builtin_call(call);
1984                         }
1985
1986 #if 0
1987                         if (irentity == rts_entities[rts_alloca]) {
1988                                 /* handle alloca() call */
1989                                 expression_t *argument = call->arguments->expression;
1990                                 ir_node      *size     = expression_to_firm(argument);
1991                                 ir_mode      *mode     = get_ir_mode_arithmetic(type_size_t);
1992
1993                                 size = create_conv(dbgi, size, mode);
1994
1995                                 ir_node  *store  = get_store();
1996                                 ir_node  *alloca = new_d_Alloc(dbgi, store, size,
1997                                                                firm_unknown_type, stack_alloc);
1998                                 ir_node  *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1999                                 set_store(proj_m);
2000                                 ir_node  *res    = new_Proj(alloca, mode_P_data, pn_Alloc_res);
2001
2002                                 return res;
2003                         }
2004 #endif
2005                 }
2006         }
2007         ir_node *callee = expression_to_firm(function);
2008
2009         type_t *type = skip_typeref(function->base.type);
2010         assert(is_type_pointer(type));
2011         pointer_type_t *pointer_type = &type->pointer;
2012         type_t         *points_to    = skip_typeref(pointer_type->points_to);
2013         assert(is_type_function(points_to));
2014         function_type_t *function_type = &points_to->function;
2015
2016         int      n_parameters = 0;
2017         ir_type *ir_method_type  = get_ir_type((type_t*) function_type);
2018         ir_type *new_method_type = NULL;
2019         if (function_type->variadic || function_type->unspecified_parameters) {
2020                 const call_argument_t *argument = call->arguments;
2021                 for ( ; argument != NULL; argument = argument->next) {
2022                         ++n_parameters;
2023                 }
2024
2025                 /* we need to construct a new method type matching the call
2026                  * arguments... */
2027                 type_dbg_info *tdbgi = get_type_dbg_info_((const type_t*) function_type);
2028                 int n_res       = get_method_n_ress(ir_method_type);
2029                 new_method_type = new_d_type_method(n_parameters, n_res, tdbgi);
2030                 set_method_calling_convention(new_method_type,
2031                                get_method_calling_convention(ir_method_type));
2032                 set_method_additional_properties(new_method_type,
2033                                get_method_additional_properties(ir_method_type));
2034                 set_method_variadicity(new_method_type,
2035                                        get_method_variadicity(ir_method_type));
2036
2037                 for (int i = 0; i < n_res; ++i) {
2038                         set_method_res_type(new_method_type, i,
2039                                             get_method_res_type(ir_method_type, i));
2040                 }
2041                 argument = call->arguments;
2042                 for (int i = 0; i < n_parameters; ++i, argument = argument->next) {
2043                         expression_t *expression = argument->expression;
2044                         ir_type      *irtype     = get_ir_type(expression->base.type);
2045                         set_method_param_type(new_method_type, i, irtype);
2046                 }
2047                 ir_method_type = new_method_type;
2048         } else {
2049                 n_parameters = get_method_n_params(ir_method_type);
2050         }
2051
2052         ir_node *in[n_parameters];
2053
2054         const call_argument_t *argument = call->arguments;
2055         for (int n = 0; n < n_parameters; ++n) {
2056                 expression_t *expression = argument->expression;
2057                 ir_node      *arg_node   = expression_to_firm(expression);
2058
2059                 type_t  *type = skip_typeref(expression->base.type);
2060                 if (!is_type_compound(type)) {
2061                         ir_mode *mode = get_ir_mode_storage(expression->base.type);
2062                         arg_node      = create_conv(dbgi, arg_node, mode);
2063                         arg_node      = do_strict_conv(dbgi, arg_node);
2064                 }
2065
2066                 in[n] = arg_node;
2067
2068                 argument = argument->next;
2069         }
2070
2071         ir_node  *store = get_store();
2072         ir_node  *node  = new_d_Call(dbgi, store, callee, n_parameters, in,
2073                                      ir_method_type);
2074         ir_node  *mem   = new_d_Proj(dbgi, node, mode_M, pn_Call_M);
2075         set_store(mem);
2076
2077         type_t  *return_type = skip_typeref(function_type->return_type);
2078         ir_node *result      = NULL;
2079
2080         if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
2081                 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
2082
2083                 if (is_type_scalar(return_type)) {
2084                         ir_mode *mode       = get_ir_mode_storage(return_type);
2085                         result              = new_d_Proj(dbgi, resproj, mode, 0);
2086                         ir_mode *mode_arith = get_ir_mode_arithmetic(return_type);
2087                         result              = create_conv(NULL, result, mode_arith);
2088                 } else {
2089                         ir_mode *mode = mode_P_data;
2090                         result        = new_d_Proj(dbgi, resproj, mode, 0);
2091                 }
2092         }
2093
2094         if (function->kind == EXPR_REFERENCE &&
2095             function->reference.entity->declaration.modifiers & DM_NORETURN) {
2096                 /* A dead end:  Keep the Call and the Block.  Also place all further
2097                  * nodes into a new and unreachable block. */
2098                 keep_alive(node);
2099                 keep_alive(get_cur_block());
2100                 ir_node *block = new_Block(0, NULL);
2101                 set_cur_block(block);
2102         }
2103
2104         return result;
2105 }
2106
2107 static void statement_to_firm(statement_t *statement);
2108 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
2109
2110 static ir_node *expression_to_addr(const expression_t *expression);
2111 static ir_node *create_condition_evaluation(const expression_t *expression,
2112                                             ir_node *true_block,
2113                                             ir_node *false_block);
2114
2115 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
2116                          ir_node *value)
2117 {
2118         if (!is_type_compound(type)) {
2119                 ir_mode *mode = get_ir_mode_storage(type);
2120                 value         = create_conv(dbgi, value, mode);
2121                 value         = do_strict_conv(dbgi, value);
2122         }
2123
2124         ir_node *memory = get_store();
2125
2126         if (is_type_scalar(type)) {
2127                 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
2128                                       ? cons_volatile : cons_none;
2129                 ir_node  *store     = new_d_Store(dbgi, memory, addr, value, flags);
2130                 ir_node  *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2131                 set_store(store_mem);
2132         } else {
2133                 ir_type *irtype    = get_ir_type(type);
2134                 ir_node *copyb     = new_d_CopyB(dbgi, memory, addr, value, irtype);
2135                 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
2136                 set_store(copyb_mem);
2137         }
2138 }
2139
2140 static ir_tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
2141 {
2142         ir_tarval *all_one   = get_mode_all_one(mode);
2143         int        mode_size = get_mode_size_bits(mode);
2144
2145         assert(offset >= 0);
2146         assert(size   >= 0);
2147         assert(offset + size <= mode_size);
2148         if (size == mode_size) {
2149                 return all_one;
2150         }
2151
2152         long       shiftr    = get_mode_size_bits(mode) - size;
2153         long       shiftl    = offset;
2154         ir_tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
2155         ir_tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
2156         ir_tarval *mask0     = tarval_shr(all_one, tv_shiftr);
2157         ir_tarval *mask1     = tarval_shl(mask0, tv_shiftl);
2158
2159         return mask1;
2160 }
2161
2162 static ir_node *bitfield_store_to_firm(dbg_info *dbgi,
2163                 ir_entity *entity, ir_node *addr, ir_node *value, bool set_volatile)
2164 {
2165         ir_type *entity_type = get_entity_type(entity);
2166         ir_type *base_type   = get_primitive_base_type(entity_type);
2167         assert(base_type != NULL);
2168         ir_mode *mode        = get_type_mode(base_type);
2169
2170         value = create_conv(dbgi, value, mode);
2171
2172         /* kill upper bits of value and shift to right position */
2173         int      bitoffset    = get_entity_offset_bits_remainder(entity);
2174         int      bitsize      = get_mode_size_bits(get_type_mode(entity_type));
2175
2176         ir_tarval *mask            = create_bitfield_mask(mode, 0, bitsize);
2177         ir_node   *mask_node       = new_d_Const(dbgi, mask);
2178         ir_node   *value_masked    = new_d_And(dbgi, value, mask_node, mode);
2179         ir_tarval *shiftl          = new_tarval_from_long(bitoffset, mode_uint);
2180         ir_node   *shiftcount      = new_d_Const(dbgi, shiftl);
2181         ir_node   *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
2182
2183         /* load current value */
2184         ir_node   *mem             = get_store();
2185         ir_node   *load            = new_d_Load(dbgi, mem, addr, mode,
2186                                           set_volatile ? cons_volatile : cons_none);
2187         ir_node   *load_mem        = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2188         ir_node   *load_res        = new_d_Proj(dbgi, load, mode, pn_Load_res);
2189         ir_tarval *shift_mask      = create_bitfield_mask(mode, bitoffset, bitsize);
2190         ir_tarval *inv_mask        = tarval_not(shift_mask);
2191         ir_node   *inv_mask_node   = new_d_Const(dbgi, inv_mask);
2192         ir_node   *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
2193
2194         /* construct new value and store */
2195         ir_node *new_val   = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
2196         ir_node *store     = new_d_Store(dbgi, load_mem, addr, new_val,
2197                                          set_volatile ? cons_volatile : cons_none);
2198         ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2199         set_store(store_mem);
2200
2201         return value_masked;
2202 }
2203
2204 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
2205                 ir_node *addr)
2206 {
2207         dbg_info *dbgi     = get_dbg_info(&expression->base.source_position);
2208         type_t   *type     = expression->base.type;
2209         ir_mode  *mode     = get_ir_mode_storage(type);
2210         ir_node  *mem      = get_store();
2211         ir_node  *load     = new_d_Load(dbgi, mem, addr, mode, cons_none);
2212         ir_node  *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2213         ir_node  *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2214
2215         load_res           = create_conv(dbgi, load_res, mode_int);
2216
2217         set_store(load_mem);
2218
2219         /* kill upper bits */
2220         assert(expression->compound_entry->kind == ENTITY_COMPOUND_MEMBER);
2221         ir_entity *entity       = expression->compound_entry->compound_member.entity;
2222         int        bitoffset    = get_entity_offset_bits_remainder(entity);
2223         ir_type   *entity_type  = get_entity_type(entity);
2224         int        bitsize      = get_mode_size_bits(get_type_mode(entity_type));
2225         long       shift_bitsl  = machine_size - bitoffset - bitsize;
2226         assert(shift_bitsl >= 0);
2227         ir_tarval *tvl          = new_tarval_from_long(shift_bitsl, mode_uint);
2228         ir_node   *countl       = new_d_Const(dbgi, tvl);
2229         ir_node   *shiftl       = new_d_Shl(dbgi, load_res, countl, mode_int);
2230
2231         long       shift_bitsr  = bitoffset + shift_bitsl;
2232         assert(shift_bitsr <= (long) machine_size);
2233         ir_tarval *tvr          = new_tarval_from_long(shift_bitsr, mode_uint);
2234         ir_node   *countr       = new_d_Const(dbgi, tvr);
2235         ir_node   *shiftr;
2236         if (mode_is_signed(mode)) {
2237                 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
2238         } else {
2239                 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
2240         }
2241
2242         return create_conv(dbgi, shiftr, mode);
2243 }
2244
2245 /* make sure the selected compound type is constructed */
2246 static void construct_select_compound(const select_expression_t *expression)
2247 {
2248         type_t *type = skip_typeref(expression->compound->base.type);
2249         if (is_type_pointer(type)) {
2250                 type = type->pointer.points_to;
2251         }
2252         (void) get_ir_type(type);
2253 }
2254
2255 static ir_node *set_value_for_expression_addr(const expression_t *expression,
2256                                               ir_node *value, ir_node *addr)
2257 {
2258         dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2259         type_t   *type = skip_typeref(expression->base.type);
2260
2261         if (!is_type_compound(type)) {
2262                 ir_mode  *mode = get_ir_mode_storage(type);
2263                 value          = create_conv(dbgi, value, mode);
2264                 value          = do_strict_conv(dbgi, value);
2265         }
2266
2267         if (expression->kind == EXPR_REFERENCE) {
2268                 const reference_expression_t *ref = &expression->reference;
2269
2270                 entity_t *entity = ref->entity;
2271                 assert(is_declaration(entity));
2272                 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2273                 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2274                         set_value(entity->variable.v.value_number, value);
2275                         return value;
2276                 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2277                         set_value(entity->parameter.v.value_number, value);
2278                         return value;
2279                 }
2280         }
2281
2282         if (addr == NULL)
2283                 addr = expression_to_addr(expression);
2284         assert(addr != NULL);
2285
2286         if (expression->kind == EXPR_SELECT) {
2287                 const select_expression_t *select = &expression->select;
2288
2289                 construct_select_compound(select);
2290
2291                 entity_t *entity = select->compound_entry;
2292                 assert(entity->kind == ENTITY_COMPOUND_MEMBER);
2293                 if (entity->declaration.type->kind == TYPE_BITFIELD) {
2294                         ir_entity *irentity = entity->compound_member.entity;
2295                         bool       set_volatile
2296                                 = select->base.type->base.qualifiers & TYPE_QUALIFIER_VOLATILE;
2297                         value = bitfield_store_to_firm(dbgi, irentity, addr, value,
2298                                                        set_volatile);
2299                         return value;
2300                 }
2301         }
2302
2303         assign_value(dbgi, addr, type, value);
2304         return value;
2305 }
2306
2307 static void set_value_for_expression(const expression_t *expression,
2308                                      ir_node *value)
2309 {
2310         set_value_for_expression_addr(expression, value, NULL);
2311 }
2312
2313 static ir_node *get_value_from_lvalue(const expression_t *expression,
2314                                       ir_node *addr)
2315 {
2316         if (expression->kind == EXPR_REFERENCE) {
2317                 const reference_expression_t *ref = &expression->reference;
2318
2319                 entity_t *entity = ref->entity;
2320                 assert(entity->kind == ENTITY_VARIABLE
2321                                 || entity->kind == ENTITY_PARAMETER);
2322                 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2323                 int value_number;
2324                 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2325                         value_number = entity->variable.v.value_number;
2326                         assert(addr == NULL);
2327                         type_t  *type = skip_typeref(expression->base.type);
2328                         ir_mode *mode = get_ir_mode_storage(type);
2329                         ir_node *res  = get_value(value_number, mode);
2330                         return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2331                 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2332                         value_number = entity->parameter.v.value_number;
2333                         assert(addr == NULL);
2334                         type_t  *type = skip_typeref(expression->base.type);
2335                         ir_mode *mode = get_ir_mode_storage(type);
2336                         ir_node *res  = get_value(value_number, mode);
2337                         return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2338                 }
2339         }
2340
2341         assert(addr != NULL);
2342         dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2343
2344         ir_node *value;
2345         if (expression->kind == EXPR_SELECT &&
2346             expression->select.compound_entry->declaration.type->kind == TYPE_BITFIELD){
2347             construct_select_compound(&expression->select);
2348                 value = bitfield_extract_to_firm(&expression->select, addr);
2349         } else {
2350                 value = deref_address(dbgi, expression->base.type, addr);
2351         }
2352
2353         return value;
2354 }
2355
2356
2357 static ir_node *create_incdec(const unary_expression_t *expression)
2358 {
2359         dbg_info *const     dbgi = get_dbg_info(&expression->base.source_position);
2360         const expression_t *value_expr = expression->value;
2361         ir_node            *addr       = expression_to_addr(value_expr);
2362         ir_node            *value      = get_value_from_lvalue(value_expr, addr);
2363
2364         type_t  *type = skip_typeref(expression->base.type);
2365         ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2366
2367         ir_node *offset;
2368         if (is_type_pointer(type)) {
2369                 pointer_type_t *pointer_type = &type->pointer;
2370                 offset = get_type_size_node(pointer_type->points_to);
2371         } else {
2372                 assert(is_type_arithmetic(type));
2373                 offset = new_Const(get_mode_one(mode));
2374         }
2375
2376         ir_node *result;
2377         ir_node *store_value;
2378         switch(expression->base.kind) {
2379         case EXPR_UNARY_POSTFIX_INCREMENT:
2380                 result      = value;
2381                 store_value = new_d_Add(dbgi, value, offset, mode);
2382                 break;
2383         case EXPR_UNARY_POSTFIX_DECREMENT:
2384                 result      = value;
2385                 store_value = new_d_Sub(dbgi, value, offset, mode);
2386                 break;
2387         case EXPR_UNARY_PREFIX_INCREMENT:
2388                 result      = new_d_Add(dbgi, value, offset, mode);
2389                 store_value = result;
2390                 break;
2391         case EXPR_UNARY_PREFIX_DECREMENT:
2392                 result      = new_d_Sub(dbgi, value, offset, mode);
2393                 store_value = result;
2394                 break;
2395         default:
2396                 panic("no incdec expr in create_incdec");
2397         }
2398
2399         set_value_for_expression_addr(value_expr, store_value, addr);
2400
2401         return result;
2402 }
2403
2404 static bool is_local_variable(expression_t *expression)
2405 {
2406         if (expression->kind != EXPR_REFERENCE)
2407                 return false;
2408         reference_expression_t *ref_expr = &expression->reference;
2409         entity_t               *entity   = ref_expr->entity;
2410         if (entity->kind != ENTITY_VARIABLE)
2411                 return false;
2412         assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2413         return entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE;
2414 }
2415
2416 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
2417 {
2418         switch(kind) {
2419         case EXPR_BINARY_EQUAL:         return pn_Cmp_Eq;
2420         case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
2421         case EXPR_BINARY_NOTEQUAL:
2422                 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
2423         case EXPR_BINARY_ISLESS:
2424         case EXPR_BINARY_LESS:          return pn_Cmp_Lt;
2425         case EXPR_BINARY_ISLESSEQUAL:
2426         case EXPR_BINARY_LESSEQUAL:     return pn_Cmp_Le;
2427         case EXPR_BINARY_ISGREATER:
2428         case EXPR_BINARY_GREATER:       return pn_Cmp_Gt;
2429         case EXPR_BINARY_ISGREATEREQUAL:
2430         case EXPR_BINARY_GREATEREQUAL:  return pn_Cmp_Ge;
2431         case EXPR_BINARY_ISUNORDERED:   return pn_Cmp_Uo;
2432
2433         default:
2434                 break;
2435         }
2436         panic("trying to get pn_Cmp from non-comparison binexpr type");
2437 }
2438
2439 /**
2440  * Handle the assume optimizer hint: check if a Confirm
2441  * node can be created.
2442  *
2443  * @param dbi    debug info
2444  * @param expr   the IL assume expression
2445  *
2446  * we support here only some simple cases:
2447  *  - var rel const
2448  *  - const rel val
2449  *  - var rel var
2450  */
2451 static ir_node *handle_assume_compare(dbg_info *dbi,
2452                                       const binary_expression_t *expression)
2453 {
2454         expression_t *op1 = expression->left;
2455         expression_t *op2 = expression->right;
2456         entity_t     *var2, *var = NULL;
2457         ir_node      *res = NULL;
2458         pn_Cmp        cmp_val;
2459
2460         cmp_val = get_pnc(expression->base.kind, op1->base.type);
2461
2462         if (is_local_variable(op1) && is_local_variable(op2)) {
2463                 var  = op1->reference.entity;
2464             var2 = op2->reference.entity;
2465
2466                 type_t  *const type = skip_typeref(var->declaration.type);
2467                 ir_mode *const mode = get_ir_mode_storage(type);
2468
2469                 ir_node *const irn1 = get_value(var->variable.v.value_number, mode);
2470                 ir_node *const irn2 = get_value(var2->variable.v.value_number, mode);
2471
2472                 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
2473                 set_value(var2->variable.v.value_number, res);
2474
2475                 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
2476                 set_value(var->variable.v.value_number, res);
2477
2478                 return res;
2479         }
2480
2481         expression_t *con;
2482         if (is_local_variable(op1) && is_constant_expression(op2)) {
2483                 var = op1->reference.entity;
2484                 con = op2;
2485         } else if (is_constant_expression(op1) && is_local_variable(op2)) {
2486                 cmp_val = get_inversed_pnc(cmp_val);
2487                 var = op2->reference.entity;
2488                 con = op1;
2489         }
2490
2491         if (var != NULL) {
2492                 type_t  *const type = skip_typeref(var->declaration.type);
2493                 ir_mode *const mode = get_ir_mode_storage(type);
2494
2495                 res = get_value(var->variable.v.value_number, mode);
2496                 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
2497                 set_value(var->variable.v.value_number, res);
2498         }
2499         return res;
2500 }
2501
2502 /**
2503  * Handle the assume optimizer hint.
2504  *
2505  * @param dbi    debug info
2506  * @param expr   the IL assume expression
2507  */
2508 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression)
2509 {
2510         switch(expression->kind) {
2511         case EXPR_BINARY_EQUAL:
2512         case EXPR_BINARY_NOTEQUAL:
2513         case EXPR_BINARY_LESS:
2514         case EXPR_BINARY_LESSEQUAL:
2515         case EXPR_BINARY_GREATER:
2516         case EXPR_BINARY_GREATEREQUAL:
2517                 return handle_assume_compare(dbi, &expression->binary);
2518         default:
2519                 return NULL;
2520         }
2521 }
2522
2523 static ir_node *create_cast(dbg_info *dbgi,     ir_node *value_node,
2524                             type_t *from_type, type_t *type)
2525 {
2526         type = skip_typeref(type);
2527         if (!is_type_scalar(type)) {
2528                 /* make sure firm type is constructed */
2529                 (void) get_ir_type(type);
2530                 return value_node;
2531         }
2532
2533         from_type     = skip_typeref(from_type);
2534         ir_mode *mode = get_ir_mode_storage(type);
2535         /* check for conversion from / to __based types */
2536         if (is_type_pointer(type) && is_type_pointer(from_type)) {
2537                 const variable_t *from_var = from_type->pointer.base_variable;
2538                 const variable_t *to_var   = type->pointer.base_variable;
2539                 if (from_var != to_var) {
2540                         if (from_var != NULL) {
2541                                 ir_node *const addr = get_global_var_address(dbgi, from_var);
2542                                 ir_node *const base = deref_address(dbgi, from_var->base.type, addr);
2543                                 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(from_type));
2544                         }
2545                         if (to_var != NULL) {
2546                                 ir_node *const addr = get_global_var_address(dbgi, to_var);
2547                                 ir_node *const base = deref_address(dbgi, to_var->base.type, addr);
2548                                 value_node = new_d_Sub(dbgi, value_node, base, mode);
2549                         }
2550                 }
2551         }
2552
2553         if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
2554                 /* bool adjustments (we save a mode_Bu, but have to temporarily
2555                  * convert to mode_b so we only get a 0/1 value */
2556                 value_node = create_conv(dbgi, value_node, mode_b);
2557         }
2558
2559         ir_mode *mode_arith = get_ir_mode_arithmetic(type);
2560         ir_node *node       = create_conv(dbgi, value_node, mode);
2561         node                = do_strict_conv(dbgi, node);
2562         node                = create_conv(dbgi, node, mode_arith);
2563
2564         return node;
2565 }
2566
2567 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2568 {
2569         dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2570         type_t   *type = skip_typeref(expression->base.type);
2571
2572         if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2573                 return expression_to_addr(expression->value);
2574
2575         const expression_t *value = expression->value;
2576
2577         switch(expression->base.kind) {
2578         case EXPR_UNARY_NEGATE: {
2579                 ir_node *value_node = expression_to_firm(value);
2580                 ir_mode *mode       = get_ir_mode_arithmetic(type);
2581                 return new_d_Minus(dbgi, value_node, mode);
2582         }
2583         case EXPR_UNARY_PLUS:
2584                 return expression_to_firm(value);
2585         case EXPR_UNARY_BITWISE_NEGATE: {
2586                 ir_node *value_node = expression_to_firm(value);
2587                 ir_mode *mode       = get_ir_mode_arithmetic(type);
2588                 return new_d_Not(dbgi, value_node, mode);
2589         }
2590         case EXPR_UNARY_NOT: {
2591                 ir_node *value_node = _expression_to_firm(value);
2592                 value_node          = create_conv(dbgi, value_node, mode_b);
2593                 ir_node *res        = new_d_Not(dbgi, value_node, mode_b);
2594                 return res;
2595         }
2596         case EXPR_UNARY_DEREFERENCE: {
2597                 ir_node *value_node = expression_to_firm(value);
2598                 type_t  *value_type = skip_typeref(value->base.type);
2599                 assert(is_type_pointer(value_type));
2600
2601                 /* check for __based */
2602                 const variable_t *const base_var = value_type->pointer.base_variable;
2603                 if (base_var != NULL) {
2604                         ir_node *const addr = get_global_var_address(dbgi, base_var);
2605                         ir_node *const base = deref_address(dbgi, base_var->base.type, addr);
2606                         value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(value_type));
2607                 }
2608                 type_t  *points_to  = value_type->pointer.points_to;
2609                 return deref_address(dbgi, points_to, value_node);
2610         }
2611         case EXPR_UNARY_POSTFIX_INCREMENT:
2612         case EXPR_UNARY_POSTFIX_DECREMENT:
2613         case EXPR_UNARY_PREFIX_INCREMENT:
2614         case EXPR_UNARY_PREFIX_DECREMENT:
2615                 return create_incdec(expression);
2616         case EXPR_UNARY_CAST_IMPLICIT:
2617         case EXPR_UNARY_CAST: {
2618                 ir_node *value_node = expression_to_firm(value);
2619                 type_t  *from_type  = value->base.type;
2620                 return create_cast(dbgi, value_node, from_type, type);
2621         }
2622         case EXPR_UNARY_ASSUME:
2623                 return handle_assume(dbgi, value);
2624
2625         default:
2626                 break;
2627         }
2628         panic("invalid UNEXPR type found");
2629 }
2630
2631 /**
2632  * produces a 0/1 depending of the value of a mode_b node
2633  */
2634 static ir_node *produce_condition_result(const expression_t *expression,
2635                                          ir_mode *mode, dbg_info *dbgi)
2636 {
2637         ir_node *cur_block = get_cur_block();
2638
2639         ir_node *one_block = new_immBlock();
2640         set_cur_block(one_block);
2641         ir_node *one       = new_Const(get_mode_one(mode));
2642         ir_node *jmp_one   = new_d_Jmp(dbgi);
2643
2644         ir_node *zero_block = new_immBlock();
2645         set_cur_block(zero_block);
2646         ir_node *zero       = new_Const(get_mode_null(mode));
2647         ir_node *jmp_zero   = new_d_Jmp(dbgi);
2648
2649         set_cur_block(cur_block);
2650         create_condition_evaluation(expression, one_block, zero_block);
2651         mature_immBlock(one_block);
2652         mature_immBlock(zero_block);
2653
2654         ir_node *in_cf[2] = { jmp_one, jmp_zero };
2655         ir_node *block = new_Block(2, in_cf);
2656         set_cur_block(block);
2657
2658         ir_node *in[2] = { one, zero };
2659         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
2660
2661         return val;
2662 }
2663
2664 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2665                 ir_node *value, type_t *type)
2666 {
2667         ir_mode        *const mode         = get_ir_mode_arithmetic(type_ptrdiff_t);
2668         assert(is_type_pointer(type));
2669         pointer_type_t *const pointer_type = &type->pointer;
2670         type_t         *const points_to    = skip_typeref(pointer_type->points_to);
2671         ir_node        *      elem_size    = get_type_size_node(points_to);
2672         elem_size                          = create_conv(dbgi, elem_size, mode);
2673         value                              = create_conv(dbgi, value,     mode);
2674         ir_node        *const mul          = new_d_Mul(dbgi, value, elem_size, mode);
2675         return mul;
2676 }
2677
2678 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2679                           ir_node *left, ir_node *right)
2680 {
2681         ir_mode  *mode;
2682         type_t   *type_left  = skip_typeref(expression->left->base.type);
2683         type_t   *type_right = skip_typeref(expression->right->base.type);
2684
2685         expression_kind_t kind = expression->base.kind;
2686
2687         switch (kind) {
2688         case EXPR_BINARY_SHIFTLEFT:
2689         case EXPR_BINARY_SHIFTRIGHT:
2690         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2691         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2692                 mode  = get_irn_mode(left);
2693                 right = create_conv(dbgi, right, mode_uint);
2694                 break;
2695
2696         case EXPR_BINARY_SUB:
2697                 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2698                         const pointer_type_t *const ptr_type = &type_left->pointer;
2699
2700                         mode = get_ir_mode_arithmetic(expression->base.type);
2701                         ir_node *const elem_size = get_type_size_node(ptr_type->points_to);
2702                         ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2703                         ir_node *const sub       = new_d_Sub(dbgi, left, right, mode);
2704                         ir_node *const no_mem    = new_NoMem();
2705                         ir_node *const div       = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2706                                                                                                    mode, op_pin_state_floats);
2707                         return new_d_Proj(dbgi, div, mode, pn_Div_res);
2708                 }
2709                 /* fallthrough */
2710         case EXPR_BINARY_SUB_ASSIGN:
2711                 if (is_type_pointer(type_left)) {
2712                         right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2713                         mode  = get_ir_mode_arithmetic(type_left);
2714                         break;
2715                 }
2716                 goto normal_node;
2717
2718         case EXPR_BINARY_ADD:
2719         case EXPR_BINARY_ADD_ASSIGN:
2720                 if (is_type_pointer(type_left)) {
2721                         right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2722                         mode  = get_ir_mode_arithmetic(type_left);
2723                         break;
2724                 } else if (is_type_pointer(type_right)) {
2725                         left  = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2726                         mode  = get_ir_mode_arithmetic(type_right);
2727                         break;
2728                 }
2729                 goto normal_node;
2730
2731         default:
2732 normal_node:
2733                 mode = get_ir_mode_arithmetic(type_right);
2734                 left = create_conv(dbgi, left, mode);
2735                 break;
2736         }
2737
2738         switch (kind) {
2739         case EXPR_BINARY_ADD_ASSIGN:
2740         case EXPR_BINARY_ADD:
2741                 return new_d_Add(dbgi, left, right, mode);
2742         case EXPR_BINARY_SUB_ASSIGN:
2743         case EXPR_BINARY_SUB:
2744                 return new_d_Sub(dbgi, left, right, mode);
2745         case EXPR_BINARY_MUL_ASSIGN:
2746         case EXPR_BINARY_MUL:
2747                 return new_d_Mul(dbgi, left, right, mode);
2748         case EXPR_BINARY_BITWISE_AND:
2749         case EXPR_BINARY_BITWISE_AND_ASSIGN:
2750                 return new_d_And(dbgi, left, right, mode);
2751         case EXPR_BINARY_BITWISE_OR:
2752         case EXPR_BINARY_BITWISE_OR_ASSIGN:
2753                 return new_d_Or(dbgi, left, right, mode);
2754         case EXPR_BINARY_BITWISE_XOR:
2755         case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2756                 return new_d_Eor(dbgi, left, right, mode);
2757         case EXPR_BINARY_SHIFTLEFT:
2758         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2759                 return new_d_Shl(dbgi, left, right, mode);
2760         case EXPR_BINARY_SHIFTRIGHT:
2761         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2762                 if (mode_is_signed(mode)) {
2763                         return new_d_Shrs(dbgi, left, right, mode);
2764                 } else {
2765                         return new_d_Shr(dbgi, left, right, mode);
2766                 }
2767         case EXPR_BINARY_DIV:
2768         case EXPR_BINARY_DIV_ASSIGN: {
2769                 ir_node *pin = new_Pin(new_NoMem());
2770                 ir_node *op;
2771                 ir_node *res;
2772                 if (mode_is_float(mode)) {
2773                         op  = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2774                         res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2775                 } else {
2776                         op  = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2777                         res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2778                 }
2779                 return res;
2780         }
2781         case EXPR_BINARY_MOD:
2782         case EXPR_BINARY_MOD_ASSIGN: {
2783                 ir_node *pin = new_Pin(new_NoMem());
2784                 assert(!mode_is_float(mode));
2785                 ir_node *op  = new_d_Mod(dbgi, pin, left, right, mode,
2786                                          op_pin_state_floats);
2787                 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2788                 return res;
2789         }
2790         default:
2791                 panic("unexpected expression kind");
2792         }
2793 }
2794
2795 static ir_node *create_lazy_op(const binary_expression_t *expression)
2796 {
2797         dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2798         type_t   *type = skip_typeref(expression->base.type);
2799         ir_mode  *mode = get_ir_mode_arithmetic(type);
2800
2801         if (is_constant_expression(expression->left)) {
2802                 bool val = fold_constant_to_bool(expression->left);
2803                 expression_kind_t ekind = expression->base.kind;
2804                 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2805                 if (ekind == EXPR_BINARY_LOGICAL_AND) {
2806                         if (!val) {
2807                                 return new_Const(get_mode_null(mode));
2808                         }
2809                 } else {
2810                         if (val) {
2811                                 return new_Const(get_mode_one(mode));
2812                         }
2813                 }
2814
2815                 if (is_constant_expression(expression->right)) {
2816                         bool valr = fold_constant_to_bool(expression->right);
2817                         return valr ?
2818                                 new_Const(get_mode_one(mode)) :
2819                                 new_Const(get_mode_null(mode));
2820                 }
2821
2822                 return produce_condition_result(expression->right, mode, dbgi);
2823         }
2824
2825         return produce_condition_result((const expression_t*) expression, mode,
2826                                         dbgi);
2827 }
2828
2829 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2830                                             ir_node *right, ir_mode *mode);
2831
2832 static ir_node *create_assign_binop(const binary_expression_t *expression)
2833 {
2834         dbg_info *const     dbgi = get_dbg_info(&expression->base.source_position);
2835         const expression_t *left_expr = expression->left;
2836         type_t             *type      = skip_typeref(left_expr->base.type);
2837         ir_node            *right     = expression_to_firm(expression->right);
2838         ir_node            *left_addr = expression_to_addr(left_expr);
2839         ir_node            *left      = get_value_from_lvalue(left_expr, left_addr);
2840         ir_node            *result    = create_op(dbgi, expression, left, right);
2841
2842         result = create_cast(dbgi, result, expression->right->base.type, type);
2843         result = do_strict_conv(dbgi, result);
2844
2845         result = set_value_for_expression_addr(left_expr, result, left_addr);
2846
2847         if (!is_type_compound(type)) {
2848                 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2849                 result = create_conv(dbgi, result, mode_arithmetic);
2850         }
2851         return result;
2852 }
2853
2854 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2855 {
2856         expression_kind_t kind = expression->base.kind;
2857
2858         switch(kind) {
2859         case EXPR_BINARY_EQUAL:
2860         case EXPR_BINARY_NOTEQUAL:
2861         case EXPR_BINARY_LESS:
2862         case EXPR_BINARY_LESSEQUAL:
2863         case EXPR_BINARY_GREATER:
2864         case EXPR_BINARY_GREATEREQUAL:
2865         case EXPR_BINARY_ISGREATER:
2866         case EXPR_BINARY_ISGREATEREQUAL:
2867         case EXPR_BINARY_ISLESS:
2868         case EXPR_BINARY_ISLESSEQUAL:
2869         case EXPR_BINARY_ISLESSGREATER:
2870         case EXPR_BINARY_ISUNORDERED: {
2871                 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2872                 ir_node *left  = expression_to_firm(expression->left);
2873                 ir_node *right = expression_to_firm(expression->right);
2874                 ir_node *cmp   = new_d_Cmp(dbgi, left, right);
2875                 long     pnc   = get_pnc(kind, expression->left->base.type);
2876                 ir_node *proj  = new_d_Proj(dbgi, cmp, mode_b, pnc);
2877                 return proj;
2878         }
2879         case EXPR_BINARY_ASSIGN: {
2880                 ir_node *addr  = expression_to_addr(expression->left);
2881                 ir_node *right = expression_to_firm(expression->right);
2882                 ir_node *res
2883                         = set_value_for_expression_addr(expression->left, right, addr);
2884
2885                 type_t  *type            = skip_typeref(expression->base.type);
2886                 if (!is_type_compound(type)) {
2887                         ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2888                         res                      = create_conv(NULL, res, mode_arithmetic);
2889                 }
2890                 return res;
2891         }
2892         case EXPR_BINARY_ADD:
2893         case EXPR_BINARY_SUB:
2894         case EXPR_BINARY_MUL:
2895         case EXPR_BINARY_DIV:
2896         case EXPR_BINARY_MOD:
2897         case EXPR_BINARY_BITWISE_AND:
2898         case EXPR_BINARY_BITWISE_OR:
2899         case EXPR_BINARY_BITWISE_XOR:
2900         case EXPR_BINARY_SHIFTLEFT:
2901         case EXPR_BINARY_SHIFTRIGHT:
2902         {
2903                 dbg_info *dbgi  = get_dbg_info(&expression->base.source_position);
2904                 ir_node  *left  = expression_to_firm(expression->left);
2905                 ir_node  *right = expression_to_firm(expression->right);
2906                 return create_op(dbgi, expression, left, right);
2907         }
2908         case EXPR_BINARY_LOGICAL_AND:
2909         case EXPR_BINARY_LOGICAL_OR:
2910                 return create_lazy_op(expression);
2911         case EXPR_BINARY_COMMA:
2912                 /* create side effects of left side */
2913                 (void) expression_to_firm(expression->left);
2914                 return _expression_to_firm(expression->right);
2915
2916         case EXPR_BINARY_ADD_ASSIGN:
2917         case EXPR_BINARY_SUB_ASSIGN:
2918         case EXPR_BINARY_MUL_ASSIGN:
2919         case EXPR_BINARY_MOD_ASSIGN:
2920         case EXPR_BINARY_DIV_ASSIGN:
2921         case EXPR_BINARY_BITWISE_AND_ASSIGN:
2922         case EXPR_BINARY_BITWISE_OR_ASSIGN:
2923         case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2924         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2925         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2926                 return create_assign_binop(expression);
2927         default:
2928                 panic("TODO binexpr type");
2929         }
2930 }
2931
2932 static ir_node *array_access_addr(const array_access_expression_t *expression)
2933 {
2934         dbg_info *dbgi        = get_dbg_info(&expression->base.source_position);
2935         ir_node  *base_addr   = expression_to_firm(expression->array_ref);
2936         ir_node  *offset      = expression_to_firm(expression->index);
2937         type_t   *ref_type    = skip_typeref(expression->array_ref->base.type);
2938         ir_node  *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
2939         ir_node  *result      = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2940
2941         return result;
2942 }
2943
2944 static ir_node *array_access_to_firm(
2945                 const array_access_expression_t *expression)
2946 {
2947         dbg_info *dbgi   = get_dbg_info(&expression->base.source_position);
2948         ir_node  *addr   = array_access_addr(expression);
2949         type_t   *type   = revert_automatic_type_conversion(
2950                         (const expression_t*) expression);
2951         type             = skip_typeref(type);
2952
2953         return deref_address(dbgi, type, addr);
2954 }
2955
2956 static long get_offsetof_offset(const offsetof_expression_t *expression)
2957 {
2958         type_t *orig_type = expression->type;
2959         long    offset    = 0;
2960
2961         designator_t *designator = expression->designator;
2962         for ( ; designator != NULL; designator = designator->next) {
2963                 type_t *type = skip_typeref(orig_type);
2964                 /* be sure the type is constructed */
2965                 (void) get_ir_type(type);
2966
2967                 if (designator->symbol != NULL) {
2968                         assert(is_type_compound(type));
2969                         symbol_t *symbol = designator->symbol;
2970
2971                         compound_t *compound = type->compound.compound;
2972                         entity_t   *iter     = compound->members.entities;
2973                         for ( ; iter != NULL; iter = iter->base.next) {
2974                                 if (iter->base.symbol == symbol) {
2975                                         break;
2976                                 }
2977                         }
2978                         assert(iter != NULL);
2979
2980                         assert(iter->kind == ENTITY_COMPOUND_MEMBER);
2981                         assert(iter->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
2982                         offset += get_entity_offset(iter->compound_member.entity);
2983
2984                         orig_type = iter->declaration.type;
2985                 } else {
2986                         expression_t *array_index = designator->array_index;
2987                         assert(designator->array_index != NULL);
2988                         assert(is_type_array(type));
2989
2990                         long index         = fold_constant_to_int(array_index);
2991                         ir_type *arr_type  = get_ir_type(type);
2992                         ir_type *elem_type = get_array_element_type(arr_type);
2993                         long     elem_size = get_type_size_bytes(elem_type);
2994
2995                         offset += index * elem_size;
2996
2997                         orig_type = type->array.element_type;
2998                 }
2999         }
3000
3001         return offset;
3002 }
3003
3004 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
3005 {
3006         ir_mode   *mode   = get_ir_mode_arithmetic(expression->base.type);
3007         long       offset = get_offsetof_offset(expression);
3008         ir_tarval *tv     = new_tarval_from_long(offset, mode);
3009         dbg_info  *dbgi   = get_dbg_info(&expression->base.source_position);
3010
3011         return new_d_Const(dbgi, tv);
3012 }
3013
3014 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3015                                      ir_entity *entity, type_t *type);
3016
3017 static ir_node *compound_literal_to_firm(
3018                 const compound_literal_expression_t *expression)
3019 {
3020         type_t *type = expression->type;
3021
3022         /* create an entity on the stack */
3023         ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3024
3025         ident     *const id     = id_unique("CompLit.%u");
3026         ir_type   *const irtype = get_ir_type(type);
3027         dbg_info  *const dbgi   = get_dbg_info(&expression->base.source_position);
3028         ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
3029         set_entity_ld_ident(entity, id);
3030
3031         /* create initialisation code */
3032         initializer_t *initializer = expression->initializer;
3033         create_local_initializer(initializer, dbgi, entity, type);
3034
3035         /* create a sel for the compound literal address */
3036         ir_node *frame = get_irg_frame(current_ir_graph);
3037         ir_node *sel   = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3038         return sel;
3039 }
3040
3041 /**
3042  * Transform a sizeof expression into Firm code.
3043  */
3044 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
3045 {
3046         type_t *const type = skip_typeref(expression->type);
3047         /* ยง6.5.3.4:2 if the type is a VLA, evaluate the expression. */
3048         if (is_type_array(type) && type->array.is_vla
3049                         && expression->tp_expression != NULL) {
3050                 expression_to_firm(expression->tp_expression);
3051         }
3052
3053         return get_type_size_node(type);
3054 }
3055
3056 static entity_t *get_expression_entity(const expression_t *expression)
3057 {
3058         if (expression->kind != EXPR_REFERENCE)
3059                 return NULL;
3060
3061         return expression->reference.entity;
3062 }
3063
3064 static unsigned get_cparser_entity_alignment(const entity_t *entity)
3065 {
3066         switch(entity->kind) {
3067         DECLARATION_KIND_CASES
3068                 return entity->declaration.alignment;
3069         case ENTITY_STRUCT:
3070         case ENTITY_UNION:
3071                 return entity->compound.alignment;
3072         case ENTITY_TYPEDEF:
3073                 return entity->typedefe.alignment;
3074         default:
3075                 break;
3076         }
3077         return 0;
3078 }
3079
3080 /**
3081  * Transform an alignof expression into Firm code.
3082  */
3083 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
3084 {
3085         unsigned alignment = 0;
3086
3087         const expression_t *tp_expression = expression->tp_expression;
3088         if (tp_expression != NULL) {
3089                 entity_t *entity = get_expression_entity(tp_expression);
3090                 if (entity != NULL) {
3091                         alignment = get_cparser_entity_alignment(entity);
3092                 }
3093         }
3094
3095         if (alignment == 0) {
3096                 type_t *type = expression->type;
3097                 alignment = get_type_alignment(type);
3098         }
3099
3100         dbg_info  *dbgi = get_dbg_info(&expression->base.source_position);
3101         ir_mode   *mode = get_ir_mode_arithmetic(expression->base.type);
3102         ir_tarval *tv   = new_tarval_from_long(alignment, mode);
3103         return new_d_Const(dbgi, tv);
3104 }
3105
3106 static void init_ir_types(void);
3107
3108 static ir_tarval *fold_constant_to_tarval(const expression_t *expression)
3109 {
3110         assert(is_type_valid(skip_typeref(expression->base.type)));
3111
3112         bool constant_folding_old = constant_folding;
3113         constant_folding = true;
3114
3115         init_ir_types();
3116
3117         assert(is_constant_expression(expression));
3118
3119         ir_graph *old_current_ir_graph = current_ir_graph;
3120         current_ir_graph = get_const_code_irg();
3121
3122         ir_node *cnst = expression_to_firm(expression);
3123         current_ir_graph = old_current_ir_graph;
3124
3125         if (!is_Const(cnst)) {
3126                 panic("couldn't fold constant");
3127         }
3128
3129         constant_folding = constant_folding_old;
3130
3131         return get_Const_tarval(cnst);
3132 }
3133
3134 long fold_constant_to_int(const expression_t *expression)
3135 {
3136         if (expression->kind == EXPR_INVALID)
3137                 return 0;
3138
3139         ir_tarval *tv = fold_constant_to_tarval(expression);
3140         if (!tarval_is_long(tv)) {
3141                 panic("result of constant folding is not integer");
3142         }
3143
3144         return get_tarval_long(tv);
3145 }
3146
3147 bool fold_constant_to_bool(const expression_t *expression)
3148 {
3149         if (expression->kind == EXPR_INVALID)
3150                 return false;
3151         ir_tarval *tv = fold_constant_to_tarval(expression);
3152         return !tarval_is_null(tv);
3153 }
3154
3155 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
3156 {
3157         dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3158
3159         /* first try to fold a constant condition */
3160         if (is_constant_expression(expression->condition)) {
3161                 bool val = fold_constant_to_bool(expression->condition);
3162                 if (val) {
3163                         expression_t *true_expression = expression->true_expression;
3164                         if (true_expression == NULL)
3165                                 true_expression = expression->condition;
3166                         return expression_to_firm(true_expression);
3167                 } else {
3168                         return expression_to_firm(expression->false_expression);
3169                 }
3170         }
3171
3172         ir_node *cur_block   = get_cur_block();
3173
3174         /* create the true block */
3175         ir_node *true_block  = new_immBlock();
3176         set_cur_block(true_block);
3177
3178         ir_node *true_val = expression->true_expression != NULL ?
3179                 expression_to_firm(expression->true_expression) : NULL;
3180         ir_node *true_jmp = new_Jmp();
3181
3182         /* create the false block */
3183         ir_node *false_block = new_immBlock();
3184         set_cur_block(false_block);
3185
3186         ir_node *false_val = expression_to_firm(expression->false_expression);
3187         ir_node *false_jmp = new_Jmp();
3188
3189         /* create the condition evaluation */
3190         set_cur_block(cur_block);
3191         ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
3192         if (expression->true_expression == NULL) {
3193                 if (cond_expr != NULL && get_irn_mode(cond_expr) != mode_b) {
3194                         true_val = cond_expr;
3195                 } else {
3196                         /* Condition ended with a short circuit (&&, ||, !) operation or a
3197                          * comparison.  Generate a "1" as value for the true branch. */
3198                         true_val = new_Const(get_mode_one(mode_Is));
3199                 }
3200         }
3201         mature_immBlock(true_block);
3202         mature_immBlock(false_block);
3203
3204         /* create the common block */
3205         ir_node *in_cf[2] = { true_jmp, false_jmp };
3206         ir_node *block = new_Block(2, in_cf);
3207         set_cur_block(block);
3208
3209         /* TODO improve static semantics, so either both or no values are NULL */
3210         if (true_val == NULL || false_val == NULL)
3211                 return NULL;
3212
3213         ir_node *in[2] = { true_val, false_val };
3214         ir_mode *mode  = get_irn_mode(true_val);
3215         assert(get_irn_mode(false_val) == mode);
3216         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
3217
3218         return val;
3219 }
3220
3221 /**
3222  * Returns an IR-node representing the address of a field.
3223  */
3224 static ir_node *select_addr(const select_expression_t *expression)
3225 {
3226         dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3227
3228         construct_select_compound(expression);
3229
3230         ir_node *compound_addr = expression_to_firm(expression->compound);
3231
3232         entity_t *entry = expression->compound_entry;
3233         assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3234         assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
3235
3236         if (constant_folding) {
3237                 ir_mode *mode = get_irn_mode(compound_addr);
3238                 /* FIXME: here, we need an integer mode with the same number of bits as mode */
3239                 ir_node *ofs  = new_Const_long(mode_uint, entry->compound_member.offset);
3240                 return new_d_Add(dbgi, compound_addr, ofs, mode);
3241         } else {
3242                 ir_entity *irentity = entry->compound_member.entity;
3243                 assert(irentity != NULL);
3244                 return new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
3245         }
3246 }
3247
3248 static ir_node *select_to_firm(const select_expression_t *expression)
3249 {
3250         dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3251         ir_node  *addr = select_addr(expression);
3252         type_t   *type = revert_automatic_type_conversion(
3253                         (const expression_t*) expression);
3254         type           = skip_typeref(type);
3255
3256         entity_t *entry      = expression->compound_entry;
3257         assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3258         type_t   *entry_type = skip_typeref(entry->declaration.type);
3259
3260         if (entry_type->kind == TYPE_BITFIELD) {
3261                 return bitfield_extract_to_firm(expression, addr);
3262         }
3263
3264         return deref_address(dbgi, type, addr);
3265 }
3266
3267 /* Values returned by __builtin_classify_type. */
3268 typedef enum gcc_type_class
3269 {
3270         no_type_class = -1,
3271         void_type_class,
3272         integer_type_class,
3273         char_type_class,
3274         enumeral_type_class,
3275         boolean_type_class,
3276         pointer_type_class,
3277         reference_type_class,
3278         offset_type_class,
3279         real_type_class,
3280         complex_type_class,
3281         function_type_class,
3282         method_type_class,
3283         record_type_class,
3284         union_type_class,
3285         array_type_class,
3286         string_type_class,
3287         set_type_class,
3288         file_type_class,
3289         lang_type_class
3290 } gcc_type_class;
3291
3292 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
3293 {
3294         type_t *type = expr->type_expression->base.type;
3295
3296         /* FIXME gcc returns different values depending on whether compiling C or C++
3297          * e.g. int x[10] is pointer_type_class in C, but array_type_class in C++ */
3298         gcc_type_class tc;
3299         for (;;) {
3300                 type = skip_typeref(type);
3301                 switch (type->kind) {
3302                         case TYPE_ATOMIC: {
3303                                 const atomic_type_t *const atomic_type = &type->atomic;
3304                                 switch (atomic_type->akind) {
3305                                         /* should not be reached */
3306                                         case ATOMIC_TYPE_INVALID:
3307                                                 tc = no_type_class;
3308                                                 goto make_const;
3309
3310                                         /* gcc cannot do that */
3311                                         case ATOMIC_TYPE_VOID:
3312                                                 tc = void_type_class;
3313                                                 goto make_const;
3314
3315                                         case ATOMIC_TYPE_WCHAR_T:   /* gcc handles this as integer */
3316                                         case ATOMIC_TYPE_CHAR:      /* gcc handles this as integer */
3317                                         case ATOMIC_TYPE_SCHAR:     /* gcc handles this as integer */
3318                                         case ATOMIC_TYPE_UCHAR:     /* gcc handles this as integer */
3319                                         case ATOMIC_TYPE_SHORT:
3320                                         case ATOMIC_TYPE_USHORT:
3321                                         case ATOMIC_TYPE_INT:
3322                                         case ATOMIC_TYPE_UINT:
3323                                         case ATOMIC_TYPE_LONG:
3324                                         case ATOMIC_TYPE_ULONG:
3325                                         case ATOMIC_TYPE_LONGLONG:
3326                                         case ATOMIC_TYPE_ULONGLONG:
3327                                         case ATOMIC_TYPE_BOOL:      /* gcc handles this as integer */
3328                                                 tc = integer_type_class;
3329                                                 goto make_const;
3330
3331                                         case ATOMIC_TYPE_FLOAT:
3332                                         case ATOMIC_TYPE_DOUBLE:
3333                                         case ATOMIC_TYPE_LONG_DOUBLE:
3334                                                 tc = real_type_class;
3335                                                 goto make_const;
3336                                 }
3337                                 panic("Unexpected atomic type in classify_type_to_firm().");
3338                         }
3339
3340                         case TYPE_COMPLEX:         tc = complex_type_class; goto make_const;
3341                         case TYPE_IMAGINARY:       tc = complex_type_class; goto make_const;
3342                         case TYPE_BITFIELD:        tc = integer_type_class; goto make_const;
3343                         case TYPE_ARRAY:           /* gcc handles this as pointer */
3344                         case TYPE_FUNCTION:        /* gcc handles this as pointer */
3345                         case TYPE_POINTER:         tc = pointer_type_class; goto make_const;
3346                         case TYPE_COMPOUND_STRUCT: tc = record_type_class;  goto make_const;
3347                         case TYPE_COMPOUND_UNION:  tc = union_type_class;   goto make_const;
3348
3349                         /* gcc handles this as integer */
3350                         case TYPE_ENUM:            tc = integer_type_class; goto make_const;
3351
3352                         /* gcc classifies the referenced type */
3353                         case TYPE_REFERENCE: type = type->reference.refers_to; continue;
3354
3355                         case TYPE_BUILTIN:
3356                         /* typedef/typeof should be skipped already */
3357                         case TYPE_TYPEDEF:
3358                         case TYPE_TYPEOF:
3359                         case TYPE_INVALID:
3360                         case TYPE_ERROR:
3361                                 break;
3362                 }
3363                 panic("unexpected TYPE classify_type_to_firm().");
3364         }
3365
3366 make_const:;
3367         dbg_info  *const dbgi = get_dbg_info(&expr->base.source_position);
3368         ir_tarval *const tv   = new_tarval_from_long(tc, mode_int);
3369         return new_d_Const(dbgi, tv);
3370 }
3371
3372 static ir_node *function_name_to_firm(
3373                 const funcname_expression_t *const expr)
3374 {
3375         switch(expr->kind) {
3376         case FUNCNAME_FUNCTION:
3377         case FUNCNAME_PRETTY_FUNCTION:
3378         case FUNCNAME_FUNCDNAME:
3379                 if (current_function_name == NULL) {
3380                         const source_position_t *const src_pos = &expr->base.source_position;
3381                         const char    *name  = current_function_entity->base.symbol->string;
3382                         const string_t string = { name, strlen(name) + 1 };
3383                         current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
3384                 }
3385                 return current_function_name;
3386         case FUNCNAME_FUNCSIG:
3387                 if (current_funcsig == NULL) {
3388                         const source_position_t *const src_pos = &expr->base.source_position;
3389                         ir_entity *ent = get_irg_entity(current_ir_graph);
3390                         const char *const name = get_entity_ld_name(ent);
3391                         const string_t string = { name, strlen(name) + 1 };
3392                         current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
3393                 }
3394                 return current_funcsig;
3395         }
3396         panic("Unsupported function name");
3397 }
3398
3399 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
3400 {
3401         statement_t *statement = expr->statement;
3402
3403         assert(statement->kind == STATEMENT_COMPOUND);
3404         return compound_statement_to_firm(&statement->compound);
3405 }
3406
3407 static ir_node *va_start_expression_to_firm(
3408         const va_start_expression_t *const expr)
3409 {
3410         type_t    *const type        = current_function_entity->declaration.type;
3411         ir_type   *const method_type = get_ir_type(type);
3412         int        const n           = get_method_n_params(method_type) - 1;
3413         ir_entity *const parm_ent    = get_method_value_param_ent(method_type, n);
3414         ir_node   *const frame       = get_irg_frame(current_ir_graph);
3415         dbg_info  *const dbgi        = get_dbg_info(&expr->base.source_position);
3416         ir_node   *const no_mem      = new_NoMem();
3417         ir_node   *const arg_sel     =
3418                 new_d_simpleSel(dbgi, no_mem, frame, parm_ent);
3419
3420         type_t    *const param_type  = expr->parameter->base.type;
3421         ir_node   *const cnst        = get_type_size_node(param_type);
3422         ir_mode   *const mode        = get_irn_mode(cnst);
3423         ir_node   *const c1          = new_Const_long(mode, stack_param_align - 1);
3424         ir_node   *const c2          = new_d_Add(dbgi, cnst, c1, mode);
3425         ir_node   *const c3          = new_Const_long(mode, -(long)stack_param_align);
3426         ir_node   *const c4          = new_d_And(dbgi, c2, c3, mode);
3427         ir_node   *const add         = new_d_Add(dbgi, arg_sel, c4, mode_P_data);
3428         set_value_for_expression(expr->ap, add);
3429
3430         return NULL;
3431 }
3432
3433 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
3434 {
3435         type_t       *const type    = expr->base.type;
3436         expression_t *const ap_expr = expr->ap;
3437         ir_node      *const ap_addr = expression_to_addr(ap_expr);
3438         ir_node      *const ap      = get_value_from_lvalue(ap_expr, ap_addr);
3439         dbg_info     *const dbgi    = get_dbg_info(&expr->base.source_position);
3440         ir_node      *const res     = deref_address(dbgi, type, ap);
3441
3442         ir_node      *const cnst    = get_type_size_node(expr->base.type);
3443         ir_mode      *const mode    = get_irn_mode(cnst);
3444         ir_node      *const c1      = new_Const_long(mode, stack_param_align - 1);
3445         ir_node      *const c2      = new_d_Add(dbgi, cnst, c1, mode);
3446         ir_node      *const c3      = new_Const_long(mode, -(long)stack_param_align);
3447         ir_node      *const c4      = new_d_And(dbgi, c2, c3, mode);
3448         ir_node      *const add     = new_d_Add(dbgi, ap, c4, mode_P_data);
3449
3450         set_value_for_expression_addr(ap_expr, add, ap_addr);
3451
3452         return res;
3453 }
3454
3455 /**
3456  * Generate Firm for a va_copy expression.
3457  */
3458 static ir_node *va_copy_expression_to_firm(const va_copy_expression_t *const expr)
3459 {
3460         ir_node *const src = expression_to_firm(expr->src);
3461         set_value_for_expression(expr->dst, src);
3462         return NULL;
3463 }
3464
3465 static ir_node *dereference_addr(const unary_expression_t *const expression)
3466 {
3467         assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
3468         return expression_to_firm(expression->value);
3469 }
3470
3471 /**
3472  * Returns a IR-node representing an lvalue of the given expression.
3473  */
3474 static ir_node *expression_to_addr(const expression_t *expression)
3475 {
3476         switch(expression->kind) {
3477         case EXPR_ARRAY_ACCESS:
3478                 return array_access_addr(&expression->array_access);
3479         case EXPR_CALL:
3480                 return call_expression_to_firm(&expression->call);
3481         case EXPR_COMPOUND_LITERAL:
3482                 return compound_literal_to_firm(&expression->compound_literal);
3483         case EXPR_REFERENCE:
3484                 return reference_addr(&expression->reference);
3485         case EXPR_SELECT:
3486                 return select_addr(&expression->select);
3487         case EXPR_UNARY_DEREFERENCE:
3488                 return dereference_addr(&expression->unary);
3489         default:
3490                 break;
3491         }
3492         panic("trying to get address of non-lvalue");
3493 }
3494
3495 static ir_node *builtin_constant_to_firm(
3496                 const builtin_constant_expression_t *expression)
3497 {
3498         ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3499         long     v;
3500
3501         if (is_constant_expression(expression->value)) {
3502                 v = 1;
3503         } else {
3504                 v = 0;
3505         }
3506         return new_Const_long(mode, v);
3507 }
3508
3509 static ir_node *builtin_types_compatible_to_firm(
3510                 const builtin_types_compatible_expression_t *expression)
3511 {
3512         type_t  *const left  = get_unqualified_type(skip_typeref(expression->left));
3513         type_t  *const right = get_unqualified_type(skip_typeref(expression->right));
3514         long     const value = types_compatible(left, right) ? 1 : 0;
3515         ir_mode *const mode  = get_ir_mode_arithmetic(expression->base.type);
3516         return new_Const_long(mode, value);
3517 }
3518
3519 static ir_node *get_label_block(label_t *label)
3520 {
3521         if (label->block != NULL)
3522                 return label->block;
3523
3524         /* beware: might be called from create initializer with current_ir_graph
3525          * set to const_code_irg. */
3526         ir_graph *rem    = current_ir_graph;
3527         current_ir_graph = current_function;
3528
3529         ir_node *block = new_immBlock();
3530
3531         label->block = block;
3532
3533         ARR_APP1(label_t *, all_labels, label);
3534
3535         current_ir_graph = rem;
3536         return block;
3537 }
3538
3539 /**
3540  * Pointer to a label.  This is used for the
3541  * GNU address-of-label extension.
3542  */
3543 static ir_node *label_address_to_firm(const label_address_expression_t *label)
3544 {
3545         dbg_info  *dbgi   = get_dbg_info(&label->base.source_position);
3546         ir_node   *block  = get_label_block(label->label);
3547         ir_entity *entity = create_Block_entity(block);
3548
3549         symconst_symbol value;
3550         value.entity_p = entity;
3551         return new_d_SymConst(dbgi, mode_P_code, value, symconst_addr_ent);
3552 }
3553
3554 /**
3555  * creates firm nodes for an expression. The difference between this function
3556  * and expression_to_firm is, that this version might produce mode_b nodes
3557  * instead of mode_Is.
3558  */
3559 static ir_node *_expression_to_firm(const expression_t *expression)
3560 {
3561 #ifndef NDEBUG
3562         if (!constant_folding) {
3563                 assert(!expression->base.transformed);
3564                 ((expression_t*) expression)->base.transformed = true;
3565         }
3566 #endif
3567
3568         switch (expression->kind) {
3569         EXPR_LITERAL_CASES
3570                 return literal_to_firm(&expression->literal);
3571         case EXPR_STRING_LITERAL:
3572                 return string_to_firm(&expression->base.source_position, "str.%u",
3573                                       &expression->literal.value);
3574         case EXPR_WIDE_STRING_LITERAL:
3575                 return wide_string_literal_to_firm(&expression->string_literal);
3576         case EXPR_REFERENCE:
3577                 return reference_expression_to_firm(&expression->reference);
3578         case EXPR_REFERENCE_ENUM_VALUE:
3579                 return reference_expression_enum_value_to_firm(&expression->reference);
3580         case EXPR_CALL:
3581                 return call_expression_to_firm(&expression->call);
3582         EXPR_UNARY_CASES
3583                 return unary_expression_to_firm(&expression->unary);
3584         EXPR_BINARY_CASES
3585                 return binary_expression_to_firm(&expression->binary);
3586         case EXPR_ARRAY_ACCESS:
3587                 return array_access_to_firm(&expression->array_access);
3588         case EXPR_SIZEOF:
3589                 return sizeof_to_firm(&expression->typeprop);
3590         case EXPR_ALIGNOF:
3591                 return alignof_to_firm(&expression->typeprop);
3592         case EXPR_CONDITIONAL:
3593                 return conditional_to_firm(&expression->conditional);
3594         case EXPR_SELECT:
3595                 return select_to_firm(&expression->select);
3596         case EXPR_CLASSIFY_TYPE:
3597                 return classify_type_to_firm(&expression->classify_type);
3598         case EXPR_FUNCNAME:
3599                 return function_name_to_firm(&expression->funcname);
3600         case EXPR_STATEMENT:
3601                 return statement_expression_to_firm(&expression->statement);
3602         case EXPR_VA_START:
3603                 return va_start_expression_to_firm(&expression->va_starte);
3604         case EXPR_VA_ARG:
3605                 return va_arg_expression_to_firm(&expression->va_arge);
3606         case EXPR_VA_COPY:
3607                 return va_copy_expression_to_firm(&expression->va_copye);
3608         case EXPR_BUILTIN_CONSTANT_P:
3609                 return builtin_constant_to_firm(&expression->builtin_constant);
3610         case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
3611                 return builtin_types_compatible_to_firm(&expression->builtin_types_compatible);
3612         case EXPR_OFFSETOF:
3613                 return offsetof_to_firm(&expression->offsetofe);
3614         case EXPR_COMPOUND_LITERAL:
3615                 return compound_literal_to_firm(&expression->compound_literal);
3616         case EXPR_LABEL_ADDRESS:
3617                 return label_address_to_firm(&expression->label_address);
3618
3619         case EXPR_UNKNOWN:
3620         case EXPR_INVALID:
3621                 break;
3622         }
3623         panic("invalid expression found");
3624 }
3625
3626 /**
3627  * Check if a given expression is a GNU __builtin_expect() call.
3628  */
3629 static bool is_builtin_expect(const expression_t *expression)
3630 {
3631         if (expression->kind != EXPR_CALL)
3632                 return false;
3633
3634         expression_t *function = expression->call.function;
3635         if (function->kind != EXPR_REFERENCE)
3636                 return false;
3637         reference_expression_t *ref = &function->reference;
3638         if (ref->entity->kind         != ENTITY_FUNCTION ||
3639             ref->entity->function.btk != bk_gnu_builtin_expect)
3640                 return false;
3641
3642         return true;
3643 }
3644
3645 static bool produces_mode_b(const expression_t *expression)
3646 {
3647         switch (expression->kind) {
3648         case EXPR_BINARY_EQUAL:
3649         case EXPR_BINARY_NOTEQUAL:
3650         case EXPR_BINARY_LESS:
3651         case EXPR_BINARY_LESSEQUAL:
3652         case EXPR_BINARY_GREATER:
3653         case EXPR_BINARY_GREATEREQUAL:
3654         case EXPR_BINARY_ISGREATER:
3655         case EXPR_BINARY_ISGREATEREQUAL:
3656         case EXPR_BINARY_ISLESS:
3657         case EXPR_BINARY_ISLESSEQUAL:
3658         case EXPR_BINARY_ISLESSGREATER:
3659         case EXPR_BINARY_ISUNORDERED:
3660         case EXPR_UNARY_NOT:
3661                 return true;
3662
3663         case EXPR_CALL:
3664                 if (is_builtin_expect(expression)) {
3665                         expression_t *argument = expression->call.arguments->expression;
3666                         return produces_mode_b(argument);
3667                 }
3668                 return false;
3669         case EXPR_BINARY_COMMA:
3670                 return produces_mode_b(expression->binary.right);
3671
3672         default:
3673                 return false;
3674         }
3675 }
3676
3677 static ir_node *expression_to_firm(const expression_t *expression)
3678 {
3679         if (!produces_mode_b(expression)) {
3680                 ir_node *res = _expression_to_firm(expression);
3681                 assert(res == NULL || get_irn_mode(res) != mode_b);
3682                 return res;
3683         }
3684
3685         if (is_constant_expression(expression)) {
3686                 ir_node *res  = _expression_to_firm(expression);
3687                 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3688                 assert(is_Const(res));
3689                 if (is_Const_null(res)) {
3690                         return new_Const_long(mode, 0);
3691                 } else {
3692                         return new_Const_long(mode, 1);
3693                 }
3694         }
3695
3696         /* we have to produce a 0/1 from the mode_b expression */
3697         dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3698         ir_mode  *mode = get_ir_mode_arithmetic(expression->base.type);
3699         return produce_condition_result(expression, mode, dbgi);
3700 }
3701
3702 /**
3703  * create a short-circuit expression evaluation that tries to construct
3704  * efficient control flow structures for &&, || and ! expressions
3705  */
3706 static ir_node *create_condition_evaluation(const expression_t *expression,
3707                                             ir_node *true_block,
3708                                             ir_node *false_block)
3709 {
3710         switch(expression->kind) {
3711         case EXPR_UNARY_NOT: {
3712                 const unary_expression_t *unary_expression = &expression->unary;
3713                 create_condition_evaluation(unary_expression->value, false_block,
3714                                             true_block);
3715                 return NULL;
3716         }
3717         case EXPR_BINARY_LOGICAL_AND: {
3718                 const binary_expression_t *binary_expression = &expression->binary;
3719
3720                 ir_node *extra_block = new_immBlock();
3721                 create_condition_evaluation(binary_expression->left, extra_block,
3722                                             false_block);
3723                 mature_immBlock(extra_block);
3724                 set_cur_block(extra_block);
3725                 create_condition_evaluation(binary_expression->right, true_block,
3726                                             false_block);
3727                 return NULL;
3728         }
3729         case EXPR_BINARY_LOGICAL_OR: {
3730                 const binary_expression_t *binary_expression = &expression->binary;
3731
3732                 ir_node *extra_block = new_immBlock();
3733                 create_condition_evaluation(binary_expression->left, true_block,
3734                                             extra_block);
3735                 mature_immBlock(extra_block);
3736                 set_cur_block(extra_block);
3737                 create_condition_evaluation(binary_expression->right, true_block,
3738                                             false_block);
3739                 return NULL;
3740         }
3741         default:
3742                 break;
3743         }
3744
3745         dbg_info *dbgi       = get_dbg_info(&expression->base.source_position);
3746         ir_node  *cond_expr  = _expression_to_firm(expression);
3747         ir_node  *condition  = create_conv(dbgi, cond_expr, mode_b);
3748         ir_node  *cond       = new_d_Cond(dbgi, condition);
3749         ir_node  *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3750         ir_node  *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3751
3752         /* set branch prediction info based on __builtin_expect */
3753         if (is_builtin_expect(expression) && is_Cond(cond)) {
3754                 call_argument_t *argument = expression->call.arguments->next;
3755                 if (is_constant_expression(argument->expression)) {
3756                         bool             cnst = fold_constant_to_bool(argument->expression);
3757                         cond_jmp_predicate pred;
3758
3759                         if (cnst == false) {
3760                                 pred = COND_JMP_PRED_FALSE;
3761                         } else {
3762                                 pred = COND_JMP_PRED_TRUE;
3763                         }
3764                         set_Cond_jmp_pred(cond, pred);
3765                 }
3766         }
3767
3768         add_immBlock_pred(true_block, true_proj);
3769         add_immBlock_pred(false_block, false_proj);
3770
3771         set_cur_block(NULL);
3772         return cond_expr;
3773 }
3774
3775 static void create_variable_entity(entity_t *variable,
3776                                    declaration_kind_t declaration_kind,
3777                                    ir_type *parent_type)
3778 {
3779         assert(variable->kind == ENTITY_VARIABLE);
3780         type_t    *type = skip_typeref(variable->declaration.type);
3781
3782         ident     *const id        = new_id_from_str(variable->base.symbol->string);
3783         ir_type   *const irtype    = get_ir_type(type);
3784         dbg_info  *const dbgi      = get_dbg_info(&variable->base.source_position);
3785         ir_entity *const irentity  = new_d_entity(parent_type, id, irtype, dbgi);
3786         unsigned         alignment = variable->declaration.alignment;
3787
3788         set_entity_alignment(irentity, alignment);
3789
3790         handle_decl_modifiers(irentity, variable);
3791
3792         variable->declaration.kind  = (unsigned char) declaration_kind;
3793         variable->variable.v.entity = irentity;
3794         set_entity_ld_ident(irentity, create_ld_ident(variable));
3795
3796         if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3797                 set_entity_volatility(irentity, volatility_is_volatile);
3798         }
3799 }
3800
3801
3802 typedef struct type_path_entry_t type_path_entry_t;
3803 struct type_path_entry_t {
3804         type_t           *type;
3805         ir_initializer_t *initializer;
3806         size_t            index;
3807         entity_t         *compound_entry;
3808 };
3809
3810 typedef struct type_path_t type_path_t;
3811 struct type_path_t {
3812         type_path_entry_t *path;
3813         type_t            *top_type;
3814         bool               invalid;
3815 };
3816
3817 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3818 {
3819         size_t len = ARR_LEN(path->path);
3820
3821         for (size_t i = 0; i < len; ++i) {
3822                 const type_path_entry_t *entry = & path->path[i];
3823
3824                 type_t *type = skip_typeref(entry->type);
3825                 if (is_type_compound(type)) {
3826                         fprintf(stderr, ".%s", entry->compound_entry->base.symbol->string);
3827                 } else if (is_type_array(type)) {
3828                         fprintf(stderr, "[%u]", (unsigned) entry->index);
3829                 } else {
3830                         fprintf(stderr, "-INVALID-");
3831                 }
3832         }
3833         fprintf(stderr, "  (");
3834         print_type(path->top_type);
3835         fprintf(stderr, ")");
3836 }
3837
3838 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3839 {
3840         size_t len = ARR_LEN(path->path);
3841         assert(len > 0);
3842         return & path->path[len-1];
3843 }
3844
3845 static type_path_entry_t *append_to_type_path(type_path_t *path)
3846 {
3847         size_t len = ARR_LEN(path->path);
3848         ARR_RESIZE(type_path_entry_t, path->path, len+1);
3849
3850         type_path_entry_t *result = & path->path[len];
3851         memset(result, 0, sizeof(result[0]));
3852         return result;
3853 }
3854
3855 static size_t get_compound_member_count(const compound_type_t *type)
3856 {
3857         compound_t *compound  = type->compound;
3858         size_t      n_members = 0;
3859         entity_t   *member    = compound->members.entities;
3860         for ( ; member != NULL; member = member->base.next) {
3861                 ++n_members;
3862         }
3863
3864         return n_members;
3865 }
3866
3867 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3868 {
3869         type_t *orig_top_type = path->top_type;
3870         type_t *top_type      = skip_typeref(orig_top_type);
3871
3872         assert(is_type_compound(top_type) || is_type_array(top_type));
3873
3874         if (ARR_LEN(path->path) == 0) {
3875                 return NULL;
3876         } else {
3877                 type_path_entry_t *top         = get_type_path_top(path);
3878                 ir_initializer_t  *initializer = top->initializer;
3879                 return get_initializer_compound_value(initializer, top->index);
3880         }
3881 }
3882
3883 static void descend_into_subtype(type_path_t *path)
3884 {
3885         type_t *orig_top_type = path->top_type;
3886         type_t *top_type      = skip_typeref(orig_top_type);
3887
3888         assert(is_type_compound(top_type) || is_type_array(top_type));
3889
3890         ir_initializer_t *initializer = get_initializer_entry(path);
3891
3892         type_path_entry_t *top = append_to_type_path(path);
3893         top->type              = top_type;
3894
3895         size_t len;
3896
3897         if (is_type_compound(top_type)) {
3898                 compound_t *compound = top_type->compound.compound;
3899                 entity_t   *entry    = compound->members.entities;
3900
3901                 top->compound_entry = entry;
3902                 top->index          = 0;
3903                 len                 = get_compound_member_count(&top_type->compound);
3904                 if (entry != NULL) {
3905                         assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3906                         path->top_type = entry->declaration.type;
3907                 }
3908         } else {
3909                 assert(is_type_array(top_type));
3910                 assert(top_type->array.size > 0);
3911
3912                 top->index     = 0;
3913                 path->top_type = top_type->array.element_type;
3914                 len            = top_type->array.size;
3915         }
3916         if (initializer == NULL
3917                         || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3918                 initializer = create_initializer_compound(len);
3919                 /* we have to set the entry at the 2nd latest path entry... */
3920                 size_t path_len = ARR_LEN(path->path);
3921                 assert(path_len >= 1);
3922                 if (path_len > 1) {
3923                         type_path_entry_t *entry        = & path->path[path_len-2];
3924                         ir_initializer_t  *tinitializer = entry->initializer;
3925                         set_initializer_compound_value(tinitializer, entry->index,
3926                                                        initializer);
3927                 }
3928         }
3929         top->initializer = initializer;
3930 }
3931
3932 static void ascend_from_subtype(type_path_t *path)
3933 {
3934         type_path_entry_t *top = get_type_path_top(path);
3935
3936         path->top_type = top->type;
3937
3938         size_t len = ARR_LEN(path->path);
3939         ARR_RESIZE(type_path_entry_t, path->path, len-1);
3940 }
3941
3942 static void walk_designator(type_path_t *path, const designator_t *designator)
3943 {
3944         /* designators start at current object type */
3945         ARR_RESIZE(type_path_entry_t, path->path, 1);
3946
3947         for ( ; designator != NULL; designator = designator->next) {
3948                 type_path_entry_t *top         = get_type_path_top(path);
3949                 type_t            *orig_type   = top->type;
3950                 type_t            *type        = skip_typeref(orig_type);
3951
3952                 if (designator->symbol != NULL) {
3953                         assert(is_type_compound(type));
3954                         size_t    index  = 0;
3955                         symbol_t *symbol = designator->symbol;
3956
3957                         compound_t *compound = type->compound.compound;
3958                         entity_t   *iter     = compound->members.entities;
3959                         for ( ; iter != NULL; iter = iter->base.next, ++index) {
3960                                 if (iter->base.symbol == symbol) {
3961                                         assert(iter->kind == ENTITY_COMPOUND_MEMBER);
3962                                         break;
3963                                 }
3964                         }
3965                         assert(iter != NULL);
3966
3967                         /* revert previous initialisations of other union elements */
3968                         if (type->kind == TYPE_COMPOUND_UNION) {
3969                                 ir_initializer_t *initializer = top->initializer;
3970                                 if (initializer != NULL
3971                                         && get_initializer_kind(initializer) == IR_INITIALIZER_COMPOUND) {
3972                                         /* are we writing to a new element? */
3973                                         ir_initializer_t *oldi
3974                                                 = get_initializer_compound_value(initializer, index);
3975                                         if (get_initializer_kind(oldi) == IR_INITIALIZER_NULL) {
3976                                                 /* clear initializer */
3977                                                 size_t len
3978                                                         = get_initializer_compound_n_entries(initializer);
3979                                                 ir_initializer_t *nulli = get_initializer_null();
3980                                                 for (size_t i = 0; i < len; ++i) {
3981                                                         set_initializer_compound_value(initializer, i,
3982                                                                                        nulli);
3983                                                 }
3984                                         }
3985                                 }
3986                         }
3987
3988                         top->type           = orig_type;
3989                         top->compound_entry = iter;
3990                         top->index          = index;
3991                         orig_type           = iter->declaration.type;
3992                 } else {
3993                         expression_t *array_index = designator->array_index;
3994                         assert(designator->array_index != NULL);
3995                         assert(is_type_array(type));
3996
3997                         long index = fold_constant_to_int(array_index);
3998                         assert(index >= 0);
3999 #ifndef NDEBUG
4000                         if (type->array.size_constant) {
4001                                 long array_size = type->array.size;
4002                                 assert(index < array_size);
4003                         }
4004 #endif
4005
4006                         top->type  = orig_type;
4007                         top->index = (size_t) index;
4008                         orig_type  = type->array.element_type;
4009                 }
4010                 path->top_type = orig_type;
4011
4012                 if (designator->next != NULL) {
4013                         descend_into_subtype(path);
4014                 }
4015         }
4016
4017         path->invalid  = false;
4018 }
4019
4020 static void advance_current_object(type_path_t *path)
4021 {
4022         if (path->invalid) {
4023                 /* TODO: handle this... */
4024                 panic("invalid initializer in ast2firm (excessive elements)");
4025         }
4026
4027         type_path_entry_t *top = get_type_path_top(path);
4028
4029         type_t *type = skip_typeref(top->type);
4030         if (is_type_union(type)) {
4031                 /* only the first element is initialized in unions */
4032                 top->compound_entry = NULL;
4033         } else if (is_type_struct(type)) {
4034                 entity_t *entry = top->compound_entry;
4035
4036                 top->index++;
4037                 entry               = entry->base.next;
4038                 top->compound_entry = entry;
4039                 if (entry != NULL) {
4040                         assert(entry->kind == ENTITY_COMPOUND_MEMBER);
4041                         path->top_type = entry->declaration.type;
4042                         return;
4043                 }
4044         } else {
4045                 assert(is_type_array(type));
4046
4047                 top->index++;
4048                 if (!type->array.size_constant || top->index < type->array.size) {
4049                         return;
4050                 }
4051         }
4052
4053         /* we're past the last member of the current sub-aggregate, try if we
4054          * can ascend in the type hierarchy and continue with another subobject */
4055         size_t len = ARR_LEN(path->path);
4056
4057         if (len > 1) {
4058                 ascend_from_subtype(path);
4059                 advance_current_object(path);
4060         } else {
4061                 path->invalid = true;
4062         }
4063 }
4064
4065
4066 static ir_initializer_t *create_ir_initializer(
4067                 const initializer_t *initializer, type_t *type);
4068
4069 static ir_initializer_t *create_ir_initializer_value(
4070                 const initializer_value_t *initializer)
4071 {
4072         if (is_type_compound(initializer->value->base.type)) {
4073                 panic("initializer creation for compounds not implemented yet");
4074         }
4075         type_t       *type = initializer->value->base.type;
4076         expression_t *expr = initializer->value;
4077         if (initializer_use_bitfield_basetype) {
4078                 type_t *skipped = skip_typeref(type);
4079                 if (skipped->kind == TYPE_BITFIELD) {
4080                         /* remove the bitfield cast... */
4081                         assert(expr->kind == EXPR_UNARY_CAST_IMPLICIT);
4082                         expr = expr->unary.value;
4083                         type = skipped->bitfield.base_type;
4084                 }
4085         }
4086         ir_node *value = expression_to_firm(expr);
4087         ir_mode *mode  = get_ir_mode_storage(type);
4088         value          = create_conv(NULL, value, mode);
4089         return create_initializer_const(value);
4090 }
4091
4092 /** test wether type can be initialized by a string constant */
4093 static bool is_string_type(type_t *type)
4094 {
4095         type_t *inner;
4096         if (is_type_pointer(type)) {
4097                 inner = skip_typeref(type->pointer.points_to);
4098         } else if(is_type_array(type)) {
4099                 inner = skip_typeref(type->array.element_type);
4100         } else {
4101                 return false;
4102         }
4103
4104         return is_type_integer(inner);
4105 }
4106
4107 static ir_initializer_t *create_ir_initializer_list(
4108                 const initializer_list_t *initializer, type_t *type)
4109 {
4110         type_path_t path;
4111         memset(&path, 0, sizeof(path));
4112         path.top_type = type;
4113         path.path     = NEW_ARR_F(type_path_entry_t, 0);
4114
4115         descend_into_subtype(&path);
4116
4117         for (size_t i = 0; i < initializer->len; ++i) {
4118                 const initializer_t *sub_initializer = initializer->initializers[i];
4119
4120                 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
4121                         walk_designator(&path, sub_initializer->designator.designator);
4122                         continue;
4123                 }
4124
4125                 if (sub_initializer->kind == INITIALIZER_VALUE) {
4126                         /* we might have to descend into types until we're at a scalar
4127                          * type */
4128                         while(true) {
4129                                 type_t *orig_top_type = path.top_type;
4130                                 type_t *top_type      = skip_typeref(orig_top_type);
4131
4132                                 if (is_type_scalar(top_type))
4133                                         break;
4134                                 descend_into_subtype(&path);
4135                         }
4136                 } else if (sub_initializer->kind == INITIALIZER_STRING
4137                                 || sub_initializer->kind == INITIALIZER_WIDE_STRING) {
4138                         /* we might have to descend into types until we're at a scalar
4139                          * type */
4140                         while (true) {
4141                                 type_t *orig_top_type = path.top_type;
4142                                 type_t *top_type      = skip_typeref(orig_top_type);
4143
4144                                 if (is_string_type(top_type))
4145                                         break;
4146                                 descend_into_subtype(&path);
4147                         }
4148                 }
4149
4150                 ir_initializer_t *sub_irinitializer
4151                         = create_ir_initializer(sub_initializer, path.top_type);
4152
4153                 size_t path_len = ARR_LEN(path.path);
4154                 assert(path_len >= 1);
4155                 type_path_entry_t *entry        = & path.path[path_len-1];
4156                 ir_initializer_t  *tinitializer = entry->initializer;
4157                 set_initializer_compound_value(tinitializer, entry->index,
4158                                                sub_irinitializer);
4159
4160                 advance_current_object(&path);
4161         }
4162
4163         assert(ARR_LEN(path.path) >= 1);
4164         ir_initializer_t *result = path.path[0].initializer;
4165         DEL_ARR_F(path.path);
4166
4167         return result;
4168 }
4169
4170 static ir_initializer_t *create_ir_initializer_string(
4171                 const initializer_string_t *initializer, type_t *type)
4172 {
4173         type = skip_typeref(type);
4174
4175         size_t            string_len    = initializer->string.size;
4176         assert(type->kind == TYPE_ARRAY);
4177         assert(type->array.size_constant);
4178         size_t            len           = type->array.size;
4179         ir_initializer_t *irinitializer = create_initializer_compound(len);
4180
4181         const char *string = initializer->string.begin;
4182         ir_mode    *mode   = get_ir_mode_storage(type->array.element_type);
4183
4184         for (size_t i = 0; i < len; ++i) {
4185                 char c = 0;
4186                 if (i < string_len)
4187                         c = string[i];
4188
4189                 ir_tarval        *tv = new_tarval_from_long(c, mode);
4190                 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4191
4192                 set_initializer_compound_value(irinitializer, i, char_initializer);
4193         }
4194
4195         return irinitializer;
4196 }
4197
4198 static ir_initializer_t *create_ir_initializer_wide_string(
4199                 const initializer_wide_string_t *initializer, type_t *type)
4200 {
4201         assert(type->kind == TYPE_ARRAY);
4202         assert(type->array.size_constant);
4203         size_t            len           = type->array.size;
4204         size_t            string_len    = wstrlen(&initializer->string);
4205         ir_initializer_t *irinitializer = create_initializer_compound(len);
4206
4207         const char *p    = initializer->string.begin;
4208         ir_mode    *mode = get_type_mode(ir_type_wchar_t);
4209
4210         for (size_t i = 0; i < len; ++i) {
4211                 utf32 c = 0;
4212                 if (i < string_len) {
4213                         c = read_utf8_char(&p);
4214                 }
4215                 ir_tarval *tv = new_tarval_from_long(c, mode);
4216                 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4217
4218                 set_initializer_compound_value(irinitializer, i, char_initializer);
4219         }
4220
4221         return irinitializer;
4222 }
4223
4224 static ir_initializer_t *create_ir_initializer(
4225                 const initializer_t *initializer, type_t *type)
4226 {
4227         switch(initializer->kind) {
4228                 case INITIALIZER_STRING:
4229                         return create_ir_initializer_string(&initializer->string, type);
4230
4231                 case INITIALIZER_WIDE_STRING:
4232                         return create_ir_initializer_wide_string(&initializer->wide_string,
4233                                                                  type);
4234
4235                 case INITIALIZER_LIST:
4236                         return create_ir_initializer_list(&initializer->list, type);
4237
4238                 case INITIALIZER_VALUE:
4239                         return create_ir_initializer_value(&initializer->value);
4240
4241                 case INITIALIZER_DESIGNATOR:
4242                         panic("unexpected designator initializer found");
4243         }
4244         panic("unknown initializer");
4245 }
4246
4247 /** ANSI C ยง6.7.8:21: If there are fewer initializers [..] than there
4248  *  are elements [...] the remainder of the aggregate shall be initialized
4249  *  implicitly the same as objects that have static storage duration. */
4250 static void create_dynamic_null_initializer(ir_entity *entity, dbg_info *dbgi,
4251                 ir_node *base_addr)
4252 {
4253         /* for unions we must NOT do anything for null initializers */
4254         ir_type *owner = get_entity_owner(entity);
4255         if (is_Union_type(owner)) {
4256                 return;
4257         }
4258
4259         ir_type *ent_type = get_entity_type(entity);
4260         /* create sub-initializers for a compound type */
4261         if (is_compound_type(ent_type)) {
4262                 unsigned n_members = get_compound_n_members(ent_type);
4263                 for (unsigned n = 0; n < n_members; ++n) {
4264                         ir_entity *member = get_compound_member(ent_type, n);
4265                         ir_node   *addr   = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4266                                                                 member);
4267                         create_dynamic_null_initializer(member, dbgi, addr);
4268                 }
4269                 return;
4270         }
4271         if (is_Array_type(ent_type)) {
4272                 assert(has_array_upper_bound(ent_type, 0));
4273                 long n = get_array_upper_bound_int(ent_type, 0);
4274                 for (long i = 0; i < n; ++i) {
4275                         ir_tarval *index_tv = new_tarval_from_long(i, mode_uint);
4276                         ir_node   *cnst     = new_d_Const(dbgi, index_tv);
4277                         ir_node   *in[1]    = { cnst };
4278                         ir_entity *arrent   = get_array_element_entity(ent_type);
4279                         ir_node   *addr     = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4280                                                         arrent);
4281                         create_dynamic_null_initializer(arrent, dbgi, addr);
4282                 }
4283                 return;
4284         }
4285
4286         ir_mode *value_mode = get_type_mode(ent_type);
4287         ir_node *node = new_Const_long(value_mode, 0);
4288
4289         /* is it a bitfield type? */
4290         if (is_Primitive_type(ent_type) &&
4291                         get_primitive_base_type(ent_type) != NULL) {
4292                 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4293                 return;
4294         }
4295
4296         ir_node *mem    = get_store();
4297         ir_node *store  = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4298         ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4299         set_store(proj_m);
4300 }
4301
4302 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
4303                 ir_entity *entity, ir_type *type, dbg_info *dbgi, ir_node *base_addr)
4304 {
4305         switch(get_initializer_kind(initializer)) {
4306         case IR_INITIALIZER_NULL:
4307                 create_dynamic_null_initializer(entity, dbgi, base_addr);
4308                 return;
4309         case IR_INITIALIZER_CONST: {
4310                 ir_node *node     = get_initializer_const_value(initializer);
4311                 ir_type *ent_type = get_entity_type(entity);
4312
4313                 /* is it a bitfield type? */
4314                 if (is_Primitive_type(ent_type) &&
4315                                 get_primitive_base_type(ent_type) != NULL) {
4316                         bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4317                         return;
4318                 }
4319
4320                 assert(get_type_mode(type) == get_irn_mode(node));
4321                 ir_node *mem    = get_store();
4322                 ir_node *store  = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4323                 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4324                 set_store(proj_m);
4325                 return;
4326         }
4327         case IR_INITIALIZER_TARVAL: {
4328                 ir_tarval *tv       = get_initializer_tarval_value(initializer);
4329                 ir_node   *cnst     = new_d_Const(dbgi, tv);
4330                 ir_type   *ent_type = get_entity_type(entity);
4331
4332                 /* is it a bitfield type? */
4333                 if (is_Primitive_type(ent_type) &&
4334                                 get_primitive_base_type(ent_type) != NULL) {
4335                         bitfield_store_to_firm(dbgi, entity, base_addr, cnst, false);
4336                         return;
4337                 }
4338
4339                 assert(get_type_mode(type) == get_tarval_mode(tv));
4340                 ir_node *mem    = get_store();
4341                 ir_node *store  = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
4342                 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4343                 set_store(proj_m);
4344                 return;
4345         }
4346         case IR_INITIALIZER_COMPOUND: {
4347                 assert(is_compound_type(type) || is_Array_type(type));
4348                 int n_members;
4349                 if (is_Array_type(type)) {
4350                         assert(has_array_upper_bound(type, 0));
4351                         n_members = get_array_upper_bound_int(type, 0);
4352                 } else {
4353                         n_members = get_compound_n_members(type);
4354                 }
4355
4356                 if (get_initializer_compound_n_entries(initializer)
4357                                 != (unsigned) n_members)
4358                         panic("initializer doesn't match compound type");
4359
4360                 for (int i = 0; i < n_members; ++i) {
4361                         ir_node   *addr;
4362                         ir_type   *irtype;
4363                         ir_entity *sub_entity;
4364                         if (is_Array_type(type)) {
4365                                 ir_tarval *index_tv = new_tarval_from_long(i, mode_uint);
4366                                 ir_node   *cnst     = new_d_Const(dbgi, index_tv);
4367                                 ir_node   *in[1]    = { cnst };
4368                                 irtype     = get_array_element_type(type);
4369                                 sub_entity = get_array_element_entity(type);
4370                                 addr       = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4371                                                        sub_entity);
4372                         } else {
4373                                 sub_entity = get_compound_member(type, i);
4374                                 irtype     = get_entity_type(sub_entity);
4375                                 addr       = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4376                                                              sub_entity);
4377                         }
4378
4379                         ir_initializer_t *sub_init
4380                                 = get_initializer_compound_value(initializer, i);
4381
4382                         create_dynamic_initializer_sub(sub_init, sub_entity, irtype, dbgi,
4383                                                        addr);
4384                 }
4385                 return;
4386         }
4387         }
4388
4389         panic("invalid IR_INITIALIZER found");
4390 }
4391
4392 static void create_dynamic_initializer(ir_initializer_t *initializer,
4393                 dbg_info *dbgi, ir_entity *entity)
4394 {
4395         ir_node *frame     = get_irg_frame(current_ir_graph);
4396         ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
4397         ir_type *type      = get_entity_type(entity);
4398
4399         create_dynamic_initializer_sub(initializer, entity, type, dbgi, base_addr);
4400 }
4401
4402 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
4403                                      ir_entity *entity, type_t *type)
4404 {
4405         ir_node *memory = get_store();
4406         ir_node *nomem  = new_NoMem();
4407         ir_node *frame  = get_irg_frame(current_ir_graph);
4408         ir_node *addr   = new_d_simpleSel(dbgi, nomem, frame, entity);
4409
4410         if (initializer->kind == INITIALIZER_VALUE) {
4411                 initializer_value_t *initializer_value = &initializer->value;
4412
4413                 ir_node *value = expression_to_firm(initializer_value->value);
4414                 type = skip_typeref(type);
4415                 assign_value(dbgi, addr, type, value);
4416                 return;
4417         }
4418
4419         if (!is_constant_initializer(initializer)) {
4420                 bool old_initializer_use_bitfield_basetype
4421                         = initializer_use_bitfield_basetype;
4422                 initializer_use_bitfield_basetype = true;
4423                 ir_initializer_t *irinitializer
4424                         = create_ir_initializer(initializer, type);
4425                 initializer_use_bitfield_basetype
4426                         = old_initializer_use_bitfield_basetype;
4427
4428                 create_dynamic_initializer(irinitializer, dbgi, entity);
4429                 return;
4430         }
4431
4432         /* create the ir_initializer */
4433         ir_graph *const old_current_ir_graph = current_ir_graph;
4434         current_ir_graph = get_const_code_irg();
4435
4436         ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
4437
4438         assert(current_ir_graph == get_const_code_irg());
4439         current_ir_graph = old_current_ir_graph;
4440
4441         /* create a "template" entity which is copied to the entity on the stack */
4442         ident     *const id          = id_unique("initializer.%u");
4443         ir_type   *const irtype      = get_ir_type(type);
4444         ir_type   *const global_type = get_glob_type();
4445         ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
4446         set_entity_ld_ident(init_entity, id);
4447
4448         set_entity_visibility(init_entity, ir_visibility_private);
4449         add_entity_linkage(init_entity, IR_LINKAGE_CONSTANT);
4450
4451         set_entity_initializer(init_entity, irinitializer);
4452
4453         ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
4454         ir_node *const copyb    = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
4455
4456         ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
4457         set_store(copyb_mem);
4458 }
4459
4460 static void create_initializer_local_variable_entity(entity_t *entity)
4461 {
4462         assert(entity->kind == ENTITY_VARIABLE);
4463         initializer_t *initializer = entity->variable.initializer;
4464         dbg_info      *dbgi        = get_dbg_info(&entity->base.source_position);
4465         ir_entity     *irentity    = entity->variable.v.entity;
4466         type_t        *type        = entity->declaration.type;
4467
4468         create_local_initializer(initializer, dbgi, irentity, type);
4469 }
4470
4471 static void create_variable_initializer(entity_t *entity)
4472 {
4473         assert(entity->kind == ENTITY_VARIABLE);
4474         initializer_t *initializer = entity->variable.initializer;
4475         if (initializer == NULL)
4476                 return;
4477
4478         declaration_kind_t declaration_kind
4479                 = (declaration_kind_t) entity->declaration.kind;
4480         if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
4481                 create_initializer_local_variable_entity(entity);
4482                 return;
4483         }
4484
4485         type_t            *type = entity->declaration.type;
4486         type_qualifiers_t  tq   = get_type_qualifier(type, true);
4487
4488         if (initializer->kind == INITIALIZER_VALUE) {
4489                 initializer_value_t *initializer_value = &initializer->value;
4490                 dbg_info            *dbgi = get_dbg_info(&entity->base.source_position);
4491
4492                 ir_node *value = expression_to_firm(initializer_value->value);
4493
4494                 type_t  *type = initializer_value->value->base.type;
4495                 ir_mode *mode = get_ir_mode_storage(type);
4496                 value = create_conv(dbgi, value, mode);
4497                 value = do_strict_conv(dbgi, value);
4498
4499                 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
4500                         set_value(entity->variable.v.value_number, value);
4501                 } else {
4502                         assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4503
4504                         ir_entity *irentity = entity->variable.v.entity;
4505
4506                         if (tq & TYPE_QUALIFIER_CONST
4507                                         && get_entity_owner(irentity) != get_tls_type()) {
4508                                 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4509                         }
4510                         set_atomic_ent_value(irentity, value);
4511                 }
4512         } else {
4513                 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
4514                        declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4515
4516                 ir_entity        *irentity        = entity->variable.v.entity;
4517                 ir_initializer_t *irinitializer
4518                         = create_ir_initializer(initializer, type);
4519
4520                 if (tq & TYPE_QUALIFIER_CONST) {
4521                         add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4522                 }
4523                 set_entity_initializer(irentity, irinitializer);
4524         }
4525 }
4526
4527 static void create_variable_length_array(entity_t *entity)
4528 {
4529         assert(entity->kind == ENTITY_VARIABLE);
4530         assert(entity->variable.initializer == NULL);
4531
4532         entity->declaration.kind    = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
4533         entity->variable.v.vla_base = NULL;
4534
4535         /* TODO: record VLA somewhere so we create the free node when we leave
4536          * it's scope */
4537 }
4538
4539 static void allocate_variable_length_array(entity_t *entity)
4540 {
4541         assert(entity->kind == ENTITY_VARIABLE);
4542         assert(entity->variable.initializer == NULL);
4543         assert(get_cur_block() != NULL);
4544
4545         dbg_info *dbgi      = get_dbg_info(&entity->base.source_position);
4546         type_t   *type      = entity->declaration.type;
4547         ir_type  *el_type   = get_ir_type(type->array.element_type);
4548
4549         /* make sure size_node is calculated */
4550         get_type_size_node(type);
4551         ir_node  *elems = type->array.size_node;
4552         ir_node  *mem   = get_store();
4553         ir_node  *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
4554
4555         ir_node  *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
4556         ir_node  *addr   = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
4557         set_store(proj_m);
4558
4559         assert(entity->declaration.kind == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
4560         entity->variable.v.vla_base = addr;
4561 }
4562
4563 /**
4564  * Creates a Firm local variable from a declaration.
4565  */
4566 static void create_local_variable(entity_t *entity)
4567 {
4568         assert(entity->kind == ENTITY_VARIABLE);
4569         assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4570
4571         bool needs_entity = entity->variable.address_taken;
4572         type_t *type = skip_typeref(entity->declaration.type);
4573
4574         /* is it a variable length array? */
4575         if (is_type_array(type) && !type->array.size_constant) {
4576                 create_variable_length_array(entity);
4577                 return;
4578         } else if (is_type_array(type) || is_type_compound(type)) {
4579                 needs_entity = true;
4580         } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4581                 needs_entity = true;
4582         }
4583
4584         if (needs_entity) {
4585                 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
4586                 create_variable_entity(entity,
4587                                        DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
4588                                        frame_type);
4589         } else {
4590                 entity->declaration.kind        = DECLARATION_KIND_LOCAL_VARIABLE;
4591                 entity->variable.v.value_number = next_value_number_function;
4592                 set_irg_loc_description(current_ir_graph, next_value_number_function,
4593                                         entity);
4594                 ++next_value_number_function;
4595         }
4596 }
4597
4598 static void create_local_static_variable(entity_t *entity)
4599 {
4600         assert(entity->kind == ENTITY_VARIABLE);
4601         assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4602
4603         type_t   *type           = skip_typeref(entity->declaration.type);
4604         ir_type  *const var_type = entity->variable.thread_local ?
4605                 get_tls_type() : get_glob_type();
4606         ir_type  *const irtype   = get_ir_type(type);
4607         dbg_info *const dbgi     = get_dbg_info(&entity->base.source_position);
4608
4609         size_t l = strlen(entity->base.symbol->string);
4610         char   buf[l + sizeof(".%u")];
4611         snprintf(buf, sizeof(buf), "%s.%%u", entity->base.symbol->string);
4612         ident     *const id       = id_unique(buf);
4613         ir_entity *const irentity = new_d_entity(var_type, id, irtype, dbgi);
4614
4615         if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4616                 set_entity_volatility(irentity, volatility_is_volatile);
4617         }
4618
4619         entity->declaration.kind  = DECLARATION_KIND_GLOBAL_VARIABLE;
4620         entity->variable.v.entity = irentity;
4621
4622         set_entity_ld_ident(irentity, id);
4623         set_entity_visibility(irentity, ir_visibility_local);
4624
4625         ir_graph *const old_current_ir_graph = current_ir_graph;
4626         current_ir_graph = get_const_code_irg();
4627
4628         create_variable_initializer(entity);
4629
4630         assert(current_ir_graph == get_const_code_irg());
4631         current_ir_graph = old_current_ir_graph;
4632 }
4633
4634
4635
4636 static void return_statement_to_firm(return_statement_t *statement)
4637 {
4638         if (get_cur_block() == NULL)
4639                 return;
4640
4641         dbg_info *dbgi        = get_dbg_info(&statement->base.source_position);
4642         type_t   *type        = current_function_entity->declaration.type;
4643         ir_type  *func_irtype = get_ir_type(type);
4644
4645
4646         ir_node *in[1];
4647         int      in_len;
4648         if (get_method_n_ress(func_irtype) > 0) {
4649                 ir_type *res_type = get_method_res_type(func_irtype, 0);
4650
4651                 if (statement->value != NULL) {
4652                         ir_node *node = expression_to_firm(statement->value);
4653                         if (!is_compound_type(res_type)) {
4654                                 type_t  *type = statement->value->base.type;
4655                                 ir_mode *mode = get_ir_mode_storage(type);
4656                                 node          = create_conv(dbgi, node, mode);
4657                                 node          = do_strict_conv(dbgi, node);
4658                         }
4659                         in[0] = node;
4660                 } else {
4661                         ir_mode *mode;
4662                         if (is_compound_type(res_type)) {
4663                                 mode = mode_P_data;
4664                         } else {
4665                                 mode = get_type_mode(res_type);
4666                         }
4667                         in[0] = new_Unknown(mode);
4668                 }
4669                 in_len = 1;
4670         } else {
4671                 /* build return_value for its side effects */
4672                 if (statement->value != NULL) {
4673                         expression_to_firm(statement->value);
4674                 }
4675                 in_len = 0;
4676         }
4677
4678         ir_node  *store = get_store();
4679         ir_node  *ret   = new_d_Return(dbgi, store, in_len, in);
4680
4681         ir_node *end_block = get_irg_end_block(current_ir_graph);
4682         add_immBlock_pred(end_block, ret);
4683
4684         set_cur_block(NULL);
4685 }
4686
4687 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4688 {
4689         if (get_cur_block() == NULL)
4690                 return NULL;
4691
4692         return expression_to_firm(statement->expression);
4693 }
4694
4695 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4696 {
4697         entity_t *entity = compound->scope.entities;
4698         for ( ; entity != NULL; entity = entity->base.next) {
4699                 if (!is_declaration(entity))
4700                         continue;
4701
4702                 create_local_declaration(entity);
4703         }
4704
4705         ir_node     *result    = NULL;
4706         statement_t *statement = compound->statements;
4707         for ( ; statement != NULL; statement = statement->base.next) {
4708                 if (statement->base.next == NULL
4709                                 && statement->kind == STATEMENT_EXPRESSION) {
4710                         result = expression_statement_to_firm(
4711                                         &statement->expression);
4712                         break;
4713                 }
4714                 statement_to_firm(statement);
4715         }
4716
4717         return result;
4718 }
4719
4720 static void create_global_variable(entity_t *entity)
4721 {
4722         ir_linkage    linkage    = IR_LINKAGE_DEFAULT;
4723         ir_visibility visibility = ir_visibility_default;
4724         ir_entity    *irentity;
4725         assert(entity->kind == ENTITY_VARIABLE);
4726
4727         switch ((storage_class_tag_t)entity->declaration.storage_class) {
4728         case STORAGE_CLASS_EXTERN: visibility = ir_visibility_external; break;
4729         case STORAGE_CLASS_STATIC: visibility = ir_visibility_local;    break;
4730         case STORAGE_CLASS_NONE:
4731                 visibility = ir_visibility_default;
4732                 /* uninitialized globals get merged in C */
4733                 if (entity->variable.initializer == NULL)
4734                         linkage |= IR_LINKAGE_MERGE;
4735                 break;
4736         case STORAGE_CLASS_TYPEDEF:
4737         case STORAGE_CLASS_AUTO:
4738         case STORAGE_CLASS_REGISTER:
4739                 panic("invalid storage class for global var");
4740         }
4741
4742         ir_type *var_type = get_glob_type();
4743         if (entity->variable.thread_local) {
4744                 var_type = get_tls_type();
4745                 /* LINKAGE_MERGE not supported by current linkers */
4746                 linkage &= ~IR_LINKAGE_MERGE;
4747         }
4748         create_variable_entity(entity, DECLARATION_KIND_GLOBAL_VARIABLE, var_type);
4749         irentity = entity->variable.v.entity;
4750         add_entity_linkage(irentity, linkage);
4751         set_entity_visibility(irentity, visibility);
4752 }
4753
4754 static void create_local_declaration(entity_t *entity)
4755 {
4756         assert(is_declaration(entity));
4757
4758         /* construct type */
4759         (void) get_ir_type(entity->declaration.type);
4760         if (entity->base.symbol == NULL) {
4761                 return;
4762         }
4763
4764         switch ((storage_class_tag_t) entity->declaration.storage_class) {
4765         case STORAGE_CLASS_STATIC:
4766                 if (entity->kind == ENTITY_FUNCTION) {
4767                         (void)get_function_entity(entity, NULL);
4768                 } else {
4769                         create_local_static_variable(entity);
4770                 }
4771                 return;
4772         case STORAGE_CLASS_EXTERN:
4773                 if (entity->kind == ENTITY_FUNCTION) {
4774                         assert(entity->function.statement == NULL);
4775                         (void)get_function_entity(entity, NULL);
4776                 } else {
4777                         create_global_variable(entity);
4778                         create_variable_initializer(entity);
4779                 }
4780                 return;
4781         case STORAGE_CLASS_NONE:
4782         case STORAGE_CLASS_AUTO:
4783         case STORAGE_CLASS_REGISTER:
4784                 if (entity->kind == ENTITY_FUNCTION) {
4785                         if (entity->function.statement != NULL) {
4786                                 ir_type *owner = get_irg_frame_type(current_ir_graph);
4787                                 (void)get_function_entity(entity, owner);
4788                                 entity->declaration.kind = DECLARATION_KIND_INNER_FUNCTION;
4789                                 enqueue_inner_function(entity);
4790                         } else {
4791                                 (void)get_function_entity(entity, NULL);
4792                         }
4793                 } else {
4794                         create_local_variable(entity);
4795                 }
4796                 return;
4797         case STORAGE_CLASS_TYPEDEF:
4798                 break;
4799         }
4800         panic("invalid storage class found");
4801 }
4802
4803 static void initialize_local_declaration(entity_t *entity)
4804 {
4805         if (entity->base.symbol == NULL)
4806                 return;
4807
4808         // no need to emit code in dead blocks
4809         if (entity->declaration.storage_class != STORAGE_CLASS_STATIC
4810                         && get_cur_block() == NULL)
4811                 return;
4812
4813         switch ((declaration_kind_t) entity->declaration.kind) {
4814         case DECLARATION_KIND_LOCAL_VARIABLE:
4815         case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4816                 create_variable_initializer(entity);
4817                 return;
4818
4819         case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4820                 allocate_variable_length_array(entity);
4821                 return;
4822
4823         case DECLARATION_KIND_COMPOUND_MEMBER:
4824         case DECLARATION_KIND_GLOBAL_VARIABLE:
4825         case DECLARATION_KIND_FUNCTION:
4826         case DECLARATION_KIND_INNER_FUNCTION:
4827                 return;
4828
4829         case DECLARATION_KIND_PARAMETER:
4830         case DECLARATION_KIND_PARAMETER_ENTITY:
4831                 panic("can't initialize parameters");
4832
4833         case DECLARATION_KIND_UNKNOWN:
4834                 panic("can't initialize unknown declaration");
4835         }
4836         panic("invalid declaration kind");
4837 }
4838
4839 static void declaration_statement_to_firm(declaration_statement_t *statement)
4840 {
4841         entity_t *entity = statement->declarations_begin;
4842         if (entity == NULL)
4843                 return;
4844
4845         entity_t *const last = statement->declarations_end;
4846         for ( ;; entity = entity->base.next) {
4847                 if (is_declaration(entity)) {
4848                         initialize_local_declaration(entity);
4849                 } else if (entity->kind == ENTITY_TYPEDEF) {
4850                         /* ยง6.7.7:3  Any array size expressions associated with variable length
4851                          * array declarators are evaluated each time the declaration of the
4852                          * typedef name is reached in the order of execution. */
4853                         type_t *const type = skip_typeref(entity->typedefe.type);
4854                         if (is_type_array(type) && type->array.is_vla)
4855                                 get_vla_size(&type->array);
4856                 }
4857                 if (entity == last)
4858                         break;
4859         }
4860 }
4861
4862 static void if_statement_to_firm(if_statement_t *statement)
4863 {
4864         ir_node *cur_block = get_cur_block();
4865
4866         ir_node *fallthrough_block = NULL;
4867
4868         /* the true (blocks) */
4869         ir_node *true_block = NULL;
4870         if (statement->true_statement != NULL) {
4871                 true_block = new_immBlock();
4872                 set_cur_block(true_block);
4873                 statement_to_firm(statement->true_statement);
4874                 if (get_cur_block() != NULL) {
4875                         ir_node *jmp = new_Jmp();
4876                         if (fallthrough_block == NULL)
4877                                 fallthrough_block = new_immBlock();
4878                         add_immBlock_pred(fallthrough_block, jmp);
4879                 }
4880         }
4881
4882         /* the false (blocks) */
4883         ir_node *false_block = NULL;
4884         if (statement->false_statement != NULL) {
4885                 false_block = new_immBlock();
4886                 set_cur_block(false_block);
4887
4888                 statement_to_firm(statement->false_statement);
4889                 if (get_cur_block() != NULL) {
4890                         ir_node *jmp = new_Jmp();
4891                         if (fallthrough_block == NULL)
4892                                 fallthrough_block = new_immBlock();
4893                         add_immBlock_pred(fallthrough_block, jmp);
4894                 }
4895         }
4896
4897         /* create the condition */
4898         if (cur_block != NULL) {
4899                 if (true_block == NULL || false_block == NULL) {
4900                         if (fallthrough_block == NULL)
4901                                 fallthrough_block = new_immBlock();
4902                         if (true_block == NULL)
4903                                 true_block = fallthrough_block;
4904                         if (false_block == NULL)
4905                                 false_block = fallthrough_block;
4906                 }
4907
4908                 set_cur_block(cur_block);
4909                 create_condition_evaluation(statement->condition, true_block,
4910                                             false_block);
4911         }
4912
4913         mature_immBlock(true_block);
4914         if (false_block != fallthrough_block && false_block != NULL) {
4915                 mature_immBlock(false_block);
4916         }
4917         if (fallthrough_block != NULL) {
4918                 mature_immBlock(fallthrough_block);
4919         }
4920
4921         set_cur_block(fallthrough_block);
4922 }
4923
4924 static void while_statement_to_firm(while_statement_t *statement)
4925 {
4926         ir_node *jmp = NULL;
4927         if (get_cur_block() != NULL) {
4928                 jmp = new_Jmp();
4929         }
4930
4931         /* create the header block */
4932         ir_node *header_block = new_immBlock();
4933         if (jmp != NULL) {
4934                 add_immBlock_pred(header_block, jmp);
4935         }
4936
4937         /* the loop body */
4938         ir_node *old_continue_label = continue_label;
4939         ir_node *old_break_label    = break_label;
4940         continue_label              = header_block;
4941         break_label                 = NULL;
4942
4943         ir_node *body_block = new_immBlock();
4944         set_cur_block(body_block);
4945         statement_to_firm(statement->body);
4946         ir_node *false_block = break_label;
4947
4948         assert(continue_label == header_block);
4949         continue_label = old_continue_label;
4950         break_label    = old_break_label;
4951
4952         if (get_cur_block() != NULL) {
4953                 jmp = new_Jmp();
4954                 add_immBlock_pred(header_block, jmp);
4955         }
4956
4957         /* shortcut for while(true) */
4958         if (is_constant_expression(statement->condition)
4959                         && fold_constant_to_bool(statement->condition) != 0) {
4960                 set_cur_block(header_block);
4961                 ir_node *header_jmp = new_Jmp();
4962                 add_immBlock_pred(body_block, header_jmp);
4963
4964                 keep_alive(body_block);
4965                 keep_all_memory(body_block);
4966                 set_cur_block(body_block);
4967         } else {
4968                 if (false_block == NULL) {
4969                         false_block = new_immBlock();
4970                 }
4971
4972                 /* create the condition */
4973                 set_cur_block(header_block);
4974
4975                 create_condition_evaluation(statement->condition, body_block,
4976                                             false_block);
4977         }
4978
4979         mature_immBlock(body_block);
4980         mature_immBlock(header_block);
4981         if (false_block != NULL) {
4982                 mature_immBlock(false_block);
4983         }
4984
4985         set_cur_block(false_block);
4986 }
4987
4988 static void do_while_statement_to_firm(do_while_statement_t *statement)
4989 {
4990         ir_node *jmp = NULL;
4991         if (get_cur_block() != NULL) {
4992                 jmp = new_Jmp();
4993         }
4994
4995         /* create the header block */
4996         ir_node *header_block = new_immBlock();
4997
4998         /* the loop body */
4999         ir_node *body_block = new_immBlock();
5000         if (jmp != NULL) {
5001                 add_immBlock_pred(body_block, jmp);
5002         }
5003
5004         ir_node *old_continue_label = continue_label;
5005         ir_node *old_break_label    = break_label;
5006         continue_label              = header_block;
5007         break_label                 = NULL;
5008
5009         set_cur_block(body_block);
5010         statement_to_firm(statement->body);
5011         ir_node *false_block = break_label;
5012
5013         assert(continue_label == header_block);
5014         continue_label = old_continue_label;
5015         break_label    = old_break_label;
5016
5017         if (get_cur_block() != NULL) {
5018                 ir_node *body_jmp = new_Jmp();
5019                 add_immBlock_pred(header_block, body_jmp);
5020                 mature_immBlock(header_block);
5021         }
5022
5023         if (false_block == NULL) {
5024                 false_block = new_immBlock();
5025         }
5026
5027         /* create the condition */
5028         set_cur_block(header_block);
5029
5030         create_condition_evaluation(statement->condition, body_block, false_block);
5031         mature_immBlock(body_block);
5032         mature_immBlock(header_block);
5033         mature_immBlock(false_block);
5034
5035         set_cur_block(false_block);
5036 }
5037
5038 static void for_statement_to_firm(for_statement_t *statement)
5039 {
5040         ir_node *jmp = NULL;
5041
5042         /* create declarations */
5043         entity_t *entity = statement->scope.entities;
5044         for ( ; entity != NULL; entity = entity->base.next) {
5045                 if (!is_declaration(entity))
5046                         continue;
5047
5048                 create_local_declaration(entity);
5049         }
5050
5051         if (get_cur_block() != NULL) {
5052                 entity = statement->scope.entities;
5053                 for ( ; entity != NULL; entity = entity->base.next) {
5054                         if (!is_declaration(entity))
5055                                 continue;
5056
5057                         initialize_local_declaration(entity);
5058                 }
5059
5060                 if (statement->initialisation != NULL) {
5061                         expression_to_firm(statement->initialisation);
5062                 }
5063
5064                 jmp = new_Jmp();
5065         }
5066
5067
5068         /* create the step block */
5069         ir_node *const step_block = new_immBlock();
5070         set_cur_block(step_block);
5071         if (statement->step != NULL) {
5072                 expression_to_firm(statement->step);
5073         }
5074         ir_node *const step_jmp = new_Jmp();
5075
5076         /* create the header block */
5077         ir_node *const header_block = new_immBlock();
5078         set_cur_block(header_block);
5079         if (jmp != NULL) {
5080                 add_immBlock_pred(header_block, jmp);
5081         }
5082         add_immBlock_pred(header_block, step_jmp);
5083
5084         /* the false block */
5085         ir_node *const false_block = new_immBlock();
5086
5087         /* the loop body */
5088         ir_node *body_block;
5089         if (statement->body != NULL) {
5090                 ir_node *const old_continue_label = continue_label;
5091                 ir_node *const old_break_label    = break_label;
5092                 continue_label = step_block;
5093                 break_label    = false_block;
5094
5095                 body_block = new_immBlock();
5096                 set_cur_block(body_block);
5097                 statement_to_firm(statement->body);
5098
5099                 assert(continue_label == step_block);
5100                 assert(break_label    == false_block);
5101                 continue_label = old_continue_label;
5102                 break_label    = old_break_label;
5103
5104                 if (get_cur_block() != NULL) {
5105                         jmp = new_Jmp();
5106                         add_immBlock_pred(step_block, jmp);
5107                 }
5108         } else {
5109                 body_block = step_block;
5110         }
5111
5112         /* create the condition */
5113         set_cur_block(header_block);
5114         if (statement->condition != NULL) {
5115                 create_condition_evaluation(statement->condition, body_block,
5116                                             false_block);
5117         } else {
5118                 keep_alive(header_block);
5119                 keep_all_memory(header_block);
5120                 jmp = new_Jmp();
5121                 add_immBlock_pred(body_block, jmp);
5122         }
5123
5124         mature_immBlock(body_block);
5125         mature_immBlock(false_block);
5126         mature_immBlock(step_block);
5127         mature_immBlock(header_block);
5128         mature_immBlock(false_block);
5129
5130         set_cur_block(false_block);
5131 }
5132
5133 static void create_jump_statement(const statement_t *statement,
5134                                   ir_node *target_block)
5135 {
5136         if (get_cur_block() == NULL)
5137                 return;
5138
5139         dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5140         ir_node  *jump = new_d_Jmp(dbgi);
5141         add_immBlock_pred(target_block, jump);
5142
5143         set_cur_block(NULL);
5144 }
5145
5146 static ir_node *get_break_label(void)
5147 {
5148         if (break_label == NULL) {
5149                 break_label = new_immBlock();
5150         }
5151         return break_label;
5152 }
5153
5154 static void switch_statement_to_firm(switch_statement_t *statement)
5155 {
5156         ir_node  *first_block = NULL;
5157         dbg_info *dbgi        = get_dbg_info(&statement->base.source_position);
5158         ir_node  *cond        = NULL;
5159
5160         if (get_cur_block() != NULL) {
5161                 ir_node *expression = expression_to_firm(statement->expression);
5162                 cond                = new_d_Cond(dbgi, expression);
5163                 first_block         = get_cur_block();
5164         }
5165
5166         set_cur_block(NULL);
5167
5168         ir_node *const old_switch_cond       = current_switch_cond;
5169         ir_node *const old_break_label       = break_label;
5170         const bool     old_saw_default_label = saw_default_label;
5171         saw_default_label                    = false;
5172         current_switch_cond                  = cond;
5173         break_label                          = NULL;
5174         switch_statement_t *const old_switch = current_switch;
5175         current_switch                       = statement;
5176
5177         /* determine a free number for the default label */
5178         unsigned long num_cases       = 0;
5179         long          default_proj_nr = 0;
5180         for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5181                 if (l->expression == NULL) {
5182                         /* default case */
5183                         continue;
5184                 }
5185                 if (l->last_case >= l->first_case)
5186                         num_cases += l->last_case - l->first_case + 1;
5187                 if (l->last_case > default_proj_nr)
5188                         default_proj_nr = l->last_case;
5189         }
5190
5191         if (default_proj_nr == INT_MAX) {
5192                 /* Bad: an overflow will occur, we cannot be sure that the
5193                  * maximum + 1 is a free number. Scan the values a second
5194                  * time to find a free number.
5195                  */
5196                 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
5197
5198                 memset(bits, 0, (num_cases + 7) >> 3);
5199                 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5200                         if (l->expression == NULL) {
5201                                 /* default case */
5202                                 continue;
5203                         }
5204                         unsigned long start = l->first_case > 0 ? (unsigned long)l->first_case : 0;
5205                         if (start < num_cases && l->last_case >= 0) {
5206                                 unsigned long end  = (unsigned long)l->last_case < num_cases ?
5207                                         (unsigned long)l->last_case : num_cases - 1;
5208                                 for (unsigned long cns = start; cns <= end; ++cns) {
5209                                         bits[cns >> 3] |= (1 << (cns & 7));
5210                                 }
5211                         }
5212                 }
5213                 /* We look at the first num_cases constants:
5214                  * Either they are dense, so we took the last (num_cases)
5215                  * one, or they are not dense, so we will find one free
5216                  * there...
5217                  */
5218                 unsigned long i;
5219                 for (i = 0; i < num_cases; ++i)
5220                         if ((bits[i >> 3] & (1 << (i & 7))) == 0)
5221                                 break;
5222
5223                 free(bits);
5224                 default_proj_nr = i;
5225         } else {
5226                 ++default_proj_nr;
5227         }
5228         statement->default_proj_nr = default_proj_nr;
5229         /* safety check: cond might already be folded to a Bad */
5230         if (cond != NULL && is_Cond(cond)) {
5231                 set_Cond_default_proj(cond, default_proj_nr);
5232         }
5233
5234         if (statement->body != NULL) {
5235                 statement_to_firm(statement->body);
5236         }
5237
5238         if (get_cur_block() != NULL) {
5239                 ir_node *jmp = new_Jmp();
5240                 add_immBlock_pred(get_break_label(), jmp);
5241         }
5242
5243         if (!saw_default_label && first_block != NULL) {
5244                 set_cur_block(first_block);
5245                 ir_node *const proj = new_d_Proj(dbgi, cond, mode_X, default_proj_nr);
5246                 add_immBlock_pred(get_break_label(), proj);
5247         }
5248
5249         if (break_label != NULL) {
5250                 mature_immBlock(break_label);
5251         }
5252         set_cur_block(break_label);
5253
5254         assert(current_switch_cond == cond);
5255         current_switch      = old_switch;
5256         current_switch_cond = old_switch_cond;
5257         break_label         = old_break_label;
5258         saw_default_label   = old_saw_default_label;
5259 }
5260
5261 static void case_label_to_firm(const case_label_statement_t *statement)
5262 {
5263         if (statement->is_empty_range)
5264                 return;
5265
5266         dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5267
5268         ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
5269
5270         ir_node *proj;
5271         ir_node *block = new_immBlock();
5272
5273         if (current_switch_cond != NULL) {
5274                 set_cur_block(get_nodes_block(current_switch_cond));
5275                 if (statement->expression != NULL) {
5276                         long pn     = statement->first_case;
5277                         long end_pn = statement->last_case;
5278                         assert(pn <= end_pn);
5279                         /* create jumps for all cases in the given range */
5280                         do {
5281                                 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
5282                                 add_immBlock_pred(block, proj);
5283                         } while (pn++ < end_pn);
5284                 } else {
5285                         saw_default_label = true;
5286                         proj = new_d_Proj(dbgi, current_switch_cond, mode_X,
5287                                           current_switch->default_proj_nr);
5288
5289                         add_immBlock_pred(block, proj);
5290                 }
5291         }
5292
5293         if (fallthrough != NULL) {
5294                 add_immBlock_pred(block, fallthrough);
5295         }
5296         mature_immBlock(block);
5297         set_cur_block(block);
5298
5299         if (statement->statement != NULL) {
5300                 statement_to_firm(statement->statement);
5301         }
5302 }
5303
5304 static void label_to_firm(const label_statement_t *statement)
5305 {
5306         ir_node *block = get_label_block(statement->label);
5307
5308         if (get_cur_block() != NULL) {
5309                 ir_node *jmp = new_Jmp();
5310                 add_immBlock_pred(block, jmp);
5311         }
5312
5313         set_cur_block(block);
5314         keep_alive(block);
5315         keep_all_memory(block);
5316
5317         if (statement->statement != NULL) {
5318                 statement_to_firm(statement->statement);
5319         }
5320 }
5321
5322 static void goto_to_firm(const goto_statement_t *statement)
5323 {
5324         if (get_cur_block() == NULL)
5325                 return;
5326
5327         if (statement->expression) {
5328                 ir_node  *irn  = expression_to_firm(statement->expression);
5329                 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5330                 ir_node  *ijmp = new_d_IJmp(dbgi, irn);
5331
5332                 set_irn_link(ijmp, ijmp_list);
5333                 ijmp_list = ijmp;
5334         } else {
5335                 ir_node *block = get_label_block(statement->label);
5336                 ir_node *jmp   = new_Jmp();
5337                 add_immBlock_pred(block, jmp);
5338         }
5339         set_cur_block(NULL);
5340 }
5341
5342 static void asm_statement_to_firm(const asm_statement_t *statement)
5343 {
5344         bool needs_memory = false;
5345
5346         if (statement->is_volatile) {
5347                 needs_memory = true;
5348         }
5349
5350         size_t         n_clobbers = 0;
5351         asm_clobber_t *clobber    = statement->clobbers;
5352         for ( ; clobber != NULL; clobber = clobber->next) {
5353                 const char *clobber_str = clobber->clobber.begin;
5354
5355                 if (!be_is_valid_clobber(clobber_str)) {
5356                         errorf(&statement->base.source_position,
5357                                    "invalid clobber '%s' specified", clobber->clobber);
5358                         continue;
5359                 }
5360
5361                 if (strcmp(clobber_str, "memory") == 0) {
5362                         needs_memory = true;
5363                         continue;
5364                 }
5365
5366                 ident *id = new_id_from_str(clobber_str);
5367                 obstack_ptr_grow(&asm_obst, id);
5368                 ++n_clobbers;
5369         }
5370         assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
5371         ident **clobbers = NULL;
5372         if (n_clobbers > 0) {
5373                 clobbers = obstack_finish(&asm_obst);
5374         }
5375
5376         size_t n_inputs  = 0;
5377         asm_argument_t *argument = statement->inputs;
5378         for ( ; argument != NULL; argument = argument->next)
5379                 n_inputs++;
5380         size_t n_outputs = 0;
5381         argument = statement->outputs;
5382         for ( ; argument != NULL; argument = argument->next)
5383                 n_outputs++;
5384
5385         unsigned next_pos = 0;
5386
5387         ir_node *ins[n_inputs + n_outputs + 1];
5388         size_t   in_size = 0;
5389
5390         ir_asm_constraint tmp_in_constraints[n_outputs];
5391
5392         const expression_t *out_exprs[n_outputs];
5393         ir_node            *out_addrs[n_outputs];
5394         size_t              out_size = 0;
5395
5396         argument = statement->outputs;
5397         for ( ; argument != NULL; argument = argument->next) {
5398                 const char *constraints = argument->constraints.begin;
5399                 asm_constraint_flags_t asm_flags
5400                         = be_parse_asm_constraints(constraints);
5401
5402                 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5403                         warningf(&statement->base.source_position,
5404                                "some constraints in '%s' are not supported", constraints);
5405                 }
5406                 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5407                         errorf(&statement->base.source_position,
5408                                "some constraints in '%s' are invalid", constraints);
5409                         continue;
5410                 }
5411                 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
5412                         errorf(&statement->base.source_position,
5413                                "no write flag specified for output constraints '%s'",
5414                                constraints);
5415                         continue;
5416                 }
5417
5418                 unsigned pos = next_pos++;
5419                 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5420                                 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5421                         expression_t *expr = argument->expression;
5422                         ir_node      *addr = expression_to_addr(expr);
5423                         /* in+output, construct an artifical same_as constraint on the
5424                          * input */
5425                         if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
5426                                 char     buf[64];
5427                                 ir_node *value = get_value_from_lvalue(expr, addr);
5428
5429                                 snprintf(buf, sizeof(buf), "%u", (unsigned) out_size);
5430
5431                                 ir_asm_constraint constraint;
5432                                 constraint.pos              = pos;
5433                                 constraint.constraint       = new_id_from_str(buf);
5434                                 constraint.mode             = get_ir_mode_storage(expr->base.type);
5435                                 tmp_in_constraints[in_size] = constraint;
5436                                 ins[in_size] = value;
5437
5438                                 ++in_size;
5439                         }
5440
5441                         out_exprs[out_size] = expr;
5442                         out_addrs[out_size] = addr;
5443                         ++out_size;
5444                 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5445                         /* pure memory ops need no input (but we have to make sure we
5446                          * attach to the memory) */
5447                         assert(! (asm_flags &
5448                                                 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5449                                                  | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5450                         needs_memory = true;
5451
5452                         /* we need to attach the address to the inputs */
5453                         expression_t *expr = argument->expression;
5454
5455                         ir_asm_constraint constraint;
5456                         constraint.pos              = pos;
5457                         constraint.constraint       = new_id_from_str(constraints);
5458                         constraint.mode             = NULL;
5459                         tmp_in_constraints[in_size] = constraint;
5460
5461                         ins[in_size]          = expression_to_addr(expr);
5462                         ++in_size;
5463                         continue;
5464                 } else {
5465                         errorf(&statement->base.source_position,
5466                                "only modifiers but no place set in constraints '%s'",
5467                                constraints);
5468                         continue;
5469                 }
5470
5471                 ir_asm_constraint constraint;
5472                 constraint.pos        = pos;
5473                 constraint.constraint = new_id_from_str(constraints);
5474                 constraint.mode       = get_ir_mode_storage(argument->expression->base.type);
5475
5476                 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5477         }
5478         assert(obstack_object_size(&asm_obst)
5479                         == out_size * sizeof(ir_asm_constraint));
5480         ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
5481
5482
5483         obstack_grow(&asm_obst, tmp_in_constraints,
5484                      in_size * sizeof(tmp_in_constraints[0]));
5485         /* find and count input and output arguments */
5486         argument = statement->inputs;
5487         for ( ; argument != NULL; argument = argument->next) {
5488                 const char *constraints = argument->constraints.begin;
5489                 asm_constraint_flags_t asm_flags
5490                         = be_parse_asm_constraints(constraints);
5491
5492                 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5493                         errorf(&statement->base.source_position,
5494                                "some constraints in '%s' are not supported", constraints);
5495                         continue;
5496                 }
5497                 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5498                         errorf(&statement->base.source_position,
5499                                "some constraints in '%s' are invalid", constraints);
5500                         continue;
5501                 }
5502                 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
5503                         errorf(&statement->base.source_position,
5504                                "write flag specified for input constraints '%s'",
5505                                constraints);
5506                         continue;
5507                 }
5508
5509                 ir_node *input;
5510                 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5511                                 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5512                         /* we can treat this as "normal" input */
5513                         input = expression_to_firm(argument->expression);
5514                 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5515                         /* pure memory ops need no input (but we have to make sure we
5516                          * attach to the memory) */
5517                         assert(! (asm_flags &
5518                                                 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5519                                                  | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5520                         needs_memory = true;
5521                         input = expression_to_addr(argument->expression);
5522                 } else {
5523                         errorf(&statement->base.source_position,
5524                                "only modifiers but no place set in constraints '%s'",
5525                                constraints);
5526                         continue;
5527                 }
5528
5529                 ir_asm_constraint constraint;
5530                 constraint.pos        = next_pos++;
5531                 constraint.constraint = new_id_from_str(constraints);
5532                 constraint.mode       = get_irn_mode(input);
5533
5534                 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5535                 ins[in_size++] = input;
5536         }
5537
5538         if (needs_memory) {
5539                 ir_asm_constraint constraint;
5540                 constraint.pos        = next_pos++;
5541                 constraint.constraint = new_id_from_str("");
5542                 constraint.mode       = mode_M;
5543
5544                 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5545                 ins[in_size++] = get_store();
5546         }
5547
5548         assert(obstack_object_size(&asm_obst)
5549                         == in_size * sizeof(ir_asm_constraint));
5550         ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
5551
5552         /* create asm node */
5553         dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5554
5555         ident *asm_text = new_id_from_str(statement->asm_text.begin);
5556
5557         ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
5558                                   out_size, output_constraints,
5559                                   n_clobbers, clobbers, asm_text);
5560
5561         if (statement->is_volatile) {
5562                 set_irn_pinned(node, op_pin_state_pinned);
5563         } else {
5564                 set_irn_pinned(node, op_pin_state_floats);
5565         }
5566
5567         /* create output projs & connect them */
5568         if (needs_memory) {
5569                 ir_node *projm = new_Proj(node, mode_M, out_size);
5570                 set_store(projm);
5571         }
5572
5573         size_t i;
5574         for (i = 0; i < out_size; ++i) {
5575                 const expression_t *out_expr = out_exprs[i];
5576                 long                pn       = i;
5577                 ir_mode            *mode     = get_ir_mode_storage(out_expr->base.type);
5578                 ir_node            *proj     = new_Proj(node, mode, pn);
5579                 ir_node            *addr     = out_addrs[i];
5580
5581                 set_value_for_expression_addr(out_expr, proj, addr);
5582         }
5583 }
5584
5585 static void     ms_try_statement_to_firm(ms_try_statement_t *statement)
5586 {
5587         statement_to_firm(statement->try_statement);
5588         warningf(&statement->base.source_position, "structured exception handling ignored");
5589 }
5590
5591 static void     leave_statement_to_firm(leave_statement_t *statement)
5592 {
5593         errorf(&statement->base.source_position, "__leave not supported yet");
5594 }
5595
5596 /**
5597  * Transform a statement.
5598  */
5599 static void statement_to_firm(statement_t *statement)
5600 {
5601 #ifndef NDEBUG
5602         assert(!statement->base.transformed);
5603         statement->base.transformed = true;
5604 #endif
5605
5606         switch (statement->kind) {
5607         case STATEMENT_INVALID:
5608                 panic("invalid statement found");
5609         case STATEMENT_EMPTY:
5610                 /* nothing */
5611                 return;
5612         case STATEMENT_COMPOUND:
5613                 compound_statement_to_firm(&statement->compound);
5614                 return;
5615         case STATEMENT_RETURN:
5616                 return_statement_to_firm(&statement->returns);
5617                 return;
5618         case STATEMENT_EXPRESSION:
5619                 expression_statement_to_firm(&statement->expression);
5620                 return;
5621         case STATEMENT_IF:
5622                 if_statement_to_firm(&statement->ifs);
5623                 return;
5624         case STATEMENT_WHILE:
5625                 while_statement_to_firm(&statement->whiles);
5626                 return;
5627         case STATEMENT_DO_WHILE:
5628                 do_while_statement_to_firm(&statement->do_while);
5629                 return;
5630         case STATEMENT_DECLARATION:
5631                 declaration_statement_to_firm(&statement->declaration);
5632                 return;
5633         case STATEMENT_BREAK:
5634                 create_jump_statement(statement, get_break_label());
5635                 return;
5636         case STATEMENT_CONTINUE:
5637                 create_jump_statement(statement, continue_label);
5638                 return;
5639         case STATEMENT_SWITCH:
5640                 switch_statement_to_firm(&statement->switchs);
5641                 return;
5642         case STATEMENT_CASE_LABEL:
5643                 case_label_to_firm(&statement->case_label);
5644                 return;
5645         case STATEMENT_FOR:
5646                 for_statement_to_firm(&statement->fors);
5647                 return;
5648         case STATEMENT_LABEL:
5649                 label_to_firm(&statement->label);
5650                 return;
5651         case STATEMENT_GOTO:
5652                 goto_to_firm(&statement->gotos);
5653                 return;
5654         case STATEMENT_ASM:
5655                 asm_statement_to_firm(&statement->asms);
5656                 return;
5657         case STATEMENT_MS_TRY:
5658                 ms_try_statement_to_firm(&statement->ms_try);
5659                 return;
5660         case STATEMENT_LEAVE:
5661                 leave_statement_to_firm(&statement->leave);
5662                 return;
5663         }
5664         panic("statement not implemented");
5665 }
5666
5667 static int count_local_variables(const entity_t *entity,
5668                                  const entity_t *const last)
5669 {
5670         int count = 0;
5671         entity_t const *const end = last != NULL ? last->base.next : NULL;
5672         for (; entity != end; entity = entity->base.next) {
5673                 type_t *type;
5674                 bool    address_taken;
5675
5676                 if (entity->kind == ENTITY_VARIABLE) {
5677                         type          = skip_typeref(entity->declaration.type);
5678                         address_taken = entity->variable.address_taken;
5679                 } else if (entity->kind == ENTITY_PARAMETER) {
5680                         type          = skip_typeref(entity->declaration.type);
5681                         address_taken = entity->parameter.address_taken;
5682                 } else {
5683                         continue;
5684                 }
5685
5686                 if (!address_taken && is_type_scalar(type))
5687                         ++count;
5688         }
5689         return count;
5690 }
5691
5692 static void count_local_variables_in_stmt(statement_t *stmt, void *const env)
5693 {
5694         int *const count = env;
5695
5696         switch (stmt->kind) {
5697         case STATEMENT_DECLARATION: {
5698                 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5699                 *count += count_local_variables(decl_stmt->declarations_begin,
5700                                 decl_stmt->declarations_end);
5701                 break;
5702         }
5703
5704         case STATEMENT_FOR:
5705                 *count += count_local_variables(stmt->fors.scope.entities, NULL);
5706                 break;
5707
5708         default:
5709                 break;
5710         }
5711 }
5712
5713 /**
5714  * Return the number of local (alias free) variables used by a function.
5715  */
5716 static int get_function_n_local_vars(entity_t *entity)
5717 {
5718         const function_t *function = &entity->function;
5719         int count = 0;
5720
5721         /* count parameters */
5722         count += count_local_variables(function->parameters.entities, NULL);
5723
5724         /* count local variables declared in body */
5725         walk_statements(function->statement, count_local_variables_in_stmt, &count);
5726         return count;
5727 }
5728
5729 /**
5730  * Build Firm code for the parameters of a function.
5731  */
5732 static void initialize_function_parameters(entity_t *entity)
5733 {
5734         assert(entity->kind == ENTITY_FUNCTION);
5735         ir_graph *irg             = current_ir_graph;
5736         ir_node  *args            = get_irg_args(irg);
5737         ir_type  *function_irtype = get_ir_type(entity->declaration.type);
5738         int      first_param_nr   = 0;
5739
5740         if (entity->function.need_closure) {
5741                 /* add an extra parameter for the static link */
5742                 entity->function.static_link = new_r_Proj(args, mode_P_data, 0);
5743                 ++first_param_nr;
5744         }
5745
5746         int       n         = 0;
5747         entity_t *parameter = entity->function.parameters.entities;
5748         for ( ; parameter != NULL; parameter = parameter->base.next, ++n) {
5749                 if (parameter->kind != ENTITY_PARAMETER)
5750                         continue;
5751
5752                 assert(parameter->declaration.kind == DECLARATION_KIND_UNKNOWN);
5753                 type_t *type = skip_typeref(parameter->declaration.type);
5754
5755                 bool needs_entity = parameter->parameter.address_taken;
5756                 assert(!is_type_array(type));
5757                 if (is_type_compound(type)) {
5758                         needs_entity = true;
5759                 }
5760
5761                 if (needs_entity) {
5762                         ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5763                         ident     *id     = new_id_from_str(parameter->base.symbol->string);
5764                         set_entity_ident(entity, id);
5765
5766                         parameter->declaration.kind
5767                                 = DECLARATION_KIND_PARAMETER_ENTITY;
5768                         parameter->parameter.v.entity = entity;
5769                         continue;
5770                 }
5771
5772                 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5773                 ir_mode *param_mode   = get_type_mode(param_irtype);
5774
5775                 long     pn    = n + first_param_nr;
5776                 ir_node *value = new_r_Proj(args, param_mode, pn);
5777
5778                 ir_mode *mode = get_ir_mode_storage(type);
5779                 value = create_conv(NULL, value, mode);
5780                 value = do_strict_conv(NULL, value);
5781
5782                 parameter->declaration.kind         = DECLARATION_KIND_PARAMETER;
5783                 parameter->parameter.v.value_number = next_value_number_function;
5784                 set_irg_loc_description(current_ir_graph, next_value_number_function,
5785                                         parameter);
5786                 ++next_value_number_function;
5787
5788                 set_value(parameter->parameter.v.value_number, value);
5789         }
5790 }
5791
5792 /**
5793  * Handle additional decl modifiers for IR-graphs
5794  *
5795  * @param irg            the IR-graph
5796  * @param dec_modifiers  additional modifiers
5797  */
5798 static void handle_decl_modifier_irg(ir_graph_ptr irg,
5799                                      decl_modifiers_t decl_modifiers)
5800 {
5801         if (decl_modifiers & DM_RETURNS_TWICE) {
5802                 /* TRUE if the declaration includes __attribute__((returns_twice)) */
5803                 add_irg_additional_properties(irg, mtp_property_returns_twice);
5804         }
5805         if (decl_modifiers & DM_NORETURN) {
5806                 /* TRUE if the declaration includes the Microsoft
5807                    __declspec(noreturn) specifier. */
5808                 add_irg_additional_properties(irg, mtp_property_noreturn);
5809         }
5810         if (decl_modifiers & DM_NOTHROW) {
5811                 /* TRUE if the declaration includes the Microsoft
5812                    __declspec(nothrow) specifier. */
5813                 add_irg_additional_properties(irg, mtp_property_nothrow);
5814         }
5815         if (decl_modifiers & DM_NAKED) {
5816                 /* TRUE if the declaration includes the Microsoft
5817                    __declspec(naked) specifier. */
5818                 add_irg_additional_properties(irg, mtp_property_naked);
5819         }
5820         if (decl_modifiers & DM_FORCEINLINE) {
5821                 /* TRUE if the declaration includes the
5822                    Microsoft __forceinline specifier. */
5823                 set_irg_inline_property(irg, irg_inline_forced);
5824         }
5825         if (decl_modifiers & DM_NOINLINE) {
5826                 /* TRUE if the declaration includes the Microsoft
5827                    __declspec(noinline) specifier. */
5828                 set_irg_inline_property(irg, irg_inline_forbidden);
5829         }
5830 }
5831
5832 static void add_function_pointer(ir_type *segment, ir_entity *method,
5833                                  const char *unique_template)
5834 {
5835         ir_type   *method_type  = get_entity_type(method);
5836         ir_type   *ptr_type     = new_type_pointer(method_type);
5837
5838         /* these entities don't really have a name but firm only allows
5839          * "" in ld_ident.
5840          * Note that we mustn't give these entities a name since for example
5841          * Mach-O doesn't allow them. */
5842         ident     *ide          = id_unique(unique_template);
5843         ir_entity *ptr          = new_entity(segment, ide, ptr_type);
5844         ir_graph  *irg          = get_const_code_irg();
5845         ir_node   *val          = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5846                                                            method);
5847
5848         set_entity_ld_ident(ptr, new_id_from_chars("", 0));
5849         set_entity_compiler_generated(ptr, 1);
5850         set_entity_visibility(ptr, ir_visibility_local);
5851         add_entity_linkage(ptr, IR_LINKAGE_CONSTANT|IR_LINKAGE_HIDDEN_USER);
5852         set_atomic_ent_value(ptr, val);
5853 }
5854
5855 /**
5856  * Generate possible IJmp branches to a given label block.
5857  */
5858 static void gen_ijmp_branches(ir_node *block)
5859 {
5860         ir_node *ijmp;
5861         for (ijmp = ijmp_list; ijmp != NULL; ijmp = get_irn_link(ijmp)) {
5862                 add_immBlock_pred(block, ijmp);
5863         }
5864 }
5865
5866 /**
5867  * Create code for a function and all inner functions.
5868  *
5869  * @param entity  the function entity
5870  */
5871 static void create_function(entity_t *entity)
5872 {
5873         assert(entity->kind == ENTITY_FUNCTION);
5874         ir_entity *function_entity = get_function_entity(entity, current_outer_frame);
5875
5876         if (entity->function.statement == NULL)
5877                 return;
5878
5879         if (is_main(entity) && enable_main_collect2_hack) {
5880                 prepare_main_collect2(entity);
5881         }
5882
5883         inner_functions     = NULL;
5884         current_trampolines = NULL;
5885
5886         if (entity->declaration.modifiers & DM_CONSTRUCTOR) {
5887                 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5888                 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5889         }
5890         if (entity->declaration.modifiers & DM_DESTRUCTOR) {
5891                 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5892                 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5893         }
5894
5895         current_function_entity = entity;
5896         current_function_name   = NULL;
5897         current_funcsig         = NULL;
5898
5899         assert(all_labels == NULL);
5900         all_labels = NEW_ARR_F(label_t *, 0);
5901         ijmp_list  = NULL;
5902
5903         int       n_local_vars = get_function_n_local_vars(entity);
5904         ir_graph *irg          = new_ir_graph(function_entity, n_local_vars);
5905         current_ir_graph = irg;
5906
5907         ir_graph *old_current_function = current_function;
5908         current_function = irg;
5909
5910         set_irg_fp_model(irg, firm_opt.fp_model);
5911         tarval_enable_fp_ops(1);
5912         set_irn_dbg_info(get_irg_start_block(irg),
5913                          get_entity_dbg_info(function_entity));
5914
5915         ir_node *first_block = get_cur_block();
5916
5917         /* set inline flags */
5918         if (entity->function.is_inline)
5919                 set_irg_inline_property(irg, irg_inline_recomended);
5920         handle_decl_modifier_irg(irg, entity->declaration.modifiers);
5921
5922         next_value_number_function = 0;
5923         initialize_function_parameters(entity);
5924         current_static_link = entity->function.static_link;
5925
5926         statement_to_firm(entity->function.statement);
5927
5928         ir_node *end_block = get_irg_end_block(irg);
5929
5930         /* do we have a return statement yet? */
5931         if (get_cur_block() != NULL) {
5932                 type_t *type = skip_typeref(entity->declaration.type);
5933                 assert(is_type_function(type));
5934                 const function_type_t *func_type   = &type->function;
5935                 const type_t          *return_type
5936                         = skip_typeref(func_type->return_type);
5937
5938                 ir_node *ret;
5939                 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5940                         ret = new_Return(get_store(), 0, NULL);
5941                 } else {
5942                         ir_mode *mode;
5943                         if (is_type_scalar(return_type)) {
5944                                 mode = get_ir_mode_storage(func_type->return_type);
5945                         } else {
5946                                 mode = mode_P_data;
5947                         }
5948
5949                         ir_node *in[1];
5950                         /* ยง5.1.2.2.3 main implicitly returns 0 */
5951                         if (is_main(entity)) {
5952                                 in[0] = new_Const(get_mode_null(mode));
5953                         } else {
5954                                 in[0] = new_Unknown(mode);
5955                         }
5956                         ret = new_Return(get_store(), 1, in);
5957                 }
5958                 add_immBlock_pred(end_block, ret);
5959         }
5960
5961         bool has_computed_gotos = false;
5962         for (int i = ARR_LEN(all_labels) - 1; i >= 0; --i) {
5963                 label_t *label = all_labels[i];
5964                 if (label->address_taken) {
5965                         gen_ijmp_branches(label->block);
5966                         has_computed_gotos = true;
5967                 }
5968                 mature_immBlock(label->block);
5969         }
5970         if (has_computed_gotos) {
5971                 /* if we have computed goto's in the function, we cannot inline it */
5972                 if (get_irg_inline_property(irg) >= irg_inline_recomended) {
5973                         warningf(&entity->base.source_position,
5974                                  "function '%Y' can never be inlined because it contains a computed goto",
5975                                  entity->base.symbol);
5976                 }
5977                 set_irg_inline_property(irg, irg_inline_forbidden);
5978         }
5979
5980         DEL_ARR_F(all_labels);
5981         all_labels = NULL;
5982
5983         mature_immBlock(first_block);
5984         mature_immBlock(end_block);
5985
5986         irg_finalize_cons(irg);
5987
5988         /* finalize the frame type */
5989         ir_type *frame_type = get_irg_frame_type(irg);
5990         int      n          = get_compound_n_members(frame_type);
5991         int      align_all  = 4;
5992         int      offset     = 0;
5993         for (int i = 0; i < n; ++i) {
5994                 ir_entity *entity      = get_compound_member(frame_type, i);
5995                 ir_type   *entity_type = get_entity_type(entity);
5996
5997                 int align = get_type_alignment_bytes(entity_type);
5998                 if (align > align_all)
5999                         align_all = align;
6000                 int misalign = 0;
6001                 if (align > 0) {
6002                         misalign  = offset % align;
6003                         if (misalign > 0) {
6004                                 offset += align - misalign;
6005                         }
6006                 }
6007
6008                 set_entity_offset(entity, offset);
6009                 offset += get_type_size_bytes(entity_type);
6010         }
6011         set_type_size_bytes(frame_type, offset);
6012         set_type_alignment_bytes(frame_type, align_all);
6013
6014         irg_verify(irg, VERIFY_ENFORCE_SSA);
6015         current_function = old_current_function;
6016
6017         if (current_trampolines != NULL) {
6018                 DEL_ARR_F(current_trampolines);
6019                 current_trampolines = NULL;
6020         }
6021
6022         /* create inner functions if any */
6023         entity_t **inner = inner_functions;
6024         if (inner != NULL) {
6025                 ir_type *rem_outer_frame      = current_outer_frame;
6026                 current_outer_frame           = get_irg_frame_type(current_ir_graph);
6027                 ir_type *rem_outer_value_type = current_outer_value_type;
6028                 current_outer_value_type      = get_irg_value_param_type(current_ir_graph);
6029                 for (int i = ARR_LEN(inner) - 1; i >= 0; --i) {
6030                         create_function(inner[i]);
6031                 }
6032                 DEL_ARR_F(inner);
6033
6034                 current_outer_value_type = rem_outer_value_type;
6035                 current_outer_frame      = rem_outer_frame;
6036         }
6037 }
6038
6039 static void scope_to_firm(scope_t *scope)
6040 {
6041         /* first pass: create declarations */
6042         entity_t *entity = scope->entities;
6043         for ( ; entity != NULL; entity = entity->base.next) {
6044                 if (entity->base.symbol == NULL)
6045                         continue;
6046
6047                 if (entity->kind == ENTITY_FUNCTION) {
6048                         if (entity->function.btk != bk_none) {
6049                                 /* builtins have no representation */
6050                                 continue;
6051                         }
6052                         (void)get_function_entity(entity, NULL);
6053                 } else if (entity->kind == ENTITY_VARIABLE) {
6054                         create_global_variable(entity);
6055                 } else if (entity->kind == ENTITY_NAMESPACE) {
6056                         scope_to_firm(&entity->namespacee.members);
6057                 }
6058         }
6059
6060         /* second pass: create code/initializers */
6061         entity = scope->entities;
6062         for ( ; entity != NULL; entity = entity->base.next) {
6063                 if (entity->base.symbol == NULL)
6064                         continue;
6065
6066                 if (entity->kind == ENTITY_FUNCTION) {
6067                         if (entity->function.btk != bk_none) {
6068                                 /* builtins have no representation */
6069                                 continue;
6070                         }
6071                         create_function(entity);
6072                 } else if (entity->kind == ENTITY_VARIABLE) {
6073                         assert(entity->declaration.kind
6074                                         == DECLARATION_KIND_GLOBAL_VARIABLE);
6075                         current_ir_graph = get_const_code_irg();
6076                         create_variable_initializer(entity);
6077                 }
6078         }
6079 }
6080
6081 void init_ast2firm(void)
6082 {
6083         obstack_init(&asm_obst);
6084         init_atomic_modes();
6085
6086         ir_set_debug_retrieve(dbg_retrieve);
6087         ir_set_type_debug_retrieve(dbg_print_type_dbg_info);
6088
6089         /* create idents for all known runtime functions */
6090         for (size_t i = 0; i < lengthof(rts_data); ++i) {
6091                 rts_idents[i] = new_id_from_str(rts_data[i].name);
6092         }
6093
6094         entitymap_init(&entitymap);
6095 }
6096
6097 static void init_ir_types(void)
6098 {
6099         static int ir_types_initialized = 0;
6100         if (ir_types_initialized)
6101                 return;
6102         ir_types_initialized = 1;
6103
6104         ir_type_int        = get_ir_type(type_int);
6105         ir_type_char       = get_ir_type(type_char);
6106         ir_type_const_char = get_ir_type(type_const_char);
6107         ir_type_wchar_t    = get_ir_type(type_wchar_t);
6108         ir_type_void       = get_ir_type(type_void);
6109
6110         be_params             = be_get_backend_param();
6111         mode_float_arithmetic = be_params->mode_float_arithmetic;
6112
6113         stack_param_align     = be_params->stack_param_align;
6114 }
6115
6116 void exit_ast2firm(void)
6117 {
6118         entitymap_destroy(&entitymap);
6119         obstack_free(&asm_obst, NULL);
6120 }
6121
6122 static void global_asm_to_firm(statement_t *s)
6123 {
6124         for (; s != NULL; s = s->base.next) {
6125                 assert(s->kind == STATEMENT_ASM);
6126
6127                 char const *const text = s->asms.asm_text.begin;
6128                 size_t            size = s->asms.asm_text.size;
6129
6130                 /* skip the last \0 */
6131                 if (text[size - 1] == '\0')
6132                         --size;
6133
6134                 ident *const id = new_id_from_chars(text, size);
6135                 add_irp_asm(id);
6136         }
6137 }
6138
6139 void translation_unit_to_firm(translation_unit_t *unit)
6140 {
6141         /* initialize firm arithmetic */
6142         tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
6143         ir_set_uninitialized_local_variable_func(uninitialized_local_var);
6144
6145         /* just to be sure */
6146         continue_label           = NULL;
6147         break_label              = NULL;
6148         current_switch_cond      = NULL;
6149         current_translation_unit = unit;
6150
6151         init_ir_types();
6152
6153         scope_to_firm(&unit->scope);
6154         global_asm_to_firm(unit->global_asm);
6155
6156         current_ir_graph         = NULL;
6157         current_translation_unit = NULL;
6158 }