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)
42 result = g_strescape (res, NULL);
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)
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_token (m, mh->clauses[i].token_or_filter);
108 fprintf (output, "\t%s%s %s { // %d\n", indent,
109 clause_names [mh->clauses[i].flags], klass, i);
113 if (mh->clauses[i].flags == MONO_EXCEPTION_CLAUSE_FAULT)
116 if (mh->clauses[i].flags == 1 && ptr == start + mh->clauses[i].token_or_filter) {
117 fprintf (output, "\t%s%s {\n", indent, clause_names[1]);
121 fprintf (output, "\t%sIL_%04x: ", indent, (int) (ptr - start));
127 entry = &mono_opcodes [i];
129 if (in_fault && entry->opval == 0xDC)
130 fprintf (output, " %s", "endfault");
132 fprintf (output, " %s ", mono_opcode_names [i]);
134 switch (entry->argument){
135 case MonoInlineBrTarget: {
136 gint target = read32 (ptr);
137 fprintf (output, "IL_%04x\n", ((int) (ptr - start)) + 4 + target);
142 case MonoInlineField: {
143 guint32 token = read32 (ptr);
146 s = get_field (m, token);
147 fprintf (output, "%s", s);
154 int value = read32 (ptr);
156 fprintf (output, "%d", value);
162 gint64 top = read64 (ptr);
164 fprintf (output, "0x%llx", (long long) top);
169 case MonoInlineMethod: {
170 guint32 token = read32 (ptr);
173 s = get_method (m, token);
174 fprintf (output, "%s", s);
186 fprintf (output, "%g", r);
191 case MonoInlineSig: {
192 guint32 token = read32 (ptr);
193 fprintf (output, "signature-0x%08x", token);
198 case MonoInlineString: {
199 guint32 token = read32 (ptr);
201 char *s = get_encoded_user_string (
202 mono_metadata_user_string (m, token & 0xffffff));
205 * See section 23.1.4 on the encoding of the #US heap
207 fprintf (output, "\"%s\"", s);
213 case MonoInlineSwitch: {
214 guint32 count = read32 (ptr);
215 const unsigned char *endswitch;
219 endswitch = ptr + sizeof (guint32) * count;
220 fprintf (output, "(\n");
222 for (n = 0; n < count; n++){
223 fprintf (output, "\t%sIL_%04x%s", indent,
224 endswitch-start+read32 (ptr),
225 n == count - 1 ? ")" : ",\n");
232 case MonoInlineTok: {
233 guint32 token = read32 (ptr);
236 s = get_token (m, token);
237 fprintf (output, "%s", s);
244 case MonoInlineType: {
245 guint32 token = read32 (ptr);
246 char *s = get_token_type (m, token);
247 fprintf (output, "%s", s);
253 case MonoInlineVar: {
254 guint16 var_idx = read16 (ptr);
256 fprintf (output, "%d\n", var_idx);
261 case MonoShortInlineBrTarget: {
262 signed char x = *ptr;
264 fprintf (output, "IL_%04x\n", ptr - start + 1 + x);
269 case MonoShortInlineI: {
272 fprintf (output, "0x%02x", x);
277 case MonoShortInlineR: {
281 fprintf (output, "%g", (double) f);
286 case MonoShortInlineVar: {
287 unsigned char x = *ptr;
289 fprintf (output, "%d", (int) x);
297 fprintf (output, "\n");
298 for (i = 0; i < mh->num_clauses; ++i) {
299 if (ptr == start + mh->clauses[i].try_offset + mh->clauses[i].try_len && trys [i]) {
301 fprintf (output, "\t%s} // end .try %d\n", indent, i);
303 if (ptr == start + mh->clauses[i].handler_offset + mh->clauses[i].handler_len) {
305 fprintf (output, "\t%s} // end handler %d\n", indent, i);
306 if (mh->clauses[i].flags == MONO_EXCEPTION_CLAUSE_FAULT)