New test.
[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 #ifndef PLATFORM_WIN32
11 #include <unistd.h>
12 #endif
13
14 #ifdef MINI_OP
15 #undef MINI_OP
16 #endif
17
18 #ifdef HAVE_ARRAY_ELEM_INIT
19 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
20 #define MSGSTRFIELD1(line) str##line
21 static const struct msgstr_t {
22 #define MINI_OP(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
23 #include "mini-ops.h"
24 #undef MINI_OP
25 } opstr = {
26 #define MINI_OP(a,b) b,
27 #include "mini-ops.h"
28 #undef MINI_OP
29 };
30 static const gint16 opidx [] = {
31 #define MINI_OP(a,b) [a - OP_LOAD] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
32 #include "mini-ops.h"
33 #undef MINI_OP
34 };
35
36 #else
37
38 #define MINI_OP(a,b) b,
39 /* keep in sync with the enum in mini.h */
40 static const char* const
41 opnames[] = {
42 #include "mini-ops.h"
43 };
44 #undef MINI_OP
45
46 #endif
47
48 #if defined(__i386__) || defined(__x86_64__)
49 #define emit_debug_info  TRUE
50 #else
51 #define emit_debug_info  FALSE
52 #endif
53
54 const char*
55 mono_inst_name (int op) {
56         if (op >= OP_LOAD && op <= OP_LAST)
57 #ifdef HAVE_ARRAY_ELEM_INIT
58                 return (const char*)&opstr + opidx [op - OP_LOAD];
59 #else
60                 return opnames [op - OP_LOAD];
61 #endif
62         if (op < OP_LOAD)
63                 return mono_opcode_name (op);
64         g_error ("unknown opcode name for %d", op);
65         return NULL;
66 }
67
68 void
69 mono_blockset_print (MonoCompile *cfg, MonoBitSet *set, const char *name, guint idom) 
70 {
71 #ifndef DISABLE_LOGGING
72         int i;
73
74         if (name)
75                 g_print ("%s:", name);
76         
77         mono_bitset_foreach_bit (set, i, cfg->num_bblocks) {
78                 if (idom == i)
79                         g_print (" [BB%d]", cfg->bblocks [i]->block_num);
80                 else
81                         g_print (" BB%d", cfg->bblocks [i]->block_num);
82                 
83         }
84         g_print ("\n");
85 #endif
86 }
87
88 /**
89  * mono_disassemble_code:
90  * @cfg: compilation context
91  * @code: a pointer to the code
92  * @size: the code size in bytes
93  *
94  * Disassemble to code to stdout.
95  */
96 void
97 mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
98 {
99 #ifndef DISABLE_LOGGING
100         GHashTable *offset_to_bb_hash = NULL;
101         int i, cindex, bb_num;
102         FILE *ofd;
103 #ifdef PLATFORM_WIN32
104         const char *tmp = g_get_tmp_dir ();
105 #endif
106         const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
107         char *as_file;
108         char *o_file;
109         char *cmd;
110
111 #ifdef PLATFORM_WIN32
112         as_file = g_strdup_printf ("%s/test.s", tmp);    
113
114         if (!(ofd = fopen (as_file, "w")))
115                 g_assert_not_reached ();
116 #else   
117         i = g_file_open_tmp (NULL, &as_file, NULL);
118         ofd = fdopen (i, "w");
119         g_assert (ofd);
120 #endif
121
122         for (i = 0; id [i]; ++i) {
123                 if (!isalnum (id [i]))
124                         fprintf (ofd, "_");
125                 else
126                         fprintf (ofd, "%c", id [i]);
127         }
128         fprintf (ofd, ":\n");
129
130         if (emit_debug_info) {
131                 MonoBasicBlock *bb;
132
133                 fprintf (ofd, ".stabs   \"\",100,0,0,.Ltext0\n");
134                 fprintf (ofd, ".stabs   \"<BB>\",100,0,0,.Ltext0\n");
135                 fprintf (ofd, ".Ltext0:\n");
136
137                 offset_to_bb_hash = g_hash_table_new (NULL, NULL);
138                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
139                         g_hash_table_insert (offset_to_bb_hash, GINT_TO_POINTER (bb->native_offset), GINT_TO_POINTER (bb->block_num + 1));
140                 }
141         }
142
143         cindex = 0;
144         for (i = 0; i < size; ++i) {
145                 if (emit_debug_info) {
146                         bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
147                         if (bb_num) {
148                                 fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
149                                 cindex = 0;
150                         }
151                 }
152                 if (cindex == 0) {
153                         fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
154                 } else {
155                         fprintf (ofd, ",%d", (unsigned int) code [i]);
156                 }
157                 cindex++;
158                 if (cindex == 64)
159                         cindex = 0;
160         }
161         fprintf (ofd, "\n");
162         fclose (ofd);
163 #ifdef __APPLE__
164 #define DIS_CMD "otool -v -t"
165 #else
166 #if defined(sparc) && !defined(__GNUC__)
167 #define DIS_CMD "dis"
168 #elif defined(__i386__) || defined(__x86_64__)
169 #define DIS_CMD "objdump -l -d"
170 #else
171 #define DIS_CMD "objdump -d"
172 #endif
173 #endif
174
175 #if defined(sparc)
176 #define AS_CMD "as -xarch=v9"
177 #elif defined(__i386__) || defined(__x86_64__)
178 #define AS_CMD "as -gstabs"
179 #else
180 #define AS_CMD "as"
181 #endif
182
183 #ifdef PLATFORM_WIN32
184         o_file = g_strdup_printf ("%s/test.o", tmp);
185 #else   
186         i = g_file_open_tmp (NULL, &o_file, NULL);
187         close (i);
188 #endif
189
190         cmd = g_strdup_printf (AS_CMD " %s -o %s", as_file, o_file);
191         system (cmd); 
192         g_free (cmd);
193         if (!objdump_args)
194                 objdump_args = "";
195         
196         cmd = g_strdup_printf (DIS_CMD " %s %s", objdump_args, o_file);
197         system (cmd);
198         g_free (cmd);
199         
200         g_free (o_file);
201         g_free (as_file);
202 #endif
203 }
204