Revert "Merge branch 'master' of https://github.com/mono/mono"
[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 #if defined(__native_client__)
134         return;
135 #endif
136 #ifndef DISABLE_LOGGING
137         GHashTable *offset_to_bb_hash = NULL;
138         int i, cindex, bb_num;
139         FILE *ofd;
140 #ifdef HOST_WIN32
141         const char *tmp = g_get_tmp_dir ();
142 #endif
143         const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
144         char *as_file;
145         char *o_file;
146         char *cmd;
147         int unused;
148
149 #ifdef HOST_WIN32
150         as_file = g_strdup_printf ("%s/test.s", tmp);    
151
152         if (!(ofd = fopen (as_file, "w")))
153                 g_assert_not_reached ();
154 #else   
155         i = g_file_open_tmp (NULL, &as_file, NULL);
156         ofd = fdopen (i, "w");
157         g_assert (ofd);
158 #endif
159
160         for (i = 0; id [i]; ++i) {
161                 if (i == 0 && isdigit (id [i]))
162                         fprintf (ofd, "_");
163                 else if (!isalnum (id [i]))
164                         fprintf (ofd, "_");
165                 else
166                         fprintf (ofd, "%c", id [i]);
167         }
168         fprintf (ofd, ":\n");
169
170         if (emit_debug_info && cfg != NULL) {
171                 MonoBasicBlock *bb;
172
173                 fprintf (ofd, ".stabs   \"\",100,0,0,.Ltext0\n");
174                 fprintf (ofd, ".stabs   \"<BB>\",100,0,0,.Ltext0\n");
175                 fprintf (ofd, ".Ltext0:\n");
176
177                 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
178                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
179                         g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
180                 }
181         }
182
183         cindex = 0;
184         for (i = 0; i < size; ++i) {
185                 if (emit_debug_info && cfg != NULL) {
186                         bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
187                         if (bb_num) {
188                                 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
189                                 cindex = 0;
190                         }
191                 }
192                 if (cindex == 0) {
193                         fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
194                 } else {
195                         fprintf (ofd, ",%d", (unsigned int) code [i]);
196                 }
197                 cindex++;
198                 if (cindex == 64)
199                         cindex = 0;
200         }
201         fprintf (ofd, "\n");
202         fclose (ofd);
203
204 #ifdef __APPLE__
205 #ifdef __ppc64__
206 #define DIS_CMD "otool64 -v -t"
207 #else
208 #define DIS_CMD "otool -v -t"
209 #endif
210 #else
211 #if defined(sparc) && !defined(__GNUC__)
212 #define DIS_CMD "dis"
213 #elif defined(__i386__) || defined(__x86_64__)
214 #define DIS_CMD "objdump -l -d"
215 #else
216 #define DIS_CMD "objdump -d"
217 #endif
218 #endif
219
220 #if defined(sparc)
221 #define AS_CMD "as -xarch=v9"
222 #elif defined (TARGET_X86)
223 #  if defined(__APPLE__)
224 #    define AS_CMD "as -arch i386"
225 #  else
226 #    define AS_CMD "as -gstabs"
227 #  endif
228 #elif defined (TARGET_AMD64)
229 #  if defined (__APPLE__)
230 #    define AS_CMD "as -arch x86_64"
231 #  else
232 #    define AS_CMD "as -gstabs"
233 #  endif
234 #elif defined (TARGET_ARM)
235 #  if defined (__APPLE__)
236 #    define AS_CMD "as -arch arm"
237 #  else
238 #    define AS_CMD "as -gstabs"
239 #  endif
240 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
241 #define AS_CMD "as -mips32"
242 #elif defined(__ppc64__)
243 #define AS_CMD "as -arch ppc64"
244 #elif defined(__powerpc64__)
245 #define AS_CMD "as -mppc64"
246 #else
247 #define AS_CMD "as"
248 #endif
249
250 #ifdef HOST_WIN32
251         o_file = g_strdup_printf ("%s/test.o", tmp);
252 #else   
253         i = g_file_open_tmp (NULL, &o_file, NULL);
254         close (i);
255 #endif
256
257         cmd = g_strdup_printf (ARCH_PREFIX AS_CMD " %s -o %s", as_file, o_file);
258         unused = system (cmd); 
259         g_free (cmd);
260         if (!objdump_args)
261                 objdump_args = "";
262
263         fflush (stdout);
264
265 #ifdef __arm__
266         /* 
267          * The arm assembler inserts ELF directives instructing objdump to display 
268          * everything as data.
269          */
270         cmd = g_strdup_printf (ARCH_PREFIX "strip -x %s", o_file);
271         unused = system (cmd);
272         g_free (cmd);
273 #endif
274         
275         cmd = g_strdup_printf (ARCH_PREFIX DIS_CMD " %s %s", objdump_args, o_file);
276         unused = system (cmd);
277         g_free (cmd);
278         
279 #ifndef HOST_WIN32
280         unlink (o_file);
281         unlink (as_file);
282 #endif
283         g_free (o_file);
284         g_free (as_file);
285 #endif
286 }
287
288 #else /* DISABLE_JIT */
289
290 void
291 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
292 {
293 }
294
295 #endif /* DISABLE_JIT */