2 * Copyright (C) 1995-2008 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"
85 ".section\t.tbss,\"awT\",@nobits",
86 ".section\t.ctors,\"aw\",@progbits"
90 assert((int) be_gas_flavour >= 0 && be_gas_flavour < GAS_FLAVOUR_MAX);
91 assert((int) section >= 0 && section < GAS_SECTION_MAX);
92 return text[be_gas_flavour][section];
96 * Emit necessary code to switch to a section.
98 * @param env the emitter environment
99 * @param section the section to switch to
101 void be_gas_emit_switch_section(be_gas_section_t section) {
103 be_emit_string(get_section_name(section));
105 be_emit_write_line();
109 * An environment containing all needed dumper data.
110 * Currently we create the file completely in memory first, then
111 * write it to the disk. This is an artifact from the old C-generating backend
112 * and even there NOT needed. So we might change it in the future.
114 typedef struct _be_gas_decl_env {
115 obstack_t *rodata_obst; /**< An obstack that will be filled with all rodata entities. */
116 obstack_t *data_obst; /**< An obstack that will be filled with the initialized entities. */
117 obstack_t *bss_obst; /**< An obstack that will be filled with the uninitialized entities. */
118 obstack_t *ctor_obst; /**< An obstack that will be filled with the constructor entities. */
119 const be_main_env_t *main_env; /**< The main backend environment, used for it's debug handle. */
120 waitq *worklist; /**< A worklist we use to place not yet handled entities on. */
123 /************************************************************************/
128 * @param obst the obstack where the data is written too
129 * @param tv the tarval
130 * @param bytes the width of the tarvals value in bytes
132 static void dump_arith_tarval(obstack_t *obst, tarval *tv, int bytes)
137 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
141 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
145 obstack_printf(obst, "0x%02x%02x%02x%02x",
146 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
150 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
151 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
152 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
160 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x"
161 "%02x%02x%02x%02x%02x%02x%02x%02x",
162 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
163 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
164 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
165 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
166 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
167 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
168 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
169 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
174 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
180 * Return the label prefix for labeled blocks.
182 const char *be_gas_label_prefix(void) {
189 static void dump_label(obstack_t *obst, ir_label_t label) {
190 obstack_printf(obst, "%s%ld", be_gas_label_prefix(), label);
194 * Return the tarval of an atomic initializer.
196 * @param init a node representing the initializer (on the const code irg)
200 static tarval *get_atomic_init_tv(ir_node *init)
203 ir_mode *mode = get_irn_mode(init);
205 switch (get_irn_opcode(init)) {
208 init = get_Cast_op(init);
212 init = get_Conv_op(init);
216 return get_Const_tarval(init);
219 switch (get_SymConst_kind(init)) {
220 case symconst_type_size:
221 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
223 case symconst_type_align:
224 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
226 case symconst_ofs_ent:
227 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
229 case symconst_enum_const:
230 return get_enumeration_value(get_SymConst_enum(init));
246 * Dump an atomic value.
248 * @param env the gas output environment
249 * @param obst an obstack the output is written to
250 * @param init a node representing the atomic value (on the const code irg)
252 static void do_dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
255 ir_mode *mode = get_irn_mode(init);
256 int bytes = get_mode_size_bytes(mode);
261 switch (get_irn_opcode(init)) {
264 do_dump_atomic_init(env, obst, get_Cast_op(init));
268 do_dump_atomic_init(env, obst, get_Conv_op(init));
272 tv = get_Const_tarval(init);
274 /* it's a arithmetic value */
275 dump_arith_tarval(obst, tv, bytes);
279 switch (get_SymConst_kind(init)) {
280 case symconst_addr_name:
281 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
284 case symconst_addr_ent:
285 ent = get_SymConst_entity(init);
286 if(!is_entity_backend_marked(ent)) {
287 waitq_put(env->worklist, ent);
288 set_entity_backend_marked(ent, 1);
290 obstack_printf(obst, "%s", get_entity_ld_name(ent));
293 case symconst_ofs_ent:
294 ent = get_SymConst_entity(init);
295 #if 0 /* not needed, is it? */
296 if(!is_entity_backend_marked(ent)) {
297 waitq_put(env->worklist, ent);
298 set_entity_backend_marked(ent, 1);
301 obstack_printf(obst, "%d", get_entity_offset(ent));
304 case symconst_type_size:
305 obstack_printf(obst, "%u", get_type_size_bytes(get_SymConst_type(init)));
308 case symconst_type_align:
309 obstack_printf(obst, "%u", get_type_alignment_bytes(get_SymConst_type(init)));
312 case symconst_enum_const:
313 tv = get_enumeration_value(get_SymConst_enum(init));
314 dump_arith_tarval(obst, tv, bytes);
318 label = get_SymConst_label(init);
319 dump_label(obst, label);
323 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
328 do_dump_atomic_init(env, obst, get_Add_left(init));
329 obstack_printf(obst, " + ");
330 do_dump_atomic_init(env, obst, get_Add_right(init));
334 do_dump_atomic_init(env, obst, get_Sub_left(init));
335 obstack_printf(obst, " - ");
336 do_dump_atomic_init(env, obst, get_Sub_right(init));
340 do_dump_atomic_init(env, obst, get_Mul_left(init));
341 obstack_printf(obst, " * ");
342 do_dump_atomic_init(env, obst, get_Mul_right(init));
346 assert(0 && "dump_atomic_init(): unknown IR-node");
351 * Dumps the type for given size (.byte, .long, ...)
353 * @param obst an obstack the output is written to
354 * @param size the size in bytes
356 static void dump_size_type(obstack_t *obst, int size) {
360 obstack_printf(obst, "\t.byte\t");
364 obstack_printf(obst, "\t.value\t");
368 obstack_printf(obst, "\t.long\t");
372 obstack_printf(obst, "\t.quad\t");
377 /* handled in arith */
381 obstack_printf(obst, "\t.octa\t");
385 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
391 * Dump an atomic value to an obstack.
393 * @param env the gas output environment
394 * @param obst an obstack the output is written to
395 * @param init a node representing the atomic value (on the const code irg)
397 static void dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
400 ir_mode *mode = get_irn_mode(init);
401 int bytes = get_mode_size_bytes(mode);
403 dump_size_type(obst, bytes);
404 do_dump_atomic_init(env, obst, init);
405 obstack_printf(obst, "\n");
408 /************************************************************************/
409 /* Routines to dump global variables */
410 /************************************************************************/
413 * Determine if an entity is a string constant
414 * @param ent The entity
415 * @return 1 if it is a string constant, 0 otherwise
417 static int ent_is_string_const(ir_entity *ent)
419 ir_type *type, *element_type;
423 type = get_entity_type(ent);
425 /* if it's an array */
426 if (!is_Array_type(type))
429 element_type = get_array_element_type(type);
431 /* and the array's element type is primitive */
432 if (!is_Primitive_type(element_type))
435 /* and the mode of the element type is an int of
436 * the same size as the byte mode */
437 mode = get_type_mode(element_type);
438 if (!mode_is_int(mode)
439 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
442 /* if it contains only printable chars and a 0 at the end */
443 n = get_compound_ent_n_values(ent);
444 for (i = 0; i < n; ++i) {
445 ir_node *irn = get_compound_ent_value(ent, i);
449 c = (int) get_tarval_long(get_Const_tarval(irn));
451 if((i < n - 1 && !(isgraph(c) || isspace(c)))
452 || (i == n - 1 && c != '\0'))
456 /* then we can emit it as a string constant */
461 * Dump a string constant.
462 * No checks are made!!
464 * @param obst The obst to dump on.
465 * @param ent The entity to dump.
467 static void dump_string_cst(obstack_t *obst, ir_entity *ent)
474 obstack_printf(obst, "\t.string \"");
475 n = get_compound_ent_n_values(ent);
477 for (i = 0; i < n-1; ++i) {
481 irn = get_compound_ent_value(ent, i);
482 c = (int) get_tarval_long(get_Const_tarval(irn));
485 case '"' : obstack_printf(obst, "\\\""); break;
486 case '\n': obstack_printf(obst, "\\n"); break;
487 case '\r': obstack_printf(obst, "\\r"); break;
488 case '\t': obstack_printf(obst, "\\t"); break;
489 case '\\': obstack_printf(obst, "\\\\"); break;
492 obstack_printf(obst, "%c", c);
494 obstack_printf(obst, "\\%o", c);
498 obstack_printf(obst, "\"\n");
500 type = get_entity_type(ent);
501 type_size = get_type_size_bytes(type);
502 remaining_space = type_size - n;
503 assert(remaining_space >= 0);
504 if(remaining_space > 0) {
505 obstack_printf(obst, "\t.skip\t%d\n", remaining_space);
509 enum normal_or_bitfield_kind {
515 enum normal_or_bitfield_kind kind;
518 unsigned char bf_val;
520 } normal_or_bitfield;
523 * Dump an initializer for a compound entity.
525 static void dump_compound_init(be_gas_decl_env_t *env, obstack_t *obst,
528 normal_or_bitfield *vals;
529 int i, j, n = get_compound_ent_n_values(ent);
530 unsigned k, last_ofs;
532 /* Find the initializer size. Sorrily gcc support a nasty feature:
533 The last field of a compound may be a flexible array. This allows
534 initializers bigger than the type size. */
535 last_ofs = get_type_size_bytes(get_entity_type(ent));
536 for (i = 0; i < n; ++i) {
537 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
538 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
539 ir_node *value = get_compound_ent_value(ent, i);
540 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
542 offset += (value_len + bits_remainder + 7) >> 3;
544 if (offset > last_ofs) {
550 * In the worst case, every initializer allocates one byte.
551 * Moreover, initializer might be big, do not allocate on stack.
553 vals = xcalloc(last_ofs, sizeof(vals[0]));
555 /* collect the values and store them at the offsets */
556 for (i = 0; i < n; ++i) {
557 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
558 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
559 ir_node *value = get_compound_ent_value(ent, i);
560 int value_len = get_mode_size_bits(get_irn_mode(value));
562 assert(offset_bits >= 0);
564 if (offset_bits != 0 ||
565 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
566 tarval *tv = get_atomic_init_tv(value);
567 unsigned char curr_bits, last_bits = 0;
569 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
570 get_entity_ld_name(ent));
572 /* normalize offset */
573 offset += offset_bits >> 3;
576 for (j = 0; value_len + offset_bits > 0; ++j) {
577 assert(offset + j < last_ofs);
578 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
579 vals[offset + j].kind = BITFIELD;
580 curr_bits = get_tarval_sub_bits(tv, j);
581 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
583 last_bits = curr_bits;
588 assert(offset < last_ofs);
589 assert(vals[offset].kind == NORMAL);
590 for (i = 1; i < value_len / 8; ++i) {
591 assert(vals[offset + i].v.value == NULL);
593 vals[offset].v.value = value;
597 /* now write them sorted */
598 for (k = 0; k < last_ofs; ) {
599 int space = 0, skip = 0;
600 if (vals[k].kind == NORMAL) {
601 if(vals[k].v.value != NULL) {
602 dump_atomic_init(env, obst, vals[k].v.value);
603 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
608 assert(vals[k].kind == BITFIELD);
609 obstack_printf(obst, "\t.byte\t%d\n", vals[k].v.bf_val);
613 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
622 obstack_printf(obst, "\t.skip\t%d\n", space);
628 * Dump a global entity.
630 * @param env the gas output environment
631 * @param ent the entity to be dumped
632 * @param emit_commons if non-zero, emit commons (non-local uninitialized entities)
634 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent, int emit_commons)
637 ir_type *type = get_entity_type(ent);
638 const char *ld_name = get_entity_ld_name(ent);
639 unsigned align = get_type_alignment_bytes(type);
640 int emit_as_common = 0;
641 ir_variability variability;
642 ir_visibility visibility;
644 obst = env->data_obst;
645 if (is_Method_type(type)) {
646 if (get_method_img_section(ent) == section_constructors) {
647 obst = env->ctor_obst;
648 obstack_printf(obst, ".balign\t%u\n", align);
649 dump_size_type(obst, align);
650 obstack_printf(obst, "%s\n", ld_name);
655 variability = get_entity_variability(ent);
656 visibility = get_entity_visibility(ent);
657 if (variability == variability_constant) {
658 /* a constant entity, put it on the rdata */
659 obst = env->rodata_obst;
660 } else if (variability == variability_uninitialized) {
661 /* uninitialized entity put it in bss segment */
662 obst = env->bss_obst;
663 if (emit_commons && visibility != visibility_local)
667 be_dbg_variable(env->main_env->db_handle, obst, ent);
669 /* global or not global */
670 if (visibility == visibility_external_visible && !emit_as_common) {
671 obstack_printf(obst, ".global\t%s\n", ld_name);
672 } else if(visibility == visibility_external_allocated) {
673 obstack_printf(obst, ".global\t%s\n", ld_name);
674 /* we can return now... */
678 if (align > 1 && !emit_as_common) {
679 obstack_printf(obst, ".balign\t%u\n", align);
682 if (!emit_as_common) {
683 obstack_printf(obst, "%s:\n", ld_name);
686 if (variability == variability_uninitialized) {
687 if (emit_as_common) {
688 switch (be_gas_flavour) {
689 case GAS_FLAVOUR_NORMAL:
690 case GAS_FLAVOUR_YASM:
691 obstack_printf(obst, "\t.comm %s,%u,%u\n",
692 ld_name, get_type_size_bytes(type), align);
694 case GAS_FLAVOUR_MINGW:
695 obstack_printf(obst, "\t.comm %s,%u # %u\n",
696 ld_name, get_type_size_bytes(type), align);
700 obstack_printf(obst, "\t.zero %u\n", get_type_size_bytes(type));
703 if (is_atomic_entity(ent)) {
704 dump_atomic_init(env, obst, get_atomic_ent_value(ent));
706 /* sort_compound_ent_values(ent); */
708 switch (get_type_tpop_code(get_entity_type(ent))) {
710 if (ent_is_string_const(ent))
711 dump_string_cst(obst, ent);
713 dump_compound_init(env, obst, ent);
718 dump_compound_init(env, obst, ent);
728 * Dumps declarations of global variables and the initialization code.
730 * @param gt a global like type, either the global or the TLS one
731 * @param env an environment
732 * @param emit_commons if non-zero, emit commons (non-local uninitialized entities)
733 * @param only_emit_marked if non-zero, external allocated entities that do not have
734 * its visited flag set are ignored
736 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
737 int emit_commons, int only_emit_marked)
739 int i, n = get_compound_n_members(gt);
740 waitq *worklist = new_waitq();
742 if (only_emit_marked) {
743 for (i = 0; i < n; i++) {
744 ir_entity *ent = get_compound_member(gt, i);
745 if (is_entity_backend_marked(ent) ||
746 get_entity_visibility(ent) != visibility_external_allocated) {
747 waitq_put(worklist, ent);
748 set_entity_backend_marked(ent, 1);
752 for (i = 0; i < n; i++) {
753 ir_entity *ent = get_compound_member(gt, i);
754 set_entity_backend_marked(ent, 1);
755 waitq_put(worklist, ent);
759 env->worklist = worklist;
761 while (!waitq_empty(worklist)) {
762 ir_entity *ent = waitq_get(worklist);
764 dump_global(env, ent, emit_commons);
768 env->worklist = NULL;
771 /************************************************************************/
773 /* Generate all entities. */
774 void be_gas_emit_decls(const be_main_env_t *main_env,
775 int only_emit_marked_entities)
777 be_gas_decl_env_t env;
785 /* dump the global type */
786 obstack_init(&rodata);
791 env.rodata_obst = &rodata;
792 env.data_obst = &data;
794 env.ctor_obst = &ctor;
795 env.main_env = main_env;
797 be_gas_dump_globals(get_glob_type(), &env, 1, only_emit_marked_entities);
799 size = obstack_object_size(&data);
800 cp = obstack_finish(&data);
802 be_gas_emit_switch_section(GAS_SECTION_DATA);
803 be_emit_string_len(cp, size);
804 be_emit_write_line();
807 size = obstack_object_size(&rodata);
808 cp = obstack_finish(&rodata);
810 be_gas_emit_switch_section(GAS_SECTION_RODATA);
811 be_emit_string_len(cp, size);
812 be_emit_write_line();
815 size = obstack_object_size(&bss);
816 cp = obstack_finish(&bss);
818 be_gas_emit_switch_section(GAS_SECTION_COMMON);
819 be_emit_string_len(cp, size);
820 be_emit_write_line();
823 size = obstack_object_size(&ctor);
824 cp = obstack_finish(&ctor);
826 be_gas_emit_switch_section(GAS_SECTION_CTOR);
827 be_emit_string_len(cp, size);
828 be_emit_write_line();
831 obstack_free(&rodata, NULL);
832 obstack_free(&data, NULL);
833 obstack_free(&bss, NULL);
834 obstack_free(&ctor, NULL);
836 /* dump the Thread Local Storage */
839 env.rodata_obst = &data;
840 env.data_obst = &data;
841 env.bss_obst = &data;
842 env.ctor_obst = NULL;
844 be_gas_dump_globals(get_tls_type(), &env, 0, only_emit_marked_entities);
846 size = obstack_object_size(&data);
847 cp = obstack_finish(&data);
849 be_gas_emit_switch_section(GAS_SECTION_TLS);
850 be_emit_cstring(".balign\t32\n");
851 be_emit_write_line();
852 be_emit_string_len(cp, size);
853 be_emit_write_line();
856 obstack_free(&data, NULL);