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 /** by default, we generate assembler code for the Linux gas */
51 be_gas_flavour_t be_gas_flavour = GAS_FLAVOUR_ELF;
53 static be_gas_section_t current_section = (be_gas_section_t) -1;
54 static int force_section = 0;
57 * Return the pseudo-instruction to be issued for a section switch
58 * depending on the current flavour.
60 * @param section the section to switch to
62 * @return the pseudo-instruction
64 static const char *get_section_name(be_gas_section_t section) {
65 static const char *text[GAS_FLAVOUR_LAST+1][GAS_SECTION_LAST+1] = {
66 { /* GAS_FLAVOUR_ELF */
71 ".section\t.tbss,\"awT\",@nobits",
72 ".section\t.ctors,\"aw\",@progbits"
74 { /* GAS_FLAVOUR_MINGW */
77 ".section .rdata,\"dr\"",
79 ".section\t.tbss,\"awT\",@nobits",
80 ".section\t.ctors,\"aw\",@progbits"
82 { /* GAS_FLAVOUR_YASM */
87 ".section\t.tbss,\"awT\",@nobits",
88 ".section\t.ctors,\"aw\",@progbits"
90 { /* GAS_FLAVOUR_MACH_O */
95 NULL, /* TLS is not supported on Mach-O */
96 NULL /* constructors aren't marked with sections in Mach-O */
100 assert((int) be_gas_flavour >= 0 && be_gas_flavour <= GAS_FLAVOUR_LAST);
101 assert((int) section >= 0 && section <= GAS_SECTION_LAST);
102 return text[be_gas_flavour][section];
105 void be_gas_emit_switch_section(be_gas_section_t section) {
106 if(current_section == section || force_section)
110 be_emit_string(get_section_name(section));
112 be_emit_write_line();
113 current_section = section;
116 void be_gas_emit_function_prolog(ir_entity *entity, unsigned alignment)
118 const char *name = get_entity_ld_name(entity);
120 be_gas_emit_switch_section(GAS_SECTION_TEXT);
122 /* write the begin line (used by scripts processing the assembler... */
123 be_emit_write_line();
124 be_emit_cstring("# -- Begin ");
125 be_emit_string(name);
127 be_emit_write_line();
129 const char *fill_byte = "";
130 /* gcc fills space between function with 0x90, no idea if this is needed */
131 if(be_gas_flavour == GAS_FLAVOUR_MACH_O) {
135 unsigned maximum_skip = (1 << alignment) - 1;
136 be_emit_cstring("\t.p2align ");
137 be_emit_irprintf("%u,%s,%u\n", alignment, fill_byte, maximum_skip);
138 be_emit_write_line();
140 if (get_entity_visibility(entity) == visibility_external_visible) {
141 be_emit_cstring(".globl ");
142 be_emit_string(name);
144 be_emit_write_line();
147 switch (be_gas_flavour) {
148 case GAS_FLAVOUR_ELF:
149 be_emit_cstring("\t.type\t");
150 be_emit_string(name);
151 be_emit_cstring(", @function\n");
152 be_emit_write_line();
154 case GAS_FLAVOUR_MINGW:
155 be_emit_cstring("\t.def\t");
156 be_emit_string(name);
157 be_emit_cstring(";\t.scl\t2;\t.type\t32;\t.endef\n");
158 be_emit_write_line();
160 case GAS_FLAVOUR_MACH_O:
161 case GAS_FLAVOUR_YASM:
164 be_emit_string(name);
165 be_emit_cstring(":\n");
166 be_emit_write_line();
169 void be_gas_emit_function_epilog(ir_entity *entity)
171 const char *name = get_entity_ld_name(entity);
173 if (be_gas_flavour == GAS_FLAVOUR_ELF) {
174 be_emit_cstring("\t.size\t");
175 be_emit_string(name);
176 be_emit_cstring(", .-");
177 be_emit_string(name);
179 be_emit_write_line();
182 be_emit_cstring("# -- End ");
183 be_emit_string(name);
185 be_emit_write_line();
189 * An environment containing all needed dumper data.
190 * Currently we create the file completely in memory first, then
191 * write it to the disk. This is an artifact from the old C-generating backend
192 * and even there NOT needed. So we might change it in the future.
194 typedef struct _be_gas_decl_env {
195 const be_main_env_t *main_env; /**< The main backend environment, used for it's debug handle. */
196 waitq *worklist; /**< A worklist we use to place not yet handled entities on. */
199 /************************************************************************/
204 * @param tv the tarval
205 * @param bytes the width of the tarvals value in bytes
207 static void dump_arith_tarval(tarval *tv, int bytes)
211 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
215 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
219 be_emit_irprintf("0x%02x%02x%02x%02x",
220 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
224 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
225 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
226 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
234 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x"
235 "%02x%02x%02x%02x%02x%02x%02x%02x",
236 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
237 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
238 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
239 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
240 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
241 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
242 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
243 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
248 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
254 * Return the label prefix for labeled blocks.
256 const char *be_gas_label_prefix(void) {
263 static void dump_label(ir_label_t label) {
264 be_emit_irprintf("%s%ld", be_gas_label_prefix(), label);
268 * Return the tarval of an atomic initializer.
270 * @param init a node representing the initializer (on the const code irg)
274 static tarval *get_atomic_init_tv(ir_node *init)
277 ir_mode *mode = get_irn_mode(init);
279 switch (get_irn_opcode(init)) {
282 init = get_Cast_op(init);
286 init = get_Conv_op(init);
290 return get_Const_tarval(init);
293 switch (get_SymConst_kind(init)) {
294 case symconst_type_size:
295 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
297 case symconst_type_align:
298 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
300 case symconst_ofs_ent:
301 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
303 case symconst_enum_const:
304 return get_enumeration_value(get_SymConst_enum(init));
320 * Dump an atomic value.
322 * @param env the gas output environment
323 * @param init a node representing the atomic value (on the const code irg)
325 static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
327 ir_mode *mode = get_irn_mode(init);
328 int bytes = get_mode_size_bytes(mode);
333 init = skip_Id(init);
335 switch (get_irn_opcode(init)) {
337 do_dump_atomic_init(env, get_Cast_op(init));
341 do_dump_atomic_init(env, get_Conv_op(init));
345 tv = get_Const_tarval(init);
347 /* it's a arithmetic value */
348 dump_arith_tarval(tv, bytes);
352 switch (get_SymConst_kind(init)) {
353 case symconst_addr_name:
354 be_emit_ident(get_SymConst_name(init));
357 case symconst_addr_ent:
358 ent = get_SymConst_entity(init);
359 if(!is_entity_backend_marked(ent)) {
360 waitq_put(env->worklist, ent);
361 set_entity_backend_marked(ent, 1);
363 be_emit_ident(get_entity_ld_ident(ent));
366 case symconst_ofs_ent:
367 ent = get_SymConst_entity(init);
368 #if 0 /* not needed, is it? */
369 if(!is_entity_backend_marked(ent)) {
370 waitq_put(env->worklist, ent);
371 set_entity_backend_marked(ent, 1);
374 be_emit_irprintf("%d", get_entity_offset(ent));
377 case symconst_type_size:
378 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
381 case symconst_type_align:
382 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
385 case symconst_enum_const:
386 tv = get_enumeration_value(get_SymConst_enum(init));
387 dump_arith_tarval(tv, bytes);
391 label = get_SymConst_label(init);
396 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
401 do_dump_atomic_init(env, get_Add_left(init));
402 be_emit_cstring(" + ");
403 do_dump_atomic_init(env, get_Add_right(init));
407 do_dump_atomic_init(env, get_Sub_left(init));
408 be_emit_cstring(" - ");
409 do_dump_atomic_init(env, get_Sub_right(init));
413 do_dump_atomic_init(env, get_Mul_left(init));
414 be_emit_cstring(" * ");
415 do_dump_atomic_init(env, get_Mul_right(init));
419 assert(0 && "dump_atomic_init(): unknown IR-node");
424 * Dumps the type for given size (.byte, .long, ...)
426 * @param size the size in bytes
428 static void dump_size_type(size_t size) {
431 be_emit_cstring("\t.byte\t");
435 be_emit_cstring("\t.word\t");
439 be_emit_cstring("\t.long\t");
443 be_emit_cstring("\t.quad\t");
448 /* handled in arith */
452 be_emit_cstring("\t.octa\t");
456 panic("Try to dump a type with %u bytes\n", (unsigned) size);
461 * Emit an atomic value.
463 * @param env the gas output environment
464 * @param init a node representing the atomic value (on the const code irg)
466 static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
468 ir_mode *mode = get_irn_mode(init);
469 int bytes = get_mode_size_bytes(mode);
471 dump_size_type(bytes);
472 do_dump_atomic_init(env, init);
474 be_emit_write_line();
477 /************************************************************************/
478 /* Routines to dump global variables */
479 /************************************************************************/
481 static int initializer_is_string_const(const ir_initializer_t *initializer)
485 if(initializer->kind != IR_INITIALIZER_COMPOUND)
488 len = initializer->compound.n_initializers;
491 for(i = 0; i < len; ++i) {
495 ir_initializer_t *sub_initializer
496 = initializer->compound.initializers[i];
498 if(sub_initializer->kind != IR_INITIALIZER_TARVAL)
501 tv = sub_initializer->tarval.value;
502 mode = get_tarval_mode(tv);
504 if (!mode_is_int(mode)
505 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
508 c = get_tarval_long(tv);
510 if (!isgraph(c) && !isspace(c))
522 * Determine if an entity is a string constant
523 * @param ent The entity
524 * @return 1 if it is a string constant, 0 otherwise
526 static int ent_is_string_const(ir_entity *ent)
528 ir_type *type, *element_type;
532 type = get_entity_type(ent);
534 /* if it's an array */
535 if (!is_Array_type(type))
538 element_type = get_array_element_type(type);
540 /* and the array's element type is primitive */
541 if (!is_Primitive_type(element_type))
544 /* and the mode of the element type is an int of
545 * the same size as the byte mode */
546 mode = get_type_mode(element_type);
547 if (!mode_is_int(mode)
548 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
551 if(ent->has_initializer) {
555 /* if it contains only printable chars and a 0 at the end */
556 n = get_compound_ent_n_values(ent);
557 for (i = 0; i < n; ++i) {
558 ir_node *irn = get_compound_ent_value(ent, i);
562 c = (int) get_tarval_long(get_Const_tarval(irn));
564 if((i < n - 1 && !(isgraph(c) || isspace(c)))
565 || (i == n - 1 && c != '\0'))
570 /* then we can emit it as a string constant */
575 * Dump a string constant.
576 * No checks are made!!
578 * @param ent The entity to dump.
580 static void dump_string_cst(ir_entity *ent)
587 be_gas_section_t last_section = current_section;
588 len = get_compound_ent_n_values(ent);
589 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
590 if (get_entity_variability(ent) == variability_constant) {
591 be_gas_emit_switch_section(GAS_SECTION_CSTRING);
593 be_emit_cstring("\t.ascii \"");
595 be_emit_cstring("\t.string \"");
599 for (i = 0; i < len; ++i) {
603 irn = get_compound_ent_value(ent, i);
604 c = (int) get_tarval_long(get_Const_tarval(irn));
607 case '"' : be_emit_cstring("\\\""); break;
608 case '\n': be_emit_cstring("\\n"); break;
609 case '\r': be_emit_cstring("\\r"); break;
610 case '\t': be_emit_cstring("\\t"); break;
611 case '\\': be_emit_cstring("\\\\"); break;
616 be_emit_irprintf("\\%o", c);
620 be_emit_cstring("\"\n");
621 be_emit_write_line();
623 type = get_entity_type(ent);
624 type_size = get_type_size_bytes(type);
625 remaining_space = type_size - len;
626 assert(remaining_space >= 0);
627 if(remaining_space > 0) {
628 be_emit_irprintf("\t.skip\t%d\n", remaining_space);
630 if(be_gas_flavour == GAS_FLAVOUR_MACH_O) {
631 be_gas_emit_switch_section(last_section);
635 static void dump_string_initializer(const ir_initializer_t *initializer)
639 len = initializer->compound.n_initializers;
640 if(be_gas_flavour == GAS_FLAVOUR_MACH_O) {
641 be_emit_cstring("\t.string \"");
644 be_emit_cstring("\t.ascii \"");
647 for(i = 0; i < len; ++i) {
648 const ir_initializer_t *sub_initializer
649 = get_initializer_compound_value(initializer, i);
651 tarval *tv = get_initializer_tarval_value(sub_initializer);
652 int c = get_tarval_long(tv);
655 case '"' : be_emit_cstring("\\\""); break;
656 case '\n': be_emit_cstring("\\n"); break;
657 case '\r': be_emit_cstring("\\r"); break;
658 case '\t': be_emit_cstring("\\t"); break;
659 case '\\': be_emit_cstring("\\\\"); break;
664 be_emit_irprintf("\\%o", c);
668 be_emit_cstring("\"\n");
669 be_emit_write_line();
672 enum normal_or_bitfield_kind {
679 enum normal_or_bitfield_kind kind;
683 unsigned char bf_val;
685 } normal_or_bitfield;
687 static int is_type_variable_size(ir_type *type)
694 static size_t get_initializer_size(const ir_initializer_t *initializer,
697 switch(get_initializer_kind(initializer)) {
698 case IR_INITIALIZER_TARVAL: {
699 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
700 return get_type_size_bytes(type);
702 case IR_INITIALIZER_CONST:
703 case IR_INITIALIZER_NULL:
704 return get_type_size_bytes(type);
705 case IR_INITIALIZER_COMPOUND: {
706 if(!is_type_variable_size(type)) {
707 return get_type_size_bytes(type);
710 = get_initializer_compound_n_entries(initializer);
712 unsigned initializer_size = get_type_size_bytes(type);
713 for(i = 0; i < n_entries; ++i) {
714 ir_entity *entity = get_compound_member(type, i);
715 ir_type *type = get_entity_type(entity);
717 const ir_initializer_t *sub_initializer
718 = get_initializer_compound_value(initializer, i);
720 unsigned offset = get_entity_offset(entity);
721 unsigned size = get_initializer_size(sub_initializer, type);
723 if(offset + size > initializer_size) {
724 initializer_size = offset + size;
727 return initializer_size;
732 panic("found invalid initializer");
736 static normal_or_bitfield *glob_vals;
737 static size_t max_vals;
740 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
741 const ir_initializer_t *initializer, ir_type *type)
743 unsigned char last_bits = 0;
744 ir_mode *mode = get_type_mode(type);
746 unsigned char curr_bits;
750 switch(get_initializer_kind(initializer)) {
751 case IR_INITIALIZER_NULL:
753 case IR_INITIALIZER_TARVAL:
754 tv = get_initializer_tarval_value(initializer);
756 case IR_INITIALIZER_CONST: {
757 ir_node *node = get_initializer_const_value(initializer);
758 if(!is_Const(node)) {
759 panic("bitfield initializer not a Const node");
761 tv = get_Const_tarval(node);
764 case IR_INITIALIZER_COMPOUND:
765 panic("bitfield initializer is compound");
768 panic("Couldn't get numeric value for bitfield initializer\n");
771 /* normalize offset */
772 vals += offset_bits >> 3;
774 value_len = get_mode_size_bits(mode);
776 /* combine bits with existing bits */
777 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
778 assert((size_t) (vals - glob_vals) + j < max_vals);
779 assert(vals[j].kind == BITFIELD ||
780 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
781 vals[j].kind = BITFIELD;
782 curr_bits = get_tarval_sub_bits(tv, j);
784 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
786 last_bits = curr_bits;
790 static void dump_ir_initializer(normal_or_bitfield *vals,
791 const ir_initializer_t *initializer,
794 assert((size_t) (vals - glob_vals) < max_vals);
796 switch(get_initializer_kind(initializer)) {
797 case IR_INITIALIZER_NULL:
799 case IR_INITIALIZER_TARVAL: {
802 assert(vals->kind != BITFIELD);
804 vals->v.tarval = get_initializer_tarval_value(initializer);
805 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
806 for(i = 1; i < get_type_size_bytes(type); ++i) {
807 vals[i].kind = NORMAL;
808 vals[i].v.value = NULL;
812 case IR_INITIALIZER_CONST: {
815 assert(vals->kind != BITFIELD);
817 vals->v.value = get_initializer_const_value(initializer);
818 for(i = 1; i < get_type_size_bytes(type); ++i) {
819 vals[i].kind = NORMAL;
820 vals[i].v.value = NULL;
824 case IR_INITIALIZER_COMPOUND: {
826 size_t n = get_initializer_compound_n_entries(initializer);
828 if(is_Array_type(type)) {
829 ir_type *element_type = get_array_element_type(type);
830 size_t skip = get_type_size_bytes(element_type);
831 size_t alignment = get_type_alignment_bytes(element_type);
832 size_t misalign = skip % alignment;
834 skip += alignment - misalign;
837 for(i = 0; i < n; ++i) {
838 ir_initializer_t *sub_initializer
839 = get_initializer_compound_value(initializer, i);
841 dump_ir_initializer(vals, sub_initializer, element_type);
847 assert(is_compound_type(type));
848 n_members = get_compound_n_members(type);
849 for(i = 0; i < n_members; ++i) {
850 ir_entity *member = get_compound_member(type, i);
851 size_t offset = get_entity_offset(member);
852 ir_type *subtype = get_entity_type(member);
853 ir_mode *mode = get_type_mode(subtype);
854 ir_initializer_t *sub_initializer;
856 assert(i < get_initializer_compound_n_entries(initializer));
858 = get_initializer_compound_value(initializer, i);
862 = get_entity_offset_bits_remainder(member);
863 size_t value_len = get_mode_size_bits(mode);
865 if(offset_bits != 0 ||
866 (value_len != 8 && value_len != 16 && value_len != 32
867 && value_len != 64)) {
868 dump_bitfield(&vals[offset], offset_bits,
869 sub_initializer, subtype);
874 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
881 panic("invalid ir_initializer kind found");
884 static void dump_initializer(be_gas_decl_env_t *env, ir_entity *entity)
886 const ir_initializer_t *initializer = entity->attr.initializer;
888 normal_or_bitfield *vals;
892 if(initializer_is_string_const(initializer)) {
893 dump_string_initializer(initializer);
897 type = get_entity_type(entity);
898 size = get_initializer_size(initializer, type);
904 * In the worst case, every initializer allocates one byte.
905 * Moreover, initializer might be big, do not allocate on stack.
907 vals = xcalloc(size, sizeof(vals[0]));
914 dump_ir_initializer(vals, initializer, type);
916 /* now write values sorted */
917 for (k = 0; k < size; ) {
918 int space = 0, skip = 0;
919 if (vals[k].kind == NORMAL) {
920 if(vals[k].v.value != NULL) {
921 dump_atomic_init(env, vals[k].v.value);
922 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
926 } else if(vals[k].kind == TARVAL) {
927 tarval *tv = vals[k].v.tarval;
928 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
933 dump_size_type(size);
934 dump_arith_tarval(tv, size);
936 be_emit_write_line();
938 assert(vals[k].kind == BITFIELD);
939 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
940 be_emit_write_line();
944 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
953 be_emit_irprintf("\t.skip\t%d\n", space);
954 be_emit_write_line();
961 * Dump an initializer for a compound entity.
963 static void dump_compound_init(be_gas_decl_env_t *env, ir_entity *ent)
965 normal_or_bitfield *vals;
967 unsigned k, last_ofs;
969 if(ent->has_initializer) {
970 dump_initializer(env, ent);
974 n = get_compound_ent_n_values(ent);
976 /* Find the initializer size. Sorrily gcc support a nasty feature:
977 The last field of a compound may be a flexible array. This allows
978 initializers bigger than the type size. */
979 last_ofs = get_type_size_bytes(get_entity_type(ent));
980 for (i = 0; i < n; ++i) {
981 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
982 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
983 ir_node *value = get_compound_ent_value(ent, i);
984 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
986 offset += (value_len + bits_remainder + 7) >> 3;
988 if (offset > last_ofs) {
994 * In the worst case, every initializer allocates one byte.
995 * Moreover, initializer might be big, do not allocate on stack.
997 vals = xcalloc(last_ofs, sizeof(vals[0]));
999 /* collect the values and store them at the offsets */
1000 for (i = 0; i < n; ++i) {
1001 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1002 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1003 ir_node *value = get_compound_ent_value(ent, i);
1004 int value_len = get_mode_size_bits(get_irn_mode(value));
1006 assert(offset_bits >= 0);
1008 if (offset_bits != 0 ||
1009 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1010 tarval *tv = get_atomic_init_tv(value);
1011 unsigned char curr_bits, last_bits = 0;
1013 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
1014 get_entity_ld_name(ent));
1016 /* normalize offset */
1017 offset += offset_bits >> 3;
1020 for (j = 0; value_len + offset_bits > 0; ++j) {
1021 assert(offset + j < last_ofs);
1022 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1023 vals[offset + j].kind = BITFIELD;
1024 curr_bits = get_tarval_sub_bits(tv, j);
1025 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1027 last_bits = curr_bits;
1032 assert(offset < last_ofs);
1033 assert(vals[offset].kind == NORMAL);
1034 for (i = 1; i < value_len / 8; ++i) {
1035 assert(vals[offset + i].v.value == NULL);
1037 vals[offset].v.value = value;
1041 /* now write them sorted */
1042 for (k = 0; k < last_ofs; ) {
1043 int space = 0, skip = 0;
1044 if (vals[k].kind == NORMAL) {
1045 if(vals[k].v.value != NULL) {
1046 dump_atomic_init(env, vals[k].v.value);
1047 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1052 assert(vals[k].kind == BITFIELD);
1053 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1057 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1066 be_emit_irprintf("\t.skip\t%d\n", space);
1067 be_emit_write_line();
1073 static void emit_align(unsigned alignment)
1075 if (!is_po2(alignment))
1076 panic("alignment not a power of 2");
1078 be_emit_irprintf(".p2align\t%u\n", log2_floor(alignment));
1079 be_emit_write_line();
1083 * Dump a global entity.
1085 * @param env the gas output environment
1086 * @param ent the entity to be dumped
1087 * @param emit_commons if non-zero, emit commons (non-local uninitialized entities)
1089 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent, int emit_commons)
1091 ir_type *type = get_entity_type(ent);
1092 ident *ld_ident = get_entity_ld_ident(ent);
1093 unsigned align = get_type_alignment_bytes(type);
1094 int emit_as_common = 0;
1095 be_gas_section_t section;
1096 ir_variability variability;
1097 ir_visibility visibility;
1099 if (is_Method_type(type)) {
1100 if (be_gas_flavour != GAS_FLAVOUR_MACH_O
1101 && get_method_img_section(ent) == section_constructors) {
1102 be_gas_emit_switch_section(GAS_SECTION_CTOR);
1104 dump_size_type(align);
1105 be_emit_ident(ld_ident);
1107 be_emit_write_line();
1113 variability = get_entity_variability(ent);
1114 visibility = get_entity_visibility(ent);
1115 section = GAS_SECTION_DATA;
1116 if (variability == variability_constant) {
1117 /* a constant entity, put it on the rdata */
1118 section = GAS_SECTION_RODATA;
1119 } else if (variability == variability_uninitialized) {
1120 /* uninitialized entity put it in bss segment */
1121 section = GAS_SECTION_COMMON;
1122 if (emit_commons && visibility != visibility_local)
1126 if(!emit_as_common) {
1127 be_gas_emit_switch_section(section);
1130 be_dbg_variable(ent);
1132 /* global or not global */
1133 if (visibility == visibility_external_visible && !emit_as_common) {
1134 be_emit_cstring(".globl\t");
1135 be_emit_ident(ld_ident);
1137 be_emit_write_line();
1138 } else if(visibility == visibility_external_allocated) {
1139 be_emit_cstring(".globl\t");
1140 be_emit_ident(ld_ident);
1142 be_emit_write_line();
1143 /* we can return now... */
1147 if (align > 1 && !emit_as_common) {
1151 if (!emit_as_common) {
1152 be_emit_ident(ld_ident);
1153 be_emit_cstring(":\n");
1154 be_emit_write_line();
1157 if (variability == variability_uninitialized) {
1158 if (emit_as_common) {
1159 switch (be_gas_flavour) {
1160 case GAS_FLAVOUR_ELF:
1161 case GAS_FLAVOUR_MACH_O:
1162 case GAS_FLAVOUR_YASM:
1163 be_emit_irprintf("\t.comm %s,%u,%u\n",
1164 get_id_str(ld_ident), get_type_size_bytes(type), align);
1165 be_emit_write_line();
1167 case GAS_FLAVOUR_MINGW:
1168 be_emit_irprintf("\t.comm %s,%u # %u\n",
1169 get_id_str(ld_ident), get_type_size_bytes(type), align);
1170 be_emit_write_line();
1174 be_emit_irprintf("\t.zero %u\n", get_type_size_bytes(type));
1175 be_emit_write_line();
1178 if (is_atomic_entity(ent)) {
1179 dump_atomic_init(env, get_atomic_ent_value(ent));
1181 /* sort_compound_ent_values(ent); */
1183 switch (get_type_tpop_code(get_entity_type(ent))) {
1185 if (ent_is_string_const(ent))
1186 dump_string_cst(ent);
1188 dump_compound_init(env, ent);
1193 dump_compound_init(env, ent);
1203 * Dumps declarations of global variables and the initialization code.
1205 * @param gt a global like type, either the global or the TLS one
1206 * @param env an environment
1207 * @param emit_commons if non-zero, emit commons (non-local uninitialized entities)
1208 * @param only_emit_marked if non-zero, external allocated entities that do not have
1209 * its visited flag set are ignored
1211 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
1212 int emit_commons, int only_emit_marked)
1214 int i, n = get_compound_n_members(gt);
1215 waitq *worklist = new_waitq();
1217 if (only_emit_marked) {
1218 for (i = 0; i < n; i++) {
1219 ir_entity *ent = get_compound_member(gt, i);
1220 if (is_entity_backend_marked(ent) ||
1221 get_entity_visibility(ent) != visibility_external_allocated) {
1222 waitq_put(worklist, ent);
1223 set_entity_backend_marked(ent, 1);
1227 for (i = 0; i < n; i++) {
1228 ir_entity *ent = get_compound_member(gt, i);
1229 set_entity_backend_marked(ent, 1);
1230 waitq_put(worklist, ent);
1234 env->worklist = worklist;
1236 while (!waitq_empty(worklist)) {
1237 ir_entity *ent = waitq_get(worklist);
1239 dump_global(env, ent, emit_commons);
1242 del_waitq(worklist);
1243 env->worklist = NULL;
1246 /************************************************************************/
1248 /* Generate all entities. */
1249 void be_gas_emit_decls(const be_main_env_t *main_env,
1250 int only_emit_marked_entities)
1252 be_gas_decl_env_t env;
1254 env.main_env = main_env;
1256 /* dump global type */
1257 be_gas_dump_globals(get_glob_type(), &env, 1, only_emit_marked_entities);
1259 /* dump the Thread Local Storage */
1260 if (be_gas_flavour != GAS_FLAVOUR_MACH_O) {
1261 be_gas_emit_switch_section(GAS_SECTION_TLS);
1263 be_gas_dump_globals(get_tls_type(), &env, 0, only_emit_marked_entities);