- fixed r22803
[libfirm] / ir / be / begnuas.c
index 7016855..0679d60 100644 (file)
@@ -24,9 +24,7 @@
  * @date        04.11.2005
  * @version     $Id$
  */
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
 #include "begnuas.h"
 
@@ -69,6 +67,7 @@ static const char *get_section_name(be_gas_section_t section) {
                        ".section\t.bss",
                        ".section\t.tbss,\"awT\",@nobits",
                        ".section\t.ctors,\"aw\",@progbits",
+                       ".section\t.dtors,\"aw\",@progbits",
                        NULL, /* no cstring section */
                        NULL,
                        NULL
@@ -80,6 +79,7 @@ static const char *get_section_name(be_gas_section_t section) {
                        ".section\t.bss",
                        ".section\t.tbss,\"awT\",@nobits",
                        ".section\t.ctors,\"aw\",@progbits",
+                       ".section\t.dtors,\"aw\",@progbits",
                        NULL,
                        NULL,
                        NULL
@@ -91,6 +91,7 @@ static const char *get_section_name(be_gas_section_t section) {
                        ".section\t.bss",
                        ".section\t.tbss,\"awT\",@nobits",
                        ".section\t.ctors,\"aw\",@progbits",
+                       ".section\t.dtors,\"aw\",@progbits",
                        NULL,
                        NULL,
                        NULL
@@ -102,6 +103,7 @@ static const char *get_section_name(be_gas_section_t section) {
                        ".data",
                        NULL,             /* TLS is not supported on Mach-O */
                        ".mod_init_func",
+                       NULL,             /* TODO: how is this called? */
                        ".cstring",
                        ".section\t__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5",
                        ".section\t__IMPORT,__pointers,non_lazy_symbol_pointers"
@@ -140,15 +142,16 @@ void be_gas_emit_function_prolog(ir_entity *entity, unsigned alignment)
        be_emit_write_line();
 
        /* gcc fills space between function with 0x90, no idea if this is needed */
-       if(be_gas_flavour == GAS_FLAVOUR_MACH_O) {
+       if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
                fill_byte = "0x90";
        }
 
-       maximum_skip = (1 << alignment) - 1;
-       be_emit_cstring("\t.p2align ");
-       be_emit_irprintf("%u,%s,%u\n", alignment, fill_byte, maximum_skip);
-       be_emit_write_line();
-
+       if (alignment > 0) {
+               maximum_skip = (1 << alignment) - 1;
+               be_emit_cstring("\t.p2align ");
+               be_emit_irprintf("%u,%s,%u\n", alignment, fill_byte, maximum_skip);
+               be_emit_write_line();
+       }
        if (get_entity_visibility(entity) == visibility_external_visible) {
                be_emit_cstring(".globl ");
                be_emit_string(name);
@@ -166,7 +169,11 @@ void be_gas_emit_function_prolog(ir_entity *entity, unsigned alignment)
        case GAS_FLAVOUR_MINGW:
                be_emit_cstring("\t.def\t");
                be_emit_string(name);
-               be_emit_cstring(";\t.scl\t2;\t.type\t32;\t.endef\n");
+               if (get_entity_visibility(entity) == visibility_external_visible) {
+                       be_emit_cstring(";\t.scl\t2;\t.type\t32;\t.endef\n");
+               } else {
+                       be_emit_cstring(";\t.scl\t3;\t.type\t32;\t.endef\n");
+               }
                be_emit_write_line();
                break;
        case GAS_FLAVOUR_MACH_O:
@@ -222,59 +229,76 @@ static void dump_arith_tarval(tarval *tv, int bytes)
        switch (bytes) {
        case 1:
                be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
-               break;
+               return;
 
        case 2:
                be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
-               break;
+               return;
 
        case 4:
                be_emit_irprintf("0x%02x%02x%02x%02x",
                        get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
-               break;
+               return;
 
        case 8:
                be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
                        get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
                        get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
-               break;
+               return;
 
-       case 10:
        case 12:
-               break;
+               /* Beware: Mixed endian output!  One little endian number emitted as
+                * three longs.  Each long initializer is written in big endian. */
+               be_emit_irprintf(
+                       "\t.long\t0x%02x%02x%02x%02x\n"
+                       "\t.long\t0x%02x%02x%02x%02x\n"
+                       "\t.long\t0x%02x%02x%02x%02x",
+                       get_tarval_sub_bits(tv,  3), get_tarval_sub_bits(tv,  2),
+                       get_tarval_sub_bits(tv,  1), get_tarval_sub_bits(tv,  0),
+                       get_tarval_sub_bits(tv,  7), get_tarval_sub_bits(tv,  6),
+                       get_tarval_sub_bits(tv,  5), get_tarval_sub_bits(tv,  4),
+                       get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
+                       get_tarval_sub_bits(tv,  9), get_tarval_sub_bits(tv,  8)
+               );
+               return;
 
        case 16:
-               be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x"
-                                              "%02x%02x%02x%02x%02x%02x%02x%02x",
+               be_emit_irprintf(
+                       "\t.long\t0x%02x%02x%02x%02x0x%02x%02x%02x%02x0x%02x%02x%02x%02x0x%02x%02x%02x%02x",
                        get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
                        get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
                        get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
-                       get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
-                       get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
-                       get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
-                       get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
-                       get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
-               break;
-
-
-       default:
-               fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
-               assert(0);
+                       get_tarval_sub_bits(tv,  9), get_tarval_sub_bits(tv,  8),
+                       get_tarval_sub_bits(tv,  7), get_tarval_sub_bits(tv,  6),
+                       get_tarval_sub_bits(tv,  5), get_tarval_sub_bits(tv,  4),
+                       get_tarval_sub_bits(tv,  3), get_tarval_sub_bits(tv,  2),
+                       get_tarval_sub_bits(tv,  1), get_tarval_sub_bits(tv,  0)
+               );
+               return;
        }
+
+       panic("Can't dump a tarval with %d bytes", bytes);
 }
 
 /**
  * Return the label prefix for labeled blocks.
  */
-const char *be_gas_label_prefix(void) {
+const char *be_gas_block_label_prefix(void) {
        return ".LG";
 }
 
+/**
+ * Return the label prefix for labeled instructions.
+ */
+const char *be_gas_insn_label_prefix(void) {
+       return ".LE";
+}
+
 /**
  * Dump a label.
  */
 static void dump_label(ir_label_t label) {
-       be_emit_irprintf("%s%ld", be_gas_label_prefix(), label);
+       be_emit_irprintf("%s%lu", be_gas_block_label_prefix(), label);
 }
 
 /**
@@ -410,26 +434,35 @@ static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
                }
                return;
 
-               case iro_Add:
-                       do_dump_atomic_init(env, get_Add_left(init));
-                       be_emit_cstring(" + ");
-                       do_dump_atomic_init(env, get_Add_right(init));
-                       return;
+       case iro_Add:
+               if (!mode_is_int(mode) && !mode_is_reference(mode)) {
+                       panic("Constant must be int or pointer for '+' to work");
+               }
+               do_dump_atomic_init(env, get_Add_left(init));
+               be_emit_cstring(" + ");
+               do_dump_atomic_init(env, get_Add_right(init));
+               return;
 
-               case iro_Sub:
-                       do_dump_atomic_init(env, get_Sub_left(init));
-                       be_emit_cstring(" - ");
-                       do_dump_atomic_init(env, get_Sub_right(init));
-                       return;
+       case iro_Sub:
+               if (!mode_is_int(mode) && !mode_is_reference(mode)) {
+                       panic("Constant must be int or pointer for '-' to work");
+               }
+               do_dump_atomic_init(env, get_Sub_left(init));
+               be_emit_cstring(" - ");
+               do_dump_atomic_init(env, get_Sub_right(init));
+               return;
 
-               case iro_Mul:
-                       do_dump_atomic_init(env, get_Mul_left(init));
-                       be_emit_cstring(" * ");
-                       do_dump_atomic_init(env, get_Mul_right(init));
-                       return;
+       case iro_Mul:
+               if (!mode_is_int(mode) && !mode_is_reference(mode)) {
+                       panic("Constant must be int or pointer for '*' to work");
+               }
+               do_dump_atomic_init(env, get_Mul_left(init));
+               be_emit_cstring(" * ");
+               do_dump_atomic_init(env, get_Mul_right(init));
+               return;
 
-               default:
-                       assert(0 && "dump_atomic_init(): unknown IR-node");
+       default:
+               panic("dump_atomic_init(): unsupported IR-node %+F", init);
        }
 }
 
@@ -466,7 +499,7 @@ static void dump_size_type(size_t size) {
                break;
 
        default:
-               panic("Try to dump a type with %u bytes\n", (unsigned) size);
+               panic("Try to dump a type with %u bytes", (unsigned)size);
        }
 }
 
@@ -599,19 +632,21 @@ static int ent_is_string_const(ir_entity *ent)
 static void dump_string_cst(ir_entity *ent)
 {
        int      i, len;
+       int      output_len;
        ir_type *type;
        int      type_size;
        int      remaining_space;
 
-       len = get_compound_ent_n_values(ent);
+       len        = get_compound_ent_n_values(ent);
+       output_len = len;
        if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
                be_emit_cstring("\t.ascii \"");
        } else {
                be_emit_cstring("\t.string \"");
-               len -= 1;
+               output_len -= 1;
        }
 
-       for (i = 0; i < len; ++i) {
+       for (i = 0; i < output_len; ++i) {
                ir_node *irn;
                int c;
 
@@ -640,7 +675,7 @@ static void dump_string_cst(ir_entity *ent)
        remaining_space = type_size - len;
        assert(remaining_space >= 0);
        if(remaining_space > 0) {
-               be_emit_irprintf("\t.skip\t%d\n", remaining_space);
+               be_emit_irprintf("\t.space\t%d\n", remaining_space);
        }
 }
 
@@ -777,8 +812,9 @@ static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
                panic("bitfield initializer is compound");
        }
        if (tv == NULL) {
-               panic("Couldn't get numeric value for bitfield initializer\n");
+               panic("Couldn't get numeric value for bitfield initializer");
        }
+       tv = tarval_convert_to(tv, get_type_mode(type));
 
        /* normalize offset */
        vals        += offset_bits >> 3;
@@ -916,7 +952,7 @@ static void dump_initializer(be_gas_decl_env_t *env, ir_entity *entity)
         * In the worst case, every initializer allocates one byte.
         * Moreover, initializer might be big, do not allocate on stack.
         */
-       vals = xcalloc(size, sizeof(vals[0]));
+       vals = XMALLOCNZ(normal_or_bitfield, size);
 
 #ifndef NDEBUG
        glob_vals = vals;
@@ -927,13 +963,14 @@ static void dump_initializer(be_gas_decl_env_t *env, ir_entity *entity)
 
        /* now write values sorted */
        for (k = 0; k < size; ) {
-               int space = 0, skip = 0;
+               int space     = 0;
+               int elem_size = 1;
                if (vals[k].kind == NORMAL) {
                        if(vals[k].v.value != NULL) {
                                dump_atomic_init(env, vals[k].v.value);
-                               skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
+                               elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
                        } else {
-                               space = 1;
+                               elem_size = 0;
                        }
                } else if(vals[k].kind == TARVAL) {
                        tarval *tv   = vals[k].v.tarval;
@@ -941,7 +978,7 @@ static void dump_initializer(be_gas_decl_env_t *env, ir_entity *entity)
 
                        assert(tv != NULL);
 
-                       skip = size - 1;
+                       elem_size = size;
                        dump_size_type(size);
                        dump_arith_tarval(tv, size);
                        be_emit_char('\n');
@@ -952,17 +989,15 @@ static void dump_initializer(be_gas_decl_env_t *env, ir_entity *entity)
                        be_emit_write_line();
                }
 
-               ++k;
+               k += elem_size;
                while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
                        ++space;
                        ++k;
                }
-               space -= skip;
-               assert(space >= 0);
 
                /* a gap */
                if (space > 0) {
-                       be_emit_irprintf("\t.skip\t%d\n", space);
+                       be_emit_irprintf("\t.space\t%d\n", space);
                        be_emit_write_line();
                }
        }
@@ -1006,7 +1041,7 @@ static void dump_compound_init(be_gas_decl_env_t *env, ir_entity *ent)
         * In the worst case, every initializer allocates one byte.
         * Moreover, initializer might be big, do not allocate on stack.
         */
-       vals = xcalloc(last_ofs, sizeof(vals[0]));
+       vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
 
        /* collect the values and store them at the offsets */
        for (i = 0; i < n; ++i) {
@@ -1022,7 +1057,7 @@ static void dump_compound_init(be_gas_decl_env_t *env, ir_entity *ent)
                        tarval *tv = get_atomic_init_tv(value);
                        unsigned char curr_bits, last_bits = 0;
                        if (tv == NULL) {
-                               panic("Couldn't get numeric value for bitfield initializer '%s'\n",
+                               panic("Couldn't get numeric value for bitfield initializer '%s'",
                                      get_entity_ld_name(ent));
                        }
                        /* normalize offset */
@@ -1075,7 +1110,7 @@ static void dump_compound_init(be_gas_decl_env_t *env, ir_entity *ent)
 
                /* a gap */
                if (space > 0) {
-                       be_emit_irprintf("\t.skip\t%d\n", space);
+                       be_emit_irprintf("\t.space\t%d\n", space);
                        be_emit_write_line();
                }
        }
@@ -1087,7 +1122,7 @@ static void emit_align(unsigned alignment)
        if (!is_po2(alignment))
                panic("alignment not a power of 2");
 
-       be_emit_irprintf(".p2align\t%u\n", log2_floor(alignment));
+       be_emit_irprintf("\t.p2align\t%u\n", log2_floor(alignment));
        be_emit_write_line();
 }
 
@@ -1125,6 +1160,8 @@ static void dump_global(be_gas_decl_env_t *env, ir_entity *ent)
                section = GAS_SECTION_COMMON;
                if (visibility != visibility_local)
                        emit_as_common = 1;
+       } else {
+               section = GAS_SECTION_DATA;
        }
 
        if(!emit_as_common) {
@@ -1153,6 +1190,15 @@ static void dump_global(be_gas_decl_env_t *env, ir_entity *ent)
                emit_align(align);
        }
 
+       if (visibility != visibility_external_allocated && !emit_as_common
+                       && be_gas_flavour == GAS_FLAVOUR_ELF) {
+               be_emit_cstring("\t.type\t");
+               be_emit_ident(ld_ident);
+               be_emit_cstring(", @object\n\t.size\t");
+               be_emit_ident(ld_ident);
+               be_emit_irprintf(", %u\n", get_type_size_bytes(type));
+       }
+
        if (!emit_as_common) {
                be_emit_ident(ld_ident);
                be_emit_cstring(":\n");
@@ -1175,14 +1221,21 @@ static void dump_global(be_gas_decl_env_t *env, ir_entity *ent)
                                be_emit_write_line();
                                break;
                        }
-               } else if (section == GAS_SECTION_PIC_TRAMPOLINES) {
+               } else if (section == GAS_SECTION_PIC_TRAMPOLINES
+                               || section == GAS_SECTION_PIC_SYMBOLS) {
                        if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
                                be_emit_cstring("\t.indirect_symbol ");
                                be_emit_ident(get_entity_ident(ent));
                                be_emit_char('\n');
                                be_emit_write_line();
-                               be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
-                               be_emit_write_line();
+                               if (section == GAS_SECTION_PIC_TRAMPOLINES) {
+                                       be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
+                                       be_emit_write_line();
+                               } else {
+                                       assert(section == GAS_SECTION_PIC_SYMBOLS);
+                                       be_emit_cstring("\t.long 0\n");
+                                       be_emit_write_line();
+                               }
                        } else {
                                panic("PIC trampolines not yet supported in this gas mode");
                        }
@@ -1209,7 +1262,7 @@ static void dump_global(be_gas_decl_env_t *env, ir_entity *ent)
                                dump_compound_init(env, ent);
                                break;
                        default:
-                               assert(0);
+                               panic("Unimplemented type kind in dump_global()");
                        }
                }
        }
@@ -1274,9 +1327,13 @@ void be_gas_emit_decls(const be_main_env_t *main_env,
        be_gas_dump_globals(get_glob_type(), &env, only_emit_marked_entities);
        env.section = GAS_SECTION_TLS;
        be_gas_dump_globals(get_tls_type(), &env, only_emit_marked_entities);
-       env.section = GAS_SECTION_CTOR;
-       be_gas_dump_globals(get_constructors_type(), &env,
+       env.section = GAS_SECTION_CONSTRUCTORS;
+       be_gas_dump_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env,
+                           only_emit_marked_entities);
+       env.section = GAS_SECTION_DESTRUCTORS;
+       be_gas_dump_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env,
                            only_emit_marked_entities);
+
        env.section = GAS_SECTION_PIC_SYMBOLS;
        be_gas_dump_globals(main_env->pic_symbols_type, &env,
                            only_emit_marked_entities);