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