0e884e0c8e79052ee3fab2c076757fc5a77aa603
[mono.git] / mono / mini / helpers.c
1 /*
2  * helpers.c: Assorted routines
3  *
4  * (C) 2003 Ximian, Inc.
5  */
6
7 #include <config.h>
8
9 #include "mini.h"
10 #include <ctype.h>
11 #include <mono/metadata/opcodes.h>
12
13 #ifndef HOST_WIN32
14 #include <unistd.h>
15 #endif
16
17 #ifndef DISABLE_JIT
18
19 #ifndef DISABLE_LOGGING
20
21 #ifdef MINI_OP
22 #undef MINI_OP
23 #endif
24 #ifdef MINI_OP3
25 #undef MINI_OP3
26 #endif
27
28 #ifdef HAVE_ARRAY_ELEM_INIT
29 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
30 #define MSGSTRFIELD1(line) str##line
31 static const struct msgstr_t {
32 #define MINI_OP(a,b,dest,src1,src2) char MSGSTRFIELD(__LINE__) [sizeof (b)];
33 #define MINI_OP3(a,b,dest,src1,src2,src3) char MSGSTRFIELD(__LINE__) [sizeof (b)];
34 #include "mini-ops.h"
35 #undef MINI_OP
36 #undef MINI_OP3
37 } opstr = {
38 #define MINI_OP(a,b,dest,src1,src2) b,
39 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
40 #include "mini-ops.h"
41 #undef MINI_OP
42 #undef MINI_OP3
43 };
44 static const gint16 opidx [] = {
45 #define MINI_OP(a,b,dest,src1,src2) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
46 #define MINI_OP3(a,b,dest,src1,src2,src3) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
47 #include "mini-ops.h"
48 #undef MINI_OP
49 #undef MINI_OP3
50 };
51
52 #else
53
54 #define MINI_OP(a,b,dest,src1,src2) b,
55 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
56 /* keep in sync with the enum in mini.h */
57 static const char* const
58 opnames[] = {
59 #include "mini-ops.h"
60 };
61 #undef MINI_OP
62 #undef MINI_OP3
63
64 #endif
65
66 #endif /* DISABLE_LOGGING */
67
68 #if defined(__i386__) || defined(__x86_64__)
69 #define emit_debug_info  TRUE
70 #else
71 #define emit_debug_info  FALSE
72 #endif
73
74 /*This enables us to use the right tooling when building the cross compiler for iOS.*/
75 #if defined (__APPLE__) && defined (TARGET_ARM) && (defined(__i386__) || defined(__x86_64__))
76
77 #define ARCH_PREFIX "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/"
78
79 #endif
80
81 #define ARCH_PREFIX ""
82 //#define ARCH_PREFIX "powerpc64-linux-gnu-"
83
84 const char*
85 mono_inst_name (int op) {
86 #ifndef DISABLE_LOGGING
87         if (op >= OP_LOAD && op <= OP_LAST)
88 #ifdef HAVE_ARRAY_ELEM_INIT
89                 return (const char*)&opstr + opidx [op - OP_LOAD];
90 #else
91                 return opnames [op - OP_LOAD];
92 #endif
93         if (op < OP_LOAD)
94                 return mono_opcode_name (op);
95         g_error ("unknown opcode name for %d", op);
96         return NULL;
97 #else
98         g_assert_not_reached ();
99 #endif
100 }
101
102 void
103 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom) 
104 {
105 #ifndef DISABLE_LOGGING
106         int i;
107
108         if (name)
109                 g_print ("%s:", name);
110         
111         mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
112                 if (idom == i)
113                         g_print (" [BB%d]", cfg->bblocks [i]->block_num);
114                 else
115                         g_print (" BB%d", cfg->bblocks [i]->block_num);
116                 
117         }
118         g_print ("\n");
119 #endif
120 }
121
122 /**
123  * mono_disassemble_code:
124  * @cfg: compilation context
125  * @code: a pointer to the code
126  * @size: the code size in bytes
127  *
128  * Disassemble to code to stdout.
129  */
130 void
131 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
132 {
133 #ifndef DISABLE_LOGGING
134         GHashTable *offset_to_bb_hash = NULL;
135         int i, cindex, bb_num;
136         FILE *ofd;
137 #ifdef HOST_WIN32
138         const char *tmp = g_get_tmp_dir ();
139 #endif
140         const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
141         char *as_file;
142         char *o_file;
143         char *cmd;
144         int unused;
145
146 #ifdef HOST_WIN32
147         as_file = g_strdup_printf ("%s/test.s", tmp);    
148
149         if (!(ofd = fopen (as_file, "w")))
150                 g_assert_not_reached ();
151 #else   
152         i = g_file_open_tmp (NULL, &as_file, NULL);
153         ofd = fdopen (i, "w");
154         g_assert (ofd);
155 #endif
156
157         for (i = 0; id [i]; ++i) {
158                 if (i == 0 && isdigit (id [i]))
159                         fprintf (ofd, "_");
160                 else if (!isalnum (id [i]))
161                         fprintf (ofd, "_");
162                 else
163                         fprintf (ofd, "%c", id [i]);
164         }
165         fprintf (ofd, ":\n");
166
167         if (emit_debug_info && cfg != NULL) {
168                 MonoBasicBlock *bb;
169
170                 fprintf (ofd, ".stabs   \"\",100,0,0,.Ltext0\n");
171                 fprintf (ofd, ".stabs   \"<BB>\",100,0,0,.Ltext0\n");
172                 fprintf (ofd, ".Ltext0:\n");
173
174                 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
175                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
176                         g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
177                 }
178         }
179
180         cindex = 0;
181         for (i = 0; i < size; ++i) {
182                 if (emit_debug_info && cfg != NULL) {
183                         bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
184                         if (bb_num) {
185                                 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
186                                 cindex = 0;
187                         }
188                 }
189                 if (cindex == 0) {
190                         fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
191                 } else {
192                         fprintf (ofd, ",%d", (unsigned int) code [i]);
193                 }
194                 cindex++;
195                 if (cindex == 64)
196                         cindex = 0;
197         }
198         fprintf (ofd, "\n");
199         fclose (ofd);
200
201 #ifdef __APPLE__
202 #ifdef __ppc64__
203 #define DIS_CMD "otool64 -v -t"
204 #else
205 #define DIS_CMD "otool -v -t"
206 #endif
207 #else
208 #if defined(sparc) && !defined(__GNUC__)
209 #define DIS_CMD "dis"
210 #elif defined(__i386__) || defined(__x86_64__)
211 #define DIS_CMD "objdump -l -d"
212 #else
213 #define DIS_CMD "objdump -d"
214 #endif
215 #endif
216
217 #if defined(sparc)
218 #define AS_CMD "as -xarch=v9"
219 #elif defined (TARGET_X86)
220 #  if defined(__APPLE__)
221 #    define AS_CMD "as -arch i386"
222 #  else
223 #    define AS_CMD "as -gstabs"
224 #  endif
225 #elif defined (TARGET_AMD64)
226 #  if defined (__APPLE__)
227 #    define AS_CMD "as -arch x86_64"
228 #  else
229 #    define AS_CMD "as -gstabs"
230 #  endif
231 #elif defined (TARGET_ARM)
232 #  if defined (__APPLE__)
233 #    define AS_CMD "as -arch arm"
234 #  else
235 #    define AS_CMD "as -gstabs"
236 #  endif
237 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
238 #define AS_CMD "as -mips32"
239 #elif defined(__ppc64__)
240 #define AS_CMD "as -arch ppc64"
241 #elif defined(__powerpc64__)
242 #define AS_CMD "as -mppc64"
243 #else
244 #define AS_CMD "as"
245 #endif
246
247 #ifdef HOST_WIN32
248         o_file = g_strdup_printf ("%s/test.o", tmp);
249 #else   
250         i = g_file_open_tmp (NULL, &o_file, NULL);
251         close (i);
252 #endif
253
254         cmd = g_strdup_printf (ARCH_PREFIX AS_CMD " %s -o %s", as_file, o_file);
255         unused = system (cmd); 
256         g_free (cmd);
257         if (!objdump_args)
258                 objdump_args = "";
259
260         fflush (stdout);
261
262 #ifdef __arm__
263         /* 
264          * The arm assembler inserts ELF directives instructing objdump to display 
265          * everything as data.
266          */
267         cmd = g_strdup_printf (ARCH_PREFIX "strip -x %s", o_file);
268         unused = system (cmd);
269         g_free (cmd);
270 #endif
271         
272         cmd = g_strdup_printf (ARCH_PREFIX DIS_CMD " %s %s", objdump_args, o_file);
273         unused = system (cmd);
274         g_free (cmd);
275         
276 #ifndef HOST_WIN32
277         unlink (o_file);
278         unlink (as_file);
279 #endif
280         g_free (o_file);
281         g_free (as_file);
282 #endif
283 }
284
285 #else /* DISABLE_JIT */
286
287 void
288 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
289 {
290 }
291
292 #endif /* DISABLE_JIT */