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