Support dtor sections on Mach-O.
[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 #include "config.h"
28
29 #include "begnuas.h"
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <assert.h>
35
36 #include "obst.h"
37 #include "tv.h"
38 #include "irnode.h"
39 #include "irprog.h"
40 #include "entity_t.h"
41 #include "error.h"
42
43 #include "be_t.h"
44 #include "beemitter.h"
45 #include "be_dbgout.h"
46
47 /** by default, we generate assembler code for the Linux gas */
48 object_file_format_t  be_gas_object_file_format = OBJECT_FILE_FORMAT_ELF;
49 bool                  be_gas_emit_types         = true;
50 char                  be_gas_elf_type_char      = '@';
51
52 static be_gas_section_t current_section = (be_gas_section_t) -1;
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 {
64         static const char *text[OBJECT_FILE_FORMAT_LAST+1][GAS_SECTION_LAST+1] = {
65                 { /* OBJECT_FILE_FORMAT_ELF */
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                         ".section\t.dtors,\"aw\",@progbits",
73                         NULL, /* no cstring section */
74                         NULL,
75                         NULL
76                 },
77                 { /* OBJECT_FILE_FORMAT_COFF */
78                         ".section\t.text",
79                         ".section\t.data",
80                         ".section .rdata,\"dr\"",
81                         ".section\t.bss",
82                         ".section\t.tbss,\"awT\",@nobits",
83                         ".section\t.ctors,\"w\"",
84                         ".section\t.dtors,\"w\"",
85                         NULL,
86                         NULL,
87                         NULL
88                 },
89                 { /* OBJECT_FILE_FORMAT_MACH_O */
90                         ".text",
91                         ".data",
92                         ".const",
93                         ".data",
94                         NULL,             /* TLS is not supported on Mach-O */
95                         ".mod_init_func",
96                         ".mod_term_func",
97                         ".cstring",
98                         ".section\t__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5",
99                         ".section\t__IMPORT,__pointers,non_lazy_symbol_pointers"
100                 }
101         };
102
103         assert((int) be_gas_object_file_format >= 0
104                         && be_gas_object_file_format <= OBJECT_FILE_FORMAT_LAST);
105         assert((int) section >= 0 && section <= GAS_SECTION_LAST);
106         return text[be_gas_object_file_format][section];
107 }
108
109 void be_gas_emit_switch_section(be_gas_section_t section)
110 {
111         if (current_section == section)
112                 return;
113
114         be_emit_char('\t');
115         be_emit_string(get_section_name(section));
116         be_emit_char('\n');
117         be_emit_write_line();
118         current_section = section;
119 }
120
121 static void emit_entity_visibility(const ir_entity *entity)
122 {
123         ir_visibility visibility = get_entity_visibility(entity);
124         ir_linkage    linkage    = get_entity_linkage(entity);
125
126         if (visibility != ir_visibility_local) {
127                 be_emit_cstring(".globl ");
128                 be_emit_ident(get_entity_ld_ident(entity));
129                 be_emit_char('\n');
130                 be_emit_write_line();
131         }
132         if (linkage & IR_LINKAGE_WEAK) {
133                 if (! (linkage & IR_LINKAGE_MERGE)) {
134                         panic("Weak symbols only supported in combination with IR_LINKAGE_MERGE on this architecture");
135                 }
136                 be_emit_cstring(".weak ");
137                 be_emit_ident(get_entity_ld_ident(entity));
138                 be_emit_char('\n');
139                 be_emit_write_line();
140         }
141 }
142
143 void be_gas_emit_function_prolog(ir_entity *entity, unsigned po2alignment)
144 {
145         const char *name = get_entity_ld_name(entity);
146
147         be_gas_emit_switch_section(GAS_SECTION_TEXT);
148
149         /* write the begin line (makes the life easier for scripts parsing the
150          * assembler) */
151         be_emit_write_line();
152         be_emit_cstring("# -- Begin  ");
153         be_emit_string(name);
154         be_emit_char('\n');
155         be_emit_write_line();
156
157         if (po2alignment > 0) {
158                 const char *fill_byte = "";
159                 unsigned    maximum_skip = (1 << po2alignment) - 1;
160                 /* gcc fills space between function with 0x90... */
161                 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
162                         fill_byte = "0x90";
163                 }
164                 be_emit_cstring("\t.p2align ");
165                 be_emit_irprintf("%u,%s,%u\n", po2alignment, fill_byte, maximum_skip);
166                 be_emit_write_line();
167         }
168         emit_entity_visibility(entity);
169
170         switch (be_gas_object_file_format) {
171         case OBJECT_FILE_FORMAT_ELF:
172                 be_emit_cstring("\t.type\t");
173                 be_emit_string(name);
174                 be_emit_cstring(", ");
175                 be_emit_char(be_gas_elf_type_char);
176                 be_emit_cstring("function\n");
177                 be_emit_write_line();
178                 break;
179         case OBJECT_FILE_FORMAT_COFF:
180                 be_emit_cstring("\t.def\t");
181                 be_emit_string(name);
182                 be_emit_cstring(";");
183                 if (get_entity_visibility(entity) == ir_visibility_local) {
184                         be_emit_cstring("\t.scl\t3;");
185                 } else {
186                         be_emit_cstring("\t.scl\t2;");
187                 }
188                 be_emit_cstring("\t.type\t32;\t.endef\n");
189                 be_emit_write_line();
190                 break;
191         case OBJECT_FILE_FORMAT_MACH_O:
192                 break;
193         }
194         be_emit_string(name);
195         be_emit_cstring(":\n");
196         be_emit_write_line();
197 }
198
199 void be_gas_emit_function_epilog(ir_entity *entity)
200 {
201         const char *name = get_entity_ld_name(entity);
202
203         if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF) {
204                 be_emit_cstring("\t.size\t");
205                 be_emit_string(name);
206                 be_emit_cstring(", .-");
207                 be_emit_string(name);
208                 be_emit_char('\n');
209                 be_emit_write_line();
210         }
211
212         be_emit_cstring("# -- End  ");
213         be_emit_string(name);
214         be_emit_char('\n');
215         be_emit_write_line();
216 }
217
218 /**
219  * An environment containing all needed dumper data.
220  * Currently we create the file completely in memory first, then
221  * write it to the disk. This is an artifact from the old C-generating backend
222  * and even there NOT needed. So we might change it in the future.
223  */
224 typedef struct _be_gas_decl_env {
225         be_gas_section_t     section;
226         const be_main_env_t *main_env;
227 } be_gas_decl_env_t;
228
229 /************************************************************************/
230
231 /**
232  * Output a tarval.
233  *
234  * @param tv     the tarval
235  * @param bytes  the width of the tarvals value in bytes
236  */
237 static void dump_arith_tarval(tarval *tv, int bytes)
238 {
239         switch (bytes) {
240         case 1:
241                 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
242                 return;
243
244         case 2:
245                 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
246                 return;
247
248         case 4:
249                 be_emit_irprintf("0x%02x%02x%02x%02x",
250                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
251                 return;
252
253         case 8:
254                 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
255                         get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
256                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
257                 return;
258
259         case 12:
260                 /* Beware: Mixed endian output!  One little endian number emitted as
261                  * three longs.  Each long initializer is written in big endian. */
262                 be_emit_irprintf(
263                         "\t.long\t0x%02x%02x%02x%02x\n"
264                         "\t.long\t0x%02x%02x%02x%02x\n"
265                         "\t.long\t0x%02x%02x%02x%02x",
266                         get_tarval_sub_bits(tv,  3), get_tarval_sub_bits(tv,  2),
267                         get_tarval_sub_bits(tv,  1), get_tarval_sub_bits(tv,  0),
268                         get_tarval_sub_bits(tv,  7), get_tarval_sub_bits(tv,  6),
269                         get_tarval_sub_bits(tv,  5), get_tarval_sub_bits(tv,  4),
270                         get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
271                         get_tarval_sub_bits(tv,  9), get_tarval_sub_bits(tv,  8)
272                 );
273                 return;
274
275         case 16:
276                 /* Beware: Mixed endian output!  One little endian number emitted as
277                  * three longs.  Each long initializer is written in big endian. */
278                 be_emit_irprintf(
279                         "\t.long\t0x%02x%02x%02x%02x\n"
280                         "\t.long\t0x%02x%02x%02x%02x\n"
281                         "\t.long\t0x%02x%02x%02x%02x\n"
282                         "\t.long\t0x%02x%02x%02x%02x",
283                         get_tarval_sub_bits(tv,  3), get_tarval_sub_bits(tv,  2),
284                         get_tarval_sub_bits(tv,  1), get_tarval_sub_bits(tv,  0),
285                         get_tarval_sub_bits(tv,  7), get_tarval_sub_bits(tv,  6),
286                         get_tarval_sub_bits(tv,  5), get_tarval_sub_bits(tv,  4),
287                         get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
288                         get_tarval_sub_bits(tv,  9), get_tarval_sub_bits(tv,  8),
289                         get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 14),
290                         get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12)
291                 );
292                 return;
293         }
294
295         panic("Can't dump a tarval with %d bytes", bytes);
296 }
297
298 /**
299  * Return the label prefix for labeled blocks.
300  */
301 const char *be_gas_block_label_prefix(void)
302 {
303         return ".LG";
304 }
305
306 /**
307  * Return the label prefix for labeled instructions.
308  */
309 const char *be_gas_insn_label_prefix(void)
310 {
311         return ".LE";
312 }
313
314 void be_gas_emit_entity(ir_entity *entity)
315 {
316         if (entity->type == firm_code_type) {
317                 ir_label_t label = get_entity_label(entity);
318                 be_emit_string(be_gas_block_label_prefix());
319                 be_emit_irprintf("%lu", label);
320         } else {
321                 be_emit_ident(get_entity_ld_ident(entity));
322         }
323 }
324
325 /**
326  * Return the tarval of an atomic initializer.
327  *
328  * @param init  a node representing the initializer (on the const code irg)
329  *
330  * @return the tarval
331  */
332 static tarval *get_atomic_init_tv(ir_node *init)
333 {
334         for (;;) {
335                 ir_mode *mode = get_irn_mode(init);
336
337                 switch (get_irn_opcode(init)) {
338
339                 case iro_Cast:
340                         init = get_Cast_op(init);
341                         continue;
342
343                 case iro_Conv:
344                         init = get_Conv_op(init);
345                         continue;
346
347                 case iro_Const:
348                         return get_Const_tarval(init);
349
350                 case iro_SymConst:
351                         switch (get_SymConst_kind(init)) {
352                         case symconst_type_size:
353                                 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
354
355                         case symconst_type_align:
356                                 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
357
358                         case symconst_ofs_ent:
359                                 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
360
361                         case symconst_enum_const:
362                                 return get_enumeration_value(get_SymConst_enum(init));
363
364                         default:
365                                 return NULL;
366                         }
367
368                 default:
369                         return NULL;
370                 }
371         }
372 }
373
374 /**
375  * Dump an atomic value.
376  *
377  * @param env   the gas output environment
378  * @param init  a node representing the atomic value (on the const code irg)
379  */
380 static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
381 {
382         ir_mode *mode = get_irn_mode(init);
383         int bytes     = get_mode_size_bytes(mode);
384         tarval *tv;
385         ir_entity *ent;
386
387         init = skip_Id(init);
388
389         switch (get_irn_opcode(init)) {
390         case iro_Cast:
391                 do_dump_atomic_init(env, get_Cast_op(init));
392                 return;
393
394         case iro_Conv:
395                 do_dump_atomic_init(env, get_Conv_op(init));
396                 return;
397
398         case iro_Const:
399                 tv = get_Const_tarval(init);
400
401                 /* it's a arithmetic value */
402                 dump_arith_tarval(tv, bytes);
403                 return;
404
405         case iro_SymConst:
406                 switch (get_SymConst_kind(init)) {
407                 case symconst_addr_name:
408                         be_emit_ident(get_SymConst_name(init));
409                         break;
410
411                 case symconst_addr_ent:
412                         ent = get_SymConst_entity(init);
413                         be_gas_emit_entity(ent);
414                         break;
415
416                 case symconst_ofs_ent:
417                         ent = get_SymConst_entity(init);
418                         be_emit_irprintf("%d", get_entity_offset(ent));
419                         break;
420
421                 case symconst_type_size:
422                         be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
423                         break;
424
425                 case symconst_type_align:
426                         be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
427                         break;
428
429                 case symconst_enum_const:
430                         tv = get_enumeration_value(get_SymConst_enum(init));
431                         dump_arith_tarval(tv, bytes);
432                         break;
433
434                 default:
435                         assert(!"dump_atomic_init(): don't know how to init from this SymConst");
436                 }
437                 return;
438
439         case iro_Add:
440                 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
441                         panic("Constant must be int or pointer for '+' to work");
442                 }
443                 do_dump_atomic_init(env, get_Add_left(init));
444                 be_emit_cstring(" + ");
445                 do_dump_atomic_init(env, get_Add_right(init));
446                 return;
447
448         case iro_Sub:
449                 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
450                         panic("Constant must be int or pointer for '-' to work");
451                 }
452                 do_dump_atomic_init(env, get_Sub_left(init));
453                 be_emit_cstring(" - ");
454                 do_dump_atomic_init(env, get_Sub_right(init));
455                 return;
456
457         case iro_Mul:
458                 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
459                         panic("Constant must be int or pointer for '*' to work");
460                 }
461                 do_dump_atomic_init(env, get_Mul_left(init));
462                 be_emit_cstring(" * ");
463                 do_dump_atomic_init(env, get_Mul_right(init));
464                 return;
465
466         case iro_Unknown:
467                 be_emit_cstring("0");
468                 return;
469
470         default:
471                 panic("dump_atomic_init(): unsupported IR-node %+F", init);
472         }
473 }
474
475 /**
476  * Dumps the type for given size (.byte, .long, ...)
477  *
478  * @param size  the size in bytes
479  */
480 static void dump_size_type(size_t size) {
481         switch (size) {
482         case 1:
483                 be_emit_cstring("\t.byte\t");
484                 break;
485
486         case 2:
487                 be_emit_cstring("\t.short\t");
488                 break;
489
490         case 4:
491                 be_emit_cstring("\t.long\t");
492                 break;
493
494         case 8:
495                 be_emit_cstring("\t.quad\t");
496                 break;
497
498         case 10:
499         case 12:
500         case 16: /* Note: .octa does not work on mac */
501                 /* handled in arith */
502                 break;
503
504         default:
505                 panic("Try to dump a type with %u bytes", (unsigned)size);
506         }
507 }
508
509 /**
510  * Emit an atomic value.
511  *
512  * @param env   the gas output environment
513  * @param init  a node representing the atomic value (on the const code irg)
514  */
515 static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
516 {
517         ir_mode *mode = get_irn_mode(init);
518         int bytes     = get_mode_size_bytes(mode);
519
520         dump_size_type(bytes);
521         do_dump_atomic_init(env, init);
522         be_emit_char('\n');
523         be_emit_write_line();
524 }
525
526 /************************************************************************/
527 /* Routines to dump global variables                                    */
528 /************************************************************************/
529
530 static int initializer_is_string_const(const ir_initializer_t *initializer)
531 {
532         size_t i, len;
533         int found_printable = 0;
534
535         if (initializer->kind != IR_INITIALIZER_COMPOUND)
536                 return 0;
537
538         len = initializer->compound.n_initializers;
539         if (len < 1)
540                 return 0;
541         for (i = 0; i < len; ++i) {
542                 int               c;
543                 tarval           *tv;
544                 ir_mode          *mode;
545                 ir_initializer_t *sub_initializer
546                         = initializer->compound.initializers[i];
547
548                 if (sub_initializer->kind != IR_INITIALIZER_TARVAL)
549                         return 0;
550
551                 tv   = sub_initializer->tarval.value;
552                 mode = get_tarval_mode(tv);
553
554                 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
555                         return 0;
556
557                 c = get_tarval_long(tv);
558                 if (isgraph(c) || isspace(c))
559                         found_printable = 1;
560                 else if (c != 0)
561                         return 0;
562
563                 if (i == len - 1 && c != '\0')
564                         return 0;
565         }
566
567         return found_printable;
568 }
569
570 /**
571  * Determine if an entity is a string constant
572  * @param ent The entity
573  * @return 1 if it is a string constant, 0 otherwise
574  */
575 static int ent_is_string_const(const ir_entity *ent)
576 {
577         ir_type *type, *element_type;
578         ir_mode *mode;
579         int i, c, n;
580
581         type = get_entity_type(ent);
582
583         /* if it's an array */
584         if (!is_Array_type(type))
585                 return 0;
586
587         element_type = get_array_element_type(type);
588
589         /* and the array's element type is primitive */
590         if (!is_Primitive_type(element_type))
591                 return 0;
592
593         /* and the mode of the element type is an int of
594          * the same size as the byte mode */
595         mode = get_type_mode(element_type);
596         if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
597                 return 0;
598
599         if (ent->initializer != NULL) {
600                 return initializer_is_string_const(ent->initializer);
601         } else if (entity_has_compound_ent_values(ent)) {
602                 int found_printable = 0;
603                 /* if it contains only printable chars and a 0 at the end */
604                 n = get_compound_ent_n_values(ent);
605                 for (i = 0; i < n; ++i) {
606                         ir_node *irn = get_compound_ent_value(ent, i);
607                         if (! is_Const(irn))
608                                 return 0;
609
610                         c = (int) get_tarval_long(get_Const_tarval(irn));
611
612                         if (isgraph(c) || isspace(c))
613                                 found_printable = 1;
614                         else if (c != 0)
615                                 return 0;
616
617                         if (i == n - 1 && c != '\0')
618                                 return 0;
619                 }
620                 return found_printable;
621         }
622
623         return 0;
624 }
625
626 /**
627  * Dump a string constant.
628  * No checks are made!!
629  *
630  * @param ent  The entity to dump.
631  */
632 static void dump_string_cst(const ir_entity *ent)
633 {
634         int      i, len;
635         int      output_len;
636         ir_type *type;
637         int      type_size;
638         int      remaining_space;
639
640         len        = get_compound_ent_n_values(ent);
641         output_len = len;
642         if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
643                 be_emit_cstring("\t.ascii \"");
644         } else {
645                 be_emit_cstring("\t.string \"");
646                 output_len -= 1;
647         }
648
649         for (i = 0; i < output_len; ++i) {
650                 ir_node *irn;
651                 int c;
652
653                 irn = get_compound_ent_value(ent, i);
654                 c = (int) get_tarval_long(get_Const_tarval(irn));
655
656                 switch (c) {
657                 case '"' : be_emit_cstring("\\\""); break;
658                 case '\n': be_emit_cstring("\\n"); break;
659                 case '\r': be_emit_cstring("\\r"); break;
660                 case '\t': be_emit_cstring("\\t"); break;
661                 case '\\': be_emit_cstring("\\\\"); break;
662                 default  :
663                         if (isprint(c))
664                                 be_emit_char(c);
665                         else
666                                 be_emit_irprintf("\\%o", c);
667                         break;
668                 }
669         }
670         be_emit_cstring("\"\n");
671         be_emit_write_line();
672
673         type            = get_entity_type(ent);
674         type_size       = get_type_size_bytes(type);
675         remaining_space = type_size - len;
676         assert(remaining_space >= 0);
677         if (remaining_space > 0) {
678                 be_emit_irprintf("\t.space\t%d\n", remaining_space);
679         }
680 }
681
682 static void dump_string_initializer(const ir_initializer_t *initializer)
683 {
684         size_t i, len;
685
686         len = initializer->compound.n_initializers;
687         if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
688                 be_emit_cstring("\t.ascii \"");
689         } else {
690                 be_emit_cstring("\t.string \"");
691                 len -= 1;
692         }
693
694         for (i = 0; i < len; ++i) {
695                 const ir_initializer_t *sub_initializer
696                         = get_initializer_compound_value(initializer, i);
697
698                 tarval *tv = get_initializer_tarval_value(sub_initializer);
699                 int     c  = get_tarval_long(tv);
700
701                 switch (c) {
702                 case '"' : be_emit_cstring("\\\""); break;
703                 case '\n': be_emit_cstring("\\n"); break;
704                 case '\r': be_emit_cstring("\\r"); break;
705                 case '\t': be_emit_cstring("\\t"); break;
706                 case '\\': be_emit_cstring("\\\\"); break;
707                 default  :
708                         if (isprint(c))
709                                 be_emit_char(c);
710                         else
711                                 be_emit_irprintf("\\%o", c);
712                         break;
713                 }
714         }
715         be_emit_cstring("\"\n");
716         be_emit_write_line();
717 }
718
719 enum normal_or_bitfield_kind {
720         NORMAL = 0,
721         TARVAL,
722         BITFIELD
723 };
724
725 typedef struct {
726         enum normal_or_bitfield_kind kind;
727         union {
728                 ir_node       *value;
729                 tarval        *tarval;
730                 unsigned char  bf_val;
731         } v;
732 } normal_or_bitfield;
733
734 static int is_type_variable_size(ir_type *type)
735 {
736         (void) type;
737         /* TODO */
738         return 0;
739 }
740
741 static size_t get_initializer_size(const ir_initializer_t *initializer,
742                                    ir_type *type)
743 {
744         switch (get_initializer_kind(initializer)) {
745         case IR_INITIALIZER_TARVAL:
746                 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
747                 return get_type_size_bytes(type);
748         case IR_INITIALIZER_CONST:
749         case IR_INITIALIZER_NULL:
750                 return get_type_size_bytes(type);
751         case IR_INITIALIZER_COMPOUND:
752                 if (!is_type_variable_size(type)) {
753                         return get_type_size_bytes(type);
754                 } else {
755                         unsigned n_entries
756                                 = get_initializer_compound_n_entries(initializer);
757                         unsigned i;
758                         unsigned initializer_size = get_type_size_bytes(type);
759                         for (i = 0; i < n_entries; ++i) {
760                                 ir_entity *entity = get_compound_member(type, i);
761                                 ir_type   *type   = get_entity_type(entity);
762
763                                 const ir_initializer_t *sub_initializer
764                                         = get_initializer_compound_value(initializer, i);
765
766                                 unsigned offset = get_entity_offset(entity);
767                                 unsigned size   = get_initializer_size(sub_initializer, type);
768
769                                 if (offset + size > initializer_size) {
770                                         initializer_size = offset + size;
771                                 }
772                         }
773                         return initializer_size;
774                 }
775         }
776
777         panic("found invalid initializer");
778 }
779
780 #ifndef NDEBUG
781 static normal_or_bitfield *glob_vals;
782 static size_t              max_vals;
783 #endif
784
785 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
786                           const ir_initializer_t *initializer, ir_type *type)
787 {
788         unsigned char  last_bits = 0;
789         ir_mode       *mode      = get_type_mode(type);
790         tarval        *tv        = NULL;
791         unsigned char  curr_bits;
792         int            value_len;
793         int            j;
794
795         switch (get_initializer_kind(initializer)) {
796         case IR_INITIALIZER_NULL:
797                 return;
798         case IR_INITIALIZER_TARVAL:
799                 tv = get_initializer_tarval_value(initializer);
800                 break;
801         case IR_INITIALIZER_CONST: {
802                 ir_node *node = get_initializer_const_value(initializer);
803                 if (!is_Const(node)) {
804                         panic("bitfield initializer not a Const node");
805                 }
806                 tv = get_Const_tarval(node);
807                 break;
808         }
809         case IR_INITIALIZER_COMPOUND:
810                 panic("bitfield initializer is compound");
811         }
812         if (tv == NULL) {
813                 panic("Couldn't get numeric value for bitfield initializer");
814         }
815         tv = tarval_convert_to(tv, get_type_mode(type));
816
817         /* normalize offset */
818         vals        += offset_bits >> 3;
819         offset_bits &= 7;
820         value_len    = get_mode_size_bits(mode);
821
822         /* combine bits with existing bits */
823         for (j = 0; value_len + (int) offset_bits > 0; ++j) {
824                 assert((size_t) (vals - glob_vals) + j < max_vals);
825                 assert(vals[j].kind == BITFIELD ||
826                                 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
827                 vals[j].kind = BITFIELD;
828                 curr_bits    = get_tarval_sub_bits(tv, j);
829                 vals[j].v.bf_val
830                         |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
831                 value_len -= 8;
832                 last_bits = curr_bits;
833         }
834 }
835
836 static void dump_ir_initializer(normal_or_bitfield *vals,
837                                 const ir_initializer_t *initializer,
838                                 ir_type *type)
839 {
840         assert((size_t) (vals - glob_vals) < max_vals);
841
842         switch (get_initializer_kind(initializer)) {
843         case IR_INITIALIZER_NULL:
844                 return;
845         case IR_INITIALIZER_TARVAL: {
846                 size_t i;
847
848                 assert(vals->kind != BITFIELD);
849                 vals->kind     = TARVAL;
850                 vals->v.tarval = get_initializer_tarval_value(initializer);
851                 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
852                 for (i = 1; i < get_type_size_bytes(type); ++i) {
853                         vals[i].kind    = NORMAL;
854                         vals[i].v.value = NULL;
855                 }
856                 return;
857         }
858         case IR_INITIALIZER_CONST: {
859                 size_t i;
860
861                 assert(vals->kind != BITFIELD);
862                 vals->kind    = NORMAL;
863                 vals->v.value = get_initializer_const_value(initializer);
864                 for (i = 1; i < get_type_size_bytes(type); ++i) {
865                         vals[i].kind    = NORMAL;
866                         vals[i].v.value = NULL;
867                 }
868                 return;
869         }
870         case IR_INITIALIZER_COMPOUND: {
871                 size_t i = 0;
872                 size_t n = get_initializer_compound_n_entries(initializer);
873
874                 if (is_Array_type(type)) {
875                         ir_type *element_type = get_array_element_type(type);
876                         size_t   skip         = get_type_size_bytes(element_type);
877                         size_t   alignment    = get_type_alignment_bytes(element_type);
878                         size_t   misalign     = skip % alignment;
879                         if (misalign != 0) {
880                                 skip += alignment - misalign;
881                         }
882
883                         for (i = 0; i < n; ++i) {
884                                 ir_initializer_t *sub_initializer
885                                         = get_initializer_compound_value(initializer, i);
886
887                                 dump_ir_initializer(vals, sub_initializer, element_type);
888
889                                 vals += skip;
890                         }
891                 } else {
892                         size_t n_members, i;
893                         assert(is_compound_type(type));
894                         n_members = get_compound_n_members(type);
895                         for (i = 0; i < n_members; ++i) {
896                                 ir_entity        *member    = get_compound_member(type, i);
897                                 size_t            offset    = get_entity_offset(member);
898                                 ir_type          *subtype   = get_entity_type(member);
899                                 ir_mode          *mode      = get_type_mode(subtype);
900                                 ir_initializer_t *sub_initializer;
901
902                                 assert(i < get_initializer_compound_n_entries(initializer));
903                                 sub_initializer
904                                         = get_initializer_compound_value(initializer, i);
905
906                                 if (mode != NULL) {
907                                         size_t offset_bits
908                                                 = get_entity_offset_bits_remainder(member);
909                                         size_t value_len   = get_mode_size_bits(mode);
910
911                                         if (offset_bits != 0 ||
912                                                 (value_len != 8 && value_len != 16 && value_len != 32
913                                                  && value_len != 64)) {
914                                                 dump_bitfield(&vals[offset], offset_bits,
915                                                               sub_initializer, subtype);
916                                                 continue;
917                                         }
918                                 }
919
920                                 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
921                         }
922                 }
923
924                 return;
925         }
926         }
927         panic("invalid ir_initializer kind found");
928 }
929
930 static void dump_initializer(be_gas_decl_env_t *env, const ir_entity *entity)
931 {
932         const ir_initializer_t *initializer = entity->initializer;
933         ir_type                *type;
934         normal_or_bitfield     *vals;
935         size_t                  size;
936         size_t                  k;
937
938         if (initializer_is_string_const(initializer)) {
939                 dump_string_initializer(initializer);
940                 return;
941         }
942
943         type = get_entity_type(entity);
944         size = get_initializer_size(initializer, type);
945
946         if (size == 0)
947                 return;
948
949         /*
950          * In the worst case, every initializer allocates one byte.
951          * Moreover, initializer might be big, do not allocate on stack.
952          */
953         vals = XMALLOCNZ(normal_or_bitfield, size);
954
955 #ifndef NDEBUG
956         glob_vals = vals;
957         max_vals  = size;
958 #endif
959
960         dump_ir_initializer(vals, initializer, type);
961
962         /* now write values sorted */
963         for (k = 0; k < size; ) {
964                 int space     = 0;
965                 int elem_size = 1;
966                 if (vals[k].kind == NORMAL) {
967                         if (vals[k].v.value != NULL) {
968                                 dump_atomic_init(env, vals[k].v.value);
969                                 elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
970                         } else {
971                                 elem_size = 0;
972                         }
973                 } else if (vals[k].kind == TARVAL) {
974                         tarval *tv   = vals[k].v.tarval;
975                         size_t  size = get_mode_size_bytes(get_tarval_mode(tv));
976
977                         assert(tv != NULL);
978
979                         elem_size = size;
980                         dump_size_type(size);
981                         dump_arith_tarval(tv, size);
982                         be_emit_char('\n');
983                         be_emit_write_line();
984                 } else {
985                         assert(vals[k].kind == BITFIELD);
986                         be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
987                         be_emit_write_line();
988                 }
989
990                 k += elem_size;
991                 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
992                         ++space;
993                         ++k;
994                 }
995
996                 /* a gap */
997                 if (space > 0) {
998                         be_emit_irprintf("\t.space\t%d\n", space);
999                         be_emit_write_line();
1000                 }
1001         }
1002         xfree(vals);
1003 }
1004
1005 static void dump_compound_graph_init(be_gas_decl_env_t *env,
1006                                      const ir_entity *ent)
1007 {
1008         normal_or_bitfield *vals;
1009         int i, j, n;
1010         unsigned k, last_ofs;
1011
1012         if (ent_is_string_const(ent)) {
1013                 dump_string_cst(ent);
1014                 return;
1015         }
1016
1017         n = get_compound_ent_n_values(ent);
1018
1019         /* Find the initializer size. Sorrily gcc support a nasty feature:
1020            The last field of a compound may be a flexible array. This allows
1021            initializers bigger than the type size. */
1022         last_ofs = get_type_size_bytes(get_entity_type(ent));
1023         for (i = 0; i < n; ++i) {
1024                 unsigned offset         = get_compound_ent_value_offset_bytes(ent, i);
1025                 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1026                 ir_node  *value         = get_compound_ent_value(ent, i);
1027                 unsigned value_len      = get_mode_size_bits(get_irn_mode(value));
1028
1029                 offset += (value_len + bits_remainder + 7) >> 3;
1030
1031                 if (offset > last_ofs) {
1032                         last_ofs = offset;
1033                 }
1034         }
1035
1036         /*
1037          * In the worst case, every initializer allocates one byte.
1038          * Moreover, initializer might be big, do not allocate on stack.
1039          */
1040         vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1041
1042         /* collect the values and store them at the offsets */
1043         for (i = 0; i < n; ++i) {
1044                 unsigned offset      = get_compound_ent_value_offset_bytes(ent, i);
1045                 int      offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1046                 ir_node  *value      = get_compound_ent_value(ent, i);
1047                 int      value_len   = get_mode_size_bits(get_irn_mode(value));
1048
1049                 assert(offset_bits >= 0);
1050
1051                 if (offset_bits != 0 ||
1052                                 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1053                         tarval *tv = get_atomic_init_tv(value);
1054                         unsigned char curr_bits, last_bits = 0;
1055                         if (tv == NULL) {
1056                                 panic("Couldn't get numeric value for bitfield initializer '%s'",
1057                                                 get_entity_ld_name(ent));
1058                         }
1059                         /* normalize offset */
1060                         offset += offset_bits >> 3;
1061                         offset_bits &= 7;
1062
1063                         for (j = 0; value_len + offset_bits > 0; ++j) {
1064                                 assert(offset + j < last_ofs);
1065                                 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1066                                 vals[offset + j].kind = BITFIELD;
1067                                 curr_bits = get_tarval_sub_bits(tv, j);
1068                                 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1069                                 value_len -= 8;
1070                                 last_bits = curr_bits;
1071                         }
1072                 } else {
1073                         int i;
1074
1075                         assert(offset < last_ofs);
1076                         assert(vals[offset].kind == NORMAL);
1077                         for (i = 1; i < value_len / 8; ++i) {
1078                                 assert(vals[offset + i].v.value == NULL);
1079                         }
1080                         vals[offset].v.value = value;
1081                 }
1082         }
1083
1084         /* now write them sorted */
1085         for (k = 0; k < last_ofs; ) {
1086                 int space = 0, skip = 0;
1087                 if (vals[k].kind == NORMAL) {
1088                         if (vals[k].v.value != NULL) {
1089                                 dump_atomic_init(env, vals[k].v.value);
1090                                 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1091                         } else {
1092                                 space = 1;
1093                         }
1094                 } else {
1095                         assert(vals[k].kind == BITFIELD);
1096                         be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1097                 }
1098
1099                 ++k;
1100                 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1101                         ++space;
1102                         ++k;
1103                 }
1104                 space -= skip;
1105                 assert(space >= 0);
1106
1107                 /* a gap */
1108                 if (space > 0) {
1109                         be_emit_irprintf("\t.space\t%d\n", space);
1110                         be_emit_write_line();
1111                 }
1112         }
1113         xfree(vals);
1114 }
1115
1116 static void emit_align(unsigned p2alignment)
1117 {
1118         be_emit_irprintf("\t.p2align\t%u\n", log2_floor(p2alignment));
1119         be_emit_write_line();
1120 }
1121
1122 static unsigned get_effective_entity_alignment(const ir_entity *entity)
1123 {
1124         unsigned alignment = get_entity_alignment(entity);
1125         if (alignment == 0) {
1126                 ir_type *type = get_entity_type(entity);
1127                 alignment     = get_type_alignment_bytes(type);
1128         }
1129         return alignment;
1130 }
1131
1132 static be_gas_section_t determine_section(be_gas_decl_env_t *env,
1133                                           const ir_entity *entity)
1134 {
1135         ir_type *owner = get_entity_owner(entity);
1136
1137         if (owner == get_segment_type(IR_SEGMENT_GLOBAL)) {
1138                 ir_linkage linkage = get_entity_linkage(entity);
1139                 if (linkage & IR_LINKAGE_CONSTANT) {
1140                         /* mach-o is the only one with a cstring section */
1141                         if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
1142                                         && ent_is_string_const(entity))
1143                                 return GAS_SECTION_CSTRING;
1144
1145                         return GAS_SECTION_RODATA;
1146                 }
1147                 if (!entity_has_definition(entity))
1148                         return GAS_SECTION_BSS;
1149
1150                 return GAS_SECTION_DATA;
1151
1152         } else if (owner == env->main_env->pic_symbols_type) {
1153                 return GAS_SECTION_PIC_SYMBOLS;
1154         } else if (owner == env->main_env->pic_trampolines_type) {
1155                 return GAS_SECTION_PIC_TRAMPOLINES;
1156         } else if (owner == get_segment_type(IR_SEGMENT_CONSTRUCTORS)) {
1157                 return GAS_SECTION_CONSTRUCTORS;
1158         } else if (owner == get_segment_type(IR_SEGMENT_DESTRUCTORS)) {
1159                 return GAS_SECTION_DESTRUCTORS;
1160         } else if (owner == get_segment_type(IR_SEGMENT_THREAD_LOCAL)) {
1161                 return GAS_SECTION_TLS;
1162         }
1163
1164         panic("Couldn't determine section for %+F?!?", entity);
1165 }
1166
1167 static void emit_common(const ir_entity *ent)
1168 {
1169         const char *name      = get_entity_ld_name(ent);
1170         unsigned    size      = get_type_size_bytes(get_entity_type(ent));
1171         unsigned    alignment = get_effective_entity_alignment(ent);
1172
1173         switch (be_gas_object_file_format) {
1174         case OBJECT_FILE_FORMAT_MACH_O:
1175                 be_emit_irprintf("\t.comm %s,%u,%u\n", name, size,
1176                                  log2_floor(alignment));
1177                 be_emit_write_line();
1178                 return;
1179         case OBJECT_FILE_FORMAT_ELF:
1180                 be_emit_irprintf("\t.comm %s,%u,%u\n", name, size, alignment);
1181                 be_emit_write_line();
1182                 return;
1183         case OBJECT_FILE_FORMAT_COFF:
1184                 be_emit_irprintf("\t.comm %s,%u # %u\n", name, size, alignment);
1185                 be_emit_write_line();
1186                 return;
1187         }
1188         panic("invalid object file format");
1189 }
1190
1191 static void dump_indirect_symbol(const ir_entity *entity, be_gas_section_t section)
1192 {
1193         /* we can only do PIC code on macho so far */
1194         assert(be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O);
1195
1196         be_emit_ident(get_entity_ld_ident(entity));
1197         be_emit_cstring(":\n");
1198         be_emit_write_line();
1199         be_emit_cstring("\t.indirect_symbol ");
1200         be_emit_ident(get_entity_ident(entity));
1201         be_emit_char('\n');
1202         be_emit_write_line();
1203         if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1204                 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1205                 be_emit_write_line();
1206         } else {
1207                 assert(section == GAS_SECTION_PIC_SYMBOLS);
1208                 be_emit_cstring("\t.long 0\n");
1209                 be_emit_write_line();
1210         }
1211 }
1212
1213 /**
1214  * Dump a global entity.
1215  *
1216  * @param env  the gas output environment
1217  * @param ent  the entity to be dumped
1218  */
1219 static void dump_global(be_gas_decl_env_t *env, const ir_entity *ent)
1220 {
1221         ir_type          *type           = get_entity_type(ent);
1222         ident            *ld_ident       = get_entity_ld_ident(ent);
1223         unsigned          alignment      = get_effective_entity_alignment(ent);
1224         be_gas_section_t  section        = determine_section(env, ent);
1225
1226         /* we already emitted all methods. Except for the trampolines which
1227          * the assembler/linker generates */
1228         if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1229                 return;
1230         }
1231         /* block labels are already emittet in the code */
1232         if (type == firm_code_type)
1233                 return;
1234
1235         be_dbg_variable(ent);
1236
1237         /* nothing to do for externally defined values */
1238         if (get_entity_visibility(ent) == ir_visibility_external)
1239                 return;
1240
1241         if (!is_po2(alignment))
1242                 panic("alignment not a power of 2");
1243
1244         if (section == GAS_SECTION_BSS &&
1245                         (get_entity_linkage(ent) & IR_LINKAGE_MERGE)) {
1246                 if (get_entity_visibility(ent) != ir_visibility_default) {
1247                         panic("merge link semantic not supported for local/extern entities");
1248                 }
1249                 emit_common(ent);
1250                 return;
1251         }
1252
1253         be_gas_emit_switch_section(section);
1254
1255         if (section == GAS_SECTION_PIC_TRAMPOLINES
1256                         || section == GAS_SECTION_PIC_SYMBOLS) {
1257                 dump_indirect_symbol(ent, section);
1258                 return;
1259         }
1260
1261         /* alignment */
1262         if (alignment > 1) {
1263                 emit_align(alignment);
1264         }
1265         emit_entity_visibility(ent);
1266         if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF
1267                         && be_gas_emit_types) {
1268                 be_emit_cstring("\t.type\t");
1269                 be_emit_ident(ld_ident);
1270                 be_emit_cstring(", ");
1271                 be_emit_char(be_gas_elf_type_char);
1272                 be_emit_cstring("object\n\t.size\t");
1273                 be_emit_ident(ld_ident);
1274                 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1275         }
1276         be_emit_ident(ld_ident);
1277         be_emit_cstring(":\n");
1278         be_emit_write_line();
1279
1280         if (ent->initializer != NULL) {
1281                 dump_initializer(env, ent);
1282         } else if(entity_has_compound_ent_values(ent)) {
1283                 dump_compound_graph_init(env, ent);
1284         } else {
1285                 /* uninitialized */
1286                 be_emit_irprintf("\t.space %u\n", get_type_size_bytes(type));
1287                 be_emit_write_line();
1288         }
1289 }
1290
1291 /**
1292  * Dumps declarations of global variables and the initialization code.
1293  *
1294  * @param gt                a global like type, either the global or the TLS one
1295  * @param env               an environment
1296  */
1297 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env)
1298 {
1299         int i, n = get_compound_n_members(gt);
1300
1301         for (i = 0; i < n; i++) {
1302                 ir_entity *ent = get_compound_member(gt, i);
1303                 dump_global(env, ent);
1304         }
1305 }
1306
1307 /************************************************************************/
1308
1309 /* Generate all entities. */
1310 void be_gas_emit_decls(const be_main_env_t *main_env)
1311 {
1312         be_gas_decl_env_t env;
1313         memset(&env, 0, sizeof(env));
1314
1315         /* dump global type */
1316         env.main_env = main_env;
1317         env.section  = (be_gas_section_t) -1;
1318
1319         be_gas_dump_globals(get_glob_type(), &env);
1320         be_gas_dump_globals(get_tls_type(), &env);
1321         be_gas_dump_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env);
1322         be_gas_dump_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env);
1323         be_gas_dump_globals(main_env->pic_symbols_type, &env);
1324         be_gas_dump_globals(main_env->pic_trampolines_type, &env);
1325
1326         /**
1327          * ".subsections_via_symbols marks object files which are OK to divide
1328          * their section contents into individual blocks".
1329          * From my understanding this means no label points in the middle of an
1330          * object which we want to address as a whole. Firm code should be fine
1331          * with this.
1332          */
1333         if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
1334                 be_emit_cstring("\t.subsections_via_symbols\n");
1335                 be_emit_write_line();
1336         }
1337 }