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