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