2 * Dumps global variables and constants as mips assembler.
24 #include "mips_gen_decls.h"
26 /************************************************************************/
29 * returns the highest bit value
31 static unsigned highest_bit(unsigned v)
35 if (v >= (1U << 16U)) {
39 if (v >= (1U << 8U)) {
43 if (v >= (1U << 4U)) {
47 if (v >= (1U << 2U)) {
51 if (v >= (1U << 1U)) {
62 * output the alignment
64 static void mips_dump_align(struct obstack *obst, int align)
66 int h = highest_bit(align);
73 obstack_printf(obst, "\t.align %d\n", align);
76 static void dump_arith_tarval(struct obstack *obst, tarval *tv, int bytes)
81 obstack_printf(obst, "0x%02x", get_tarval_sub_bits(tv, 0));
85 obstack_printf(obst, "0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
89 obstack_printf(obst, "0x%02x%02x%02x%02x",
90 get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
94 obstack_printf(obst, "0x%02x%02x%02x%02x%02x%02x%02x%02x",
95 get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
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));
104 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
110 * dump an atomic value
112 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
114 ir_mode *mode = get_irn_mode(init);
115 int bytes = get_mode_size_bytes(mode);
118 switch (get_irn_opcode(init)) {
121 do_dump_atomic_init(obst, get_Cast_op(init));
125 do_dump_atomic_init(obst, get_Conv_op(init));
129 tv = get_Const_tarval(init);
131 /* beware of old stuff */
132 assert(! mode_is_reference(mode));
134 /* it's a arithmetic value */
135 dump_arith_tarval(obst, tv, bytes);
139 switch (get_SymConst_kind(init)) {
140 case symconst_addr_name:
141 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
144 case symconst_addr_ent:
145 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
148 case symconst_ofs_ent:
149 obstack_printf(obst, "%d", get_entity_offset(get_SymConst_entity(init)));
152 case symconst_type_size:
153 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
156 case symconst_type_align:
157 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
160 case symconst_enum_const:
161 tv = get_enumeration_value(get_SymConst_enum(init));
162 dump_arith_tarval(obst, tv, bytes);
166 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
171 do_dump_atomic_init(obst, get_Add_left(init));
172 obstack_printf(obst, " + ");
173 do_dump_atomic_init(obst, get_Add_right(init));
177 do_dump_atomic_init(obst, get_Sub_left(init));
178 obstack_printf(obst, " - ");
179 do_dump_atomic_init(obst, get_Sub_right(init));
183 do_dump_atomic_init(obst, get_Mul_left(init));
184 obstack_printf(obst, " * ");
185 do_dump_atomic_init(obst, get_Mul_right(init));
189 assert(0 && "dump_atomic_init(): unknown IR-node");
194 * dump an atomic value
196 static void dump_atomic_init(struct obstack *obst, ir_node *init)
198 ir_mode *mode = get_irn_mode(init);
199 int bytes = get_mode_size_bytes(mode);
204 obstack_printf(obst, "\t.byte\t");
208 obstack_printf(obst, "\t.half\t");
212 obstack_printf(obst, "\t.word\t");
216 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
220 do_dump_atomic_init(obst, init);
221 obstack_printf(obst, "\n");
224 /************************************************************************/
225 /* Routines to dump global variables */
226 /************************************************************************/
229 * Determine if an entity is a string constant
230 * @param ent The entity
231 * @return 1 if it is a string constant, 0 otherwise
233 static int ent_is_string_const(entity *ent)
238 ty = get_entity_type(ent);
240 /* if it's an array */
241 if (is_Array_type(ty)) {
242 ir_type *elm_ty = get_array_element_type(ty);
244 /* and the array's element type is primitive */
245 if (is_Primitive_type(elm_ty)) {
246 ir_mode *mode = get_type_mode(elm_ty);
249 * and the mode of the element type is an int of
250 * the same size as the byte mode
252 if (mode_is_int(mode)
253 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
257 n = get_compound_ent_n_values(ent);
258 for (i = 0; i < n; ++i) {
259 ir_node *irn = get_compound_ent_value(ent, i);
260 if(get_irn_opcode(irn) != iro_Const)
263 c = (int) get_tarval_long(get_Const_tarval(irn));
265 if((i < n - 1 && !(isgraph(c) || isspace(c)))
266 || (i == n - 1 && c != '\0'))
279 * Dump a atring constant.
280 * No checks are made!!
281 * @param obst The obst to dump on.
282 * @param ent The entity to dump.
284 static void dump_string_cst(struct obstack *obst, entity *ent)
288 obstack_printf(obst, "\t.asciiz \"");
289 n = get_compound_ent_n_values(ent);
291 for (i = 0; i < n-1; ++i) {
295 irn = get_compound_ent_value(ent, i);
296 c = (int) get_tarval_long(get_Const_tarval(irn));
299 case '"' : obstack_printf(obst, "\\\""); break;
300 case '\n': obstack_printf(obst, "\\n"); break;
301 case '\r': obstack_printf(obst, "\\r"); break;
302 case '\t': obstack_printf(obst, "\\t"); break;
305 obstack_printf(obst, "%c", c);
307 obstack_printf(obst, "%O", c);
311 obstack_printf(obst, "\"\n");
321 * Dumps the initialization of global variables that are not
324 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
326 ir_type *ty = get_entity_type(ent);
327 const char *ld_name = get_entity_ld_name(ent);
329 struct obstack *obst = data_obstack;
332 * FIXME: did NOT work for partly constant values
334 if (! is_Method_type(ty)) {
335 ir_variability variability = get_entity_variability(ent);
336 ir_visibility visibility = get_entity_visibility(ent);
338 if (variability == variability_constant) {
339 /* a constant entity, put it on the rdata */
340 obst = rdata_obstack;
343 /* check, wether it is initialized, if yes create data */
344 if (variability != variability_uninitialized) {
345 if (visibility == visibility_external_visible) {
346 obstack_printf(obst, ".globl\t%s\n", ld_name);
349 align = get_type_alignment_bytes(ty);
350 mips_dump_align(obst, align);
352 obstack_printf(obst, "%s:\n", ld_name);
354 if (is_atomic_type(ty)) {
355 if (get_entity_visibility(ent) != visibility_external_allocated)
356 dump_atomic_init(obst, get_atomic_ent_value(ent));
361 if (ent_is_string_const(ent)) {
362 dump_string_cst(obst, ent);
364 else if (is_Array_type(ty)) {
367 /* potential spare values should be already included! */
368 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
369 entity *step = get_compound_ent_value_member(ent, i);
370 ir_type *stype = get_entity_type(step);
372 if (get_type_mode(stype)) {
373 int align = (get_type_alignment_bits(stype) + 7) >> 3;
374 int n = size % align;
377 obstack_printf(obst, "\t.space\t%d\n", align - n);
381 dump_atomic_init(obst, get_compound_ent_value(ent, i));
382 size += get_type_size_bytes(stype);
384 filler = get_type_size_bytes(ty) - size;
387 obstack_printf(obst, "\t.space\t%d\n", filler);
389 else if (is_compound_type(ty)) {
393 /* Compound entities are NOT sorted.
394 * The sorting strategy used doesn't work for `value' compound fields nor
395 * for partially_constant entities.
399 * in the worst case, every entity allocates one byte, so the type
400 * size should be equal or bigger the number of fields
402 type_size = get_type_size_bytes(ty);
403 vals = xcalloc(type_size, sizeof(*vals));
405 /* collect the values and store them at the offsets */
406 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
407 int graph_length, aipos, offset;
410 compound_graph_path *path = get_compound_ent_value_path(ent, i);
412 /* get the access path to the costant value */
413 graph_length = get_compound_graph_path_length(path);
414 ai = xcalloc(graph_length, sizeof(struct arr_info));
416 /* We wanna know how many arrays are on the path to the entity. We also have to know how
417 * many elements each array holds to calculate the offset for the entity. */
418 for (j = 0; j < graph_length; j++) {
419 entity *step = get_compound_graph_path_node(path, j);
420 ir_type *step_type = get_entity_type(step);
421 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
424 if (is_Array_type(step_type))
425 for (k = 0; k < get_array_n_dimensions(step_type); k++)
426 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
428 ai[j].n_elems = n ? all_n + 1 : 0;
430 ai[j].size = ty_size;
433 aipos = graph_length - 1;
436 for (offset = j = 0; j < graph_length; j++) {
437 entity *step = get_compound_graph_path_node(path, j);
438 ir_type *step_type = get_entity_type(step);
439 int ent_ofs = get_entity_offset(step);
442 /* add all positive offsets (= offsets in structs) */
443 if (ent_ofs >= 0) offset += ent_ofs;
445 if (j == graph_length - 1) {
446 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
448 /* Search the next free position in vals depending on the information from above (ai). */
449 while (vals[offset]) {
450 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
452 ai[aipos].visit_cnt++;
455 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
456 stepsize = ai[aipos--].size;
461 assert(aipos >= 0 && "couldn't store entity");
462 vals[offset] = get_compound_ent_value(ent, i);
469 /* now write them sorted */
470 for(i = 0; i < type_size; ) {
472 dump_atomic_init(obst, vals[i]);
473 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
477 obstack_printf(obst, "\t.byte\t0\n");
484 assert(0 && "unsupported type");
487 obstack_printf(obst, "\n");
489 else if (visibility != visibility_external_allocated) {
490 if (visibility == visibility_local) {
491 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
494 /* calculate the alignment */
495 align = get_type_alignment_bytes(ty);
496 h = highest_bit(align);
498 if ((1 << h) < align)
505 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
511 * Dumps declarations of global variables and the initialization code.
513 void mips_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
515 ir_type *gt = get_glob_type();
516 int i, n = get_class_n_members(gt);
518 for (i = 0; i < n; i++)
519 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
523 static void mips_emit_stdlib_call(FILE *F, const char* name, int num) {
524 fprintf(F, "%s:\n", name);
525 fprintf(F, "\tori $v0, $zero, %d\n", num);
526 fprintf(F, "\tsyscall\n");
527 fprintf(F, "\tj $ra\n");
532 * Emits a default library for spim... Hack for now...
534 static void mips_emit_standard_lib(FILE* F) {
535 static int output = 0;
540 mips_emit_stdlib_call(F, "print_int", 1);
541 mips_emit_stdlib_call(F, "print_string", 4);
542 mips_emit_stdlib_call(F, "read_int", 5);
543 mips_emit_stdlib_call(F, "read_string", 8);
544 mips_emit_stdlib_call(F, "sbrk", 9);
545 mips_emit_stdlib_call(F, "exit", 10);
548 /************************************************************************/
550 void mips_gen_decls(FILE *out) {
551 struct obstack rodata, data, comm;
555 obstack_init(&rodata);
559 mips_dump_globals(&rodata, &data, &comm);
561 size = obstack_object_size(&data);
562 cp = obstack_finish(&data);
564 fprintf(out, "\t.data\n");
565 fwrite(cp, 1, size, out);
568 size = obstack_object_size(&rodata);
569 cp = obstack_finish(&rodata);
571 fprintf(out, "\t.data\n");
572 fwrite(cp, 1, size, out);
575 size = obstack_object_size(&comm);
576 cp = obstack_finish(&comm);
578 fprintf(out, "\t.data\n");
579 fwrite(cp, 1, size, out);
582 obstack_free(&rodata, NULL);
583 obstack_free(&data, NULL);
584 obstack_free(&comm, NULL);
586 fprintf(out, "\t.text\n");
588 mips_emit_standard_lib(out);