Remove the unused STATEMENT_LOCAL_LABEL/struct local_label_statement_t. It never...
[cparser] / ast2firm.c
index 459be89..c30463c 100644 (file)
 #include "diagnostic.h"
 #include "lang_features.h"
 #include "types.h"
+#include "type_hash.h"
+#include "mangle.h"
 #include "walk_statements.h"
 #include "warning.h"
 #include "entitymap_t.h"
 #include "driver/firm_opt.h"
 #include "driver/firm_cmdline.h"
 
-/* some idents needed for name mangling */
-static ident *id_underscore;
-static ident *id_imp;
-
 static ir_type *ir_type_const_char;
 static ir_type *ir_type_wchar_t;
 static ir_type *ir_type_void;
@@ -73,6 +71,7 @@ static ir_node            *current_function_name;
 static ir_node            *current_funcsig;
 static switch_statement_t *current_switch;
 static ir_graph           *current_function;
+static translation_unit_t *current_translation_unit;
 
 static entitymap_t  entitymap;
 
@@ -84,12 +83,14 @@ typedef enum declaration_kind_t {
        DECLARATION_KIND_GLOBAL_VARIABLE,
        DECLARATION_KIND_LOCAL_VARIABLE,
        DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
+       DECLARATION_KIND_PARAMETER,
+       DECLARATION_KIND_PARAMETER_ENTITY,
        DECLARATION_KIND_FUNCTION,
        DECLARATION_KIND_COMPOUND_MEMBER,
        DECLARATION_KIND_INNER_FUNCTION
 } declaration_kind_t;
 
-static ir_type *get_ir_type(type_t *type);
+static ir_mode *get_ir_mode(type_t *type);
 static ir_type *get_ir_type_incomplete(type_t *type);
 
 static void enqueue_inner_function(entity_t *entity)
@@ -111,12 +112,13 @@ static entity_t *next_inner_function(void)
 
 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
 {
-       const variable_t *variable = get_irg_loc_description(irg, pos);
+       const entity_t *entity = get_irg_loc_description(irg, pos);
 
-       if (variable != NULL) {
-               warningf(&variable->base.base.source_position,
-                        "variable '%#T' might be used uninitialized",
-                        variable->base.type, variable->base.base.symbol);
+       if (entity != NULL) {
+               warningf(&entity->base.source_position,
+                        "%s '%#T' might be used uninitialized",
+                        get_entity_kind_name(entity->kind),
+                        entity->declaration.type, entity->base.symbol);
        }
        return new_r_Unknown(irg, mode);
 }
@@ -151,7 +153,6 @@ static ir_mode *mode_int, *mode_uint;
 
 static ir_node *_expression_to_firm(const expression_t *expression);
 static ir_node *expression_to_firm(const expression_t *expression);
-static inline ir_mode *get_ir_mode(type_t *type);
 static void create_local_declaration(entity_t *entity);
 
 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
@@ -164,6 +165,7 @@ static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
                ir_mode_sort    sort;
                unsigned        bit_size     = size * 8;
                bool            is_signed    = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
+               unsigned        modulo_shift;
                ir_mode_arithmetic arithmetic;
 
                if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
@@ -172,16 +174,16 @@ static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
                                 bit_size);
                        sort         = irms_int_number;
                        arithmetic   = irma_twos_complement;
+                       modulo_shift = bit_size < machine_size ? machine_size : bit_size;
                } else {
                        assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
                        snprintf(name, sizeof(name), "F%u", bit_size);
                        sort         = irms_float_number;
                        arithmetic   = irma_ieee754;
+                       modulo_shift = 0;
                }
-               /* note: modulo_shift is 0, as in C it's undefined anyway to shift
-                * a too big amount */
                return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
-                                  0);
+                                  modulo_shift);
        }
 
        return NULL;
@@ -199,7 +201,7 @@ static void init_atomic_modes(void)
        mode_uint = atomic_modes[ATOMIC_TYPE_UINT];
 
        /* there's no real void type in firm */
-       atomic_modes[ATOMIC_TYPE_VOID] = mode_int;
+       atomic_modes[ATOMIC_TYPE_VOID] = atomic_modes[ATOMIC_TYPE_CHAR];
 
        /* initialize pointer modes */
        char            name[64];
@@ -261,6 +263,7 @@ static unsigned get_type_size_const(type_t *type)
                /* just a pointer to the function */
                return get_mode_size_bytes(mode_P_code);
        case TYPE_POINTER:
+       case TYPE_REFERENCE:
                return get_mode_size_bytes(mode_P_data);
        case TYPE_ARRAY:
                return get_array_type_size(&type->array);
@@ -276,18 +279,22 @@ static unsigned get_type_size_const(type_t *type)
        panic("Trying to determine size of invalid type");
 }
 
+static ir_node *get_vla_size(array_type_t *const type)
+{
+       ir_node *size_node = type->size_node;
+       if (size_node == NULL) {
+               size_node = expression_to_firm(type->size_expression);
+               type->size_node = size_node;
+       }
+       return size_node;
+}
+
 static ir_node *get_type_size(type_t *type)
 {
        type = skip_typeref(type);
 
        if (is_type_array(type) && type->array.is_vla) {
-               ir_node *size_node = type->array.size_node;
-               if (size_node == NULL) {
-                       size_node = expression_to_firm(type->array.size_expression);
-                       assert(!is_Const(size_node));
-                       type->array.size_node = size_node;
-               }
-
+               ir_node *size_node = get_vla_size(&type->array);
                ir_node *elem_size = get_type_size(type->array.element_type);
                ir_mode *mode      = get_irn_mode(size_node);
                ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
@@ -426,6 +433,7 @@ is_cdecl:
                /* Hmm, leave default, not accepted by the parser yet. */
                break;
        }
+
        return irtype;
 }
 
@@ -439,6 +447,16 @@ static ir_type *create_pointer_type(pointer_type_t *type)
        return ir_type;
 }
 
+static ir_type *create_reference_type(reference_type_t *type)
+{
+       type_t  *refers_to    = type->refers_to;
+       ir_type *ir_refers_to = get_ir_type_incomplete(refers_to);
+       ir_type *ir_type      = new_type_pointer(id_unique("reference.%u"),
+                                                ir_refers_to, mode_P_data);
+
+       return ir_type;
+}
+
 static ir_type *create_array_type(array_type_t *type)
 {
        type_t  *element_type    = type->element_type;
@@ -497,7 +515,7 @@ static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
        snprintf(name, sizeof(name), "I%u", size);
        ident *id = new_id_from_str(name);
        dbg_info *dbgi = get_dbg_info(&builtin_source_position);
-       res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
+       res = new_d_type_primitive(id_mangle_u(get_type_ident(base_tp), id), mode, dbgi);
        set_primitive_base_type(res, base_tp);
 
        return res;
@@ -533,7 +551,7 @@ static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
        snprintf(name, sizeof(name), "U%u", size);
        ident *id = new_id_from_str(name);
        dbg_info *dbgi = get_dbg_info(&builtin_source_position);
-       res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
+       res = new_d_type_primitive(id_mangle_u(get_type_ident(base_tp), id), mode, dbgi);
        set_primitive_base_type(res, base_tp);
 
        return res;
@@ -803,7 +821,7 @@ static ir_type *get_ir_type_incomplete(type_t *type)
        }
 }
 
-static ir_type *get_ir_type(type_t *type)
+ir_type *get_ir_type(type_t *type)
 {
        assert(type != NULL);
 
@@ -835,6 +853,9 @@ static ir_type *get_ir_type(type_t *type)
        case TYPE_POINTER:
                firm_type = create_pointer_type(&type->pointer);
                break;
+       case TYPE_REFERENCE:
+               firm_type = create_reference_type(&type->reference);
+               break;
        case TYPE_ARRAY:
                firm_type = create_array_type(&type->array);
                break;
@@ -868,7 +889,7 @@ static ir_type *get_ir_type(type_t *type)
        return firm_type;
 }
 
