2 * helpers.c: Assorted routines
4 * (C) 2003 Ximian, Inc.
8 #include <mono/metadata/opcodes.h>
14 #ifndef DISABLE_LOGGING
23 #ifdef HAVE_ARRAY_ELEM_INIT
24 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
25 #define MSGSTRFIELD1(line) str##line
26 static const struct msgstr_t {
27 #define MINI_OP(a,b,dest,src1,src2) char MSGSTRFIELD(__LINE__) [sizeof (b)];
28 #define MINI_OP3(a,b,dest,src1,src2,src3) char MSGSTRFIELD(__LINE__) [sizeof (b)];
33 #define MINI_OP(a,b,dest,src1,src2) b,
34 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
39 static const gint16 opidx [] = {
40 #define MINI_OP(a,b,dest,src1,src2) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
41 #define MINI_OP3(a,b,dest,src1,src2,src3) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
49 #define MINI_OP(a,b,dest,src1,src2) b,
50 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
51 /* keep in sync with the enum in mini.h */
52 static const char* const
61 #endif /* DISABLE_LOGGING */
63 #if defined(__i386__) || defined(__x86_64__)
64 #define emit_debug_info TRUE
66 #define emit_debug_info FALSE
69 #define ARCH_PREFIX ""
70 //#define ARCH_PREFIX "powerpc64-linux-gnu-"
73 mono_inst_name (int op) {
74 #ifndef DISABLE_LOGGING
75 if (op >= OP_LOAD && op <= OP_LAST)
76 #ifdef HAVE_ARRAY_ELEM_INIT
77 return (const char*)&opstr + opidx [op - OP_LOAD];
79 return opnames [op - OP_LOAD];
82 return mono_opcode_name (op);
83 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 * mono_disassemble_code:
112 * @cfg: compilation context
113 * @code: a pointer to the code
114 * @size: the code size in bytes
116 * Disassemble to code to stdout.
119 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
121 #ifndef DISABLE_LOGGING
122 GHashTable *offset_to_bb_hash = NULL;
123 int i, cindex, bb_num;
126 const char *tmp = g_get_tmp_dir ();
128 const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
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 %d", (unsigned int) code [i]);
179 fprintf (ofd, ",%d", (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(__i386__) || defined(__x86_64__)
198 #define DIS_CMD "objdump -l -d"
200 #define DIS_CMD "objdump -d"
205 #define AS_CMD "as -xarch=v9"
206 #elif defined(__i386__) || defined(__x86_64__)
207 # if defined(__APPLE__)
210 # define AS_CMD "as -gstabs"
212 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
213 #define AS_CMD "as -mips32"
214 #elif defined(__ppc64__)
215 #define AS_CMD "as -arch ppc64"
216 #elif defined(__powerpc64__)
217 #define AS_CMD "as -mppc64"
223 o_file = g_strdup_printf ("%s/test.o", tmp);
225 i = g_file_open_tmp (NULL, &o_file, NULL);
229 cmd = g_strdup_printf (ARCH_PREFIX AS_CMD " %s -o %s", as_file, o_file);
237 * The arm assembler inserts ELF directives instructing objdump to display
238 * everything as data.
240 cmd = g_strdup_printf (ARCH_PREFIX "strip -x %s", o_file);
245 cmd = g_strdup_printf (ARCH_PREFIX DIS_CMD " %s %s", objdump_args, o_file);