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 elf_variant_t be_gas_elf_variant = ELF_VARIANT_NORMAL;
51 bool be_gas_emit_types = true;
52 char be_gas_elf_type_char = '@';
54 static be_gas_section_t current_section = (be_gas_section_t) -1;
57 * An environment containing all needed dumper data.
58 * Currently we create the file completely in memory first, then
59 * write it to the disk. This is an artifact from the old C-generating backend
60 * and even there NOT needed. So we might change it in the future.
62 typedef struct be_gas_decl_env {
63 be_gas_section_t section;
64 const be_main_env_t *main_env;
67 static void emit_section_macho(be_gas_section_t section)
69 be_gas_section_t base = section & GAS_SECTION_TYPE_MASK;
70 be_gas_section_t flags = section & ~GAS_SECTION_TYPE_MASK;
73 if (current_section == section)
75 current_section = section;
80 case GAS_SECTION_TEXT: name = "text"; break;
81 case GAS_SECTION_DATA: name = "data"; break;
82 case GAS_SECTION_RODATA: name = "const"; break;
83 case GAS_SECTION_BSS: name = "data"; break;
84 case GAS_SECTION_CONSTRUCTORS: name = "mod_init_func"; break;
85 case GAS_SECTION_DESTRUCTORS: name = "mod_term_func"; break;
86 case GAS_SECTION_PIC_TRAMPOLINES: name = "section\t__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5"; break;
87 case GAS_SECTION_PIC_SYMBOLS: name = "section\t__IMPORT,__pointers,non_lazy_symbol_pointers"; break;
88 case GAS_SECTION_CSTRING: name = "cstring"; break;
89 case GAS_SECTION_DEBUG_INFO: name = "section __DWARF,__debug_info,regular,debug"; break;
90 case GAS_SECTION_DEBUG_ABBREV: name = "section __DWARF,__debug_abbrev,regular,debug"; break;
91 case GAS_SECTION_DEBUG_LINE: name = "section __DWARF,__debug_line,regular,debug"; break;
92 case GAS_SECTION_DEBUG_PUBNAMES: name = "section __DWARF,__debug_pubnames,regular,debug"; break;
93 default: panic("unsupported scetion type 0x%X", section);
95 be_emit_irprintf("\t.%s\n", name);
97 } else if (flags & GAS_SECTION_FLAG_COMDAT) {
99 case GAS_SECTION_TEXT: name = "section __TEXT,__textcoal_nt,coalesced,pure_instructions"; break;
100 case GAS_SECTION_BSS:
101 case GAS_SECTION_DATA: name = "section __DATA,__datacoal_nt,coalesced"; break;
102 case GAS_SECTION_RODATA: name = "section __TEXT,__const_coal,coalesced"; break;
103 case GAS_SECTION_CSTRING: name = "section __TEXT,__const_coal,coalesced"; break;
104 default: panic("unsupported scetion type 0x%X", section);
107 panic("unsupported section type 0x%X\n", section);
111 static void emit_section_sparc(be_gas_section_t section, const ir_entity *entity)
113 be_gas_section_t base = section & GAS_SECTION_TYPE_MASK;
114 be_gas_section_t flags = section & ~GAS_SECTION_TYPE_MASK;
115 static const char *const basename[GAS_SECTION_LAST+1] = {
123 NULL, /* pic trampolines */
124 NULL, /* pic symbols */
131 if (current_section == section && !(section & GAS_SECTION_FLAG_COMDAT))
133 current_section = section;
135 be_emit_cstring("\t.section\t\".");
137 /* Part1: section-name */
138 if (flags & GAS_SECTION_FLAG_TLS)
140 assert(base < (be_gas_section_t)ARRAY_SIZE(basename));
141 be_emit_string(basename[base]);
143 if (flags & GAS_SECTION_FLAG_COMDAT) {
145 be_gas_emit_entity(entity);
149 /* for the simple sections we're done here */
153 be_emit_cstring(",#alloc");
156 case GAS_SECTION_TEXT: be_emit_cstring(",#execinstr"); break;
157 case GAS_SECTION_DATA:
158 case GAS_SECTION_BSS: be_emit_cstring(",#write"); break;
163 if (flags & GAS_SECTION_FLAG_TLS) {
164 be_emit_cstring(",#tls");
169 be_emit_write_line();
172 static void emit_section(be_gas_section_t section, const ir_entity *entity)
174 be_gas_section_t base = section & GAS_SECTION_TYPE_MASK;
175 be_gas_section_t flags = section & ~GAS_SECTION_TYPE_MASK;
177 static const struct {
181 } sectioninfos[GAS_SECTION_LAST+1] = {
182 { "text", "progbits", "ax" },
183 { "data", "progbits", "aw" },
184 { "rodata", "progbits", "a" },
185 { "bss", "nobits", "aw" },
186 { "ctors", "progbits", "aw" },
187 { "dtors", "progbits", "aw" },
188 { NULL, NULL, NULL }, /* cstring */
189 { NULL, NULL, NULL }, /* pic trampolines */
190 { NULL, NULL, NULL }, /* pic symbols */
191 { "debug_info", "progbits", "" },
192 { "debug_abbrev", "progbits", "" },
193 { "debug_line", "progbits", "" },
194 { "debug_pubnames", "progbits", "" },
197 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
198 emit_section_macho(section);
200 } else if(be_gas_elf_variant == ELF_VARIANT_SPARC) {
201 emit_section_sparc(section, entity);
205 if (current_section == section && !(section & GAS_SECTION_FLAG_COMDAT))
207 current_section = section;
212 case GAS_SECTION_TEXT:
213 be_emit_cstring("\t.text\n");
214 be_emit_write_line();
216 case GAS_SECTION_DATA:
217 be_emit_cstring("\t.data\n");
218 be_emit_write_line();
220 case GAS_SECTION_RODATA:
221 be_emit_cstring("\t.section\t.rodata\n");
222 be_emit_write_line();
224 case GAS_SECTION_BSS:
225 be_emit_cstring("\t.bss\n");
226 be_emit_write_line();
233 assert(base < (be_gas_section_t) ARRAY_SIZE(sectioninfos));
234 be_emit_cstring("\t.section\t.");
236 if (flags & GAS_SECTION_FLAG_TLS)
238 be_emit_string(sectioninfos[base].name);
239 if (flags & GAS_SECTION_FLAG_COMDAT) {
241 be_gas_emit_entity(entity);
245 be_emit_cstring(",\"");
246 for (f = sectioninfos[base].flags; *f != '\0'; ++f) {
249 if (flags & GAS_SECTION_FLAG_TLS)
251 if (flags & GAS_SECTION_FLAG_COMDAT)
255 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_COFF) {
256 be_emit_cstring("\",");
257 be_emit_char(be_gas_elf_type_char);
258 be_emit_string(sectioninfos[base].type);
261 if (flags & GAS_SECTION_FLAG_COMDAT) {
263 be_gas_emit_entity(entity);
264 be_emit_cstring(",comdat");
267 be_emit_write_line();
272 void be_gas_emit_switch_section(be_gas_section_t section)
274 /* you have to produce a switch_section call with entity manually
275 * for comdat sections */
276 assert( !(section & GAS_SECTION_FLAG_COMDAT));
278 emit_section(section, NULL);
281 static ir_tarval *get_initializer_tarval(const ir_initializer_t *initializer)
283 if (initializer->kind == IR_INITIALIZER_TARVAL)
284 return initializer->tarval.value;
285 if (initializer->kind == IR_INITIALIZER_CONST) {
286 ir_node *node = initializer->consti.value;
287 if (is_Const(node)) {
288 return get_Const_tarval(node);
291 return get_tarval_undefined();
294 static bool initializer_is_string_const(const ir_initializer_t *initializer)
297 bool found_printable = false;
299 if (initializer->kind != IR_INITIALIZER_COMPOUND)
302 len = initializer->compound.n_initializers;
305 for (i = 0; i < len; ++i) {
309 ir_initializer_t *sub_initializer
310 = initializer->compound.initializers[i];
312 tv = get_initializer_tarval(sub_initializer);
313 if (!tarval_is_constant(tv))
316 mode = get_tarval_mode(tv);
317 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
320 c = get_tarval_long(tv);
321 if (isgraph(c) || isspace(c))
322 found_printable = true;
326 if (i == len - 1 && c != '\0')
330 return found_printable;
333 static bool initializer_is_null(const ir_initializer_t *initializer)
335 switch (initializer->kind) {
336 case IR_INITIALIZER_NULL:
338 case IR_INITIALIZER_TARVAL: {
339 ir_tarval *tv = initializer->tarval.value;
340 return tarval_is_null(tv);
342 case IR_INITIALIZER_CONST: {
343 ir_node *value = initializer->consti.value;
344 if (!is_Const(value))
346 return is_Const_null(value);
348 case IR_INITIALIZER_COMPOUND: {
350 for (i = 0; i < initializer->compound.n_initializers; ++i) {
351 ir_initializer_t *subinitializer
352 = initializer->compound.initializers[i];
353 if (!initializer_is_null(subinitializer))
359 panic("invalid initializer in initializer_is_null");
363 * Determine if an entity is a string constant
364 * @param ent The entity
365 * @return 1 if it is a string constant, 0 otherwise
367 static int entity_is_string_const(const ir_entity *ent)
369 ir_type *type, *element_type;
373 type = get_entity_type(ent);
375 /* if it's an array */
376 if (!is_Array_type(type))
379 element_type = get_array_element_type(type);
381 /* and the array's element type is primitive */
382 if (!is_Primitive_type(element_type))
385 /* and the mode of the element type is an int of
386 * the same size as the byte mode */
387 mode = get_type_mode(element_type);
388 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
391 if (ent->initializer != NULL) {
392 return initializer_is_string_const(ent->initializer);
393 } else if (entity_has_compound_ent_values(ent)) {
394 int found_printable = 0;
395 /* if it contains only printable chars and a 0 at the end */
396 n = get_compound_ent_n_values(ent);
397 for (i = 0; i < n; ++i) {
398 ir_node *irn = get_compound_ent_value(ent, i);
402 c = (int) get_tarval_long(get_Const_tarval(irn));
404 if (isgraph(c) || isspace(c))
409 if (i == n - 1 && c != '\0')
412 return found_printable;
418 static bool entity_is_null(const ir_entity *entity)
420 if (entity->initializer != NULL) {
421 return initializer_is_null(entity->initializer);
422 } else if (entity_has_compound_ent_values(entity)) {
423 /* I'm too lazy to implement this case as compound graph paths will be
424 * remove anyway in the future */
427 /* uninitialized, NULL is fine */
431 static bool is_comdat(const ir_entity *entity)
433 ir_linkage linkage = get_entity_linkage(entity);
434 return (linkage & IR_LINKAGE_MERGE)
435 && (linkage & IR_LINKAGE_GARBAGE_COLLECT);
438 static be_gas_section_t determine_basic_section(const ir_entity *entity)
442 if (is_method_entity(entity))
443 return GAS_SECTION_TEXT;
445 linkage = get_entity_linkage(entity);
446 if (linkage & IR_LINKAGE_CONSTANT) {
447 /* mach-o is the only one with a cstring section */
448 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
449 && entity_is_string_const(entity))
450 return GAS_SECTION_CSTRING;
452 return GAS_SECTION_RODATA;
454 if (entity_is_null(entity))
455 return GAS_SECTION_BSS;
457 return GAS_SECTION_DATA;
460 static be_gas_section_t determine_section(be_gas_decl_env_t *env,
461 const ir_entity *entity)
463 ir_type *owner = get_entity_owner(entity);
465 if (owner == get_segment_type(IR_SEGMENT_GLOBAL)) {
466 be_gas_section_t section = determine_basic_section(entity);
467 if (is_comdat(entity))
468 section |= GAS_SECTION_FLAG_COMDAT;
470 } else if (env != NULL && owner == env->main_env->pic_symbols_type) {
471 return GAS_SECTION_PIC_SYMBOLS;
472 } else if (env != NULL && owner == env->main_env->pic_trampolines_type) {
473 return GAS_SECTION_PIC_TRAMPOLINES;
474 } else if (owner == get_segment_type(IR_SEGMENT_CONSTRUCTORS)) {
475 return GAS_SECTION_CONSTRUCTORS;
476 } else if (owner == get_segment_type(IR_SEGMENT_DESTRUCTORS)) {
477 return GAS_SECTION_DESTRUCTORS;
478 } else if (owner == get_segment_type(IR_SEGMENT_THREAD_LOCAL)) {
479 be_gas_section_t section = determine_basic_section(entity);
480 if (is_comdat(entity))
481 section |= GAS_SECTION_FLAG_COMDAT;
483 return section | GAS_SECTION_FLAG_TLS;
486 /* the java frontend keeps some functions inside classes */
487 if (is_Class_type(owner)) {
488 return determine_basic_section(entity);
491 panic("Couldn't determine section for %+F?!?", entity);
494 static void emit_weak(const ir_entity *entity)
496 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
497 be_emit_cstring("\t.weak_reference ");
499 be_emit_cstring("\t.weak ");
501 be_gas_emit_entity(entity);
503 be_emit_write_line();
506 static void emit_visibility(const ir_entity *entity)
508 ir_linkage linkage = get_entity_linkage(entity);
510 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
512 /* Note: .weak seems to imply .globl so no need to output .globl */
513 } else if (get_entity_visibility(entity) == ir_visibility_default) {
514 be_emit_cstring(".globl ");
515 be_gas_emit_entity(entity);
517 be_emit_write_line();
520 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
521 && (linkage & IR_LINKAGE_HIDDEN_USER)
522 && get_entity_ld_name(entity)[0] != '\0') {
523 be_emit_cstring("\t.no_dead_strip ");
524 be_gas_emit_entity(entity);
526 be_emit_write_line();
530 void be_gas_emit_function_prolog(const ir_entity *entity, unsigned po2alignment)
532 be_gas_section_t section;
534 be_dbg_method_begin(entity);
536 section = determine_section(NULL, entity);
537 emit_section(section, entity);
539 /* write the begin line (makes the life easier for scripts parsing the
541 be_emit_write_line();
542 be_emit_cstring("# -- Begin ");
543 be_gas_emit_entity(entity);
545 be_emit_write_line();
547 if (po2alignment > 0) {
548 const char *fill_byte = "";
549 unsigned maximum_skip = (1 << po2alignment) - 1;
550 /* gcc fills space between function with 0x90... */
551 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
554 be_emit_cstring("\t.p2align ");
555 be_emit_irprintf("%u,%s,%u\n", po2alignment, fill_byte, maximum_skip);
556 be_emit_write_line();
558 emit_visibility(entity);
560 switch (be_gas_object_file_format) {
561 case OBJECT_FILE_FORMAT_ELF:
562 be_emit_cstring("\t.type\t");
563 be_gas_emit_entity(entity);
564 be_emit_cstring(", ");
565 be_emit_char(be_gas_elf_type_char);
566 be_emit_cstring("function\n");
567 be_emit_write_line();
569 case OBJECT_FILE_FORMAT_COFF:
570 be_emit_cstring("\t.def\t");
571 be_gas_emit_entity(entity);
572 be_emit_cstring(";");
573 if (get_entity_visibility(entity) == ir_visibility_local) {
574 be_emit_cstring("\t.scl\t3;");
576 be_emit_cstring("\t.scl\t2;");
578 be_emit_cstring("\t.type\t32;\t.endef\n");
579 be_emit_write_line();
581 case OBJECT_FILE_FORMAT_MACH_O:
584 be_gas_emit_entity(entity);
585 be_emit_cstring(":\n");
586 be_emit_write_line();
589 void be_gas_emit_function_epilog(const ir_entity *entity)
591 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF) {
592 be_emit_cstring("\t.size\t");
593 be_gas_emit_entity(entity);
594 be_emit_cstring(", .-");
595 be_gas_emit_entity(entity);
597 be_emit_write_line();
600 be_emit_cstring("# -- End ");
601 be_gas_emit_entity(entity);
603 be_emit_write_line();
608 be_emit_write_line();
614 * @param tv the tarval
615 * @param bytes the width of the tarvals value in bytes
617 static void emit_arith_tarval(ir_tarval *tv, unsigned bytes)
621 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
625 be_emit_irprintf("0x%02x%02x",
626 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
630 be_emit_irprintf("0x%02x%02x%02x%02x",
631 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
632 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
636 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
637 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
638 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
639 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
640 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
644 panic("Can't dump a tarval with %d bytes", bytes);
648 * Return the label prefix for labeled instructions.
650 const char *be_gas_insn_label_prefix(void)
656 * Return the tarval of an atomic initializer.
658 * @param init a node representing the initializer (on the const code irg)
662 static ir_tarval *get_atomic_init_tv(ir_node *init)
665 ir_mode *mode = get_irn_mode(init);
667 switch (get_irn_opcode(init)) {
670 init = get_Cast_op(init);
674 init = get_Conv_op(init);
678 return get_Const_tarval(init);
681 switch (get_SymConst_kind(init)) {
682 case symconst_type_size:
683 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
685 case symconst_type_align:
686 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
688 case symconst_ofs_ent:
689 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
691 case symconst_enum_const:
692 return get_enumeration_value(get_SymConst_enum(init));
705 * Dump an atomic value.
707 * @param env the gas output environment
708 * @param init a node representing the atomic value (on the const code irg)
710 static void emit_init_expression(be_gas_decl_env_t *env, ir_node *init)
712 ir_mode *mode = get_irn_mode(init);
713 int bytes = get_mode_size_bytes(mode);
717 init = skip_Id(init);
719 switch (get_irn_opcode(init)) {
721 emit_init_expression(env, get_Cast_op(init));
725 emit_init_expression(env, get_Conv_op(init));
729 tv = get_Const_tarval(init);
731 /* it's an arithmetic value */
732 emit_arith_tarval(tv, bytes);
736 switch (get_SymConst_kind(init)) {
737 case symconst_addr_ent:
738 ent = get_SymConst_entity(init);
739 be_gas_emit_entity(ent);
742 case symconst_ofs_ent:
743 ent = get_SymConst_entity(init);
744 be_emit_irprintf("%d", get_entity_offset(ent));
747 case symconst_type_size:
748 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
751 case symconst_type_align:
752 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
755 case symconst_enum_const:
756 tv = get_enumeration_value(get_SymConst_enum(init));
757 emit_arith_tarval(tv, bytes);
761 assert(!"emit_atomic_init(): don't know how to init from this SymConst");
766 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
767 panic("Constant must be int or pointer for '+' to work");
769 emit_init_expression(env, get_Add_left(init));
770 be_emit_cstring(" + ");
771 emit_init_expression(env, get_Add_right(init));
775 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
776 panic("Constant must be int or pointer for '-' to work");
778 emit_init_expression(env, get_Sub_left(init));
779 be_emit_cstring(" - ");
780 emit_init_expression(env, get_Sub_right(init));
784 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
785 panic("Constant must be int or pointer for '*' to work");
787 emit_init_expression(env, get_Mul_left(init));
788 be_emit_cstring(" * ");
789 emit_init_expression(env, get_Mul_right(init));
793 be_emit_cstring("0");
797 panic("emit_atomic_init(): unsupported IR-node %+F", init);
802 * Dumps the type for given size (.byte, .long, ...)
804 * @param size the size in bytes
806 static void emit_size_type(size_t size)
809 case 1: be_emit_cstring("\t.byte\t"); break;
810 case 2: be_emit_cstring("\t.short\t"); break;
811 case 4: be_emit_cstring("\t.long\t"); break;
812 case 8: be_emit_cstring("\t.quad\t"); break;
815 panic("Try to dump a type with %u bytes", (unsigned)size);
820 * Dump a string constant.
821 * No checks are made!!
823 * @param ent The entity to dump.
825 static void emit_string_cst(const ir_entity *ent)
833 len = get_compound_ent_n_values(ent);
835 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
836 be_emit_cstring("\t.ascii \"");
838 be_emit_cstring("\t.string \"");
842 for (i = 0; i < output_len; ++i) {
846 irn = get_compound_ent_value(ent, i);
847 c = (int) get_tarval_long(get_Const_tarval(irn));
850 case '"' : be_emit_cstring("\\\""); break;
851 case '\n': be_emit_cstring("\\n"); break;
852 case '\r': be_emit_cstring("\\r"); break;
853 case '\t': be_emit_cstring("\\t"); break;
854 case '\\': be_emit_cstring("\\\\"); break;
859 be_emit_irprintf("\\%03o", c);
863 be_emit_cstring("\"\n");
864 be_emit_write_line();
866 type = get_entity_type(ent);
867 type_size = get_type_size_bytes(type);
868 remaining_space = type_size - len;
869 assert(remaining_space >= 0);
870 if (remaining_space > 0) {
871 be_emit_irprintf("\t.space\t%d, 0\n", remaining_space);
875 static size_t emit_string_initializer(const ir_initializer_t *initializer)
879 len = initializer->compound.n_initializers;
880 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
881 be_emit_cstring("\t.ascii \"");
883 be_emit_cstring("\t.string \"");
887 for (i = 0; i < len; ++i) {
888 const ir_initializer_t *sub_initializer
889 = get_initializer_compound_value(initializer, i);
891 ir_tarval *tv = get_initializer_tarval(sub_initializer);
892 int c = get_tarval_long(tv);
895 case '"' : be_emit_cstring("\\\""); break;
896 case '\n': be_emit_cstring("\\n"); break;
897 case '\r': be_emit_cstring("\\r"); break;
898 case '\t': be_emit_cstring("\\t"); break;
899 case '\\': be_emit_cstring("\\\\"); break;
904 be_emit_irprintf("\\%03o", c);
908 be_emit_cstring("\"\n");
909 be_emit_write_line();
911 return initializer->compound.n_initializers;
914 typedef enum normal_or_bitfield_kind {
919 } normal_or_bitfield_kind;
922 normal_or_bitfield_kind kind;
927 unsigned char bf_val;
928 const ir_initializer_t *string;
930 } normal_or_bitfield;
932 static int is_type_variable_size(ir_type *type)
939 static size_t get_initializer_size(const ir_initializer_t *initializer,
942 switch (get_initializer_kind(initializer)) {
943 case IR_INITIALIZER_TARVAL:
944 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
945 return get_type_size_bytes(type);
946 case IR_INITIALIZER_CONST:
947 case IR_INITIALIZER_NULL:
948 return get_type_size_bytes(type);
949 case IR_INITIALIZER_COMPOUND:
950 if (!is_type_variable_size(type)) {
951 return get_type_size_bytes(type);
954 = get_initializer_compound_n_entries(initializer);
956 unsigned initializer_size = get_type_size_bytes(type);
957 for (i = 0; i < n_entries; ++i) {
958 ir_entity *entity = get_compound_member(type, i);
959 ir_type *type = get_entity_type(entity);
961 const ir_initializer_t *sub_initializer
962 = get_initializer_compound_value(initializer, i);
964 unsigned offset = get_entity_offset(entity);
965 unsigned size = get_initializer_size(sub_initializer, type);
967 if (offset + size > initializer_size) {
968 initializer_size = offset + size;
971 return initializer_size;
975 panic("found invalid initializer");
979 static normal_or_bitfield *glob_vals;
980 static size_t max_vals;
983 static void emit_bitfield(normal_or_bitfield *vals, size_t offset_bits,
984 const ir_initializer_t *initializer, ir_type *type)
986 static const size_t BITS_PER_BYTE = 8;
987 ir_mode *mode = get_type_mode(type);
988 ir_tarval *tv = NULL;
992 bool big_endian = be_get_backend_param()->byte_order_big_endian;
994 switch (get_initializer_kind(initializer)) {
995 case IR_INITIALIZER_NULL:
997 case IR_INITIALIZER_TARVAL:
998 tv = get_initializer_tarval_value(initializer);
1000 case IR_INITIALIZER_CONST: {
1001 ir_node *node = get_initializer_const_value(initializer);
1002 if (!is_Const(node)) {
1003 panic("bitfield initializer not a Const node");
1005 tv = get_Const_tarval(node);
1008 case IR_INITIALIZER_COMPOUND:
1009 panic("bitfield initializer is compound");
1012 panic("Couldn't get numeric value for bitfield initializer");
1014 tv = tarval_convert_to(tv, get_type_mode(type));
1016 value_len = get_type_size_bytes(get_primitive_base_type(type));
1018 end = get_mode_size_bits(mode);
1019 while (bit_offset < end) {
1020 size_t src_offset = bit_offset / BITS_PER_BYTE;
1021 size_t src_offset_bits = bit_offset % BITS_PER_BYTE;
1022 size_t dst_offset = (bit_offset+offset_bits) / BITS_PER_BYTE;
1023 size_t dst_offset_bits = (bit_offset+offset_bits) % BITS_PER_BYTE;
1024 size_t src_bits_len = end-bit_offset;
1025 size_t dst_bits_len = BITS_PER_BYTE-dst_offset_bits;
1026 unsigned char curr_bits;
1027 normal_or_bitfield *val;
1028 if (src_bits_len > dst_bits_len)
1029 src_bits_len = dst_bits_len;
1032 val = &vals[value_len - dst_offset - 1];
1034 val = &vals[dst_offset];
1037 assert((val-glob_vals) < (ptrdiff_t) max_vals);
1038 assert(val->kind == BITFIELD ||
1039 (val->kind == NORMAL && val->v.value == NULL));
1040 val->kind = BITFIELD;
1041 curr_bits = get_tarval_sub_bits(tv, src_offset);
1042 curr_bits = curr_bits >> src_offset_bits;
1043 if (src_offset_bits + src_bits_len > 8) {
1044 unsigned next_bits = get_tarval_sub_bits(tv, src_offset+1);
1045 curr_bits |= next_bits << (8 - src_offset_bits);
1047 curr_bits &= (1 << src_bits_len) - 1;
1048 val->v.bf_val |= curr_bits << dst_offset_bits;
1050 bit_offset += dst_bits_len;
1054 static void emit_ir_initializer(normal_or_bitfield *vals,
1055 const ir_initializer_t *initializer,
1058 assert((size_t) (vals - glob_vals) < max_vals);
1060 if (initializer_is_string_const(initializer)) {
1061 assert(vals->kind != BITFIELD);
1062 vals->kind = STRING;
1063 vals->v.string = initializer;
1067 switch (get_initializer_kind(initializer)) {
1068 case IR_INITIALIZER_NULL:
1070 case IR_INITIALIZER_TARVAL: {
1073 assert(vals->kind != BITFIELD);
1074 vals->kind = TARVAL;
1076 vals->v.tarval = get_initializer_tarval_value(initializer);
1077 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
1078 for (i = 1; i < get_type_size_bytes(type); ++i) {
1079 vals[i].kind = NORMAL;
1080 vals[i].type = NULL;
1081 vals[i].v.value = NULL;
1085 case IR_INITIALIZER_CONST: {
1088 assert(vals->kind != BITFIELD);
1089 vals->kind = NORMAL;
1091 vals->v.value = get_initializer_const_value(initializer);
1092 for (i = 1; i < get_type_size_bytes(type); ++i) {
1093 vals[i].kind = NORMAL;
1094 vals[i].type = NULL;
1095 vals[i].v.value = NULL;
1099 case IR_INITIALIZER_COMPOUND: {
1101 size_t n = get_initializer_compound_n_entries(initializer);
1103 if (is_Array_type(type)) {
1104 ir_type *element_type = get_array_element_type(type);
1105 size_t skip = get_type_size_bytes(element_type);
1106 size_t alignment = get_type_alignment_bytes(element_type);
1107 size_t misalign = skip % alignment;
1108 if (misalign != 0) {
1109 skip += alignment - misalign;
1112 for (i = 0; i < n; ++i) {
1113 ir_initializer_t *sub_initializer
1114 = get_initializer_compound_value(initializer, i);
1116 emit_ir_initializer(vals, sub_initializer, element_type);
1121 size_t n_members, i;
1122 assert(is_compound_type(type));
1123 n_members = get_compound_n_members(type);
1124 for (i = 0; i < n_members; ++i) {
1125 ir_entity *member = get_compound_member(type, i);
1126 size_t offset = get_entity_offset(member);
1127 ir_type *subtype = get_entity_type(member);
1128 ir_mode *mode = get_type_mode(subtype);
1129 ir_initializer_t *sub_initializer;
1131 assert(i < get_initializer_compound_n_entries(initializer));
1133 = get_initializer_compound_value(initializer, i);
1137 = get_entity_offset_bits_remainder(member);
1139 if (is_Primitive_type(subtype)
1140 && get_primitive_base_type(subtype) != NULL) {
1141 emit_bitfield(&vals[offset], offset_bits,
1142 sub_initializer, subtype);
1145 assert(offset_bits == 0);
1149 emit_ir_initializer(&vals[offset], sub_initializer, subtype);
1156 panic("invalid ir_initializer kind found");
1159 static void emit_tarval_data(ir_type *type, ir_tarval *tv)
1161 size_t size = get_type_size_bytes(type);
1163 /* this should be an x86 extended float */
1164 assert(be_get_backend_param()->byte_order_big_endian == 0);
1166 /* Beware: Mixed endian output! One little endian number emitted as
1167 * three longs. Each long initializer is written in big endian. */
1169 "\t.long\t0x%02x%02x%02x%02x\n"
1170 "\t.long\t0x%02x%02x%02x%02x\n"
1171 "\t.long\t0x%02x%02x%02x%02x\n",
1172 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
1173 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
1174 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
1175 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
1176 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
1177 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8)
1179 be_emit_write_line();
1180 } else if (size == 16) {
1181 if (be_get_backend_param()->byte_order_big_endian) {
1183 "\t.long\t0x%02x%02x%02x%02x\n"
1184 "\t.long\t0x%02x%02x%02x%02x\n"
1185 "\t.long\t0x%02x%02x%02x%02x\n"
1186 "\t.long\t0x%02x%02x%02x%02x\n",
1187 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 14),
1188 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
1189 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
1190 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
1191 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
1192 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
1193 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
1194 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0)
1197 /* Beware: Mixed endian output! One little endian number emitted as
1198 * three longs. Each long initializer is written in big endian. */
1200 "\t.long\t0x%02x%02x%02x%02x\n"
1201 "\t.long\t0x%02x%02x%02x%02x\n"
1202 "\t.long\t0x%02x%02x%02x%02x\n"
1203 "\t.long\t0x%02x%02x%02x%02x\n",
1204 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
1205 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
1206 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
1207 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
1208 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
1209 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
1210 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 14),
1211 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12)
1214 be_emit_write_line();
1218 emit_size_type(size);
1219 emit_arith_tarval(tv, size);
1221 be_emit_write_line();
1226 * Emit an atomic value.
1228 * @param env the gas output environment
1229 * @param init a node representing the atomic value (on the const code irg)
1231 static void emit_node_data(be_gas_decl_env_t *env, ir_node *init, ir_type *type)
1233 size_t size = get_type_size_bytes(type);
1234 if (size == 12 || size == 16) {
1236 if (!is_Const(init)) {
1237 panic("12/16byte initializers only support Const nodes yet");
1239 tv = get_Const_tarval(init);
1240 emit_tarval_data(type, tv);
1244 emit_size_type(size);
1245 emit_init_expression(env, init);
1247 be_emit_write_line();
1250 static void emit_initializer(be_gas_decl_env_t *env, const ir_entity *entity)
1252 const ir_initializer_t *initializer = entity->initializer;
1254 normal_or_bitfield *vals;
1258 if (initializer_is_string_const(initializer)) {
1259 emit_string_initializer(initializer);
1263 type = get_entity_type(entity);
1264 size = get_initializer_size(initializer, type);
1270 * In the worst case, every initializer allocates one byte.
1271 * Moreover, initializer might be big, do not allocate on stack.
1273 vals = XMALLOCNZ(normal_or_bitfield, size);
1280 emit_ir_initializer(vals, initializer, type);
1282 /* now write values sorted */
1283 for (k = 0; k < size; ) {
1285 normal_or_bitfield_kind kind = vals[k].kind;
1289 if (vals[k].v.value != NULL) {
1290 emit_node_data(env, vals[k].v.value, vals[k].type);
1291 elem_size = get_type_size_bytes(vals[k].type);
1297 emit_tarval_data(vals[k].type, vals[k].v.tarval);
1298 elem_size = get_type_size_bytes(vals[k].type);
1301 elem_size = emit_string_initializer(vals[k].v.string);
1304 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1305 be_emit_write_line();
1309 panic("internal compiler error (invalid normal_or_bitfield_kind");
1313 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1320 be_emit_irprintf("\t.space\t%d, 0\n", space);
1321 be_emit_write_line();
1327 static void emit_compound_graph_init(be_gas_decl_env_t *env,
1328 const ir_entity *ent)
1330 normal_or_bitfield *vals;
1332 unsigned k, last_ofs;
1334 if (entity_is_string_const(ent)) {
1335 emit_string_cst(ent);
1339 n = get_compound_ent_n_values(ent);
1341 /* Find the initializer size. Sorrily gcc support a nasty feature:
1342 The last field of a compound may be a flexible array. This allows
1343 initializers bigger than the type size. */
1344 last_ofs = get_type_size_bytes(get_entity_type(ent));
1345 for (i = 0; i < n; ++i) {
1346 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1347 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1348 ir_node *value = get_compound_ent_value(ent, i);
1349 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
1351 offset += (value_len + bits_remainder + 7) >> 3;
1353 if (offset > last_ofs) {
1359 * In the worst case, every initializer allocates one byte.
1360 * Moreover, initializer might be big, do not allocate on stack.
1362 vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1364 /* collect the values and store them at the offsets */
1365 for (i = 0; i < n; ++i) {
1366 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1367 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1368 ir_node *value = get_compound_ent_value(ent, i);
1369 int value_len = get_mode_size_bits(get_irn_mode(value));
1371 assert(offset_bits >= 0);
1373 if (offset_bits != 0 ||
1374 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1375 ir_tarval *tv = get_atomic_init_tv(value);
1376 unsigned char curr_bits, last_bits = 0;
1378 panic("Couldn't get numeric value for bitfield initializer '%s'",
1379 get_entity_ld_name(ent));
1381 /* normalize offset */
1382 offset += offset_bits >> 3;
1385 for (j = 0; value_len + offset_bits > 0; ++j) {
1386 assert(offset + j < last_ofs);
1387 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1388 vals[offset + j].kind = BITFIELD;
1389 curr_bits = get_tarval_sub_bits(tv, j);
1390 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1392 last_bits = curr_bits;
1397 assert(offset < last_ofs);
1398 assert(vals[offset].kind == NORMAL);
1399 for (i = 1; i < value_len / 8; ++i) {
1400 assert(vals[offset + i].v.value == NULL);
1402 vals[offset].v.value = value;
1406 /* now write them sorted */
1407 for (k = 0; k < last_ofs; ) {
1408 int space = 0, skip = 0;
1409 if (vals[k].kind == NORMAL) {
1410 if (vals[k].v.value != NULL) {
1411 emit_node_data(env, vals[k].v.value, vals[k].type);
1412 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1417 assert(vals[k].kind == BITFIELD);
1418 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1422 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1431 be_emit_irprintf("\t.space\t%d, 0\n", space);
1432 be_emit_write_line();
1438 static void emit_align(unsigned p2alignment)
1440 be_emit_irprintf("\t.p2align\t%u\n", log2_floor(p2alignment));
1441 be_emit_write_line();
1444 static unsigned get_effective_entity_alignment(const ir_entity *entity)
1446 unsigned alignment = get_entity_alignment(entity);
1447 if (alignment == 0) {
1448 ir_type *type = get_entity_type(entity);
1449 alignment = get_type_alignment_bytes(type);
1454 static void emit_common(const ir_entity *entity)
1456 unsigned size = get_type_size_bytes(get_entity_type(entity));
1457 unsigned alignment = get_effective_entity_alignment(entity);
1459 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
1463 switch (be_gas_object_file_format) {
1464 case OBJECT_FILE_FORMAT_MACH_O:
1465 be_emit_cstring("\t.comm ");
1466 be_gas_emit_entity(entity);
1467 be_emit_irprintf(",%u,%u\n", size, log2_floor(alignment));
1468 be_emit_write_line();
1470 case OBJECT_FILE_FORMAT_ELF:
1471 be_emit_cstring("\t.comm ");
1472 be_gas_emit_entity(entity);
1473 be_emit_irprintf(",%u,%u\n", size, alignment);
1474 be_emit_write_line();
1476 case OBJECT_FILE_FORMAT_COFF:
1477 be_emit_cstring("\t.comm ");
1478 be_gas_emit_entity(entity);
1479 be_emit_irprintf(",%u # %u\n", size, alignment);
1480 be_emit_write_line();
1483 panic("invalid object file format");
1486 static void emit_local_common(const ir_entity *entity)
1488 unsigned size = get_type_size_bytes(get_entity_type(entity));
1489 unsigned alignment = get_effective_entity_alignment(entity);
1491 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
1495 switch (be_gas_object_file_format) {
1496 case OBJECT_FILE_FORMAT_MACH_O:
1497 be_emit_cstring("\t.lcomm ");
1498 be_gas_emit_entity(entity);
1499 be_emit_irprintf(",%u,%u\n", size, log2_floor(alignment));
1500 be_emit_write_line();
1502 case OBJECT_FILE_FORMAT_ELF:
1503 be_emit_cstring("\t.local ");
1504 be_gas_emit_entity(entity);
1505 be_emit_cstring("\n");
1506 be_emit_write_line();
1507 be_emit_cstring("\t.comm ");
1508 be_gas_emit_entity(entity);
1509 be_emit_irprintf(",%u,%u\n", size, alignment);
1510 be_emit_write_line();
1512 case OBJECT_FILE_FORMAT_COFF:
1513 be_emit_cstring("\t.lcomm ");
1514 be_gas_emit_entity(entity);
1515 be_emit_irprintf(",%u # %u\n", size, alignment);
1516 be_emit_write_line();
1519 panic("invalid object file format");
1522 static void emit_indirect_symbol(const ir_entity *entity, be_gas_section_t section)
1524 /* we can only do PIC code on macho so far */
1525 assert(be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O);
1527 be_gas_emit_entity(entity);
1528 be_emit_cstring(":\n");
1529 be_emit_write_line();
1530 be_emit_cstring("\t.indirect_symbol ");
1531 be_emit_ident(get_entity_ident(entity));
1533 be_emit_write_line();
1534 if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1535 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1536 be_emit_write_line();
1538 assert(section == GAS_SECTION_PIC_SYMBOLS);
1539 be_emit_cstring("\t.long 0\n");
1540 be_emit_write_line();
1544 char const *be_gas_get_private_prefix(void)
1546 return be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O ? "L" : ".L";
1549 void be_gas_emit_entity(const ir_entity *entity)
1551 if (entity->type == firm_code_type) {
1552 ir_label_t label = get_entity_label(entity);
1553 be_emit_irprintf("%s_%lu", be_gas_get_private_prefix(), label);
1557 if (get_entity_visibility(entity) == ir_visibility_private) {
1558 be_emit_string(be_gas_get_private_prefix());
1560 be_emit_ident(get_entity_ld_ident(entity));
1563 void be_gas_emit_block_name(const ir_node *block)
1565 if (has_Block_entity(block)) {
1566 ir_entity *entity = get_Block_entity(block);
1567 be_gas_emit_entity(entity);
1569 be_emit_irprintf("%s%ld", be_gas_get_private_prefix(), get_irn_node_nr(block));
1574 * Dump a global entity.
1576 * @param env the gas output environment
1577 * @param ent the entity to be dumped
1579 static void emit_global(be_gas_decl_env_t *env, const ir_entity *entity)
1581 ir_type *type = get_entity_type(entity);
1582 ident *ld_ident = get_entity_ld_ident(entity);
1583 unsigned alignment = get_effective_entity_alignment(entity);
1584 be_gas_section_t section = determine_section(env, entity);
1585 ir_visibility visibility = get_entity_visibility(entity);
1586 ir_linkage linkage = get_entity_linkage(entity);
1588 /* block labels are already emittet in the code */
1589 if (type == firm_code_type)
1592 /* we already emitted all methods. Except for the trampolines which
1593 * the assembler/linker generates */
1594 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1595 /* functions with graph are already emitted with
1596 * be_gas_emit_function_prolog */
1597 if (get_entity_irg(entity) == NULL) {
1598 emit_visibility(entity);
1603 be_dbg_variable(entity);
1605 if (section == GAS_SECTION_BSS) {
1606 switch (visibility) {
1607 case ir_visibility_local:
1608 case ir_visibility_private:
1609 emit_local_common(entity);
1611 case ir_visibility_default:
1612 if (linkage & IR_LINKAGE_MERGE) {
1613 emit_common(entity);
1617 case ir_visibility_external:
1618 if (linkage & IR_LINKAGE_MERGE)
1619 panic("merge link semantic not supported for extern entities");
1624 emit_visibility(entity);
1625 if (visibility == ir_visibility_external) {
1626 /* nothing to do for externally defined values */
1630 if (!is_po2(alignment))
1631 panic("alignment not a power of 2");
1633 emit_section(section, entity);
1635 if (section == GAS_SECTION_PIC_TRAMPOLINES
1636 || section == GAS_SECTION_PIC_SYMBOLS) {
1637 emit_indirect_symbol(entity, section);
1642 if (alignment > 1) {
1643 emit_align(alignment);
1645 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF
1646 && be_gas_emit_types
1647 && visibility != ir_visibility_private) {
1648 be_emit_cstring("\t.type\t");
1649 be_gas_emit_entity(entity);
1650 be_emit_cstring(", ");
1651 be_emit_char(be_gas_elf_type_char);
1652 be_emit_cstring("object\n\t.size\t");\
1653 be_gas_emit_entity(entity);
1654 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1657 if (get_id_str(ld_ident)[0] != '\0') {
1658 be_gas_emit_entity(entity);
1659 be_emit_cstring(":\n");
1660 be_emit_write_line();
1663 if (entity_is_null(entity)) {
1664 unsigned size = get_type_size_bytes(type);
1666 be_emit_irprintf("\t.space %u, 0\n", get_type_size_bytes(type));
1667 be_emit_write_line();
1669 } else if (entity_has_compound_ent_values(entity)) {
1670 emit_compound_graph_init(env, entity);
1672 assert(entity->initializer != NULL);
1673 emit_initializer(env, entity);
1678 * Dumps declarations of global variables and the initialization code.
1680 * @param gt a global like type, either the global or the TLS one
1681 * @param env an environment
1683 static void be_gas_emit_globals(ir_type *gt, be_gas_decl_env_t *env)
1685 size_t i, n = get_compound_n_members(gt);
1687 for (i = 0; i < n; i++) {
1688 ir_entity *ent = get_compound_member(gt, i);
1689 emit_global(env, ent);
1693 /* Generate all entities. */
1694 static void emit_global_decls(const be_main_env_t *main_env)
1696 be_gas_decl_env_t env;
1697 memset(&env, 0, sizeof(env));
1699 /* dump global type */
1700 env.main_env = main_env;
1701 env.section = (be_gas_section_t) -1;
1703 be_gas_emit_globals(get_glob_type(), &env);
1704 be_gas_emit_globals(get_tls_type(), &env);
1705 be_gas_emit_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env);
1706 be_gas_emit_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env);
1707 be_gas_emit_globals(main_env->pic_symbols_type, &env);
1708 be_gas_emit_globals(main_env->pic_trampolines_type, &env);
1711 * ".subsections_via_symbols marks object files which are OK to divide
1712 * their section contents into individual blocks".
1713 * From my understanding this means no label points in the middle of an
1714 * object which we want to address as a whole. Firm code should be fine
1717 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
1718 be_emit_cstring("\t.subsections_via_symbols\n");
1719 be_emit_write_line();
1723 void be_emit_jump_table(const ir_node *node, const ir_switch_table *table,
1724 ir_entity *entity, get_cfop_target_func get_cfop_target)
1726 unsigned n_outs = arch_get_irn_n_outs(node);
1727 const ir_node **targets = XMALLOCNZ(const ir_node*, n_outs);
1728 size_t n_entries = ir_switch_table_get_n_entries(table);
1729 unsigned long length = 0;
1731 const ir_edge_t *edge;
1733 const ir_node **labels;
1735 /* go over all proj's and collect their jump targets */
1736 foreach_out_edge(node, edge) {
1737 ir_node *proj = get_edge_src_irn(edge);
1738 long pn = get_Proj_proj(proj);
1739 ir_node *target = get_cfop_target(proj);
1740 assert(targets[pn] == NULL);
1741 targets[pn] = target;
1744 /* go over table to determine max value (note that we normalized the
1745 * ranges so that the minimum is 0) */
1746 for (e = 0; e < n_entries; ++e) {
1747 const ir_switch_table_entry *entry
1748 = ir_switch_table_get_entry_const(table, e);
1749 ir_tarval *max = entry->max;
1753 if (!tarval_is_long(max))
1754 panic("switch case overflow (%+F)", node);
1755 val = (unsigned long) get_tarval_long(max);
1761 /* the 16000 isn't a real limit of the architecture. But should protect us
1762 * from seamingly endless compiler runs */
1763 if (length > 16000) {
1764 /* switch lowerer should have broken this monster to pieces... */
1765 panic("too large switch encountered (%+F)", node);
1769 labels = XMALLOCNZ(const ir_node*, length);
1770 for (e = 0; e < n_entries; ++e) {
1771 const ir_switch_table_entry *entry
1772 = ir_switch_table_get_entry_const(table, e);
1773 ir_tarval *min = entry->min;
1774 ir_tarval *max = entry->max;
1775 const ir_node *target = targets[entry->pn];
1776 assert(entry->pn < (long)n_outs);
1778 unsigned long val = (unsigned long)get_tarval_long(max);
1779 labels[val] = target;
1781 unsigned long min_val;
1782 unsigned long max_val;
1784 if (!tarval_is_long(min))
1785 panic("switch case overflow (%+F)", node);
1786 min_val = (unsigned long)get_tarval_long(min);
1787 max_val = (unsigned long)get_tarval_long(max);
1788 assert(min_val <= max_val);
1789 for (i = min_val; i <= max_val; ++i) {
1796 if (entity != NULL) {
1797 be_gas_emit_switch_section(GAS_SECTION_RODATA);
1798 be_emit_cstring("\t.align 4\n");
1799 be_gas_emit_entity(entity);
1800 be_emit_cstring(":\n");
1803 for (i = 0; i < length; ++i) {
1804 const ir_node *block = labels[i];
1807 be_emit_cstring("\t.long ");
1808 be_gas_emit_block_name(block);
1810 be_emit_write_line();
1814 be_gas_emit_switch_section(GAS_SECTION_TEXT);
1820 static void emit_global_asms(void)
1822 size_t n = get_irp_n_asms();
1825 be_gas_emit_switch_section(GAS_SECTION_TEXT);
1826 for (i = 0; i < n; ++i) {
1827 ident *asmtext = get_irp_asm(i);
1829 be_emit_cstring("#APP\n");
1830 be_emit_write_line();
1831 be_emit_ident(asmtext);
1833 be_emit_write_line();
1834 be_emit_cstring("#NO_APP\n");
1835 be_emit_write_line();
1839 void be_gas_begin_compilation_unit(const be_main_env_t *env)
1842 be_dbg_unit_begin(env->cup_name);
1848 void be_gas_end_compilation_unit(const be_main_env_t *env)
1850 emit_global_decls(env);