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_sparc(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"
113 if (current_section == section && !(section & GAS_SECTION_FLAG_COMDAT))
115 current_section = section;
117 be_emit_cstring("\t.section\t\".");
119 /* Part1: section-name */
120 if (flags & GAS_SECTION_FLAG_TLS)
122 assert(base < sizeof(basename)/sizeof(basename[0]));
123 be_emit_string(basename[base]);
125 if (flags & GAS_SECTION_FLAG_COMDAT) {
127 be_gas_emit_entity(entity);
131 /* for the simple sections we're done here */
135 be_emit_cstring(",#alloc");
138 case GAS_SECTION_TEXT: be_emit_cstring(",#execinstr"); break;
139 case GAS_SECTION_DATA:
140 case GAS_SECTION_BSS: be_emit_cstring(",#write"); break;
145 if (flags & GAS_SECTION_FLAG_TLS) {
146 be_emit_cstring(",#tls");
151 be_emit_write_line();
154 static void emit_section(be_gas_section_t section, const ir_entity *entity)
156 be_gas_section_t base = section & GAS_SECTION_TYPE_MASK;
157 be_gas_section_t flags = section & ~GAS_SECTION_TYPE_MASK;
158 static const char *const basename[] = {
159 "text", "data", "rodata", "bss", "ctors", "dtors"
161 static const char *const type[] = {
162 "progbits", "progbits", "progbits", "nobits", "init_array", "fini_array"
165 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
166 emit_section_macho(section);
168 } else if(be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF_SPARC) {
169 emit_section_sparc(section, entity);
173 if (current_section == section && !(section & GAS_SECTION_FLAG_COMDAT))
175 current_section = section;
180 case GAS_SECTION_TEXT:
181 be_emit_cstring("\t.text\n");
182 be_emit_write_line();
184 case GAS_SECTION_DATA:
185 be_emit_cstring("\t.data\n");
186 be_emit_write_line();
188 case GAS_SECTION_RODATA:
189 be_emit_cstring("\t.section\t.rodata\n");
190 be_emit_write_line();
192 case GAS_SECTION_BSS:
193 be_emit_cstring("\t.bss\n");
194 be_emit_write_line();
201 assert(base < sizeof(basename)/sizeof(basename[0]));
202 be_emit_cstring("\t.section\t.");
204 if (flags & GAS_SECTION_FLAG_TLS)
206 be_emit_string(basename[base]);
207 if (flags & GAS_SECTION_FLAG_COMDAT) {
209 be_gas_emit_entity(entity);
213 be_emit_cstring(",\"");
214 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_COFF)
216 if (base == GAS_SECTION_TEXT)
218 if (base != GAS_SECTION_RODATA && base != GAS_SECTION_TEXT)
220 if (flags & GAS_SECTION_FLAG_TLS)
222 if (flags & GAS_SECTION_FLAG_COMDAT)
225 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_COFF) {
226 be_emit_cstring("\",");
227 be_emit_char(be_gas_elf_type_char);
228 be_emit_string(type[base]);
231 if (flags & GAS_SECTION_FLAG_COMDAT) {
233 be_gas_emit_entity(entity);
234 be_emit_cstring(",comdat");
237 be_emit_write_line();
242 void be_gas_emit_switch_section(be_gas_section_t section)
244 /* you have to produce a switch_section call with entity manually
245 * for comdat sections */
246 assert( !(section & GAS_SECTION_FLAG_COMDAT));
248 emit_section(section, NULL);
251 static tarval *get_initializer_tarval(const ir_initializer_t *initializer)
253 if (initializer->kind == IR_INITIALIZER_TARVAL)
254 return initializer->tarval.value;
255 if (initializer->kind == IR_INITIALIZER_CONST) {
256 ir_node *node = initializer->consti.value;
257 if (is_Const(node)) {
258 return get_Const_tarval(node);
261 return get_tarval_undefined();
264 static int initializer_is_string_const(const ir_initializer_t *initializer)
267 int found_printable = 0;
269 if (initializer->kind != IR_INITIALIZER_COMPOUND)
272 len = initializer->compound.n_initializers;
275 for (i = 0; i < len; ++i) {
279 ir_initializer_t *sub_initializer
280 = initializer->compound.initializers[i];
282 tv = get_initializer_tarval(sub_initializer);
283 if (!tarval_is_constant(tv))
286 mode = get_tarval_mode(tv);
287 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
290 c = get_tarval_long(tv);
291 if (isgraph(c) || isspace(c))
296 if (i == len - 1 && c != '\0')
300 return found_printable;
303 static bool initializer_is_null(const ir_initializer_t *initializer)
305 switch (initializer->kind) {
306 case IR_INITIALIZER_NULL:
308 case IR_INITIALIZER_TARVAL: {
309 tarval *tv = initializer->tarval.value;
310 return tarval_is_null(tv);
312 case IR_INITIALIZER_CONST: {
313 ir_node *value = initializer->consti.value;
314 if (!is_Const(value))
316 return is_Const_null(value);
318 case IR_INITIALIZER_COMPOUND: {
320 for (i = 0; i < initializer->compound.n_initializers; ++i) {
321 ir_initializer_t *subinitializer
322 = initializer->compound.initializers[i];
323 if (!initializer_is_null(subinitializer))
329 panic("invalid initializer in initializer_is_null");
333 * Determine if an entity is a string constant
334 * @param ent The entity
335 * @return 1 if it is a string constant, 0 otherwise
337 static int entity_is_string_const(const ir_entity *ent)
339 ir_type *type, *element_type;
343 type = get_entity_type(ent);
345 /* if it's an array */
346 if (!is_Array_type(type))
349 element_type = get_array_element_type(type);
351 /* and the array's element type is primitive */
352 if (!is_Primitive_type(element_type))
355 /* and the mode of the element type is an int of
356 * the same size as the byte mode */
357 mode = get_type_mode(element_type);
358 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
361 if (ent->initializer != NULL) {
362 return initializer_is_string_const(ent->initializer);
363 } else if (entity_has_compound_ent_values(ent)) {
364 int found_printable = 0;
365 /* if it contains only printable chars and a 0 at the end */
366 n = get_compound_ent_n_values(ent);
367 for (i = 0; i < n; ++i) {
368 ir_node *irn = get_compound_ent_value(ent, i);
372 c = (int) get_tarval_long(get_Const_tarval(irn));
374 if (isgraph(c) || isspace(c))
379 if (i == n - 1 && c != '\0')
382 return found_printable;
388 static bool entity_is_null(const ir_entity *entity)
390 if (entity->initializer != NULL) {
391 return initializer_is_null(entity->initializer);
392 } else if (entity_has_compound_ent_values(entity)) {
393 /* I'm too lazy to implement this case as compound graph paths will be
394 * remove anyway in the future */
397 /* uninitialized, NULL is fine */
401 static bool is_comdat(const ir_entity *entity)
403 ir_linkage linkage = get_entity_linkage(entity);
404 return (linkage & IR_LINKAGE_MERGE)
405 && (linkage & IR_LINKAGE_GARBAGE_COLLECT);
408 static be_gas_section_t determine_basic_section(const ir_entity *entity)
412 if (is_method_entity(entity))
413 return GAS_SECTION_TEXT;
415 linkage = get_entity_linkage(entity);
416 if (linkage & IR_LINKAGE_CONSTANT) {
417 /* mach-o is the only one with a cstring section */
418 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
419 && entity_is_string_const(entity))
420 return GAS_SECTION_CSTRING;
422 return GAS_SECTION_RODATA;
424 if (entity_is_null(entity))
425 return GAS_SECTION_BSS;
427 return GAS_SECTION_DATA;
430 static be_gas_section_t determine_section(be_gas_decl_env_t *env,
431 const ir_entity *entity)
433 ir_type *owner = get_entity_owner(entity);
435 if (owner == get_segment_type(IR_SEGMENT_GLOBAL)) {
436 be_gas_section_t section = determine_basic_section(entity);
437 if (is_comdat(entity))
438 section |= GAS_SECTION_FLAG_COMDAT;
440 } else if (env != NULL && owner == env->main_env->pic_symbols_type) {
441 return GAS_SECTION_PIC_SYMBOLS;
442 } else if (env != NULL && owner == env->main_env->pic_trampolines_type) {
443 return GAS_SECTION_PIC_TRAMPOLINES;
444 } else if (owner == get_segment_type(IR_SEGMENT_CONSTRUCTORS)) {
445 return GAS_SECTION_CONSTRUCTORS;
446 } else if (owner == get_segment_type(IR_SEGMENT_DESTRUCTORS)) {
447 return GAS_SECTION_DESTRUCTORS;
448 } else if (owner == get_segment_type(IR_SEGMENT_THREAD_LOCAL)) {
449 be_gas_section_t section = determine_basic_section(entity);
450 if (is_comdat(entity))
451 section |= GAS_SECTION_FLAG_COMDAT;
453 return section | GAS_SECTION_FLAG_TLS;
456 /* the java frontend keeps some functions inside classes */
457 if (is_Class_type(owner)) {
458 return determine_basic_section(entity);
461 panic("Couldn't determine section for %+F?!?", entity);
464 static void emit_weak(const ir_entity *entity)
466 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
467 be_emit_cstring("\t.weak_reference ");
469 be_emit_cstring("\t.weak ");
471 be_gas_emit_entity(entity);
473 be_emit_write_line();
476 static void emit_visibility(const ir_entity *entity)
478 ir_linkage linkage = get_entity_linkage(entity);
480 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
482 /* Note: .weak seems to imply .globl so no need to output .globl */
483 } else if (get_entity_visibility(entity) == ir_visibility_default) {
484 be_emit_cstring(".globl ");
485 be_gas_emit_entity(entity);
487 be_emit_write_line();
490 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
491 && (linkage & IR_LINKAGE_HIDDEN_USER)) {
492 be_emit_cstring("\t.no_dead_strip ");
493 be_gas_emit_entity(entity);
495 be_emit_write_line();
499 void be_gas_emit_function_prolog(const ir_entity *entity, unsigned po2alignment)
501 be_gas_section_t section = determine_section(NULL, entity);
502 emit_section(section, entity);
504 /* write the begin line (makes the life easier for scripts parsing the
506 be_emit_write_line();
507 be_emit_cstring("# -- Begin ");
508 be_gas_emit_entity(entity);
510 be_emit_write_line();
512 if (po2alignment > 0) {
513 const char *fill_byte = "";
514 unsigned maximum_skip = (1 << po2alignment) - 1;
515 /* gcc fills space between function with 0x90... */
516 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
519 be_emit_cstring("\t.p2align ");
520 be_emit_irprintf("%u,%s,%u\n", po2alignment, fill_byte, maximum_skip);
521 be_emit_write_line();
523 emit_visibility(entity);
525 switch (be_gas_object_file_format) {
526 case OBJECT_FILE_FORMAT_ELF:
527 case OBJECT_FILE_FORMAT_ELF_SPARC:
528 be_emit_cstring("\t.type\t");
529 be_gas_emit_entity(entity);
530 be_emit_cstring(", ");
531 be_emit_char(be_gas_elf_type_char);
532 be_emit_cstring("function\n");
533 be_emit_write_line();
535 case OBJECT_FILE_FORMAT_COFF:
536 be_emit_cstring("\t.def\t");
537 be_gas_emit_entity(entity);
538 be_emit_cstring(";");
539 if (get_entity_visibility(entity) == ir_visibility_local) {
540 be_emit_cstring("\t.scl\t3;");
542 be_emit_cstring("\t.scl\t2;");
544 be_emit_cstring("\t.type\t32;\t.endef\n");
545 be_emit_write_line();
547 case OBJECT_FILE_FORMAT_MACH_O:
550 be_gas_emit_entity(entity);
551 be_emit_cstring(":\n");
552 be_emit_write_line();
555 void be_gas_emit_function_epilog(const ir_entity *entity)
557 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF) {
558 be_emit_cstring("\t.size\t");
559 be_gas_emit_entity(entity);
560 be_emit_cstring(", .-");
561 be_gas_emit_entity(entity);
563 be_emit_write_line();
566 be_emit_cstring("# -- End ");
567 be_gas_emit_entity(entity);
569 be_emit_write_line();
575 * @param tv the tarval
576 * @param bytes the width of the tarvals value in bytes
578 static void emit_arith_tarval(tarval *tv, int bytes)
582 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
586 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
590 be_emit_irprintf("0x%02x%02x%02x%02x",
591 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
595 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
596 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
597 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
601 /* Beware: Mixed endian output! One little endian number emitted as
602 * three longs. Each long initializer is written in big endian. */
604 "\t.long\t0x%02x%02x%02x%02x\n"
605 "\t.long\t0x%02x%02x%02x%02x\n"
606 "\t.long\t0x%02x%02x%02x%02x",
607 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
608 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
609 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
610 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
611 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
612 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8)
617 /* Beware: Mixed endian output! One little endian number emitted as
618 * three longs. Each long initializer is written in big endian. */
620 "\t.long\t0x%02x%02x%02x%02x\n"
621 "\t.long\t0x%02x%02x%02x%02x\n"
622 "\t.long\t0x%02x%02x%02x%02x\n"
623 "\t.long\t0x%02x%02x%02x%02x",
624 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
625 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
626 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
627 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
628 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
629 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
630 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 14),
631 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12)
636 panic("Can't dump a tarval with %d bytes", bytes);
640 * Return the label prefix for labeled instructions.
642 const char *be_gas_insn_label_prefix(void)
648 * Return the tarval of an atomic initializer.
650 * @param init a node representing the initializer (on the const code irg)
654 static tarval *get_atomic_init_tv(ir_node *init)
657 ir_mode *mode = get_irn_mode(init);
659 switch (get_irn_opcode(init)) {
662 init = get_Cast_op(init);
666 init = get_Conv_op(init);
670 return get_Const_tarval(init);
673 switch (get_SymConst_kind(init)) {
674 case symconst_type_size:
675 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
677 case symconst_type_align:
678 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
680 case symconst_ofs_ent:
681 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
683 case symconst_enum_const:
684 return get_enumeration_value(get_SymConst_enum(init));
697 * Dump an atomic value.
699 * @param env the gas output environment
700 * @param init a node representing the atomic value (on the const code irg)
702 static void do_emit_atomic_init(be_gas_decl_env_t *env, ir_node *init)
704 ir_mode *mode = get_irn_mode(init);
705 int bytes = get_mode_size_bytes(mode);
709 init = skip_Id(init);
711 switch (get_irn_opcode(init)) {
713 do_emit_atomic_init(env, get_Cast_op(init));
717 do_emit_atomic_init(env, get_Conv_op(init));
721 tv = get_Const_tarval(init);
723 /* it's a arithmetic value */
724 emit_arith_tarval(tv, bytes);
728 switch (get_SymConst_kind(init)) {
729 case symconst_addr_ent:
730 ent = get_SymConst_entity(init);
731 be_gas_emit_entity(ent);
734 case symconst_ofs_ent:
735 ent = get_SymConst_entity(init);
736 be_emit_irprintf("%d", get_entity_offset(ent));
739 case symconst_type_size:
740 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
743 case symconst_type_align:
744 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
747 case symconst_enum_const:
748 tv = get_enumeration_value(get_SymConst_enum(init));
749 emit_arith_tarval(tv, bytes);
753 assert(!"emit_atomic_init(): don't know how to init from this SymConst");
758 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
759 panic("Constant must be int or pointer for '+' to work");
761 do_emit_atomic_init(env, get_Add_left(init));
762 be_emit_cstring(" + ");
763 do_emit_atomic_init(env, get_Add_right(init));
767 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
768 panic("Constant must be int or pointer for '-' to work");
770 do_emit_atomic_init(env, get_Sub_left(init));
771 be_emit_cstring(" - ");
772 do_emit_atomic_init(env, get_Sub_right(init));
776 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
777 panic("Constant must be int or pointer for '*' to work");
779 do_emit_atomic_init(env, get_Mul_left(init));
780 be_emit_cstring(" * ");
781 do_emit_atomic_init(env, get_Mul_right(init));
785 be_emit_cstring("0");
789 panic("emit_atomic_init(): unsupported IR-node %+F", init);
794 * Dumps the type for given size (.byte, .long, ...)
796 * @param size the size in bytes
798 static void emit_size_type(size_t size)
802 be_emit_cstring("\t.byte\t");
806 be_emit_cstring("\t.short\t");
810 be_emit_cstring("\t.long\t");
814 be_emit_cstring("\t.quad\t");
819 case 16: /* Note: .octa does not work on mac */
820 /* handled in arith */
824 panic("Try to dump a type with %u bytes", (unsigned)size);
829 * Emit an atomic value.
831 * @param env the gas output environment
832 * @param init a node representing the atomic value (on the const code irg)
834 static void emit_atomic_init(be_gas_decl_env_t *env, ir_node *init)
836 ir_mode *mode = get_irn_mode(init);
837 int bytes = get_mode_size_bytes(mode);
839 emit_size_type(bytes);
840 do_emit_atomic_init(env, init);
842 be_emit_write_line();
846 * Dump a string constant.
847 * No checks are made!!
849 * @param ent The entity to dump.
851 static void emit_string_cst(const ir_entity *ent)
859 len = get_compound_ent_n_values(ent);
861 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
862 be_emit_cstring("\t.ascii \"");
864 be_emit_cstring("\t.string \"");
868 for (i = 0; i < output_len; ++i) {
872 irn = get_compound_ent_value(ent, i);
873 c = (int) get_tarval_long(get_Const_tarval(irn));
876 case '"' : be_emit_cstring("\\\""); break;
877 case '\n': be_emit_cstring("\\n"); break;
878 case '\r': be_emit_cstring("\\r"); break;
879 case '\t': be_emit_cstring("\\t"); break;
880 case '\\': be_emit_cstring("\\\\"); break;
885 be_emit_irprintf("\\%o", c);
889 be_emit_cstring("\"\n");
890 be_emit_write_line();
892 type = get_entity_type(ent);
893 type_size = get_type_size_bytes(type);
894 remaining_space = type_size - len;
895 assert(remaining_space >= 0);
896 if (remaining_space > 0) {
897 be_emit_irprintf("\t.space\t%d, 0\n", remaining_space);
901 static void emit_string_initializer(const ir_initializer_t *initializer)
905 len = initializer->compound.n_initializers;
906 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
907 be_emit_cstring("\t.ascii \"");
909 be_emit_cstring("\t.string \"");
913 for (i = 0; i < len; ++i) {
914 const ir_initializer_t *sub_initializer
915 = get_initializer_compound_value(initializer, i);
917 tarval *tv = get_initializer_tarval(sub_initializer);
918 int c = get_tarval_long(tv);
921 case '"' : be_emit_cstring("\\\""); break;
922 case '\n': be_emit_cstring("\\n"); break;
923 case '\r': be_emit_cstring("\\r"); break;
924 case '\t': be_emit_cstring("\\t"); break;
925 case '\\': be_emit_cstring("\\\\"); break;
930 be_emit_irprintf("\\%o", c);
934 be_emit_cstring("\"\n");
935 be_emit_write_line();
938 enum normal_or_bitfield_kind {
945 enum normal_or_bitfield_kind kind;
949 unsigned char bf_val;
951 } normal_or_bitfield;
953 static int is_type_variable_size(ir_type *type)
960 static size_t get_initializer_size(const ir_initializer_t *initializer,
963 switch (get_initializer_kind(initializer)) {
964 case IR_INITIALIZER_TARVAL:
965 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
966 return get_type_size_bytes(type);
967 case IR_INITIALIZER_CONST:
968 case IR_INITIALIZER_NULL:
969 return get_type_size_bytes(type);
970 case IR_INITIALIZER_COMPOUND:
971 if (!is_type_variable_size(type)) {
972 return get_type_size_bytes(type);
975 = get_initializer_compound_n_entries(initializer);
977 unsigned initializer_size = get_type_size_bytes(type);
978 for (i = 0; i < n_entries; ++i) {
979 ir_entity *entity = get_compound_member(type, i);
980 ir_type *type = get_entity_type(entity);
982 const ir_initializer_t *sub_initializer
983 = get_initializer_compound_value(initializer, i);
985 unsigned offset = get_entity_offset(entity);
986 unsigned size = get_initializer_size(sub_initializer, type);
988 if (offset + size > initializer_size) {
989 initializer_size = offset + size;
992 return initializer_size;
996 panic("found invalid initializer");
1000 static normal_or_bitfield *glob_vals;
1001 static size_t max_vals;
1004 static void emit_bitfield(normal_or_bitfield *vals, size_t offset_bits,
1005 const ir_initializer_t *initializer, ir_type *type)
1007 static const size_t BITS_PER_BYTE = 8;
1008 ir_mode *mode = get_type_mode(type);
1013 bool big_endian = be_get_backend_param()->byte_order_big_endian;
1015 switch (get_initializer_kind(initializer)) {
1016 case IR_INITIALIZER_NULL:
1018 case IR_INITIALIZER_TARVAL:
1019 tv = get_initializer_tarval_value(initializer);
1021 case IR_INITIALIZER_CONST: {
1022 ir_node *node = get_initializer_const_value(initializer);
1023 if (!is_Const(node)) {
1024 panic("bitfield initializer not a Const node");
1026 tv = get_Const_tarval(node);
1029 case IR_INITIALIZER_COMPOUND:
1030 panic("bitfield initializer is compound");
1033 panic("Couldn't get numeric value for bitfield initializer");
1035 tv = tarval_convert_to(tv, get_type_mode(type));
1037 value_len = get_type_size_bytes(get_primitive_base_type(type));
1039 end = get_mode_size_bits(mode);
1040 while (bit_offset < end) {
1041 size_t src_offset = bit_offset / BITS_PER_BYTE;
1042 size_t src_offset_bits = bit_offset % BITS_PER_BYTE;
1043 size_t dst_offset = (bit_offset+offset_bits) / BITS_PER_BYTE;
1044 size_t dst_offset_bits = (bit_offset+offset_bits) % BITS_PER_BYTE;
1045 size_t src_bits_len = end-bit_offset;
1046 size_t dst_bits_len = BITS_PER_BYTE-dst_offset_bits;
1047 unsigned char curr_bits;
1048 normal_or_bitfield *val;
1049 if (src_bits_len > dst_bits_len)
1050 src_bits_len = dst_bits_len;
1053 val = &vals[value_len - dst_offset - 1];
1055 val = &vals[dst_offset];
1058 assert((val-glob_vals) < (ptrdiff_t) max_vals);
1059 assert(val->kind == BITFIELD ||
1060 (val->kind == NORMAL && val->v.value == NULL));
1061 val->kind = BITFIELD;
1062 curr_bits = get_tarval_sub_bits(tv, src_offset);
1063 curr_bits = curr_bits >> src_offset_bits;
1064 if (src_offset_bits + src_bits_len > 8) {
1065 unsigned next_bits = get_tarval_sub_bits(tv, src_offset+1);
1066 curr_bits |= next_bits << (8 - src_offset_bits);
1068 curr_bits &= (1 << src_bits_len) - 1;
1069 val->v.bf_val |= curr_bits << dst_offset_bits;
1071 bit_offset += dst_bits_len;
1075 static void emit_ir_initializer(normal_or_bitfield *vals,
1076 const ir_initializer_t *initializer,
1079 assert((size_t) (vals - glob_vals) < max_vals);
1081 switch (get_initializer_kind(initializer)) {
1082 case IR_INITIALIZER_NULL:
1084 case IR_INITIALIZER_TARVAL: {
1087 assert(vals->kind != BITFIELD);
1088 vals->kind = TARVAL;
1089 vals->v.tarval = get_initializer_tarval_value(initializer);
1090 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
1091 for (i = 1; i < get_type_size_bytes(type); ++i) {
1092 vals[i].kind = NORMAL;
1093 vals[i].v.value = NULL;
1097 case IR_INITIALIZER_CONST: {
1100 assert(vals->kind != BITFIELD);
1101 vals->kind = NORMAL;
1102 vals->v.value = get_initializer_const_value(initializer);
1103 for (i = 1; i < get_type_size_bytes(type); ++i) {
1104 vals[i].kind = NORMAL;
1105 vals[i].v.value = NULL;
1109 case IR_INITIALIZER_COMPOUND: {
1111 size_t n = get_initializer_compound_n_entries(initializer);
1113 if (is_Array_type(type)) {
1114 ir_type *element_type = get_array_element_type(type);
1115 size_t skip = get_type_size_bytes(element_type);
1116 size_t alignment = get_type_alignment_bytes(element_type);
1117 size_t misalign = skip % alignment;
1118 if (misalign != 0) {
1119 skip += alignment - misalign;
1122 for (i = 0; i < n; ++i) {
1123 ir_initializer_t *sub_initializer
1124 = get_initializer_compound_value(initializer, i);
1126 emit_ir_initializer(vals, sub_initializer, element_type);
1131 size_t n_members, i;
1132 assert(is_compound_type(type));
1133 n_members = get_compound_n_members(type);
1134 for (i = 0; i < n_members; ++i) {
1135 ir_entity *member = get_compound_member(type, i);
1136 size_t offset = get_entity_offset(member);
1137 ir_type *subtype = get_entity_type(member);
1138 ir_mode *mode = get_type_mode(subtype);
1139 ir_initializer_t *sub_initializer;
1141 assert(i < get_initializer_compound_n_entries(initializer));
1143 = get_initializer_compound_value(initializer, i);
1147 = get_entity_offset_bits_remainder(member);
1148 size_t value_len = get_mode_size_bits(mode);
1150 if (offset_bits != 0 ||
1151 (value_len != 8 && value_len != 16 && value_len != 32
1152 && value_len != 64) ||
1153 (is_Primitive_type(subtype) && get_primitive_base_type(subtype) != NULL)) {
1154 emit_bitfield(&vals[offset], offset_bits,
1155 sub_initializer, subtype);
1160 emit_ir_initializer(&vals[offset], sub_initializer, subtype);
1167 panic("invalid ir_initializer kind found");
1170 static void emit_initializer(be_gas_decl_env_t *env, const ir_entity *entity)
1172 const ir_initializer_t *initializer = entity->initializer;
1174 normal_or_bitfield *vals;
1178 if (initializer_is_string_const(initializer)) {
1179 emit_string_initializer(initializer);
1183 type = get_entity_type(entity);
1184 size = get_initializer_size(initializer, type);
1190 * In the worst case, every initializer allocates one byte.
1191 * Moreover, initializer might be big, do not allocate on stack.
1193 vals = XMALLOCNZ(normal_or_bitfield, size);
1200 emit_ir_initializer(vals, initializer, type);
1202 /* now write values sorted */
1203 for (k = 0; k < size; ) {
1206 if (vals[k].kind == NORMAL) {
1207 if (vals[k].v.value != NULL) {
1208 emit_atomic_init(env, vals[k].v.value);
1209 elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
1213 } else if (vals[k].kind == TARVAL) {
1214 tarval *tv = vals[k].v.tarval;
1215 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
1220 emit_size_type(size);
1221 emit_arith_tarval(tv, size);
1223 be_emit_write_line();
1225 assert(vals[k].kind == BITFIELD);
1226 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1227 be_emit_write_line();
1231 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1238 be_emit_irprintf("\t.space\t%d, 0\n", space);
1239 be_emit_write_line();
1245 static void emit_compound_graph_init(be_gas_decl_env_t *env,
1246 const ir_entity *ent)
1248 normal_or_bitfield *vals;
1250 unsigned k, last_ofs;
1252 if (entity_is_string_const(ent)) {
1253 emit_string_cst(ent);
1257 n = get_compound_ent_n_values(ent);
1259 /* Find the initializer size. Sorrily gcc support a nasty feature:
1260 The last field of a compound may be a flexible array. This allows
1261 initializers bigger than the type size. */
1262 last_ofs = get_type_size_bytes(get_entity_type(ent));
1263 for (i = 0; i < n; ++i) {
1264 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1265 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1266 ir_node *value = get_compound_ent_value(ent, i);
1267 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
1269 offset += (value_len + bits_remainder + 7) >> 3;
1271 if (offset > last_ofs) {
1277 * In the worst case, every initializer allocates one byte.
1278 * Moreover, initializer might be big, do not allocate on stack.
1280 vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1282 /* collect the values and store them at the offsets */
1283 for (i = 0; i < n; ++i) {
1284 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1285 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1286 ir_node *value = get_compound_ent_value(ent, i);
1287 int value_len = get_mode_size_bits(get_irn_mode(value));
1289 assert(offset_bits >= 0);
1291 if (offset_bits != 0 ||
1292 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1293 tarval *tv = get_atomic_init_tv(value);
1294 unsigned char curr_bits, last_bits = 0;
1296 panic("Couldn't get numeric value for bitfield initializer '%s'",
1297 get_entity_ld_name(ent));
1299 /* normalize offset */
1300 offset += offset_bits >> 3;
1303 for (j = 0; value_len + offset_bits > 0; ++j) {
1304 assert(offset + j < last_ofs);
1305 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1306 vals[offset + j].kind = BITFIELD;
1307 curr_bits = get_tarval_sub_bits(tv, j);
1308 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1310 last_bits = curr_bits;
1315 assert(offset < last_ofs);
1316 assert(vals[offset].kind == NORMAL);
1317 for (i = 1; i < value_len / 8; ++i) {
1318 assert(vals[offset + i].v.value == NULL);
1320 vals[offset].v.value = value;
1324 /* now write them sorted */
1325 for (k = 0; k < last_ofs; ) {
1326 int space = 0, skip = 0;
1327 if (vals[k].kind == NORMAL) {
1328 if (vals[k].v.value != NULL) {
1329 emit_atomic_init(env, vals[k].v.value);
1330 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1335 assert(vals[k].kind == BITFIELD);
1336 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1340 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1349 be_emit_irprintf("\t.space\t%d, 0\n", space);
1350 be_emit_write_line();
1356 static void emit_align(unsigned p2alignment)
1358 be_emit_irprintf("\t.p2align\t%u\n", log2_floor(p2alignment));
1359 be_emit_write_line();
1362 static unsigned get_effective_entity_alignment(const ir_entity *entity)
1364 unsigned alignment = get_entity_alignment(entity);
1365 if (alignment == 0) {
1366 ir_type *type = get_entity_type(entity);
1367 alignment = get_type_alignment_bytes(type);
1372 static void emit_common(const ir_entity *entity)
1374 unsigned size = get_type_size_bytes(get_entity_type(entity));
1375 unsigned alignment = get_effective_entity_alignment(entity);
1377 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
1381 switch (be_gas_object_file_format) {
1382 case OBJECT_FILE_FORMAT_MACH_O:
1383 be_emit_cstring("\t.comm ");
1384 be_gas_emit_entity(entity);
1385 be_emit_irprintf(",%u,%u\n", size, log2_floor(alignment));
1386 be_emit_write_line();
1388 case OBJECT_FILE_FORMAT_ELF:
1389 case OBJECT_FILE_FORMAT_ELF_SPARC:
1390 be_emit_cstring("\t.comm ");
1391 be_gas_emit_entity(entity);
1392 be_emit_irprintf(",%u,%u\n", size, alignment);
1393 be_emit_write_line();
1395 case OBJECT_FILE_FORMAT_COFF:
1396 be_emit_cstring("\t.comm ");
1397 be_gas_emit_entity(entity);
1398 be_emit_irprintf(",%u # %u\n", size, alignment);
1399 be_emit_write_line();
1402 panic("invalid object file format");
1405 static void emit_local_common(const ir_entity *entity)
1407 unsigned size = get_type_size_bytes(get_entity_type(entity));
1408 unsigned alignment = get_effective_entity_alignment(entity);
1410 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
1414 switch (be_gas_object_file_format) {
1415 case OBJECT_FILE_FORMAT_MACH_O:
1416 be_emit_cstring("\t.lcomm ");
1417 be_gas_emit_entity(entity);
1418 be_emit_irprintf(",%u,%u\n", size, log2_floor(alignment));
1419 be_emit_write_line();
1421 case OBJECT_FILE_FORMAT_ELF:
1422 case OBJECT_FILE_FORMAT_ELF_SPARC:
1423 be_emit_cstring("\t.local ");
1424 be_gas_emit_entity(entity);
1425 be_emit_cstring("\n");
1426 be_emit_write_line();
1427 be_emit_cstring("\t.comm ");
1428 be_gas_emit_entity(entity);
1429 be_emit_irprintf(",%u,%u\n", size, alignment);
1430 be_emit_write_line();
1432 case OBJECT_FILE_FORMAT_COFF:
1433 be_emit_cstring("\t.lcomm ");
1434 be_gas_emit_entity(entity);
1435 be_emit_irprintf(",%u # %u\n", size, alignment);
1436 be_emit_write_line();
1439 panic("invalid object file format");
1442 static void emit_indirect_symbol(const ir_entity *entity, be_gas_section_t section)
1444 /* we can only do PIC code on macho so far */
1445 assert(be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O);
1447 be_gas_emit_entity(entity);
1448 be_emit_cstring(":\n");
1449 be_emit_write_line();
1450 be_emit_cstring("\t.indirect_symbol ");
1451 be_emit_ident(get_entity_ident(entity));
1453 be_emit_write_line();
1454 if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1455 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1456 be_emit_write_line();
1458 assert(section == GAS_SECTION_PIC_SYMBOLS);
1459 be_emit_cstring("\t.long 0\n");
1460 be_emit_write_line();
1464 char const *be_gas_get_private_prefix(void)
1466 return be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O ? "L" : ".L";
1469 void be_gas_emit_entity(const ir_entity *entity)
1471 if (entity->type == firm_code_type) {
1472 ir_label_t label = get_entity_label(entity);
1473 be_emit_irprintf("%s_%lu", be_gas_get_private_prefix(), label);
1477 if (get_entity_visibility(entity) == ir_visibility_private) {
1478 be_emit_string(be_gas_get_private_prefix());
1480 be_emit_ident(get_entity_ld_ident(entity));
1483 void be_gas_emit_block_name(const ir_node *block)
1485 if (has_Block_entity(block)) {
1486 ir_entity *entity = get_Block_entity(block);
1487 be_gas_emit_entity(entity);
1489 be_emit_irprintf("%s%ld", be_gas_get_private_prefix(), get_irn_node_nr(block));
1494 * Dump a global entity.
1496 * @param env the gas output environment
1497 * @param ent the entity to be dumped
1499 static void emit_global(be_gas_decl_env_t *env, const ir_entity *entity)
1501 ir_type *type = get_entity_type(entity);
1502 ident *ld_ident = get_entity_ld_ident(entity);
1503 unsigned alignment = get_effective_entity_alignment(entity);
1504 be_gas_section_t section = determine_section(env, entity);
1505 ir_visibility visibility = get_entity_visibility(entity);
1506 ir_linkage linkage = get_entity_linkage(entity);
1508 /* block labels are already emittet in the code */
1509 if (type == firm_code_type)
1512 /* we already emitted all methods. Except for the trampolines which
1513 * the assembler/linker generates */
1514 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1515 /* functions with graph are already emitted with
1516 * be_gas_emit_function_prolog */
1517 if (get_entity_irg(entity) == NULL) {
1518 emit_visibility(entity);
1523 be_dbg_variable(entity);
1525 if (section == GAS_SECTION_BSS) {
1526 ir_visibility visibility = get_entity_visibility(entity);
1528 switch (visibility) {
1529 case ir_visibility_local:
1530 case ir_visibility_private:
1531 emit_local_common(entity);
1533 case ir_visibility_default:
1534 if (linkage & IR_LINKAGE_MERGE) {
1535 emit_common(entity);
1539 case ir_visibility_external:
1540 if (linkage & IR_LINKAGE_MERGE)
1541 panic("merge link semantic not supported for extern entities");
1546 emit_visibility(entity);
1547 if (visibility == ir_visibility_external) {
1548 /* nothing to do for externally defined values */
1552 if (!is_po2(alignment))
1553 panic("alignment not a power of 2");
1555 emit_section(section, entity);
1557 if (section == GAS_SECTION_PIC_TRAMPOLINES
1558 || section == GAS_SECTION_PIC_SYMBOLS) {
1559 emit_indirect_symbol(entity, section);
1564 if (alignment > 1) {
1565 emit_align(alignment);
1567 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF
1568 && be_gas_emit_types) {
1569 be_emit_cstring("\t.type\t");
1570 be_gas_emit_entity(entity);
1571 be_emit_cstring(", ");
1572 be_emit_char(be_gas_elf_type_char);
1573 be_emit_cstring("object\n\t.size\t");\
1574 be_gas_emit_entity(entity);
1575 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1578 if (get_id_str(ld_ident)[0] != '\0') {
1579 be_gas_emit_entity(entity);
1580 be_emit_cstring(":\n");
1581 be_emit_write_line();
1584 if (entity_is_null(entity)) {
1585 be_emit_irprintf("\t.space %u, 0\n", get_type_size_bytes(type));
1586 be_emit_write_line();
1587 } else if (entity_has_compound_ent_values(entity)) {
1588 emit_compound_graph_init(env, entity);
1590 assert(entity->initializer != NULL);
1591 emit_initializer(env, entity);
1596 * Dumps declarations of global variables and the initialization code.
1598 * @param gt a global like type, either the global or the TLS one
1599 * @param env an environment
1601 static void be_gas_emit_globals(ir_type *gt, be_gas_decl_env_t *env)
1603 int i, n = get_compound_n_members(gt);
1605 for (i = 0; i < n; i++) {
1606 ir_entity *ent = get_compound_member(gt, i);
1607 emit_global(env, ent);
1611 /* Generate all entities. */
1612 void be_gas_emit_decls(const be_main_env_t *main_env)
1614 be_gas_decl_env_t env;
1615 memset(&env, 0, sizeof(env));
1617 /* dump global type */
1618 env.main_env = main_env;
1619 env.section = (be_gas_section_t) -1;
1621 be_gas_emit_globals(get_glob_type(), &env);
1622 be_gas_emit_globals(get_tls_type(), &env);
1623 be_gas_emit_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env);
1624 be_gas_emit_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env);
1625 be_gas_emit_globals(main_env->pic_symbols_type, &env);
1626 be_gas_emit_globals(main_env->pic_trampolines_type, &env);
1629 * ".subsections_via_symbols marks object files which are OK to divide
1630 * their section contents into individual blocks".
1631 * From my understanding this means no label points in the middle of an
1632 * object which we want to address as a whole. Firm code should be fine
1635 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
1636 be_emit_cstring("\t.subsections_via_symbols\n");
1637 be_emit_write_line();