2 * Dumps global variables and constants as ia32 assembler.
3 * @author Christian Wuerdig
14 #ifdef obstack_chunk_alloc
15 # undef obstack_chunk_alloc
16 # define obstack_chunk_alloc malloc
18 # define obstack_chunk_alloc malloc
19 # define obstack_chunk_free free
27 #include "gen_decls.h"
29 /************************************************************************/
32 * returns the highest bit value
34 static unsigned highest_bit(unsigned v)
38 if (v >= (1U << 16U)) {
42 if (v >= (1U << 8U)) {
46 if (v >= (1U << 4U)) {
50 if (v >= (1U << 2U)) {
54 if (v >= (1U << 1U)) {
65 * output the alignment
67 void ia32_dump_align(struct obstack *obst, int align)
69 int h = highest_bit(align);
76 obstack_printf(obst, "\t.align %d\n", align);
79 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
84 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
88 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
92 obstack_printf(obst, "0x%02x%02x%02x%02x",
93 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
97 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
98 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
99 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
107 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
113 * dump an arithmetic tarval
115 void ia32_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
120 obstack_printf(obst, "\t.byte\t");
124 obstack_printf(obst, "\t.value\t");
128 obstack_printf(obst, "\t.long\t");
132 obstack_printf(obst, "\t.quad\t");
140 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
143 dump_arith_tarval(obst, tv, bytes);
148 * dump an atomic value
150 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
152 ir_mode *mode = get_irn_mode(init);
153 int bytes = get_mode_size_bytes(mode);
156 switch (get_irn_opcode(init)) {
159 do_dump_atomic_init(obst, get_Cast_op(init));
163 do_dump_atomic_init(obst, get_Conv_op(init));
167 tv = get_Const_tarval(init);
169 /* beware of old stuff */
170 assert(! mode_is_reference(mode));
172 /* it's a arithmetic value */
173 dump_arith_tarval(obst, tv, bytes);
177 switch (get_SymConst_kind(init)) {
178 case symconst_addr_name:
179 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
182 case symconst_addr_ent:
183 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
187 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
191 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
196 do_dump_atomic_init(obst, get_Add_left(init));
197 obstack_printf(obst, " + ");
198 do_dump_atomic_init(obst, get_Add_right(init));
202 do_dump_atomic_init(obst, get_Sub_left(init));
203 obstack_printf(obst, " - ");
204 do_dump_atomic_init(obst, get_Sub_right(init));
208 do_dump_atomic_init(obst, get_Mul_left(init));
209 obstack_printf(obst, " * ");
210 do_dump_atomic_init(obst, get_Mul_right(init));
214 assert(0 && "dump_atomic_init(): unknown IR-node");
219 * dump an atomic value
221 static void dump_atomic_init(struct obstack *obst, ir_node *init)
223 ir_mode *mode = get_irn_mode(init);
224 int bytes = get_mode_size_bytes(mode);
229 obstack_printf(obst, "\t.byte\t");
233 obstack_printf(obst, "\t.value\t");
237 obstack_printf(obst, "\t.long\t");
241 obstack_printf(obst, "\t.quad\t");
246 /* handled in arith */
250 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
254 do_dump_atomic_init(obst, init);
255 obstack_printf(obst, "\n");
258 /************************************************************************/
259 /* Routines to dump global variables */
260 /************************************************************************/
263 * Determine if an entity is a string constant
264 * @param ent The entity
265 * @return 1 if it is a string constant, 0 otherwise
267 static int ent_is_string_const(entity *ent)
272 ty = get_entity_type(ent);
274 /* if it's an array */
275 if (is_Array_type(ty)) {
276 type *elm_ty = get_array_element_type(ty);
278 /* and the array's alement type is primitive */
279 if (is_Primitive_type(elm_ty)) {
280 ir_mode *mode = get_type_mode(elm_ty);
283 * and the mode of the element type is an int of
284 * the same size as the byte mode
286 if (mode_is_int(mode)
287 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
291 n = get_compound_ent_n_values(ent);
292 for (i = 0; i < n; ++i) {
293 ir_node *irn = get_compound_ent_value(ent, i);
294 if(get_irn_opcode(irn) != iro_Const)
297 c = (int) get_tarval_long(get_Const_tarval(irn));
299 if((i < n - 1 && !(isgraph(c) || isspace(c)))
300 || (i == n - 1 && c != '\0'))
313 * Dump a atring constant.
314 * No checks are made!!
315 * @param obst The obst to dump on.
316 * @param ent The entity to dump.
318 static void dump_string_cst(struct obstack *obst, entity *ent)
322 obstack_printf(obst, "\t.string \"");
323 n = get_compound_ent_n_values(ent);
325 for (i = 0; i < n-1; ++i) {
329 irn = get_compound_ent_value(ent, i);
330 c = (int) get_tarval_long(get_Const_tarval(irn));
333 case '"' : obstack_printf(obst, "\\\""); break;
334 case '\n': obstack_printf(obst, "\\n"); break;
335 case '\r': obstack_printf(obst, "\\r"); break;
336 case '\t': obstack_printf(obst, "\\t"); break;
339 obstack_printf(obst, "%c", c);
341 obstack_printf(obst, "%O", c);
345 obstack_printf(obst, "\"\n");
355 * Dumps the initialization of global variables that are not
358 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
360 type *ty = get_entity_type(ent);
361 const char *ld_name = get_entity_ld_name(ent);
363 struct obstack *obst = data_obstack;
366 * FIXME: did NOT work for partly constant values
368 if (! is_Method_type(ty)) {
369 ent_variability variability = get_entity_variability(ent);
370 visibility visibility = get_entity_visibility(ent);
372 if (variability == variability_constant) {
373 /* a constant entity, put it on the rdata */
374 obst = rdata_obstack;
377 /* check, wether it is initialized, if yes create data */
378 if (variability != variability_uninitialized) {
379 if (visibility == visibility_external_visible) {
380 obstack_printf(obst, ".globl\t%s\n", ld_name);
382 obstack_printf(obst, "\t.type\t%s,@object\n", ld_name);
383 obstack_printf(obst, "\t.size\t%s,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3);
385 align = get_type_alignment_bytes(ty);
386 ia32_dump_align(obst, align);
388 obstack_printf(obst, "%s:\n", ld_name);
390 if (is_atomic_type(ty)) {
391 if (get_entity_visibility(ent) != visibility_external_allocated)
392 dump_atomic_init(obst, get_atomic_ent_value(ent));
397 if (ent_is_string_const(ent)) {
398 dump_string_cst(obst, ent);
400 else if (is_Array_type(ty)) {
403 /* potential spare values should be already included! */
404 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
405 entity *step = get_compound_ent_value_member(ent, i);
406 type *stype = get_entity_type(step);
408 if (get_type_mode(stype)) {
409 int align = (get_type_alignment_bits(stype) + 7) >> 3;
410 int n = size % align;
413 obstack_printf(obst, "\t.zero\t%d\n", align - n);
417 dump_atomic_init(obst, get_compound_ent_value(ent, i));
418 size += get_type_size_bytes(stype);
420 filler = get_type_size_bytes(ty) - size;
423 obstack_printf(obst, "\t.zero\t%d\n", filler);
425 else if (is_compound_type(ty)) {
429 /* Compound entities are NOT sorted.
430 * The sorting strategy used doesn't work for `value' compound fields nor
431 * for partially_constant entities.
435 * in the worst case, every entity allocates one byte, so the type
436 * size should be equal or bigger the number of fields
438 type_size = get_type_size_bytes(ty);
439 vals = calloc(type_size, sizeof(*vals));
441 /* collect the values and store them at the offsets */
442 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
443 int graph_length, aipos, offset;
446 compound_graph_path *path = get_compound_ent_value_path(ent, i);
448 /* get the access path to the costant value */
449 graph_length = get_compound_graph_path_length(path);
450 ai = calloc(graph_length, sizeof(struct arr_info));
452 /* We wanna know how many arrays are on the path to the entity. We also have to know how
453 * many elements each array holds to calculate the offset for the entity. */
454 for (j = 0; j < graph_length; j++) {
455 entity *step = get_compound_graph_path_node(path, j);
456 type *step_type = get_entity_type(step);
457 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
460 if (is_Array_type(step_type))
461 for (k = 0; k < get_array_n_dimensions(step_type); k++)
462 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
464 ai[j].n_elems = n ? all_n + 1 : 0;
466 ai[j].size = ty_size;
469 aipos = graph_length - 1;
472 for (offset = j = 0; j < graph_length; j++) {
473 entity *step = get_compound_graph_path_node(path, j);
474 type *step_type = get_entity_type(step);
475 int ent_ofs = get_entity_offset_bytes(step);
478 /* add all positive offsets (= offsets in structs) */
479 if (ent_ofs >= 0) offset += ent_ofs;
481 if (j == graph_length - 1) {
482 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
484 /* Search the next free position in vals depending on the information from above (ai). */
485 while (vals[offset]) {
486 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
488 ai[aipos].visit_cnt++;
491 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
492 stepsize = ai[aipos--].size;
497 assert(aipos >= 0 && "couldn't store entity");
498 vals[offset] = get_compound_ent_value(ent, i);
505 /* now write them sorted */
506 for(i = 0; i < type_size; ) {
508 dump_atomic_init(obst, vals[i]);
509 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
513 obstack_printf(obst, "\t.byte\t0\n");
520 assert(0 && "unsupported type");
523 obstack_printf(obst, "\n");
525 else if (visibility != visibility_external_allocated) {
526 if (visibility == visibility_local) {
527 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
530 /* calculate the alignment */
531 align = get_type_alignment_bytes(ty);
532 h = highest_bit(align);
534 if ((1 << h) < align)
541 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
547 * Dumps declarations of global variables and the initialization code.
549 void ia32_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
551 type *gt = get_glob_type();
552 int i, n = get_class_n_members(gt);
554 for (i = 0; i < n; i++)
555 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
558 /************************************************************************/
560 void firmbe_gen_decls(FILE *out) {
561 struct obstack rodata, data, comm;
565 obstack_init(&rodata);
569 ia32_dump_globals(&rodata, &data, &comm);
571 size = obstack_object_size(&data);
572 cp = obstack_finish(&data);
574 fprintf(out, "\t.data\n");
575 fwrite(cp, 1, size, out);
578 size = obstack_object_size(&rodata);
579 cp = obstack_finish(&rodata);
581 fprintf(out, "\t.section\t.rodata\n");
582 fwrite(cp, 1, size, out);
585 size = obstack_object_size(&comm);
586 cp = obstack_finish(&comm);
588 fprintf(out, "\t.common\n");
589 fwrite(cp, 1, size, out);
592 obstack_free(&rodata, NULL);
593 obstack_free(&data, NULL);
594 obstack_free(&comm, NULL);