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
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"
86 ".section\t.tbss,\"awT\",@nobits",
87 ".section\t.ctors,\"aw\",@progbits"
91 assert((int) be_gas_flavour >= 0 && be_gas_flavour < GAS_FLAVOUR_MAX);
92 assert((int) section >= 0 && section < GAS_SECTION_MAX);
93 return text[be_gas_flavour][section];
97 * Emit necessary code to switch to a section.
99 * @param env the emitter environment
100 * @param section the section to switch to
102 void be_gas_emit_switch_section(be_gas_section_t section) {
104 be_emit_string(get_section_name(section));
106 be_emit_write_line();
110 * An environment containing all needed dumper data.
111 * Currently we create the file completely in memory first, then
112 * write it to the disk. This is an artifact from the old C-generating backend
113 * and even there NOT needed. So we might change it in the future.
115 typedef struct _be_gas_decl_env {
116 obstack_t *rodata_obst; /**< An obstack that will be filled with all rodata entities. */
117 obstack_t *data_obst; /**< An obstack that will be filled with the initialized entities. */
118 obstack_t *bss_obst; /**< An obstack that will be filled with the uninitialized entities. */
119 obstack_t *ctor_obst; /**< An obstack that will be filled with the constructor entities. */
120 const be_main_env_t *main_env; /**< The main backend environment, used for it's debug handle. */
121 waitq *worklist; /**< A worklist we use to place not yet handled entities on. */
124 /************************************************************************/
129 * @param obst the obstack where the data is written too
130 * @param tv the tarval
131 * @param bytes the width of the tarvals value in bytes
133 static void dump_arith_tarval(obstack_t *obst, tarval *tv, int bytes)
138 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
142 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
146 obstack_printf(obst, "0x%02x%02x%02x%02x",
147 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
151 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
152 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
153 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
161 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x"
162 "%02x%02x%02x%02x%02x%02x%02x%02x",
163 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
164 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
165 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
166 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
167 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
168 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
169 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
170 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
175 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
181 * Return the label prefix for labeled blocks.
183 const char *be_gas_label_prefix(void) {
190 static void dump_label(obstack_t *obst, ir_label_t label) {
191 obstack_printf(obst, "%s%ld", be_gas_label_prefix(), label);
195 * Return the tarval of an atomic initializer.
197 * @param init a node representing the initializer (on the const code irg)
201 static tarval *get_atomic_init_tv(ir_node *init)
204 ir_mode *mode = get_irn_mode(init);
206 switch (get_irn_opcode(init)) {
209 init = get_Cast_op(init);
213 init = get_Conv_op(init);
217 return get_Const_tarval(init);
220 switch (get_SymConst_kind(init)) {
221 case symconst_type_size:
222 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
224 case symconst_type_align:
225 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
227 case symconst_ofs_ent:
228 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
230 case symconst_enum_const:
231 return get_enumeration_value(get_SymConst_enum(init));
247 * Dump an atomic value.
249 * @param env the gas output environment
250 * @param obst an obstack the output is written to
251 * @param init a node representing the atomic value (on the const code irg)
253 static void do_dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
256 ir_mode *mode = get_irn_mode(init);
257 int bytes = get_mode_size_bytes(mode);
262 init = skip_Id(init);
264 switch (get_irn_opcode(init)) {
267 do_dump_atomic_init(env, obst, get_Cast_op(init));
271 do_dump_atomic_init(env, obst, get_Conv_op(init));
275 tv = get_Const_tarval(init);
277 /* it's a arithmetic value */
278 dump_arith_tarval(obst, tv, bytes);
282 switch (get_SymConst_kind(init)) {
283 case symconst_addr_name:
284 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
287 case symconst_addr_ent:
288 ent = get_SymConst_entity(init);
289 if(!is_entity_backend_marked(ent)) {
290 waitq_put(env->worklist, ent);
291 set_entity_backend_marked(ent, 1);
293 obstack_printf(obst, "%s", get_entity_ld_name(ent));
296 case symconst_ofs_ent:
297 ent = get_SymConst_entity(init);
298 #if 0 /* not needed, is it? */
299 if(!is_entity_backend_marked(ent)) {
300 waitq_put(env->worklist, ent);
301 set_entity_backend_marked(ent, 1);
304 obstack_printf(obst, "%d", get_entity_offset(ent));
307 case symconst_type_size:
308 obstack_printf(obst, "%u", get_type_size_bytes(get_SymConst_type(init)));
311 case symconst_type_align:
312 obstack_printf(obst, "%u", get_type_alignment_bytes(get_SymConst_type(init)));
315 case symconst_enum_const:
316 tv = get_enumeration_value(get_SymConst_enum(init));
317 dump_arith_tarval(obst, tv, bytes);
321 label = get_SymConst_label(init);
322 dump_label(obst, label);
326 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
331 do_dump_atomic_init(env, obst, get_Add_left(init));
332 obstack_printf(obst, " + ");
333 do_dump_atomic_init(env, obst, get_Add_right(init));
337 do_dump_atomic_init(env, obst, get_Sub_left(init));
338 obstack_printf(obst, " - ");
339 do_dump_atomic_init(env, obst, get_Sub_right(init));
343 do_dump_atomic_init(env, obst, get_Mul_left(init));
344 obstack_printf(obst, " * ");
345 do_dump_atomic_init(env, obst, get_Mul_right(init));
349 assert(0 && "dump_atomic_init(): unknown IR-node");
354 * Dumps the type for given size (.byte, .long, ...)
356 * @param obst an obstack the output is written to
357 * @param size the size in bytes
359 static void dump_size_type(obstack_t *obst, size_t size) {
363 obstack_printf(obst, "\t.byte\t");
367 obstack_printf(obst, "\t.value\t");
371 obstack_printf(obst, "\t.long\t");
375 obstack_printf(obst, "\t.quad\t");
380 /* handled in arith */
384 obstack_printf(obst, "\t.octa\t");
388 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
394 * Dump an atomic value to an obstack.
396 * @param env the gas output environment
397 * @param obst an obstack the output is written to
398 * @param init a node representing the atomic value (on the const code irg)
400 static void dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
403 ir_mode *mode = get_irn_mode(init);
404 int bytes = get_mode_size_bytes(mode);
406 dump_size_type(obst, bytes);
407 do_dump_atomic_init(env, obst, init);
408 obstack_printf(obst, "\n");
411 /************************************************************************/
412 /* Routines to dump global variables */
413 /************************************************************************/
415 static int initializer_is_string_const(const ir_initializer_t *initializer)
419 if(initializer->kind != IR_INITIALIZER_COMPOUND)
422 len = initializer->compound.n_initializers;
423 for(i = 0; i < len; ++i) {
427 ir_initializer_t *sub_initializer
428 = initializer->compound.initializers[i];
430 if(sub_initializer->kind != IR_INITIALIZER_TARVAL)
433 tv = sub_initializer->tarval.value;
434 mode = get_tarval_mode(tv);
436 if (!mode_is_int(mode)
437 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
440 c = get_tarval_long(tv);
441 if((i < len - 1 && !(isgraph(c) || isspace(c)))
442 || (i >= len - 1 && c != 0))
450 * Determine if an entity is a string constant
451 * @param ent The entity
452 * @return 1 if it is a string constant, 0 otherwise
454 static int ent_is_string_const(ir_entity *ent)
456 ir_type *type, *element_type;
460 type = get_entity_type(ent);
462 /* if it's an array */
463 if (!is_Array_type(type))
466 element_type = get_array_element_type(type);
468 /* and the array's element type is primitive */
469 if (!is_Primitive_type(element_type))
472 /* and the mode of the element type is an int of
473 * the same size as the byte mode */
474 mode = get_type_mode(element_type);
475 if (!mode_is_int(mode)
476 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
479 if(ent->has_initializer) {
483 /* if it contains only printable chars and a 0 at the end */
484 n = get_compound_ent_n_values(ent);
485 for (i = 0; i < n; ++i) {
486 ir_node *irn = get_compound_ent_value(ent, i);
490 c = (int) get_tarval_long(get_Const_tarval(irn));
492 if((i < n - 1 && !(isgraph(c) || isspace(c)))
493 || (i == n - 1 && c != '\0'))
498 /* then we can emit it as a string constant */
503 * Dump a string constant.
504 * No checks are made!!
506 * @param obst The obst to dump on.
507 * @param ent The entity to dump.
509 static void dump_string_cst(obstack_t *obst, ir_entity *ent)
516 obstack_printf(obst, "\t.string \"");
517 n = get_compound_ent_n_values(ent);
519 for (i = 0; i < n-1; ++i) {
523 irn = get_compound_ent_value(ent, i);
524 c = (int) get_tarval_long(get_Const_tarval(irn));
527 case '"' : obstack_printf(obst, "\\\""); break;
528 case '\n': obstack_printf(obst, "\\n"); break;
529 case '\r': obstack_printf(obst, "\\r"); break;
530 case '\t': obstack_printf(obst, "\\t"); break;
531 case '\\': obstack_printf(obst, "\\\\"); break;
534 obstack_printf(obst, "%c", c);
536 obstack_printf(obst, "\\%o", c);
540 obstack_printf(obst, "\"\n");
542 type = get_entity_type(ent);
543 type_size = get_type_size_bytes(type);
544 remaining_space = type_size - n;
545 assert(remaining_space >= 0);
546 if(remaining_space > 0) {
547 obstack_printf(obst, "\t.skip\t%d\n", remaining_space);
551 static void dump_string_initializer(obstack_t *obst,
552 const ir_initializer_t *initializer)
556 obstack_printf(obst, "\t.string \"");
558 len = initializer->compound.n_initializers;
559 for(i = 0; i < len - 1; ++i) {
560 const ir_initializer_t *sub_initializer
561 = get_initializer_compound_value(initializer, i);
563 tarval *tv = get_initializer_tarval_value(sub_initializer);
564 int c = get_tarval_long(tv);
567 case '"' : obstack_printf(obst, "\\\""); break;
568 case '\n': obstack_printf(obst, "\\n"); break;
569 case '\r': obstack_printf(obst, "\\r"); break;
570 case '\t': obstack_printf(obst, "\\t"); break;
571 case '\\': obstack_printf(obst, "\\\\"); break;
574 obstack_printf(obst, "%c", c);
576 obstack_printf(obst, "\\%o", c);
580 obstack_printf(obst, "\"\n");
583 enum normal_or_bitfield_kind {
590 enum normal_or_bitfield_kind kind;
594 unsigned char bf_val;
596 } normal_or_bitfield;
598 static int is_type_variable_size(ir_type *type)
605 static size_t get_initializer_size(const ir_initializer_t *initializer,
608 switch(get_initializer_kind(initializer)) {
609 case IR_INITIALIZER_TARVAL: {
610 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
611 return get_type_size_bytes(type);
613 case IR_INITIALIZER_CONST:
614 case IR_INITIALIZER_NULL:
615 return get_type_size_bytes(type);
616 case IR_INITIALIZER_COMPOUND: {
617 if(!is_type_variable_size(type)) {
618 return get_type_size_bytes(type);
621 = get_initializer_compound_n_entries(initializer);
623 unsigned initializer_size = get_type_size_bytes(type);
624 for(i = 0; i < n_entries; ++i) {
625 ir_entity *entity = get_compound_member(type, i);
626 ir_type *type = get_entity_type(entity);
628 const ir_initializer_t *sub_initializer
629 = get_initializer_compound_value(initializer, i);
631 unsigned offset = get_entity_offset(entity);
632 unsigned size = get_initializer_size(sub_initializer, type);
634 if(offset + size > initializer_size) {
635 initializer_size = offset + size;
638 return initializer_size;
643 panic("found invalid initializer");
647 static normal_or_bitfield *glob_vals;
648 static size_t max_vals;
651 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
652 const ir_initializer_t *initializer, ir_type *type)
654 unsigned char last_bits = 0;
655 ir_mode *mode = get_type_mode(type);
657 unsigned char curr_bits;
661 switch(get_initializer_kind(initializer)) {
662 case IR_INITIALIZER_NULL:
664 case IR_INITIALIZER_TARVAL:
665 tv = get_initializer_tarval_value(initializer);
667 case IR_INITIALIZER_CONST: {
668 ir_node *node = get_initializer_const_value(initializer);
669 if(!is_Const(node)) {
670 panic("bitfield initializer not a Const node");
672 tv = get_Const_tarval(node);
675 case IR_INITIALIZER_COMPOUND:
676 panic("bitfield initializer is compound");
679 panic("Couldn't get numeric value for bitfield initializer\n");
682 /* normalize offset */
683 vals += offset_bits >> 3;
685 value_len = get_mode_size_bits(mode);
687 /* combine bits with existing bits */
688 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
689 assert((size_t) (vals - glob_vals) + j < max_vals);
690 assert(vals[j].kind == BITFIELD ||
691 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
692 vals[j].kind = BITFIELD;
693 curr_bits = get_tarval_sub_bits(tv, j);
695 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
697 last_bits = curr_bits;
701 static void dump_ir_initializer(normal_or_bitfield *vals,
702 const ir_initializer_t *initializer,
705 assert((size_t) (vals - glob_vals) < max_vals);
707 switch(get_initializer_kind(initializer)) {
708 case IR_INITIALIZER_NULL:
710 case IR_INITIALIZER_TARVAL: {
713 assert(vals->kind != BITFIELD);
715 vals->v.tarval = get_initializer_tarval_value(initializer);
716 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
717 for(i = 1; i < get_type_size_bytes(type); ++i) {
718 vals[i].kind = NORMAL;
719 vals[i].v.value = NULL;
723 case IR_INITIALIZER_CONST: {
726 assert(vals->kind != BITFIELD);
728 vals->v.value = get_initializer_const_value(initializer);
729 for(i = 1; i < get_type_size_bytes(type); ++i) {
730 vals[i].kind = NORMAL;
731 vals[i].v.value = NULL;
735 case IR_INITIALIZER_COMPOUND: {
737 size_t n = get_initializer_compound_n_entries(initializer);
739 if(is_Array_type(type)) {
740 ir_type *element_type = get_array_element_type(type);
741 size_t skip = get_type_size_bytes(element_type);
742 size_t alignment = get_type_alignment_bytes(element_type);
743 size_t misalign = skip % alignment;
745 skip += alignment - misalign;
748 for(i = 0; i < n; ++i) {
749 ir_initializer_t *sub_initializer
750 = get_initializer_compound_value(initializer, i);
752 dump_ir_initializer(vals, sub_initializer, element_type);
758 assert(is_compound_type(type));
759 n_members = get_compound_n_members(type);
760 for(i = 0; i < n_members; ++i) {
761 ir_entity *member = get_compound_member(type, i);
762 size_t offset = get_entity_offset(member);
763 ir_type *subtype = get_entity_type(member);
764 ir_mode *mode = get_type_mode(subtype);
765 ir_initializer_t *sub_initializer;
767 assert(i < get_initializer_compound_n_entries(initializer));
769 = get_initializer_compound_value(initializer, i);
773 = get_entity_offset_bits_remainder(member);
774 size_t value_len = get_mode_size_bits(mode);
776 if(offset_bits != 0 ||
777 (value_len != 8 && value_len != 16 && value_len != 32
778 && value_len != 64)) {
779 dump_bitfield(&vals[offset], offset_bits,
780 sub_initializer, subtype);
785 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
792 panic("invalid ir_initializer kind found");
795 static void dump_initializer(be_gas_decl_env_t *env, obstack_t *obst,
798 const ir_initializer_t *initializer = entity->attr.initializer;
800 normal_or_bitfield *vals;
804 if(initializer_is_string_const(initializer)) {
805 dump_string_initializer(obst, initializer);
809 type = get_entity_type(entity);
810 size = get_initializer_size(initializer, type);
813 * In the worst case, every initializer allocates one byte.
814 * Moreover, initializer might be big, do not allocate on stack.
816 vals = xcalloc(size, sizeof(vals[0]));
823 dump_ir_initializer(vals, initializer, type);
825 /* now write values sorted */
826 for (k = 0; k < size; ) {
827 int space = 0, skip = 0;
828 if (vals[k].kind == NORMAL) {
829 if(vals[k].v.value != NULL) {
830 dump_atomic_init(env, obst, vals[k].v.value);
831 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
835 } else if(vals[k].kind == TARVAL) {
836 tarval *tv = vals[k].v.tarval;
837 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
842 dump_size_type(obst, size);
843 dump_arith_tarval(obst, tv, size);
844 obstack_1grow(obst, '\n');
846 assert(vals[k].kind == BITFIELD);
847 obstack_printf(obst, "\t.byte\t%d\n", vals[k].v.bf_val);
851 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
860 obstack_printf(obst, "\t.skip\t%d\n", space);
867 * Dump an initializer for a compound entity.
869 static void dump_compound_init(be_gas_decl_env_t *env, obstack_t *obst,
872 normal_or_bitfield *vals;
874 unsigned k, last_ofs;
876 if(ent->has_initializer) {
877 dump_initializer(env, obst, ent);
881 n = get_compound_ent_n_values(ent);
883 /* Find the initializer size. Sorrily gcc support a nasty feature:
884 The last field of a compound may be a flexible array. This allows
885 initializers bigger than the type size. */
886 last_ofs = get_type_size_bytes(get_entity_type(ent));
887 for (i = 0; i < n; ++i) {
888 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
889 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
890 ir_node *value = get_compound_ent_value(ent, i);
891 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
893 offset += (value_len + bits_remainder + 7) >> 3;
895 if (offset > last_ofs) {
901 * In the worst case, every initializer allocates one byte.
902 * Moreover, initializer might be big, do not allocate on stack.
904 vals = xcalloc(last_ofs, sizeof(vals[0]));
906 /* collect the values and store them at the offsets */
907 for (i = 0; i < n; ++i) {
908 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
909 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
910 ir_node *value = get_compound_ent_value(ent, i);
911 int value_len = get_mode_size_bits(get_irn_mode(value));
913 assert(offset_bits >= 0);
915 if (offset_bits != 0 ||
916 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
917 tarval *tv = get_atomic_init_tv(value);
918 unsigned char curr_bits, last_bits = 0;
920 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
921 get_entity_ld_name(ent));
923 /* normalize offset */
924 offset += offset_bits >> 3;
927 for (j = 0; value_len + offset_bits > 0; ++j) {
928 assert(offset + j < last_ofs);
929 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
930 vals[offset + j].kind = BITFIELD;
931 curr_bits = get_tarval_sub_bits(tv, j);
932 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
934 last_bits = curr_bits;
939 assert(offset < last_ofs);
940 assert(vals[offset].kind == NORMAL);
941 for (i = 1; i < value_len / 8; ++i) {
942 assert(vals[offset + i].v.value == NULL);
944 vals[offset].v.value = value;
948 /* now write them sorted */
949 for (k = 0; k < last_ofs; ) {
950 int space = 0, skip = 0;
951 if (vals[k].kind == NORMAL) {
952 if(vals[k].v.value != NULL) {
953 dump_atomic_init(env, obst, vals[k].v.value);
954 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
959 assert(vals[k].kind == BITFIELD);
960 obstack_printf(obst, "\t.byte\t%d\n", vals[k].v.bf_val);
964 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
973 obstack_printf(obst, "\t.skip\t%d\n", space);
979 * Dump a global entity.
981 * @param env the gas output environment
982 * @param ent the entity to be dumped
983 * @param emit_commons if non-zero, emit commons (non-local uninitialized entities)
985 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent, int emit_commons)
988 ir_type *type = get_entity_type(ent);
989 const char *ld_name = get_entity_ld_name(ent);
990 unsigned align = get_type_alignment_bytes(type);
991 int emit_as_common = 0;
992 ir_variability variability;
993 ir_visibility visibility;
995 obst = env->data_obst;
996 if (is_Method_type(type)) {
997 if (get_method_img_section(ent) == section_constructors) {
998 obst = env->ctor_obst;
999 obstack_printf(obst, ".balign\t%u\n", align);
1000 dump_size_type(obst, align);
1001 obstack_printf(obst, "%s\n", ld_name);
1006 variability = get_entity_variability(ent);
1007 visibility = get_entity_visibility(ent);
1008 if (variability == variability_constant) {
1009 /* a constant entity, put it on the rdata */
1010 obst = env->rodata_obst;
1011 } else if (variability == variability_uninitialized) {
1012 /* uninitialized entity put it in bss segment */
1013 obst = env->bss_obst;
1014 if (emit_commons && visibility != visibility_local)
1018 be_dbg_variable(env->main_env->db_handle, obst, ent);
1020 /* global or not global */
1021 if (visibility == visibility_external_visible && !emit_as_common) {
1022 obstack_printf(obst, ".global\t%s\n", ld_name);
1023 } else if(visibility == visibility_external_allocated) {
1024 obstack_printf(obst, ".global\t%s\n", ld_name);
1025 /* we can return now... */
1029 if (align > 1 && !emit_as_common) {
1030 obstack_printf(obst, ".balign\t%u\n", align);
1033 if (!emit_as_common) {
1034 obstack_printf(obst, "%s:\n", ld_name);
1037 if (variability == variability_uninitialized) {
1038 if (emit_as_common) {
1039 switch (be_gas_flavour) {
1040 case GAS_FLAVOUR_NORMAL:
1041 case GAS_FLAVOUR_YASM:
1042 obstack_printf(obst, "\t.comm %s,%u,%u\n",
1043 ld_name, get_type_size_bytes(type), align);
1045 case GAS_FLAVOUR_MINGW:
1046 obstack_printf(obst, "\t.comm %s,%u # %u\n",
1047 ld_name, get_type_size_bytes(type), align);
1049 case GAS_FLAVOUR_MAX:
1050 panic("invalid gas flavour selected");
1053 obstack_printf(obst, "\t.zero %u\n", get_type_size_bytes(type));
1056 if (is_atomic_entity(ent)) {
1057 dump_atomic_init(env, obst, get_atomic_ent_value(ent));
1059 /* sort_compound_ent_values(ent); */
1061 switch (get_type_tpop_code(get_entity_type(ent))) {
1063 if (ent_is_string_const(ent))
1064 dump_string_cst(obst, ent);
1066 dump_compound_init(env, obst, ent);
1071 dump_compound_init(env, obst, ent);
1081 * Dumps declarations of global variables and the initialization code.
1083 * @param gt a global like type, either the global or the TLS one
1084 * @param env an environment
1085 * @param emit_commons if non-zero, emit commons (non-local uninitialized entities)
1086 * @param only_emit_marked if non-zero, external allocated entities that do not have
1087 * its visited flag set are ignored
1089 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
1090 int emit_commons, int only_emit_marked)
1092 int i, n = get_compound_n_members(gt);
1093 waitq *worklist = new_waitq();
1095 if (only_emit_marked) {
1096 for (i = 0; i < n; i++) {
1097 ir_entity *ent = get_compound_member(gt, i);
1098 if (is_entity_backend_marked(ent) ||
1099 get_entity_visibility(ent) != visibility_external_allocated) {
1100 waitq_put(worklist, ent);
1101 set_entity_backend_marked(ent, 1);
1105 for (i = 0; i < n; i++) {
1106 ir_entity *ent = get_compound_member(gt, i);
1107 set_entity_backend_marked(ent, 1);
1108 waitq_put(worklist, ent);
1112 env->worklist = worklist;
1114 while (!waitq_empty(worklist)) {
1115 ir_entity *ent = waitq_get(worklist);
1117 dump_global(env, ent, emit_commons);
1120 del_waitq(worklist);
1121 env->worklist = NULL;
1124 /************************************************************************/
1126 /* Generate all entities. */
1127 void be_gas_emit_decls(const be_main_env_t *main_env,
1128 int only_emit_marked_entities)
1130 be_gas_decl_env_t env;
1138 /* dump the global type */
1139 obstack_init(&rodata);
1140 obstack_init(&data);
1142 obstack_init(&ctor);
1144 env.rodata_obst = &rodata;
1145 env.data_obst = &data;
1146 env.bss_obst = &bss;
1147 env.ctor_obst = &ctor;
1148 env.main_env = main_env;
1150 be_gas_dump_globals(get_glob_type(), &env, 1, only_emit_marked_entities);
1152 size = obstack_object_size(&data);
1153 cp = obstack_finish(&data);
1155 be_gas_emit_switch_section(GAS_SECTION_DATA);
1156 be_emit_string_len(cp, size);
1157 be_emit_write_line();
1160 size = obstack_object_size(&rodata);
1161 cp = obstack_finish(&rodata);
1163 be_gas_emit_switch_section(GAS_SECTION_RODATA);
1164 be_emit_string_len(cp, size);
1165 be_emit_write_line();
1168 size = obstack_object_size(&bss);
1169 cp = obstack_finish(&bss);
1171 be_gas_emit_switch_section(GAS_SECTION_COMMON);
1172 be_emit_string_len(cp, size);
1173 be_emit_write_line();
1176 size = obstack_object_size(&ctor);
1177 cp = obstack_finish(&ctor);
1179 be_gas_emit_switch_section(GAS_SECTION_CTOR);
1180 be_emit_string_len(cp, size);
1181 be_emit_write_line();
1184 obstack_free(&rodata, NULL);
1185 obstack_free(&data, NULL);
1186 obstack_free(&bss, NULL);
1187 obstack_free(&ctor, NULL);
1189 /* dump the Thread Local Storage */
1190 obstack_init(&data);
1192 env.rodata_obst = &data;
1193 env.data_obst = &data;
1194 env.bss_obst = &data;
1195 env.ctor_obst = NULL;
1197 be_gas_dump_globals(get_tls_type(), &env, 0, only_emit_marked_entities);
1199 size = obstack_object_size(&data);
1200 cp = obstack_finish(&data);
1202 be_gas_emit_switch_section(GAS_SECTION_TLS);
1203 be_emit_cstring(".balign\t32\n");
1204 be_emit_write_line();
1205 be_emit_string_len(cp, size);
1206 be_emit_write_line();
1209 obstack_free(&data, NULL);