2 * Dumps global variables and constants as gas assembler.
3 * @author Christian Wuerdig, Matthias Braun
27 #include "beemitter.h"
28 #include "be_dbgout.h"
30 typedef struct obstack obstack_t;
32 /** by default, we generate assembler code for the Linux gas */
33 be_gas_flavour_t be_gas_flavour = GAS_FLAVOUR_NORMAL;
36 * Return the pseudo-instruction to be issued for a section switch
37 * depending on the current flavour.
39 * @param section the section to switch to
41 * @return the pseudo-instruction
43 static const char *get_section_name(be_gas_section_t section) {
44 static const char *text[GAS_FLAVOUR_MAX][GAS_SECTION_MAX] = {
50 ".section\t.tbss,\"awT\",@nobits",
51 ".section\t.ctors,\"aw\",@progbits"
56 ".section .rdata,\"dr\"",
58 ".section\t.tbss,\"awT\",@nobits",
59 ".section\t.ctors,\"aw\",@progbits"
63 assert(be_gas_flavour >= 0 && be_gas_flavour < GAS_FLAVOUR_MAX);
64 assert(section >= 0 && section < GAS_SECTION_MAX);
65 return text[be_gas_flavour][section];
69 * Emit necessary code to switch to a section.
71 * @param env the emitter environment
72 * @param section the section to switch to
74 void be_gas_emit_switch_section(be_emit_env_t *env, be_gas_section_t section) {
75 be_emit_char(env, '\t');
76 be_emit_string(env, get_section_name(section));
77 be_emit_char(env, '\n');
78 be_emit_write_line(env);
81 typedef struct _ia32_decl_env {
82 obstack_t *rodata_obst;
86 const be_main_env_t *main_env;
90 /************************************************************************/
95 static void dump_arith_tarval(obstack_t *obst, tarval *tv, int bytes)
100 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
104 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
108 obstack_printf(obst, "0x%02x%02x%02x%02x",
109 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
113 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
114 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
115 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
123 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x"
124 "%02x%02x%02x%02x%02x%02x%02x%02x",
125 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
126 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
127 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
128 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
129 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
130 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
131 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
132 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
137 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
143 * Return the tarval of an atomic initializer.
145 static tarval *get_atomic_init_tv(ir_node *init)
148 ir_mode *mode = get_irn_mode(init);
150 switch (get_irn_opcode(init)) {
153 init = get_Cast_op(init);
157 init = get_Conv_op(init);
161 return get_Const_tarval(init);
164 switch (get_SymConst_kind(init)) {
165 case symconst_ofs_ent:
166 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
168 case symconst_type_size:
169 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
171 case symconst_type_align:
172 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
174 case symconst_enum_const:
175 return get_enumeration_value(get_SymConst_enum(init));
188 * dump an atomic value
190 static void do_dump_atomic_init(ia32_decl_env_t *env, obstack_t *obst,
193 ir_mode *mode = get_irn_mode(init);
194 int bytes = get_mode_size_bytes(mode);
198 switch (get_irn_opcode(init)) {
201 do_dump_atomic_init(env, obst, get_Cast_op(init));
205 do_dump_atomic_init(env, obst, get_Conv_op(init));
209 tv = get_Const_tarval(init);
211 /* it's a arithmetic value */
212 dump_arith_tarval(obst, tv, bytes);
216 switch (get_SymConst_kind(init)) {
217 case symconst_addr_name:
218 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
221 case symconst_addr_ent:
222 ent = get_SymConst_entity(init);
223 if(!entity_visited(ent)) {
224 waitq_put(env->worklist, ent);
225 mark_entity_visited(ent);
227 obstack_printf(obst, "%s", get_entity_ld_name(ent));
230 case symconst_ofs_ent:
231 ent = get_SymConst_entity(init);
232 #if 0 /* not needed, is it? */
233 if(!entity_visited(ent)) {
234 waitq_put(env->worklist, ent);
235 mark_entity_visited(ent);
238 obstack_printf(obst, "%d", get_entity_offset(ent));
241 case symconst_type_size:
242 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
245 case symconst_type_align:
246 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
249 case symconst_enum_const:
250 tv = get_enumeration_value(get_SymConst_enum(init));
251 dump_arith_tarval(obst, tv, bytes);
255 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
260 do_dump_atomic_init(env, obst, get_Add_left(init));
261 obstack_printf(obst, " + ");
262 do_dump_atomic_init(env, obst, get_Add_right(init));
266 do_dump_atomic_init(env, obst, get_Sub_left(init));
267 obstack_printf(obst, " - ");
268 do_dump_atomic_init(env, obst, get_Sub_right(init));
272 do_dump_atomic_init(env, obst, get_Mul_left(init));
273 obstack_printf(obst, " * ");
274 do_dump_atomic_init(env, obst, get_Mul_right(init));
278 assert(0 && "dump_atomic_init(): unknown IR-node");
283 * dumps the type for given size (.byte, .long, ...)
285 static void dump_size_type(obstack_t *obst, int size) {
289 obstack_printf(obst, "\t.byte\t");
293 obstack_printf(obst, "\t.value\t");
297 obstack_printf(obst, "\t.long\t");
301 obstack_printf(obst, "\t.quad\t");
306 /* handled in arith */
310 obstack_printf(obst, "\t.octa\t");
314 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
320 * dump an atomic value to an obstack
322 static void dump_atomic_init(ia32_decl_env_t *env, obstack_t *obst,
325 ir_mode *mode = get_irn_mode(init);
326 int bytes = get_mode_size_bytes(mode);
328 dump_size_type(obst, bytes);
329 do_dump_atomic_init(env, obst, init);
330 obstack_printf(obst, "\n");
333 /************************************************************************/
334 /* Routines to dump global variables */
335 /************************************************************************/
338 * Determine if an entity is a string constant
339 * @param ent The entity
340 * @return 1 if it is a string constant, 0 otherwise
342 static int ent_is_string_const(ir_entity *ent)
344 ir_type *type, *element_type;
348 type = get_entity_type(ent);
350 /* if it's an array */
351 if (!is_Array_type(type))
354 element_type = get_array_element_type(type);
356 /* and the array's element type is primitive */
357 if (!is_Primitive_type(element_type))
360 /* and the mode of the element type is an int of
361 * the same size as the byte mode */
362 mode = get_type_mode(element_type);
363 if (!mode_is_int(mode)
364 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
367 /* if it contains only printable chars and a 0 at the end */
368 n = get_compound_ent_n_values(ent);
369 for (i = 0; i < n; ++i) {
370 ir_node *irn = get_compound_ent_value(ent, i);
371 if(get_irn_opcode(irn) != iro_Const)
374 c = (int) get_tarval_long(get_Const_tarval(irn));
376 if((i < n - 1 && !(isgraph(c) || isspace(c)))
377 || (i == n - 1 && c != '\0'))
381 /* then we can emit it as a string constant */
386 * Dump a string constant.
387 * No checks are made!!
388 * @param obst The obst to dump on.
389 * @param ent The entity to dump.
391 static void dump_string_cst(obstack_t *obst, ir_entity *ent)
395 obstack_printf(obst, "\t.string \"");
396 n = get_compound_ent_n_values(ent);
398 for (i = 0; i < n-1; ++i) {
402 irn = get_compound_ent_value(ent, i);
403 c = (int) get_tarval_long(get_Const_tarval(irn));
406 case '"' : obstack_printf(obst, "\\\""); break;
407 case '\n': obstack_printf(obst, "\\n"); break;
408 case '\r': obstack_printf(obst, "\\r"); break;
409 case '\t': obstack_printf(obst, "\\t"); break;
410 case '\\': obstack_printf(obst, "\\\\"); break;
413 obstack_printf(obst, "%c", c);
415 obstack_printf(obst, "\\%o", c);
419 obstack_printf(obst, "\"\n");
422 static void dump_array_init(ia32_decl_env_t *env, obstack_t *obst,
425 const ir_type *ty = get_entity_type(ent);
430 /* potential spare values should be already included! */
431 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
432 ir_entity *step = get_compound_ent_value_member(ent, i);
433 ir_type *stype = get_entity_type(step);
435 if (get_type_mode(stype)) {
436 int align = (get_type_alignment_bits(stype) + 7) >> 3;
437 int n = size % align;
440 obstack_printf(obst, "\t.zero\t%d\n", align - n);
444 dump_atomic_init(env, obst, get_compound_ent_value(ent, i));
445 size += get_type_size_bytes(stype);
447 filler = get_type_size_bytes(ty) - size;
450 obstack_printf(obst, "\t.skip\t%d\n", filler);
453 enum normal_or_bitfield_kind {
459 enum normal_or_bitfield_kind kind;
462 unsigned char bf_val;
464 } normal_or_bitfield;
467 * Dump an initializer for a compound entity.
469 static void dump_compound_init(ia32_decl_env_t *env, obstack_t *obst,
472 normal_or_bitfield *vals;
473 int i, j, n = get_compound_ent_n_values(ent);
476 /* Find the initializer size. Sorrily gcc support a nasty feature:
477 The last field of a compound may be a flexible array. This allows
478 initializers bigger than the type size. */
479 last_ofs = get_type_size_bytes(get_entity_type(ent));
480 for (i = 0; i < n; ++i) {
481 int offset = get_compound_ent_value_offset_bytes(ent, i);
482 int bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
483 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
484 int path_len = get_compound_graph_path_length(path);
485 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
486 int value_len = get_type_size_bits(get_entity_type(last_ent));
488 offset += (value_len + bits_remainder + 7) >> 3;
490 if (offset > last_ofs) {
496 * In the worst case, every initializer allocates one byte.
497 * Moreover, initializer might be big, do not allocate on stack.
499 vals = xcalloc(last_ofs, sizeof(vals[0]));
501 /* collect the values and store them at the offsets */
502 for (i = 0; i < n; ++i) {
503 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
504 int path_len = get_compound_graph_path_length(path);
505 int offset = get_compound_ent_value_offset_bytes(ent, i);
506 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
507 ir_node *value = get_compound_ent_value(ent, i);
508 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
509 int value_len = get_type_size_bits(get_entity_type(last_ent));
511 assert(offset_bits >= 0);
513 if (offset_bits != 0 ||
514 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
515 tarval *shift, *shifted;
516 tarval *tv = get_atomic_init_tv(value);
518 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
519 get_entity_ld_name(ent));
521 tv = tarval_convert_to(tv, mode_Lu);
522 shift = new_tarval_from_long(offset_bits, mode_Is);
523 shifted = tarval_shl(tv, shift);
524 if (shifted == tarval_bad || shifted == tarval_undefined) {
525 panic("Couldn't shift numeric value for bitfield initializer '%s'\n",
526 get_entity_ld_name(ent));
529 for (j = 0; value_len > 0; ++j) {
530 assert(offset + j < last_ofs);
531 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
532 vals[offset + j].kind = BITFIELD;
533 vals[offset + j].v.bf_val |= get_tarval_sub_bits(shifted, j);
534 value_len -= 8 - offset_bits;
538 assert(offset < last_ofs);
539 assert(vals[offset].kind == NORMAL);
540 assert(vals[offset].v.value == NULL);
541 vals[offset].v.value = value;
545 /* now write them sorted */
546 for (i = 0; i < last_ofs; ) {
547 int space = 0, skip = 0;
548 if (vals[i].kind == NORMAL) {
549 if(vals[i].v.value != NULL) {
550 dump_atomic_init(env, obst, vals[i].v.value);
551 skip = get_mode_size_bytes(get_irn_mode(vals[i].v.value)) - 1;
556 assert(vals[i].kind == BITFIELD);
557 obstack_printf(obst, "\t.byte\t%d\n", vals[i].v.bf_val);
562 while (i < last_ofs && vals[i].kind == NORMAL && vals[i].v.value == NULL) {
571 obstack_printf(obst, "\t.skip\t%d\n", space);
577 * Dump a global entity.
579 static void dump_global(ia32_decl_env_t *env, ir_entity *ent, int emit_commons)
582 ir_type *type = get_entity_type(ent);
583 const char *ld_name = get_entity_ld_name(ent);
584 ir_variability variability = get_entity_variability(ent);
585 ir_visibility visibility = get_entity_visibility(ent);
586 int align = get_type_alignment_bytes(type);
587 int emit_as_common = 0;
589 obst = env->data_obst;
590 if (is_Method_type(type)) {
591 if (get_method_img_section(ent) == section_constructors) {
592 obst = env->ctor_obst;
593 obstack_printf(obst, ".balign\t%d\n", align);
594 dump_size_type(obst, align);
595 obstack_printf(obst, "%s\n", ld_name);
598 } else if (variability == variability_constant) {
599 /* a constant entity, put it on the rdata */
600 obst = env->rodata_obst;
601 } else if (variability == variability_uninitialized) {
602 /* uninitialized entity put it in bss segment */
603 obst = env->bss_obst;
604 if(emit_commons && visibility != visibility_local)
608 be_dbg_variable(env->main_env->db_handle, obst, ent);
610 /* global or not global */
611 if (visibility == visibility_external_visible && !emit_as_common) {
612 obstack_printf(obst, ".global\t%s\n", ld_name);
613 } else if(visibility == visibility_external_allocated) {
614 obstack_printf(obst, ".global\t%s\n", ld_name);
615 /* we can return now... */
619 if (align > 1 && !emit_as_common) {
620 obstack_printf(obst, ".balign\t%d\n", align);
623 if (!emit_as_common) {
624 obstack_printf(obst, "%s:\n", ld_name);
627 if (variability == variability_uninitialized) {
629 obstack_printf(obst, "\t.comm %s,%d,%d\n",
630 ld_name, get_type_size_bytes(type), align);
632 obstack_printf(obst, "\t.zero %d\n", get_type_size_bytes(type));
634 } else if (is_atomic_type(type)) {
635 dump_atomic_init(env, obst, get_atomic_ent_value(ent));
636 } else if (ent_is_string_const(ent)) {
637 dump_string_cst(obst, ent);
638 } else if (is_Array_type(type)) {
639 dump_array_init(env, obst, ent);
640 } else if (is_compound_type(type)) {
641 dump_compound_init(env, obst, ent);
643 assert(0 && "unsupported type");
648 * Dumps declarations of global variables and the initialization code.
650 static void ia32_dump_globals(ir_type *gt, ia32_decl_env_t *env,
651 int emit_commons, int only_emit_marked)
653 int i, n = get_compound_n_members(gt);
654 waitq *worklist = new_waitq();
656 if(only_emit_marked) {
657 for (i = 0; i < n; i++) {
658 ir_entity *ent = get_compound_member(gt, i);
659 if(entity_visited(ent) ||
660 get_entity_visibility(ent) != visibility_external_allocated) {
661 waitq_put(worklist, ent);
662 mark_entity_visited(ent);
666 inc_master_type_visited();
667 for (i = 0; i < n; i++) {
668 ir_entity *ent = get_compound_member(gt, i);
669 mark_entity_visited(ent);
670 waitq_put(worklist, ent);
674 env->worklist = worklist;
676 while(!waitq_empty(worklist)) {
677 ir_entity *ent = waitq_get(worklist);
679 dump_global(env, ent, emit_commons);
683 env->worklist = NULL;
686 /************************************************************************/
688 void be_gas_emit_decls(be_emit_env_t *emit, const be_main_env_t *main_env,
689 int only_emit_marked_entities)
692 obstack_t rodata, data, bss, ctor;
696 /* dump the global type */
697 obstack_init(&rodata);
702 env.rodata_obst = &rodata;
703 env.data_obst = &data;
705 env.ctor_obst = &ctor;
706 env.main_env = main_env;
708 ia32_dump_globals(get_glob_type(), &env, 1, only_emit_marked_entities);
710 size = obstack_object_size(&data);
711 cp = obstack_finish(&data);
713 be_gas_emit_switch_section(emit, GAS_SECTION_DATA);
714 be_emit_string_len(emit, cp, size);
715 be_emit_write_line(emit);
718 size = obstack_object_size(&rodata);
719 cp = obstack_finish(&rodata);
721 be_gas_emit_switch_section(emit, GAS_SECTION_RODATA);
722 be_emit_string_len(emit, cp, size);
723 be_emit_write_line(emit);
726 size = obstack_object_size(&bss);
727 cp = obstack_finish(&bss);
729 be_gas_emit_switch_section(emit, GAS_SECTION_COMMON);
730 be_emit_string_len(emit, cp, size);
731 be_emit_write_line(emit);
734 size = obstack_object_size(&ctor);
735 cp = obstack_finish(&ctor);
737 be_gas_emit_switch_section(emit, GAS_SECTION_CTOR);
738 be_emit_string_len(emit, cp, size);
739 be_emit_write_line(emit);
742 obstack_free(&rodata, NULL);
743 obstack_free(&data, NULL);
744 obstack_free(&bss, NULL);
745 obstack_free(&ctor, NULL);
747 /* dump the Thread Local Storage */
750 env.rodata_obst = &data;
751 env.data_obst = &data;
752 env.bss_obst = &data;
753 env.ctor_obst = NULL;
755 ia32_dump_globals(get_tls_type(), &env, 0, only_emit_marked_entities);
757 size = obstack_object_size(&data);
758 cp = obstack_finish(&data);
760 be_gas_emit_switch_section(emit, GAS_SECTION_TLS);
761 be_emit_cstring(emit, ".balign\t32\n");
762 be_emit_write_line(emit);
763 be_emit_string_len(emit, cp, size);
764 be_emit_write_line(emit);
767 obstack_free(&data, NULL);