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