2 * helpers.c: Assorted routines
4 * (C) 2003 Ximian, Inc.
8 #include <mono/metadata/opcodes.h>
10 #ifndef PLATFORM_WIN32
18 #ifdef HAVE_ARRAY_ELEM_INIT
19 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
20 #define MSGSTRFIELD1(line) str##line
21 static const struct msgstr_t
{
22 #define MINI_OP(a,b,dest,src1,src2) char MSGSTRFIELD(__LINE__) [sizeof (b)];
26 #define MINI_OP(a,b,dest,src1,src2) b,
30 static const gint16 opidx
[] = {
31 #define MINI_OP(a,b,dest,src1,src2) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
38 #define MINI_OP(a,b,dest,src1,src2) b,
39 /* keep in sync with the enum in mini.h */
40 static const char* const
48 #if defined(__i386__) || defined(__x86_64__)
49 #define emit_debug_info TRUE
51 #define emit_debug_info FALSE
55 mono_inst_name (int op
) {
56 if (op
>= OP_LOAD
&& op
<= OP_LAST
)
57 #ifdef HAVE_ARRAY_ELEM_INIT
58 return (const char*)&opstr
+ opidx
[op
- OP_LOAD
];
60 return opnames
[op
- OP_LOAD
];
63 return mono_opcode_name (op
);
64 g_error ("unknown opcode name for %d", op
);
69 mono_blockset_print (MonoCompile
*cfg
, MonoBitSet
*set
, const char *name
, guint idom
)
71 #ifndef DISABLE_LOGGING
75 g_print ("%s:", name
);
77 mono_bitset_foreach_bit (set
, i
, cfg
->num_bblocks
) {
79 g_print (" [BB%d]", cfg
->bblocks
[i
]->block_num
);
81 g_print (" BB%d", cfg
->bblocks
[i
]->block_num
);
89 * mono_disassemble_code:
90 * @cfg: compilation context
91 * @code: a pointer to the code
92 * @size: the code size in bytes
94 * Disassemble to code to stdout.
97 mono_disassemble_code (MonoCompile
*cfg
, guint8
*code
, int size
, char *id
)
99 #ifndef DISABLE_LOGGING
100 GHashTable
*offset_to_bb_hash
= NULL
;
101 int i
, cindex
, bb_num
;
103 #ifdef PLATFORM_WIN32
104 const char *tmp
= g_get_tmp_dir ();
106 const char *objdump_args
= g_getenv ("MONO_OBJDUMP_ARGS");
111 #ifdef PLATFORM_WIN32
112 as_file
= g_strdup_printf ("%s/test.s", tmp
);
114 if (!(ofd
= fopen (as_file
, "w")))
115 g_assert_not_reached ();
117 i
= g_file_open_tmp (NULL
, &as_file
, NULL
);
118 ofd
= fdopen (i
, "w");
122 for (i
= 0; id
[i
]; ++i
) {
123 if (i
== 0 && isdigit (id
[i
]))
125 else if (!isalnum (id
[i
]))
128 fprintf (ofd
, "%c", id
[i
]);
130 fprintf (ofd
, ":\n");
132 if (emit_debug_info
&& cfg
!= NULL
) {
135 fprintf (ofd
, ".stabs \"\",100,0,0,.Ltext0\n");
136 fprintf (ofd
, ".stabs \"<BB>\",100,0,0,.Ltext0\n");
137 fprintf (ofd
, ".Ltext0:\n");
139 offset_to_bb_hash
= g_hash_table_new (NULL
, NULL
);
140 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
141 g_hash_table_insert (offset_to_bb_hash
, GINT_TO_POINTER (bb
->native_offset
), GINT_TO_POINTER (bb
->block_num
+ 1));
146 for (i
= 0; i
< size
; ++i
) {
147 if (emit_debug_info
) {
148 bb_num
= GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash
, GINT_TO_POINTER (i
)));
150 fprintf (ofd
, "\n.stabd 68,0,%d\n", bb_num
- 1);
155 fprintf (ofd
, "\n.byte %d", (unsigned int) code
[i
]);
157 fprintf (ofd
, ",%d", (unsigned int) code
[i
]);
168 #define DIS_CMD "otool64 -v -t"
170 #define DIS_CMD "otool -v -t"
173 #if defined(sparc) && !defined(__GNUC__)
174 #define DIS_CMD "dis"
175 #elif defined(__i386__) || defined(__x86_64__)
176 #define DIS_CMD "objdump -l -d"
178 #define DIS_CMD "objdump -d"
183 #define AS_CMD "as -xarch=v9"
184 #elif defined(__i386__) || defined(__x86_64__)
185 # if defined(__APPLE__)
188 # define AS_CMD "as -gstabs"
190 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
191 #define AS_CMD "as -mips32"
192 #elif defined(__ppc64__)
193 #define AS_CMD "as -arch ppc64"
194 #elif defined(__powerpc64__)
195 #define AS_CMD "as -mppc64"
200 #ifdef PLATFORM_WIN32
201 o_file
= g_strdup_printf ("%s/test.o", tmp
);
203 i
= g_file_open_tmp (NULL
, &o_file
, NULL
);
207 cmd
= g_strdup_printf (AS_CMD
" %s -o %s", as_file
, o_file
);
215 * The arm assembler inserts ELF directives instructing objdump to display
216 * everything as data.
218 cmd
= g_strdup_printf ("strip -x %s", o_file
);
223 cmd
= g_strdup_printf (DIS_CMD
" %s %s", objdump_args
, o_file
);
227 #ifndef PLATFORM_WIN32