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