move backend into libfirm
[libfirm] / ir / be / begnuas.c
1 /**
2  * Dumps global variables and constants as gas assembler.
3  * @author Christian Wuerdig, Matthias Braun
4  * @date 04.11.2005
5  * @version $Id$
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include "begnuas.h"
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <assert.h>
17
18 #include "obst.h"
19 #include "tv.h"
20 #include "irnode.h"
21 #include "entity.h"
22 #include "irprog.h"
23 #include "error.h"
24
25 #include "be_t.h"
26 #include "beemitter.h"
27 #include "be_dbgout.h"
28
29 typedef struct obstack obstack_t;
30
31 /** by default, we generate assembler code for the Linux gas */
32 be_gas_flavour_t be_gas_flavour = GAS_FLAVOUR_NORMAL;
33
34 static const char* get_section_name(be_gas_section_t section) {
35         static const char *text[GAS_FLAVOUR_MAX][GAS_SECTION_MAX] = {
36                 {
37                         ".section\t.text",
38                         ".section\t.data",
39                         ".section\t.rodata",
40                         ".section\t.bss",
41                         ".section\t.tbss,\"awT\",@nobits",
42                         ".section\t.ctors,\"aw\",@progbits"
43                 },
44                 {
45                         ".section\t.text",
46                         ".section\t.data",
47                         ".section .rdata,\"dr\"",
48                         ".section\t.bss",
49                         ".section\t.tbss,\"awT\",@nobits",
50                         ".section\t.ctors,\"aw\",@progbits"
51                 }
52         };
53
54         assert(be_gas_flavour >= 0 && be_gas_flavour < GAS_FLAVOUR_MAX);
55         assert(section >= 0 && section < GAS_SECTION_MAX);
56         return text[be_gas_flavour][section];
57 }
58
59 void be_gas_emit_switch_section(be_emit_env_t *env, be_gas_section_t section) {
60         be_emit_char(env, '\t');
61         be_emit_string(env, get_section_name(section));
62         be_emit_char(env, '\n');
63         be_emit_write_line(env);
64 }
65
66 typedef struct _ia32_decl_env {
67         obstack_t *rodata_obst;
68         obstack_t *data_obst;
69         obstack_t *bss_obst;
70         obstack_t *ctor_obst;
71         const be_main_env_t *main_env;
72 } ia32_decl_env_t;
73
74 /************************************************************************/
75
76 /**
77  * output a tarval
78  */
79 static void dump_arith_tarval(obstack_t *obst, tarval *tv, int bytes)
80 {
81         switch (bytes) {
82
83         case 1:
84                 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
85                 break;
86
87         case 2:
88                 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
89                 break;
90
91         case 4:
92                 obstack_printf(obst, "0x%02x%02x%02x%02x",
93                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
94                 break;
95
96         case 8:
97                 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
98                         get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
99                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
100                 break;
101
102         case 10:
103         case 12:
104                 break;
105
106         case 16:
107                 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x"
108                                                "%02x%02x%02x%02x%02x%02x%02x%02x",
109                         get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 16),
110                         get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12),
111                         get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
112                         get_tarval_sub_bits(tv, 9), get_tarval_sub_bits(tv, 8),
113                         get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6),
114                         get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
115                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2),
116                         get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
117                 break;
118
119
120         default:
121                 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
122                 assert(0);
123         }
124 }
125
126 /**
127  * Return the tarval of an atomic initializer.
128  */
129 static tarval *get_atomic_init_tv(ir_node *init)
130 {
131         for (;;) {
132                 ir_mode *mode = get_irn_mode(init);
133
134                 switch (get_irn_opcode(init)) {
135
136                 case iro_Cast:
137                         init = get_Cast_op(init);
138                         continue;
139
140                 case iro_Conv:
141                         init = get_Conv_op(init);
142                         continue;
143
144                 case iro_Const:
145                         return get_Const_tarval(init);
146
147                 case iro_SymConst:
148                         switch (get_SymConst_kind(init)) {
149                         case symconst_ofs_ent:
150                                 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
151
152                         case symconst_type_size:
153                                 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
154
155                         case symconst_type_align:
156                                 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
157
158                         case symconst_enum_const:
159                                 return get_enumeration_value(get_SymConst_enum(init));
160
161                         default:
162                                 return NULL;
163                         }
164
165                 default:
166                         return NULL;
167                 }
168         }
169 }
170
171 /**
172  * dump an atomic value
173  */
174 static void do_dump_atomic_init(obstack_t *obst, ir_node *init)
175 {
176         ir_mode *mode = get_irn_mode(init);
177         int bytes     = get_mode_size_bytes(mode);
178         tarval *tv;
179
180         switch (get_irn_opcode(init)) {
181
182         case iro_Cast:
183                 do_dump_atomic_init(obst, get_Cast_op(init));
184                 return;
185
186         case iro_Conv:
187                 do_dump_atomic_init(obst, get_Conv_op(init));
188                 return;
189
190         case iro_Const:
191                 tv = get_Const_tarval(init);
192
193                 /* it's a arithmetic value */
194                 dump_arith_tarval(obst, tv, bytes);
195                 return;
196
197         case iro_SymConst:
198                 switch (get_SymConst_kind(init)) {
199                 case symconst_addr_name:
200                         obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
201                         break;
202
203                 case symconst_addr_ent:
204                         obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
205                         break;
206
207                 case symconst_ofs_ent:
208                         obstack_printf(obst, "%d", get_entity_offset(get_SymConst_entity(init)));
209                         break;
210
211                 case symconst_type_size:
212                         obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
213                         break;
214
215                 case symconst_type_align:
216                         obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
217                         break;
218
219                 case symconst_enum_const:
220                         tv = get_enumeration_value(get_SymConst_enum(init));
221                         dump_arith_tarval(obst, tv, bytes);
222                         break;
223
224                 default:
225                         assert(!"dump_atomic_init(): don't know how to init from this SymConst");
226                 }
227                 return;
228
229                 case iro_Add:
230                         do_dump_atomic_init(obst, get_Add_left(init));
231                         obstack_printf(obst, " + ");
232                         do_dump_atomic_init(obst, get_Add_right(init));
233                         return;
234
235                 case iro_Sub:
236                         do_dump_atomic_init(obst, get_Sub_left(init));
237                         obstack_printf(obst, " - ");
238                         do_dump_atomic_init(obst, get_Sub_right(init));
239                         return;
240
241                 case iro_Mul:
242                         do_dump_atomic_init(obst, get_Mul_left(init));
243                         obstack_printf(obst, " * ");
244                         do_dump_atomic_init(obst, get_Mul_right(init));
245                         return;
246
247                 default:
248                         assert(0 && "dump_atomic_init(): unknown IR-node");
249         }
250 }
251
252 /**
253  * dumps the type for given size (.byte, .long, ...)
254  */
255 static void dump_size_type(obstack_t *obst, int size) {
256         switch (size) {
257
258         case 1:
259                 obstack_printf(obst, "\t.byte\t");
260                 break;
261
262         case 2:
263                 obstack_printf(obst, "\t.value\t");
264                 break;
265
266         case 4:
267                 obstack_printf(obst, "\t.long\t");
268                 break;
269
270         case 8:
271                 obstack_printf(obst, "\t.quad\t");
272                 break;
273
274         case 10:
275         case 12:
276                 /* handled in arith */
277                 break;
278
279         case 16:
280                 obstack_printf(obst, "\t.octa\t");
281                 break;
282
283         default:
284                 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
285                 assert(0);
286         }
287 }
288
289 /**
290  * dump an atomic value to an obstack
291  */
292 static void dump_atomic_init(obstack_t *obst, ir_node *init)
293 {
294         ir_mode *mode = get_irn_mode(init);
295         int bytes     = get_mode_size_bytes(mode);
296
297         dump_size_type(obst, bytes);
298         do_dump_atomic_init(obst, init);
299         obstack_printf(obst, "\n");
300 }
301
302 /************************************************************************/
303 /* Routines to dump global variables                                    */
304 /************************************************************************/
305
306 /**
307  * Determine if an entity is a string constant
308  * @param ent The entity
309  * @return 1 if it is a string constant, 0 otherwise
310  */
311 static int ent_is_string_const(ir_entity *ent)
312 {
313         ir_type *type, *element_type;
314         ir_mode *mode;
315         int i, c, n;
316
317         type = get_entity_type(ent);
318
319         /* if it's an array */
320         if (!is_Array_type(type))
321                 return 0;
322
323         element_type = get_array_element_type(type);
324
325         /* and the array's element type is primitive */
326         if (!is_Primitive_type(element_type))
327                 return 0;
328
329         /* and the mode of the element type is an int of
330          * the same size as the byte mode */
331         mode = get_type_mode(element_type);
332         if (!mode_is_int(mode)
333                 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
334                 return 0;
335
336         /* if it contains only printable chars and a 0 at the end */
337         n = get_compound_ent_n_values(ent);
338         for (i = 0; i < n; ++i) {
339                 ir_node *irn = get_compound_ent_value(ent, i);
340                 if(get_irn_opcode(irn) != iro_Const)
341                         return 0;
342
343                 c = (int) get_tarval_long(get_Const_tarval(irn));
344
345                 if((i < n - 1 && !(isgraph(c) || isspace(c)))
346                                 || (i == n - 1 && c != '\0'))
347                         return 0;
348         }
349
350         /* then we can emit it as a string constant */
351         return 1;
352 }
353
354 /**
355  * Dump a string constant.
356  * No checks are made!!
357  * @param obst The obst to dump on.
358  * @param ent The entity to dump.
359  */
360 static void dump_string_cst(obstack_t *obst, ir_entity *ent)
361 {
362         int i, n;
363
364         obstack_printf(obst, "\t.string \"");
365         n = get_compound_ent_n_values(ent);
366
367         for (i = 0; i < n-1; ++i) {
368                 ir_node *irn;
369                 int c;
370
371                 irn = get_compound_ent_value(ent, i);
372                 c = (int) get_tarval_long(get_Const_tarval(irn));
373
374                 switch (c) {
375                 case '"' : obstack_printf(obst, "\\\""); break;
376                 case '\n': obstack_printf(obst, "\\n"); break;
377                 case '\r': obstack_printf(obst, "\\r"); break;
378                 case '\t': obstack_printf(obst, "\\t"); break;
379                 case '\\': obstack_printf(obst, "\\\\"); break;
380                 default  :
381                         if (isprint(c))
382                                 obstack_printf(obst, "%c", c);
383                         else
384                                 obstack_printf(obst, "\\%o", c);
385                         break;
386                 }
387         }
388         obstack_printf(obst, "\"\n");
389 }
390
391 static void dump_array_init(obstack_t *obst, ir_entity *ent)
392 {
393         const ir_type *ty = get_entity_type(ent);
394         int i;
395         int filler;
396         int size = 0;
397
398         /* potential spare values should be already included! */
399         for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
400                 ir_entity *step = get_compound_ent_value_member(ent, i);
401                 ir_type *stype = get_entity_type(step);
402
403                 if (get_type_mode(stype)) {
404                         int align = (get_type_alignment_bits(stype) + 7) >> 3;
405                         int n     = size % align;
406
407                         if (n > 0) {
408                                 obstack_printf(obst, "\t.zero\t%d\n", align - n);
409                                 size += align - n;
410                         }
411                 }
412                 dump_atomic_init(obst, get_compound_ent_value(ent, i));
413                 size += get_type_size_bytes(stype);
414         }
415         filler = get_type_size_bytes(ty) - size;
416
417         if (filler > 0)
418                 obstack_printf(obst, "\t.skip\t%d\n", filler);
419 }
420
421 enum normal_or_bitfield_kind {
422         NORMAL = 0,
423         BITFIELD
424 };
425
426 typedef struct {
427         enum normal_or_bitfield_kind kind;
428         union {
429                 ir_node *value;
430                 unsigned char bf_val;
431         } v;
432 } normal_or_bitfield;
433
434 /**
435  * Dump an initializer for a compound entity.
436  */
437 static void dump_compound_init(obstack_t *obst, ir_entity *ent)
438 {
439         normal_or_bitfield *vals;
440         int i, j, n = get_compound_ent_n_values(ent);
441         int last_ofs;
442
443         /* Find the initializer size. Sorrily gcc support a nasty feature:
444            The last field of a compound may be a flexible array. This allows
445            initializers bigger than the type size. */
446         last_ofs = 0;
447         for (i = 0; i < n; ++i) {
448                 int offset = get_compound_ent_value_offset_bytes(ent, i);
449                 int bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
450                 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
451                 int path_len = get_compound_graph_path_length(path);
452                 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
453                 int value_len = get_type_size_bits(get_entity_type(last_ent));
454
455                 offset += (value_len + bits_remainder + 7) >> 3;
456
457                 if (offset > last_ofs) {
458                         last_ofs = offset;
459                 }
460         }
461
462         /*
463          * In the worst case, every initializer allocates one byte.
464          * Moreover, initializer might be big, do not allocate on stack.
465          */
466         vals = xcalloc(last_ofs, sizeof(vals[0]));
467
468         /* collect the values and store them at the offsets */
469         for (i = 0; i < n; ++i) {
470                 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
471                 int path_len = get_compound_graph_path_length(path);
472                 int offset = get_compound_ent_value_offset_bytes(ent, i);
473                 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
474                 ir_node *value = get_compound_ent_value(ent, i);
475                 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
476                 int value_len = get_type_size_bits(get_entity_type(last_ent));
477                 assert(offset >= 0);
478                 assert(offset_bits >= 0);
479
480                 if (offset_bits != 0 ||
481                         (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
482                         tarval *shift, *shifted;
483                         tarval *tv = get_atomic_init_tv(value);
484                         if (tv == NULL) {
485                                 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
486                                       get_entity_ld_name(ent));
487                         }
488                         tv = tarval_convert_to(tv, mode_Lu);
489                         shift = new_tarval_from_long(offset_bits, mode_Is);
490                         shifted = tarval_shl(tv, shift);
491                         if (shifted == tarval_bad || shifted == tarval_undefined) {
492                                 panic("Couldn't shift numeric value for bitfield initializer '%s'\n",
493                                       get_entity_ld_name(ent));
494                         }
495
496                         for (j = 0; value_len > 0; ++j) {
497                                 assert(offset + j < last_ofs);
498                                 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
499                                 vals[offset + j].kind = BITFIELD;
500                                 vals[offset + j].v.bf_val |= get_tarval_sub_bits(shifted, j);
501                                 value_len -= 8 - offset_bits;
502                                 offset_bits = 0;
503                         }
504                 } else {
505                         assert(offset < last_ofs);
506                         assert(vals[offset].kind == NORMAL);
507                         assert(vals[offset].v.value == NULL);
508                         vals[offset].v.value = value;
509                 }
510         }
511
512         /* now write them sorted */
513         for (i = 0; i < last_ofs; ) {
514                 int space = 0, skip = 0;
515                 if (vals[i].kind == NORMAL) {
516                         if(vals[i].v.value != NULL) {
517                                 dump_atomic_init(obst, vals[i].v.value);
518                                 skip = get_mode_size_bytes(get_irn_mode(vals[i].v.value)) - 1;
519                         } else {
520                                 space = 1;
521                         }
522                 } else {
523                         assert(vals[i].kind == BITFIELD);
524                         obstack_printf(obst, "\t.byte\t%d\n", vals[i].v.bf_val);
525                 }
526
527                 ++i;
528                 space = 0;
529                 while (i < last_ofs && vals[i].kind == NORMAL && vals[i].v.value == NULL) {
530                         ++space;
531                         ++i;
532                 }
533                 space -= skip;
534                 assert(space >= 0);
535
536                 /* a gap */
537                 if (space > 0)
538                         obstack_printf(obst, "\t.skip\t%d\n", space);
539         }
540         xfree(vals);
541 }
542
543 /**
544  * Dump a global entity.
545  */
546 static void dump_global(ia32_decl_env_t *env, ir_entity *ent, int emit_commons)
547 {
548         obstack_t *obst;
549         ir_type *type = get_entity_type(ent);
550         const char *ld_name = get_entity_ld_name(ent);
551         ir_variability variability = get_entity_variability(ent);
552         ir_visibility visibility = get_entity_visibility(ent);
553         int align = get_type_alignment_bytes(type);
554         int emit_as_common = 0;
555
556         obst = env->data_obst;
557         if (is_Method_type(type)) {
558                 if (get_method_img_section(ent) == section_constructors) {
559                         obst = env->ctor_obst;
560                         obstack_printf(obst, ".balign\t%d\n", align);
561                         dump_size_type(obst, align);
562                         obstack_printf(obst, "%s\n", ld_name);
563                 }
564                 return;
565         } else if (variability == variability_constant) {
566                 /* a constant entity, put it on the rdata */
567                 obst = env->rodata_obst;
568         } else if (variability == variability_uninitialized) {
569                 /* uninitialized entity put it in bss segment */
570                 obst = env->bss_obst;
571                 if(emit_commons && visibility != visibility_local)
572                         emit_as_common = 1;
573         }
574
575         be_dbg_variable(env->main_env->db_handle, obst, ent);
576
577         /* global or not global */
578         if (visibility == visibility_external_visible && !emit_as_common) {
579                 obstack_printf(obst, ".global\t%s\n", ld_name);
580         } else if(visibility == visibility_external_allocated) {
581                 obstack_printf(obst, ".global\t%s\n", ld_name);
582                 /* we can return now... */
583                 return;
584         }
585         /* alignment */
586         if (align > 1 && !emit_as_common) {
587                 obstack_printf(obst, ".balign\t%d\n", align);
588         }
589
590         if (!emit_as_common) {
591                 obstack_printf(obst, "%s:\n", ld_name);
592         }
593
594         if (variability == variability_uninitialized) {
595                 if(emit_as_common) {
596                         obstack_printf(obst, "\t.comm %s,%d,%d\n",
597                                         ld_name, get_type_size_bytes(type), align);
598                 } else {
599                         obstack_printf(obst, "\t.zero %d\n", get_type_size_bytes(type));
600                 }
601         } else if (is_atomic_type(type)) {
602                 dump_atomic_init(obst, get_atomic_ent_value(ent));
603         } else if (ent_is_string_const(ent)) {
604                 dump_string_cst(obst, ent);
605         } else if (is_Array_type(type)) {
606                 dump_array_init(obst, ent);
607         } else if (is_compound_type(type)) {
608                 dump_compound_init(obst, ent);
609         } else {
610                 assert(0 && "unsupported type");
611         }
612 }
613
614 /**
615  * Dumps declarations of global variables and the initialization code.
616  */
617 static void ia32_dump_globals(ir_type *gt, ia32_decl_env_t *env, int emit_commons)
618 {
619         int i, n = get_compound_n_members(gt);
620
621         for (i = 0; i < n; i++) {
622                 ir_entity *ent = get_compound_member(gt, i);
623                 dump_global(env, ent, emit_commons);
624         }
625 }
626
627 /************************************************************************/
628
629 void be_gas_emit_decls(be_emit_env_t *emit, const be_main_env_t *main_env) {
630         ia32_decl_env_t env;
631         obstack_t rodata, data, bss, ctor;
632         int    size;
633         char   *cp;
634
635         /* dump the global type */
636         obstack_init(&rodata);
637         obstack_init(&data);
638         obstack_init(&bss);
639         obstack_init(&ctor);
640
641         env.rodata_obst = &rodata;
642         env.data_obst   = &data;
643         env.bss_obst    = &bss;
644         env.ctor_obst   = &ctor;
645         env.main_env    = main_env;
646
647         ia32_dump_globals(get_glob_type(), &env, 1);
648
649         size = obstack_object_size(&data);
650         cp   = obstack_finish(&data);
651         if (size > 0) {
652                 be_gas_emit_switch_section(emit, GAS_SECTION_DATA);
653                 be_emit_string_len(emit, cp, size);
654                 be_emit_write_line(emit);
655         }
656
657         size = obstack_object_size(&rodata);
658         cp   = obstack_finish(&rodata);
659         if (size > 0) {
660                 be_gas_emit_switch_section(emit, GAS_SECTION_RODATA);
661                 be_emit_string_len(emit, cp, size);
662                 be_emit_write_line(emit);
663         }
664
665         size = obstack_object_size(&bss);
666         cp   = obstack_finish(&bss);
667         if (size > 0) {
668                 be_gas_emit_switch_section(emit, GAS_SECTION_COMMON);
669                 be_emit_string_len(emit, cp, size);
670                 be_emit_write_line(emit);
671         }
672
673         size = obstack_object_size(&ctor);
674         cp   = obstack_finish(&ctor);
675         if (size > 0) {
676                 be_gas_emit_switch_section(emit, GAS_SECTION_CTOR);
677                 be_emit_string_len(emit, cp, size);
678                 be_emit_write_line(emit);
679         }
680
681         obstack_free(&rodata, NULL);
682         obstack_free(&data, NULL);
683         obstack_free(&bss, NULL);
684         obstack_free(&ctor, NULL);
685
686         /* dump the Thread Local Storage */
687         obstack_init(&data);
688
689         env.rodata_obst = &data;
690         env.data_obst   = &data;
691         env.bss_obst    = &data;
692         env.ctor_obst   = NULL;
693
694         ia32_dump_globals(get_tls_type(), &env, 0);
695
696         size = obstack_object_size(&data);
697         cp   = obstack_finish(&data);
698         if (size > 0) {
699                 be_gas_emit_switch_section(emit, GAS_SECTION_TLS);
700                 be_emit_cstring(emit, ".balign\t32\n");
701                 be_emit_write_line(emit);
702                 be_emit_string_len(emit, cp, size);
703                 be_emit_write_line(emit);
704         }
705
706         obstack_free(&data, NULL);
707 }