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 switch (get_irn_opcode(init)) {
265 do_dump_atomic_init(env, obst, get_Cast_op(init));
269 do_dump_atomic_init(env, obst, get_Conv_op(init));
273 tv = get_Const_tarval(init);
275 /* it's a arithmetic value */
276 dump_arith_tarval(obst, tv, bytes);
280 switch (get_SymConst_kind(init)) {
281 case symconst_addr_name:
282 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
285 case symconst_addr_ent:
286 ent = get_SymConst_entity(init);
287 if(!is_entity_backend_marked(ent)) {
288 waitq_put(env->worklist, ent);
289 set_entity_backend_marked(ent, 1);
291 obstack_printf(obst, "%s", get_entity_ld_name(ent));
294 case symconst_ofs_ent:
295 ent = get_SymConst_entity(init);
296 #if 0 /* not needed, is it? */
297 if(!is_entity_backend_marked(ent)) {
298 waitq_put(env->worklist, ent);
299 set_entity_backend_marked(ent, 1);
302 obstack_printf(obst, "%d", get_entity_offset(ent));
305 case symconst_type_size:
306 obstack_printf(obst, "%u", get_type_size_bytes(get_SymConst_type(init)));
309 case symconst_type_align:
310 obstack_printf(obst, "%u", get_type_alignment_bytes(get_SymConst_type(init)));
313 case symconst_enum_const:
314 tv = get_enumeration_value(get_SymConst_enum(init));
315 dump_arith_tarval(obst, tv, bytes);
319 label = get_SymConst_label(init);
320 dump_label(obst, label);
324 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
329 do_dump_atomic_init(env, obst, get_Add_left(init));
330 obstack_printf(obst, " + ");
331 do_dump_atomic_init(env, obst, get_Add_right(init));
335 do_dump_atomic_init(env, obst, get_Sub_left(init));
336 obstack_printf(obst, " - ");
337 do_dump_atomic_init(env, obst, get_Sub_right(init));
341 do_dump_atomic_init(env, obst, get_Mul_left(init));
342 obstack_printf(obst, " * ");
343 do_dump_atomic_init(env, obst, get_Mul_right(init));
347 assert(0 && "dump_atomic_init(): unknown IR-node");
352 * Dumps the type for given size (.byte, .long, ...)
354 * @param obst an obstack the output is written to
355 * @param size the size in bytes
357 static void dump_size_type(obstack_t *obst, size_t size) {
361 obstack_printf(obst, "\t.byte\t");
365 obstack_printf(obst, "\t.value\t");
369 obstack_printf(obst, "\t.long\t");
373 obstack_printf(obst, "\t.quad\t");
378 /* handled in arith */
382 obstack_printf(obst, "\t.octa\t");
386 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
392 * Dump an atomic value to an obstack.
394 * @param env the gas output environment
395 * @param obst an obstack the output is written to
396 * @param init a node representing the atomic value (on the const code irg)
398 static void dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
401 ir_mode *mode = get_irn_mode(init);
402 int bytes = get_mode_size_bytes(mode);
404 dump_size_type(obst, bytes);
405 do_dump_atomic_init(env, obst, init);
406 obstack_printf(obst, "\n");
409 /************************************************************************/
410 /* Routines to dump global variables */
411 /************************************************************************/
413 static int initializer_is_string_const(const ir_initializer_t *initializer)
417 if(initializer->kind != IR_INITIALIZER_COMPOUND)
420 len = initializer->compound.n_initializers;
421 for(i = 0; i < len; ++i) {
425 ir_initializer_t *sub_initializer
426 = initializer->compound.initializers[i];
428 if(sub_initializer->kind != IR_INITIALIZER_TARVAL)
431 tv = sub_initializer->tarval.value;
432 mode = get_tarval_mode(tv);
434 if (!mode_is_int(mode)
435 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
438 c = get_tarval_long(tv);
439 if((i < len - 1 && !(isgraph(c) || isspace(c)))
440 || (i >= len - 1 && c != 0))
448 * Determine if an entity is a string constant
449 * @param ent The entity
450 * @return 1 if it is a string constant, 0 otherwise
452 static int ent_is_string_const(ir_entity *ent)
454 ir_type *type, *element_type;
458 type = get_entity_type(ent);
460 /* if it's an array */
461 if (!is_Array_type(type))
464 element_type = get_array_element_type(type);
466 /* and the array's element type is primitive */
467 if (!is_Primitive_type(element_type))
470 /* and the mode of the element type is an int of
471 * the same size as the byte mode */
472 mode = get_type_mode(element_type);
473 if (!mode_is_int(mode)
474 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
477 if(ent->has_initializer) {
481 /* if it contains only printable chars and a 0 at the end */
482 n = get_compound_ent_n_values(ent);
483 for (i = 0; i < n; ++i) {
484 ir_node *irn = get_compound_ent_value(ent, i);
488 c = (int) get_tarval_long(get_Const_tarval(irn));
490 if((i < n - 1 && !(isgraph(c) || isspace(c)))
491 || (i == n - 1 && c != '\0'))
496 /* then we can emit it as a string constant */
501 * Dump a string constant.
502 * No checks are made!!
504 * @param obst The obst to dump on.
505 * @param ent The entity to dump.
507 static void dump_string_cst(obstack_t *obst, ir_entity *ent)
514 obstack_printf(obst, "\t.string \"");
515 n = get_compound_ent_n_values(ent);
517 for (i = 0; i < n-1; ++i) {
521 irn = get_compound_ent_value(ent, i);
522 c = (int) get_tarval_long(get_Const_tarval(irn));
525 case '"' : obstack_printf(obst, "\\\""); break;
526 case '\n': obstack_printf(obst, "\\n"); break;
527 case '\r': obstack_printf(obst, "\\r"); break;
528 case '\t': obstack_printf(obst, "\\t"); break;
529 case '\\': obstack_printf(obst, "\\\\"); break;
532 obstack_printf(obst, "%c", c);
534 obstack_printf(obst, "\\%o", c);
538 obstack_printf(obst, "\"\n");
540 type = get_entity_type(ent);
541 type_size = get_type_size_bytes(type);
542 remaining_space = type_size - n;
543 assert(remaining_space >= 0);
544 if(remaining_space > 0) {
545 obstack_printf(obst, "\t.skip\t%d\n", remaining_space);
549 static void dump_string_initializer(obstack_t *obst,
550 const ir_initializer_t *initializer)
554 obstack_printf(obst, "\t.string \"");
556 len = initializer->compound.n_initializers;
557 for(i = 0; i < len; ++i) {
558 const ir_initializer_t *sub_initializer
559 = get_initializer_compound_value(initializer, i);
561 tarval *tv = get_initializer_tarval_value(sub_initializer);
562 int c = get_tarval_long(tv);
565 case '"' : obstack_printf(obst, "\\\""); break;
566 case '\n': obstack_printf(obst, "\\n"); break;
567 case '\r': obstack_printf(obst, "\\r"); break;
568 case '\t': obstack_printf(obst, "\\t"); break;
569 case '\\': obstack_printf(obst, "\\\\"); break;
572 obstack_printf(obst, "%c", c);
574 obstack_printf(obst, "\\%o", c);
578 obstack_printf(obst, "\"\n");
581 enum normal_or_bitfield_kind {
588 enum normal_or_bitfield_kind kind;
592 unsigned char bf_val;
594 } normal_or_bitfield;
596 static int is_type_variable_size(ir_type *type)
603 static size_t get_initializer_size(const ir_initializer_t *initializer,
606 switch(get_initializer_kind(initializer)) {
607 case IR_INITIALIZER_TARVAL: {
608 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
609 return get_type_size_bytes(type);
611 case IR_INITIALIZER_CONST:
612 case IR_INITIALIZER_NULL:
613 return get_type_size_bytes(type);
614 case IR_INITIALIZER_COMPOUND: {
615 if(!is_type_variable_size(type)) {
616 return get_type_size_bytes(type);
619 = get_initializer_compound_n_entries(initializer);
621 unsigned initializer_size = get_type_size_bytes(type);
622 for(i = 0; i < n_entries; ++i) {
623 ir_entity *entity = get_compound_member(type, i);
624 ir_type *type = get_entity_type(entity);
626 const ir_initializer_t *sub_initializer
627 = get_initializer_compound_value(initializer, i);
629 unsigned offset = get_entity_offset(entity);
630 unsigned size = get_initializer_size(sub_initializer, type);
632 if(offset + size > initializer_size) {
633 initializer_size = offset + size;
636 return initializer_size;
641 panic("found invalid initializer");
645 static normal_or_bitfield *glob_vals;
646 static size_t max_vals;
649 static void dump_ir_initializer(normal_or_bitfield *vals,
650 const ir_initializer_t *initializer,
653 assert((size_t) (vals - glob_vals) < max_vals);
655 switch(get_initializer_kind(initializer)) {
656 case IR_INITIALIZER_NULL:
658 case IR_INITIALIZER_TARVAL: {
661 assert(vals->kind != BITFIELD);
663 vals->v.tarval = get_initializer_tarval_value(initializer);
664 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
665 for(i = 1; i < get_type_size_bytes(type); ++i) {
666 vals[i].kind = NORMAL;
667 vals[i].v.value = NULL;
671 case IR_INITIALIZER_CONST: {
674 assert(vals->kind != BITFIELD);
676 vals->v.value = get_initializer_const_value(initializer);
677 for(i = 1; i < get_type_size_bytes(type); ++i) {
678 vals[i].kind = NORMAL;
679 vals[i].v.value = NULL;
683 case IR_INITIALIZER_COMPOUND: {
685 size_t n = get_initializer_compound_n_entries(initializer);
687 if(is_Array_type(type)) {
688 ir_type *element_type = get_array_element_type(type);
689 size_t skip = get_type_size_bytes(element_type);
690 size_t alignment = get_type_alignment_bytes(element_type);
691 size_t misalign = skip % alignment;
693 skip += alignment - misalign;
696 for(i = 0; i < n; ++i) {
697 ir_initializer_t *sub_initializer
698 = get_initializer_compound_value(initializer, i);
700 dump_ir_initializer(vals, sub_initializer, element_type);
706 assert(is_compound_type(type));
707 n_members = get_compound_n_members(type);
708 for(i = 0; i < n_members; ++i) {
709 ir_entity *member = get_compound_member(type, i);
710 size_t offset = get_entity_offset(member);
711 ir_type *subtype = get_entity_type(member);
712 ir_initializer_t *sub_initializer;
714 assert(i < get_initializer_compound_n_entries(initializer));
716 = get_initializer_compound_value(initializer, i);
718 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
726 /* collect the values and store them at the offsets */
727 for (i = 0; i < n; ++i) {
728 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
729 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
730 ir_node *value = get_compound_ent_value(ent, i);
731 int value_len = get_mode_size_bits(get_irn_mode(value));
733 assert(offset_bits >= 0);
735 if (offset_bits != 0 ||
736 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
737 tarval *tv = get_atomic_init_tv(value);
738 unsigned char curr_bits, last_bits = 0;
740 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
741 get_entity_ld_name(ent));
743 /* normalize offset */
744 offset += offset_bits >> 3;
747 for (j = 0; value_len + offset_bits > 0; ++j) {
748 assert(offset + j < last_ofs);
749 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
750 vals[offset + j].kind = BITFIELD;
751 curr_bits = get_tarval_sub_bits(tv, j);
752 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
754 last_bits = curr_bits;
759 assert(offset < last_ofs);
760 assert(vals[offset].kind == NORMAL);
761 for (i = 1; i < value_len / 8; ++i) {
762 assert(vals[offset + i].v.value == NULL);
764 vals[offset].v.value = value;
770 static void dump_initializer(be_gas_decl_env_t *env, obstack_t *obst,
773 const ir_initializer_t *initializer = entity->attr.initializer;
775 normal_or_bitfield *vals;
779 if(initializer_is_string_const(initializer)) {
780 dump_string_initializer(obst, initializer);
784 type = get_entity_type(entity);
785 size = get_initializer_size(initializer, type);
788 * In the worst case, every initializer allocates one byte.
789 * Moreover, initializer might be big, do not allocate on stack.
791 vals = xcalloc(size, sizeof(vals[0]));
798 dump_ir_initializer(vals, initializer, type);
800 /* now write values sorted */
801 for (k = 0; k < size; ) {
802 int space = 0, skip = 0;
803 if (vals[k].kind == NORMAL) {
804 if(vals[k].v.value != NULL) {
805 dump_atomic_init(env, obst, vals[k].v.value);
806 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
810 } else if(vals[k].kind == TARVAL) {
811 tarval *tv = vals[k].v.tarval;
812 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
817 dump_size_type(obst, size);
818 dump_arith_tarval(obst, tv, size);
819 obstack_1grow(obst, '\n');
821 assert(vals[k].kind == BITFIELD);
822 obstack_printf(obst, "\t.byte\t%d\n", vals[k].v.bf_val);
826 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
835 obstack_printf(obst, "\t.skip\t%d\n", space);
842 * Dump an initializer for a compound entity.
844 static void dump_compound_init(be_gas_decl_env_t *env, obstack_t *obst,
847 normal_or_bitfield *vals;
849 unsigned k, last_ofs;
851 if(ent->has_initializer) {
852 dump_initializer(env, obst, ent);
856 n = get_compound_ent_n_values(ent);
858 /* Find the initializer size. Sorrily gcc support a nasty feature:
859 The last field of a compound may be a flexible array. This allows
860 initializers bigger than the type size. */
861 last_ofs = get_type_size_bytes(get_entity_type(ent));
862 for (i = 0; i < n; ++i) {
863 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
864 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
865 ir_node *value = get_compound_ent_value(ent, i);
866 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
868 offset += (value_len + bits_remainder + 7) >> 3;
870 if (offset > last_ofs) {
876 * In the worst case, every initializer allocates one byte.
877 * Moreover, initializer might be big, do not allocate on stack.
879 vals = xcalloc(last_ofs, sizeof(vals[0]));
881 /* collect the values and store them at the offsets */
882 for (i = 0; i < n; ++i) {
883 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
884 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
885 ir_node *value = get_compound_ent_value(ent, i);
886 int value_len = get_mode_size_bits(get_irn_mode(value));
888 assert(offset_bits >= 0);
890 if (offset_bits != 0 ||
891 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
892 tarval *tv = get_atomic_init_tv(value);
893 unsigned char curr_bits, last_bits = 0;
895 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
896 get_entity_ld_name(ent));
898 /* normalize offset */
899 offset += offset_bits >> 3;
902 for (j = 0; value_len + offset_bits > 0; ++j) {
903 assert(offset + j < last_ofs);
904 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
905 vals[offset + j].kind = BITFIELD;
906 curr_bits = get_tarval_sub_bits(tv, j);
907 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
909 last_bits = curr_bits;
914 assert(offset < last_ofs);
915 assert(vals[offset].kind == NORMAL);
916 for (i = 1; i < value_len / 8; ++i) {
917 assert(vals[offset + i].v.value == NULL);
919 vals[offset].v.value = value;
923 /* now write them sorted */
924 for (k = 0; k < last_ofs; ) {
925 int space = 0, skip = 0;
926 if (vals[k].kind == NORMAL) {
927 if(vals[k].v.value != NULL) {
928 dump_atomic_init(env, obst, vals[k].v.value);
929 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
934 assert(vals[k].kind == BITFIELD);
935 obstack_printf(obst, "\t.byte\t%d\n", vals[k].v.bf_val);
939 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
948 obstack_printf(obst, "\t.skip\t%d\n", space);
954 * Dump a global entity.
956 * @param env the gas output environment
957 * @param ent the entity to be dumped
958 * @param emit_commons if non-zero, emit commons (non-local uninitialized entities)
960 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent, int emit_commons)
963 ir_type *type = get_entity_type(ent);
964 const char *ld_name = get_entity_ld_name(ent);
965 unsigned align = get_type_alignment_bytes(type);
966 int emit_as_common = 0;
967 ir_variability variability;
968 ir_visibility visibility;
970 obst = env->data_obst;
971 if (is_Method_type(type)) {
972 if (get_method_img_section(ent) == section_constructors) {
973 obst = env->ctor_obst;
974 obstack_printf(obst, ".balign\t%u\n", align);
975 dump_size_type(obst, align);
976 obstack_printf(obst, "%s\n", ld_name);
981 variability = get_entity_variability(ent);
982 visibility = get_entity_visibility(ent);
983 if (variability == variability_constant) {
984 /* a constant entity, put it on the rdata */
985 obst = env->rodata_obst;
986 } else if (variability == variability_uninitialized) {
987 /* uninitialized entity put it in bss segment */
988 obst = env->bss_obst;
989 if (emit_commons && visibility != visibility_local)
993 be_dbg_variable(env->main_env->db_handle, obst, ent);
995 /* global or not global */
996 if (visibility == visibility_external_visible && !emit_as_common) {
997 obstack_printf(obst, ".global\t%s\n", ld_name);
998 } else if(visibility == visibility_external_allocated) {
999 obstack_printf(obst, ".global\t%s\n", ld_name);
1000 /* we can return now... */
1004 if (align > 1 && !emit_as_common) {
1005 obstack_printf(obst, ".balign\t%u\n", align);
1008 if (!emit_as_common) {
1009 obstack_printf(obst, "%s:\n", ld_name);
1012 if (variability == variability_uninitialized) {
1013 if (emit_as_common) {
1014 switch (be_gas_flavour) {
1015 case GAS_FLAVOUR_NORMAL:
1016 case GAS_FLAVOUR_YASM:
1017 obstack_printf(obst, "\t.comm %s,%u,%u\n",
1018 ld_name, get_type_size_bytes(type), align);
1020 case GAS_FLAVOUR_MINGW:
1021 obstack_printf(obst, "\t.comm %s,%u # %u\n",
1022 ld_name, get_type_size_bytes(type), align);
1024 case GAS_FLAVOUR_MAX:
1025 panic("invalid gas flavour selected");
1028 obstack_printf(obst, "\t.zero %u\n", get_type_size_bytes(type));
1031 if (is_atomic_entity(ent)) {
1032 dump_atomic_init(env, obst, get_atomic_ent_value(ent));
1034 /* sort_compound_ent_values(ent); */
1036 switch (get_type_tpop_code(get_entity_type(ent))) {
1038 if (ent_is_string_const(ent))
1039 dump_string_cst(obst, ent);
1041 dump_compound_init(env, obst, ent);
1046 dump_compound_init(env, obst, ent);
1056 * Dumps declarations of global variables and the initialization code.
1058 * @param gt a global like type, either the global or the TLS one
1059 * @param env an environment
1060 * @param emit_commons if non-zero, emit commons (non-local uninitialized entities)
1061 * @param only_emit_marked if non-zero, external allocated entities that do not have
1062 * its visited flag set are ignored
1064 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
1065 int emit_commons, int only_emit_marked)
1067 int i, n = get_compound_n_members(gt);
1068 waitq *worklist = new_waitq();
1070 if (only_emit_marked) {
1071 for (i = 0; i < n; i++) {
1072 ir_entity *ent = get_compound_member(gt, i);
1073 if (is_entity_backend_marked(ent) ||
1074 get_entity_visibility(ent) != visibility_external_allocated) {
1075 waitq_put(worklist, ent);
1076 set_entity_backend_marked(ent, 1);
1080 for (i = 0; i < n; i++) {
1081 ir_entity *ent = get_compound_member(gt, i);
1082 set_entity_backend_marked(ent, 1);
1083 waitq_put(worklist, ent);
1087 env->worklist = worklist;
1089 while (!waitq_empty(worklist)) {
1090 ir_entity *ent = waitq_get(worklist);
1092 dump_global(env, ent, emit_commons);
1095 del_waitq(worklist);
1096 env->worklist = NULL;
1099 /************************************************************************/
1101 /* Generate all entities. */
1102 void be_gas_emit_decls(const be_main_env_t *main_env,
1103 int only_emit_marked_entities)
1105 be_gas_decl_env_t env;
1113 /* dump the global type */
1114 obstack_init(&rodata);
1115 obstack_init(&data);
1117 obstack_init(&ctor);
1119 env.rodata_obst = &rodata;
1120 env.data_obst = &data;
1121 env.bss_obst = &bss;
1122 env.ctor_obst = &ctor;
1123 env.main_env = main_env;
1125 be_gas_dump_globals(get_glob_type(), &env, 1, only_emit_marked_entities);
1127 size = obstack_object_size(&data);
1128 cp = obstack_finish(&data);
1130 be_gas_emit_switch_section(GAS_SECTION_DATA);
1131 be_emit_string_len(cp, size);
1132 be_emit_write_line();
1135 size = obstack_object_size(&rodata);
1136 cp = obstack_finish(&rodata);
1138 be_gas_emit_switch_section(GAS_SECTION_RODATA);
1139 be_emit_string_len(cp, size);
1140 be_emit_write_line();
1143 size = obstack_object_size(&bss);
1144 cp = obstack_finish(&bss);
1146 be_gas_emit_switch_section(GAS_SECTION_COMMON);
1147 be_emit_string_len(cp, size);
1148 be_emit_write_line();
1151 size = obstack_object_size(&ctor);
1152 cp = obstack_finish(&ctor);
1154 be_gas_emit_switch_section(GAS_SECTION_CTOR);
1155 be_emit_string_len(cp, size);
1156 be_emit_write_line();
1159 obstack_free(&rodata, NULL);
1160 obstack_free(&data, NULL);
1161 obstack_free(&bss, NULL);
1162 obstack_free(&ctor, NULL);
1164 /* dump the Thread Local Storage */
1165 obstack_init(&data);
1167 env.rodata_obst = &data;
1168 env.data_obst = &data;
1169 env.bss_obst = &data;
1170 env.ctor_obst = NULL;
1172 be_gas_dump_globals(get_tls_type(), &env, 0, only_emit_marked_entities);
1174 size = obstack_object_size(&data);
1175 cp = obstack_finish(&data);
1177 be_gas_emit_switch_section(GAS_SECTION_TLS);
1178 be_emit_cstring(".balign\t32\n");
1179 be_emit_write_line();
1180 be_emit_string_len(cp, size);
1181 be_emit_write_line();
1184 obstack_free(&data, NULL);