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