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
44 #include "beemitter.h"
45 #include "be_dbgout.h"
47 /** by default, we generate assembler code for the Linux gas */
48 object_file_format_t be_gas_object_file_format = OBJECT_FILE_FORMAT_ELF;
49 bool be_gas_emit_types = true;
50 char be_gas_elf_type_char = '@';
52 static be_gas_section_t current_section = (be_gas_section_t) -1;
55 * Return the pseudo-instruction to be issued for a section switch
56 * depending on the current flavour.
58 * @param section the section to switch to
60 * @return the pseudo-instruction
62 static const char *get_section_name(be_gas_section_t section)
64 static const char *text[OBJECT_FILE_FORMAT_LAST+1][GAS_SECTION_LAST+1] = {
65 { /* OBJECT_FILE_FORMAT_ELF */
70 ".section\t.tdata,\"awT\",@progbits",
71 ".section\t.tbss,\"awT\",@nobits",
72 ".section\t.ctors,\"aw\",@progbits",
73 ".section\t.dtors,\"aw\",@progbits",
74 NULL, /* no cstring section */
78 { /* OBJECT_FILE_FORMAT_COFF */
81 ".section\t.rdata,\"dr\"",
83 ".section\t.tdata,\"awT\",@progbits",
84 ".section\t.tbss,\"awT\",@nobits",
85 ".section\t.ctors,\"w\"",
86 ".section\t.dtors,\"w\"",
91 { /* OBJECT_FILE_FORMAT_MACH_O */
96 NULL, /* TLS is not supported on Mach-O */
97 NULL, /* TLS is not supported on Mach-O */
101 ".section\t__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5",
102 ".section\t__IMPORT,__pointers,non_lazy_symbol_pointers"
106 assert((int) be_gas_object_file_format >= 0
107 && be_gas_object_file_format <= OBJECT_FILE_FORMAT_LAST);
108 assert((int) section >= 0 && section <= GAS_SECTION_LAST);
109 return text[be_gas_object_file_format][section];
112 void be_gas_emit_switch_section(be_gas_section_t section)
114 if (current_section == section)
118 be_emit_string(get_section_name(section));
120 be_emit_write_line();
121 current_section = section;
124 static void emit_visibility(const ir_entity *entity)
126 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
127 be_emit_cstring("\t.weak ");
128 be_gas_emit_entity(entity);
130 be_emit_write_line();
131 /* Note: no need ot output .global after a .weak */
132 } else if (get_entity_visibility(entity) == ir_visibility_default) {
133 be_emit_cstring(".globl ");
134 be_gas_emit_entity(entity);
136 be_emit_write_line();
140 void be_gas_emit_function_prolog(const ir_entity *entity, unsigned po2alignment)
142 be_gas_emit_switch_section(GAS_SECTION_TEXT);
144 /* write the begin line (makes the life easier for scripts parsing the
146 be_emit_write_line();
147 be_emit_cstring("# -- Begin ");
148 be_gas_emit_entity(entity);
150 be_emit_write_line();
152 if (po2alignment > 0) {
153 const char *fill_byte = "";
154 unsigned maximum_skip = (1 << po2alignment) - 1;
155 /* gcc fills space between function with 0x90... */
156 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
159 be_emit_cstring("\t.p2align ");
160 be_emit_irprintf("%u,%s,%u\n", po2alignment, fill_byte, maximum_skip);
161 be_emit_write_line();
163 emit_visibility(entity);
165 switch (be_gas_object_file_format) {
166 case OBJECT_FILE_FORMAT_ELF:
167 be_emit_cstring("\t.type\t");
168 be_gas_emit_entity(entity);
169 be_emit_cstring(", ");
170 be_emit_char(be_gas_elf_type_char);
171 be_emit_cstring("function\n");
172 be_emit_write_line();
174 case OBJECT_FILE_FORMAT_COFF:
175 be_emit_cstring("\t.def\t");
176 be_gas_emit_entity(entity);
177 be_emit_cstring(";");
178 if (get_entity_visibility(entity) == ir_visibility_local) {
179 be_emit_cstring("\t.scl\t3;");
181 be_emit_cstring("\t.scl\t2;");
183 be_emit_cstring("\t.type\t32;\t.endef\n");
184 be_emit_write_line();
186 case OBJECT_FILE_FORMAT_MACH_O:
189 be_gas_emit_entity(entity);
190 be_emit_cstring(":\n");
191 be_emit_write_line();
194 void be_gas_emit_function_epilog(const ir_entity *entity)
196 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF) {
197 be_emit_cstring("\t.size\t");
198 be_gas_emit_entity(entity);
199 be_emit_cstring(", .-");
200 be_gas_emit_entity(entity);
202 be_emit_write_line();
205 be_emit_cstring("# -- End ");
206 be_gas_emit_entity(entity);
208 be_emit_write_line();
212 * An environment containing all needed dumper data.
213 * Currently we create the file completely in memory first, then
214 * write it to the disk. This is an artifact from the old C-generating backend
215 * and even there NOT needed. So we might change it in the future.
217 typedef struct _be_gas_decl_env {
218 be_gas_section_t section;
219 const be_main_env_t *main_env;
222 /************************************************************************/
227 * @param tv the tarval
228 * @param bytes the width of the tarvals value in bytes
230 static void dump_arith_tarval(tarval *tv, int bytes)
234 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
238 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
242 be_emit_irprintf("0x%02x%02x%02x%02x",
243 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
247 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
248 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
249 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
253 /* Beware: Mixed endian output! One little endian number emitted as
254 * three longs. Each long initializer is written in big endian. */
256 "\t.long\t0x%02x%02x%02x%02x\n"
257 "\t.long\t0x%02x%02x%02x%02x\n"
258 "\t.long\t0x%02x%02x%02x%02x",
259 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
260 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
261 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
262 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
263 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
264 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8)
269 /* Beware: Mixed endian output! One little endian number emitted as
270 * three longs. Each long initializer is written in big endian. */
272 "\t.long\t0x%02x%02x%02x%02x\n"
273 "\t.long\t0x%02x%02x%02x%02x\n"
274 "\t.long\t0x%02x%02x%02x%02x\n"
275 "\t.long\t0x%02x%02x%02x%02x",
276 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
277 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
278 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
279 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
280 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
281 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
282 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 14),
283 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12)
288 panic("Can't dump a tarval with %d bytes", bytes);
292 * Return the label prefix for labeled blocks.
294 const char *be_gas_block_label_prefix(void)
300 * Return the label prefix for labeled instructions.
302 const char *be_gas_insn_label_prefix(void)
308 * Return the tarval of an atomic initializer.
310 * @param init a node representing the initializer (on the const code irg)
314 static tarval *get_atomic_init_tv(ir_node *init)
317 ir_mode *mode = get_irn_mode(init);
319 switch (get_irn_opcode(init)) {
322 init = get_Cast_op(init);
326 init = get_Conv_op(init);
330 return get_Const_tarval(init);
333 switch (get_SymConst_kind(init)) {
334 case symconst_type_size:
335 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
337 case symconst_type_align:
338 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
340 case symconst_ofs_ent:
341 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
343 case symconst_enum_const:
344 return get_enumeration_value(get_SymConst_enum(init));
357 * Dump an atomic value.
359 * @param env the gas output environment
360 * @param init a node representing the atomic value (on the const code irg)
362 static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
364 ir_mode *mode = get_irn_mode(init);
365 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 be_gas_emit_entity(ent);
398 case symconst_ofs_ent:
399 ent = get_SymConst_entity(init);
400 be_emit_irprintf("%d", get_entity_offset(ent));
403 case symconst_type_size:
404 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
407 case symconst_type_align:
408 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
411 case symconst_enum_const:
412 tv = get_enumeration_value(get_SymConst_enum(init));
413 dump_arith_tarval(tv, bytes);
417 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
422 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
423 panic("Constant must be int or pointer for '+' to work");
425 do_dump_atomic_init(env, get_Add_left(init));
426 be_emit_cstring(" + ");
427 do_dump_atomic_init(env, get_Add_right(init));
431 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
432 panic("Constant must be int or pointer for '-' to work");
434 do_dump_atomic_init(env, get_Sub_left(init));
435 be_emit_cstring(" - ");
436 do_dump_atomic_init(env, get_Sub_right(init));
440 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
441 panic("Constant must be int or pointer for '*' to work");
443 do_dump_atomic_init(env, get_Mul_left(init));
444 be_emit_cstring(" * ");
445 do_dump_atomic_init(env, get_Mul_right(init));
449 be_emit_cstring("0");
453 panic("dump_atomic_init(): unsupported IR-node %+F", init);
458 * Dumps the type for given size (.byte, .long, ...)
460 * @param size the size in bytes
462 static void dump_size_type(size_t size)
466 be_emit_cstring("\t.byte\t");
470 be_emit_cstring("\t.short\t");
474 be_emit_cstring("\t.long\t");
478 be_emit_cstring("\t.quad\t");
483 case 16: /* Note: .octa does not work on mac */
484 /* handled in arith */
488 panic("Try to dump a type with %u bytes", (unsigned)size);
493 * Emit an atomic value.
495 * @param env the gas output environment
496 * @param init a node representing the atomic value (on the const code irg)
498 static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
500 ir_mode *mode = get_irn_mode(init);
501 int bytes = get_mode_size_bytes(mode);
503 dump_size_type(bytes);
504 do_dump_atomic_init(env, init);
506 be_emit_write_line();
509 /************************************************************************/
510 /* Routines to dump global variables */
511 /************************************************************************/
513 static int initializer_is_string_const(const ir_initializer_t *initializer)
516 int found_printable = 0;
518 if (initializer->kind != IR_INITIALIZER_COMPOUND)
521 len = initializer->compound.n_initializers;
524 for (i = 0; i < len; ++i) {
528 ir_initializer_t *sub_initializer
529 = initializer->compound.initializers[i];
531 if (sub_initializer->kind != IR_INITIALIZER_TARVAL)
534 tv = sub_initializer->tarval.value;
535 mode = get_tarval_mode(tv);
537 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
540 c = get_tarval_long(tv);
541 if (isgraph(c) || isspace(c))
546 if (i == len - 1 && c != '\0')
550 return found_printable;
554 * Determine if an entity is a string constant
555 * @param ent The entity
556 * @return 1 if it is a string constant, 0 otherwise
558 static int ent_is_string_const(const ir_entity *ent)
560 ir_type *type, *element_type;
564 type = get_entity_type(ent);
566 /* if it's an array */
567 if (!is_Array_type(type))
570 element_type = get_array_element_type(type);
572 /* and the array's element type is primitive */
573 if (!is_Primitive_type(element_type))
576 /* and the mode of the element type is an int of
577 * the same size as the byte mode */
578 mode = get_type_mode(element_type);
579 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
582 if (ent->initializer != NULL) {
583 return initializer_is_string_const(ent->initializer);
584 } else if (entity_has_compound_ent_values(ent)) {
585 int found_printable = 0;
586 /* if it contains only printable chars and a 0 at the end */
587 n = get_compound_ent_n_values(ent);
588 for (i = 0; i < n; ++i) {
589 ir_node *irn = get_compound_ent_value(ent, i);
593 c = (int) get_tarval_long(get_Const_tarval(irn));
595 if (isgraph(c) || isspace(c))
600 if (i == n - 1 && c != '\0')
603 return found_printable;
609 static bool initializer_is_null(const ir_initializer_t *initializer)
611 switch (initializer->kind) {
612 case IR_INITIALIZER_NULL:
614 case IR_INITIALIZER_TARVAL: {
615 tarval *tv = initializer->tarval.value;
616 return tarval_is_null(tv);
618 case IR_INITIALIZER_CONST: {
619 ir_node *value = initializer->consti.value;
620 if (!is_Const(value))
622 return is_Const_null(value);
624 case IR_INITIALIZER_COMPOUND: {
626 for (i = 0; i < initializer->compound.n_initializers; ++i) {
627 ir_initializer_t *subinitializer
628 = initializer->compound.initializers[i];
629 if (!initializer_is_null(subinitializer))
635 panic("invalid initializer in initializer_is_null");
638 static bool entity_is_null(const ir_entity *entity)
640 if (entity->initializer != NULL) {
641 return initializer_is_null(entity->initializer);
642 } else if (entity_has_compound_ent_values(entity)) {
643 /* I'm too lazy to implement this case as compound graph paths will be
644 * remove anyway in the future */
646 /* uninitialized, NULL is fine */
651 * Dump a string constant.
652 * No checks are made!!
654 * @param ent The entity to dump.
656 static void dump_string_cst(const ir_entity *ent)
664 len = get_compound_ent_n_values(ent);
666 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
667 be_emit_cstring("\t.ascii \"");
669 be_emit_cstring("\t.string \"");
673 for (i = 0; i < output_len; ++i) {
677 irn = get_compound_ent_value(ent, i);
678 c = (int) get_tarval_long(get_Const_tarval(irn));
681 case '"' : be_emit_cstring("\\\""); break;
682 case '\n': be_emit_cstring("\\n"); break;
683 case '\r': be_emit_cstring("\\r"); break;
684 case '\t': be_emit_cstring("\\t"); break;
685 case '\\': be_emit_cstring("\\\\"); break;
690 be_emit_irprintf("\\%o", c);
694 be_emit_cstring("\"\n");
695 be_emit_write_line();
697 type = get_entity_type(ent);
698 type_size = get_type_size_bytes(type);
699 remaining_space = type_size - len;
700 assert(remaining_space >= 0);
701 if (remaining_space > 0) {
702 be_emit_irprintf("\t.zero\t%d\n", remaining_space);
706 static void dump_string_initializer(const ir_initializer_t *initializer)
710 len = initializer->compound.n_initializers;
711 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
712 be_emit_cstring("\t.ascii \"");
714 be_emit_cstring("\t.string \"");
718 for (i = 0; i < len; ++i) {
719 const ir_initializer_t *sub_initializer
720 = get_initializer_compound_value(initializer, i);
722 tarval *tv = get_initializer_tarval_value(sub_initializer);
723 int c = get_tarval_long(tv);
726 case '"' : be_emit_cstring("\\\""); break;
727 case '\n': be_emit_cstring("\\n"); break;
728 case '\r': be_emit_cstring("\\r"); break;
729 case '\t': be_emit_cstring("\\t"); break;
730 case '\\': be_emit_cstring("\\\\"); break;
735 be_emit_irprintf("\\%o", c);
739 be_emit_cstring("\"\n");
740 be_emit_write_line();
743 enum normal_or_bitfield_kind {
750 enum normal_or_bitfield_kind kind;
754 unsigned char bf_val;
756 } normal_or_bitfield;
758 static int is_type_variable_size(ir_type *type)
765 static size_t get_initializer_size(const ir_initializer_t *initializer,
768 switch (get_initializer_kind(initializer)) {
769 case IR_INITIALIZER_TARVAL:
770 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
771 return get_type_size_bytes(type);
772 case IR_INITIALIZER_CONST:
773 case IR_INITIALIZER_NULL:
774 return get_type_size_bytes(type);
775 case IR_INITIALIZER_COMPOUND:
776 if (!is_type_variable_size(type)) {
777 return get_type_size_bytes(type);
780 = get_initializer_compound_n_entries(initializer);
782 unsigned initializer_size = get_type_size_bytes(type);
783 for (i = 0; i < n_entries; ++i) {
784 ir_entity *entity = get_compound_member(type, i);
785 ir_type *type = get_entity_type(entity);
787 const ir_initializer_t *sub_initializer
788 = get_initializer_compound_value(initializer, i);
790 unsigned offset = get_entity_offset(entity);
791 unsigned size = get_initializer_size(sub_initializer, type);
793 if (offset + size > initializer_size) {
794 initializer_size = offset + size;
797 return initializer_size;
801 panic("found invalid initializer");
805 static normal_or_bitfield *glob_vals;
806 static size_t max_vals;
809 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
810 const ir_initializer_t *initializer, ir_type *type)
812 unsigned char last_bits = 0;
813 ir_mode *mode = get_type_mode(type);
815 unsigned char curr_bits;
819 switch (get_initializer_kind(initializer)) {
820 case IR_INITIALIZER_NULL:
822 case IR_INITIALIZER_TARVAL:
823 tv = get_initializer_tarval_value(initializer);
825 case IR_INITIALIZER_CONST: {
826 ir_node *node = get_initializer_const_value(initializer);
827 if (!is_Const(node)) {
828 panic("bitfield initializer not a Const node");
830 tv = get_Const_tarval(node);
833 case IR_INITIALIZER_COMPOUND:
834 panic("bitfield initializer is compound");
837 panic("Couldn't get numeric value for bitfield initializer");
839 tv = tarval_convert_to(tv, get_type_mode(type));
841 /* normalize offset */
842 vals += offset_bits >> 3;
844 value_len = get_mode_size_bits(mode);
846 /* combine bits with existing bits */
847 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
848 assert((size_t) (vals - glob_vals) + j < max_vals);
849 assert(vals[j].kind == BITFIELD ||
850 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
851 vals[j].kind = BITFIELD;
852 curr_bits = get_tarval_sub_bits(tv, j);
854 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
856 last_bits = curr_bits;
860 static void dump_ir_initializer(normal_or_bitfield *vals,
861 const ir_initializer_t *initializer,
864 assert((size_t) (vals - glob_vals) < max_vals);
866 switch (get_initializer_kind(initializer)) {
867 case IR_INITIALIZER_NULL:
869 case IR_INITIALIZER_TARVAL: {
872 assert(vals->kind != BITFIELD);
874 vals->v.tarval = get_initializer_tarval_value(initializer);
875 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
876 for (i = 1; i < get_type_size_bytes(type); ++i) {
877 vals[i].kind = NORMAL;
878 vals[i].v.value = NULL;
882 case IR_INITIALIZER_CONST: {
885 assert(vals->kind != BITFIELD);
887 vals->v.value = get_initializer_const_value(initializer);
888 for (i = 1; i < get_type_size_bytes(type); ++i) {
889 vals[i].kind = NORMAL;
890 vals[i].v.value = NULL;
894 case IR_INITIALIZER_COMPOUND: {
896 size_t n = get_initializer_compound_n_entries(initializer);
898 if (is_Array_type(type)) {
899 ir_type *element_type = get_array_element_type(type);
900 size_t skip = get_type_size_bytes(element_type);
901 size_t alignment = get_type_alignment_bytes(element_type);
902 size_t misalign = skip % alignment;
904 skip += alignment - misalign;
907 for (i = 0; i < n; ++i) {
908 ir_initializer_t *sub_initializer
909 = get_initializer_compound_value(initializer, i);
911 dump_ir_initializer(vals, sub_initializer, element_type);
917 assert(is_compound_type(type));
918 n_members = get_compound_n_members(type);
919 for (i = 0; i < n_members; ++i) {
920 ir_entity *member = get_compound_member(type, i);
921 size_t offset = get_entity_offset(member);
922 ir_type *subtype = get_entity_type(member);
923 ir_mode *mode = get_type_mode(subtype);
924 ir_initializer_t *sub_initializer;
926 assert(i < get_initializer_compound_n_entries(initializer));
928 = get_initializer_compound_value(initializer, i);
932 = get_entity_offset_bits_remainder(member);
933 size_t value_len = get_mode_size_bits(mode);
935 if (offset_bits != 0 ||
936 (value_len != 8 && value_len != 16 && value_len != 32
937 && value_len != 64)) {
938 dump_bitfield(&vals[offset], offset_bits,
939 sub_initializer, subtype);
944 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
951 panic("invalid ir_initializer kind found");
954 static void dump_initializer(be_gas_decl_env_t *env, const ir_entity *entity)
956 const ir_initializer_t *initializer = entity->initializer;
958 normal_or_bitfield *vals;
962 if (initializer_is_string_const(initializer)) {
963 dump_string_initializer(initializer);
967 type = get_entity_type(entity);
968 size = get_initializer_size(initializer, type);
974 * In the worst case, every initializer allocates one byte.
975 * Moreover, initializer might be big, do not allocate on stack.
977 vals = XMALLOCNZ(normal_or_bitfield, size);
984 dump_ir_initializer(vals, initializer, type);
986 /* now write values sorted */
987 for (k = 0; k < size; ) {
990 if (vals[k].kind == NORMAL) {
991 if (vals[k].v.value != NULL) {
992 dump_atomic_init(env, vals[k].v.value);
993 elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
997 } else if (vals[k].kind == TARVAL) {
998 tarval *tv = vals[k].v.tarval;
999 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
1004 dump_size_type(size);
1005 dump_arith_tarval(tv, size);
1007 be_emit_write_line();
1009 assert(vals[k].kind == BITFIELD);
1010 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1011 be_emit_write_line();
1015 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1022 be_emit_irprintf("\t.space\t%d\n", space);
1023 be_emit_write_line();
1029 static void dump_compound_graph_init(be_gas_decl_env_t *env,
1030 const ir_entity *ent)
1032 normal_or_bitfield *vals;
1034 unsigned k, last_ofs;
1036 if (ent_is_string_const(ent)) {
1037 dump_string_cst(ent);
1041 n = get_compound_ent_n_values(ent);
1043 /* Find the initializer size. Sorrily gcc support a nasty feature:
1044 The last field of a compound may be a flexible array. This allows
1045 initializers bigger than the type size. */
1046 last_ofs = get_type_size_bytes(get_entity_type(ent));
1047 for (i = 0; i < n; ++i) {
1048 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1049 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1050 ir_node *value = get_compound_ent_value(ent, i);
1051 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
1053 offset += (value_len + bits_remainder + 7) >> 3;
1055 if (offset > last_ofs) {
1061 * In the worst case, every initializer allocates one byte.
1062 * Moreover, initializer might be big, do not allocate on stack.
1064 vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1066 /* collect the values and store them at the offsets */
1067 for (i = 0; i < n; ++i) {
1068 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1069 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1070 ir_node *value = get_compound_ent_value(ent, i);
1071 int value_len = get_mode_size_bits(get_irn_mode(value));
1073 assert(offset_bits >= 0);
1075 if (offset_bits != 0 ||
1076 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1077 tarval *tv = get_atomic_init_tv(value);
1078 unsigned char curr_bits, last_bits = 0;
1080 panic("Couldn't get numeric value for bitfield initializer '%s'",
1081 get_entity_ld_name(ent));
1083 /* normalize offset */
1084 offset += offset_bits >> 3;
1087 for (j = 0; value_len + offset_bits > 0; ++j) {
1088 assert(offset + j < last_ofs);
1089 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1090 vals[offset + j].kind = BITFIELD;
1091 curr_bits = get_tarval_sub_bits(tv, j);
1092 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1094 last_bits = curr_bits;
1099 assert(offset < last_ofs);
1100 assert(vals[offset].kind == NORMAL);
1101 for (i = 1; i < value_len / 8; ++i) {
1102 assert(vals[offset + i].v.value == NULL);
1104 vals[offset].v.value = value;
1108 /* now write them sorted */
1109 for (k = 0; k < last_ofs; ) {
1110 int space = 0, skip = 0;
1111 if (vals[k].kind == NORMAL) {
1112 if (vals[k].v.value != NULL) {
1113 dump_atomic_init(env, vals[k].v.value);
1114 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1119 assert(vals[k].kind == BITFIELD);
1120 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1124 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1133 be_emit_irprintf("\t.space\t%d\n", space);
1134 be_emit_write_line();
1140 static void emit_align(unsigned p2alignment)
1142 be_emit_irprintf("\t.p2align\t%u\n", log2_floor(p2alignment));
1143 be_emit_write_line();
1146 static unsigned get_effective_entity_alignment(const ir_entity *entity)
1148 unsigned alignment = get_entity_alignment(entity);
1149 if (alignment == 0) {
1150 ir_type *type = get_entity_type(entity);
1151 alignment = get_type_alignment_bytes(type);
1156 static be_gas_section_t determine_section(be_gas_decl_env_t *env,
1157 const ir_entity *entity)
1159 ir_type *owner = get_entity_owner(entity);
1161 if (owner == get_segment_type(IR_SEGMENT_GLOBAL)) {
1162 if (is_method_entity(entity))
1163 return GAS_SECTION_TEXT;
1165 ir_linkage linkage = get_entity_linkage(entity);
1166 if (linkage & IR_LINKAGE_CONSTANT) {
1167 /* mach-o is the only one with a cstring section */
1168 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
1169 && ent_is_string_const(entity))
1170 return GAS_SECTION_CSTRING;
1172 return GAS_SECTION_RODATA;
1174 if (entity_is_null(entity))
1175 return GAS_SECTION_BSS;
1177 return GAS_SECTION_DATA;
1178 } else if (owner == env->main_env->pic_symbols_type) {
1179 return GAS_SECTION_PIC_SYMBOLS;
1180 } else if (owner == env->main_env->pic_trampolines_type) {
1181 return GAS_SECTION_PIC_TRAMPOLINES;
1182 } else if (owner == get_segment_type(IR_SEGMENT_CONSTRUCTORS)) {
1183 return GAS_SECTION_CONSTRUCTORS;
1184 } else if (owner == get_segment_type(IR_SEGMENT_DESTRUCTORS)) {
1185 return GAS_SECTION_DESTRUCTORS;
1186 } else if (owner == get_segment_type(IR_SEGMENT_THREAD_LOCAL)) {
1187 ir_linkage linkage = get_entity_linkage(entity);
1188 if (linkage & IR_LINKAGE_MERGE) {
1189 panic("IR_LINKAGE_MERGE not supported for THREAD_LOCAL entities");
1192 if (entity_is_null(entity)) {
1193 return GAS_SECTION_TLS_BSS;
1195 return GAS_SECTION_TLS_DATA;
1198 panic("Couldn't determine section for %+F?!?", entity);
1201 static void emit_common(const ir_entity *entity)
1203 const char *name = get_entity_ld_name(entity);
1204 unsigned size = get_type_size_bytes(get_entity_type(entity));
1205 unsigned alignment = get_effective_entity_alignment(entity);
1206 ir_visibility visibility = get_entity_visibility(entity);
1208 if (visibility == ir_visibility_local
1209 || visibility == ir_visibility_private) {
1210 /* counter the visibility_global effect of .comm
1211 * ... and to be honest I have no idea what local common symbols
1214 be_emit_irprintf("\t.local %s\n", name);
1215 be_emit_write_line();
1218 switch (be_gas_object_file_format) {
1219 case OBJECT_FILE_FORMAT_MACH_O:
1220 be_emit_irprintf("\t.comm %s,%u,%u\n", name, size,
1221 log2_floor(alignment));
1222 be_emit_write_line();
1224 case OBJECT_FILE_FORMAT_ELF:
1225 be_emit_irprintf("\t.comm %s,%u,%u\n", name, size, alignment);
1226 be_emit_write_line();
1228 case OBJECT_FILE_FORMAT_COFF:
1229 be_emit_irprintf("\t.comm %s,%u # %u\n", name, size, alignment);
1230 be_emit_write_line();
1233 panic("invalid object file format");
1236 static void dump_indirect_symbol(const ir_entity *entity, be_gas_section_t section)
1238 /* we can only do PIC code on macho so far */
1239 assert(be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O);
1241 be_emit_ident(get_entity_ld_ident(entity));
1242 be_emit_cstring(":\n");
1243 be_emit_write_line();
1244 be_emit_cstring("\t.indirect_symbol ");
1245 be_emit_ident(get_entity_ident(entity));
1247 be_emit_write_line();
1248 if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1249 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1250 be_emit_write_line();
1252 assert(section == GAS_SECTION_PIC_SYMBOLS);
1253 be_emit_cstring("\t.long 0\n");
1254 be_emit_write_line();
1258 void be_gas_emit_entity(const ir_entity *entity)
1260 if (entity->type == firm_code_type) {
1261 ir_label_t label = get_entity_label(entity);
1262 be_emit_string(be_gas_block_label_prefix());
1263 be_emit_irprintf("%lu", label);
1267 if (get_entity_visibility(entity) == ir_visibility_private) {
1268 be_emit_cstring(".L");
1270 be_emit_ident(get_entity_ld_ident(entity));
1274 * Dump a global entity.
1276 * @param env the gas output environment
1277 * @param ent the entity to be dumped
1279 static void dump_global(be_gas_decl_env_t *env, const ir_entity *ent)
1281 ir_type *type = get_entity_type(ent);
1282 ident *ld_ident = get_entity_ld_ident(ent);
1283 unsigned alignment = get_effective_entity_alignment(ent);
1284 be_gas_section_t section = determine_section(env, ent);
1285 ir_visibility visibility = get_entity_visibility(ent);
1286 ir_linkage linkage = get_entity_linkage(ent);
1288 /* block labels are already emittet in the code */
1289 if (type == firm_code_type)
1292 /* we already emitted all methods. Except for the trampolines which
1293 * the assembler/linker generates */
1294 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1295 /* functions with graph are already emitted with
1296 * be_gas_emit_function_prolog */
1297 if (get_entity_irg(ent) == NULL) {
1298 emit_visibility(ent);
1303 be_dbg_variable(ent);
1305 emit_visibility(ent);
1306 if (visibility == ir_visibility_external) {
1307 /* nothing to do for externally defined values */
1311 if (!is_po2(alignment))
1312 panic("alignment not a power of 2");
1314 if (section == GAS_SECTION_BSS && (linkage & IR_LINKAGE_MERGE)) {
1315 if (get_entity_visibility(ent) == ir_visibility_external) {
1316 panic("merge link semantic not supported for extern entities");
1322 be_gas_emit_switch_section(section);
1324 if (section == GAS_SECTION_PIC_TRAMPOLINES
1325 || section == GAS_SECTION_PIC_SYMBOLS) {
1326 dump_indirect_symbol(ent, section);
1331 if (alignment > 1) {
1332 emit_align(alignment);
1334 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF
1335 && be_gas_emit_types) {
1336 be_emit_cstring("\t.type\t");
1337 be_gas_emit_entity(ent);
1338 be_emit_cstring(", ");
1339 be_emit_char(be_gas_elf_type_char);
1340 be_emit_cstring("object\n\t.size\t");\
1341 be_gas_emit_entity(ent);
1342 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1345 if (get_id_str(ld_ident)[0] != '\0') {
1346 be_gas_emit_entity(ent);
1347 be_emit_cstring(":\n");
1348 be_emit_write_line();
1351 if (entity_is_null(ent)) {
1352 be_emit_irprintf("\t.zero %u\n", get_type_size_bytes(type));
1353 be_emit_write_line();
1354 } else if(entity_has_compound_ent_values(ent)) {
1355 dump_compound_graph_init(env, ent);
1357 assert(ent->initializer != NULL);
1358 dump_initializer(env, ent);
1363 * Dumps declarations of global variables and the initialization code.
1365 * @param gt a global like type, either the global or the TLS one
1366 * @param env an environment
1368 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env)
1370 int i, n = get_compound_n_members(gt);
1372 for (i = 0; i < n; i++) {
1373 ir_entity *ent = get_compound_member(gt, i);
1374 dump_global(env, ent);
1378 /************************************************************************/
1380 /* Generate all entities. */
1381 void be_gas_emit_decls(const be_main_env_t *main_env)
1383 be_gas_decl_env_t env;
1384 memset(&env, 0, sizeof(env));
1386 /* dump global type */
1387 env.main_env = main_env;
1388 env.section = (be_gas_section_t) -1;
1390 be_gas_dump_globals(get_glob_type(), &env);
1391 be_gas_dump_globals(get_tls_type(), &env);
1392 be_gas_dump_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env);
1393 be_gas_dump_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env);
1394 be_gas_dump_globals(main_env->pic_symbols_type, &env);
1395 be_gas_dump_globals(main_env->pic_trampolines_type, &env);
1398 * ".subsections_via_symbols marks object files which are OK to divide
1399 * their section contents into individual blocks".
1400 * From my understanding this means no label points in the middle of an
1401 * object which we want to address as a whole. Firm code should be fine
1404 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
1405 be_emit_cstring("\t.subsections_via_symbols\n");
1406 be_emit_write_line();