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.tbss,\"awT\",@nobits",
71 ".section\t.ctors,\"aw\",@progbits",
72 ".section\t.dtors,\"aw\",@progbits",
73 NULL, /* no cstring section */
77 { /* OBJECT_FILE_FORMAT_COFF */
80 ".section .rdata,\"dr\"",
82 ".section\t.tbss,\"awT\",@nobits",
83 ".section\t.ctors,\"w\"",
84 ".section\t.dtors,\"w\"",
89 { /* OBJECT_FILE_FORMAT_MACH_O */
94 NULL, /* TLS is not supported on Mach-O */
98 ".section\t__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5",
99 ".section\t__IMPORT,__pointers,non_lazy_symbol_pointers"
103 assert((int) be_gas_object_file_format >= 0
104 && be_gas_object_file_format <= OBJECT_FILE_FORMAT_LAST);
105 assert((int) section >= 0 && section <= GAS_SECTION_LAST);
106 return text[be_gas_object_file_format][section];
109 void be_gas_emit_switch_section(be_gas_section_t section)
111 if (current_section == section)
115 be_emit_string(get_section_name(section));
117 be_emit_write_line();
118 current_section = section;
121 static void emit_entity_visibility(const ir_entity *entity)
123 ir_visibility visibility = get_entity_visibility(entity);
124 ir_linkage linkage = get_entity_linkage(entity);
126 if (visibility != ir_visibility_local) {
127 be_emit_cstring(".globl ");
128 be_emit_ident(get_entity_ld_ident(entity));
130 be_emit_write_line();
132 if (linkage & IR_LINKAGE_WEAK) {
133 if (! (linkage & IR_LINKAGE_MERGE)) {
134 panic("Weak symbols only supported in combination with IR_LINKAGE_MERGE on this architecture");
136 be_emit_cstring(".weak ");
137 be_emit_ident(get_entity_ld_ident(entity));
139 be_emit_write_line();
143 void be_gas_emit_function_prolog(ir_entity *entity, unsigned po2alignment)
145 const char *name = get_entity_ld_name(entity);
147 be_gas_emit_switch_section(GAS_SECTION_TEXT);
149 /* write the begin line (makes the life easier for scripts parsing the
151 be_emit_write_line();
152 be_emit_cstring("# -- Begin ");
153 be_emit_string(name);
155 be_emit_write_line();
157 if (po2alignment > 0) {
158 const char *fill_byte = "";
159 unsigned maximum_skip = (1 << po2alignment) - 1;
160 /* gcc fills space between function with 0x90... */
161 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
164 be_emit_cstring("\t.p2align ");
165 be_emit_irprintf("%u,%s,%u\n", po2alignment, fill_byte, maximum_skip);
166 be_emit_write_line();
168 emit_entity_visibility(entity);
170 switch (be_gas_object_file_format) {
171 case OBJECT_FILE_FORMAT_ELF:
172 be_emit_cstring("\t.type\t");
173 be_emit_string(name);
174 be_emit_cstring(", ");
175 be_emit_char(be_gas_elf_type_char);
176 be_emit_cstring("function\n");
177 be_emit_write_line();
179 case OBJECT_FILE_FORMAT_COFF:
180 be_emit_cstring("\t.def\t");
181 be_emit_string(name);
182 be_emit_cstring(";");
183 if (get_entity_visibility(entity) == ir_visibility_local) {
184 be_emit_cstring("\t.scl\t3;");
186 be_emit_cstring("\t.scl\t2;");
188 be_emit_cstring("\t.type\t32;\t.endef\n");
189 be_emit_write_line();
191 case OBJECT_FILE_FORMAT_MACH_O:
194 be_emit_string(name);
195 be_emit_cstring(":\n");
196 be_emit_write_line();
199 void be_gas_emit_function_epilog(ir_entity *entity)
201 const char *name = get_entity_ld_name(entity);
203 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF) {
204 be_emit_cstring("\t.size\t");
205 be_emit_string(name);
206 be_emit_cstring(", .-");
207 be_emit_string(name);
209 be_emit_write_line();
212 be_emit_cstring("# -- End ");
213 be_emit_string(name);
215 be_emit_write_line();
219 * An environment containing all needed dumper data.
220 * Currently we create the file completely in memory first, then
221 * write it to the disk. This is an artifact from the old C-generating backend
222 * and even there NOT needed. So we might change it in the future.
224 typedef struct _be_gas_decl_env {
225 be_gas_section_t section;
226 const be_main_env_t *main_env;
229 /************************************************************************/
234 * @param tv the tarval
235 * @param bytes the width of the tarvals value in bytes
237 static void dump_arith_tarval(tarval *tv, int bytes)
241 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
245 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
249 be_emit_irprintf("0x%02x%02x%02x%02x",
250 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
254 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
255 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
256 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
260 /* Beware: Mixed endian output! One little endian number emitted as
261 * three longs. Each long initializer is written in big endian. */
263 "\t.long\t0x%02x%02x%02x%02x\n"
264 "\t.long\t0x%02x%02x%02x%02x\n"
265 "\t.long\t0x%02x%02x%02x%02x",
266 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
267 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
268 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
269 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
270 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
271 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8)
276 /* Beware: Mixed endian output! One little endian number emitted as
277 * three longs. Each long initializer is written in big endian. */
279 "\t.long\t0x%02x%02x%02x%02x\n"
280 "\t.long\t0x%02x%02x%02x%02x\n"
281 "\t.long\t0x%02x%02x%02x%02x\n"
282 "\t.long\t0x%02x%02x%02x%02x",
283 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
284 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
285 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
286 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
287 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
288 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
289 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 14),
290 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12)
295 panic("Can't dump a tarval with %d bytes", bytes);
299 * Return the label prefix for labeled blocks.
301 const char *be_gas_block_label_prefix(void)
307 * Return the label prefix for labeled instructions.
309 const char *be_gas_insn_label_prefix(void)
314 void be_gas_emit_entity(ir_entity *entity)
316 if (entity->type == firm_code_type) {
317 ir_label_t label = get_entity_label(entity);
318 be_emit_string(be_gas_block_label_prefix());
319 be_emit_irprintf("%lu", label);
321 be_emit_ident(get_entity_ld_ident(entity));
326 * Return the tarval of an atomic initializer.
328 * @param init a node representing the initializer (on the const code irg)
332 static tarval *get_atomic_init_tv(ir_node *init)
335 ir_mode *mode = get_irn_mode(init);
337 switch (get_irn_opcode(init)) {
340 init = get_Cast_op(init);
344 init = get_Conv_op(init);
348 return get_Const_tarval(init);
351 switch (get_SymConst_kind(init)) {
352 case symconst_type_size:
353 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
355 case symconst_type_align:
356 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
358 case symconst_ofs_ent:
359 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
361 case symconst_enum_const:
362 return get_enumeration_value(get_SymConst_enum(init));
375 * Dump an atomic value.
377 * @param env the gas output environment
378 * @param init a node representing the atomic value (on the const code irg)
380 static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
382 ir_mode *mode = get_irn_mode(init);
383 int bytes = get_mode_size_bytes(mode);
387 init = skip_Id(init);
389 switch (get_irn_opcode(init)) {
391 do_dump_atomic_init(env, get_Cast_op(init));
395 do_dump_atomic_init(env, get_Conv_op(init));
399 tv = get_Const_tarval(init);
401 /* it's a arithmetic value */
402 dump_arith_tarval(tv, bytes);
406 switch (get_SymConst_kind(init)) {
407 case symconst_addr_name:
408 be_emit_ident(get_SymConst_name(init));
411 case symconst_addr_ent:
412 ent = get_SymConst_entity(init);
413 be_gas_emit_entity(ent);
416 case symconst_ofs_ent:
417 ent = get_SymConst_entity(init);
418 be_emit_irprintf("%d", get_entity_offset(ent));
421 case symconst_type_size:
422 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
425 case symconst_type_align:
426 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
429 case symconst_enum_const:
430 tv = get_enumeration_value(get_SymConst_enum(init));
431 dump_arith_tarval(tv, bytes);
435 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
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_Add_left(init));
444 be_emit_cstring(" + ");
445 do_dump_atomic_init(env, get_Add_right(init));
449 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
450 panic("Constant must be int or pointer for '-' to work");
452 do_dump_atomic_init(env, get_Sub_left(init));
453 be_emit_cstring(" - ");
454 do_dump_atomic_init(env, get_Sub_right(init));
458 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
459 panic("Constant must be int or pointer for '*' to work");
461 do_dump_atomic_init(env, get_Mul_left(init));
462 be_emit_cstring(" * ");
463 do_dump_atomic_init(env, get_Mul_right(init));
467 be_emit_cstring("0");
471 panic("dump_atomic_init(): unsupported IR-node %+F", init);
476 * Dumps the type for given size (.byte, .long, ...)
478 * @param size the size in bytes
480 static void dump_size_type(size_t size) {
483 be_emit_cstring("\t.byte\t");
487 be_emit_cstring("\t.short\t");
491 be_emit_cstring("\t.long\t");
495 be_emit_cstring("\t.quad\t");
500 case 16: /* Note: .octa does not work on mac */
501 /* handled in arith */
505 panic("Try to dump a type with %u bytes", (unsigned)size);
510 * Emit an atomic value.
512 * @param env the gas output environment
513 * @param init a node representing the atomic value (on the const code irg)
515 static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
517 ir_mode *mode = get_irn_mode(init);
518 int bytes = get_mode_size_bytes(mode);
520 dump_size_type(bytes);
521 do_dump_atomic_init(env, init);
523 be_emit_write_line();
526 /************************************************************************/
527 /* Routines to dump global variables */
528 /************************************************************************/
530 static int initializer_is_string_const(const ir_initializer_t *initializer)
533 int found_printable = 0;
535 if (initializer->kind != IR_INITIALIZER_COMPOUND)
538 len = initializer->compound.n_initializers;
541 for (i = 0; i < len; ++i) {
545 ir_initializer_t *sub_initializer
546 = initializer->compound.initializers[i];
548 if (sub_initializer->kind != IR_INITIALIZER_TARVAL)
551 tv = sub_initializer->tarval.value;
552 mode = get_tarval_mode(tv);
554 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
557 c = get_tarval_long(tv);
558 if (isgraph(c) || isspace(c))
563 if (i == len - 1 && c != '\0')
567 return found_printable;
571 * Determine if an entity is a string constant
572 * @param ent The entity
573 * @return 1 if it is a string constant, 0 otherwise
575 static int ent_is_string_const(const ir_entity *ent)
577 ir_type *type, *element_type;
581 type = get_entity_type(ent);
583 /* if it's an array */
584 if (!is_Array_type(type))
587 element_type = get_array_element_type(type);
589 /* and the array's element type is primitive */
590 if (!is_Primitive_type(element_type))
593 /* and the mode of the element type is an int of
594 * the same size as the byte mode */
595 mode = get_type_mode(element_type);
596 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
599 if (ent->initializer != NULL) {
600 return initializer_is_string_const(ent->initializer);
601 } else if (entity_has_compound_ent_values(ent)) {
602 int found_printable = 0;
603 /* if it contains only printable chars and a 0 at the end */
604 n = get_compound_ent_n_values(ent);
605 for (i = 0; i < n; ++i) {
606 ir_node *irn = get_compound_ent_value(ent, i);
610 c = (int) get_tarval_long(get_Const_tarval(irn));
612 if (isgraph(c) || isspace(c))
617 if (i == n - 1 && c != '\0')
620 return found_printable;
627 * Dump a string constant.
628 * No checks are made!!
630 * @param ent The entity to dump.
632 static void dump_string_cst(const ir_entity *ent)
640 len = get_compound_ent_n_values(ent);
642 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
643 be_emit_cstring("\t.ascii \"");
645 be_emit_cstring("\t.string \"");
649 for (i = 0; i < output_len; ++i) {
653 irn = get_compound_ent_value(ent, i);
654 c = (int) get_tarval_long(get_Const_tarval(irn));
657 case '"' : be_emit_cstring("\\\""); break;
658 case '\n': be_emit_cstring("\\n"); break;
659 case '\r': be_emit_cstring("\\r"); break;
660 case '\t': be_emit_cstring("\\t"); break;
661 case '\\': be_emit_cstring("\\\\"); break;
666 be_emit_irprintf("\\%o", c);
670 be_emit_cstring("\"\n");
671 be_emit_write_line();
673 type = get_entity_type(ent);
674 type_size = get_type_size_bytes(type);
675 remaining_space = type_size - len;
676 assert(remaining_space >= 0);
677 if (remaining_space > 0) {
678 be_emit_irprintf("\t.space\t%d\n", remaining_space);
682 static void dump_string_initializer(const ir_initializer_t *initializer)
686 len = initializer->compound.n_initializers;
687 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
688 be_emit_cstring("\t.ascii \"");
690 be_emit_cstring("\t.string \"");
694 for (i = 0; i < len; ++i) {
695 const ir_initializer_t *sub_initializer
696 = get_initializer_compound_value(initializer, i);
698 tarval *tv = get_initializer_tarval_value(sub_initializer);
699 int c = get_tarval_long(tv);
702 case '"' : be_emit_cstring("\\\""); break;
703 case '\n': be_emit_cstring("\\n"); break;
704 case '\r': be_emit_cstring("\\r"); break;
705 case '\t': be_emit_cstring("\\t"); break;
706 case '\\': be_emit_cstring("\\\\"); break;
711 be_emit_irprintf("\\%o", c);
715 be_emit_cstring("\"\n");
716 be_emit_write_line();
719 enum normal_or_bitfield_kind {
726 enum normal_or_bitfield_kind kind;
730 unsigned char bf_val;
732 } normal_or_bitfield;
734 static int is_type_variable_size(ir_type *type)
741 static size_t get_initializer_size(const ir_initializer_t *initializer,
744 switch (get_initializer_kind(initializer)) {
745 case IR_INITIALIZER_TARVAL:
746 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
747 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;
777 panic("found invalid initializer");
781 static normal_or_bitfield *glob_vals;
782 static size_t max_vals;
785 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
786 const ir_initializer_t *initializer, ir_type *type)
788 unsigned char last_bits = 0;
789 ir_mode *mode = get_type_mode(type);
791 unsigned char curr_bits;
795 switch (get_initializer_kind(initializer)) {
796 case IR_INITIALIZER_NULL:
798 case IR_INITIALIZER_TARVAL:
799 tv = get_initializer_tarval_value(initializer);
801 case IR_INITIALIZER_CONST: {
802 ir_node *node = get_initializer_const_value(initializer);
803 if (!is_Const(node)) {
804 panic("bitfield initializer not a Const node");
806 tv = get_Const_tarval(node);
809 case IR_INITIALIZER_COMPOUND:
810 panic("bitfield initializer is compound");
813 panic("Couldn't get numeric value for bitfield initializer");
815 tv = tarval_convert_to(tv, get_type_mode(type));
817 /* normalize offset */
818 vals += offset_bits >> 3;
820 value_len = get_mode_size_bits(mode);
822 /* combine bits with existing bits */
823 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
824 assert((size_t) (vals - glob_vals) + j < max_vals);
825 assert(vals[j].kind == BITFIELD ||
826 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
827 vals[j].kind = BITFIELD;
828 curr_bits = get_tarval_sub_bits(tv, j);
830 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
832 last_bits = curr_bits;
836 static void dump_ir_initializer(normal_or_bitfield *vals,
837 const ir_initializer_t *initializer,
840 assert((size_t) (vals - glob_vals) < max_vals);
842 switch (get_initializer_kind(initializer)) {
843 case IR_INITIALIZER_NULL:
845 case IR_INITIALIZER_TARVAL: {
848 assert(vals->kind != BITFIELD);
850 vals->v.tarval = get_initializer_tarval_value(initializer);
851 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
852 for (i = 1; i < get_type_size_bytes(type); ++i) {
853 vals[i].kind = NORMAL;
854 vals[i].v.value = NULL;
858 case IR_INITIALIZER_CONST: {
861 assert(vals->kind != BITFIELD);
863 vals->v.value = get_initializer_const_value(initializer);
864 for (i = 1; i < get_type_size_bytes(type); ++i) {
865 vals[i].kind = NORMAL;
866 vals[i].v.value = NULL;
870 case IR_INITIALIZER_COMPOUND: {
872 size_t n = get_initializer_compound_n_entries(initializer);
874 if (is_Array_type(type)) {
875 ir_type *element_type = get_array_element_type(type);
876 size_t skip = get_type_size_bytes(element_type);
877 size_t alignment = get_type_alignment_bytes(element_type);
878 size_t misalign = skip % alignment;
880 skip += alignment - misalign;
883 for (i = 0; i < n; ++i) {
884 ir_initializer_t *sub_initializer
885 = get_initializer_compound_value(initializer, i);
887 dump_ir_initializer(vals, sub_initializer, element_type);
893 assert(is_compound_type(type));
894 n_members = get_compound_n_members(type);
895 for (i = 0; i < n_members; ++i) {
896 ir_entity *member = get_compound_member(type, i);
897 size_t offset = get_entity_offset(member);
898 ir_type *subtype = get_entity_type(member);
899 ir_mode *mode = get_type_mode(subtype);
900 ir_initializer_t *sub_initializer;
902 assert(i < get_initializer_compound_n_entries(initializer));
904 = get_initializer_compound_value(initializer, i);
908 = get_entity_offset_bits_remainder(member);
909 size_t value_len = get_mode_size_bits(mode);
911 if (offset_bits != 0 ||
912 (value_len != 8 && value_len != 16 && value_len != 32
913 && value_len != 64)) {
914 dump_bitfield(&vals[offset], offset_bits,
915 sub_initializer, subtype);
920 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
927 panic("invalid ir_initializer kind found");
930 static void dump_initializer(be_gas_decl_env_t *env, const ir_entity *entity)
932 const ir_initializer_t *initializer = entity->initializer;
934 normal_or_bitfield *vals;
938 if (initializer_is_string_const(initializer)) {
939 dump_string_initializer(initializer);
943 type = get_entity_type(entity);
944 size = get_initializer_size(initializer, type);
950 * In the worst case, every initializer allocates one byte.
951 * Moreover, initializer might be big, do not allocate on stack.
953 vals = XMALLOCNZ(normal_or_bitfield, size);
960 dump_ir_initializer(vals, initializer, type);
962 /* now write values sorted */
963 for (k = 0; k < size; ) {
966 if (vals[k].kind == NORMAL) {
967 if (vals[k].v.value != NULL) {
968 dump_atomic_init(env, vals[k].v.value);
969 elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
973 } else if (vals[k].kind == TARVAL) {
974 tarval *tv = vals[k].v.tarval;
975 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
980 dump_size_type(size);
981 dump_arith_tarval(tv, size);
983 be_emit_write_line();
985 assert(vals[k].kind == BITFIELD);
986 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
987 be_emit_write_line();
991 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
998 be_emit_irprintf("\t.space\t%d\n", space);
999 be_emit_write_line();
1005 static void dump_compound_graph_init(be_gas_decl_env_t *env,
1006 const ir_entity *ent)
1008 normal_or_bitfield *vals;
1010 unsigned k, last_ofs;
1012 if (ent_is_string_const(ent)) {
1013 dump_string_cst(ent);
1017 n = get_compound_ent_n_values(ent);
1019 /* Find the initializer size. Sorrily gcc support a nasty feature:
1020 The last field of a compound may be a flexible array. This allows
1021 initializers bigger than the type size. */
1022 last_ofs = get_type_size_bytes(get_entity_type(ent));
1023 for (i = 0; i < n; ++i) {
1024 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1025 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1026 ir_node *value = get_compound_ent_value(ent, i);
1027 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
1029 offset += (value_len + bits_remainder + 7) >> 3;
1031 if (offset > last_ofs) {
1037 * In the worst case, every initializer allocates one byte.
1038 * Moreover, initializer might be big, do not allocate on stack.
1040 vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1042 /* collect the values and store them at the offsets */
1043 for (i = 0; i < n; ++i) {
1044 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1045 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1046 ir_node *value = get_compound_ent_value(ent, i);
1047 int value_len = get_mode_size_bits(get_irn_mode(value));
1049 assert(offset_bits >= 0);
1051 if (offset_bits != 0 ||
1052 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1053 tarval *tv = get_atomic_init_tv(value);
1054 unsigned char curr_bits, last_bits = 0;
1056 panic("Couldn't get numeric value for bitfield initializer '%s'",
1057 get_entity_ld_name(ent));
1059 /* normalize offset */
1060 offset += offset_bits >> 3;
1063 for (j = 0; value_len + offset_bits > 0; ++j) {
1064 assert(offset + j < last_ofs);
1065 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1066 vals[offset + j].kind = BITFIELD;
1067 curr_bits = get_tarval_sub_bits(tv, j);
1068 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1070 last_bits = curr_bits;
1075 assert(offset < last_ofs);
1076 assert(vals[offset].kind == NORMAL);
1077 for (i = 1; i < value_len / 8; ++i) {
1078 assert(vals[offset + i].v.value == NULL);
1080 vals[offset].v.value = value;
1084 /* now write them sorted */
1085 for (k = 0; k < last_ofs; ) {
1086 int space = 0, skip = 0;
1087 if (vals[k].kind == NORMAL) {
1088 if (vals[k].v.value != NULL) {
1089 dump_atomic_init(env, vals[k].v.value);
1090 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1095 assert(vals[k].kind == BITFIELD);
1096 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1100 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1109 be_emit_irprintf("\t.space\t%d\n", space);
1110 be_emit_write_line();
1116 static void emit_align(unsigned p2alignment)
1118 be_emit_irprintf("\t.p2align\t%u\n", log2_floor(p2alignment));
1119 be_emit_write_line();
1122 static unsigned get_effective_entity_alignment(const ir_entity *entity)
1124 unsigned alignment = get_entity_alignment(entity);
1125 if (alignment == 0) {
1126 ir_type *type = get_entity_type(entity);
1127 alignment = get_type_alignment_bytes(type);
1132 static be_gas_section_t determine_section(be_gas_decl_env_t *env,
1133 const ir_entity *entity)
1135 ir_type *owner = get_entity_owner(entity);
1137 if (owner == get_segment_type(IR_SEGMENT_GLOBAL)) {
1138 ir_linkage linkage = get_entity_linkage(entity);
1139 if (linkage & IR_LINKAGE_CONSTANT) {
1140 /* mach-o is the only one with a cstring section */
1141 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
1142 && ent_is_string_const(entity))
1143 return GAS_SECTION_CSTRING;
1145 return GAS_SECTION_RODATA;
1147 if (!entity_has_definition(entity))
1148 return GAS_SECTION_BSS;
1150 return GAS_SECTION_DATA;
1152 } else if (owner == env->main_env->pic_symbols_type) {
1153 return GAS_SECTION_PIC_SYMBOLS;
1154 } else if (owner == env->main_env->pic_trampolines_type) {
1155 return GAS_SECTION_PIC_TRAMPOLINES;
1156 } else if (owner == get_segment_type(IR_SEGMENT_CONSTRUCTORS)) {
1157 return GAS_SECTION_CONSTRUCTORS;
1158 } else if (owner == get_segment_type(IR_SEGMENT_DESTRUCTORS)) {
1159 return GAS_SECTION_DESTRUCTORS;
1160 } else if (owner == get_segment_type(IR_SEGMENT_THREAD_LOCAL)) {
1161 return GAS_SECTION_TLS;
1164 panic("Couldn't determine section for %+F?!?", entity);
1167 static void emit_common(const ir_entity *ent)
1169 const char *name = get_entity_ld_name(ent);
1170 unsigned size = get_type_size_bytes(get_entity_type(ent));
1171 unsigned alignment = get_effective_entity_alignment(ent);
1173 switch (be_gas_object_file_format) {
1174 case OBJECT_FILE_FORMAT_MACH_O:
1175 be_emit_irprintf("\t.comm %s,%u,%u\n", name, size,
1176 log2_floor(alignment));
1177 be_emit_write_line();
1179 case OBJECT_FILE_FORMAT_ELF:
1180 be_emit_irprintf("\t.comm %s,%u,%u\n", name, size, alignment);
1181 be_emit_write_line();
1183 case OBJECT_FILE_FORMAT_COFF:
1184 be_emit_irprintf("\t.comm %s,%u # %u\n", name, size, alignment);
1185 be_emit_write_line();
1188 panic("invalid object file format");
1191 static void dump_indirect_symbol(const ir_entity *entity, be_gas_section_t section)
1193 /* we can only do PIC code on macho so far */
1194 assert(be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O);
1196 be_emit_ident(get_entity_ld_ident(entity));
1197 be_emit_cstring(":\n");
1198 be_emit_write_line();
1199 be_emit_cstring("\t.indirect_symbol ");
1200 be_emit_ident(get_entity_ident(entity));
1202 be_emit_write_line();
1203 if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1204 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1205 be_emit_write_line();
1207 assert(section == GAS_SECTION_PIC_SYMBOLS);
1208 be_emit_cstring("\t.long 0\n");
1209 be_emit_write_line();
1214 * Dump a global entity.
1216 * @param env the gas output environment
1217 * @param ent the entity to be dumped
1219 static void dump_global(be_gas_decl_env_t *env, const ir_entity *ent)
1221 ir_type *type = get_entity_type(ent);
1222 ident *ld_ident = get_entity_ld_ident(ent);
1223 unsigned alignment = get_effective_entity_alignment(ent);
1224 be_gas_section_t section = determine_section(env, ent);
1226 /* we already emitted all methods. Except for the trampolines which
1227 * the assembler/linker generates */
1228 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1231 /* block labels are already emittet in the code */
1232 if (type == firm_code_type)
1235 be_dbg_variable(ent);
1237 /* nothing to do for externally defined values */
1238 if (get_entity_visibility(ent) == ir_visibility_external)
1241 if (!is_po2(alignment))
1242 panic("alignment not a power of 2");
1244 if (section == GAS_SECTION_BSS &&
1245 (get_entity_linkage(ent) & IR_LINKAGE_MERGE)) {
1246 if (get_entity_visibility(ent) != ir_visibility_default) {
1247 panic("merge link semantic not supported for local/extern entities");
1253 be_gas_emit_switch_section(section);
1255 if (section == GAS_SECTION_PIC_TRAMPOLINES
1256 || section == GAS_SECTION_PIC_SYMBOLS) {
1257 dump_indirect_symbol(ent, section);
1262 if (alignment > 1) {
1263 emit_align(alignment);
1265 emit_entity_visibility(ent);
1266 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF
1267 && be_gas_emit_types) {
1268 be_emit_cstring("\t.type\t");
1269 be_emit_ident(ld_ident);
1270 be_emit_cstring(", ");
1271 be_emit_char(be_gas_elf_type_char);
1272 be_emit_cstring("object\n\t.size\t");
1273 be_emit_ident(ld_ident);
1274 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1277 if (get_id_str(ld_ident)[0] != '\0') {
1278 be_emit_ident(ld_ident);
1279 be_emit_cstring(":\n");
1280 be_emit_write_line();
1283 if (ent->initializer != NULL) {
1284 dump_initializer(env, ent);
1285 } else if(entity_has_compound_ent_values(ent)) {
1286 dump_compound_graph_init(env, ent);
1289 be_emit_irprintf("\t.space %u\n", get_type_size_bytes(type));
1290 be_emit_write_line();
1295 * Dumps declarations of global variables and the initialization code.
1297 * @param gt a global like type, either the global or the TLS one
1298 * @param env an environment
1300 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env)
1302 int i, n = get_compound_n_members(gt);
1304 for (i = 0; i < n; i++) {
1305 ir_entity *ent = get_compound_member(gt, i);
1306 dump_global(env, ent);
1310 /************************************************************************/
1312 /* Generate all entities. */
1313 void be_gas_emit_decls(const be_main_env_t *main_env)
1315 be_gas_decl_env_t env;
1316 memset(&env, 0, sizeof(env));
1318 /* dump global type */
1319 env.main_env = main_env;
1320 env.section = (be_gas_section_t) -1;
1322 be_gas_dump_globals(get_glob_type(), &env);
1323 be_gas_dump_globals(get_tls_type(), &env);
1324 be_gas_dump_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env);
1325 be_gas_dump_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env);
1326 be_gas_dump_globals(main_env->pic_symbols_type, &env);
1327 be_gas_dump_globals(main_env->pic_trampolines_type, &env);
1330 * ".subsections_via_symbols marks object files which are OK to divide
1331 * their section contents into individual blocks".
1332 * From my understanding this means no label points in the middle of an
1333 * object which we want to address as a whole. Firm code should be fine
1336 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
1337 be_emit_cstring("\t.subsections_via_symbols\n");
1338 be_emit_write_line();