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