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