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, int size, int align) {
58 switch (asm_flavour) {
60 obstack_printf(obst, "\t.comm\t%s,%d,%d\n", name, size, align);
63 obstack_printf(obst, "\t.comm\t%s,%d\n", name, size);
69 * output the alignment
71 static void ia32_dump_align(struct obstack *obst, int align)
73 int h = highest_bit(align);
80 obstack_printf(obst, "\t.align %d\n", align);
83 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
88 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
92 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
96 obstack_printf(obst, "0x%02x%02x%02x%02x",
97 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
101 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
102 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
103 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
111 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
117 * dump an atomic value
119 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
121 ir_mode *mode = get_irn_mode(init);
122 int bytes = get_mode_size_bytes(mode);
125 switch (get_irn_opcode(init)) {
128 do_dump_atomic_init(obst, get_Cast_op(init));
132 do_dump_atomic_init(obst, get_Conv_op(init));
136 tv = get_Const_tarval(init);
138 /* beware of old stuff */
139 assert(! mode_is_reference(mode));
141 /* it's a arithmetic value */
142 dump_arith_tarval(obst, tv, bytes);
146 switch (get_SymConst_kind(init)) {
147 case symconst_addr_name:
148 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
151 case symconst_addr_ent:
152 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
156 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
160 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
165 do_dump_atomic_init(obst, get_Add_left(init));
166 obstack_printf(obst, " + ");
167 do_dump_atomic_init(obst, get_Add_right(init));
171 do_dump_atomic_init(obst, get_Sub_left(init));
172 obstack_printf(obst, " - ");
173 do_dump_atomic_init(obst, get_Sub_right(init));
177 do_dump_atomic_init(obst, get_Mul_left(init));
178 obstack_printf(obst, " * ");
179 do_dump_atomic_init(obst, get_Mul_right(init));
183 assert(0 && "dump_atomic_init(): unknown IR-node");
188 * dump an atomic value
190 static void dump_atomic_init(struct obstack *obst, ir_node *init)
192 ir_mode *mode = get_irn_mode(init);
193 int bytes = get_mode_size_bytes(mode);
198 obstack_printf(obst, "\t.byte\t");
202 obstack_printf(obst, "\t.value\t");
206 obstack_printf(obst, "\t.long\t");
210 obstack_printf(obst, "\t.quad\t");
215 /* handled in arith */
219 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
223 do_dump_atomic_init(obst, init);
224 obstack_printf(obst, "\n");
227 /************************************************************************/
228 /* Routines to dump global variables */
229 /************************************************************************/
232 * Determine if an entity is a string constant
233 * @param ent The entity
234 * @return 1 if it is a string constant, 0 otherwise
236 static int ent_is_string_const(entity *ent)
241 ty = get_entity_type(ent);
243 /* if it's an array */
244 if (is_Array_type(ty)) {
245 ir_type *elm_ty = get_array_element_type(ty);
247 /* and the array's element type is primitive */
248 if (is_Primitive_type(elm_ty)) {
249 ir_mode *mode = get_type_mode(elm_ty);
252 * and the mode of the element type is an int of
253 * the same size as the byte mode
255 if (mode_is_int(mode)
256 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
260 n = get_compound_ent_n_values(ent);
261 for (i = 0; i < n; ++i) {
262 ir_node *irn = get_compound_ent_value(ent, i);
263 if(get_irn_opcode(irn) != iro_Const)
266 c = (int) get_tarval_long(get_Const_tarval(irn));
268 if((i < n - 1 && !(isgraph(c) || isspace(c)))
269 || (i == n - 1 && c != '\0'))
282 * Dump a atring constant.
283 * No checks are made!!
284 * @param obst The obst to dump on.
285 * @param ent The entity to dump.
287 static void dump_string_cst(struct obstack *obst, entity *ent)
291 obstack_printf(obst, "\t.string \"");
292 n = get_compound_ent_n_values(ent);
294 for (i = 0; i < n-1; ++i) {
298 irn = get_compound_ent_value(ent, i);
299 c = (int) get_tarval_long(get_Const_tarval(irn));
302 case '"' : obstack_printf(obst, "\\\""); break;
303 case '\n': obstack_printf(obst, "\\n"); break;
304 case '\r': obstack_printf(obst, "\\r"); break;
305 case '\t': obstack_printf(obst, "\\t"); break;
308 obstack_printf(obst, "%c", c);
310 obstack_printf(obst, "%O", c);
314 obstack_printf(obst, "\"\n");
323 static void dump_object_size(struct obstack *obst, const char *name, int size) {
324 switch (asm_flavour) {
326 obstack_printf(obst, "\t.type\t%s,@object\n", name);
327 obstack_printf(obst, "\t.size\t%s,%d\n", name, size);
333 * Dumps the initialization of global variables that are not
336 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
338 ir_type *ty = get_entity_type(ent);
339 const char *ld_name = get_entity_ld_name(ent);
341 struct obstack *obst = data_obstack;
344 * FIXME: did NOT work for partly constant values
346 if (! is_Method_type(ty)) {
347 ent_variability variability = get_entity_variability(ent);
348 visibility visibility = get_entity_visibility(ent);
350 if (variability == variability_constant) {
351 /* a constant entity, put it on the rdata */
352 obst = rdata_obstack;
355 /* check, whether it is initialized, if yes create data */
356 if (variability != variability_uninitialized) {
357 if (visibility == visibility_external_visible) {
358 obstack_printf(obst, ".globl\t%s\n", ld_name);
360 dump_object_size(obst, ld_name, (get_type_size_bits(ty) + 7) >> 3);
362 align = get_type_alignment_bytes(ty);
363 ia32_dump_align(obst, align);
365 obstack_printf(obst, "%s:\n", ld_name);
367 if (is_atomic_type(ty)) {
368 if (get_entity_visibility(ent) != visibility_external_allocated)
369 dump_atomic_init(obst, get_atomic_ent_value(ent));
374 if (ent_is_string_const(ent)) {
375 dump_string_cst(obst, ent);
377 else if (is_Array_type(ty)) {
380 /* potential spare values should be already included! */
381 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
382 entity *step = get_compound_ent_value_member(ent, i);
383 ir_type *stype = get_entity_type(step);
385 if (get_type_mode(stype)) {
386 int align = (get_type_alignment_bits(stype) + 7) >> 3;
387 int n = size % align;
390 obstack_printf(obst, "\t.zero\t%d\n", align - n);
394 dump_atomic_init(obst, get_compound_ent_value(ent, i));
395 size += get_type_size_bytes(stype);
397 filler = get_type_size_bytes(ty) - size;
400 obstack_printf(obst, "\t.zero\t%d\n", filler);
402 else if (is_compound_type(ty)) {
406 /* Compound entities are NOT sorted.
407 * The sorting strategy used doesn't work for `value' compound fields nor
408 * for partially_constant entities.
412 * in the worst case, every entity allocates one byte, so the type
413 * size should be equal or bigger the number of fields
415 type_size = get_type_size_bytes(ty);
416 vals = xcalloc(type_size, sizeof(*vals));
418 /* collect the values and store them at the offsets */
419 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
420 int graph_length, aipos, offset;
423 compound_graph_path *path = get_compound_ent_value_path(ent, i);
425 /* get the access path to the costant value */
426 graph_length = get_compound_graph_path_length(path);
427 ai = xcalloc(graph_length, sizeof(struct arr_info));
429 /* We wanna know how many arrays are on the path to the entity. We also have to know how
430 * many elements each array holds to calculate the offset for the entity. */
431 for (j = 0; j < graph_length; j++) {
432 entity *step = get_compound_graph_path_node(path, j);
433 ir_type *step_type = get_entity_type(step);
434 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
437 if (is_Array_type(step_type))
438 for (k = 0; k < get_array_n_dimensions(step_type); k++)
439 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
441 ai[j].n_elems = n ? all_n + 1 : 0;
443 ai[j].size = ty_size;
446 aipos = graph_length - 1;
449 for (offset = j = 0; j < graph_length; j++) {
450 entity *step = get_compound_graph_path_node(path, j);
451 ir_type *step_type = get_entity_type(step);
452 int ent_ofs = get_entity_offset_bytes(step);
455 /* add all positive offsets (= offsets in structs) */
456 if (ent_ofs >= 0) offset += ent_ofs;
458 if (j == graph_length - 1) {
459 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
461 /* Search the next free position in vals depending on the information from above (ai). */
462 while (vals[offset]) {
463 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
465 ai[aipos].visit_cnt++;
468 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
469 stepsize = ai[aipos--].size;
474 assert(aipos >= 0 && "couldn't store entity");
475 vals[offset] = get_compound_ent_value(ent, i);
482 /* now write them sorted */
483 for(i = 0; i < type_size; ) {
485 dump_atomic_init(obst, vals[i]);
486 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
490 obstack_printf(obst, "\t.byte\t0\n");
497 assert(0 && "unsupported type");
500 obstack_printf(obst, "\n");
502 else if (visibility != visibility_external_allocated) {
503 if (visibility == visibility_local) {
504 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
507 /* calculate the alignment */
508 align = get_type_alignment_bytes(ty);
509 h = highest_bit(align);
511 if ((1 << h) < align)
518 ia32_dump_comm(comm_obstack, ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
524 * Dumps declarations of global variables and the initialization code.
526 void ia32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
528 ir_type *gt = get_glob_type();
529 int i, n = get_class_n_members(gt);
531 for (i = 0; i < n; i++)
532 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
535 /************************************************************************/
537 void ia32_gen_decls(FILE *out) {
538 struct obstack rodata, data, comm;
542 obstack_init(&rodata);
546 ia32_dump_globals(&rodata, &data, &comm);
548 size = obstack_object_size(&data);
549 cp = obstack_finish(&data);
551 ia32_switch_section(out, SECTION_DATA);
552 fwrite(cp, 1, size, out);
555 size = obstack_object_size(&rodata);
556 cp = obstack_finish(&rodata);
558 fprintf(out, "\t.section\t.rodata\n");
559 fwrite(cp, 1, size, out);
562 size = obstack_object_size(&comm);
563 cp = obstack_finish(&comm);
565 ia32_switch_section(out, SECTION_COMMON);
566 fwrite(cp, 1, size, out);
569 obstack_free(&rodata, NULL);
570 obstack_free(&data, NULL);
571 obstack_free(&comm, NULL);