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 * An environment containing all needed dumper data.
56 * Currently we create the file completely in memory first, then
57 * write it to the disk. This is an artifact from the old C-generating backend
58 * and even there NOT needed. So we might change it in the future.
60 typedef struct _be_gas_decl_env {
61 be_gas_section_t section;
62 const be_main_env_t *main_env;
65 static void emit_section_macho(be_gas_section_t section)
67 be_gas_section_t base = section & GAS_SECTION_TYPE_MASK;
68 be_gas_section_t flags = section & ~GAS_SECTION_TYPE_MASK;
71 if (current_section == section)
73 current_section = section;
78 case GAS_SECTION_TEXT: name = "text"; break;
79 case GAS_SECTION_DATA: name = "data"; break;
80 case GAS_SECTION_RODATA: name = "const"; break;
81 case GAS_SECTION_BSS: name = "data"; break;
82 case GAS_SECTION_CONSTRUCTORS: name = "mod_init_func"; break;
83 case GAS_SECTION_DESTRUCTORS: name = "mod_term_func"; break;
84 case GAS_SECTION_PIC_TRAMPOLINES: name = "section\t__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5"; break;
85 case GAS_SECTION_PIC_SYMBOLS: name = "section\t__IMPORT,__pointers,non_lazy_symbol_pointers"; break;
86 case GAS_SECTION_CSTRING: name = "cstring"; break;
87 default: panic("unsupported scetion type 0x%X", section);
89 be_emit_irprintf("\t.%s\n", name);
91 } else if (flags & GAS_SECTION_FLAG_COMDAT) {
93 case GAS_SECTION_TEXT: name = "section __TEXT,__textcoal_nt,coalesced,pure_instructions"; break;
95 case GAS_SECTION_DATA: name = "section __DATA,__datacoal_nt,coalesced"; break;
96 case GAS_SECTION_RODATA: name = "section __TEXT,__const_coal,coalesced"; break;
97 case GAS_SECTION_CSTRING: name = "section __TEXT,__const_coal,coalesced"; break;
98 default: panic("unsupported scetion type 0x%X", section);
101 panic("unsupported section type 0x%X\n", section);
105 static void emit_section(be_gas_section_t section, const ir_entity *entity)
107 be_gas_section_t base = section & GAS_SECTION_TYPE_MASK;
108 be_gas_section_t flags = section & ~GAS_SECTION_TYPE_MASK;
109 static const char *const basename[] = {
110 "text", "data", "rodata", "bss", "ctors", "dtors"
112 static const char *const type[] = {
113 "progbits", "progbits", "progbits", "nobits", "init_array", "fini_array"
116 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
117 emit_section_macho(section);
121 if (current_section == section && !(section & GAS_SECTION_FLAG_COMDAT))
123 current_section = section;
128 case GAS_SECTION_TEXT:
129 be_emit_cstring("\t.text\n");
130 be_emit_write_line();
132 case GAS_SECTION_DATA:
133 be_emit_cstring("\t.data\n");
134 be_emit_write_line();
136 case GAS_SECTION_RODATA:
137 be_emit_cstring("\t.section\t.rodata\n");
138 be_emit_write_line();
140 case GAS_SECTION_BSS:
141 be_emit_cstring("\t.bss\n");
142 be_emit_write_line();
149 assert(base < sizeof(basename)/sizeof(basename[0]));
150 be_emit_cstring("\t.section\t.");
152 if (flags & GAS_SECTION_FLAG_TLS)
154 be_emit_string(basename[base]);
155 if (flags & GAS_SECTION_FLAG_COMDAT) {
157 be_gas_emit_entity(entity);
161 be_emit_cstring(",\"");
162 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_COFF)
164 if (base == GAS_SECTION_TEXT)
166 if (base != GAS_SECTION_RODATA && base != GAS_SECTION_TEXT)
168 if (flags & GAS_SECTION_FLAG_TLS)
170 if (flags & GAS_SECTION_FLAG_COMDAT)
173 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_COFF) {
174 be_emit_cstring("\",");
175 be_emit_char(be_gas_elf_type_char);
176 be_emit_string(type[base]);
179 if (flags & GAS_SECTION_FLAG_COMDAT) {
181 be_gas_emit_entity(entity);
182 be_emit_cstring(",comdat");
185 be_emit_write_line();
188 void be_gas_emit_switch_section(be_gas_section_t section)
190 /* you have to produce a switch_section call with entity manually
191 * for comdat sections */
192 assert( !(section & GAS_SECTION_FLAG_COMDAT));
194 emit_section(section, NULL);
197 static tarval *get_initializer_tarval(const ir_initializer_t *initializer)
199 if (initializer->kind == IR_INITIALIZER_TARVAL)
200 return initializer->tarval.value;
201 if (initializer->kind == IR_INITIALIZER_CONST) {
202 ir_node *node = initializer->consti.value;
203 if (is_Const(node)) {
204 return get_Const_tarval(node);
207 return get_tarval_undefined();
210 static int initializer_is_string_const(const ir_initializer_t *initializer)
213 int found_printable = 0;
215 if (initializer->kind != IR_INITIALIZER_COMPOUND)
218 len = initializer->compound.n_initializers;
221 for (i = 0; i < len; ++i) {
225 ir_initializer_t *sub_initializer
226 = initializer->compound.initializers[i];
228 tv = get_initializer_tarval(sub_initializer);
229 if (!tarval_is_constant(tv))
232 mode = get_tarval_mode(tv);
233 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
236 c = get_tarval_long(tv);
237 if (isgraph(c) || isspace(c))
242 if (i == len - 1 && c != '\0')
246 return found_printable;
249 static bool initializer_is_null(const ir_initializer_t *initializer)
251 switch (initializer->kind) {
252 case IR_INITIALIZER_NULL:
254 case IR_INITIALIZER_TARVAL: {
255 tarval *tv = initializer->tarval.value;
256 return tarval_is_null(tv);
258 case IR_INITIALIZER_CONST: {
259 ir_node *value = initializer->consti.value;
260 if (!is_Const(value))
262 return is_Const_null(value);
264 case IR_INITIALIZER_COMPOUND: {
266 for (i = 0; i < initializer->compound.n_initializers; ++i) {
267 ir_initializer_t *subinitializer
268 = initializer->compound.initializers[i];
269 if (!initializer_is_null(subinitializer))
275 panic("invalid initializer in initializer_is_null");
279 * Determine if an entity is a string constant
280 * @param ent The entity
281 * @return 1 if it is a string constant, 0 otherwise
283 static int entity_is_string_const(const ir_entity *ent)
285 ir_type *type, *element_type;
289 type = get_entity_type(ent);
291 /* if it's an array */
292 if (!is_Array_type(type))
295 element_type = get_array_element_type(type);
297 /* and the array's element type is primitive */
298 if (!is_Primitive_type(element_type))
301 /* and the mode of the element type is an int of
302 * the same size as the byte mode */
303 mode = get_type_mode(element_type);
304 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
307 if (ent->initializer != NULL) {
308 return initializer_is_string_const(ent->initializer);
309 } else if (entity_has_compound_ent_values(ent)) {
310 int found_printable = 0;
311 /* if it contains only printable chars and a 0 at the end */
312 n = get_compound_ent_n_values(ent);
313 for (i = 0; i < n; ++i) {
314 ir_node *irn = get_compound_ent_value(ent, i);
318 c = (int) get_tarval_long(get_Const_tarval(irn));
320 if (isgraph(c) || isspace(c))
325 if (i == n - 1 && c != '\0')
328 return found_printable;
334 static bool entity_is_null(const ir_entity *entity)
336 if (entity->initializer != NULL) {
337 return initializer_is_null(entity->initializer);
338 } else if (entity_has_compound_ent_values(entity)) {
339 /* I'm too lazy to implement this case as compound graph paths will be
340 * remove anyway in the future */
343 /* uninitialized, NULL is fine */
347 static bool is_comdat(const ir_entity *entity)
349 ir_linkage linkage = get_entity_linkage(entity);
350 return (linkage & IR_LINKAGE_MERGE)
351 && (linkage & IR_LINKAGE_GARBAGE_COLLECT);
354 static be_gas_section_t determine_basic_section(const ir_entity *entity)
358 if (is_method_entity(entity))
359 return GAS_SECTION_TEXT;
361 linkage = get_entity_linkage(entity);
362 if (linkage & IR_LINKAGE_CONSTANT) {
363 /* mach-o is the only one with a cstring section */
364 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
365 && entity_is_string_const(entity))
366 return GAS_SECTION_CSTRING;
368 return GAS_SECTION_RODATA;
370 if (entity_is_null(entity))
371 return GAS_SECTION_BSS;
373 return GAS_SECTION_DATA;
376 static be_gas_section_t determine_section(be_gas_decl_env_t *env,
377 const ir_entity *entity)
379 ir_type *owner = get_entity_owner(entity);
381 if (owner == get_segment_type(IR_SEGMENT_GLOBAL)) {
382 be_gas_section_t section = determine_basic_section(entity);
383 if (is_comdat(entity))
384 section |= GAS_SECTION_FLAG_COMDAT;
386 } else if (env != NULL && owner == env->main_env->pic_symbols_type) {
387 return GAS_SECTION_PIC_SYMBOLS;
388 } else if (env != NULL && owner == env->main_env->pic_trampolines_type) {
389 return GAS_SECTION_PIC_TRAMPOLINES;
390 } else if (owner == get_segment_type(IR_SEGMENT_CONSTRUCTORS)) {
391 return GAS_SECTION_CONSTRUCTORS;
392 } else if (owner == get_segment_type(IR_SEGMENT_DESTRUCTORS)) {
393 return GAS_SECTION_DESTRUCTORS;
394 } else if (owner == get_segment_type(IR_SEGMENT_THREAD_LOCAL)) {
395 be_gas_section_t section = determine_basic_section(entity);
396 if (is_comdat(entity))
397 section |= GAS_SECTION_FLAG_COMDAT;
399 return section | GAS_SECTION_FLAG_TLS;
402 /* the java frontend keeps some functions inside classes */
403 if (is_Class_type(owner)) {
404 return determine_basic_section(entity);
407 panic("Couldn't determine section for %+F?!?", entity);
410 static void emit_weak(const ir_entity *entity)
412 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
413 be_emit_cstring("\t.weak_reference ");
415 be_emit_cstring("\t.weak ");
417 be_gas_emit_entity(entity);
419 be_emit_write_line();
422 static void emit_visibility(const ir_entity *entity)
424 ir_linkage linkage = get_entity_linkage(entity);
426 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
428 /* Note: .weak seems to imply .globl so no need to output .globl */
429 } else if (get_entity_visibility(entity) == ir_visibility_default) {
430 be_emit_cstring(".globl ");
431 be_gas_emit_entity(entity);
433 be_emit_write_line();
436 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
437 && (linkage & IR_LINKAGE_HIDDEN_USER)) {
438 be_emit_cstring("\t.no_dead_strip ");
439 be_gas_emit_entity(entity);
441 be_emit_write_line();
445 void be_gas_emit_function_prolog(const ir_entity *entity, unsigned po2alignment)
447 be_gas_section_t section = determine_section(NULL, entity);
448 emit_section(section, entity);
450 /* write the begin line (makes the life easier for scripts parsing the
452 be_emit_write_line();
453 be_emit_cstring("# -- Begin ");
454 be_gas_emit_entity(entity);
456 be_emit_write_line();
458 if (po2alignment > 0) {
459 const char *fill_byte = "";
460 unsigned maximum_skip = (1 << po2alignment) - 1;
461 /* gcc fills space between function with 0x90... */
462 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
465 be_emit_cstring("\t.p2align ");
466 be_emit_irprintf("%u,%s,%u\n", po2alignment, fill_byte, maximum_skip);
467 be_emit_write_line();
469 emit_visibility(entity);
471 switch (be_gas_object_file_format) {
472 case OBJECT_FILE_FORMAT_ELF:
473 be_emit_cstring("\t.type\t");
474 be_gas_emit_entity(entity);
475 be_emit_cstring(", ");
476 be_emit_char(be_gas_elf_type_char);
477 be_emit_cstring("function\n");
478 be_emit_write_line();
480 case OBJECT_FILE_FORMAT_COFF:
481 be_emit_cstring("\t.def\t");
482 be_gas_emit_entity(entity);
483 be_emit_cstring(";");
484 if (get_entity_visibility(entity) == ir_visibility_local) {
485 be_emit_cstring("\t.scl\t3;");
487 be_emit_cstring("\t.scl\t2;");
489 be_emit_cstring("\t.type\t32;\t.endef\n");
490 be_emit_write_line();
492 case OBJECT_FILE_FORMAT_MACH_O:
495 be_gas_emit_entity(entity);
496 be_emit_cstring(":\n");
497 be_emit_write_line();
500 void be_gas_emit_function_epilog(const ir_entity *entity)
502 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF) {
503 be_emit_cstring("\t.size\t");
504 be_gas_emit_entity(entity);
505 be_emit_cstring(", .-");
506 be_gas_emit_entity(entity);
508 be_emit_write_line();
511 be_emit_cstring("# -- End ");
512 be_gas_emit_entity(entity);
514 be_emit_write_line();
517 /************************************************************************/
522 * @param tv the tarval
523 * @param bytes the width of the tarvals value in bytes
525 static void emit_arith_tarval(tarval *tv, int bytes)
529 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
533 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
537 be_emit_irprintf("0x%02x%02x%02x%02x",
538 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
542 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
543 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
544 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
548 /* Beware: Mixed endian output! One little endian number emitted as
549 * three longs. Each long initializer is written in big endian. */
551 "\t.long\t0x%02x%02x%02x%02x\n"
552 "\t.long\t0x%02x%02x%02x%02x\n"
553 "\t.long\t0x%02x%02x%02x%02x",
554 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
555 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
556 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
557 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
558 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
559 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8)
564 /* Beware: Mixed endian output! One little endian number emitted as
565 * three longs. Each long initializer is written in big endian. */
567 "\t.long\t0x%02x%02x%02x%02x\n"
568 "\t.long\t0x%02x%02x%02x%02x\n"
569 "\t.long\t0x%02x%02x%02x%02x\n"
570 "\t.long\t0x%02x%02x%02x%02x",
571 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
572 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
573 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
574 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
575 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
576 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
577 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 14),
578 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12)
583 panic("Can't dump a tarval with %d bytes", bytes);
587 * Return the label prefix for labeled instructions.
589 const char *be_gas_insn_label_prefix(void)
595 * Return the tarval of an atomic initializer.
597 * @param init a node representing the initializer (on the const code irg)
601 static tarval *get_atomic_init_tv(ir_node *init)
604 ir_mode *mode = get_irn_mode(init);
606 switch (get_irn_opcode(init)) {
609 init = get_Cast_op(init);
613 init = get_Conv_op(init);
617 return get_Const_tarval(init);
620 switch (get_SymConst_kind(init)) {
621 case symconst_type_size:
622 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
624 case symconst_type_align:
625 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
627 case symconst_ofs_ent:
628 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
630 case symconst_enum_const:
631 return get_enumeration_value(get_SymConst_enum(init));
644 * Dump an atomic value.
646 * @param env the gas output environment
647 * @param init a node representing the atomic value (on the const code irg)
649 static void do_emit_atomic_init(be_gas_decl_env_t *env, ir_node *init)
651 ir_mode *mode = get_irn_mode(init);
652 int bytes = get_mode_size_bytes(mode);
656 init = skip_Id(init);
658 switch (get_irn_opcode(init)) {
660 do_emit_atomic_init(env, get_Cast_op(init));
664 do_emit_atomic_init(env, get_Conv_op(init));
668 tv = get_Const_tarval(init);
670 /* it's a arithmetic value */
671 emit_arith_tarval(tv, bytes);
675 switch (get_SymConst_kind(init)) {
676 case symconst_addr_ent:
677 ent = get_SymConst_entity(init);
678 be_gas_emit_entity(ent);
681 case symconst_ofs_ent:
682 ent = get_SymConst_entity(init);
683 be_emit_irprintf("%d", get_entity_offset(ent));
686 case symconst_type_size:
687 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
690 case symconst_type_align:
691 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
694 case symconst_enum_const:
695 tv = get_enumeration_value(get_SymConst_enum(init));
696 emit_arith_tarval(tv, bytes);
700 assert(!"emit_atomic_init(): don't know how to init from this SymConst");
705 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
706 panic("Constant must be int or pointer for '+' to work");
708 do_emit_atomic_init(env, get_Add_left(init));
709 be_emit_cstring(" + ");
710 do_emit_atomic_init(env, get_Add_right(init));
714 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
715 panic("Constant must be int or pointer for '-' to work");
717 do_emit_atomic_init(env, get_Sub_left(init));
718 be_emit_cstring(" - ");
719 do_emit_atomic_init(env, get_Sub_right(init));
723 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
724 panic("Constant must be int or pointer for '*' to work");
726 do_emit_atomic_init(env, get_Mul_left(init));
727 be_emit_cstring(" * ");
728 do_emit_atomic_init(env, get_Mul_right(init));
732 be_emit_cstring("0");
736 panic("emit_atomic_init(): unsupported IR-node %+F", init);
741 * Dumps the type for given size (.byte, .long, ...)
743 * @param size the size in bytes
745 static void emit_size_type(size_t size)
749 be_emit_cstring("\t.byte\t");
753 be_emit_cstring("\t.short\t");
757 be_emit_cstring("\t.long\t");
761 be_emit_cstring("\t.quad\t");
766 case 16: /* Note: .octa does not work on mac */
767 /* handled in arith */
771 panic("Try to dump a type with %u bytes", (unsigned)size);
776 * Emit an atomic value.
778 * @param env the gas output environment
779 * @param init a node representing the atomic value (on the const code irg)
781 static void emit_atomic_init(be_gas_decl_env_t *env, ir_node *init)
783 ir_mode *mode = get_irn_mode(init);
784 int bytes = get_mode_size_bytes(mode);
786 emit_size_type(bytes);
787 do_emit_atomic_init(env, init);
789 be_emit_write_line();
792 /************************************************************************/
793 /* Routines to dump global variables */
794 /************************************************************************/
797 * Dump a string constant.
798 * No checks are made!!
800 * @param ent The entity to dump.
802 static void emit_string_cst(const ir_entity *ent)
810 len = get_compound_ent_n_values(ent);
812 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
813 be_emit_cstring("\t.ascii \"");
815 be_emit_cstring("\t.string \"");
819 for (i = 0; i < output_len; ++i) {
823 irn = get_compound_ent_value(ent, i);
824 c = (int) get_tarval_long(get_Const_tarval(irn));
827 case '"' : be_emit_cstring("\\\""); break;
828 case '\n': be_emit_cstring("\\n"); break;
829 case '\r': be_emit_cstring("\\r"); break;
830 case '\t': be_emit_cstring("\\t"); break;
831 case '\\': be_emit_cstring("\\\\"); break;
836 be_emit_irprintf("\\%o", c);
840 be_emit_cstring("\"\n");
841 be_emit_write_line();
843 type = get_entity_type(ent);
844 type_size = get_type_size_bytes(type);
845 remaining_space = type_size - len;
846 assert(remaining_space >= 0);
847 if (remaining_space > 0) {
848 be_emit_irprintf("\t.space\t%d\n", remaining_space);
852 static void emit_string_initializer(const ir_initializer_t *initializer)
856 len = initializer->compound.n_initializers;
857 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
858 be_emit_cstring("\t.ascii \"");
860 be_emit_cstring("\t.string \"");
864 for (i = 0; i < len; ++i) {
865 const ir_initializer_t *sub_initializer
866 = get_initializer_compound_value(initializer, i);
868 tarval *tv = get_initializer_tarval(sub_initializer);
869 int c = get_tarval_long(tv);
872 case '"' : be_emit_cstring("\\\""); break;
873 case '\n': be_emit_cstring("\\n"); break;
874 case '\r': be_emit_cstring("\\r"); break;
875 case '\t': be_emit_cstring("\\t"); break;
876 case '\\': be_emit_cstring("\\\\"); break;
881 be_emit_irprintf("\\%o", c);
885 be_emit_cstring("\"\n");
886 be_emit_write_line();
889 enum normal_or_bitfield_kind {
896 enum normal_or_bitfield_kind kind;
900 unsigned char bf_val;
902 } normal_or_bitfield;
904 static int is_type_variable_size(ir_type *type)
911 static size_t get_initializer_size(const ir_initializer_t *initializer,
914 switch (get_initializer_kind(initializer)) {
915 case IR_INITIALIZER_TARVAL:
916 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
917 return get_type_size_bytes(type);
918 case IR_INITIALIZER_CONST:
919 case IR_INITIALIZER_NULL:
920 return get_type_size_bytes(type);
921 case IR_INITIALIZER_COMPOUND:
922 if (!is_type_variable_size(type)) {
923 return get_type_size_bytes(type);
926 = get_initializer_compound_n_entries(initializer);
928 unsigned initializer_size = get_type_size_bytes(type);
929 for (i = 0; i < n_entries; ++i) {
930 ir_entity *entity = get_compound_member(type, i);
931 ir_type *type = get_entity_type(entity);
933 const ir_initializer_t *sub_initializer
934 = get_initializer_compound_value(initializer, i);
936 unsigned offset = get_entity_offset(entity);
937 unsigned size = get_initializer_size(sub_initializer, type);
939 if (offset + size > initializer_size) {
940 initializer_size = offset + size;
943 return initializer_size;
947 panic("found invalid initializer");
951 static normal_or_bitfield *glob_vals;
952 static size_t max_vals;
955 static void emit_bitfield(normal_or_bitfield *vals, size_t offset_bits,
956 const ir_initializer_t *initializer, ir_type *type)
958 unsigned char last_bits = 0;
959 ir_mode *mode = get_type_mode(type);
961 unsigned char curr_bits;
965 switch (get_initializer_kind(initializer)) {
966 case IR_INITIALIZER_NULL:
968 case IR_INITIALIZER_TARVAL:
969 tv = get_initializer_tarval_value(initializer);
971 case IR_INITIALIZER_CONST: {
972 ir_node *node = get_initializer_const_value(initializer);
973 if (!is_Const(node)) {
974 panic("bitfield initializer not a Const node");
976 tv = get_Const_tarval(node);
979 case IR_INITIALIZER_COMPOUND:
980 panic("bitfield initializer is compound");
983 panic("Couldn't get numeric value for bitfield initializer");
985 tv = tarval_convert_to(tv, get_type_mode(type));
987 /* normalize offset */
988 vals += offset_bits >> 3;
990 value_len = get_mode_size_bits(mode);
992 /* combine bits with existing bits */
993 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
994 assert((size_t) (vals - glob_vals) + j < max_vals);
995 assert(vals[j].kind == BITFIELD ||
996 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
997 vals[j].kind = BITFIELD;
998 curr_bits = get_tarval_sub_bits(tv, j);
1000 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1002 last_bits = curr_bits;
1006 static void emit_ir_initializer(normal_or_bitfield *vals,
1007 const ir_initializer_t *initializer,
1010 assert((size_t) (vals - glob_vals) < max_vals);
1012 switch (get_initializer_kind(initializer)) {
1013 case IR_INITIALIZER_NULL:
1015 case IR_INITIALIZER_TARVAL: {
1018 assert(vals->kind != BITFIELD);
1019 vals->kind = TARVAL;
1020 vals->v.tarval = get_initializer_tarval_value(initializer);
1021 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
1022 for (i = 1; i < get_type_size_bytes(type); ++i) {
1023 vals[i].kind = NORMAL;
1024 vals[i].v.value = NULL;
1028 case IR_INITIALIZER_CONST: {
1031 assert(vals->kind != BITFIELD);
1032 vals->kind = NORMAL;
1033 vals->v.value = get_initializer_const_value(initializer);
1034 for (i = 1; i < get_type_size_bytes(type); ++i) {
1035 vals[i].kind = NORMAL;
1036 vals[i].v.value = NULL;
1040 case IR_INITIALIZER_COMPOUND: {
1042 size_t n = get_initializer_compound_n_entries(initializer);
1044 if (is_Array_type(type)) {
1045 ir_type *element_type = get_array_element_type(type);
1046 size_t skip = get_type_size_bytes(element_type);
1047 size_t alignment = get_type_alignment_bytes(element_type);
1048 size_t misalign = skip % alignment;
1049 if (misalign != 0) {
1050 skip += alignment - misalign;
1053 for (i = 0; i < n; ++i) {
1054 ir_initializer_t *sub_initializer
1055 = get_initializer_compound_value(initializer, i);
1057 emit_ir_initializer(vals, sub_initializer, element_type);
1062 size_t n_members, i;
1063 assert(is_compound_type(type));
1064 n_members = get_compound_n_members(type);
1065 for (i = 0; i < n_members; ++i) {
1066 ir_entity *member = get_compound_member(type, i);
1067 size_t offset = get_entity_offset(member);
1068 ir_type *subtype = get_entity_type(member);
1069 ir_mode *mode = get_type_mode(subtype);
1070 ir_initializer_t *sub_initializer;
1072 assert(i < get_initializer_compound_n_entries(initializer));
1074 = get_initializer_compound_value(initializer, i);
1078 = get_entity_offset_bits_remainder(member);
1079 size_t value_len = get_mode_size_bits(mode);
1081 if (offset_bits != 0 ||
1082 (value_len != 8 && value_len != 16 && value_len != 32
1083 && value_len != 64)) {
1084 emit_bitfield(&vals[offset], offset_bits,
1085 sub_initializer, subtype);
1090 emit_ir_initializer(&vals[offset], sub_initializer, subtype);
1097 panic("invalid ir_initializer kind found");
1100 static void emit_initializer(be_gas_decl_env_t *env, const ir_entity *entity)
1102 const ir_initializer_t *initializer = entity->initializer;
1104 normal_or_bitfield *vals;
1108 if (initializer_is_string_const(initializer)) {
1109 emit_string_initializer(initializer);
1113 type = get_entity_type(entity);
1114 size = get_initializer_size(initializer, type);
1120 * In the worst case, every initializer allocates one byte.
1121 * Moreover, initializer might be big, do not allocate on stack.
1123 vals = XMALLOCNZ(normal_or_bitfield, size);
1130 emit_ir_initializer(vals, initializer, type);
1132 /* now write values sorted */
1133 for (k = 0; k < size; ) {
1136 if (vals[k].kind == NORMAL) {
1137 if (vals[k].v.value != NULL) {
1138 emit_atomic_init(env, vals[k].v.value);
1139 elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
1143 } else if (vals[k].kind == TARVAL) {
1144 tarval *tv = vals[k].v.tarval;
1145 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
1150 emit_size_type(size);
1151 emit_arith_tarval(tv, size);
1153 be_emit_write_line();
1155 assert(vals[k].kind == BITFIELD);
1156 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1157 be_emit_write_line();
1161 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1168 be_emit_irprintf("\t.space\t%d\n", space);
1169 be_emit_write_line();
1175 static void emit_compound_graph_init(be_gas_decl_env_t *env,
1176 const ir_entity *ent)
1178 normal_or_bitfield *vals;
1180 unsigned k, last_ofs;
1182 if (entity_is_string_const(ent)) {
1183 emit_string_cst(ent);
1187 n = get_compound_ent_n_values(ent);
1189 /* Find the initializer size. Sorrily gcc support a nasty feature:
1190 The last field of a compound may be a flexible array. This allows
1191 initializers bigger than the type size. */
1192 last_ofs = get_type_size_bytes(get_entity_type(ent));
1193 for (i = 0; i < n; ++i) {
1194 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1195 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1196 ir_node *value = get_compound_ent_value(ent, i);
1197 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
1199 offset += (value_len + bits_remainder + 7) >> 3;
1201 if (offset > last_ofs) {
1207 * In the worst case, every initializer allocates one byte.
1208 * Moreover, initializer might be big, do not allocate on stack.
1210 vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1212 /* collect the values and store them at the offsets */
1213 for (i = 0; i < n; ++i) {
1214 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1215 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1216 ir_node *value = get_compound_ent_value(ent, i);
1217 int value_len = get_mode_size_bits(get_irn_mode(value));
1219 assert(offset_bits >= 0);
1221 if (offset_bits != 0 ||
1222 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1223 tarval *tv = get_atomic_init_tv(value);
1224 unsigned char curr_bits, last_bits = 0;
1226 panic("Couldn't get numeric value for bitfield initializer '%s'",
1227 get_entity_ld_name(ent));
1229 /* normalize offset */
1230 offset += offset_bits >> 3;
1233 for (j = 0; value_len + offset_bits > 0; ++j) {
1234 assert(offset + j < last_ofs);
1235 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1236 vals[offset + j].kind = BITFIELD;
1237 curr_bits = get_tarval_sub_bits(tv, j);
1238 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1240 last_bits = curr_bits;
1245 assert(offset < last_ofs);
1246 assert(vals[offset].kind == NORMAL);
1247 for (i = 1; i < value_len / 8; ++i) {
1248 assert(vals[offset + i].v.value == NULL);
1250 vals[offset].v.value = value;
1254 /* now write them sorted */
1255 for (k = 0; k < last_ofs; ) {
1256 int space = 0, skip = 0;
1257 if (vals[k].kind == NORMAL) {
1258 if (vals[k].v.value != NULL) {
1259 emit_atomic_init(env, vals[k].v.value);
1260 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1265 assert(vals[k].kind == BITFIELD);
1266 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1270 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1279 be_emit_irprintf("\t.space\t%d\n", space);
1280 be_emit_write_line();
1286 static void emit_align(unsigned p2alignment)
1288 be_emit_irprintf("\t.p2align\t%u\n", log2_floor(p2alignment));
1289 be_emit_write_line();
1292 static unsigned get_effective_entity_alignment(const ir_entity *entity)
1294 unsigned alignment = get_entity_alignment(entity);
1295 if (alignment == 0) {
1296 ir_type *type = get_entity_type(entity);
1297 alignment = get_type_alignment_bytes(type);
1302 static void emit_common(const ir_entity *entity)
1304 unsigned size = get_type_size_bytes(get_entity_type(entity));
1305 unsigned alignment = get_effective_entity_alignment(entity);
1307 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
1311 switch (be_gas_object_file_format) {
1312 case OBJECT_FILE_FORMAT_MACH_O:
1313 be_emit_cstring("\t.comm ");
1314 be_gas_emit_entity(entity);
1315 be_emit_irprintf(",%u,%u\n", size, log2_floor(alignment));
1316 be_emit_write_line();
1318 case OBJECT_FILE_FORMAT_ELF:
1319 be_emit_cstring("\t.comm ");
1320 be_gas_emit_entity(entity);
1321 be_emit_irprintf(",%u,%u\n", size, alignment);
1322 be_emit_write_line();
1324 case OBJECT_FILE_FORMAT_COFF:
1325 be_emit_cstring("\t.comm ");
1326 be_gas_emit_entity(entity);
1327 be_emit_irprintf(",%u # %u\n", size, alignment);
1328 be_emit_write_line();
1331 panic("invalid object file format");
1334 static void emit_local_common(const ir_entity *entity)
1336 unsigned size = get_type_size_bytes(get_entity_type(entity));
1337 unsigned alignment = get_effective_entity_alignment(entity);
1339 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
1343 switch (be_gas_object_file_format) {
1344 case OBJECT_FILE_FORMAT_MACH_O:
1345 be_emit_cstring("\t.lcomm ");
1346 be_gas_emit_entity(entity);
1347 be_emit_irprintf(",%u,%u\n", size, log2_floor(alignment));
1348 be_emit_write_line();
1350 case OBJECT_FILE_FORMAT_ELF:
1351 be_emit_cstring("\t.local ");
1352 be_gas_emit_entity(entity);
1353 be_emit_cstring("\n");
1354 be_emit_write_line();
1355 be_emit_cstring("\t.comm ");
1356 be_gas_emit_entity(entity);
1357 be_emit_irprintf(",%u,%u\n", size, alignment);
1358 be_emit_write_line();
1360 case OBJECT_FILE_FORMAT_COFF:
1361 be_emit_cstring("\t.lcomm ");
1362 be_gas_emit_entity(entity);
1363 be_emit_irprintf(",%u # %u\n", size, alignment);
1364 be_emit_write_line();
1367 panic("invalid object file format");
1370 static void emit_indirect_symbol(const ir_entity *entity, be_gas_section_t section)
1372 /* we can only do PIC code on macho so far */
1373 assert(be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O);
1375 be_gas_emit_entity(entity);
1376 be_emit_cstring(":\n");
1377 be_emit_write_line();
1378 be_emit_cstring("\t.indirect_symbol ");
1379 be_emit_ident(get_entity_ident(entity));
1381 be_emit_write_line();
1382 if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1383 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1384 be_emit_write_line();
1386 assert(section == GAS_SECTION_PIC_SYMBOLS);
1387 be_emit_cstring("\t.long 0\n");
1388 be_emit_write_line();
1392 char const *be_gas_get_private_prefix(void)
1395 be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O ? "L" :
1399 void be_gas_emit_entity(const ir_entity *entity)
1401 if (entity->type == firm_code_type) {
1402 ir_label_t label = get_entity_label(entity);
1403 be_emit_irprintf("%s_%lu", be_gas_get_private_prefix(), label);
1407 if (get_entity_visibility(entity) == ir_visibility_private) {
1408 be_emit_string(be_gas_get_private_prefix());
1410 be_emit_ident(get_entity_ld_ident(entity));
1413 void be_gas_emit_block_name(const ir_node *block)
1415 if (has_Block_entity(block)) {
1416 ir_entity *entity = get_Block_entity(block);
1417 be_gas_emit_entity(entity);
1419 be_emit_irprintf("%s%ld", be_gas_get_private_prefix(), get_irn_node_nr(block));
1424 * Dump a global entity.
1426 * @param env the gas output environment
1427 * @param ent the entity to be dumped
1429 static void emit_global(be_gas_decl_env_t *env, const ir_entity *entity)
1431 ir_type *type = get_entity_type(entity);
1432 ident *ld_ident = get_entity_ld_ident(entity);
1433 unsigned alignment = get_effective_entity_alignment(entity);
1434 be_gas_section_t section = determine_section(env, entity);
1435 ir_visibility visibility = get_entity_visibility(entity);
1436 ir_linkage linkage = get_entity_linkage(entity);
1438 /* block labels are already emittet in the code */
1439 if (type == firm_code_type)
1442 /* we already emitted all methods. Except for the trampolines which
1443 * the assembler/linker generates */
1444 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1445 /* functions with graph are already emitted with
1446 * be_gas_emit_function_prolog */
1447 if (get_entity_irg(entity) == NULL) {
1448 emit_visibility(entity);
1453 be_dbg_variable(entity);
1455 if (section == GAS_SECTION_BSS) {
1456 ir_visibility visibility = get_entity_visibility(entity);
1458 switch (visibility) {
1459 case ir_visibility_local:
1460 case ir_visibility_private:
1461 emit_local_common(entity);
1463 case ir_visibility_default:
1464 if (linkage & IR_LINKAGE_MERGE) {
1465 emit_common(entity);
1469 case ir_visibility_external:
1470 if (linkage & IR_LINKAGE_MERGE)
1471 panic("merge link semantic not supported for extern entities");
1476 emit_visibility(entity);
1477 if (visibility == ir_visibility_external) {
1478 /* nothing to do for externally defined values */
1482 if (!is_po2(alignment))
1483 panic("alignment not a power of 2");
1485 emit_section(section, entity);
1487 if (section == GAS_SECTION_PIC_TRAMPOLINES
1488 || section == GAS_SECTION_PIC_SYMBOLS) {
1489 emit_indirect_symbol(entity, section);
1494 if (alignment > 1) {
1495 emit_align(alignment);
1497 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF
1498 && be_gas_emit_types) {
1499 be_emit_cstring("\t.type\t");
1500 be_gas_emit_entity(entity);
1501 be_emit_cstring(", ");
1502 be_emit_char(be_gas_elf_type_char);
1503 be_emit_cstring("object\n\t.size\t");\
1504 be_gas_emit_entity(entity);
1505 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1508 if (get_id_str(ld_ident)[0] != '\0') {
1509 be_gas_emit_entity(entity);
1510 be_emit_cstring(":\n");
1511 be_emit_write_line();
1514 if (entity_is_null(entity)) {
1515 be_emit_irprintf("\t.space %u\n", get_type_size_bytes(type));
1516 be_emit_write_line();
1517 } else if (entity_has_compound_ent_values(entity)) {
1518 emit_compound_graph_init(env, entity);
1520 assert(entity->initializer != NULL);
1521 emit_initializer(env, entity);
1526 * Dumps declarations of global variables and the initialization code.
1528 * @param gt a global like type, either the global or the TLS one
1529 * @param env an environment
1531 static void be_gas_emit_globals(ir_type *gt, be_gas_decl_env_t *env)
1533 int i, n = get_compound_n_members(gt);
1535 for (i = 0; i < n; i++) {
1536 ir_entity *ent = get_compound_member(gt, i);
1537 emit_global(env, ent);
1541 /************************************************************************/
1543 /* Generate all entities. */
1544 void be_gas_emit_decls(const be_main_env_t *main_env)
1546 be_gas_decl_env_t env;
1547 memset(&env, 0, sizeof(env));
1549 /* dump global type */
1550 env.main_env = main_env;
1551 env.section = (be_gas_section_t) -1;
1553 be_gas_emit_globals(get_glob_type(), &env);
1554 be_gas_emit_globals(get_tls_type(), &env);
1555 be_gas_emit_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env);
1556 be_gas_emit_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env);
1557 be_gas_emit_globals(main_env->pic_symbols_type, &env);
1558 be_gas_emit_globals(main_env->pic_trampolines_type, &env);
1561 * ".subsections_via_symbols marks object files which are OK to divide
1562 * their section contents into individual blocks".
1563 * From my understanding this means no label points in the middle of an
1564 * object which we want to address as a whole. Firm code should be fine
1567 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
1568 be_emit_cstring("\t.subsections_via_symbols\n");
1569 be_emit_write_line();