2 * Dumps global variables and constants as ia32 assembler.
3 * @author Christian Wuerdig
26 #include "ia32_emitter.h"
27 #include "ia32_gen_decls.h"
29 typedef struct obstack obstack_t;
31 typedef struct _ia32_decl_env {
32 obstack_t *rodata_obst;
36 const be_main_env_t *main_env;
39 /************************************************************************/
44 static void dump_arith_tarval(obstack_t *obst, tarval *tv, int bytes)
49 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
53 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
57 obstack_printf(obst, "0x%02x%02x%02x%02x",
58 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
62 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
63 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
64 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
72 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
78 * Return the tarval of an atomic initializer.
80 static tarval *get_atomic_init_tv(ir_node *init)
83 ir_mode *mode = get_irn_mode(init);
85 switch (get_irn_opcode(init)) {
88 init = get_Cast_op(init);
92 init = get_Conv_op(init);
96 return get_Const_tarval(init);
99 switch (get_SymConst_kind(init)) {
100 case symconst_ofs_ent:
101 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
103 case symconst_type_size:
104 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
106 case symconst_type_align:
107 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
109 case symconst_enum_const:
110 return get_enumeration_value(get_SymConst_enum(init));
123 * dump an atomic value
125 static void do_dump_atomic_init(obstack_t *obst, ir_node *init)
127 ir_mode *mode = get_irn_mode(init);
128 int bytes = get_mode_size_bytes(mode);
131 switch (get_irn_opcode(init)) {
134 do_dump_atomic_init(obst, get_Cast_op(init));
138 do_dump_atomic_init(obst, get_Conv_op(init));
142 tv = get_Const_tarval(init);
144 /* it's a arithmetic value */
145 dump_arith_tarval(obst, tv, bytes);
149 switch (get_SymConst_kind(init)) {
150 case symconst_addr_name:
151 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
154 case symconst_addr_ent:
155 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
158 case symconst_ofs_ent:
159 obstack_printf(obst, "%d", get_entity_offset(get_SymConst_entity(init)));
162 case symconst_type_size:
163 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
166 case symconst_type_align:
167 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
170 case symconst_enum_const:
171 tv = get_enumeration_value(get_SymConst_enum(init));
172 dump_arith_tarval(obst, tv, bytes);
176 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
181 do_dump_atomic_init(obst, get_Add_left(init));
182 obstack_printf(obst, " + ");
183 do_dump_atomic_init(obst, get_Add_right(init));
187 do_dump_atomic_init(obst, get_Sub_left(init));
188 obstack_printf(obst, " - ");
189 do_dump_atomic_init(obst, get_Sub_right(init));
193 do_dump_atomic_init(obst, get_Mul_left(init));
194 obstack_printf(obst, " * ");
195 do_dump_atomic_init(obst, get_Mul_right(init));
199 assert(0 && "dump_atomic_init(): unknown IR-node");
204 * dumps the type for given size (.byte, .long, ...)
206 static void dump_size_type(obstack_t *obst, int size) {
210 obstack_printf(obst, "\t.byte\t");
214 obstack_printf(obst, "\t.value\t");
218 obstack_printf(obst, "\t.long\t");
222 obstack_printf(obst, "\t.quad\t");
227 /* handled in arith */
231 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
237 * dump an atomic value to an obstack
239 static void dump_atomic_init(obstack_t *obst, ir_node *init)
241 ir_mode *mode = get_irn_mode(init);
242 int bytes = get_mode_size_bytes(mode);
244 dump_size_type(obst, bytes);
245 do_dump_atomic_init(obst, init);
246 obstack_printf(obst, "\n");
249 /************************************************************************/
250 /* Routines to dump global variables */
251 /************************************************************************/
254 * Determine if an entity is a string constant
255 * @param ent The entity
256 * @return 1 if it is a string constant, 0 otherwise
258 static int ent_is_string_const(ir_entity *ent)
260 ir_type *type, *element_type;
264 type = get_entity_type(ent);
266 /* if it's an array */
267 if (!is_Array_type(type))
270 element_type = get_array_element_type(type);
272 /* and the array's element type is primitive */
273 if (!is_Primitive_type(element_type))
276 /* and the mode of the element type is an int of
277 * the same size as the byte mode */
278 mode = get_type_mode(element_type);
279 if (!mode_is_int(mode)
280 || get_mode_size_bits(mode) != get_mode_size_bits(mode_Bs))
283 /* if it contains only printable chars and a 0 at the end */
284 n = get_compound_ent_n_values(ent);
285 for (i = 0; i < n; ++i) {
286 ir_node *irn = get_compound_ent_value(ent, i);
287 if(get_irn_opcode(irn) != iro_Const)
290 c = (int) get_tarval_long(get_Const_tarval(irn));
292 if((i < n - 1 && !(isgraph(c) || isspace(c)))
293 || (i == n - 1 && c != '\0'))
297 /* then we can emit it as a string constant */
302 * Dump a string constant.
303 * No checks are made!!
304 * @param obst The obst to dump on.
305 * @param ent The entity to dump.
307 static void dump_string_cst(obstack_t *obst, ir_entity *ent)
311 obstack_printf(obst, "\t.string \"");
312 n = get_compound_ent_n_values(ent);
314 for (i = 0; i < n-1; ++i) {
318 irn = get_compound_ent_value(ent, i);
319 c = (int) get_tarval_long(get_Const_tarval(irn));
322 case '"' : obstack_printf(obst, "\\\""); break;
323 case '\n': obstack_printf(obst, "\\n"); break;
324 case '\r': obstack_printf(obst, "\\r"); break;
325 case '\t': obstack_printf(obst, "\\t"); break;
326 case '\\': obstack_printf(obst, "\\\\"); break;
329 obstack_printf(obst, "%c", c);
331 obstack_printf(obst, "\\%o", c);
335 obstack_printf(obst, "\"\n");
338 static void dump_array_init(obstack_t *obst, ir_entity *ent)
340 const ir_type *ty = get_entity_type(ent);
345 /* potential spare values should be already included! */
346 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
347 ir_entity *step = get_compound_ent_value_member(ent, i);
348 ir_type *stype = get_entity_type(step);
350 if (get_type_mode(stype)) {
351 int align = (get_type_alignment_bits(stype) + 7) >> 3;
352 int n = size % align;
355 obstack_printf(obst, "\t.zero\t%d\n", align - n);
359 dump_atomic_init(obst, get_compound_ent_value(ent, i));
360 size += get_type_size_bytes(stype);
362 filler = get_type_size_bytes(ty) - size;
365 obstack_printf(obst, "\t.skip\t%d\n", filler);
368 enum normal_or_bitfield_kind {
374 enum normal_or_bitfield_kind kind;
377 unsigned char bf_val;
379 } normal_or_bitfield;
382 * Dump an initializer for a compound entity.
384 static void dump_compound_init(obstack_t *obst, ir_entity *ent)
386 normal_or_bitfield *vals;
387 int i, j, n = get_compound_ent_n_values(ent);
390 /* Find the initializer size. Sorrily gcc support a nasty feature:
391 The last field of a compound may be a flexible array. This allows
392 initializers bigger than the type size. */
394 for (i = 0; i < n; ++i) {
395 int offset = get_compound_ent_value_offset_bytes(ent, i);
396 int bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
397 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
398 int path_len = get_compound_graph_path_length(path);
399 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
400 int value_len = get_type_size_bits(get_entity_type(last_ent));
402 offset += (value_len + bits_remainder + 7) >> 3;
404 if (offset > last_ofs) {
410 * In the worst case, every initializer allocates one byte.
411 * Moreover, initializer might be big, do not allocate an stack.
413 vals = xcalloc(last_ofs, sizeof(vals[0]));
415 /* collect the values and store them at the offsets */
416 for (i = 0; i < n; ++i) {
417 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
418 int path_len = get_compound_graph_path_length(path);
419 int offset = get_compound_ent_value_offset_bytes(ent, i);
420 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
421 ir_node *value = get_compound_ent_value(ent, i);
422 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
423 int value_len = get_type_size_bits(get_entity_type(last_ent));
425 assert(offset_bits >= 0);
427 if (offset_bits != 0 ||
428 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
429 tarval *shift, *shifted;
430 tarval *tv = get_atomic_init_tv(value);
432 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
433 get_entity_ld_name(ent));
435 tv = tarval_convert_to(tv, mode_Lu);
436 shift = new_tarval_from_long(offset_bits, mode_Is);
437 shifted = tarval_shl(tv, shift);
438 if (shifted == tarval_bad || shifted == tarval_undefined) {
439 panic("Couldn't shift numeric value for bitfield initializer '%s'\n",
440 get_entity_ld_name(ent));
443 for (j = 0; value_len > 0; ++j) {
444 assert(offset + j < last_ofs);
445 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
446 vals[offset + j].kind = BITFIELD;
447 vals[offset + j].v.bf_val |= get_tarval_sub_bits(shifted, j);
448 value_len -= 8 - offset_bits;
452 assert(offset < last_ofs);
453 assert(vals[offset].kind == NORMAL);
454 assert(vals[offset].v.value == NULL);
455 vals[offset].v.value = value;
459 /* now write them sorted */
460 for (i = 0; i < last_ofs; ) {
461 int space = 0, skip = 0;
462 if (vals[i].kind == NORMAL) {
463 if(vals[i].v.value != NULL) {
464 dump_atomic_init(obst, vals[i].v.value);
465 skip = get_mode_size_bytes(get_irn_mode(vals[i].v.value)) - 1;
470 assert(vals[i].kind == BITFIELD);
471 obstack_printf(obst, "\t.byte\t%d\n", vals[i].v.bf_val);
476 while (i < last_ofs && vals[i].kind == NORMAL && vals[i].v.value == NULL) {
485 obstack_printf(obst, "\t.skip\t%d\n", space);
491 * Dump a global entity.
493 static void dump_global(ia32_decl_env_t *env, ir_entity *ent)
496 ir_type *type = get_entity_type(ent);
497 const char *ld_name = get_entity_ld_name(ent);
498 ir_variability variability = get_entity_variability(ent);
499 ir_visibility visibility = get_entity_visibility(ent);
500 int align = get_type_alignment_bytes(type);
502 obst = env->data_obst;
503 if (is_Method_type(type)) {
504 if (get_method_img_section(ent) == section_constructors) {
505 obst = env->ctor_obst;
506 obstack_printf(obst, ".balign\t%d\n", align);
507 dump_size_type(obst, align);
508 obstack_printf(obst, "%s\n", ld_name);
511 } else if (variability == variability_constant) {
512 /* a constant entity, put it on the rdata */
513 obst = env->rodata_obst;
514 } else if (variability == variability_uninitialized) {
515 /* uninitialized entity put it in bss segment */
516 obst = env->bss_obst;
519 be_dbg_variable(env->main_env->db_handle, obst, ent);
521 /* global or not global */
522 if(visibility == visibility_external_visible
523 && variability != variability_uninitialized) {
524 obstack_printf(obst, ".global\t%s\n", ld_name);
525 } else if(visibility == visibility_external_allocated) {
526 obstack_printf(obst, ".global\t%s\n", ld_name);
527 /* we can return now... */
531 if(align > 1 && variability != variability_uninitialized) {
532 obstack_printf(obst, ".balign\t%d\n", align);
535 if(variability != variability_uninitialized) {
536 obstack_printf(obst, "%s:\n", ld_name);
539 if (variability == variability_uninitialized) {
540 obstack_printf(obst, "\t.comm %s,%d,%d\n",
541 ld_name, get_type_size_bytes(type), align);
542 } else if (is_atomic_type(type)) {
543 dump_atomic_init(obst, get_atomic_ent_value(ent));
544 } else if (ent_is_string_const(ent)) {
545 dump_string_cst(obst, ent);
546 } else if (is_Array_type(type)) {
547 dump_array_init(obst, ent);
548 } else if (is_compound_type(type)) {
549 dump_compound_init(obst, ent);
551 assert(0 && "unsupported type");
555 * Dumps declarations of global variables and the initialization code.
557 static void ia32_dump_globals(ir_type *gt, ia32_decl_env_t *env)
559 int i, n = get_compound_n_members(gt);
561 for (i = 0; i < n; i++) {
562 ir_entity *ent = get_compound_member(gt, i);
563 dump_global(env, ent);
567 /************************************************************************/
569 void ia32_gen_decls(FILE *out, const be_main_env_t *main_env) {
571 obstack_t rodata, data, bss, ctor;
575 /* dump the global type */
576 obstack_init(&rodata);
580 if (main_env->options->opt_profile)
583 env.rodata_obst = &rodata;
584 env.data_obst = &data;
586 env.ctor_obst = main_env->options->opt_profile ? &ctor : NULL;
587 env.main_env = main_env;
589 ia32_dump_globals(get_glob_type(), &env);
591 size = obstack_object_size(&data);
592 cp = obstack_finish(&data);
594 ia32_switch_section(out, SECTION_DATA);
595 fwrite(cp, 1, size, out);
598 size = obstack_object_size(&rodata);
599 cp = obstack_finish(&rodata);
601 ia32_switch_section(out, SECTION_RODATA);
602 fwrite(cp, 1, size, out);
605 size = obstack_object_size(&bss);
606 cp = obstack_finish(&bss);
608 ia32_switch_section(out, SECTION_COMMON);
609 fwrite(cp, 1, size, out);
612 if (main_env->options->opt_profile) {
613 size = obstack_object_size(&ctor);
614 cp = obstack_finish(&ctor);
616 ia32_switch_section(out, SECTION_CTOR);
617 fwrite(cp, 1, size, out);
619 obstack_free(&ctor, NULL);
622 obstack_free(&rodata, NULL);
623 obstack_free(&data, NULL);
624 obstack_free(&bss, NULL);
626 /* dump the Thread Local Storage */
629 env.rodata_obst = &data;
630 env.data_obst = &data;
631 env.bss_obst = &data;
632 env.ctor_obst = NULL;
634 ia32_dump_globals(get_tls_type(), &env);
636 size = obstack_object_size(&data);
637 cp = obstack_finish(&data);
639 ia32_switch_section(out, SECTION_TLS);
640 fprintf(out, ".balign\t%d\n", 32);
641 fwrite(cp, 1, size, out);
644 obstack_free(&data, NULL);