2003-01-25 Martin Baulig <martin@ximian.com>
[mono.git] / mono / dis / dis-cil.c
index bfc2bdd661e3c23df2c0cb3e8bc0cf54e66197c8..a90eda186c46e83dcc7d38113ed28058693cec39 100644 (file)
@@ -9,49 +9,14 @@
 #include <config.h>
 #include <glib.h>
 #include <stdio.h>
+#ifdef HAVE_WCHAR_H
 #include <wchar.h>
+#endif
 #include "meta.h"
 #include "get.h"
 #include "dump.h"
 #include "dis-cil.h"
-
-enum {
-       InlineBrTarget,
-       InlineField,
-       InlineI,
-       InlineI8,
-       InlineMethod,
-       InlineNone,
-       InlineR,
-       InlineSig,
-       InlineString,
-       InlineSwitch,
-       InlineTok,
-       InlineType,
-       InlineVar,
-       ShortInlineBrTarget,
-       ShortInlineI,
-       ShortInlineR,
-       ShortInlineVar
-};
-
-#define OPDEF(a,b,c,d,e,f,g,h,i,j) \
-       { b, e, g, h, i },
-
-typedef struct {
-       char *name;
-       int   argument;
-
-       /*
-        * we are not really using any of the following:
-        */
-       int   bytes;
-       unsigned char  o1, o2;
-} opcode_t;
-
-static opcode_t opcodes [300] = {
-#include "mono/cil/opcode.def"
-};
+#include "mono/metadata/opcodes.h"
 
 /*
  * Strings on the US heap are encoded using UTF-16.  Poor man's
@@ -87,22 +52,30 @@ get_encoded_user_string (const char *ptr)
        indent[indent_level*2] = 0;
 
 void
-dissasemble_cil (MonoMetadata *m, MonoMethodHeader *mh) 
+dissasemble_cil (MonoImage *m, MonoMethodHeader *mh) 
 {
        const unsigned char *start = mh->code;
        int size = mh->code_size;
        const unsigned char *end = start + size;
        const unsigned char *ptr = start;
-       opcode_t *entry;
+       const MonoOpcode *entry;
        char indent[1024];
        int i, indent_level = 0;
-       char *clause_names[] = {"catch", "filter", "finally", "fault"};
+       const char *clause_names[] = {"catch", "filter", "finally", "fault"};
 
        indent [0] = 0;
 
+#ifdef DEBUG
+       for (i = 0; i < mh->num_clauses; ++i) {
+#define clause mh->clauses [i]
+               g_print ("out clause %d: from %d len=%d, handler at %d, %d\n", 
+                       clause.flags, clause.try_offset, clause.try_len, clause.handler_offset, clause.handler_len);
+#undef clause
+       }
+#endif
        while (ptr < end){
-               for (i = 0; i < mh->num_clauses; ++i) {
-                       if (!mh->clauses[i].flags && ptr == start + mh->clauses[i].try_offset) {
+               for (i = mh->num_clauses - 1; i >= 0 ; --i) {
+                       if ((mh->clauses[i].flags == 0 || mh->clauses[i].flags == 2) && ptr == start + mh->clauses[i].try_offset) {
                                fprintf (output, "\t%s.try { // %d\n", indent, i);
                                CODE_INDENT;
                        }
@@ -114,23 +87,24 @@ dissasemble_cil (MonoMetadata *m, MonoMethodHeader *mh)
                        }
                }
                fprintf (output, "\t%sIL_%04x: ", indent, (int) (ptr - start));
+               i = *ptr;
                if (*ptr == 0xfe){
                        ptr++;
-                       entry = &opcodes [*ptr + 256];
-               } else 
-                       entry = &opcodes [*ptr];
+                       i = *ptr + 256;
+               } 
+               entry = &mono_opcodes [i];
 
-               fprintf (output, "%s ", entry->name);
+               fprintf (output, "%s ", mono_opcode_names [i]);
                ptr++;
                switch (entry->argument){
-               case InlineBrTarget: {
+               case MonoInlineBrTarget: {
                        gint target = read32 (ptr);
                        fprintf (output, "IL_%04x\n", ((int) (ptr - start)) + 4 + target);
                        ptr += 4;
                        break;
                }
                        
-               case InlineField: {
+               case MonoInlineField: {
                        guint32 token = read32 (ptr);
                        char *s;
                        
@@ -141,7 +115,7 @@ dissasemble_cil (MonoMetadata *m, MonoMethodHeader *mh)
                        break;
                }
                
-               case InlineI: {
+               case MonoInlineI: {
                        int value = read32 (ptr);
 
                        fprintf (output, "%d", value);
@@ -149,15 +123,15 @@ dissasemble_cil (MonoMetadata *m, MonoMethodHeader *mh)
                        break;
                }
                
-               case InlineI8: {
+               case MonoInlineI8: {
                        gint64 top = read64 (ptr);
 
-                       fprintf (output, "%lld", (long long) top);
+                       fprintf (output, "0x%llx", (long long) top);
                        ptr += 8;
                        break;
                }
                
-               case InlineMethod: {
+               case MonoInlineMethod: {
                        guint32 token = read32 (ptr);
                        char *s;
 
@@ -168,24 +142,25 @@ dissasemble_cil (MonoMetadata *m, MonoMethodHeader *mh)
                        break;
                }
                
-               case InlineNone:
+               case MonoInlineNone:
                        break;
                        
-               case InlineR: {
-                       double r = *(double *) ptr;
+               case MonoInlineR: {
+                       double r;
+                       readr8 (ptr, &r);
                        fprintf (output, "%g", r);
                        ptr += 8;
                        break;
                }
                
-               case InlineSig: {
+               case MonoInlineSig: {
                        guint32 token = read32 (ptr);
                        fprintf (output, "signature-0x%08x", token);
                        ptr += 4;
                        break;
                }
                
-               case InlineString: {
+               case MonoInlineString: {
                        guint32 token = read32 (ptr);
                        
                        char *s = get_encoded_user_string (
@@ -200,26 +175,26 @@ dissasemble_cil (MonoMetadata *m, MonoMethodHeader *mh)
                        break;
                }
 
-               case InlineSwitch: {
+               case MonoInlineSwitch: {
                        guint32 count = read32 (ptr);
                        const unsigned char *endswitch;
-                       guint32 i;
+                       guint32 n;
                        
                        ptr += 4;
                        endswitch = ptr + sizeof (guint32) * count;
                        fprintf (output, "(\n");
                        CODE_INDENT;
-                       for (i = 0; i < count; i++){
+                       for (n = 0; n < count; n++){
                                fprintf (output, "\t%sIL_%04x%s", indent, 
                                        endswitch-start+read32 (ptr), 
-                                       i == count - 1 ? ")" : ",\n");
+                                       n == count - 1 ? ")" : ",\n");
                                ptr += 4;
                        }
                        CODE_UNINDENT;
                        break;
                }
 
-               case InlineTok: {
+               case MonoInlineTok: {
                        guint32 token = read32 (ptr);
                        char *s;
                        
@@ -231,7 +206,7 @@ dissasemble_cil (MonoMetadata *m, MonoMethodHeader *mh)
                        break;
                }
                
-               case InlineType: {
+               case MonoInlineType: {
                        guint32 token = read32 (ptr);
                        char *s = get_token_type (m, token);
                        fprintf (output, "%s", s);
@@ -240,15 +215,15 @@ dissasemble_cil (MonoMetadata *m, MonoMethodHeader *mh)
                        break;
                }
 
-               case InlineVar: {
-                       gint16 var_idx = read16 (ptr);
+               case MonoInlineVar: {
+                       guint16 var_idx = read16 (ptr);
 
-                       fprintf (output, "variable-%d\n", var_idx);
+                       fprintf (output, "%d\n", var_idx);
                        ptr += 2;
                        break;
                }
 
-               case ShortInlineBrTarget: {
+               case MonoShortInlineBrTarget: {
                        signed char x = *ptr;
                        
                        fprintf (output, "IL_%04x\n", ptr - start + 1 + x);
@@ -256,7 +231,7 @@ dissasemble_cil (MonoMetadata *m, MonoMethodHeader *mh)
                        break;
                }
 
-               case ShortInlineI: {
+               case MonoShortInlineI: {
                        char x = *ptr;
 
                        fprintf (output, "0x%02x", x);
@@ -264,18 +239,19 @@ dissasemble_cil (MonoMetadata *m, MonoMethodHeader *mh)
                        break;
                }
 
-               case ShortInlineR: {
-                       float f = *(float *) ptr;
+               case MonoShortInlineR: {
+                       float f;
+                       readr4 (ptr, &f);
 
                        fprintf (output, "%g", (double) f);
                        ptr += 4;
                        break;
                }
 
-               case ShortInlineVar: {
-                       signed char x = *ptr;
+               case MonoShortInlineVar: {
+                       unsigned char x = *ptr;
 
-                       fprintf (output, "V_%d", (int) x);
+                       fprintf (output, "%d", (int) x);
                        ptr++;
                        break;
                }
@@ -285,7 +261,7 @@ dissasemble_cil (MonoMetadata *m, MonoMethodHeader *mh)
 
                fprintf (output, "\n");
                for (i = 0; i < mh->num_clauses; ++i) {
-                       if (!mh->clauses[i].flags && ptr == start + mh->clauses[i].try_offset + mh->clauses[i].try_len) {
+                       if ((mh->clauses[i].flags == 0 || mh->clauses[i].flags == 2)  && ptr == start + mh->clauses[i].try_offset + mh->clauses[i].try_len) {
                                CODE_UNINDENT;
                                fprintf (output, "\t%s} // end .try %d\n", indent, i);
                        }