2 * Dumps global variables and constants as ia32 assembler.
3 * @author Christian Wuerdig
19 #include "ia32_emitter.h"
20 #include "ia32_gen_decls.h"
22 /************************************************************************/
25 * returns the highest bit value
27 static unsigned highest_bit(unsigned v)
31 if (v >= (1U << 16U)) {
35 if (v >= (1U << 8U)) {
39 if (v >= (1U << 4U)) {
43 if (v >= (1U << 2U)) {
47 if (v >= (1U << 1U)) {
57 static void ia32_dump_comm(struct obstack *obst, const char *name, visibility vis, int size, int align) {
58 switch (asm_flavour) {
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);
65 if (vis == visibility_local)
66 obstack_printf(obst, "\t.lcomm\t%s,%d\n", name, size);
68 obstack_printf(obst, "\t.comm\t%s,%d\n", name, size);
74 * output the alignment
76 static void ia32_dump_align(struct obstack *obst, int align)
78 int h = highest_bit(align);
85 obstack_printf(obst, "\t.align %d\n", align);
88 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
93 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
97 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
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));
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));
116 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
122 * dump an atomic value
124 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
126 ir_mode *mode = get_irn_mode(init);
127 int bytes = get_mode_size_bytes(mode);
130 switch (get_irn_opcode(init)) {
133 do_dump_atomic_init(obst, get_Cast_op(init));
137 do_dump_atomic_init(obst, get_Conv_op(init));
141 tv = get_Const_tarval(init);
143 /* beware of old stuff */
144 //assert(! mode_is_reference(mode));
146 /* it's a arithmetic value */
147 dump_arith_tarval(obst, tv, bytes);
151 switch (get_SymConst_kind(init)) {
152 case symconst_addr_name:
153 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
156 case symconst_addr_ent:
157 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
160 case symconst_type_size:
161 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
164 case symconst_type_align:
165 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
169 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
174 do_dump_atomic_init(obst, get_Add_left(init));
175 obstack_printf(obst, " + ");
176 do_dump_atomic_init(obst, get_Add_right(init));
180 do_dump_atomic_init(obst, get_Sub_left(init));
181 obstack_printf(obst, " - ");
182 do_dump_atomic_init(obst, get_Sub_right(init));
186 do_dump_atomic_init(obst, get_Mul_left(init));
187 obstack_printf(obst, " * ");
188 do_dump_atomic_init(obst, get_Mul_right(init));
192 assert(0 && "dump_atomic_init(): unknown IR-node");
197 * dump an atomic value
199 static void dump_atomic_init(struct obstack *obst, ir_node *init)
201 ir_mode *mode = get_irn_mode(init);
202 int bytes = get_mode_size_bytes(mode);
207 obstack_printf(obst, "\t.byte\t");
211 obstack_printf(obst, "\t.value\t");
215 obstack_printf(obst, "\t.long\t");
219 obstack_printf(obst, "\t.quad\t");
224 /* handled in arith */
228 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
232 do_dump_atomic_init(obst, init);
233 obstack_printf(obst, "\n");
236 /************************************************************************/
237 /* Routines to dump global variables */
238 /************************************************************************/
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
245 static int ent_is_string_const(entity *ent)
250 ty = get_entity_type(ent);
252 /* if it's an array */
253 if (is_Array_type(ty)) {
254 ir_type *elm_ty = get_array_element_type(ty);
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);
261 * and the mode of the element type is an int of
262 * the same size as the byte mode
264 if (mode_is_int(mode)
265 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
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)
275 c = (int) get_tarval_long(get_Const_tarval(irn));
277 if((i < n - 1 && !(isgraph(c) || isspace(c)))
278 || (i == n - 1 && c != '\0'))
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.
296 static void dump_string_cst(struct obstack *obst, entity *ent)
300 obstack_printf(obst, "\t.string \"");
301 n = get_compound_ent_n_values(ent);
303 for (i = 0; i < n-1; ++i) {
307 irn = get_compound_ent_value(ent, i);
308 c = (int) get_tarval_long(get_Const_tarval(irn));
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;
317 obstack_printf(obst, "%c", c);
319 obstack_printf(obst, "%O", c);
323 obstack_printf(obst, "\"\n");
332 static void dump_object_size(struct obstack *obst, const char *name, int size) {
333 switch (asm_flavour) {
335 obstack_printf(obst, "\t.type\t%s,@object\n", name);
336 obstack_printf(obst, "\t.size\t%s,%d\n", name, size);
342 * Dumps the initialization of global variables that are not
345 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
347 ir_type *ty = get_entity_type(ent);
348 const char *ld_name = get_entity_ld_name(ent);
350 struct obstack *obst = data_obstack;
353 * FIXME: did NOT work for partly constant values
355 if (! is_Method_type(ty)) {
356 ent_variability variability = get_entity_variability(ent);
357 visibility visibility = get_entity_visibility(ent);
359 if (variability == variability_constant) {
360 /* a constant entity, put it on the rdata */
361 obst = rdata_obstack;
364 /* check, whether it is initialized, if yes create data */
365 if (variability != variability_uninitialized) {
366 if (visibility == visibility_external_visible) {
367 obstack_printf(obst, ".globl\t%s\n", ld_name);
369 dump_object_size(obst, ld_name, (get_type_size_bits(ty) + 7) >> 3);
371 align = get_type_alignment_bytes(ty);
372 ia32_dump_align(obst, align);
374 obstack_printf(obst, "%s:\n", ld_name);
376 if (is_atomic_type(ty)) {
377 if (get_entity_visibility(ent) != visibility_external_allocated)
378 dump_atomic_init(obst, get_atomic_ent_value(ent));
383 if (ent_is_string_const(ent)) {
384 dump_string_cst(obst, ent);
386 else if (is_Array_type(ty)) {
389 /* potential spare values should be already included! */
390 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
391 entity *step = get_compound_ent_value_member(ent, i);
392 ir_type *stype = get_entity_type(step);
394 if (get_type_mode(stype)) {
395 int align = (get_type_alignment_bits(stype) + 7) >> 3;
396 int n = size % align;
399 obstack_printf(obst, "\t.zero\t%d\n", align - n);
403 dump_atomic_init(obst, get_compound_ent_value(ent, i));
404 size += get_type_size_bytes(stype);
406 filler = get_type_size_bytes(ty) - size;
409 obstack_printf(obst, "\t.zero\t%d\n", filler);
411 else if (is_compound_type(ty)) {
415 /* Compound entities are NOT sorted.
416 * The sorting strategy used doesn't work for `value' compound fields nor
417 * for partially_constant entities.
421 * in the worst case, every entity allocates one byte, so the type
422 * size should be equal or bigger the number of fields
424 type_size = get_type_size_bytes(ty);
425 vals = xcalloc(type_size, sizeof(*vals));
427 /* collect the values and store them at the offsets */
428 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
429 int graph_length, aipos, offset;
432 compound_graph_path *path = get_compound_ent_value_path(ent, i);
434 /* get the access path to the costant value */
435 graph_length = get_compound_graph_path_length(path);
436 ai = xcalloc(graph_length, sizeof(struct arr_info));
438 /* We wanna know how many arrays are on the path to the entity. We also have to know how
439 * many elements each array holds to calculate the offset for the entity. */
440 for (j = 0; j < graph_length; j++) {
441 entity *step = get_compound_graph_path_node(path, j);
442 ir_type *step_type = get_entity_type(step);
443 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
446 if (is_Array_type(step_type))
447 for (k = 0; k < get_array_n_dimensions(step_type); k++)
448 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
450 ai[j].n_elems = n ? all_n + 1 : 0;
452 ai[j].size = ty_size;
455 aipos = graph_length - 1;
458 for (offset = j = 0; j < graph_length; j++) {
459 entity *step = get_compound_graph_path_node(path, j);
460 ir_type *step_type = get_entity_type(step);
461 int ent_ofs = get_entity_offset_bytes(step);
464 /* add all positive offsets (= offsets in structs) */
465 if (ent_ofs >= 0) offset += ent_ofs;
467 if (j == graph_length - 1) {
468 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
470 /* Search the next free position in vals depending on the information from above (ai). */
471 while (vals[offset]) {
472 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
474 ai[aipos].visit_cnt++;
477 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
478 stepsize = ai[aipos--].size;
483 assert(aipos >= 0 && "couldn't store entity");
484 vals[offset] = get_compound_ent_value(ent, i);
491 /* now write them sorted */
492 for(i = 0; i < type_size; ) {
494 dump_atomic_init(obst, vals[i]);
495 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
499 obstack_printf(obst, "\t.byte\t0\n");
506 assert(0 && "unsupported type");
509 obstack_printf(obst, "\n");
511 else if (visibility != visibility_external_allocated) {
512 /* calculate the alignment */
513 align = get_type_alignment_bytes(ty);
514 h = highest_bit(align);
516 if ((1 << h) < align)
523 ia32_dump_comm(comm_obstack, ld_name, visibility,
524 (get_type_size_bits(ty) + 7) >> 3, align);
530 * Dumps declarations of global variables and the initialization code.
532 void ia32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
534 ir_type *gt = get_glob_type();
535 int i, n = get_class_n_members(gt);
537 for (i = 0; i < n; i++)
538 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
541 /************************************************************************/
543 void ia32_gen_decls(FILE *out) {
544 struct obstack rodata, data, comm;
548 obstack_init(&rodata);
552 ia32_dump_globals(&rodata, &data, &comm);
554 size = obstack_object_size(&data);
555 cp = obstack_finish(&data);
557 ia32_switch_section(out, SECTION_DATA);
558 fwrite(cp, 1, size, out);
561 size = obstack_object_size(&rodata);
562 cp = obstack_finish(&rodata);
564 fprintf(out, "\t.section\t.rodata\n");
565 fwrite(cp, 1, size, out);
568 size = obstack_object_size(&comm);
569 cp = obstack_finish(&comm);
571 ia32_switch_section(out, SECTION_COMMON);
572 fwrite(cp, 1, size, out);
575 obstack_free(&rodata, NULL);
576 obstack_free(&data, NULL);
577 obstack_free(&comm, NULL);