2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Dumps global variables and constants as gas assembler.
23 * @author Christian Wuerdig, Matthias Braun
44 #include "beemitter.h"
45 #include "be_dbgout.h"
47 /** by default, we generate assembler code for the Linux gas */
48 object_file_format_t be_gas_object_file_format = OBJECT_FILE_FORMAT_ELF;
49 bool be_gas_emit_types = true;
50 char be_gas_elf_type_char = '@';
52 static be_gas_section_t current_section = (be_gas_section_t) -1;
55 * An environment containing all needed dumper data.
56 * Currently we create the file completely in memory first, then
57 * write it to the disk. This is an artifact from the old C-generating backend
58 * and even there NOT needed. So we might change it in the future.
60 typedef struct _be_gas_decl_env {
61 be_gas_section_t section;
62 const be_main_env_t *main_env;
65 static void emit_section_macho(be_gas_section_t section)
67 be_gas_section_t base = section & GAS_SECTION_TYPE_MASK;
68 be_gas_section_t flags = section & ~GAS_SECTION_TYPE_MASK;
71 if (current_section == section)
73 current_section = section;
78 case GAS_SECTION_TEXT: name = "text"; break;
79 case GAS_SECTION_DATA: name = "data"; break;
80 case GAS_SECTION_RODATA: name = "const"; break;
81 case GAS_SECTION_BSS: name = "data"; break;
82 case GAS_SECTION_CONSTRUCTORS: name = "mod_init_func"; break;
83 case GAS_SECTION_DESTRUCTORS: name = "mod_term_func"; break;
84 case GAS_SECTION_PIC_TRAMPOLINES: name = "section\t__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5"; break;
85 case GAS_SECTION_PIC_SYMBOLS: name = "section\t__IMPORT,__pointers,non_lazy_symbol_pointers"; break;
86 case GAS_SECTION_CSTRING: name = "cstring"; break;
87 default: panic("unsupported scetion type 0x%X", section);
89 be_emit_irprintf("\t.%s\n", name);
91 } else if (flags & GAS_SECTION_FLAG_COMDAT) {
93 case GAS_SECTION_TEXT: name = "section __TEXT,__textcoal_nt,coalesced,pure_instructions"; break;
95 case GAS_SECTION_DATA: name = "section __DATA,__datacoal_nt,coalesced"; break;
96 case GAS_SECTION_RODATA: name = "section __TEXT,__const_coal,coalesced"; break;
97 case GAS_SECTION_CSTRING: name = "section __TEXT,__const_coal,coalesced"; break;
98 default: panic("unsupported scetion type 0x%X", section);
101 panic("unsupported section type 0x%X\n", section);
105 static void emit_section(be_gas_section_t section, const ir_entity *entity)
107 be_gas_section_t base = section & GAS_SECTION_TYPE_MASK;
108 be_gas_section_t flags = section & ~GAS_SECTION_TYPE_MASK;
109 static const char *const basename[] = {
110 "text", "data", "rodata", "bss", "ctors", "dtors"
112 static const char *const type[] = {
113 "progbits", "progbits", "progbits", "nobits", "init_array", "fini_array"
116 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
117 emit_section_macho(section);
121 if (current_section == section && !(section & GAS_SECTION_FLAG_COMDAT))
123 current_section = section;
128 case GAS_SECTION_TEXT:
129 be_emit_cstring("\t.text\n");
130 be_emit_write_line();
132 case GAS_SECTION_DATA:
133 be_emit_cstring("\t.data\n");
134 be_emit_write_line();
136 case GAS_SECTION_RODATA:
137 be_emit_cstring("\t.section\t.rodata\n");
138 be_emit_write_line();
140 case GAS_SECTION_BSS:
141 be_emit_cstring("\t.bss\n");
142 be_emit_write_line();
149 assert(base < sizeof(basename)/sizeof(basename[0]));
150 be_emit_cstring("\t.section\t.");
152 if (flags & GAS_SECTION_FLAG_TLS)
154 be_emit_string(basename[base]);
155 if (flags & GAS_SECTION_FLAG_COMDAT) {
157 be_gas_emit_entity(entity);
161 be_emit_cstring(",\"");
162 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_COFF)
164 if (base == GAS_SECTION_TEXT)
166 if (base != GAS_SECTION_RODATA && base != GAS_SECTION_TEXT)
168 if (flags & GAS_SECTION_FLAG_TLS)
170 if (flags & GAS_SECTION_FLAG_COMDAT)
172 be_emit_cstring("\",");
173 be_emit_char(be_gas_elf_type_char);
174 be_emit_string(type[base]);
176 if (flags & GAS_SECTION_FLAG_COMDAT) {
178 be_gas_emit_entity(entity);
179 be_emit_cstring(",comdat");
182 be_emit_write_line();
185 void be_gas_emit_switch_section(be_gas_section_t section)
187 /* you have to produce a switch_section call with entity manually
188 * for comdat sections */
189 assert( !(section & GAS_SECTION_FLAG_COMDAT));
191 emit_section(section, NULL);
194 static tarval *get_initializer_tarval(const ir_initializer_t *initializer)
196 if (initializer->kind == IR_INITIALIZER_TARVAL)
197 return initializer->tarval.value;
198 if (initializer->kind == IR_INITIALIZER_CONST) {
199 ir_node *node = initializer->consti.value;
200 if (is_Const(node)) {
201 return get_Const_tarval(node);
204 return get_tarval_undefined();
207 static int initializer_is_string_const(const ir_initializer_t *initializer)
210 int found_printable = 0;
212 if (initializer->kind != IR_INITIALIZER_COMPOUND)
215 len = initializer->compound.n_initializers;
218 for (i = 0; i < len; ++i) {
222 ir_initializer_t *sub_initializer
223 = initializer->compound.initializers[i];
225 tv = get_initializer_tarval(sub_initializer);
226 if (!tarval_is_constant(tv))
229 mode = get_tarval_mode(tv);
230 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
233 c = get_tarval_long(tv);
234 if (isgraph(c) || isspace(c))
239 if (i == len - 1 && c != '\0')
243 return found_printable;
246 static bool initializer_is_null(const ir_initializer_t *initializer)
248 switch (initializer->kind) {
249 case IR_INITIALIZER_NULL:
251 case IR_INITIALIZER_TARVAL: {
252 tarval *tv = initializer->tarval.value;
253 return tarval_is_null(tv);
255 case IR_INITIALIZER_CONST: {
256 ir_node *value = initializer->consti.value;
257 if (!is_Const(value))
259 return is_Const_null(value);
261 case IR_INITIALIZER_COMPOUND: {
263 for (i = 0; i < initializer->compound.n_initializers; ++i) {
264 ir_initializer_t *subinitializer
265 = initializer->compound.initializers[i];
266 if (!initializer_is_null(subinitializer))
272 panic("invalid initializer in initializer_is_null");
276 * Determine if an entity is a string constant
277 * @param ent The entity
278 * @return 1 if it is a string constant, 0 otherwise
280 static int entity_is_string_const(const ir_entity *ent)
282 ir_type *type, *element_type;
286 type = get_entity_type(ent);
288 /* if it's an array */
289 if (!is_Array_type(type))
292 element_type = get_array_element_type(type);
294 /* and the array's element type is primitive */
295 if (!is_Primitive_type(element_type))
298 /* and the mode of the element type is an int of
299 * the same size as the byte mode */
300 mode = get_type_mode(element_type);
301 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
304 if (ent->initializer != NULL) {
305 return initializer_is_string_const(ent->initializer);
306 } else if (entity_has_compound_ent_values(ent)) {
307 int found_printable = 0;
308 /* if it contains only printable chars and a 0 at the end */
309 n = get_compound_ent_n_values(ent);
310 for (i = 0; i < n; ++i) {
311 ir_node *irn = get_compound_ent_value(ent, i);
315 c = (int) get_tarval_long(get_Const_tarval(irn));
317 if (isgraph(c) || isspace(c))
322 if (i == n - 1 && c != '\0')
325 return found_printable;
331 static bool entity_is_null(const ir_entity *entity)
333 if (entity->initializer != NULL) {
334 return initializer_is_null(entity->initializer);
335 } else if (entity_has_compound_ent_values(entity)) {
336 /* I'm too lazy to implement this case as compound graph paths will be
337 * remove anyway in the future */
340 /* uninitialized, NULL is fine */
344 static bool is_comdat(const ir_entity *entity)
346 ir_linkage linkage = get_entity_linkage(entity);
347 return (linkage & IR_LINKAGE_MERGE)
348 && (linkage & IR_LINKAGE_GARBAGE_COLLECT);
351 static be_gas_section_t determine_basic_section(const ir_entity *entity)
355 if (is_method_entity(entity))
356 return GAS_SECTION_TEXT;
358 linkage = get_entity_linkage(entity);
359 if (linkage & IR_LINKAGE_CONSTANT) {
360 /* mach-o is the only one with a cstring section */
361 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
362 && entity_is_string_const(entity))
363 return GAS_SECTION_CSTRING;
365 return GAS_SECTION_RODATA;
367 if (entity_is_null(entity))
368 return GAS_SECTION_BSS;
370 return GAS_SECTION_DATA;
373 static be_gas_section_t determine_section(be_gas_decl_env_t *env,
374 const ir_entity *entity)
376 ir_type *owner = get_entity_owner(entity);
378 if (owner == get_segment_type(IR_SEGMENT_GLOBAL)) {
379 be_gas_section_t section = determine_basic_section(entity);
380 if (is_comdat(entity))
381 section |= GAS_SECTION_FLAG_COMDAT;
383 } else if (env != NULL && owner == env->main_env->pic_symbols_type) {
384 return GAS_SECTION_PIC_SYMBOLS;
385 } else if (env != NULL && owner == env->main_env->pic_trampolines_type) {
386 return GAS_SECTION_PIC_TRAMPOLINES;
387 } else if (owner == get_segment_type(IR_SEGMENT_CONSTRUCTORS)) {
388 return GAS_SECTION_CONSTRUCTORS;
389 } else if (owner == get_segment_type(IR_SEGMENT_DESTRUCTORS)) {
390 return GAS_SECTION_DESTRUCTORS;
391 } else if (owner == get_segment_type(IR_SEGMENT_THREAD_LOCAL)) {
392 be_gas_section_t section = determine_basic_section(entity);
393 if (is_comdat(entity))
394 section |= GAS_SECTION_FLAG_COMDAT;
396 return section | GAS_SECTION_FLAG_TLS;
399 /* the java frontend keeps some functions inside classes */
400 if (is_Class_type(owner)) {
401 return determine_basic_section(entity);
404 panic("Couldn't determine section for %+F?!?", entity);
407 static void emit_weak(const ir_entity *entity)
409 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
410 be_emit_cstring("\t.weak_reference ");
412 be_emit_cstring("\t.weak ");
414 be_gas_emit_entity(entity);
416 be_emit_write_line();
419 static void emit_visibility(const ir_entity *entity)
421 ir_linkage linkage = get_entity_linkage(entity);
423 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
425 /* Note: .weak seems to imply .globl so no need to output .globl */
426 } else if (get_entity_visibility(entity) == ir_visibility_default) {
427 be_emit_cstring(".globl ");
428 be_gas_emit_entity(entity);
430 be_emit_write_line();
433 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
434 && (linkage & IR_LINKAGE_HIDDEN_USER)) {
435 be_emit_cstring("\t.no_dead_strip ");
436 be_gas_emit_entity(entity);
438 be_emit_write_line();
442 void be_gas_emit_function_prolog(const ir_entity *entity, unsigned po2alignment)
444 be_gas_section_t section = determine_section(NULL, entity);
445 emit_section(section, entity);
447 /* write the begin line (makes the life easier for scripts parsing the
449 be_emit_write_line();
450 be_emit_cstring("# -- Begin ");
451 be_gas_emit_entity(entity);
453 be_emit_write_line();
455 if (po2alignment > 0) {
456 const char *fill_byte = "";
457 unsigned maximum_skip = (1 << po2alignment) - 1;
458 /* gcc fills space between function with 0x90... */
459 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
462 be_emit_cstring("\t.p2align ");
463 be_emit_irprintf("%u,%s,%u\n", po2alignment, fill_byte, maximum_skip);
464 be_emit_write_line();
466 emit_visibility(entity);
468 switch (be_gas_object_file_format) {
469 case OBJECT_FILE_FORMAT_ELF:
470 be_emit_cstring("\t.type\t");
471 be_gas_emit_entity(entity);
472 be_emit_cstring(", ");
473 be_emit_char(be_gas_elf_type_char);
474 be_emit_cstring("function\n");
475 be_emit_write_line();
477 case OBJECT_FILE_FORMAT_COFF:
478 be_emit_cstring("\t.def\t");
479 be_gas_emit_entity(entity);
480 be_emit_cstring(";");
481 if (get_entity_visibility(entity) == ir_visibility_local) {
482 be_emit_cstring("\t.scl\t3;");
484 be_emit_cstring("\t.scl\t2;");
486 be_emit_cstring("\t.type\t32;\t.endef\n");
487 be_emit_write_line();
489 case OBJECT_FILE_FORMAT_MACH_O:
492 be_gas_emit_entity(entity);
493 be_emit_cstring(":\n");
494 be_emit_write_line();
497 void be_gas_emit_function_epilog(const ir_entity *entity)
499 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF) {
500 be_emit_cstring("\t.size\t");
501 be_gas_emit_entity(entity);
502 be_emit_cstring(", .-");
503 be_gas_emit_entity(entity);
505 be_emit_write_line();
508 be_emit_cstring("# -- End ");
509 be_gas_emit_entity(entity);
511 be_emit_write_line();
514 /************************************************************************/
519 * @param tv the tarval
520 * @param bytes the width of the tarvals value in bytes
522 static void emit_arith_tarval(tarval *tv, int bytes)
526 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
530 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
534 be_emit_irprintf("0x%02x%02x%02x%02x",
535 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
539 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
540 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
541 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
545 /* Beware: Mixed endian output! One little endian number emitted as
546 * three longs. Each long initializer is written in big endian. */
548 "\t.long\t0x%02x%02x%02x%02x\n"
549 "\t.long\t0x%02x%02x%02x%02x\n"
550 "\t.long\t0x%02x%02x%02x%02x",
551 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
552 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
553 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
554 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
555 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
556 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8)
561 /* Beware: Mixed endian output! One little endian number emitted as
562 * three longs. Each long initializer is written in big endian. */
564 "\t.long\t0x%02x%02x%02x%02x\n"
565 "\t.long\t0x%02x%02x%02x%02x\n"
566 "\t.long\t0x%02x%02x%02x%02x\n"
567 "\t.long\t0x%02x%02x%02x%02x",
568 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
569 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
570 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
571 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
572 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
573 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
574 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 14),
575 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12)
580 panic("Can't dump a tarval with %d bytes", bytes);
584 * Return the label prefix for labeled instructions.
586 const char *be_gas_insn_label_prefix(void)
592 * Return the tarval of an atomic initializer.
594 * @param init a node representing the initializer (on the const code irg)
598 static tarval *get_atomic_init_tv(ir_node *init)
601 ir_mode *mode = get_irn_mode(init);
603 switch (get_irn_opcode(init)) {
606 init = get_Cast_op(init);
610 init = get_Conv_op(init);
614 return get_Const_tarval(init);
617 switch (get_SymConst_kind(init)) {
618 case symconst_type_size:
619 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
621 case symconst_type_align:
622 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
624 case symconst_ofs_ent:
625 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
627 case symconst_enum_const:
628 return get_enumeration_value(get_SymConst_enum(init));
641 * Dump an atomic value.
643 * @param env the gas output environment
644 * @param init a node representing the atomic value (on the const code irg)
646 static void do_emit_atomic_init(be_gas_decl_env_t *env, ir_node *init)
648 ir_mode *mode = get_irn_mode(init);
649 int bytes = get_mode_size_bytes(mode);
653 init = skip_Id(init);
655 switch (get_irn_opcode(init)) {
657 do_emit_atomic_init(env, get_Cast_op(init));
661 do_emit_atomic_init(env, get_Conv_op(init));
665 tv = get_Const_tarval(init);
667 /* it's a arithmetic value */
668 emit_arith_tarval(tv, bytes);
672 switch (get_SymConst_kind(init)) {
673 case symconst_addr_ent:
674 ent = get_SymConst_entity(init);
675 be_gas_emit_entity(ent);
678 case symconst_ofs_ent:
679 ent = get_SymConst_entity(init);
680 be_emit_irprintf("%d", get_entity_offset(ent));
683 case symconst_type_size:
684 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
687 case symconst_type_align:
688 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
691 case symconst_enum_const:
692 tv = get_enumeration_value(get_SymConst_enum(init));
693 emit_arith_tarval(tv, bytes);
697 assert(!"emit_atomic_init(): don't know how to init from this SymConst");
702 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
703 panic("Constant must be int or pointer for '+' to work");
705 do_emit_atomic_init(env, get_Add_left(init));
706 be_emit_cstring(" + ");
707 do_emit_atomic_init(env, get_Add_right(init));
711 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
712 panic("Constant must be int or pointer for '-' to work");
714 do_emit_atomic_init(env, get_Sub_left(init));
715 be_emit_cstring(" - ");
716 do_emit_atomic_init(env, get_Sub_right(init));
720 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
721 panic("Constant must be int or pointer for '*' to work");
723 do_emit_atomic_init(env, get_Mul_left(init));
724 be_emit_cstring(" * ");
725 do_emit_atomic_init(env, get_Mul_right(init));
729 be_emit_cstring("0");
733 panic("emit_atomic_init(): unsupported IR-node %+F", init);
738 * Dumps the type for given size (.byte, .long, ...)
740 * @param size the size in bytes
742 static void emit_size_type(size_t size)
746 be_emit_cstring("\t.byte\t");
750 be_emit_cstring("\t.short\t");
754 be_emit_cstring("\t.long\t");
758 be_emit_cstring("\t.quad\t");
763 case 16: /* Note: .octa does not work on mac */
764 /* handled in arith */
768 panic("Try to dump a type with %u bytes", (unsigned)size);
773 * Emit an atomic value.
775 * @param env the gas output environment
776 * @param init a node representing the atomic value (on the const code irg)
778 static void emit_atomic_init(be_gas_decl_env_t *env, ir_node *init)
780 ir_mode *mode = get_irn_mode(init);
781 int bytes = get_mode_size_bytes(mode);
783 emit_size_type(bytes);
784 do_emit_atomic_init(env, init);
786 be_emit_write_line();
789 /************************************************************************/
790 /* Routines to dump global variables */
791 /************************************************************************/
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("\\%o", 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\n", remaining_space);
849 static void 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 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("\\%o", c);
882 be_emit_cstring("\"\n");
883 be_emit_write_line();
886 enum normal_or_bitfield_kind {
893 enum normal_or_bitfield_kind kind;
897 unsigned char bf_val;
899 } normal_or_bitfield;
901 static int is_type_variable_size(ir_type *type)
908 static size_t get_initializer_size(const ir_initializer_t *initializer,
911 switch (get_initializer_kind(initializer)) {
912 case IR_INITIALIZER_TARVAL:
913 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
914 return get_type_size_bytes(type);
915 case IR_INITIALIZER_CONST:
916 case IR_INITIALIZER_NULL:
917 return get_type_size_bytes(type);
918 case IR_INITIALIZER_COMPOUND:
919 if (!is_type_variable_size(type)) {
920 return get_type_size_bytes(type);
923 = get_initializer_compound_n_entries(initializer);
925 unsigned initializer_size = get_type_size_bytes(type);
926 for (i = 0; i < n_entries; ++i) {
927 ir_entity *entity = get_compound_member(type, i);
928 ir_type *type = get_entity_type(entity);
930 const ir_initializer_t *sub_initializer
931 = get_initializer_compound_value(initializer, i);
933 unsigned offset = get_entity_offset(entity);
934 unsigned size = get_initializer_size(sub_initializer, type);
936 if (offset + size > initializer_size) {
937 initializer_size = offset + size;
940 return initializer_size;
944 panic("found invalid initializer");
948 static normal_or_bitfield *glob_vals;
949 static size_t max_vals;
952 static void emit_bitfield(normal_or_bitfield *vals, size_t offset_bits,
953 const ir_initializer_t *initializer, ir_type *type)
955 unsigned char last_bits = 0;
956 ir_mode *mode = get_type_mode(type);
958 unsigned char curr_bits;
962 switch (get_initializer_kind(initializer)) {
963 case IR_INITIALIZER_NULL:
965 case IR_INITIALIZER_TARVAL:
966 tv = get_initializer_tarval_value(initializer);
968 case IR_INITIALIZER_CONST: {
969 ir_node *node = get_initializer_const_value(initializer);
970 if (!is_Const(node)) {
971 panic("bitfield initializer not a Const node");
973 tv = get_Const_tarval(node);
976 case IR_INITIALIZER_COMPOUND:
977 panic("bitfield initializer is compound");
980 panic("Couldn't get numeric value for bitfield initializer");
982 tv = tarval_convert_to(tv, get_type_mode(type));
984 /* normalize offset */
985 vals += offset_bits >> 3;
987 value_len = get_mode_size_bits(mode);
989 /* combine bits with existing bits */
990 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
991 assert((size_t) (vals - glob_vals) + j < max_vals);
992 assert(vals[j].kind == BITFIELD ||
993 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
994 vals[j].kind = BITFIELD;
995 curr_bits = get_tarval_sub_bits(tv, j);
997 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
999 last_bits = curr_bits;
1003 static void emit_ir_initializer(normal_or_bitfield *vals,
1004 const ir_initializer_t *initializer,
1007 assert((size_t) (vals - glob_vals) < max_vals);
1009 switch (get_initializer_kind(initializer)) {
1010 case IR_INITIALIZER_NULL:
1012 case IR_INITIALIZER_TARVAL: {
1015 assert(vals->kind != BITFIELD);
1016 vals->kind = TARVAL;
1017 vals->v.tarval = get_initializer_tarval_value(initializer);
1018 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
1019 for (i = 1; i < get_type_size_bytes(type); ++i) {
1020 vals[i].kind = NORMAL;
1021 vals[i].v.value = NULL;
1025 case IR_INITIALIZER_CONST: {
1028 assert(vals->kind != BITFIELD);
1029 vals->kind = NORMAL;
1030 vals->v.value = get_initializer_const_value(initializer);
1031 for (i = 1; i < get_type_size_bytes(type); ++i) {
1032 vals[i].kind = NORMAL;
1033 vals[i].v.value = NULL;
1037 case IR_INITIALIZER_COMPOUND: {
1039 size_t n = get_initializer_compound_n_entries(initializer);
1041 if (is_Array_type(type)) {
1042 ir_type *element_type = get_array_element_type(type);
1043 size_t skip = get_type_size_bytes(element_type);
1044 size_t alignment = get_type_alignment_bytes(element_type);
1045 size_t misalign = skip % alignment;
1046 if (misalign != 0) {
1047 skip += alignment - misalign;
1050 for (i = 0; i < n; ++i) {
1051 ir_initializer_t *sub_initializer
1052 = get_initializer_compound_value(initializer, i);
1054 emit_ir_initializer(vals, sub_initializer, element_type);
1059 size_t n_members, i;
1060 assert(is_compound_type(type));
1061 n_members = get_compound_n_members(type);
1062 for (i = 0; i < n_members; ++i) {
1063 ir_entity *member = get_compound_member(type, i);
1064 size_t offset = get_entity_offset(member);
1065 ir_type *subtype = get_entity_type(member);
1066 ir_mode *mode = get_type_mode(subtype);
1067 ir_initializer_t *sub_initializer;
1069 assert(i < get_initializer_compound_n_entries(initializer));
1071 = get_initializer_compound_value(initializer, i);
1075 = get_entity_offset_bits_remainder(member);
1076 size_t value_len = get_mode_size_bits(mode);
1078 if (offset_bits != 0 ||
1079 (value_len != 8 && value_len != 16 && value_len != 32
1080 && value_len != 64)) {
1081 emit_bitfield(&vals[offset], offset_bits,
1082 sub_initializer, subtype);
1087 emit_ir_initializer(&vals[offset], sub_initializer, subtype);
1094 panic("invalid ir_initializer kind found");
1097 static void emit_initializer(be_gas_decl_env_t *env, const ir_entity *entity)
1099 const ir_initializer_t *initializer = entity->initializer;
1101 normal_or_bitfield *vals;
1105 if (initializer_is_string_const(initializer)) {
1106 emit_string_initializer(initializer);
1110 type = get_entity_type(entity);
1111 size = get_initializer_size(initializer, type);
1117 * In the worst case, every initializer allocates one byte.
1118 * Moreover, initializer might be big, do not allocate on stack.
1120 vals = XMALLOCNZ(normal_or_bitfield, size);
1127 emit_ir_initializer(vals, initializer, type);
1129 /* now write values sorted */
1130 for (k = 0; k < size; ) {
1133 if (vals[k].kind == NORMAL) {
1134 if (vals[k].v.value != NULL) {
1135 emit_atomic_init(env, vals[k].v.value);
1136 elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
1140 } else if (vals[k].kind == TARVAL) {
1141 tarval *tv = vals[k].v.tarval;
1142 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
1147 emit_size_type(size);
1148 emit_arith_tarval(tv, size);
1150 be_emit_write_line();
1152 assert(vals[k].kind == BITFIELD);
1153 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1154 be_emit_write_line();
1158 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1165 be_emit_irprintf("\t.space\t%d\n", space);
1166 be_emit_write_line();
1172 static void emit_compound_graph_init(be_gas_decl_env_t *env,
1173 const ir_entity *ent)
1175 normal_or_bitfield *vals;
1177 unsigned k, last_ofs;
1179 if (entity_is_string_const(ent)) {
1180 emit_string_cst(ent);
1184 n = get_compound_ent_n_values(ent);
1186 /* Find the initializer size. Sorrily gcc support a nasty feature:
1187 The last field of a compound may be a flexible array. This allows
1188 initializers bigger than the type size. */
1189 last_ofs = get_type_size_bytes(get_entity_type(ent));
1190 for (i = 0; i < n; ++i) {
1191 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1192 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1193 ir_node *value = get_compound_ent_value(ent, i);
1194 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
1196 offset += (value_len + bits_remainder + 7) >> 3;
1198 if (offset > last_ofs) {
1204 * In the worst case, every initializer allocates one byte.
1205 * Moreover, initializer might be big, do not allocate on stack.
1207 vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1209 /* collect the values and store them at the offsets */
1210 for (i = 0; i < n; ++i) {
1211 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1212 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1213 ir_node *value = get_compound_ent_value(ent, i);
1214 int value_len = get_mode_size_bits(get_irn_mode(value));
1216 assert(offset_bits >= 0);
1218 if (offset_bits != 0 ||
1219 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1220 tarval *tv = get_atomic_init_tv(value);
1221 unsigned char curr_bits, last_bits = 0;
1223 panic("Couldn't get numeric value for bitfield initializer '%s'",
1224 get_entity_ld_name(ent));
1226 /* normalize offset */
1227 offset += offset_bits >> 3;
1230 for (j = 0; value_len + offset_bits > 0; ++j) {
1231 assert(offset + j < last_ofs);
1232 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1233 vals[offset + j].kind = BITFIELD;
1234 curr_bits = get_tarval_sub_bits(tv, j);
1235 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1237 last_bits = curr_bits;
1242 assert(offset < last_ofs);
1243 assert(vals[offset].kind == NORMAL);
1244 for (i = 1; i < value_len / 8; ++i) {
1245 assert(vals[offset + i].v.value == NULL);
1247 vals[offset].v.value = value;
1251 /* now write them sorted */
1252 for (k = 0; k < last_ofs; ) {
1253 int space = 0, skip = 0;
1254 if (vals[k].kind == NORMAL) {
1255 if (vals[k].v.value != NULL) {
1256 emit_atomic_init(env, vals[k].v.value);
1257 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1262 assert(vals[k].kind == BITFIELD);
1263 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1267 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1276 be_emit_irprintf("\t.space\t%d\n", space);
1277 be_emit_write_line();
1283 static void emit_align(unsigned p2alignment)
1285 be_emit_irprintf("\t.p2align\t%u\n", log2_floor(p2alignment));
1286 be_emit_write_line();
1289 static unsigned get_effective_entity_alignment(const ir_entity *entity)
1291 unsigned alignment = get_entity_alignment(entity);
1292 if (alignment == 0) {
1293 ir_type *type = get_entity_type(entity);
1294 alignment = get_type_alignment_bytes(type);
1299 static void emit_common(const ir_entity *entity)
1301 unsigned size = get_type_size_bytes(get_entity_type(entity));
1302 unsigned alignment = get_effective_entity_alignment(entity);
1304 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
1308 switch (be_gas_object_file_format) {
1309 case OBJECT_FILE_FORMAT_MACH_O:
1310 be_emit_cstring("\t.comm ");
1311 be_gas_emit_entity(entity);
1312 be_emit_irprintf(",%u,%u\n", size, log2_floor(alignment));
1313 be_emit_write_line();
1315 case OBJECT_FILE_FORMAT_ELF:
1316 be_emit_cstring("\t.comm ");
1317 be_gas_emit_entity(entity);
1318 be_emit_irprintf(",%u,%u\n", size, alignment);
1319 be_emit_write_line();
1321 case OBJECT_FILE_FORMAT_COFF:
1322 be_emit_cstring("\t.comm ");
1323 be_gas_emit_entity(entity);
1324 be_emit_irprintf(",%u # %u\n", size, alignment);
1325 be_emit_write_line();
1328 panic("invalid object file format");
1331 static void emit_local_common(const ir_entity *entity)
1333 unsigned size = get_type_size_bytes(get_entity_type(entity));
1334 unsigned alignment = get_effective_entity_alignment(entity);
1336 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
1340 switch (be_gas_object_file_format) {
1341 case OBJECT_FILE_FORMAT_MACH_O:
1342 be_emit_cstring("\t.lcomm ");
1343 be_gas_emit_entity(entity);
1344 be_emit_irprintf(",%u,%u\n", size, log2_floor(alignment));
1345 be_emit_write_line();
1347 case OBJECT_FILE_FORMAT_ELF:
1348 be_emit_cstring("\t.local ");
1349 be_gas_emit_entity(entity);
1350 be_emit_cstring("\n");
1351 be_emit_write_line();
1352 be_emit_cstring("\t.comm ");
1353 be_gas_emit_entity(entity);
1354 be_emit_irprintf(",%u,%u\n", size, alignment);
1355 be_emit_write_line();
1357 case OBJECT_FILE_FORMAT_COFF:
1358 be_emit_cstring("\t.lcomm ");
1359 be_gas_emit_entity(entity);
1360 be_emit_irprintf(",%u # %u\n", size, alignment);
1361 be_emit_write_line();
1364 panic("invalid object file format");
1367 static void emit_indirect_symbol(const ir_entity *entity, be_gas_section_t section)
1369 /* we can only do PIC code on macho so far */
1370 assert(be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O);
1372 be_gas_emit_entity(entity);
1373 be_emit_cstring(":\n");
1374 be_emit_write_line();
1375 be_emit_cstring("\t.indirect_symbol ");
1376 be_emit_ident(get_entity_ident(entity));
1378 be_emit_write_line();
1379 if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1380 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1381 be_emit_write_line();
1383 assert(section == GAS_SECTION_PIC_SYMBOLS);
1384 be_emit_cstring("\t.long 0\n");
1385 be_emit_write_line();
1389 char const *be_gas_get_private_prefix(void)
1392 be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O ? "L" :
1396 void be_gas_emit_entity(const ir_entity *entity)
1398 if (entity->type == firm_code_type) {
1399 ir_label_t label = get_entity_label(entity);
1400 be_emit_irprintf("%s_%lu", be_gas_get_private_prefix(), label);
1404 if (get_entity_visibility(entity) == ir_visibility_private) {
1405 be_emit_string(be_gas_get_private_prefix());
1407 be_emit_ident(get_entity_ld_ident(entity));
1410 void be_gas_emit_block_name(const ir_node *block)
1412 if (has_Block_entity(block)) {
1413 ir_entity *entity = get_Block_entity(block);
1414 be_gas_emit_entity(entity);
1416 be_emit_irprintf("%s%ld", be_gas_get_private_prefix(), get_irn_node_nr(block));
1421 * Dump a global entity.
1423 * @param env the gas output environment
1424 * @param ent the entity to be dumped
1426 static void emit_global(be_gas_decl_env_t *env, const ir_entity *entity)
1428 ir_type *type = get_entity_type(entity);
1429 ident *ld_ident = get_entity_ld_ident(entity);
1430 unsigned alignment = get_effective_entity_alignment(entity);
1431 be_gas_section_t section = determine_section(env, entity);
1432 ir_visibility visibility = get_entity_visibility(entity);
1433 ir_linkage linkage = get_entity_linkage(entity);
1435 /* block labels are already emittet in the code */
1436 if (type == firm_code_type)
1439 /* we already emitted all methods. Except for the trampolines which
1440 * the assembler/linker generates */
1441 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1442 /* functions with graph are already emitted with
1443 * be_gas_emit_function_prolog */
1444 if (get_entity_irg(entity) == NULL) {
1445 emit_visibility(entity);
1450 be_dbg_variable(entity);
1452 if (section == GAS_SECTION_BSS) {
1453 ir_visibility visibility = get_entity_visibility(entity);
1455 switch (visibility) {
1456 case ir_visibility_local:
1457 case ir_visibility_private:
1458 emit_local_common(entity);
1460 case ir_visibility_default:
1461 if (linkage & IR_LINKAGE_MERGE) {
1462 emit_common(entity);
1466 case ir_visibility_external:
1467 if (linkage & IR_LINKAGE_MERGE)
1468 panic("merge link semantic not supported for extern entities");
1473 emit_visibility(entity);
1474 if (visibility == ir_visibility_external) {
1475 /* nothing to do for externally defined values */
1479 if (!is_po2(alignment))
1480 panic("alignment not a power of 2");
1482 emit_section(section, entity);
1484 if (section == GAS_SECTION_PIC_TRAMPOLINES
1485 || section == GAS_SECTION_PIC_SYMBOLS) {
1486 emit_indirect_symbol(entity, section);
1491 if (alignment > 1) {
1492 emit_align(alignment);
1494 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF
1495 && be_gas_emit_types) {
1496 be_emit_cstring("\t.type\t");
1497 be_gas_emit_entity(entity);
1498 be_emit_cstring(", ");
1499 be_emit_char(be_gas_elf_type_char);
1500 be_emit_cstring("object\n\t.size\t");\
1501 be_gas_emit_entity(entity);
1502 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1505 if (get_id_str(ld_ident)[0] != '\0') {
1506 be_gas_emit_entity(entity);
1507 be_emit_cstring(":\n");
1508 be_emit_write_line();
1511 if (entity_is_null(entity)) {
1512 be_emit_irprintf("\t.space %u\n", get_type_size_bytes(type));
1513 be_emit_write_line();
1514 } else if (entity_has_compound_ent_values(entity)) {
1515 emit_compound_graph_init(env, entity);
1517 assert(entity->initializer != NULL);
1518 emit_initializer(env, entity);
1523 * Dumps declarations of global variables and the initialization code.
1525 * @param gt a global like type, either the global or the TLS one
1526 * @param env an environment
1528 static void be_gas_emit_globals(ir_type *gt, be_gas_decl_env_t *env)
1530 int i, n = get_compound_n_members(gt);
1532 for (i = 0; i < n; i++) {
1533 ir_entity *ent = get_compound_member(gt, i);
1534 emit_global(env, ent);
1538 /************************************************************************/
1540 /* Generate all entities. */
1541 void be_gas_emit_decls(const be_main_env_t *main_env)
1543 be_gas_decl_env_t env;
1544 memset(&env, 0, sizeof(env));
1546 /* dump global type */
1547 env.main_env = main_env;
1548 env.section = (be_gas_section_t) -1;
1550 be_gas_emit_globals(get_glob_type(), &env);
1551 be_gas_emit_globals(get_tls_type(), &env);
1552 be_gas_emit_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env);
1553 be_gas_emit_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env);
1554 be_gas_emit_globals(main_env->pic_symbols_type, &env);
1555 be_gas_emit_globals(main_env->pic_trampolines_type, &env);
1558 * ".subsections_via_symbols marks object files which are OK to divide
1559 * their section contents into individual blocks".
1560 * From my understanding this means no label points in the middle of an
1561 * object which we want to address as a whole. Firm code should be fine
1564 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
1565 be_emit_cstring("\t.subsections_via_symbols\n");
1566 be_emit_write_line();