-static inline ir_mode *get_ir_mode(type_t *type)
+ir_mode *get_ir_mode(type_t *type)
 {
        ir_type *irtype = get_ir_type(type);
 
@@ -966,59 +987,7 @@ static const struct {
 
 static ident *rts_idents[sizeof(rts_data) / sizeof(rts_data[0])];
 
-/**
- * Mangles an entity linker (ld) name for win32 usage.
- *
- * @param ent          the entity to be mangled
- * @param declaration  the declaration
- */
-static ident *create_ld_ident_win32(ir_entity *irentity, entity_t *entity)
-{
-       ident *id;
-
-       if (is_Method_type(get_entity_type(irentity)))
-               id = decorate_win32_c_fkt(irentity, get_entity_ident(irentity));
-       else {
-               /* always add an underscore in win32 */
-               id = mangle(id_underscore, get_entity_ident(irentity));
-       }
-
-       assert(is_declaration(entity));
-       decl_modifiers_t decl_modifiers = entity->declaration.modifiers;
-       if (decl_modifiers & DM_DLLIMPORT) {
-               /* add prefix for imported symbols */
-               id = mangle(id_imp, id);
-       }
-       return id;
-}
-
-/**
- * Mangles an entity linker (ld) name for Linux ELF usage.
- *
- * @param ent          the entity to be mangled
- * @param declaration  the declaration
- */
-static ident *create_ld_ident_linux_elf(ir_entity *irentity, entity_t *entity)
-{
-       (void) entity;
-       return get_entity_ident(irentity);
-}
-
-/**
- * Mangles an entity linker (ld) name for Mach-O usage.
- *
- * @param ent          the entity to be mangled
- * @param declaration  the declaration
- */
-static ident *create_ld_ident_macho(ir_entity *irentity, entity_t *entity)
-{
-       (void) entity;
-       ident *id = mangle(id_underscore, get_entity_ident(irentity));
-       return id;
-}
-
-typedef ident* (*create_ld_ident_func)(ir_entity *irentity, entity_t *entity);
-create_ld_ident_func create_ld_ident = create_ld_ident_linux_elf;
+static ident* (*create_ld_ident)(entity_t*) = create_name_linux_elf;
 
 /**
  * Handle GNU attributes for entities
@@ -1046,6 +1015,22 @@ static void handle_gnu_attributes_ent(ir_entity *irentity, entity_t *entity)
        }
 }
 
+static bool is_main(entity_t *entity)
+{
+       static symbol_t *sym_main = NULL;
+       if (sym_main == NULL) {
+               sym_main = symbol_table_insert("main");
+       }
+
+       if (entity->base.symbol != sym_main)
+               return false;
+       /* must be in outermost scope */
+       if (entity->base.parent_scope != &current_translation_unit->scope)
+               return false;
+
+       return true;
+}
+
 /**
  * Creates an entity representing a function.
  *
@@ -1058,6 +1043,22 @@ static ir_entity *get_function_entity(entity_t *entity)
                return entity->function.entity;
        }
 
+       if (is_main(entity)) {
+               /* force main to C linkage */
+               type_t *type = entity->declaration.type;
+               assert(is_type_function(type));
+               if (type->function.linkage != LINKAGE_C) {
+                       type_t *new_type           = duplicate_type(type);
+                       new_type->function.linkage = LINKAGE_C;
+
+                       type = typehash_insert(new_type);
+                       if (type != new_type) {
+                               obstack_free(type_obst, new_type);
+                       }
+                       entity->declaration.type = type;
+               }
+       }
+
        symbol_t *symbol = entity->base.symbol;
        ident    *id     = new_id_from_str(symbol->string);
 
@@ -1077,7 +1078,7 @@ static ir_entity *get_function_entity(entity_t *entity)
 
        dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
        irentity             = new_d_entity(global_type, id, ir_type_method, dbgi);
-       set_entity_ld_ident(irentity, create_ld_ident(irentity, entity));
+       set_entity_ld_ident(irentity, create_ld_ident(entity));
 
        handle_gnu_attributes_ent(irentity, entity);
 
@@ -1152,7 +1153,7 @@ static ir_node *const_to_firm(const const_expression_t *cnst)
                tv = new_tarval_from_str(buf, len, mode);
        }
 
-       return new_d_Const(dbgi, mode, tv);
+       return new_d_Const(dbgi, tv);
 }
 
 /**
@@ -1175,7 +1176,7 @@ static ir_node *character_constant_to_firm(const const_expression_t *cnst)
        size_t  len = snprintf(buf, sizeof(buf), "%lld", v);
        tarval *tv = new_tarval_from_str(buf, len, mode);
 
-       return new_d_Const(dbgi, mode, tv);
+       return new_d_Const(dbgi, tv);
 }
 
 /**
@@ -1192,7 +1193,7 @@ static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
        size_t  len = snprintf(buf, sizeof(buf), "%lld", v);
        tarval *tv = new_tarval_from_str(buf, len, mode);
 
-       return new_d_Const(dbgi, mode, tv);
+       return new_d_Const(dbgi, tv);
 }
 
 /**
@@ -1324,7 +1325,7 @@ static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
        ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
        ir_node *const load_res = new_d_Proj(dbgi, load, mode,   pn_Load_res);
 
-       if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
+       if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE && !is_Bad(load)) {
                set_Load_volatility(load, volatility_is_volatile);
        }
 
@@ -1362,17 +1363,12 @@ static ir_node *get_global_var_address(dbg_info *const dbgi,
        assert(entity->declaration.kind == DECLARATION_KIND_GLOBAL_VARIABLE);
 
        ir_entity *const irentity = entity->variable.v.entity;
-       switch ((storage_class_tag_t) entity->declaration.storage_class) {
-               case STORAGE_CLASS_THREAD:
-               case STORAGE_CLASS_THREAD_EXTERN:
-               case STORAGE_CLASS_THREAD_STATIC: {
-                       ir_node *const no_mem = new_NoMem();
-                       ir_node *const tls    = get_irg_tls(current_ir_graph);
-                       return new_d_simpleSel(dbgi, no_mem, tls, irentity);
-               }
-
-               default:
-                       return create_symconst(dbgi, mode_P_data, irentity);
+       if (entity->variable.thread_local) {
+               ir_node *const no_mem = new_NoMem();
+               ir_node *const tls    = get_irg_tls(current_ir_graph);
+               return new_d_simpleSel(dbgi, no_mem, tls, irentity);
+       } else {
+               return create_symconst(dbgi, mode_P_data, irentity);
        }
 }
 
@@ -1400,7 +1396,7 @@ static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
                return value;
 
        if (dest_mode == mode_b) {
-               ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
+               ir_node *zero = new_Const(get_mode_null(value_mode));
                ir_node *cmp  = new_d_Cmp(dbgi, value, zero);
                ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
                return proj;
@@ -1429,8 +1425,7 @@ static ir_node *reference_expression_enum_value_to_firm(
        /* make sure the type is constructed */
        (void) get_ir_type(type);
 
-       ir_mode *const mode = get_ir_mode(type);
-       return new_Const(mode, entity->enum_value.tv);
+       return new_Const(entity->enum_value.tv);
 }
 
 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
@@ -1451,6 +1446,10 @@ static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
                ir_mode *const mode = get_ir_mode(type);
                return get_value(entity->variable.v.value_number, mode);
        }
+       case DECLARATION_KIND_PARAMETER: {
+               ir_mode *const mode = get_ir_mode(type);
+               return get_value(entity->parameter.v.value_number, mode);
+       }
        case DECLARATION_KIND_FUNCTION: {
                ir_mode *const mode = get_ir_mode(type);
                return create_symconst(dbgi, mode, entity->function.entity);
@@ -1477,6 +1476,12 @@ static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
                ir_node   *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
                return deref_address(dbgi, entity->declaration.type, sel);
        }
+       case DECLARATION_KIND_PARAMETER_ENTITY: {
+               ir_entity *irentity = entity->parameter.v.entity;
+               ir_node   *frame    = get_local_frame(irentity);
+               ir_node   *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
+               return deref_address(dbgi, entity->declaration.type, sel);
+       }
 
        case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
                return entity->variable.v.vla_base;
@@ -1497,6 +1502,7 @@ static ir_node *reference_addr(const reference_expression_t *ref)
        switch((declaration_kind_t) entity->declaration.kind) {
        case DECLARATION_KIND_UNKNOWN:
                break;
+       case DECLARATION_KIND_PARAMETER:
        case DECLARATION_KIND_LOCAL_VARIABLE:
                /* you can store to a local variable (so we don't panic but return NULL
                 * as an indicator for no real address) */
@@ -1512,6 +1518,13 @@ static ir_node *reference_addr(const reference_expression_t *ref)
 
                return sel;
        }
+       case DECLARATION_KIND_PARAMETER_ENTITY: {
+               ir_entity *irentity = entity->parameter.v.entity;
+               ir_node   *frame    = get_local_frame(irentity);
+               ir_node   *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
+
+               return sel;
+       }
 
        case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
                return entity->variable.v.vla_base;
