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
47 #include "beemitter.h"
48 #include "be_dbgout.h"
50 typedef struct obstack obstack_t;
52 /** by default, we generate assembler code for the Linux gas */
53 be_gas_flavour_t be_gas_flavour = GAS_FLAVOUR_NORMAL;
56 * Return the pseudo-instruction to be issued for a section switch
57 * depending on the current flavour.
59 * @param section the section to switch to
61 * @return the pseudo-instruction
63 static const char *get_section_name(be_gas_section_t section) {
64 static const char *text[GAS_FLAVOUR_MAX][GAS_SECTION_MAX] = {
70 ".section\t.tbss,\"awT\",@nobits",
71 ".section\t.ctors,\"aw\",@progbits"
76 ".section .rdata,\"dr\"",
78 ".section\t.tbss,\"awT\",@nobits",
79 ".section\t.ctors,\"aw\",@progbits"
83 assert(be_gas_flavour >= 0 && be_gas_flavour < GAS_FLAVOUR_MAX);
84 assert(section >= 0 && section < GAS_SECTION_MAX);
85 return text[be_gas_flavour][section];
89 * Emit necessary code to switch to a section.
91 * @param env the emitter environment
92 * @param section the section to switch to
94 void be_gas_emit_switch_section(be_emit_env_t *env, be_gas_section_t section) {
95 be_emit_char(env, '\t');
96 be_emit_string(env, get_section_name(section));
97 be_emit_char(env, '\n');
98 be_emit_write_line(env);
102 * An environment containing all needed dumper data.
103 * Currently we create the file completely in memory first, then
104 * write it to the disk. This is an artifact from the old C-generating backend
105 * and even there NOT needed. So we might change it in the future.
107 typedef struct _be_gas_decl_env {
108 obstack_t *rodata_obst; /**< An obstack that will be filled with all rodata entities. */
109 obstack_t *data_obst; /**< An obstack that will be filled with the initialized entities. */
110 obstack_t *bss_obst; /**< An obstack that will be filled with the uninitialized entities. */
111 obstack_t *ctor_obst; /**< An obstack that will be filled with the constructor entities. */
112 const be_main_env_t *main_env; /**< The main backend environment, used for it's debug handle. */
113 waitq *worklist; /**< A worklist we use to place not yet handled entities on. */
116 /************************************************************************/
121 * @param obst the obstack where the data is written too
122 * @param tv the tarval
123 * @param bytes the width of the tarvals value in bytes
125 static void dump_arith_tarval(obstack_t *obst, tarval *tv, int bytes)
130 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
134 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
138 obstack_printf(obst, "0x%02x%02x%02x%02x",
139 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
143 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
144 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
145 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
153 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x"
154 "%02x%02x%02x%02x%02x%02x%02x%02x",
155 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
156 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
157 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
158 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
159 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
160 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
161 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
162 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
167 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
173 * Return the tarval of an atomic initializer.
175 * @param init a node representing the initializer (on teh const code irg)
179 static tarval *get_atomic_init_tv(ir_node *init)
182 ir_mode *mode = get_irn_mode(init);
184 switch (get_irn_opcode(init)) {
187 init = get_Cast_op(init);
191 init = get_Conv_op(init);
195 return get_Const_tarval(init);
198 switch (get_SymConst_kind(init)) {
199 case symconst_type_size:
200 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
202 case symconst_type_align:
203 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
205 case symconst_ofs_ent:
206 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
208 case symconst_enum_const:
209 return get_enumeration_value(get_SymConst_enum(init));
222 * Dump an atomic value.
224 * @param env the gas output environment
225 * @param obst an obstack the output is written to
226 * @param init a node representing the atomic value (on the const code irg)
228 static void do_dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
231 ir_mode *mode = get_irn_mode(init);
232 int bytes = get_mode_size_bytes(mode);
236 switch (get_irn_opcode(init)) {
239 do_dump_atomic_init(env, obst, get_Cast_op(init));
243 do_dump_atomic_init(env, obst, get_Conv_op(init));
247 tv = get_Const_tarval(init);
249 /* it's a arithmetic value */
250 dump_arith_tarval(obst, tv, bytes);
254 switch (get_SymConst_kind(init)) {
255 case symconst_addr_name:
256 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
259 case symconst_addr_ent:
260 ent = get_SymConst_entity(init);
261 if(!entity_visited(ent)) {
262 waitq_put(env->worklist, ent);
263 mark_entity_visited(ent);
265 obstack_printf(obst, "%s", get_entity_ld_name(ent));
268 case symconst_ofs_ent:
269 ent = get_SymConst_entity(init);
270 #if 0 /* not needed, is it? */
271 if(!entity_visited(ent)) {
272 waitq_put(env->worklist, ent);
273 mark_entity_visited(ent);
276 obstack_printf(obst, "%d", get_entity_offset(ent));
279 case symconst_type_size:
280 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
283 case symconst_type_align:
284 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
287 case symconst_enum_const:
288 tv = get_enumeration_value(get_SymConst_enum(init));
289 dump_arith_tarval(obst, tv, bytes);
293 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
298 do_dump_atomic_init(env, obst, get_Add_left(init));
299 obstack_printf(obst, " + ");
300 do_dump_atomic_init(env, obst, get_Add_right(init));
304 do_dump_atomic_init(env, obst, get_Sub_left(init));
305 obstack_printf(obst, " - ");
306 do_dump_atomic_init(env, obst, get_Sub_right(init));
310 do_dump_atomic_init(env, obst, get_Mul_left(init));
311 obstack_printf(obst, " * ");
312 do_dump_atomic_init(env, obst, get_Mul_right(init));
316 assert(0 && "dump_atomic_init(): unknown IR-node");
321 * Dumps the type for given size (.byte, .long, ...)
323 * @param obst an obstack the output is written to
324 * @param size the size in bytes
326 static void dump_size_type(obstack_t *obst, int size) {
330 obstack_printf(obst, "\t.byte\t");
334 obstack_printf(obst, "\t.value\t");
338 obstack_printf(obst, "\t.long\t");
342 obstack_printf(obst, "\t.quad\t");
347 /* handled in arith */
351 obstack_printf(obst, "\t.octa\t");
355 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
361 * Dump an atomic value to an obstack.
363 * @param env the gas output environment
364 * @param obst an obstack the output is written to
365 * @param init a node representing the atomic value (on the const code irg)
367 static void dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
370 ir_mode *mode = get_irn_mode(init);
371 int bytes = get_mode_size_bytes(mode);
373 dump_size_type(obst, bytes);
374 do_dump_atomic_init(env, obst, init);
375 obstack_printf(obst, "\n");
378 /************************************************************************/
379 /* Routines to dump global variables */
380 /************************************************************************/
383 * Determine if an entity is a string constant
384 * @param ent The entity
385 * @return 1 if it is a string constant, 0 otherwise
387 static int ent_is_string_const(ir_entity *ent)
389 ir_type *type, *element_type;
393 type = get_entity_type(ent);
395 /* if it's an array */
396 if (!is_Array_type(type))
399 element_type = get_array_element_type(type);
401 /* and the array's element type is primitive */
402 if (!is_Primitive_type(element_type))
405 /* and the mode of the element type is an int of
406 * the same size as the byte mode */
407 mode = get_type_mode(element_type);
408 if (!mode_is_int(mode)
409 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
412 /* if it contains only printable chars and a 0 at the end */
413 n = get_compound_ent_n_values(ent);
414 for (i = 0; i < n; ++i) {
415 ir_node *irn = get_compound_ent_value(ent, i);
416 if(get_irn_opcode(irn) != iro_Const)
419 c = (int) get_tarval_long(get_Const_tarval(irn));
421 if((i < n - 1 && !(isgraph(c) || isspace(c)))
422 || (i == n - 1 && c != '\0'))
426 /* then we can emit it as a string constant */
431 * Dump a string constant.
432 * No checks are made!!
434 * @param obst The obst to dump on.
435 * @param ent The entity to dump.
437 static void dump_string_cst(obstack_t *obst, ir_entity *ent)
441 obstack_printf(obst, "\t.string \"");
442 n = get_compound_ent_n_values(ent);
444 for (i = 0; i < n-1; ++i) {
448 irn = get_compound_ent_value(ent, i);
449 c = (int) get_tarval_long(get_Const_tarval(irn));
452 case '"' : obstack_printf(obst, "\\\""); break;
453 case '\n': obstack_printf(obst, "\\n"); break;
454 case '\r': obstack_printf(obst, "\\r"); break;
455 case '\t': obstack_printf(obst, "\\t"); break;
456 case '\\': obstack_printf(obst, "\\\\"); break;
459 obstack_printf(obst, "%c", c);
461 obstack_printf(obst, "\\%o", c);
465 obstack_printf(obst, "\"\n");
468 static void dump_array_init(be_gas_decl_env_t *env, obstack_t *obst,
471 const ir_type *ty = get_entity_type(ent);
476 /* potential spare values should be already included! */
477 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
478 ir_entity *step = get_compound_ent_value_member(ent, i);
479 ir_type *stype = get_entity_type(step);
481 if (get_type_mode(stype)) {
482 int align = (get_type_alignment_bits(stype) + 7) >> 3;
483 int n = size % align;
486 obstack_printf(obst, "\t.zero\t%d\n", align - n);
490 dump_atomic_init(env, obst, get_compound_ent_value(ent, i));
491 size += get_type_size_bytes(stype);
493 filler = get_type_size_bytes(ty) - size;
496 obstack_printf(obst, "\t.skip\t%d\n", filler);
499 enum normal_or_bitfield_kind {
505 enum normal_or_bitfield_kind kind;
508 unsigned char bf_val;
510 } normal_or_bitfield;
513 * Dump an initializer for a compound entity.
515 static void dump_compound_init(be_gas_decl_env_t *env, obstack_t *obst,
518 normal_or_bitfield *vals;
519 int i, j, n = get_compound_ent_n_values(ent);
522 /* Find the initializer size. Sorrily gcc support a nasty feature:
523 The last field of a compound may be a flexible array. This allows
524 initializers bigger than the type size. */
525 last_ofs = get_type_size_bytes(get_entity_type(ent));
526 for (i = 0; i < n; ++i) {
527 int offset = get_compound_ent_value_offset_bytes(ent, i);
528 int bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
529 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
530 int path_len = get_compound_graph_path_length(path);
531 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
532 int value_len = get_type_size_bits(get_entity_type(last_ent));
534 offset += (value_len + bits_remainder + 7) >> 3;
536 if (offset > last_ofs) {
542 * In the worst case, every initializer allocates one byte.
543 * Moreover, initializer might be big, do not allocate on stack.
545 vals = xcalloc(last_ofs, sizeof(vals[0]));
547 /* collect the values and store them at the offsets */
548 for (i = 0; i < n; ++i) {
549 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
550 int path_len = get_compound_graph_path_length(path);
551 int offset = get_compound_ent_value_offset_bytes(ent, i);
552 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
553 ir_node *value = get_compound_ent_value(ent, i);
554 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
555 int value_len = get_type_size_bits(get_entity_type(last_ent));
557 assert(offset_bits >= 0);
559 if (offset_bits != 0 ||
560 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
561 tarval *shift, *shifted;
562 tarval *tv = get_atomic_init_tv(value);
564 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
565 get_entity_ld_name(ent));
567 tv = tarval_convert_to(tv, mode_Lu);
568 shift = new_tarval_from_long(offset_bits, mode_Is);
569 shifted = tarval_shl(tv, shift);
570 if (shifted == tarval_bad || shifted == tarval_undefined) {
571 panic("Couldn't shift numeric value for bitfield initializer '%s'\n",
572 get_entity_ld_name(ent));
575 for (j = 0; value_len > 0; ++j) {
576 assert(offset + j < last_ofs);
577 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
578 vals[offset + j].kind = BITFIELD;
579 vals[offset + j].v.bf_val |= get_tarval_sub_bits(shifted, j);
580 value_len -= 8 - offset_bits;
584 assert(offset < last_ofs);
585 assert(vals[offset].kind == NORMAL);
586 assert(vals[offset].v.value == NULL);
587 vals[offset].v.value = value;
591 /* now write them sorted */
592 for (i = 0; i < last_ofs; ) {
593 int space = 0, skip = 0;
594 if (vals[i].kind == NORMAL) {
595 if(vals[i].v.value != NULL) {
596 dump_atomic_init(env, obst, vals[i].v.value);
597 skip = get_mode_size_bytes(get_irn_mode(vals[i].v.value)) - 1;
602 assert(vals[i].kind == BITFIELD);
603 obstack_printf(obst, "\t.byte\t%d\n", vals[i].v.bf_val);
608 while (i < last_ofs && vals[i].kind == NORMAL && vals[i].v.value == NULL) {
617 obstack_printf(obst, "\t.skip\t%d\n", space);
623 * Dump a global entity.
625 * @param env the gas output environment
626 * @param ent the entity to be dumped
627 * @param emit_commons if non-zero, emit commons (non-local uninitialized entities)
629 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent, int emit_commons)
632 ir_type *type = get_entity_type(ent);
633 const char *ld_name = get_entity_ld_name(ent);
634 ir_variability variability = get_entity_variability(ent);
635 ir_visibility visibility = get_entity_visibility(ent);
636 int align = get_type_alignment_bytes(type);
637 int emit_as_common = 0;
639 obst = env->data_obst;
640 if (is_Method_type(type)) {
641 if (get_method_img_section(ent) == section_constructors) {
642 obst = env->ctor_obst;
643 obstack_printf(obst, ".balign\t%d\n", align);
644 dump_size_type(obst, align);
645 obstack_printf(obst, "%s\n", ld_name);
648 } else if (variability == variability_constant) {
649 /* a constant entity, put it on the rdata */
650 obst = env->rodata_obst;
651 } else if (variability == variability_uninitialized) {
652 /* uninitialized entity put it in bss segment */
653 obst = env->bss_obst;
654 if(emit_commons && visibility != visibility_local)
658 be_dbg_variable(env->main_env->db_handle, obst, ent);
660 /* global or not global */
661 if (visibility == visibility_external_visible && !emit_as_common) {
662 obstack_printf(obst, ".global\t%s\n", ld_name);
663 } else if(visibility == visibility_external_allocated) {
664 obstack_printf(obst, ".global\t%s\n", ld_name);
665 /* we can return now... */
669 if (align > 1 && !emit_as_common) {
670 obstack_printf(obst, ".balign\t%d\n", align);
673 if (!emit_as_common) {
674 obstack_printf(obst, "%s:\n", ld_name);
677 if (variability == variability_uninitialized) {
679 if (be_gas_flavour == GAS_FLAVOUR_NORMAL)
680 obstack_printf(obst, "\t.comm %s,%d,%d\n",
681 ld_name, get_type_size_bytes(type), align);
683 obstack_printf(obst, "\t.comm %s,%d # %d\n",
684 ld_name, get_type_size_bytes(type), align);
686 obstack_printf(obst, "\t.zero %d\n", get_type_size_bytes(type));
688 } else if (is_atomic_type(type)) {
689 dump_atomic_init(env, obst, get_atomic_ent_value(ent));
690 } else if (ent_is_string_const(ent)) {
691 dump_string_cst(obst, ent);
692 } else if (is_Array_type(type)) {
693 dump_array_init(env, obst, ent);
694 } else if (is_compound_type(type)) {
695 dump_compound_init(env, obst, ent);
697 assert(0 && "unsupported type");
702 * Dumps declarations of global variables and the initialization code.
704 * @param gt a global like type, either the global or the TLS one
705 * @param env an environment
706 * @param emit_commons if non-zero, emit commons (non-local uninitialized entities)
707 * @param only_emit_marked if non-zero, external allocated entities that do not have
708 * its visited flag set are ignored
710 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
711 int emit_commons, int only_emit_marked)
713 int i, n = get_compound_n_members(gt);
714 waitq *worklist = new_waitq();
716 if(only_emit_marked) {
717 for (i = 0; i < n; i++) {
718 ir_entity *ent = get_compound_member(gt, i);
719 if(entity_visited(ent) ||
720 get_entity_visibility(ent) != visibility_external_allocated) {
721 waitq_put(worklist, ent);
722 mark_entity_visited(ent);
726 inc_master_type_visited();
727 for (i = 0; i < n; i++) {
728 ir_entity *ent = get_compound_member(gt, i);
729 mark_entity_visited(ent);
730 waitq_put(worklist, ent);
734 env->worklist = worklist;
736 while(!waitq_empty(worklist)) {
737 ir_entity *ent = waitq_get(worklist);
739 dump_global(env, ent, emit_commons);
743 env->worklist = NULL;
746 /************************************************************************/
748 /* Generate all entities. */
749 void be_gas_emit_decls(be_emit_env_t *emit, const be_main_env_t *main_env,
750 int only_emit_marked_entities)
752 be_gas_decl_env_t env;
753 obstack_t rodata, data, bss, ctor;
757 /* dump the global type */
758 obstack_init(&rodata);
763 env.rodata_obst = &rodata;
764 env.data_obst = &data;
766 env.ctor_obst = &ctor;
767 env.main_env = main_env;
769 be_gas_dump_globals(get_glob_type(), &env, 1, only_emit_marked_entities);
771 size = obstack_object_size(&data);
772 cp = obstack_finish(&data);
774 be_gas_emit_switch_section(emit, GAS_SECTION_DATA);
775 be_emit_string_len(emit, cp, size);
776 be_emit_write_line(emit);
779 size = obstack_object_size(&rodata);
780 cp = obstack_finish(&rodata);
782 be_gas_emit_switch_section(emit, GAS_SECTION_RODATA);
783 be_emit_string_len(emit, cp, size);
784 be_emit_write_line(emit);
787 size = obstack_object_size(&bss);
788 cp = obstack_finish(&bss);
790 be_gas_emit_switch_section(emit, GAS_SECTION_COMMON);
791 be_emit_string_len(emit, cp, size);
792 be_emit_write_line(emit);
795 size = obstack_object_size(&ctor);
796 cp = obstack_finish(&ctor);
798 be_gas_emit_switch_section(emit, GAS_SECTION_CTOR);
799 be_emit_string_len(emit, cp, size);
800 be_emit_write_line(emit);
803 obstack_free(&rodata, NULL);
804 obstack_free(&data, NULL);
805 obstack_free(&bss, NULL);
806 obstack_free(&ctor, NULL);
808 /* dump the Thread Local Storage */
811 env.rodata_obst = &data;
812 env.data_obst = &data;
813 env.bss_obst = &data;
814 env.ctor_obst = NULL;
816 be_gas_dump_globals(get_tls_type(), &env, 0, only_emit_marked_entities);
818 size = obstack_object_size(&data);
819 cp = obstack_finish(&data);
821 be_gas_emit_switch_section(emit, GAS_SECTION_TLS);
822 be_emit_cstring(emit, ".balign\t32\n");
823 be_emit_write_line(emit);
824 be_emit_string_len(emit, cp, size);
825 be_emit_write_line(emit);
828 obstack_free(&data, NULL);