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