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