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 (!isalnum (id [i]))
126 fprintf (ofd, "%c", id [i]);
128 fprintf (ofd, ":\n");
130 if (emit_debug_info && cfg != NULL) {
133 fprintf (ofd, ".stabs \"\",100,0,0,.Ltext0\n");
134 fprintf (ofd, ".stabs \"<BB>\",100,0,0,.Ltext0\n");
135 fprintf (ofd, ".Ltext0:\n");
137 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
138 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
139 g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
144 for (i = 0; i < size; ++i) {
145 if (emit_debug_info) {
146 bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
148 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
153 fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
155 fprintf (ofd, ",%d", (unsigned int) code [i]);
166 #define DIS_CMD "otool64 -v -t"
168 #define DIS_CMD "otool -v -t"
171 #if defined(sparc) && !defined(__GNUC__)
172 #define DIS_CMD "dis"
173 #elif defined(__i386__) || defined(__x86_64__)
174 #define DIS_CMD "objdump -l -d"
176 #define DIS_CMD "objdump -d"
181 #define AS_CMD "as -xarch=v9"
182 #elif defined(__i386__) || defined(__x86_64__)
183 #define AS_CMD "as -gstabs"
184 #elif defined(__mips__)
185 #define AS_CMD "as -mips32"
186 #elif defined(__ppc64__)
187 #define AS_CMD "as -arch ppc64"
188 #elif defined(__powerpc64__)
189 #define AS_CMD "as -mppc64"
194 #ifdef PLATFORM_WIN32
195 o_file = g_strdup_printf ("%s/test.o", tmp);
197 i = g_file_open_tmp (NULL, &o_file, NULL);
201 cmd = g_strdup_printf (AS_CMD " %s -o %s", as_file, o_file);
209 * The arm assembler inserts ELF directives instructing objdump to display
210 * everything as data.
212 cmd = g_strdup_printf ("strip -x %s", o_file);
217 cmd = g_strdup_printf (DIS_CMD " %s %s", objdump_args, o_file);
221 #ifndef PLATFORM_WIN32