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 .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_entity_visibility(const ir_entity *entity)
126 ir_visibility visibility = get_entity_visibility(entity);
127 ir_linkage linkage = get_entity_linkage(entity);
129 if (visibility != ir_visibility_local) {
130 be_emit_cstring(".globl ");
131 be_emit_ident(get_entity_ld_ident(entity));
133 be_emit_write_line();
135 if (linkage & IR_LINKAGE_WEAK) {
136 if (! (linkage & IR_LINKAGE_MERGE)) {
137 panic("Weak symbols only supported in combination with IR_LINKAGE_MERGE on this architecture");
139 be_emit_cstring(".weak ");
140 be_emit_ident(get_entity_ld_ident(entity));
142 be_emit_write_line();
146 void be_gas_emit_function_prolog(ir_entity *entity, unsigned po2alignment)
148 const char *name = get_entity_ld_name(entity);
150 be_gas_emit_switch_section(GAS_SECTION_TEXT);
152 /* write the begin line (makes the life easier for scripts parsing the
154 be_emit_write_line();
155 be_emit_cstring("# -- Begin ");
156 be_emit_string(name);
158 be_emit_write_line();
160 if (po2alignment > 0) {
161 const char *fill_byte = "";
162 unsigned maximum_skip = (1 << po2alignment) - 1;
163 /* gcc fills space between function with 0x90... */
164 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
167 be_emit_cstring("\t.p2align ");
168 be_emit_irprintf("%u,%s,%u\n", po2alignment, fill_byte, maximum_skip);
169 be_emit_write_line();
171 emit_entity_visibility(entity);
173 switch (be_gas_object_file_format) {
174 case OBJECT_FILE_FORMAT_ELF:
175 be_emit_cstring("\t.type\t");
176 be_emit_string(name);
177 be_emit_cstring(", ");
178 be_emit_char(be_gas_elf_type_char);
179 be_emit_cstring("function\n");
180 be_emit_write_line();
182 case OBJECT_FILE_FORMAT_COFF:
183 be_emit_cstring("\t.def\t");
184 be_emit_string(name);
185 be_emit_cstring(";");
186 if (get_entity_visibility(entity) == ir_visibility_local) {
187 be_emit_cstring("\t.scl\t3;");
189 be_emit_cstring("\t.scl\t2;");
191 be_emit_cstring("\t.type\t32;\t.endef\n");
192 be_emit_write_line();
194 case OBJECT_FILE_FORMAT_MACH_O:
197 be_emit_string(name);
198 be_emit_cstring(":\n");
199 be_emit_write_line();
202 void be_gas_emit_function_epilog(ir_entity *entity)
204 const char *name = get_entity_ld_name(entity);
206 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF) {
207 be_emit_cstring("\t.size\t");
208 be_emit_string(name);
209 be_emit_cstring(", .-");
210 be_emit_string(name);
212 be_emit_write_line();
215 be_emit_cstring("# -- End ");
216 be_emit_string(name);
218 be_emit_write_line();
222 * An environment containing all needed dumper data.
223 * Currently we create the file completely in memory first, then
224 * write it to the disk. This is an artifact from the old C-generating backend
225 * and even there NOT needed. So we might change it in the future.
227 typedef struct _be_gas_decl_env {
228 be_gas_section_t section;
229 const be_main_env_t *main_env;
232 /************************************************************************/
237 * @param tv the tarval
238 * @param bytes the width of the tarvals value in bytes
240 static void dump_arith_tarval(tarval *tv, int bytes)
244 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
248 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
252 be_emit_irprintf("0x%02x%02x%02x%02x",
253 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
257 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
258 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
259 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
263 /* Beware: Mixed endian output! One little endian number emitted as
264 * three longs. Each long initializer is written in big endian. */
266 "\t.long\t0x%02x%02x%02x%02x\n"
267 "\t.long\t0x%02x%02x%02x%02x\n"
268 "\t.long\t0x%02x%02x%02x%02x",
269 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
270 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
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, 11), get_tarval_sub_bits(tv, 10),
274 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8)
279 /* Beware: Mixed endian output! One little endian number emitted as
280 * three longs. Each long initializer is written in big endian. */
282 "\t.long\t0x%02x%02x%02x%02x\n"
283 "\t.long\t0x%02x%02x%02x%02x\n"
284 "\t.long\t0x%02x%02x%02x%02x\n"
285 "\t.long\t0x%02x%02x%02x%02x",
286 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
287 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
288 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
289 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
290 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
291 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
292 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 14),
293 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12)
298 panic("Can't dump a tarval with %d bytes", bytes);
302 * Return the label prefix for labeled blocks.
304 const char *be_gas_block_label_prefix(void)
310 * Return the label prefix for labeled instructions.
312 const char *be_gas_insn_label_prefix(void)
317 void be_gas_emit_entity(ir_entity *entity)
319 if (entity->type == firm_code_type) {
320 ir_label_t label = get_entity_label(entity);
321 be_emit_string(be_gas_block_label_prefix());
322 be_emit_irprintf("%lu", label);
324 be_emit_ident(get_entity_ld_ident(entity));
329 * Return the tarval of an atomic initializer.
331 * @param init a node representing the initializer (on the const code irg)
335 static tarval *get_atomic_init_tv(ir_node *init)
338 ir_mode *mode = get_irn_mode(init);
340 switch (get_irn_opcode(init)) {
343 init = get_Cast_op(init);
347 init = get_Conv_op(init);
351 return get_Const_tarval(init);
354 switch (get_SymConst_kind(init)) {
355 case symconst_type_size:
356 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
358 case symconst_type_align:
359 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
361 case symconst_ofs_ent:
362 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
364 case symconst_enum_const:
365 return get_enumeration_value(get_SymConst_enum(init));
378 * Dump an atomic value.
380 * @param env the gas output environment
381 * @param init a node representing the atomic value (on the const code irg)
383 static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
385 ir_mode *mode = get_irn_mode(init);
386 int bytes = get_mode_size_bytes(mode);
390 init = skip_Id(init);
392 switch (get_irn_opcode(init)) {
394 do_dump_atomic_init(env, get_Cast_op(init));
398 do_dump_atomic_init(env, get_Conv_op(init));
402 tv = get_Const_tarval(init);
404 /* it's a arithmetic value */
405 dump_arith_tarval(tv, bytes);
409 switch (get_SymConst_kind(init)) {
410 case symconst_addr_name:
411 be_emit_ident(get_SymConst_name(init));
414 case symconst_addr_ent:
415 ent = get_SymConst_entity(init);
416 be_gas_emit_entity(ent);
419 case symconst_ofs_ent:
420 ent = get_SymConst_entity(init);
421 be_emit_irprintf("%d", get_entity_offset(ent));
424 case symconst_type_size:
425 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
428 case symconst_type_align:
429 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
432 case symconst_enum_const:
433 tv = get_enumeration_value(get_SymConst_enum(init));
434 dump_arith_tarval(tv, bytes);
438 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
443 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
444 panic("Constant must be int or pointer for '+' to work");
446 do_dump_atomic_init(env, get_Add_left(init));
447 be_emit_cstring(" + ");
448 do_dump_atomic_init(env, get_Add_right(init));
452 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
453 panic("Constant must be int or pointer for '-' to work");
455 do_dump_atomic_init(env, get_Sub_left(init));
456 be_emit_cstring(" - ");
457 do_dump_atomic_init(env, get_Sub_right(init));
461 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
462 panic("Constant must be int or pointer for '*' to work");
464 do_dump_atomic_init(env, get_Mul_left(init));
465 be_emit_cstring(" * ");
466 do_dump_atomic_init(env, get_Mul_right(init));
470 be_emit_cstring("0");
474 panic("dump_atomic_init(): unsupported IR-node %+F", init);
479 * Dumps the type for given size (.byte, .long, ...)
481 * @param size the size in bytes
483 static void dump_size_type(size_t size) {
486 be_emit_cstring("\t.byte\t");
490 be_emit_cstring("\t.short\t");
494 be_emit_cstring("\t.long\t");
498 be_emit_cstring("\t.quad\t");
503 case 16: /* Note: .octa does not work on mac */
504 /* handled in arith */
508 panic("Try to dump a type with %u bytes", (unsigned)size);
513 * Emit an atomic value.
515 * @param env the gas output environment
516 * @param init a node representing the atomic value (on the const code irg)
518 static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
520 ir_mode *mode = get_irn_mode(init);
521 int bytes = get_mode_size_bytes(mode);
523 dump_size_type(bytes);
524 do_dump_atomic_init(env, init);
526 be_emit_write_line();
529 /************************************************************************/
530 /* Routines to dump global variables */
531 /************************************************************************/
533 static int initializer_is_string_const(const ir_initializer_t *initializer)
536 int found_printable = 0;
538 if (initializer->kind != IR_INITIALIZER_COMPOUND)
541 len = initializer->compound.n_initializers;
544 for (i = 0; i < len; ++i) {
548 ir_initializer_t *sub_initializer
549 = initializer->compound.initializers[i];
551 if (sub_initializer->kind != IR_INITIALIZER_TARVAL)
554 tv = sub_initializer->tarval.value;
555 mode = get_tarval_mode(tv);
557 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
560 c = get_tarval_long(tv);
561 if (isgraph(c) || isspace(c))
566 if (i == len - 1 && c != '\0')
570 return found_printable;
574 * Determine if an entity is a string constant
575 * @param ent The entity
576 * @return 1 if it is a string constant, 0 otherwise
578 static int ent_is_string_const(const ir_entity *ent)
580 ir_type *type, *element_type;
584 type = get_entity_type(ent);
586 /* if it's an array */
587 if (!is_Array_type(type))
590 element_type = get_array_element_type(type);
592 /* and the array's element type is primitive */
593 if (!is_Primitive_type(element_type))
596 /* and the mode of the element type is an int of
597 * the same size as the byte mode */
598 mode = get_type_mode(element_type);
599 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
602 if (ent->initializer != NULL) {
603 return initializer_is_string_const(ent->initializer);
604 } else if (entity_has_compound_ent_values(ent)) {
605 int found_printable = 0;
606 /* if it contains only printable chars and a 0 at the end */
607 n = get_compound_ent_n_values(ent);
608 for (i = 0; i < n; ++i) {
609 ir_node *irn = get_compound_ent_value(ent, i);
613 c = (int) get_tarval_long(get_Const_tarval(irn));
615 if (isgraph(c) || isspace(c))
620 if (i == n - 1 && c != '\0')
623 return found_printable;
629 static bool initializer_is_null(const ir_initializer_t *initializer)
631 switch (initializer->kind) {
632 case IR_INITIALIZER_NULL:
634 case IR_INITIALIZER_TARVAL: {
635 tarval *tv = initializer->tarval.value;
636 return tarval_is_null(tv);
638 case IR_INITIALIZER_CONST: {
639 ir_node *value = initializer->consti.value;
640 if (!is_Const(value))
642 return is_Const_null(value);
644 case IR_INITIALIZER_COMPOUND: {
646 for (i = 0; i < initializer->compound.n_initializers; ++i) {
647 ir_initializer_t *subinitializer
648 = initializer->compound.initializers[i];
649 if (!initializer_is_null(subinitializer))
655 panic("invalid initializer in initializer_is_null");
658 static bool entity_is_null(const ir_entity *entity)
660 if (entity->initializer != NULL) {
661 return initializer_is_null(entity->initializer);
662 } else if (entity_has_compound_ent_values(entity)) {
663 /* I'm too lazy to implement this case as compound graph paths will be
664 * remove anyway in the future */
666 /* uninitialized, NULL is fine */
671 * Dump a string constant.
672 * No checks are made!!
674 * @param ent The entity to dump.
676 static void dump_string_cst(const ir_entity *ent)
684 len = get_compound_ent_n_values(ent);
686 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
687 be_emit_cstring("\t.ascii \"");
689 be_emit_cstring("\t.string \"");
693 for (i = 0; i < output_len; ++i) {
697 irn = get_compound_ent_value(ent, i);
698 c = (int) get_tarval_long(get_Const_tarval(irn));
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();
717 type = get_entity_type(ent);
718 type_size = get_type_size_bytes(type);
719 remaining_space = type_size - len;
720 assert(remaining_space >= 0);
721 if (remaining_space > 0) {
722 be_emit_irprintf("\t.zero\t%d\n", remaining_space);
726 static void dump_string_initializer(const ir_initializer_t *initializer)
730 len = initializer->compound.n_initializers;
731 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
732 be_emit_cstring("\t.ascii \"");
734 be_emit_cstring("\t.string \"");
738 for (i = 0; i < len; ++i) {
739 const ir_initializer_t *sub_initializer
740 = get_initializer_compound_value(initializer, i);
742 tarval *tv = get_initializer_tarval_value(sub_initializer);
743 int c = get_tarval_long(tv);
746 case '"' : be_emit_cstring("\\\""); break;
747 case '\n': be_emit_cstring("\\n"); break;
748 case '\r': be_emit_cstring("\\r"); break;
749 case '\t': be_emit_cstring("\\t"); break;
750 case '\\': be_emit_cstring("\\\\"); break;
755 be_emit_irprintf("\\%o", c);
759 be_emit_cstring("\"\n");
760 be_emit_write_line();
763 enum normal_or_bitfield_kind {
770 enum normal_or_bitfield_kind kind;
774 unsigned char bf_val;
776 } normal_or_bitfield;
778 static int is_type_variable_size(ir_type *type)
785 static size_t get_initializer_size(const ir_initializer_t *initializer,
788 switch (get_initializer_kind(initializer)) {
789 case IR_INITIALIZER_TARVAL:
790 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
791 return get_type_size_bytes(type);
792 case IR_INITIALIZER_CONST:
793 case IR_INITIALIZER_NULL:
794 return get_type_size_bytes(type);
795 case IR_INITIALIZER_COMPOUND:
796 if (!is_type_variable_size(type)) {
797 return get_type_size_bytes(type);
800 = get_initializer_compound_n_entries(initializer);
802 unsigned initializer_size = get_type_size_bytes(type);
803 for (i = 0; i < n_entries; ++i) {
804 ir_entity *entity = get_compound_member(type, i);
805 ir_type *type = get_entity_type(entity);
807 const ir_initializer_t *sub_initializer
808 = get_initializer_compound_value(initializer, i);
810 unsigned offset = get_entity_offset(entity);
811 unsigned size = get_initializer_size(sub_initializer, type);
813 if (offset + size > initializer_size) {
814 initializer_size = offset + size;
817 return initializer_size;
821 panic("found invalid initializer");
825 static normal_or_bitfield *glob_vals;
826 static size_t max_vals;
829 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
830 const ir_initializer_t *initializer, ir_type *type)
832 unsigned char last_bits = 0;
833 ir_mode *mode = get_type_mode(type);
835 unsigned char curr_bits;
839 switch (get_initializer_kind(initializer)) {
840 case IR_INITIALIZER_NULL:
842 case IR_INITIALIZER_TARVAL:
843 tv = get_initializer_tarval_value(initializer);
845 case IR_INITIALIZER_CONST: {
846 ir_node *node = get_initializer_const_value(initializer);
847 if (!is_Const(node)) {
848 panic("bitfield initializer not a Const node");
850 tv = get_Const_tarval(node);
853 case IR_INITIALIZER_COMPOUND:
854 panic("bitfield initializer is compound");
857 panic("Couldn't get numeric value for bitfield initializer");
859 tv = tarval_convert_to(tv, get_type_mode(type));
861 /* normalize offset */
862 vals += offset_bits >> 3;
864 value_len = get_mode_size_bits(mode);
866 /* combine bits with existing bits */
867 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
868 assert((size_t) (vals - glob_vals) + j < max_vals);
869 assert(vals[j].kind == BITFIELD ||
870 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
871 vals[j].kind = BITFIELD;
872 curr_bits = get_tarval_sub_bits(tv, j);
874 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
876 last_bits = curr_bits;
880 static void dump_ir_initializer(normal_or_bitfield *vals,
881 const ir_initializer_t *initializer,
884 assert((size_t) (vals - glob_vals) < max_vals);
886 switch (get_initializer_kind(initializer)) {
887 case IR_INITIALIZER_NULL:
889 case IR_INITIALIZER_TARVAL: {
892 assert(vals->kind != BITFIELD);
894 vals->v.tarval = get_initializer_tarval_value(initializer);
895 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
896 for (i = 1; i < get_type_size_bytes(type); ++i) {
897 vals[i].kind = NORMAL;
898 vals[i].v.value = NULL;
902 case IR_INITIALIZER_CONST: {
905 assert(vals->kind != BITFIELD);
907 vals->v.value = get_initializer_const_value(initializer);
908 for (i = 1; i < get_type_size_bytes(type); ++i) {
909 vals[i].kind = NORMAL;
910 vals[i].v.value = NULL;
914 case IR_INITIALIZER_COMPOUND: {
916 size_t n = get_initializer_compound_n_entries(initializer);
918 if (is_Array_type(type)) {
919 ir_type *element_type = get_array_element_type(type);
920 size_t skip = get_type_size_bytes(element_type);
921 size_t alignment = get_type_alignment_bytes(element_type);
922 size_t misalign = skip % alignment;
924 skip += alignment - misalign;
927 for (i = 0; i < n; ++i) {
928 ir_initializer_t *sub_initializer
929 = get_initializer_compound_value(initializer, i);
931 dump_ir_initializer(vals, sub_initializer, element_type);
937 assert(is_compound_type(type));
938 n_members = get_compound_n_members(type);
939 for (i = 0; i < n_members; ++i) {
940 ir_entity *member = get_compound_member(type, i);
941 size_t offset = get_entity_offset(member);
942 ir_type *subtype = get_entity_type(member);
943 ir_mode *mode = get_type_mode(subtype);
944 ir_initializer_t *sub_initializer;
946 assert(i < get_initializer_compound_n_entries(initializer));
948 = get_initializer_compound_value(initializer, i);
952 = get_entity_offset_bits_remainder(member);
953 size_t value_len = get_mode_size_bits(mode);
955 if (offset_bits != 0 ||
956 (value_len != 8 && value_len != 16 && value_len != 32
957 && value_len != 64)) {
958 dump_bitfield(&vals[offset], offset_bits,
959 sub_initializer, subtype);
964 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
971 panic("invalid ir_initializer kind found");
974 static void dump_initializer(be_gas_decl_env_t *env, const ir_entity *entity)
976 const ir_initializer_t *initializer = entity->initializer;
978 normal_or_bitfield *vals;
982 if (initializer_is_string_const(initializer)) {
983 dump_string_initializer(initializer);
987 type = get_entity_type(entity);
988 size = get_initializer_size(initializer, type);
994 * In the worst case, every initializer allocates one byte.
995 * Moreover, initializer might be big, do not allocate on stack.
997 vals = XMALLOCNZ(normal_or_bitfield, size);
1004 dump_ir_initializer(vals, initializer, type);
1006 /* now write values sorted */
1007 for (k = 0; k < size; ) {
1010 if (vals[k].kind == NORMAL) {
1011 if (vals[k].v.value != NULL) {
1012 dump_atomic_init(env, vals[k].v.value);
1013 elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
1017 } else if (vals[k].kind == TARVAL) {
1018 tarval *tv = vals[k].v.tarval;
1019 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
1024 dump_size_type(size);
1025 dump_arith_tarval(tv, size);
1027 be_emit_write_line();
1029 assert(vals[k].kind == BITFIELD);
1030 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1031 be_emit_write_line();
1035 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1042 be_emit_irprintf("\t.space\t%d\n", space);
1043 be_emit_write_line();
1049 static void dump_compound_graph_init(be_gas_decl_env_t *env,
1050 const ir_entity *ent)
1052 normal_or_bitfield *vals;
1054 unsigned k, last_ofs;
1056 if (ent_is_string_const(ent)) {
1057 dump_string_cst(ent);
1061 n = get_compound_ent_n_values(ent);
1063 /* Find the initializer size. Sorrily gcc support a nasty feature:
1064 The last field of a compound may be a flexible array. This allows
1065 initializers bigger than the type size. */
1066 last_ofs = get_type_size_bytes(get_entity_type(ent));
1067 for (i = 0; i < n; ++i) {
1068 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1069 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1070 ir_node *value = get_compound_ent_value(ent, i);
1071 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
1073 offset += (value_len + bits_remainder + 7) >> 3;
1075 if (offset > last_ofs) {
1081 * In the worst case, every initializer allocates one byte.
1082 * Moreover, initializer might be big, do not allocate on stack.
1084 vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1086 /* collect the values and store them at the offsets */
1087 for (i = 0; i < n; ++i) {
1088 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1089 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1090 ir_node *value = get_compound_ent_value(ent, i);
1091 int value_len = get_mode_size_bits(get_irn_mode(value));
1093 assert(offset_bits >= 0);
1095 if (offset_bits != 0 ||
1096 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1097 tarval *tv = get_atomic_init_tv(value);
1098 unsigned char curr_bits, last_bits = 0;
1100 panic("Couldn't get numeric value for bitfield initializer '%s'",
1101 get_entity_ld_name(ent));
1103 /* normalize offset */
1104 offset += offset_bits >> 3;
1107 for (j = 0; value_len + offset_bits > 0; ++j) {
1108 assert(offset + j < last_ofs);
1109 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1110 vals[offset + j].kind = BITFIELD;
1111 curr_bits = get_tarval_sub_bits(tv, j);
1112 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1114 last_bits = curr_bits;
1119 assert(offset < last_ofs);
1120 assert(vals[offset].kind == NORMAL);
1121 for (i = 1; i < value_len / 8; ++i) {
1122 assert(vals[offset + i].v.value == NULL);
1124 vals[offset].v.value = value;
1128 /* now write them sorted */
1129 for (k = 0; k < last_ofs; ) {
1130 int space = 0, skip = 0;
1131 if (vals[k].kind == NORMAL) {
1132 if (vals[k].v.value != NULL) {
1133 dump_atomic_init(env, vals[k].v.value);
1134 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1139 assert(vals[k].kind == BITFIELD);
1140 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1144 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1153 be_emit_irprintf("\t.space\t%d\n", space);
1154 be_emit_write_line();
1160 static void emit_align(unsigned p2alignment)
1162 be_emit_irprintf("\t.p2align\t%u\n", log2_floor(p2alignment));
1163 be_emit_write_line();
1166 static unsigned get_effective_entity_alignment(const ir_entity *entity)
1168 unsigned alignment = get_entity_alignment(entity);
1169 if (alignment == 0) {
1170 ir_type *type = get_entity_type(entity);
1171 alignment = get_type_alignment_bytes(type);
1176 static be_gas_section_t determine_section(be_gas_decl_env_t *env,
1177 const ir_entity *entity)
1179 ir_type *owner = get_entity_owner(entity);
1181 if (owner == get_segment_type(IR_SEGMENT_GLOBAL)) {
1182 if (is_method_entity(entity))
1183 return GAS_SECTION_TEXT;
1185 ir_linkage linkage = get_entity_linkage(entity);
1186 if (linkage & IR_LINKAGE_CONSTANT) {
1187 /* mach-o is the only one with a cstring section */
1188 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
1189 && ent_is_string_const(entity))
1190 return GAS_SECTION_CSTRING;
1192 return GAS_SECTION_RODATA;
1194 if (entity_is_null(entity))
1195 return GAS_SECTION_BSS;
1197 return GAS_SECTION_DATA;
1198 } else if (owner == env->main_env->pic_symbols_type) {
1199 return GAS_SECTION_PIC_SYMBOLS;
1200 } else if (owner == env->main_env->pic_trampolines_type) {
1201 return GAS_SECTION_PIC_TRAMPOLINES;
1202 } else if (owner == get_segment_type(IR_SEGMENT_CONSTRUCTORS)) {
1203 return GAS_SECTION_CONSTRUCTORS;
1204 } else if (owner == get_segment_type(IR_SEGMENT_DESTRUCTORS)) {
1205 return GAS_SECTION_DESTRUCTORS;
1206 } else if (owner == get_segment_type(IR_SEGMENT_THREAD_LOCAL)) {
1207 ir_linkage linkage = get_entity_linkage(entity);
1208 if (linkage & IR_LINKAGE_MERGE) {
1209 panic("IR_LINKAGE_MERGE not supported for THREAD_LOCAL entities");
1212 if (entity_is_null(entity)) {
1213 return GAS_SECTION_TLS_BSS;
1215 return GAS_SECTION_TLS_DATA;
1218 panic("Couldn't determine section for %+F?!?", entity);
1221 static void emit_common(const ir_entity *ent)
1223 const char *name = get_entity_ld_name(ent);
1224 unsigned size = get_type_size_bytes(get_entity_type(ent));
1225 unsigned alignment = get_effective_entity_alignment(ent);
1227 switch (be_gas_object_file_format) {
1228 case OBJECT_FILE_FORMAT_MACH_O:
1229 be_emit_irprintf("\t.comm %s,%u,%u\n", name, size,
1230 log2_floor(alignment));
1231 be_emit_write_line();
1233 case OBJECT_FILE_FORMAT_ELF:
1234 be_emit_irprintf("\t.comm %s,%u,%u\n", name, size, alignment);
1235 be_emit_write_line();
1237 case OBJECT_FILE_FORMAT_COFF:
1238 be_emit_irprintf("\t.comm %s,%u # %u\n", name, size, alignment);
1239 be_emit_write_line();
1242 panic("invalid object file format");
1245 static void dump_indirect_symbol(const ir_entity *entity, be_gas_section_t section)
1247 /* we can only do PIC code on macho so far */
1248 assert(be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O);
1250 be_emit_ident(get_entity_ld_ident(entity));
1251 be_emit_cstring(":\n");
1252 be_emit_write_line();
1253 be_emit_cstring("\t.indirect_symbol ");
1254 be_emit_ident(get_entity_ident(entity));
1256 be_emit_write_line();
1257 if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1258 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1259 be_emit_write_line();
1261 assert(section == GAS_SECTION_PIC_SYMBOLS);
1262 be_emit_cstring("\t.long 0\n");
1263 be_emit_write_line();
1268 * Dump a global entity.
1270 * @param env the gas output environment
1271 * @param ent the entity to be dumped
1273 static void dump_global(be_gas_decl_env_t *env, const ir_entity *ent)
1275 ir_type *type = get_entity_type(ent);
1276 ident *ld_ident = get_entity_ld_ident(ent);
1277 unsigned alignment = get_effective_entity_alignment(ent);
1278 be_gas_section_t section = determine_section(env, ent);
1280 /* we already emitted all methods. Except for the trampolines which
1281 * the assembler/linker generates */
1282 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1285 /* block labels are already emittet in the code */
1286 if (type == firm_code_type)
1289 be_dbg_variable(ent);
1291 /* nothing to do for externally defined values */
1292 if (get_entity_visibility(ent) == ir_visibility_external)
1295 if (!is_po2(alignment))
1296 panic("alignment not a power of 2");
1298 if (section == GAS_SECTION_BSS &&
1299 (get_entity_linkage(ent) & IR_LINKAGE_MERGE)) {
1300 if (get_entity_visibility(ent) != ir_visibility_default) {
1301 panic("merge link semantic not supported for local/extern entities");
1307 be_gas_emit_switch_section(section);
1309 if (section == GAS_SECTION_PIC_TRAMPOLINES
1310 || section == GAS_SECTION_PIC_SYMBOLS) {
1311 dump_indirect_symbol(ent, section);
1316 if (alignment > 1) {
1317 emit_align(alignment);
1319 emit_entity_visibility(ent);
1320 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF
1321 && be_gas_emit_types) {
1322 be_emit_cstring("\t.type\t");
1323 be_emit_ident(ld_ident);
1324 be_emit_cstring(", ");
1325 be_emit_char(be_gas_elf_type_char);
1326 be_emit_cstring("object\n\t.size\t");
1327 be_emit_ident(ld_ident);
1328 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1331 if (get_id_str(ld_ident)[0] != '\0') {
1332 be_emit_ident(ld_ident);
1333 be_emit_cstring(":\n");
1334 be_emit_write_line();
1337 if (section == GAS_SECTION_BSS || section == GAS_SECTION_TLS_BSS) {
1338 be_emit_irprintf("\t.zero %u\n", get_type_size_bytes(type));
1339 be_emit_write_line();
1340 } else if(entity_has_compound_ent_values(ent)) {
1341 dump_compound_graph_init(env, ent);
1343 assert(ent->initializer != NULL);
1344 dump_initializer(env, ent);
1349 * Dumps declarations of global variables and the initialization code.
1351 * @param gt a global like type, either the global or the TLS one
1352 * @param env an environment
1354 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env)
1356 int i, n = get_compound_n_members(gt);
1358 for (i = 0; i < n; i++) {
1359 ir_entity *ent = get_compound_member(gt, i);
1360 dump_global(env, ent);
1364 /************************************************************************/
1366 /* Generate all entities. */
1367 void be_gas_emit_decls(const be_main_env_t *main_env)
1369 be_gas_decl_env_t env;
1370 memset(&env, 0, sizeof(env));
1372 /* dump global type */
1373 env.main_env = main_env;
1374 env.section = (be_gas_section_t) -1;
1376 be_gas_dump_globals(get_glob_type(), &env);
1377 be_gas_dump_globals(get_tls_type(), &env);
1378 be_gas_dump_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env);
1379 be_gas_dump_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env);
1380 be_gas_dump_globals(main_env->pic_symbols_type, &env);
1381 be_gas_dump_globals(main_env->pic_trampolines_type, &env);
1384 * ".subsections_via_symbols marks object files which are OK to divide
1385 * their section contents into individual blocks".
1386 * From my understanding this means no label points in the middle of an
1387 * object which we want to address as a whole. Firm code should be fine
1390 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
1391 be_emit_cstring("\t.subsections_via_symbols\n");
1392 be_emit_write_line();