copy_entity_own(): assert if the new type has already a fixed layout
[libfirm] / ir / be / begnuas.c
1 /*
2  * Copyright (C) 1995-2008 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 "entity_t.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                         ".section\t.text",
83                                 ".section\t.data",
84                                 ".section\t.rodata",
85                                 ".section\t.bss",
86                                 ".section\t.tbss,\"awT\",@nobits",
87                                 ".section\t.ctors,\"aw\",@progbits"
88                 }
89         };
90
91         assert((int) be_gas_flavour >= 0 && be_gas_flavour < GAS_FLAVOUR_MAX);
92         assert((int) section >= 0 && section < GAS_SECTION_MAX);
93         return text[be_gas_flavour][section];
94 }
95
96 /**
97  * Emit necessary code to switch to a section.
98  *
99  * @param env      the emitter environment
100  * @param section  the section to switch to
101  */
102 void be_gas_emit_switch_section(be_gas_section_t section) {
103         be_emit_char('\t');
104         be_emit_string(get_section_name(section));
105         be_emit_char('\n');
106         be_emit_write_line();
107 }
108
109 /**
110  * An environment containing all needed dumper data.
111  * Currently we create the file completely in memory first, then
112  * write it to the disk. This is an artifact from the old C-generating backend
113  * and even there NOT needed. So we might change it in the future.
114  */
115 typedef struct _be_gas_decl_env {
116         obstack_t *rodata_obst;        /**< An obstack that will be filled with all rodata entities. */
117         obstack_t *data_obst;          /**< An obstack that will be filled with the initialized entities. */
118         obstack_t *bss_obst;           /**< An obstack that will be filled with the uninitialized entities. */
119         obstack_t *ctor_obst;          /**< An obstack that will be filled with the constructor entities. */
120         const be_main_env_t *main_env; /**< The main backend environment, used for it's debug handle. */
121         waitq     *worklist;           /**< A worklist we use to place not yet handled entities on. */
122 } be_gas_decl_env_t;
123
124 /************************************************************************/
125
126 /**
127  * Output a tarval.
128  *
129  * @param obst   the obstack where the data is written too
130  * @param tv     the tarval
131  * @param bytes  the width of the tarvals value in bytes
132  */
133 static void dump_arith_tarval(obstack_t *obst, tarval *tv, int bytes)
134 {
135         switch (bytes) {
136
137         case 1:
138                 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
139                 break;
140
141         case 2:
142                 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
143                 break;
144
145         case 4:
146                 obstack_printf(obst, "0x%02x%02x%02x%02x",
147                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
148                 break;
149
150         case 8:
151                 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
152                         get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
153                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
154                 break;
155
156         case 10:
157         case 12:
158                 break;
159
160         case 16:
161                 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x"
162                                                "%02x%02x%02x%02x%02x%02x%02x%02x",
163                         get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
164                         get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
165                         get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
166                         get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
167                         get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
168                         get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
169                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
170                         get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
171                 break;
172
173
174         default:
175                 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
176                 assert(0);
177         }
178 }
179
180 /**
181  * Return the label prefix for labeled blocks.
182  */
183 const char *be_gas_label_prefix(void) {
184         return ".LG";
185 }
186
187 /**
188  * Dump a label.
189  */
190 static void dump_label(obstack_t *obst, ir_label_t label) {
191         obstack_printf(obst, "%s%ld", be_gas_label_prefix(), label);
192 }
193
194 /**
195  * Return the tarval of an atomic initializer.
196  *
197  * @param init  a node representing the initializer (on the const code irg)
198  *
199  * @return the tarval
200  */
201 static tarval *get_atomic_init_tv(ir_node *init)
202 {
203         for (;;) {
204                 ir_mode *mode = get_irn_mode(init);
205
206                 switch (get_irn_opcode(init)) {
207
208                 case iro_Cast:
209                         init = get_Cast_op(init);
210                         continue;
211
212                 case iro_Conv:
213                         init = get_Conv_op(init);
214                         continue;
215
216                 case iro_Const:
217                         return get_Const_tarval(init);
218
219                 case iro_SymConst:
220                         switch (get_SymConst_kind(init)) {
221                         case symconst_type_size:
222                                 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
223
224                         case symconst_type_align:
225                                 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
226
227                         case symconst_ofs_ent:
228                                 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
229
230                         case symconst_enum_const:
231                                 return get_enumeration_value(get_SymConst_enum(init));
232
233                         case symconst_label:
234                                 return NULL;
235
236                         default:
237                                 return NULL;
238                         }
239
240                 default:
241                         return NULL;
242                 }
243         }
244 }
245
246 /**
247  * Dump an atomic value.
248  *
249  * @param env   the gas output environment
250  * @param obst  an obstack the output is written to
251  * @param init  a node representing the atomic value (on the const code irg)
252  */
253 static void do_dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
254                                 ir_node *init)
255 {
256         ir_mode *mode = get_irn_mode(init);
257         int bytes     = get_mode_size_bytes(mode);
258         tarval *tv;
259         ir_label_t label;
260         ir_entity *ent;
261
262         init = skip_Id(init);
263
264         switch (get_irn_opcode(init)) {
265
266         case iro_Cast:
267                 do_dump_atomic_init(env, obst, get_Cast_op(init));
268                 return;
269
270         case iro_Conv:
271                 do_dump_atomic_init(env, obst, get_Conv_op(init));
272                 return;
273
274         case iro_Const:
275                 tv = get_Const_tarval(init);
276
277                 /* it's a arithmetic value */
278                 dump_arith_tarval(obst, tv, bytes);
279                 return;
280
281         case iro_SymConst:
282                 switch (get_SymConst_kind(init)) {
283                 case symconst_addr_name:
284                         obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
285                         break;
286
287                 case symconst_addr_ent:
288                         ent = get_SymConst_entity(init);
289                         if(!is_entity_backend_marked(ent)) {
290                                 waitq_put(env->worklist, ent);
291                                 set_entity_backend_marked(ent, 1);
292                         }
293                         obstack_printf(obst, "%s", get_entity_ld_name(ent));
294                         break;
295
296                 case symconst_ofs_ent:
297                         ent = get_SymConst_entity(init);
298 #if 0       /* not needed, is it? */
299                         if(!is_entity_backend_marked(ent)) {
300                                 waitq_put(env->worklist, ent);
301                                 set_entity_backend_marked(ent, 1);
302                         }
303 #endif
304                         obstack_printf(obst, "%d", get_entity_offset(ent));
305                         break;
306
307                 case symconst_type_size:
308                         obstack_printf(obst, "%u", get_type_size_bytes(get_SymConst_type(init)));
309                         break;
310
311                 case symconst_type_align:
312                         obstack_printf(obst, "%u", get_type_alignment_bytes(get_SymConst_type(init)));
313                         break;
314
315                 case symconst_enum_const:
316                         tv = get_enumeration_value(get_SymConst_enum(init));
317                         dump_arith_tarval(obst, tv, bytes);
318                         break;
319
320                 case symconst_label:
321                         label = get_SymConst_label(init);
322                         dump_label(obst, label);
323                         break;
324
325                 default:
326                         assert(!"dump_atomic_init(): don't know how to init from this SymConst");
327                 }
328                 return;
329
330                 case iro_Add:
331                         do_dump_atomic_init(env, obst, get_Add_left(init));
332                         obstack_printf(obst, " + ");
333                         do_dump_atomic_init(env, obst, get_Add_right(init));
334                         return;
335
336                 case iro_Sub:
337                         do_dump_atomic_init(env, obst, get_Sub_left(init));
338                         obstack_printf(obst, " - ");
339                         do_dump_atomic_init(env, obst, get_Sub_right(init));
340                         return;
341
342                 case iro_Mul:
343                         do_dump_atomic_init(env, obst, get_Mul_left(init));
344                         obstack_printf(obst, " * ");
345                         do_dump_atomic_init(env, obst, get_Mul_right(init));
346                         return;
347
348                 default:
349                         assert(0 && "dump_atomic_init(): unknown IR-node");
350         }
351 }
352
353 /**
354  * Dumps the type for given size (.byte, .long, ...)
355  *
356  * @param obst  an obstack the output is written to
357  * @param size  the size in bytes
358  */
359 static void dump_size_type(obstack_t *obst, size_t size) {
360         switch (size) {
361
362         case 1:
363                 obstack_printf(obst, "\t.byte\t");
364                 break;
365
366         case 2:
367                 obstack_printf(obst, "\t.value\t");
368                 break;
369
370         case 4:
371                 obstack_printf(obst, "\t.long\t");
372                 break;
373
374         case 8:
375                 obstack_printf(obst, "\t.quad\t");
376                 break;
377
378         case 10:
379         case 12:
380                 /* handled in arith */
381                 break;
382
383         case 16:
384                 obstack_printf(obst, "\t.octa\t");
385                 break;
386
387         default:
388                 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
389                 assert(0);
390         }
391 }
392
393 /**
394  * Dump an atomic value to an obstack.
395  *
396  * @param env   the gas output environment
397  * @param obst  an obstack the output is written to
398  * @param init  a node representing the atomic value (on the const code irg)
399  */
400 static void dump_atomic_init(be_gas_decl_env_t *env, obstack_t *obst,
401                              ir_node *init)
402 {
403         ir_mode *mode = get_irn_mode(init);
404         int bytes     = get_mode_size_bytes(mode);
405
406         dump_size_type(obst, bytes);
407         do_dump_atomic_init(env, obst, init);
408         obstack_printf(obst, "\n");
409 }
410
411 /************************************************************************/
412 /* Routines to dump global variables                                    */
413 /************************************************************************/
414
415 static int initializer_is_string_const(const ir_initializer_t *initializer)
416 {
417         size_t i, len;
418
419         if(initializer->kind != IR_INITIALIZER_COMPOUND)
420                 return 0;
421
422         len = initializer->compound.n_initializers;
423         if (len < 1)
424                 return 0;
425         for(i = 0; i < len; ++i) {
426                 int               c;
427                 tarval           *tv;
428                 ir_mode          *mode;
429                 ir_initializer_t *sub_initializer
430                         = initializer->compound.initializers[i];
431
432                 if(sub_initializer->kind != IR_INITIALIZER_TARVAL)
433                         return 0;
434
435                 tv   = sub_initializer->tarval.value;
436                 mode = get_tarval_mode(tv);
437
438                 if (!mode_is_int(mode)
439                                 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
440                         return 0;
441
442                 c = get_tarval_long(tv);
443                 if (i < len - 1) {
444                         if (!isgraph(c) && !isspace(c))
445                                 return 0;
446                 } else {
447                         if (c != 0)
448                                 return 0;
449                 }
450         }
451
452         return 1;
453 }
454
455 /**
456  * Determine if an entity is a string constant
457  * @param ent The entity
458  * @return 1 if it is a string constant, 0 otherwise
459  */
460 static int ent_is_string_const(ir_entity *ent)
461 {
462         ir_type *type, *element_type;
463         ir_mode *mode;
464         int i, c, n;
465
466         type = get_entity_type(ent);
467
468         /* if it's an array */
469         if (!is_Array_type(type))
470                 return 0;
471
472         element_type = get_array_element_type(type);
473
474         /* and the array's element type is primitive */
475         if (!is_Primitive_type(element_type))
476                 return 0;
477
478         /* and the mode of the element type is an int of
479          * the same size as the byte mode */
480         mode = get_type_mode(element_type);
481         if (!mode_is_int(mode)
482                 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
483                 return 0;
484
485         if(ent->has_initializer) {
486                 /* TODO */
487                 return 0;
488         } else {
489                 /* if it contains only printable chars and a 0 at the end */
490                 n = get_compound_ent_n_values(ent);
491                 for (i = 0; i < n; ++i) {
492                         ir_node *irn = get_compound_ent_value(ent, i);
493                         if (! is_Const(irn))
494                                 return 0;
495
496                         c = (int) get_tarval_long(get_Const_tarval(irn));
497
498                         if((i < n - 1 && !(isgraph(c) || isspace(c)))
499                                         || (i == n - 1 && c != '\0'))
500                                 return 0;
501                 }
502         }
503
504         /* then we can emit it as a string constant */
505         return 1;
506 }
507
508 /**
509  * Dump a string constant.
510  * No checks are made!!
511  *
512  * @param obst The obst to dump on.
513  * @param ent  The entity to dump.
514  */
515 static void dump_string_cst(obstack_t *obst, ir_entity *ent)
516 {
517         int      i, n;
518         ir_type *type;
519         int      type_size;
520         int      remaining_space;
521
522         obstack_printf(obst, "\t.string \"");
523         n = get_compound_ent_n_values(ent);
524
525         for (i = 0; i < n-1; ++i) {
526                 ir_node *irn;
527                 int c;
528
529                 irn = get_compound_ent_value(ent, i);
530                 c = (int) get_tarval_long(get_Const_tarval(irn));
531
532                 switch (c) {
533                 case '"' : obstack_printf(obst, "\\\""); break;
534                 case '\n': obstack_printf(obst, "\\n"); break;
535                 case '\r': obstack_printf(obst, "\\r"); break;
536                 case '\t': obstack_printf(obst, "\\t"); break;
537                 case '\\': obstack_printf(obst, "\\\\"); break;
538                 default  :
539                         if (isprint(c))
540                                 obstack_printf(obst, "%c", c);
541                         else
542                                 obstack_printf(obst, "\\%o", c);
543                         break;
544                 }
545         }
546         obstack_printf(obst, "\"\n");
547
548         type            = get_entity_type(ent);
549         type_size       = get_type_size_bytes(type);
550         remaining_space = type_size - n;
551         assert(remaining_space >= 0);
552         if(remaining_space > 0) {
553                 obstack_printf(obst, "\t.skip\t%d\n", remaining_space);
554         }
555 }
556
557 static void dump_string_initializer(obstack_t *obst,
558                                     const ir_initializer_t *initializer)
559 {
560         size_t i, len;
561
562         obstack_printf(obst, "\t.string \"");
563
564         len = initializer->compound.n_initializers;
565         for(i = 0; i < len - 1; ++i) {
566                 const ir_initializer_t *sub_initializer
567                         = get_initializer_compound_value(initializer, i);
568
569                 tarval *tv = get_initializer_tarval_value(sub_initializer);
570                 int     c  = get_tarval_long(tv);
571
572                 switch (c) {
573                 case '"' : obstack_printf(obst, "\\\""); break;
574                 case '\n': obstack_printf(obst, "\\n"); break;
575                 case '\r': obstack_printf(obst, "\\r"); break;
576                 case '\t': obstack_printf(obst, "\\t"); break;
577                 case '\\': obstack_printf(obst, "\\\\"); break;
578                 default  :
579                         if (isprint(c))
580                                 obstack_printf(obst, "%c", c);
581                         else
582                                 obstack_printf(obst, "\\%o", c);
583                         break;
584                 }
585         }
586         obstack_printf(obst, "\"\n");
587 }
588
589 enum normal_or_bitfield_kind {
590         NORMAL = 0,
591         TARVAL,
592         BITFIELD
593 };
594
595 typedef struct {
596         enum normal_or_bitfield_kind kind;
597         union {
598                 ir_node       *value;
599                 tarval        *tarval;
600                 unsigned char  bf_val;
601         } v;
602 } normal_or_bitfield;
603
604 static int is_type_variable_size(ir_type *type)
605 {
606         (void) type;
607         /* TODO */
608         return 0;
609 }
610
611 static size_t get_initializer_size(const ir_initializer_t *initializer,
612                                    ir_type *type)
613 {
614         switch(get_initializer_kind(initializer)) {
615         case IR_INITIALIZER_TARVAL: {
616                 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
617                 return get_type_size_bytes(type);
618         }
619         case IR_INITIALIZER_CONST:
620         case IR_INITIALIZER_NULL:
621                 return get_type_size_bytes(type);
622         case IR_INITIALIZER_COMPOUND: {
623                 if(!is_type_variable_size(type)) {
624                         return get_type_size_bytes(type);
625                 } else {
626                         unsigned n_entries
627                                 = get_initializer_compound_n_entries(initializer);
628                         unsigned i;
629                         unsigned initializer_size = get_type_size_bytes(type);
630                         for(i = 0; i < n_entries; ++i) {
631                                 ir_entity *entity = get_compound_member(type, i);
632                                 ir_type   *type   = get_entity_type(entity);
633
634                                 const ir_initializer_t *sub_initializer
635                                         = get_initializer_compound_value(initializer, i);
636
637                                 unsigned offset = get_entity_offset(entity);
638                                 unsigned size   = get_initializer_size(sub_initializer, type);
639
640                                 if(offset + size > initializer_size) {
641                                         initializer_size = offset + size;
642                                 }
643                         }
644                         return initializer_size;
645                 }
646         }
647         }
648
649         panic("found invalid initializer");
650 }
651
652 #ifndef NDEBUG
653 static normal_or_bitfield *glob_vals;
654 static size_t              max_vals;
655 #endif
656
657 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
658                           const ir_initializer_t *initializer, ir_type *type)
659 {
660         unsigned char  last_bits = 0;
661         ir_mode       *mode      = get_type_mode(type);
662         tarval        *tv        = NULL;
663         unsigned char  curr_bits;
664         int            value_len;
665         int            j;
666
667         switch(get_initializer_kind(initializer)) {
668         case IR_INITIALIZER_NULL:
669                 return;
670         case IR_INITIALIZER_TARVAL:
671                 tv = get_initializer_tarval_value(initializer);
672                 break;
673         case IR_INITIALIZER_CONST: {
674                 ir_node *node = get_initializer_const_value(initializer);
675                 if(!is_Const(node)) {
676                         panic("bitfield initializer not a Const node");
677                 }
678                 tv = get_Const_tarval(node);
679                 break;
680         }
681         case IR_INITIALIZER_COMPOUND:
682                 panic("bitfield initializer is compound");
683         }
684         if (tv == NULL) {
685                 panic("Couldn't get numeric value for bitfield initializer\n");
686         }
687
688         /* normalize offset */
689         vals        += offset_bits >> 3;
690         offset_bits &= 7;
691         value_len    = get_mode_size_bits(mode);
692
693         /* combine bits with existing bits */
694         for (j = 0; value_len + (int) offset_bits > 0; ++j) {
695                 assert((size_t) (vals - glob_vals) + j < max_vals);
696                 assert(vals[j].kind == BITFIELD ||
697                                 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
698                 vals[j].kind = BITFIELD;
699                 curr_bits    = get_tarval_sub_bits(tv, j);
700                 vals[j].v.bf_val
701                         |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
702                 value_len -= 8;
703                 last_bits = curr_bits;
704         }
705 }
706
707 static void dump_ir_initializer(normal_or_bitfield *vals,
708                                 const ir_initializer_t *initializer,
709                                 ir_type *type)
710 {
711         assert((size_t) (vals - glob_vals) < max_vals);
712
713         switch(get_initializer_kind(initializer)) {
714         case IR_INITIALIZER_NULL:
715                 return;
716         case IR_INITIALIZER_TARVAL: {
717                 size_t i;
718
719                 assert(vals->kind != BITFIELD);
720                 vals->kind     = TARVAL;
721                 vals->v.tarval = get_initializer_tarval_value(initializer);
722                 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
723                 for(i = 1; i < get_type_size_bytes(type); ++i) {
724                         vals[i].kind    = NORMAL;
725                         vals[i].v.value = NULL;
726                 }
727                 return;
728         }
729         case IR_INITIALIZER_CONST: {
730                 size_t i;
731
732                 assert(vals->kind != BITFIELD);
733                 vals->kind    = NORMAL;
734                 vals->v.value = get_initializer_const_value(initializer);
735                 for(i = 1; i < get_type_size_bytes(type); ++i) {
736                         vals[i].kind    = NORMAL;
737                         vals[i].v.value = NULL;
738                 }
739                 return;
740         }
741         case IR_INITIALIZER_COMPOUND: {
742                 size_t i = 0;
743                 size_t n = get_initializer_compound_n_entries(initializer);
744
745                 if(is_Array_type(type)) {
746                         ir_type *element_type = get_array_element_type(type);
747                         size_t   skip         = get_type_size_bytes(element_type);
748                         size_t   alignment    = get_type_alignment_bytes(element_type);
749                         size_t   misalign     = skip % alignment;
750                         if(misalign != 0) {
751                                 skip += alignment - misalign;
752                         }
753
754                         for(i = 0; i < n; ++i) {
755                                 ir_initializer_t *sub_initializer
756                                         = get_initializer_compound_value(initializer, i);
757
758                                 dump_ir_initializer(vals, sub_initializer, element_type);
759
760                                 vals += skip;
761                         }
762                 } else {
763                         size_t n_members, i;
764                         assert(is_compound_type(type));
765                         n_members = get_compound_n_members(type);
766                         for(i = 0; i < n_members; ++i) {
767                                 ir_entity        *member    = get_compound_member(type, i);
768                                 size_t            offset    = get_entity_offset(member);
769                                 ir_type          *subtype   = get_entity_type(member);
770                                 ir_mode          *mode      = get_type_mode(subtype);
771                                 ir_initializer_t *sub_initializer;
772
773                                 assert(i < get_initializer_compound_n_entries(initializer));
774                                 sub_initializer
775                                         = get_initializer_compound_value(initializer, i);
776
777                                 if(mode != NULL) {
778                                         size_t offset_bits
779                                                 = get_entity_offset_bits_remainder(member);
780                                         size_t value_len   = get_mode_size_bits(mode);
781
782                                         if(offset_bits != 0 ||
783                                                 (value_len != 8 && value_len != 16 && value_len != 32
784                                                  && value_len != 64)) {
785                                                 dump_bitfield(&vals[offset], offset_bits,
786                                                               sub_initializer, subtype);
787                                                 continue;
788                                         }
789                                 }
790
791                                 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
792                         }
793                 }
794
795                 return;
796         }
797         }
798         panic("invalid ir_initializer kind found");
799 }
800
801 static void dump_initializer(be_gas_decl_env_t *env, obstack_t *obst,
802                              ir_entity *entity)
803 {
804         const ir_initializer_t *initializer = entity->attr.initializer;
805         ir_type                *type;
806         normal_or_bitfield     *vals;
807         size_t                  size;
808         size_t                  k;
809
810         if(initializer_is_string_const(initializer)) {
811                 dump_string_initializer(obst, initializer);
812                 return;
813         }
814
815         type = get_entity_type(entity);
816         size = get_initializer_size(initializer, type);
817
818         if (size == 0)
819                 return;
820
821         /*
822          * In the worst case, every initializer allocates one byte.
823          * Moreover, initializer might be big, do not allocate on stack.
824          */
825         vals = xcalloc(size, sizeof(vals[0]));
826
827 #ifndef NDEBUG
828         glob_vals = vals;
829         max_vals  = size;
830 #endif
831
832         dump_ir_initializer(vals, initializer, type);
833
834         /* now write values sorted */
835         for (k = 0; k < size; ) {
836                 int space = 0, skip = 0;
837                 if (vals[k].kind == NORMAL) {
838                         if(vals[k].v.value != NULL) {
839                                 dump_atomic_init(env, obst, vals[k].v.value);
840                                 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
841                         } else {
842                                 space = 1;
843                         }
844                 } else if(vals[k].kind == TARVAL) {
845                         tarval *tv   = vals[k].v.tarval;
846                         size_t  size = get_mode_size_bytes(get_tarval_mode(tv));
847
848                         assert(tv != NULL);
849
850                         skip = size - 1;
851                         dump_size_type(obst, size);
852                         dump_arith_tarval(obst, tv, size);
853                         obstack_1grow(obst, '\n');
854                 } else {
855                         assert(vals[k].kind == BITFIELD);
856                         obstack_printf(obst, "\t.byte\t%d\n", vals[k].v.bf_val);
857                 }
858
859                 ++k;
860                 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
861                         ++space;
862                         ++k;
863                 }
864                 space -= skip;
865                 assert(space >= 0);
866
867                 /* a gap */
868                 if (space > 0)
869                         obstack_printf(obst, "\t.skip\t%d\n", space);
870         }
871         xfree(vals);
872
873 }
874
875 /**
876  * Dump an initializer for a compound entity.
877  */
878 static void dump_compound_init(be_gas_decl_env_t *env, obstack_t *obst,
879                                ir_entity *ent)
880 {
881         normal_or_bitfield *vals;
882         int i, j, n;
883         unsigned k, last_ofs;
884
885         if(ent->has_initializer) {
886                 dump_initializer(env, obst, ent);
887                 return;
888         }
889
890         n = get_compound_ent_n_values(ent);
891
892         /* Find the initializer size. Sorrily gcc support a nasty feature:
893            The last field of a compound may be a flexible array. This allows
894            initializers bigger than the type size. */
895         last_ofs = get_type_size_bytes(get_entity_type(ent));
896         for (i = 0; i < n; ++i) {
897                 unsigned offset         = get_compound_ent_value_offset_bytes(ent, i);
898                 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
899                 ir_node  *value         = get_compound_ent_value(ent, i);
900                 unsigned value_len      = get_mode_size_bits(get_irn_mode(value));
901
902                 offset += (value_len + bits_remainder + 7) >> 3;
903
904                 if (offset > last_ofs) {
905                         last_ofs = offset;
906                 }
907         }
908
909         /*
910          * In the worst case, every initializer allocates one byte.
911          * Moreover, initializer might be big, do not allocate on stack.
912          */
913         vals = xcalloc(last_ofs, sizeof(vals[0]));
914
915         /* collect the values and store them at the offsets */
916         for (i = 0; i < n; ++i) {
917                 unsigned offset      = get_compound_ent_value_offset_bytes(ent, i);
918                 int      offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
919                 ir_node  *value      = get_compound_ent_value(ent, i);
920                 int      value_len   = get_mode_size_bits(get_irn_mode(value));
921
922                 assert(offset_bits >= 0);
923
924                 if (offset_bits != 0 ||
925                         (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
926                         tarval *tv = get_atomic_init_tv(value);
927                         unsigned char curr_bits, last_bits = 0;
928                         if (tv == NULL) {
929                                 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
930                                       get_entity_ld_name(ent));
931                         }
932                         /* normalize offset */
933                         offset += offset_bits >> 3;
934                         offset_bits &= 7;
935
936                         for (j = 0; value_len + offset_bits > 0; ++j) {
937                                 assert(offset + j < last_ofs);
938                                 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
939                                 vals[offset + j].kind = BITFIELD;
940                                 curr_bits = get_tarval_sub_bits(tv, j);
941                                 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
942                                 value_len -= 8;
943                                 last_bits = curr_bits;
944                         }
945                 } else {
946                         int i;
947
948                         assert(offset < last_ofs);
949                         assert(vals[offset].kind == NORMAL);
950                         for (i = 1; i < value_len / 8; ++i) {
951                                 assert(vals[offset + i].v.value == NULL);
952                         }
953                         vals[offset].v.value = value;
954                 }
955         }
956
957         /* now write them sorted */
958         for (k = 0; k < last_ofs; ) {
959                 int space = 0, skip = 0;
960                 if (vals[k].kind == NORMAL) {
961                         if(vals[k].v.value != NULL) {
962                                 dump_atomic_init(env, obst, vals[k].v.value);
963                                 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
964                         } else {
965                                 space = 1;
966                         }
967                 } else {
968                         assert(vals[k].kind == BITFIELD);
969                         obstack_printf(obst, "\t.byte\t%d\n", vals[k].v.bf_val);
970                 }
971
972                 ++k;
973                 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
974                         ++space;
975                         ++k;
976                 }
977                 space -= skip;
978                 assert(space >= 0);
979
980                 /* a gap */
981                 if (space > 0)
982                         obstack_printf(obst, "\t.skip\t%d\n", space);
983         }
984         xfree(vals);
985 }
986
987 /**
988  * Dump a global entity.
989  *
990  * @param env           the gas output environment
991  * @param ent           the entity to be dumped
992  * @param emit_commons  if non-zero, emit commons (non-local uninitialized entities)
993  */
994 static void dump_global(be_gas_decl_env_t *env, ir_entity *ent, int emit_commons)
995 {
996         obstack_t *obst;
997         ir_type *type = get_entity_type(ent);
998         const char *ld_name = get_entity_ld_name(ent);
999         unsigned align = get_type_alignment_bytes(type);
1000         int emit_as_common = 0;
1001         ir_variability variability;
1002         ir_visibility visibility;
1003
1004         obst = env->data_obst;
1005         if (is_Method_type(type)) {
1006                 if (get_method_img_section(ent) == section_constructors) {
1007                         obst = env->ctor_obst;
1008                         obstack_printf(obst, ".balign\t%u\n", align);
1009                         dump_size_type(obst, align);
1010                         obstack_printf(obst, "%s\n", ld_name);
1011                 }
1012                 return;
1013         }
1014
1015         variability = get_entity_variability(ent);
1016         visibility = get_entity_visibility(ent);
1017         if (variability == variability_constant) {
1018                 /* a constant entity, put it on the rdata */
1019                 obst = env->rodata_obst;
1020         } else if (variability == variability_uninitialized) {
1021                 /* uninitialized entity put it in bss segment */
1022                 obst = env->bss_obst;
1023                 if (emit_commons && visibility != visibility_local)
1024                         emit_as_common = 1;
1025         }
1026
1027         be_dbg_variable(env->main_env->db_handle, obst, ent);
1028
1029         /* global or not global */
1030         if (visibility == visibility_external_visible && !emit_as_common) {
1031                 obstack_printf(obst, ".global\t%s\n", ld_name);
1032         } else if(visibility == visibility_external_allocated) {
1033                 obstack_printf(obst, ".global\t%s\n", ld_name);
1034                 /* we can return now... */
1035                 return;
1036         }
1037         /* alignment */
1038         if (align > 1 && !emit_as_common) {
1039                 obstack_printf(obst, ".balign\t%u\n", align);
1040         }
1041
1042         if (!emit_as_common) {
1043                 obstack_printf(obst, "%s:\n", ld_name);
1044         }
1045
1046         if (variability == variability_uninitialized) {
1047                 if (emit_as_common) {
1048                         switch (be_gas_flavour) {
1049                         case GAS_FLAVOUR_NORMAL:
1050                         case GAS_FLAVOUR_YASM:
1051                                 obstack_printf(obst, "\t.comm %s,%u,%u\n",
1052                                         ld_name, get_type_size_bytes(type), align);
1053                                 break;
1054                         case GAS_FLAVOUR_MINGW:
1055                                 obstack_printf(obst, "\t.comm %s,%u # %u\n",
1056                                         ld_name, get_type_size_bytes(type), align);
1057                                 break;
1058                         case GAS_FLAVOUR_MAX:
1059                                 panic("invalid gas flavour selected");
1060                         }
1061                 } else {
1062                         obstack_printf(obst, "\t.zero %u\n", get_type_size_bytes(type));
1063                 }
1064         } else {
1065                 if (is_atomic_entity(ent)) {
1066                         dump_atomic_init(env, obst, get_atomic_ent_value(ent));
1067                 } else {
1068                         /* sort_compound_ent_values(ent); */
1069
1070                         switch (get_type_tpop_code(get_entity_type(ent))) {
1071                         case tpo_array:
1072                                 if (ent_is_string_const(ent))
1073                                         dump_string_cst(obst, ent);
1074                                 else
1075                                         dump_compound_init(env, obst, ent);
1076                                 break;
1077                         case tpo_struct:
1078                         case tpo_class:
1079                         case tpo_union:
1080                                 dump_compound_init(env, obst, ent);
1081                                 break;
1082                         default:
1083                                 assert(0);
1084                         }
1085                 }
1086         }
1087 }
1088
1089 /**
1090  * Dumps declarations of global variables and the initialization code.
1091  *
1092  * @param gt                a global like type, either the global or the TLS one
1093  * @param env               an environment
1094  * @param emit_commons      if non-zero, emit commons (non-local uninitialized entities)
1095  * @param only_emit_marked  if non-zero, external allocated entities that do not have
1096  *                          its visited flag set are ignored
1097  */
1098 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env,
1099                               int emit_commons, int only_emit_marked)
1100 {
1101         int i, n = get_compound_n_members(gt);
1102         waitq *worklist = new_waitq();
1103
1104         if (only_emit_marked) {
1105                 for (i = 0; i < n; i++) {
1106                         ir_entity *ent = get_compound_member(gt, i);
1107                         if (is_entity_backend_marked(ent) ||
1108                             get_entity_visibility(ent) != visibility_external_allocated) {
1109                                 waitq_put(worklist, ent);
1110                                 set_entity_backend_marked(ent, 1);
1111                         }
1112                 }
1113         } else {
1114                 for (i = 0; i < n; i++) {
1115                         ir_entity *ent = get_compound_member(gt, i);
1116                         set_entity_backend_marked(ent, 1);
1117                         waitq_put(worklist, ent);
1118                 }
1119         }
1120
1121         env->worklist = worklist;
1122
1123         while (!waitq_empty(worklist)) {
1124                 ir_entity *ent = waitq_get(worklist);
1125
1126                 dump_global(env, ent, emit_commons);
1127         }
1128
1129         del_waitq(worklist);
1130         env->worklist = NULL;
1131 }
1132
1133 /************************************************************************/
1134
1135 /* Generate all entities. */
1136 void be_gas_emit_decls(const be_main_env_t *main_env,
1137                        int only_emit_marked_entities)
1138 {
1139         be_gas_decl_env_t env;
1140         obstack_t         rodata;
1141         obstack_t         data;
1142         obstack_t         bss;
1143         obstack_t         ctor;
1144         int               size;
1145         char              *cp;
1146
1147         /* dump the global type */
1148         obstack_init(&rodata);
1149         obstack_init(&data);
1150         obstack_init(&bss);
1151         obstack_init(&ctor);
1152
1153         env.rodata_obst = &rodata;
1154         env.data_obst   = &data;
1155         env.bss_obst    = &bss;
1156         env.ctor_obst   = &ctor;
1157         env.main_env    = main_env;
1158
1159         be_gas_dump_globals(get_glob_type(), &env, 1, only_emit_marked_entities);
1160
1161         size = obstack_object_size(&data);
1162         cp   = obstack_finish(&data);
1163         if (size > 0) {
1164                 be_gas_emit_switch_section(GAS_SECTION_DATA);
1165                 be_emit_string_len(cp, size);
1166                 be_emit_write_line();
1167         }
1168
1169         size = obstack_object_size(&rodata);
1170         cp   = obstack_finish(&rodata);
1171         if (size > 0) {
1172                 be_gas_emit_switch_section(GAS_SECTION_RODATA);
1173                 be_emit_string_len(cp, size);
1174                 be_emit_write_line();
1175         }
1176
1177         size = obstack_object_size(&bss);
1178         cp   = obstack_finish(&bss);
1179         if (size > 0) {
1180                 be_gas_emit_switch_section(GAS_SECTION_COMMON);
1181                 be_emit_string_len(cp, size);
1182                 be_emit_write_line();
1183         }
1184
1185         size = obstack_object_size(&ctor);
1186         cp   = obstack_finish(&ctor);
1187         if (size > 0) {
1188                 be_gas_emit_switch_section(GAS_SECTION_CTOR);
1189                 be_emit_string_len(cp, size);
1190                 be_emit_write_line();
1191         }
1192
1193         obstack_free(&rodata, NULL);
1194         obstack_free(&data, NULL);
1195         obstack_free(&bss, NULL);
1196         obstack_free(&ctor, NULL);
1197
1198         /* dump the Thread Local Storage */
1199         obstack_init(&data);
1200
1201         env.rodata_obst = &data;
1202         env.data_obst   = &data;
1203         env.bss_obst    = &data;
1204         env.ctor_obst   = NULL;
1205
1206         be_gas_dump_globals(get_tls_type(), &env, 0, only_emit_marked_entities);
1207
1208         size = obstack_object_size(&data);
1209         cp   = obstack_finish(&data);
1210         if (size > 0) {
1211                 be_gas_emit_switch_section(GAS_SECTION_TLS);
1212                 be_emit_cstring(".balign\t32\n");
1213                 be_emit_write_line();
1214                 be_emit_string_len(cp, size);
1215                 be_emit_write_line();
1216         }
1217
1218         obstack_free(&data, NULL);
1219 }