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 atomic value
118 static void do_dump_atomic_init(struct obstack *obst, ir_node *init)
120 ir_mode *mode = get_irn_mode(init);
121 int bytes = get_mode_size_bytes(mode);
124 switch (get_irn_opcode(init)) {
127 do_dump_atomic_init(obst, get_Cast_op(init));
131 do_dump_atomic_init(obst, get_Conv_op(init));
135 tv = get_Const_tarval(init);
137 /* beware of old stuff */
138 assert(! mode_is_reference(mode));
140 /* it's a arithmetic value */
141 dump_arith_tarval(obst, tv, bytes);
145 switch (get_SymConst_kind(init)) {
146 case symconst_addr_name:
147 obstack_printf(obst, "%s", get_id_str(get_SymConst_name(init)));
150 case symconst_addr_ent:
151 obstack_printf(obst, "%s", get_entity_ld_name(get_SymConst_entity(init)));
155 obstack_printf(obst, "%d", get_type_size_bytes(get_SymConst_type(init)));
159 assert(0 && "dump_atomic_init(): don't know how to init from this SymConst");
164 do_dump_atomic_init(obst, get_Add_left(init));
165 obstack_printf(obst, " + ");
166 do_dump_atomic_init(obst, get_Add_right(init));
170 do_dump_atomic_init(obst, get_Sub_left(init));
171 obstack_printf(obst, " - ");
172 do_dump_atomic_init(obst, get_Sub_right(init));
176 do_dump_atomic_init(obst, get_Mul_left(init));
177 obstack_printf(obst, " * ");
178 do_dump_atomic_init(obst, get_Mul_right(init));
182 assert(0 && "dump_atomic_init(): unknown IR-node");
187 * dump an atomic value
189 static void dump_atomic_init(struct obstack *obst, ir_node *init)
191 ir_mode *mode = get_irn_mode(init);
192 int bytes = get_mode_size_bytes(mode);
197 obstack_printf(obst, "\t.byte\t");
201 obstack_printf(obst, "\t.half\t");
205 obstack_printf(obst, "\t.word\t");
209 fprintf(stderr, "Try to dump an tarval with %d bytes\n", bytes);
213 do_dump_atomic_init(obst, init);
214 obstack_printf(obst, "\n");
217 /************************************************************************/
218 /* Routines to dump global variables */
219 /************************************************************************/
222 * Determine if an entity is a string constant
223 * @param ent The entity
224 * @return 1 if it is a string constant, 0 otherwise
226 static int ent_is_string_const(entity *ent)
231 ty = get_entity_type(ent);
233 /* if it's an array */
234 if (is_Array_type(ty)) {
235 ir_type *elm_ty = get_array_element_type(ty);
237 /* and the array's element type is primitive */
238 if (is_Primitive_type(elm_ty)) {
239 ir_mode *mode = get_type_mode(elm_ty);
242 * and the mode of the element type is an int of
243 * the same size as the byte mode
245 if (mode_is_int(mode)
246 && get_mode_size_bits(mode) == get_mode_size_bits(mode_Bs))
250 n = get_compound_ent_n_values(ent);
251 for (i = 0; i < n; ++i) {
252 ir_node *irn = get_compound_ent_value(ent, i);
253 if(get_irn_opcode(irn) != iro_Const)
256 c = (int) get_tarval_long(get_Const_tarval(irn));
258 if((i < n - 1 && !(isgraph(c) || isspace(c)))
259 || (i == n - 1 && c != '\0'))
272 * Dump a atring constant.
273 * No checks are made!!
274 * @param obst The obst to dump on.
275 * @param ent The entity to dump.
277 static void dump_string_cst(struct obstack *obst, entity *ent)
281 obstack_printf(obst, "\t.asciiz \"");
282 n = get_compound_ent_n_values(ent);
284 for (i = 0; i < n-1; ++i) {
288 irn = get_compound_ent_value(ent, i);
289 c = (int) get_tarval_long(get_Const_tarval(irn));
292 case '"' : obstack_printf(obst, "\\\""); break;
293 case '\n': obstack_printf(obst, "\\n"); break;
294 case '\r': obstack_printf(obst, "\\r"); break;
295 case '\t': obstack_printf(obst, "\\t"); break;
298 obstack_printf(obst, "%c", c);
300 obstack_printf(obst, "%O", c);
304 obstack_printf(obst, "\"\n");
314 * Dumps the initialization of global variables that are not
317 static void dump_global(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack, entity *ent)
319 ir_type *ty = get_entity_type(ent);
320 const char *ld_name = get_entity_ld_name(ent);
322 struct obstack *obst = data_obstack;
325 * FIXME: did NOT work for partly constant values
327 if (! is_Method_type(ty)) {
328 ent_variability variability = get_entity_variability(ent);
329 visibility visibility = get_entity_visibility(ent);
331 if (variability == variability_constant) {
332 /* a constant entity, put it on the rdata */
333 obst = rdata_obstack;
336 /* check, wether it is initialized, if yes create data */
337 if (variability != variability_uninitialized) {
338 if (visibility == visibility_external_visible) {
339 obstack_printf(obst, ".globl\t%s\n", ld_name);
342 align = get_type_alignment_bytes(ty);
343 mips_dump_align(obst, align);
345 obstack_printf(obst, "%s:\n", ld_name);
347 if (is_atomic_type(ty)) {
348 if (get_entity_visibility(ent) != visibility_external_allocated)
349 dump_atomic_init(obst, get_atomic_ent_value(ent));
354 if (ent_is_string_const(ent)) {
355 dump_string_cst(obst, ent);
357 else if (is_Array_type(ty)) {
360 /* potential spare values should be already included! */
361 for (i = 0; i < get_compound_ent_n_values(ent); ++i) {
362 entity *step = get_compound_ent_value_member(ent, i);
363 ir_type *stype = get_entity_type(step);
365 if (get_type_mode(stype)) {
366 int align = (get_type_alignment_bits(stype) + 7) >> 3;
367 int n = size % align;
370 obstack_printf(obst, "\t.space\t%d\n", align - n);
374 dump_atomic_init(obst, get_compound_ent_value(ent, i));
375 size += get_type_size_bytes(stype);
377 filler = get_type_size_bytes(ty) - size;
380 obstack_printf(obst, "\t.space\t%d\n", filler);
382 else if (is_compound_type(ty)) {
386 /* Compound entities are NOT sorted.
387 * The sorting strategy used doesn't work for `value' compound fields nor
388 * for partially_constant entities.
392 * in the worst case, every entity allocates one byte, so the type
393 * size should be equal or bigger the number of fields
395 type_size = get_type_size_bytes(ty);
396 vals = xcalloc(type_size, sizeof(*vals));
398 /* collect the values and store them at the offsets */
399 for(i = 0; i < get_compound_ent_n_values(ent); ++i) {
400 int graph_length, aipos, offset;
403 compound_graph_path *path = get_compound_ent_value_path(ent, i);
405 /* get the access path to the costant value */
406 graph_length = get_compound_graph_path_length(path);
407 ai = xcalloc(graph_length, sizeof(struct arr_info));
409 /* We wanna know how many arrays are on the path to the entity. We also have to know how
410 * many elements each array holds to calculate the offset for the entity. */
411 for (j = 0; j < graph_length; j++) {
412 entity *step = get_compound_graph_path_node(path, j);
413 ir_type *step_type = get_entity_type(step);
414 int ty_size = (get_type_size_bits(step_type) + 7) >> 3;
417 if (is_Array_type(step_type))
418 for (k = 0; k < get_array_n_dimensions(step_type); k++)
419 n += get_tarval_long(get_Const_tarval(get_array_upper_bound(step_type, k)));
421 ai[j].n_elems = n ? all_n + 1 : 0;
423 ai[j].size = ty_size;
426 aipos = graph_length - 1;
429 for (offset = j = 0; j < graph_length; j++) {
430 entity *step = get_compound_graph_path_node(path, j);
431 ir_type *step_type = get_entity_type(step);
432 int ent_ofs = get_entity_offset_bytes(step);
435 /* add all positive offsets (= offsets in structs) */
436 if (ent_ofs >= 0) offset += ent_ofs;
438 if (j == graph_length - 1) {
439 stepsize = (get_type_size_bits(step_type) + 7) >> 3;
441 /* Search the next free position in vals depending on the information from above (ai). */
442 while (vals[offset]) {
443 if (ai[aipos].visit_cnt < ai[aipos].n_elems) {
445 ai[aipos].visit_cnt++;
448 while (aipos >= 0 && ai[aipos].visit_cnt == ai[aipos].n_elems) {
449 stepsize = ai[aipos--].size;
454 assert(aipos >= 0 && "couldn't store entity");
455 vals[offset] = get_compound_ent_value(ent, i);
462 /* now write them sorted */
463 for(i = 0; i < type_size; ) {
465 dump_atomic_init(obst, vals[i]);
466 i += (get_mode_size_bytes(get_irn_mode(vals[i])));
470 obstack_printf(obst, "\t.byte\t0\n");
477 assert(0 && "unsupported type");
480 obstack_printf(obst, "\n");
482 else if (visibility != visibility_external_allocated) {
483 if (visibility == visibility_local) {
484 obstack_printf(comm_obstack, "\t.local\t%s\n", ld_name);
487 /* calculate the alignment */
488 align = get_type_alignment_bytes(ty);
489 h = highest_bit(align);
491 if ((1 << h) < align)
498 obstack_printf(comm_obstack, "\t.comm\t%s,%d,%d\n", ld_name, (get_type_size_bits(ty) + 7) >> 3, align);
504 * Dumps declarations of global variables and the initialization code.
506 void mips_dump_globals(struct obstack *rdata_obstack, struct obstack *data_obstack, struct obstack *comm_obstack)
508 ir_type *gt = get_glob_type();
509 int i, n = get_class_n_members(gt);
511 for (i = 0; i < n; i++)
512 dump_global(rdata_obstack, data_obstack, comm_obstack, get_class_member(gt, i));
516 static void mips_emit_stdlib_call(FILE *F, const char* name, int num) {
517 fprintf(F, "%s:\n", name);
518 fprintf(F, "\tori $v0, $zero, %d\n", num);
519 fprintf(F, "\tsyscall\n");
520 fprintf(F, "\tj $ra\n");
525 * Emits a default library for spim... Hack for now...
527 static void mips_emit_standard_lib(FILE* F) {
528 static int output = 0;
533 mips_emit_stdlib_call(F, "print_int", 1);
534 mips_emit_stdlib_call(F, "print_string", 4);
535 mips_emit_stdlib_call(F, "read_int", 5);
536 mips_emit_stdlib_call(F, "read_string", 8);
537 mips_emit_stdlib_call(F, "sbrk", 9);
538 mips_emit_stdlib_call(F, "exit", 10);
541 /************************************************************************/
543 void mips_gen_decls(FILE *out) {
544 struct obstack rodata, data, comm;
548 obstack_init(&rodata);
552 mips_dump_globals(&rodata, &data, &comm);
554 size = obstack_object_size(&data);
555 cp = obstack_finish(&data);
557 fprintf(out, "\t.data\n");
558 fwrite(cp, 1, size, out);
561 size = obstack_object_size(&rodata);
562 cp = obstack_finish(&rodata);
564 fprintf(out, "\t.data\n");
565 fwrite(cp, 1, size, out);
568 size = obstack_object_size(&comm);
569 cp = obstack_finish(&comm);
571 fprintf(out, "\t.data\n");
572 fwrite(cp, 1, size, out);
575 obstack_free(&rodata, NULL);
576 obstack_free(&data, NULL);
577 obstack_free(&comm, NULL);
579 fprintf(out, "\t.text\n");
581 mips_emit_standard_lib(out);