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