add support for stabs debug info
[libfirm] / ir / be / ia32 / ia32_gen_decls.c
1 /**
2  * Dumps global variables and constants as ia32 assembler.
3  * @author Christian Wuerdig
4  * @date 04.11.2005
5  * @version $Id$
6  */
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <assert.h>
12
13 #include "obst.h"
14 #include "tv.h"
15 #include "irnode.h"
16 #include "entity.h"
17 #include "irprog.h"
18
19 #include "../be.h"
20
21 #include "ia32_emitter.h"
22 #include "ia32_gen_decls.h"
23
24 typedef struct obstack obstack_t;
25
26 typedef struct _ia32_decl_env {
27         obstack_t *rodata_obst;
28         obstack_t *data_obst;
29         obstack_t *comm_obst;
30         obstack_t *ctor_obst;
31         const be_main_env_t *main_env;
32 } ia32_decl_env_t;
33
34 /************************************************************************/
35
36 /*
37  * returns the highest bit value
38  */
39 static unsigned highest_bit(unsigned v)
40 {
41         int res = -1;
42
43         if (v >= (1U << 16U)) {
44                 res += 16;
45                 v >>= 16;
46         }
47         if (v >= (1U << 8U)) {
48                 res += 8;
49                 v >>= 8;
50         }
51         if (v >= (1U << 4U)) {
52                 res += 4;
53                 v >>= 4;
54         }
55         if (v >= (1U << 2U)) {
56                 res += 2;
57                 v >>= 2;
58         }
59         if (v >= (1U << 1U)) {
60                 res += 1;
61                 v >>= 1;
62         }
63         if (v >= 1)
64                 res += 1;
65
66         return res;
67 }
68
69 static void ia32_dump_comm(obstack_t *obst, const char *name, visibility vis, int size, int align) {
70         switch (asm_flavour) {
71         case ASM_LINUX_GAS:
72                 if (vis == visibility_local)
73                         obstack_printf(obst, "\t.local\t%s\n", name);
74                 obstack_printf(obst, "\t.comm\t%s,%d,%d\n", name, size, align);
75                 break;
76         case ASM_MINGW_GAS:
77                 if (vis == visibility_local)
78                         obstack_printf(obst, "\t.lcomm\t%s,%d\n", name, size);
79                 else
80                         obstack_printf(obst, "\t.comm\t%s,%d\n", name, size);
81                 break;
82         default:
83                 break;
84         }
85 }
86
87 /**
88  * output the alignment to an obstack
89  */
90 static void ia32_dump_align(obstack_t *obst, int align)
91 {
92         int h = highest_bit(align);
93
94         if ((1 << h) < align)
95                 ++h;
96         align = (1 << h);
97
98         if (align > 1)
99                 obstack_printf(obst, "\t.align %d\n", align);
100 }
101
102 /**
103  * output the alignment to a FILE
104  */
105 static void ia32_dump_align_f(FILE *f, int align)
106 {
107         int h = highest_bit(align);
108
109         if ((1 << h) < align)
110                 ++h;
111         align = (1 << h);
112
113         if (align > 1)
114                 fprintf(f, "\t.align %d\n", align);
115 }
116
117 /**
118  * output a tarval
119  */
120 static void dump_arith_tarval(obstack_t *obst, tarval *tv, int bytes)
121 {
122         switch (bytes) {
123
124         case 1:
125                 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
126                 break;
127
128         case 2:
129                 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
130                 break;
131
132         case 4:
133                 obstack_printf(obst, "0x%02x%02x%02x%02x",
134                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
135                 break;
136
137         case 8:
138                 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
139                         get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
140                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
141                 break;
142
143         case 10:
144         case 12:
145                 break;
146
147         default:
148                 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
149                 assert(0);
150         }
151 }
152
153 /*
154  * dump an atomic value
155  */
156 static void do_dump_atomic_init(obstack_t *obst, ir_node *init)
157 {
158         ir_mode *mode = get_irn_mode(init);
159         int bytes     = get_mode_size_bytes(mode);
160         tarval *tv;
161
162         switch (get_irn_opcode(init)) {
163
164         case iro_Cast:
165                 do_dump_atomic_init(obst, get_Cast_op(init));
166                 return;
167
168         case iro_Conv:
169                 do_dump_atomic_init(obst, get_Conv_op(init));
170                 return;
171
172         case iro_Const:
173                 tv = get_Const_tarval(init);
174
175                 /* beware of old stuff */
176                 //assert(! mode_is_reference(mode));
177
178                 /* it's a arithmetic value */
179                 dump_arith_tarval(obst, tv, bytes);
180                 return;
181
182         case iro_SymConst:
183                 switch (get_SymConst_kind(init)) {
184                 case symconst_addr_name:
185                         obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
186                         break;
187
188                 case symconst_addr_ent:
189                         obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
190                         break;
191
192                 case symconst_type_size:
193                         obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
194                         break;
195
196                 case symconst_type_align:
197                         obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
198                         break;
199
200                 case symconst_enum_const:
201                         tv = get_enumeration_value(get_SymConst_enum(init));
202                         dump_arith_tarval(obst, tv, bytes);
203                         break;
204
205                 default:
206                         assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
207                 }
208                 return;
209
210                 case iro_Add:
211                         do_dump_atomic_init(obst, get_Add_left(init));
212                         obstack_printf(obst, " + ");
213                         do_dump_atomic_init(obst, get_Add_right(init));
214                         return;
215
216                 case iro_Sub:
217                         do_dump_atomic_init(obst, get_Sub_left(init));
218                         obstack_printf(obst, " - ");
219                         do_dump_atomic_init(obst, get_Sub_right(init));
220                         return;
221
222                 case iro_Mul:
223                         do_dump_atomic_init(obst, get_Mul_left(init));
224                         obstack_printf(obst, " * ");
225                         do_dump_atomic_init(obst, get_Mul_right(init));
226                         return;
227
228                 default:
229                         assert(0 && "dump_atomic_init(): unknown IR-node");
230         }
231 }
232
233 /*
234  * dumps the type for given size (.byte, .long, ...)
235  */
236 static void dump_size_type(obstack_t *obst, int size) {
237         switch (size) {
238
239         case 1:
240                 obstack_printf(obst, "\t.byte\t");
241                 break;
242
243         case 2:
244                 obstack_printf(obst, "\t.value\t");
245                 break;
246
247         case 4:
248                 obstack_printf(obst, "\t.long\t");
249                 break;
250
251         case 8:
252                 obstack_printf(obst, "\t.quad\t");
253                 break;
254
255         case 10:
256         case 12:
257                 /* handled in arith */
258                 break;
259
260         default:
261                 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
262                 assert(0);
263         }
264 }
265
266 /*
267  * dump an atomic value
268  */
269 static void dump_atomic_init(obstack_t *obst, ir_node *init)
270 {
271         ir_mode *mode = get_irn_mode(init);
272         int bytes     = get_mode_size_bytes(mode);
273
274         dump_size_type(obst, bytes);
275         do_dump_atomic_init(obst, init);
276         obstack_printf(obst, "\n");
277 }
278
279 /************************************************************************/
280 /* Routines to dump global variables                                    */
281 /************************************************************************/
282
283 /**
284  * Determine if an entity is a string constant
285  * @param ent The entity
286  * @return 1 if it is a string constant, 0 otherwise
287  */
288 static int ent_is_string_const(entity *ent)
289 {
290         int res = 0;
291         ir_type *ty;
292
293         ty = get_entity_type(ent);
294
295         /* if it's an array */
296         if (is_Array_type(ty)) {
297                 ir_type *elm_ty = get_array_element_type(ty);
298
299                 /* and the array's element type is primitive */
300                 if (is_Primitive_type(elm_ty)) {
301                         ir_mode *mode = get_type_mode(elm_ty);
302
303                         /*
304                         * and the mode of the element type is an int of
305                         * the same size as the byte mode
306                         */
307                         if (mode_is_int(mode)
308                                 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
309                         {
310                                 int i, c, n;
311
312                                 n = get_compound_ent_n_values(ent);
313                                 for (i = 0; i < n; ++i) {
314                                         ir_node *irn = get_compound_ent_value(ent, i);
315                                         if(get_irn_opcode(irn) != iro_Const)
316                                                 return 0;
317
318                                         c = (int) get_tarval_long(get_Const_tarval(irn));
319
320                                         if((i < n - 1 && !(isgraph(c) || isspace(c)))
321                                                 || (i == n - 1 && c != '\0'))
322                                                 return 0;
323                                 }
324
325                                 res = 1;
326                         }
327                 }
328         }
329
330         return res;
331 }
332
333 /**
334  * Dump a atring constant.
335  * No checks are made!!
336  * @param obst The obst to dump on.
337  * @param ent The entity to dump.
338  */
339 static void dump_string_cst(obstack_t *obst, entity *ent)
340 {
341         int i, n;
342
343         obstack_printf(obst, "\t.string \"");
344         n = get_compound_ent_n_values(ent);
345
346         for (i = 0; i < n-1; ++i) {
347                 ir_node *irn;
348                 int c;
349
350                 irn = get_compound_ent_value(ent, i);
351                 c = (int) get_tarval_long(get_Const_tarval(irn));
352
353                 switch (c) {
354                 case '"' : obstack_printf(obst, "\\\""); break;
355                 case '\n': obstack_printf(obst, "\\n"); break;
356                 case '\r': obstack_printf(obst, "\\r"); break;
357                 case '\t': obstack_printf(obst, "\\t"); break;
358                 case '\\': obstack_printf(obst, "\\\\"); break;
359                 default  :
360                         if (isprint(c))
361                                 obstack_printf(obst, "%c", c);
362                         else
363                                 obstack_printf(obst, "%O", c);
364                         break;
365                 }
366         }
367         obstack_printf(obst, "\"\n");
368 }
369
370 struct arr_info {
371         int n_elems;
372         int visit_cnt;
373         int size;
374 };
375
376 /**
377  * Dump the size of an object
378  */
379 static void dump_object_size(obstack_t *obst, const char *name, int size) {
380         switch (asm_flavour) {
381         case ASM_LINUX_GAS:
382                 obstack_printf(obst, "\t.type\t%s,@object\n", name);
383                 obstack_printf(obst, "\t.size\t%s,%d\n", name, size);
384                 break;
385         default:
386                 break;
387         }
388 }
389
390 /*
391  * Dumps the initialization of global variables that are not
392  * "uninitialized".
393  */
394 static void dump_global(const be_main_env_t *main_env,
395                                                 obstack_t *rdata_obstack, obstack_t *data_obstack,
396                                                 obstack_t *comm_obstack, obstack_t *ctor_obstack,
397                                                 entity *ent)
398 {
399         ir_type *ty         = get_entity_type(ent);
400         const char *ld_name = get_entity_ld_name(ent);
401         obstack_t *obst     = data_obstack;
402         int align, h;
403         const arch_env_t *arch_env = main_env->arch_env;
404
405         /*
406          * FIXME: did NOT work for partly constant values
407          */
408         if (! is_Method_type(ty)) {
409                 ent_variability variability = get_entity_variability(ent);
410                 visibility visibility = get_entity_visibility(ent);
411
412                 if (variability == variability_constant) {
413                         /* a constant entity, put it on the rdata */
414                         obst = rdata_obstack;
415                 }
416
417                 /* check, whether it is initialized, if yes create data */
418                 if (variability != variability_uninitialized) {
419                         be_dbg_global(main_env->db_handle, obst, ent);
420
421                         if (visibility == visibility_external_visible) {
422                                 obstack_printf(obst, ".globl\t%s\n", ld_name);
423                         }
424                         dump_object_size(obst, ld_name, get_type_size_bytes(ty));
425
426                         align = get_type_alignment_bytes(ty);
427                         ia32_dump_align(obst, align);
428
429                         obstack_printf(obst, "%s:\n", ld_name);
430
431                         if (is_atomic_type(ty)) {
432                                 if (get_entity_visibility(ent) != visibility_external_allocated)
433                                         dump_atomic_init(obst, get_atomic_ent_value(ent));
434                         }
435                         else {
436                                 int i, size = 0;
437
438                                 if (ent_is_string_const(ent)) {
439                                         dump_string_cst(obst, ent);
440                                 }
441                                 else if (is_Array_type(ty)) {
442                                         int filler;
443
444                                         /* potential spare values should be already included! */
445                                         for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
446                                                 entity *step = get_compound_ent_value_member(ent, i);
447                                                 ir_type *stype = get_entity_type(step);
448
449                                                 if (get_type_mode(stype)) {
450                                                         int align = (get_type_alignment_bits(stype) + 7) >> 3;
451                                                         int n     = size % align;
452
453                                                         if (n > 0) {
454                                                                 obstack_printf(obst, "\t.zero\t%d\n", align - n);
455                                                                 size += align - n;
456                                                         }
457                                                 }
458                                                 dump_atomic_init(obst, get_compound_ent_value(ent, i));
459                                                 size += get_type_size_bytes(stype);
460                                         }
461                                         filler = get_type_size_bytes(ty) - size;
462
463                                         if (filler > 0)
464                                                 obstack_printf(obst, "\t.zero\t%d\n", filler);
465                                 }
466                                 else if (is_compound_type(ty)) {
467                                         ir_node **vals;
468                                         int type_size, j;
469
470                                         /* Compound entities are NOT sorted.
471                                          * The sorting strategy used doesn't work for `value' compound fields nor
472                                          * for partially_constant entities.
473                                          */
474
475                                         /*
476                                          * in the worst case, every entity allocates one byte, so the type
477                                          * size should be equal or bigger the number of fields
478                                          */
479                                         type_size = get_type_size_bytes(ty);
480                                         vals      = xcalloc(type_size, sizeof(*vals));
481
482                                         /* collect the values and store them at the offsets */
483                                         for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
484                                                 int                 graph_length, aipos, offset;
485                                                 struct arr_info     *ai;
486                                                 int                 all_n = 1;
487                                                 compound_graph_path *path = get_compound_ent_value_path(ent, i);
488
489                                                 /* get the access path to the costant value */
490                                                 graph_length = get_compound_graph_path_length(path);
491                                                 ai = xcalloc(graph_length, sizeof(struct arr_info));
492
493                                                 /* We wanna know how many arrays are on the path to the entity. We also have to know how
494                                                  * many elements each array holds to calculate the offset for the entity. */
495                                                 for (j = 0; j < graph_length; j++) {
496                                                         entity  *step      = get_compound_graph_path_node(path, j);
497                                                         ir_type *step_type = get_entity_type(step);
498                                                         int     ty_size    = (get_type_size_bits(step_type) + 7) >> 3;
499                                                         int     k, n       = 0;
500
501                                                         if (is_Array_type(step_type))
502                                                                 for (k = 0; k < get_array_n_dimensions(step_type); k++)
503                                                                         n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
504                                                                 if (n) all_n *= n;
505                                                                 ai[j].n_elems = n ? all_n + 1 : 0;
506                                                                 ai[j].visit_cnt = 0;
507                                                                 ai[j].size = ty_size;
508                                                 }
509
510                                                 aipos = graph_length - 1;
511                                                 if (aipos) aipos--;
512
513                                                 for (offset = j = 0; j < graph_length; j++) {
514                                                         entity *step       = get_compound_graph_path_node(path, j);
515                                                         ir_type *step_type = get_entity_type(step);
516                                                         int ent_ofs        = get_entity_offset_bytes(step);
517                                                         int stepsize       = 0;
518
519                                                         /* add all positive offsets (= offsets in structs) */
520                                                         if (ent_ofs >= 0) offset += ent_ofs;
521
522                                                         if (j == graph_length - 1) {
523                                                                 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
524
525                                                                 /* Search the next free position in vals depending on the information from above (ai). */
526                                                                 while (vals[offset] && aipos >= 0) {
527                                                                         if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
528                                                                                 offset += stepsize;
529                                                                                 ai[aipos].visit_cnt++;
530                                                                         }
531                                                                         else
532                                                                                 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
533                                                                                         stepsize = ai[aipos--].size;
534                                                                                         offset  += stepsize;
535                                                                                 }
536                                                                 }
537
538                                                                 assert(aipos >= 0 && "couldn't store entity");
539                                                                 vals[offset] = get_compound_ent_value(ent, i);
540                                                         }
541                                                 }
542
543                                                 free(ai);
544                                         }
545
546                                         /* now write them sorted */
547                                         for(i = 0; i < type_size; ) {
548                                                 if (vals[i]) {
549                                                         dump_atomic_init(obst, vals[i]);
550                                                         i += (get_mode_size_bytes(get_irn_mode(vals[i])));
551                                                 }
552                                                 else {
553                                                         /* a gap */
554                                                         obstack_printf(obst, "\t.byte\t0\n");
555                                                         ++i;
556                                                 }
557                                         }
558                                         free(vals);
559                                 }
560                                 else {
561                                         assert(0 && "unsupported type");
562                                 }
563                         }
564                         obstack_printf(obst, "\n");
565                 }
566                 else if (visibility != visibility_external_allocated) {
567                         be_dbg_global(main_env->db_handle, comm_obstack, ent);
568
569                         /* uninitialized and NOT external */
570                         if (get_entity_owner(ent) != get_tls_type()) {
571                                 /* calculate the alignment */
572                                 align = get_type_alignment_bytes(ty);
573                                 h = highest_bit(align);
574
575                                 if ((1 << h) < align)
576                                         ++h;
577                                 align = (1 << h);
578
579                                 if (align < 1)
580                                         align = 1;
581
582                                 ia32_dump_comm(comm_obstack, ld_name, visibility,
583                                         get_type_size_bytes(ty), align);
584                         } else {
585                                 /* TLS */
586                                 if (visibility == visibility_external_visible) {
587                                         obstack_printf(comm_obstack, ".globl\t%s\n", ld_name);
588                                 }
589                                 dump_object_size(comm_obstack, ld_name, get_type_size_bytes(ty));
590                                 align = get_type_alignment_bytes(ty);
591                                 ia32_dump_align(comm_obstack, align);
592                                 obstack_printf(comm_obstack, "%s:\n\t.zero %d\n", ld_name, get_type_size_bytes(ty));
593                         }
594                 }
595         } /* ! is method type */
596         else if (ctor_obstack && get_method_img_section(ent) == section_constructors) {
597                 ia32_dump_align(ctor_obstack, get_type_alignment_bytes(ty));
598                 dump_size_type(ctor_obstack, get_type_alignment_bytes(ty));
599                 obstack_printf(ctor_obstack, "%s\n", ld_name);
600         }
601 }
602
603 /**
604  * Dumps declarations of global variables and the initialization code.
605  */
606 static void ia32_dump_globals(ir_type *gt, ia32_decl_env_t *env)
607 {
608         int i, n = get_compound_n_members(gt);
609
610         for (i = 0; i < n; i++)
611                 dump_global(env->main_env, env->rodata_obst, env->data_obst, env->comm_obst, env->ctor_obst,
612                         get_compound_member(gt, i));
613 }
614
615 /************************************************************************/
616
617 void ia32_gen_decls(FILE *out, const be_main_env_t *main_env) {
618         ia32_decl_env_t env;
619         obstack_t rodata, data, comm, ctor;
620         int    size;
621         char   *cp;
622
623         /* dump the global type */
624         obstack_init(&rodata);
625         obstack_init(&data);
626         obstack_init(&comm);
627
628         if (main_env->options->opt_profile)
629                 obstack_init(&ctor);
630
631         env.rodata_obst = &rodata;
632         env.data_obst   = &data;
633         env.comm_obst   = &comm;
634         env.ctor_obst   = main_env->options->opt_profile ? &ctor : NULL;
635         env.main_env    = main_env;
636
637         ia32_dump_globals(get_glob_type(), &env);
638
639         size = obstack_object_size(&data);
640         cp   = obstack_finish(&data);
641         if (size > 0) {
642                 ia32_switch_section(out, SECTION_DATA);
643                 fwrite(cp, 1, size, out);
644         }
645
646         size = obstack_object_size(&rodata);
647         cp   = obstack_finish(&rodata);
648         if (size > 0) {
649                 ia32_switch_section(out, SECTION_RODATA);
650                 fwrite(cp, 1, size, out);
651         }
652
653         size = obstack_object_size(&comm);
654         cp   = obstack_finish(&comm);
655         if (size > 0) {
656                 ia32_switch_section(out, SECTION_COMMON);
657                 fwrite(cp, 1, size, out);
658         }
659
660         if (main_env->options->opt_profile) {
661                 size = obstack_object_size(&ctor);
662                 cp   = obstack_finish(&ctor);
663                 if (size > 0) {
664                         ia32_switch_section(out, SECTION_CTOR);
665                         fwrite(cp, 1, size, out);
666                 }
667                 obstack_free(&ctor, NULL);
668         }
669
670         obstack_free(&rodata, NULL);
671         obstack_free(&data, NULL);
672         obstack_free(&comm, NULL);
673
674         /* dump the Thread Local Storage */
675         obstack_init(&data);
676
677         env.rodata_obst = &data;
678         env.data_obst   = &data;
679         env.comm_obst   = &data;
680         env.ctor_obst   = NULL;
681
682         ia32_dump_globals(get_tls_type(), &env);
683
684         size = obstack_object_size(&data);
685         cp   = obstack_finish(&data);
686         if (size > 0) {
687                 ia32_switch_section(out, SECTION_TLS);
688                 ia32_dump_align_f(out, 32);
689                 fwrite(cp, 1, size, out);
690         }
691
692         obstack_free(&data, NULL);
693 }