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