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
66 mono_inst_name (int op) {
67 if (op >= OP_LOAD && op <= OP_LAST)
68 #ifdef HAVE_ARRAY_ELEM_INIT
69 return (const char*)&opstr + opidx [op - OP_LOAD];
71 return opnames [op - OP_LOAD];
74 return mono_opcode_name (op);
75 g_error ("unknown opcode name for %d", op);
80 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
82 #ifndef DISABLE_LOGGING
86 g_print ("%s:", name);
88 mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
90 g_print (" [BB%d]", cfg->bblocks [i]->block_num);
92 g_print (" BB%d", cfg->bblocks [i]->block_num);
100 * mono_disassemble_code:
101 * @cfg: compilation context
102 * @code: a pointer to the code
103 * @size: the code size in bytes
105 * Disassemble to code to stdout.
108 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
110 #ifndef DISABLE_LOGGING
111 GHashTable *offset_to_bb_hash = NULL;
112 int i, cindex, bb_num;
114 #ifdef PLATFORM_WIN32
115 const char *tmp = g_get_tmp_dir ();
117 const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
122 #ifdef PLATFORM_WIN32
123 as_file = g_strdup_printf ("%s/test.s", tmp);
125 if (!(ofd = fopen (as_file, "w")))
126 g_assert_not_reached ();
128 i = g_file_open_tmp (NULL, &as_file, NULL);
129 ofd = fdopen (i, "w");
133 for (i = 0; id [i]; ++i) {
134 if (i == 0 && isdigit (id [i]))
136 else if (!isalnum (id [i]))
139 fprintf (ofd, "%c", id [i]);
141 fprintf (ofd, ":\n");
143 if (emit_debug_info && cfg != NULL) {
146 fprintf (ofd, ".stabs \"\",100,0,0,.Ltext0\n");
147 fprintf (ofd, ".stabs \"<BB>\",100,0,0,.Ltext0\n");
148 fprintf (ofd, ".Ltext0:\n");
150 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
151 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
152 g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
157 for (i = 0; i < size; ++i) {
158 if (emit_debug_info) {
159 bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
161 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
166 fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
168 fprintf (ofd, ",%d", (unsigned int) code [i]);
179 #define DIS_CMD "otool64 -v -t"
181 #define DIS_CMD "otool -v -t"
184 #if defined(sparc) && !defined(__GNUC__)
185 #define DIS_CMD "dis"
186 #elif defined(__i386__) || defined(__x86_64__)
187 #define DIS_CMD "objdump -l -d"
189 #define DIS_CMD "objdump -d"
194 #define AS_CMD "as -xarch=v9"
195 #elif defined(__i386__) || defined(__x86_64__)
196 # if defined(__APPLE__)
199 # define AS_CMD "as -gstabs"
201 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
202 #define AS_CMD "as -mips32"
203 #elif defined(__ppc64__)
204 #define AS_CMD "as -arch ppc64"
205 #elif defined(__powerpc64__)
206 #define AS_CMD "as -mppc64"
211 #ifdef PLATFORM_WIN32
212 o_file = g_strdup_printf ("%s/test.o", tmp);
214 i = g_file_open_tmp (NULL, &o_file, NULL);
218 cmd = g_strdup_printf (AS_CMD " %s -o %s", as_file, o_file);
226 * The arm assembler inserts ELF directives instructing objdump to display
227 * everything as data.
229 cmd = g_strdup_printf ("strip -x %s", o_file);
234 cmd = g_strdup_printf (DIS_CMD " %s %s", objdump_args, o_file);
238 #ifndef PLATFORM_WIN32