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_ofs_ent:
145 obstack_printf(obst, "%d", get_entity_offset_bytes(get_SymConst_entity(init)));
148 case symconst_type_size:
149 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
152 case symconst_type_align:
153 obstack_printf(obst, "%d", get_type_alignment_bytes(get_SymConst_type(init)));
156 case symconst_enum_const:
157 tv = get_enumeration_value(get_SymConst_enum(init));
158 dump_arith_tarval(obst, tv, bytes);
162 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
167 do_dump_atomic_init(obst, get_Add_left(init));
168 obstack_printf(obst, " + ");
169 do_dump_atomic_init(obst, get_Add_right(init));
173 do_dump_atomic_init(obst, get_Sub_left(init));
174 obstack_printf(obst, " - ");
175 do_dump_atomic_init(obst, get_Sub_right(init));
179 do_dump_atomic_init(obst, get_Mul_left(init));
180 obstack_printf(obst, " * ");
181 do_dump_atomic_init(obst, get_Mul_right(init));
185 assert(0 && "dump_atomic_init(): unknown IR-node");
190 * dump an atomic value
192 static void dump_atomic_init(struct obstack *obst, ir_node *init)
194 ir_mode *mode = get_irn_mode(init);
195 int bytes = get_mode_size_bytes(mode);
200 obstack_printf(obst, "\t.byte\t");
204 obstack_printf(obst, "\t.half\t");
208 obstack_printf(obst, "\t.word\t");
212 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
216 do_dump_atomic_init(obst, init);
217 obstack_printf(obst, "\n");
220 /************************************************************************/
221 /* Routines to dump global variables */
222 /************************************************************************/
225 * Determine if an entity is a string constant
226 * @param ent The entity
227 * @return 1 if it is a string constant, 0 otherwise
229 static int ent_is_string_const(entity *ent)
234 ty = get_entity_type(ent);
236 /* if it's an array */
237 if (is_Array_type(ty)) {
238 ir_type *elm_ty = get_array_element_type(ty);
240 /* and the array's element type is primitive */
241 if (is_Primitive_type(elm_ty)) {
242 ir_mode *mode = get_type_mode(elm_ty);
245 * and the mode of the element type is an int of
246 * the same size as the byte mode
248 if (mode_is_int(mode)
249 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
253 n = get_compound_ent_n_values(ent);
254 for (i = 0; i < n; ++i) {
255 ir_node *irn = get_compound_ent_value(ent, i);
256 if(get_irn_opcode(irn) != iro_Const)
259 c = (int) get_tarval_long(get_Const_tarval(irn));
261 if((i < n - 1 && !(isgraph(c) || isspace(c)))
262 || (i == n - 1 && c != '\0'))
275 * Dump a atring constant.
276 * No checks are made!!
277 * @param obst The obst to dump on.
278 * @param ent The entity to dump.
280 static void dump_string_cst(struct obstack *obst, entity *ent)
284 obstack_printf(obst, "\t.asciiz \"");
285 n = get_compound_ent_n_values(ent);
287 for (i = 0; i < n-1; ++i) {
291 irn = get_compound_ent_value(ent, i);
292 c = (int) get_tarval_long(get_Const_tarval(irn));
295 case '"' : obstack_printf(obst, "\\\""); break;
296 case '\n': obstack_printf(obst, "\\n"); break;
297 case '\r': obstack_printf(obst, "\\r"); break;
298 case '\t': obstack_printf(obst, "\\t"); break;
301 obstack_printf(obst, "%c", c);
303 obstack_printf(obst, "%O", c);
307 obstack_printf(obst, "\"\n");
317 * Dumps the initialization of global variables that are not
320 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
322 ir_type *ty = get_entity_type(ent);
323 const char *ld_name = get_entity_ld_name(ent);
325 struct obstack *obst = data_obstack;
328 * FIXME: did NOT work for partly constant values
330 if (! is_Method_type(ty)) {
331 ir_variability variability = get_entity_variability(ent);
332 ir_visibility visibility = get_entity_visibility(ent);
334 if (variability == variability_constant) {
335 /* a constant entity, put it on the rdata */
336 obst = rdata_obstack;
339 /* check, wether it is initialized, if yes create data */
340 if (variability != variability_uninitialized) {
341 if (visibility == visibility_external_visible) {
342 obstack_printf(obst, ".globl\t%s\n", ld_name);
345 align = get_type_alignment_bytes(ty);
346 mips_dump_align(obst, align);
348 obstack_printf(obst, "%s:\n", ld_name);
350 if (is_atomic_type(ty)) {
351 if (get_entity_visibility(ent) != visibility_external_allocated)
352 dump_atomic_init(obst, get_atomic_ent_value(ent));
357 if (ent_is_string_const(ent)) {
358 dump_string_cst(obst, ent);
360 else if (is_Array_type(ty)) {
363 /* potential spare values should be already included! */
364 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
365 entity *step = get_compound_ent_value_member(ent, i);
366 ir_type *stype = get_entity_type(step);
368 if (get_type_mode(stype)) {
369 int align = (get_type_alignment_bits(stype) + 7) >> 3;
370 int n = size % align;
373 obstack_printf(obst, "\t.space\t%d\n", align - n);
377 dump_atomic_init(obst, get_compound_ent_value(ent, i));
378 size += get_type_size_bytes(stype);
380 filler = get_type_size_bytes(ty) - size;
383 obstack_printf(obst, "\t.space\t%d\n", filler);
385 else if (is_compound_type(ty)) {
389 /* Compound entities are NOT sorted.
390 * The sorting strategy used doesn't work for `value' compound fields nor
391 * for partially_constant entities.
395 * in the worst case, every entity allocates one byte, so the type
396 * size should be equal or bigger the number of fields
398 type_size = get_type_size_bytes(ty);
399 vals = xcalloc(type_size, sizeof(*vals));
401 /* collect the values and store them at the offsets */
402 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
403 int graph_length, aipos, offset;
406 compound_graph_path *path = get_compound_ent_value_path(ent, i);
408 /* get the access path to the costant value */
409 graph_length = get_compound_graph_path_length(path);
410 ai = xcalloc(graph_length, sizeof(struct arr_info));
412 /* We wanna know how many arrays are on the path to the entity. We also have to know how
413 * many elements each array holds to calculate the offset for the entity. */
414 for (j = 0; j < graph_length; j++) {
415 entity *step = get_compound_graph_path_node(path, j);
416 ir_type *step_type = get_entity_type(step);
417 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
420 if (is_Array_type(step_type))
421 for (k = 0; k < get_array_n_dimensions(step_type); k++)
422 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
424 ai[j].n_elems = n ? all_n + 1 : 0;
426 ai[j].size = ty_size;
429 aipos = graph_length - 1;
432 for (offset = j = 0; j < graph_length; j++) {
433 entity *step = get_compound_graph_path_node(path, j);
434 ir_type *step_type = get_entity_type(step);
435 int ent_ofs = get_entity_offset_bytes(step);
438 /* add all positive offsets (= offsets in structs) */
439 if (ent_ofs >= 0) offset += ent_ofs;
441 if (j == graph_length - 1) {
442 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
444 /* Search the next free position in vals depending on the information from above (ai). */
445 while (vals[offset]) {
446 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
448 ai[aipos].visit_cnt++;
451 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
452 stepsize = ai[aipos--].size;
457 assert(aipos >= 0 && "couldn't store entity");
458 vals[offset] = get_compound_ent_value(ent, i);
465 /* now write them sorted */
466 for(i = 0; i < type_size; ) {
468 dump_atomic_init(obst, vals[i]);
469 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
473 obstack_printf(obst, "\t.byte\t0\n");
480 assert(0 && "unsupported type");
483 obstack_printf(obst, "\n");
485 else if (visibility != visibility_external_allocated) {
486 if (visibility == visibility_local) {
487 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
490 /* calculate the alignment */
491 align = get_type_alignment_bytes(ty);
492 h = highest_bit(align);
494 if ((1 << h) < align)
501 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
507 * Dumps declarations of global variables and the initialization code.
509 void mips_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
511 ir_type *gt = get_glob_type();
512 int i, n = get_class_n_members(gt);
514 for (i = 0; i < n; i++)
515 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
519 static void mips_emit_stdlib_call(FILE *F, const char* name, int num) {
520 fprintf(F, "%s:\n", name);
521 fprintf(F, "\tori $v0, $zero, %d\n", num);
522 fprintf(F, "\tsyscall\n");
523 fprintf(F, "\tj $ra\n");
528 * Emits a default library for spim... Hack for now...
530 static void mips_emit_standard_lib(FILE* F) {
531 static int output = 0;
536 mips_emit_stdlib_call(F, "print_int", 1);
537 mips_emit_stdlib_call(F, "print_string", 4);
538 mips_emit_stdlib_call(F, "read_int", 5);
539 mips_emit_stdlib_call(F, "read_string", 8);
540 mips_emit_stdlib_call(F, "sbrk", 9);
541 mips_emit_stdlib_call(F, "exit", 10);
544 /************************************************************************/
546 void mips_gen_decls(FILE *out) {
547 struct obstack rodata, data, comm;
551 obstack_init(&rodata);
555 mips_dump_globals(&rodata, &data, &comm);
557 size = obstack_object_size(&data);
558 cp = obstack_finish(&data);
560 fprintf(out, "\t.data\n");
561 fwrite(cp, 1, size, out);
564 size = obstack_object_size(&rodata);
565 cp = obstack_finish(&rodata);
567 fprintf(out, "\t.data\n");
568 fwrite(cp, 1, size, out);
571 size = obstack_object_size(&comm);
572 cp = obstack_finish(&comm);
574 fprintf(out, "\t.data\n");
575 fwrite(cp, 1, size, out);
578 obstack_free(&rodata, NULL);
579 obstack_free(&data, NULL);
580 obstack_free(&comm, NULL);
582 fprintf(out, "\t.text\n");
584 mips_emit_standard_lib(out);