@@ -1570,7 +1583,7 @@ static ir_node *process_builtin_call(const call_expression_t *call)
        case T___builtin_infl: {
                ir_mode *mode = get_ir_mode(function_type->function.return_type);
                tarval  *tv   = get_mode_infinite(mode);
-               ir_node *res  = new_d_Const(dbgi, mode, tv);
+               ir_node *res  = new_d_Const(dbgi, tv);
                return   res;
        }
        case T___builtin_nan:
@@ -1580,13 +1593,19 @@ static ir_node *process_builtin_call(const call_expression_t *call)
                assert(is_type_function(function_type));
                ir_mode *mode = get_ir_mode(function_type->function.return_type);
                tarval  *tv   = get_mode_NAN(mode);
-               ir_node *res  = new_d_Const(dbgi, mode, tv);
+               ir_node *res  = new_d_Const(dbgi, tv);
                return res;
        }
+       case T___builtin_expect: {
+               expression_t *argument = call->arguments->expression;
+               return _expression_to_firm(argument);
+       }
        case T___builtin_va_end:
+               /* evaluate the argument of va_end for its side effects */
+               _expression_to_firm(call->arguments->expression);
                return NULL;
        default:
-               panic("Unsupported builtin found\n");
+               panic("unsupported builtin found");
        }
 }
 
@@ -1739,7 +1758,7 @@ static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
        if (is_type_scalar(type)) {
                ir_node  *store     = new_d_Store(dbgi, memory, addr, value);
                ir_node  *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
-               if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
+               if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE && !is_Bad(store))
                        set_Store_volatility(store, volatility_is_volatile);
                set_store(store_mem);
        } else {
@@ -1787,10 +1806,10 @@ static void bitfield_store_to_firm(dbg_info *dbgi,
        int      bitsize      = get_mode_size_bits(get_type_mode(entity_type));
 
        tarval  *mask            = create_bitfield_mask(mode, 0, bitsize);
-       ir_node *mask_node       = new_d_Const(dbgi, mode, mask);
+       ir_node *mask_node       = new_d_Const(dbgi, mask);
        ir_node *value_masked    = new_d_And(dbgi, value, mask_node, mode);
        tarval  *shiftl          = new_tarval_from_long(bitoffset, mode_uint);
-       ir_node *shiftcount      = new_d_Const(dbgi, mode_uint, shiftl);
+       ir_node *shiftcount      = new_d_Const(dbgi, shiftl);
        ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
 
        /* load current value */
@@ -1800,7 +1819,7 @@ static void bitfield_store_to_firm(dbg_info *dbgi,
        ir_node  *load_res        = new_d_Proj(dbgi, load, mode, pn_Load_res);
        tarval   *shift_mask      = create_bitfield_mask(mode, bitoffset, bitsize);
        tarval   *inv_mask        = tarval_not(shift_mask);
-       ir_node  *inv_mask_node   = new_d_Const(dbgi, mode, inv_mask);
+       ir_node  *inv_mask_node   = new_d_Const(dbgi, inv_mask);
        ir_node  *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
 
        /* construct new value and store */
@@ -1810,8 +1829,10 @@ static void bitfield_store_to_firm(dbg_info *dbgi,
        set_store(store_mem);
 
        if (set_volatile) {
-               set_Load_volatility(load, volatility_is_volatile);
-               set_Store_volatility(store, volatility_is_volatile);
+               if (!is_Bad(load))
+                       set_Load_volatility(load, volatility_is_volatile);
+               if (!is_Bad(store))
+                       set_Store_volatility(store, volatility_is_volatile);
        }
 }
 
@@ -1839,13 +1860,13 @@ static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
        long       shift_bitsl  = machine_size - bitoffset - bitsize;
        assert(shift_bitsl >= 0);
        tarval    *tvl          = new_tarval_from_long(shift_bitsl, mode_uint);
-       ir_node   *countl       = new_d_Const(dbgi, mode_uint, tvl);
+       ir_node   *countl       = new_d_Const(dbgi, tvl);
        ir_node   *shiftl       = new_d_Shl(dbgi, load_res, countl, mode_int);
 
        long       shift_bitsr  = bitoffset + shift_bitsl;
        assert(shift_bitsr <= (long) machine_size);
        tarval    *tvr          = new_tarval_from_long(shift_bitsr, mode_uint);
-       ir_node   *countr       = new_d_Const(dbgi, mode_uint, tvr);
+       ir_node   *countr       = new_d_Const(dbgi, tvr);
        ir_node   *shiftr;
        if (mode_is_signed(mode)) {
                shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
@@ -1881,11 +1902,15 @@ static void set_value_for_expression_addr(const expression_t *expression,
                if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
                        set_value(entity->variable.v.value_number, value);
                        return;
+               } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
+                       set_value(entity->parameter.v.value_number, value);
+                       return;
                }
        }
 
        if (addr == NULL)
                addr = expression_to_addr(expression);
+       assert(addr != NULL);
 
        type_t *type = skip_typeref(expression->base.type);
 
@@ -1921,12 +1946,17 @@ static ir_node *get_value_from_lvalue(const expression_t *expression,
                const reference_expression_t *ref = &expression->reference;
 
                entity_t *entity = ref->entity;
-               assert(entity->kind == ENTITY_VARIABLE);
+               assert(entity->kind == ENTITY_VARIABLE
+                               || entity->kind == ENTITY_PARAMETER);
                assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
                if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
                        assert(addr == NULL);
                        ir_mode *mode = get_ir_mode(expression->base.type);
                        return get_value(entity->variable.v.value_number, mode);
+               } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
+                       assert(addr == NULL);
+                       ir_mode *mode = get_ir_mode(expression->base.type);
+                       return get_value(entity->parameter.v.value_number, mode);
                }
        }
 
@@ -1962,7 +1992,7 @@ static ir_node *create_incdec(const unary_expression_t *expression)
                offset                       = get_type_size(pointer_type->points_to);
        } else {
                assert(is_type_arithmetic(type));
-               offset = new_Const(mode, get_mode_one(mode));
+               offset = new_Const(get_mode_one(mode));
        }
 
        ir_node *result;
@@ -2190,17 +2220,18 @@ static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
  * produces a 0/1 depending of the value of a mode_b node
  */
 static ir_node *produce_condition_result(const expression_t *expression,
-                                         dbg_info *dbgi)
+                                         ir_mode *mode, dbg_info *dbgi)
 {
-       ir_mode *mode      = get_ir_mode(expression->base.type);
        ir_node *cur_block = get_cur_block();
 
        ir_node *one_block = new_immBlock();
-       ir_node *one       = new_Const(mode, get_mode_one(mode));
+       set_cur_block(one_block);
+       ir_node *one       = new_Const(get_mode_one(mode));
        ir_node *jmp_one   = new_d_Jmp(dbgi);
 
        ir_node *zero_block = new_immBlock();
-       ir_node *zero       = new_Const(mode, get_mode_null(mode));
+       set_cur_block(zero_block);
+       ir_node *zero       = new_Const(get_mode_null(mode));
        ir_node *jmp_zero   = new_d_Jmp(dbgi);
 
        set_cur_block(cur_block);
@@ -2220,10 +2251,14 @@ static ir_node *produce_condition_result(const expression_t *expression,
 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
                ir_node *value, type_t *type)
 {
+       ir_mode        *const mode         = get_ir_mode(type_ptrdiff_t);
+       assert(is_type_pointer(type));
        pointer_type_t *const pointer_type = &type->pointer;
        type_t         *const points_to    = skip_typeref(pointer_type->points_to);
        unsigned              elem_size    = get_type_size_const(points_to);
 
+       value = create_conv(dbgi, value, mode);
+
        /* gcc extension: allow arithmetic with void * and function * */
        if ((elem_size == 0 && is_type_atomic(points_to, ATOMIC_TYPE_VOID)) ||
            is_type_function(points_to))  {
@@ -2234,9 +2269,8 @@ static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
        if (elem_size == 1)
                return value;
 
-       value = create_conv(dbgi, value, mode_int);
-       ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
-       ir_node *const mul  = new_d_Mul(dbgi, value, cnst, mode_int);
+       ir_node *const cnst = new_Const_long(mode, (long)elem_size);
+       ir_node *const mul  = new_d_Mul(dbgi, value, cnst, mode);
        return mul;
 }
 
@@ -2295,7 +2329,7 @@ static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
 
        default:
 normal_node:
-               mode = get_irn_mode(right);
+               mode = get_ir_mode(type_right);
                left = create_conv(dbgi, left, mode);
                break;
        }
@@ -2367,15 +2401,28 @@ static ir_node *create_lazy_op(const binary_expression_t *expression)
                long val = fold_constant(expression->left);
                expression_kind_t ekind = expression->base.kind;
                assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
-               if ((ekind == EXPR_BINARY_LOGICAL_AND && val != 0) ||
-                   (ekind == EXPR_BINARY_LOGICAL_OR  && val == 0)) {
-                       return expression_to_firm(expression->right);
+               if (ekind == EXPR_BINARY_LOGICAL_AND) {
+                       if (val == 0) {
+                               return new_Const(get_mode_null(mode));
+                       }
                } else {
-                       return new_Const(mode, get_mode_one(mode));
+                       if (val != 0) {
+                               return new_Const(get_mode_one(mode));
+                       }
+               }
+
+               if (is_constant_expression(expression->right)) {
+                       long const valr = fold_constant(expression->left);
+                       return valr != 0 ?
+                               new_Const(get_mode_one(mode)) :
+                               new_Const(get_mode_null(mode));
                }
+
+               return produce_condition_result(expression->right, mode, dbgi);
        }
 
-       return produce_condition_result((const expression_t*) expression, dbgi);
+       return produce_condition_result((const expression_t*) expression, mode,
+                                       dbgi);
 }
 
 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
