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));
248 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x"
249 "%02x%02x%02x%02x%02x%02x%02x%02x",
250 get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
251 get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
252 get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
253 get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
254 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
255 get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
256 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
257 get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
262 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
268 * Return the label prefix for labeled blocks.
270 const char *be_gas_label_prefix(void) {
277 static void dump_label(ir_label_t label) {
278 be_emit_irprintf("%s%ld", be_gas_label_prefix(), label);
282 * Return the tarval of an atomic initializer.
284 * @param init a node representing the initializer (on the const code irg)
288 static tarval *get_atomic_init_tv(ir_node *init)
291 ir_mode *mode = get_irn_mode(init);
293 switch (get_irn_opcode(init)) {
296 init = get_Cast_op(init);
300 init = get_Conv_op(init);
304 return get_Const_tarval(init);
307 switch (get_SymConst_kind(init)) {
308 case symconst_type_size:
309 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
311 case symconst_type_align:
312 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
314 case symconst_ofs_ent:
315 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
317 case symconst_enum_const:
318 return get_enumeration_value(get_SymConst_enum(init));
334 * Dump an atomic value.
336 * @param env the gas output environment
337 * @param init a node representing the atomic value (on the const code irg)
339 static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
341 ir_mode *mode = get_irn_mode(init);
342 int bytes = get_mode_size_bytes(mode);
347 init = skip_Id(init);
349 switch (get_irn_opcode(init)) {
351 do_dump_atomic_init(env, get_Cast_op(init));
355 do_dump_atomic_init(env, get_Conv_op(init));
359 tv = get_Const_tarval(init);
361 /* it's a arithmetic value */
362 dump_arith_tarval(tv, bytes);
366 switch (get_SymConst_kind(init)) {
367 case symconst_addr_name:
368 be_emit_ident(get_SymConst_name(init));
371 case symconst_addr_ent:
372 ent = get_SymConst_entity(init);
373 if(!is_entity_backend_marked(ent)) {
374 waitq_put(env->worklist, ent);
375 set_entity_backend_marked(ent, 1);
377 be_emit_ident(get_entity_ld_ident(ent));
380 case symconst_ofs_ent:
381 ent = get_SymConst_entity(init);
382 #if 0 /* not needed, is it? */
383 if(!is_entity_backend_marked(ent)) {
384 waitq_put(env->worklist, ent);
385 set_entity_backend_marked(ent, 1);
388 be_emit_irprintf("%d", get_entity_offset(ent));
391 case symconst_type_size:
392 be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
395 case symconst_type_align:
396 be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
399 case symconst_enum_const:
400 tv = get_enumeration_value(get_SymConst_enum(init));
401 dump_arith_tarval(tv, bytes);
405 label = get_SymConst_label(init);
410 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
415 do_dump_atomic_init(env, get_Add_left(init));
416 be_emit_cstring(" + ");
417 do_dump_atomic_init(env, get_Add_right(init));
421 do_dump_atomic_init(env, get_Sub_left(init));
422 be_emit_cstring(" - ");
423 do_dump_atomic_init(env, get_Sub_right(init));
427 do_dump_atomic_init(env, get_Mul_left(init));
428 be_emit_cstring(" * ");
429 do_dump_atomic_init(env, get_Mul_right(init));
433 assert(0 && "dump_atomic_init(): unknown IR-node");
438 * Dumps the type for given size (.byte, .long, ...)
440 * @param size the size in bytes
442 static void dump_size_type(size_t size) {
445 be_emit_cstring("\t.byte\t");
449 be_emit_cstring("\t.word\t");
453 be_emit_cstring("\t.long\t");
457 be_emit_cstring("\t.quad\t");
462 /* handled in arith */
466 be_emit_cstring("\t.octa\t");
470 panic("Try to dump a type with %u bytes\n", (unsigned) size);
475 * Emit an atomic value.
477 * @param env the gas output environment
478 * @param init a node representing the atomic value (on the const code irg)
480 static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
482 ir_mode *mode = get_irn_mode(init);
483 int bytes = get_mode_size_bytes(mode);
485 dump_size_type(bytes);
486 do_dump_atomic_init(env, init);
488 be_emit_write_line();
491 /************************************************************************/
492 /* Routines to dump global variables */
493 /************************************************************************/
495 static int initializer_is_string_const(const ir_initializer_t *initializer)
498 int found_printable = 0;
500 if(initializer->kind != IR_INITIALIZER_COMPOUND)
503 len = initializer->compound.n_initializers;
506 for(i = 0; i < len; ++i) {
510 ir_initializer_t *sub_initializer
511 = initializer->compound.initializers[i];
513 if(sub_initializer->kind != IR_INITIALIZER_TARVAL)
516 tv = sub_initializer->tarval.value;
517 mode = get_tarval_mode(tv);
519 if (!mode_is_int(mode)
520 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
523 c = get_tarval_long(tv);
524 if (isgraph(c) || isspace(c))
529 if (i == len - 1 && c != '\0')
533 return found_printable;
537 * Determine if an entity is a string constant
538 * @param ent The entity
539 * @return 1 if it is a string constant, 0 otherwise
541 static int ent_is_string_const(ir_entity *ent)
543 ir_type *type, *element_type;
546 int found_printable = 0;
548 type = get_entity_type(ent);
550 /* if it's an array */
551 if (!is_Array_type(type))
554 element_type = get_array_element_type(type);
556 /* and the array's element type is primitive */
557 if (!is_Primitive_type(element_type))
560 /* and the mode of the element type is an int of
561 * the same size as the byte mode */
562 mode = get_type_mode(element_type);
563 if (!mode_is_int(mode)
564 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
567 if(ent->has_initializer) {
571 /* if it contains only printable chars and a 0 at the end */
572 n = get_compound_ent_n_values(ent);
573 for (i = 0; i < n; ++i) {
574 ir_node *irn = get_compound_ent_value(ent, i);
578 c = (int) get_tarval_long(get_Const_tarval(irn));
580 if (isgraph(c) || isspace(c))
585 if (i == n - 1 && c != '\0')
590 /* then we can emit it as a string constant */
591 return found_printable;
595 * Dump a string constant.
596 * No checks are made!!
598 * @param ent The entity to dump.
600 static void dump_string_cst(ir_entity *ent)
607 len = get_compound_ent_n_values(ent);
608 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
609 be_emit_cstring("\t.ascii \"");
611 be_emit_cstring("\t.string \"");
615 for (i = 0; i < len; ++i) {
619 irn = get_compound_ent_value(ent, i);
620 c = (int) get_tarval_long(get_Const_tarval(irn));
623 case '"' : be_emit_cstring("\\\""); break;
624 case '\n': be_emit_cstring("\\n"); break;
625 case '\r': be_emit_cstring("\\r"); break;
626 case '\t': be_emit_cstring("\\t"); break;
627 case '\\': be_emit_cstring("\\\\"); break;
632 be_emit_irprintf("\\%o", c);
636 be_emit_cstring("\"\n");
637 be_emit_write_line();
639 type = get_entity_type(ent);
640 type_size = get_type_size_bytes(type);
641 remaining_space = type_size - len;
642 assert(remaining_space >= 0);
643 if(remaining_space > 0) {
644 be_emit_irprintf("\t.skip\t%d\n", remaining_space);
648 static void dump_string_initializer(const ir_initializer_t *initializer)
652 len = initializer->compound.n_initializers;
653 if(be_gas_flavour == GAS_FLAVOUR_MACH_O) {
654 be_emit_cstring("\t.ascii \"");
656 be_emit_cstring("\t.string \"");
660 for(i = 0; i < len; ++i) {
661 const ir_initializer_t *sub_initializer
662 = get_initializer_compound_value(initializer, i);
664 tarval *tv = get_initializer_tarval_value(sub_initializer);
665 int c = get_tarval_long(tv);
668 case '"' : be_emit_cstring("\\\""); break;
669 case '\n': be_emit_cstring("\\n"); break;
670 case '\r': be_emit_cstring("\\r"); break;
671 case '\t': be_emit_cstring("\\t"); break;
672 case '\\': be_emit_cstring("\\\\"); break;
677 be_emit_irprintf("\\%o", c);
681 be_emit_cstring("\"\n");
682 be_emit_write_line();
685 enum normal_or_bitfield_kind {
692 enum normal_or_bitfield_kind kind;
696 unsigned char bf_val;
698 } normal_or_bitfield;
700 static int is_type_variable_size(ir_type *type)
707 static size_t get_initializer_size(const ir_initializer_t *initializer,
710 switch(get_initializer_kind(initializer)) {
711 case IR_INITIALIZER_TARVAL: {
712 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
713 return get_type_size_bytes(type);
715 case IR_INITIALIZER_CONST:
716 case IR_INITIALIZER_NULL:
717 return get_type_size_bytes(type);
718 case IR_INITIALIZER_COMPOUND: {
719 if(!is_type_variable_size(type)) {
720 return get_type_size_bytes(type);
723 = get_initializer_compound_n_entries(initializer);
725 unsigned initializer_size = get_type_size_bytes(type);
726 for(i = 0; i < n_entries; ++i) {
727 ir_entity *entity = get_compound_member(type, i);
728 ir_type *type = get_entity_type(entity);
730 const ir_initializer_t *sub_initializer
731 = get_initializer_compound_value(initializer, i);
733 unsigned offset = get_entity_offset(entity);
734 unsigned size = get_initializer_size(sub_initializer, type);
736 if(offset + size > initializer_size) {
737 initializer_size = offset + size;
740 return initializer_size;
745 panic("found invalid initializer");
749 static normal_or_bitfield *glob_vals;
750 static size_t max_vals;
753 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
754 const ir_initializer_t *initializer, ir_type *type)
756 unsigned char last_bits = 0;
757 ir_mode *mode = get_type_mode(type);
759 unsigned char curr_bits;
763 switch(get_initializer_kind(initializer)) {
764 case IR_INITIALIZER_NULL:
766 case IR_INITIALIZER_TARVAL:
767 tv = get_initializer_tarval_value(initializer);
769 case IR_INITIALIZER_CONST: {
770 ir_node *node = get_initializer_const_value(initializer);
771 if(!is_Const(node)) {
772 panic("bitfield initializer not a Const node");
774 tv = get_Const_tarval(node);
777 case IR_INITIALIZER_COMPOUND:
778 panic("bitfield initializer is compound");
781 panic("Couldn't get numeric value for bitfield initializer\n");
784 /* normalize offset */
785 vals += offset_bits >> 3;
787 value_len = get_mode_size_bits(mode);
789 /* combine bits with existing bits */
790 for (j = 0; value_len + (int) offset_bits > 0; ++j) {
791 assert((size_t) (vals - glob_vals) + j < max_vals);
792 assert(vals[j].kind == BITFIELD ||
793 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
794 vals[j].kind = BITFIELD;
795 curr_bits = get_tarval_sub_bits(tv, j);
797 |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
799 last_bits = curr_bits;
803 static void dump_ir_initializer(normal_or_bitfield *vals,
804 const ir_initializer_t *initializer,
807 assert((size_t) (vals - glob_vals) < max_vals);
809 switch(get_initializer_kind(initializer)) {
810 case IR_INITIALIZER_NULL:
812 case IR_INITIALIZER_TARVAL: {
815 assert(vals->kind != BITFIELD);
817 vals->v.tarval = get_initializer_tarval_value(initializer);
818 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
819 for(i = 1; i < get_type_size_bytes(type); ++i) {
820 vals[i].kind = NORMAL;
821 vals[i].v.value = NULL;
825 case IR_INITIALIZER_CONST: {
828 assert(vals->kind != BITFIELD);
830 vals->v.value = get_initializer_const_value(initializer);
831 for(i = 1; i < get_type_size_bytes(type); ++i) {
832 vals[i].kind = NORMAL;
833 vals[i].v.value = NULL;
837 case IR_INITIALIZER_COMPOUND: {
839 size_t n = get_initializer_compound_n_entries(initializer);
841 if(is_Array_type(type)) {
842 ir_type *element_type = get_array_element_type(type);
843 size_t skip = get_type_size_bytes(element_type);
844 size_t alignment = get_type_alignment_bytes(element_type);
845 size_t misalign = skip % alignment;
847 skip += alignment - misalign;
850 for(i = 0; i < n; ++i) {
851 ir_initializer_t *sub_initializer
852 = get_initializer_compound_value(initializer, i);
854 dump_ir_initializer(vals, sub_initializer, element_type);
860 assert(is_compound_type(type));
861 n_members = get_compound_n_members(type);
862 for(i = 0; i < n_members; ++i) {
863 ir_entity *member = get_compound_member(type, i);
864 size_t offset = get_entity_offset(member);
865 ir_type *subtype = get_entity_type(member);
866 ir_mode *mode = get_type_mode(subtype);
867 ir_initializer_t *sub_initializer;
869 assert(i < get_initializer_compound_n_entries(initializer));
871 = get_initializer_compound_value(initializer, i);
875 = get_entity_offset_bits_remainder(member);
876 size_t value_len = get_mode_size_bits(mode);
878 if(offset_bits != 0 ||
879 (value_len != 8 && value_len != 16 && value_len != 32
880 && value_len != 64)) {
881 dump_bitfield(&vals[offset], offset_bits,
882 sub_initializer, subtype);
887 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
894 panic("invalid ir_initializer kind found");
897 static void dump_initializer(be_gas_decl_env_t *env, ir_entity *entity)
899 const ir_initializer_t *initializer = entity->attr.initializer;
901 normal_or_bitfield *vals;
905 if(initializer_is_string_const(initializer)) {
906 dump_string_initializer(initializer);
910 type = get_entity_type(entity);
911 size = get_initializer_size(initializer, type);
917 * In the worst case, every initializer allocates one byte.
918 * Moreover, initializer might be big, do not allocate on stack.
920 vals = xcalloc(size, sizeof(vals[0]));
927 dump_ir_initializer(vals, initializer, type);
929 /* now write values sorted */
930 for (k = 0; k < size; ) {
931 int space = 0, skip = 0;
932 if (vals[k].kind == NORMAL) {
933 if(vals[k].v.value != NULL) {
934 dump_atomic_init(env, vals[k].v.value);
935 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
939 } else if(vals[k].kind == TARVAL) {
940 tarval *tv = vals[k].v.tarval;
941 size_t size = get_mode_size_bytes(get_tarval_mode(tv));
946 dump_size_type(size);
947 dump_arith_tarval(tv, size);
949 be_emit_write_line();
951 assert(vals[k].kind == BITFIELD);
952 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
953 be_emit_write_line();
957 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
966 be_emit_irprintf("\t.skip\t%d\n", space);
967 be_emit_write_line();
974 * Dump an initializer for a compound entity.
976 static void dump_compound_init(be_gas_decl_env_t *env, ir_entity *ent)
978 normal_or_bitfield *vals;
980 unsigned k, last_ofs;
982 if(ent->has_initializer) {
983 dump_initializer(env, ent);
987 n = get_compound_ent_n_values(ent);
989 /* Find the initializer size. Sorrily gcc support a nasty feature:
990 The last field of a compound may be a flexible array. This allows
991 initializers bigger than the type size. */
992 last_ofs = get_type_size_bytes(get_entity_type(ent));
993 for (i = 0; i < n; ++i) {
994 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
995 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
996 ir_node *value = get_compound_ent_value(ent, i);
997 unsigned value_len = get_mode_size_bits(get_irn_mode(value));
999 offset += (value_len + bits_remainder + 7) >> 3;
1001 if (offset > last_ofs) {
1007 * In the worst case, every initializer allocates one byte.
1008 * Moreover, initializer might be big, do not allocate on stack.
1010 vals = xcalloc(last_ofs, sizeof(vals[0]));
1012 /* collect the values and store them at the offsets */
1013 for (i = 0; i < n; ++i) {
1014 unsigned offset = get_compound_ent_value_offset_bytes(ent, i);
1015 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1016 ir_node *value = get_compound_ent_value(ent, i);
1017 int value_len = get_mode_size_bits(get_irn_mode(value));
1019 assert(offset_bits >= 0);
1021 if (offset_bits != 0 ||
1022 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1023 tarval *tv = get_atomic_init_tv(value);
1024 unsigned char curr_bits, last_bits = 0;
1026 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
1027 get_entity_ld_name(ent));
1029 /* normalize offset */
1030 offset += offset_bits >> 3;
1033 for (j = 0; value_len + offset_bits > 0; ++j) {
1034 assert(offset + j < last_ofs);
1035 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1036 vals[offset + j].kind = BITFIELD;
1037 curr_bits = get_tarval_sub_bits(tv, j);
1038 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1040 last_bits = curr_bits;
1045 assert(offset < last_ofs);
1046 assert(vals[offset].kind == NORMAL);
1047 for (i = 1; i < value_len / 8; ++i) {
1048 assert(vals[offset + i].v.value == NULL);
1050 vals[offset].v.value = value;
1054 /* now write them sorted */
1055 for (k = 0; k < last_ofs; ) {
1056 int space = 0, skip = 0;
1057 if (vals[k].kind == NORMAL) {
1058 if(vals[k].v.value != NULL) {
1059 dump_atomic_init(env, vals[k].v.value);
1060 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1065 assert(vals[k].kind == BITFIELD);
1066 be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1070 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1079 be_emit_irprintf("\t.skip\t%d\n", space);
1080 be_emit_write_line();
1086 static void emit_align(unsigned alignment)
1088 if (!is_po2(alignment))
1089 panic("alignment not a power of 2");
1091 be_emit_irprintf(".p2align\t%u\n", log2_floor(alignment));
1092 be_emit_write_line();
1096 * Dump a global entity.
1098 * @param env the gas output environment
1099 * @param ent the entity to be dumped
1101 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent)
1103 ir_type *type = get_entity_type(ent);
1104 ident *ld_ident = get_entity_ld_ident(ent);
1105 unsigned align = get_type_alignment_bytes(type);
1106 int emit_as_common = 0;
1107 be_gas_section_t section = env->section;
1108 ir_variability variability = get_entity_variability(ent);
1109 ir_visibility visibility = get_entity_visibility(ent);
1111 if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1115 if (section != (be_gas_section_t) -1) {
1117 } else if (variability == variability_constant) {
1118 /* a constant entity, put it on the rdata */
1119 section = GAS_SECTION_RODATA;
1120 if (be_gas_flavour == GAS_FLAVOUR_MACH_O
1121 && ent_is_string_const(ent)) {
1122 section = GAS_SECTION_CSTRING;
1124 } else if (variability == variability_uninitialized) {
1125 /* uninitialized entity put it in bss segment */
1126 section = GAS_SECTION_COMMON;
1127 if (visibility != visibility_local)
1130 section = GAS_SECTION_DATA;
1133 if(!emit_as_common) {
1134 be_gas_emit_switch_section(section);
1137 be_dbg_variable(ent);
1139 /* global or not global */
1140 if (visibility == visibility_external_visible && !emit_as_common) {
1141 be_emit_cstring(".globl\t");
1142 be_emit_ident(ld_ident);
1144 be_emit_write_line();
1145 } else if(visibility == visibility_external_allocated) {
1146 be_emit_cstring(".globl\t");
1147 be_emit_ident(ld_ident);
1149 be_emit_write_line();
1150 /* we can return now... */
1154 if (align > 1 && !emit_as_common && section != GAS_SECTION_PIC_TRAMPOLINES
1155 && section != GAS_SECTION_PIC_SYMBOLS) {
1159 if (!emit_as_common) {
1160 be_emit_ident(ld_ident);
1161 be_emit_cstring(":\n");
1162 be_emit_write_line();
1165 if (variability == variability_uninitialized) {
1166 if (emit_as_common) {
1167 switch (be_gas_flavour) {
1168 case GAS_FLAVOUR_ELF:
1169 case GAS_FLAVOUR_MACH_O:
1170 case GAS_FLAVOUR_YASM:
1171 be_emit_irprintf("\t.comm %s,%u,%u\n",
1172 get_id_str(ld_ident), get_type_size_bytes(type), align);
1173 be_emit_write_line();
1175 case GAS_FLAVOUR_MINGW:
1176 be_emit_irprintf("\t.comm %s,%u # %u\n",
1177 get_id_str(ld_ident), get_type_size_bytes(type), align);
1178 be_emit_write_line();
1181 } else if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1182 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
1183 be_emit_cstring("\t.indirect_symbol ");
1184 be_emit_ident(get_entity_ident(ent));
1186 be_emit_write_line();
1187 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1188 be_emit_write_line();
1190 panic("PIC trampolines not yet supported in this gas mode");
1193 be_emit_irprintf("\t.space %u\n", get_type_size_bytes(type));
1194 be_emit_write_line();
1197 if (is_atomic_entity(ent)) {
1198 dump_atomic_init(env, get_atomic_ent_value(ent));
1200 /* sort_compound_ent_values(ent); */
1202 switch (get_type_tpop_code(get_entity_type(ent))) {
1204 if (ent_is_string_const(ent))
1205 dump_string_cst(ent);
1207 dump_compound_init(env, ent);
1212 dump_compound_init(env, ent);
1222 * Dumps declarations of global variables and the initialization code.
1224 * @param gt a global like type, either the global or the TLS one
1225 * @param env an environment
1226 * @param only_emit_marked if non-zero, external allocated entities that do not have
1227 * its visited flag set are ignored
1229 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
1230 int only_emit_marked)
1232 int i, n = get_compound_n_members(gt);
1233 waitq *worklist = new_waitq();
1235 if (only_emit_marked) {
1236 for (i = 0; i < n; i++) {
1237 ir_entity *ent = get_compound_member(gt, i);
1238 if (is_entity_backend_marked(ent) ||
1239 get_entity_visibility(ent) != visibility_external_allocated) {
1240 waitq_put(worklist, ent);
1241 set_entity_backend_marked(ent, 1);
1245 for (i = 0; i < n; i++) {
1246 ir_entity *ent = get_compound_member(gt, i);
1247 set_entity_backend_marked(ent, 1);
1248 waitq_put(worklist, ent);
1252 env->worklist = worklist;
1254 while (!waitq_empty(worklist)) {
1255 ir_entity *ent = waitq_get(worklist);
1257 dump_global(env, ent);
1260 del_waitq(worklist);
1261 env->worklist = NULL;
1264 /************************************************************************/
1266 /* Generate all entities. */
1267 void be_gas_emit_decls(const be_main_env_t *main_env,
1268 int only_emit_marked_entities)
1270 be_gas_decl_env_t env;
1271 memset(&env, 0, sizeof(env));
1273 env.main_env = main_env;
1275 /* dump global type */
1276 env.section = (be_gas_section_t) -1;
1277 be_gas_dump_globals(get_glob_type(), &env, only_emit_marked_entities);
1278 env.section = GAS_SECTION_TLS;
1279 be_gas_dump_globals(get_tls_type(), &env, only_emit_marked_entities);
1280 env.section = GAS_SECTION_CTOR;
1281 be_gas_dump_globals(get_constructors_type(), &env,
1282 only_emit_marked_entities);
1283 env.section = GAS_SECTION_PIC_SYMBOLS;
1284 be_gas_dump_globals(main_env->pic_symbols_type, &env,
1285 only_emit_marked_entities);
1287 if (get_compound_n_members(main_env->pic_trampolines_type) > 0) {
1288 env.section = GAS_SECTION_PIC_TRAMPOLINES;
1289 be_gas_dump_globals(main_env->pic_trampolines_type, &env,
1290 only_emit_marked_entities);
1291 if (be_gas_flavour == GAS_FLAVOUR_MACH_O) {
1292 be_emit_cstring("\t.subsections_via_symbols\n");
1293 be_emit_write_line();