- refactor emitter next/prev block handling a bit
[libfirm] / ir / be / begnuas.c
index b79ba28..42d9e2a 100644 (file)
@@ -69,7 +69,9 @@ static const char *get_section_name(be_gas_section_t section) {
                        ".section\t.bss",
                        ".section\t.tbss,\"awT\",@nobits",
                        ".section\t.ctors,\"aw\",@progbits",
+                       NULL, /* no cstring section */
                        NULL,
+                       NULL
                },
                { /* GAS_FLAVOUR_MINGW */
                        ".section\t.text",
@@ -79,6 +81,8 @@ static const char *get_section_name(be_gas_section_t section) {
                        ".section\t.tbss,\"awT\",@nobits",
                        ".section\t.ctors,\"aw\",@progbits",
                        NULL,
+                       NULL,
+                       NULL
                },
                { /* GAS_FLAVOUR_YASM */
                        ".section\t.text",
@@ -87,6 +91,8 @@ static const char *get_section_name(be_gas_section_t section) {
                        ".section\t.bss",
                        ".section\t.tbss,\"awT\",@nobits",
                        ".section\t.ctors,\"aw\",@progbits",
+                       NULL,
+                       NULL,
                        NULL
                },
                { /* GAS_FLAVOUR_MACH_O */
@@ -96,7 +102,9 @@ static const char *get_section_name(be_gas_section_t section) {
                        ".data",
                        NULL,             /* TLS is not supported on Mach-O */
                        ".mod_init_func",
-                       ".cstring"
+                       ".cstring",
+                       ".section\t__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5",
+                       ".section\t__IMPORT,__pointers,non_lazy_symbol_pointers"
                }
        };
 
@@ -132,15 +140,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);
@@ -214,26 +223,33 @@ 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;
+               be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x"
+                                  "%02x%02x%02x%02x", 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));
+               return;
 
        case 16:
                be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x"
@@ -246,27 +262,31 @@ static void dump_arith_tarval(tarval *tv, int bytes)
                        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);
+               return;
        }
+
+       panic("Can't dump a tarval with %d bytes\n", 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);
 }
 
 /**
@@ -403,18 +423,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));
@@ -919,13 +948,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;
@@ -933,7 +963,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');
@@ -944,13 +974,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) {
@@ -1099,6 +1127,10 @@ static void dump_global(be_gas_decl_env_t *env, ir_entity *ent)
        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;
+       }
+
        if (section != (be_gas_section_t) -1) {
                emit_as_common = 0;
        } else if (variability == variability_constant) {
@@ -1113,6 +1145,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) {
@@ -1136,7 +1170,8 @@ static void dump_global(be_gas_decl_env_t *env, ir_entity *ent)
                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);
        }
 
@@ -1162,6 +1197,17 @@ 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) {
+                       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.space %u\n", get_type_size_bytes(type));
                        be_emit_write_line();
@@ -1253,4 +1299,17 @@ void be_gas_emit_decls(const be_main_env_t *main_env,
        env.section = GAS_SECTION_CTOR;
        be_gas_dump_globals(get_constructors_type(), &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();
+               }
+       }
 }