Merge pull request #1654 from alexanderkyte/xunit-fixes
[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 #ifndef TARGET_ARM64
70 #define emit_debug_info  TRUE
71 #else
72 #define emit_debug_info  FALSE
73 #endif
74 #else
75 #define emit_debug_info  FALSE
76 #endif
77
78 /*This enables us to use the right tooling when building the cross compiler for iOS.*/
79 #if defined (__APPLE__) && defined (TARGET_ARM) && (defined(__i386__) || defined(__x86_64__))
80
81 #define ARCH_PREFIX "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/"
82
83 #endif
84
85 #define ARCH_PREFIX ""
86 //#define ARCH_PREFIX "powerpc64-linux-gnu-"
87
88 const char*
89 mono_inst_name (int op) {
90 #ifndef DISABLE_LOGGING
91         if (op >= OP_LOAD && op <= OP_LAST)
92 #ifdef HAVE_ARRAY_ELEM_INIT
93                 return (const char*)&opstr + opidx [op - OP_LOAD];
94 #else
95                 return opnames [op - OP_LOAD];
96 #endif
97         if (op < OP_LOAD)
98                 return mono_opcode_name (op);
99         g_error ("unknown opcode name for %d", op);
100         return NULL;
101 #else
102         g_error ("unknown opcode name for %d", op);
103         g_assert_not_reached ();
104 #endif
105 }
106
107 void
108 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom) 
109 {
110 #ifndef DISABLE_LOGGING
111         int i;
112
113         if (name)
114                 g_print ("%s:", name);
115         
116         mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
117                 if (idom == i)
118                         g_print (" [BB%d]", cfg->bblocks [i]->block_num);
119                 else
120                         g_print (" BB%d", cfg->bblocks [i]->block_num);
121                 
122         }
123         g_print ("\n");
124 #endif
125 }
126
127 /**
128  * mono_disassemble_code:
129  * @cfg: compilation context
130  * @code: a pointer to the code
131  * @size: the code size in bytes
132  *
133  * Disassemble to code to stdout.
134  */
135 void
136 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
137 {
138 #if defined(__native_client__)
139         return;
140 #endif
141 #ifndef DISABLE_LOGGING
142         GHashTable *offset_to_bb_hash = NULL;
143         int i, cindex, bb_num;
144         FILE *ofd;
145 #ifdef HOST_WIN32
146         const char *tmp = g_get_tmp_dir ();
147 #endif
148         const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
149         char *as_file;
150         char *o_file;
151         char *cmd;
152         int unused G_GNUC_UNUSED;
153
154 #ifdef HOST_WIN32
155         as_file = g_strdup_printf ("%s/test.s", tmp);    
156
157         if (!(ofd = fopen (as_file, "w")))
158                 g_assert_not_reached ();
159 #else   
160         i = g_file_open_tmp (NULL, &as_file, NULL);
161         ofd = fdopen (i, "w");
162         g_assert (ofd);
163 #endif
164
165         for (i = 0; id [i]; ++i) {
166                 if (i == 0 && isdigit (id [i]))
167                         fprintf (ofd, "_");
168                 else if (!isalnum (id [i]))
169                         fprintf (ofd, "_");
170                 else
171                         fprintf (ofd, "%c", id [i]);
172         }
173         fprintf (ofd, ":\n");
174
175         if (emit_debug_info && cfg != NULL) {
176                 MonoBasicBlock *bb;
177
178                 fprintf (ofd, ".stabs   \"\",100,0,0,.Ltext0\n");
179                 fprintf (ofd, ".stabs   \"<BB>\",100,0,0,.Ltext0\n");
180                 fprintf (ofd, ".Ltext0:\n");
181
182                 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
183                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
184                         g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
185                 }
186         }
187
188         cindex = 0;
189         for (i = 0; i < size; ++i) {
190                 if (emit_debug_info && cfg != NULL) {
191                         bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
192                         if (bb_num) {
193                                 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
194                                 cindex = 0;
195                         }
196                 }
197                 if (cindex == 0) {
198                         fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
199                 } else {
200                         fprintf (ofd, ",%d", (unsigned int) code [i]);
201                 }
202                 cindex++;
203                 if (cindex == 64)
204                         cindex = 0;
205         }
206         fprintf (ofd, "\n");
207         fclose (ofd);
208
209 #ifdef __APPLE__
210 #ifdef __ppc64__
211 #define DIS_CMD "otool64 -v -t"
212 #else
213 #define DIS_CMD "otool -v -t"
214 #endif
215 #else
216 #if defined(sparc) && !defined(__GNUC__)
217 #define DIS_CMD "dis"
218 #elif defined(TARGET_X86)
219 #define DIS_CMD "objdump -l -d"
220 #elif defined(TARGET_AMD64)
221   #if defined(HOST_WIN32)
222   #define DIS_CMD "x86_64-w64-mingw32-objdump.exe -M x86-64 -d"
223   #else
224   #define DIS_CMD "objdump -l -d"
225   #endif
226 #else
227 #define DIS_CMD "objdump -d"
228 #endif
229 #endif
230
231 #if defined(sparc)
232 #define AS_CMD "as -xarch=v9"
233 #elif defined (TARGET_X86)
234 #  if defined(__APPLE__)
235 #    define AS_CMD "as -arch i386"
236 #  else
237 #    define AS_CMD "as -gstabs"
238 #  endif
239 #elif defined (TARGET_AMD64)
240 #  if defined (__APPLE__)
241 #    define AS_CMD "as -arch x86_64"
242 #  else
243 #    define AS_CMD "as -gstabs"
244 #  endif
245 #elif defined (TARGET_ARM)
246 #  if defined (__APPLE__)
247 #    define AS_CMD "as -arch arm"
248 #  else
249 #    define AS_CMD "as -gstabs"
250 #  endif
251 #elif defined (TARGET_ARM64)
252 #  if defined (__APPLE__)
253 #    define AS_CMD "clang -c -arch arm64 -g -x assembler"
254 #  else
255 #    define AS_CMD "as -gstabs"
256 #  endif
257 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
258 #define AS_CMD "as -mips32"
259 #elif defined(__ppc64__)
260 #define AS_CMD "as -arch ppc64"
261 #elif defined(__powerpc64__)
262 #define AS_CMD "as -mppc64"
263 #else
264 #define AS_CMD "as"
265 #endif
266
267 #ifdef HOST_WIN32
268         o_file = g_strdup_printf ("%s/test.o", tmp);
269 #else   
270         i = g_file_open_tmp (NULL, &o_file, NULL);
271         close (i);
272 #endif
273
274         cmd = g_strdup_printf (ARCH_PREFIX AS_CMD " %s -o %s", as_file, o_file);
275         unused = system (cmd); 
276         g_free (cmd);
277         if (!objdump_args)
278                 objdump_args = "";
279
280         fflush (stdout);
281
282 #ifdef __arm__
283         /* 
284          * The arm assembler inserts ELF directives instructing objdump to display 
285          * everything as data.
286          */
287         cmd = g_strdup_printf (ARCH_PREFIX "strip -x %s", o_file);
288         unused = system (cmd);
289         g_free (cmd);
290 #endif
291
292         cmd = g_strdup_printf (ARCH_PREFIX DIS_CMD " %s %s", objdump_args, o_file);
293         unused = system (cmd);
294         g_free (cmd);
295
296 #ifndef HOST_WIN32
297         unlink (o_file);
298         unlink (as_file);
299 #endif
300         g_free (o_file);
301         g_free (as_file);
302 #endif
303 }
304
305 #else /* DISABLE_JIT */
306
307 void
308 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
309 {
310 }
311
312 #endif /* DISABLE_JIT */