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 default: panic("unsupported scetion type 0x%X", section);
91 be_emit_irprintf("\t.%s\n", name);
93 } else if (flags & GAS_SECTION_FLAG_COMDAT) {
95 case GAS_SECTION_TEXT: name = "section __TEXT,__textcoal_nt,coalesced,pure_instructions"; break;
97 case GAS_SECTION_DATA: name = "section __DATA,__datacoal_nt,coalesced"; break;
98 case GAS_SECTION_RODATA: name = "section __TEXT,__const_coal,coalesced"; break;
99 case GAS_SECTION_CSTRING: name = "section __TEXT,__const_coal,coalesced"; break;
100 default: panic("unsupported scetion type 0x%X", section);
103 panic("unsupported section type 0x%X\n", section);
107 static void emit_section_sparc(be_gas_section_t section, const ir_entity *entity)
109 be_gas_section_t base = section & GAS_SECTION_TYPE_MASK;
110 be_gas_section_t flags = section & ~GAS_SECTION_TYPE_MASK;
111 static const char *const basename[] = {
112 "text", "data", "rodata", "bss", "ctors", "dtors"
115 if (current_section == section && !(section & GAS_SECTION_FLAG_COMDAT))
117 current_section = section;
119 be_emit_cstring("\t.section\t\".");
121 /* Part1: section-name */
122 if (flags & GAS_SECTION_FLAG_TLS)
124 assert(base < (be_gas_section_t)ARRAY_SIZE(basename));
125 be_emit_string(basename[base]);
127 if (flags & GAS_SECTION_FLAG_COMDAT) {
129 be_gas_emit_entity(entity);
133 /* for the simple sections we're done here */
137 be_emit_cstring(",#alloc");
140 case GAS_SECTION_TEXT: be_emit_cstring(",#execinstr"); break;
141 case GAS_SECTION_DATA:
142 case GAS_SECTION_BSS: be_emit_cstring(",#write"); break;
147 if (flags & GAS_SECTION_FLAG_TLS) {
148 be_emit_cstring(",#tls");
153 be_emit_write_line();
156 static void emit_section(be_gas_section_t section, const ir_entity *entity)
158 be_gas_section_t base = section & GAS_SECTION_TYPE_MASK;
159 be_gas_section_t flags = section & ~GAS_SECTION_TYPE_MASK;
161 static const struct {
166 { "text", "progbits", "ax" },
167 { "data", "progbits", "aw" },
168 { "rodata", "progbits", "a" },
169 { "bss", "nobits", "aw" },
170 { "ctors", "progbits", "aw" },
171 { "dtors", "progbits", "aw" },
172 { NULL, NULL, NULL }, /* cstring */
173 { NULL, NULL, NULL }, /* pic trampolines */
174 { NULL, NULL, NULL }, /* pic symbols */
175 { "debug_info", "progbits", "" },
176 { "debug_abbrev", "progbits", "" },
177 { "debug_line", "progbits", "" },
180 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
181 emit_section_macho(section);
183 } else if(be_gas_elf_variant == ELF_VARIANT_SPARC) {
184 emit_section_sparc(section, entity);
188 if (current_section == section && !(section & GAS_SECTION_FLAG_COMDAT))
190 current_section = section;
195 case GAS_SECTION_TEXT:
196 be_emit_cstring("\t.text\n");
197 be_emit_write_line();
199 case GAS_SECTION_DATA:
200 be_emit_cstring("\t.data\n");
201 be_emit_write_line();
203 case GAS_SECTION_RODATA:
204 be_emit_cstring("\t.section\t.rodata\n");
205 be_emit_write_line();
207 case GAS_SECTION_BSS:
208 be_emit_cstring("\t.bss\n");
209 be_emit_write_line();
216 assert(base < (be_gas_section_t) ARRAY_SIZE(sectioninfos));
217 be_emit_cstring("\t.section\t.");
219 if (flags & GAS_SECTION_FLAG_TLS)
221 be_emit_string(sectioninfos[base].name);
222 if (flags & GAS_SECTION_FLAG_COMDAT) {
224 be_gas_emit_entity(entity);
228 be_emit_cstring(",\"");
229 for (f = sectioninfos[base].flags; *f != '\0'; ++f) {
232 if (flags & GAS_SECTION_FLAG_TLS)
234 if (flags & GAS_SECTION_FLAG_COMDAT)
238 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_COFF) {
239 be_emit_cstring("\",");
240 be_emit_char(be_gas_elf_type_char);
241 be_emit_string(sectioninfos[base].type);
244 if (flags & GAS_SECTION_FLAG_COMDAT) {
246 be_gas_emit_entity(entity);
247 be_emit_cstring(",comdat");
250 be_emit_write_line();
255 void be_gas_emit_switch_section(be_gas_section_t section)
257 /* you have to produce a switch_section call with entity manually
258 * for comdat sections */
259 assert( !(section & GAS_SECTION_FLAG_COMDAT));
261 emit_section(section, NULL);
264 static ir_tarval *get_initializer_tarval(const ir_initializer_t *initializer)
266 if (initializer->kind == IR_INITIALIZER_TARVAL)
267 return initializer->tarval.value;
268 if (initializer->kind == IR_INITIALIZER_CONST) {
269 ir_node *node = initializer->consti.value;
270 if (is_Const(node)) {
271 return get_Const_tarval(node);
274 return get_tarval_undefined();
277 static bool initializer_is_string_const(const ir_initializer_t *initializer)
280 bool found_printable = false;
282 if (initializer->kind != IR_INITIALIZER_COMPOUND)
285 len = initializer->compound.n_initializers;
288 for (i = 0; i < len; ++i) {
292 ir_initializer_t *sub_initializer
293 = initializer->compound.initializers[i];
295 tv = get_initializer_tarval(sub_initializer);
296 if (!tarval_is_constant(tv))
299 mode = get_tarval_mode(tv);
300 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
303 c = get_tarval_long(tv);
304 if (isgraph(c) || isspace(c))
305 found_printable = true;
309 if (i == len - 1 && c != '\0')
313 return found_printable;
316 static bool initializer_is_null(const ir_initializer_t *initializer)
318 switch (initializer->kind) {
319 case IR_INITIALIZER_NULL:
321 case IR_INITIALIZER_TARVAL: {
322 ir_tarval *tv = initializer->tarval.value;
323 return tarval_is_null(tv);
325 case IR_INITIALIZER_CONST: {
326 ir_node *value = initializer->consti.value;
327 if (!is_Const(value))
329 return is_Const_null(value);
331 case IR_INITIALIZER_COMPOUND: {
333 for (i = 0; i < initializer->compound.n_initializers; ++i) {
334 ir_initializer_t *subinitializer
335 = initializer->compound.initializers[i];
336 if (!initializer_is_null(subinitializer))
342 panic("invalid initializer in initializer_is_null");
346 * Determine if an entity is a string constant
347 * @param ent The entity
348 * @return 1 if it is a string constant, 0 otherwise
350 static int entity_is_string_const(const ir_entity *ent)
352 ir_type *type, *element_type;
356 type = get_entity_type(ent);
358 /* if it's an array */
359 if (!is_Array_type(type))
362 element_type = get_array_element_type(type);
364 /* and the array's element type is primitive */
365 if (!is_Primitive_type(element_type))
368 /* and the mode of the element type is an int of
369 * the same size as the byte mode */
370 mode = get_type_mode(element_type);
371 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
374 if (ent->initializer != NULL) {
375 return initializer_is_string_const(ent->initializer);
376 } else if (entity_has_compound_ent_values(ent)) {
377 int found_printable = 0;
378 /* if it contains only printable chars and a 0 at the end */
379 n = get_compound_ent_n_values(ent);
380 for (i = 0; i < n; ++i) {
381 ir_node *irn = get_compound_ent_value(ent, i);
385 c = (int) get_tarval_long(get_Const_tarval(irn));
387 if (isgraph(c) || isspace(c))
392 if (i == n - 1 && c != '\0')
395 return found_printable;
401 static bool entity_is_null(const ir_entity *entity)
403 if (entity->initializer != NULL) {
404 return initializer_is_null(entity->initializer);
405 } else if (entity_has_compound_ent_values(entity)) {
406 /* I'm too lazy to implement this case as compound graph paths will be
407 * remove anyway in the future */
410 /* uninitialized, NULL is fine */
414 static bool is_comdat(const ir_entity *entity)
416 ir_linkage linkage = get_entity_linkage(entity);
417 return (linkage & IR_LINKAGE_MERGE)
418 && (linkage & IR_LINKAGE_GARBAGE_COLLECT);
421 static be_gas_section_t determine_basic_section(const ir_entity *entity)
425 if (is_method_entity(entity))
426 return GAS_SECTION_TEXT;
428 linkage = get_entity_linkage(entity);
429 if (linkage & IR_LINKAGE_CONSTANT) {
430 /* mach-o is the only one with a cstring section */
431 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
432 && entity_is_string_const(entity))
433 return GAS_SECTION_CSTRING;
435 return GAS_SECTION_RODATA;
437 if (entity_is_null(entity))
438 return GAS_SECTION_BSS;
440 return GAS_SECTION_DATA;
443 static be_gas_section_t determine_section(be_gas_decl_env_t *env,
444 const ir_entity *entity)
446 ir_type *owner = get_entity_owner(entity);
448 if (owner == get_segment_type(IR_SEGMENT_GLOBAL)) {
449 be_gas_section_t section = determine_basic_section(entity);
450 if (is_comdat(entity))
451 section |= GAS_SECTION_FLAG_COMDAT;
453 } else if (env != NULL && owner == env->main_env->pic_symbols_type) {
454 return GAS_SECTION_PIC_SYMBOLS;
455 } else if (env != NULL && owner == env->main_env->pic_trampolines_type) {
456 return GAS_SECTION_PIC_TRAMPOLINES;
457 } else if (owner == get_segment_type(IR_SEGMENT_CONSTRUCTORS)) {
458 return GAS_SECTION_CONSTRUCTORS;
459 } else if (owner == get_segment_type(IR_SEGMENT_DESTRUCTORS)) {
460 return GAS_SECTION_DESTRUCTORS;
461 } else if (owner == get_segment_type(IR_SEGMENT_THREAD_LOCAL)) {
462 be_gas_section_t section = determine_basic_section(entity);
463 if (is_comdat(entity))
464 section |= GAS_SECTION_FLAG_COMDAT;
466 return section | GAS_SECTION_FLAG_TLS;
469 /* the java frontend keeps some functions inside classes */
470 if (is_Class_type(owner)) {
471 return determine_basic_section(entity);
474 panic("Couldn't determine section for %+F?!?", entity);
477 static void emit_weak(const ir_entity *entity)
479 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
480 be_emit_cstring("\t.weak_reference ");
482 be_emit_cstring("\t.weak ");
484 be_gas_emit_entity(entity);
486 be_emit_write_line();
489 static void emit_visibility(const ir_entity *entity)
491 ir_linkage linkage = get_entity_linkage(entity);
493 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
495 /* Note: .weak seems to imply .globl so no need to output .globl */
496 } else if (get_entity_visibility(entity) == ir_visibility_default) {
497 be_emit_cstring(".globl ");
498 be_gas_emit_entity(entity);
500 be_emit_write_line();
503 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
504 && (linkage & IR_LINKAGE_HIDDEN_USER)
505 && get_entity_ld_name(entity)[0] != '\0') {
506 be_emit_cstring("\t.no_dead_strip ");
507 be_gas_emit_entity(entity);
509 be_emit_write_line();
513 void be_gas_emit_function_prolog(const ir_entity *entity, unsigned po2alignment)
515 be_gas_section_t section = determine_section(NULL, entity);
516 emit_section(section, entity);
518 /* write the begin line (makes the life easier for scripts parsing the
520 be_emit_write_line();
521 be_emit_cstring("# -- Begin ");
522 be_gas_emit_entity(entity);
524 be_emit_write_line();
526 if (po2alignment > 0) {
527 const char *fill_byte = "";
528 unsigned maximum_skip = (1 << po2alignment) - 1;
529 /* gcc fills space between function with 0x90... */
530 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
533 be_emit_cstring("\t.p2align ");
534 be_emit_irprintf("%u,%s,%u\n", po2alignment, fill_byte, maximum_skip);
535 be_emit_write_line();
537 emit_visibility(entity);
539 switch (be_gas_object_file_format) {
540 case OBJECT_FILE_FORMAT_ELF:
541 be_emit_cstring("\t.type\t");
542 be_gas_emit_entity(entity);
543 be_emit_cstring(", ");
544 be_emit_char(be_gas_elf_type_char);
545 be_emit_cstring("function\n");
546 be_emit_write_line();
548 case OBJECT_FILE_FORMAT_COFF:
549 be_emit_cstring("\t.def\t");
550 be_gas_emit_entity(entity);
551 be_emit_cstring(";");
552 if (get_entity_visibility(entity) == ir_visibility_local) {
553 be_emit_cstring("\t.scl\t3;");
555 be_emit_cstring("\t.scl\t2;");
557 be_emit_cstring("\t.type\t32;\t.endef\n");
558 be_emit_write_line();
560 case OBJECT_FILE_FORMAT_MACH_O:
563 be_gas_emit_entity(entity);
564 be_emit_cstring(":\n");
565 be_emit_write_line();
568 void be_gas_emit_function_epilog(const ir_entity *entity)
570 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF) {
571 be_emit_cstring("\t.size\t");
572 be_gas_emit_entity(entity);
573 be_emit_cstring(", .-");
574 be_gas_emit_entity(entity);
576 be_emit_write_line();
579 be_emit_cstring("# -- End ");
580 be_gas_emit_entity(entity);
582 be_emit_write_line();
588 * @param tv the tarval
589 * @param bytes the width of the tarvals value in bytes
591 static void emit_arith_tarval(ir_tarval *tv, unsigned bytes)
595 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
599 be_emit_irprintf("0x%02x%02x",
600 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
604 be_emit_irprintf("0x%02x%02x%02x%02x",
605 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
606 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
610 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
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, 3), get_tarval_sub_bits(tv, 2),
614 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
618 panic("Can't dump a tarval with %d bytes", bytes);
622 * Return the label prefix for labeled instructions.
624 const char *be_gas_insn_label_prefix(void)
630 * Return the tarval of an atomic initializer.
632 * @param init a node representing the initializer (on the const code irg)
636 static ir_tarval *get_atomic_init_tv(ir_node *init)
639 ir_mode *mode = get_irn_mode(init);
641 switch (get_irn_opcode(init)) {
644 init = get_Cast_op(init);
648 init = get_Conv_op(init);
652 return get_Const_tarval(init);
655 switch (get_SymConst_kind(init)) {
656 case symconst_type_size:
657 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
659 case symconst_type_align:
660 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
662 case symconst_ofs_ent:
663 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
665 case symconst_enum_const:
666 return get_enumeration_value(get_SymConst_enum(init));
679 * Dump an atomic value.
681 * @param env the gas output environment
682 * @param init a node representing the atomic value (on the const code irg)
684 static void emit_init_expression(be_gas_decl_env_t *env, ir_node *init)
686 ir_mode *mode = get_irn_mode(init);
687 int bytes = get_mode_size_bytes(mode);
691 init = skip_Id(init);
693 switch (get_irn_opcode(init)) {
695 emit_init_expression(env, get_Cast_op(init));
699 emit_init_expression(env, get_Conv_op(init));
703 tv = get_Const_tarval(init);
705 /* it's an arithmetic value */
706 emit_arith_tarval(tv, bytes);
710 switch (get_SymConst_kind(init)) {
711 case symconst_addr_ent:
712 ent = get_SymConst_entity(init);
713 be_gas_emit_entity(ent);
716 case symconst_ofs_ent:
717 ent = get_SymConst_entity(init);
718 be_emit_irprintf("%d", get_entity_offset(ent));
721 case symconst_type_size:
722 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
725 case symconst_type_align:
726 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
729 case symconst_enum_const:
730 tv = get_enumeration_value(get_SymConst_enum(init));
731 emit_arith_tarval(tv, bytes);
735 assert(!"emit_atomic_init(): don't know how to init from this SymConst");
740 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
741 panic("Constant must be int or pointer for '+' to work");
743 emit_init_expression(env, get_Add_left(init));
744 be_emit_cstring(" + ");
745 emit_init_expression(env, get_Add_right(init));
749 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
750 panic("Constant must be int or pointer for '-' to work");
752 emit_init_expression(env, get_Sub_left(init));
753 be_emit_cstring(" - ");
754 emit_init_expression(env, get_Sub_right(init));
758 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
759 panic("Constant must be int or pointer for '*' to work");
761 emit_init_expression(env, get_Mul_left(init));
762 be_emit_cstring(" * ");
763 emit_init_expression(env, get_Mul_right(init));
767 be_emit_cstring("0");
771 panic("emit_atomic_init(): unsupported IR-node %+F", init);
776 * Dumps the type for given size (.byte, .long, ...)
778 * @param size the size in bytes
780 static void emit_size_type(size_t size)
783 case 1: be_emit_cstring("\t.byte\t"); break;
784 case 2: be_emit_cstring("\t.short\t"); break;
785 case 4: be_emit_cstring("\t.long\t"); break;
786 case 8: be_emit_cstring("\t.quad\t"); break;
789 panic("Try to dump a type with %u bytes", (unsigned)size);
794 * Dump a string constant.
795 * No checks are made!!
797 * @param ent The entity to dump.
799 static void emit_string_cst(const ir_entity *ent)
807 len = get_compound_ent_n_values(ent);
809 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
810 be_emit_cstring("\t.ascii \"");
812 be_emit_cstring("\t.string \"");
816 for (i = 0; i < output_len; ++i) {
820 irn = get_compound_ent_value(ent, i);
821 c = (int) get_tarval_long(get_Const_tarval(irn));
824 case '"' : be_emit_cstring("\\\""); break;
825 case '\n': be_emit_cstring("\\n"); break;
826 case '\r': be_emit_cstring("\\r"); break;
827 case '\t': be_emit_cstring("\\t"); break;
828 case '\\': be_emit_cstring("\\\\"); break;
833 be_emit_irprintf("\\%03o", c);
837 be_emit_cstring("\"\n");
838 be_emit_write_line();
840 type = get_entity_type(ent);
841 type_size = get_type_size_bytes(type);
842 remaining_space = type_size - len;
843 assert(remaining_space >= 0);
844 if (remaining_space > 0) {
845 be_emit_irprintf("\t.space\t%d, 0\n", remaining_space);
849 static size_t emit_string_initializer(const ir_initializer_t *initializer)
853 len = initializer->compound.n_initializers;
854 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
855 be_emit_cstring("\t.ascii \"");
857 be_emit_cstring("\t.string \"");
861 for (i = 0; i < len; ++i) {
862 const ir_initializer_t *sub_initializer
863 = get_initializer_compound_value(initializer, i);
865 ir_tarval *tv = get_initializer_tarval(sub_initializer);
866 int c = get_tarval_long(tv);
869 case '"' : be_emit_cstring("\\\""); break;
870 case '\n': be_emit_cstring("\\n"); break;
871 case '\r': be_emit_cstring("\\r"); break;
872 case '\t': be_emit_cstring("\\t"); break;
873 case '\\': be_emit_cstring("\\\\"); break;
878 be_emit_irprintf("\\%03o", c);
882 be_emit_cstring("\"\n");
883 be_emit_write_line();
885 return initializer->compound.n_initializers;
888 typedef enum normal_or_bitfield_kind {
893 } normal_or_bitfield_kind;
896 normal_or_bitfield_kind kind;
901 unsigned char bf_val;
902 const ir_initializer_t *string;
904 } normal_or_bitfield;
906 static int is_type_variable_size(ir_type *type)
913 static size_t get_initializer_size(const ir_initializer_t *initializer,
916 switch (get_initializer_kind(initializer)) {
917 case IR_INITIALIZER_TARVAL:
918 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
919 return get_type_size_bytes(type);
920 case IR_INITIALIZER_CONST:
921 case IR_INITIALIZER_NULL:
922 return get_type_size_bytes(type);
923 case IR_INITIALIZER_COMPOUND:
924 if (!is_type_variable_size(type)) {
925 return get_type_size_bytes(type);
928 = get_initializer_compound_n_entries(initializer);
930 unsigned initializer_size = get_type_size_bytes(type);
931 for (i = 0; i < n_entries; ++i) {
932 ir_entity *entity = get_compound_member(type, i);
933 ir_type *type = get_entity_type(entity);
935 const ir_initializer_t *sub_initializer
936 = get_initializer_compound_value(initializer, i);
938 unsigned offset = get_entity_offset(entity);
939 unsigned size = get_initializer_size(sub_initializer, type);
941 if (offset + size > initializer_size) {
942 initializer_size = offset + size;
945 return initializer_size;
949 panic("found invalid initializer");
953 static normal_or_bitfield *glob_vals;
954 static size_t max_vals;
957 static void emit_bitfield(normal_or_bitfield *vals, size_t offset_bits,
958 const ir_initializer_t *initializer, ir_type *type)
960 static const size_t BITS_PER_BYTE = 8;
961 ir_mode *mode = get_type_mode(type);
962 ir_tarval *tv = NULL;
966 bool big_endian = be_get_backend_param()->byte_order_big_endian;
968 switch (get_initializer_kind(initializer)) {
969 case IR_INITIALIZER_NULL:
971 case IR_INITIALIZER_TARVAL:
972 tv = get_initializer_tarval_value(initializer);
974 case IR_INITIALIZER_CONST: {
975 ir_node *node = get_initializer_const_value(initializer);
976 if (!is_Const(node)) {
977 panic("bitfield initializer not a Const node");
979 tv = get_Const_tarval(node);
982 case IR_INITIALIZER_COMPOUND:
983 panic("bitfield initializer is compound");
986 panic("Couldn't get numeric value for bitfield initializer");
988 tv = tarval_convert_to(tv, get_type_mode(type));
990 value_len = get_type_size_bytes(get_primitive_base_type(type));
992 end = get_mode_size_bits(mode);
993 while (bit_offset < end) {
994 size_t src_offset = bit_offset / BITS_PER_BYTE;
995 size_t src_offset_bits = bit_offset % BITS_PER_BYTE;
996 size_t dst_offset = (bit_offset+offset_bits) / BITS_PER_BYTE;
997 size_t dst_offset_bits = (bit_offset+offset_bits) % BITS_PER_BYTE;
998 size_t src_bits_len = end-bit_offset;
999 size_t dst_bits_len = BITS_PER_BYTE-dst_offset_bits;
1000 unsigned char curr_bits;
1001 normal_or_bitfield *val;
1002 if (src_bits_len > dst_bits_len)
1003 src_bits_len = dst_bits_len;
1006 val = &vals[value_len - dst_offset - 1];
1008 val = &vals[dst_offset];
1011 assert((val-glob_vals) < (ptrdiff_t) max_vals);
1012 assert(val->kind == BITFIELD ||
1013 (val->kind == NORMAL && val->v.value == NULL));
1014 val->kind = BITFIELD;
1015 curr_bits = get_tarval_sub_bits(tv, src_offset);
1016 curr_bits = curr_bits >> src_offset_bits;
1017 if (src_offset_bits + src_bits_len > 8) {
1018 unsigned next_bits = get_tarval_sub_bits(tv, src_offset+1);
1019 curr_bits |= next_bits << (8 - src_offset_bits);
1021 curr_bits &= (1 << src_bits_len) - 1;
1022 val->v.bf_val |= curr_bits << dst_offset_bits;
1024 bit_offset += dst_bits_len;
1028 static void emit_ir_initializer(normal_or_bitfield *vals,
1029 const ir_initializer_t *initializer,
1032 assert((size_t) (vals - glob_vals) < max_vals);
1034 if (initializer_is_string_const(initializer)) {
1035 assert(vals->kind != BITFIELD);
1036 vals->kind = STRING;
1037 vals->v.string = initializer;
1041 switch (get_initializer_kind(initializer)) {
1042 case IR_INITIALIZER_NULL:
1044 case IR_INITIALIZER_TARVAL: {
1047 assert(vals->kind != BITFIELD);
1048 vals->kind = TARVAL;
1050 vals->v.tarval = get_initializer_tarval_value(initializer);
1051 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
1052 for (i = 1; i < get_type_size_bytes(type); ++i) {
1053 vals[i].kind = NORMAL;
1054 vals[i].type = NULL;
1055 vals[i].v.value = NULL;
1059 case IR_INITIALIZER_CONST: {
1062 assert(vals->kind != BITFIELD);
1063 vals->kind = NORMAL;
1065 vals->v.value = get_initializer_const_value(initializer);
1066 for (i = 1; i < get_type_size_bytes(type); ++i) {
1067 vals[i].kind = NORMAL;
1068 vals[i].type = NULL;
1069 vals[i].v.value = NULL;
1073 case IR_INITIALIZER_COMPOUND: {
1075 size_t n = get_initializer_compound_n_entries(initializer);
1077 if (is_Array_type(type)) {
1078 ir_type *element_type = get_array_element_type(type);
1079 size_t skip = get_type_size_bytes(element_type);
1080 size_t alignment = get_type_alignment_bytes(element_type);
1081 size_t misalign = skip % alignment;
1082 if (misalign != 0) {
1083 skip += alignment - misalign;
1086 for (i = 0; i < n; ++i) {
1087 ir_initializer_t *sub_initializer
1088 = get_initializer_compound_value(initializer, i);
1090 emit_ir_initializer(vals, sub_initializer, element_type);
1095 size_t n_members, i;
1096 assert(is_compound_type(type));
1097 n_members = get_compound_n_members(type);
1098 for (i = 0; i < n_members; ++i) {
1099 ir_entity *member = get_compound_member(type, i);
1100 size_t offset = get_entity_offset(member);
1101 ir_type *subtype = get_entity_type(member);
1102 ir_mode *mode = get_type_mode(subtype);
1103 ir_initializer_t *sub_initializer;
1105 assert(i < get_initializer_compound_n_entries(initializer));
1107 = get_initializer_compound_value(initializer, i);
1111 = get_entity_offset_bits_remainder(member);
1113 if (is_Primitive_type(subtype)
1114 && get_primitive_base_type(subtype) != NULL) {
1115 emit_bitfield(&vals[offset], offset_bits,
1116 sub_initializer, subtype);
1119 assert(offset_bits == 0);
1123 emit_ir_initializer(&vals[offset], sub_initializer, subtype);
1130 panic("invalid ir_initializer kind found");
1133 static void emit_tarval_data(ir_type *type, ir_tarval *tv)
1135 size_t size = get_type_size_bytes(type);
1137 /* this should be an x86 extended float */
1138 assert(be_get_backend_param()->byte_order_big_endian == 0);
1140 /* Beware: Mixed endian output! One little endian number emitted as
1141 * three longs. Each long initializer is written in big endian. */
1143 "\t.long\t0x%02x%02x%02x%02x\n"
1144 "\t.long\t0x%02x%02x%02x%02x\n"
1145 "\t.long\t0x%02x%02x%02x%02x\n",
1146 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
1147 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
1148 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
1149 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
1150 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
1151 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8)
1153 be_emit_write_line();
1154 } else if (size == 16) {
1155 if (be_get_backend_param()->byte_order_big_endian) {
1157 "\t.long\t0x%02x%02x%02x%02x\n"
1158 "\t.long\t0x%02x%02x%02x%02x\n"
1159 "\t.long\t0x%02x%02x%02x%02x\n"
1160 "\t.long\t0x%02x%02x%02x%02x\n",
1161 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 14),
1162 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
1163 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
1164 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
1165 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
1166 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
1167 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
1168 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0)
1171 /* Beware: Mixed endian output! One little endian number emitted as
1172 * three longs. Each long initializer is written in big endian. */
1174 "\t.long\t0x%02x%02x%02x%02x\n"
1175 "\t.long\t0x%02x%02x%02x%02x\n"
1176 "\t.long\t0x%02x%02x%02x%02x\n"
1177 "\t.long\t0x%02x%02x%02x%02x\n",
1178 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
1179 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
1180 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
1181 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
1182 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
1183 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
1184 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 14),
1185 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12)
1188 be_emit_write_line();
1192 emit_size_type(size);
1193 emit_arith_tarval(tv, size);
1195 be_emit_write_line();
1200 * Emit an atomic value.
1202 * @param env the gas output environment
1203 * @param init a node representing the atomic value (on the const code irg)
1205 static void emit_node_data(be_gas_decl_env_t *env, ir_node *init, ir_type *type)
1207 size_t size = get_type_size_bytes(type);
1208 if (size == 12 || size == 16) {
1210 if (!is_Const(init)) {
1211 panic("12/16byte initializers only support Const nodes yet");
1213 tv = get_Const_tarval(init);
1214 emit_tarval_data(type, tv);
1218 emit_size_type(size);
1219 emit_init_expression(env, init);
1221 be_emit_write_line();
1224 static void emit_initializer(be_gas_decl_env_t *env, const ir_entity *entity)
1226 const ir_initializer_t *initializer = entity->initializer;
1228 normal_or_bitfield *vals;
1232 if (initializer_is_string_const(initializer)) {
1233 emit_string_initializer(initializer);
1237 type = get_entity_type(entity);
1238 size = get_initializer_size(initializer, type);
1244 * In the worst case, every initializer allocates one byte.
1245 * Moreover, initializer might be big, do not allocate on stack.
1247 vals = XMALLOCNZ(normal_or_bitfield, size);
1254 emit_ir_initializer(vals, initializer, type);
1256 /* now write values sorted */
1257 for (k = 0; k < size; ) {
1259 normal_or_bitfield_kind kind = vals[k].kind;
1263 if (vals[k].v.value != NULL) {
1264 emit_node_data(env, vals[k].v.value, vals[k].type);
1265 elem_size = get_type_size_bytes(vals[k].type);
1271 emit_tarval_data(vals[k].type, vals[k].v.tarval);
1272 elem_size = get_type_size_bytes(vals[k].type);
1275 elem_size = emit_string_initializer(vals[k].v.string);
1278 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1279 be_emit_write_line();
1283 panic("internal compiler error (invalid normal_or_bitfield_kind");
1287 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1294 be_emit_irprintf("\t.space\t%d, 0\n", space);
1295 be_emit_write_line();
1301 static void emit_compound_graph_init(be_gas_decl_env_t *env,
1302 const ir_entity *ent)
1304 normal_or_bitfield *vals;
1306 unsigned k, last_ofs;
1308 if (entity_is_string_const(ent)) {
1309 emit_string_cst(ent);
1313 n = get_compound_ent_n_values(ent);
1315 /* Find the initializer size. Sorrily gcc support a nasty feature:
1316 The last field of a compound may be a flexible array. This allows
1317 initializers bigger than the type size. */
1318 last_ofs = get_type_size_bytes(get_entity_type(ent));
1319 for (i = 0; i < n; ++i) {
1320 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1321 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1322 ir_node *value = get_compound_ent_value(ent, i);
1323 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
1325 offset += (value_len + bits_remainder + 7) >> 3;
1327 if (offset > last_ofs) {
1333 * In the worst case, every initializer allocates one byte.
1334 * Moreover, initializer might be big, do not allocate on stack.
1336 vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1338 /* collect the values and store them at the offsets */
1339 for (i = 0; i < n; ++i) {
1340 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1341 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1342 ir_node *value = get_compound_ent_value(ent, i);
1343 int value_len = get_mode_size_bits(get_irn_mode(value));
1345 assert(offset_bits >= 0);
1347 if (offset_bits != 0 ||
1348 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1349 ir_tarval *tv = get_atomic_init_tv(value);
1350 unsigned char curr_bits, last_bits = 0;
1352 panic("Couldn't get numeric value for bitfield initializer '%s'",
1353 get_entity_ld_name(ent));
1355 /* normalize offset */
1356 offset += offset_bits >> 3;
1359 for (j = 0; value_len + offset_bits > 0; ++j) {
1360 assert(offset + j < last_ofs);
1361 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1362 vals[offset + j].kind = BITFIELD;
1363 curr_bits = get_tarval_sub_bits(tv, j);
1364 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1366 last_bits = curr_bits;
1371 assert(offset < last_ofs);
1372 assert(vals[offset].kind == NORMAL);
1373 for (i = 1; i < value_len / 8; ++i) {
1374 assert(vals[offset + i].v.value == NULL);
1376 vals[offset].v.value = value;
1380 /* now write them sorted */
1381 for (k = 0; k < last_ofs; ) {
1382 int space = 0, skip = 0;
1383 if (vals[k].kind == NORMAL) {
1384 if (vals[k].v.value != NULL) {
1385 emit_node_data(env, vals[k].v.value, vals[k].type);
1386 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1391 assert(vals[k].kind == BITFIELD);
1392 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1396 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1405 be_emit_irprintf("\t.space\t%d, 0\n", space);
1406 be_emit_write_line();
1412 static void emit_align(unsigned p2alignment)
1414 be_emit_irprintf("\t.p2align\t%u\n", log2_floor(p2alignment));
1415 be_emit_write_line();
1418 static unsigned get_effective_entity_alignment(const ir_entity *entity)
1420 unsigned alignment = get_entity_alignment(entity);
1421 if (alignment == 0) {
1422 ir_type *type = get_entity_type(entity);
1423 alignment = get_type_alignment_bytes(type);
1428 static void emit_common(const ir_entity *entity)
1430 unsigned size = get_type_size_bytes(get_entity_type(entity));
1431 unsigned alignment = get_effective_entity_alignment(entity);
1433 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
1437 switch (be_gas_object_file_format) {
1438 case OBJECT_FILE_FORMAT_MACH_O:
1439 be_emit_cstring("\t.comm ");
1440 be_gas_emit_entity(entity);
1441 be_emit_irprintf(",%u,%u\n", size, log2_floor(alignment));
1442 be_emit_write_line();
1444 case OBJECT_FILE_FORMAT_ELF:
1445 be_emit_cstring("\t.comm ");
1446 be_gas_emit_entity(entity);
1447 be_emit_irprintf(",%u,%u\n", size, alignment);
1448 be_emit_write_line();
1450 case OBJECT_FILE_FORMAT_COFF:
1451 be_emit_cstring("\t.comm ");
1452 be_gas_emit_entity(entity);
1453 be_emit_irprintf(",%u # %u\n", size, alignment);
1454 be_emit_write_line();
1457 panic("invalid object file format");
1460 static void emit_local_common(const ir_entity *entity)
1462 unsigned size = get_type_size_bytes(get_entity_type(entity));
1463 unsigned alignment = get_effective_entity_alignment(entity);
1465 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
1469 switch (be_gas_object_file_format) {
1470 case OBJECT_FILE_FORMAT_MACH_O:
1471 be_emit_cstring("\t.lcomm ");
1472 be_gas_emit_entity(entity);
1473 be_emit_irprintf(",%u,%u\n", size, log2_floor(alignment));
1474 be_emit_write_line();
1476 case OBJECT_FILE_FORMAT_ELF:
1477 be_emit_cstring("\t.local ");
1478 be_gas_emit_entity(entity);
1479 be_emit_cstring("\n");
1480 be_emit_write_line();
1481 be_emit_cstring("\t.comm ");
1482 be_gas_emit_entity(entity);
1483 be_emit_irprintf(",%u,%u\n", size, alignment);
1484 be_emit_write_line();
1486 case OBJECT_FILE_FORMAT_COFF:
1487 be_emit_cstring("\t.lcomm ");
1488 be_gas_emit_entity(entity);
1489 be_emit_irprintf(",%u # %u\n", size, alignment);
1490 be_emit_write_line();
1493 panic("invalid object file format");
1496 static void emit_indirect_symbol(const ir_entity *entity, be_gas_section_t section)
1498 /* we can only do PIC code on macho so far */
1499 assert(be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O);
1501 be_gas_emit_entity(entity);
1502 be_emit_cstring(":\n");
1503 be_emit_write_line();
1504 be_emit_cstring("\t.indirect_symbol ");
1505 be_emit_ident(get_entity_ident(entity));
1507 be_emit_write_line();
1508 if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1509 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1510 be_emit_write_line();
1512 assert(section == GAS_SECTION_PIC_SYMBOLS);
1513 be_emit_cstring("\t.long 0\n");
1514 be_emit_write_line();
1518 char const *be_gas_get_private_prefix(void)
1520 return be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O ? "L" : ".L";
1523 void be_gas_emit_entity(const ir_entity *entity)
1525 if (entity->type == firm_code_type) {
1526 ir_label_t label = get_entity_label(entity);
1527 be_emit_irprintf("%s_%lu", be_gas_get_private_prefix(), label);
1531 if (get_entity_visibility(entity) == ir_visibility_private) {
1532 be_emit_string(be_gas_get_private_prefix());
1534 be_emit_ident(get_entity_ld_ident(entity));
1537 void be_gas_emit_block_name(const ir_node *block)
1539 if (has_Block_entity(block)) {
1540 ir_entity *entity = get_Block_entity(block);
1541 be_gas_emit_entity(entity);
1543 be_emit_irprintf("%s%ld", be_gas_get_private_prefix(), get_irn_node_nr(block));
1548 * Dump a global entity.
1550 * @param env the gas output environment
1551 * @param ent the entity to be dumped
1553 static void emit_global(be_gas_decl_env_t *env, const ir_entity *entity)
1555 ir_type *type = get_entity_type(entity);
1556 ident *ld_ident = get_entity_ld_ident(entity);
1557 unsigned alignment = get_effective_entity_alignment(entity);
1558 be_gas_section_t section = determine_section(env, entity);
1559 ir_visibility visibility = get_entity_visibility(entity);
1560 ir_linkage linkage = get_entity_linkage(entity);
1562 /* block labels are already emittet in the code */
1563 if (type == firm_code_type)
1566 /* we already emitted all methods. Except for the trampolines which
1567 * the assembler/linker generates */
1568 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1569 /* functions with graph are already emitted with
1570 * be_gas_emit_function_prolog */
1571 if (get_entity_irg(entity) == NULL) {
1572 emit_visibility(entity);
1577 be_dbg_variable(entity);
1579 if (section == GAS_SECTION_BSS) {
1580 switch (visibility) {
1581 case ir_visibility_local:
1582 case ir_visibility_private:
1583 emit_local_common(entity);
1585 case ir_visibility_default:
1586 if (linkage & IR_LINKAGE_MERGE) {
1587 emit_common(entity);
1591 case ir_visibility_external:
1592 if (linkage & IR_LINKAGE_MERGE)
1593 panic("merge link semantic not supported for extern entities");
1598 emit_visibility(entity);
1599 if (visibility == ir_visibility_external) {
1600 /* nothing to do for externally defined values */
1604 if (!is_po2(alignment))
1605 panic("alignment not a power of 2");
1607 emit_section(section, entity);
1609 if (section == GAS_SECTION_PIC_TRAMPOLINES
1610 || section == GAS_SECTION_PIC_SYMBOLS) {
1611 emit_indirect_symbol(entity, section);
1616 if (alignment > 1) {
1617 emit_align(alignment);
1619 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF
1620 && be_gas_emit_types
1621 && visibility != ir_visibility_private) {
1622 be_emit_cstring("\t.type\t");
1623 be_gas_emit_entity(entity);
1624 be_emit_cstring(", ");
1625 be_emit_char(be_gas_elf_type_char);
1626 be_emit_cstring("object\n\t.size\t");\
1627 be_gas_emit_entity(entity);
1628 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1631 if (get_id_str(ld_ident)[0] != '\0') {
1632 be_gas_emit_entity(entity);
1633 be_emit_cstring(":\n");
1634 be_emit_write_line();
1637 if (entity_is_null(entity)) {
1638 unsigned size = get_type_size_bytes(type);
1640 be_emit_irprintf("\t.space %u, 0\n", get_type_size_bytes(type));
1641 be_emit_write_line();
1643 } else if (entity_has_compound_ent_values(entity)) {
1644 emit_compound_graph_init(env, entity);
1646 assert(entity->initializer != NULL);
1647 emit_initializer(env, entity);
1652 * Dumps declarations of global variables and the initialization code.
1654 * @param gt a global like type, either the global or the TLS one
1655 * @param env an environment
1657 static void be_gas_emit_globals(ir_type *gt, be_gas_decl_env_t *env)
1659 size_t i, n = get_compound_n_members(gt);
1661 for (i = 0; i < n; i++) {
1662 ir_entity *ent = get_compound_member(gt, i);
1663 emit_global(env, ent);
1667 /* Generate all entities. */
1668 static void emit_global_decls(const be_main_env_t *main_env)
1670 be_gas_decl_env_t env;
1671 memset(&env, 0, sizeof(env));
1673 /* dump global type */
1674 env.main_env = main_env;
1675 env.section = (be_gas_section_t) -1;
1677 be_gas_emit_globals(get_glob_type(), &env);
1678 be_gas_emit_globals(get_tls_type(), &env);
1679 be_gas_emit_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env);
1680 be_gas_emit_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env);
1681 be_gas_emit_globals(main_env->pic_symbols_type, &env);
1682 be_gas_emit_globals(main_env->pic_trampolines_type, &env);
1685 * ".subsections_via_symbols marks object files which are OK to divide
1686 * their section contents into individual blocks".
1687 * From my understanding this means no label points in the middle of an
1688 * object which we want to address as a whole. Firm code should be fine
1691 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
1692 be_emit_cstring("\t.subsections_via_symbols\n");
1693 be_emit_write_line();
1697 void emit_jump_table(const ir_node *node, long default_pn, ir_entity *entity,
1698 get_cfop_target_func get_cfop_target)
1700 long switch_max = LONG_MIN;
1701 ir_node *default_block = NULL;
1702 unsigned long length;
1703 const ir_edge_t *edge;
1707 /* go over all proj's and collect them */
1708 foreach_out_edge(node, edge) {
1709 ir_node *proj = get_edge_src_irn(edge);
1710 long pn = get_Proj_proj(proj);
1712 /* check for default proj */
1713 if (pn == default_pn) {
1714 assert(default_block == NULL); /* more than 1 default_pn? */
1715 default_block = get_cfop_target(proj);
1717 switch_max = pn > switch_max ? pn : switch_max;
1720 assert(switch_max > LONG_MIN);
1722 length = (unsigned long) switch_max + 1;
1723 /* the 16000 isn't a real limit of the architecture. But should protect us
1724 * from seamingly endless compiler runs */
1725 if (length > 16000) {
1726 /* switch lowerer should have broken this monster to pieces... */
1727 panic("too large switch encountered");
1730 table = XMALLOCNZ(ir_node*, length);
1731 foreach_out_edge(node, edge) {
1732 ir_node *proj = get_edge_src_irn(edge);
1733 long pn = get_Proj_proj(proj);
1734 if (pn == default_pn)
1737 table[pn] = get_cfop_target(proj);
1741 be_gas_emit_switch_section(GAS_SECTION_RODATA);
1742 be_emit_cstring("\t.align 4\n");
1743 be_gas_emit_entity(entity);
1744 be_emit_cstring(":\n");
1745 for (i = 0; i < length; ++i) {
1746 ir_node *block = table[i];
1748 block = default_block;
1749 be_emit_cstring("\t.long ");
1750 be_gas_emit_block_name(block);
1752 be_emit_write_line();
1754 be_gas_emit_switch_section(GAS_SECTION_TEXT);
1759 static void emit_global_asms(void)
1761 size_t n = get_irp_n_asms();
1764 be_gas_emit_switch_section(GAS_SECTION_TEXT);
1765 for (i = 0; i < n; ++i) {
1766 ident *asmtext = get_irp_asm(i);
1768 be_emit_cstring("#APP\n");
1769 be_emit_write_line();
1770 be_emit_ident(asmtext);
1772 be_emit_write_line();
1773 be_emit_cstring("#NO_APP\n");
1774 be_emit_write_line();
1778 void be_gas_begin_compilation_unit(const be_main_env_t *env)
1781 be_dbg_unit_begin(env->cup_name);
1787 void be_gas_end_compilation_unit(const be_main_env_t *env)
1789 emit_global_decls(env);