2 * Dumps global variables and constants as mips assembler.
15 #ifdef obstack_chunk_alloc
16 # undef obstack_chunk_alloc
17 # define obstack_chunk_alloc xmalloc
19 # define obstack_chunk_alloc xmalloc
20 # define obstack_chunk_free free
28 #include "mips_gen_decls.h"
30 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
32 /************************************************************************/
35 * returns the highest bit value
37 static unsigned highest_bit(unsigned v)
41 if (v >= (1U << 16U)) {
45 if (v >= (1U << 8U)) {
49 if (v >= (1U << 4U)) {
53 if (v >= (1U << 2U)) {
57 if (v >= (1U << 1U)) {
68 * output the alignment
70 static void mips_dump_align(struct obstack *obst, int align)
72 int h = highest_bit(align);
79 obstack_printf(obst, "\t.align %d\n", align);
82 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
87 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
91 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
95 obstack_printf(obst, "0x%02x%02x%02x%02x",
96 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
100 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
101 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
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));
110 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
116 * dump an arithmetic tarval
118 static void mips_dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
123 obstack_printf(obst, "\t.byte\t");
127 obstack_printf(obst, "\t.half\t");
131 obstack_printf(obst, "\t.word\t");
135 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
138 dump_arith_tarval(obst, tv, bytes);
143 * dump an atomic value
145 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
147 ir_mode *mode = get_irn_mode(init);
148 int bytes = get_mode_size_bytes(mode);
151 switch (get_irn_opcode(init)) {
154 do_dump_atomic_init(obst, get_Cast_op(init));
158 do_dump_atomic_init(obst, get_Conv_op(init));
162 tv = get_Const_tarval(init);
164 /* beware of old stuff */
165 assert(! mode_is_reference(mode));
167 /* it's a arithmetic value */
168 dump_arith_tarval(obst, tv, bytes);
172 switch (get_SymConst_kind(init)) {
173 case symconst_addr_name:
174 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
177 case symconst_addr_ent:
178 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
182 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
186 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
191 do_dump_atomic_init(obst, get_Add_left(init));
192 obstack_printf(obst, " + ");
193 do_dump_atomic_init(obst, get_Add_right(init));
197 do_dump_atomic_init(obst, get_Sub_left(init));
198 obstack_printf(obst, " - ");
199 do_dump_atomic_init(obst, get_Sub_right(init));
203 do_dump_atomic_init(obst, get_Mul_left(init));
204 obstack_printf(obst, " * ");
205 do_dump_atomic_init(obst, get_Mul_right(init));
209 assert(0 && "dump_atomic_init(): unknown IR-node");
214 * dump an atomic value
216 static void dump_atomic_init(struct obstack *obst, ir_node *init)
218 ir_mode *mode = get_irn_mode(init);
219 int bytes = get_mode_size_bytes(mode);
224 obstack_printf(obst, "\t.byte\t");
228 obstack_printf(obst, "\t.half\t");
232 obstack_printf(obst, "\t.word\t");
236 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
240 do_dump_atomic_init(obst, init);
241 obstack_printf(obst, "\n");
244 /************************************************************************/
245 /* Routines to dump global variables */
246 /************************************************************************/
249 * Determine if an entity is a string constant
250 * @param ent The entity
251 * @return 1 if it is a string constant, 0 otherwise
253 static int ent_is_string_const(entity *ent)
258 ty = get_entity_type(ent);
260 /* if it's an array */
261 if (is_Array_type(ty)) {
262 ir_type *elm_ty = get_array_element_type(ty);
264 /* and the array's element type is primitive */
265 if (is_Primitive_type(elm_ty)) {
266 ir_mode *mode = get_type_mode(elm_ty);
269 * and the mode of the element type is an int of
270 * the same size as the byte mode
272 if (mode_is_int(mode)
273 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
277 n = get_compound_ent_n_values(ent);
278 for (i = 0; i < n; ++i) {
279 ir_node *irn = get_compound_ent_value(ent, i);
280 if(get_irn_opcode(irn) != iro_Const)
283 c = (int) get_tarval_long(get_Const_tarval(irn));
285 if((i < n - 1 && !(isgraph(c) || isspace(c)))
286 || (i == n - 1 && c != '\0'))
299 * Dump a atring constant.
300 * No checks are made!!
301 * @param obst The obst to dump on.
302 * @param ent The entity to dump.
304 static void dump_string_cst(struct obstack *obst, entity *ent)
308 obstack_printf(obst, "\t.asciiz \"");
309 n = get_compound_ent_n_values(ent);
311 for (i = 0; i < n-1; ++i) {
315 irn = get_compound_ent_value(ent, i);
316 c = (int) get_tarval_long(get_Const_tarval(irn));
319 case '"' : obstack_printf(obst, "\\\""); break;
320 case '\n': obstack_printf(obst, "\\n"); break;
321 case '\r': obstack_printf(obst, "\\r"); break;
322 case '\t': obstack_printf(obst, "\\t"); break;
325 obstack_printf(obst, "%c", c);
327 obstack_printf(obst, "%O", c);
331 obstack_printf(obst, "\"\n");
341 * Dumps the initialization of global variables that are not
344 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
346 ir_type *ty = get_entity_type(ent);
347 const char *ld_name = get_entity_ld_name(ent);
349 struct obstack *obst = data_obstack;
352 * FIXME: did NOT work for partly constant values
354 if (! is_Method_type(ty)) {
355 ent_variability variability = get_entity_variability(ent);
356 visibility visibility = get_entity_visibility(ent);
358 if (variability == variability_constant) {
359 /* a constant entity, put it on the rdata */
360 obst = rdata_obstack;
363 /* check, wether it is initialized, if yes create data */
364 if (variability != variability_uninitialized) {
365 if (visibility == visibility_external_visible) {
366 obstack_printf(obst, ".globl\t%s\n", ld_name);
369 align = get_type_alignment_bytes(ty);
370 mips_dump_align(obst, align);
372 obstack_printf(obst, "%s:\n", ld_name);
374 if (is_atomic_type(ty)) {
375 if (get_entity_visibility(ent) != visibility_external_allocated)
376 dump_atomic_init(obst, get_atomic_ent_value(ent));
381 if (ent_is_string_const(ent)) {
382 dump_string_cst(obst, ent);
384 else if (is_Array_type(ty)) {
387 /* potential spare values should be already included! */
388 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
389 entity *step = get_compound_ent_value_member(ent, i);
390 ir_type *stype = get_entity_type(step);
392 if (get_type_mode(stype)) {
393 int align = (get_type_alignment_bits(stype) + 7) >> 3;
394 int n = size % align;
397 obstack_printf(obst, "\t.space\t%d\n", align - n);
401 dump_atomic_init(obst, get_compound_ent_value(ent, i));
402 size += get_type_size_bytes(stype);
404 filler = get_type_size_bytes(ty) - size;
407 obstack_printf(obst, "\t.space\t%d\n", filler);
409 else if (is_compound_type(ty)) {
413 /* Compound entities are NOT sorted.
414 * The sorting strategy used doesn't work for `value' compound fields nor
415 * for partially_constant entities.
419 * in the worst case, every entity allocates one byte, so the type
420 * size should be equal or bigger the number of fields
422 type_size = get_type_size_bytes(ty);
423 vals = xcalloc(type_size, sizeof(*vals));
425 /* collect the values and store them at the offsets */
426 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
427 int graph_length, aipos, offset;
430 compound_graph_path *path = get_compound_ent_value_path(ent, i);
432 /* get the access path to the costant value */
433 graph_length = get_compound_graph_path_length(path);
434 ai = xcalloc(graph_length, sizeof(struct arr_info));
436 /* We wanna know how many arrays are on the path to the entity. We also have to know how
437 * many elements each array holds to calculate the offset for the entity. */
438 for (j = 0; j < graph_length; j++) {
439 entity *step = get_compound_graph_path_node(path, j);
440 ir_type *step_type = get_entity_type(step);
441 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
444 if (is_Array_type(step_type))
445 for (k = 0; k < get_array_n_dimensions(step_type); k++)
446 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
448 ai[j].n_elems = n ? all_n + 1 : 0;
450 ai[j].size = ty_size;
453 aipos = graph_length - 1;
456 for (offset = j = 0; j < graph_length; j++) {
457 entity *step = get_compound_graph_path_node(path, j);
458 ir_type *step_type = get_entity_type(step);
459 int ent_ofs = get_entity_offset_bytes(step);
462 /* add all positive offsets (= offsets in structs) */
463 if (ent_ofs >= 0) offset += ent_ofs;
465 if (j == graph_length - 1) {
466 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
468 /* Search the next free position in vals depending on the information from above (ai). */
469 while (vals[offset]) {
470 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
472 ai[aipos].visit_cnt++;
475 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
476 stepsize = ai[aipos--].size;
481 assert(aipos >= 0 && "couldn't store entity");
482 vals[offset] = get_compound_ent_value(ent, i);
489 /* now write them sorted */
490 for(i = 0; i < type_size; ) {
492 dump_atomic_init(obst, vals[i]);
493 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
497 obstack_printf(obst, "\t.byte\t0\n");
504 assert(0 && "unsupported type");
507 obstack_printf(obst, "\n");
509 else if (visibility != visibility_external_allocated) {
510 if (visibility == visibility_local) {
511 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
514 /* calculate the alignment */
515 align = get_type_alignment_bytes(ty);
516 h = highest_bit(align);
518 if ((1 << h) < align)
525 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
531 * Dumps declarations of global variables and the initialization code.
533 void mips_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
535 ir_type *gt = get_glob_type();
536 int i, n = get_class_n_members(gt);
538 for (i = 0; i < n; i++)
539 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
543 static void mips_emit_stdlib_call(FILE *F, const char* name, int num) {
544 fprintf(F, "%s:\n", name);
545 fprintf(F, "\tori $v0, $zero, %d\n", num);
546 fprintf(F, "\tsyscall\n");
547 fprintf(F, "\tj $ra\n");
552 * Emits a default library for spim... Hack for now...
554 static void mips_emit_standard_lib(FILE* F) {
555 static int output = 0;
560 mips_emit_stdlib_call(F, "print_int", 1);
561 mips_emit_stdlib_call(F, "print_string", 4);
562 mips_emit_stdlib_call(F, "read_int", 5);
563 mips_emit_stdlib_call(F, "read_string", 8);
564 mips_emit_stdlib_call(F, "sbrk", 9);
565 mips_emit_stdlib_call(F, "exit", 10);
568 /************************************************************************/
570 void mips_gen_decls(FILE *out) {
571 struct obstack rodata, data, comm;
575 obstack_init(&rodata);
579 mips_dump_globals(&rodata, &data, &comm);
581 size = obstack_object_size(&data);
582 cp = obstack_finish(&data);
584 fprintf(out, "\t.data\n");
585 fwrite(cp, 1, size, out);
588 size = obstack_object_size(&rodata);
589 cp = obstack_finish(&rodata);
591 fprintf(out, "\t.data\n");
592 fwrite(cp, 1, size, out);
595 size = obstack_object_size(&comm);
596 cp = obstack_finish(&comm);
598 fprintf(out, "\t.data\n");
599 fwrite(cp, 1, size, out);
602 obstack_free(&rodata, NULL);
603 obstack_free(&data, NULL);
604 obstack_free(&comm, NULL);
606 fprintf(out, "\t.text\n");
608 mips_emit_standard_lib(out);