@@ -2466,8 +2513,6 @@ static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
        case EXPR_BINARY_SHIFTLEFT_ASSIGN:
        case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
                return create_assign_binop(expression);
-       case EXPR_BINARY_BUILTIN_EXPECT:
-               return _expression_to_firm(expression->left);
        default:
                panic("TODO binexpr type");
        }
@@ -2475,28 +2520,12 @@ static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
 
 static ir_node *array_access_addr(const array_access_expression_t *expression)
 {
-       dbg_info *dbgi      = get_dbg_info(&expression->base.source_position);
-       ir_node  *base_addr = expression_to_firm(expression->array_ref);
-       ir_node  *offset    = expression_to_firm(expression->index);
-
-       type_t  *offset_type = skip_typeref(expression->index->base.type);
-       ir_mode *mode;
-       if (is_type_signed(offset_type)) {
-               mode = get_ir_mode(type_ssize_t);
-       } else {
-               mode = get_ir_mode(type_size_t);
-       }
-       offset = create_conv(dbgi, offset, mode);
-
-       type_t *ref_type = skip_typeref(expression->array_ref->base.type);
-       assert(is_type_pointer(ref_type));
-       pointer_type_t *pointer_type = &ref_type->pointer;
-
-       ir_node *elem_size_const = get_type_size(pointer_type->points_to);
-       elem_size_const          = create_conv(dbgi, elem_size_const, mode);
-       ir_node *real_offset     = new_d_Mul(dbgi, offset, elem_size_const,
-                                            mode);
-       ir_node *result          = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
+       dbg_info *dbgi        = get_dbg_info(&expression->base.source_position);
+       ir_node  *base_addr   = expression_to_firm(expression->array_ref);
+       ir_node  *offset      = expression_to_firm(expression->index);
+       type_t   *ref_type    = skip_typeref(expression->array_ref->base.type);
+       ir_node  *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
+       ir_node  *result      = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
 
        return result;
 }
@@ -2568,7 +2597,7 @@ static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
        tarval   *tv     = new_tarval_from_long(offset, mode);
        dbg_info *dbgi   = get_dbg_info(&expression->base.source_position);
 
-       return new_d_Const(dbgi, mode, tv);
+       return new_d_Const(dbgi, tv);
 }
 
 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
@@ -2668,12 +2697,12 @@ long fold_constant(const expression_t *expression)
        current_ir_graph = old_current_ir_graph;
 
        if (!is_Const(cnst)) {
-               panic("couldn't fold constant\n");
+               panic("couldn't fold constant");
        }
 
        tarval *tv = get_Const_tarval(cnst);
        if (!tarval_is_long(tv)) {
-               panic("result of constant folding is not integer\n");
+               panic("result of constant folding is not integer");
        }
 
        constant_folding = constant_folding_old;
@@ -2702,6 +2731,7 @@ static ir_node *conditional_to_firm(const conditional_expression_t *expression)
 
        /* create the true block */
        ir_node *true_block  = new_immBlock();
+       set_cur_block(true_block);
 
        ir_node *true_val = expression->true_expression != NULL ?
                expression_to_firm(expression->true_expression) : NULL;
@@ -2709,6 +2739,7 @@ static ir_node *conditional_to_firm(const conditional_expression_t *expression)
 
        /* create the false block */
        ir_node *false_block = new_immBlock();
+       set_cur_block(false_block);
 
        ir_node *false_val = expression_to_firm(expression->false_expression);
        ir_node *false_jmp = new_Jmp();
@@ -2722,8 +2753,7 @@ static ir_node *conditional_to_firm(const conditional_expression_t *expression)
                } else {
                        /* Condition ended with a short circuit (&&, ||, !) operation.
                         * Generate a "1" as value for the true branch. */
-                       ir_mode *const mode = mode_Is;
-                       true_val = new_Const(mode, get_mode_one(mode));
+                       true_val = new_Const(get_mode_one(mode_Is));
                }
        }
        mature_immBlock(true_block);
@@ -2759,13 +2789,17 @@ static ir_node *select_addr(const select_expression_t *expression)
        entity_t *entry = expression->compound_entry;
        assert(entry->kind == ENTITY_COMPOUND_MEMBER);
        assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
-       ir_entity *irentity = entry->compound_member.entity;
-
-       assert(irentity != NULL);
 
-       ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
-
-       return sel;
+       if (constant_folding) {
+               ir_mode *mode = get_irn_mode(compound_addr);
+               /* FIXME: here, we need an integer mode with the same number of bits as mode */
+               ir_node *ofs  = new_Const_long(mode_uint, entry->compound_member.offset);
+               return new_d_Add(dbgi, compound_addr, ofs, mode);
+       } else {
+               ir_entity *irentity = entry->compound_member.entity;
+               assert(irentity != NULL);
+               return new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
+       }
 }
 
 static ir_node *select_to_firm(const select_expression_t *expression)
