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