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