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