2 * Copyright (C) 1995-2011 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
45 #include "beemitter.h"
46 #include "be_dbgout.h"
48 /** by default, we generate assembler code for the Linux gas */
49 object_file_format_t be_gas_object_file_format = OBJECT_FILE_FORMAT_ELF;
50 bool be_gas_emit_types = true;
51 char be_gas_elf_type_char = '@';
53 static be_gas_section_t current_section = (be_gas_section_t) -1;
56 * An environment containing all needed dumper data.
57 * Currently we create the file completely in memory first, then
58 * write it to the disk. This is an artifact from the old C-generating backend
59 * and even there NOT needed. So we might change it in the future.
61 typedef struct be_gas_decl_env {
62 be_gas_section_t section;
63 const be_main_env_t *main_env;
66 static void emit_section_macho(be_gas_section_t section)
68 be_gas_section_t base = section & GAS_SECTION_TYPE_MASK;
69 be_gas_section_t flags = section & ~GAS_SECTION_TYPE_MASK;
72 if (current_section == section)
74 current_section = section;
79 case GAS_SECTION_TEXT: name = "text"; break;
80 case GAS_SECTION_DATA: name = "data"; break;
81 case GAS_SECTION_RODATA: name = "const"; break;
82 case GAS_SECTION_BSS: name = "data"; break;
83 case GAS_SECTION_CONSTRUCTORS: name = "mod_init_func"; break;
84 case GAS_SECTION_DESTRUCTORS: name = "mod_term_func"; break;
85 case GAS_SECTION_PIC_TRAMPOLINES: name = "section\t__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5"; break;
86 case GAS_SECTION_PIC_SYMBOLS: name = "section\t__IMPORT,__pointers,non_lazy_symbol_pointers"; break;
87 case GAS_SECTION_CSTRING: name = "cstring"; break;
88 default: panic("unsupported scetion type 0x%X", section);
90 be_emit_irprintf("\t.%s\n", name);
92 } else if (flags & GAS_SECTION_FLAG_COMDAT) {
94 case GAS_SECTION_TEXT: name = "section __TEXT,__textcoal_nt,coalesced,pure_instructions"; break;
96 case GAS_SECTION_DATA: name = "section __DATA,__datacoal_nt,coalesced"; break;
97 case GAS_SECTION_RODATA: name = "section __TEXT,__const_coal,coalesced"; break;
98 case GAS_SECTION_CSTRING: name = "section __TEXT,__const_coal,coalesced"; break;
99 default: panic("unsupported scetion type 0x%X", section);
102 panic("unsupported section type 0x%X\n", section);
106 static void emit_section_sparc(be_gas_section_t section, const ir_entity *entity)
108 be_gas_section_t base = section & GAS_SECTION_TYPE_MASK;
109 be_gas_section_t flags = section & ~GAS_SECTION_TYPE_MASK;
110 static const char *const basename[] = {
111 "text", "data", "rodata", "bss", "ctors", "dtors"
114 if (current_section == section && !(section & GAS_SECTION_FLAG_COMDAT))
116 current_section = section;
118 be_emit_cstring("\t.section\t\".");
120 /* Part1: section-name */
121 if (flags & GAS_SECTION_FLAG_TLS)
123 assert(base < (be_gas_section_t)ARRAY_SIZE(basename));
124 be_emit_string(basename[base]);
126 if (flags & GAS_SECTION_FLAG_COMDAT) {
128 be_gas_emit_entity(entity);
132 /* for the simple sections we're done here */
136 be_emit_cstring(",#alloc");
139 case GAS_SECTION_TEXT: be_emit_cstring(",#execinstr"); break;
140 case GAS_SECTION_DATA:
141 case GAS_SECTION_BSS: be_emit_cstring(",#write"); break;
146 if (flags & GAS_SECTION_FLAG_TLS) {
147 be_emit_cstring(",#tls");
152 be_emit_write_line();
155 static void emit_section(be_gas_section_t section, const ir_entity *entity)
157 be_gas_section_t base = section & GAS_SECTION_TYPE_MASK;
158 be_gas_section_t flags = section & ~GAS_SECTION_TYPE_MASK;
159 static const char *const basename[] = {
160 "text", "data", "rodata", "bss", "ctors", "dtors"
162 static const char *const type[] = {
163 "progbits", "progbits", "progbits", "nobits", "init_array", "fini_array"
166 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
167 emit_section_macho(section);
169 } else if(be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF_SPARC) {
170 emit_section_sparc(section, entity);
174 if (current_section == section && !(section & GAS_SECTION_FLAG_COMDAT))
176 current_section = section;
181 case GAS_SECTION_TEXT:
182 be_emit_cstring("\t.text\n");
183 be_emit_write_line();
185 case GAS_SECTION_DATA:
186 be_emit_cstring("\t.data\n");
187 be_emit_write_line();
189 case GAS_SECTION_RODATA:
190 be_emit_cstring("\t.section\t.rodata\n");
191 be_emit_write_line();
193 case GAS_SECTION_BSS:
194 be_emit_cstring("\t.bss\n");
195 be_emit_write_line();
202 assert(base < (be_gas_section_t) ARRAY_SIZE(basename));
203 be_emit_cstring("\t.section\t.");
205 if (flags & GAS_SECTION_FLAG_TLS)
207 be_emit_string(basename[base]);
208 if (flags & GAS_SECTION_FLAG_COMDAT) {
210 be_gas_emit_entity(entity);
214 be_emit_cstring(",\"");
215 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_COFF)
217 if (base == GAS_SECTION_TEXT)
219 if (base != GAS_SECTION_RODATA && base != GAS_SECTION_TEXT)
221 if (flags & GAS_SECTION_FLAG_TLS)
223 if (flags & GAS_SECTION_FLAG_COMDAT)
226 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_COFF) {
227 be_emit_cstring("\",");
228 be_emit_char(be_gas_elf_type_char);
229 be_emit_string(type[base]);
232 if (flags & GAS_SECTION_FLAG_COMDAT) {
234 be_gas_emit_entity(entity);
235 be_emit_cstring(",comdat");
238 be_emit_write_line();
243 void be_gas_emit_switch_section(be_gas_section_t section)
245 /* you have to produce a switch_section call with entity manually
246 * for comdat sections */
247 assert( !(section & GAS_SECTION_FLAG_COMDAT));
249 emit_section(section, NULL);
252 static ir_tarval *get_initializer_tarval(const ir_initializer_t *initializer)
254 if (initializer->kind == IR_INITIALIZER_TARVAL)
255 return initializer->tarval.value;
256 if (initializer->kind == IR_INITIALIZER_CONST) {
257 ir_node *node = initializer->consti.value;
258 if (is_Const(node)) {
259 return get_Const_tarval(node);
262 return get_tarval_undefined();
265 static bool initializer_is_string_const(const ir_initializer_t *initializer)
268 bool found_printable = false;
270 if (initializer->kind != IR_INITIALIZER_COMPOUND)
273 len = initializer->compound.n_initializers;
276 for (i = 0; i < len; ++i) {
280 ir_initializer_t *sub_initializer
281 = initializer->compound.initializers[i];
283 tv = get_initializer_tarval(sub_initializer);
284 if (!tarval_is_constant(tv))
287 mode = get_tarval_mode(tv);
288 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
291 c = get_tarval_long(tv);
292 if (isgraph(c) || isspace(c))
293 found_printable = true;
297 if (i == len - 1 && c != '\0')
301 return found_printable;
304 static bool initializer_is_null(const ir_initializer_t *initializer)
306 switch (initializer->kind) {
307 case IR_INITIALIZER_NULL:
309 case IR_INITIALIZER_TARVAL: {
310 ir_tarval *tv = initializer->tarval.value;
311 return tarval_is_null(tv);
313 case IR_INITIALIZER_CONST: {
314 ir_node *value = initializer->consti.value;
315 if (!is_Const(value))
317 return is_Const_null(value);
319 case IR_INITIALIZER_COMPOUND: {
321 for (i = 0; i < initializer->compound.n_initializers; ++i) {
322 ir_initializer_t *subinitializer
323 = initializer->compound.initializers[i];
324 if (!initializer_is_null(subinitializer))
330 panic("invalid initializer in initializer_is_null");
334 * Determine if an entity is a string constant
335 * @param ent The entity
336 * @return 1 if it is a string constant, 0 otherwise
338 static int entity_is_string_const(const ir_entity *ent)
340 ir_type *type, *element_type;
344 type = get_entity_type(ent);
346 /* if it's an array */
347 if (!is_Array_type(type))
350 element_type = get_array_element_type(type);
352 /* and the array's element type is primitive */
353 if (!is_Primitive_type(element_type))
356 /* and the mode of the element type is an int of
357 * the same size as the byte mode */
358 mode = get_type_mode(element_type);
359 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
362 if (ent->initializer != NULL) {
363 return initializer_is_string_const(ent->initializer);
364 } else if (entity_has_compound_ent_values(ent)) {
365 int found_printable = 0;
366 /* if it contains only printable chars and a 0 at the end */
367 n = get_compound_ent_n_values(ent);
368 for (i = 0; i < n; ++i) {
369 ir_node *irn = get_compound_ent_value(ent, i);
373 c = (int) get_tarval_long(get_Const_tarval(irn));
375 if (isgraph(c) || isspace(c))
380 if (i == n - 1 && c != '\0')
383 return found_printable;
389 static bool entity_is_null(const ir_entity *entity)
391 if (entity->initializer != NULL) {
392 return initializer_is_null(entity->initializer);
393 } else if (entity_has_compound_ent_values(entity)) {
394 /* I'm too lazy to implement this case as compound graph paths will be
395 * remove anyway in the future */
398 /* uninitialized, NULL is fine */
402 static bool is_comdat(const ir_entity *entity)
404 ir_linkage linkage = get_entity_linkage(entity);
405 return (linkage & IR_LINKAGE_MERGE)
406 && (linkage & IR_LINKAGE_GARBAGE_COLLECT);
409 static be_gas_section_t determine_basic_section(const ir_entity *entity)
413 if (is_method_entity(entity))
414 return GAS_SECTION_TEXT;
416 linkage = get_entity_linkage(entity);
417 if (linkage & IR_LINKAGE_CONSTANT) {
418 /* mach-o is the only one with a cstring section */
419 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
420 && entity_is_string_const(entity))
421 return GAS_SECTION_CSTRING;
423 return GAS_SECTION_RODATA;
425 if (entity_is_null(entity))
426 return GAS_SECTION_BSS;
428 return GAS_SECTION_DATA;
431 static be_gas_section_t determine_section(be_gas_decl_env_t *env,
432 const ir_entity *entity)
434 ir_type *owner = get_entity_owner(entity);
436 if (owner == get_segment_type(IR_SEGMENT_GLOBAL)) {
437 be_gas_section_t section = determine_basic_section(entity);
438 if (is_comdat(entity))
439 section |= GAS_SECTION_FLAG_COMDAT;
441 } else if (env != NULL && owner == env->main_env->pic_symbols_type) {
442 return GAS_SECTION_PIC_SYMBOLS;
443 } else if (env != NULL && owner == env->main_env->pic_trampolines_type) {
444 return GAS_SECTION_PIC_TRAMPOLINES;
445 } else if (owner == get_segment_type(IR_SEGMENT_CONSTRUCTORS)) {
446 return GAS_SECTION_CONSTRUCTORS;
447 } else if (owner == get_segment_type(IR_SEGMENT_DESTRUCTORS)) {
448 return GAS_SECTION_DESTRUCTORS;
449 } else if (owner == get_segment_type(IR_SEGMENT_THREAD_LOCAL)) {
450 be_gas_section_t section = determine_basic_section(entity);
451 if (is_comdat(entity))
452 section |= GAS_SECTION_FLAG_COMDAT;
454 return section | GAS_SECTION_FLAG_TLS;
457 /* the java frontend keeps some functions inside classes */
458 if (is_Class_type(owner)) {
459 return determine_basic_section(entity);
462 panic("Couldn't determine section for %+F?!?", entity);
465 static void emit_weak(const ir_entity *entity)
467 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
468 be_emit_cstring("\t.weak_reference ");
470 be_emit_cstring("\t.weak ");
472 be_gas_emit_entity(entity);
474 be_emit_write_line();
477 static void emit_visibility(const ir_entity *entity)
479 ir_linkage linkage = get_entity_linkage(entity);
481 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
483 /* Note: .weak seems to imply .globl so no need to output .globl */
484 } else if (get_entity_visibility(entity) == ir_visibility_default) {
485 be_emit_cstring(".globl ");
486 be_gas_emit_entity(entity);
488 be_emit_write_line();
491 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
492 && (linkage & IR_LINKAGE_HIDDEN_USER)
493 && get_entity_ld_name(entity)[0] != '\0') {
494 be_emit_cstring("\t.no_dead_strip ");
495 be_gas_emit_entity(entity);
497 be_emit_write_line();
501 void be_gas_emit_function_prolog(const ir_entity *entity, unsigned po2alignment)
503 be_gas_section_t section = determine_section(NULL, entity);
504 emit_section(section, entity);
506 /* write the begin line (makes the life easier for scripts parsing the
508 be_emit_write_line();
509 be_emit_cstring("# -- Begin ");
510 be_gas_emit_entity(entity);
512 be_emit_write_line();
514 if (po2alignment > 0) {
515 const char *fill_byte = "";
516 unsigned maximum_skip = (1 << po2alignment) - 1;
517 /* gcc fills space between function with 0x90... */
518 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
521 be_emit_cstring("\t.p2align ");
522 be_emit_irprintf("%u,%s,%u\n", po2alignment, fill_byte, maximum_skip);
523 be_emit_write_line();
525 emit_visibility(entity);
527 switch (be_gas_object_file_format) {
528 case OBJECT_FILE_FORMAT_ELF:
529 case OBJECT_FILE_FORMAT_ELF_SPARC:
530 be_emit_cstring("\t.type\t");
531 be_gas_emit_entity(entity);
532 be_emit_cstring(", ");
533 be_emit_char(be_gas_elf_type_char);
534 be_emit_cstring("function\n");
535 be_emit_write_line();
537 case OBJECT_FILE_FORMAT_COFF:
538 be_emit_cstring("\t.def\t");
539 be_gas_emit_entity(entity);
540 be_emit_cstring(";");
541 if (get_entity_visibility(entity) == ir_visibility_local) {
542 be_emit_cstring("\t.scl\t3;");
544 be_emit_cstring("\t.scl\t2;");
546 be_emit_cstring("\t.type\t32;\t.endef\n");
547 be_emit_write_line();
549 case OBJECT_FILE_FORMAT_MACH_O:
552 be_gas_emit_entity(entity);
553 be_emit_cstring(":\n");
554 be_emit_write_line();
557 void be_gas_emit_function_epilog(const ir_entity *entity)
559 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF) {
560 be_emit_cstring("\t.size\t");
561 be_gas_emit_entity(entity);
562 be_emit_cstring(", .-");
563 be_gas_emit_entity(entity);
565 be_emit_write_line();
568 be_emit_cstring("# -- End ");
569 be_gas_emit_entity(entity);
571 be_emit_write_line();
577 * @param tv the tarval
578 * @param bytes the width of the tarvals value in bytes
580 static void emit_arith_tarval(ir_tarval *tv, int bytes)
584 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
588 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
592 be_emit_irprintf("0x%02x%02x%02x%02x",
593 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
597 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
598 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
599 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
603 /* Beware: Mixed endian output! One little endian number emitted as
604 * three longs. Each long initializer is written in big endian. */
606 "\t.long\t0x%02x%02x%02x%02x\n"
607 "\t.long\t0x%02x%02x%02x%02x\n"
608 "\t.long\t0x%02x%02x%02x%02x",
609 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
610 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
611 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
612 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
613 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
614 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8)
619 /* Beware: Mixed endian output! One little endian number emitted as
620 * three longs. Each long initializer is written in big endian. */
622 "\t.long\t0x%02x%02x%02x%02x\n"
623 "\t.long\t0x%02x%02x%02x%02x\n"
624 "\t.long\t0x%02x%02x%02x%02x\n"
625 "\t.long\t0x%02x%02x%02x%02x",
626 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
627 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
628 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
629 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
630 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
631 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
632 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 14),
633 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12)
638 panic("Can't dump a tarval with %d bytes", bytes);
642 * Return the label prefix for labeled instructions.
644 const char *be_gas_insn_label_prefix(void)
650 * Return the tarval of an atomic initializer.
652 * @param init a node representing the initializer (on the const code irg)
656 static ir_tarval *get_atomic_init_tv(ir_node *init)
659 ir_mode *mode = get_irn_mode(init);
661 switch (get_irn_opcode(init)) {
664 init = get_Cast_op(init);
668 init = get_Conv_op(init);
672 return get_Const_tarval(init);
675 switch (get_SymConst_kind(init)) {
676 case symconst_type_size:
677 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
679 case symconst_type_align:
680 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
682 case symconst_ofs_ent:
683 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
685 case symconst_enum_const:
686 return get_enumeration_value(get_SymConst_enum(init));
699 * Dump an atomic value.
701 * @param env the gas output environment
702 * @param init a node representing the atomic value (on the const code irg)
704 static void do_emit_atomic_init(be_gas_decl_env_t *env, ir_node *init)
706 ir_mode *mode = get_irn_mode(init);
707 int bytes = get_mode_size_bytes(mode);
711 init = skip_Id(init);
713 switch (get_irn_opcode(init)) {
715 do_emit_atomic_init(env, get_Cast_op(init));
719 do_emit_atomic_init(env, get_Conv_op(init));
723 tv = get_Const_tarval(init);
725 /* it's an arithmetic value */
726 emit_arith_tarval(tv, bytes);
730 switch (get_SymConst_kind(init)) {
731 case symconst_addr_ent:
732 ent = get_SymConst_entity(init);
733 be_gas_emit_entity(ent);
736 case symconst_ofs_ent:
737 ent = get_SymConst_entity(init);
738 be_emit_irprintf("%d", get_entity_offset(ent));
741 case symconst_type_size:
742 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
745 case symconst_type_align:
746 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
749 case symconst_enum_const:
750 tv = get_enumeration_value(get_SymConst_enum(init));
751 emit_arith_tarval(tv, bytes);
755 assert(!"emit_atomic_init(): don't know how to init from this SymConst");
760 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
761 panic("Constant must be int or pointer for '+' to work");
763 do_emit_atomic_init(env, get_Add_left(init));
764 be_emit_cstring(" + ");
765 do_emit_atomic_init(env, get_Add_right(init));
769 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
770 panic("Constant must be int or pointer for '-' to work");
772 do_emit_atomic_init(env, get_Sub_left(init));
773 be_emit_cstring(" - ");
774 do_emit_atomic_init(env, get_Sub_right(init));
778 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
779 panic("Constant must be int or pointer for '*' to work");
781 do_emit_atomic_init(env, get_Mul_left(init));
782 be_emit_cstring(" * ");
783 do_emit_atomic_init(env, get_Mul_right(init));
787 be_emit_cstring("0");
791 panic("emit_atomic_init(): unsupported IR-node %+F", init);
796 * Dumps the type for given size (.byte, .long, ...)
798 * @param size the size in bytes
800 static void emit_size_type(size_t size)
804 be_emit_cstring("\t.byte\t");
808 be_emit_cstring("\t.short\t");
812 be_emit_cstring("\t.long\t");
816 be_emit_cstring("\t.quad\t");
821 case 16: /* Note: .octa does not work on mac */
822 /* handled in arith */
826 panic("Try to dump a type with %u bytes", (unsigned)size);
831 * Emit an atomic value.
833 * @param env the gas output environment
834 * @param init a node representing the atomic value (on the const code irg)
836 static void emit_atomic_init(be_gas_decl_env_t *env, ir_node *init)
838 ir_mode *mode = get_irn_mode(init);
839 int bytes = get_mode_size_bytes(mode);
841 emit_size_type(bytes);
842 do_emit_atomic_init(env, init);
844 be_emit_write_line();
848 * Dump a string constant.
849 * No checks are made!!
851 * @param ent The entity to dump.
853 static void emit_string_cst(const ir_entity *ent)
861 len = get_compound_ent_n_values(ent);
863 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
864 be_emit_cstring("\t.ascii \"");
866 be_emit_cstring("\t.string \"");
870 for (i = 0; i < output_len; ++i) {
874 irn = get_compound_ent_value(ent, i);
875 c = (int) get_tarval_long(get_Const_tarval(irn));
878 case '"' : be_emit_cstring("\\\""); break;
879 case '\n': be_emit_cstring("\\n"); break;
880 case '\r': be_emit_cstring("\\r"); break;
881 case '\t': be_emit_cstring("\\t"); break;
882 case '\\': be_emit_cstring("\\\\"); break;
887 be_emit_irprintf("\\%o", c);
891 be_emit_cstring("\"\n");
892 be_emit_write_line();
894 type = get_entity_type(ent);
895 type_size = get_type_size_bytes(type);
896 remaining_space = type_size - len;
897 assert(remaining_space >= 0);
898 if (remaining_space > 0) {
899 be_emit_irprintf("\t.space\t%d, 0\n", remaining_space);
903 static size_t emit_string_initializer(const ir_initializer_t *initializer)
907 len = initializer->compound.n_initializers;
908 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
909 be_emit_cstring("\t.ascii \"");
911 be_emit_cstring("\t.string \"");
915 for (i = 0; i < len; ++i) {
916 const ir_initializer_t *sub_initializer
917 = get_initializer_compound_value(initializer, i);
919 ir_tarval *tv = get_initializer_tarval(sub_initializer);
920 int c = get_tarval_long(tv);
923 case '"' : be_emit_cstring("\\\""); break;
924 case '\n': be_emit_cstring("\\n"); break;
925 case '\r': be_emit_cstring("\\r"); break;
926 case '\t': be_emit_cstring("\\t"); break;
927 case '\\': be_emit_cstring("\\\\"); break;
932 be_emit_irprintf("\\%o", c);
936 be_emit_cstring("\"\n");
937 be_emit_write_line();
939 return initializer->compound.n_initializers;
942 typedef enum normal_or_bitfield_kind {
947 } normal_or_bitfield_kind;
950 normal_or_bitfield_kind kind;
954 unsigned char bf_val;
955 const ir_initializer_t *string;
957 } normal_or_bitfield;
959 static int is_type_variable_size(ir_type *type)
966 static size_t get_initializer_size(const ir_initializer_t *initializer,
969 switch (get_initializer_kind(initializer)) {
970 case IR_INITIALIZER_TARVAL:
971 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
972 return get_type_size_bytes(type);
973 case IR_INITIALIZER_CONST:
974 case IR_INITIALIZER_NULL:
975 return get_type_size_bytes(type);
976 case IR_INITIALIZER_COMPOUND:
977 if (!is_type_variable_size(type)) {
978 return get_type_size_bytes(type);
981 = get_initializer_compound_n_entries(initializer);
983 unsigned initializer_size = get_type_size_bytes(type);
984 for (i = 0; i < n_entries; ++i) {
985 ir_entity *entity = get_compound_member(type, i);
986 ir_type *type = get_entity_type(entity);
988 const ir_initializer_t *sub_initializer
989 = get_initializer_compound_value(initializer, i);
991 unsigned offset = get_entity_offset(entity);
992 unsigned size = get_initializer_size(sub_initializer, type);
994 if (offset + size > initializer_size) {
995 initializer_size = offset + size;
998 return initializer_size;
1002 panic("found invalid initializer");
1006 static normal_or_bitfield *glob_vals;
1007 static size_t max_vals;
1010 static void emit_bitfield(normal_or_bitfield *vals, size_t offset_bits,
1011 const ir_initializer_t *initializer, ir_type *type)
1013 static const size_t BITS_PER_BYTE = 8;
1014 ir_mode *mode = get_type_mode(type);
1015 ir_tarval *tv = NULL;
1019 bool big_endian = be_get_backend_param()->byte_order_big_endian;
1021 switch (get_initializer_kind(initializer)) {
1022 case IR_INITIALIZER_NULL:
1024 case IR_INITIALIZER_TARVAL:
1025 tv = get_initializer_tarval_value(initializer);
1027 case IR_INITIALIZER_CONST: {
1028 ir_node *node = get_initializer_const_value(initializer);
1029 if (!is_Const(node)) {
1030 panic("bitfield initializer not a Const node");
1032 tv = get_Const_tarval(node);
1035 case IR_INITIALIZER_COMPOUND:
1036 panic("bitfield initializer is compound");
1039 panic("Couldn't get numeric value for bitfield initializer");
1041 tv = tarval_convert_to(tv, get_type_mode(type));
1043 value_len = get_type_size_bytes(get_primitive_base_type(type));
1045 end = get_mode_size_bits(mode);
1046 while (bit_offset < end) {
1047 size_t src_offset = bit_offset / BITS_PER_BYTE;
1048 size_t src_offset_bits = bit_offset % BITS_PER_BYTE;
1049 size_t dst_offset = (bit_offset+offset_bits) / BITS_PER_BYTE;
1050 size_t dst_offset_bits = (bit_offset+offset_bits) % BITS_PER_BYTE;
1051 size_t src_bits_len = end-bit_offset;
1052 size_t dst_bits_len = BITS_PER_BYTE-dst_offset_bits;
1053 unsigned char curr_bits;
1054 normal_or_bitfield *val;
1055 if (src_bits_len > dst_bits_len)
1056 src_bits_len = dst_bits_len;
1059 val = &vals[value_len - dst_offset - 1];
1061 val = &vals[dst_offset];
1064 assert((val-glob_vals) < (ptrdiff_t) max_vals);
1065 assert(val->kind == BITFIELD ||
1066 (val->kind == NORMAL && val->v.value == NULL));
1067 val->kind = BITFIELD;
1068 curr_bits = get_tarval_sub_bits(tv, src_offset);
1069 curr_bits = curr_bits >> src_offset_bits;
1070 if (src_offset_bits + src_bits_len > 8) {
1071 unsigned next_bits = get_tarval_sub_bits(tv, src_offset+1);
1072 curr_bits |= next_bits << (8 - src_offset_bits);
1074 curr_bits &= (1 << src_bits_len) - 1;
1075 val->v.bf_val |= curr_bits << dst_offset_bits;
1077 bit_offset += dst_bits_len;
1081 static void emit_ir_initializer(normal_or_bitfield *vals,
1082 const ir_initializer_t *initializer,
1085 assert((size_t) (vals - glob_vals) < max_vals);
1087 if (initializer_is_string_const(initializer)) {
1088 assert(vals->kind != BITFIELD);
1089 vals->kind = STRING;
1090 vals->v.string = initializer;
1094 switch (get_initializer_kind(initializer)) {
1095 case IR_INITIALIZER_NULL:
1097 case IR_INITIALIZER_TARVAL: {
1100 assert(vals->kind != BITFIELD);
1101 vals->kind = TARVAL;
1102 vals->v.tarval = get_initializer_tarval_value(initializer);
1103 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
1104 for (i = 1; i < get_type_size_bytes(type); ++i) {
1105 vals[i].kind = NORMAL;
1106 vals[i].v.value = NULL;
1110 case IR_INITIALIZER_CONST: {
1113 assert(vals->kind != BITFIELD);
1114 vals->kind = NORMAL;
1115 vals->v.value = get_initializer_const_value(initializer);
1116 for (i = 1; i < get_type_size_bytes(type); ++i) {
1117 vals[i].kind = NORMAL;
1118 vals[i].v.value = NULL;
1122 case IR_INITIALIZER_COMPOUND: {
1124 size_t n = get_initializer_compound_n_entries(initializer);
1126 if (is_Array_type(type)) {
1127 ir_type *element_type = get_array_element_type(type);
1128 size_t skip = get_type_size_bytes(element_type);
1129 size_t alignment = get_type_alignment_bytes(element_type);
1130 size_t misalign = skip % alignment;
1131 if (misalign != 0) {
1132 skip += alignment - misalign;
1135 for (i = 0; i < n; ++i) {
1136 ir_initializer_t *sub_initializer
1137 = get_initializer_compound_value(initializer, i);
1139 emit_ir_initializer(vals, sub_initializer, element_type);
1144 size_t n_members, i;
1145 assert(is_compound_type(type));
1146 n_members = get_compound_n_members(type);
1147 for (i = 0; i < n_members; ++i) {
1148 ir_entity *member = get_compound_member(type, i);
1149 size_t offset = get_entity_offset(member);
1150 ir_type *subtype = get_entity_type(member);
1151 ir_mode *mode = get_type_mode(subtype);
1152 ir_initializer_t *sub_initializer;
1154 assert(i < get_initializer_compound_n_entries(initializer));
1156 = get_initializer_compound_value(initializer, i);
1160 = get_entity_offset_bits_remainder(member);
1161 size_t value_len = get_mode_size_bits(mode);
1163 if (offset_bits != 0 ||
1164 (value_len != 8 && value_len != 16 && value_len != 32
1165 && value_len != 64) ||
1166 (is_Primitive_type(subtype) && get_primitive_base_type(subtype) != NULL)) {
1167 emit_bitfield(&vals[offset], offset_bits,
1168 sub_initializer, subtype);
1173 emit_ir_initializer(&vals[offset], sub_initializer, subtype);
1180 panic("invalid ir_initializer kind found");
1183 static void emit_initializer(be_gas_decl_env_t *env, const ir_entity *entity)
1185 const ir_initializer_t *initializer = entity->initializer;
1187 normal_or_bitfield *vals;
1191 if (initializer_is_string_const(initializer)) {
1192 emit_string_initializer(initializer);
1196 type = get_entity_type(entity);
1197 size = get_initializer_size(initializer, type);
1203 * In the worst case, every initializer allocates one byte.
1204 * Moreover, initializer might be big, do not allocate on stack.
1206 vals = XMALLOCNZ(normal_or_bitfield, size);
1213 emit_ir_initializer(vals, initializer, type);
1215 /* now write values sorted */
1216 for (k = 0; k < size; ) {
1219 normal_or_bitfield_kind kind = vals[k].kind;
1222 if (vals[k].v.value != NULL) {
1223 emit_atomic_init(env, vals[k].v.value);
1224 elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
1230 ir_tarval *tv = vals[k].v.tarval;
1231 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
1236 emit_size_type(size);
1237 emit_arith_tarval(tv, size);
1239 be_emit_write_line();
1243 elem_size = emit_string_initializer(vals[k].v.string);
1246 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1247 be_emit_write_line();
1252 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1259 be_emit_irprintf("\t.space\t%d, 0\n", space);
1260 be_emit_write_line();
1266 static void emit_compound_graph_init(be_gas_decl_env_t *env,
1267 const ir_entity *ent)
1269 normal_or_bitfield *vals;
1271 unsigned k, last_ofs;
1273 if (entity_is_string_const(ent)) {
1274 emit_string_cst(ent);
1278 n = get_compound_ent_n_values(ent);
1280 /* Find the initializer size. Sorrily gcc support a nasty feature:
1281 The last field of a compound may be a flexible array. This allows
1282 initializers bigger than the type size. */
1283 last_ofs = get_type_size_bytes(get_entity_type(ent));
1284 for (i = 0; i < n; ++i) {
1285 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1286 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1287 ir_node *value = get_compound_ent_value(ent, i);
1288 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
1290 offset += (value_len + bits_remainder + 7) >> 3;
1292 if (offset > last_ofs) {
1298 * In the worst case, every initializer allocates one byte.
1299 * Moreover, initializer might be big, do not allocate on stack.
1301 vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1303 /* collect the values and store them at the offsets */
1304 for (i = 0; i < n; ++i) {
1305 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1306 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1307 ir_node *value = get_compound_ent_value(ent, i);
1308 int value_len = get_mode_size_bits(get_irn_mode(value));
1310 assert(offset_bits >= 0);
1312 if (offset_bits != 0 ||
1313 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1314 ir_tarval *tv = get_atomic_init_tv(value);
1315 unsigned char curr_bits, last_bits = 0;
1317 panic("Couldn't get numeric value for bitfield initializer '%s'",
1318 get_entity_ld_name(ent));
1320 /* normalize offset */
1321 offset += offset_bits >> 3;
1324 for (j = 0; value_len + offset_bits > 0; ++j) {
1325 assert(offset + j < last_ofs);
1326 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1327 vals[offset + j].kind = BITFIELD;
1328 curr_bits = get_tarval_sub_bits(tv, j);
1329 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1331 last_bits = curr_bits;
1336 assert(offset < last_ofs);
1337 assert(vals[offset].kind == NORMAL);
1338 for (i = 1; i < value_len / 8; ++i) {
1339 assert(vals[offset + i].v.value == NULL);
1341 vals[offset].v.value = value;
1345 /* now write them sorted */
1346 for (k = 0; k < last_ofs; ) {
1347 int space = 0, skip = 0;
1348 if (vals[k].kind == NORMAL) {
1349 if (vals[k].v.value != NULL) {
1350 emit_atomic_init(env, vals[k].v.value);
1351 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1356 assert(vals[k].kind == BITFIELD);
1357 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1361 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1370 be_emit_irprintf("\t.space\t%d, 0\n", space);
1371 be_emit_write_line();
1377 static void emit_align(unsigned p2alignment)
1379 be_emit_irprintf("\t.p2align\t%u\n", log2_floor(p2alignment));
1380 be_emit_write_line();
1383 static unsigned get_effective_entity_alignment(const ir_entity *entity)
1385 unsigned alignment = get_entity_alignment(entity);
1386 if (alignment == 0) {
1387 ir_type *type = get_entity_type(entity);
1388 alignment = get_type_alignment_bytes(type);
1393 static void emit_common(const ir_entity *entity)
1395 unsigned size = get_type_size_bytes(get_entity_type(entity));
1396 unsigned alignment = get_effective_entity_alignment(entity);
1398 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
1402 switch (be_gas_object_file_format) {
1403 case OBJECT_FILE_FORMAT_MACH_O:
1404 be_emit_cstring("\t.comm ");
1405 be_gas_emit_entity(entity);
1406 be_emit_irprintf(",%u,%u\n", size, log2_floor(alignment));
1407 be_emit_write_line();
1409 case OBJECT_FILE_FORMAT_ELF:
1410 case OBJECT_FILE_FORMAT_ELF_SPARC:
1411 be_emit_cstring("\t.comm ");
1412 be_gas_emit_entity(entity);
1413 be_emit_irprintf(",%u,%u\n", size, alignment);
1414 be_emit_write_line();
1416 case OBJECT_FILE_FORMAT_COFF:
1417 be_emit_cstring("\t.comm ");
1418 be_gas_emit_entity(entity);
1419 be_emit_irprintf(",%u # %u\n", size, alignment);
1420 be_emit_write_line();
1423 panic("invalid object file format");
1426 static void emit_local_common(const ir_entity *entity)
1428 unsigned size = get_type_size_bytes(get_entity_type(entity));
1429 unsigned alignment = get_effective_entity_alignment(entity);
1431 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
1435 switch (be_gas_object_file_format) {
1436 case OBJECT_FILE_FORMAT_MACH_O:
1437 be_emit_cstring("\t.lcomm ");
1438 be_gas_emit_entity(entity);
1439 be_emit_irprintf(",%u,%u\n", size, log2_floor(alignment));
1440 be_emit_write_line();
1442 case OBJECT_FILE_FORMAT_ELF:
1443 case OBJECT_FILE_FORMAT_ELF_SPARC:
1444 be_emit_cstring("\t.local ");
1445 be_gas_emit_entity(entity);
1446 be_emit_cstring("\n");
1447 be_emit_write_line();
1448 be_emit_cstring("\t.comm ");
1449 be_gas_emit_entity(entity);
1450 be_emit_irprintf(",%u,%u\n", size, alignment);
1451 be_emit_write_line();
1453 case OBJECT_FILE_FORMAT_COFF:
1454 be_emit_cstring("\t.lcomm ");
1455 be_gas_emit_entity(entity);
1456 be_emit_irprintf(",%u # %u\n", size, alignment);
1457 be_emit_write_line();
1460 panic("invalid object file format");
1463 static void emit_indirect_symbol(const ir_entity *entity, be_gas_section_t section)
1465 /* we can only do PIC code on macho so far */
1466 assert(be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O);
1468 be_gas_emit_entity(entity);
1469 be_emit_cstring(":\n");
1470 be_emit_write_line();
1471 be_emit_cstring("\t.indirect_symbol ");
1472 be_emit_ident(get_entity_ident(entity));
1474 be_emit_write_line();
1475 if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1476 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1477 be_emit_write_line();
1479 assert(section == GAS_SECTION_PIC_SYMBOLS);
1480 be_emit_cstring("\t.long 0\n");
1481 be_emit_write_line();
1485 char const *be_gas_get_private_prefix(void)
1487 return be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O ? "L" : ".L";
1490 void be_gas_emit_entity(const ir_entity *entity)
1492 if (entity->type == firm_code_type) {
1493 ir_label_t label = get_entity_label(entity);
1494 be_emit_irprintf("%s_%lu", be_gas_get_private_prefix(), label);
1498 if (get_entity_visibility(entity) == ir_visibility_private) {
1499 be_emit_string(be_gas_get_private_prefix());
1501 be_emit_ident(get_entity_ld_ident(entity));
1504 void be_gas_emit_block_name(const ir_node *block)
1506 if (has_Block_entity(block)) {
1507 ir_entity *entity = get_Block_entity(block);
1508 be_gas_emit_entity(entity);
1510 be_emit_irprintf("%s%ld", be_gas_get_private_prefix(), get_irn_node_nr(block));
1515 * Dump a global entity.
1517 * @param env the gas output environment
1518 * @param ent the entity to be dumped
1520 static void emit_global(be_gas_decl_env_t *env, const ir_entity *entity)
1522 ir_type *type = get_entity_type(entity);
1523 ident *ld_ident = get_entity_ld_ident(entity);
1524 unsigned alignment = get_effective_entity_alignment(entity);
1525 be_gas_section_t section = determine_section(env, entity);
1526 ir_visibility visibility = get_entity_visibility(entity);
1527 ir_linkage linkage = get_entity_linkage(entity);
1529 /* block labels are already emittet in the code */
1530 if (type == firm_code_type)
1533 /* we already emitted all methods. Except for the trampolines which
1534 * the assembler/linker generates */
1535 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1536 /* functions with graph are already emitted with
1537 * be_gas_emit_function_prolog */
1538 if (get_entity_irg(entity) == NULL) {
1539 emit_visibility(entity);
1544 be_dbg_variable(entity);
1546 if (section == GAS_SECTION_BSS) {
1547 switch (visibility) {
1548 case ir_visibility_local:
1549 case ir_visibility_private:
1550 emit_local_common(entity);
1552 case ir_visibility_default:
1553 if (linkage & IR_LINKAGE_MERGE) {
1554 emit_common(entity);
1558 case ir_visibility_external:
1559 if (linkage & IR_LINKAGE_MERGE)
1560 panic("merge link semantic not supported for extern entities");
1565 emit_visibility(entity);
1566 if (visibility == ir_visibility_external) {
1567 /* nothing to do for externally defined values */
1571 if (!is_po2(alignment))
1572 panic("alignment not a power of 2");
1574 emit_section(section, entity);
1576 if (section == GAS_SECTION_PIC_TRAMPOLINES
1577 || section == GAS_SECTION_PIC_SYMBOLS) {
1578 emit_indirect_symbol(entity, section);
1583 if (alignment > 1) {
1584 emit_align(alignment);
1586 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF
1587 && be_gas_emit_types
1588 && visibility != ir_visibility_private) {
1589 be_emit_cstring("\t.type\t");
1590 be_gas_emit_entity(entity);
1591 be_emit_cstring(", ");
1592 be_emit_char(be_gas_elf_type_char);
1593 be_emit_cstring("object\n\t.size\t");\
1594 be_gas_emit_entity(entity);
1595 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1598 if (get_id_str(ld_ident)[0] != '\0') {
1599 be_gas_emit_entity(entity);
1600 be_emit_cstring(":\n");
1601 be_emit_write_line();
1604 if (entity_is_null(entity)) {
1605 unsigned size = get_type_size_bytes(type);
1607 be_emit_irprintf("\t.space %u, 0\n", get_type_size_bytes(type));
1608 be_emit_write_line();
1610 } else if (entity_has_compound_ent_values(entity)) {
1611 emit_compound_graph_init(env, entity);
1613 assert(entity->initializer != NULL);
1614 emit_initializer(env, entity);
1619 * Dumps declarations of global variables and the initialization code.
1621 * @param gt a global like type, either the global or the TLS one
1622 * @param env an environment
1624 static void be_gas_emit_globals(ir_type *gt, be_gas_decl_env_t *env)
1626 size_t i, n = get_compound_n_members(gt);
1628 for (i = 0; i < n; i++) {
1629 ir_entity *ent = get_compound_member(gt, i);
1630 emit_global(env, ent);
1634 /* Generate all entities. */
1635 void be_gas_emit_decls(const be_main_env_t *main_env)
1637 be_gas_decl_env_t env;
1638 memset(&env, 0, sizeof(env));
1640 /* dump global type */
1641 env.main_env = main_env;
1642 env.section = (be_gas_section_t) -1;
1644 be_gas_emit_globals(get_glob_type(), &env);
1645 be_gas_emit_globals(get_tls_type(), &env);
1646 be_gas_emit_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env);
1647 be_gas_emit_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env);
1648 be_gas_emit_globals(main_env->pic_symbols_type, &env);
1649 be_gas_emit_globals(main_env->pic_trampolines_type, &env);
1652 * ".subsections_via_symbols marks object files which are OK to divide
1653 * their section contents into individual blocks".
1654 * From my understanding this means no label points in the middle of an
1655 * object which we want to address as a whole. Firm code should be fine
1658 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
1659 be_emit_cstring("\t.subsections_via_symbols\n");
1660 be_emit_write_line();