Fix output for long double initializers.
[libfirm] / ir / be / begnuas.c
index 65e4e35..1079824 100644 (file)
@@ -68,7 +68,11 @@ static const char *get_section_name(be_gas_section_t section) {
                        ".section\t.rodata",
                        ".section\t.bss",
                        ".section\t.tbss,\"awT\",@nobits",
-                       ".section\t.ctors,\"aw\",@progbits"
+                       ".section\t.ctors,\"aw\",@progbits",
+                       ".section\t.dtors,\"aw\",@progbits",
+                       NULL, /* no cstring section */
+                       NULL,
+                       NULL
                },
                { /* GAS_FLAVOUR_MINGW */
                        ".section\t.text",
@@ -76,7 +80,11 @@ static const char *get_section_name(be_gas_section_t section) {
                        ".section .rdata,\"dr\"",
                        ".section\t.bss",
                        ".section\t.tbss,\"awT\",@nobits",
-                       ".section\t.ctors,\"aw\",@progbits"
+                       ".section\t.ctors,\"aw\",@progbits",
+                       ".section\t.dtors,\"aw\",@progbits",
+                       NULL,
+                       NULL,
+                       NULL
                },
                { /* GAS_FLAVOUR_YASM */
                        ".section\t.text",
@@ -84,15 +92,23 @@ static const char *get_section_name(be_gas_section_t section) {
                        ".section\t.rodata",
                        ".section\t.bss",
                        ".section\t.tbss,\"awT\",@nobits",
-                       ".section\t.ctors,\"aw\",@progbits"
+                       ".section\t.ctors,\"aw\",@progbits",
+                       ".section\t.dtors,\"aw\",@progbits",
+                       NULL,
+                       NULL,
+                       NULL
                },
                { /* GAS_FLAVOUR_MACH_O */
                        ".text",
                        ".data",
                        ".const",
                        ".data",
-                       NULL, /* TLS is not supported on Mach-O */
-                       NULL  /* constructors aren't marked with sections in Mach-O */
+                       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"
                }
        };
 
@@ -115,6 +131,8 @@ void be_gas_emit_switch_section(be_gas_section_t section) {
 void be_gas_emit_function_prolog(ir_entity *entity, unsigned alignment)
 {
        const char *name = get_entity_ld_name(entity);
+       const char *fill_byte = "";
+       unsigned maximum_skip;
 
        be_gas_emit_switch_section(GAS_SECTION_TEXT);
 
@@ -125,17 +143,17 @@ void be_gas_emit_function_prolog(ir_entity *entity, unsigned alignment)
        be_emit_char('\n');
        be_emit_write_line();
 
-       const char *fill_byte = "";
        /* 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";
        }
 
-       unsigned 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);
@@ -153,7 +171,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:
@@ -192,8 +214,8 @@ void be_gas_emit_function_epilog(ir_entity *entity)
  */
 typedef struct _be_gas_decl_env {
        const be_main_env_t *main_env; /**< The main backend environment, used for it's debug handle. */
-       int                  dump_tls;
-       waitq     *worklist;           /**< A worklist we use to place not yet handled entities on. */
+       be_gas_section_t     section;
+       waitq               *worklist;           /**< A worklist we use to place not yet handled entities on. */
 } be_gas_decl_env_t;
 
 /************************************************************************/
@@ -209,59 +231,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);
 }
 
 /**
@@ -398,18 +437,27 @@ static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *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:
+                       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:
+                       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));
@@ -453,7 +501,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);
        }
 }
 
@@ -481,6 +529,7 @@ static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
 static int initializer_is_string_const(const ir_initializer_t *initializer)
 {
        size_t i, len;
+       int found_printable = 0;
 
        if(initializer->kind != IR_INITIALIZER_COMPOUND)
                return 0;
@@ -506,16 +555,16 @@ static int initializer_is_string_const(const ir_initializer_t *initializer)
                        return 0;
 
                c = get_tarval_long(tv);
-               if (i < len - 1) {
-                       if (!isgraph(c) && !isspace(c))
-                               return 0;
-               } else {
-                       if (c != 0)
-                               return 0;
-               }
+               if (isgraph(c) || isspace(c))
+                       found_printable = 1;
+               else if(c != 0)
+                       return 0;
+
+               if (i == len - 1 && c != '\0')
+                       return 0;
        }
 
-       return 1;
+       return found_printable;
 }
 
 /**
@@ -528,6 +577,7 @@ static int ent_is_string_const(ir_entity *ent)
        ir_type *type, *element_type;
        ir_mode *mode;
        int i, c, n;
+       int found_printable = 0;
 
        type = get_entity_type(ent);
 
@@ -561,14 +611,18 @@ static int ent_is_string_const(ir_entity *ent)
 
                        c = (int) get_tarval_long(get_Const_tarval(irn));
 
-                       if((i < n - 1 && !(isgraph(c) || isspace(c)))
-                                       || (i == n - 1 && c != '\0'))
+                       if (isgraph(c) || isspace(c))
+                               found_printable = 1;
+                       else if(c != 0)
+                               return 0;
+
+                       if (i == n - 1 && c != '\0')
                                return 0;
                }
        }
 
        /* then we can emit it as a string constant */
-       return 1;
+       return found_printable;
 }
 
 /**
@@ -584,12 +638,8 @@ static void dump_string_cst(ir_entity *ent)
        int      type_size;
        int      remaining_space;
 
-       be_gas_section_t last_section = current_section;
        len = get_compound_ent_n_values(ent);
        if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
-               if (get_entity_variability(ent) == variability_constant) {
-                       be_gas_emit_switch_section(GAS_SECTION_CSTRING);
-               }
                be_emit_cstring("\t.ascii \"");
        } else {
                be_emit_cstring("\t.string \"");
@@ -627,9 +677,6 @@ static void dump_string_cst(ir_entity *ent)
        if(remaining_space > 0) {
                be_emit_irprintf("\t.skip\t%d\n", remaining_space);
        }
-       if(be_gas_flavour == GAS_FLAVOUR_MACH_O) {
-               be_gas_emit_switch_section(last_section);
-       }
 }
 
 static void dump_string_initializer(const ir_initializer_t *initializer)
@@ -638,10 +685,10 @@ static void dump_string_initializer(const ir_initializer_t *initializer)
 
        len = initializer->compound.n_initializers;
        if(be_gas_flavour == GAS_FLAVOUR_MACH_O) {
+               be_emit_cstring("\t.ascii \"");
+       } else {
                be_emit_cstring("\t.string \"");
                len -= 1;
-       } else {
-               be_emit_cstring("\t.ascii \"");
        }
 
        for(i = 0; i < len; ++i) {
@@ -765,7 +812,7 @@ 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");
        }
 
        /* normalize offset */
