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