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", "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_RODATA)
166 if (flags & GAS_SECTION_FLAG_TLS)
168 if (flags & GAS_SECTION_FLAG_COMDAT)
170 be_emit_cstring("\",");
171 be_emit_char(be_gas_elf_type_char);
172 be_emit_string(type[base]);
174 if (flags & GAS_SECTION_FLAG_COMDAT) {
176 be_gas_emit_entity(entity);
177 be_emit_cstring(",comdat");
180 be_emit_write_line();
183 void be_gas_emit_switch_section(be_gas_section_t section)
185 /* you have to produce a switch_section call with entity manually
186 * for comdat sections */
187 assert( !(section & GAS_SECTION_FLAG_COMDAT));
189 emit_section(section, NULL);
192 static tarval *get_initializer_tarval(const ir_initializer_t *initializer)
194 if (initializer->kind == IR_INITIALIZER_TARVAL)
195 return initializer->tarval.value;
196 if (initializer->kind == IR_INITIALIZER_CONST) {
197 ir_node *node = initializer->consti.value;
198 if (is_Const(node)) {
199 return get_Const_tarval(node);
202 return get_tarval_undefined();
205 static int initializer_is_string_const(const ir_initializer_t *initializer)
208 int found_printable = 0;
210 if (initializer->kind != IR_INITIALIZER_COMPOUND)
213 len = initializer->compound.n_initializers;
216 for (i = 0; i < len; ++i) {
220 ir_initializer_t *sub_initializer
221 = initializer->compound.initializers[i];
223 tv = get_initializer_tarval(sub_initializer);
224 if (!tarval_is_constant(tv))
227 mode = get_tarval_mode(tv);
228 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
231 c = get_tarval_long(tv);
232 if (isgraph(c) || isspace(c))
237 if (i == len - 1 && c != '\0')
241 return found_printable;
244 static bool initializer_is_null(const ir_initializer_t *initializer)
246 switch (initializer->kind) {
247 case IR_INITIALIZER_NULL:
249 case IR_INITIALIZER_TARVAL: {
250 tarval *tv = initializer->tarval.value;
251 return tarval_is_null(tv);
253 case IR_INITIALIZER_CONST: {
254 ir_node *value = initializer->consti.value;
255 if (!is_Const(value))
257 return is_Const_null(value);
259 case IR_INITIALIZER_COMPOUND: {
261 for (i = 0; i < initializer->compound.n_initializers; ++i) {
262 ir_initializer_t *subinitializer
263 = initializer->compound.initializers[i];
264 if (!initializer_is_null(subinitializer))
270 panic("invalid initializer in initializer_is_null");
274 * Determine if an entity is a string constant
275 * @param ent The entity
276 * @return 1 if it is a string constant, 0 otherwise
278 static int entity_is_string_const(const ir_entity *ent)
280 ir_type *type, *element_type;
284 type = get_entity_type(ent);
286 /* if it's an array */
287 if (!is_Array_type(type))
290 element_type = get_array_element_type(type);
292 /* and the array's element type is primitive */
293 if (!is_Primitive_type(element_type))
296 /* and the mode of the element type is an int of
297 * the same size as the byte mode */
298 mode = get_type_mode(element_type);
299 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
302 if (ent->initializer != NULL) {
303 return initializer_is_string_const(ent->initializer);
304 } else if (entity_has_compound_ent_values(ent)) {
305 int found_printable = 0;
306 /* if it contains only printable chars and a 0 at the end */
307 n = get_compound_ent_n_values(ent);
308 for (i = 0; i < n; ++i) {
309 ir_node *irn = get_compound_ent_value(ent, i);
313 c = (int) get_tarval_long(get_Const_tarval(irn));
315 if (isgraph(c) || isspace(c))
320 if (i == n - 1 && c != '\0')
323 return found_printable;
329 static bool entity_is_null(const ir_entity *entity)
331 if (entity->initializer != NULL) {
332 return initializer_is_null(entity->initializer);
333 } else if (entity_has_compound_ent_values(entity)) {
334 /* I'm too lazy to implement this case as compound graph paths will be
335 * remove anyway in the future */
337 /* uninitialized, NULL is fine */
341 static bool is_comdat(const ir_entity *entity)
343 ir_linkage linkage = get_entity_linkage(entity);
344 return (linkage & IR_LINKAGE_MERGE)
345 && (linkage & IR_LINKAGE_GARBAGE_COLLECT);
348 static be_gas_section_t determine_basic_section(const ir_entity *entity)
352 if (is_method_entity(entity))
353 return GAS_SECTION_TEXT;
355 linkage = get_entity_linkage(entity);
356 if (linkage & IR_LINKAGE_CONSTANT) {
357 /* mach-o is the only one with a cstring section */
358 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
359 && entity_is_string_const(entity))
360 return GAS_SECTION_CSTRING;
362 return GAS_SECTION_RODATA;
364 if (entity_is_null(entity))
365 return GAS_SECTION_BSS;
367 return GAS_SECTION_DATA;
370 static be_gas_section_t determine_section(be_gas_decl_env_t *env,
371 const ir_entity *entity)
373 ir_type *owner = get_entity_owner(entity);
375 if (owner == get_segment_type(IR_SEGMENT_GLOBAL)) {
376 be_gas_section_t section = determine_basic_section(entity);
377 if (is_comdat(entity))
378 section |= GAS_SECTION_FLAG_COMDAT;
380 } else if (env != NULL && owner == env->main_env->pic_symbols_type) {
381 return GAS_SECTION_PIC_SYMBOLS;
382 } else if (env != NULL && owner == env->main_env->pic_trampolines_type) {
383 return GAS_SECTION_PIC_TRAMPOLINES;
384 } else if (owner == get_segment_type(IR_SEGMENT_CONSTRUCTORS)) {
385 return GAS_SECTION_CONSTRUCTORS;
386 } else if (owner == get_segment_type(IR_SEGMENT_DESTRUCTORS)) {
387 return GAS_SECTION_DESTRUCTORS;
388 } else if (owner == get_segment_type(IR_SEGMENT_THREAD_LOCAL)) {
389 be_gas_section_t section = determine_basic_section(entity);
390 if (is_comdat(entity))
391 section |= GAS_SECTION_FLAG_COMDAT;
393 return section | GAS_SECTION_FLAG_TLS;
396 panic("Couldn't determine section for %+F?!?", entity);
399 static void emit_weak(const ir_entity *entity)
401 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
402 be_emit_cstring("\t.weak_reference ");
404 be_emit_cstring("\t.weak ");
406 be_gas_emit_entity(entity);
408 be_emit_write_line();
411 static void emit_visibility(const ir_entity *entity)
413 ir_linkage linkage = get_entity_linkage(entity);
415 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
417 /* Note: .weak seems to imply .globl so no need to output .globl */
418 } else if (get_entity_visibility(entity) == ir_visibility_default) {
419 be_emit_cstring(".globl ");
420 be_gas_emit_entity(entity);
422 be_emit_write_line();
425 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
426 && (linkage & IR_LINKAGE_HIDDEN_USER)) {
427 be_emit_cstring("\t.no_dead_strip ");
428 be_gas_emit_entity(entity);
430 be_emit_write_line();
434 void be_gas_emit_function_prolog(const ir_entity *entity, unsigned po2alignment)
436 be_gas_section_t section = determine_section(NULL, entity);
437 emit_section(section, entity);
439 /* write the begin line (makes the life easier for scripts parsing the
441 be_emit_write_line();
442 be_emit_cstring("# -- Begin ");
443 be_gas_emit_entity(entity);
445 be_emit_write_line();
447 if (po2alignment > 0) {
448 const char *fill_byte = "";
449 unsigned maximum_skip = (1 << po2alignment) - 1;
450 /* gcc fills space between function with 0x90... */
451 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
454 be_emit_cstring("\t.p2align ");
455 be_emit_irprintf("%u,%s,%u\n", po2alignment, fill_byte, maximum_skip);
456 be_emit_write_line();
458 emit_visibility(entity);
460 switch (be_gas_object_file_format) {
461 case OBJECT_FILE_FORMAT_ELF:
462 be_emit_cstring("\t.type\t");
463 be_gas_emit_entity(entity);
464 be_emit_cstring(", ");
465 be_emit_char(be_gas_elf_type_char);
466 be_emit_cstring("function\n");
467 be_emit_write_line();
469 case OBJECT_FILE_FORMAT_COFF:
470 be_emit_cstring("\t.def\t");
471 be_gas_emit_entity(entity);
472 be_emit_cstring(";");
473 if (get_entity_visibility(entity) == ir_visibility_local) {
474 be_emit_cstring("\t.scl\t3;");
476 be_emit_cstring("\t.scl\t2;");
478 be_emit_cstring("\t.type\t32;\t.endef\n");
479 be_emit_write_line();
481 case OBJECT_FILE_FORMAT_MACH_O:
484 be_gas_emit_entity(entity);
485 be_emit_cstring(":\n");
486 be_emit_write_line();
489 void be_gas_emit_function_epilog(const ir_entity *entity)
491 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF) {
492 be_emit_cstring("\t.size\t");
493 be_gas_emit_entity(entity);
494 be_emit_cstring(", .-");
495 be_gas_emit_entity(entity);
497 be_emit_write_line();
500 be_emit_cstring("# -- End ");
501 be_gas_emit_entity(entity);
503 be_emit_write_line();
506 /************************************************************************/
511 * @param tv the tarval
512 * @param bytes the width of the tarvals value in bytes
514 static void emit_arith_tarval(tarval *tv, int bytes)
518 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
522 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
526 be_emit_irprintf("0x%02x%02x%02x%02x",
527 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
531 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
532 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
533 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
537 /* Beware: Mixed endian output! One little endian number emitted as
538 * three longs. Each long initializer is written in big endian. */
540 "\t.long\t0x%02x%02x%02x%02x\n"
541 "\t.long\t0x%02x%02x%02x%02x\n"
542 "\t.long\t0x%02x%02x%02x%02x",
543 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
544 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
545 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
546 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
547 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
548 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8)
553 /* Beware: Mixed endian output! One little endian number emitted as
554 * three longs. Each long initializer is written in big endian. */
556 "\t.long\t0x%02x%02x%02x%02x\n"
557 "\t.long\t0x%02x%02x%02x%02x\n"
558 "\t.long\t0x%02x%02x%02x%02x\n"
559 "\t.long\t0x%02x%02x%02x%02x",
560 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
561 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
562 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
563 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
564 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
565 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
566 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 14),
567 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12)
572 panic("Can't dump a tarval with %d bytes", bytes);
576 * Return the label prefix for labeled blocks.
578 const char *be_gas_block_label_prefix(void)
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_name:
674 be_emit_ident(get_SymConst_name(init));
677 case symconst_addr_ent:
678 ent = get_SymConst_entity(init);
679 be_gas_emit_entity(ent);
682 case symconst_ofs_ent:
683 ent = get_SymConst_entity(init);
684 be_emit_irprintf("%d", get_entity_offset(ent));
687 case symconst_type_size:
688 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
691 case symconst_type_align:
692 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
695 case symconst_enum_const:
696 tv = get_enumeration_value(get_SymConst_enum(init));
697 emit_arith_tarval(tv, bytes);
701 assert(!"emit_atomic_init(): don't know how to init from this SymConst");
706 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
707 panic("Constant must be int or pointer for '+' to work");
709 do_emit_atomic_init(env, get_Add_left(init));
710 be_emit_cstring(" + ");
711 do_emit_atomic_init(env, get_Add_right(init));
715 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
716 panic("Constant must be int or pointer for '-' to work");
718 do_emit_atomic_init(env, get_Sub_left(init));
719 be_emit_cstring(" - ");
720 do_emit_atomic_init(env, get_Sub_right(init));
724 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
725 panic("Constant must be int or pointer for '*' to work");
727 do_emit_atomic_init(env, get_Mul_left(init));
728 be_emit_cstring(" * ");
729 do_emit_atomic_init(env, get_Mul_right(init));
733 be_emit_cstring("0");
737 panic("emit_atomic_init(): unsupported IR-node %+F", init);
742 * Dumps the type for given size (.byte, .long, ...)
744 * @param size the size in bytes
746 static void emit_size_type(size_t size)
750 be_emit_cstring("\t.byte\t");
754 be_emit_cstring("\t.short\t");
758 be_emit_cstring("\t.long\t");
762 be_emit_cstring("\t.quad\t");
767 case 16: /* Note: .octa does not work on mac */
768 /* handled in arith */
772 panic("Try to dump a type with %u bytes", (unsigned)size);
777 * Emit an atomic value.
779 * @param env the gas output environment
780 * @param init a node representing the atomic value (on the const code irg)
782 static void emit_atomic_init(be_gas_decl_env_t *env, ir_node *init)
784 ir_mode *mode = get_irn_mode(init);
785 int bytes = get_mode_size_bytes(mode);
787 emit_size_type(bytes);
788 do_emit_atomic_init(env, init);
790 be_emit_write_line();
793 /************************************************************************/
794 /* Routines to dump global variables */
795 /************************************************************************/
798 * Dump a string constant.
799 * No checks are made!!
801 * @param ent The entity to dump.
803 static void emit_string_cst(const ir_entity *ent)
811 len = get_compound_ent_n_values(ent);
813 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
814 be_emit_cstring("\t.ascii \"");
816 be_emit_cstring("\t.string \"");
820 for (i = 0; i < output_len; ++i) {
824 irn = get_compound_ent_value(ent, i);
825 c = (int) get_tarval_long(get_Const_tarval(irn));
828 case '"' : be_emit_cstring("\\\""); break;
829 case '\n': be_emit_cstring("\\n"); break;
830 case '\r': be_emit_cstring("\\r"); break;
831 case '\t': be_emit_cstring("\\t"); break;
832 case '\\': be_emit_cstring("\\\\"); break;
837 be_emit_irprintf("\\%o", c);
841 be_emit_cstring("\"\n");
842 be_emit_write_line();
844 type = get_entity_type(ent);
845 type_size = get_type_size_bytes(type);
846 remaining_space = type_size - len;
847 assert(remaining_space >= 0);
848 if (remaining_space > 0) {
849 be_emit_irprintf("\t.zero\t%d\n", remaining_space);
853 static void emit_string_initializer(const ir_initializer_t *initializer)
857 len = initializer->compound.n_initializers;
858 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
859 be_emit_cstring("\t.ascii \"");
861 be_emit_cstring("\t.string \"");
865 for (i = 0; i < len; ++i) {
866 const ir_initializer_t *sub_initializer
867 = get_initializer_compound_value(initializer, i);
869 tarval *tv = get_initializer_tarval(sub_initializer);
870 int c = get_tarval_long(tv);
873 case '"' : be_emit_cstring("\\\""); break;
874 case '\n': be_emit_cstring("\\n"); break;
875 case '\r': be_emit_cstring("\\r"); break;
876 case '\t': be_emit_cstring("\\t"); break;
877 case '\\': be_emit_cstring("\\\\"); break;
882 be_emit_irprintf("\\%o", c);
886 be_emit_cstring("\"\n");
887 be_emit_write_line();
890 enum normal_or_bitfield_kind {
897 enum normal_or_bitfield_kind kind;
901 unsigned char bf_val;
903 } normal_or_bitfield;
905 static int is_type_variable_size(ir_type *type)
912 static size_t get_initializer_size(const ir_initializer_t *initializer,
915 switch (get_initializer_kind(initializer)) {
916 case IR_INITIALIZER_TARVAL:
917 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
918 return get_type_size_bytes(type);
919 case IR_INITIALIZER_CONST:
920 case IR_INITIALIZER_NULL:
921 return get_type_size_bytes(type);
922 case IR_INITIALIZER_COMPOUND:
923 if (!is_type_variable_size(type)) {
924 return get_type_size_bytes(type);
927 = get_initializer_compound_n_entries(initializer);
929 unsigned initializer_size = get_type_size_bytes(type);
930 for (i = 0; i < n_entries; ++i) {
931 ir_entity *entity = get_compound_member(type, i);
932 ir_type *type = get_entity_type(entity);
934 const ir_initializer_t *sub_initializer
935 = get_initializer_compound_value(initializer, i);
937 unsigned offset = get_entity_offset(entity);
938 unsigned size = get_initializer_size(sub_initializer, type);
940 if (offset + size > initializer_size) {
941 initializer_size = offset + size;
944 return initializer_size;
948 panic("found invalid initializer");
952 static normal_or_bitfield *glob_vals;
953 static size_t max_vals;
956 static void emit_bitfield(normal_or_bitfield *vals, size_t offset_bits,
957 const ir_initializer_t *initializer, ir_type *type)
959 unsigned char last_bits = 0;
960 ir_mode *mode = get_type_mode(type);
962 unsigned char curr_bits;
966 switch (get_initializer_kind(initializer)) {
967 case IR_INITIALIZER_NULL:
969 case IR_INITIALIZER_TARVAL:
970 tv = get_initializer_tarval_value(initializer);
972 case IR_INITIALIZER_CONST: {
973 ir_node *node = get_initializer_const_value(initializer);
974 if (!is_Const(node)) {
975 panic("bitfield initializer not a Const node");
977 tv = get_Const_tarval(node);
980 case IR_INITIALIZER_COMPOUND:
981 panic("bitfield initializer is compound");
984 panic("Couldn't get numeric value for bitfield initializer");
986 tv = tarval_convert_to(tv, get_type_mode(type));
988 /* normalize offset */
989 vals += offset_bits >> 3;
991 value_len = get_mode_size_bits(mode);
993 /* combine bits with existing bits */
994 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
995 assert((size_t) (vals - glob_vals) + j < max_vals);
996 assert(vals[j].kind == BITFIELD ||
997 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
998 vals[j].kind = BITFIELD;
999 curr_bits = get_tarval_sub_bits(tv, j);
1001 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1003 last_bits = curr_bits;
1007 static void emit_ir_initializer(normal_or_bitfield *vals,
1008 const ir_initializer_t *initializer,
1011 assert((size_t) (vals - glob_vals) < max_vals);
1013 switch (get_initializer_kind(initializer)) {
1014 case IR_INITIALIZER_NULL:
1016 case IR_INITIALIZER_TARVAL: {
1019 assert(vals->kind != BITFIELD);
1020 vals->kind = TARVAL;
1021 vals->v.tarval = get_initializer_tarval_value(initializer);
1022 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
1023 for (i = 1; i < get_type_size_bytes(type); ++i) {
1024 vals[i].kind = NORMAL;
1025 vals[i].v.value = NULL;
1029 case IR_INITIALIZER_CONST: {
1032 assert(vals->kind != BITFIELD);
1033 vals->kind = NORMAL;
1034 vals->v.value = get_initializer_const_value(initializer);
1035 for (i = 1; i < get_type_size_bytes(type); ++i) {
1036 vals[i].kind = NORMAL;
1037 vals[i].v.value = NULL;
1041 case IR_INITIALIZER_COMPOUND: {
1043 size_t n = get_initializer_compound_n_entries(initializer);
1045 if (is_Array_type(type)) {
1046 ir_type *element_type = get_array_element_type(type);
1047 size_t skip = get_type_size_bytes(element_type);
1048 size_t alignment = get_type_alignment_bytes(element_type);
1049 size_t misalign = skip % alignment;
1050 if (misalign != 0) {
1051 skip += alignment - misalign;
1054 for (i = 0; i < n; ++i) {
1055 ir_initializer_t *sub_initializer
1056 = get_initializer_compound_value(initializer, i);
1058 emit_ir_initializer(vals, sub_initializer, element_type);
1063 size_t n_members, i;
1064 assert(is_compound_type(type));
1065 n_members = get_compound_n_members(type);
1066 for (i = 0; i < n_members; ++i) {
1067 ir_entity *member = get_compound_member(type, i);
1068 size_t offset = get_entity_offset(member);
1069 ir_type *subtype = get_entity_type(member);
1070 ir_mode *mode = get_type_mode(subtype);
1071 ir_initializer_t *sub_initializer;
1073 assert(i < get_initializer_compound_n_entries(initializer));
1075 = get_initializer_compound_value(initializer, i);
1079 = get_entity_offset_bits_remainder(member);
1080 size_t value_len = get_mode_size_bits(mode);
1082 if (offset_bits != 0 ||
1083 (value_len != 8 && value_len != 16 && value_len != 32
1084 && value_len != 64)) {
1085 emit_bitfield(&vals[offset], offset_bits,
1086 sub_initializer, subtype);
1091 emit_ir_initializer(&vals[offset], sub_initializer, subtype);
1098 panic("invalid ir_initializer kind found");
1101 static void emit_initializer(be_gas_decl_env_t *env, const ir_entity *entity)
1103 const ir_initializer_t *initializer = entity->initializer;
1105 normal_or_bitfield *vals;
1109 if (initializer_is_string_const(initializer)) {
1110 emit_string_initializer(initializer);
1114 type = get_entity_type(entity);
1115 size = get_initializer_size(initializer, type);
1121 * In the worst case, every initializer allocates one byte.
1122 * Moreover, initializer might be big, do not allocate on stack.
1124 vals = XMALLOCNZ(normal_or_bitfield, size);
1131 emit_ir_initializer(vals, initializer, type);
1133 /* now write values sorted */
1134 for (k = 0; k < size; ) {
1137 if (vals[k].kind == NORMAL) {
1138 if (vals[k].v.value != NULL) {
1139 emit_atomic_init(env, vals[k].v.value);
1140 elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
1144 } else if (vals[k].kind == TARVAL) {
1145 tarval *tv = vals[k].v.tarval;
1146 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
1151 emit_size_type(size);
1152 emit_arith_tarval(tv, size);
1154 be_emit_write_line();
1156 assert(vals[k].kind == BITFIELD);
1157 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1158 be_emit_write_line();
1162 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1169 be_emit_irprintf("\t.space\t%d\n", space);
1170 be_emit_write_line();
1176 static void emit_compound_graph_init(be_gas_decl_env_t *env,
1177 const ir_entity *ent)
1179 normal_or_bitfield *vals;
1181 unsigned k, last_ofs;
1183 if (entity_is_string_const(ent)) {
1184 emit_string_cst(ent);
1188 n = get_compound_ent_n_values(ent);
1190 /* Find the initializer size. Sorrily gcc support a nasty feature:
1191 The last field of a compound may be a flexible array. This allows
1192 initializers bigger than the type size. */
1193 last_ofs = get_type_size_bytes(get_entity_type(ent));
1194 for (i = 0; i < n; ++i) {
1195 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1196 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1197 ir_node *value = get_compound_ent_value(ent, i);
1198 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
1200 offset += (value_len + bits_remainder + 7) >> 3;
1202 if (offset > last_ofs) {
1208 * In the worst case, every initializer allocates one byte.
1209 * Moreover, initializer might be big, do not allocate on stack.
1211 vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1213 /* collect the values and store them at the offsets */
1214 for (i = 0; i < n; ++i) {
1215 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1216 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1217 ir_node *value = get_compound_ent_value(ent, i);
1218 int value_len = get_mode_size_bits(get_irn_mode(value));
1220 assert(offset_bits >= 0);
1222 if (offset_bits != 0 ||
1223 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1224 tarval *tv = get_atomic_init_tv(value);
1225 unsigned char curr_bits, last_bits = 0;
1227 panic("Couldn't get numeric value for bitfield initializer '%s'",
1228 get_entity_ld_name(ent));
1230 /* normalize offset */
1231 offset += offset_bits >> 3;
1234 for (j = 0; value_len + offset_bits > 0; ++j) {
1235 assert(offset + j < last_ofs);
1236 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1237 vals[offset + j].kind = BITFIELD;
1238 curr_bits = get_tarval_sub_bits(tv, j);
1239 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1241 last_bits = curr_bits;
1246 assert(offset < last_ofs);
1247 assert(vals[offset].kind == NORMAL);
1248 for (i = 1; i < value_len / 8; ++i) {
1249 assert(vals[offset + i].v.value == NULL);
1251 vals[offset].v.value = value;
1255 /* now write them sorted */
1256 for (k = 0; k < last_ofs; ) {
1257 int space = 0, skip = 0;
1258 if (vals[k].kind == NORMAL) {
1259 if (vals[k].v.value != NULL) {
1260 emit_atomic_init(env, vals[k].v.value);
1261 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1266 assert(vals[k].kind == BITFIELD);
1267 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1271 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1280 be_emit_irprintf("\t.space\t%d\n", space);
1281 be_emit_write_line();
1287 static void emit_align(unsigned p2alignment)
1289 be_emit_irprintf("\t.p2align\t%u\n", log2_floor(p2alignment));
1290 be_emit_write_line();
1293 static unsigned get_effective_entity_alignment(const ir_entity *entity)
1295 unsigned alignment = get_entity_alignment(entity);
1296 if (alignment == 0) {
1297 ir_type *type = get_entity_type(entity);
1298 alignment = get_type_alignment_bytes(type);
1303 static void emit_common(const ir_entity *entity)
1305 unsigned size = get_type_size_bytes(get_entity_type(entity));
1306 unsigned alignment = get_effective_entity_alignment(entity);
1308 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
1312 switch (be_gas_object_file_format) {
1313 case OBJECT_FILE_FORMAT_MACH_O:
1314 be_emit_cstring("\t.lcomm ");
1315 be_gas_emit_entity(entity);
1316 be_emit_irprintf(",%u,%u\n", size, log2_floor(alignment));
1317 be_emit_write_line();
1319 case OBJECT_FILE_FORMAT_ELF:
1320 be_emit_cstring("\t.comm ");
1321 be_gas_emit_entity(entity);
1322 be_emit_irprintf(",%u,%u\n", size, alignment);
1323 be_emit_write_line();
1325 case OBJECT_FILE_FORMAT_COFF:
1326 be_emit_cstring("\t.comm ");
1327 be_gas_emit_entity(entity);
1328 be_emit_irprintf(",%u # %u\n", size, alignment);
1329 be_emit_write_line();
1332 panic("invalid object file format");
1335 static void emit_local_common(const ir_entity *entity)
1337 unsigned size = get_type_size_bytes(get_entity_type(entity));
1338 unsigned alignment = get_effective_entity_alignment(entity);
1340 if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
1344 switch (be_gas_object_file_format) {
1345 case OBJECT_FILE_FORMAT_MACH_O:
1346 be_emit_cstring("\t.lcomm ");
1347 be_gas_emit_entity(entity);
1348 be_emit_irprintf(",%u,%u\n", size, log2_floor(alignment));
1349 be_emit_write_line();
1351 case OBJECT_FILE_FORMAT_ELF:
1352 be_emit_cstring("\t.local ");
1353 be_gas_emit_entity(entity);
1354 be_emit_cstring("\n");
1355 be_emit_write_line();
1356 be_emit_cstring("\t.comm ");
1357 be_gas_emit_entity(entity);
1358 be_emit_irprintf(",%u,%u\n", size, alignment);
1359 be_emit_write_line();
1361 case OBJECT_FILE_FORMAT_COFF:
1362 be_emit_cstring("\t.lcomm ");
1363 be_gas_emit_entity(entity);
1364 be_emit_irprintf(",%u # %u\n", size, alignment);
1365 be_emit_write_line();
1368 panic("invalid object file format");
1371 static void emit_indirect_symbol(const ir_entity *entity, be_gas_section_t section)
1373 /* we can only do PIC code on macho so far */
1374 assert(be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O);
1376 be_emit_ident(get_entity_ld_ident(entity));
1377 be_emit_cstring(":\n");
1378 be_emit_write_line();
1379 be_emit_cstring("\t.indirect_symbol ");
1380 be_emit_ident(get_entity_ident(entity));
1382 be_emit_write_line();
1383 if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1384 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1385 be_emit_write_line();
1387 assert(section == GAS_SECTION_PIC_SYMBOLS);
1388 be_emit_cstring("\t.long 0\n");
1389 be_emit_write_line();
1393 void be_gas_emit_entity(const ir_entity *entity)
1395 if (entity->type == firm_code_type) {
1396 ir_label_t label = get_entity_label(entity);
1397 be_emit_string(be_gas_block_label_prefix());
1398 be_emit_irprintf("%lu", label);
1402 if (get_entity_visibility(entity) == ir_visibility_private) {
1403 be_emit_cstring(".L");
1405 be_emit_ident(get_entity_ld_ident(entity));
1409 * Dump a global entity.
1411 * @param env the gas output environment
1412 * @param ent the entity to be dumped
1414 static void emit_global(be_gas_decl_env_t *env, const ir_entity *entity)
1416 ir_type *type = get_entity_type(entity);
1417 ident *ld_ident = get_entity_ld_ident(entity);
1418 unsigned alignment = get_effective_entity_alignment(entity);
1419 be_gas_section_t section = determine_section(env, entity);
1420 ir_visibility visibility = get_entity_visibility(entity);
1421 ir_linkage linkage = get_entity_linkage(entity);
1423 /* block labels are already emittet in the code */
1424 if (type == firm_code_type)
1427 /* we already emitted all methods. Except for the trampolines which
1428 * the assembler/linker generates */
1429 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1430 /* functions with graph are already emitted with
1431 * be_gas_emit_function_prolog */
1432 if (get_entity_irg(entity) == NULL) {
1433 emit_visibility(entity);
1438 be_dbg_variable(entity);
1440 if (section == GAS_SECTION_BSS) {
1441 ir_visibility visibility = get_entity_visibility(entity);
1443 switch (visibility) {
1444 case ir_visibility_local:
1445 case ir_visibility_private:
1446 emit_local_common(entity);
1448 case ir_visibility_default:
1449 if (linkage & IR_LINKAGE_MERGE) {
1450 emit_common(entity);
1454 case ir_visibility_external:
1455 panic("merge link semantic not supported for extern entities");
1460 emit_visibility(entity);
1461 if (visibility == ir_visibility_external) {
1462 /* nothing to do for externally defined values */
1466 if (!is_po2(alignment))
1467 panic("alignment not a power of 2");
1469 emit_section(section, entity);
1471 if (section == GAS_SECTION_PIC_TRAMPOLINES
1472 || section == GAS_SECTION_PIC_SYMBOLS) {
1473 emit_indirect_symbol(entity, section);
1478 if (alignment > 1) {
1479 emit_align(alignment);
1481 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF
1482 && be_gas_emit_types) {
1483 be_emit_cstring("\t.type\t");
1484 be_gas_emit_entity(entity);
1485 be_emit_cstring(", ");
1486 be_emit_char(be_gas_elf_type_char);
1487 be_emit_cstring("object\n\t.size\t");\
1488 be_gas_emit_entity(entity);
1489 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1492 if (get_id_str(ld_ident)[0] != '\0') {
1493 be_gas_emit_entity(entity);
1494 be_emit_cstring(":\n");
1495 be_emit_write_line();
1498 if (entity_is_null(entity)) {
1499 be_emit_irprintf("\t.zero %u\n", get_type_size_bytes(type));
1500 be_emit_write_line();
1501 } else if(entity_has_compound_ent_values(entity)) {
1502 emit_compound_graph_init(env, entity);
1504 assert(entity->initializer != NULL);
1505 emit_initializer(env, entity);
1510 * Dumps declarations of global variables and the initialization code.
1512 * @param gt a global like type, either the global or the TLS one
1513 * @param env an environment
1515 static void be_gas_emit_globals(ir_type *gt, be_gas_decl_env_t *env)
1517 int i, n = get_compound_n_members(gt);
1519 for (i = 0; i < n; i++) {
1520 ir_entity *ent = get_compound_member(gt, i);
1521 emit_global(env, ent);
1525 /************************************************************************/
1527 /* Generate all entities. */
1528 void be_gas_emit_decls(const be_main_env_t *main_env)
1530 be_gas_decl_env_t env;
1531 memset(&env, 0, sizeof(env));
1533 /* dump global type */
1534 env.main_env = main_env;
1535 env.section = (be_gas_section_t) -1;
1537 be_gas_emit_globals(get_glob_type(), &env);
1538 be_gas_emit_globals(get_tls_type(), &env);
1539 be_gas_emit_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env);
1540 be_gas_emit_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env);
1541 be_gas_emit_globals(main_env->pic_symbols_type, &env);
1542 be_gas_emit_globals(main_env->pic_trampolines_type, &env);
1545 * ".subsections_via_symbols marks object files which are OK to divide
1546 * their section contents into individual blocks".
1547 * From my understanding this means no label points in the middle of an
1548 * object which we want to address as a whole. Firm code should be fine
1551 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
1552 be_emit_cstring("\t.subsections_via_symbols\n");
1553 be_emit_write_line();