Merge branch 'cecil-light'
[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 #define ARCH_PREFIX ""
70 //#define ARCH_PREFIX "powerpc64-linux-gnu-"
71
72 const char*
73 mono_inst_name (int op) {
74 #ifndef DISABLE_LOGGING
75         if (op >= OP_LOAD && op <= OP_LAST)
76 #ifdef HAVE_ARRAY_ELEM_INIT
77                 return (const char*)&opstr + opidx [op - OP_LOAD];
78 #else
79                 return opnames [op - OP_LOAD];
80 #endif
81         if (op < OP_LOAD)
82                 return mono_opcode_name (op);
83         g_error ("unknown opcode name for %d", op);
84         return NULL;
85 #else
86         g_assert_not_reached ();
87 #endif
88 }
89
90 void
91 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom) 
92 {
93 #ifndef DISABLE_LOGGING
94         int i;
95
96         if (name)
97                 g_print ("%s:", name);
98         
99         mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
100                 if (idom == i)
101                         g_print (" [BB%d]", cfg->bblocks [i]->block_num);
102                 else
103                         g_print (" BB%d", cfg->bblocks [i]->block_num);
104                 
105         }
106         g_print ("\n");
107 #endif
108 }
109
110 /**
111  * mono_disassemble_code:
112  * @cfg: compilation context
113  * @code: a pointer to the code
114  * @size: the code size in bytes
115  *
116  * Disassemble to code to stdout.
117  */
118 void
119 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
120 {
121 #ifndef DISABLE_LOGGING
122         GHashTable *offset_to_bb_hash = NULL;
123         int i, cindex, bb_num;
124         FILE *ofd;
125 #ifdef HOST_WIN32
126         const char *tmp = g_get_tmp_dir ();
127 #endif
128         const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
129         char *as_file;
130         char *o_file;
131         char *cmd;
132         int unused;
133
134 #ifdef HOST_WIN32
135         as_file = g_strdup_printf ("%s/test.s", tmp);    
136
137         if (!(ofd = fopen (as_file, "w")))
138                 g_assert_not_reached ();
139 #else   
140         i = g_file_open_tmp (NULL, &as_file, NULL);
141         ofd = fdopen (i, "w");
142         g_assert (ofd);
143 #endif
144
145         for (i = 0; id [i]; ++i) {
146                 if (i == 0 && isdigit (id [i]))
147                         fprintf (ofd, "_");
148                 else if (!isalnum (id [i]))
149                         fprintf (ofd, "_");
150                 else
151                         fprintf (ofd, "%c", id [i]);
152         }
153         fprintf (ofd, ":\n");
154
155         if (emit_debug_info && cfg != NULL) {
156                 MonoBasicBlock *bb;
157
158                 fprintf (ofd, ".stabs   \"\",100,0,0,.Ltext0\n");
159                 fprintf (ofd, ".stabs   \"<BB>\",100,0,0,.Ltext0\n");
160                 fprintf (ofd, ".Ltext0:\n");
161
162                 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
163                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
164                         g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
165                 }
166         }
167
168         cindex = 0;
169         for (i = 0; i < size; ++i) {
170                 if (emit_debug_info && cfg != NULL) {
171                         bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
172                         if (bb_num) {
173                                 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
174                                 cindex = 0;
175                         }
176                 }
177                 if (cindex == 0) {
178                         fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
179                 } else {
180                         fprintf (ofd, ",%d", (unsigned int) code [i]);
181                 }
182                 cindex++;
183                 if (cindex == 64)
184                         cindex = 0;
185         }
186         fprintf (ofd, "\n");
187         fclose (ofd);
188
189 #ifdef __APPLE__
190 #ifdef __ppc64__
191 #define DIS_CMD "otool64 -v -t"
192 #else
193 #define DIS_CMD "otool -v -t"
194 #endif
195 #else
196 #if defined(sparc) && !defined(__GNUC__)
197 #define DIS_CMD "dis"
198 #elif defined(__i386__) || defined(__x86_64__)
199 #define DIS_CMD "objdump -l -d"
200 #else
201 #define DIS_CMD "objdump -d"
202 #endif
203 #endif
204
205 #if defined(sparc)
206 #define AS_CMD "as -xarch=v9"
207 #elif defined(__i386__) || defined(__x86_64__)
208 #  if defined(__APPLE__)
209 #    define AS_CMD "as"
210 #  else
211 #    define AS_CMD "as -gstabs"
212 #endif
213 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
214 #define AS_CMD "as -mips32"
215 #elif defined(__ppc64__)
216 #define AS_CMD "as -arch ppc64"
217 #elif defined(__powerpc64__)
218 #define AS_CMD "as -mppc64"
219 #else
220 #define AS_CMD "as"
221 #endif
222
223 #ifdef HOST_WIN32
224         o_file = g_strdup_printf ("%s/test.o", tmp);
225 #else   
226         i = g_file_open_tmp (NULL, &o_file, NULL);
227         close (i);
228 #endif
229
230         cmd = g_strdup_printf (ARCH_PREFIX AS_CMD " %s -o %s", as_file, o_file);
231         unused = system (cmd); 
232         g_free (cmd);
233         if (!objdump_args)
234                 objdump_args = "";
235
236         fflush (stdout);
237
238 #ifdef __arm__
239         /* 
240          * The arm assembler inserts ELF directives instructing objdump to display 
241          * everything as data.
242          */
243         cmd = g_strdup_printf (ARCH_PREFIX "strip -x %s", o_file);
244         unused = system (cmd);
245         g_free (cmd);
246 #endif
247         
248         cmd = g_strdup_printf (ARCH_PREFIX DIS_CMD " %s %s", objdump_args, o_file);
249         unused = system (cmd);
250         g_free (cmd);
251         
252 #ifndef HOST_WIN32
253         unlink (o_file);
254         unlink (as_file);
255 #endif
256         g_free (o_file);
257         g_free (as_file);
258 #endif
259 }
260