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