Tue Aug 5 12:21:08 CEST 2003 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / mini / helpers.c
1 /*
2  * helpers.c: Assorted routines
3  *
4  * (C) 2003 Ximian, Inc.
5  */
6 #include "mini.h"
7 #include <ctype.h>
8 #include <mono/metadata/opcodes.h>
9
10 #ifdef MINI_OP
11 #undef MINI_OP
12 #endif
13 #define MINI_OP(a,b) b,
14 /* keep in sync with the enum in mini.h */
15 static const char* 
16 opnames[] = {
17 #include "mini-ops.h"
18 };
19 #undef MINI_OP
20
21 const char*
22 mono_inst_name (int op) {
23         if (op >= OP_LOAD && op <= OP_LAST)
24                 return opnames [op - OP_LOAD];
25         if (op < OP_LOAD)
26                 return mono_opcode_names [op];
27         g_error ("unknown opcode name for %d", op);
28         return NULL;
29 }
30
31 void
32 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom) 
33 {
34         int i;
35
36         if (name)
37                 g_print ("%s:", name);
38         
39         mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
40                 if (idom == i)
41                         g_print (" [BB%d]", cfg->bblocks [i]->block_num);
42                 else
43                         g_print (" BB%d", cfg->bblocks [i]->block_num);
44                 
45         }
46         g_print ("\n");
47 }
48
49 /**
50  * mono_disassemble_code:
51  * @code: a pointer to the code
52  * @size: the code size in bytes
53  *
54  * Disassemble to code to stdout.
55  */
56 void
57 mono_disassemble_code (guint8 *code, int size, char *id)
58 {
59         int i;
60         FILE *ofd;
61
62         if (!(ofd = fopen ("/tmp/test.s", "w")))
63                 g_assert_not_reached ();
64
65         for (i = 0; id [i]; ++i) {
66                 if (!isalnum (id [i]))
67                         fprintf (ofd, "_");
68                 else
69                         fprintf (ofd, "%c", id [i]);
70         }
71         fprintf (ofd, ":\n");
72
73         for (i = 0; i < size; ++i) 
74                 fprintf (ofd, ".byte %d\n", (unsigned int) code [i]);
75
76         fclose (ofd);
77 #ifdef __APPLE__
78 #define DIS_CMD "otool -V -v -t"
79 #else
80 #define DIS_CMD "objdump -d"
81 #endif
82         system ("as /tmp/test.s -o /tmp/test.o;" DIS_CMD " /tmp/test.o"); 
83 }
84