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