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