2 * dis-cil.c: Disassembles CIL byte codes
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
20 #include "mono/metadata/opcodes.h"
21 #include "mono/utils/mono-compiler.h"
33 klass = fpclass (num);
43 #error "Don't know how to implement isinf for this platform."
48 #define CODE_INDENT g_assert (indent_level < 512); \
49 indent[indent_level*2] = ' '; \
50 indent[indent_level*2+1] = ' '; \
52 indent[indent_level*2] = 0;
53 #define CODE_UNINDENT g_assert (indent_level); \
55 indent[indent_level*2] = 0;
58 dissasemble_cil (MonoImage *m, MonoMethodHeader *mh, MonoGenericContext *context)
60 const unsigned char *start = mh->code;
61 int size = mh->code_size;
62 const unsigned char *end = start + size;
63 const unsigned char *ptr = start;
64 const MonoOpcode *entry;
66 int i, indent_level = 0;
67 gboolean in_fault = 0;
68 const char *clause_names[] = {"catch", "filter", "finally", "", "fault"};
69 gboolean *trys = NULL;
73 for (i = 0; i < mh->num_clauses; ++i) {
74 #define clause mh->clauses [i]
75 g_print ("/* out clause %d: from %d len=%d, handler at %d, %d */\n",
76 clause.flags, clause.try_offset, clause.try_len, clause.handler_offset, clause.handler_len);
81 if (mh->num_clauses) {
82 trys = g_malloc0 (sizeof (gboolean) * mh->num_clauses);
84 for (i=1; i < mh->num_clauses; ++i) {
85 #define pcl mh->clauses [i-1]
86 #define cl mh->clauses [i]
87 if (pcl.try_offset != cl.try_offset || pcl.try_len != cl.try_len)
95 for (i = mh->num_clauses - 1; i >= 0 ; --i) {
96 if (ptr == start + mh->clauses[i].try_offset && trys [i]) {
97 fprintf (output, "\t%s.try { // %d\n", indent, i);
101 if (ptr == start + mh->clauses[i].handler_offset) {
102 if (mh->clauses[i].flags == MONO_EXCEPTION_CLAUSE_FILTER) {
104 fprintf (output, "\t%s} { // %d\n", indent, i);
106 char * klass = mh->clauses[i].flags ? g_strdup ("") :
107 dis_stringify_object_with_class (m, mh->clauses[i].data.catch_class,
109 fprintf (output, "\t%s%s %s { // %d\n", indent,
110 clause_names [mh->clauses[i].flags], klass, i);
114 if (mh->clauses[i].flags == MONO_EXCEPTION_CLAUSE_FAULT)
117 if (mh->clauses[i].flags == MONO_EXCEPTION_CLAUSE_FILTER && ptr == start + mh->clauses[i].data.filter_offset) {
118 fprintf (output, "\t%s%s {\n", indent, clause_names[1]);
122 fprintf (output, "\t%sIL_%04x: ", indent, (int) (ptr - start));
128 entry = &mono_opcodes [i];
130 if (in_fault && entry->opval == 0xDC)
131 fprintf (output, " %s", "endfault");
133 fprintf (output, " %s ", mono_opcode_name (i));
135 switch (entry->argument){
136 case MonoInlineBrTarget: {
137 gint target = read32 (ptr);
138 fprintf (output, "IL_%04x\n", ((int) (ptr - start)) + 4 + target);
143 case MonoInlineField: {
144 guint32 token = read32 (ptr);
147 s = get_field (m, token, context);
148 fprintf (output, "%s", s);
155 int value = read32 (ptr);
157 fprintf (output, "%d", value);
163 gint64 top = read64 (ptr);
165 fprintf (output, "0x%llx", (long long) top);
170 case MonoInlineMethod: {
171 guint32 token = read32 (ptr);
174 s = get_method (m, token, context);
175 fprintf (output, "%s", s);
190 fprintf (output, "(00 00 00 00 00 00 f0 ff)"); /* negative infinity */
192 fprintf (output, "(00 00 00 00 00 00 f0 7f)"); /* positive infinity */
194 fprintf (output, "(00 00 00 00 00 00 f8 ff)"); /* NaN */
196 fprintf (output, "%.20g", r);
201 case MonoInlineSig: {
202 guint32 token = read32 (ptr);
203 fprintf (output, "signature-0x%08x", token);
208 case MonoInlineString: {
209 guint32 token = read32 (ptr);
210 const char *us_ptr = mono_metadata_user_string (m, token & 0xffffff);
211 int len = mono_metadata_decode_blob_size (us_ptr, (const char**)&us_ptr);
213 char *s = get_encoded_user_string_or_bytearray (us_ptr, len);
216 * See section 23.1.4 on the encoding of the #US heap
218 fprintf (output, "%s", s);
224 case MonoInlineSwitch: {
225 guint32 count = read32 (ptr);
226 const unsigned char *endswitch;
230 endswitch = ptr + sizeof (guint32) * count;
231 fprintf (output, count > 0 ? "(\n" : "( )");
233 for (n = 0; n < count; n++){
234 fprintf (output, "\t%sIL_%04x%s", indent,
235 (int)(endswitch-start+read32 (ptr)),
236 n == count - 1 ? ")" : ",\n");
243 case MonoInlineTok: {
244 guint32 token = read32 (ptr);
247 s = get_token (m, token, context);
248 fprintf (output, "%s", s);
255 case MonoInlineType: {
256 guint32 token = read32 (ptr);
257 char *s = get_token_type (m, token, context);
258 fprintf (output, "%s", s);
264 case MonoInlineVar: {
265 guint16 var_idx = read16 (ptr);
267 fprintf (output, "%d\n", var_idx);
272 case MonoShortInlineBrTarget: {
273 signed char x = *ptr;
275 fprintf (output, "IL_%04x\n", (int)(ptr - start + 1 + x));
280 case MonoShortInlineI: {
283 fprintf (output, "0x%02x", x);
288 case MonoShortInlineR: {
296 fprintf (output, "(00 00 80 ff)"); /* negative infinity */
298 fprintf (output, "(00 00 80 7f)"); /* positive infinity */
300 fprintf (output, "(00 00 c0 ff)"); /* NaN */
302 fprintf (output, "%.20g", (double) f);
307 case MonoShortInlineVar: {
308 unsigned char x = *ptr;
310 fprintf (output, "%d", (int) x);
318 fprintf (output, "\n");
319 for (i = 0; i < mh->num_clauses; ++i) {
320 if (ptr == start + mh->clauses[i].try_offset + mh->clauses[i].try_len && trys [i]) {
322 fprintf (output, "\t%s} // end .try %d\n", indent, i);
324 if (ptr == start + mh->clauses[i].handler_offset + mh->clauses[i].handler_len) {
326 fprintf (output, "\t%s} // end handler %d\n", indent, i);
327 if (mh->clauses[i].flags == MONO_EXCEPTION_CLAUSE_FAULT)