added support for static variables
[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, ir_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
404         /*
405          * FIXME: did NOT work for partly constant values
406          */
407         if (! is_Method_type(ty)) {
408                 ir_variability variability = get_entity_variability(ent);
409                 ir_visibility visibility = get_entity_visibility(ent);
410
411                 if (variability == variability_constant) {
412                         /* a constant entity, put it on the rdata */
413                         obst = rdata_obstack;
414                 }
415
416                 /* check, whether it is initialized, if yes create data */
417                 if (variability != variability_uninitialized) {
418                         be_dbg_variable(main_env->db_handle, obst, ent);
419
420                         if (visibility == visibility_external_visible) {
421                                 obstack_printf(obst, ".globl\t%s\n", ld_name);
422                         }
423                         dump_object_size(obst, ld_name, get_type_size_bytes(ty));
424
425                         align = get_type_alignment_bytes(ty);
426                         ia32_dump_align(obst, align);
427
428                         obstack_printf(obst, "%s:\n", ld_name);
429
430                         if (is_atomic_type(ty)) {
431                                 if (get_entity_visibility(ent) != visibility_external_allocated)
432                                         dump_atomic_init(obst, get_atomic_ent_value(ent));
433                         }
434                         else {
435                                 int i, size = 0;
436
437                                 if (ent_is_string_const(ent)) {
438                                         dump_string_cst(obst, ent);
439                                 }
440                                 else if (is_Array_type(ty)) {
441                                         int filler;
442
443                                         /* potential spare values should be already included! */
444                                         for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
445                                                 entity *step = get_compound_ent_value_member(ent, i);
446                                                 ir_type *stype = get_entity_type(step);
447
448                                                 if (get_type_mode(stype)) {
449                                                         int align = (get_type_alignment_bits(stype) + 7) >> 3;
450                                                         int n     = size % align;
451
452                                                         if (n > 0) {
453                                                                 obstack_printf(obst, "\t.zero\t%d\n", align - n);
454                                                                 size += align - n;
455                                                         }
456                                                 }
457                                                 dump_atomic_init(obst, get_compound_ent_value(ent, i));
458                                                 size += get_type_size_bytes(stype);
459                                         }
460                                         filler = get_type_size_bytes(ty) - size;
461
462                                         if (filler > 0)
463                                                 obstack_printf(obst, "\t.zero\t%d\n", filler);
464                                 }
465                                 else if (is_compound_type(ty)) {
466                                         ir_node **vals;
467                                         int type_size, j;
468
469                                         /* Compound entities are NOT sorted.
470                                          * The sorting strategy used doesn't work for `value' compound fields nor
471                                          * for partially_constant entities.
472                                          */
473
474                                         /*
475                                          * in the worst case, every entity allocates one byte, so the type
476                                          * size should be equal or bigger the number of fields
477                                          */
478                                         type_size = get_type_size_bytes(ty);
479                                         vals      = xcalloc(type_size, sizeof(*vals));
480
481                                         /* collect the values and store them at the offsets */
482                                         for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
483                                                 int                 graph_length, aipos, offset;
484                                                 struct arr_info     *ai;
485                                                 int                 all_n = 1;
486                                                 compound_graph_path *path = get_compound_ent_value_path(ent, i);
487
488                                                 /* get the access path to the costant value */
489                                                 graph_length = get_compound_graph_path_length(path);
490                                                 ai = xcalloc(graph_length, sizeof(struct arr_info));
491
492                                                 /* We wanna know how many arrays are on the path to the entity. We also have to know how
493                                                  * many elements each array holds to calculate the offset for the entity. */
494                                                 for (j = 0; j < graph_length; j++) {
495                                                         entity  *step      = get_compound_graph_path_node(path, j);
496                                                         ir_type *step_type = get_entity_type(step);
497                                                         int     ty_size    = (get_type_size_bits(step_type) + 7) >> 3;
498                                                         int     k, n       = 0;
499
500                                                         if (is_Array_type(step_type))
501                                                                 for (k = 0; k < get_array_n_dimensions(step_type); k++)
502                                                                         n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
503                                                                 if (n) all_n *= n;
504                                                                 ai[j].n_elems = n ? all_n + 1 : 0;
505                                                                 ai[j].visit_cnt = 0;
506                                                                 ai[j].size = ty_size;
507                                                 }
508
509                                                 aipos = graph_length - 1;
510                                                 if (aipos) aipos--;
511
512                                                 for (offset = j = 0; j < graph_length; j++) {
513                                                         entity *step       = get_compound_graph_path_node(path, j);
514                                                         ir_type *step_type = get_entity_type(step);
515                                                         int ent_ofs        = get_entity_offset_bytes(step);
516                                                         int stepsize       = 0;
517
518                                                         /* add all positive offsets (= offsets in structs) */
519                                                         if (ent_ofs >= 0) offset += ent_ofs;
520
521                                                         if (j == graph_length - 1) {
522                                                                 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
523
524                                                                 /* Search the next free position in vals depending on the information from above (ai). */
525                                                                 while (vals[offset] && aipos >= 0) {
526                                                                         if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
527                                                                                 offset += stepsize;
528                                                                                 ai[aipos].visit_cnt++;
529                                                                         }
530                                                                         else
531                                                                                 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
532                                                                                         stepsize = ai[aipos--].size;
533                                                                                         offset  += stepsize;
534                                                                                 }
535                                                                 }
536
537                                                                 assert(aipos >= 0 && "couldn't store entity");
538                                                                 vals[offset] = get_compound_ent_value(ent, i);
539                                                         }
540                                                 }
541
542                                                 free(ai);
543                                         }
544
545                                         /* now write them sorted */
546                                         for(i = 0; i < type_size; ) {
547                                                 if (vals[i]) {
548                                                         dump_atomic_init(obst, vals[i]);
549                                                         i += (get_mode_size_bytes(get_irn_mode(vals[i])));
550                                                 }
551                                                 else {
552                                                         /* a gap */
553                                                         obstack_printf(obst, "\t.byte\t0\n");
554                                                         ++i;
555                                                 }
556                                         }
557                                         free(vals);
558                                 }
559                                 else {
560                                         assert(0 && "unsupported type");
561                                 }
562                         }
563                         obstack_printf(obst, "\n");
564                 }
565                 else if (visibility != visibility_external_allocated) {
566                         be_dbg_variable(main_env->db_handle, comm_obstack, ent);
567
568                         /* uninitialized and NOT external */
569                         if (get_entity_owner(ent) != get_tls_type()) {
570                                 /* calculate the alignment */
571                                 align = get_type_alignment_bytes(ty);
572                                 h = highest_bit(align);
573
574                                 if ((1 << h) < align)
575                                         ++h;
576                                 align = (1 << h);
577
578                                 if (align < 1)
579                                         align = 1;
580
581                                 ia32_dump_comm(comm_obstack, ld_name, visibility,
582                                         get_type_size_bytes(ty), align);
583                         } else {
584                                 /* TLS */
585                                 if (visibility == visibility_external_visible) {
586                                         obstack_printf(comm_obstack, ".globl\t%s\n", ld_name);
587                                 }
588                                 dump_object_size(comm_obstack, ld_name, get_type_size_bytes(ty));
589                                 align = get_type_alignment_bytes(ty);
590                                 ia32_dump_align(comm_obstack, align);
591                                 obstack_printf(comm_obstack, "%s:\n\t.zero %d\n", ld_name, get_type_size_bytes(ty));
592                         }
593                 }
594         } /* ! is method type */
595         else if (ctor_obstack && get_method_img_section(ent) == section_constructors) {
596                 ia32_dump_align(ctor_obstack, get_type_alignment_bytes(ty));
597                 dump_size_type(ctor_obstack, get_type_alignment_bytes(ty));
598                 obstack_printf(ctor_obstack, "%s\n", ld_name);
599         }
600 }
601
602 /**
603  * Dumps declarations of global variables and the initialization code.
604  */
605 static void ia32_dump_globals(ir_type *gt, ia32_decl_env_t *env)
606 {
607         int i, n = get_compound_n_members(gt);
608
609         for (i = 0; i < n; i++)
610                 dump_global(env->main_env, env->rodata_obst, env->data_obst, env->comm_obst, env->ctor_obst,
611                         get_compound_member(gt, i));
612 }
613
614 /************************************************************************/
615
616 void ia32_gen_decls(FILE *out, const be_main_env_t *main_env) {
617         ia32_decl_env_t env;
618         obstack_t rodata, data, comm, ctor;
619         int    size;
620         char   *cp;
621
622         /* dump the global type */
623         obstack_init(&rodata);
624         obstack_init(&data);
625         obstack_init(&comm);
626
627         if (main_env->options->opt_profile)
628                 obstack_init(&ctor);
629
630         env.rodata_obst = &rodata;
631         env.data_obst   = &data;
632         env.comm_obst   = &comm;
633         env.ctor_obst   = main_env->options->opt_profile ? &ctor : NULL;
634         env.main_env    = main_env;
635
636         ia32_dump_globals(get_glob_type(), &env);
637
638         size = obstack_object_size(&data);
639         cp   = obstack_finish(&data);
640         if (size > 0) {
641                 ia32_switch_section(out, SECTION_DATA);
642                 fwrite(cp, 1, size, out);
643         }
644
645         size = obstack_object_size(&rodata);
646         cp   = obstack_finish(&rodata);
647         if (size > 0) {
648                 ia32_switch_section(out, SECTION_RODATA);
649                 fwrite(cp, 1, size, out);
650         }
651
652         size = obstack_object_size(&comm);
653         cp   = obstack_finish(&comm);
654         if (size > 0) {
655                 ia32_switch_section(out, SECTION_COMMON);
656                 fwrite(cp, 1, size, out);
657         }
658
659         if (main_env->options->opt_profile) {
660                 size = obstack_object_size(&ctor);
661                 cp   = obstack_finish(&ctor);
662                 if (size > 0) {
663                         ia32_switch_section(out, SECTION_CTOR);
664                         fwrite(cp, 1, size, out);
665                 }
666                 obstack_free(&ctor, NULL);
667         }
668
669         obstack_free(&rodata, NULL);
670         obstack_free(&data, NULL);
671         obstack_free(&comm, NULL);
672
673         /* dump the Thread Local Storage */
674         obstack_init(&data);
675
676         env.rodata_obst = &data;
677         env.data_obst   = &data;
678         env.comm_obst   = &data;
679         env.ctor_obst   = NULL;
680
681         ia32_dump_globals(get_tls_type(), &env);
682
683         size = obstack_object_size(&data);
684         cp   = obstack_finish(&data);
685         if (size > 0) {
686                 ia32_switch_section(out, SECTION_TLS);
687                 ia32_dump_align_f(out, 32);
688                 fwrite(cp, 1, size, out);
689         }
690
691         obstack_free(&data, NULL);
692 }