Big Changes:
[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_size:
181       obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
182       break;
183
184     default:
185       assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
186     }
187     return;
188
189   case iro_Add:
190     do_dump_atomic_init(obst, get_Add_left(init));
191     obstack_printf(obst, " + ");
192     do_dump_atomic_init(obst, get_Add_right(init));
193     return;
194
195   case iro_Sub:
196     do_dump_atomic_init(obst, get_Sub_left(init));
197     obstack_printf(obst, " - ");
198     do_dump_atomic_init(obst, get_Sub_right(init));
199     return;
200
201   case iro_Mul:
202     do_dump_atomic_init(obst, get_Mul_left(init));
203     obstack_printf(obst, " * ");
204     do_dump_atomic_init(obst, get_Mul_right(init));
205     return;
206
207   default:
208     assert(0 && "dump_atomic_init(): unknown IR-node");
209   }
210 }
211
212 /*
213  * dump an atomic value
214  */
215 static void dump_atomic_init(struct obstack *obst, ir_node *init)
216 {
217   ir_mode *mode = get_irn_mode(init);
218   int bytes     = get_mode_size_bytes(mode);
219
220   switch (bytes) {
221
222   case 1:
223     obstack_printf(obst, "\t.byte\t");
224     break;
225
226   case 2:
227     obstack_printf(obst, "\t.value\t");
228     break;
229
230   case 4:
231     obstack_printf(obst, "\t.long\t");
232     break;
233
234   case 8:
235     obstack_printf(obst, "\t.quad\t");
236     break;
237
238   case 10:
239   case 12:
240     /* handled in arith */
241     break;
242
243   default:
244     fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
245     assert(0);
246   }
247
248   do_dump_atomic_init(obst, init);
249   obstack_printf(obst, "\n");
250 }
251
252 /************************************************************************/
253 /* Routines to dump global variables                                    */
254 /************************************************************************/
255
256 /**
257  * Determine if an entity is a string constant
258  * @param ent The entity
259  * @return 1 if it is a string constant, 0 otherwise
260  */
261 static int ent_is_string_const(entity *ent)
262 {
263   int res = 0;
264   ir_type *ty;
265
266   ty = get_entity_type(ent);
267
268   /* if it's an array */
269   if (is_Array_type(ty)) {
270     ir_type *elm_ty = get_array_element_type(ty);
271
272     /* and the array's element type is primitive */
273     if (is_Primitive_type(elm_ty)) {
274       ir_mode *mode = get_type_mode(elm_ty);
275
276       /*
277        * and the mode of the element type is an int of
278        * the same size as the byte mode
279        */
280       if (mode_is_int(mode)
281          && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
282       {
283         int i, c, n;
284
285         n = get_compound_ent_n_values(ent);
286         for (i = 0; i < n; ++i) {
287           ir_node *irn = get_compound_ent_value(ent, i);
288           if(get_irn_opcode(irn) != iro_Const)
289             return 0;
290
291           c = (int) get_tarval_long(get_Const_tarval(irn));
292
293           if((i < n - 1 && !(isgraph(c) || isspace(c)))
294              || (i == n - 1 && c != '\0'))
295             return 0;
296         }
297
298         res = 1;
299       }
300     }
301   }
302
303   return res;
304 }
305
306 /**
307  * Dump a atring constant.
308  * No checks are made!!
309  * @param obst The obst to dump on.
310  * @param ent The entity to dump.
311  */
312 static void dump_string_cst(struct obstack *obst, entity *ent)
313 {
314   int i, n;
315
316   obstack_printf(obst, "\t.string \"");
317   n = get_compound_ent_n_values(ent);
318
319   for (i = 0; i < n-1; ++i) {
320     ir_node *irn;
321     int c;
322
323     irn = get_compound_ent_value(ent, i);
324     c = (int) get_tarval_long(get_Const_tarval(irn));
325
326     switch (c) {
327     case '"' : obstack_printf(obst, "\\\""); break;
328     case '\n': obstack_printf(obst, "\\n"); break;
329     case '\r': obstack_printf(obst, "\\r"); break;
330     case '\t': obstack_printf(obst, "\\t"); break;
331     default  :
332       if (isprint(c))
333         obstack_printf(obst, "%c", c);
334       else
335         obstack_printf(obst, "%O", c);
336       break;
337     }
338   }
339   obstack_printf(obst, "\"\n");
340 }
341
342 struct arr_info {
343   int n_elems;
344   int visit_cnt;
345   int size;
346 };
347
348 /*
349  * Dumps the initialization of global variables that are not
350  * "uninitialized".
351  */
352 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
353 {
354   ir_type *ty         = get_entity_type(ent);
355   const char *ld_name = get_entity_ld_name(ent);
356   int align, h;
357   struct obstack *obst = data_obstack;
358
359   /*
360    * FIXME: did NOT work for partly constant values
361    */
362   if (! is_Method_type(ty)) {
363     ent_variability variability = get_entity_variability(ent);
364     visibility visibility = get_entity_visibility(ent);
365
366     if (variability == variability_constant) {
367       /* a constant entity, put it on the rdata */
368       obst = rdata_obstack;
369     }
370
371     /* check, wether it is initialized, if yes create data */
372     if (variability != variability_uninitialized) {
373       if (visibility == visibility_external_visible) {
374         obstack_printf(obst, ".globl\t%s\n", ld_name);
375       }
376       obstack_printf(obst, "\t.type\t%s,%%object\n", ld_name);
377       obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
378
379       align = get_type_alignment_bytes(ty);
380       arm_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]) {
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       if (visibility == visibility_local) {
521         obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
522       }
523
524       /* calculate the alignment */
525       align = get_type_alignment_bytes(ty);
526       h = highest_bit(align);
527
528       if ((1 << h) < align)
529         ++h;
530       align = (1 << h);
531
532       if (align < 1)
533         align = 1;
534
535       obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
536     }
537   }
538 }
539
540 /*
541  * Dumps declarations of global variables and the initialization code.
542  */
543 void arm_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
544 {
545   ir_type *gt = get_glob_type();
546   int i, n = get_class_n_members(gt);
547
548   for (i = 0; i < n; i++)
549     dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
550 }
551
552 /************************************************************************/
553
554 void arm_gen_decls(FILE *out) {
555   struct obstack rodata, data, comm;
556   int    size;
557   char   *cp;
558
559   obstack_init(&rodata);
560   obstack_init(&data);
561   obstack_init(&comm);
562
563   arm_dump_globals(&rodata, &data, &comm);
564
565   size = obstack_object_size(&data);
566   cp   = obstack_finish(&data);
567   if (size > 0) {
568                 arm_switch_section(out, SECTION_DATA);
569     fwrite(cp, 1, size, out);
570   }
571
572   size = obstack_object_size(&rodata);
573   cp   = obstack_finish(&rodata);
574   if (size > 0) {
575                 arm_switch_section(out, SECTION_RODATA);
576     fwrite(cp, 1, size, out);
577   }
578
579   size = obstack_object_size(&comm);
580   cp   = obstack_finish(&comm);
581   if (size > 0) {
582                 arm_switch_section(out, SECTION_COMMON);
583     fwrite(cp, 1, size, out);
584   }
585
586   obstack_free(&rodata, NULL);
587   obstack_free(&data, NULL);
588   obstack_free(&comm, NULL);
589 }