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
45 #include "beemitter.h"
46 #include "be_dbgout.h"
48 /** by default, we generate assembler code for the Linux gas */
49 be_gas_flavour_t be_gas_flavour = GAS_FLAVOUR_ELF;
51 static be_gas_section_t current_section = (be_gas_section_t) -1;
54 * Return the pseudo-instruction to be issued for a section switch
55 * depending on the current flavour.
57 * @param section the section to switch to
59 * @return the pseudo-instruction
61 static const char *get_section_name(be_gas_section_t section) {
62 static const char *text[GAS_FLAVOUR_LAST+1][GAS_SECTION_LAST+1] = {
63 { /* GAS_FLAVOUR_ELF */
68 ".section\t.tbss,\"awT\",@nobits",
69 ".section\t.ctors,\"aw\",@progbits",
70 ".section\t.dtors,\"aw\",@progbits",
71 NULL, /* no cstring section */
75 { /* GAS_FLAVOUR_MINGW */
78 ".section .rdata,\"dr\"",
80 ".section\t.tbss,\"awT\",@nobits",
81 ".section\t.ctors,\"aw\",@progbits",
82 ".section\t.dtors,\"aw\",@progbits",
87 { /* GAS_FLAVOUR_YASM */
92 ".section\t.tbss,\"awT\",@nobits",
93 ".section\t.ctors,\"aw\",@progbits",
94 ".section\t.dtors,\"aw\",@progbits",
99 { /* GAS_FLAVOUR_MACH_O */
104 NULL, /* TLS is not supported on Mach-O */
106 NULL, /* TODO: how is this called? */
108 ".section\t__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5",
109 ".section\t__IMPORT,__pointers,non_lazy_symbol_pointers"
113 assert((int) be_gas_flavour >= 0 && be_gas_flavour <= GAS_FLAVOUR_LAST);
114 assert((int) section >= 0 && section <= GAS_SECTION_LAST);
115 return text[be_gas_flavour][section];
118 void be_gas_emit_switch_section(be_gas_section_t section) {
119 if (current_section == section)
123 be_emit_string(get_section_name(section));
125 be_emit_write_line();
126 current_section = section;
129 void be_gas_emit_function_prolog(ir_entity *entity, unsigned alignment)
131 const char *name = get_entity_ld_name(entity);
132 const char *fill_byte = "";
133 unsigned maximum_skip;
135 be_gas_emit_switch_section(GAS_SECTION_TEXT);
137 /* write the begin line (used by scripts processing the assembler... */
138 be_emit_write_line();
139 be_emit_cstring("# -- Begin ");
140 be_emit_string(name);
142 be_emit_write_line();
144 /* gcc fills space between function with 0x90, no idea if this is needed */
145 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
150 maximum_skip = (1 << alignment) - 1;
151 be_emit_cstring("\t.p2align ");
152 be_emit_irprintf("%u,%s,%u\n", alignment, fill_byte, maximum_skip);
153 be_emit_write_line();
155 if (get_entity_visibility(entity) == visibility_external_visible) {
156 be_emit_cstring(".globl ");
157 be_emit_string(name);
159 be_emit_write_line();
162 switch (be_gas_flavour) {
163 case GAS_FLAVOUR_ELF:
164 be_emit_cstring("\t.type\t");
165 be_emit_string(name);
166 be_emit_cstring(", @function\n");
167 be_emit_write_line();
169 case GAS_FLAVOUR_MINGW:
170 be_emit_cstring("\t.def\t");
171 be_emit_string(name);
172 if (get_entity_visibility(entity) == visibility_external_visible) {
173 be_emit_cstring(";\t.scl\t2;\t.type\t32;\t.endef\n");
175 be_emit_cstring(";\t.scl\t3;\t.type\t32;\t.endef\n");
177 be_emit_write_line();
179 case GAS_FLAVOUR_MACH_O:
180 case GAS_FLAVOUR_YASM:
183 be_emit_string(name);
184 be_emit_cstring(":\n");
185 be_emit_write_line();
188 void be_gas_emit_function_epilog(ir_entity *entity)
190 const char *name = get_entity_ld_name(entity);
192 if (be_gas_flavour == GAS_FLAVOUR_ELF) {
193 be_emit_cstring("\t.size\t");
194 be_emit_string(name);
195 be_emit_cstring(", .-");
196 be_emit_string(name);
198 be_emit_write_line();
201 be_emit_cstring("# -- End ");
202 be_emit_string(name);
204 be_emit_write_line();
208 * An environment containing all needed dumper data.
209 * Currently we create the file completely in memory first, then
210 * write it to the disk. This is an artifact from the old C-generating backend
211 * and even there NOT needed. So we might change it in the future.
213 typedef struct _be_gas_decl_env {
214 be_gas_section_t section;
215 waitq *worklist; /**< A worklist we use to place not yet handled entities on. */
218 /************************************************************************/
223 * @param tv the tarval
224 * @param bytes the width of the tarvals value in bytes
226 static void dump_arith_tarval(tarval *tv, int bytes)
230 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
234 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
238 be_emit_irprintf("0x%02x%02x%02x%02x",
239 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
243 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
244 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
245 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
249 /* Beware: Mixed endian output! One little endian number emitted as
250 * three longs. Each long initializer is written in big endian. */
252 "\t.long\t0x%02x%02x%02x%02x\n"
253 "\t.long\t0x%02x%02x%02x%02x\n"
254 "\t.long\t0x%02x%02x%02x%02x",
255 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
256 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
257 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
258 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
259 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
260 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8)
266 "\t.long\t0x%02x%02x%02x%02x0x%02x%02x%02x%02x0x%02x%02x%02x%02x0x%02x%02x%02x%02x",
267 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
268 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
269 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
270 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
271 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
272 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
273 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
274 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0)
279 panic("Can't dump a tarval with %d bytes", bytes);
283 * Return the label prefix for labeled blocks.
285 const char *be_gas_block_label_prefix(void) {
290 * Return the label prefix for labeled instructions.
292 const char *be_gas_insn_label_prefix(void) {
299 static void dump_label(ir_label_t label) {
300 be_emit_irprintf("%s%lu", be_gas_block_label_prefix(), label);
304 * Return the tarval of an atomic initializer.
306 * @param init a node representing the initializer (on the const code irg)
310 static tarval *get_atomic_init_tv(ir_node *init)
313 ir_mode *mode = get_irn_mode(init);
315 switch (get_irn_opcode(init)) {
318 init = get_Cast_op(init);
322 init = get_Conv_op(init);
326 return get_Const_tarval(init);
329 switch (get_SymConst_kind(init)) {
330 case symconst_type_size:
331 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
333 case symconst_type_align:
334 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
336 case symconst_ofs_ent:
337 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
339 case symconst_enum_const:
340 return get_enumeration_value(get_SymConst_enum(init));
356 * Dump an atomic value.
358 * @param env the gas output environment
359 * @param init a node representing the atomic value (on the const code irg)
361 static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
363 ir_mode *mode = get_irn_mode(init);
364 int bytes = get_mode_size_bytes(mode);
369 init = skip_Id(init);
371 switch (get_irn_opcode(init)) {
373 do_dump_atomic_init(env, get_Cast_op(init));
377 do_dump_atomic_init(env, get_Conv_op(init));
381 tv = get_Const_tarval(init);
383 /* it's a arithmetic value */
384 dump_arith_tarval(tv, bytes);
388 switch (get_SymConst_kind(init)) {
389 case symconst_addr_name:
390 be_emit_ident(get_SymConst_name(init));
393 case symconst_addr_ent:
394 ent = get_SymConst_entity(init);
395 if (!is_entity_backend_marked(ent)) {
396 waitq_put(env->worklist, ent);
397 set_entity_backend_marked(ent, 1);
399 be_emit_ident(get_entity_ld_ident(ent));
402 case symconst_ofs_ent:
403 ent = get_SymConst_entity(init);
404 #if 0 /* not needed, is it? */
405 if (!is_entity_backend_marked(ent)) {
406 waitq_put(env->worklist, ent);
407 set_entity_backend_marked(ent, 1);
410 be_emit_irprintf("%d", get_entity_offset(ent));
413 case symconst_type_size:
414 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
417 case symconst_type_align:
418 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
421 case symconst_enum_const:
422 tv = get_enumeration_value(get_SymConst_enum(init));
423 dump_arith_tarval(tv, bytes);
427 label = get_SymConst_label(init);
432 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
437 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
438 panic("Constant must be int or pointer for '+' to work");
440 do_dump_atomic_init(env, get_Add_left(init));
441 be_emit_cstring(" + ");
442 do_dump_atomic_init(env, get_Add_right(init));
446 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
447 panic("Constant must be int or pointer for '-' to work");
449 do_dump_atomic_init(env, get_Sub_left(init));
450 be_emit_cstring(" - ");
451 do_dump_atomic_init(env, get_Sub_right(init));
455 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
456 panic("Constant must be int or pointer for '*' to work");
458 do_dump_atomic_init(env, get_Mul_left(init));
459 be_emit_cstring(" * ");
460 do_dump_atomic_init(env, get_Mul_right(init));
464 panic("dump_atomic_init(): unsupported IR-node %+F", init);
469 * Dumps the type for given size (.byte, .long, ...)
471 * @param size the size in bytes
473 static void dump_size_type(size_t size) {
476 be_emit_cstring("\t.byte\t");
480 be_emit_cstring("\t.word\t");
484 be_emit_cstring("\t.long\t");
488 be_emit_cstring("\t.quad\t");
493 /* handled in arith */
497 be_emit_cstring("\t.octa\t");
501 panic("Try to dump a type with %u bytes", (unsigned)size);
506 * Emit an atomic value.
508 * @param env the gas output environment
509 * @param init a node representing the atomic value (on the const code irg)
511 static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
513 ir_mode *mode = get_irn_mode(init);
514 int bytes = get_mode_size_bytes(mode);
516 dump_size_type(bytes);
517 do_dump_atomic_init(env, init);
519 be_emit_write_line();
522 /************************************************************************/
523 /* Routines to dump global variables */
524 /************************************************************************/
526 static int initializer_is_string_const(const ir_initializer_t *initializer)
529 int found_printable = 0;
531 if (initializer->kind != IR_INITIALIZER_COMPOUND)
534 len = initializer->compound.n_initializers;
537 for (i = 0; i < len; ++i) {
541 ir_initializer_t *sub_initializer
542 = initializer->compound.initializers[i];
544 if (sub_initializer->kind != IR_INITIALIZER_TARVAL)
547 tv = sub_initializer->tarval.value;
548 mode = get_tarval_mode(tv);
550 if (!mode_is_int(mode)
551 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
554 c = get_tarval_long(tv);
555 if (isgraph(c) || isspace(c))
560 if (i == len - 1 && c != '\0')
564 return found_printable;
568 * Determine if an entity is a string constant
569 * @param ent The entity
570 * @return 1 if it is a string constant, 0 otherwise
572 static int ent_is_string_const(ir_entity *ent)
574 ir_type *type, *element_type;
577 int found_printable = 0;
579 type = get_entity_type(ent);
581 /* if it's an array */
582 if (!is_Array_type(type))
585 element_type = get_array_element_type(type);
587 /* and the array's element type is primitive */
588 if (!is_Primitive_type(element_type))
591 /* and the mode of the element type is an int of
592 * the same size as the byte mode */
593 mode = get_type_mode(element_type);
594 if (!mode_is_int(mode)
595 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
598 if (ent->has_initializer) {
602 /* if it contains only printable chars and a 0 at the end */
603 n = get_compound_ent_n_values(ent);
604 for (i = 0; i < n; ++i) {
605 ir_node *irn = get_compound_ent_value(ent, i);
609 c = (int) get_tarval_long(get_Const_tarval(irn));
611 if (isgraph(c) || isspace(c))
616 if (i == n - 1 && c != '\0')
621 /* then we can emit it as a string constant */
622 return found_printable;
626 * Dump a string constant.
627 * No checks are made!!
629 * @param ent The entity to dump.
631 static void dump_string_cst(ir_entity *ent)
639 len = get_compound_ent_n_values(ent);
641 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
642 be_emit_cstring("\t.ascii \"");
644 be_emit_cstring("\t.string \"");
648 for (i = 0; i < output_len; ++i) {
652 irn = get_compound_ent_value(ent, i);
653 c = (int) get_tarval_long(get_Const_tarval(irn));
656 case '"' : be_emit_cstring("\\\""); break;
657 case '\n': be_emit_cstring("\\n"); break;
658 case '\r': be_emit_cstring("\\r"); break;
659 case '\t': be_emit_cstring("\\t"); break;
660 case '\\': be_emit_cstring("\\\\"); break;
665 be_emit_irprintf("\\%o", c);
669 be_emit_cstring("\"\n");
670 be_emit_write_line();
672 type = get_entity_type(ent);
673 type_size = get_type_size_bytes(type);
674 remaining_space = type_size - len;
675 assert(remaining_space >= 0);
676 if (remaining_space > 0) {
677 be_emit_irprintf("\t.space\t%d\n", remaining_space);
681 static void dump_string_initializer(const ir_initializer_t *initializer)
685 len = initializer->compound.n_initializers;
686 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
687 be_emit_cstring("\t.ascii \"");
689 be_emit_cstring("\t.string \"");
693 for (i = 0; i < len; ++i) {
694 const ir_initializer_t *sub_initializer
695 = get_initializer_compound_value(initializer, i);
697 tarval *tv = get_initializer_tarval_value(sub_initializer);
698 int c = get_tarval_long(tv);
701 case '"' : be_emit_cstring("\\\""); break;
702 case '\n': be_emit_cstring("\\n"); break;
703 case '\r': be_emit_cstring("\\r"); break;
704 case '\t': be_emit_cstring("\\t"); break;
705 case '\\': be_emit_cstring("\\\\"); break;
710 be_emit_irprintf("\\%o", c);
714 be_emit_cstring("\"\n");
715 be_emit_write_line();
718 enum normal_or_bitfield_kind {
725 enum normal_or_bitfield_kind kind;
729 unsigned char bf_val;
731 } normal_or_bitfield;
733 static int is_type_variable_size(ir_type *type)
740 static size_t get_initializer_size(const ir_initializer_t *initializer,
743 switch (get_initializer_kind(initializer)) {
744 case IR_INITIALIZER_TARVAL: {
745 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
746 return get_type_size_bytes(type);
748 case IR_INITIALIZER_CONST:
749 case IR_INITIALIZER_NULL:
750 return get_type_size_bytes(type);
751 case IR_INITIALIZER_COMPOUND: {
752 if (!is_type_variable_size(type)) {
753 return get_type_size_bytes(type);
756 = get_initializer_compound_n_entries(initializer);
758 unsigned initializer_size = get_type_size_bytes(type);
759 for (i = 0; i < n_entries; ++i) {
760 ir_entity *entity = get_compound_member(type, i);
761 ir_type *type = get_entity_type(entity);
763 const ir_initializer_t *sub_initializer
764 = get_initializer_compound_value(initializer, i);
766 unsigned offset = get_entity_offset(entity);
767 unsigned size = get_initializer_size(sub_initializer, type);
769 if (offset + size > initializer_size) {
770 initializer_size = offset + size;
773 return initializer_size;
778 panic("found invalid initializer");
782 static normal_or_bitfield *glob_vals;
783 static size_t max_vals;
786 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
787 const ir_initializer_t *initializer, ir_type *type)
789 unsigned char last_bits = 0;
790 ir_mode *mode = get_type_mode(type);
792 unsigned char curr_bits;
796 switch (get_initializer_kind(initializer)) {
797 case IR_INITIALIZER_NULL:
799 case IR_INITIALIZER_TARVAL:
800 tv = get_initializer_tarval_value(initializer);
802 case IR_INITIALIZER_CONST: {
803 ir_node *node = get_initializer_const_value(initializer);
804 if (!is_Const(node)) {
805 panic("bitfield initializer not a Const node");
807 tv = get_Const_tarval(node);
810 case IR_INITIALIZER_COMPOUND:
811 panic("bitfield initializer is compound");
814 panic("Couldn't get numeric value for bitfield initializer");
816 tv = tarval_convert_to(tv, get_type_mode(type));
818 /* normalize offset */
819 vals += offset_bits >> 3;
821 value_len = get_mode_size_bits(mode);
823 /* combine bits with existing bits */
824 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
825 assert((size_t) (vals - glob_vals) + j < max_vals);
826 assert(vals[j].kind == BITFIELD ||
827 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
828 vals[j].kind = BITFIELD;
829 curr_bits = get_tarval_sub_bits(tv, j);
831 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
833 last_bits = curr_bits;
837 static void dump_ir_initializer(normal_or_bitfield *vals,
838 const ir_initializer_t *initializer,
841 assert((size_t) (vals - glob_vals) < max_vals);
843 switch (get_initializer_kind(initializer)) {
844 case IR_INITIALIZER_NULL:
846 case IR_INITIALIZER_TARVAL: {
849 assert(vals->kind != BITFIELD);
851 vals->v.tarval = get_initializer_tarval_value(initializer);
852 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
853 for (i = 1; i < get_type_size_bytes(type); ++i) {
854 vals[i].kind = NORMAL;
855 vals[i].v.value = NULL;
859 case IR_INITIALIZER_CONST: {
862 assert(vals->kind != BITFIELD);
864 vals->v.value = get_initializer_const_value(initializer);
865 for (i = 1; i < get_type_size_bytes(type); ++i) {
866 vals[i].kind = NORMAL;
867 vals[i].v.value = NULL;
871 case IR_INITIALIZER_COMPOUND: {
873 size_t n = get_initializer_compound_n_entries(initializer);
875 if (is_Array_type(type)) {
876 ir_type *element_type = get_array_element_type(type);
877 size_t skip = get_type_size_bytes(element_type);
878 size_t alignment = get_type_alignment_bytes(element_type);
879 size_t misalign = skip % alignment;
881 skip += alignment - misalign;
884 for (i = 0; i < n; ++i) {
885 ir_initializer_t *sub_initializer
886 = get_initializer_compound_value(initializer, i);
888 dump_ir_initializer(vals, sub_initializer, element_type);
894 assert(is_compound_type(type));
895 n_members = get_compound_n_members(type);
896 for (i = 0; i < n_members; ++i) {
897 ir_entity *member = get_compound_member(type, i);
898 size_t offset = get_entity_offset(member);
899 ir_type *subtype = get_entity_type(member);
900 ir_mode *mode = get_type_mode(subtype);
901 ir_initializer_t *sub_initializer;
903 assert(i < get_initializer_compound_n_entries(initializer));
905 = get_initializer_compound_value(initializer, i);
909 = get_entity_offset_bits_remainder(member);
910 size_t value_len = get_mode_size_bits(mode);
912 if (offset_bits != 0 ||
913 (value_len != 8 && value_len != 16 && value_len != 32
914 && value_len != 64)) {
915 dump_bitfield(&vals[offset], offset_bits,
916 sub_initializer, subtype);
921 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
928 panic("invalid ir_initializer kind found");
931 static void dump_initializer(be_gas_decl_env_t *env, ir_entity *entity)
933 const ir_initializer_t *initializer = entity->attr.initializer;
935 normal_or_bitfield *vals;
939 if (initializer_is_string_const(initializer)) {
940 dump_string_initializer(initializer);
944 type = get_entity_type(entity);
945 size = get_initializer_size(initializer, type);
951 * In the worst case, every initializer allocates one byte.
952 * Moreover, initializer might be big, do not allocate on stack.
954 vals = XMALLOCNZ(normal_or_bitfield, size);
961 dump_ir_initializer(vals, initializer, type);
963 /* now write values sorted */
964 for (k = 0; k < size; ) {
967 if (vals[k].kind == NORMAL) {
968 if (vals[k].v.value != NULL) {
969 dump_atomic_init(env, vals[k].v.value);
970 elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
974 } else if (vals[k].kind == TARVAL) {
975 tarval *tv = vals[k].v.tarval;
976 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
981 dump_size_type(size);
982 dump_arith_tarval(tv, size);
984 be_emit_write_line();
986 assert(vals[k].kind == BITFIELD);
987 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
988 be_emit_write_line();
992 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
999 be_emit_irprintf("\t.space\t%d\n", space);
1000 be_emit_write_line();
1007 * Dump an initializer for a compound entity.
1009 static void dump_compound_init(be_gas_decl_env_t *env, ir_entity *ent)
1011 normal_or_bitfield *vals;
1013 unsigned k, last_ofs;
1015 if (ent->has_initializer) {
1016 dump_initializer(env, ent);
1020 n = get_compound_ent_n_values(ent);
1022 /* Find the initializer size. Sorrily gcc support a nasty feature:
1023 The last field of a compound may be a flexible array. This allows
1024 initializers bigger than the type size. */
1025 last_ofs = get_type_size_bytes(get_entity_type(ent));
1026 for (i = 0; i < n; ++i) {
1027 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1028 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1029 ir_node *value = get_compound_ent_value(ent, i);
1030 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
1032 offset += (value_len + bits_remainder + 7) >> 3;
1034 if (offset > last_ofs) {
1040 * In the worst case, every initializer allocates one byte.
1041 * Moreover, initializer might be big, do not allocate on stack.
1043 vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1045 /* collect the values and store them at the offsets */
1046 for (i = 0; i < n; ++i) {
1047 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1048 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1049 ir_node *value = get_compound_ent_value(ent, i);
1050 int value_len = get_mode_size_bits(get_irn_mode(value));
1052 assert(offset_bits >= 0);
1054 if (offset_bits != 0 ||
1055 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1056 tarval *tv = get_atomic_init_tv(value);
1057 unsigned char curr_bits, last_bits = 0;
1059 panic("Couldn't get numeric value for bitfield initializer '%s'",
1060 get_entity_ld_name(ent));
1062 /* normalize offset */
1063 offset += offset_bits >> 3;
1066 for (j = 0; value_len + offset_bits > 0; ++j) {
1067 assert(offset + j < last_ofs);
1068 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1069 vals[offset + j].kind = BITFIELD;
1070 curr_bits = get_tarval_sub_bits(tv, j);
1071 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1073 last_bits = curr_bits;
1078 assert(offset < last_ofs);
1079 assert(vals[offset].kind == NORMAL);
1080 for (i = 1; i < value_len / 8; ++i) {
1081 assert(vals[offset + i].v.value == NULL);
1083 vals[offset].v.value = value;
1087 /* now write them sorted */
1088 for (k = 0; k < last_ofs; ) {
1089 int space = 0, skip = 0;
1090 if (vals[k].kind == NORMAL) {
1091 if (vals[k].v.value != NULL) {
1092 dump_atomic_init(env, vals[k].v.value);
1093 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1098 assert(vals[k].kind == BITFIELD);
1099 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1103 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1112 be_emit_irprintf("\t.space\t%d\n", space);
1113 be_emit_write_line();
1119 static void emit_align(unsigned alignment)
1121 if (!is_po2(alignment))
1122 panic("alignment not a power of 2");
1124 be_emit_irprintf("\t.p2align\t%u\n", log2_floor(alignment));
1125 be_emit_write_line();
1129 * Dump a global entity.
1131 * @param env the gas output environment
1132 * @param ent the entity to be dumped
1134 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent)
1136 ir_type *type = get_entity_type(ent);
1137 ident *ld_ident = get_entity_ld_ident(ent);
1138 unsigned align = get_type_alignment_bytes(type);
1139 int emit_as_common = 0;
1140 be_gas_section_t section = env->section;
1141 ir_variability variability = get_entity_variability(ent);
1142 ir_visibility visibility = get_entity_visibility(ent);
1144 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1148 if (section != (be_gas_section_t) -1) {
1150 } else if (variability == variability_constant) {
1151 /* a constant entity, put it on the rdata */
1152 section = GAS_SECTION_RODATA;
1153 if (be_gas_flavour == GAS_FLAVOUR_MACH_O
1154 && ent_is_string_const(ent)) {
1155 section = GAS_SECTION_CSTRING;
1157 } else if (variability == variability_uninitialized) {
1158 /* uninitialized entity put it in bss segment */
1159 section = GAS_SECTION_COMMON;
1160 if (visibility != visibility_local)
1163 section = GAS_SECTION_DATA;
1166 if (!emit_as_common) {
1167 be_gas_emit_switch_section(section);
1170 be_dbg_variable(ent);
1172 /* global or not global */
1173 if (visibility == visibility_external_visible && !emit_as_common) {
1174 be_emit_cstring(".globl\t");
1175 be_emit_ident(ld_ident);
1177 be_emit_write_line();
1178 } else if (visibility == visibility_external_allocated) {
1179 be_emit_cstring(".globl\t");
1180 be_emit_ident(ld_ident);
1182 be_emit_write_line();
1183 /* we can return now... */
1187 if (align > 1 && !emit_as_common && section != GAS_SECTION_PIC_TRAMPOLINES
1188 && section != GAS_SECTION_PIC_SYMBOLS) {
1192 if (visibility != visibility_external_allocated && !emit_as_common
1193 && be_gas_flavour == GAS_FLAVOUR_ELF) {
1194 be_emit_cstring("\t.type\t");
1195 be_emit_ident(ld_ident);
1196 be_emit_cstring(", @object\n\t.size\t");
1197 be_emit_ident(ld_ident);
1198 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1201 if (!emit_as_common) {
1202 be_emit_ident(ld_ident);
1203 be_emit_cstring(":\n");
1204 be_emit_write_line();
1207 if (variability == variability_uninitialized) {
1208 if (emit_as_common) {
1209 switch (be_gas_flavour) {
1210 case GAS_FLAVOUR_ELF:
1211 case GAS_FLAVOUR_MACH_O:
1212 case GAS_FLAVOUR_YASM:
1213 be_emit_irprintf("\t.comm %s,%u,%u\n",
1214 get_id_str(ld_ident), get_type_size_bytes(type), align);
1215 be_emit_write_line();
1217 case GAS_FLAVOUR_MINGW:
1218 be_emit_irprintf("\t.comm %s,%u # %u\n",
1219 get_id_str(ld_ident), get_type_size_bytes(type), align);
1220 be_emit_write_line();
1223 } else if (section == GAS_SECTION_PIC_TRAMPOLINES
1224 || section == GAS_SECTION_PIC_SYMBOLS) {
1225 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
1226 be_emit_cstring("\t.indirect_symbol ");
1227 be_emit_ident(get_entity_ident(ent));
1229 be_emit_write_line();
1230 if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1231 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1232 be_emit_write_line();
1234 assert(section == GAS_SECTION_PIC_SYMBOLS);
1235 be_emit_cstring("\t.long 0\n");
1236 be_emit_write_line();
1239 panic("PIC trampolines not yet supported in this gas mode");
1242 be_emit_irprintf("\t.space %u\n", get_type_size_bytes(type));
1243 be_emit_write_line();
1246 if (is_atomic_entity(ent)) {
1247 dump_atomic_init(env, get_atomic_ent_value(ent));
1249 /* sort_compound_ent_values(ent); */
1251 switch (get_type_tpop_code(get_entity_type(ent))) {
1253 if (ent_is_string_const(ent))
1254 dump_string_cst(ent);
1256 dump_compound_init(env, ent);
1261 dump_compound_init(env, ent);
1264 panic("Unimplemented type kind in dump_global()");
1271 * Dumps declarations of global variables and the initialization code.
1273 * @param gt a global like type, either the global or the TLS one
1274 * @param env an environment
1275 * @param only_emit_marked if non-zero, external allocated entities that do not have
1276 * its visited flag set are ignored
1278 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
1279 int only_emit_marked)
1281 int i, n = get_compound_n_members(gt);
1282 waitq *worklist = new_waitq();
1284 if (only_emit_marked) {
1285 for (i = 0; i < n; i++) {
1286 ir_entity *ent = get_compound_member(gt, i);
1287 if (is_entity_backend_marked(ent) ||
1288 get_entity_visibility(ent) != visibility_external_allocated) {
1289 waitq_put(worklist, ent);
1290 set_entity_backend_marked(ent, 1);
1294 for (i = 0; i < n; i++) {
1295 ir_entity *ent = get_compound_member(gt, i);
1296 set_entity_backend_marked(ent, 1);
1297 waitq_put(worklist, ent);
1301 env->worklist = worklist;
1303 while (!waitq_empty(worklist)) {
1304 ir_entity *ent = waitq_get(worklist);
1306 dump_global(env, ent);
1309 del_waitq(worklist);
1310 env->worklist = NULL;
1313 /************************************************************************/
1315 /* Generate all entities. */
1316 void be_gas_emit_decls(const be_main_env_t *main_env,
1317 int only_emit_marked_entities)
1319 be_gas_decl_env_t env;
1320 memset(&env, 0, sizeof(env));
1322 /* dump global type */
1323 env.section = (be_gas_section_t) -1;
1324 be_gas_dump_globals(get_glob_type(), &env, only_emit_marked_entities);
1325 env.section = GAS_SECTION_TLS;
1326 be_gas_dump_globals(get_tls_type(), &env, only_emit_marked_entities);
1327 env.section = GAS_SECTION_CONSTRUCTORS;
1328 be_gas_dump_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env,
1329 only_emit_marked_entities);
1330 env.section = GAS_SECTION_DESTRUCTORS;
1331 be_gas_dump_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env,
1332 only_emit_marked_entities);
1334 env.section = GAS_SECTION_PIC_SYMBOLS;
1335 be_gas_dump_globals(main_env->pic_symbols_type, &env,
1336 only_emit_marked_entities);
1338 if (get_compound_n_members(main_env->pic_trampolines_type) > 0) {
1339 env.section = GAS_SECTION_PIC_TRAMPOLINES;
1340 be_gas_dump_globals(main_env->pic_trampolines_type, &env,
1341 only_emit_marked_entities);
1342 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
1343 be_emit_cstring("\t.subsections_via_symbols\n");
1344 be_emit_write_line();