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);
123 be_gas_emit_switch_section(GAS_SECTION_TEXT);
125 /* write the begin line (used by scripts processing the assembler... */
126 be_emit_write_line();
127 be_emit_cstring("# -- Begin ");
128 be_emit_string(name);
130 be_emit_write_line();
132 const char *fill_byte = "";
133 /* gcc fills space between function with 0x90, no idea if this is needed */
134 if(be_gas_flavour == GAS_FLAVOUR_MACH_O) {
138 unsigned maximum_skip = (1 << alignment) - 1;
139 be_emit_cstring("\t.p2align ");
140 be_emit_irprintf("%u,%s,%u\n", alignment, fill_byte, maximum_skip);
141 be_emit_write_line();
143 if (get_entity_visibility(entity) == visibility_external_visible) {
144 be_emit_cstring(".globl ");
145 be_emit_string(name);
147 be_emit_write_line();
150 switch (be_gas_flavour) {
151 case GAS_FLAVOUR_ELF:
152 be_emit_cstring("\t.type\t");
153 be_emit_string(name);
154 be_emit_cstring(", @function\n");
155 be_emit_write_line();
157 case GAS_FLAVOUR_MINGW:
158 be_emit_cstring("\t.def\t");
159 be_emit_string(name);
160 be_emit_cstring(";\t.scl\t2;\t.type\t32;\t.endef\n");
161 be_emit_write_line();
163 case GAS_FLAVOUR_MACH_O:
164 case GAS_FLAVOUR_YASM:
167 be_emit_string(name);
168 be_emit_cstring(":\n");
169 be_emit_write_line();
172 void be_gas_emit_function_epilog(ir_entity *entity)
174 const char *name = get_entity_ld_name(entity);
176 if (be_gas_flavour == GAS_FLAVOUR_ELF) {
177 be_emit_cstring("\t.size\t");
178 be_emit_string(name);
179 be_emit_cstring(", .-");
180 be_emit_string(name);
182 be_emit_write_line();
185 be_emit_cstring("# -- End ");
186 be_emit_string(name);
188 be_emit_write_line();
192 * An environment containing all needed dumper data.
193 * Currently we create the file completely in memory first, then
194 * write it to the disk. This is an artifact from the old C-generating backend
195 * and even there NOT needed. So we might change it in the future.
197 typedef struct _be_gas_decl_env {
198 const be_main_env_t *main_env; /**< The main backend environment, used for it's debug handle. */
200 waitq *worklist; /**< A worklist we use to place not yet handled entities on. */
203 /************************************************************************/
208 * @param tv the tarval
209 * @param bytes the width of the tarvals value in bytes
211 static void dump_arith_tarval(tarval *tv, int bytes)
215 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
219 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
223 be_emit_irprintf("0x%02x%02x%02x%02x",
224 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
228 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
229 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
230 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 "%02x%02x%02x%02x%02x%02x%02x%02x",
240 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
241 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
242 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
243 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
244 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
245 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),
247 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
252 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
258 * Return the label prefix for labeled blocks.
260 const char *be_gas_label_prefix(void) {
267 static void dump_label(ir_label_t label) {
268 be_emit_irprintf("%s%ld", be_gas_label_prefix(), label);
272 * Return the tarval of an atomic initializer.
274 * @param init a node representing the initializer (on the const code irg)
278 static tarval *get_atomic_init_tv(ir_node *init)
281 ir_mode *mode = get_irn_mode(init);
283 switch (get_irn_opcode(init)) {
286 init = get_Cast_op(init);
290 init = get_Conv_op(init);
294 return get_Const_tarval(init);
297 switch (get_SymConst_kind(init)) {
298 case symconst_type_size:
299 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
301 case symconst_type_align:
302 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
304 case symconst_ofs_ent:
305 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
307 case symconst_enum_const:
308 return get_enumeration_value(get_SymConst_enum(init));
324 * Dump an atomic value.
326 * @param env the gas output environment
327 * @param init a node representing the atomic value (on the const code irg)
329 static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
331 ir_mode *mode = get_irn_mode(init);
332 int bytes = get_mode_size_bytes(mode);
337 init = skip_Id(init);
339 switch (get_irn_opcode(init)) {
341 do_dump_atomic_init(env, get_Cast_op(init));
345 do_dump_atomic_init(env, get_Conv_op(init));
349 tv = get_Const_tarval(init);
351 /* it's a arithmetic value */
352 dump_arith_tarval(tv, bytes);
356 switch (get_SymConst_kind(init)) {
357 case symconst_addr_name:
358 be_emit_ident(get_SymConst_name(init));
361 case symconst_addr_ent:
362 ent = get_SymConst_entity(init);
363 if(!is_entity_backend_marked(ent)) {
364 waitq_put(env->worklist, ent);
365 set_entity_backend_marked(ent, 1);
367 be_emit_ident(get_entity_ld_ident(ent));
370 case symconst_ofs_ent:
371 ent = get_SymConst_entity(init);
372 #if 0 /* not needed, is it? */
373 if(!is_entity_backend_marked(ent)) {
374 waitq_put(env->worklist, ent);
375 set_entity_backend_marked(ent, 1);
378 be_emit_irprintf("%d", get_entity_offset(ent));
381 case symconst_type_size:
382 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
385 case symconst_type_align:
386 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
389 case symconst_enum_const:
390 tv = get_enumeration_value(get_SymConst_enum(init));
391 dump_arith_tarval(tv, bytes);
395 label = get_SymConst_label(init);
400 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
405 do_dump_atomic_init(env, get_Add_left(init));
406 be_emit_cstring(" + ");
407 do_dump_atomic_init(env, get_Add_right(init));
411 do_dump_atomic_init(env, get_Sub_left(init));
412 be_emit_cstring(" - ");
413 do_dump_atomic_init(env, get_Sub_right(init));
417 do_dump_atomic_init(env, get_Mul_left(init));
418 be_emit_cstring(" * ");
419 do_dump_atomic_init(env, get_Mul_right(init));
423 assert(0 && "dump_atomic_init(): unknown IR-node");
428 * Dumps the type for given size (.byte, .long, ...)
430 * @param size the size in bytes
432 static void dump_size_type(size_t size) {
435 be_emit_cstring("\t.byte\t");
439 be_emit_cstring("\t.word\t");
443 be_emit_cstring("\t.long\t");
447 be_emit_cstring("\t.quad\t");
452 /* handled in arith */
456 be_emit_cstring("\t.octa\t");
460 panic("Try to dump a type with %u bytes\n", (unsigned) size);
465 * Emit an atomic value.
467 * @param env the gas output environment
468 * @param init a node representing the atomic value (on the const code irg)
470 static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
472 ir_mode *mode = get_irn_mode(init);
473 int bytes = get_mode_size_bytes(mode);
475 dump_size_type(bytes);
476 do_dump_atomic_init(env, init);
478 be_emit_write_line();
481 /************************************************************************/
482 /* Routines to dump global variables */
483 /************************************************************************/
485 static int initializer_is_string_const(const ir_initializer_t *initializer)
488 int found_printable = 0;
490 if(initializer->kind != IR_INITIALIZER_COMPOUND)
493 len = initializer->compound.n_initializers;
496 for(i = 0; i < len; ++i) {
500 ir_initializer_t *sub_initializer
501 = initializer->compound.initializers[i];
503 if(sub_initializer->kind != IR_INITIALIZER_TARVAL)
506 tv = sub_initializer->tarval.value;
507 mode = get_tarval_mode(tv);
509 if (!mode_is_int(mode)
510 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
513 c = get_tarval_long(tv);
514 if (isgraph(c) || isspace(c))
519 if (i == len - 1 && c != '\0')
523 return found_printable;
527 * Determine if an entity is a string constant
528 * @param ent The entity
529 * @return 1 if it is a string constant, 0 otherwise
531 static int ent_is_string_const(ir_entity *ent)
533 ir_type *type, *element_type;
536 int found_printable = 0;
538 type = get_entity_type(ent);
540 /* if it's an array */
541 if (!is_Array_type(type))
544 element_type = get_array_element_type(type);
546 /* and the array's element type is primitive */
547 if (!is_Primitive_type(element_type))
550 /* and the mode of the element type is an int of
551 * the same size as the byte mode */
552 mode = get_type_mode(element_type);
553 if (!mode_is_int(mode)
554 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
557 if(ent->has_initializer) {
561 /* if it contains only printable chars and a 0 at the end */
562 n = get_compound_ent_n_values(ent);
563 for (i = 0; i < n; ++i) {
564 ir_node *irn = get_compound_ent_value(ent, i);
568 c = (int) get_tarval_long(get_Const_tarval(irn));
570 if (isgraph(c) || isspace(c))
575 if (i == n - 1 && c != '\0')
580 /* then we can emit it as a string constant */
581 return found_printable;
585 * Dump a string constant.
586 * No checks are made!!
588 * @param ent The entity to dump.
590 static void dump_string_cst(ir_entity *ent)
597 len = get_compound_ent_n_values(ent);
598 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
599 be_emit_cstring("\t.ascii \"");
601 be_emit_cstring("\t.string \"");
605 for (i = 0; i < len; ++i) {
609 irn = get_compound_ent_value(ent, i);
610 c = (int) get_tarval_long(get_Const_tarval(irn));
613 case '"' : be_emit_cstring("\\\""); break;
614 case '\n': be_emit_cstring("\\n"); break;
615 case '\r': be_emit_cstring("\\r"); break;
616 case '\t': be_emit_cstring("\\t"); break;
617 case '\\': be_emit_cstring("\\\\"); break;
622 be_emit_irprintf("\\%o", c);
626 be_emit_cstring("\"\n");
627 be_emit_write_line();
629 type = get_entity_type(ent);
630 type_size = get_type_size_bytes(type);
631 remaining_space = type_size - len;
632 assert(remaining_space >= 0);
633 if(remaining_space > 0) {
634 be_emit_irprintf("\t.skip\t%d\n", remaining_space);
638 static void dump_string_initializer(const ir_initializer_t *initializer)
642 len = initializer->compound.n_initializers;
643 if(be_gas_flavour == GAS_FLAVOUR_MACH_O) {
644 be_emit_cstring("\t.ascii \"");
646 be_emit_cstring("\t.string \"");
650 for(i = 0; i < len; ++i) {
651 const ir_initializer_t *sub_initializer
652 = get_initializer_compound_value(initializer, i);
654 tarval *tv = get_initializer_tarval_value(sub_initializer);
655 int c = get_tarval_long(tv);
658 case '"' : be_emit_cstring("\\\""); break;
659 case '\n': be_emit_cstring("\\n"); break;
660 case '\r': be_emit_cstring("\\r"); break;
661 case '\t': be_emit_cstring("\\t"); break;
662 case '\\': be_emit_cstring("\\\\"); break;
667 be_emit_irprintf("\\%o", c);
671 be_emit_cstring("\"\n");
672 be_emit_write_line();
675 enum normal_or_bitfield_kind {
682 enum normal_or_bitfield_kind kind;
686 unsigned char bf_val;
688 } normal_or_bitfield;
690 static int is_type_variable_size(ir_type *type)
697 static size_t get_initializer_size(const ir_initializer_t *initializer,
700 switch(get_initializer_kind(initializer)) {
701 case IR_INITIALIZER_TARVAL: {
702 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
703 return get_type_size_bytes(type);
705 case IR_INITIALIZER_CONST:
706 case IR_INITIALIZER_NULL:
707 return get_type_size_bytes(type);
708 case IR_INITIALIZER_COMPOUND: {
709 if(!is_type_variable_size(type)) {
710 return get_type_size_bytes(type);
713 = get_initializer_compound_n_entries(initializer);
715 unsigned initializer_size = get_type_size_bytes(type);
716 for(i = 0; i < n_entries; ++i) {
717 ir_entity *entity = get_compound_member(type, i);
718 ir_type *type = get_entity_type(entity);
720 const ir_initializer_t *sub_initializer
721 = get_initializer_compound_value(initializer, i);
723 unsigned offset = get_entity_offset(entity);
724 unsigned size = get_initializer_size(sub_initializer, type);
726 if(offset + size > initializer_size) {
727 initializer_size = offset + size;
730 return initializer_size;
735 panic("found invalid initializer");
739 static normal_or_bitfield *glob_vals;
740 static size_t max_vals;
743 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
744 const ir_initializer_t *initializer, ir_type *type)
746 unsigned char last_bits = 0;
747 ir_mode *mode = get_type_mode(type);
749 unsigned char curr_bits;
753 switch(get_initializer_kind(initializer)) {
754 case IR_INITIALIZER_NULL:
756 case IR_INITIALIZER_TARVAL:
757 tv = get_initializer_tarval_value(initializer);
759 case IR_INITIALIZER_CONST: {
760 ir_node *node = get_initializer_const_value(initializer);
761 if(!is_Const(node)) {
762 panic("bitfield initializer not a Const node");
764 tv = get_Const_tarval(node);
767 case IR_INITIALIZER_COMPOUND:
768 panic("bitfield initializer is compound");
771 panic("Couldn't get numeric value for bitfield initializer\n");
774 /* normalize offset */
775 vals += offset_bits >> 3;
777 value_len = get_mode_size_bits(mode);
779 /* combine bits with existing bits */
780 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
781 assert((size_t) (vals - glob_vals) + j < max_vals);
782 assert(vals[j].kind == BITFIELD ||
783 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
784 vals[j].kind = BITFIELD;
785 curr_bits = get_tarval_sub_bits(tv, j);
787 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
789 last_bits = curr_bits;
793 static void dump_ir_initializer(normal_or_bitfield *vals,
794 const ir_initializer_t *initializer,
797 assert((size_t) (vals - glob_vals) < max_vals);
799 switch(get_initializer_kind(initializer)) {
800 case IR_INITIALIZER_NULL:
802 case IR_INITIALIZER_TARVAL: {
805 assert(vals->kind != BITFIELD);
807 vals->v.tarval = get_initializer_tarval_value(initializer);
808 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
809 for(i = 1; i < get_type_size_bytes(type); ++i) {
810 vals[i].kind = NORMAL;
811 vals[i].v.value = NULL;
815 case IR_INITIALIZER_CONST: {
818 assert(vals->kind != BITFIELD);
820 vals->v.value = get_initializer_const_value(initializer);
821 for(i = 1; i < get_type_size_bytes(type); ++i) {
822 vals[i].kind = NORMAL;
823 vals[i].v.value = NULL;
827 case IR_INITIALIZER_COMPOUND: {
829 size_t n = get_initializer_compound_n_entries(initializer);
831 if(is_Array_type(type)) {
832 ir_type *element_type = get_array_element_type(type);
833 size_t skip = get_type_size_bytes(element_type);
834 size_t alignment = get_type_alignment_bytes(element_type);
835 size_t misalign = skip % alignment;
837 skip += alignment - misalign;
840 for(i = 0; i < n; ++i) {
841 ir_initializer_t *sub_initializer
842 = get_initializer_compound_value(initializer, i);
844 dump_ir_initializer(vals, sub_initializer, element_type);
850 assert(is_compound_type(type));
851 n_members = get_compound_n_members(type);
852 for(i = 0; i < n_members; ++i) {
853 ir_entity *member = get_compound_member(type, i);
854 size_t offset = get_entity_offset(member);
855 ir_type *subtype = get_entity_type(member);
856 ir_mode *mode = get_type_mode(subtype);
857 ir_initializer_t *sub_initializer;
859 assert(i < get_initializer_compound_n_entries(initializer));
861 = get_initializer_compound_value(initializer, i);
865 = get_entity_offset_bits_remainder(member);
866 size_t value_len = get_mode_size_bits(mode);
868 if(offset_bits != 0 ||
869 (value_len != 8 && value_len != 16 && value_len != 32
870 && value_len != 64)) {
871 dump_bitfield(&vals[offset], offset_bits,
872 sub_initializer, subtype);
877 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
884 panic("invalid ir_initializer kind found");
887 static void dump_initializer(be_gas_decl_env_t *env, ir_entity *entity)
889 const ir_initializer_t *initializer = entity->attr.initializer;
891 normal_or_bitfield *vals;
895 if(initializer_is_string_const(initializer)) {
896 dump_string_initializer(initializer);
900 type = get_entity_type(entity);
901 size = get_initializer_size(initializer, type);
907 * In the worst case, every initializer allocates one byte.
908 * Moreover, initializer might be big, do not allocate on stack.
910 vals = xcalloc(size, sizeof(vals[0]));
917 dump_ir_initializer(vals, initializer, type);
919 /* now write values sorted */
920 for (k = 0; k < size; ) {
921 int space = 0, skip = 0;
922 if (vals[k].kind == NORMAL) {
923 if(vals[k].v.value != NULL) {
924 dump_atomic_init(env, vals[k].v.value);
925 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
929 } else if(vals[k].kind == TARVAL) {
930 tarval *tv = vals[k].v.tarval;
931 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
936 dump_size_type(size);
937 dump_arith_tarval(tv, size);
939 be_emit_write_line();
941 assert(vals[k].kind == BITFIELD);
942 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
943 be_emit_write_line();
947 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
956 be_emit_irprintf("\t.skip\t%d\n", space);
957 be_emit_write_line();
964 * Dump an initializer for a compound entity.
966 static void dump_compound_init(be_gas_decl_env_t *env, ir_entity *ent)
968 normal_or_bitfield *vals;
970 unsigned k, last_ofs;
972 if(ent->has_initializer) {
973 dump_initializer(env, ent);
977 n = get_compound_ent_n_values(ent);
979 /* Find the initializer size. Sorrily gcc support a nasty feature:
980 The last field of a compound may be a flexible array. This allows
981 initializers bigger than the type size. */
982 last_ofs = get_type_size_bytes(get_entity_type(ent));
983 for (i = 0; i < n; ++i) {
984 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
985 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
986 ir_node *value = get_compound_ent_value(ent, i);
987 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
989 offset += (value_len + bits_remainder + 7) >> 3;
991 if (offset > last_ofs) {
997 * In the worst case, every initializer allocates one byte.
998 * Moreover, initializer might be big, do not allocate on stack.
1000 vals = xcalloc(last_ofs, sizeof(vals[0]));
1002 /* collect the values and store them at the offsets */
1003 for (i = 0; i < n; ++i) {
1004 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1005 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1006 ir_node *value = get_compound_ent_value(ent, i);
1007 int value_len = get_mode_size_bits(get_irn_mode(value));
1009 assert(offset_bits >= 0);
1011 if (offset_bits != 0 ||
1012 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1013 tarval *tv = get_atomic_init_tv(value);
1014 unsigned char curr_bits, last_bits = 0;
1016 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
1017 get_entity_ld_name(ent));
1019 /* normalize offset */
1020 offset += offset_bits >> 3;
1023 for (j = 0; value_len + offset_bits > 0; ++j) {
1024 assert(offset + j < last_ofs);
1025 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1026 vals[offset + j].kind = BITFIELD;
1027 curr_bits = get_tarval_sub_bits(tv, j);
1028 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1030 last_bits = curr_bits;
1035 assert(offset < last_ofs);
1036 assert(vals[offset].kind == NORMAL);
1037 for (i = 1; i < value_len / 8; ++i) {
1038 assert(vals[offset + i].v.value == NULL);
1040 vals[offset].v.value = value;
1044 /* now write them sorted */
1045 for (k = 0; k < last_ofs; ) {
1046 int space = 0, skip = 0;
1047 if (vals[k].kind == NORMAL) {
1048 if(vals[k].v.value != NULL) {
1049 dump_atomic_init(env, vals[k].v.value);
1050 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1055 assert(vals[k].kind == BITFIELD);
1056 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1060 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1069 be_emit_irprintf("\t.skip\t%d\n", space);
1070 be_emit_write_line();
1076 static void emit_align(unsigned alignment)
1078 if (!is_po2(alignment))
1079 panic("alignment not a power of 2");
1081 be_emit_irprintf(".p2align\t%u\n", log2_floor(alignment));
1082 be_emit_write_line();
1086 * Dump a global entity.
1088 * @param env the gas output environment
1089 * @param ent the entity to be dumped
1090 * @param emit_commons if non-zero, emit commons (non-local uninitialized entities)
1092 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent, int emit_commons)
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;
1099 ir_variability variability;
1100 ir_visibility visibility;
1102 if (is_Method_type(type)) {
1103 if (be_gas_flavour != GAS_FLAVOUR_MACH_O
1104 && get_method_img_section(ent) == section_constructors) {
1105 be_gas_emit_switch_section(GAS_SECTION_CTOR);
1107 dump_size_type(align);
1108 be_emit_ident(ld_ident);
1110 be_emit_write_line();
1116 variability = get_entity_variability(ent);
1117 visibility = get_entity_visibility(ent);
1118 section = GAS_SECTION_DATA;
1119 if (env->dump_tls) {
1120 section = GAS_SECTION_TLS;
1122 } else if (variability == variability_constant) {
1123 /* a constant entity, put it on the rdata */
1124 section = GAS_SECTION_RODATA;
1125 if (be_gas_flavour == GAS_FLAVOUR_MACH_O
1126 && ent_is_string_const(ent)) {
1127 section = GAS_SECTION_CSTRING;
1129 } else if (variability == variability_uninitialized) {
1130 /* uninitialized entity put it in bss segment */
1131 section = GAS_SECTION_COMMON;
1132 if (emit_commons && visibility != visibility_local)
1136 if(!emit_as_common) {
1137 be_gas_emit_switch_section(section);
1140 be_dbg_variable(ent);
1142 /* global or not global */
1143 if (visibility == visibility_external_visible && !emit_as_common) {
1144 be_emit_cstring(".globl\t");
1145 be_emit_ident(ld_ident);
1147 be_emit_write_line();
1148 } else if(visibility == visibility_external_allocated) {
1149 be_emit_cstring(".globl\t");
1150 be_emit_ident(ld_ident);
1152 be_emit_write_line();
1153 /* we can return now... */
1157 if (align > 1 && !emit_as_common) {
1161 if (!emit_as_common) {
1162 be_emit_ident(ld_ident);
1163 be_emit_cstring(":\n");
1164 be_emit_write_line();
1167 if (variability == variability_uninitialized) {
1168 if (emit_as_common) {
1169 switch (be_gas_flavour) {
1170 case GAS_FLAVOUR_ELF:
1171 case GAS_FLAVOUR_MACH_O:
1172 case GAS_FLAVOUR_YASM:
1173 be_emit_irprintf("\t.comm %s,%u,%u\n",
1174 get_id_str(ld_ident), get_type_size_bytes(type), align);
1175 be_emit_write_line();
1177 case GAS_FLAVOUR_MINGW:
1178 be_emit_irprintf("\t.comm %s,%u # %u\n",
1179 get_id_str(ld_ident), get_type_size_bytes(type), align);
1180 be_emit_write_line();
1184 be_emit_irprintf("\t.space %u\n", get_type_size_bytes(type));
1185 be_emit_write_line();
1188 if (is_atomic_entity(ent)) {
1189 dump_atomic_init(env, get_atomic_ent_value(ent));
1191 /* sort_compound_ent_values(ent); */
1193 switch (get_type_tpop_code(get_entity_type(ent))) {
1195 if (ent_is_string_const(ent))
1196 dump_string_cst(ent);
1198 dump_compound_init(env, ent);
1203 dump_compound_init(env, ent);
1213 * Dumps declarations of global variables and the initialization code.
1215 * @param gt a global like type, either the global or the TLS one
1216 * @param env an environment
1217 * @param emit_commons if non-zero, emit commons (non-local uninitialized entities)
1218 * @param only_emit_marked if non-zero, external allocated entities that do not have
1219 * its visited flag set are ignored
1221 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
1222 int emit_commons, int only_emit_marked)
1224 int i, n = get_compound_n_members(gt);
1225 waitq *worklist = new_waitq();
1227 if (only_emit_marked) {
1228 for (i = 0; i < n; i++) {
1229 ir_entity *ent = get_compound_member(gt, i);
1230 if (is_entity_backend_marked(ent) ||
1231 get_entity_visibility(ent) != visibility_external_allocated) {
1232 waitq_put(worklist, ent);
1233 set_entity_backend_marked(ent, 1);
1237 for (i = 0; i < n; i++) {
1238 ir_entity *ent = get_compound_member(gt, i);
1239 set_entity_backend_marked(ent, 1);
1240 waitq_put(worklist, ent);
1244 env->worklist = worklist;
1246 while (!waitq_empty(worklist)) {
1247 ir_entity *ent = waitq_get(worklist);
1249 dump_global(env, ent, emit_commons);
1252 del_waitq(worklist);
1253 env->worklist = NULL;
1256 /************************************************************************/
1258 /* Generate all entities. */
1259 void be_gas_emit_decls(const be_main_env_t *main_env,
1260 int only_emit_marked_entities)
1262 be_gas_decl_env_t env;
1263 memset(&env, 0, sizeof(env));
1265 env.main_env = main_env;
1267 /* dump global type */
1268 be_gas_dump_globals(get_glob_type(), &env, 1, only_emit_marked_entities);
1270 /* dump the Thread Local Storage */
1272 be_gas_dump_globals(get_tls_type(), &env, 0, only_emit_marked_entities);