copy result mode on final transformations (lea->add and sub->neg-add)
[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, int size, int align) {
58         switch (asm_flavour) {
59         case ASM_LINUX_GAS:
60                 obstack_printf(obst, "\t.comm\t%s,%d,%d\n", name, size, align);
61                 break;
62         case ASM_MINGW_GAS:
63                 obstack_printf(obst, "\t.comm\t%s,%d\n", name, size);
64                 break;
65         }
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 static void dump_object_size(struct obstack *obst, const char *name, int size) {
324         switch (asm_flavour) {
325         case ASM_LINUX_GAS:
326                 obstack_printf(obst, "\t.type\t%s,@object\n", name);
327                 obstack_printf(obst, "\t.size\t%s,%d\n", name, size);
328                 break;
329         }
330 }
331
332 /*
333  * Dumps the initialization of global variables that are not
334  * "uninitialized".
335  */
336 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
337 {
338         ir_type *ty         = get_entity_type(ent);
339         const char *ld_name = get_entity_ld_name(ent);
340         int align, h;
341         struct obstack *obst = data_obstack;
342
343         /*
344         * FIXME: did NOT work for partly constant values
345         */
346         if (! is_Method_type(ty)) {
347                 ent_variability variability = get_entity_variability(ent);
348                 visibility visibility = get_entity_visibility(ent);
349
350                 if (variability == variability_constant) {
351                         /* a constant entity, put it on the rdata */
352                         obst = rdata_obstack;
353                 }
354
355                 /* check, whether it is initialized, if yes create data */
356                 if (variability != variability_uninitialized) {
357                         if (visibility == visibility_external_visible) {
358                                 obstack_printf(obst, ".globl\t%s\n", ld_name);
359                         }
360                         dump_object_size(obst, ld_name, (get_type_size_bits(ty) + 7) >> 3);
361
362                         align = get_type_alignment_bytes(ty);
363                         ia32_dump_align(obst, align);
364
365                         obstack_printf(obst, "%s:\n", ld_name);
366
367                         if (is_atomic_type(ty)) {
368                                 if (get_entity_visibility(ent) != visibility_external_allocated)
369                                         dump_atomic_init(obst, get_atomic_ent_value(ent));
370                         }
371                         else {
372                                 int i, size = 0;
373
374                                 if (ent_is_string_const(ent)) {
375                                         dump_string_cst(obst, ent);
376                                 }
377                                 else if (is_Array_type(ty)) {
378                                         int filler;
379
380                                         /* potential spare values should be already included! */
381                                         for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
382                                                 entity *step = get_compound_ent_value_member(ent, i);
383                                                 ir_type *stype = get_entity_type(step);
384
385                                                 if (get_type_mode(stype)) {
386                                                         int align = (get_type_alignment_bits(stype) + 7) >> 3;
387                                                         int n     = size % align;
388
389                                                         if (n > 0) {
390                                                                 obstack_printf(obst, "\t.zero\t%d\n", align - n);
391                                                                 size += align - n;
392                                                         }
393                                                 }
394                                                 dump_atomic_init(obst, get_compound_ent_value(ent, i));
395                                                 size += get_type_size_bytes(stype);
396                                         }
397                                         filler = get_type_size_bytes(ty) - size;
398
399                                         if (filler > 0)
400                                                 obstack_printf(obst, "\t.zero\t%d\n", filler);
401                                 }
402                                 else if (is_compound_type(ty)) {
403                                         ir_node **vals;
404                                         int type_size, j;
405
406                                         /* Compound entities are NOT sorted.
407                                         * The sorting strategy used doesn't work for `value' compound fields nor
408                                         * for partially_constant entities.
409                                         */
410
411                                         /*
412                                         * in the worst case, every entity allocates one byte, so the type
413                                         * size should be equal or bigger the number of fields
414                                         */
415                                         type_size = get_type_size_bytes(ty);
416                                         vals      = xcalloc(type_size, sizeof(*vals));
417
418                                         /* collect the values and store them at the offsets */
419                                         for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
420                                                 int                 graph_length, aipos, offset;
421                                                 struct arr_info     *ai;
422                                                 int                 all_n = 1;
423                                                 compound_graph_path *path = get_compound_ent_value_path(ent, i);
424
425                                                 /* get the access path to the costant value */
426                                                 graph_length = get_compound_graph_path_length(path);
427                                                 ai = xcalloc(graph_length, sizeof(struct arr_info));
428
429                                                 /* We wanna know how many arrays are on the path to the entity. We also have to know how
430                                                 * many elements each array holds to calculate the offset for the entity. */
431                                                 for (j = 0; j < graph_length; j++) {
432                                                         entity  *step      = get_compound_graph_path_node(path, j);
433                                                         ir_type *step_type = get_entity_type(step);
434                                                         int     ty_size    = (get_type_size_bits(step_type) + 7) >> 3;
435                                                         int     k, n       = 0;
436
437                                                         if (is_Array_type(step_type))
438                                                                 for (k = 0; k < get_array_n_dimensions(step_type); k++)
439                                                                         n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
440                                                                 if (n) all_n *= n;
441                                                                 ai[j].n_elems = n ? all_n + 1 : 0;
442                                                                 ai[j].visit_cnt = 0;
443                                                                 ai[j].size = ty_size;
444                                                 }
445
446                                                 aipos = graph_length - 1;
447                                                 if (aipos) aipos--;
448
449                                                 for (offset = j = 0; j < graph_length; j++) {
450                                                         entity *step       = get_compound_graph_path_node(path, j);
451                                                         ir_type *step_type = get_entity_type(step);
452                                                         int ent_ofs        = get_entity_offset_bytes(step);
453                                                         int stepsize       = 0;
454
455                                                         /* add all positive offsets (= offsets in structs) */
456                                                         if (ent_ofs >= 0) offset += ent_ofs;
457
458                                                         if (j == graph_length - 1) {
459                                                                 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
460
461                                                                 /* Search the next free position in vals depending on the information from above (ai). */
462                                                                 while (vals[offset]) {
463                                                                         if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
464                                                                                 offset += stepsize;
465                                                                                 ai[aipos].visit_cnt++;
466                                                                         }
467                                                                         else
468                                                                                 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
469                                                                                         stepsize = ai[aipos--].size;
470                                                                                         offset  += stepsize;
471                                                                                 }
472                                                                 }
473
474                                                                 assert(aipos >= 0 && "couldn't store entity");
475                                                                 vals[offset] = get_compound_ent_value(ent, i);
476                                                         }
477                                                 }
478
479                                                 free(ai);
480                                         }
481
482                                         /* now write them sorted */
483                                         for(i = 0; i < type_size; ) {
484                                                 if (vals[i]) {
485                                                         dump_atomic_init(obst, vals[i]);
486                                                         i += (get_mode_size_bytes(get_irn_mode(vals[i])));
487                                                 }
488                                                 else {
489                                                         /* a gap */
490                                                         obstack_printf(obst, "\t.byte\t0\n");
491                                                         ++i;
492                                                 }
493                                         }
494                                         free(vals);
495                                 }
496                                 else {
497                                         assert(0 && "unsupported type");
498                                 }
499                         }
500                         obstack_printf(obst, "\n");
501                 }
502                 else if (visibility != visibility_external_allocated) {
503                         if (visibility == visibility_local) {
504                                 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
505                         }
506
507                         /* calculate the alignment */
508                         align = get_type_alignment_bytes(ty);
509                         h = highest_bit(align);
510
511                         if ((1 << h) < align)
512                                 ++h;
513                         align = (1 << h);
514
515                         if (align < 1)
516                                 align = 1;
517
518                         ia32_dump_comm(comm_obstack, ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
519                 }
520         }
521 }
522
523 /*
524  * Dumps declarations of global variables and the initialization code.
525  */
526 void ia32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
527 {
528         ir_type *gt = get_glob_type();
529         int i, n = get_class_n_members(gt);
530
531         for (i = 0; i < n; i++)
532                 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
533 }
534
535 /************************************************************************/
536
537 void ia32_gen_decls(FILE *out) {
538         struct obstack rodata, data, comm;
539         int    size;
540         char   *cp;
541
542         obstack_init(&rodata);
543         obstack_init(&data);
544         obstack_init(&comm);
545
546         ia32_dump_globals(&rodata, &data, &comm);
547
548         size = obstack_object_size(&data);
549         cp   = obstack_finish(&data);
550         if (size > 0) {
551                 ia32_switch_section(out, SECTION_DATA);
552                 fwrite(cp, 1, size, out);
553         }
554
555         size = obstack_object_size(&rodata);
556         cp   = obstack_finish(&rodata);
557         if (size > 0) {
558                 fprintf(out, "\t.section\t.rodata\n");
559                 fwrite(cp, 1, size, out);
560         }
561
562         size = obstack_object_size(&comm);
563         cp   = obstack_finish(&comm);
564         if (size > 0) {
565                 ia32_switch_section(out, SECTION_COMMON);
566                 fwrite(cp, 1, size, out);
567         }
568
569         obstack_free(&rodata, NULL);
570         obstack_free(&data, NULL);
571         obstack_free(&comm, NULL);
572 }