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