Make sure x86 ATOMIC_CAS doesn't overwrite its own operands.
[mono-debugger.git] / mono / mini / helpers.c
blob679eb869a93e0b181057aa840fa85db54de4641d
1 /*
2 * helpers.c: Assorted routines
4 * (C) 2003 Ximian, Inc.
5 */
6 #include "mini.h"
7 #include <ctype.h>
8 #include <mono/metadata/opcodes.h>
10 #ifndef PLATFORM_WIN32
11 #include <unistd.h>
12 #endif
14 #ifdef MINI_OP
15 #undef MINI_OP
16 #endif
18 #ifdef HAVE_ARRAY_ELEM_INIT
19 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
20 #define MSGSTRFIELD1(line) str##line
21 static const struct msgstr_t {
22 #define MINI_OP(a,b,dest,src1,src2) char MSGSTRFIELD(__LINE__) [sizeof (b)];
23 #include "mini-ops.h"
24 #undef MINI_OP
25 } opstr = {
26 #define MINI_OP(a,b,dest,src1,src2) b,
27 #include "mini-ops.h"
28 #undef MINI_OP
30 static const gint16 opidx [] = {
31 #define MINI_OP(a,b,dest,src1,src2) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
32 #include "mini-ops.h"
33 #undef MINI_OP
36 #else
38 #define MINI_OP(a,b,dest,src1,src2) b,
39 /* keep in sync with the enum in mini.h */
40 static const char* const
41 opnames[] = {
42 #include "mini-ops.h"
44 #undef MINI_OP
46 #endif
48 #if defined(__i386__) || defined(__x86_64__)
49 #define emit_debug_info TRUE
50 #else
51 #define emit_debug_info FALSE
52 #endif
54 const char*
55 mono_inst_name (int op) {
56 if (op >= OP_LOAD && op <= OP_LAST)
57 #ifdef HAVE_ARRAY_ELEM_INIT
58 return (const char*)&opstr + opidx [op - OP_LOAD];
59 #else
60 return opnames [op - OP_LOAD];
61 #endif
62 if (op < OP_LOAD)
63 return mono_opcode_name (op);
64 g_error ("unknown opcode name for %d", op);
65 return NULL;
68 void
69 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
71 #ifndef DISABLE_LOGGING
72 int i;
74 if (name)
75 g_print ("%s:", name);
77 mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
78 if (idom == i)
79 g_print (" [BB%d]", cfg->bblocks [i]->block_num);
80 else
81 g_print (" BB%d", cfg->bblocks [i]->block_num);
84 g_print ("\n");
85 #endif
88 /**
89 * mono_disassemble_code:
90 * @cfg: compilation context
91 * @code: a pointer to the code
92 * @size: the code size in bytes
94 * Disassemble to code to stdout.
96 void
97 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
99 #ifndef DISABLE_LOGGING
100 GHashTable *offset_to_bb_hash = NULL;
101 int i, cindex, bb_num;
102 FILE *ofd;
103 #ifdef PLATFORM_WIN32
104 const char *tmp = g_get_tmp_dir ();
105 #endif
106 const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
107 char *as_file;
108 char *o_file;
109 char *cmd;
111 #ifdef PLATFORM_WIN32
112 as_file = g_strdup_printf ("%s/test.s", tmp);
114 if (!(ofd = fopen (as_file, "w")))
115 g_assert_not_reached ();
116 #else
117 i = g_file_open_tmp (NULL, &as_file, NULL);
118 ofd = fdopen (i, "w");
119 g_assert (ofd);
120 #endif
122 for (i = 0; id [i]; ++i) {
123 if (i == 0 && isdigit (id [i]))
124 fprintf (ofd, "_");
125 else if (!isalnum (id [i]))
126 fprintf (ofd, "_");
127 else
128 fprintf (ofd, "%c", id [i]);
130 fprintf (ofd, ":\n");
132 if (emit_debug_info && cfg != NULL) {
133 MonoBasicBlock *bb;
135 fprintf (ofd, ".stabs \"\",100,0,0,.Ltext0\n");
136 fprintf (ofd, ".stabs \"<BB>\",100,0,0,.Ltext0\n");
137 fprintf (ofd, ".Ltext0:\n");
139 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
140 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
141 g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
145 cindex = 0;
146 for (i = 0; i < size; ++i) {
147 if (emit_debug_info) {
148 bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
149 if (bb_num) {
150 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
151 cindex = 0;
154 if (cindex == 0) {
155 fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
156 } else {
157 fprintf (ofd, ",%d", (unsigned int) code [i]);
159 cindex++;
160 if (cindex == 64)
161 cindex = 0;
163 fprintf (ofd, "\n");
164 fclose (ofd);
166 #ifdef __APPLE__
167 #ifdef __ppc64__
168 #define DIS_CMD "otool64 -v -t"
169 #else
170 #define DIS_CMD "otool -v -t"
171 #endif
172 #else
173 #if defined(sparc) && !defined(__GNUC__)
174 #define DIS_CMD "dis"
175 #elif defined(__i386__) || defined(__x86_64__)
176 #define DIS_CMD "objdump -l -d"
177 #else
178 #define DIS_CMD "objdump -d"
179 #endif
180 #endif
182 #if defined(sparc)
183 #define AS_CMD "as -xarch=v9"
184 #elif defined(__i386__) || defined(__x86_64__)
185 # if defined(__APPLE__)
186 # define AS_CMD "as"
187 # else
188 # define AS_CMD "as -gstabs"
189 #endif
190 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
191 #define AS_CMD "as -mips32"
192 #elif defined(__ppc64__)
193 #define AS_CMD "as -arch ppc64"
194 #elif defined(__powerpc64__)
195 #define AS_CMD "as -mppc64"
196 #else
197 #define AS_CMD "as"
198 #endif
200 #ifdef PLATFORM_WIN32
201 o_file = g_strdup_printf ("%s/test.o", tmp);
202 #else
203 i = g_file_open_tmp (NULL, &o_file, NULL);
204 close (i);
205 #endif
207 cmd = g_strdup_printf (AS_CMD " %s -o %s", as_file, o_file);
208 system (cmd);
209 g_free (cmd);
210 if (!objdump_args)
211 objdump_args = "";
213 #ifdef __arm__
215 * The arm assembler inserts ELF directives instructing objdump to display
216 * everything as data.
218 cmd = g_strdup_printf ("strip -x %s", o_file);
219 system (cmd);
220 g_free (cmd);
221 #endif
223 cmd = g_strdup_printf (DIS_CMD " %s %s", objdump_args, o_file);
224 system (cmd);
225 g_free (cmd);
227 #ifndef PLATFORM_WIN32
228 unlink (o_file);
229 unlink (as_file);
230 #endif
231 g_free (o_file);
232 g_free (as_file);
233 #endif