2 * helpers.c: Assorted routines
4 * (C) 2003 Ximian, Inc.
11 #include <mono/metadata/opcodes.h>
19 #ifndef DISABLE_LOGGING
28 #ifdef HAVE_ARRAY_ELEM_INIT
29 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
30 #define MSGSTRFIELD1(line) str##line
31 static const struct msgstr_t {
32 #define MINI_OP(a,b,dest,src1,src2) char MSGSTRFIELD(__LINE__) [sizeof (b)];
33 #define MINI_OP3(a,b,dest,src1,src2,src3) char MSGSTRFIELD(__LINE__) [sizeof (b)];
38 #define MINI_OP(a,b,dest,src1,src2) b,
39 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
44 static const gint16 opidx [] = {
45 #define MINI_OP(a,b,dest,src1,src2) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
46 #define MINI_OP3(a,b,dest,src1,src2,src3) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
54 #define MINI_OP(a,b,dest,src1,src2) b,
55 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
56 /* keep in sync with the enum in mini.h */
57 static const char* const
66 #endif /* DISABLE_LOGGING */
68 #if defined(__i386__) || defined(__x86_64__)
70 #define emit_debug_info TRUE
72 #define emit_debug_info FALSE
75 #define emit_debug_info FALSE
78 /*This enables us to use the right tooling when building the cross compiler for iOS.*/
79 #if defined (__APPLE__) && defined (TARGET_ARM) && (defined(__i386__) || defined(__x86_64__))
81 #define ARCH_PREFIX "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/"
85 #define ARCH_PREFIX ""
86 //#define ARCH_PREFIX "powerpc64-linux-gnu-"
89 mono_inst_name (int op) {
90 #ifndef DISABLE_LOGGING
91 if (op >= OP_LOAD && op <= OP_LAST)
92 #ifdef HAVE_ARRAY_ELEM_INIT
93 return (const char*)&opstr + opidx [op - OP_LOAD];
95 return opnames [op - OP_LOAD];
98 return mono_opcode_name (op);
99 g_error ("unknown opcode name for %d", op);
102 g_assert_not_reached ();
107 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
109 #ifndef DISABLE_LOGGING
113 g_print ("%s:", name);
115 mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
117 g_print (" [BB%d]", cfg->bblocks [i]->block_num);
119 g_print (" BB%d", cfg->bblocks [i]->block_num);
127 * mono_disassemble_code:
128 * @cfg: compilation context
129 * @code: a pointer to the code
130 * @size: the code size in bytes
132 * Disassemble to code to stdout.
135 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
137 #if defined(__native_client__)
140 #ifndef DISABLE_LOGGING
141 GHashTable *offset_to_bb_hash = NULL;
142 int i, cindex, bb_num;
145 const char *tmp = g_get_tmp_dir ();
147 const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
151 int unused G_GNUC_UNUSED;
154 as_file = g_strdup_printf ("%s/test.s", tmp);
156 if (!(ofd = fopen (as_file, "w")))
157 g_assert_not_reached ();
159 i = g_file_open_tmp (NULL, &as_file, NULL);
160 ofd = fdopen (i, "w");
164 for (i = 0; id [i]; ++i) {
165 if (i == 0 && isdigit (id [i]))
167 else if (!isalnum (id [i]))
170 fprintf (ofd, "%c", id [i]);
172 fprintf (ofd, ":\n");
174 if (emit_debug_info && cfg != NULL) {
177 fprintf (ofd, ".stabs \"\",100,0,0,.Ltext0\n");
178 fprintf (ofd, ".stabs \"<BB>\",100,0,0,.Ltext0\n");
179 fprintf (ofd, ".Ltext0:\n");
181 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
182 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
183 g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
188 for (i = 0; i < size; ++i) {
189 if (emit_debug_info && cfg != NULL) {
190 bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
192 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
197 fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
199 fprintf (ofd, ",%d", (unsigned int) code [i]);
210 #define DIS_CMD "otool64 -v -t"
212 #define DIS_CMD "otool -v -t"
215 #if defined(sparc) && !defined(__GNUC__)
216 #define DIS_CMD "dis"
217 #elif defined(TARGET_X86)
218 #define DIS_CMD "objdump -l -d"
219 #elif defined(TARGET_AMD64)
220 #if defined(HOST_WIN32)
221 #define DIS_CMD "x86_64-w64-mingw32-objdump.exe -M x86-64 -d"
223 #define DIS_CMD "objdump -l -d"
226 #define DIS_CMD "objdump -d"
231 #define AS_CMD "as -xarch=v9"
232 #elif defined (TARGET_X86)
233 # if defined(__APPLE__)
234 # define AS_CMD "as -arch i386"
236 # define AS_CMD "as -gstabs"
238 #elif defined (TARGET_AMD64)
239 # if defined (__APPLE__)
240 # define AS_CMD "as -arch x86_64"
242 # define AS_CMD "as -gstabs"
244 #elif defined (TARGET_ARM)
245 # if defined (__APPLE__)
246 # define AS_CMD "as -arch arm"
248 # define AS_CMD "as -gstabs"
250 #elif defined (TARGET_ARM64)
251 # if defined (__APPLE__)
252 # define AS_CMD "clang -c -arch arm64 -g -x assembler"
254 # define AS_CMD "as -gstabs"
256 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
257 #define AS_CMD "as -mips32"
258 #elif defined(__ppc64__)
259 #define AS_CMD "as -arch ppc64"
260 #elif defined(__powerpc64__)
261 #define AS_CMD "as -mppc64"
267 o_file = g_strdup_printf ("%s/test.o", tmp);
269 i = g_file_open_tmp (NULL, &o_file, NULL);
273 cmd = g_strdup_printf (ARCH_PREFIX AS_CMD " %s -o %s", as_file, o_file);
274 unused = system (cmd);
283 * The arm assembler inserts ELF directives instructing objdump to display
284 * everything as data.
286 cmd = g_strdup_printf (ARCH_PREFIX "strip -x %s", o_file);
287 unused = system (cmd);
291 cmd = g_strdup_printf (ARCH_PREFIX DIS_CMD " %s %s", objdump_args, o_file);
292 unused = system (cmd);
304 #else /* DISABLE_JIT */
307 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
311 #endif /* DISABLE_JIT */