2 * helpers.c: Assorted routines
4 * (C) 2003 Ximian, Inc.
8 #include <mono/metadata/opcodes.h>
14 #ifdef HAVE_ARRAY_ELEM_INIT
15 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
16 #define MSGSTRFIELD1(line) str##line
17 static const struct msgstr_t {
18 #define MINI_OP(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
22 #define MINI_OP(a,b) b,
26 static const gint16 opidx [] = {
27 #define MINI_OP(a,b) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
34 #define MINI_OP(a,b) b,
35 /* keep in sync with the enum in mini.h */
36 static const char* const
45 #define emit_debug_info TRUE
47 #define emit_debug_info FALSE
51 mono_inst_name (int op) {
52 if (op >= OP_LOAD && op <= OP_LAST)
53 #ifdef HAVE_ARRAY_ELEM_INIT
54 return (const char*)&opstr + opidx [op - OP_LOAD];
56 return opnames [op - OP_LOAD];
59 return mono_opcode_name (op);
60 g_error ("unknown opcode name for %d", op);
65 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom)
67 #ifndef DISABLE_LOGGING
71 g_print ("%s:", name);
73 mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
75 g_print (" [BB%d]", cfg->bblocks [i]->block_num);
77 g_print (" BB%d", cfg->bblocks [i]->block_num);
85 * mono_disassemble_code:
86 * @cfg: compilation context
87 * @code: a pointer to the code
88 * @size: the code size in bytes
90 * Disassemble to code to stdout.
93 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
95 #ifndef DISABLE_LOGGING
96 GHashTable *offset_to_bb_hash = NULL;
97 int i, cindex, bb_num;
99 const char *tmp = g_get_tmp_dir ();
100 const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
105 as_file = g_strdup_printf ("%s/test.s", tmp);
107 if (!(ofd = fopen (as_file, "w")))
108 g_assert_not_reached ();
110 for (i = 0; id [i]; ++i) {
111 if (!isalnum (id [i]))
114 fprintf (ofd, "%c", id [i]);
116 fprintf (ofd, ":\n");
118 if (emit_debug_info) {
121 fprintf (ofd, ".stabs \"\",100,0,0,.Ltext0\n");
122 fprintf (ofd, ".stabs \"<BB>\",100,0,0,.Ltext0\n");
123 fprintf (ofd, ".Ltext0:\n");
125 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
126 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
127 g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
132 for (i = 0; i < size; ++i) {
133 if (emit_debug_info) {
134 bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
136 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
141 fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
143 fprintf (ofd, ",%d", (unsigned int) code [i]);
152 #define DIS_CMD "otool -v -t"
154 #if defined(sparc) && !defined(__GNUC__)
155 #define DIS_CMD "dis"
156 #elif defined(__i386__)
157 #define DIS_CMD "objdump -l -d"
159 #define DIS_CMD "objdump -d"
164 #define AS_CMD "as -xarch=v9"
165 #elif defined(__i386__)
166 #define AS_CMD "as -gstabs"
171 o_file = g_strdup_printf ("%s/test.o", tmp);
172 cmd = g_strdup_printf (AS_CMD " %s -o %s", as_file, o_file);
178 cmd = g_strdup_printf (DIS_CMD " %s %s", objdump_args, o_file);