2009-01-07 Geoff Norton <gnorton@novell.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 #ifndef PLATFORM_WIN32
11 #include <unistd.h>
12 #endif
13
14 #ifdef MINI_OP
15 #undef MINI_OP
16 #endif
17
18 #ifdef HAVE_ARRAY_ELEM_INIT
19 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
20 #define MSGSTRFIELD1(line) str##line
21 static const struct msgstr_t {
22 #define MINI_OP(a,b,dest,src1,src2) char MSGSTRFIELD(__LINE__) [sizeof (b)];
23 #include "mini-ops.h"
24 #undef MINI_OP
25 } opstr = {
26 #define MINI_OP(a,b,dest,src1,src2) b,
27 #include "mini-ops.h"
28 #undef MINI_OP
29 };
30 static const gint16 opidx [] = {
31 #define MINI_OP(a,b,dest,src1,src2) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
32 #include "mini-ops.h"
33 #undef MINI_OP
34 };
35
36 #else
37
38 #define MINI_OP(a,b,dest,src1,src2) b,
39 /* keep in sync with the enum in mini.h */
40 static const char* const
41 opnames[] = {
42 #include "mini-ops.h"
43 };
44 #undef MINI_OP
45
46 #endif
47
48 #if defined(__i386__) || defined(__x86_64__)
49 #define emit_debug_info  TRUE
50 #else
51 #define emit_debug_info  FALSE
52 #endif
53
54 const char*
55 mono_inst_name (int op) {
56         if (op >= OP_LOAD && op <= OP_LAST)
57 #ifdef HAVE_ARRAY_ELEM_INIT
58                 return (const char*)&opstr + opidx [op - OP_LOAD];
59 #else
60                 return opnames [op - OP_LOAD];
61 #endif
62         if (op < OP_LOAD)
63                 return mono_opcode_name (op);
64         g_error ("unknown opcode name for %d", op);
65         return NULL;
66 }
67
68 void
69 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom) 
70 {
71 #ifndef DISABLE_LOGGING
72         int i;
73
74         if (name)
75                 g_print ("%s:", name);
76         
77         mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
78                 if (idom == i)
79                         g_print (" [BB%d]", cfg->bblocks [i]->block_num);
80                 else
81                         g_print (" BB%d", cfg->bblocks [i]->block_num);
82                 
83         }
84         g_print ("\n");
85 #endif
86 }
87
88 /**
89  * mono_disassemble_code:
90  * @cfg: compilation context
91  * @code: a pointer to the code
92  * @size: the code size in bytes
93  *
94  * Disassemble to code to stdout.
95  */
96 void
97 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
98 {
99 #ifndef DISABLE_LOGGING
100         GHashTable *offset_to_bb_hash = NULL;
101         int i, cindex, bb_num;
102         FILE *ofd;
103 #ifdef PLATFORM_WIN32
104         const char *tmp = g_get_tmp_dir ();
105 #endif
106         const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
107         char *as_file;
108         char *o_file;
109         char *cmd;
110
111 #ifdef PLATFORM_WIN32
112         as_file = g_strdup_printf ("%s/test.s", tmp);    
113
114         if (!(ofd = fopen (as_file, "w")))
115                 g_assert_not_reached ();
116 #else   
117         i = g_file_open_tmp (NULL, &as_file, NULL);
118         ofd = fdopen (i, "w");
119         g_assert (ofd);
120 #endif
121
122         for (i = 0; id [i]; ++i) {
123                 if (!isalnum (id [i]))
124                         fprintf (ofd, "_");
125                 else
126                         fprintf (ofd, "%c", id [i]);
127         }
128         fprintf (ofd, ":\n");
129
130         if (emit_debug_info && cfg != NULL) {
131                 MonoBasicBlock *bb;
132
133                 fprintf (ofd, ".stabs   \"\",100,0,0,.Ltext0\n");
134                 fprintf (ofd, ".stabs   \"<BB>\",100,0,0,.Ltext0\n");
135                 fprintf (ofd, ".Ltext0:\n");
136
137                 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
138                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
139                         g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
140                 }
141         }
142
143         cindex = 0;
144         for (i = 0; i < size; ++i) {
145                 if (emit_debug_info) {
146                         bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
147                         if (bb_num) {
148                                 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
149                                 cindex = 0;
150                         }
151                 }
152                 if (cindex == 0) {
153                         fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
154                 } else {
155                         fprintf (ofd, ",%d", (unsigned int) code [i]);
156                 }
157                 cindex++;
158                 if (cindex == 64)
159                         cindex = 0;
160         }
161         fprintf (ofd, "\n");
162         fclose (ofd);
163
164 #ifdef __APPLE__
165 #ifdef __ppc64__
166 #define DIS_CMD "otool64 -v -t"
167 #else
168 #define DIS_CMD "otool -v -t"
169 #endif
170 #else
171 #if defined(sparc) && !defined(__GNUC__)
172 #define DIS_CMD "dis"
173 #elif defined(__i386__) || defined(__x86_64__)
174 #define DIS_CMD "objdump -l -d"
175 #else
176 #define DIS_CMD "objdump -d"
177 #endif
178 #endif
179
180 #if defined(sparc)
181 #define AS_CMD "as -xarch=v9"
182 #elif defined(__i386__) || defined(__x86_64__)
183 #  if defined(__APPLE__)
184 #    define AS_CMD "as"
185 #  else
186 #    define AS_CMD "as -gstabs"
187 #endif
188 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
189 #define AS_CMD "as -mips32"
190 #elif defined(__ppc64__)
191 #define AS_CMD "as -arch ppc64"
192 #elif defined(__powerpc64__)
193 #define AS_CMD "as -mppc64"
194 #else
195 #define AS_CMD "as"
196 #endif
197
198 #ifdef PLATFORM_WIN32
199         o_file = g_strdup_printf ("%s/test.o", tmp);
200 #else   
201         i = g_file_open_tmp (NULL, &o_file, NULL);
202         close (i);
203 #endif
204
205         cmd = g_strdup_printf (AS_CMD " %s -o %s", as_file, o_file);
206         system (cmd); 
207         g_free (cmd);
208         if (!objdump_args)
209                 objdump_args = "";
210
211 #ifdef __arm__
212         /* 
213          * The arm assembler inserts ELF directives instructing objdump to display 
214          * everything as data.
215          */
216         cmd = g_strdup_printf ("strip -x %s", o_file);
217         system (cmd);
218         g_free (cmd);
219 #endif
220         
221         cmd = g_strdup_printf (DIS_CMD " %s %s", objdump_args, o_file);
222         system (cmd);
223         g_free (cmd);
224         
225 #ifndef PLATFORM_WIN32
226         unlink (o_file);
227         unlink (as_file);
228 #endif
229         g_free (o_file);
230         g_free (as_file);
231 #endif
232 }
233