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