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