X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fbe%2Fia32%2Fia32_gen_decls.c;h=46f0e31f7706fdb15b97d11af87f3bcb0566397a;hb=a1a465eb2b3f54027b29f829423fffd0396937f4;hp=7754ff70e7f25bbe08923e286b64453b17227edc;hpb=83d772860b82f138033fbbfd2f66155950a4a42d;p=libfirm diff --git a/ir/be/ia32/ia32_gen_decls.c b/ir/be/ia32/ia32_gen_decls.c index 7754ff70e..46f0e31f7 100644 --- a/ir/be/ia32/ia32_gen_decls.c +++ b/ir/be/ia32/ia32_gen_decls.c @@ -16,9 +16,21 @@ #include "entity.h" #include "irprog.h" +#include "../be.h" + #include "ia32_emitter.h" #include "ia32_gen_decls.h" +typedef struct obstack obstack_t; + +typedef struct _ia32_decl_env { + obstack_t *rodata_obst; + obstack_t *data_obst; + obstack_t *comm_obst; + obstack_t *ctor_obst; + const be_main_env_t *main_env; +} ia32_decl_env_t; + /************************************************************************/ /* @@ -54,7 +66,7 @@ static unsigned highest_bit(unsigned v) return res; } -static void ia32_dump_comm(struct obstack *obst, const char *name, visibility vis, int size, int align) { +static void ia32_dump_comm(obstack_t *obst, const char *name, ir_visibility vis, int size, int align) { switch (asm_flavour) { case ASM_LINUX_GAS: if (vis == visibility_local) @@ -67,13 +79,15 @@ static void ia32_dump_comm(struct obstack *obst, const char *name, visibility vi else obstack_printf(obst, "\t.comm\t%s,%d\n", name, size); break; + default: + break; } } -/* - * output the alignment +/** + * output the alignment to an obstack */ -static void ia32_dump_align(struct obstack *obst, int align) +static void ia32_dump_align(obstack_t *obst, int align) { int h = highest_bit(align); @@ -85,7 +99,25 @@ static void ia32_dump_align(struct obstack *obst, int align) obstack_printf(obst, "\t.align %d\n", align); } -static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes) +/** + * output the alignment to a FILE + */ +static void ia32_dump_align_f(FILE *f, int align) +{ + int h = highest_bit(align); + + if ((1 << h) < align) + ++h; + align = (1 << h); + + if (align > 1) + fprintf(f, "\t.align %d\n", align); +} + +/** + * output a tarval + */ +static void dump_arith_tarval(obstack_t *obst, tarval *tv, int bytes) { switch (bytes) { @@ -121,7 +153,7 @@ static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes) /* * dump an atomic value */ -static void do_dump_atomic_init(struct obstack *obst, ir_node *init) +static void do_dump_atomic_init(obstack_t *obst, ir_node *init) { ir_mode *mode = get_irn_mode(init); int bytes = get_mode_size_bytes(mode); @@ -141,7 +173,7 @@ static void do_dump_atomic_init(struct obstack *obst, ir_node *init) tv = get_Const_tarval(init); /* beware of old stuff */ - assert(! mode_is_reference(mode)); + //assert(! mode_is_reference(mode)); /* it's a arithmetic value */ dump_arith_tarval(obst, tv, bytes); @@ -157,12 +189,25 @@ static void do_dump_atomic_init(struct obstack *obst, ir_node *init) obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init))); break; - case symconst_size: + case symconst_ofs_ent: + obstack_printf(obst, "%d", get_entity_offset_bytes(get_SymConst_entity(init))); + break; + + case symconst_type_size: obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init))); break; + case symconst_type_align: + obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init))); + break; + + case symconst_enum_const: + tv = get_enumeration_value(get_SymConst_enum(init)); + dump_arith_tarval(obst, tv, bytes); + break; + default: - assert(0 && "dump_atomic_init(): don't know how to init from this SymConst"); + assert(!"dump_atomic_init(): don't know how to init from this SymConst"); } return; @@ -190,14 +235,10 @@ static void do_dump_atomic_init(struct obstack *obst, ir_node *init) } /* - * dump an atomic value + * dumps the type for given size (.byte, .long, ...) */ -static void dump_atomic_init(struct obstack *obst, ir_node *init) -{ - ir_mode *mode = get_irn_mode(init); - int bytes = get_mode_size_bytes(mode); - - switch (bytes) { +static void dump_size_type(obstack_t *obst, int size) { + switch (size) { case 1: obstack_printf(obst, "\t.byte\t"); @@ -221,10 +262,20 @@ static void dump_atomic_init(struct obstack *obst, ir_node *init) break; default: - fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes); + fprintf(stderr, "Try to dump a type with %d bytes\n", size); assert(0); } +} + +/* + * dump an atomic value + */ +static void dump_atomic_init(obstack_t *obst, ir_node *init) +{ + ir_mode *mode = get_irn_mode(init); + int bytes = get_mode_size_bytes(mode); + dump_size_type(obst, bytes); do_dump_atomic_init(obst, init); obstack_printf(obst, "\n"); } @@ -289,7 +340,7 @@ static int ent_is_string_const(entity *ent) * @param obst The obst to dump on. * @param ent The entity to dump. */ -static void dump_string_cst(struct obstack *obst, entity *ent) +static void dump_string_cst(obstack_t *obst, entity *ent) { int i, n; @@ -308,6 +359,7 @@ static void dump_string_cst(struct obstack *obst, entity *ent) case '\n': obstack_printf(obst, "\\n"); break; case '\r': obstack_printf(obst, "\\r"); break; case '\t': obstack_printf(obst, "\\t"); break; + case '\\': obstack_printf(obst, "\\\\"); break; default : if (isprint(c)) obstack_printf(obst, "%c", c); @@ -325,12 +377,17 @@ struct arr_info { int size; }; -static void dump_object_size(struct obstack *obst, const char *name, int size) { +/** + * Dump the size of an object + */ +static void dump_object_size(obstack_t *obst, const char *name, int size) { switch (asm_flavour) { case ASM_LINUX_GAS: obstack_printf(obst, "\t.type\t%s,@object\n", name); obstack_printf(obst, "\t.size\t%s,%d\n", name, size); break; + default: + break; } } @@ -338,19 +395,22 @@ static void dump_object_size(struct obstack *obst, const char *name, int size) { * Dumps the initialization of global variables that are not * "uninitialized". */ -static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent) +static void dump_global(const be_main_env_t *main_env, + obstack_t *rdata_obstack, obstack_t *data_obstack, + obstack_t *comm_obstack, obstack_t *ctor_obstack, + entity *ent) { ir_type *ty = get_entity_type(ent); const char *ld_name = get_entity_ld_name(ent); + obstack_t *obst = data_obstack; int align, h; - struct obstack *obst = data_obstack; /* - * FIXME: did NOT work for partly constant values - */ + * FIXME: did NOT work for partly constant values + */ if (! is_Method_type(ty)) { - ent_variability variability = get_entity_variability(ent); - visibility visibility = get_entity_visibility(ent); + ir_variability variability = get_entity_variability(ent); + ir_visibility visibility = get_entity_visibility(ent); if (variability == variability_constant) { /* a constant entity, put it on the rdata */ @@ -359,10 +419,12 @@ static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obst /* check, whether it is initialized, if yes create data */ if (variability != variability_uninitialized) { + be_dbg_variable(main_env->db_handle, obst, ent); + if (visibility == visibility_external_visible) { obstack_printf(obst, ".globl\t%s\n", ld_name); } - dump_object_size(obst, ld_name, (get_type_size_bits(ty) + 7) >> 3); + dump_object_size(obst, ld_name, get_type_size_bytes(ty)); align = get_type_alignment_bytes(ty); ia32_dump_align(obst, align); @@ -409,14 +471,14 @@ static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obst int type_size, j; /* Compound entities are NOT sorted. - * The sorting strategy used doesn't work for `value' compound fields nor - * for partially_constant entities. - */ + * The sorting strategy used doesn't work for `value' compound fields nor + * for partially_constant entities. + */ /* - * in the worst case, every entity allocates one byte, so the type - * size should be equal or bigger the number of fields - */ + * in the worst case, every entity allocates one byte, so the type + * size should be equal or bigger the number of fields + */ type_size = get_type_size_bytes(ty); vals = xcalloc(type_size, sizeof(*vals)); @@ -432,7 +494,7 @@ static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obst ai = xcalloc(graph_length, sizeof(struct arr_info)); /* We wanna know how many arrays are on the path to the entity. We also have to know how - * many elements each array holds to calculate the offset for the entity. */ + * many elements each array holds to calculate the offset for the entity. */ for (j = 0; j < graph_length; j++) { entity *step = get_compound_graph_path_node(path, j); ir_type *step_type = get_entity_type(step); @@ -464,7 +526,7 @@ static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obst stepsize = (get_type_size_bits(step_type) + 7) >> 3; /* Search the next free position in vals depending on the information from above (ai). */ - while (vals[offset]) { + while (vals[offset] && aipos >= 0) { if (ai[aipos].visit_cnt < ai[aipos].n_elems) { offset += stepsize; ai[aipos].visit_cnt++; @@ -505,47 +567,77 @@ static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obst obstack_printf(obst, "\n"); } else if (visibility != visibility_external_allocated) { - /* calculate the alignment */ - align = get_type_alignment_bytes(ty); - h = highest_bit(align); - - if ((1 << h) < align) - ++h; - align = (1 << h); - - if (align < 1) - align = 1; - - ia32_dump_comm(comm_obstack, ld_name, visibility, - (get_type_size_bits(ty) + 7) >> 3, align); + be_dbg_variable(main_env->db_handle, comm_obstack, ent); + + /* uninitialized and NOT external */ + if (get_entity_owner(ent) != get_tls_type()) { + /* calculate the alignment */ + align = get_type_alignment_bytes(ty); + h = highest_bit(align); + + if ((1 << h) < align) + ++h; + align = (1 << h); + + if (align < 1) + align = 1; + + ia32_dump_comm(comm_obstack, ld_name, visibility, + get_type_size_bytes(ty), align); + } else { + /* TLS */ + if (visibility == visibility_external_visible) { + obstack_printf(comm_obstack, ".globl\t%s\n", ld_name); + } + dump_object_size(comm_obstack, ld_name, get_type_size_bytes(ty)); + align = get_type_alignment_bytes(ty); + ia32_dump_align(comm_obstack, align); + obstack_printf(comm_obstack, "%s:\n\t.zero %d\n", ld_name, get_type_size_bytes(ty)); + } } + } /* ! is method type */ + else if (ctor_obstack && get_method_img_section(ent) == section_constructors) { + ia32_dump_align(ctor_obstack, get_type_alignment_bytes(ty)); + dump_size_type(ctor_obstack, get_type_alignment_bytes(ty)); + obstack_printf(ctor_obstack, "%s\n", ld_name); } } -/* +/** * Dumps declarations of global variables and the initialization code. */ -void ia32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack) +static void ia32_dump_globals(ir_type *gt, ia32_decl_env_t *env) { - ir_type *gt = get_glob_type(); - int i, n = get_class_n_members(gt); + int i, n = get_compound_n_members(gt); for (i = 0; i < n; i++) - dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i)); + dump_global(env->main_env, env->rodata_obst, env->data_obst, env->comm_obst, env->ctor_obst, + get_compound_member(gt, i)); } /************************************************************************/ -void ia32_gen_decls(FILE *out) { - struct obstack rodata, data, comm; +void ia32_gen_decls(FILE *out, const be_main_env_t *main_env) { + ia32_decl_env_t env; + obstack_t rodata, data, comm, ctor; int size; char *cp; + /* dump the global type */ obstack_init(&rodata); obstack_init(&data); obstack_init(&comm); - ia32_dump_globals(&rodata, &data, &comm); + if (main_env->options->opt_profile) + obstack_init(&ctor); + + env.rodata_obst = &rodata; + env.data_obst = &data; + env.comm_obst = &comm; + env.ctor_obst = main_env->options->opt_profile ? &ctor : NULL; + env.main_env = main_env; + + ia32_dump_globals(get_glob_type(), &env); size = obstack_object_size(&data); cp = obstack_finish(&data); @@ -557,7 +649,7 @@ void ia32_gen_decls(FILE *out) { size = obstack_object_size(&rodata); cp = obstack_finish(&rodata); if (size > 0) { - fprintf(out, "\t.section\t.rodata\n"); + ia32_switch_section(out, SECTION_RODATA); fwrite(cp, 1, size, out); } @@ -568,7 +660,37 @@ void ia32_gen_decls(FILE *out) { fwrite(cp, 1, size, out); } + if (main_env->options->opt_profile) { + size = obstack_object_size(&ctor); + cp = obstack_finish(&ctor); + if (size > 0) { + ia32_switch_section(out, SECTION_CTOR); + fwrite(cp, 1, size, out); + } + obstack_free(&ctor, NULL); + } + obstack_free(&rodata, NULL); obstack_free(&data, NULL); obstack_free(&comm, NULL); + + /* dump the Thread Local Storage */ + obstack_init(&data); + + env.rodata_obst = &data; + env.data_obst = &data; + env.comm_obst = &data; + env.ctor_obst = NULL; + + ia32_dump_globals(get_tls_type(), &env); + + size = obstack_object_size(&data); + cp = obstack_finish(&data); + if (size > 0) { + ia32_switch_section(out, SECTION_TLS); + ia32_dump_align_f(out, 32); + fwrite(cp, 1, size, out); + } + + obstack_free(&data, NULL); }