svn path=/trunk/mono/; revision=53658
[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 #ifdef __i386__
22 static gboolean emit_debug_info = TRUE;
23 #else
24 static gboolean emit_debug_info = FALSE;
25 #endif
26
27 const char*
28 mono_inst_name (int op) {
29         if (op >= OP_LOAD && op <= OP_LAST)
30                 return opnames [op - OP_LOAD];
31         if (op < OP_LOAD)
32                 return mono_opcode_name (op);
33         g_error ("unknown opcode name for %d", op);
34         return NULL;
35 }
36
37 void
38 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom) 
39 {
40         int i;
41
42         if (name)
43                 g_print ("%s:", name);
44         
45         mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
46                 if (idom == i)
47                         g_print (" [BB%d]", cfg->bblocks [i]->block_num);
48                 else
49                         g_print (" BB%d", cfg->bblocks [i]->block_num);
50                 
51         }
52         g_print ("\n");
53 }
54
55 /**
56  * mono_disassemble_code:
57  * @cfg: compilation context
58  * @code: a pointer to the code
59  * @size: the code size in bytes
60  *
61  * Disassemble to code to stdout.
62  */
63 void
64 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
65 {
66         GHashTable *offset_to_bb_hash = NULL;
67         int i, bb_num;
68         FILE *ofd;
69         const char *tmp = g_get_tmp_dir ();
70         const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
71         char *as_file;
72         char *o_file;
73         char *cmd;
74         
75         as_file = g_strdup_printf ("%s/test.s", tmp);    
76
77         if (!(ofd = fopen (as_file, "w")))
78                 g_assert_not_reached ();
79
80         for (i = 0; id [i]; ++i) {
81                 if (!isalnum (id [i]))
82                         fprintf (ofd, "_");
83                 else
84                         fprintf (ofd, "%c", id [i]);
85         }
86         fprintf (ofd, ":\n");
87
88         if (emit_debug_info) {
89                 MonoBasicBlock *bb;
90
91                 fprintf (ofd, ".stabs   \"\",100,0,0,.Ltext0\n");
92                 fprintf (ofd, ".stabs   \"<BB>\",100,0,0,.Ltext0\n");
93                 fprintf (ofd, ".Ltext0:\n");
94
95                 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
96                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
97                         g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
98                 }
99         }
100
101         for (i = 0; i < size; ++i) {
102                 if (emit_debug_info) {
103                         bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
104                         if (bb_num)
105                                 fprintf (ofd, ".stabd 68,0,%d\n", bb_num - 1);
106                 }
107                 fprintf (ofd, ".byte %d\n", (unsigned int) code [i]);
108         }
109         fclose (ofd);
110 #ifdef __APPLE__
111 #define DIS_CMD "otool -v -t"
112 #else
113 #if defined(sparc) && !defined(__GNUC__)
114 #define DIS_CMD "dis"
115 #elif defined(__i386__)
116 #define DIS_CMD "objdump -l -d"
117 #else
118 #define DIS_CMD "objdump -d"
119 #endif
120 #endif
121
122 #if defined(sparc)
123 #define AS_CMD "as -xarch=v9"
124 #elif defined(__i386__)
125 #define AS_CMD "as -gstabs"
126 #else
127 #define AS_CMD "as"
128 #endif
129
130         o_file = g_strdup_printf ("%s/test.o", tmp);    
131         cmd = g_strdup_printf (AS_CMD " %s -o %s", as_file, o_file);
132         system (cmd); 
133         g_free (cmd);
134         if (!objdump_args)
135                 objdump_args = "";
136         
137         cmd = g_strdup_printf (DIS_CMD " %s %s", objdump_args, o_file);
138         system (cmd);
139         g_free (cmd);
140         
141         g_free (o_file);
142         g_free (as_file);
143 }
144