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