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