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