2 * Dumps global variables and constants as mips assembler.
20 #include "mips_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)) {
58 * output the alignment
60 static void mips_dump_align(struct obstack *obst, int align)
62 int h = highest_bit(align);
69 obstack_printf(obst, "\t.align %d\n", align);
72 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
77 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
81 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
85 obstack_printf(obst, "0x%02x%02x%02x%02x",
86 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
90 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
91 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
92 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 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
106 * dump an atomic value
108 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
110 ir_mode *mode = get_irn_mode(init);
111 int bytes = get_mode_size_bytes(mode);
114 switch (get_irn_opcode(init)) {
117 do_dump_atomic_init(obst, get_Cast_op(init));
121 do_dump_atomic_init(obst, get_Conv_op(init));
125 tv = get_Const_tarval(init);
127 /* beware of old stuff */
128 assert(! mode_is_reference(mode));
130 /* it's a arithmetic value */
131 dump_arith_tarval(obst, tv, bytes);
135 switch (get_SymConst_kind(init)) {
136 case symconst_addr_name:
137 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
140 case symconst_addr_ent:
141 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
144 case symconst_type_size:
145 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
148 case symconst_type_align:
149 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
152 case symconst_enum_const:
153 tv = get_enumeration_value(get_SymConst_enum(init));
154 dump_arith_tarval(obst, tv, bytes);
158 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
163 do_dump_atomic_init(obst, get_Add_left(init));
164 obstack_printf(obst, " + ");
165 do_dump_atomic_init(obst, get_Add_right(init));
169 do_dump_atomic_init(obst, get_Sub_left(init));
170 obstack_printf(obst, " - ");
171 do_dump_atomic_init(obst, get_Sub_right(init));
175 do_dump_atomic_init(obst, get_Mul_left(init));
176 obstack_printf(obst, " * ");
177 do_dump_atomic_init(obst, get_Mul_right(init));
181 assert(0 && "dump_atomic_init(): unknown IR-node");
186 * dump an atomic value
188 static void dump_atomic_init(struct obstack *obst, ir_node *init)
190 ir_mode *mode = get_irn_mode(init);
191 int bytes = get_mode_size_bytes(mode);
196 obstack_printf(obst, "\t.byte\t");
200 obstack_printf(obst, "\t.half\t");
204 obstack_printf(obst, "\t.word\t");
208 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
212 do_dump_atomic_init(obst, init);
213 obstack_printf(obst, "\n");
216 /************************************************************************/
217 /* Routines to dump global variables */
218 /************************************************************************/
221 * Determine if an entity is a string constant
222 * @param ent The entity
223 * @return 1 if it is a string constant, 0 otherwise
225 static int ent_is_string_const(entity *ent)
230 ty = get_entity_type(ent);
232 /* if it's an array */
233 if (is_Array_type(ty)) {
234 ir_type *elm_ty = get_array_element_type(ty);
236 /* and the array's element type is primitive */
237 if (is_Primitive_type(elm_ty)) {
238 ir_mode *mode = get_type_mode(elm_ty);
241 * and the mode of the element type is an int of
242 * the same size as the byte mode
244 if (mode_is_int(mode)
245 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
249 n = get_compound_ent_n_values(ent);
250 for (i = 0; i < n; ++i) {
251 ir_node *irn = get_compound_ent_value(ent, i);
252 if(get_irn_opcode(irn) != iro_Const)
255 c = (int) get_tarval_long(get_Const_tarval(irn));
257 if((i < n - 1 && !(isgraph(c) || isspace(c)))
258 || (i == n - 1 && c != '\0'))
271 * Dump a atring constant.
272 * No checks are made!!
273 * @param obst The obst to dump on.
274 * @param ent The entity to dump.
276 static void dump_string_cst(struct obstack *obst, entity *ent)
280 obstack_printf(obst, "\t.asciiz \"");
281 n = get_compound_ent_n_values(ent);
283 for (i = 0; i < n-1; ++i) {
287 irn = get_compound_ent_value(ent, i);
288 c = (int) get_tarval_long(get_Const_tarval(irn));
291 case '"' : obstack_printf(obst, "\\\""); break;
292 case '\n': obstack_printf(obst, "\\n"); break;
293 case '\r': obstack_printf(obst, "\\r"); break;
294 case '\t': obstack_printf(obst, "\\t"); break;
297 obstack_printf(obst, "%c", c);
299 obstack_printf(obst, "%O", c);
303 obstack_printf(obst, "\"\n");
313 * Dumps the initialization of global variables that are not
316 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
318 ir_type *ty = get_entity_type(ent);
319 const char *ld_name = get_entity_ld_name(ent);
321 struct obstack *obst = data_obstack;
324 * FIXME: did NOT work for partly constant values
326 if (! is_Method_type(ty)) {
327 ent_variability variability = get_entity_variability(ent);
328 visibility visibility = get_entity_visibility(ent);
330 if (variability == variability_constant) {
331 /* a constant entity, put it on the rdata */
332 obst = rdata_obstack;
335 /* check, wether it is initialized, if yes create data */
336 if (variability != variability_uninitialized) {
337 if (visibility == visibility_external_visible) {
338 obstack_printf(obst, ".globl\t%s\n", ld_name);
341 align = get_type_alignment_bytes(ty);
342 mips_dump_align(obst, align);
344 obstack_printf(obst, "%s:\n", ld_name);
346 if (is_atomic_type(ty)) {
347 if (get_entity_visibility(ent) != visibility_external_allocated)
348 dump_atomic_init(obst, get_atomic_ent_value(ent));
353 if (ent_is_string_const(ent)) {
354 dump_string_cst(obst, ent);
356 else if (is_Array_type(ty)) {
359 /* potential spare values should be already included! */
360 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
361 entity *step = get_compound_ent_value_member(ent, i);
362 ir_type *stype = get_entity_type(step);
364 if (get_type_mode(stype)) {
365 int align = (get_type_alignment_bits(stype) + 7) >> 3;
366 int n = size % align;
369 obstack_printf(obst, "\t.space\t%d\n", align - n);
373 dump_atomic_init(obst, get_compound_ent_value(ent, i));
374 size += get_type_size_bytes(stype);
376 filler = get_type_size_bytes(ty) - size;
379 obstack_printf(obst, "\t.space\t%d\n", filler);
381 else if (is_compound_type(ty)) {
385 /* Compound entities are NOT sorted.
386 * The sorting strategy used doesn't work for `value' compound fields nor
387 * for partially_constant entities.
391 * in the worst case, every entity allocates one byte, so the type
392 * size should be equal or bigger the number of fields
394 type_size = get_type_size_bytes(ty);
395 vals = xcalloc(type_size, sizeof(*vals));
397 /* collect the values and store them at the offsets */
398 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
399 int graph_length, aipos, offset;
402 compound_graph_path *path = get_compound_ent_value_path(ent, i);
404 /* get the access path to the costant value */
405 graph_length = get_compound_graph_path_length(path);
406 ai = xcalloc(graph_length, sizeof(struct arr_info));
408 /* We wanna know how many arrays are on the path to the entity. We also have to know how
409 * many elements each array holds to calculate the offset for the entity. */
410 for (j = 0; j < graph_length; j++) {
411 entity *step = get_compound_graph_path_node(path, j);
412 ir_type *step_type = get_entity_type(step);
413 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
416 if (is_Array_type(step_type))
417 for (k = 0; k < get_array_n_dimensions(step_type); k++)
418 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
420 ai[j].n_elems = n ? all_n + 1 : 0;
422 ai[j].size = ty_size;
425 aipos = graph_length - 1;
428 for (offset = j = 0; j < graph_length; j++) {
429 entity *step = get_compound_graph_path_node(path, j);
430 ir_type *step_type = get_entity_type(step);
431 int ent_ofs = get_entity_offset_bytes(step);
434 /* add all positive offsets (= offsets in structs) */
435 if (ent_ofs >= 0) offset += ent_ofs;
437 if (j == graph_length - 1) {
438 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
440 /* Search the next free position in vals depending on the information from above (ai). */
441 while (vals[offset]) {
442 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
444 ai[aipos].visit_cnt++;
447 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
448 stepsize = ai[aipos--].size;
453 assert(aipos >= 0 && "couldn't store entity");
454 vals[offset] = get_compound_ent_value(ent, i);
461 /* now write them sorted */
462 for(i = 0; i < type_size; ) {
464 dump_atomic_init(obst, vals[i]);
465 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
469 obstack_printf(obst, "\t.byte\t0\n");
476 assert(0 && "unsupported type");
479 obstack_printf(obst, "\n");
481 else if (visibility != visibility_external_allocated) {
482 if (visibility == visibility_local) {
483 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
486 /* calculate the alignment */
487 align = get_type_alignment_bytes(ty);
488 h = highest_bit(align);
490 if ((1 << h) < align)
497 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
503 * Dumps declarations of global variables and the initialization code.
505 void mips_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
507 ir_type *gt = get_glob_type();
508 int i, n = get_class_n_members(gt);
510 for (i = 0; i < n; i++)
511 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
515 static void mips_emit_stdlib_call(FILE *F, const char* name, int num) {
516 fprintf(F, "%s:\n", name);
517 fprintf(F, "\tori $v0, $zero, %d\n", num);
518 fprintf(F, "\tsyscall\n");
519 fprintf(F, "\tj $ra\n");
524 * Emits a default library for spim... Hack for now...
526 static void mips_emit_standard_lib(FILE* F) {
527 static int output = 0;
532 mips_emit_stdlib_call(F, "print_int", 1);
533 mips_emit_stdlib_call(F, "print_string", 4);
534 mips_emit_stdlib_call(F, "read_int", 5);
535 mips_emit_stdlib_call(F, "read_string", 8);
536 mips_emit_stdlib_call(F, "sbrk", 9);
537 mips_emit_stdlib_call(F, "exit", 10);
540 /************************************************************************/
542 void mips_gen_decls(FILE *out) {
543 struct obstack rodata, data, comm;
547 obstack_init(&rodata);
551 mips_dump_globals(&rodata, &data, &comm);
553 size = obstack_object_size(&data);
554 cp = obstack_finish(&data);
556 fprintf(out, "\t.data\n");
557 fwrite(cp, 1, size, out);
560 size = obstack_object_size(&rodata);
561 cp = obstack_finish(&rodata);
563 fprintf(out, "\t.data\n");
564 fwrite(cp, 1, size, out);
567 size = obstack_object_size(&comm);
568 cp = obstack_finish(&comm);
570 fprintf(out, "\t.data\n");
571 fwrite(cp, 1, size, out);
574 obstack_free(&rodata, NULL);
575 obstack_free(&data, NULL);
576 obstack_free(&comm, NULL);
578 fprintf(out, "\t.text\n");
580 mips_emit_standard_lib(out);