5 * (C) 2003 Ximian, Inc.
12 #include <mono/metadata/opcodes.h>
20 #ifndef DISABLE_LOGGING
29 #ifdef HAVE_ARRAY_ELEM_INIT
30 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
31 #define MSGSTRFIELD1(line) str##line
32 static const struct msgstr_t {
33 #define MINI_OP(a,b,dest,src1,src2) char MSGSTRFIELD(__LINE__) [sizeof (b)];
34 #define MINI_OP3(a,b,dest,src1,src2,src3) char MSGSTRFIELD(__LINE__) [sizeof (b)];
39 #define MINI_OP(a,b,dest,src1,src2) b,
40 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
45 static const gint16 opidx [] = {
46 #define MINI_OP(a,b,dest,src1,src2) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
47 #define MINI_OP3(a,b,dest,src1,src2,src3) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
55 #define MINI_OP(a,b,dest,src1,src2) b,
56 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
57 /* keep in sync with the enum in mini.h */
58 static const char* const
67 #endif /* DISABLE_LOGGING */
69 #if defined(__i386__) || defined(__x86_64__)
70 #if !defined(TARGET_ARM64) && !defined(__APPLE__)
71 #define emit_debug_info TRUE
73 #define emit_debug_info FALSE
76 #define emit_debug_info FALSE
79 /*This enables us to use the right tooling when building the cross compiler for iOS.*/
80 #if defined (__APPLE__) && defined (TARGET_ARM) && (defined(__i386__) || defined(__x86_64__))
82 #define ARCH_PREFIX "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/"
86 #define ARCH_PREFIX ""
87 //#define ARCH_PREFIX "powerpc64-linux-gnu-"
90 mono_inst_name (int op) {
91 #ifndef DISABLE_LOGGING
92 if (op >= OP_LOAD && op <= OP_LAST)
93 #ifdef HAVE_ARRAY_ELEM_INIT
94 return (const char*)&opstr + opidx [op - OP_LOAD];
96 return opnames [op - OP_LOAD];
99 return mono_opcode_name (op);
100 g_error ("unknown opcode name for %d", op);
103 g_error ("unknown opcode name for %d", op);
104 g_assert_not_reached ();
109 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
111 #ifndef DISABLE_LOGGING
115 g_print ("%s:", name);
117 mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
119 g_print (" [BB%d]", cfg->bblocks [i]->block_num);
121 g_print (" BB%d", cfg->bblocks [i]->block_num);
129 * \param cfg compilation context
130 * \param code a pointer to the code
131 * \param size the code size in bytes
133 * Disassemble to code to stdout.
136 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
138 #ifndef DISABLE_LOGGING
139 GHashTable *offset_to_bb_hash = NULL;
140 int i, cindex, bb_num;
143 const char *tmp = g_get_tmp_dir ();
145 char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
149 int unused G_GNUC_UNUSED;
152 as_file = g_strdup_printf ("%s/test.s", tmp);
154 if (!(ofd = fopen (as_file, "w")))
155 g_assert_not_reached ();
157 i = g_file_open_tmp (NULL, &as_file, NULL);
158 ofd = fdopen (i, "w");
162 for (i = 0; id [i]; ++i) {
163 if (i == 0 && isdigit (id [i]))
165 else if (!isalnum (id [i]))
168 fprintf (ofd, "%c", id [i]);
170 fprintf (ofd, ":\n");
172 if (emit_debug_info && cfg != NULL) {
175 fprintf (ofd, ".stabs \"\",100,0,0,.Ltext0\n");
176 fprintf (ofd, ".stabs \"<BB>\",100,0,0,.Ltext0\n");
177 fprintf (ofd, ".Ltext0:\n");
179 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
180 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
181 g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
186 for (i = 0; i < size; ++i) {
187 if (emit_debug_info && cfg != NULL) {
188 bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
190 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
195 fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
197 fprintf (ofd, ",%d", (unsigned int) code [i]);
208 #define DIS_CMD "otool64 -v -t"
210 #define DIS_CMD "otool -v -t"
213 #if defined(sparc) && !defined(__GNUC__)
214 #define DIS_CMD "dis"
215 #elif defined(TARGET_X86)
216 #define DIS_CMD "objdump -l -d"
217 #elif defined(TARGET_AMD64)
218 #if defined(HOST_WIN32)
219 #define DIS_CMD "x86_64-w64-mingw32-objdump.exe -M x86-64 -d"
221 #define DIS_CMD "objdump -l -d"
224 #define DIS_CMD "objdump -d"
229 #define AS_CMD "as -xarch=v9"
230 #elif defined (TARGET_X86)
231 # if defined(__APPLE__)
232 # define AS_CMD "as -arch i386"
234 # define AS_CMD "as -gstabs"
236 #elif defined (TARGET_AMD64)
237 # if defined (__APPLE__)
238 # define AS_CMD "as -arch x86_64"
240 # define AS_CMD "as -gstabs"
242 #elif defined (TARGET_ARM)
243 # if defined (__APPLE__)
244 # define AS_CMD "as -arch arm"
246 # define AS_CMD "as -gstabs"
248 #elif defined (TARGET_ARM64)
249 # if defined (__APPLE__)
250 # define AS_CMD "clang -c -arch arm64 -g -x assembler"
252 # define AS_CMD "as -gstabs"
254 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
255 #define AS_CMD "as -mips32"
256 #elif defined(__ppc64__)
257 #define AS_CMD "as -arch ppc64"
258 #elif defined(__powerpc64__)
259 #define AS_CMD "as -mppc64"
265 o_file = g_strdup_printf ("%s/test.o", tmp);
267 i = g_file_open_tmp (NULL, &o_file, NULL);
272 cmd = g_strdup_printf (ARCH_PREFIX AS_CMD " %s -o %s", as_file, o_file);
273 unused = system (cmd);
276 objdump_args = g_strdup ("");
282 * The arm assembler inserts ELF directives instructing objdump to display
283 * everything as data.
285 cmd = g_strdup_printf (ARCH_PREFIX "strip -x %s", o_file);
286 unused = system (cmd);
290 cmd = g_strdup_printf (ARCH_PREFIX DIS_CMD " %s %s", objdump_args, o_file);
291 unused = system (cmd);
293 g_free (objdump_args);
295 g_assert_not_reached ();
296 #endif /* HAVE_SYSTEM */
307 #else /* DISABLE_JIT */
310 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
314 #endif /* DISABLE_JIT */