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