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