2 * helpers.c: Assorted routines
4 * (C) 2003 Ximian, Inc.
8 #include <mono/metadata/opcodes.h>
10 #ifndef PLATFORM_WIN32
21 #ifdef HAVE_ARRAY_ELEM_INIT
22 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
23 #define MSGSTRFIELD1(line) str##line
24 static const struct msgstr_t {
25 #define MINI_OP(a,b,dest,src1,src2) char MSGSTRFIELD(__LINE__) [sizeof (b)];
26 #define MINI_OP3(a,b,dest,src1,src2,src3) char MSGSTRFIELD(__LINE__) [sizeof (b)];
31 #define MINI_OP(a,b,dest,src1,src2) b,
32 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
37 static const gint16 opidx [] = {
38 #define MINI_OP(a,b,dest,src1,src2) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
39 #define MINI_OP3(a,b,dest,src1,src2,src3) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
47 #define MINI_OP(a,b,dest,src1,src2) b,
48 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
49 /* keep in sync with the enum in mini.h */
50 static const char* const
59 #if defined(__i386__) || defined(__x86_64__)
60 #define emit_debug_info TRUE
62 #define emit_debug_info FALSE
65 #define ARCH_PREFIX ""
66 //#define ARCH_PREFIX "powerpc64-linux-gnu-"
69 mono_inst_name (int op) {
70 if (op >= OP_LOAD && op <= OP_LAST)
71 #ifdef HAVE_ARRAY_ELEM_INIT
72 return (const char*)&opstr + opidx [op - OP_LOAD];
74 return opnames [op - OP_LOAD];
77 return mono_opcode_name (op);
78 g_error ("unknown opcode name for %d", op);
83 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
85 #ifndef DISABLE_LOGGING
89 g_print ("%s:", name);
91 mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
93 g_print (" [BB%d]", cfg->bblocks [i]->block_num);
95 g_print (" BB%d", cfg->bblocks [i]->block_num);
103 * mono_disassemble_code:
104 * @cfg: compilation context
105 * @code: a pointer to the code
106 * @size: the code size in bytes
108 * Disassemble to code to stdout.
111 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
113 #ifndef DISABLE_LOGGING
114 GHashTable *offset_to_bb_hash = NULL;
115 int i, cindex, bb_num;
117 #ifdef PLATFORM_WIN32
118 const char *tmp = g_get_tmp_dir ();
120 const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
125 #ifdef PLATFORM_WIN32
126 as_file = g_strdup_printf ("%s/test.s", tmp);
128 if (!(ofd = fopen (as_file, "w")))
129 g_assert_not_reached ();
131 i = g_file_open_tmp (NULL, &as_file, NULL);
132 ofd = fdopen (i, "w");
136 for (i = 0; id [i]; ++i) {
137 if (i == 0 && isdigit (id [i]))
139 else if (!isalnum (id [i]))
142 fprintf (ofd, "%c", id [i]);
144 fprintf (ofd, ":\n");
146 if (emit_debug_info && cfg != NULL) {
149 fprintf (ofd, ".stabs \"\",100,0,0,.Ltext0\n");
150 fprintf (ofd, ".stabs \"<BB>\",100,0,0,.Ltext0\n");
151 fprintf (ofd, ".Ltext0:\n");
153 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
154 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
155 g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
160 for (i = 0; i < size; ++i) {
161 if (emit_debug_info) {
162 bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
164 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
169 fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
171 fprintf (ofd, ",%d", (unsigned int) code [i]);
182 #define DIS_CMD "otool64 -v -t"
184 #define DIS_CMD "otool -v -t"
187 #if defined(sparc) && !defined(__GNUC__)
188 #define DIS_CMD "dis"
189 #elif defined(__i386__) || defined(__x86_64__)
190 #define DIS_CMD "objdump -l -d"
192 #define DIS_CMD "objdump -d"
197 #define AS_CMD "as -xarch=v9"
198 #elif defined(__i386__) || defined(__x86_64__)
199 # if defined(__APPLE__)
202 # define AS_CMD "as -gstabs"
204 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
205 #define AS_CMD "as -mips32"
206 #elif defined(__ppc64__)
207 #define AS_CMD "as -arch ppc64"
208 #elif defined(__powerpc64__)
209 #define AS_CMD "as -mppc64"
214 #ifdef PLATFORM_WIN32
215 o_file = g_strdup_printf ("%s/test.o", tmp);
217 i = g_file_open_tmp (NULL, &o_file, NULL);
221 cmd = g_strdup_printf (ARCH_PREFIX AS_CMD " %s -o %s", as_file, o_file);
229 * The arm assembler inserts ELF directives instructing objdump to display
230 * everything as data.
232 cmd = g_strdup_printf (ARCH_PREFIX "strip -x %s", o_file);
237 cmd = g_strdup_printf (ARCH_PREFIX DIS_CMD " %s %s", objdump_args, o_file);
241 #ifndef PLATFORM_WIN32