@@ -2814,75 +2848,82 @@ typedef enum gcc_type_class
 
 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
 {
-       const type_t *const type = skip_typeref(expr->type_expression->base.type);
+       type_t *type = expr->type_expression->base.type;
 
+       /* FIXME gcc returns different values depending on whether compiling C or C++
+        * e.g. int x[10] is pointer_type_class in C, but array_type_class in C++ */
        gcc_type_class tc;
-       switch (type->kind)
-       {
-               case TYPE_ATOMIC: {
-                       const atomic_type_t *const atomic_type = &type->atomic;
-                       switch (atomic_type->akind) {
-                               /* should not be reached */
-                               case ATOMIC_TYPE_INVALID:
-                                       tc = no_type_class;
-                                       goto make_const;
-
-                               /* gcc cannot do that */
-                               case ATOMIC_TYPE_VOID:
-                                       tc = void_type_class;
-                                       goto make_const;
-
-                               case ATOMIC_TYPE_CHAR:      /* gcc handles this as integer */
-                               case ATOMIC_TYPE_SCHAR:     /* gcc handles this as integer */
-                               case ATOMIC_TYPE_UCHAR:     /* gcc handles this as integer */
-                               case ATOMIC_TYPE_SHORT:
-                               case ATOMIC_TYPE_USHORT:
-                               case ATOMIC_TYPE_INT:
-                               case ATOMIC_TYPE_UINT:
-                               case ATOMIC_TYPE_LONG:
-                               case ATOMIC_TYPE_ULONG:
-                               case ATOMIC_TYPE_LONGLONG:
-                               case ATOMIC_TYPE_ULONGLONG:
-                               case ATOMIC_TYPE_BOOL:      /* gcc handles this as integer */
-                                       tc = integer_type_class;
-                                       goto make_const;
-
-                               case ATOMIC_TYPE_FLOAT:
-                               case ATOMIC_TYPE_DOUBLE:
-                               case ATOMIC_TYPE_LONG_DOUBLE:
-                                       tc = real_type_class;
-                                       goto make_const;
+       for (;;) {
+               type = skip_typeref(type);
+               switch (type->kind) {
+                       case TYPE_ATOMIC: {
+                               const atomic_type_t *const atomic_type = &type->atomic;
+                               switch (atomic_type->akind) {
+                                       /* should not be reached */
+                                       case ATOMIC_TYPE_INVALID:
+                                               tc = no_type_class;
+                                               goto make_const;
+
+                                       /* gcc cannot do that */
+                                       case ATOMIC_TYPE_VOID:
+                                               tc = void_type_class;
+                                               goto make_const;
+
+                                       case ATOMIC_TYPE_WCHAR_T:   /* gcc handles this as integer */
+                                       case ATOMIC_TYPE_CHAR:      /* gcc handles this as integer */
+                                       case ATOMIC_TYPE_SCHAR:     /* gcc handles this as integer */
+                                       case ATOMIC_TYPE_UCHAR:     /* gcc handles this as integer */
+                                       case ATOMIC_TYPE_SHORT:
+                                       case ATOMIC_TYPE_USHORT:
+                                       case ATOMIC_TYPE_INT:
+                                       case ATOMIC_TYPE_UINT:
+                                       case ATOMIC_TYPE_LONG:
+                                       case ATOMIC_TYPE_ULONG:
+                                       case ATOMIC_TYPE_LONGLONG:
+                                       case ATOMIC_TYPE_ULONGLONG:
+                                       case ATOMIC_TYPE_BOOL:      /* gcc handles this as integer */
+                                               tc = integer_type_class;
+                                               goto make_const;
+
+                                       case ATOMIC_TYPE_FLOAT:
+                                       case ATOMIC_TYPE_DOUBLE:
+                                       case ATOMIC_TYPE_LONG_DOUBLE:
+                                               tc = real_type_class;
+                                               goto make_const;
+                               }
+                               panic("Unexpected atomic type in classify_type_to_firm().");
                        }
-                       panic("Unexpected atomic type in classify_type_to_firm().");
-               }
 
-               case TYPE_COMPLEX:         tc = complex_type_class; goto make_const;
-               case TYPE_IMAGINARY:       tc = complex_type_class; goto make_const;
-               case TYPE_BITFIELD:        tc = integer_type_class; goto make_const;
-               case TYPE_ARRAY:           /* gcc handles this as pointer */
-               case TYPE_FUNCTION:        /* gcc handles this as pointer */
-               case TYPE_POINTER:         tc = pointer_type_class; goto make_const;
-               case TYPE_COMPOUND_STRUCT: tc = record_type_class;  goto make_const;
-               case TYPE_COMPOUND_UNION:  tc = union_type_class;   goto make_const;
-
-               /* gcc handles this as integer */
-               case TYPE_ENUM:            tc = integer_type_class; goto make_const;
-
-               case TYPE_BUILTIN:
-               /* typedef/typeof should be skipped already */
-               case TYPE_TYPEDEF:
-               case TYPE_TYPEOF:
-               case TYPE_INVALID:
-               case TYPE_ERROR:
-                       break;
+                       case TYPE_COMPLEX:         tc = complex_type_class; goto make_const;
+                       case TYPE_IMAGINARY:       tc = complex_type_class; goto make_const;
+                       case TYPE_BITFIELD:        tc = integer_type_class; goto make_const;
+                       case TYPE_ARRAY:           /* gcc handles this as pointer */
+                       case TYPE_FUNCTION:        /* gcc handles this as pointer */
+                       case TYPE_POINTER:         tc = pointer_type_class; goto make_const;
+                       case TYPE_COMPOUND_STRUCT: tc = record_type_class;  goto make_const;
+                       case TYPE_COMPOUND_UNION:  tc = union_type_class;   goto make_const;
+
+                       /* gcc handles this as integer */
+                       case TYPE_ENUM:            tc = integer_type_class; goto make_const;
+
+                       /* gcc classifies the referenced type */
+                       case TYPE_REFERENCE: type = type->reference.refers_to; continue;
+
+                       case TYPE_BUILTIN:
+                       /* typedef/typeof should be skipped already */
+                       case TYPE_TYPEDEF:
+                       case TYPE_TYPEOF:
+                       case TYPE_INVALID:
+                       case TYPE_ERROR:
+                               break;
+               }
+               panic("unexpected TYPE classify_type_to_firm().");
        }
-       panic("unexpected TYPE classify_type_to_firm().");
 
-make_const: ;
+make_const:;
        dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
-       ir_mode  *const mode = mode_int;
-       tarval   *const tv   = new_tarval_from_long(tc, mode);
-       return new_d_Const(dbgi, mode, tv);
+       tarval   *const tv   = new_tarval_from_long(tc, mode_int);
+       return new_d_Const(dbgi, tv);
 }
 
 static ir_node *function_name_to_firm(
@@ -2969,17 +3010,18 @@ static ir_node *dereference_addr(const unary_expression_t *const expression)
 static ir_node *expression_to_addr(const expression_t *expression)
 {
        switch(expression->kind) {
-       case EXPR_REFERENCE:
-               return reference_addr(&expression->reference);
        case EXPR_ARRAY_ACCESS:
                return array_access_addr(&expression->array_access);
-       case EXPR_SELECT:
-               return select_addr(&expression->select);
        case EXPR_CALL:
                return call_expression_to_firm(&expression->call);
-       case EXPR_UNARY_DEREFERENCE: {
+       case EXPR_COMPOUND_LITERAL:
+               return compound_literal_to_firm(&expression->compound_literal);
+       case EXPR_REFERENCE:
+               return reference_addr(&expression->reference);
+       case EXPR_SELECT:
+               return select_addr(&expression->select);
+       case EXPR_UNARY_DEREFERENCE:
                return dereference_addr(&expression->unary);
-       }
        default:
                break;
        }
@@ -3019,9 +3061,7 @@ static ir_node *get_label_block(label_t *label)
        ir_graph *rem    = current_ir_graph;
        current_ir_graph = current_function;
 
-       ir_node *old_cur_block = get_cur_block();
-       ir_node *block         = new_immBlock();
-       set_cur_block(old_cur_block);
+       ir_node *block = new_immBlock();
 
        label->block = block;
 
@@ -3050,6 +3090,23 @@ static ir_node *label_address_to_firm(
        return new_SymConst(mode_P_code, value, symconst_label);
 }
 
+static ir_node *builtin_symbol_to_firm(
+               const builtin_symbol_expression_t *expression)
+{
+       /* for gcc compatibility we have to produce (dummy) addresses for some
+        * builtins */
+       if (warning.other) {
+               warningf(&expression->base.source_position,
+                                "taking address of builtin '%Y'", expression->symbol);
+       }
+
+       /* simply create a NULL pointer */
+       ir_mode  *mode = get_ir_mode(type_void_ptr);
+       ir_node  *res  = new_Const_long(mode, 0);
+
+       return res;
+}
+
 /**
  * creates firm nodes for an expression. The difference between this function
  * and expression_to_firm is, that this version might produce mode_b nodes
@@ -3106,7 +3163,7 @@ static ir_node *_expression_to_firm(const expression_t *expression)
        case EXPR_VA_ARG:
                return va_arg_expression_to_firm(&expression->va_arge);
        case EXPR_BUILTIN_SYMBOL:
-               panic("unimplemented expression found");
+               return builtin_symbol_to_firm(&expression->builtin_symbol);
        case EXPR_BUILTIN_CONSTANT_P:
                return builtin_constant_to_firm(&expression->builtin_constant);
        case EXPR_BUILTIN_PREFETCH:
@@ -3125,6 +3182,20 @@ static ir_node *_expression_to_firm(const expression_t *expression)
        panic("invalid expression found");
 }
 
+static bool is_builtin_expect(const expression_t *expression)
+{
+       if (expression->kind != EXPR_CALL)
+               return false;
+
+       expression_t *function = expression->call.function;
+       if (function->kind != EXPR_BUILTIN_SYMBOL)
+               return false;
+       if (function->builtin_symbol.symbol->ID != T___builtin_expect)
+               return false;
+
+       return true;
+}
+
 static bool produces_mode_b(const expression_t *expression)
 {
        switch (expression->kind) {
@@ -3143,8 +3214,12 @@ static bool produces_mode_b(const expression_t *expression)
        case EXPR_UNARY_NOT:
                return true;
 
-       case EXPR_BINARY_BUILTIN_EXPECT:
-               return produces_mode_b(expression->binary.left);
+       case EXPR_CALL:
+               if (is_builtin_expect(expression)) {
+                       expression_t *argument = expression->call.arguments->expression;
+                       return produces_mode_b(argument);
+               }
+               return false;
        case EXPR_BINARY_COMMA:
                return produces_mode_b(expression->binary.right);
 
@@ -3174,7 +3249,8 @@ static ir_node *expression_to_firm(const expression_t *expression)
 
        /* we have to produce a 0/1 from the mode_b expression */
        dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
-       return produce_condition_result(expression, dbgi);
+       ir_mode  *mode = get_ir_mode(expression->base.type);
+       return produce_condition_result(expression, mode, dbgi);
 }
 
 /**
@@ -3195,9 +3271,7 @@ static ir_node *create_condition_evaluation(const expression_t *expression,
        case EXPR_BINARY_LOGICAL_AND: {
                const binary_expression_t *binary_expression = &expression->binary;
 
-               ir_node *cur_block   = get_cur_block();
                ir_node *extra_block = new_immBlock();
-               set_cur_block(cur_block);
                create_condition_evaluation(binary_expression->left, extra_block,
                                            false_block);
                mature_immBlock(extra_block);
@@ -3209,9 +3283,7 @@ static ir_node *create_condition_evaluation(const expression_t *expression,
        case EXPR_BINARY_LOGICAL_OR: {
                const binary_expression_t *binary_expression = &expression->binary;
 
-               ir_node *cur_block   = get_cur_block();
                ir_node *extra_block = new_immBlock();
-               set_cur_block(cur_block);
                create_condition_evaluation(binary_expression->left, true_block,
                                            extra_block);
                mature_immBlock(extra_block);
@@ -3232,22 +3304,23 @@ static ir_node *create_condition_evaluation(const expression_t *expression,
        ir_node  *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
 
        /* set branch prediction info based on __builtin_expect */
-       if (expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
-               long               cnst = fold_constant(expression->binary.right);
-               cond_jmp_predicate pred;
-
-               if (cnst == 0) {
-                       pred = COND_JMP_PRED_FALSE;
-               } else {
-                       pred = COND_JMP_PRED_TRUE;
+       if (is_builtin_expect(expression)) {
+               call_argument_t *argument = expression->call.arguments->next;
+               if (is_constant_expression(argument->expression)) {
+                       long               cnst = fold_constant(argument->expression);
+                       cond_jmp_predicate pred;
+
+                       if (cnst == 0) {
+                               pred = COND_JMP_PRED_FALSE;
+                       } else {
+                               pred = COND_JMP_PRED_TRUE;
+                       }
+                       set_Cond_jmp_pred(cond, pred);
                }
-               set_Cond_jmp_pred(cond, pred);
        }
 
        add_immBlock_pred(true_block, true_proj);
-       if (false_block != NULL) {
-               add_immBlock_pred(false_block, false_proj);
-       }
+       add_immBlock_pred(false_block, false_proj);
 
        set_cur_block(NULL);
        return cond_expr;
@@ -3270,7 +3343,7 @@ static void create_variable_entity(entity_t *variable,
        variable->declaration.kind  = (unsigned char) declaration_kind;
        variable->variable.v.entity = irentity;
        set_entity_variability(irentity, variability_uninitialized);
-       set_entity_ld_ident(irentity, create_ld_ident(irentity, variable));
+       set_entity_ld_ident(irentity, create_ld_ident(variable));
        if (parent_type == get_tls_type())
                set_entity_allocation(irentity, allocation_automatic);
        else if (declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
@@ -3308,7 +3381,7 @@ static __attribute__((unused)) void debug_print_type_path(const type_path_t *pat
                if (is_type_compound(type)) {
                        fprintf(stderr, ".%s", entry->compound_entry->base.symbol->string);
                } else if (is_type_array(type)) {
-                       fprintf(stderr, "[%zu]", entry->index);
+                       fprintf(stderr, "[%u]", (unsigned) entry->index);
                } else {
                        fprintf(stderr, "-INVALID-");
                }
@@ -3484,7 +3557,6 @@ static void advance_current_object(type_path_t *path)
        if (path->invalid) {
                /* TODO: handle this... */
                panic("invalid initializer in ast2firm (excessive elements)");
-               return;
        }
 
        type_path_entry_t *top = get_type_path_top(path);
@@ -3538,6 +3610,21 @@ static ir_initializer_t *create_ir_initializer_value(
        return create_initializer_const(value);
 }
 
+/** test wether type can be initialized by a string constant */
+static bool is_string_type(type_t *type)
+{
+       type_t *inner;
+       if (is_type_pointer(type)) {
+               inner = skip_typeref(type->pointer.points_to);
+       } else if(is_type_array(type)) {
+               inner = skip_typeref(type->array.element_type);
+       } else {
+               return false;
+       }
+
+       return is_type_integer(inner);
+}
+
 static ir_initializer_t *create_ir_initializer_list(
                const initializer_list_t *initializer, type_t *type)
 {
@@ -3567,6 +3654,18 @@ static ir_initializer_t *create_ir_initializer_list(
                                        break;
                                descend_into_subtype(&path);
                        }
+               } else if (sub_initializer->kind == INITIALIZER_STRING
+                               || sub_initializer->kind == INITIALIZER_WIDE_STRING) {
+                       /* we might have to descend into types until we're at a scalar
+                        * type */
+                       while (true) {
+                               type_t *orig_top_type = path.top_type;
+                               type_t *top_type      = skip_typeref(orig_top_type);
+
+                               if (is_string_type(top_type))
+                                       break;
+                               descend_into_subtype(&path);
+                       }
                }
 
                ir_initializer_t *sub_irinitializer
@@ -3672,7 +3771,7 @@ static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
        if (is_atomic_type(type)) {
                ir_mode *mode = get_type_mode(type);
                tarval  *zero = get_mode_null(mode);
-               ir_node *cnst = new_d_Const(dbgi, mode, zero);
+               ir_node *cnst = new_d_Const(dbgi, zero);
 
                /* TODO: bitfields */
                ir_node *mem    = get_store();
@@ -3696,7 +3795,7 @@ static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
                        if (is_Array_type(type)) {
                                ir_entity *entity   = get_array_element_entity(type);
                                tarval    *index_tv = new_tarval_from_long(i, mode_uint);
-                               ir_node   *cnst     = new_d_Const(dbgi, mode_uint, index_tv);
+                               ir_node   *cnst     = new_d_Const(dbgi, index_tv);
                                ir_node   *in[1]    = { cnst };
                                irtype = get_array_element_type(type);
                                addr   = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
@@ -3742,7 +3841,7 @@ static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
        case IR_INITIALIZER_TARVAL: {
                tarval  *tv       = get_initializer_tarval_value(initializer);
                ir_mode *mode     = get_tarval_mode(tv);
-               ir_node *cnst     = new_d_Const(dbgi, mode, tv);
+               ir_node *cnst     = new_d_Const(dbgi, tv);
                ir_type *ent_type = get_entity_type(entity);
 
                /* is it a bitfield type? */
@@ -3779,7 +3878,7 @@ static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
                        ir_entity *sub_entity;
                        if (is_Array_type(type)) {
                                tarval    *index_tv = new_tarval_from_long(i, mode_uint);
-                               ir_node   *cnst     = new_d_Const(dbgi, mode_uint, index_tv);
+                               ir_node   *cnst     = new_d_Const(dbgi, index_tv);
                                ir_node   *in[1]    = { cnst };
                                irtype     = get_array_element_type(type);
                                sub_entity = get_array_element_entity(type);
@@ -4000,7 +4099,7 @@ static void create_local_variable(entity_t *entity)
                entity->declaration.kind        = DECLARATION_KIND_LOCAL_VARIABLE;
                entity->variable.v.value_number = next_value_number_function;
                set_irg_loc_description(current_ir_graph, next_value_number_function,
-                                       (variable_t*) &entity->variable);
+                                       entity);
                ++next_value_number_function;
        }
 }
@@ -4010,17 +4109,18 @@ static void create_local_static_variable(entity_t *entity)
        assert(entity->kind == ENTITY_VARIABLE);
        assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
 
-       type_t    *const type        = skip_typeref(entity->declaration.type);
-       ir_type   *const global_type = get_glob_type();
-       ir_type   *const irtype      = get_ir_type(type);
-       dbg_info  *const dbgi        = get_dbg_info(&entity->base.source_position);
+       type_t    *const type     = skip_typeref(entity->declaration.type);
+       ir_type   *const var_type = entity->variable.thread_local ?
+               get_tls_type() : get_glob_type();
+       ir_type   *const irtype   = get_ir_type(type);
+       dbg_info  *const dbgi     = get_dbg_info(&entity->base.source_position);
 
        size_t l = strlen(entity->base.symbol->string);
        char   buf[l + sizeof(".%u")];
        snprintf(buf, sizeof(buf), "%s.%%u", entity->base.symbol->string);
        ident     *const id = id_unique(buf);
 
-       ir_entity *const irentity = new_d_entity(global_type, id, irtype, dbgi);
+       ir_entity *const irentity = new_d_entity(var_type, id, irtype, dbgi);
 
        if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
                set_entity_volatility(irentity, volatility_is_volatile);
@@ -4028,10 +4128,11 @@ static void create_local_static_variable(entity_t *entity)
 
        entity->declaration.kind  = DECLARATION_KIND_GLOBAL_VARIABLE;
        entity->variable.v.entity = irentity;
-       set_entity_ld_ident(irentity, create_ld_ident(irentity, entity));
+       set_entity_ld_ident(irentity, id);
        set_entity_variability(irentity, variability_uninitialized);
        set_entity_visibility(irentity, visibility_local);
-       set_entity_allocation(irentity, allocation_static);
+       set_entity_allocation(irentity, entity->variable.thread_local ?
+               allocation_automatic : allocation_static);
 
        ir_graph *const old_current_ir_graph = current_ir_graph;
        current_ir_graph = get_const_code_irg();
@@ -4127,63 +4228,20 @@ static void create_global_variable(entity_t *entity)
 {
        assert(entity->kind == ENTITY_VARIABLE);
 
-       ir_visibility  vis;
-       ir_type       *var_type;
-       switch ((storage_class_tag_t) entity->declaration.storage_class) {
-               case STORAGE_CLASS_STATIC:
-                       vis = visibility_local;
-                       goto global_var;
-
-               case STORAGE_CLASS_EXTERN:
-                       vis = visibility_external_allocated;
-                       goto global_var;
-
-               case STORAGE_CLASS_NONE:
-                       vis = visibility_external_visible;
-                       goto global_var;
-
-               case STORAGE_CLASS_THREAD:
-                       vis = visibility_external_visible;
-                       goto tls_var;
-
-               case STORAGE_CLASS_THREAD_EXTERN:
-                       vis = visibility_external_allocated;
-                       goto tls_var;
-
-               case STORAGE_CLASS_THREAD_STATIC:
-                       vis = visibility_local;
-                       goto tls_var;
-
-tls_var:
-                       var_type = get_tls_type();
-                       goto create_var;
-
-global_var:
-                       var_type = get_glob_type();
-                       goto create_var;
-
-create_var:
-                       create_variable_entity(entity,
-                                              DECLARATION_KIND_GLOBAL_VARIABLE,
-                                              var_type);
-                       /* Matze: I'm confused, shouldn't we only be here when creating
-                        * variables? */
-#if 0
-                       if (!is_type_function(skip_typeref(entity->declaration.type))) {
-                               set_entity_visibility(declaration->v.entity, vis);
-                       }
-#else
-                       set_entity_visibility(entity->variable.v.entity, vis);
-#endif
-
-                       return;
+       ir_visibility vis;
+       switch ((storage_class_tag_t)entity->declaration.storage_class) {
+               case STORAGE_CLASS_STATIC: vis = visibility_local;              break;
+               case STORAGE_CLASS_EXTERN: vis = visibility_external_allocated; break;
+               case STORAGE_CLASS_NONE:   vis = visibility_external_visible;   break;
 
-               case STORAGE_CLASS_TYPEDEF:
-               case STORAGE_CLASS_AUTO:
-               case STORAGE_CLASS_REGISTER:
-                       break;
+               default: panic("Invalid storage class for global variable");
        }
-       panic("Invalid storage class for global variable");
+
+       ir_type *var_type = entity->variable.thread_local ?
+               get_tls_type() : get_glob_type();
+       create_variable_entity(entity,
+                       DECLARATION_KIND_GLOBAL_VARIABLE, var_type);
+       set_entity_visibility(entity->variable.v.entity, vis);
 }
 
 static void create_local_declaration(entity_t *entity)
@@ -4225,9 +4283,6 @@ static void create_local_declaration(entity_t *entity)
                }
                return;
        case STORAGE_CLASS_TYPEDEF:
-       case STORAGE_CLASS_THREAD:
-       case STORAGE_CLASS_THREAD_EXTERN:
-       case STORAGE_CLASS_THREAD_STATIC:
                break;
        }
        panic("invalid storage class found");
@@ -4254,6 +4309,10 @@ static void initialize_local_declaration(entity_t *entity)
        case DECLARATION_KIND_INNER_FUNCTION:
                return;
 
+       case DECLARATION_KIND_PARAMETER:
+       case DECLARATION_KIND_PARAMETER_ENTITY:
+               panic("can't initialize parameters");
+
        case DECLARATION_KIND_UNKNOWN:
                panic("can't initialize unknown declaration");
        }
@@ -4262,12 +4321,20 @@ static void initialize_local_declaration(entity_t *entity)
 
 static void declaration_statement_to_firm(declaration_statement_t *statement)
 {
-       entity_t *entity = statement->declarations_begin;
-       entity_t *end    = statement->declarations_end->base.next;
-       for ( ; entity != end; entity = entity->base.next) {
-               if (!is_declaration(entity))
-                       continue;
-               initialize_local_declaration(entity);
+       entity_t *      entity = statement->declarations_begin;
+       entity_t *const last   = statement->declarations_end;
+       if (entity != NULL) {
+               for ( ;; entity = entity->base.next) {
+                       if (is_declaration(entity)) {
+                               initialize_local_declaration(entity);
+                       } else if (entity->kind == ENTITY_TYPEDEF) {
+                               type_t *const type = entity->typedefe.type;
+                               if (is_type_array(type) && type->array.is_vla)
+                                       get_vla_size(&type->array);
+                       }
+                       if (entity == last)
+                               break;
+               }
        }
 }
 
@@ -4281,6 +4348,7 @@ static void if_statement_to_firm(if_statement_t *statement)
        ir_node *true_block = NULL;
        if (statement->true_statement != NULL) {
                true_block = new_immBlock();
+               set_cur_block(true_block);
                statement_to_firm(statement->true_statement);
                if (get_cur_block() != NULL) {
                        ir_node *jmp = new_Jmp();
@@ -4294,6 +4362,7 @@ static void if_statement_to_firm(if_statement_t *statement)
        ir_node *false_block = NULL;
        if (statement->false_statement != NULL) {
                false_block = new_immBlock();
+               set_cur_block(false_block);
 
                statement_to_firm(statement->false_statement);
                if (get_cur_block() != NULL) {
@@ -4351,6 +4420,7 @@ static void while_statement_to_firm(while_statement_t *statement)
        break_label                 = NULL;
 
        ir_node *body_block = new_immBlock();
+       set_cur_block(body_block);
        statement_to_firm(statement->body);
        ir_node *false_block = break_label;
 
@@ -4415,6 +4485,7 @@ static void do_while_statement_to_firm(do_while_statement_t *statement)
        continue_label              = header_block;
        break_label                 = NULL;
 
+       set_cur_block(body_block);
        statement_to_firm(statement->body);
        ir_node *false_block = break_label;
 
@@ -4438,9 +4509,7 @@ static void do_while_statement_to_firm(do_while_statement_t *statement)
        create_condition_evaluation(statement->condition, body_block, false_block);
        mature_immBlock(body_block);
        mature_immBlock(header_block);
-       if (false_block != NULL) {
-               mature_immBlock(false_block);
-       }
+       mature_immBlock(false_block);
 
        set_cur_block(false_block);
 }
@@ -4477,6 +4546,7 @@ static void for_statement_to_firm(for_statement_t *statement)
 
        /* create the step block */
        ir_node *const step_block = new_immBlock();
+       set_cur_block(step_block);
        if (statement->step != NULL) {
                expression_to_firm(statement->step);
        }
@@ -4484,6 +4554,7 @@ static void for_statement_to_firm(for_statement_t *statement)
 
        /* create the header block */
        ir_node *const header_block = new_immBlock();
+       set_cur_block(header_block);
        if (jmp != NULL) {
                add_immBlock_pred(header_block, jmp);
        }
@@ -4493,7 +4564,7 @@ static void for_statement_to_firm(for_statement_t *statement)
        ir_node *const false_block = new_immBlock();
 
        /* the loop body */
-       ir_node * body_block;
+       ir_node *body_block;
        if (statement->body != NULL) {
                ir_node *const old_continue_label = continue_label;
                ir_node *const old_break_label    = break_label;
@@ -4501,6 +4572,7 @@ static void for_statement_to_firm(for_statement_t *statement)
                break_label    = false_block;
 
                body_block = new_immBlock();
+               set_cur_block(body_block);
                statement_to_firm(statement->body);
 
                assert(continue_label == step_block);
@@ -4553,9 +4625,7 @@ static void create_jump_statement(const statement_t *statement,
 static ir_node *get_break_label(void)
 {
        if (break_label == NULL) {
-               ir_node *cur_block = get_cur_block();
                break_label = new_immBlock();
-               set_cur_block(cur_block);
        }
        return break_label;
 }
@@ -4669,10 +4739,9 @@ static void case_label_to_firm(const case_label_statement_t *statement)
        ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
 
        ir_node *proj;
-       ir_node *old_block = get_nodes_block(current_switch_cond);
        ir_node *block     = new_immBlock();
 
-       set_cur_block(old_block);
+       set_cur_block(get_nodes_block(current_switch_cond));
        if (statement->expression != NULL) {
                long pn     = statement->first_case;
                long end_pn = statement->last_case;
@@ -4800,9 +4869,8 @@ static void asm_statement_to_firm(const asm_statement_t *statement)
                        = be_parse_asm_constraints(constraints);
 
                if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
-                       errorf(&statement->base.source_position,
+                       warningf(&statement->base.source_position,
                               "some constraints in '%s' are not supported", constraints);
-                       continue;
                }
                if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
                        errorf(&statement->base.source_position,
@@ -5005,7 +5073,6 @@ static void statement_to_firm(statement_t *statement)
        switch (statement->kind) {
        case STATEMENT_INVALID:
                panic("invalid statement found");
-               return;
        case STATEMENT_EMPTY:
                /* nothing */
                return;
@@ -5048,10 +5115,6 @@ static void statement_to_firm(statement_t *statement)
        case STATEMENT_LABEL:
                label_to_firm(&statement->label);
                return;
-       case STATEMENT_LOCAL_LABEL:
-               /* local labels transform the semantics of labels while parsing
-                * they don't need any special treatment here */
-               return;
        case STATEMENT_GOTO:
                goto_to_firm(&statement->gotos);
                return;
@@ -5065,20 +5128,32 @@ static void statement_to_firm(statement_t *statement)
                leave_statement_to_firm(&statement->leave);
                return;
        }
-       panic("Statement not implemented\n");
+       panic("statement not implemented");
 }
 
 static int count_local_variables(const entity_t *entity,
-                                 const entity_t *const end)
+                                 const entity_t *const last)
 {
        int count = 0;
-       for (; entity != end; entity = entity->base.next) {
-               if (entity->kind != ENTITY_VARIABLE)
+       for (; entity != NULL; entity = entity->base.next) {
+               type_t *type;
+               bool    address_taken;
+
+               if (entity->kind == ENTITY_VARIABLE) {
+                       type          = skip_typeref(entity->declaration.type);
+                       address_taken = entity->variable.address_taken;
+               } else if (entity->kind == ENTITY_PARAMETER) {
+                       type          = skip_typeref(entity->declaration.type);
+                       address_taken = entity->parameter.address_taken;
+               } else {
                        continue;
-               type_t *type = skip_typeref(entity->declaration.type);
+               }
 
-               if (!entity->variable.address_taken && is_type_scalar(type))
+               if (!address_taken && is_type_scalar(type))
                        ++count;
+
+               if (entity == last)
+                       break;
        }
        return count;
 }
@@ -5091,7 +5166,7 @@ static void count_local_variables_in_stmt(statement_t *stmt, void *const env)
        case STATEMENT_DECLARATION: {
                const declaration_statement_t *const decl_stmt = &stmt->declaration;
                *count += count_local_variables(decl_stmt->declarations_begin,
-                                                                               decl_stmt->declarations_end->base.next);
+                               decl_stmt->declarations_end);
                break;
        }
 
@@ -5128,11 +5203,11 @@ static void initialize_function_parameters(entity_t *entity)
        int       n         = 0;
        entity_t *parameter = entity->function.parameters.entities;
        for ( ; parameter != NULL; parameter = parameter->base.next, ++n) {
-               assert(parameter->kind == ENTITY_VARIABLE);
+               assert(parameter->kind == ENTITY_PARAMETER);
                assert(parameter->declaration.kind == DECLARATION_KIND_UNKNOWN);
                type_t *type = skip_typeref(parameter->declaration.type);
 
-               bool needs_entity = parameter->variable.address_taken;
+               bool needs_entity = parameter->parameter.address_taken;
                assert(!is_type_array(type));
                if (is_type_compound(type)) {
                        needs_entity = true;
@@ -5144,8 +5219,8 @@ static void initialize_function_parameters(entity_t *entity)
                        set_entity_ident(entity, id);
 
                        parameter->declaration.kind
-                               = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
-                       parameter->variable.v.entity = entity;
+                               = DECLARATION_KIND_PARAMETER_ENTITY;
+                       parameter->parameter.v.entity = entity;
                        continue;
                }
 
@@ -5159,13 +5234,13 @@ static void initialize_function_parameters(entity_t *entity)
                value = create_conv(NULL, value, mode);
                value = do_strict_conv(NULL, value);
 
-               parameter->declaration.kind        = DECLARATION_KIND_LOCAL_VARIABLE;
-               parameter->variable.v.value_number = next_value_number_function;
+               parameter->declaration.kind         = DECLARATION_KIND_PARAMETER;
+               parameter->parameter.v.value_number = next_value_number_function;
                set_irg_loc_description(current_ir_graph, next_value_number_function,
-                                       (variable_t*) &parameter->variable);
+                                       parameter);
                ++next_value_number_function;
 
-               set_value(parameter->variable.v.value_number, value);
+               set_value(parameter->parameter.v.value_number, value);
        }
 }
 
@@ -5177,6 +5252,10 @@ static void initialize_function_parameters(entity_t *entity)
  */
 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
 {
+       if (decl_modifiers & DM_RETURNS_TWICE) {
+               /* TRUE if the declaration includes __attribute__((returns_twice)) */
+               set_irg_additional_property(irg, mtp_property_returns_twice);
+       }
        if (decl_modifiers & DM_NORETURN) {
                /* TRUE if the declaration includes the Microsoft
                   __declspec(noreturn) specifier. */
@@ -5305,8 +5384,8 @@ static void create_function(entity_t *entity)
 
                        ir_node *in[1];
                        /* ยง5.1.2.2.3 main implicitly returns 0 */
-                       if (strcmp(entity->base.symbol->string, "main") == 0) {
-                               in[0] = new_Const(mode, get_mode_null(mode));
+                       if (is_main(entity)) {
+                               in[0] = new_Const(get_mode_null(mode));
                        } else {
                                in[0] = new_Unknown(mode);
                        }
@@ -5416,19 +5495,19 @@ void init_ast2firm(void)
        obstack_init(&asm_obst);
        init_atomic_modes();
 
-       id_underscore = new_id_from_chars("_", 1);
-       id_imp        = new_id_from_chars("__imp_", 6);
-
        /* OS option must be set to the backend */
        switch (firm_opt.os_support) {
        case OS_SUPPORT_MINGW:
-               create_ld_ident = create_ld_ident_win32;
+               create_ld_ident = create_name_win32;
+               break;
        case OS_SUPPORT_LINUX:
-               create_ld_ident = create_ld_ident_linux_elf;
+               create_ld_ident = create_name_linux_elf;
                break;
        case OS_SUPPORT_MACHO:
-               create_ld_ident = create_ld_ident_macho;
+               create_ld_ident = create_name_macho;
                break;
+       default:
+               panic("unexpected OS support mode");
        }
 
        /* create idents for all known runtime functions */
@@ -5478,9 +5557,10 @@ static void global_asm_to_firm(statement_t *s)
 void translation_unit_to_firm(translation_unit_t *unit)
 {
        /* just to be sure */
-       continue_label      = NULL;
-       break_label         = NULL;
-       current_switch_cond = NULL;
+       continue_label           = NULL;
+       break_label              = NULL;
+       current_switch_cond      = NULL;
+       current_translation_unit = unit;
 
        init_ir_types();
        inner_functions = NEW_ARR_F(entity_t *, 0);
@@ -5491,5 +5571,6 @@ void translation_unit_to_firm(translation_unit_t *unit)
        DEL_ARR_F(inner_functions);
        inner_functions  = NULL;
 
-       current_ir_graph = NULL;
+       current_ir_graph         = NULL;
+       current_translation_unit = NULL;
 }