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