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