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