2 * dis-cil.c: Disassembles CIL byte codes
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
19 #include "mono/metadata/opcodes.h"
22 * Strings on the US heap are encoded using UTF-16. Poor man's
23 * UTF-16 to UTF-8. I know its broken, use libunicode later.
26 get_encoded_user_string (const char *ptr)
31 len = mono_metadata_decode_blob_size (ptr, &ptr);
32 res = g_malloc (len + 1);
35 * I should really use some kind of libunicode here
37 for (i = 0, j = 0; i < len; j++, i += 2)
45 #define CODE_INDENT g_assert (indent_level < 512); \
46 indent[indent_level*2] = ' '; \
47 indent[indent_level*2+1] = ' '; \
49 indent[indent_level*2] = 0;
50 #define CODE_UNINDENT g_assert (indent_level); \
52 indent[indent_level*2] = 0;
55 dissasemble_cil (MonoImage *m, MonoMethodHeader *mh)
57 const unsigned char *start = mh->code;
58 int size = mh->code_size;
59 const unsigned char *end = start + size;
60 const unsigned char *ptr = start;
61 const MonoOpcode *entry;
63 int i, indent_level = 0;
64 const char *clause_names[] = {"catch", "filter", "finally", "fault"};
69 for (i = 0; i < mh->num_clauses; ++i) {
70 #define clause mh->clauses [i]
71 g_print ("out clause %d: from %d len=%d, handler at %d, %d\n",
72 clause.flags, clause.try_offset, clause.try_len, clause.handler_offset, clause.handler_len);
77 for (i = mh->num_clauses - 1; i >= 0 ; --i) {
78 if ((mh->clauses[i].flags == 0 || mh->clauses[i].flags == 2) && ptr == start + mh->clauses[i].try_offset) {
79 fprintf (output, "\t%s.try { // %d\n", indent, i);
82 if (ptr == start + mh->clauses[i].handler_offset) {
83 char * klass = mh->clauses[i].flags ? g_strdup ("") : dis_stringify_token (m, mh->clauses[i].token_or_filter);
84 fprintf (output, "\t%s%s %s { // %d\n", indent, clause_names [mh->clauses[i].flags], klass, i);
89 fprintf (output, "\t%sIL_%04x: ", indent, (int) (ptr - start));
95 entry = &mono_opcodes [i];
97 fprintf (output, "%s ", mono_opcode_names [i]);
99 switch (entry->argument){
100 case MonoInlineBrTarget: {
101 gint target = read32 (ptr);
102 fprintf (output, "IL_%04x\n", ((int) (ptr - start)) + 4 + target);
107 case MonoInlineField: {
108 guint32 token = read32 (ptr);
111 s = get_field (m, token);
112 fprintf (output, "%s", s);
119 int value = read32 (ptr);
121 fprintf (output, "%d", value);
127 gint64 top = read64 (ptr);
129 fprintf (output, "0x%llx", (long long) top);
134 case MonoInlineMethod: {
135 guint32 token = read32 (ptr);
138 s = get_method (m, token);
139 fprintf (output, "%s", s);
151 fprintf (output, "%g", r);
156 case MonoInlineSig: {
157 guint32 token = read32 (ptr);
158 fprintf (output, "signature-0x%08x", token);
163 case MonoInlineString: {
164 guint32 token = read32 (ptr);
166 char *s = get_encoded_user_string (
167 mono_metadata_user_string (m, token & 0xffffff));
170 * See section 23.1.4 on the encoding of the #US heap
172 fprintf (output, "\"%s\"", s);
178 case MonoInlineSwitch: {
179 guint32 count = read32 (ptr);
180 const unsigned char *endswitch;
184 endswitch = ptr + sizeof (guint32) * count;
185 fprintf (output, "(\n");
187 for (n = 0; n < count; n++){
188 fprintf (output, "\t%sIL_%04x%s", indent,
189 endswitch-start+read32 (ptr),
190 n == count - 1 ? ")" : ",\n");
197 case MonoInlineTok: {
198 guint32 token = read32 (ptr);
201 s = get_token (m, token);
202 fprintf (output, "%s", s);
209 case MonoInlineType: {
210 guint32 token = read32 (ptr);
211 char *s = get_token_type (m, token);
212 fprintf (output, "%s", s);
218 case MonoInlineVar: {
219 guint16 var_idx = read16 (ptr);
221 fprintf (output, "%d\n", var_idx);
226 case MonoShortInlineBrTarget: {
227 signed char x = *ptr;
229 fprintf (output, "IL_%04x\n", ptr - start + 1 + x);
234 case MonoShortInlineI: {
237 fprintf (output, "0x%02x", x);
242 case MonoShortInlineR: {
246 fprintf (output, "%g", (double) f);
251 case MonoShortInlineVar: {
252 unsigned char x = *ptr;
254 fprintf (output, "%d", (int) x);
262 fprintf (output, "\n");
263 for (i = 0; i < mh->num_clauses; ++i) {
264 if ((mh->clauses[i].flags == 0 || mh->clauses[i].flags == 2) && ptr == start + mh->clauses[i].try_offset + mh->clauses[i].try_len) {
266 fprintf (output, "\t%s} // end .try %d\n", indent, i);
268 if (ptr == start + mh->clauses[i].handler_offset + mh->clauses[i].handler_len) {
270 fprintf (output, "\t%s} // end handler %d\n", indent, i);