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);
77 static tarval *get_atomic_init_tv(ir_node *init)
79 ir_mode *mode = get_irn_mode(init);
81 switch (get_irn_opcode(init)) {
84 return get_atomic_init_tv(get_Cast_op(init));
87 return get_atomic_init_tv(get_Conv_op(init));
90 return get_Const_tarval(init);
93 switch (get_SymConst_kind(init)) {
94 case symconst_ofs_ent:
95 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
98 case symconst_type_size:
99 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
101 case symconst_type_align:
102 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
104 case symconst_enum_const:
105 return get_enumeration_value(get_SymConst_enum(init));
119 * dump an atomic value
121 static void do_dump_atomic_init(obstack_t *obst, ir_node *init)
123 ir_mode *mode = get_irn_mode(init);
124 int bytes = get_mode_size_bytes(mode);
127 switch (get_irn_opcode(init)) {
130 do_dump_atomic_init(obst, get_Cast_op(init));
134 do_dump_atomic_init(obst, get_Conv_op(init));
138 tv = get_Const_tarval(init);
140 /* beware of old stuff */
141 //assert(! mode_is_reference(mode));
143 /* it's a arithmetic value */
144 dump_arith_tarval(obst, tv, bytes);
148 switch (get_SymConst_kind(init)) {
149 case symconst_addr_name:
150 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
153 case symconst_addr_ent:
154 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
157 case symconst_ofs_ent:
158 obstack_printf(obst, "%d", get_entity_offset(get_SymConst_entity(init)));
161 case symconst_type_size:
162 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
165 case symconst_type_align:
166 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
169 case symconst_enum_const:
170 tv = get_enumeration_value(get_SymConst_enum(init));
171 dump_arith_tarval(obst, tv, bytes);
175 assert(!"dump_atomic_init(): don't know how to init from this SymConst");
180 do_dump_atomic_init(obst, get_Add_left(init));
181 obstack_printf(obst, " + ");
182 do_dump_atomic_init(obst, get_Add_right(init));
186 do_dump_atomic_init(obst, get_Sub_left(init));
187 obstack_printf(obst, " - ");
188 do_dump_atomic_init(obst, get_Sub_right(init));
192 do_dump_atomic_init(obst, get_Mul_left(init));
193 obstack_printf(obst, " * ");
194 do_dump_atomic_init(obst, get_Mul_right(init));
198 assert(0 && "dump_atomic_init(): unknown IR-node");
203 * dumps the type for given size (.byte, .long, ...)
205 static void dump_size_type(obstack_t *obst, int size) {
209 obstack_printf(obst, "\t.byte\t");
213 obstack_printf(obst, "\t.value\t");
217 obstack_printf(obst, "\t.long\t");
221 obstack_printf(obst, "\t.quad\t");
226 /* handled in arith */
230 fprintf(stderr, "Try to dump a type with %d bytes\n", size);
236 * dump an atomic value
238 static void dump_atomic_init(obstack_t *obst, ir_node *init)
240 ir_mode *mode = get_irn_mode(init);
241 int bytes = get_mode_size_bytes(mode);
243 dump_size_type(obst, bytes);
244 do_dump_atomic_init(obst, init);
245 obstack_printf(obst, "\n");
248 /************************************************************************/
249 /* Routines to dump global variables */
250 /************************************************************************/
253 * Determine if an entity is a string constant
254 * @param ent The entity
255 * @return 1 if it is a string constant, 0 otherwise
257 static int ent_is_string_const(ir_entity *ent)
262 ty = get_entity_type(ent);
264 /* if it's an array */
265 if (is_Array_type(ty)) {
266 ir_type *elm_ty = get_array_element_type(ty);
268 /* and the array's element type is primitive */
269 if (is_Primitive_type(elm_ty)) {
270 ir_mode *mode = get_type_mode(elm_ty);
273 * and the mode of the element type is an int of
274 * the same size as the byte mode
276 if (mode_is_int(mode)
277 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
281 n = get_compound_ent_n_values(ent);
282 for (i = 0; i < n; ++i) {
283 ir_node *irn = get_compound_ent_value(ent, i);
284 if(get_irn_opcode(irn) != iro_Const)
287 c = (int) get_tarval_long(get_Const_tarval(irn));
289 if((i < n - 1 && !(isgraph(c) || isspace(c)))
290 || (i == n - 1 && c != '\0'))
303 * Dump a atring constant.
304 * No checks are made!!
305 * @param obst The obst to dump on.
306 * @param ent The entity to dump.
308 static void dump_string_cst(obstack_t *obst, ir_entity *ent)
312 obstack_printf(obst, "\t.string \"");
313 n = get_compound_ent_n_values(ent);
315 for (i = 0; i < n-1; ++i) {
319 irn = get_compound_ent_value(ent, i);
320 c = (int) get_tarval_long(get_Const_tarval(irn));
323 case '"' : obstack_printf(obst, "\\\""); break;
324 case '\n': obstack_printf(obst, "\\n"); break;
325 case '\r': obstack_printf(obst, "\\r"); break;
326 case '\t': obstack_printf(obst, "\\t"); break;
327 case '\\': obstack_printf(obst, "\\\\"); break;
330 obstack_printf(obst, "%c", c);
332 obstack_printf(obst, "\\%o", c);
336 obstack_printf(obst, "\"\n");
339 static void dump_array_init(obstack_t *obst, ir_entity *ent)
341 const ir_type *ty = get_entity_type(ent);
346 /* potential spare values should be already included! */
347 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
348 ir_entity *step = get_compound_ent_value_member(ent, i);
349 ir_type *stype = get_entity_type(step);
351 if (get_type_mode(stype)) {
352 int align = (get_type_alignment_bits(stype) + 7) >> 3;
353 int n = size % align;
356 obstack_printf(obst, "\t.zero\t%d\n", align - n);
360 dump_atomic_init(obst, get_compound_ent_value(ent, i));
361 size += get_type_size_bytes(stype);
363 filler = get_type_size_bytes(ty) - size;
366 obstack_printf(obst, "\t.skip\t%d\n", filler);
369 enum normal_or_bitfield_kind {
375 enum normal_or_bitfield_kind kind;
378 unsigned char bf_val;
380 } normal_or_bitfield;
382 static void dump_compound_init(obstack_t *obst, ir_entity *ent)
384 ir_type *ty = get_entity_type(ent);
385 normal_or_bitfield *vals;
390 res = compute_compound_ent_array_indices(ent);
392 panic("Couldn't emit initializer for entity '%s'\n", get_entity_ld_name(ent));
396 * in the worst case, every entity allocates one byte, so the type
397 * size should be equal or bigger the number of fields
399 type_size = get_type_size_bytes(ty);
400 vals = alloca(type_size * sizeof(vals[0]));
401 memset(vals, 0, type_size * sizeof(vals[0]));
403 /* collect the values and store them at the offsets */
404 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
405 const compound_graph_path *path = get_compound_ent_value_path(ent, i);
406 int path_len = get_compound_graph_path_length(path);
407 int offset = get_compound_ent_value_offset_bytes(ent, i);
408 int offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
409 ir_node *value = get_compound_ent_value(ent, i);
410 ir_entity *last_ent = get_compound_graph_path_node(path, path_len - 1);
411 int value_len = get_type_size_bits(get_entity_type(last_ent));
413 if(offset_bits != 0 || value_len % 8 != 0) {
414 tarval *shift, *shifted;
415 tarval *tv = get_atomic_init_tv(value);
417 panic("Couldn't get numeric value for bitfield initializer '%s'\n",
418 get_entity_ld_name(ent));
420 shift = new_tarval_from_long(offset_bits, mode_Is);
421 shifted = tarval_shl(tv, shift);
422 if(shifted == tarval_bad || shifted == tarval_undefined) {
423 panic("Couldn't shift numeric value for bitfield initializer '%s'\n",
424 get_entity_ld_name(ent));
427 for(j = 0; j < 4 && value_len > 0; ++j) {
428 assert(offset + j < type_size);
429 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
430 vals[offset + j].kind = BITFIELD;
431 vals[offset + j].v.bf_val |= get_tarval_sub_bits(shifted, j);
432 value_len -= 8 - offset_bits;
436 assert(offset < type_size);
437 assert(vals[offset].kind == NORMAL);
438 assert(vals[offset].v.value == NULL);
439 vals[offset].v.value = value;
443 /* now write them sorted */
444 for(i = 0; i < type_size; ) {
445 int space = 0, skip = 0;
446 if (vals[i].kind == NORMAL) {
447 if(vals[i].v.value != NULL) {
448 dump_atomic_init(obst, vals[i].v.value);
449 skip = get_mode_size_bytes(get_irn_mode(vals[i].v.value)) - 1;
454 obstack_printf(obst, "\t.byte\t%d\n", vals[i].v.bf_val);
459 while(i < type_size && vals[i].kind == NORMAL && vals[i].v.value == NULL) {
468 obstack_printf(obst, "\t.skip\t%d\n", space);
472 static void dump_global(ia32_decl_env_t *env, ir_entity *ent)
475 ir_type *type = get_entity_type(ent);
476 const char *ld_name = get_entity_ld_name(ent);
477 ir_variability variability = get_entity_variability(ent);
478 ir_visibility visibility = get_entity_visibility(ent);
479 int align = get_type_alignment_bytes(type);
481 obst = env->data_obst;
482 if(is_Method_type(type)) {
483 if(get_method_img_section(ent) == section_constructors) {
484 obst = env->ctor_obst;
485 obstack_printf(obst, ".balign\t%d\n", align);
486 dump_size_type(obst, align);
487 obstack_printf(obst, "%s\n", ld_name);
490 } else if(variability == variability_constant) {
491 /* a constant entity, put it on the rdata */
492 obst = env->rodata_obst;
493 } else if(variability == variability_uninitialized) {
494 /* uninitialized entity put it in bss segment */
495 obst = env->bss_obst;
498 be_dbg_variable(env->main_env->db_handle, obst, ent);
500 /* global or not global */
501 if(visibility == visibility_external_visible) {
502 obstack_printf(obst, ".global\t%s\n", ld_name);
503 } else if(visibility == visibility_external_allocated) {
504 obstack_printf(obst, ".global\t%s\n", ld_name);
505 /* we can return now... */
510 obstack_printf(obst, ".balign\t%d\n", align);
513 obstack_printf(obst, "%s:\n", ld_name);
515 if(variability == variability_uninitialized) {
516 obstack_printf(obst, "\t.zero %d\n", get_type_size_bytes(type));
520 if (is_atomic_type(type)) {
521 dump_atomic_init(obst, get_atomic_ent_value(ent));
525 if (ent_is_string_const(ent)) {
526 dump_string_cst(obst, ent);
530 if (is_Array_type(type)) {
531 dump_array_init(obst, ent);
535 if (is_compound_type(type)) {
536 dump_compound_init(obst, ent);
540 assert(0 && "unsupported type");
544 * Dumps declarations of global variables and the initialization code.
546 static void ia32_dump_globals(ir_type *gt, ia32_decl_env_t *env)
548 int i, n = get_compound_n_members(gt);
550 for (i = 0; i < n; i++) {
551 ir_entity *ent = get_compound_member(gt, i);
552 dump_global(env, ent);
556 /************************************************************************/
558 void ia32_gen_decls(FILE *out, const be_main_env_t *main_env) {
560 obstack_t rodata, data, bss, ctor;
564 /* dump the global type */
565 obstack_init(&rodata);
569 if (main_env->options->opt_profile)
572 env.rodata_obst = &rodata;
573 env.data_obst = &data;
575 env.ctor_obst = main_env->options->opt_profile ? &ctor : NULL;
576 env.main_env = main_env;
578 ia32_dump_globals(get_glob_type(), &env);
580 size = obstack_object_size(&data);
581 cp = obstack_finish(&data);
583 ia32_switch_section(out, SECTION_DATA);
584 fwrite(cp, 1, size, out);
587 size = obstack_object_size(&rodata);
588 cp = obstack_finish(&rodata);
590 ia32_switch_section(out, SECTION_RODATA);
591 fwrite(cp, 1, size, out);
594 size = obstack_object_size(&bss);
595 cp = obstack_finish(&bss);
597 ia32_switch_section(out, SECTION_COMMON);
598 fwrite(cp, 1, size, out);
601 if (main_env->options->opt_profile) {
602 size = obstack_object_size(&ctor);
603 cp = obstack_finish(&ctor);
605 ia32_switch_section(out, SECTION_CTOR);
606 fwrite(cp, 1, size, out);
608 obstack_free(&ctor, NULL);
611 obstack_free(&rodata, NULL);
612 obstack_free(&data, NULL);
613 obstack_free(&bss, NULL);
615 /* dump the Thread Local Storage */
618 env.rodata_obst = &data;
619 env.data_obst = &data;
620 env.bss_obst = &data;
621 env.ctor_obst = NULL;
623 ia32_dump_globals(get_tls_type(), &env);
625 size = obstack_object_size(&data);
626 cp = obstack_finish(&data);
628 ia32_switch_section(out, SECTION_TLS);
629 fprintf(out, ".balign\t%d\n", 32);
630 fwrite(cp, 1, size, out);
633 obstack_free(&data, NULL);