2 * Dumps global variables and constants as ia32 assembler.
3 * @author Christian Wuerdig
21 #include "ia32_emitter.h"
22 #include "ia32_gen_decls.h"
24 typedef struct obstack obstack_t;
26 typedef struct _ia32_decl_env {
27 obstack_t *rodata_obst;
31 const be_main_env_t *main_env;
34 /************************************************************************/
37 * returns the highest bit value
39 static unsigned highest_bit(unsigned v)
43 if (v >= (1U << 16U)) {
47 if (v >= (1U << 8U)) {
51 if (v >= (1U << 4U)) {
55 if (v >= (1U << 2U)) {
59 if (v >= (1U << 1U)) {
69 static void ia32_dump_comm(obstack_t *obst, const char *name, ir_visibility vis, int size, int align) {
70 switch (asm_flavour) {
72 if (vis == visibility_local)
73 obstack_printf(obst, "\t.local\t%s\n", name);
74 obstack_printf(obst, "\t.comm\t%s,%d,%d\n", name, size, align);
77 if (vis == visibility_local)
78 obstack_printf(obst, "\t.lcomm\t%s,%d\n", name, size);
80 obstack_printf(obst, "\t.comm\t%s,%d\n", name, size);
88 * output the alignment to an obstack
90 static void ia32_dump_align(obstack_t *obst, int align)
92 int h = highest_bit(align);
99 obstack_printf(obst, "\t.align %d\n", align);
103 * output the alignment to a FILE
105 static void ia32_dump_align_f(FILE *f, int align)
107 int h = highest_bit(align);
109 if ((1 << h) < align)
114 fprintf(f, "\t.align %d\n", align);
120 static void dump_arith_tarval(obstack_t *obst, tarval *tv, int bytes)
125 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
129 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
133 obstack_printf(obst, "0x%02x%02x%02x%02x",
134 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
138 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
139 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
140 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
148 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
154 * dump an atomic value
156 static void do_dump_atomic_init(obstack_t *obst, ir_node *init)
158 ir_mode *mode = get_irn_mode(init);
159 int bytes = get_mode_size_bytes(mode);
162 switch (get_irn_opcode(init)) {
165 do_dump_atomic_init(obst, get_Cast_op(init));
169 do_dump_atomic_init(obst, get_Conv_op(init));
173 tv = get_Const_tarval(init);
175 /* beware of old stuff */
176 //assert(! mode_is_reference(mode));
178 /* it's a arithmetic value */
179 dump_arith_tarval(obst, tv, bytes);
183 switch (get_SymConst_kind(init)) {
184 case symconst_addr_name:
185 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
188 case symconst_addr_ent:
189 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
192 case symconst_ofs_ent:
193 obstack_printf(obst, "%d", get_entity_offset_bytes(get_SymConst_entity(init)));
196 case symconst_type_size:
197 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
200 case symconst_type_align:
201 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
204 case symconst_enum_const:
205 tv = get_enumeration_value(get_SymConst_enum(init));
206 dump_arith_tarval(obst, tv, bytes);
210 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
215 do_dump_atomic_init(obst, get_Add_left(init));
216 obstack_printf(obst, " + ");
217 do_dump_atomic_init(obst, get_Add_right(init));
221 do_dump_atomic_init(obst, get_Sub_left(init));
222 obstack_printf(obst, " - ");
223 do_dump_atomic_init(obst, get_Sub_right(init));
227 do_dump_atomic_init(obst, get_Mul_left(init));
228 obstack_printf(obst, " * ");
229 do_dump_atomic_init(obst, get_Mul_right(init));
233 assert(0 && "dump_atomic_init(): unknown IR-node");
238 * dumps the type for given size (.byte, .long, ...)
240 static void dump_size_type(obstack_t *obst, int size) {
244 obstack_printf(obst, "\t.byte\t");
248 obstack_printf(obst, "\t.value\t");
252 obstack_printf(obst, "\t.long\t");
256 obstack_printf(obst, "\t.quad\t");
261 /* handled in arith */
265 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
271 * dump an atomic value
273 static void dump_atomic_init(obstack_t *obst, ir_node *init)
275 ir_mode *mode = get_irn_mode(init);
276 int bytes = get_mode_size_bytes(mode);
278 dump_size_type(obst, bytes);
279 do_dump_atomic_init(obst, init);
280 obstack_printf(obst, "\n");
283 /************************************************************************/
284 /* Routines to dump global variables */
285 /************************************************************************/
288 * Determine if an entity is a string constant
289 * @param ent The entity
290 * @return 1 if it is a string constant, 0 otherwise
292 static int ent_is_string_const(entity *ent)
297 ty = get_entity_type(ent);
299 /* if it's an array */
300 if (is_Array_type(ty)) {
301 ir_type *elm_ty = get_array_element_type(ty);
303 /* and the array's element type is primitive */
304 if (is_Primitive_type(elm_ty)) {
305 ir_mode *mode = get_type_mode(elm_ty);
308 * and the mode of the element type is an int of
309 * the same size as the byte mode
311 if (mode_is_int(mode)
312 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
316 n = get_compound_ent_n_values(ent);
317 for (i = 0; i < n; ++i) {
318 ir_node *irn = get_compound_ent_value(ent, i);
319 if(get_irn_opcode(irn) != iro_Const)
322 c = (int) get_tarval_long(get_Const_tarval(irn));
324 if((i < n - 1 && !(isgraph(c) || isspace(c)))
325 || (i == n - 1 && c != '\0'))
338 * Dump a atring constant.
339 * No checks are made!!
340 * @param obst The obst to dump on.
341 * @param ent The entity to dump.
343 static void dump_string_cst(obstack_t *obst, entity *ent)
347 obstack_printf(obst, "\t.string \"");
348 n = get_compound_ent_n_values(ent);
350 for (i = 0; i < n-1; ++i) {
354 irn = get_compound_ent_value(ent, i);
355 c = (int) get_tarval_long(get_Const_tarval(irn));
358 case '"' : obstack_printf(obst, "\\\""); break;
359 case '\n': obstack_printf(obst, "\\n"); break;
360 case '\r': obstack_printf(obst, "\\r"); break;
361 case '\t': obstack_printf(obst, "\\t"); break;
362 case '\\': obstack_printf(obst, "\\\\"); break;
365 obstack_printf(obst, "%c", c);
367 obstack_printf(obst, "%O", c);
371 obstack_printf(obst, "\"\n");
381 * Dump the size of an object
383 static void dump_object_size(obstack_t *obst, const char *name, int size) {
384 switch (asm_flavour) {
386 obstack_printf(obst, "\t.type\t%s,@object\n", name);
387 obstack_printf(obst, "\t.size\t%s,%d\n", name, size);
395 * Dumps the initialization of global variables that are not
398 static void dump_global(const be_main_env_t *main_env,
399 obstack_t *rdata_obstack, obstack_t *data_obstack,
400 obstack_t *comm_obstack, obstack_t *ctor_obstack,
403 ir_type *ty = get_entity_type(ent);
404 const char *ld_name = get_entity_ld_name(ent);
405 obstack_t *obst = data_obstack;
409 * FIXME: did NOT work for partly constant values
411 if (! is_Method_type(ty)) {
412 ir_variability variability = get_entity_variability(ent);
413 ir_visibility visibility = get_entity_visibility(ent);
415 if (variability == variability_constant) {
416 /* a constant entity, put it on the rdata */
417 obst = rdata_obstack;
420 /* check, whether it is initialized, if yes create data */
421 if (variability != variability_uninitialized) {
422 be_dbg_variable(main_env->db_handle, obst, ent);
424 if (visibility == visibility_external_visible) {
425 obstack_printf(obst, ".globl\t%s\n", ld_name);
427 dump_object_size(obst, ld_name, get_type_size_bytes(ty));
429 align = get_type_alignment_bytes(ty);
430 ia32_dump_align(obst, align);
432 obstack_printf(obst, "%s:\n", ld_name);
434 if (is_atomic_type(ty)) {
435 if (get_entity_visibility(ent) != visibility_external_allocated)
436 dump_atomic_init(obst, get_atomic_ent_value(ent));
441 if (ent_is_string_const(ent)) {
442 dump_string_cst(obst, ent);
444 else if (is_Array_type(ty)) {
447 /* potential spare values should be already included! */
448 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
449 entity *step = get_compound_ent_value_member(ent, i);
450 ir_type *stype = get_entity_type(step);
452 if (get_type_mode(stype)) {
453 int align = (get_type_alignment_bits(stype) + 7) >> 3;
454 int n = size % align;
457 obstack_printf(obst, "\t.zero\t%d\n", align - n);
461 dump_atomic_init(obst, get_compound_ent_value(ent, i));
462 size += get_type_size_bytes(stype);
464 filler = get_type_size_bytes(ty) - size;
467 obstack_printf(obst, "\t.zero\t%d\n", filler);
469 else if (is_compound_type(ty)) {
473 /* Compound entities are NOT sorted.
474 * The sorting strategy used doesn't work for `value' compound fields nor
475 * for partially_constant entities.
479 * in the worst case, every entity allocates one byte, so the type
480 * size should be equal or bigger the number of fields
482 type_size = get_type_size_bytes(ty);
483 vals = xcalloc(type_size, sizeof(*vals));
485 /* collect the values and store them at the offsets */
486 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
487 int graph_length, aipos, offset;
490 compound_graph_path *path = get_compound_ent_value_path(ent, i);
492 /* get the access path to the costant value */
493 graph_length = get_compound_graph_path_length(path);
494 ai = xcalloc(graph_length, sizeof(struct arr_info));
496 /* We wanna know how many arrays are on the path to the entity. We also have to know how
497 * many elements each array holds to calculate the offset for the entity. */
498 for (j = 0; j < graph_length; j++) {
499 entity *step = get_compound_graph_path_node(path, j);
500 ir_type *step_type = get_entity_type(step);
501 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
504 if (is_Array_type(step_type))
505 for (k = 0; k < get_array_n_dimensions(step_type); k++)
506 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
508 ai[j].n_elems = n ? all_n + 1 : 0;
510 ai[j].size = ty_size;
513 aipos = graph_length - 1;
516 for (offset = j = 0; j < graph_length; j++) {
517 entity *step = get_compound_graph_path_node(path, j);
518 ir_type *step_type = get_entity_type(step);
519 int ent_ofs = get_entity_offset_bytes(step);
522 /* add all positive offsets (= offsets in structs) */
523 if (ent_ofs >= 0) offset += ent_ofs;
525 if (j == graph_length - 1) {
526 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
528 /* Search the next free position in vals depending on the information from above (ai). */
529 while (vals[offset] && aipos >= 0) {
530 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
532 ai[aipos].visit_cnt++;
535 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
536 stepsize = ai[aipos--].size;
541 assert(aipos >= 0 && "couldn't store entity");
542 vals[offset] = get_compound_ent_value(ent, i);
549 /* now write them sorted */
550 for(i = 0; i < type_size; ) {
552 dump_atomic_init(obst, vals[i]);
553 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
557 obstack_printf(obst, "\t.byte\t0\n");
564 assert(0 && "unsupported type");
567 obstack_printf(obst, "\n");
569 else if (visibility != visibility_external_allocated) {
570 be_dbg_variable(main_env->db_handle, comm_obstack, ent);
572 /* uninitialized and NOT external */
573 if (get_entity_owner(ent) != get_tls_type()) {
574 /* calculate the alignment */
575 align = get_type_alignment_bytes(ty);
576 h = highest_bit(align);
578 if ((1 << h) < align)
585 ia32_dump_comm(comm_obstack, ld_name, visibility,
586 get_type_size_bytes(ty), align);
589 if (visibility == visibility_external_visible) {
590 obstack_printf(comm_obstack, ".globl\t%s\n", ld_name);
592 dump_object_size(comm_obstack, ld_name, get_type_size_bytes(ty));
593 align = get_type_alignment_bytes(ty);
594 ia32_dump_align(comm_obstack, align);
595 obstack_printf(comm_obstack, "%s:\n\t.zero %d\n", ld_name, get_type_size_bytes(ty));
598 } /* ! is method type */
599 else if (ctor_obstack && get_method_img_section(ent) == section_constructors) {
600 ia32_dump_align(ctor_obstack, get_type_alignment_bytes(ty));
601 dump_size_type(ctor_obstack, get_type_alignment_bytes(ty));
602 obstack_printf(ctor_obstack, "%s\n", ld_name);
607 * Dumps declarations of global variables and the initialization code.
609 static void ia32_dump_globals(ir_type *gt, ia32_decl_env_t *env)
611 int i, n = get_compound_n_members(gt);
613 for (i = 0; i < n; i++)
614 dump_global(env->main_env, env->rodata_obst, env->data_obst, env->comm_obst, env->ctor_obst,
615 get_compound_member(gt, i));
618 /************************************************************************/
620 void ia32_gen_decls(FILE *out, const be_main_env_t *main_env) {
622 obstack_t rodata, data, comm, ctor;
626 /* dump the global type */
627 obstack_init(&rodata);
631 if (main_env->options->opt_profile)
634 env.rodata_obst = &rodata;
635 env.data_obst = &data;
636 env.comm_obst = &comm;
637 env.ctor_obst = main_env->options->opt_profile ? &ctor : NULL;
638 env.main_env = main_env;
640 ia32_dump_globals(get_glob_type(), &env);
642 size = obstack_object_size(&data);
643 cp = obstack_finish(&data);
645 ia32_switch_section(out, SECTION_DATA);
646 fwrite(cp, 1, size, out);
649 size = obstack_object_size(&rodata);
650 cp = obstack_finish(&rodata);
652 ia32_switch_section(out, SECTION_RODATA);
653 fwrite(cp, 1, size, out);
656 size = obstack_object_size(&comm);
657 cp = obstack_finish(&comm);
659 ia32_switch_section(out, SECTION_COMMON);
660 fwrite(cp, 1, size, out);
663 if (main_env->options->opt_profile) {
664 size = obstack_object_size(&ctor);
665 cp = obstack_finish(&ctor);
667 ia32_switch_section(out, SECTION_CTOR);
668 fwrite(cp, 1, size, out);
670 obstack_free(&ctor, NULL);
673 obstack_free(&rodata, NULL);
674 obstack_free(&data, NULL);
675 obstack_free(&comm, NULL);
677 /* dump the Thread Local Storage */
680 env.rodata_obst = &data;
681 env.data_obst = &data;
682 env.comm_obst = &data;
683 env.ctor_obst = NULL;
685 ia32_dump_globals(get_tls_type(), &env);
687 size = obstack_object_size(&data);
688 cp = obstack_finish(&data);
690 ia32_switch_section(out, SECTION_TLS);
691 ia32_dump_align_f(out, 32);
692 fwrite(cp, 1, size, out);
695 obstack_free(&data, NULL);