2006-03-10 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / helpers.c
1 /*
2  * helpers.c: Assorted routines
3  *
4  * (C) 2003 Ximian, Inc.
5  */
6 #include "mini.h"
7 #include <ctype.h>
8 #include <mono/metadata/opcodes.h>
9
10 #ifdef MINI_OP
11 #undef MINI_OP
12 #endif
13
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)];
19 #include "mini-ops.h"
20 #undef MINI_OP
21 } opstr = {
22 #define MINI_OP(a,b) b,
23 #include "mini-ops.h"
24 #undef MINI_OP
25 };
26 static const gint16 opidx [] = {
27 #define MINI_OP(a,b) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
28 #include "mini-ops.h"
29 #undef MINI_OP
30 };
31
32 #else
33
34 #define MINI_OP(a,b) b,
35 /* keep in sync with the enum in mini.h */
36 static const char* const
37 opnames[] = {
38 #include "mini-ops.h"
39 };
40 #undef MINI_OP
41
42 #endif
43
44 #ifdef __i386__
45 #define emit_debug_info  TRUE
46 #else
47 #define emit_debug_info  FALSE
48 #endif
49
50 const char*
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];
55 #else
56                 return opnames [op - OP_LOAD];
57 #endif
58         if (op < OP_LOAD)
59                 return mono_opcode_name (op);
60         g_error ("unknown opcode name for %d", op);
61         return NULL;
62 }
63
64 void
65 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom) 
66 {
67 #ifndef DISABLE_LOGGING
68         int i;
69
70         if (name)
71                 g_print ("%s:", name);
72         
73         mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
74                 if (idom == i)
75                         g_print (" [BB%d]", cfg->bblocks [i]->block_num);
76                 else
77                         g_print (" BB%d", cfg->bblocks [i]->block_num);
78                 
79         }
80         g_print ("\n");
81 #endif
82 }
83
84 /**
85  * mono_disassemble_code:
86  * @cfg: compilation context
87  * @code: a pointer to the code
88  * @size: the code size in bytes
89  *
90  * Disassemble to code to stdout.
91  */
92 void
93 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
94 {
95 #ifndef DISABLE_LOGGING
96         GHashTable *offset_to_bb_hash = NULL;
97         int i, cindex, bb_num;
98         FILE *ofd;
99         const char *tmp = g_get_tmp_dir ();
100         const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
101         char *as_file;
102         char *o_file;
103         char *cmd;
104         
105         as_file = g_strdup_printf ("%s/test.s", tmp);    
106
107         if (!(ofd = fopen (as_file, "w")))
108                 g_assert_not_reached ();
109
110         for (i = 0; id [i]; ++i) {
111                 if (!isalnum (id [i]))
112                         fprintf (ofd, "_");
113                 else
114                         fprintf (ofd, "%c", id [i]);
115         }
116         fprintf (ofd, ":\n");
117
118         if (emit_debug_info) {
119                 MonoBasicBlock *bb;
120
121                 fprintf (ofd, ".stabs   \"\",100,0,0,.Ltext0\n");
122                 fprintf (ofd, ".stabs   \"<BB>\",100,0,0,.Ltext0\n");
123                 fprintf (ofd, ".Ltext0:\n");
124
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));
128                 }
129         }
130
131         cindex = 0;
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)));
135                         if (bb_num) {
136                                 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
137                                 cindex = 0;
138                         }
139                 }
140                 if (cindex == 0) {
141                         fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
142                 } else {
143                         fprintf (ofd, ",%d", (unsigned int) code [i]);
144                 }
145                 cindex++;
146                 if (cindex == 64)
147                         cindex = 0;
148         }
149         fprintf (ofd, "\n");
150         fclose (ofd);
151 #ifdef __APPLE__
152 #define DIS_CMD "otool -v -t"
153 #else
154 #if defined(sparc) && !defined(__GNUC__)
155 #define DIS_CMD "dis"
156 #elif defined(__i386__)
157 #define DIS_CMD "objdump -l -d"
158 #else
159 #define DIS_CMD "objdump -d"
160 #endif
161 #endif
162
163 #if defined(sparc)
164 #define AS_CMD "as -xarch=v9"
165 #elif defined(__i386__)
166 #define AS_CMD "as -gstabs"
167 #else
168 #define AS_CMD "as"
169 #endif
170
171         o_file = g_strdup_printf ("%s/test.o", tmp);    
172         cmd = g_strdup_printf (AS_CMD " %s -o %s", as_file, o_file);
173         system (cmd); 
174         g_free (cmd);
175         if (!objdump_args)
176                 objdump_args = "";
177         
178         cmd = g_strdup_printf (DIS_CMD " %s %s", objdump_args, o_file);
179         system (cmd);
180         g_free (cmd);
181         
182         g_free (o_file);
183         g_free (as_file);
184 #endif
185 }
186