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
47 #include "beemitter.h"
48 #include "be_dbgout.h"
50 /** by default, we generate assembler code for the Linux gas */
51 be_gas_flavour_t be_gas_flavour = GAS_FLAVOUR_ELF;
53 static be_gas_section_t current_section = (be_gas_section_t) -1;
56 * Return the pseudo-instruction to be issued for a section switch
57 * depending on the current flavour.
59 * @param section the section to switch to
61 * @return the pseudo-instruction
63 static const char *get_section_name(be_gas_section_t section) {
64 static const char *text[GAS_FLAVOUR_LAST+1][GAS_SECTION_LAST+1] = {
65 { /* GAS_FLAVOUR_ELF */
70 ".section\t.tbss,\"awT\",@nobits",
71 ".section\t.ctors,\"aw\",@progbits",
72 ".section\t.dtors,\"aw\",@progbits",
73 NULL, /* no cstring section */
77 { /* GAS_FLAVOUR_MINGW */
80 ".section .rdata,\"dr\"",
82 ".section\t.tbss,\"awT\",@nobits",
83 ".section\t.ctors,\"aw\",@progbits",
84 ".section\t.dtors,\"aw\",@progbits",
89 { /* GAS_FLAVOUR_YASM */
94 ".section\t.tbss,\"awT\",@nobits",
95 ".section\t.ctors,\"aw\",@progbits",
96 ".section\t.dtors,\"aw\",@progbits",
101 { /* GAS_FLAVOUR_MACH_O */
106 NULL, /* TLS is not supported on Mach-O */
108 NULL, /* TODO: how is this called? */
110 ".section\t__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5",
111 ".section\t__IMPORT,__pointers,non_lazy_symbol_pointers"
115 assert((int) be_gas_flavour >= 0 && be_gas_flavour <= GAS_FLAVOUR_LAST);
116 assert((int) section >= 0 && section <= GAS_SECTION_LAST);
117 return text[be_gas_flavour][section];
120 void be_gas_emit_switch_section(be_gas_section_t section) {
121 if(current_section == section)
125 be_emit_string(get_section_name(section));
127 be_emit_write_line();
128 current_section = section;
131 void be_gas_emit_function_prolog(ir_entity *entity, unsigned alignment)
133 const char *name = get_entity_ld_name(entity);
134 const char *fill_byte = "";
135 unsigned maximum_skip;
137 be_gas_emit_switch_section(GAS_SECTION_TEXT);
139 /* write the begin line (used by scripts processing the assembler... */
140 be_emit_write_line();
141 be_emit_cstring("# -- Begin ");
142 be_emit_string(name);
144 be_emit_write_line();
146 /* gcc fills space between function with 0x90, no idea if this is needed */
147 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
152 maximum_skip = (1 << alignment) - 1;
153 be_emit_cstring("\t.p2align ");
154 be_emit_irprintf("%u,%s,%u\n", alignment, fill_byte, maximum_skip);
155 be_emit_write_line();
157 if (get_entity_visibility(entity) == visibility_external_visible) {
158 be_emit_cstring(".globl ");
159 be_emit_string(name);
161 be_emit_write_line();
164 switch (be_gas_flavour) {
165 case GAS_FLAVOUR_ELF:
166 be_emit_cstring("\t.type\t");
167 be_emit_string(name);
168 be_emit_cstring(", @function\n");
169 be_emit_write_line();
171 case GAS_FLAVOUR_MINGW:
172 be_emit_cstring("\t.def\t");
173 be_emit_string(name);
174 if (get_entity_visibility(entity) == visibility_external_visible) {
175 be_emit_cstring(";\t.scl\t2;\t.type\t32;\t.endef\n");
177 be_emit_cstring(";\t.scl\t3;\t.type\t32;\t.endef\n");
179 be_emit_write_line();
181 case GAS_FLAVOUR_MACH_O:
182 case GAS_FLAVOUR_YASM:
185 be_emit_string(name);
186 be_emit_cstring(":\n");
187 be_emit_write_line();
190 void be_gas_emit_function_epilog(ir_entity *entity)
192 const char *name = get_entity_ld_name(entity);
194 if (be_gas_flavour == GAS_FLAVOUR_ELF) {
195 be_emit_cstring("\t.size\t");
196 be_emit_string(name);
197 be_emit_cstring(", .-");
198 be_emit_string(name);
200 be_emit_write_line();
203 be_emit_cstring("# -- End ");
204 be_emit_string(name);
206 be_emit_write_line();
210 * An environment containing all needed dumper data.
211 * Currently we create the file completely in memory first, then
212 * write it to the disk. This is an artifact from the old C-generating backend
213 * and even there NOT needed. So we might change it in the future.
215 typedef struct _be_gas_decl_env {
216 const be_main_env_t *main_env; /**< The main backend environment, used for it's debug handle. */
217 be_gas_section_t section;
218 waitq *worklist; /**< A worklist we use to place not yet handled entities on. */
221 /************************************************************************/
226 * @param tv the tarval
227 * @param bytes the width of the tarvals value in bytes
229 static void dump_arith_tarval(tarval *tv, int bytes)
233 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
237 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
241 be_emit_irprintf("0x%02x%02x%02x%02x",
242 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
246 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
247 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
248 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
252 /* Beware: Mixed endian output! One little endian number emitted as
253 * three longs. Each long initializer is written in big endian. */
255 "\t.long\t0x%02x%02x%02x%02x\n"
256 "\t.long\t0x%02x%02x%02x%02x\n"
257 "\t.long\t0x%02x%02x%02x%02x",
258 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
259 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0),
260 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
261 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
262 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
263 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8)
269 "\t.long\t0x%02x%02x%02x%02x0x%02x%02x%02x%02x0x%02x%02x%02x%02x0x%02x%02x%02x%02x",
270 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
271 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
272 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
273 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
274 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
275 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
276 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
277 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0)
282 panic("Can't dump a tarval with %d bytes", bytes);
286 * Return the label prefix for labeled blocks.
288 const char *be_gas_block_label_prefix(void) {
293 * Return the label prefix for labeled instructions.
295 const char *be_gas_insn_label_prefix(void) {
302 static void dump_label(ir_label_t label) {
303 be_emit_irprintf("%s%lu", be_gas_block_label_prefix(), label);
307 * Return the tarval of an atomic initializer.
309 * @param init a node representing the initializer (on the const code irg)
313 static tarval *get_atomic_init_tv(ir_node *init)
316 ir_mode *mode = get_irn_mode(init);
318 switch (get_irn_opcode(init)) {
321 init = get_Cast_op(init);
325 init = get_Conv_op(init);
329 return get_Const_tarval(init);
332 switch (get_SymConst_kind(init)) {
333 case symconst_type_size:
334 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
336 case symconst_type_align:
337 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
339 case symconst_ofs_ent:
340 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
342 case symconst_enum_const:
343 return get_enumeration_value(get_SymConst_enum(init));
359 * Dump an atomic value.
361 * @param env the gas output environment
362 * @param init a node representing the atomic value (on the const code irg)
364 static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
366 ir_mode *mode = get_irn_mode(init);
367 int bytes = get_mode_size_bytes(mode);
372 init = skip_Id(init);
374 switch (get_irn_opcode(init)) {
376 do_dump_atomic_init(env, get_Cast_op(init));
380 do_dump_atomic_init(env, get_Conv_op(init));
384 tv = get_Const_tarval(init);
386 /* it's a arithmetic value */
387 dump_arith_tarval(tv, bytes);
391 switch (get_SymConst_kind(init)) {
392 case symconst_addr_name:
393 be_emit_ident(get_SymConst_name(init));
396 case symconst_addr_ent:
397 ent = get_SymConst_entity(init);
398 if(!is_entity_backend_marked(ent)) {
399 waitq_put(env->worklist, ent);
400 set_entity_backend_marked(ent, 1);
402 be_emit_ident(get_entity_ld_ident(ent));
405 case symconst_ofs_ent:
406 ent = get_SymConst_entity(init);
407 #if 0 /* not needed, is it? */
408 if(!is_entity_backend_marked(ent)) {
409 waitq_put(env->worklist, ent);
410 set_entity_backend_marked(ent, 1);
413 be_emit_irprintf("%d", get_entity_offset(ent));
416 case symconst_type_size:
417 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
420 case symconst_type_align:
421 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
424 case symconst_enum_const:
425 tv = get_enumeration_value(get_SymConst_enum(init));
426 dump_arith_tarval(tv, bytes);
430 label = get_SymConst_label(init);
435 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
440 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
441 panic("Constant must be int or pointer for '+' to work");
443 do_dump_atomic_init(env, get_Add_left(init));
444 be_emit_cstring(" + ");
445 do_dump_atomic_init(env, get_Add_right(init));
449 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
450 panic("Constant must be int or pointer for '-' to work");
452 do_dump_atomic_init(env, get_Sub_left(init));
453 be_emit_cstring(" - ");
454 do_dump_atomic_init(env, get_Sub_right(init));
458 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
459 panic("Constant must be int or pointer for '*' to work");
461 do_dump_atomic_init(env, get_Mul_left(init));
462 be_emit_cstring(" * ");
463 do_dump_atomic_init(env, get_Mul_right(init));
467 panic("dump_atomic_init(): unsupported IR-node %+F", init);
472 * Dumps the type for given size (.byte, .long, ...)
474 * @param size the size in bytes
476 static void dump_size_type(size_t size) {
479 be_emit_cstring("\t.byte\t");
483 be_emit_cstring("\t.word\t");
487 be_emit_cstring("\t.long\t");
491 be_emit_cstring("\t.quad\t");
496 /* handled in arith */
500 be_emit_cstring("\t.octa\t");
504 panic("Try to dump a type with %u bytes", (unsigned)size);
509 * Emit an atomic value.
511 * @param env the gas output environment
512 * @param init a node representing the atomic value (on the const code irg)
514 static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
516 ir_mode *mode = get_irn_mode(init);
517 int bytes = get_mode_size_bytes(mode);
519 dump_size_type(bytes);
520 do_dump_atomic_init(env, init);
522 be_emit_write_line();
525 /************************************************************************/
526 /* Routines to dump global variables */
527 /************************************************************************/
529 static int initializer_is_string_const(const ir_initializer_t *initializer)
532 int found_printable = 0;
534 if(initializer->kind != IR_INITIALIZER_COMPOUND)
537 len = initializer->compound.n_initializers;
540 for(i = 0; i < len; ++i) {
544 ir_initializer_t *sub_initializer
545 = initializer->compound.initializers[i];
547 if(sub_initializer->kind != IR_INITIALIZER_TARVAL)
550 tv = sub_initializer->tarval.value;
551 mode = get_tarval_mode(tv);
553 if (!mode_is_int(mode)
554 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
557 c = get_tarval_long(tv);
558 if (isgraph(c) || isspace(c))
563 if (i == len - 1 && c != '\0')
567 return found_printable;
571 * Determine if an entity is a string constant
572 * @param ent The entity
573 * @return 1 if it is a string constant, 0 otherwise
575 static int ent_is_string_const(ir_entity *ent)
577 ir_type *type, *element_type;
580 int found_printable = 0;
582 type = get_entity_type(ent);
584 /* if it's an array */
585 if (!is_Array_type(type))
588 element_type = get_array_element_type(type);
590 /* and the array's element type is primitive */
591 if (!is_Primitive_type(element_type))
594 /* and the mode of the element type is an int of
595 * the same size as the byte mode */
596 mode = get_type_mode(element_type);
597 if (!mode_is_int(mode)
598 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
601 if(ent->has_initializer) {
605 /* if it contains only printable chars and a 0 at the end */
606 n = get_compound_ent_n_values(ent);
607 for (i = 0; i < n; ++i) {
608 ir_node *irn = get_compound_ent_value(ent, i);
612 c = (int) get_tarval_long(get_Const_tarval(irn));
614 if (isgraph(c) || isspace(c))
619 if (i == n - 1 && c != '\0')
624 /* then we can emit it as a string constant */
625 return found_printable;
629 * Dump a string constant.
630 * No checks are made!!
632 * @param ent The entity to dump.
634 static void dump_string_cst(ir_entity *ent)
642 len = get_compound_ent_n_values(ent);
644 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
645 be_emit_cstring("\t.ascii \"");
647 be_emit_cstring("\t.string \"");
651 for (i = 0; i < output_len; ++i) {
655 irn = get_compound_ent_value(ent, i);
656 c = (int) get_tarval_long(get_Const_tarval(irn));
659 case '"' : be_emit_cstring("\\\""); break;
660 case '\n': be_emit_cstring("\\n"); break;
661 case '\r': be_emit_cstring("\\r"); break;
662 case '\t': be_emit_cstring("\\t"); break;
663 case '\\': be_emit_cstring("\\\\"); break;
668 be_emit_irprintf("\\%o", c);
672 be_emit_cstring("\"\n");
673 be_emit_write_line();
675 type = get_entity_type(ent);
676 type_size = get_type_size_bytes(type);
677 remaining_space = type_size - len;
678 assert(remaining_space >= 0);
679 if(remaining_space > 0) {
680 be_emit_irprintf("\t.space\t%d\n", remaining_space);
684 static void dump_string_initializer(const ir_initializer_t *initializer)
688 len = initializer->compound.n_initializers;
689 if(be_gas_flavour == GAS_FLAVOUR_MACH_O) {
690 be_emit_cstring("\t.ascii \"");
692 be_emit_cstring("\t.string \"");
696 for(i = 0; i < len; ++i) {
697 const ir_initializer_t *sub_initializer
698 = get_initializer_compound_value(initializer, i);
700 tarval *tv = get_initializer_tarval_value(sub_initializer);
701 int c = get_tarval_long(tv);
704 case '"' : be_emit_cstring("\\\""); break;
705 case '\n': be_emit_cstring("\\n"); break;
706 case '\r': be_emit_cstring("\\r"); break;
707 case '\t': be_emit_cstring("\\t"); break;
708 case '\\': be_emit_cstring("\\\\"); break;
713 be_emit_irprintf("\\%o", c);
717 be_emit_cstring("\"\n");
718 be_emit_write_line();
721 enum normal_or_bitfield_kind {
728 enum normal_or_bitfield_kind kind;
732 unsigned char bf_val;
734 } normal_or_bitfield;
736 static int is_type_variable_size(ir_type *type)
743 static size_t get_initializer_size(const ir_initializer_t *initializer,
746 switch(get_initializer_kind(initializer)) {
747 case IR_INITIALIZER_TARVAL: {
748 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
749 return get_type_size_bytes(type);
751 case IR_INITIALIZER_CONST:
752 case IR_INITIALIZER_NULL:
753 return get_type_size_bytes(type);
754 case IR_INITIALIZER_COMPOUND: {
755 if(!is_type_variable_size(type)) {
756 return get_type_size_bytes(type);
759 = get_initializer_compound_n_entries(initializer);
761 unsigned initializer_size = get_type_size_bytes(type);
762 for(i = 0; i < n_entries; ++i) {
763 ir_entity *entity = get_compound_member(type, i);
764 ir_type *type = get_entity_type(entity);
766 const ir_initializer_t *sub_initializer
767 = get_initializer_compound_value(initializer, i);
769 unsigned offset = get_entity_offset(entity);
770 unsigned size = get_initializer_size(sub_initializer, type);
772 if(offset + size > initializer_size) {
773 initializer_size = offset + size;
776 return initializer_size;
781 panic("found invalid initializer");
785 static normal_or_bitfield *glob_vals;
786 static size_t max_vals;
789 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
790 const ir_initializer_t *initializer, ir_type *type)
792 unsigned char last_bits = 0;
793 ir_mode *mode = get_type_mode(type);
795 unsigned char curr_bits;
799 switch(get_initializer_kind(initializer)) {
800 case IR_INITIALIZER_NULL:
802 case IR_INITIALIZER_TARVAL:
803 tv = get_initializer_tarval_value(initializer);
805 case IR_INITIALIZER_CONST: {
806 ir_node *node = get_initializer_const_value(initializer);
807 if(!is_Const(node)) {
808 panic("bitfield initializer not a Const node");
810 tv = get_Const_tarval(node);
813 case IR_INITIALIZER_COMPOUND:
814 panic("bitfield initializer is compound");
817 panic("Couldn't get numeric value for bitfield initializer");
819 tv = tarval_convert_to(tv, get_type_mode(type));
821 /* normalize offset */
822 vals += offset_bits >> 3;
824 value_len = get_mode_size_bits(mode);
826 /* combine bits with existing bits */
827 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
828 assert((size_t) (vals - glob_vals) + j < max_vals);
829 assert(vals[j].kind == BITFIELD ||
830 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
831 vals[j].kind = BITFIELD;
832 curr_bits = get_tarval_sub_bits(tv, j);
834 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
836 last_bits = curr_bits;
840 static void dump_ir_initializer(normal_or_bitfield *vals,
841 const ir_initializer_t *initializer,
844 assert((size_t) (vals - glob_vals) < max_vals);
846 switch(get_initializer_kind(initializer)) {
847 case IR_INITIALIZER_NULL:
849 case IR_INITIALIZER_TARVAL: {
852 assert(vals->kind != BITFIELD);
854 vals->v.tarval = get_initializer_tarval_value(initializer);
855 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
856 for(i = 1; i < get_type_size_bytes(type); ++i) {
857 vals[i].kind = NORMAL;
858 vals[i].v.value = NULL;
862 case IR_INITIALIZER_CONST: {
865 assert(vals->kind != BITFIELD);
867 vals->v.value = get_initializer_const_value(initializer);
868 for(i = 1; i < get_type_size_bytes(type); ++i) {
869 vals[i].kind = NORMAL;
870 vals[i].v.value = NULL;
874 case IR_INITIALIZER_COMPOUND: {
876 size_t n = get_initializer_compound_n_entries(initializer);
878 if(is_Array_type(type)) {
879 ir_type *element_type = get_array_element_type(type);
880 size_t skip = get_type_size_bytes(element_type);
881 size_t alignment = get_type_alignment_bytes(element_type);
882 size_t misalign = skip % alignment;
884 skip += alignment - misalign;
887 for(i = 0; i < n; ++i) {
888 ir_initializer_t *sub_initializer
889 = get_initializer_compound_value(initializer, i);
891 dump_ir_initializer(vals, sub_initializer, element_type);
897 assert(is_compound_type(type));
898 n_members = get_compound_n_members(type);
899 for(i = 0; i < n_members; ++i) {
900 ir_entity *member = get_compound_member(type, i);
901 size_t offset = get_entity_offset(member);
902 ir_type *subtype = get_entity_type(member);
903 ir_mode *mode = get_type_mode(subtype);
904 ir_initializer_t *sub_initializer;
906 assert(i < get_initializer_compound_n_entries(initializer));
908 = get_initializer_compound_value(initializer, i);
912 = get_entity_offset_bits_remainder(member);
913 size_t value_len = get_mode_size_bits(mode);
915 if(offset_bits != 0 ||
916 (value_len != 8 && value_len != 16 && value_len != 32
917 && value_len != 64)) {
918 dump_bitfield(&vals[offset], offset_bits,
919 sub_initializer, subtype);
924 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
931 panic("invalid ir_initializer kind found");
934 static void dump_initializer(be_gas_decl_env_t *env, ir_entity *entity)
936 const ir_initializer_t *initializer = entity->attr.initializer;
938 normal_or_bitfield *vals;
942 if(initializer_is_string_const(initializer)) {
943 dump_string_initializer(initializer);
947 type = get_entity_type(entity);
948 size = get_initializer_size(initializer, type);
954 * In the worst case, every initializer allocates one byte.
955 * Moreover, initializer might be big, do not allocate on stack.
957 vals = XMALLOCNZ(normal_or_bitfield, size);
964 dump_ir_initializer(vals, initializer, type);
966 /* now write values sorted */
967 for (k = 0; k < size; ) {
970 if (vals[k].kind == NORMAL) {
971 if(vals[k].v.value != NULL) {
972 dump_atomic_init(env, vals[k].v.value);
973 elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
977 } else if(vals[k].kind == TARVAL) {
978 tarval *tv = vals[k].v.tarval;
979 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
984 dump_size_type(size);
985 dump_arith_tarval(tv, size);
987 be_emit_write_line();
989 assert(vals[k].kind == BITFIELD);
990 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
991 be_emit_write_line();
995 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1002 be_emit_irprintf("\t.space\t%d\n", space);
1003 be_emit_write_line();
1010 * Dump an initializer for a compound entity.
1012 static void dump_compound_init(be_gas_decl_env_t *env, ir_entity *ent)
1014 normal_or_bitfield *vals;
1016 unsigned k, last_ofs;
1018 if(ent->has_initializer) {
1019 dump_initializer(env, ent);
1023 n = get_compound_ent_n_values(ent);
1025 /* Find the initializer size. Sorrily gcc support a nasty feature:
1026 The last field of a compound may be a flexible array. This allows
1027 initializers bigger than the type size. */
1028 last_ofs = get_type_size_bytes(get_entity_type(ent));
1029 for (i = 0; i < n; ++i) {
1030 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1031 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1032 ir_node *value = get_compound_ent_value(ent, i);
1033 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
1035 offset += (value_len + bits_remainder + 7) >> 3;
1037 if (offset > last_ofs) {
1043 * In the worst case, every initializer allocates one byte.
1044 * Moreover, initializer might be big, do not allocate on stack.
1046 vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1048 /* collect the values and store them at the offsets */
1049 for (i = 0; i < n; ++i) {
1050 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1051 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1052 ir_node *value = get_compound_ent_value(ent, i);
1053 int value_len = get_mode_size_bits(get_irn_mode(value));
1055 assert(offset_bits >= 0);
1057 if (offset_bits != 0 ||
1058 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1059 tarval *tv = get_atomic_init_tv(value);
1060 unsigned char curr_bits, last_bits = 0;
1062 panic("Couldn't get numeric value for bitfield initializer '%s'",
1063 get_entity_ld_name(ent));
1065 /* normalize offset */
1066 offset += offset_bits >> 3;
1069 for (j = 0; value_len + offset_bits > 0; ++j) {
1070 assert(offset + j < last_ofs);
1071 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1072 vals[offset + j].kind = BITFIELD;
1073 curr_bits = get_tarval_sub_bits(tv, j);
1074 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1076 last_bits = curr_bits;
1081 assert(offset < last_ofs);
1082 assert(vals[offset].kind == NORMAL);
1083 for (i = 1; i < value_len / 8; ++i) {
1084 assert(vals[offset + i].v.value == NULL);
1086 vals[offset].v.value = value;
1090 /* now write them sorted */
1091 for (k = 0; k < last_ofs; ) {
1092 int space = 0, skip = 0;
1093 if (vals[k].kind == NORMAL) {
1094 if(vals[k].v.value != NULL) {
1095 dump_atomic_init(env, vals[k].v.value);
1096 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1101 assert(vals[k].kind == BITFIELD);
1102 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1106 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1115 be_emit_irprintf("\t.space\t%d\n", space);
1116 be_emit_write_line();
1122 static void emit_align(unsigned alignment)
1124 if (!is_po2(alignment))
1125 panic("alignment not a power of 2");
1127 be_emit_irprintf("\t.p2align\t%u\n", log2_floor(alignment));
1128 be_emit_write_line();
1132 * Dump a global entity.
1134 * @param env the gas output environment
1135 * @param ent the entity to be dumped
1137 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent)
1139 ir_type *type = get_entity_type(ent);
1140 ident *ld_ident = get_entity_ld_ident(ent);
1141 unsigned align = get_type_alignment_bytes(type);
1142 int emit_as_common = 0;
1143 be_gas_section_t section = env->section;
1144 ir_variability variability = get_entity_variability(ent);
1145 ir_visibility visibility = get_entity_visibility(ent);
1147 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1151 if (section != (be_gas_section_t) -1) {
1153 } else if (variability == variability_constant) {
1154 /* a constant entity, put it on the rdata */
1155 section = GAS_SECTION_RODATA;
1156 if (be_gas_flavour == GAS_FLAVOUR_MACH_O
1157 && ent_is_string_const(ent)) {
1158 section = GAS_SECTION_CSTRING;
1160 } else if (variability == variability_uninitialized) {
1161 /* uninitialized entity put it in bss segment */
1162 section = GAS_SECTION_COMMON;
1163 if (visibility != visibility_local)
1166 section = GAS_SECTION_DATA;
1169 if(!emit_as_common) {
1170 be_gas_emit_switch_section(section);
1173 be_dbg_variable(ent);
1175 /* global or not global */
1176 if (visibility == visibility_external_visible && !emit_as_common) {
1177 be_emit_cstring(".globl\t");
1178 be_emit_ident(ld_ident);
1180 be_emit_write_line();
1181 } else if(visibility == visibility_external_allocated) {
1182 be_emit_cstring(".globl\t");
1183 be_emit_ident(ld_ident);
1185 be_emit_write_line();
1186 /* we can return now... */
1190 if (align > 1 && !emit_as_common && section != GAS_SECTION_PIC_TRAMPOLINES
1191 && section != GAS_SECTION_PIC_SYMBOLS) {
1195 if (visibility != visibility_external_allocated && !emit_as_common
1196 && be_gas_flavour == GAS_FLAVOUR_ELF) {
1197 be_emit_cstring("\t.type\t");
1198 be_emit_ident(ld_ident);
1199 be_emit_cstring(", @object\n\t.size\t");
1200 be_emit_ident(ld_ident);
1201 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1204 if (!emit_as_common) {
1205 be_emit_ident(ld_ident);
1206 be_emit_cstring(":\n");
1207 be_emit_write_line();
1210 if (variability == variability_uninitialized) {
1211 if (emit_as_common) {
1212 switch (be_gas_flavour) {
1213 case GAS_FLAVOUR_ELF:
1214 case GAS_FLAVOUR_MACH_O:
1215 case GAS_FLAVOUR_YASM:
1216 be_emit_irprintf("\t.comm %s,%u,%u\n",
1217 get_id_str(ld_ident), get_type_size_bytes(type), align);
1218 be_emit_write_line();
1220 case GAS_FLAVOUR_MINGW:
1221 be_emit_irprintf("\t.comm %s,%u # %u\n",
1222 get_id_str(ld_ident), get_type_size_bytes(type), align);
1223 be_emit_write_line();
1226 } else if (section == GAS_SECTION_PIC_TRAMPOLINES
1227 || section == GAS_SECTION_PIC_SYMBOLS) {
1228 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
1229 be_emit_cstring("\t.indirect_symbol ");
1230 be_emit_ident(get_entity_ident(ent));
1232 be_emit_write_line();
1233 if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1234 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1235 be_emit_write_line();
1237 assert(section == GAS_SECTION_PIC_SYMBOLS);
1238 be_emit_cstring("\t.long 0\n");
1239 be_emit_write_line();
1242 panic("PIC trampolines not yet supported in this gas mode");
1245 be_emit_irprintf("\t.space %u\n", get_type_size_bytes(type));
1246 be_emit_write_line();
1249 if (is_atomic_entity(ent)) {
1250 dump_atomic_init(env, get_atomic_ent_value(ent));
1252 /* sort_compound_ent_values(ent); */
1254 switch (get_type_tpop_code(get_entity_type(ent))) {
1256 if (ent_is_string_const(ent))
1257 dump_string_cst(ent);
1259 dump_compound_init(env, ent);
1264 dump_compound_init(env, ent);
1267 panic("Unimplemented type kind in dump_global()");
1274 * Dumps declarations of global variables and the initialization code.
1276 * @param gt a global like type, either the global or the TLS one
1277 * @param env an environment
1278 * @param only_emit_marked if non-zero, external allocated entities that do not have
1279 * its visited flag set are ignored
1281 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
1282 int only_emit_marked)
1284 int i, n = get_compound_n_members(gt);
1285 waitq *worklist = new_waitq();
1287 if (only_emit_marked) {
1288 for (i = 0; i < n; i++) {
1289 ir_entity *ent = get_compound_member(gt, i);
1290 if (is_entity_backend_marked(ent) ||
1291 get_entity_visibility(ent) != visibility_external_allocated) {
1292 waitq_put(worklist, ent);
1293 set_entity_backend_marked(ent, 1);
1297 for (i = 0; i < n; i++) {
1298 ir_entity *ent = get_compound_member(gt, i);
1299 set_entity_backend_marked(ent, 1);
1300 waitq_put(worklist, ent);
1304 env->worklist = worklist;
1306 while (!waitq_empty(worklist)) {
1307 ir_entity *ent = waitq_get(worklist);
1309 dump_global(env, ent);
1312 del_waitq(worklist);
1313 env->worklist = NULL;
1316 /************************************************************************/
1318 /* Generate all entities. */
1319 void be_gas_emit_decls(const be_main_env_t *main_env,
1320 int only_emit_marked_entities)
1322 be_gas_decl_env_t env;
1323 memset(&env, 0, sizeof(env));
1325 env.main_env = main_env;
1327 /* dump global type */
1328 env.section = (be_gas_section_t) -1;
1329 be_gas_dump_globals(get_glob_type(), &env, only_emit_marked_entities);
1330 env.section = GAS_SECTION_TLS;
1331 be_gas_dump_globals(get_tls_type(), &env, only_emit_marked_entities);
1332 env.section = GAS_SECTION_CONSTRUCTORS;
1333 be_gas_dump_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env,
1334 only_emit_marked_entities);
1335 env.section = GAS_SECTION_DESTRUCTORS;
1336 be_gas_dump_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env,
1337 only_emit_marked_entities);
1339 env.section = GAS_SECTION_PIC_SYMBOLS;
1340 be_gas_dump_globals(main_env->pic_symbols_type, &env,
1341 only_emit_marked_entities);
1343 if (get_compound_n_members(main_env->pic_trampolines_type) > 0) {
1344 env.section = GAS_SECTION_PIC_TRAMPOLINES;
1345 be_gas_dump_globals(main_env->pic_trampolines_type, &env,
1346 only_emit_marked_entities);
1347 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
1348 be_emit_cstring("\t.subsections_via_symbols\n");
1349 be_emit_write_line();