fixed warnings
[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 /**
172  * Return the tarval of an atomic initializer.
173  *
174  * @param init  a node representing the initializer (on teh const code irg)
175  *
176  * @return the tarval
177  */
178 static tarval *get_atomic_init_tv(ir_node *init)
179 {
180         for (;;) {
181                 ir_mode *mode = get_irn_mode(init);
182
183                 switch (get_irn_opcode(init)) {
184
185                 case iro_Cast:
186                         init = get_Cast_op(init);
187                         continue;
188
189                 case iro_Conv:
190                         init = get_Conv_op(init);
191                         continue;
192
193                 case iro_Const:
194                         return get_Const_tarval(init);
195
196                 case iro_SymConst:
197                         switch (get_SymConst_kind(init)) {
198                         case symconst_type_size:
199                                 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
200
201                         case symconst_type_align:
202                                 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
203
204                         case symconst_ofs_ent:
205                                 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
206
207                         case symconst_enum_const:
208                                 return get_enumeration_value(get_SymConst_enum(init));
209
210                         default:
211                                 return NULL;
212                         }
213
214                 default:
215                         return NULL;
216                 }
217         }
218 }
219
220 /**
221  * Dump an atomic value.
222  *
223  * @param env   the gas output environment
224  * @param obst  an obstack the output is written to
225  * @param init  a node representing the atomic value (on the const code irg)
226  */
227 static void do_dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
228                                 ir_node *init)
229 {
230         ir_mode *mode = get_irn_mode(init);
231         int bytes     = get_mode_size_bytes(mode);
232         tarval *tv;
233         ir_entity *ent;
234
235         switch (get_irn_opcode(init)) {
236
237         case iro_Cast:
238                 do_dump_atomic_init(env, obst, get_Cast_op(init));
239                 return;
240
241         case iro_Conv:
242                 do_dump_atomic_init(env, obst, get_Conv_op(init));
243                 return;
244
245         case iro_Const:
246                 tv = get_Const_tarval(init);
247
248                 /* it's a arithmetic value */
249                 dump_arith_tarval(obst, tv, bytes);
250                 return;
251
252         case iro_SymConst:
253                 switch (get_SymConst_kind(init)) {
254                 case symconst_addr_name:
255                         obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
256                         break;
257
258                 case symconst_addr_ent:
259                         ent = get_SymConst_entity(init);
260                         if(!entity_visited(ent)) {
261                                 waitq_put(env->worklist, ent);
262                                 mark_entity_visited(ent);
263                         }
264                         obstack_printf(obst, "%s", get_entity_ld_name(ent));
265                         break;
266
267                 case symconst_ofs_ent:
268                         ent = get_SymConst_entity(init);
269 #if 0       /* not needed, is it? */
270                         if(!entity_visited(ent)) {
271                                 waitq_put(env->worklist, ent);
272                                 mark_entity_visited(ent);
273                         }
274 #endif
275                         obstack_printf(obst, "%d", get_entity_offset(ent));
276                         break;
277
278                 case symconst_type_size:
279                         obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
280                         break;
281
282                 case symconst_type_align:
283                         obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
284                         break;
285
286                 case symconst_enum_const:
287                         tv = get_enumeration_value(get_SymConst_enum(init));
288                         dump_arith_tarval(obst, tv, bytes);
289                         break;
290
291                 default:
292                         assert(!"dump_atomic_init(): don't know how to init from this SymConst");
293                 }
294                 return;
295
296                 case iro_Add:
297                         do_dump_atomic_init(env, obst, get_Add_left(init));
298                         obstack_printf(obst, " + ");
299                         do_dump_atomic_init(env, obst, get_Add_right(init));
300                         return;
301
302                 case iro_Sub:
303                         do_dump_atomic_init(env, obst, get_Sub_left(init));
304                         obstack_printf(obst, " - ");
305                         do_dump_atomic_init(env, obst, get_Sub_right(init));
306                         return;
307
308                 case iro_Mul:
309                         do_dump_atomic_init(env, obst, get_Mul_left(init));
310                         obstack_printf(obst, " * ");
311                         do_dump_atomic_init(env, obst, get_Mul_right(init));
312                         return;
313
314                 default:
315                         assert(0 && "dump_atomic_init(): unknown IR-node");
316         }
317 }
318
319 /**
320  * Dumps the type for given size (.byte, .long, ...)
321  *
322  * @param obst  an obstack the output is written to
323  * @param size  the size in bytes
324  */
325 static void dump_size_type(obstack_t *obst, int size) {
326         switch (size) {
327
328         case 1:
329                 obstack_printf(obst, "\t.byte\t");
330                 break;
331
332         case 2:
333                 obstack_printf(obst, "\t.value\t");
334                 break;
335
336         case 4:
337                 obstack_printf(obst, "\t.long\t");
338                 break;
339
340         case 8:
341                 obstack_printf(obst, "\t.quad\t");
342                 break;
343
344         case 10:
345         case 12:
346                 /* handled in arith */
347                 break;
348
349         case 16:
350                 obstack_printf(obst, "\t.octa\t");
351                 break;
352
353         default:
354                 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
355                 assert(0);
356         }
357 }
358
359 /**
360  * Dump an atomic value to an obstack.
361  *
362  * @param env   the gas output environment
363  * @param obst  an obstack the output is written to
364  * @param init  a node representing the atomic value (on the const code irg)
365  */
366 static void dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
367                              ir_node *init)
368 {
369         ir_mode *mode = get_irn_mode(init);
370         int bytes     = get_mode_size_bytes(mode);
371
372         dump_size_type(obst, bytes);
373         do_dump_atomic_init(env, obst, init);
374         obstack_printf(obst, "\n");
375 }
376
377 /************************************************************************/
378 /* Routines to dump global variables                                    */
379 /************************************************************************/
380
381 /**
382  * Determine if an entity is a string constant
383  * @param ent The entity
384  * @return 1 if it is a string constant, 0 otherwise
385  */
386 static int ent_is_string_const(ir_entity *ent)
387 {
388         ir_type *type, *element_type;
389         ir_mode *mode;
390         int i, c, n;
391
392         type = get_entity_type(ent);
393
394         /* if it's an array */
395         if (!is_Array_type(type))
396                 return 0;
397
398         element_type = get_array_element_type(type);
399
400         /* and the array's element type is primitive */
401         if (!is_Primitive_type(element_type))
402                 return 0;
403
404         /* and the mode of the element type is an int of
405          * the same size as the byte mode */
406         mode = get_type_mode(element_type);
407         if (!mode_is_int(mode)
408                 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
409                 return 0;
410
411         /* if it contains only printable chars and a 0 at the end */
412         n = get_compound_ent_n_values(ent);
413         for (i = 0; i < n; ++i) {
414                 ir_node *irn = get_compound_ent_value(ent, i);
415                 if(get_irn_opcode(irn) != iro_Const)
416                         return 0;
417
418                 c = (int) get_tarval_long(get_Const_tarval(irn));
419
420                 if((i < n - 1 && !(isgraph(c) || isspace(c)))
421                                 || (i == n - 1 && c != '\0'))
422                         return 0;
423         }
424
425         /* then we can emit it as a string constant */
426         return 1;
427 }
428
429 /**
430  * Dump a string constant.
431  * No checks are made!!
432  *
433  * @param obst The obst to dump on.
434  * @param ent  The entity to dump.
435  */
436 static void dump_string_cst(obstack_t *obst, ir_entity *ent)
437 {
438         int      i, n;
439         ir_type *type;
440         int      type_size;
441         int      remaining_space;
442
443         obstack_printf(obst, "\t.string \"");
444         n = get_compound_ent_n_values(ent);
445
446         for (i = 0; i < n-1; ++i) {
447                 ir_node *irn;
448                 int c;
449
450                 irn = get_compound_ent_value(ent, i);
451                 c = (int) get_tarval_long(get_Const_tarval(irn));
452
453                 switch (c) {
454                 case '"' : obstack_printf(obst, "\\\""); break;
455                 case '\n': obstack_printf(obst, "\\n"); break;
456                 case '\r': obstack_printf(obst, "\\r"); break;
457                 case '\t': obstack_printf(obst, "\\t"); break;
458                 case '\\': obstack_printf(obst, "\\\\"); break;
459                 default  :
460                         if (isprint(c))
461                                 obstack_printf(obst, "%c", c);
462                         else
463                                 obstack_printf(obst, "\\%o", c);
464                         break;
465                 }
466         }
467         obstack_printf(obst, "\"\n");
468
469         type            = get_entity_type(ent);
470         type_size       = get_type_size_bytes(type);
471         remaining_space = type_size - n;
472         assert(remaining_space >= 0);
473         if(remaining_space > 0) {
474                 obstack_printf(obst, "\t.skip\t%d\n", remaining_space);
475         }
476 }
477
478 static void dump_array_init(be_gas_decl_env_t *env, obstack_t *obst,
479                             ir_entity *ent)
480 {
481         const ir_type *ty = get_entity_type(ent);
482         int i;
483         int filler;
484         int size = 0;
485
486         /* potential spare values should be already included! */
487         for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
488                 ir_entity *step = get_compound_ent_value_member(ent, i);
489                 ir_type *stype = get_entity_type(step);
490
491                 if (get_type_mode(stype)) {
492                         int align = (get_type_alignment_bits(stype) + 7) >> 3;
493                         int n     = size % align;
494
495                         if (n > 0) {
496                                 obstack_printf(obst, "\t.zero\t%d\n", align - n);
497                                 size += align - n;
498                         }
499                 }
500                 dump_atomic_init(env, obst, get_compound_ent_value(ent, i));
501                 size += get_type_size_bytes(stype);
502         }
503         filler = get_type_size_bytes(ty) - size;
504
505         if (filler > 0)
506                 obstack_printf(obst, "\t.skip\t%d\n", filler);
507 }
508
509 enum normal_or_bitfield_kind {
510         NORMAL = 0,
511         BITFIELD
512 };
513
514 typedef struct {
515         enum normal_or_bitfield_kind kind;
516         union {
517                 ir_node *value;
518                 unsigned char bf_val;
519         } v;
520 } normal_or_bitfield;
521
522 /**
523  * Dump an initializer for a compound entity.
524  */
525 static void dump_compound_init(be_gas_decl_env_t *env, obstack_t *obst,
526                                ir_entity *ent)
527 {
528         normal_or_bitfield *vals;
529         int i, j, n = get_compound_ent_n_values(ent);
530         int last_ofs;
531
532         /* Find the initializer size. Sorrily gcc support a nasty feature:
533            The last field of a compound may be a flexible array. This allows
534            initializers bigger than the type size. */
535         last_ofs = get_type_size_bytes(get_entity_type(ent));
536         for (i = 0; i < n; ++i) {
537                 int offset = get_compound_ent_value_offset_bytes(ent, i);
538                 int bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
539                 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
540                 int path_len = get_compound_graph_path_length(path);
541                 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
542                 int value_len = get_type_size_bits(get_entity_type(last_ent));
543
544                 offset += (value_len + bits_remainder + 7) >> 3;
545
546                 if (offset > last_ofs) {
547                         last_ofs = offset;
548                 }
549         }
550
551         /*
552          * In the worst case, every initializer allocates one byte.
553          * Moreover, initializer might be big, do not allocate on stack.
554          */
555         vals = xcalloc(last_ofs, sizeof(vals[0]));
556
557         /* collect the values and store them at the offsets */
558         for (i = 0; i < n; ++i) {
559                 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
560                 int path_len = get_compound_graph_path_length(path);
561                 int offset = get_compound_ent_value_offset_bytes(ent, i);
562                 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
563                 ir_node *value = get_compound_ent_value(ent, i);
564                 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
565                 int value_len = get_type_size_bits(get_entity_type(last_ent));
566                 assert(offset >= 0);
567                 assert(offset_bits >= 0);
568
569                 if (offset_bits != 0 ||
570                         (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
571                         tarval *shift, *shifted;
572                         tarval *tv = get_atomic_init_tv(value);
573                         if (tv == NULL) {
574                                 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
575                                       get_entity_ld_name(ent));
576                         }
577                         tv = tarval_convert_to(tv, mode_Lu);
578                         shift = new_tarval_from_long(offset_bits, mode_Is);
579                         shifted = tarval_shl(tv, shift);
580                         if (shifted == tarval_bad || shifted == tarval_undefined) {
581                                 panic("Couldn't shift numeric value for bitfield initializer '%s'\n",
582                                       get_entity_ld_name(ent));
583                         }
584
585                         for (j = 0; value_len > 0; ++j) {
586                                 assert(offset + j < last_ofs);
587                                 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
588                                 vals[offset + j].kind = BITFIELD;
589                                 vals[offset + j].v.bf_val |= get_tarval_sub_bits(shifted, j);
590                                 value_len -= 8 - offset_bits;
591                                 offset_bits = 0;
592                         }
593                 } else {
594                         assert(offset < last_ofs);
595                         assert(vals[offset].kind == NORMAL);
596                         assert(vals[offset].v.value == NULL);
597                         vals[offset].v.value = value;
598                 }
599         }
600
601         /* now write them sorted */
602         for (i = 0; i < last_ofs; ) {
603                 int space = 0, skip = 0;
604                 if (vals[i].kind == NORMAL) {
605                         if(vals[i].v.value != NULL) {
606                                 dump_atomic_init(env, obst, vals[i].v.value);
607                                 skip = get_mode_size_bytes(get_irn_mode(vals[i].v.value)) - 1;
608                         } else {
609                                 space = 1;
610                         }
611                 } else {
612                         assert(vals[i].kind == BITFIELD);
613                         obstack_printf(obst, "\t.byte\t%d\n", vals[i].v.bf_val);
614                 }
615
616                 ++i;
617                 space = 0;
618                 while (i < last_ofs && vals[i].kind == NORMAL && vals[i].v.value == NULL) {
619                         ++space;
620                         ++i;
621                 }
622                 space -= skip;
623                 assert(space >= 0);
624
625                 /* a gap */
626                 if (space > 0)
627                         obstack_printf(obst, "\t.skip\t%d\n", space);
628         }
629         xfree(vals);
630 }
631
632 /**
633  * Dump a global entity.
634  *
635  * @param env           the gas output environment
636  * @param ent           the entity to be dumped
637  * @param emit_commons  if non-zero, emit commons (non-local uninitialized entities)
638  */
639 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent, int emit_commons)
640 {
641         obstack_t *obst;
642         ir_type *type = get_entity_type(ent);
643         const char *ld_name = get_entity_ld_name(ent);
644         ir_variability variability = get_entity_variability(ent);
645         ir_visibility visibility = get_entity_visibility(ent);
646         int align = get_type_alignment_bytes(type);
647         int emit_as_common = 0;
648
649         obst = env->data_obst;
650         if (is_Method_type(type)) {
651                 if (get_method_img_section(ent) == section_constructors) {
652                         obst = env->ctor_obst;
653                         obstack_printf(obst, ".balign\t%d\n", align);
654                         dump_size_type(obst, align);
655                         obstack_printf(obst, "%s\n", ld_name);
656                 }
657                 return;
658         } else if (variability == variability_constant) {
659                 /* a constant entity, put it on the rdata */
660                 obst = env->rodata_obst;
661         } else if (variability == variability_uninitialized) {
662                 /* uninitialized entity put it in bss segment */
663                 obst = env->bss_obst;
664                 if(emit_commons && visibility != visibility_local)
665                         emit_as_common = 1;
666         }
667
668         be_dbg_variable(env->main_env->db_handle, obst, ent);
669
670         /* global or not global */
671         if (visibility == visibility_external_visible && !emit_as_common) {
672                 obstack_printf(obst, ".global\t%s\n", ld_name);
673         } else if(visibility == visibility_external_allocated) {
674                 obstack_printf(obst, ".global\t%s\n", ld_name);
675                 /* we can return now... */
676                 return;
677         }
678         /* alignment */
679         if (align > 1 && !emit_as_common) {
680                 obstack_printf(obst, ".balign\t%d\n", align);
681         }
682
683         if (!emit_as_common) {
684                 obstack_printf(obst, "%s:\n", ld_name);
685         }
686
687         if (variability == variability_uninitialized) {
688                 if(emit_as_common) {
689                         if (be_gas_flavour == GAS_FLAVOUR_NORMAL)
690                                 obstack_printf(obst, "\t.comm %s,%d,%d\n",
691                                         ld_name, get_type_size_bytes(type), align);
692                         else
693                                 obstack_printf(obst, "\t.comm %s,%d # %d\n",
694                                         ld_name, get_type_size_bytes(type), align);
695                 } else {
696                         obstack_printf(obst, "\t.zero %d\n", get_type_size_bytes(type));
697                 }
698         } else if (is_atomic_type(type)) {
699                 dump_atomic_init(env, obst, get_atomic_ent_value(ent));
700         } else if (ent_is_string_const(ent)) {
701                 dump_string_cst(obst, ent);
702         } else if (is_Array_type(type)) {
703                 dump_array_init(env, obst, ent);
704         } else if (is_compound_type(type)) {
705                 dump_compound_init(env, obst, ent);
706         } else {
707                 assert(0 && "unsupported type");
708         }
709 }
710
711 /**
712  * Dumps declarations of global variables and the initialization code.
713  *
714  * @param gt                a global like type, either the global or the TLS one
715  * @param env               an environment
716  * @param emit_commons      if non-zero, emit commons (non-local uninitialized entities)
717  * @param only_emit_marked  if non-zero, external allocated entities that do not have
718  *                          its visited flag set are ignored
719  */
720 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
721                               int emit_commons, int only_emit_marked)
722 {
723         int i, n = get_compound_n_members(gt);
724         waitq *worklist = new_waitq();
725
726         if(only_emit_marked) {
727                 for (i = 0; i < n; i++) {
728                         ir_entity *ent = get_compound_member(gt, i);
729                         if(entity_visited(ent) ||
730                                         get_entity_visibility(ent) != visibility_external_allocated) {
731                                 waitq_put(worklist, ent);
732                                 mark_entity_visited(ent);
733                         }
734                 }
735         } else {
736                 inc_master_type_visited();
737                 for (i = 0; i < n; i++) {
738                         ir_entity *ent = get_compound_member(gt, i);
739                         mark_entity_visited(ent);
740                         waitq_put(worklist, ent);
741                 }
742         }
743
744         env->worklist = worklist;
745
746         while(!waitq_empty(worklist)) {
747                 ir_entity *ent = waitq_get(worklist);
748
749                 dump_global(env, ent, emit_commons);
750         }
751
752         del_waitq(worklist);
753         env->worklist = NULL;
754 }
755
756 /************************************************************************/
757
758 /* Generate all entities. */
759 void be_gas_emit_decls(be_emit_env_t *emit, const be_main_env_t *main_env,
760                        int only_emit_marked_entities)
761 {
762         be_gas_decl_env_t env;
763         obstack_t rodata, data, bss, ctor;
764         int    size;
765         char   *cp;
766
767         /* dump the global type */
768         obstack_init(&rodata);
769         obstack_init(&data);
770         obstack_init(&bss);
771         obstack_init(&ctor);
772
773         env.rodata_obst = &rodata;
774         env.data_obst   = &data;
775         env.bss_obst    = &bss;
776         env.ctor_obst   = &ctor;
777         env.main_env    = main_env;
778
779         be_gas_dump_globals(get_glob_type(), &env, 1, only_emit_marked_entities);
780
781         size = obstack_object_size(&data);
782         cp   = obstack_finish(&data);
783         if (size > 0) {
784                 be_gas_emit_switch_section(emit, GAS_SECTION_DATA);
785                 be_emit_string_len(emit, cp, size);
786                 be_emit_write_line(emit);
787         }
788
789         size = obstack_object_size(&rodata);
790         cp   = obstack_finish(&rodata);
791         if (size > 0) {
792                 be_gas_emit_switch_section(emit, GAS_SECTION_RODATA);
793                 be_emit_string_len(emit, cp, size);
794                 be_emit_write_line(emit);
795         }
796
797         size = obstack_object_size(&bss);
798         cp   = obstack_finish(&bss);
799         if (size > 0) {
800                 be_gas_emit_switch_section(emit, GAS_SECTION_COMMON);
801                 be_emit_string_len(emit, cp, size);
802                 be_emit_write_line(emit);
803         }
804
805         size = obstack_object_size(&ctor);
806         cp   = obstack_finish(&ctor);
807         if (size > 0) {
808                 be_gas_emit_switch_section(emit, GAS_SECTION_CTOR);
809                 be_emit_string_len(emit, cp, size);
810                 be_emit_write_line(emit);
811         }
812
813         obstack_free(&rodata, NULL);
814         obstack_free(&data, NULL);
815         obstack_free(&bss, NULL);
816         obstack_free(&ctor, NULL);
817
818         /* dump the Thread Local Storage */
819         obstack_init(&data);
820
821         env.rodata_obst = &data;
822         env.data_obst   = &data;
823         env.bss_obst    = &data;
824         env.ctor_obst   = NULL;
825
826         be_gas_dump_globals(get_tls_type(), &env, 0, only_emit_marked_entities);
827
828         size = obstack_object_size(&data);
829         cp   = obstack_finish(&data);
830         if (size > 0) {
831                 be_gas_emit_switch_section(emit, GAS_SECTION_TLS);
832                 be_emit_cstring(emit, ".balign\t32\n");
833                 be_emit_write_line(emit);
834                 be_emit_string_len(emit, cp, size);
835                 be_emit_write_line(emit);
836         }
837
838         obstack_free(&data, NULL);
839 }