5f580e2823f920f53f0a28e73f106884069c7326
[libfirm] / ir / be / begnuas.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Dumps global variables and constants as gas assembler.
23  * @author      Christian Wuerdig, Matthias Braun
24  * @date        04.11.2005
25  * @version     $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "begnuas.h"
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <assert.h>
37
38 #include "obst.h"
39 #include "tv.h"
40 #include "irnode.h"
41 #include "irprog.h"
42 #include "pdeq.h"
43 #include "error.h"
44
45 #include "be_t.h"
46 #include "beemitter.h"
47 #include "be_dbgout.h"
48
49 typedef struct obstack obstack_t;
50
51 /** by default, we generate assembler code for the Linux gas */
52 be_gas_flavour_t be_gas_flavour = GAS_FLAVOUR_NORMAL;
53
54 /**
55  * Return the pseudo-instruction to be issued for a section switch
56  * depending on the current flavour.
57  *
58  * @param section  the section to switch to
59  *
60  * @return  the pseudo-instruction
61  */
62 static const char *get_section_name(be_gas_section_t section) {
63         static const char *text[GAS_FLAVOUR_MAX][GAS_SECTION_MAX] = {
64                 {
65                         ".section\t.text",
66                         ".section\t.data",
67                         ".section\t.rodata",
68                         ".section\t.bss",
69                         ".section\t.tbss,\"awT\",@nobits",
70                         ".section\t.ctors,\"aw\",@progbits"
71                 },
72                 {
73                         ".section\t.text",
74                         ".section\t.data",
75                         ".section .rdata,\"dr\"",
76                         ".section\t.bss",
77                         ".section\t.tbss,\"awT\",@nobits",
78                         ".section\t.ctors,\"aw\",@progbits"
79                 }
80         };
81
82         assert((int) be_gas_flavour >= 0 && be_gas_flavour < GAS_FLAVOUR_MAX);
83         assert((int) section >= 0 && section < GAS_SECTION_MAX);
84         return text[be_gas_flavour][section];
85 }
86
87 /**
88  * Emit necessary code to switch to a section.
89  *
90  * @param env      the emitter environment
91  * @param section  the section to switch to
92  */
93 void be_gas_emit_switch_section(be_emit_env_t *env, be_gas_section_t section) {
94         be_emit_char(env, '\t');
95         be_emit_string(env, get_section_name(section));
96         be_emit_char(env, '\n');
97         be_emit_write_line(env);
98 }
99
100 /**
101  * An environment containing all needed dumper data.
102  * Currently we create the file completely in memory first, then
103  * write it to the disk. This is an artifact from the old C-generating backend
104  * and even there NOT needed. So we might change it in the future.
105  */
106 typedef struct _be_gas_decl_env {
107         obstack_t *rodata_obst;        /**< An obstack that will be filled with all rodata entities. */
108         obstack_t *data_obst;          /**< An obstack that will be filled with the initialized entities. */
109         obstack_t *bss_obst;           /**< An obstack that will be filled with the uninitialized entities. */
110         obstack_t *ctor_obst;          /**< An obstack that will be filled with the constructor entities. */
111         const be_main_env_t *main_env; /**< The main backend environment, used for it's debug handle. */
112         waitq     *worklist;           /**< A worklist we use to place not yet handled entities on. */
113 } be_gas_decl_env_t;
114
115 /************************************************************************/
116
117 /**
118  * Output a tarval.
119  *
120  * @param obst   the obstack where the data is written too
121  * @param tv     the tarval
122  * @param bytes  the width of the tarvals value in bytes
123  */
124 static void dump_arith_tarval(obstack_t *obst, tarval *tv, int bytes)
125 {
126         switch (bytes) {
127
128         case 1:
129                 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
130                 break;
131
132         case 2:
133                 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
134                 break;
135
136         case 4:
137                 obstack_printf(obst, "0x%02x%02x%02x%02x",
138                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
139                 break;
140
141         case 8:
142                 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
143                         get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
144                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
145                 break;
146
147         case 10:
148         case 12:
149                 break;
150
151         case 16:
152                 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x"
153                                                "%02x%02x%02x%02x%02x%02x%02x%02x",
154                         get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
155                         get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
156                         get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
157                         get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
158                         get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
159                         get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
160                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
161                         get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
162                 break;
163
164
165         default:
166                 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
167                 assert(0);
168         }
169 }
170
171 const char *gnu_label_prefix(void) {
172         return ".LG";
173 }
174
175 /**
176  * Dump a label.
177  */
178 static void dump_label(obstack_t *obst, ir_label_t label) {
179         obstack_printf(obst, "%s%ld", gnu_label_prefix(), label);
180 }
181
182 /**
183  * Return the tarval of an atomic initializer.
184  *
185  * @param init  a node representing the initializer (on the const code irg)
186  *
187  * @return the tarval
188  */
189 static tarval *get_atomic_init_tv(ir_node *init)
190 {
191         for (;;) {
192                 ir_mode *mode = get_irn_mode(init);
193
194                 switch (get_irn_opcode(init)) {
195
196                 case iro_Cast:
197                         init = get_Cast_op(init);
198                         continue;
199
200                 case iro_Conv:
201                         init = get_Conv_op(init);
202                         continue;
203
204                 case iro_Const:
205                         return get_Const_tarval(init);
206
207                 case iro_SymConst:
208                         switch (get_SymConst_kind(init)) {
209                         case symconst_type_size:
210                                 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
211
212                         case symconst_type_align:
213                                 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
214
215                         case symconst_ofs_ent:
216                                 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
217
218                         case symconst_enum_const:
219                                 return get_enumeration_value(get_SymConst_enum(init));
220
221                         case symconst_label:
222                                 return NULL;
223
224                         default:
225                                 return NULL;
226                         }
227
228                 default:
229                         return NULL;
230                 }
231         }
232 }
233
234 /**
235  * Dump an atomic value.
236  *
237  * @param env   the gas output environment
238  * @param obst  an obstack the output is written to
239  * @param init  a node representing the atomic value (on the const code irg)
240  */
241 static void do_dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
242                                 ir_node *init)
243 {
244         ir_mode *mode = get_irn_mode(init);
245         int bytes     = get_mode_size_bytes(mode);
246         tarval *tv;
247         ir_label_t label;
248         ir_entity *ent;
249
250         switch (get_irn_opcode(init)) {
251
252         case iro_Cast:
253                 do_dump_atomic_init(env, obst, get_Cast_op(init));
254                 return;
255
256         case iro_Conv:
257                 do_dump_atomic_init(env, obst, get_Conv_op(init));
258                 return;
259
260         case iro_Const:
261                 tv = get_Const_tarval(init);
262
263                 /* it's a arithmetic value */
264                 dump_arith_tarval(obst, tv, bytes);
265                 return;
266
267         case iro_SymConst:
268                 switch (get_SymConst_kind(init)) {
269                 case symconst_addr_name:
270                         obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
271                         break;
272
273                 case symconst_addr_ent:
274                         ent = get_SymConst_entity(init);
275                         if(!is_entity_backend_marked(ent)) {
276                                 waitq_put(env->worklist, ent);
277                                 set_entity_backend_marked(ent, 1);
278                         }
279                         obstack_printf(obst, "%s", get_entity_ld_name(ent));
280                         break;
281
282                 case symconst_ofs_ent:
283                         ent = get_SymConst_entity(init);
284 #if 0       /* not needed, is it? */
285                         if(!is_entity_backend_marked(ent)) {
286                                 waitq_put(env->worklist, ent);
287                                 set_entity_backend_marked(ent, 1);
288                         }
289 #endif
290                         obstack_printf(obst, "%d", get_entity_offset(ent));
291                         break;
292
293                 case symconst_type_size:
294                         obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
295                         break;
296
297                 case symconst_type_align:
298                         obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
299                         break;
300
301                 case symconst_enum_const:
302                         tv = get_enumeration_value(get_SymConst_enum(init));
303                         dump_arith_tarval(obst, tv, bytes);
304                         break;
305
306                 case symconst_label:
307                         label = get_SymConst_label(init);
308                         dump_label(obst, label);
309                         break;
310
311                 default:
312                         assert(!"dump_atomic_init(): don't know how to init from this SymConst");
313                 }
314                 return;
315
316                 case iro_Add:
317                         do_dump_atomic_init(env, obst, get_Add_left(init));
318                         obstack_printf(obst, " + ");
319                         do_dump_atomic_init(env, obst, get_Add_right(init));
320                         return;
321
322                 case iro_Sub:
323                         do_dump_atomic_init(env, obst, get_Sub_left(init));
324                         obstack_printf(obst, " - ");
325                         do_dump_atomic_init(env, obst, get_Sub_right(init));
326                         return;
327
328                 case iro_Mul:
329                         do_dump_atomic_init(env, obst, get_Mul_left(init));
330                         obstack_printf(obst, " * ");
331                         do_dump_atomic_init(env, obst, get_Mul_right(init));
332                         return;
333
334                 default:
335                         assert(0 && "dump_atomic_init(): unknown IR-node");
336         }
337 }
338
339 /**
340  * Dumps the type for given size (.byte, .long, ...)
341  *
342  * @param obst  an obstack the output is written to
343  * @param size  the size in bytes
344  */
345 static void dump_size_type(obstack_t *obst, int size) {
346         switch (size) {
347
348         case 1:
349                 obstack_printf(obst, "\t.byte\t");
350                 break;
351
352         case 2:
353                 obstack_printf(obst, "\t.value\t");
354                 break;
355
356         case 4:
357                 obstack_printf(obst, "\t.long\t");
358                 break;
359
360         case 8:
361                 obstack_printf(obst, "\t.quad\t");
362                 break;
363
364         case 10:
365         case 12:
366                 /* handled in arith */
367                 break;
368
369         case 16:
370                 obstack_printf(obst, "\t.octa\t");
371                 break;
372
373         default:
374                 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
375                 assert(0);
376         }
377 }
378
379 /**
380  * Dump an atomic value to an obstack.
381  *
382  * @param env   the gas output environment
383  * @param obst  an obstack the output is written to
384  * @param init  a node representing the atomic value (on the const code irg)
385  */
386 static void dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
387                              ir_node *init)
388 {
389         ir_mode *mode = get_irn_mode(init);
390         int bytes     = get_mode_size_bytes(mode);
391
392         dump_size_type(obst, bytes);
393         do_dump_atomic_init(env, obst, init);
394         obstack_printf(obst, "\n");
395 }
396
397 /************************************************************************/
398 /* Routines to dump global variables                                    */
399 /************************************************************************/
400
401 /**
402  * Determine if an entity is a string constant
403  * @param ent The entity
404  * @return 1 if it is a string constant, 0 otherwise
405  */
406 static int ent_is_string_const(ir_entity *ent)
407 {
408         ir_type *type, *element_type;
409         ir_mode *mode;
410         int i, c, n;
411
412         type = get_entity_type(ent);
413
414         /* if it's an array */
415         if (!is_Array_type(type))
416                 return 0;
417
418         element_type = get_array_element_type(type);
419
420         /* and the array's element type is primitive */
421         if (!is_Primitive_type(element_type))
422                 return 0;
423
424         /* and the mode of the element type is an int of
425          * the same size as the byte mode */
426         mode = get_type_mode(element_type);
427         if (!mode_is_int(mode)
428                 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
429                 return 0;
430
431         /* if it contains only printable chars and a 0 at the end */
432         n = get_compound_ent_n_values(ent);
433         for (i = 0; i < n; ++i) {
434                 ir_node *irn = get_compound_ent_value(ent, i);
435                 if(get_irn_opcode(irn) != iro_Const)
436                         return 0;
437
438                 c = (int) get_tarval_long(get_Const_tarval(irn));
439
440                 if((i < n - 1 && !(isgraph(c) || isspace(c)))
441                                 || (i == n - 1 && c != '\0'))
442                         return 0;
443         }
444
445         /* then we can emit it as a string constant */
446         return 1;
447 }
448
449 /**
450  * Dump a string constant.
451  * No checks are made!!
452  *
453  * @param obst The obst to dump on.
454  * @param ent  The entity to dump.
455  */
456 static void dump_string_cst(obstack_t *obst, ir_entity *ent)
457 {
458         int      i, n;
459         ir_type *type;
460         int      type_size;
461         int      remaining_space;
462
463         obstack_printf(obst, "\t.string \"");
464         n = get_compound_ent_n_values(ent);
465
466         for (i = 0; i < n-1; ++i) {
467                 ir_node *irn;
468                 int c;
469
470                 irn = get_compound_ent_value(ent, i);
471                 c = (int) get_tarval_long(get_Const_tarval(irn));
472
473                 switch (c) {
474                 case '"' : obstack_printf(obst, "\\\""); break;
475                 case '\n': obstack_printf(obst, "\\n"); break;
476                 case '\r': obstack_printf(obst, "\\r"); break;
477                 case '\t': obstack_printf(obst, "\\t"); break;
478                 case '\\': obstack_printf(obst, "\\\\"); break;
479                 default  :
480                         if (isprint(c))
481                                 obstack_printf(obst, "%c", c);
482                         else
483                                 obstack_printf(obst, "\\%o", c);
484                         break;
485                 }
486         }
487         obstack_printf(obst, "\"\n");
488
489         type            = get_entity_type(ent);
490         type_size       = get_type_size_bytes(type);
491         remaining_space = type_size - n;
492         assert(remaining_space >= 0);
493         if(remaining_space > 0) {
494                 obstack_printf(obst, "\t.skip\t%d\n", remaining_space);
495         }
496 }
497
498 enum normal_or_bitfield_kind {
499         NORMAL = 0,
500         BITFIELD
501 };
502
503 typedef struct {
504         enum normal_or_bitfield_kind kind;
505         union {
506                 ir_node *value;
507                 unsigned char bf_val;
508         } v;
509 } normal_or_bitfield;
510
511 /**
512  * Dump an initializer for a compound entity.
513  */
514 static void dump_compound_init(be_gas_decl_env_t *env, obstack_t *obst,
515                                ir_entity *ent)
516 {
517         normal_or_bitfield *vals;
518         int i, j, n = get_compound_ent_n_values(ent);
519         int last_ofs;
520
521         /* Find the initializer size. Sorrily gcc support a nasty feature:
522            The last field of a compound may be a flexible array. This allows
523            initializers bigger than the type size. */
524         last_ofs = get_type_size_bytes(get_entity_type(ent));
525         for (i = 0; i < n; ++i) {
526                 int offset = get_compound_ent_value_offset_bytes(ent, i);
527                 int bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
528                 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
529                 int path_len = get_compound_graph_path_length(path);
530                 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
531                 int value_len = get_type_size_bits(get_entity_type(last_ent));
532
533                 offset += (value_len + bits_remainder + 7) >> 3;
534
535                 if (offset > last_ofs) {
536                         last_ofs = offset;
537                 }
538         }
539
540         /*
541          * In the worst case, every initializer allocates one byte.
542          * Moreover, initializer might be big, do not allocate on stack.
543          */
544         vals = xcalloc(last_ofs, sizeof(vals[0]));
545
546         /* collect the values and store them at the offsets */
547         for (i = 0; i < n; ++i) {
548                 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
549                 int path_len = get_compound_graph_path_length(path);
550                 int offset = get_compound_ent_value_offset_bytes(ent, i);
551                 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
552                 ir_node *value = get_compound_ent_value(ent, i);
553                 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
554                 int value_len = get_type_size_bits(get_entity_type(last_ent));
555                 assert(offset >= 0);
556                 assert(offset_bits >= 0);
557
558                 if (offset_bits != 0 ||
559                         (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
560                         tarval *shift, *shifted;
561                         tarval *tv = get_atomic_init_tv(value);
562                         if (tv == NULL) {
563                                 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
564                                       get_entity_ld_name(ent));
565                         }
566                         tv = tarval_convert_to(tv, mode_Lu);
567                         shift = new_tarval_from_long(offset_bits, mode_Is);
568                         shifted = tarval_shl(tv, shift);
569                         if (shifted == tarval_bad || shifted == tarval_undefined) {
570                                 panic("Couldn't shift numeric value for bitfield initializer '%s'\n",
571                                       get_entity_ld_name(ent));
572                         }
573
574                         for (j = 0; value_len > 0; ++j) {
575                                 assert(offset + j < last_ofs);
576                                 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
577                                 vals[offset + j].kind = BITFIELD;
578                                 vals[offset + j].v.bf_val |= get_tarval_sub_bits(shifted, j);
579                                 value_len -= 8 - offset_bits;
580                                 offset_bits = 0;
581                         }
582                 } else {
583                         assert(offset < last_ofs);
584                         assert(vals[offset].kind == NORMAL);
585                         assert(vals[offset].v.value == NULL);
586                         vals[offset].v.value = value;
587                 }
588         }
589
590         /* now write them sorted */
591         for (i = 0; i < last_ofs; ) {
592                 int space = 0, skip = 0;
593                 if (vals[i].kind == NORMAL) {
594                         if(vals[i].v.value != NULL) {
595                                 dump_atomic_init(env, obst, vals[i].v.value);
596                                 skip = get_mode_size_bytes(get_irn_mode(vals[i].v.value)) - 1;
597                         } else {
598                                 space = 1;
599                         }
600                 } else {
601                         assert(vals[i].kind == BITFIELD);
602                         obstack_printf(obst, "\t.byte\t%d\n", vals[i].v.bf_val);
603                 }
604
605                 ++i;
606                 while (i < last_ofs && vals[i].kind == NORMAL && vals[i].v.value == NULL) {
607                         ++space;
608                         ++i;
609                 }
610                 space -= skip;
611                 assert(space >= 0);
612
613                 /* a gap */
614                 if (space > 0)
615                         obstack_printf(obst, "\t.skip\t%d\n", space);
616         }
617         xfree(vals);
618 }
619
620 /**
621  * Dump a global entity.
622  *
623  * @param env           the gas output environment
624  * @param ent           the entity to be dumped
625  * @param emit_commons  if non-zero, emit commons (non-local uninitialized entities)
626  */
627 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent, int emit_commons)
628 {
629         obstack_t *obst;
630         ir_type *type = get_entity_type(ent);
631         const char *ld_name = get_entity_ld_name(ent);
632         int align = get_type_alignment_bytes(type);
633         int emit_as_common = 0;
634         ir_variability variability;
635         ir_visibility visibility;
636
637         obst = env->data_obst;
638         if (is_Method_type(type)) {
639                 if (get_method_img_section(ent) == section_constructors) {
640                         obst = env->ctor_obst;
641                         obstack_printf(obst, ".balign\t%d\n", align);
642                         dump_size_type(obst, align);
643                         obstack_printf(obst, "%s\n", ld_name);
644                 }
645                 return;
646         }
647
648         variability = get_entity_variability(ent);
649         visibility = get_entity_visibility(ent);
650         if (variability == variability_constant) {
651                 /* a constant entity, put it on the rdata */
652                 obst = env->rodata_obst;
653         } else if (variability == variability_uninitialized) {
654                 /* uninitialized entity put it in bss segment */
655                 obst = env->bss_obst;
656                 if (emit_commons && visibility != visibility_local)
657                         emit_as_common = 1;
658         }
659
660         be_dbg_variable(env->main_env->db_handle, obst, ent);
661
662         /* global or not global */
663         if (visibility == visibility_external_visible && !emit_as_common) {
664                 obstack_printf(obst, ".global\t%s\n", ld_name);
665         } else if(visibility == visibility_external_allocated) {
666                 obstack_printf(obst, ".global\t%s\n", ld_name);
667                 /* we can return now... */
668                 return;
669         }
670         /* alignment */
671         if (align > 1 && !emit_as_common) {
672                 obstack_printf(obst, ".balign\t%d\n", align);
673         }
674
675         if (!emit_as_common) {
676                 obstack_printf(obst, "%s:\n", ld_name);
677         }
678
679         if (variability == variability_uninitialized) {
680                 if(emit_as_common) {
681                         if (be_gas_flavour == GAS_FLAVOUR_NORMAL)
682                                 obstack_printf(obst, "\t.comm %s,%d,%d\n",
683                                         ld_name, get_type_size_bytes(type), align);
684                         else
685                                 obstack_printf(obst, "\t.comm %s,%d # %d\n",
686                                         ld_name, get_type_size_bytes(type), align);
687                 } else {
688                         obstack_printf(obst, "\t.zero %d\n", get_type_size_bytes(type));
689                 }
690         } else {
691                 if (is_atomic_entity(ent)) {
692                         dump_atomic_init(env, obst, get_atomic_ent_value(ent));
693                 } else {
694                         /* sort_compound_ent_values(ent); */
695
696                         switch (get_type_tpop_code(get_entity_type(ent))) {
697                         case tpo_array:
698                                 if (ent_is_string_const(ent))
699                                         dump_string_cst(obst, ent);
700                                 else
701                                         dump_compound_init(env, obst, ent);
702                                 break;
703                         case tpo_struct:
704                         case tpo_class:
705                         case tpo_union:
706                                 dump_compound_init(env, obst, ent);
707                                 break;
708                         default:
709                                 assert(0);
710                         }
711                 }
712         }
713 }
714
715 /**
716  * Dumps declarations of global variables and the initialization code.
717  *
718  * @param gt                a global like type, either the global or the TLS one
719  * @param env               an environment
720  * @param emit_commons      if non-zero, emit commons (non-local uninitialized entities)
721  * @param only_emit_marked  if non-zero, external allocated entities that do not have
722  *                          its visited flag set are ignored
723  */
724 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
725                               int emit_commons, int only_emit_marked)
726 {
727         int i, n = get_compound_n_members(gt);
728         waitq *worklist = new_waitq();
729
730         if (only_emit_marked) {
731                 for (i = 0; i < n; i++) {
732                         ir_entity *ent = get_compound_member(gt, i);
733                         if (is_entity_backend_marked(ent) ||
734                             get_entity_visibility(ent) != visibility_external_allocated) {
735                                 waitq_put(worklist, ent);
736                                 set_entity_backend_marked(ent, 1);
737                         }
738                 }
739         } else {
740                 for (i = 0; i < n; i++) {
741                         ir_entity *ent = get_compound_member(gt, i);
742                         set_entity_backend_marked(ent, 1);
743                         waitq_put(worklist, ent);
744                 }
745         }
746
747         env->worklist = worklist;
748
749         while (!waitq_empty(worklist)) {
750                 ir_entity *ent = waitq_get(worklist);
751
752                 dump_global(env, ent, emit_commons);
753         }
754
755         del_waitq(worklist);
756         env->worklist = NULL;
757 }
758
759 /************************************************************************/
760
761 /* Generate all entities. */
762 void be_gas_emit_decls(be_emit_env_t *emit, const be_main_env_t *main_env,
763                        int only_emit_marked_entities)
764 {
765         be_gas_decl_env_t env;
766         obstack_t rodata, data, bss, ctor;
767         int    size;
768         char   *cp;
769
770         /* dump the global type */
771         obstack_init(&rodata);
772         obstack_init(&data);
773         obstack_init(&bss);
774         obstack_init(&ctor);
775
776         env.rodata_obst = &rodata;
777         env.data_obst   = &data;
778         env.bss_obst    = &bss;
779         env.ctor_obst   = &ctor;
780         env.main_env    = main_env;
781
782         be_gas_dump_globals(get_glob_type(), &env, 1, only_emit_marked_entities);
783
784         size = obstack_object_size(&data);
785         cp   = obstack_finish(&data);
786         if (size > 0) {
787                 be_gas_emit_switch_section(emit, GAS_SECTION_DATA);
788                 be_emit_string_len(emit, cp, size);
789                 be_emit_write_line(emit);
790         }
791
792         size = obstack_object_size(&rodata);
793         cp   = obstack_finish(&rodata);
794         if (size > 0) {
795                 be_gas_emit_switch_section(emit, GAS_SECTION_RODATA);
796                 be_emit_string_len(emit, cp, size);
797                 be_emit_write_line(emit);
798         }
799
800         size = obstack_object_size(&bss);
801         cp   = obstack_finish(&bss);
802         if (size > 0) {
803                 be_gas_emit_switch_section(emit, GAS_SECTION_COMMON);
804                 be_emit_string_len(emit, cp, size);
805                 be_emit_write_line(emit);
806         }
807
808         size = obstack_object_size(&ctor);
809         cp   = obstack_finish(&ctor);
810         if (size > 0) {
811                 be_gas_emit_switch_section(emit, GAS_SECTION_CTOR);
812                 be_emit_string_len(emit, cp, size);
813                 be_emit_write_line(emit);
814         }
815
816         obstack_free(&rodata, NULL);
817         obstack_free(&data, NULL);
818         obstack_free(&bss, NULL);
819         obstack_free(&ctor, NULL);
820
821         /* dump the Thread Local Storage */
822         obstack_init(&data);
823
824         env.rodata_obst = &data;
825         env.data_obst   = &data;
826         env.bss_obst    = &data;
827         env.ctor_obst   = NULL;
828
829         be_gas_dump_globals(get_tls_type(), &env, 0, only_emit_marked_entities);
830
831         size = obstack_object_size(&data);
832         cp   = obstack_finish(&data);
833         if (size > 0) {
834                 be_gas_emit_switch_section(emit, GAS_SECTION_TLS);
835                 be_emit_cstring(emit, ".balign\t32\n");
836                 be_emit_write_line(emit);
837                 be_emit_string_len(emit, cp, size);
838                 be_emit_write_line(emit);
839         }
840
841         obstack_free(&data, NULL);
842 }