updating to the latest module.
[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* const
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_name (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         const char *tmp = g_get_tmp_dir ();
62         const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
63         char *as_file;
64         char *o_file;
65         char *cmd;
66         
67         as_file = g_strdup_printf ("%s/test.s", tmp);    
68
69         if (!(ofd = fopen (as_file, "w")))
70                 g_assert_not_reached ();
71
72         for (i = 0; id [i]; ++i) {
73                 if (!isalnum (id [i]))
74                         fprintf (ofd, "_");
75                 else
76                         fprintf (ofd, "%c", id [i]);
77         }
78         fprintf (ofd, ":\n");
79
80         for (i = 0; i < size; ++i) 
81                 fprintf (ofd, ".byte %d\n", (unsigned int) code [i]);
82
83         fclose (ofd);
84 #ifdef __APPLE__
85 #define DIS_CMD "otool -v -t"
86 #else
87 #if defined(sparc) && !defined(__GNUC__)
88 #define DIS_CMD "dis"
89 #else
90 #define DIS_CMD "objdump -d"
91 #endif
92 #endif
93 #if defined(sparc)
94 #define AS_CMD "as -xarch=v9"
95 #else
96 #define AS_CMD "as"
97 #endif
98
99         o_file = g_strdup_printf ("%s/test.o", tmp);    
100         cmd = g_strdup_printf (AS_CMD " %s -o %s", as_file, o_file);
101         system (cmd); 
102         g_free (cmd);
103         if (!objdump_args)
104                 objdump_args = "";
105         
106         cmd = g_strdup_printf (DIS_CMD " %s %s", objdump_args, o_file);
107         system (cmd);
108         g_free (cmd);
109         
110         g_free (o_file);
111         g_free (as_file);
112 }
113