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
45 #include "beemitter.h"
46 #include "be_dbgout.h"
48 /** by default, we generate assembler code for the Linux gas */
49 be_gas_flavour_t be_gas_flavour = GAS_FLAVOUR_ELF;
51 static be_gas_section_t current_section = (be_gas_section_t) -1;
54 * Return the pseudo-instruction to be issued for a section switch
55 * depending on the current flavour.
57 * @param section the section to switch to
59 * @return the pseudo-instruction
61 static const char *get_section_name(be_gas_section_t section) {
62 static const char *text[GAS_FLAVOUR_LAST+1][GAS_SECTION_LAST+1] = {
63 { /* GAS_FLAVOUR_ELF */
68 ".section\t.tbss,\"awT\",@nobits",
69 ".section\t.ctors,\"aw\",@progbits",
70 ".section\t.dtors,\"aw\",@progbits",
71 NULL, /* no cstring section */
75 { /* GAS_FLAVOUR_MINGW */
78 ".section .rdata,\"dr\"",
80 ".section\t.tbss,\"awT\",@nobits",
81 ".section\t.ctors,\"aw\",@progbits",
82 ".section\t.dtors,\"aw\",@progbits",
87 { /* GAS_FLAVOUR_YASM */
92 ".section\t.tbss,\"awT\",@nobits",
93 ".section\t.ctors,\"aw\",@progbits",
94 ".section\t.dtors,\"aw\",@progbits",
99 { /* GAS_FLAVOUR_MACH_O */
104 NULL, /* TLS is not supported on Mach-O */
106 NULL, /* TODO: how is this called? */
108 ".section\t__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5",
109 ".section\t__IMPORT,__pointers,non_lazy_symbol_pointers"
113 assert((int) be_gas_flavour >= 0 && be_gas_flavour <= GAS_FLAVOUR_LAST);
114 assert((int) section >= 0 && section <= GAS_SECTION_LAST);
115 return text[be_gas_flavour][section];
118 void be_gas_emit_switch_section(be_gas_section_t section) {
119 if(current_section == section)
123 be_emit_string(get_section_name(section));
125 be_emit_write_line();
126 current_section = section;
129 void be_gas_emit_function_prolog(ir_entity *entity, unsigned alignment)
131 const char *name = get_entity_ld_name(entity);
132 const char *fill_byte = "";
133 unsigned maximum_skip;
135 be_gas_emit_switch_section(GAS_SECTION_TEXT);
137 /* write the begin line (used by scripts processing the assembler... */
138 be_emit_write_line();
139 be_emit_cstring("# -- Begin ");
140 be_emit_string(name);
142 be_emit_write_line();
144 /* gcc fills space between function with 0x90, no idea if this is needed */
145 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
150 maximum_skip = (1 << alignment) - 1;
151 be_emit_cstring("\t.p2align ");
152 be_emit_irprintf("%u,%s,%u\n", alignment, fill_byte, maximum_skip);
153 be_emit_write_line();
155 if (get_entity_visibility(entity) == visibility_external_visible) {
156 be_emit_cstring(".globl ");
157 be_emit_string(name);
159 be_emit_write_line();
162 switch (be_gas_flavour) {
163 case GAS_FLAVOUR_ELF:
164 be_emit_cstring("\t.type\t");
165 be_emit_string(name);
166 be_emit_cstring(", @function\n");
167 be_emit_write_line();
169 case GAS_FLAVOUR_MINGW:
170 be_emit_cstring("\t.def\t");
171 be_emit_string(name);
172 if (get_entity_visibility(entity) == visibility_external_visible) {
173 be_emit_cstring(";\t.scl\t2;\t.type\t32;\t.endef\n");
175 be_emit_cstring(";\t.scl\t3;\t.type\t32;\t.endef\n");
177 be_emit_write_line();
179 case GAS_FLAVOUR_MACH_O:
180 case GAS_FLAVOUR_YASM:
183 be_emit_string(name);
184 be_emit_cstring(":\n");
185 be_emit_write_line();
188 void be_gas_emit_function_epilog(ir_entity *entity)
190 const char *name = get_entity_ld_name(entity);
192 if (be_gas_flavour == GAS_FLAVOUR_ELF) {
193 be_emit_cstring("\t.size\t");
194 be_emit_string(name);
195 be_emit_cstring(", .-");
196 be_emit_string(name);
198 be_emit_write_line();
201 be_emit_cstring("# -- End ");
202 be_emit_string(name);
204 be_emit_write_line();
208 * An environment containing all needed dumper data.
209 * Currently we create the file completely in memory first, then
210 * write it to the disk. This is an artifact from the old C-generating backend
211 * and even there NOT needed. So we might change it in the future.
213 typedef struct _be_gas_decl_env {
214 const be_main_env_t *main_env; /**< The main backend environment, used for it's debug handle. */
215 be_gas_section_t section;
216 waitq *worklist; /**< A worklist we use to place not yet handled entities on. */
219 /************************************************************************/
224 * @param tv the tarval
225 * @param bytes the width of the tarvals value in bytes
227 static void dump_arith_tarval(tarval *tv, int bytes)
231 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
235 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
239 be_emit_irprintf("0x%02x%02x%02x%02x",
240 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
244 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
245 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
246 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
250 /* Beware: Mixed endian output! One little endian number emitted as
251 * three longs. Each long initializer is written in big endian. */
253 "\t.long\t0x%02x%02x%02x%02x\n"
254 "\t.long\t0x%02x%02x%02x%02x\n"
255 "\t.long\t0x%02x%02x%02x%02x",
256 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
257 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
258 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
259 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
260 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
261 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8)
267 "\t.long\t0x%02x%02x%02x%02x0x%02x%02x%02x%02x0x%02x%02x%02x%02x0x%02x%02x%02x%02x",
268 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
269 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
270 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
271 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
272 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
273 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
274 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
275 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0)
280 panic("Can't dump a tarval with %d bytes", bytes);
284 * Return the label prefix for labeled blocks.
286 const char *be_gas_block_label_prefix(void) {
291 * Return the label prefix for labeled instructions.
293 const char *be_gas_insn_label_prefix(void) {
300 static void dump_label(ir_label_t label) {
301 be_emit_irprintf("%s%lu", be_gas_block_label_prefix(), label);
305 * Return the tarval of an atomic initializer.
307 * @param init a node representing the initializer (on the const code irg)
311 static tarval *get_atomic_init_tv(ir_node *init)
314 ir_mode *mode = get_irn_mode(init);
316 switch (get_irn_opcode(init)) {
319 init = get_Cast_op(init);
323 init = get_Conv_op(init);
327 return get_Const_tarval(init);
330 switch (get_SymConst_kind(init)) {
331 case symconst_type_size:
332 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
334 case symconst_type_align:
335 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
337 case symconst_ofs_ent:
338 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
340 case symconst_enum_const:
341 return get_enumeration_value(get_SymConst_enum(init));
357 * Dump an atomic value.
359 * @param env the gas output environment
360 * @param init a node representing the atomic value (on the const code irg)
362 static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
364 ir_mode *mode = get_irn_mode(init);
365 int bytes = get_mode_size_bytes(mode);
370 init = skip_Id(init);
372 switch (get_irn_opcode(init)) {
374 do_dump_atomic_init(env, get_Cast_op(init));
378 do_dump_atomic_init(env, get_Conv_op(init));
382 tv = get_Const_tarval(init);
384 /* it's a arithmetic value */
385 dump_arith_tarval(tv, bytes);
389 switch (get_SymConst_kind(init)) {
390 case symconst_addr_name:
391 be_emit_ident(get_SymConst_name(init));
394 case symconst_addr_ent:
395 ent = get_SymConst_entity(init);
396 if(!is_entity_backend_marked(ent)) {
397 waitq_put(env->worklist, ent);
398 set_entity_backend_marked(ent, 1);
400 be_emit_ident(get_entity_ld_ident(ent));
403 case symconst_ofs_ent:
404 ent = get_SymConst_entity(init);
405 #if 0 /* not needed, is it? */
406 if(!is_entity_backend_marked(ent)) {
407 waitq_put(env->worklist, ent);
408 set_entity_backend_marked(ent, 1);
411 be_emit_irprintf("%d", get_entity_offset(ent));
414 case symconst_type_size:
415 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
418 case symconst_type_align:
419 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
422 case symconst_enum_const:
423 tv = get_enumeration_value(get_SymConst_enum(init));
424 dump_arith_tarval(tv, bytes);
428 label = get_SymConst_label(init);
433 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
438 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
439 panic("Constant must be int or pointer for '+' to work");
441 do_dump_atomic_init(env, get_Add_left(init));
442 be_emit_cstring(" + ");
443 do_dump_atomic_init(env, get_Add_right(init));
447 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
448 panic("Constant must be int or pointer for '-' to work");
450 do_dump_atomic_init(env, get_Sub_left(init));
451 be_emit_cstring(" - ");
452 do_dump_atomic_init(env, get_Sub_right(init));
456 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
457 panic("Constant must be int or pointer for '*' to work");
459 do_dump_atomic_init(env, get_Mul_left(init));
460 be_emit_cstring(" * ");
461 do_dump_atomic_init(env, get_Mul_right(init));
465 panic("dump_atomic_init(): unsupported IR-node %+F", init);
470 * Dumps the type for given size (.byte, .long, ...)
472 * @param size the size in bytes
474 static void dump_size_type(size_t size) {
477 be_emit_cstring("\t.byte\t");
481 be_emit_cstring("\t.word\t");
485 be_emit_cstring("\t.long\t");
489 be_emit_cstring("\t.quad\t");
494 /* handled in arith */
498 be_emit_cstring("\t.octa\t");
502 panic("Try to dump a type with %u bytes", (unsigned)size);
507 * Emit an atomic value.
509 * @param env the gas output environment
510 * @param init a node representing the atomic value (on the const code irg)
512 static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
514 ir_mode *mode = get_irn_mode(init);
515 int bytes = get_mode_size_bytes(mode);
517 dump_size_type(bytes);
518 do_dump_atomic_init(env, init);
520 be_emit_write_line();
523 /************************************************************************/
524 /* Routines to dump global variables */
525 /************************************************************************/
527 static int initializer_is_string_const(const ir_initializer_t *initializer)
530 int found_printable = 0;
532 if(initializer->kind != IR_INITIALIZER_COMPOUND)
535 len = initializer->compound.n_initializers;
538 for(i = 0; i < len; ++i) {
542 ir_initializer_t *sub_initializer
543 = initializer->compound.initializers[i];
545 if(sub_initializer->kind != IR_INITIALIZER_TARVAL)
548 tv = sub_initializer->tarval.value;
549 mode = get_tarval_mode(tv);
551 if (!mode_is_int(mode)
552 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
555 c = get_tarval_long(tv);
556 if (isgraph(c) || isspace(c))
561 if (i == len - 1 && c != '\0')
565 return found_printable;
569 * Determine if an entity is a string constant
570 * @param ent The entity
571 * @return 1 if it is a string constant, 0 otherwise
573 static int ent_is_string_const(ir_entity *ent)
575 ir_type *type, *element_type;
578 int found_printable = 0;
580 type = get_entity_type(ent);
582 /* if it's an array */
583 if (!is_Array_type(type))
586 element_type = get_array_element_type(type);
588 /* and the array's element type is primitive */
589 if (!is_Primitive_type(element_type))
592 /* and the mode of the element type is an int of
593 * the same size as the byte mode */
594 mode = get_type_mode(element_type);
595 if (!mode_is_int(mode)
596 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
599 if(ent->has_initializer) {
603 /* if it contains only printable chars and a 0 at the end */
604 n = get_compound_ent_n_values(ent);
605 for (i = 0; i < n; ++i) {
606 ir_node *irn = get_compound_ent_value(ent, i);
610 c = (int) get_tarval_long(get_Const_tarval(irn));
612 if (isgraph(c) || isspace(c))
617 if (i == n - 1 && c != '\0')
622 /* then we can emit it as a string constant */
623 return found_printable;
627 * Dump a string constant.
628 * No checks are made!!
630 * @param ent The entity to dump.
632 static void dump_string_cst(ir_entity *ent)
640 len = get_compound_ent_n_values(ent);
642 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
643 be_emit_cstring("\t.ascii \"");
645 be_emit_cstring("\t.string \"");
649 for (i = 0; i < output_len; ++i) {
653 irn = get_compound_ent_value(ent, i);
654 c = (int) get_tarval_long(get_Const_tarval(irn));
657 case '"' : be_emit_cstring("\\\""); break;
658 case '\n': be_emit_cstring("\\n"); break;
659 case '\r': be_emit_cstring("\\r"); break;
660 case '\t': be_emit_cstring("\\t"); break;
661 case '\\': be_emit_cstring("\\\\"); break;
666 be_emit_irprintf("\\%o", c);
670 be_emit_cstring("\"\n");
671 be_emit_write_line();
673 type = get_entity_type(ent);
674 type_size = get_type_size_bytes(type);
675 remaining_space = type_size - len;
676 assert(remaining_space >= 0);
677 if(remaining_space > 0) {
678 be_emit_irprintf("\t.space\t%d\n", remaining_space);
682 static void dump_string_initializer(const ir_initializer_t *initializer)
686 len = initializer->compound.n_initializers;
687 if(be_gas_flavour == GAS_FLAVOUR_MACH_O) {
688 be_emit_cstring("\t.ascii \"");
690 be_emit_cstring("\t.string \"");
694 for(i = 0; i < len; ++i) {
695 const ir_initializer_t *sub_initializer
696 = get_initializer_compound_value(initializer, i);
698 tarval *tv = get_initializer_tarval_value(sub_initializer);
699 int c = get_tarval_long(tv);
702 case '"' : be_emit_cstring("\\\""); break;
703 case '\n': be_emit_cstring("\\n"); break;
704 case '\r': be_emit_cstring("\\r"); break;
705 case '\t': be_emit_cstring("\\t"); break;
706 case '\\': be_emit_cstring("\\\\"); break;
711 be_emit_irprintf("\\%o", c);
715 be_emit_cstring("\"\n");
716 be_emit_write_line();
719 enum normal_or_bitfield_kind {
726 enum normal_or_bitfield_kind kind;
730 unsigned char bf_val;
732 } normal_or_bitfield;
734 static int is_type_variable_size(ir_type *type)
741 static size_t get_initializer_size(const ir_initializer_t *initializer,
744 switch(get_initializer_kind(initializer)) {
745 case IR_INITIALIZER_TARVAL: {
746 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
747 return get_type_size_bytes(type);
749 case IR_INITIALIZER_CONST:
750 case IR_INITIALIZER_NULL:
751 return get_type_size_bytes(type);
752 case IR_INITIALIZER_COMPOUND: {
753 if(!is_type_variable_size(type)) {
754 return get_type_size_bytes(type);
757 = get_initializer_compound_n_entries(initializer);
759 unsigned initializer_size = get_type_size_bytes(type);
760 for(i = 0; i < n_entries; ++i) {
761 ir_entity *entity = get_compound_member(type, i);
762 ir_type *type = get_entity_type(entity);
764 const ir_initializer_t *sub_initializer
765 = get_initializer_compound_value(initializer, i);
767 unsigned offset = get_entity_offset(entity);
768 unsigned size = get_initializer_size(sub_initializer, type);
770 if(offset + size > initializer_size) {
771 initializer_size = offset + size;
774 return initializer_size;
779 panic("found invalid initializer");
783 static normal_or_bitfield *glob_vals;
784 static size_t max_vals;
787 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
788 const ir_initializer_t *initializer, ir_type *type)
790 unsigned char last_bits = 0;
791 ir_mode *mode = get_type_mode(type);
793 unsigned char curr_bits;
797 switch(get_initializer_kind(initializer)) {
798 case IR_INITIALIZER_NULL:
800 case IR_INITIALIZER_TARVAL:
801 tv = get_initializer_tarval_value(initializer);
803 case IR_INITIALIZER_CONST: {
804 ir_node *node = get_initializer_const_value(initializer);
805 if(!is_Const(node)) {
806 panic("bitfield initializer not a Const node");
808 tv = get_Const_tarval(node);
811 case IR_INITIALIZER_COMPOUND:
812 panic("bitfield initializer is compound");
815 panic("Couldn't get numeric value for bitfield initializer");
817 tv = tarval_convert_to(tv, get_type_mode(type));
819 /* normalize offset */
820 vals += offset_bits >> 3;
822 value_len = get_mode_size_bits(mode);
824 /* combine bits with existing bits */
825 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
826 assert((size_t) (vals - glob_vals) + j < max_vals);
827 assert(vals[j].kind == BITFIELD ||
828 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
829 vals[j].kind = BITFIELD;
830 curr_bits = get_tarval_sub_bits(tv, j);
832 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
834 last_bits = curr_bits;
838 static void dump_ir_initializer(normal_or_bitfield *vals,
839 const ir_initializer_t *initializer,
842 assert((size_t) (vals - glob_vals) < max_vals);
844 switch(get_initializer_kind(initializer)) {
845 case IR_INITIALIZER_NULL:
847 case IR_INITIALIZER_TARVAL: {
850 assert(vals->kind != BITFIELD);
852 vals->v.tarval = get_initializer_tarval_value(initializer);
853 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
854 for(i = 1; i < get_type_size_bytes(type); ++i) {
855 vals[i].kind = NORMAL;
856 vals[i].v.value = NULL;
860 case IR_INITIALIZER_CONST: {
863 assert(vals->kind != BITFIELD);
865 vals->v.value = get_initializer_const_value(initializer);
866 for(i = 1; i < get_type_size_bytes(type); ++i) {
867 vals[i].kind = NORMAL;
868 vals[i].v.value = NULL;
872 case IR_INITIALIZER_COMPOUND: {
874 size_t n = get_initializer_compound_n_entries(initializer);
876 if(is_Array_type(type)) {
877 ir_type *element_type = get_array_element_type(type);
878 size_t skip = get_type_size_bytes(element_type);
879 size_t alignment = get_type_alignment_bytes(element_type);
880 size_t misalign = skip % alignment;
882 skip += alignment - misalign;
885 for(i = 0; i < n; ++i) {
886 ir_initializer_t *sub_initializer
887 = get_initializer_compound_value(initializer, i);
889 dump_ir_initializer(vals, sub_initializer, element_type);
895 assert(is_compound_type(type));
896 n_members = get_compound_n_members(type);
897 for(i = 0; i < n_members; ++i) {
898 ir_entity *member = get_compound_member(type, i);
899 size_t offset = get_entity_offset(member);
900 ir_type *subtype = get_entity_type(member);
901 ir_mode *mode = get_type_mode(subtype);
902 ir_initializer_t *sub_initializer;
904 assert(i < get_initializer_compound_n_entries(initializer));
906 = get_initializer_compound_value(initializer, i);
910 = get_entity_offset_bits_remainder(member);
911 size_t value_len = get_mode_size_bits(mode);
913 if(offset_bits != 0 ||
914 (value_len != 8 && value_len != 16 && value_len != 32
915 && value_len != 64)) {
916 dump_bitfield(&vals[offset], offset_bits,
917 sub_initializer, subtype);
922 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
929 panic("invalid ir_initializer kind found");
932 static void dump_initializer(be_gas_decl_env_t *env, ir_entity *entity)
934 const ir_initializer_t *initializer = entity->attr.initializer;
936 normal_or_bitfield *vals;
940 if(initializer_is_string_const(initializer)) {
941 dump_string_initializer(initializer);
945 type = get_entity_type(entity);
946 size = get_initializer_size(initializer, type);
952 * In the worst case, every initializer allocates one byte.
953 * Moreover, initializer might be big, do not allocate on stack.
955 vals = XMALLOCNZ(normal_or_bitfield, size);
962 dump_ir_initializer(vals, initializer, type);
964 /* now write values sorted */
965 for (k = 0; k < size; ) {
968 if (vals[k].kind == NORMAL) {
969 if(vals[k].v.value != NULL) {
970 dump_atomic_init(env, vals[k].v.value);
971 elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
975 } else if(vals[k].kind == TARVAL) {
976 tarval *tv = vals[k].v.tarval;
977 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
982 dump_size_type(size);
983 dump_arith_tarval(tv, size);
985 be_emit_write_line();
987 assert(vals[k].kind == BITFIELD);
988 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
989 be_emit_write_line();
993 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1000 be_emit_irprintf("\t.space\t%d\n", space);
1001 be_emit_write_line();
1008 * Dump an initializer for a compound entity.
1010 static void dump_compound_init(be_gas_decl_env_t *env, ir_entity *ent)
1012 normal_or_bitfield *vals;
1014 unsigned k, last_ofs;
1016 if(ent->has_initializer) {
1017 dump_initializer(env, ent);
1021 n = get_compound_ent_n_values(ent);
1023 /* Find the initializer size. Sorrily gcc support a nasty feature:
1024 The last field of a compound may be a flexible array. This allows
1025 initializers bigger than the type size. */
1026 last_ofs = get_type_size_bytes(get_entity_type(ent));
1027 for (i = 0; i < n; ++i) {
1028 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1029 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1030 ir_node *value = get_compound_ent_value(ent, i);
1031 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
1033 offset += (value_len + bits_remainder + 7) >> 3;
1035 if (offset > last_ofs) {
1041 * In the worst case, every initializer allocates one byte.
1042 * Moreover, initializer might be big, do not allocate on stack.
1044 vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1046 /* collect the values and store them at the offsets */
1047 for (i = 0; i < n; ++i) {
1048 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1049 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1050 ir_node *value = get_compound_ent_value(ent, i);
1051 int value_len = get_mode_size_bits(get_irn_mode(value));
1053 assert(offset_bits >= 0);
1055 if (offset_bits != 0 ||
1056 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1057 tarval *tv = get_atomic_init_tv(value);
1058 unsigned char curr_bits, last_bits = 0;
1060 panic("Couldn't get numeric value for bitfield initializer '%s'",
1061 get_entity_ld_name(ent));
1063 /* normalize offset */
1064 offset += offset_bits >> 3;
1067 for (j = 0; value_len + offset_bits > 0; ++j) {
1068 assert(offset + j < last_ofs);
1069 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1070 vals[offset + j].kind = BITFIELD;
1071 curr_bits = get_tarval_sub_bits(tv, j);
1072 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1074 last_bits = curr_bits;
1079 assert(offset < last_ofs);
1080 assert(vals[offset].kind == NORMAL);
1081 for (i = 1; i < value_len / 8; ++i) {
1082 assert(vals[offset + i].v.value == NULL);
1084 vals[offset].v.value = value;
1088 /* now write them sorted */
1089 for (k = 0; k < last_ofs; ) {
1090 int space = 0, skip = 0;
1091 if (vals[k].kind == NORMAL) {
1092 if(vals[k].v.value != NULL) {
1093 dump_atomic_init(env, vals[k].v.value);
1094 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1099 assert(vals[k].kind == BITFIELD);
1100 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1104 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1113 be_emit_irprintf("\t.space\t%d\n", space);
1114 be_emit_write_line();
1120 static void emit_align(unsigned alignment)
1122 if (!is_po2(alignment))
1123 panic("alignment not a power of 2");
1125 be_emit_irprintf("\t.p2align\t%u\n", log2_floor(alignment));
1126 be_emit_write_line();
1130 * Dump a global entity.
1132 * @param env the gas output environment
1133 * @param ent the entity to be dumped
1135 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent)
1137 ir_type *type = get_entity_type(ent);
1138 ident *ld_ident = get_entity_ld_ident(ent);
1139 unsigned align = get_type_alignment_bytes(type);
1140 int emit_as_common = 0;
1141 be_gas_section_t section = env->section;
1142 ir_variability variability = get_entity_variability(ent);
1143 ir_visibility visibility = get_entity_visibility(ent);
1145 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1149 if (section != (be_gas_section_t) -1) {
1151 } else if (variability == variability_constant) {
1152 /* a constant entity, put it on the rdata */
1153 section = GAS_SECTION_RODATA;
1154 if (be_gas_flavour == GAS_FLAVOUR_MACH_O
1155 && ent_is_string_const(ent)) {
1156 section = GAS_SECTION_CSTRING;
1158 } else if (variability == variability_uninitialized) {
1159 /* uninitialized entity put it in bss segment */
1160 section = GAS_SECTION_COMMON;
1161 if (visibility != visibility_local)
1164 section = GAS_SECTION_DATA;
1167 if(!emit_as_common) {
1168 be_gas_emit_switch_section(section);
1171 be_dbg_variable(ent);
1173 /* global or not global */
1174 if (visibility == visibility_external_visible && !emit_as_common) {
1175 be_emit_cstring(".globl\t");
1176 be_emit_ident(ld_ident);
1178 be_emit_write_line();
1179 } else if(visibility == visibility_external_allocated) {
1180 be_emit_cstring(".globl\t");
1181 be_emit_ident(ld_ident);
1183 be_emit_write_line();
1184 /* we can return now... */
1188 if (align > 1 && !emit_as_common && section != GAS_SECTION_PIC_TRAMPOLINES
1189 && section != GAS_SECTION_PIC_SYMBOLS) {
1193 if (visibility != visibility_external_allocated && !emit_as_common
1194 && be_gas_flavour == GAS_FLAVOUR_ELF) {
1195 be_emit_cstring("\t.type\t");
1196 be_emit_ident(ld_ident);
1197 be_emit_cstring(", @object\n\t.size\t");
1198 be_emit_ident(ld_ident);
1199 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1202 if (!emit_as_common) {
1203 be_emit_ident(ld_ident);
1204 be_emit_cstring(":\n");
1205 be_emit_write_line();
1208 if (variability == variability_uninitialized) {
1209 if (emit_as_common) {
1210 switch (be_gas_flavour) {
1211 case GAS_FLAVOUR_ELF:
1212 case GAS_FLAVOUR_MACH_O:
1213 case GAS_FLAVOUR_YASM:
1214 be_emit_irprintf("\t.comm %s,%u,%u\n",
1215 get_id_str(ld_ident), get_type_size_bytes(type), align);
1216 be_emit_write_line();
1218 case GAS_FLAVOUR_MINGW:
1219 be_emit_irprintf("\t.comm %s,%u # %u\n",
1220 get_id_str(ld_ident), get_type_size_bytes(type), align);
1221 be_emit_write_line();
1224 } else if (section == GAS_SECTION_PIC_TRAMPOLINES
1225 || section == GAS_SECTION_PIC_SYMBOLS) {
1226 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
1227 be_emit_cstring("\t.indirect_symbol ");
1228 be_emit_ident(get_entity_ident(ent));
1230 be_emit_write_line();
1231 if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1232 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1233 be_emit_write_line();
1235 assert(section == GAS_SECTION_PIC_SYMBOLS);
1236 be_emit_cstring("\t.long 0\n");
1237 be_emit_write_line();
1240 panic("PIC trampolines not yet supported in this gas mode");
1243 be_emit_irprintf("\t.space %u\n", get_type_size_bytes(type));
1244 be_emit_write_line();
1247 if (is_atomic_entity(ent)) {
1248 dump_atomic_init(env, get_atomic_ent_value(ent));
1250 /* sort_compound_ent_values(ent); */
1252 switch (get_type_tpop_code(get_entity_type(ent))) {
1254 if (ent_is_string_const(ent))
1255 dump_string_cst(ent);
1257 dump_compound_init(env, ent);
1262 dump_compound_init(env, ent);
1265 panic("Unimplemented type kind in dump_global()");
1272 * Dumps declarations of global variables and the initialization code.
1274 * @param gt a global like type, either the global or the TLS one
1275 * @param env an environment
1276 * @param only_emit_marked if non-zero, external allocated entities that do not have
1277 * its visited flag set are ignored
1279 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
1280 int only_emit_marked)
1282 int i, n = get_compound_n_members(gt);
1283 waitq *worklist = new_waitq();
1285 if (only_emit_marked) {
1286 for (i = 0; i < n; i++) {
1287 ir_entity *ent = get_compound_member(gt, i);
1288 if (is_entity_backend_marked(ent) ||
1289 get_entity_visibility(ent) != visibility_external_allocated) {
1290 waitq_put(worklist, ent);
1291 set_entity_backend_marked(ent, 1);
1295 for (i = 0; i < n; i++) {
1296 ir_entity *ent = get_compound_member(gt, i);
1297 set_entity_backend_marked(ent, 1);
1298 waitq_put(worklist, ent);
1302 env->worklist = worklist;
1304 while (!waitq_empty(worklist)) {
1305 ir_entity *ent = waitq_get(worklist);
1307 dump_global(env, ent);
1310 del_waitq(worklist);
1311 env->worklist = NULL;
1314 /************************************************************************/
1316 /* Generate all entities. */
1317 void be_gas_emit_decls(const be_main_env_t *main_env,
1318 int only_emit_marked_entities)
1320 be_gas_decl_env_t env;
1321 memset(&env, 0, sizeof(env));
1323 env.main_env = main_env;
1325 /* dump global type */
1326 env.section = (be_gas_section_t) -1;
1327 be_gas_dump_globals(get_glob_type(), &env, only_emit_marked_entities);
1328 env.section = GAS_SECTION_TLS;
1329 be_gas_dump_globals(get_tls_type(), &env, only_emit_marked_entities);
1330 env.section = GAS_SECTION_CONSTRUCTORS;
1331 be_gas_dump_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env,
1332 only_emit_marked_entities);
1333 env.section = GAS_SECTION_DESTRUCTORS;
1334 be_gas_dump_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env,
1335 only_emit_marked_entities);
1337 env.section = GAS_SECTION_PIC_SYMBOLS;
1338 be_gas_dump_globals(main_env->pic_symbols_type, &env,
1339 only_emit_marked_entities);
1341 if (get_compound_n_members(main_env->pic_trampolines_type) > 0) {
1342 env.section = GAS_SECTION_PIC_TRAMPOLINES;
1343 be_gas_dump_globals(main_env->pic_trampolines_type, &env,
1344 only_emit_marked_entities);
1345 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
1346 be_emit_cstring("\t.subsections_via_symbols\n");
1347 be_emit_write_line();