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 NULL, /* no cstring section */
76 { /* GAS_FLAVOUR_MINGW */
79 ".section .rdata,\"dr\"",
81 ".section\t.tbss,\"awT\",@nobits",
82 ".section\t.ctors,\"aw\",@progbits",
87 { /* GAS_FLAVOUR_YASM */
92 ".section\t.tbss,\"awT\",@nobits",
93 ".section\t.ctors,\"aw\",@progbits",
98 { /* GAS_FLAVOUR_MACH_O */
103 NULL, /* TLS is not supported on Mach-O */
106 ".section\t__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5",
107 ".section\t__IMPORT,__pointers,non_lazy_symbol_pointers"
111 assert((int) be_gas_flavour >= 0 && be_gas_flavour <= GAS_FLAVOUR_LAST);
112 assert((int) section >= 0 && section <= GAS_SECTION_LAST);
113 return text[be_gas_flavour][section];
116 void be_gas_emit_switch_section(be_gas_section_t section) {
117 if(current_section == section)
121 be_emit_string(get_section_name(section));
123 be_emit_write_line();
124 current_section = section;
127 void be_gas_emit_function_prolog(ir_entity *entity, unsigned alignment)
129 const char *name = get_entity_ld_name(entity);
130 const char *fill_byte = "";
131 unsigned maximum_skip;
133 be_gas_emit_switch_section(GAS_SECTION_TEXT);
135 /* write the begin line (used by scripts processing the assembler... */
136 be_emit_write_line();
137 be_emit_cstring("# -- Begin ");
138 be_emit_string(name);
140 be_emit_write_line();
142 /* gcc fills space between function with 0x90, no idea if this is needed */
143 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
148 maximum_skip = (1 << alignment) - 1;
149 be_emit_cstring("\t.p2align ");
150 be_emit_irprintf("%u,%s,%u\n", alignment, fill_byte, maximum_skip);
151 be_emit_write_line();
153 if (get_entity_visibility(entity) == visibility_external_visible) {
154 be_emit_cstring(".globl ");
155 be_emit_string(name);
157 be_emit_write_line();
160 switch (be_gas_flavour) {
161 case GAS_FLAVOUR_ELF:
162 be_emit_cstring("\t.type\t");
163 be_emit_string(name);
164 be_emit_cstring(", @function\n");
165 be_emit_write_line();
167 case GAS_FLAVOUR_MINGW:
168 be_emit_cstring("\t.def\t");
169 be_emit_string(name);
170 be_emit_cstring(";\t.scl\t2;\t.type\t32;\t.endef\n");
171 be_emit_write_line();
173 case GAS_FLAVOUR_MACH_O:
174 case GAS_FLAVOUR_YASM:
177 be_emit_string(name);
178 be_emit_cstring(":\n");
179 be_emit_write_line();
182 void be_gas_emit_function_epilog(ir_entity *entity)
184 const char *name = get_entity_ld_name(entity);
186 if (be_gas_flavour == GAS_FLAVOUR_ELF) {
187 be_emit_cstring("\t.size\t");
188 be_emit_string(name);
189 be_emit_cstring(", .-");
190 be_emit_string(name);
192 be_emit_write_line();
195 be_emit_cstring("# -- End ");
196 be_emit_string(name);
198 be_emit_write_line();
202 * An environment containing all needed dumper data.
203 * Currently we create the file completely in memory first, then
204 * write it to the disk. This is an artifact from the old C-generating backend
205 * and even there NOT needed. So we might change it in the future.
207 typedef struct _be_gas_decl_env {
208 const be_main_env_t *main_env; /**< The main backend environment, used for it's debug handle. */
209 be_gas_section_t section;
210 waitq *worklist; /**< A worklist we use to place not yet handled entities on. */
213 /************************************************************************/
218 * @param tv the tarval
219 * @param bytes the width of the tarvals value in bytes
221 static void dump_arith_tarval(tarval *tv, int bytes)
225 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
229 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
233 be_emit_irprintf("0x%02x%02x%02x%02x",
234 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
238 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
239 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
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 "%02x%02x%02x%02x", get_tarval_sub_bits(tv, 11),
246 get_tarval_sub_bits(tv, 10), get_tarval_sub_bits(tv, 9),
247 get_tarval_sub_bits(tv, 8), get_tarval_sub_bits(tv, 7),
248 get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5),
249 get_tarval_sub_bits(tv, 4), get_tarval_sub_bits(tv, 3),
250 get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1),
251 get_tarval_sub_bits(tv, 0));
255 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x"
256 "%02x%02x%02x%02x%02x%02x%02x%02x",
257 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
258 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
259 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
260 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
261 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
262 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
263 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
264 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
268 panic("Can't dump a tarval with %d bytes\n", bytes);
272 * Return the label prefix for labeled blocks.
274 const char *be_gas_label_prefix(void) {
281 static void dump_label(ir_label_t label) {
282 be_emit_irprintf("%s%ld", be_gas_label_prefix(), label);
286 * Return the tarval of an atomic initializer.
288 * @param init a node representing the initializer (on the const code irg)
292 static tarval *get_atomic_init_tv(ir_node *init)
295 ir_mode *mode = get_irn_mode(init);
297 switch (get_irn_opcode(init)) {
300 init = get_Cast_op(init);
304 init = get_Conv_op(init);
308 return get_Const_tarval(init);
311 switch (get_SymConst_kind(init)) {
312 case symconst_type_size:
313 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
315 case symconst_type_align:
316 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
318 case symconst_ofs_ent:
319 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
321 case symconst_enum_const:
322 return get_enumeration_value(get_SymConst_enum(init));
338 * Dump an atomic value.
340 * @param env the gas output environment
341 * @param init a node representing the atomic value (on the const code irg)
343 static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
345 ir_mode *mode = get_irn_mode(init);
346 int bytes = get_mode_size_bytes(mode);
351 init = skip_Id(init);
353 switch (get_irn_opcode(init)) {
356 do_dump_atomic_init(env, get_Cast_op(init));
360 do_dump_atomic_init(env, get_Conv_op(init));
365 tv = get_Const_tarval(init);
367 /* it's a arithmetic value */
368 dump_arith_tarval(tv, bytes);
372 switch (get_SymConst_kind(init)) {
373 case symconst_addr_name:
374 be_emit_ident(get_SymConst_name(init));
377 case symconst_addr_ent:
378 ent = get_SymConst_entity(init);
379 if(!is_entity_backend_marked(ent)) {
380 waitq_put(env->worklist, ent);
381 set_entity_backend_marked(ent, 1);
383 be_emit_ident(get_entity_ld_ident(ent));
386 case symconst_ofs_ent:
387 ent = get_SymConst_entity(init);
388 #if 0 /* not needed, is it? */
389 if(!is_entity_backend_marked(ent)) {
390 waitq_put(env->worklist, ent);
391 set_entity_backend_marked(ent, 1);
394 be_emit_irprintf("%d", get_entity_offset(ent));
397 case symconst_type_size:
398 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
401 case symconst_type_align:
402 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
405 case symconst_enum_const:
406 tv = get_enumeration_value(get_SymConst_enum(init));
407 dump_arith_tarval(tv, bytes);
411 label = get_SymConst_label(init);
416 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
421 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
422 panic("Constant must be int or pointer for '+' to work");
424 do_dump_atomic_init(env, get_Add_left(init));
425 be_emit_cstring(" + ");
426 do_dump_atomic_init(env, get_Add_right(init));
430 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
431 panic("Constant must be int or pointer for '-' to work");
433 do_dump_atomic_init(env, get_Sub_left(init));
434 be_emit_cstring(" - ");
435 do_dump_atomic_init(env, get_Sub_right(init));
439 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
440 panic("Constant must be int or pointer for '*' to work");
442 do_dump_atomic_init(env, get_Mul_left(init));
443 be_emit_cstring(" * ");
444 do_dump_atomic_init(env, get_Mul_right(init));
448 assert(0 && "dump_atomic_init(): unknown IR-node");
453 * Dumps the type for given size (.byte, .long, ...)
455 * @param size the size in bytes
457 static void dump_size_type(size_t size) {
460 be_emit_cstring("\t.byte\t");
464 be_emit_cstring("\t.word\t");
468 be_emit_cstring("\t.long\t");
472 be_emit_cstring("\t.quad\t");
477 /* handled in arith */
481 be_emit_cstring("\t.octa\t");
485 panic("Try to dump a type with %u bytes\n", (unsigned) size);
490 * Emit an atomic value.
492 * @param env the gas output environment
493 * @param init a node representing the atomic value (on the const code irg)
495 static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
497 ir_mode *mode = get_irn_mode(init);
498 int bytes = get_mode_size_bytes(mode);
500 dump_size_type(bytes);
501 do_dump_atomic_init(env, init);
503 be_emit_write_line();
506 /************************************************************************/
507 /* Routines to dump global variables */
508 /************************************************************************/
510 static int initializer_is_string_const(const ir_initializer_t *initializer)
513 int found_printable = 0;
515 if(initializer->kind != IR_INITIALIZER_COMPOUND)
518 len = initializer->compound.n_initializers;
521 for(i = 0; i < len; ++i) {
525 ir_initializer_t *sub_initializer
526 = initializer->compound.initializers[i];
528 if(sub_initializer->kind != IR_INITIALIZER_TARVAL)
531 tv = sub_initializer->tarval.value;
532 mode = get_tarval_mode(tv);
534 if (!mode_is_int(mode)
535 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
538 c = get_tarval_long(tv);
539 if (isgraph(c) || isspace(c))
544 if (i == len - 1 && c != '\0')
548 return found_printable;
552 * Determine if an entity is a string constant
553 * @param ent The entity
554 * @return 1 if it is a string constant, 0 otherwise
556 static int ent_is_string_const(ir_entity *ent)
558 ir_type *type, *element_type;
561 int found_printable = 0;
563 type = get_entity_type(ent);
565 /* if it's an array */
566 if (!is_Array_type(type))
569 element_type = get_array_element_type(type);
571 /* and the array's element type is primitive */
572 if (!is_Primitive_type(element_type))
575 /* and the mode of the element type is an int of
576 * the same size as the byte mode */
577 mode = get_type_mode(element_type);
578 if (!mode_is_int(mode)
579 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
582 if(ent->has_initializer) {
586 /* if it contains only printable chars and a 0 at the end */
587 n = get_compound_ent_n_values(ent);
588 for (i = 0; i < n; ++i) {
589 ir_node *irn = get_compound_ent_value(ent, i);
593 c = (int) get_tarval_long(get_Const_tarval(irn));
595 if (isgraph(c) || isspace(c))
600 if (i == n - 1 && c != '\0')
605 /* then we can emit it as a string constant */
606 return found_printable;
610 * Dump a string constant.
611 * No checks are made!!
613 * @param ent The entity to dump.
615 static void dump_string_cst(ir_entity *ent)
622 len = get_compound_ent_n_values(ent);
623 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
624 be_emit_cstring("\t.ascii \"");
626 be_emit_cstring("\t.string \"");
630 for (i = 0; i < len; ++i) {
634 irn = get_compound_ent_value(ent, i);
635 c = (int) get_tarval_long(get_Const_tarval(irn));
638 case '"' : be_emit_cstring("\\\""); break;
639 case '\n': be_emit_cstring("\\n"); break;
640 case '\r': be_emit_cstring("\\r"); break;
641 case '\t': be_emit_cstring("\\t"); break;
642 case '\\': be_emit_cstring("\\\\"); break;
647 be_emit_irprintf("\\%o", c);
651 be_emit_cstring("\"\n");
652 be_emit_write_line();
654 type = get_entity_type(ent);
655 type_size = get_type_size_bytes(type);
656 remaining_space = type_size - len;
657 assert(remaining_space >= 0);
658 if(remaining_space > 0) {
659 be_emit_irprintf("\t.skip\t%d\n", remaining_space);
663 static void dump_string_initializer(const ir_initializer_t *initializer)
667 len = initializer->compound.n_initializers;
668 if(be_gas_flavour == GAS_FLAVOUR_MACH_O) {
669 be_emit_cstring("\t.ascii \"");
671 be_emit_cstring("\t.string \"");
675 for(i = 0; i < len; ++i) {
676 const ir_initializer_t *sub_initializer
677 = get_initializer_compound_value(initializer, i);
679 tarval *tv = get_initializer_tarval_value(sub_initializer);
680 int c = get_tarval_long(tv);
683 case '"' : be_emit_cstring("\\\""); break;
684 case '\n': be_emit_cstring("\\n"); break;
685 case '\r': be_emit_cstring("\\r"); break;
686 case '\t': be_emit_cstring("\\t"); break;
687 case '\\': be_emit_cstring("\\\\"); break;
692 be_emit_irprintf("\\%o", c);
696 be_emit_cstring("\"\n");
697 be_emit_write_line();
700 enum normal_or_bitfield_kind {
707 enum normal_or_bitfield_kind kind;
711 unsigned char bf_val;
713 } normal_or_bitfield;
715 static int is_type_variable_size(ir_type *type)
722 static size_t get_initializer_size(const ir_initializer_t *initializer,
725 switch(get_initializer_kind(initializer)) {
726 case IR_INITIALIZER_TARVAL: {
727 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
728 return get_type_size_bytes(type);
730 case IR_INITIALIZER_CONST:
731 case IR_INITIALIZER_NULL:
732 return get_type_size_bytes(type);
733 case IR_INITIALIZER_COMPOUND: {
734 if(!is_type_variable_size(type)) {
735 return get_type_size_bytes(type);
738 = get_initializer_compound_n_entries(initializer);
740 unsigned initializer_size = get_type_size_bytes(type);
741 for(i = 0; i < n_entries; ++i) {
742 ir_entity *entity = get_compound_member(type, i);
743 ir_type *type = get_entity_type(entity);
745 const ir_initializer_t *sub_initializer
746 = get_initializer_compound_value(initializer, i);
748 unsigned offset = get_entity_offset(entity);
749 unsigned size = get_initializer_size(sub_initializer, type);
751 if(offset + size > initializer_size) {
752 initializer_size = offset + size;
755 return initializer_size;
760 panic("found invalid initializer");
764 static normal_or_bitfield *glob_vals;
765 static size_t max_vals;
768 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
769 const ir_initializer_t *initializer, ir_type *type)
771 unsigned char last_bits = 0;
772 ir_mode *mode = get_type_mode(type);
774 unsigned char curr_bits;
778 switch(get_initializer_kind(initializer)) {
779 case IR_INITIALIZER_NULL:
781 case IR_INITIALIZER_TARVAL:
782 tv = get_initializer_tarval_value(initializer);
784 case IR_INITIALIZER_CONST: {
785 ir_node *node = get_initializer_const_value(initializer);
786 if(!is_Const(node)) {
787 panic("bitfield initializer not a Const node");
789 tv = get_Const_tarval(node);
792 case IR_INITIALIZER_COMPOUND:
793 panic("bitfield initializer is compound");
796 panic("Couldn't get numeric value for bitfield initializer\n");
799 /* normalize offset */
800 vals += offset_bits >> 3;
802 value_len = get_mode_size_bits(mode);
804 /* combine bits with existing bits */
805 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
806 assert((size_t) (vals - glob_vals) + j < max_vals);
807 assert(vals[j].kind == BITFIELD ||
808 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
809 vals[j].kind = BITFIELD;
810 curr_bits = get_tarval_sub_bits(tv, j);
812 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
814 last_bits = curr_bits;
818 static void dump_ir_initializer(normal_or_bitfield *vals,
819 const ir_initializer_t *initializer,
822 assert((size_t) (vals - glob_vals) < max_vals);
824 switch(get_initializer_kind(initializer)) {
825 case IR_INITIALIZER_NULL:
827 case IR_INITIALIZER_TARVAL: {
830 assert(vals->kind != BITFIELD);
832 vals->v.tarval = get_initializer_tarval_value(initializer);
833 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
834 for(i = 1; i < get_type_size_bytes(type); ++i) {
835 vals[i].kind = NORMAL;
836 vals[i].v.value = NULL;
840 case IR_INITIALIZER_CONST: {
843 assert(vals->kind != BITFIELD);
845 vals->v.value = get_initializer_const_value(initializer);
846 for(i = 1; i < get_type_size_bytes(type); ++i) {
847 vals[i].kind = NORMAL;
848 vals[i].v.value = NULL;
852 case IR_INITIALIZER_COMPOUND: {
854 size_t n = get_initializer_compound_n_entries(initializer);
856 if(is_Array_type(type)) {
857 ir_type *element_type = get_array_element_type(type);
858 size_t skip = get_type_size_bytes(element_type);
859 size_t alignment = get_type_alignment_bytes(element_type);
860 size_t misalign = skip % alignment;
862 skip += alignment - misalign;
865 for(i = 0; i < n; ++i) {
866 ir_initializer_t *sub_initializer
867 = get_initializer_compound_value(initializer, i);
869 dump_ir_initializer(vals, sub_initializer, element_type);
875 assert(is_compound_type(type));
876 n_members = get_compound_n_members(type);
877 for(i = 0; i < n_members; ++i) {
878 ir_entity *member = get_compound_member(type, i);
879 size_t offset = get_entity_offset(member);
880 ir_type *subtype = get_entity_type(member);
881 ir_mode *mode = get_type_mode(subtype);
882 ir_initializer_t *sub_initializer;
884 assert(i < get_initializer_compound_n_entries(initializer));
886 = get_initializer_compound_value(initializer, i);
890 = get_entity_offset_bits_remainder(member);
891 size_t value_len = get_mode_size_bits(mode);
893 if(offset_bits != 0 ||
894 (value_len != 8 && value_len != 16 && value_len != 32
895 && value_len != 64)) {
896 dump_bitfield(&vals[offset], offset_bits,
897 sub_initializer, subtype);
902 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
909 panic("invalid ir_initializer kind found");
912 static void dump_initializer(be_gas_decl_env_t *env, ir_entity *entity)
914 const ir_initializer_t *initializer = entity->attr.initializer;
916 normal_or_bitfield *vals;
920 if(initializer_is_string_const(initializer)) {
921 dump_string_initializer(initializer);
925 type = get_entity_type(entity);
926 size = get_initializer_size(initializer, type);
932 * In the worst case, every initializer allocates one byte.
933 * Moreover, initializer might be big, do not allocate on stack.
935 vals = xcalloc(size, sizeof(vals[0]));
942 dump_ir_initializer(vals, initializer, type);
944 /* now write values sorted */
945 for (k = 0; k < size; ) {
948 if (vals[k].kind == NORMAL) {
949 if(vals[k].v.value != NULL) {
950 dump_atomic_init(env, vals[k].v.value);
951 elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
955 } else if(vals[k].kind == TARVAL) {
956 tarval *tv = vals[k].v.tarval;
957 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
962 dump_size_type(size);
963 dump_arith_tarval(tv, size);
965 be_emit_write_line();
967 assert(vals[k].kind == BITFIELD);
968 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
969 be_emit_write_line();
973 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
980 be_emit_irprintf("\t.skip\t%d\n", space);
981 be_emit_write_line();
988 * Dump an initializer for a compound entity.
990 static void dump_compound_init(be_gas_decl_env_t *env, ir_entity *ent)
992 normal_or_bitfield *vals;
994 unsigned k, last_ofs;
996 if(ent->has_initializer) {
997 dump_initializer(env, ent);
1001 n = get_compound_ent_n_values(ent);
1003 /* Find the initializer size. Sorrily gcc support a nasty feature:
1004 The last field of a compound may be a flexible array. This allows
1005 initializers bigger than the type size. */
1006 last_ofs = get_type_size_bytes(get_entity_type(ent));
1007 for (i = 0; i < n; ++i) {
1008 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1009 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1010 ir_node *value = get_compound_ent_value(ent, i);
1011 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
1013 offset += (value_len + bits_remainder + 7) >> 3;
1015 if (offset > last_ofs) {
1021 * In the worst case, every initializer allocates one byte.
1022 * Moreover, initializer might be big, do not allocate on stack.
1024 vals = xcalloc(last_ofs, sizeof(vals[0]));
1026 /* collect the values and store them at the offsets */
1027 for (i = 0; i < n; ++i) {
1028 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1029 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1030 ir_node *value = get_compound_ent_value(ent, i);
1031 int value_len = get_mode_size_bits(get_irn_mode(value));
1033 assert(offset_bits >= 0);
1035 if (offset_bits != 0 ||
1036 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1037 tarval *tv = get_atomic_init_tv(value);
1038 unsigned char curr_bits, last_bits = 0;
1040 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
1041 get_entity_ld_name(ent));
1043 /* normalize offset */
1044 offset += offset_bits >> 3;
1047 for (j = 0; value_len + offset_bits > 0; ++j) {
1048 assert(offset + j < last_ofs);
1049 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1050 vals[offset + j].kind = BITFIELD;
1051 curr_bits = get_tarval_sub_bits(tv, j);
1052 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1054 last_bits = curr_bits;
1059 assert(offset < last_ofs);
1060 assert(vals[offset].kind == NORMAL);
1061 for (i = 1; i < value_len / 8; ++i) {
1062 assert(vals[offset + i].v.value == NULL);
1064 vals[offset].v.value = value;
1068 /* now write them sorted */
1069 for (k = 0; k < last_ofs; ) {
1070 int space = 0, skip = 0;
1071 if (vals[k].kind == NORMAL) {
1072 if(vals[k].v.value != NULL) {
1073 dump_atomic_init(env, vals[k].v.value);
1074 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1079 assert(vals[k].kind == BITFIELD);
1080 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1084 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1093 be_emit_irprintf("\t.skip\t%d\n", space);
1094 be_emit_write_line();
1100 static void emit_align(unsigned alignment)
1102 if (!is_po2(alignment))
1103 panic("alignment not a power of 2");
1105 be_emit_irprintf(".p2align\t%u\n", log2_floor(alignment));
1106 be_emit_write_line();
1110 * Dump a global entity.
1112 * @param env the gas output environment
1113 * @param ent the entity to be dumped
1115 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent)
1117 ir_type *type = get_entity_type(ent);
1118 ident *ld_ident = get_entity_ld_ident(ent);
1119 unsigned align = get_type_alignment_bytes(type);
1120 int emit_as_common = 0;
1121 be_gas_section_t section = env->section;
1122 ir_variability variability = get_entity_variability(ent);
1123 ir_visibility visibility = get_entity_visibility(ent);
1125 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1129 if (section != (be_gas_section_t) -1) {
1131 } else if (variability == variability_constant) {
1132 /* a constant entity, put it on the rdata */
1133 section = GAS_SECTION_RODATA;
1134 if (be_gas_flavour == GAS_FLAVOUR_MACH_O
1135 && ent_is_string_const(ent)) {
1136 section = GAS_SECTION_CSTRING;
1138 } else if (variability == variability_uninitialized) {
1139 /* uninitialized entity put it in bss segment */
1140 section = GAS_SECTION_COMMON;
1141 if (visibility != visibility_local)
1144 section = GAS_SECTION_DATA;
1147 if(!emit_as_common) {
1148 be_gas_emit_switch_section(section);
1151 be_dbg_variable(ent);
1153 /* global or not global */
1154 if (visibility == visibility_external_visible && !emit_as_common) {
1155 be_emit_cstring(".globl\t");
1156 be_emit_ident(ld_ident);
1158 be_emit_write_line();
1159 } else if(visibility == visibility_external_allocated) {
1160 be_emit_cstring(".globl\t");
1161 be_emit_ident(ld_ident);
1163 be_emit_write_line();
1164 /* we can return now... */
1168 if (align > 1 && !emit_as_common && section != GAS_SECTION_PIC_TRAMPOLINES
1169 && section != GAS_SECTION_PIC_SYMBOLS) {
1173 if (!emit_as_common) {
1174 be_emit_ident(ld_ident);
1175 be_emit_cstring(":\n");
1176 be_emit_write_line();
1179 if (variability == variability_uninitialized) {
1180 if (emit_as_common) {
1181 switch (be_gas_flavour) {
1182 case GAS_FLAVOUR_ELF:
1183 case GAS_FLAVOUR_MACH_O:
1184 case GAS_FLAVOUR_YASM:
1185 be_emit_irprintf("\t.comm %s,%u,%u\n",
1186 get_id_str(ld_ident), get_type_size_bytes(type), align);
1187 be_emit_write_line();
1189 case GAS_FLAVOUR_MINGW:
1190 be_emit_irprintf("\t.comm %s,%u # %u\n",
1191 get_id_str(ld_ident), get_type_size_bytes(type), align);
1192 be_emit_write_line();
1195 } else if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1196 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
1197 be_emit_cstring("\t.indirect_symbol ");
1198 be_emit_ident(get_entity_ident(ent));
1200 be_emit_write_line();
1201 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1202 be_emit_write_line();
1204 panic("PIC trampolines not yet supported in this gas mode");
1207 be_emit_irprintf("\t.space %u\n", get_type_size_bytes(type));
1208 be_emit_write_line();
1211 if (is_atomic_entity(ent)) {
1212 dump_atomic_init(env, get_atomic_ent_value(ent));
1214 /* sort_compound_ent_values(ent); */
1216 switch (get_type_tpop_code(get_entity_type(ent))) {
1218 if (ent_is_string_const(ent))
1219 dump_string_cst(ent);
1221 dump_compound_init(env, ent);
1226 dump_compound_init(env, ent);
1236 * Dumps declarations of global variables and the initialization code.
1238 * @param gt a global like type, either the global or the TLS one
1239 * @param env an environment
1240 * @param only_emit_marked if non-zero, external allocated entities that do not have
1241 * its visited flag set are ignored
1243 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
1244 int only_emit_marked)
1246 int i, n = get_compound_n_members(gt);
1247 waitq *worklist = new_waitq();
1249 if (only_emit_marked) {
1250 for (i = 0; i < n; i++) {
1251 ir_entity *ent = get_compound_member(gt, i);
1252 if (is_entity_backend_marked(ent) ||
1253 get_entity_visibility(ent) != visibility_external_allocated) {
1254 waitq_put(worklist, ent);
1255 set_entity_backend_marked(ent, 1);
1259 for (i = 0; i < n; i++) {
1260 ir_entity *ent = get_compound_member(gt, i);
1261 set_entity_backend_marked(ent, 1);
1262 waitq_put(worklist, ent);
1266 env->worklist = worklist;
1268 while (!waitq_empty(worklist)) {
1269 ir_entity *ent = waitq_get(worklist);
1271 dump_global(env, ent);
1274 del_waitq(worklist);
1275 env->worklist = NULL;
1278 /************************************************************************/
1280 /* Generate all entities. */
1281 void be_gas_emit_decls(const be_main_env_t *main_env,
1282 int only_emit_marked_entities)
1284 be_gas_decl_env_t env;
1285 memset(&env, 0, sizeof(env));
1287 env.main_env = main_env;
1289 /* dump global type */
1290 env.section = (be_gas_section_t) -1;
1291 be_gas_dump_globals(get_glob_type(), &env, only_emit_marked_entities);
1292 env.section = GAS_SECTION_TLS;
1293 be_gas_dump_globals(get_tls_type(), &env, only_emit_marked_entities);
1294 env.section = GAS_SECTION_CTOR;
1295 be_gas_dump_globals(get_constructors_type(), &env,
1296 only_emit_marked_entities);
1297 env.section = GAS_SECTION_PIC_SYMBOLS;
1298 be_gas_dump_globals(main_env->pic_symbols_type, &env,
1299 only_emit_marked_entities);
1301 if (get_compound_n_members(main_env->pic_trampolines_type) > 0) {
1302 env.section = GAS_SECTION_PIC_TRAMPOLINES;
1303 be_gas_dump_globals(main_env->pic_trampolines_type, &env,
1304 only_emit_marked_entities);
1305 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
1306 be_emit_cstring("\t.subsections_via_symbols\n");
1307 be_emit_write_line();