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