New tests.
[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
133 #ifdef HOST_WIN32
134         as_file = g_strdup_printf ("%s/test.s", tmp);    
135
136         if (!(ofd = fopen (as_file, "w")))
137                 g_assert_not_reached ();
138 #else   
139         i = g_file_open_tmp (NULL, &as_file, NULL);
140         ofd = fdopen (i, "w");
141         g_assert (ofd);
142 #endif
143
144         for (i = 0; id [i]; ++i) {
145                 if (i == 0 && isdigit (id [i]))
146                         fprintf (ofd, "_");
147                 else if (!isalnum (id [i]))
148                         fprintf (ofd, "_");
149                 else
150                         fprintf (ofd, "%c", id [i]);
151         }
152         fprintf (ofd, ":\n");
153
154         if (emit_debug_info && cfg != NULL) {
155                 MonoBasicBlock *bb;
156
157                 fprintf (ofd, ".stabs   \"\",100,0,0,.Ltext0\n");
158                 fprintf (ofd, ".stabs   \"<BB>\",100,0,0,.Ltext0\n");
159                 fprintf (ofd, ".Ltext0:\n");
160
161                 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
162                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
163                         g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
164                 }
165         }
166
167         cindex = 0;
168         for (i = 0; i < size; ++i) {
169                 if (emit_debug_info && cfg != NULL) {
170                         bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
171                         if (bb_num) {
172                                 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
173                                 cindex = 0;
174                         }
175                 }
176                 if (cindex == 0) {
177                         fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
178                 } else {
179                         fprintf (ofd, ",%d", (unsigned int) code [i]);
180                 }
181                 cindex++;
182                 if (cindex == 64)
183                         cindex = 0;
184         }
185         fprintf (ofd, "\n");
186         fclose (ofd);
187
188 #ifdef __APPLE__
189 #ifdef __ppc64__
190 #define DIS_CMD "otool64 -v -t"
191 #else
192 #define DIS_CMD "otool -v -t"
193 #endif
194 #else
195 #if defined(sparc) && !defined(__GNUC__)
196 #define DIS_CMD "dis"
197 #elif defined(__i386__) || defined(__x86_64__)
198 #define DIS_CMD "objdump -l -d"
199 #else
200 #define DIS_CMD "objdump -d"
201 #endif
202 #endif
203
204 #if defined(sparc)
205 #define AS_CMD "as -xarch=v9"
206 #elif defined(__i386__) || defined(__x86_64__)
207 #  if defined(__APPLE__)
208 #    define AS_CMD "as"
209 #  else
210 #    define AS_CMD "as -gstabs"
211 #endif
212 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
213 #define AS_CMD "as -mips32"
214 #elif defined(__ppc64__)
215 #define AS_CMD "as -arch ppc64"
216 #elif defined(__powerpc64__)
217 #define AS_CMD "as -mppc64"
218 #else
219 #define AS_CMD "as"
220 #endif
221
222 #ifdef HOST_WIN32
223         o_file = g_strdup_printf ("%s/test.o", tmp);
224 #else   
225         i = g_file_open_tmp (NULL, &o_file, NULL);
226         close (i);
227 #endif
228
229         cmd = g_strdup_printf (ARCH_PREFIX AS_CMD " %s -o %s", as_file, o_file);
230         system (cmd); 
231         g_free (cmd);
232         if (!objdump_args)
233                 objdump_args = "";
234
235 #ifdef __arm__
236         /* 
237          * The arm assembler inserts ELF directives instructing objdump to display 
238          * everything as data.
239          */
240         cmd = g_strdup_printf (ARCH_PREFIX "strip -x %s", o_file);
241         system (cmd);
242         g_free (cmd);
243 #endif
244         
245         cmd = g_strdup_printf (ARCH_PREFIX DIS_CMD " %s %s", objdump_args, o_file);
246         system (cmd);
247         g_free (cmd);
248         
249 #ifndef HOST_WIN32
250         unlink (o_file);
251         unlink (as_file);
252 #endif
253         g_free (o_file);
254         g_free (as_file);
255 #endif
256 }
257