2 * helpers.c: Assorted routines
4 * (C) 2003 Ximian, Inc.
8 #include <mono/metadata/opcodes.h>
13 #define MINI_OP(a,b) b,
14 /* keep in sync with the enum in mini.h */
15 static const char* const
22 static gboolean emit_debug_info = TRUE;
24 static gboolean emit_debug_info = FALSE;
28 mono_inst_name (int op) {
29 if (op >= OP_LOAD && op <= OP_LAST)
30 return opnames [op - OP_LOAD];
32 return mono_opcode_name (op);
33 g_error ("unknown opcode name for %d", op);
38 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
43 g_print ("%s:", name);
45 mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
47 g_print (" [BB%d]", cfg->bblocks [i]->block_num);
49 g_print (" BB%d", cfg->bblocks [i]->block_num);
56 * mono_disassemble_code:
57 * @cfg: compilation context
58 * @code: a pointer to the code
59 * @size: the code size in bytes
61 * Disassemble to code to stdout.
64 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
66 GHashTable *offset_to_bb_hash = NULL;
69 const char *tmp = g_get_tmp_dir ();
70 const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
75 as_file = g_strdup_printf ("%s/test.s", tmp);
77 if (!(ofd = fopen (as_file, "w")))
78 g_assert_not_reached ();
80 for (i = 0; id [i]; ++i) {
81 if (!isalnum (id [i]))
84 fprintf (ofd, "%c", id [i]);
88 if (emit_debug_info) {
91 fprintf (ofd, ".stabs \"\",100,0,0,.Ltext0\n");
92 fprintf (ofd, ".stabs \"<BB>\",100,0,0,.Ltext0\n");
93 fprintf (ofd, ".Ltext0:\n");
95 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
96 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
97 g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
101 for (i = 0; i < size; ++i) {
102 if (emit_debug_info) {
103 bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
105 fprintf (ofd, ".stabd 68,0,%d\n", bb_num - 1);
107 fprintf (ofd, ".byte %d\n", (unsigned int) code [i]);
111 #define DIS_CMD "otool -v -t"
113 #if defined(sparc) && !defined(__GNUC__)
114 #define DIS_CMD "dis"
115 #elif defined(__i386__)
116 #define DIS_CMD "objdump -l -d"
118 #define DIS_CMD "objdump -d"
123 #define AS_CMD "as -xarch=v9"
124 #elif defined(__i386__)
125 #define AS_CMD "as -gstabs"
130 o_file = g_strdup_printf ("%s/test.o", tmp);
131 cmd = g_strdup_printf (AS_CMD " %s -o %s", as_file, o_file);
137 cmd = g_strdup_printf (DIS_CMD " %s %s", objdump_args, o_file);