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