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