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__)
69 #define emit_debug_info TRUE
71 #define emit_debug_info FALSE
74 /*This enables us to use the right tooling when building the cross compiler for iOS.*/
75 #if defined (__APPLE__) && defined (TARGET_ARM) && (defined(__i386__) || defined(__x86_64__))
77 #define ARCH_PREFIX "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/"
81 #define ARCH_PREFIX ""
82 //#define ARCH_PREFIX "powerpc64-linux-gnu-"
85 mono_inst_name (int op) {
86 #ifndef DISABLE_LOGGING
87 if (op >= OP_LOAD && op <= OP_LAST)
88 #ifdef HAVE_ARRAY_ELEM_INIT
89 return (const char*)&opstr + opidx [op - OP_LOAD];
91 return opnames [op - OP_LOAD];
94 return mono_opcode_name (op);
95 g_error ("unknown opcode name for %d", op);
98 g_assert_not_reached ();
103 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
105 #ifndef DISABLE_LOGGING
109 g_print ("%s:", name);
111 mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
113 g_print (" [BB%d]", cfg->bblocks [i]->block_num);
115 g_print (" BB%d", cfg->bblocks [i]->block_num);
123 * mono_disassemble_code:
124 * @cfg: compilation context
125 * @code: a pointer to the code
126 * @size: the code size in bytes
128 * Disassemble to code to stdout.
131 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
133 #if defined(__native_client__)
136 #ifndef DISABLE_LOGGING
137 GHashTable *offset_to_bb_hash = NULL;
138 int i, cindex, bb_num;
141 const char *tmp = g_get_tmp_dir ();
143 const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
150 as_file = g_strdup_printf ("%s/test.s", tmp);
152 if (!(ofd = fopen (as_file, "w")))
153 g_assert_not_reached ();
155 i = g_file_open_tmp (NULL, &as_file, NULL);
156 ofd = fdopen (i, "w");
160 for (i = 0; id [i]; ++i) {
161 if (i == 0 && isdigit (id [i]))
163 else if (!isalnum (id [i]))
166 fprintf (ofd, "%c", id [i]);
168 fprintf (ofd, ":\n");
170 if (emit_debug_info && cfg != NULL) {
173 fprintf (ofd, ".stabs \"\",100,0,0,.Ltext0\n");
174 fprintf (ofd, ".stabs \"<BB>\",100,0,0,.Ltext0\n");
175 fprintf (ofd, ".Ltext0:\n");
177 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
178 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
179 g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
184 for (i = 0; i < size; ++i) {
185 if (emit_debug_info && cfg != NULL) {
186 bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
188 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
193 fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
195 fprintf (ofd, ",%d", (unsigned int) code [i]);
206 #define DIS_CMD "otool64 -v -t"
208 #define DIS_CMD "otool -v -t"
211 #if defined(sparc) && !defined(__GNUC__)
212 #define DIS_CMD "dis"
213 #elif defined(__i386__) || defined(__x86_64__)
214 #define DIS_CMD "objdump -l -d"
216 #define DIS_CMD "objdump -d"
221 #define AS_CMD "as -xarch=v9"
222 #elif defined (TARGET_X86)
223 # if defined(__APPLE__)
224 # define AS_CMD "as -arch i386"
226 # define AS_CMD "as -gstabs"
228 #elif defined (TARGET_AMD64)
229 # if defined (__APPLE__)
230 # define AS_CMD "as -arch x86_64"
232 # define AS_CMD "as -gstabs"
234 #elif defined (TARGET_ARM)
235 # if defined (__APPLE__)
236 # define AS_CMD "as -arch arm"
238 # define AS_CMD "as -gstabs"
240 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
241 #define AS_CMD "as -mips32"
242 #elif defined(__ppc64__)
243 #define AS_CMD "as -arch ppc64"
244 #elif defined(__powerpc64__)
245 #define AS_CMD "as -mppc64"
251 o_file = g_strdup_printf ("%s/test.o", tmp);
253 i = g_file_open_tmp (NULL, &o_file, NULL);
257 cmd = g_strdup_printf (ARCH_PREFIX AS_CMD " %s -o %s", as_file, o_file);
258 unused = system (cmd);
267 * The arm assembler inserts ELF directives instructing objdump to display
268 * everything as data.
270 cmd = g_strdup_printf (ARCH_PREFIX "strip -x %s", o_file);
271 unused = system (cmd);
275 cmd = g_strdup_printf (ARCH_PREFIX DIS_CMD " %s %s", objdump_args, o_file);
276 unused = system (cmd);
288 #else /* DISABLE_JIT */
291 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
295 #endif /* DISABLE_JIT */