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",
74 { /* GAS_FLAVOUR_MINGW */
77 ".section .rdata,\"dr\"",
79 ".section\t.tbss,\"awT\",@nobits",
80 ".section\t.ctors,\"aw\",@progbits",
83 { /* GAS_FLAVOUR_YASM */
88 ".section\t.tbss,\"awT\",@nobits",
89 ".section\t.ctors,\"aw\",@progbits",
92 { /* GAS_FLAVOUR_MACH_O */
97 NULL, /* TLS is not supported on Mach-O */
103 assert((int) be_gas_flavour >= 0 && be_gas_flavour <= GAS_FLAVOUR_LAST);
104 assert((int) section >= 0 && section <= GAS_SECTION_LAST);
105 return text[be_gas_flavour][section];
108 void be_gas_emit_switch_section(be_gas_section_t section) {
109 if(current_section == section)
113 be_emit_string(get_section_name(section));
115 be_emit_write_line();
116 current_section = section;
119 void be_gas_emit_function_prolog(ir_entity *entity, unsigned alignment)
121 const char *name = get_entity_ld_name(entity);
122 const char *fill_byte = "";
123 unsigned maximum_skip;
125 be_gas_emit_switch_section(GAS_SECTION_TEXT);
127 /* write the begin line (used by scripts processing the assembler... */
128 be_emit_write_line();
129 be_emit_cstring("# -- Begin ");
130 be_emit_string(name);
132 be_emit_write_line();
134 /* gcc fills space between function with 0x90, no idea if this is needed */
135 if(be_gas_flavour == GAS_FLAVOUR_MACH_O) {
139 maximum_skip = (1 << alignment) - 1;
140 be_emit_cstring("\t.p2align ");
141 be_emit_irprintf("%u,%s,%u\n", alignment, fill_byte, maximum_skip);
142 be_emit_write_line();
144 if (get_entity_visibility(entity) == visibility_external_visible) {
145 be_emit_cstring(".globl ");
146 be_emit_string(name);
148 be_emit_write_line();
151 switch (be_gas_flavour) {
152 case GAS_FLAVOUR_ELF:
153 be_emit_cstring("\t.type\t");
154 be_emit_string(name);
155 be_emit_cstring(", @function\n");
156 be_emit_write_line();
158 case GAS_FLAVOUR_MINGW:
159 be_emit_cstring("\t.def\t");
160 be_emit_string(name);
161 be_emit_cstring(";\t.scl\t2;\t.type\t32;\t.endef\n");
162 be_emit_write_line();
164 case GAS_FLAVOUR_MACH_O:
165 case GAS_FLAVOUR_YASM:
168 be_emit_string(name);
169 be_emit_cstring(":\n");
170 be_emit_write_line();
173 void be_gas_emit_function_epilog(ir_entity *entity)
175 const char *name = get_entity_ld_name(entity);
177 if (be_gas_flavour == GAS_FLAVOUR_ELF) {
178 be_emit_cstring("\t.size\t");
179 be_emit_string(name);
180 be_emit_cstring(", .-");
181 be_emit_string(name);
183 be_emit_write_line();
186 be_emit_cstring("# -- End ");
187 be_emit_string(name);
189 be_emit_write_line();
193 * An environment containing all needed dumper data.
194 * Currently we create the file completely in memory first, then
195 * write it to the disk. This is an artifact from the old C-generating backend
196 * and even there NOT needed. So we might change it in the future.
198 typedef struct _be_gas_decl_env {
199 const be_main_env_t *main_env; /**< The main backend environment, used for it's debug handle. */
200 be_gas_section_t section;
201 waitq *worklist; /**< A worklist we use to place not yet handled entities on. */
204 /************************************************************************/
209 * @param tv the tarval
210 * @param bytes the width of the tarvals value in bytes
212 static void dump_arith_tarval(tarval *tv, int bytes)
216 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
220 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
224 be_emit_irprintf("0x%02x%02x%02x%02x",
225 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
229 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
230 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
231 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
239 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x"
240 "%02x%02x%02x%02x%02x%02x%02x%02x",
241 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
242 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
243 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
244 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
245 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
246 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
247 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
248 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
253 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
259 * Return the label prefix for labeled blocks.
261 const char *be_gas_label_prefix(void) {
268 static void dump_label(ir_label_t label) {
269 be_emit_irprintf("%s%ld", be_gas_label_prefix(), label);
273 * Return the tarval of an atomic initializer.
275 * @param init a node representing the initializer (on the const code irg)
279 static tarval *get_atomic_init_tv(ir_node *init)
282 ir_mode *mode = get_irn_mode(init);
284 switch (get_irn_opcode(init)) {
287 init = get_Cast_op(init);
291 init = get_Conv_op(init);
295 return get_Const_tarval(init);
298 switch (get_SymConst_kind(init)) {
299 case symconst_type_size:
300 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
302 case symconst_type_align:
303 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
305 case symconst_ofs_ent:
306 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
308 case symconst_enum_const:
309 return get_enumeration_value(get_SymConst_enum(init));
325 * Dump an atomic value.
327 * @param env the gas output environment
328 * @param init a node representing the atomic value (on the const code irg)
330 static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
332 ir_mode *mode = get_irn_mode(init);
333 int bytes = get_mode_size_bytes(mode);
338 init = skip_Id(init);
340 switch (get_irn_opcode(init)) {
342 do_dump_atomic_init(env, get_Cast_op(init));
346 do_dump_atomic_init(env, get_Conv_op(init));
350 tv = get_Const_tarval(init);
352 /* it's a arithmetic value */
353 dump_arith_tarval(tv, bytes);
357 switch (get_SymConst_kind(init)) {
358 case symconst_addr_name:
359 be_emit_ident(get_SymConst_name(init));
362 case symconst_addr_ent:
363 ent = get_SymConst_entity(init);
364 if(!is_entity_backend_marked(ent)) {
365 waitq_put(env->worklist, ent);
366 set_entity_backend_marked(ent, 1);
368 be_emit_ident(get_entity_ld_ident(ent));
371 case symconst_ofs_ent:
372 ent = get_SymConst_entity(init);
373 #if 0 /* not needed, is it? */
374 if(!is_entity_backend_marked(ent)) {
375 waitq_put(env->worklist, ent);
376 set_entity_backend_marked(ent, 1);
379 be_emit_irprintf("%d", get_entity_offset(ent));
382 case symconst_type_size:
383 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
386 case symconst_type_align:
387 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
390 case symconst_enum_const:
391 tv = get_enumeration_value(get_SymConst_enum(init));
392 dump_arith_tarval(tv, bytes);
396 label = get_SymConst_label(init);
401 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
406 do_dump_atomic_init(env, get_Add_left(init));
407 be_emit_cstring(" + ");
408 do_dump_atomic_init(env, get_Add_right(init));
412 do_dump_atomic_init(env, get_Sub_left(init));
413 be_emit_cstring(" - ");
414 do_dump_atomic_init(env, get_Sub_right(init));
418 do_dump_atomic_init(env, get_Mul_left(init));
419 be_emit_cstring(" * ");
420 do_dump_atomic_init(env, get_Mul_right(init));
424 assert(0 && "dump_atomic_init(): unknown IR-node");
429 * Dumps the type for given size (.byte, .long, ...)
431 * @param size the size in bytes
433 static void dump_size_type(size_t size) {
436 be_emit_cstring("\t.byte\t");
440 be_emit_cstring("\t.word\t");
444 be_emit_cstring("\t.long\t");
448 be_emit_cstring("\t.quad\t");
453 /* handled in arith */
457 be_emit_cstring("\t.octa\t");
461 panic("Try to dump a type with %u bytes\n", (unsigned) size);
466 * Emit an atomic value.
468 * @param env the gas output environment
469 * @param init a node representing the atomic value (on the const code irg)
471 static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
473 ir_mode *mode = get_irn_mode(init);
474 int bytes = get_mode_size_bytes(mode);
476 dump_size_type(bytes);
477 do_dump_atomic_init(env, init);
479 be_emit_write_line();
482 /************************************************************************/
483 /* Routines to dump global variables */
484 /************************************************************************/
486 static int initializer_is_string_const(const ir_initializer_t *initializer)
489 int found_printable = 0;
491 if(initializer->kind != IR_INITIALIZER_COMPOUND)
494 len = initializer->compound.n_initializers;
497 for(i = 0; i < len; ++i) {
501 ir_initializer_t *sub_initializer
502 = initializer->compound.initializers[i];
504 if(sub_initializer->kind != IR_INITIALIZER_TARVAL)
507 tv = sub_initializer->tarval.value;
508 mode = get_tarval_mode(tv);
510 if (!mode_is_int(mode)
511 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
514 c = get_tarval_long(tv);
515 if (isgraph(c) || isspace(c))
520 if (i == len - 1 && c != '\0')
524 return found_printable;
528 * Determine if an entity is a string constant
529 * @param ent The entity
530 * @return 1 if it is a string constant, 0 otherwise
532 static int ent_is_string_const(ir_entity *ent)
534 ir_type *type, *element_type;
537 int found_printable = 0;
539 type = get_entity_type(ent);
541 /* if it's an array */
542 if (!is_Array_type(type))
545 element_type = get_array_element_type(type);
547 /* and the array's element type is primitive */
548 if (!is_Primitive_type(element_type))
551 /* and the mode of the element type is an int of
552 * the same size as the byte mode */
553 mode = get_type_mode(element_type);
554 if (!mode_is_int(mode)
555 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
558 if(ent->has_initializer) {
562 /* if it contains only printable chars and a 0 at the end */
563 n = get_compound_ent_n_values(ent);
564 for (i = 0; i < n; ++i) {
565 ir_node *irn = get_compound_ent_value(ent, i);
569 c = (int) get_tarval_long(get_Const_tarval(irn));
571 if (isgraph(c) || isspace(c))
576 if (i == n - 1 && c != '\0')
581 /* then we can emit it as a string constant */
582 return found_printable;
586 * Dump a string constant.
587 * No checks are made!!
589 * @param ent The entity to dump.
591 static void dump_string_cst(ir_entity *ent)
598 len = get_compound_ent_n_values(ent);
599 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
600 be_emit_cstring("\t.ascii \"");
602 be_emit_cstring("\t.string \"");
606 for (i = 0; i < len; ++i) {
610 irn = get_compound_ent_value(ent, i);
611 c = (int) get_tarval_long(get_Const_tarval(irn));
614 case '"' : be_emit_cstring("\\\""); break;
615 case '\n': be_emit_cstring("\\n"); break;
616 case '\r': be_emit_cstring("\\r"); break;
617 case '\t': be_emit_cstring("\\t"); break;
618 case '\\': be_emit_cstring("\\\\"); break;
623 be_emit_irprintf("\\%o", c);
627 be_emit_cstring("\"\n");
628 be_emit_write_line();
630 type = get_entity_type(ent);
631 type_size = get_type_size_bytes(type);
632 remaining_space = type_size - len;
633 assert(remaining_space >= 0);
634 if(remaining_space > 0) {
635 be_emit_irprintf("\t.skip\t%d\n", remaining_space);
639 static void dump_string_initializer(const ir_initializer_t *initializer)
643 len = initializer->compound.n_initializers;
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 < len; ++i) {
652 const ir_initializer_t *sub_initializer
653 = get_initializer_compound_value(initializer, i);
655 tarval *tv = get_initializer_tarval_value(sub_initializer);
656 int c = get_tarval_long(tv);
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();
676 enum normal_or_bitfield_kind {
683 enum normal_or_bitfield_kind kind;
687 unsigned char bf_val;
689 } normal_or_bitfield;
691 static int is_type_variable_size(ir_type *type)
698 static size_t get_initializer_size(const ir_initializer_t *initializer,
701 switch(get_initializer_kind(initializer)) {
702 case IR_INITIALIZER_TARVAL: {
703 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
704 return get_type_size_bytes(type);
706 case IR_INITIALIZER_CONST:
707 case IR_INITIALIZER_NULL:
708 return get_type_size_bytes(type);
709 case IR_INITIALIZER_COMPOUND: {
710 if(!is_type_variable_size(type)) {
711 return get_type_size_bytes(type);
714 = get_initializer_compound_n_entries(initializer);
716 unsigned initializer_size = get_type_size_bytes(type);
717 for(i = 0; i < n_entries; ++i) {
718 ir_entity *entity = get_compound_member(type, i);
719 ir_type *type = get_entity_type(entity);
721 const ir_initializer_t *sub_initializer
722 = get_initializer_compound_value(initializer, i);
724 unsigned offset = get_entity_offset(entity);
725 unsigned size = get_initializer_size(sub_initializer, type);
727 if(offset + size > initializer_size) {
728 initializer_size = offset + size;
731 return initializer_size;
736 panic("found invalid initializer");
740 static normal_or_bitfield *glob_vals;
741 static size_t max_vals;
744 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
745 const ir_initializer_t *initializer, ir_type *type)
747 unsigned char last_bits = 0;
748 ir_mode *mode = get_type_mode(type);
750 unsigned char curr_bits;
754 switch(get_initializer_kind(initializer)) {
755 case IR_INITIALIZER_NULL:
757 case IR_INITIALIZER_TARVAL:
758 tv = get_initializer_tarval_value(initializer);
760 case IR_INITIALIZER_CONST: {
761 ir_node *node = get_initializer_const_value(initializer);
762 if(!is_Const(node)) {
763 panic("bitfield initializer not a Const node");
765 tv = get_Const_tarval(node);
768 case IR_INITIALIZER_COMPOUND:
769 panic("bitfield initializer is compound");
772 panic("Couldn't get numeric value for bitfield initializer\n");
775 /* normalize offset */
776 vals += offset_bits >> 3;
778 value_len = get_mode_size_bits(mode);
780 /* combine bits with existing bits */
781 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
782 assert((size_t) (vals - glob_vals) + j < max_vals);
783 assert(vals[j].kind == BITFIELD ||
784 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
785 vals[j].kind = BITFIELD;
786 curr_bits = get_tarval_sub_bits(tv, j);
788 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
790 last_bits = curr_bits;
794 static void dump_ir_initializer(normal_or_bitfield *vals,
795 const ir_initializer_t *initializer,
798 assert((size_t) (vals - glob_vals) < max_vals);
800 switch(get_initializer_kind(initializer)) {
801 case IR_INITIALIZER_NULL:
803 case IR_INITIALIZER_TARVAL: {
806 assert(vals->kind != BITFIELD);
808 vals->v.tarval = get_initializer_tarval_value(initializer);
809 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
810 for(i = 1; i < get_type_size_bytes(type); ++i) {
811 vals[i].kind = NORMAL;
812 vals[i].v.value = NULL;
816 case IR_INITIALIZER_CONST: {
819 assert(vals->kind != BITFIELD);
821 vals->v.value = get_initializer_const_value(initializer);
822 for(i = 1; i < get_type_size_bytes(type); ++i) {
823 vals[i].kind = NORMAL;
824 vals[i].v.value = NULL;
828 case IR_INITIALIZER_COMPOUND: {
830 size_t n = get_initializer_compound_n_entries(initializer);
832 if(is_Array_type(type)) {
833 ir_type *element_type = get_array_element_type(type);
834 size_t skip = get_type_size_bytes(element_type);
835 size_t alignment = get_type_alignment_bytes(element_type);
836 size_t misalign = skip % alignment;
838 skip += alignment - misalign;
841 for(i = 0; i < n; ++i) {
842 ir_initializer_t *sub_initializer
843 = get_initializer_compound_value(initializer, i);
845 dump_ir_initializer(vals, sub_initializer, element_type);
851 assert(is_compound_type(type));
852 n_members = get_compound_n_members(type);
853 for(i = 0; i < n_members; ++i) {
854 ir_entity *member = get_compound_member(type, i);
855 size_t offset = get_entity_offset(member);
856 ir_type *subtype = get_entity_type(member);
857 ir_mode *mode = get_type_mode(subtype);
858 ir_initializer_t *sub_initializer;
860 assert(i < get_initializer_compound_n_entries(initializer));
862 = get_initializer_compound_value(initializer, i);
866 = get_entity_offset_bits_remainder(member);
867 size_t value_len = get_mode_size_bits(mode);
869 if(offset_bits != 0 ||
870 (value_len != 8 && value_len != 16 && value_len != 32
871 && value_len != 64)) {
872 dump_bitfield(&vals[offset], offset_bits,
873 sub_initializer, subtype);
878 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
885 panic("invalid ir_initializer kind found");
888 static void dump_initializer(be_gas_decl_env_t *env, ir_entity *entity)
890 const ir_initializer_t *initializer = entity->attr.initializer;
892 normal_or_bitfield *vals;
896 if(initializer_is_string_const(initializer)) {
897 dump_string_initializer(initializer);
901 type = get_entity_type(entity);
902 size = get_initializer_size(initializer, type);
908 * In the worst case, every initializer allocates one byte.
909 * Moreover, initializer might be big, do not allocate on stack.
911 vals = xcalloc(size, sizeof(vals[0]));
918 dump_ir_initializer(vals, initializer, type);
920 /* now write values sorted */
921 for (k = 0; k < size; ) {
922 int space = 0, skip = 0;
923 if (vals[k].kind == NORMAL) {
924 if(vals[k].v.value != NULL) {
925 dump_atomic_init(env, vals[k].v.value);
926 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
930 } else if(vals[k].kind == TARVAL) {
931 tarval *tv = vals[k].v.tarval;
932 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
937 dump_size_type(size);
938 dump_arith_tarval(tv, size);
940 be_emit_write_line();
942 assert(vals[k].kind == BITFIELD);
943 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
944 be_emit_write_line();
948 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
957 be_emit_irprintf("\t.skip\t%d\n", space);
958 be_emit_write_line();
965 * Dump an initializer for a compound entity.
967 static void dump_compound_init(be_gas_decl_env_t *env, ir_entity *ent)
969 normal_or_bitfield *vals;
971 unsigned k, last_ofs;
973 if(ent->has_initializer) {
974 dump_initializer(env, ent);
978 n = get_compound_ent_n_values(ent);
980 /* Find the initializer size. Sorrily gcc support a nasty feature:
981 The last field of a compound may be a flexible array. This allows
982 initializers bigger than the type size. */
983 last_ofs = get_type_size_bytes(get_entity_type(ent));
984 for (i = 0; i < n; ++i) {
985 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
986 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
987 ir_node *value = get_compound_ent_value(ent, i);
988 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
990 offset += (value_len + bits_remainder + 7) >> 3;
992 if (offset > last_ofs) {
998 * In the worst case, every initializer allocates one byte.
999 * Moreover, initializer might be big, do not allocate on stack.
1001 vals = xcalloc(last_ofs, sizeof(vals[0]));
1003 /* collect the values and store them at the offsets */
1004 for (i = 0; i < n; ++i) {
1005 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1006 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1007 ir_node *value = get_compound_ent_value(ent, i);
1008 int value_len = get_mode_size_bits(get_irn_mode(value));
1010 assert(offset_bits >= 0);
1012 if (offset_bits != 0 ||
1013 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1014 tarval *tv = get_atomic_init_tv(value);
1015 unsigned char curr_bits, last_bits = 0;
1017 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
1018 get_entity_ld_name(ent));
1020 /* normalize offset */
1021 offset += offset_bits >> 3;
1024 for (j = 0; value_len + offset_bits > 0; ++j) {
1025 assert(offset + j < last_ofs);
1026 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1027 vals[offset + j].kind = BITFIELD;
1028 curr_bits = get_tarval_sub_bits(tv, j);
1029 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1031 last_bits = curr_bits;
1036 assert(offset < last_ofs);
1037 assert(vals[offset].kind == NORMAL);
1038 for (i = 1; i < value_len / 8; ++i) {
1039 assert(vals[offset + i].v.value == NULL);
1041 vals[offset].v.value = value;
1045 /* now write them sorted */
1046 for (k = 0; k < last_ofs; ) {
1047 int space = 0, skip = 0;
1048 if (vals[k].kind == NORMAL) {
1049 if(vals[k].v.value != NULL) {
1050 dump_atomic_init(env, vals[k].v.value);
1051 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1056 assert(vals[k].kind == BITFIELD);
1057 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1061 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1070 be_emit_irprintf("\t.skip\t%d\n", space);
1071 be_emit_write_line();
1077 static void emit_align(unsigned alignment)
1079 if (!is_po2(alignment))
1080 panic("alignment not a power of 2");
1082 be_emit_irprintf(".p2align\t%u\n", log2_floor(alignment));
1083 be_emit_write_line();
1087 * Dump a global entity.
1089 * @param env the gas output environment
1090 * @param ent the entity to be dumped
1092 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent)
1094 ir_type *type = get_entity_type(ent);
1095 ident *ld_ident = get_entity_ld_ident(ent);
1096 unsigned align = get_type_alignment_bytes(type);
1097 int emit_as_common = 0;
1098 be_gas_section_t section = env->section;
1099 ir_variability variability = get_entity_variability(ent);
1100 ir_visibility visibility = get_entity_visibility(ent);
1102 if (section != (be_gas_section_t) -1) {
1104 } else if (variability == variability_constant) {
1105 /* a constant entity, put it on the rdata */
1106 section = GAS_SECTION_RODATA;
1107 if (be_gas_flavour == GAS_FLAVOUR_MACH_O
1108 && ent_is_string_const(ent)) {
1109 section = GAS_SECTION_CSTRING;
1111 } else if (variability == variability_uninitialized) {
1112 /* uninitialized entity put it in bss segment */
1113 section = GAS_SECTION_COMMON;
1114 if (visibility != visibility_local)
1118 if(!emit_as_common) {
1119 be_gas_emit_switch_section(section);
1122 be_dbg_variable(ent);
1124 /* global or not global */
1125 if (visibility == visibility_external_visible && !emit_as_common) {
1126 be_emit_cstring(".globl\t");
1127 be_emit_ident(ld_ident);
1129 be_emit_write_line();
1130 } else if(visibility == visibility_external_allocated) {
1131 be_emit_cstring(".globl\t");
1132 be_emit_ident(ld_ident);
1134 be_emit_write_line();
1135 /* we can return now... */
1139 if (align > 1 && !emit_as_common) {
1143 if (!emit_as_common) {
1144 be_emit_ident(ld_ident);
1145 be_emit_cstring(":\n");
1146 be_emit_write_line();
1149 if (variability == variability_uninitialized) {
1150 if (emit_as_common) {
1151 switch (be_gas_flavour) {
1152 case GAS_FLAVOUR_ELF:
1153 case GAS_FLAVOUR_MACH_O:
1154 case GAS_FLAVOUR_YASM:
1155 be_emit_irprintf("\t.comm %s,%u,%u\n",
1156 get_id_str(ld_ident), get_type_size_bytes(type), align);
1157 be_emit_write_line();
1159 case GAS_FLAVOUR_MINGW:
1160 be_emit_irprintf("\t.comm %s,%u # %u\n",
1161 get_id_str(ld_ident), get_type_size_bytes(type), align);
1162 be_emit_write_line();
1166 be_emit_irprintf("\t.space %u\n", get_type_size_bytes(type));
1167 be_emit_write_line();
1170 if (is_atomic_entity(ent)) {
1171 dump_atomic_init(env, get_atomic_ent_value(ent));
1173 /* sort_compound_ent_values(ent); */
1175 switch (get_type_tpop_code(get_entity_type(ent))) {
1177 if (ent_is_string_const(ent))
1178 dump_string_cst(ent);
1180 dump_compound_init(env, ent);
1185 dump_compound_init(env, ent);
1195 * Dumps declarations of global variables and the initialization code.
1197 * @param gt a global like type, either the global or the TLS one
1198 * @param env an environment
1199 * @param only_emit_marked if non-zero, external allocated entities that do not have
1200 * its visited flag set are ignored
1202 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
1203 int only_emit_marked)
1205 int i, n = get_compound_n_members(gt);
1206 waitq *worklist = new_waitq();
1208 if (only_emit_marked) {
1209 for (i = 0; i < n; i++) {
1210 ir_entity *ent = get_compound_member(gt, i);
1211 if (is_entity_backend_marked(ent) ||
1212 get_entity_visibility(ent) != visibility_external_allocated) {
1213 waitq_put(worklist, ent);
1214 set_entity_backend_marked(ent, 1);
1218 for (i = 0; i < n; i++) {
1219 ir_entity *ent = get_compound_member(gt, i);
1220 set_entity_backend_marked(ent, 1);
1221 waitq_put(worklist, ent);
1225 env->worklist = worklist;
1227 while (!waitq_empty(worklist)) {
1228 ir_entity *ent = waitq_get(worklist);
1230 dump_global(env, ent);
1233 del_waitq(worklist);
1234 env->worklist = NULL;
1237 /************************************************************************/
1239 /* Generate all entities. */
1240 void be_gas_emit_decls(const be_main_env_t *main_env,
1241 int only_emit_marked_entities)
1243 be_gas_decl_env_t env;
1244 memset(&env, 0, sizeof(env));
1246 env.main_env = main_env;
1248 /* dump global type */
1249 env.section = (be_gas_section_t) -1;
1250 be_gas_dump_globals(get_glob_type(), &env, only_emit_marked_entities);
1251 env.section = GAS_SECTION_TLS;
1252 be_gas_dump_globals(get_tls_type(), &env, only_emit_marked_entities);
1253 env.section = GAS_SECTION_CTOR;
1254 be_gas_dump_globals(get_constructors_type(), &env,
1255 only_emit_marked_entities);