5 * (C) 2003 Ximian, Inc.
12 #include <mono/metadata/opcodes.h>
20 #ifndef DISABLE_LOGGING
29 // This, instead of an array of pointers, to optimize away a pointer and a relocation per string.
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) offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
47 #define MINI_OP3(a,b,dest,src1,src2,src3) offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
53 #endif /* DISABLE_LOGGING */
55 #if defined(__i386__) || defined(__x86_64__)
56 #if !defined(TARGET_ARM64) && !defined(__APPLE__)
57 #define emit_debug_info TRUE
59 #define emit_debug_info FALSE
62 #define emit_debug_info FALSE
65 /*This enables us to use the right tooling when building the cross compiler for iOS.*/
66 #if defined (__APPLE__) && defined (TARGET_ARM) && (defined(__i386__) || defined(__x86_64__))
68 //#define ARCH_PREFIX "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/"
72 #define ARCH_PREFIX ""
73 //#define ARCH_PREFIX "powerpc64-linux-gnu-"
76 mono_inst_name (int op
) {
77 #ifndef DISABLE_LOGGING
78 if (op
>= OP_LOAD
&& op
<= OP_LAST
)
79 return (const char*)&opstr
+ opidx
[op
- OP_LOAD
];
81 return mono_opcode_name (op
);
82 g_error ("unknown opcode name for %d", op
);
85 g_error ("unknown opcode name for %d", op
);
86 g_assert_not_reached ();
91 mono_blockset_print (MonoCompile
*cfg
, MonoBitSet
*set
, const char *name
, guint idom
)
93 #ifndef DISABLE_LOGGING
97 g_print ("%s:", name
);
99 mono_bitset_foreach_bit (set
, i
, cfg
->num_bblocks
) {
101 g_print (" [BB%d]", cfg
->bblocks
[i
]->block_num
);
103 g_print (" BB%d", cfg
->bblocks
[i
]->block_num
);
111 * \param cfg compilation context
112 * \param code a pointer to the code
113 * \param size the code size in bytes
115 * Disassemble to code to stdout.
118 mono_disassemble_code (MonoCompile
*cfg
, guint8
*code
, int size
, char *id
)
120 #ifndef DISABLE_LOGGING
121 GHashTable
*offset_to_bb_hash
= NULL
;
122 int i
, cindex
, bb_num
;
125 const char *tmp
= g_get_tmp_dir ();
127 char *objdump_args
= g_getenv ("MONO_OBJDUMP_ARGS");
131 int unused G_GNUC_UNUSED
;
134 as_file
= g_strdup_printf ("%s/test.s", tmp
);
136 if (!(ofd
= fopen (as_file
, "w")))
137 g_assert_not_reached ();
139 i
= g_file_open_tmp (NULL
, &as_file
, NULL
);
140 ofd
= fdopen (i
, "w");
144 for (i
= 0; id
[i
]; ++i
) {
145 if (i
== 0 && isdigit (id
[i
]))
147 else if (!isalnum (id
[i
]))
150 fprintf (ofd
, "%c", id
[i
]);
152 fprintf (ofd
, ":\n");
154 if (emit_debug_info
&& cfg
!= NULL
) {
157 fprintf (ofd
, ".stabs \"\",100,0,0,.Ltext0\n");
158 fprintf (ofd
, ".stabs \"<BB>\",100,0,0,.Ltext0\n");
159 fprintf (ofd
, ".Ltext0:\n");
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));
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
)));
172 fprintf (ofd
, "\n.stabd 68,0,%d\n", bb_num
- 1);
177 fprintf (ofd
, "\n.byte %u", (unsigned int) code
[i
]);
179 fprintf (ofd
, ",%u", (unsigned int) code
[i
]);
190 #define DIS_CMD "otool64 -v -t"
192 #define DIS_CMD "otool -v -t"
195 #if defined(sparc) && !defined(__GNUC__)
196 #define DIS_CMD "dis"
197 #elif defined(TARGET_X86)
198 #define DIS_CMD "objdump -l -d"
199 #elif defined(TARGET_AMD64)
200 #if defined(HOST_WIN32)
201 #define DIS_CMD "x86_64-w64-mingw32-objdump.exe -M x86-64 -d"
203 #define DIS_CMD "objdump -l -d"
206 #define DIS_CMD "objdump -d"
211 #define AS_CMD "as -xarch=v9"
212 #elif defined (TARGET_X86)
213 # if defined(__APPLE__)
214 # define AS_CMD "as -arch i386"
216 # define AS_CMD "as -gstabs"
218 #elif defined (TARGET_AMD64)
219 # if defined (__APPLE__)
220 # define AS_CMD "as -arch x86_64"
222 # define AS_CMD "as -gstabs"
224 #elif defined (TARGET_ARM)
225 # if defined (__APPLE__)
226 # define AS_CMD "as -arch arm"
228 # define AS_CMD "as -gstabs"
230 #elif defined (TARGET_ARM64)
231 # if defined (__APPLE__)
232 # define AS_CMD "clang -c -arch arm64 -g -x assembler"
234 # define AS_CMD "as -gstabs"
236 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
237 #define AS_CMD "as -mips32"
238 #elif defined(__ppc64__)
239 #define AS_CMD "as -arch ppc64"
240 #elif defined(__powerpc64__)
241 #define AS_CMD "as -mppc64"
242 #elif defined (TARGET_RISCV64)
243 #define AS_CMD "as -march=rv64ima"
244 #elif defined (TARGET_RISCV32)
245 #define AS_CMD "as -march=rv32ima"
251 o_file
= g_strdup_printf ("%s/test.o", tmp
);
253 i
= g_file_open_tmp (NULL
, &o_file
, NULL
);
258 cmd
= g_strdup_printf (ARCH_PREFIX AS_CMD
" %s -o %s", as_file
, o_file
);
259 unused
= system (cmd
);
262 objdump_args
= g_strdup ("");
266 #if defined(__arm__) || defined(__aarch64__)
268 * The arm assembler inserts ELF directives instructing objdump to display
269 * everything as data.
271 cmd
= g_strdup_printf (ARCH_PREFIX
"strip -s %s", o_file
);
272 unused
= system (cmd
);
276 cmd
= g_strdup_printf (ARCH_PREFIX DIS_CMD
" %s %s", objdump_args
, o_file
);
277 unused
= system (cmd
);
279 g_free (objdump_args
);
281 g_assert_not_reached ();
282 #endif /* HAVE_SYSTEM */
293 #else /* DISABLE_JIT */
296 mono_blockset_print (MonoCompile
*cfg
, MonoBitSet
*set
, const char *name
, guint idom
)
300 #endif /* DISABLE_JIT */