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) {
147 maximum_skip = (1 << alignment) - 1;
148 be_emit_cstring("\t.p2align ");
149 be_emit_irprintf("%u,%s,%u\n", alignment, fill_byte, maximum_skip);
150 be_emit_write_line();
152 if (get_entity_visibility(entity) == visibility_external_visible) {
153 be_emit_cstring(".globl ");
154 be_emit_string(name);
156 be_emit_write_line();
159 switch (be_gas_flavour) {
160 case GAS_FLAVOUR_ELF:
161 be_emit_cstring("\t.type\t");
162 be_emit_string(name);
163 be_emit_cstring(", @function\n");
164 be_emit_write_line();
166 case GAS_FLAVOUR_MINGW:
167 be_emit_cstring("\t.def\t");
168 be_emit_string(name);
169 be_emit_cstring(";\t.scl\t2;\t.type\t32;\t.endef\n");
170 be_emit_write_line();
172 case GAS_FLAVOUR_MACH_O:
173 case GAS_FLAVOUR_YASM:
176 be_emit_string(name);
177 be_emit_cstring(":\n");
178 be_emit_write_line();
181 void be_gas_emit_function_epilog(ir_entity *entity)
183 const char *name = get_entity_ld_name(entity);
185 if (be_gas_flavour == GAS_FLAVOUR_ELF) {
186 be_emit_cstring("\t.size\t");
187 be_emit_string(name);
188 be_emit_cstring(", .-");
189 be_emit_string(name);
191 be_emit_write_line();
194 be_emit_cstring("# -- End ");
195 be_emit_string(name);
197 be_emit_write_line();
201 * An environment containing all needed dumper data.
202 * Currently we create the file completely in memory first, then
203 * write it to the disk. This is an artifact from the old C-generating backend
204 * and even there NOT needed. So we might change it in the future.
206 typedef struct _be_gas_decl_env {
207 const be_main_env_t *main_env; /**< The main backend environment, used for it's debug handle. */
208 be_gas_section_t section;
209 waitq *worklist; /**< A worklist we use to place not yet handled entities on. */
212 /************************************************************************/
217 * @param tv the tarval
218 * @param bytes the width of the tarvals value in bytes
220 static void dump_arith_tarval(tarval *tv, int bytes)
224 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
228 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
232 be_emit_irprintf("0x%02x%02x%02x%02x",
233 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
237 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
238 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
239 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
247 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x"
248 "%02x%02x%02x%02x%02x%02x%02x%02x",
249 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
250 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
251 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
252 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
253 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
254 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
255 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
256 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
261 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
267 * Return the label prefix for labeled blocks.
269 const char *be_gas_label_prefix(void) {
276 static void dump_label(ir_label_t label) {
277 be_emit_irprintf("%s%ld", be_gas_label_prefix(), label);
281 * Return the tarval of an atomic initializer.
283 * @param init a node representing the initializer (on the const code irg)
287 static tarval *get_atomic_init_tv(ir_node *init)
290 ir_mode *mode = get_irn_mode(init);
292 switch (get_irn_opcode(init)) {
295 init = get_Cast_op(init);
299 init = get_Conv_op(init);
303 return get_Const_tarval(init);
306 switch (get_SymConst_kind(init)) {
307 case symconst_type_size:
308 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
310 case symconst_type_align:
311 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
313 case symconst_ofs_ent:
314 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
316 case symconst_enum_const:
317 return get_enumeration_value(get_SymConst_enum(init));
333 * Dump an atomic value.
335 * @param env the gas output environment
336 * @param init a node representing the atomic value (on the const code irg)
338 static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
340 ir_mode *mode = get_irn_mode(init);
341 int bytes = get_mode_size_bytes(mode);
346 init = skip_Id(init);
348 switch (get_irn_opcode(init)) {
350 do_dump_atomic_init(env, get_Cast_op(init));
354 do_dump_atomic_init(env, get_Conv_op(init));
358 tv = get_Const_tarval(init);
360 /* it's a arithmetic value */
361 dump_arith_tarval(tv, bytes);
365 switch (get_SymConst_kind(init)) {
366 case symconst_addr_name:
367 be_emit_ident(get_SymConst_name(init));
370 case symconst_addr_ent:
371 ent = get_SymConst_entity(init);
372 if(!is_entity_backend_marked(ent)) {
373 waitq_put(env->worklist, ent);
374 set_entity_backend_marked(ent, 1);
376 be_emit_ident(get_entity_ld_ident(ent));
379 case symconst_ofs_ent:
380 ent = get_SymConst_entity(init);
381 #if 0 /* not needed, is it? */
382 if(!is_entity_backend_marked(ent)) {
383 waitq_put(env->worklist, ent);
384 set_entity_backend_marked(ent, 1);
387 be_emit_irprintf("%d", get_entity_offset(ent));
390 case symconst_type_size:
391 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
394 case symconst_type_align:
395 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
398 case symconst_enum_const:
399 tv = get_enumeration_value(get_SymConst_enum(init));
400 dump_arith_tarval(tv, bytes);
404 label = get_SymConst_label(init);
409 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
414 do_dump_atomic_init(env, get_Add_left(init));
415 be_emit_cstring(" + ");
416 do_dump_atomic_init(env, get_Add_right(init));
420 do_dump_atomic_init(env, get_Sub_left(init));
421 be_emit_cstring(" - ");
422 do_dump_atomic_init(env, get_Sub_right(init));
426 do_dump_atomic_init(env, get_Mul_left(init));
427 be_emit_cstring(" * ");
428 do_dump_atomic_init(env, get_Mul_right(init));
432 assert(0 && "dump_atomic_init(): unknown IR-node");
437 * Dumps the type for given size (.byte, .long, ...)
439 * @param size the size in bytes
441 static void dump_size_type(size_t size) {
444 be_emit_cstring("\t.byte\t");
448 be_emit_cstring("\t.word\t");
452 be_emit_cstring("\t.long\t");
456 be_emit_cstring("\t.quad\t");
461 /* handled in arith */
465 be_emit_cstring("\t.octa\t");
469 panic("Try to dump a type with %u bytes\n", (unsigned) size);
474 * Emit an atomic value.
476 * @param env the gas output environment
477 * @param init a node representing the atomic value (on the const code irg)
479 static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
481 ir_mode *mode = get_irn_mode(init);
482 int bytes = get_mode_size_bytes(mode);
484 dump_size_type(bytes);
485 do_dump_atomic_init(env, init);
487 be_emit_write_line();
490 /************************************************************************/
491 /* Routines to dump global variables */
492 /************************************************************************/
494 static int initializer_is_string_const(const ir_initializer_t *initializer)
497 int found_printable = 0;
499 if(initializer->kind != IR_INITIALIZER_COMPOUND)
502 len = initializer->compound.n_initializers;
505 for(i = 0; i < len; ++i) {
509 ir_initializer_t *sub_initializer
510 = initializer->compound.initializers[i];
512 if(sub_initializer->kind != IR_INITIALIZER_TARVAL)
515 tv = sub_initializer->tarval.value;
516 mode = get_tarval_mode(tv);
518 if (!mode_is_int(mode)
519 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
522 c = get_tarval_long(tv);
523 if (isgraph(c) || isspace(c))
528 if (i == len - 1 && c != '\0')
532 return found_printable;
536 * Determine if an entity is a string constant
537 * @param ent The entity
538 * @return 1 if it is a string constant, 0 otherwise
540 static int ent_is_string_const(ir_entity *ent)
542 ir_type *type, *element_type;
545 int found_printable = 0;
547 type = get_entity_type(ent);
549 /* if it's an array */
550 if (!is_Array_type(type))
553 element_type = get_array_element_type(type);
555 /* and the array's element type is primitive */
556 if (!is_Primitive_type(element_type))
559 /* and the mode of the element type is an int of
560 * the same size as the byte mode */
561 mode = get_type_mode(element_type);
562 if (!mode_is_int(mode)
563 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
566 if(ent->has_initializer) {
570 /* if it contains only printable chars and a 0 at the end */
571 n = get_compound_ent_n_values(ent);
572 for (i = 0; i < n; ++i) {
573 ir_node *irn = get_compound_ent_value(ent, i);
577 c = (int) get_tarval_long(get_Const_tarval(irn));
579 if (isgraph(c) || isspace(c))
584 if (i == n - 1 && c != '\0')
589 /* then we can emit it as a string constant */
590 return found_printable;
594 * Dump a string constant.
595 * No checks are made!!
597 * @param ent The entity to dump.
599 static void dump_string_cst(ir_entity *ent)
606 len = get_compound_ent_n_values(ent);
607 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
608 be_emit_cstring("\t.ascii \"");
610 be_emit_cstring("\t.string \"");
614 for (i = 0; i < len; ++i) {
618 irn = get_compound_ent_value(ent, i);
619 c = (int) get_tarval_long(get_Const_tarval(irn));
622 case '"' : be_emit_cstring("\\\""); break;
623 case '\n': be_emit_cstring("\\n"); break;
624 case '\r': be_emit_cstring("\\r"); break;
625 case '\t': be_emit_cstring("\\t"); break;
626 case '\\': be_emit_cstring("\\\\"); break;
631 be_emit_irprintf("\\%o", c);
635 be_emit_cstring("\"\n");
636 be_emit_write_line();
638 type = get_entity_type(ent);
639 type_size = get_type_size_bytes(type);
640 remaining_space = type_size - len;
641 assert(remaining_space >= 0);
642 if(remaining_space > 0) {
643 be_emit_irprintf("\t.skip\t%d\n", remaining_space);
647 static void dump_string_initializer(const ir_initializer_t *initializer)
651 len = initializer->compound.n_initializers;
652 if(be_gas_flavour == GAS_FLAVOUR_MACH_O) {
653 be_emit_cstring("\t.ascii \"");
655 be_emit_cstring("\t.string \"");
659 for(i = 0; i < len; ++i) {
660 const ir_initializer_t *sub_initializer
661 = get_initializer_compound_value(initializer, i);
663 tarval *tv = get_initializer_tarval_value(sub_initializer);
664 int c = get_tarval_long(tv);
667 case '"' : be_emit_cstring("\\\""); break;
668 case '\n': be_emit_cstring("\\n"); break;
669 case '\r': be_emit_cstring("\\r"); break;
670 case '\t': be_emit_cstring("\\t"); break;
671 case '\\': be_emit_cstring("\\\\"); break;
676 be_emit_irprintf("\\%o", c);
680 be_emit_cstring("\"\n");
681 be_emit_write_line();
684 enum normal_or_bitfield_kind {
691 enum normal_or_bitfield_kind kind;
695 unsigned char bf_val;
697 } normal_or_bitfield;
699 static int is_type_variable_size(ir_type *type)
706 static size_t get_initializer_size(const ir_initializer_t *initializer,
709 switch(get_initializer_kind(initializer)) {
710 case IR_INITIALIZER_TARVAL: {
711 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
712 return get_type_size_bytes(type);
714 case IR_INITIALIZER_CONST:
715 case IR_INITIALIZER_NULL:
716 return get_type_size_bytes(type);
717 case IR_INITIALIZER_COMPOUND: {
718 if(!is_type_variable_size(type)) {
719 return get_type_size_bytes(type);
722 = get_initializer_compound_n_entries(initializer);
724 unsigned initializer_size = get_type_size_bytes(type);
725 for(i = 0; i < n_entries; ++i) {
726 ir_entity *entity = get_compound_member(type, i);
727 ir_type *type = get_entity_type(entity);
729 const ir_initializer_t *sub_initializer
730 = get_initializer_compound_value(initializer, i);
732 unsigned offset = get_entity_offset(entity);
733 unsigned size = get_initializer_size(sub_initializer, type);
735 if(offset + size > initializer_size) {
736 initializer_size = offset + size;
739 return initializer_size;
744 panic("found invalid initializer");
748 static normal_or_bitfield *glob_vals;
749 static size_t max_vals;
752 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
753 const ir_initializer_t *initializer, ir_type *type)
755 unsigned char last_bits = 0;
756 ir_mode *mode = get_type_mode(type);
758 unsigned char curr_bits;
762 switch(get_initializer_kind(initializer)) {
763 case IR_INITIALIZER_NULL:
765 case IR_INITIALIZER_TARVAL:
766 tv = get_initializer_tarval_value(initializer);
768 case IR_INITIALIZER_CONST: {
769 ir_node *node = get_initializer_const_value(initializer);
770 if(!is_Const(node)) {
771 panic("bitfield initializer not a Const node");
773 tv = get_Const_tarval(node);
776 case IR_INITIALIZER_COMPOUND:
777 panic("bitfield initializer is compound");
780 panic("Couldn't get numeric value for bitfield initializer\n");
783 /* normalize offset */
784 vals += offset_bits >> 3;
786 value_len = get_mode_size_bits(mode);
788 /* combine bits with existing bits */
789 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
790 assert((size_t) (vals - glob_vals) + j < max_vals);
791 assert(vals[j].kind == BITFIELD ||
792 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
793 vals[j].kind = BITFIELD;
794 curr_bits = get_tarval_sub_bits(tv, j);
796 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
798 last_bits = curr_bits;
802 static void dump_ir_initializer(normal_or_bitfield *vals,
803 const ir_initializer_t *initializer,
806 assert((size_t) (vals - glob_vals) < max_vals);
808 switch(get_initializer_kind(initializer)) {
809 case IR_INITIALIZER_NULL:
811 case IR_INITIALIZER_TARVAL: {
814 assert(vals->kind != BITFIELD);
816 vals->v.tarval = get_initializer_tarval_value(initializer);
817 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
818 for(i = 1; i < get_type_size_bytes(type); ++i) {
819 vals[i].kind = NORMAL;
820 vals[i].v.value = NULL;
824 case IR_INITIALIZER_CONST: {
827 assert(vals->kind != BITFIELD);
829 vals->v.value = get_initializer_const_value(initializer);
830 for(i = 1; i < get_type_size_bytes(type); ++i) {
831 vals[i].kind = NORMAL;
832 vals[i].v.value = NULL;
836 case IR_INITIALIZER_COMPOUND: {
838 size_t n = get_initializer_compound_n_entries(initializer);
840 if(is_Array_type(type)) {
841 ir_type *element_type = get_array_element_type(type);
842 size_t skip = get_type_size_bytes(element_type);
843 size_t alignment = get_type_alignment_bytes(element_type);
844 size_t misalign = skip % alignment;
846 skip += alignment - misalign;
849 for(i = 0; i < n; ++i) {
850 ir_initializer_t *sub_initializer
851 = get_initializer_compound_value(initializer, i);
853 dump_ir_initializer(vals, sub_initializer, element_type);
859 assert(is_compound_type(type));
860 n_members = get_compound_n_members(type);
861 for(i = 0; i < n_members; ++i) {
862 ir_entity *member = get_compound_member(type, i);
863 size_t offset = get_entity_offset(member);
864 ir_type *subtype = get_entity_type(member);
865 ir_mode *mode = get_type_mode(subtype);
866 ir_initializer_t *sub_initializer;
868 assert(i < get_initializer_compound_n_entries(initializer));
870 = get_initializer_compound_value(initializer, i);
874 = get_entity_offset_bits_remainder(member);
875 size_t value_len = get_mode_size_bits(mode);
877 if(offset_bits != 0 ||
878 (value_len != 8 && value_len != 16 && value_len != 32
879 && value_len != 64)) {
880 dump_bitfield(&vals[offset], offset_bits,
881 sub_initializer, subtype);
886 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
893 panic("invalid ir_initializer kind found");
896 static void dump_initializer(be_gas_decl_env_t *env, ir_entity *entity)
898 const ir_initializer_t *initializer = entity->attr.initializer;
900 normal_or_bitfield *vals;
904 if(initializer_is_string_const(initializer)) {
905 dump_string_initializer(initializer);
909 type = get_entity_type(entity);
910 size = get_initializer_size(initializer, type);
916 * In the worst case, every initializer allocates one byte.
917 * Moreover, initializer might be big, do not allocate on stack.
919 vals = xcalloc(size, sizeof(vals[0]));
926 dump_ir_initializer(vals, initializer, type);
928 /* now write values sorted */
929 for (k = 0; k < size; ) {
930 int space = 0, skip = 0;
931 if (vals[k].kind == NORMAL) {
932 if(vals[k].v.value != NULL) {
933 dump_atomic_init(env, vals[k].v.value);
934 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
938 } else if(vals[k].kind == TARVAL) {
939 tarval *tv = vals[k].v.tarval;
940 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
945 dump_size_type(size);
946 dump_arith_tarval(tv, size);
948 be_emit_write_line();
950 assert(vals[k].kind == BITFIELD);
951 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
952 be_emit_write_line();
956 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
965 be_emit_irprintf("\t.skip\t%d\n", space);
966 be_emit_write_line();
973 * Dump an initializer for a compound entity.
975 static void dump_compound_init(be_gas_decl_env_t *env, ir_entity *ent)
977 normal_or_bitfield *vals;
979 unsigned k, last_ofs;
981 if(ent->has_initializer) {
982 dump_initializer(env, ent);
986 n = get_compound_ent_n_values(ent);
988 /* Find the initializer size. Sorrily gcc support a nasty feature:
989 The last field of a compound may be a flexible array. This allows
990 initializers bigger than the type size. */
991 last_ofs = get_type_size_bytes(get_entity_type(ent));
992 for (i = 0; i < n; ++i) {
993 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
994 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
995 ir_node *value = get_compound_ent_value(ent, i);
996 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
998 offset += (value_len + bits_remainder + 7) >> 3;
1000 if (offset > last_ofs) {
1006 * In the worst case, every initializer allocates one byte.
1007 * Moreover, initializer might be big, do not allocate on stack.
1009 vals = xcalloc(last_ofs, sizeof(vals[0]));
1011 /* collect the values and store them at the offsets */
1012 for (i = 0; i < n; ++i) {
1013 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1014 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1015 ir_node *value = get_compound_ent_value(ent, i);
1016 int value_len = get_mode_size_bits(get_irn_mode(value));
1018 assert(offset_bits >= 0);
1020 if (offset_bits != 0 ||
1021 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1022 tarval *tv = get_atomic_init_tv(value);
1023 unsigned char curr_bits, last_bits = 0;
1025 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
1026 get_entity_ld_name(ent));
1028 /* normalize offset */
1029 offset += offset_bits >> 3;
1032 for (j = 0; value_len + offset_bits > 0; ++j) {
1033 assert(offset + j < last_ofs);
1034 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1035 vals[offset + j].kind = BITFIELD;
1036 curr_bits = get_tarval_sub_bits(tv, j);
1037 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1039 last_bits = curr_bits;
1044 assert(offset < last_ofs);
1045 assert(vals[offset].kind == NORMAL);
1046 for (i = 1; i < value_len / 8; ++i) {
1047 assert(vals[offset + i].v.value == NULL);
1049 vals[offset].v.value = value;
1053 /* now write them sorted */
1054 for (k = 0; k < last_ofs; ) {
1055 int space = 0, skip = 0;
1056 if (vals[k].kind == NORMAL) {
1057 if(vals[k].v.value != NULL) {
1058 dump_atomic_init(env, vals[k].v.value);
1059 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1064 assert(vals[k].kind == BITFIELD);
1065 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1069 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1078 be_emit_irprintf("\t.skip\t%d\n", space);
1079 be_emit_write_line();
1085 static void emit_align(unsigned alignment)
1087 if (!is_po2(alignment))
1088 panic("alignment not a power of 2");
1090 be_emit_irprintf(".p2align\t%u\n", log2_floor(alignment));
1091 be_emit_write_line();
1095 * Dump a global entity.
1097 * @param env the gas output environment
1098 * @param ent the entity to be dumped
1100 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent)
1102 ir_type *type = get_entity_type(ent);
1103 ident *ld_ident = get_entity_ld_ident(ent);
1104 unsigned align = get_type_alignment_bytes(type);
1105 int emit_as_common = 0;
1106 be_gas_section_t section = env->section;
1107 ir_variability variability = get_entity_variability(ent);
1108 ir_visibility visibility = get_entity_visibility(ent);
1110 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1114 if (section != (be_gas_section_t) -1) {
1116 } else if (variability == variability_constant) {
1117 /* a constant entity, put it on the rdata */
1118 section = GAS_SECTION_RODATA;
1119 if (be_gas_flavour == GAS_FLAVOUR_MACH_O
1120 && ent_is_string_const(ent)) {
1121 section = GAS_SECTION_CSTRING;
1123 } else if (variability == variability_uninitialized) {
1124 /* uninitialized entity put it in bss segment */
1125 section = GAS_SECTION_COMMON;
1126 if (visibility != visibility_local)
1129 section = GAS_SECTION_DATA;
1132 if(!emit_as_common) {
1133 be_gas_emit_switch_section(section);
1136 be_dbg_variable(ent);
1138 /* global or not global */
1139 if (visibility == visibility_external_visible && !emit_as_common) {
1140 be_emit_cstring(".globl\t");
1141 be_emit_ident(ld_ident);
1143 be_emit_write_line();
1144 } else if(visibility == visibility_external_allocated) {
1145 be_emit_cstring(".globl\t");
1146 be_emit_ident(ld_ident);
1148 be_emit_write_line();
1149 /* we can return now... */
1153 if (align > 1 && !emit_as_common && section != GAS_SECTION_PIC_TRAMPOLINES
1154 && section != GAS_SECTION_PIC_SYMBOLS) {
1158 if (!emit_as_common) {
1159 be_emit_ident(ld_ident);
1160 be_emit_cstring(":\n");
1161 be_emit_write_line();
1164 if (variability == variability_uninitialized) {
1165 if (emit_as_common) {
1166 switch (be_gas_flavour) {
1167 case GAS_FLAVOUR_ELF:
1168 case GAS_FLAVOUR_MACH_O:
1169 case GAS_FLAVOUR_YASM:
1170 be_emit_irprintf("\t.comm %s,%u,%u\n",
1171 get_id_str(ld_ident), get_type_size_bytes(type), align);
1172 be_emit_write_line();
1174 case GAS_FLAVOUR_MINGW:
1175 be_emit_irprintf("\t.comm %s,%u # %u\n",
1176 get_id_str(ld_ident), get_type_size_bytes(type), align);
1177 be_emit_write_line();
1180 } else if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1181 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
1182 be_emit_cstring("\t.indirect_symbol ");
1183 be_emit_ident(get_entity_ident(ent));
1185 be_emit_write_line();
1186 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1187 be_emit_write_line();
1189 panic("PIC trampolines not yet supported in this gas mode");
1192 be_emit_irprintf("\t.space %u\n", get_type_size_bytes(type));
1193 be_emit_write_line();
1196 if (is_atomic_entity(ent)) {
1197 dump_atomic_init(env, get_atomic_ent_value(ent));
1199 /* sort_compound_ent_values(ent); */
1201 switch (get_type_tpop_code(get_entity_type(ent))) {
1203 if (ent_is_string_const(ent))
1204 dump_string_cst(ent);
1206 dump_compound_init(env, ent);
1211 dump_compound_init(env, ent);
1221 * Dumps declarations of global variables and the initialization code.
1223 * @param gt a global like type, either the global or the TLS one
1224 * @param env an environment
1225 * @param only_emit_marked if non-zero, external allocated entities that do not have
1226 * its visited flag set are ignored
1228 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
1229 int only_emit_marked)
1231 int i, n = get_compound_n_members(gt);
1232 waitq *worklist = new_waitq();
1234 if (only_emit_marked) {
1235 for (i = 0; i < n; i++) {
1236 ir_entity *ent = get_compound_member(gt, i);
1237 if (is_entity_backend_marked(ent) ||
1238 get_entity_visibility(ent) != visibility_external_allocated) {
1239 waitq_put(worklist, ent);
1240 set_entity_backend_marked(ent, 1);
1244 for (i = 0; i < n; i++) {
1245 ir_entity *ent = get_compound_member(gt, i);
1246 set_entity_backend_marked(ent, 1);
1247 waitq_put(worklist, ent);
1251 env->worklist = worklist;
1253 while (!waitq_empty(worklist)) {
1254 ir_entity *ent = waitq_get(worklist);
1256 dump_global(env, ent);
1259 del_waitq(worklist);
1260 env->worklist = NULL;
1263 /************************************************************************/
1265 /* Generate all entities. */
1266 void be_gas_emit_decls(const be_main_env_t *main_env,
1267 int only_emit_marked_entities)
1269 be_gas_decl_env_t env;
1270 memset(&env, 0, sizeof(env));
1272 env.main_env = main_env;
1274 /* dump global type */
1275 env.section = (be_gas_section_t) -1;
1276 be_gas_dump_globals(get_glob_type(), &env, only_emit_marked_entities);
1277 env.section = GAS_SECTION_TLS;
1278 be_gas_dump_globals(get_tls_type(), &env, only_emit_marked_entities);
1279 env.section = GAS_SECTION_CTOR;
1280 be_gas_dump_globals(get_constructors_type(), &env,
1281 only_emit_marked_entities);
1282 env.section = GAS_SECTION_PIC_SYMBOLS;
1283 be_gas_dump_globals(main_env->pic_symbols_type, &env,
1284 only_emit_marked_entities);
1286 if (get_compound_n_members(main_env->pic_trampolines_type) > 0) {
1287 env.section = GAS_SECTION_PIC_TRAMPOLINES;
1288 be_gas_dump_globals(main_env->pic_trampolines_type, &env,
1289 only_emit_marked_entities);
1290 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
1291 be_emit_cstring("\t.subsections_via_symbols\n");
1292 be_emit_write_line();