@@ -915,13 +962,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;
@@ -929,7 +977,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');
@@ -940,13 +988,11 @@ 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) {
@@ -1010,7 +1056,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 +1121,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();
 }
 
@@ -1084,46 +1130,37 @@ static void emit_align(unsigned alignment)
  *
  * @param env           the gas output environment
  * @param ent           the entity to be dumped
- * @param emit_commons  if non-zero, emit commons (non-local uninitialized entities)
  */
-static void dump_global(be_gas_decl_env_t *env, ir_entity *ent, int emit_commons)
+static void dump_global(be_gas_decl_env_t *env, ir_entity *ent)
 {
        ir_type          *type           = get_entity_type(ent);
        ident            *ld_ident       = get_entity_ld_ident(ent);
        unsigned          align          = get_type_alignment_bytes(type);
        int               emit_as_common = 0;
-       be_gas_section_t  section;
-       ir_variability    variability;
-       ir_visibility     visibility;
-
-       if (is_Method_type(type)) {
-               if (be_gas_flavour != GAS_FLAVOUR_MACH_O
-                               && get_method_img_section(ent) == section_constructors) {
-                       be_gas_emit_switch_section(GAS_SECTION_CTOR);
-                       emit_align(align);
-                       dump_size_type(align);
-                       be_emit_ident(ld_ident);
-                       be_emit_char('\n');
-                       be_emit_write_line();
-               }
+       be_gas_section_t  section        = env->section;
+       ir_variability    variability    = get_entity_variability(ent);
+       ir_visibility     visibility     = get_entity_visibility(ent);
 
+       if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
                return;
        }
 
-       variability = get_entity_variability(ent);
-       visibility  = get_entity_visibility(ent);
-       section     = GAS_SECTION_DATA;
-       if (env->dump_tls) {
-               section = GAS_SECTION_TLS;
+       if (section != (be_gas_section_t) -1) {
                emit_as_common = 0;
        } else if (variability == variability_constant) {
                /* a constant entity, put it on the rdata */
                section = GAS_SECTION_RODATA;
+               if (be_gas_flavour == GAS_FLAVOUR_MACH_O
+                               && ent_is_string_const(ent)) {
+                       section = GAS_SECTION_CSTRING;
+               }
        } else if (variability == variability_uninitialized) {
                /* uninitialized entity put it in bss segment */
                section = GAS_SECTION_COMMON;
-               if (emit_commons && visibility != visibility_local)
+               if (visibility != visibility_local)
                        emit_as_common = 1;
+       } else {
+               section = GAS_SECTION_DATA;
        }
 
        if(!emit_as_common) {
@@ -1147,10 +1184,20 @@ static void dump_global(be_gas_decl_env_t *env, ir_entity *ent, int emit_commons
                return;
        }
        /* alignment */
-       if (align > 1 && !emit_as_common) {
+       if (align > 1 && !emit_as_common && section != GAS_SECTION_PIC_TRAMPOLINES
+                       && section != GAS_SECTION_PIC_SYMBOLS) {
                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");
@@ -1173,8 +1220,19 @@ static void dump_global(be_gas_decl_env_t *env, ir_entity *ent, int emit_commons
                                be_emit_write_line();
                                break;
                        }
+               } else if (section == GAS_SECTION_PIC_TRAMPOLINES) {
+                       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();
+                       } else {
+                               panic("PIC trampolines not yet supported in this gas mode");
+                       }
                } else {
-                       be_emit_irprintf("\t.zero %u\n", get_type_size_bytes(type));
+                       be_emit_irprintf("\t.space %u\n", get_type_size_bytes(type));
                        be_emit_write_line();
                }
        } else {
@@ -1207,12 +1265,11 @@ static void dump_global(be_gas_decl_env_t *env, ir_entity *ent, int emit_commons
  *
  * @param gt                a global like type, either the global or the TLS one
  * @param env               an environment
- * @param emit_commons      if non-zero, emit commons (non-local uninitialized entities)
  * @param only_emit_marked  if non-zero, external allocated entities that do not have
  *                          its visited flag set are ignored
  */
 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
-                              int emit_commons, int only_emit_marked)
+                                int only_emit_marked)
 {
        int i, n = get_compound_n_members(gt);
        waitq *worklist = new_waitq();
@@ -1239,7 +1296,7 @@ static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
        while (!waitq_empty(worklist)) {
                ir_entity *ent = waitq_get(worklist);
 
-               dump_global(env, ent, emit_commons);
+               dump_global(env, ent);
        }
 
        del_waitq(worklist);
@@ -1258,10 +1315,28 @@ void be_gas_emit_decls(const be_main_env_t *main_env,
        env.main_env = main_env;
 
        /* dump global type */
-       be_gas_dump_globals(get_glob_type(), &env, 1, only_emit_marked_entities);
-
-       /* dump the Thread Local Storage */
-       env.dump_tls = 1;
-       be_gas_dump_globals(get_tls_type(), &env, 0, only_emit_marked_entities);
-       env.dump_tls = 0;
+       env.section = (be_gas_section_t) -1;
+       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_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);
+
+       if (get_compound_n_members(main_env->pic_trampolines_type) > 0) {
+               env.section = GAS_SECTION_PIC_TRAMPOLINES;
+               be_gas_dump_globals(main_env->pic_trampolines_type, &env,
+                                   only_emit_marked_entities);
+               if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
+                       be_emit_cstring("\t.subsections_via_symbols\n");
+                       be_emit_write_line();
+               }
+       }
 }