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