2 * Dumps global variables and constants as gas assembler.
3 * @author Christian Wuerdig, Matthias Braun
26 #include "beemitter.h"
27 #include "be_dbgout.h"
29 typedef struct obstack obstack_t;
31 /** by default, we generate assembler code for the Linux gas */
32 be_gas_flavour_t be_gas_flavour = GAS_FLAVOUR_NORMAL;
34 static const char* get_section_name(be_gas_section_t section) {
35 static const char *text[GAS_FLAVOUR_MAX][GAS_SECTION_MAX] = {
41 ".section\t.tbss,\"awT\",@nobits",
42 ".section\t.ctors,\"aw\",@progbits"
47 ".section .rdata,\"dr\"",
49 ".section\t.tbss,\"awT\",@nobits",
50 ".section\t.ctors,\"aw\",@progbits"
54 assert(be_gas_flavour >= 0 && be_gas_flavour < GAS_FLAVOUR_MAX);
55 assert(section >= 0 && section < GAS_SECTION_MAX);
56 return text[be_gas_flavour][section];
59 void be_gas_emit_switch_section(be_emit_env_t *env, be_gas_section_t section) {
60 be_emit_char(env, '\t');
61 be_emit_string(env, get_section_name(section));
62 be_emit_char(env, '\n');
63 be_emit_write_line(env);
66 typedef struct _ia32_decl_env {
67 obstack_t *rodata_obst;
71 const be_main_env_t *main_env;
74 /************************************************************************/
79 static void dump_arith_tarval(obstack_t *obst, tarval *tv, int bytes)
84 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
88 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
92 obstack_printf(obst, "0x%02x%02x%02x%02x",
93 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
97 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
98 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
99 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
107 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x"
108 "%02x%02x%02x%02x%02x%02x%02x%02x",
109 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
110 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
111 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
112 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
113 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
114 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),
116 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
121 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
127 * Return the tarval of an atomic initializer.
129 static tarval *get_atomic_init_tv(ir_node *init)
132 ir_mode *mode = get_irn_mode(init);
134 switch (get_irn_opcode(init)) {
137 init = get_Cast_op(init);
141 init = get_Conv_op(init);
145 return get_Const_tarval(init);
148 switch (get_SymConst_kind(init)) {
149 case symconst_ofs_ent:
150 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
152 case symconst_type_size:
153 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
155 case symconst_type_align:
156 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
158 case symconst_enum_const:
159 return get_enumeration_value(get_SymConst_enum(init));
172 * dump an atomic value
174 static void do_dump_atomic_init(obstack_t *obst, ir_node *init)
176 ir_mode *mode = get_irn_mode(init);
177 int bytes = get_mode_size_bytes(mode);
180 switch (get_irn_opcode(init)) {
183 do_dump_atomic_init(obst, get_Cast_op(init));
187 do_dump_atomic_init(obst, get_Conv_op(init));
191 tv = get_Const_tarval(init);
193 /* it's a arithmetic value */
194 dump_arith_tarval(obst, tv, bytes);
198 switch (get_SymConst_kind(init)) {
199 case symconst_addr_name:
200 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
203 case symconst_addr_ent:
204 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
207 case symconst_ofs_ent:
208 obstack_printf(obst, "%d", get_entity_offset(get_SymConst_entity(init)));
211 case symconst_type_size:
212 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
215 case symconst_type_align:
216 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
219 case symconst_enum_const:
220 tv = get_enumeration_value(get_SymConst_enum(init));
221 dump_arith_tarval(obst, tv, bytes);
225 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
230 do_dump_atomic_init(obst, get_Add_left(init));
231 obstack_printf(obst, " + ");
232 do_dump_atomic_init(obst, get_Add_right(init));
236 do_dump_atomic_init(obst, get_Sub_left(init));
237 obstack_printf(obst, " - ");
238 do_dump_atomic_init(obst, get_Sub_right(init));
242 do_dump_atomic_init(obst, get_Mul_left(init));
243 obstack_printf(obst, " * ");
244 do_dump_atomic_init(obst, get_Mul_right(init));
248 assert(0 && "dump_atomic_init(): unknown IR-node");
253 * dumps the type for given size (.byte, .long, ...)
255 static void dump_size_type(obstack_t *obst, int size) {
259 obstack_printf(obst, "\t.byte\t");
263 obstack_printf(obst, "\t.value\t");
267 obstack_printf(obst, "\t.long\t");
271 obstack_printf(obst, "\t.quad\t");
276 /* handled in arith */
280 obstack_printf(obst, "\t.octa\t");
284 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
290 * dump an atomic value to an obstack
292 static void dump_atomic_init(obstack_t *obst, ir_node *init)
294 ir_mode *mode = get_irn_mode(init);
295 int bytes = get_mode_size_bytes(mode);
297 dump_size_type(obst, bytes);
298 do_dump_atomic_init(obst, init);
299 obstack_printf(obst, "\n");
302 /************************************************************************/
303 /* Routines to dump global variables */
304 /************************************************************************/
307 * Determine if an entity is a string constant
308 * @param ent The entity
309 * @return 1 if it is a string constant, 0 otherwise
311 static int ent_is_string_const(ir_entity *ent)
313 ir_type *type, *element_type;
317 type = get_entity_type(ent);
319 /* if it's an array */
320 if (!is_Array_type(type))
323 element_type = get_array_element_type(type);
325 /* and the array's element type is primitive */
326 if (!is_Primitive_type(element_type))
329 /* and the mode of the element type is an int of
330 * the same size as the byte mode */
331 mode = get_type_mode(element_type);
332 if (!mode_is_int(mode)
333 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
336 /* if it contains only printable chars and a 0 at the end */
337 n = get_compound_ent_n_values(ent);
338 for (i = 0; i < n; ++i) {
339 ir_node *irn = get_compound_ent_value(ent, i);
340 if(get_irn_opcode(irn) != iro_Const)
343 c = (int) get_tarval_long(get_Const_tarval(irn));
345 if((i < n - 1 && !(isgraph(c) || isspace(c)))
346 || (i == n - 1 && c != '\0'))
350 /* then we can emit it as a string constant */
355 * Dump a string constant.
356 * No checks are made!!
357 * @param obst The obst to dump on.
358 * @param ent The entity to dump.
360 static void dump_string_cst(obstack_t *obst, ir_entity *ent)
364 obstack_printf(obst, "\t.string \"");
365 n = get_compound_ent_n_values(ent);
367 for (i = 0; i < n-1; ++i) {
371 irn = get_compound_ent_value(ent, i);
372 c = (int) get_tarval_long(get_Const_tarval(irn));
375 case '"' : obstack_printf(obst, "\\\""); break;
376 case '\n': obstack_printf(obst, "\\n"); break;
377 case '\r': obstack_printf(obst, "\\r"); break;
378 case '\t': obstack_printf(obst, "\\t"); break;
379 case '\\': obstack_printf(obst, "\\\\"); break;
382 obstack_printf(obst, "%c", c);
384 obstack_printf(obst, "\\%o", c);
388 obstack_printf(obst, "\"\n");
391 static void dump_array_init(obstack_t *obst, ir_entity *ent)
393 const ir_type *ty = get_entity_type(ent);
398 /* potential spare values should be already included! */
399 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
400 ir_entity *step = get_compound_ent_value_member(ent, i);
401 ir_type *stype = get_entity_type(step);
403 if (get_type_mode(stype)) {
404 int align = (get_type_alignment_bits(stype) + 7) >> 3;
405 int n = size % align;
408 obstack_printf(obst, "\t.zero\t%d\n", align - n);
412 dump_atomic_init(obst, get_compound_ent_value(ent, i));
413 size += get_type_size_bytes(stype);
415 filler = get_type_size_bytes(ty) - size;
418 obstack_printf(obst, "\t.skip\t%d\n", filler);
421 enum normal_or_bitfield_kind {
427 enum normal_or_bitfield_kind kind;
430 unsigned char bf_val;
432 } normal_or_bitfield;
435 * Dump an initializer for a compound entity.
437 static void dump_compound_init(obstack_t *obst, ir_entity *ent)
439 normal_or_bitfield *vals;
440 int i, j, n = get_compound_ent_n_values(ent);
443 /* Find the initializer size. Sorrily gcc support a nasty feature:
444 The last field of a compound may be a flexible array. This allows
445 initializers bigger than the type size. */
447 for (i = 0; i < n; ++i) {
448 int offset = get_compound_ent_value_offset_bytes(ent, i);
449 int bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
450 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
451 int path_len = get_compound_graph_path_length(path);
452 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
453 int value_len = get_type_size_bits(get_entity_type(last_ent));
455 offset += (value_len + bits_remainder + 7) >> 3;
457 if (offset > last_ofs) {
463 * In the worst case, every initializer allocates one byte.
464 * Moreover, initializer might be big, do not allocate on stack.
466 vals = xcalloc(last_ofs, sizeof(vals[0]));
468 /* collect the values and store them at the offsets */
469 for (i = 0; i < n; ++i) {
470 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
471 int path_len = get_compound_graph_path_length(path);
472 int offset = get_compound_ent_value_offset_bytes(ent, i);
473 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
474 ir_node *value = get_compound_ent_value(ent, i);
475 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
476 int value_len = get_type_size_bits(get_entity_type(last_ent));
478 assert(offset_bits >= 0);
480 if (offset_bits != 0 ||
481 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
482 tarval *shift, *shifted;
483 tarval *tv = get_atomic_init_tv(value);
485 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
486 get_entity_ld_name(ent));
488 tv = tarval_convert_to(tv, mode_Lu);
489 shift = new_tarval_from_long(offset_bits, mode_Is);
490 shifted = tarval_shl(tv, shift);
491 if (shifted == tarval_bad || shifted == tarval_undefined) {
492 panic("Couldn't shift numeric value for bitfield initializer '%s'\n",
493 get_entity_ld_name(ent));
496 for (j = 0; value_len > 0; ++j) {
497 assert(offset + j < last_ofs);
498 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
499 vals[offset + j].kind = BITFIELD;
500 vals[offset + j].v.bf_val |= get_tarval_sub_bits(shifted, j);
501 value_len -= 8 - offset_bits;
505 assert(offset < last_ofs);
506 assert(vals[offset].kind == NORMAL);
507 assert(vals[offset].v.value == NULL);
508 vals[offset].v.value = value;
512 /* now write them sorted */
513 for (i = 0; i < last_ofs; ) {
514 int space = 0, skip = 0;
515 if (vals[i].kind == NORMAL) {
516 if(vals[i].v.value != NULL) {
517 dump_atomic_init(obst, vals[i].v.value);
518 skip = get_mode_size_bytes(get_irn_mode(vals[i].v.value)) - 1;
523 assert(vals[i].kind == BITFIELD);
524 obstack_printf(obst, "\t.byte\t%d\n", vals[i].v.bf_val);
529 while (i < last_ofs && vals[i].kind == NORMAL && vals[i].v.value == NULL) {
538 obstack_printf(obst, "\t.skip\t%d\n", space);
544 * Dump a global entity.
546 static void dump_global(ia32_decl_env_t *env, ir_entity *ent, int emit_commons)
549 ir_type *type = get_entity_type(ent);
550 const char *ld_name = get_entity_ld_name(ent);
551 ir_variability variability = get_entity_variability(ent);
552 ir_visibility visibility = get_entity_visibility(ent);
553 int align = get_type_alignment_bytes(type);
554 int emit_as_common = 0;
556 obst = env->data_obst;
557 if (is_Method_type(type)) {
558 if (get_method_img_section(ent) == section_constructors) {
559 obst = env->ctor_obst;
560 obstack_printf(obst, ".balign\t%d\n", align);
561 dump_size_type(obst, align);
562 obstack_printf(obst, "%s\n", ld_name);
565 } else if (variability == variability_constant) {
566 /* a constant entity, put it on the rdata */
567 obst = env->rodata_obst;
568 } else if (variability == variability_uninitialized) {
569 /* uninitialized entity put it in bss segment */
570 obst = env->bss_obst;
571 if(emit_commons && visibility != visibility_local)
575 be_dbg_variable(env->main_env->db_handle, obst, ent);
577 /* global or not global */
578 if (visibility == visibility_external_visible && !emit_as_common) {
579 obstack_printf(obst, ".global\t%s\n", ld_name);
580 } else if(visibility == visibility_external_allocated) {
581 obstack_printf(obst, ".global\t%s\n", ld_name);
582 /* we can return now... */
586 if (align > 1 && !emit_as_common) {
587 obstack_printf(obst, ".balign\t%d\n", align);
590 if (!emit_as_common) {
591 obstack_printf(obst, "%s:\n", ld_name);
594 if (variability == variability_uninitialized) {
596 obstack_printf(obst, "\t.comm %s,%d,%d\n",
597 ld_name, get_type_size_bytes(type), align);
599 obstack_printf(obst, "\t.zero %d\n", get_type_size_bytes(type));
601 } else if (is_atomic_type(type)) {
602 dump_atomic_init(obst, get_atomic_ent_value(ent));
603 } else if (ent_is_string_const(ent)) {
604 dump_string_cst(obst, ent);
605 } else if (is_Array_type(type)) {
606 dump_array_init(obst, ent);
607 } else if (is_compound_type(type)) {
608 dump_compound_init(obst, ent);
610 assert(0 && "unsupported type");
615 * Dumps declarations of global variables and the initialization code.
617 static void ia32_dump_globals(ir_type *gt, ia32_decl_env_t *env, int emit_commons)
619 int i, n = get_compound_n_members(gt);
621 for (i = 0; i < n; i++) {
622 ir_entity *ent = get_compound_member(gt, i);
623 dump_global(env, ent, emit_commons);
627 /************************************************************************/
629 void be_gas_emit_decls(be_emit_env_t *emit, const be_main_env_t *main_env) {
631 obstack_t rodata, data, bss, ctor;
635 /* dump the global type */
636 obstack_init(&rodata);
641 env.rodata_obst = &rodata;
642 env.data_obst = &data;
644 env.ctor_obst = &ctor;
645 env.main_env = main_env;
647 ia32_dump_globals(get_glob_type(), &env, 1);
649 size = obstack_object_size(&data);
650 cp = obstack_finish(&data);
652 be_gas_emit_switch_section(emit, GAS_SECTION_DATA);
653 be_emit_string_len(emit, cp, size);
654 be_emit_write_line(emit);
657 size = obstack_object_size(&rodata);
658 cp = obstack_finish(&rodata);
660 be_gas_emit_switch_section(emit, GAS_SECTION_RODATA);
661 be_emit_string_len(emit, cp, size);
662 be_emit_write_line(emit);
665 size = obstack_object_size(&bss);
666 cp = obstack_finish(&bss);
668 be_gas_emit_switch_section(emit, GAS_SECTION_COMMON);
669 be_emit_string_len(emit, cp, size);
670 be_emit_write_line(emit);
673 size = obstack_object_size(&ctor);
674 cp = obstack_finish(&ctor);
676 be_gas_emit_switch_section(emit, GAS_SECTION_CTOR);
677 be_emit_string_len(emit, cp, size);
678 be_emit_write_line(emit);
681 obstack_free(&rodata, NULL);
682 obstack_free(&data, NULL);
683 obstack_free(&bss, NULL);
684 obstack_free(&ctor, NULL);
686 /* dump the Thread Local Storage */
689 env.rodata_obst = &data;
690 env.data_obst = &data;
691 env.bss_obst = &data;
692 env.ctor_obst = NULL;
694 ia32_dump_globals(get_tls_type(), &env, 0);
696 size = obstack_object_size(&data);
697 cp = obstack_finish(&data);
699 be_gas_emit_switch_section(emit, GAS_SECTION_TLS);
700 be_emit_cstring(emit, ".balign\t32\n");
701 be_emit_write_line(emit);
702 be_emit_string_len(emit, cp, size);
703 be_emit_write_line(emit);
706 obstack_free(&data, NULL);