2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Dumps global variables and constants as gas assembler.
23 * @author Christian Wuerdig, Matthias Braun
46 #include "beemitter.h"
47 #include "be_dbgout.h"
49 typedef struct obstack obstack_t;
51 /** by default, we generate assembler code for the Linux gas */
52 be_gas_flavour_t be_gas_flavour = GAS_FLAVOUR_NORMAL;
55 * Return the pseudo-instruction to be issued for a section switch
56 * depending on the current flavour.
58 * @param section the section to switch to
60 * @return the pseudo-instruction
62 static const char *get_section_name(be_gas_section_t section) {
63 static const char *text[GAS_FLAVOUR_MAX][GAS_SECTION_MAX] = {
69 ".section\t.tbss,\"awT\",@nobits",
70 ".section\t.ctors,\"aw\",@progbits"
75 ".section .rdata,\"dr\"",
77 ".section\t.tbss,\"awT\",@nobits",
78 ".section\t.ctors,\"aw\",@progbits"
82 assert((int) be_gas_flavour >= 0 && be_gas_flavour < GAS_FLAVOUR_MAX);
83 assert((int) section >= 0 && section < GAS_SECTION_MAX);
84 return text[be_gas_flavour][section];
88 * Emit necessary code to switch to a section.
90 * @param env the emitter environment
91 * @param section the section to switch to
93 void be_gas_emit_switch_section(be_emit_env_t *env, be_gas_section_t section) {
94 be_emit_char(env, '\t');
95 be_emit_string(env, get_section_name(section));
96 be_emit_char(env, '\n');
97 be_emit_write_line(env);
101 * An environment containing all needed dumper data.
102 * Currently we create the file completely in memory first, then
103 * write it to the disk. This is an artifact from the old C-generating backend
104 * and even there NOT needed. So we might change it in the future.
106 typedef struct _be_gas_decl_env {
107 obstack_t *rodata_obst; /**< An obstack that will be filled with all rodata entities. */
108 obstack_t *data_obst; /**< An obstack that will be filled with the initialized entities. */
109 obstack_t *bss_obst; /**< An obstack that will be filled with the uninitialized entities. */
110 obstack_t *ctor_obst; /**< An obstack that will be filled with the constructor entities. */
111 const be_main_env_t *main_env; /**< The main backend environment, used for it's debug handle. */
112 waitq *worklist; /**< A worklist we use to place not yet handled entities on. */
115 /************************************************************************/
120 * @param obst the obstack where the data is written too
121 * @param tv the tarval
122 * @param bytes the width of the tarvals value in bytes
124 static void dump_arith_tarval(obstack_t *obst, tarval *tv, int bytes)
129 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
133 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
137 obstack_printf(obst, "0x%02x%02x%02x%02x",
138 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
142 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
143 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
144 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
152 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x"
153 "%02x%02x%02x%02x%02x%02x%02x%02x",
154 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
155 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
156 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
157 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
158 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
159 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
160 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
161 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
166 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
172 * Return the label prefix for labeled blocks.
174 const char *be_gas_label_prefix(void) {
181 static void dump_label(obstack_t *obst, ir_label_t label) {
182 obstack_printf(obst, "%s%ld", be_gas_label_prefix(), label);
186 * Return the tarval of an atomic initializer.
188 * @param init a node representing the initializer (on the const code irg)
192 static tarval *get_atomic_init_tv(ir_node *init)
195 ir_mode *mode = get_irn_mode(init);
197 switch (get_irn_opcode(init)) {
200 init = get_Cast_op(init);
204 init = get_Conv_op(init);
208 return get_Const_tarval(init);
211 switch (get_SymConst_kind(init)) {
212 case symconst_type_size:
213 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
215 case symconst_type_align:
216 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
218 case symconst_ofs_ent:
219 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
221 case symconst_enum_const:
222 return get_enumeration_value(get_SymConst_enum(init));
238 * Dump an atomic value.
240 * @param env the gas output environment
241 * @param obst an obstack the output is written to
242 * @param init a node representing the atomic value (on the const code irg)
244 static void do_dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
247 ir_mode *mode = get_irn_mode(init);
248 int bytes = get_mode_size_bytes(mode);
253 switch (get_irn_opcode(init)) {
256 do_dump_atomic_init(env, obst, get_Cast_op(init));
260 do_dump_atomic_init(env, obst, get_Conv_op(init));
264 tv = get_Const_tarval(init);
266 /* it's a arithmetic value */
267 dump_arith_tarval(obst, tv, bytes);
271 switch (get_SymConst_kind(init)) {
272 case symconst_addr_name:
273 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
276 case symconst_addr_ent:
277 ent = get_SymConst_entity(init);
278 if(!is_entity_backend_marked(ent)) {
279 waitq_put(env->worklist, ent);
280 set_entity_backend_marked(ent, 1);
282 obstack_printf(obst, "%s", get_entity_ld_name(ent));
285 case symconst_ofs_ent:
286 ent = get_SymConst_entity(init);
287 #if 0 /* not needed, is it? */
288 if(!is_entity_backend_marked(ent)) {
289 waitq_put(env->worklist, ent);
290 set_entity_backend_marked(ent, 1);
293 obstack_printf(obst, "%d", get_entity_offset(ent));
296 case symconst_type_size:
297 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
300 case symconst_type_align:
301 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
304 case symconst_enum_const:
305 tv = get_enumeration_value(get_SymConst_enum(init));
306 dump_arith_tarval(obst, tv, bytes);
310 label = get_SymConst_label(init);
311 dump_label(obst, label);
315 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
320 do_dump_atomic_init(env, obst, get_Add_left(init));
321 obstack_printf(obst, " + ");
322 do_dump_atomic_init(env, obst, get_Add_right(init));
326 do_dump_atomic_init(env, obst, get_Sub_left(init));
327 obstack_printf(obst, " - ");
328 do_dump_atomic_init(env, obst, get_Sub_right(init));
332 do_dump_atomic_init(env, obst, get_Mul_left(init));
333 obstack_printf(obst, " * ");
334 do_dump_atomic_init(env, obst, get_Mul_right(init));
338 assert(0 && "dump_atomic_init(): unknown IR-node");
343 * Dumps the type for given size (.byte, .long, ...)
345 * @param obst an obstack the output is written to
346 * @param size the size in bytes
348 static void dump_size_type(obstack_t *obst, int size) {
352 obstack_printf(obst, "\t.byte\t");
356 obstack_printf(obst, "\t.value\t");
360 obstack_printf(obst, "\t.long\t");
364 obstack_printf(obst, "\t.quad\t");
369 /* handled in arith */
373 obstack_printf(obst, "\t.octa\t");
377 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
383 * Dump an atomic value to an obstack.
385 * @param env the gas output environment
386 * @param obst an obstack the output is written to
387 * @param init a node representing the atomic value (on the const code irg)
389 static void dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
392 ir_mode *mode = get_irn_mode(init);
393 int bytes = get_mode_size_bytes(mode);
395 dump_size_type(obst, bytes);
396 do_dump_atomic_init(env, obst, init);
397 obstack_printf(obst, "\n");
400 /************************************************************************/
401 /* Routines to dump global variables */
402 /************************************************************************/
405 * Determine if an entity is a string constant
406 * @param ent The entity
407 * @return 1 if it is a string constant, 0 otherwise
409 static int ent_is_string_const(ir_entity *ent)
411 ir_type *type, *element_type;
415 type = get_entity_type(ent);
417 /* if it's an array */
418 if (!is_Array_type(type))
421 element_type = get_array_element_type(type);
423 /* and the array's element type is primitive */
424 if (!is_Primitive_type(element_type))
427 /* and the mode of the element type is an int of
428 * the same size as the byte mode */
429 mode = get_type_mode(element_type);
430 if (!mode_is_int(mode)
431 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
434 /* if it contains only printable chars and a 0 at the end */
435 n = get_compound_ent_n_values(ent);
436 for (i = 0; i < n; ++i) {
437 ir_node *irn = get_compound_ent_value(ent, i);
438 if(get_irn_opcode(irn) != iro_Const)
441 c = (int) get_tarval_long(get_Const_tarval(irn));
443 if((i < n - 1 && !(isgraph(c) || isspace(c)))
444 || (i == n - 1 && c != '\0'))
448 /* then we can emit it as a string constant */
453 * Dump a string constant.
454 * No checks are made!!
456 * @param obst The obst to dump on.
457 * @param ent The entity to dump.
459 static void dump_string_cst(obstack_t *obst, ir_entity *ent)
466 obstack_printf(obst, "\t.string \"");
467 n = get_compound_ent_n_values(ent);
469 for (i = 0; i < n-1; ++i) {
473 irn = get_compound_ent_value(ent, i);
474 c = (int) get_tarval_long(get_Const_tarval(irn));
477 case '"' : obstack_printf(obst, "\\\""); break;
478 case '\n': obstack_printf(obst, "\\n"); break;
479 case '\r': obstack_printf(obst, "\\r"); break;
480 case '\t': obstack_printf(obst, "\\t"); break;
481 case '\\': obstack_printf(obst, "\\\\"); break;
484 obstack_printf(obst, "%c", c);
486 obstack_printf(obst, "\\%o", c);
490 obstack_printf(obst, "\"\n");
492 type = get_entity_type(ent);
493 type_size = get_type_size_bytes(type);
494 remaining_space = type_size - n;
495 assert(remaining_space >= 0);
496 if(remaining_space > 0) {
497 obstack_printf(obst, "\t.skip\t%d\n", remaining_space);
501 enum normal_or_bitfield_kind {
507 enum normal_or_bitfield_kind kind;
510 unsigned char bf_val;
512 } normal_or_bitfield;
515 * Dump an initializer for a compound entity.
517 static void dump_compound_init(be_gas_decl_env_t *env, obstack_t *obst,
520 normal_or_bitfield *vals;
521 int i, j, n = get_compound_ent_n_values(ent);
524 /* Find the initializer size. Sorrily gcc support a nasty feature:
525 The last field of a compound may be a flexible array. This allows
526 initializers bigger than the type size. */
527 last_ofs = get_type_size_bytes(get_entity_type(ent));
528 for (i = 0; i < n; ++i) {
529 int offset = get_compound_ent_value_offset_bytes(ent, i);
530 int bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
531 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
532 int path_len = get_compound_graph_path_length(path);
533 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
534 int value_len = get_type_size_bits(get_entity_type(last_ent));
536 offset += (value_len + bits_remainder + 7) >> 3;
538 if (offset > last_ofs) {
544 * In the worst case, every initializer allocates one byte.
545 * Moreover, initializer might be big, do not allocate on stack.
547 vals = xcalloc(last_ofs, sizeof(vals[0]));
549 /* collect the values and store them at the offsets */
550 for (i = 0; i < n; ++i) {
551 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
552 int path_len = get_compound_graph_path_length(path);
553 int offset = get_compound_ent_value_offset_bytes(ent, i);
554 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
555 ir_node *value = get_compound_ent_value(ent, i);
556 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
557 int value_len = get_type_size_bits(get_entity_type(last_ent));
559 assert(offset_bits >= 0);
561 if (offset_bits != 0 ||
562 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
563 tarval *tv = get_atomic_init_tv(value);
564 unsigned char curr_bits, last_bits = 0;
566 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
567 get_entity_ld_name(ent));
569 /* normalize offset */
570 offset += offset_bits >> 3;
573 for (j = 0; value_len + offset_bits > 0; ++j) {
574 assert(offset + j < last_ofs);
575 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
576 vals[offset + j].kind = BITFIELD;
577 curr_bits = get_tarval_sub_bits(tv, j);
578 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
580 last_bits = curr_bits;
585 assert(offset < last_ofs);
586 assert(vals[offset].kind == NORMAL);
587 for (i = 1; i < value_len / 8; ++i) {
588 assert(vals[offset + i].v.value == NULL);
590 vals[offset].v.value = value;
594 /* now write them sorted */
595 for (i = 0; i < last_ofs; ) {
596 int space = 0, skip = 0;
597 if (vals[i].kind == NORMAL) {
598 if(vals[i].v.value != NULL) {
599 dump_atomic_init(env, obst, vals[i].v.value);
600 skip = get_mode_size_bytes(get_irn_mode(vals[i].v.value)) - 1;
605 assert(vals[i].kind == BITFIELD);
606 obstack_printf(obst, "\t.byte\t%d\n", vals[i].v.bf_val);
610 while (i < last_ofs && vals[i].kind == NORMAL && vals[i].v.value == NULL) {
619 obstack_printf(obst, "\t.skip\t%d\n", space);
625 * Dump a global entity.
627 * @param env the gas output environment
628 * @param ent the entity to be dumped
629 * @param emit_commons if non-zero, emit commons (non-local uninitialized entities)
631 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent, int emit_commons)
634 ir_type *type = get_entity_type(ent);
635 const char *ld_name = get_entity_ld_name(ent);
636 int align = get_type_alignment_bytes(type);
637 int emit_as_common = 0;
638 ir_variability variability;
639 ir_visibility visibility;
641 obst = env->data_obst;
642 if (is_Method_type(type)) {
643 if (get_method_img_section(ent) == section_constructors) {
644 obst = env->ctor_obst;
645 obstack_printf(obst, ".balign\t%d\n", align);
646 dump_size_type(obst, align);
647 obstack_printf(obst, "%s\n", ld_name);
652 variability = get_entity_variability(ent);
653 visibility = get_entity_visibility(ent);
654 if (variability == variability_constant) {
655 /* a constant entity, put it on the rdata */
656 obst = env->rodata_obst;
657 } else if (variability == variability_uninitialized) {
658 /* uninitialized entity put it in bss segment */
659 obst = env->bss_obst;
660 if (emit_commons && visibility != visibility_local)
664 be_dbg_variable(env->main_env->db_handle, obst, ent);
666 /* global or not global */
667 if (visibility == visibility_external_visible && !emit_as_common) {
668 obstack_printf(obst, ".global\t%s\n", ld_name);
669 } else if(visibility == visibility_external_allocated) {
670 obstack_printf(obst, ".global\t%s\n", ld_name);
671 /* we can return now... */
675 if (align > 1 && !emit_as_common) {
676 obstack_printf(obst, ".balign\t%d\n", align);
679 if (!emit_as_common) {
680 obstack_printf(obst, "%s:\n", ld_name);
683 if (variability == variability_uninitialized) {
685 if (be_gas_flavour == GAS_FLAVOUR_NORMAL)
686 obstack_printf(obst, "\t.comm %s,%d,%d\n",
687 ld_name, get_type_size_bytes(type), align);
689 obstack_printf(obst, "\t.comm %s,%d # %d\n",
690 ld_name, get_type_size_bytes(type), align);
692 obstack_printf(obst, "\t.zero %d\n", get_type_size_bytes(type));
695 if (is_atomic_entity(ent)) {
696 dump_atomic_init(env, obst, get_atomic_ent_value(ent));
698 /* sort_compound_ent_values(ent); */
700 switch (get_type_tpop_code(get_entity_type(ent))) {
702 if (ent_is_string_const(ent))
703 dump_string_cst(obst, ent);
705 dump_compound_init(env, obst, ent);
710 dump_compound_init(env, obst, ent);
720 * Dumps declarations of global variables and the initialization code.
722 * @param gt a global like type, either the global or the TLS one
723 * @param env an environment
724 * @param emit_commons if non-zero, emit commons (non-local uninitialized entities)
725 * @param only_emit_marked if non-zero, external allocated entities that do not have
726 * its visited flag set are ignored
728 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
729 int emit_commons, int only_emit_marked)
731 int i, n = get_compound_n_members(gt);
732 waitq *worklist = new_waitq();
734 if (only_emit_marked) {
735 for (i = 0; i < n; i++) {
736 ir_entity *ent = get_compound_member(gt, i);
737 if (is_entity_backend_marked(ent) ||
738 get_entity_visibility(ent) != visibility_external_allocated) {
739 waitq_put(worklist, ent);
740 set_entity_backend_marked(ent, 1);
744 for (i = 0; i < n; i++) {
745 ir_entity *ent = get_compound_member(gt, i);
746 set_entity_backend_marked(ent, 1);
747 waitq_put(worklist, ent);
751 env->worklist = worklist;
753 while (!waitq_empty(worklist)) {
754 ir_entity *ent = waitq_get(worklist);
756 dump_global(env, ent, emit_commons);
760 env->worklist = NULL;
763 /************************************************************************/
765 /* Generate all entities. */
766 void be_gas_emit_decls(be_emit_env_t *emit, const be_main_env_t *main_env,
767 int only_emit_marked_entities)
769 be_gas_decl_env_t env;
770 obstack_t rodata, data, bss, ctor;
774 /* dump the global type */
775 obstack_init(&rodata);
780 env.rodata_obst = &rodata;
781 env.data_obst = &data;
783 env.ctor_obst = &ctor;
784 env.main_env = main_env;
786 be_gas_dump_globals(get_glob_type(), &env, 1, only_emit_marked_entities);
788 size = obstack_object_size(&data);
789 cp = obstack_finish(&data);
791 be_gas_emit_switch_section(emit, GAS_SECTION_DATA);
792 be_emit_string_len(emit, cp, size);
793 be_emit_write_line(emit);
796 size = obstack_object_size(&rodata);
797 cp = obstack_finish(&rodata);
799 be_gas_emit_switch_section(emit, GAS_SECTION_RODATA);
800 be_emit_string_len(emit, cp, size);
801 be_emit_write_line(emit);
804 size = obstack_object_size(&bss);
805 cp = obstack_finish(&bss);
807 be_gas_emit_switch_section(emit, GAS_SECTION_COMMON);
808 be_emit_string_len(emit, cp, size);
809 be_emit_write_line(emit);
812 size = obstack_object_size(&ctor);
813 cp = obstack_finish(&ctor);
815 be_gas_emit_switch_section(emit, GAS_SECTION_CTOR);
816 be_emit_string_len(emit, cp, size);
817 be_emit_write_line(emit);
820 obstack_free(&rodata, NULL);
821 obstack_free(&data, NULL);
822 obstack_free(&bss, NULL);
823 obstack_free(&ctor, NULL);
825 /* dump the Thread Local Storage */
828 env.rodata_obst = &data;
829 env.data_obst = &data;
830 env.bss_obst = &data;
831 env.ctor_obst = NULL;
833 be_gas_dump_globals(get_tls_type(), &env, 0, only_emit_marked_entities);
835 size = obstack_object_size(&data);
836 cp = obstack_finish(&data);
838 be_gas_emit_switch_section(emit, GAS_SECTION_TLS);
839 be_emit_cstring(emit, ".balign\t32\n");
840 be_emit_write_line(emit);
841 be_emit_string_len(emit, cp, size);
842 be_emit_write_line(emit);
845 obstack_free(&data, NULL);