2 * dis-cil.c: Disassembles CIL byte codes
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
38 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
46 * we are not really using any of the following:
52 static opcode_t opcodes [300] = {
53 #include "mono/cil/opcode.def"
57 * Strings on the US heap are encoded using UTF-16. Poor man's
58 * UTF-16 to UTF-8. I know its broken, use libunicode later.
61 get_encoded_user_string (const char *ptr)
66 len = mono_metadata_decode_blob_size (ptr, &ptr);
67 res = g_malloc (len + 1);
70 * I should really use some kind of libunicode here
72 for (i = 0, j = 0; i < len; j++, i += 2)
81 dissasemble_cil (metadata_t *m, const unsigned char *start, int size)
83 const unsigned char *end = start + size;
84 const unsigned char *ptr = start;
90 entry = &opcodes [*ptr + 256];
92 entry = &opcodes [*ptr];
94 fprintf (output, "\tIL_%04x: %s ", (int) (ptr - start), entry->name);
96 switch (entry->argument){
97 case InlineBrTarget: {
98 gint target = *(gint32 *) ptr;
99 fprintf (output, "IL_%04x", ((int) (ptr - start)) + 4 + target);
105 guint32 token = *(guint32 *) ptr;
108 s = get_field (m, token);
109 fprintf (output, "%s", s);
116 int value = *(int *) ptr;
118 fprintf (output, "%d", value);
124 gint64 top = *(guint64 *) ptr;
126 fprintf (output, "%lld", (long long) top);
132 guint32 token = *(guint32 *) ptr;
135 s = get_method (m, token);
136 fprintf (output, "%s", s);
146 double r = *(double *) ptr;
147 fprintf (output, "%g", r);
153 guint32 token = *(guint32 *) ptr;
154 fprintf (output, "signature-0x%08x", token);
160 guint32 token = *(guint32 *) ptr;
162 char *s = get_encoded_user_string (
163 mono_metadata_user_string (m, token & 0xffffff));
166 * See section 23.1.4 on the encoding of the #US heap
168 fprintf (output, "\"%s\"", s);
175 guint32 count = *(guint32 *) ptr;
179 fprintf (output, "(\n\t\t\t");
180 for (i = 0; i < count; i++){
181 fprintf (output, "IL_%x", *(guint32 *) ptr);
184 fprintf (output, "\t\t\t)");
189 guint32 token = *(guint32 *) ptr;
192 s = get_token (m, token);
193 fprintf (output, "%s", s);
201 guint32 token = *(guint32 *) ptr;
202 char *s = get_token_type (m, token);
203 fprintf (output, "%s", s);
210 gint16 var_idx = *(gint16 *) ptr;
212 fprintf (output, "variable-%d\n", var_idx);
217 case ShortInlineBrTarget: {
218 signed char x = *ptr;
220 fprintf (output, "IL_%04x", ptr - start + 1 + x);
228 fprintf (output, "0x%02x", x);
234 float f = *(float *) ptr;
236 fprintf (output, "%g", (double) f);
241 case ShortInlineVar: {
242 signed char x = *ptr;
244 fprintf (output, "V_%d", (int) x);
251 fprintf (output, "\n");