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