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