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 /*This enables us to use the right tooling when building the cross compiler for iOS.*/
70 #if defined (__APPLE__) && defined (TARGET_ARM) && (defined(__i386__) || defined(__x86_64__))
72 #define ARCH_PREFIX "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/"
76 #define ARCH_PREFIX ""
77 //#define ARCH_PREFIX "powerpc64-linux-gnu-"
80 mono_inst_name (int op
) {
81 #ifndef DISABLE_LOGGING
82 if (op
>= OP_LOAD
&& op
<= OP_LAST
)
83 #ifdef HAVE_ARRAY_ELEM_INIT
84 return (const char*)&opstr
+ opidx
[op
- OP_LOAD
];
86 return opnames
[op
- OP_LOAD
];
89 return mono_opcode_name (op
);
90 g_error ("unknown opcode name for %d", op
);
93 g_assert_not_reached ();
98 mono_blockset_print (MonoCompile
*cfg
, MonoBitSet
*set
, const char *name
, guint idom
)
100 #ifndef DISABLE_LOGGING
104 g_print ("%s:", name
);
106 mono_bitset_foreach_bit (set
, i
, cfg
->num_bblocks
) {
108 g_print (" [BB%d]", cfg
->bblocks
[i
]->block_num
);
110 g_print (" BB%d", cfg
->bblocks
[i
]->block_num
);
118 * mono_disassemble_code:
119 * @cfg: compilation context
120 * @code: a pointer to the code
121 * @size: the code size in bytes
123 * Disassemble to code to stdout.
126 mono_disassemble_code (MonoCompile
*cfg
, guint8
*code
, int size
, char *id
)
128 #ifndef DISABLE_LOGGING
129 GHashTable
*offset_to_bb_hash
= NULL
;
130 int i
, cindex
, bb_num
;
133 const char *tmp
= g_get_tmp_dir ();
135 const char *objdump_args
= g_getenv ("MONO_OBJDUMP_ARGS");
142 as_file
= g_strdup_printf ("%s/test.s", tmp
);
144 if (!(ofd
= fopen (as_file
, "w")))
145 g_assert_not_reached ();
147 i
= g_file_open_tmp (NULL
, &as_file
, NULL
);
148 ofd
= fdopen (i
, "w");
152 for (i
= 0; id
[i
]; ++i
) {
153 if (i
== 0 && isdigit (id
[i
]))
155 else if (!isalnum (id
[i
]))
158 fprintf (ofd
, "%c", id
[i
]);
160 fprintf (ofd
, ":\n");
162 if (emit_debug_info
&& cfg
!= NULL
) {
165 fprintf (ofd
, ".stabs \"\",100,0,0,.Ltext0\n");
166 fprintf (ofd
, ".stabs \"<BB>\",100,0,0,.Ltext0\n");
167 fprintf (ofd
, ".Ltext0:\n");
169 offset_to_bb_hash
= g_hash_table_new (NULL
, NULL
);
170 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
171 g_hash_table_insert (offset_to_bb_hash
, GINT_TO_POINTER (bb
->native_offset
), GINT_TO_POINTER (bb
->block_num
+ 1));
176 for (i
= 0; i
< size
; ++i
) {
177 if (emit_debug_info
&& cfg
!= NULL
) {
178 bb_num
= GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash
, GINT_TO_POINTER (i
)));
180 fprintf (ofd
, "\n.stabd 68,0,%d\n", bb_num
- 1);
185 fprintf (ofd
, "\n.byte %d", (unsigned int) code
[i
]);
187 fprintf (ofd
, ",%d", (unsigned int) code
[i
]);
198 #define DIS_CMD "otool64 -v -t"
200 #define DIS_CMD "otool -v -t"
203 #if defined(sparc) && !defined(__GNUC__)
204 #define DIS_CMD "dis"
205 #elif defined(__i386__) || defined(__x86_64__)
206 #define DIS_CMD "objdump -l -d"
208 #define DIS_CMD "objdump -d"
213 #define AS_CMD "as -xarch=v9"
214 #elif defined (TARGET_X86)
215 # if defined(__APPLE__)
216 # define AS_CMD "as -arch i386"
218 # define AS_CMD "as -gstabs"
220 #elif defined (TARGET_AMD64)
221 # if defined (__APPLE__)
222 # define AS_CMD "as -arch x86_64"
224 # define AS_CMD "as -gstabs"
226 #elif defined (TARGET_ARM)
227 # if defined (__APPLE__)
228 # define AS_CMD "as -arch arm"
230 # define AS_CMD "as -gstabs"
232 #elif defined(__mips__) && (_MIPS_SIM == _ABIO32)
233 #define AS_CMD "as -mips32"
234 #elif defined(__ppc64__)
235 #define AS_CMD "as -arch ppc64"
236 #elif defined(__powerpc64__)
237 #define AS_CMD "as -mppc64"
243 o_file
= g_strdup_printf ("%s/test.o", tmp
);
245 i
= g_file_open_tmp (NULL
, &o_file
, NULL
);
249 cmd
= g_strdup_printf (ARCH_PREFIX AS_CMD
" %s -o %s", as_file
, o_file
);
250 unused
= system (cmd
);
259 * The arm assembler inserts ELF directives instructing objdump to display
260 * everything as data.
262 cmd
= g_strdup_printf (ARCH_PREFIX
"strip -x %s", o_file
);
263 unused
= system (cmd
);
267 cmd
= g_strdup_printf (ARCH_PREFIX DIS_CMD
" %s %s", objdump_args
, o_file
);
268 unused
= system (cmd
);