2005-11-14 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mono / monograph / monograph.c
index 72103b0b9af9347499a7a0d5dbd1be260caa9494..7fb66c17e04372ed6c9a244c401d79fbab51d77b 100644 (file)
@@ -8,6 +8,7 @@
 #include "mono/metadata/mono-endian.h"
 #include "mono/metadata/appdomain.h" /* mono_init */
 #include "mono/metadata/debug-helpers.h"
+#include "mono/utils/mono-compiler.h"
 
 static FILE *output;
 static int include_namespace = 0;
@@ -26,7 +27,7 @@ output_type_edge (MonoClass *first, MonoClass *second) {
 static void
 print_subtypes (MonoImage *image, MonoClass *class, int depth) {
        int i, token;
-       MonoTableInfo *t;
+       const MonoTableInfo *t;
        MonoClass *child;
 
        if (depth++ > max_depth)
@@ -91,7 +92,7 @@ interface_graph (MonoImage *image, const char* cname) {
        char *p;
        guint32 cols [MONO_INTERFACEIMPL_SIZE];
        guint32 token, i, count = 0;
-       MonoTableInfo *intf = mono_image_get_table_info (image, MONO_TABLE_INTERFACEIMPL);
+       const MonoTableInfo *intf = mono_image_get_table_info (image, MONO_TABLE_INTERFACEIMPL);
 
        cname = g_strdup (cname);
        p = strrchr (cname, '.');
@@ -184,7 +185,7 @@ method_stats (MonoMethod *method) {
        if (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))
                return;
 
-       header = ((MonoMethodNormal *)method)->header;
+       header = mono_method_get_header (method);
        if (header->num_clauses)
                has_exceptions++;
        num_exceptions += header->num_clauses;
@@ -236,10 +237,10 @@ method_stats (MonoMethod *method) {
                        n = read32 (ip + 1);
                        if (n >= -1 && n <= 8) {
                                int_waste += 4;
-                               g_print ("%s %d\n", mono_opcode_names [i], n);
+                               g_print ("%s %d\n", mono_opcode_name (i), n);
                        } else if (n < 128 && n >= -128) {
                                int_waste += 3;
-                               g_print ("%s %d\n", mono_opcode_names [i], n);
+                               g_print ("%s %d\n", mono_opcode_name (i), n);
                        }
                        ip += 5;
                        break;
@@ -280,16 +281,16 @@ method_stats (MonoMethod *method) {
                                        case MONO_CEE_LDLOC:
                                        case MONO_CEE_STLOC:
                                                var_waste += 3;
-                                               g_print ("%s %d\n", mono_opcode_names [i], n);
+                                               g_print ("%s %d\n", mono_opcode_name (i), n);
                                                break;
                                        default:
                                                var_waste += 2;
-                                               g_print ("%s %d\n", mono_opcode_names [i], n);
+                                               g_print ("%s %d\n", mono_opcode_name (i), n);
                                                break;
                                        }
                                } else {
                                        var_waste += 2;
-                                       g_print ("%s %d\n", mono_opcode_names [i], n);
+                                       g_print ("%s %d\n", mono_opcode_name (i), n);
                                }
                        }
                        ip += 3;
@@ -301,7 +302,7 @@ method_stats (MonoMethod *method) {
                                case MONO_CEE_LDLOC_S:
                                case MONO_CEE_STLOC_S:
                                        var_waste++;
-                                       g_print ("%s %d\n", mono_opcode_names [i], (signed char)ip [1]);
+                                       g_print ("%s %d\n", mono_opcode_name (i), (signed char)ip [1]);
                                        break;
                                default:
                                        break;
@@ -311,7 +312,7 @@ method_stats (MonoMethod *method) {
                        break;
                case MonoShortInlineI:
                        if ((signed char)ip [1] <= 8 && (signed char)ip [1] >= -1) {
-                               g_print ("%s %d\n", mono_opcode_names [i], (signed char)ip [1]);
+                               g_print ("%s %d\n", mono_opcode_name (i), (signed char)ip [1]);
                                int_waste ++;
                        }
                        ip += 2;
@@ -514,21 +515,27 @@ print_method (MonoMethod *method, int depth) {
        const MonoOpcode *opcode;
        MonoMethodHeader *header;
        GHashTable *hash;
+       static GHashTable *visited = NULL;
        const unsigned char *ip;
        int i;
 
        if (depth++ > max_depth)
                return;
-       if (method->info) /* avoid recursion */
+       
+       if (! visited)
+               visited = g_hash_table_new (NULL, NULL);
+       
+       if (g_hash_table_lookup (visited, method))
                return;
-       method->info = method;
+       
+       g_hash_table_insert (visited, method, method);
 
        if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
                return;
        if (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))
                return;
 
-       header = ((MonoMethodNormal *)method)->header;
+       header = mono_method_get_header (method);
        ip = header->code;
 
        hash = g_hash_table_new (g_direct_hash, g_direct_equal);
@@ -874,7 +881,7 @@ print_method_cfg (MonoMethod *method) {
        int i, dfn;
        char *code;
 
-       header = ((MonoMethodNormal*)method)->header;
+       header = mono_method_get_header (method);
        bblocks = mono_method_find_bblocks (header);
        for (i = 0; i < bblocks->len; ++i) {
                bb = (MonoBasicBlock*)g_ptr_array_index (bblocks, i);
@@ -1044,12 +1051,14 @@ main (int argc, char *argv[]) {
                aname = argv [i];
        if (argc > i + 1)
                cname = argv [i + 1];
-       if (!aname)
-               aname = "mscorlib";
+       if (aname) {
+               assembly = mono_assembly_open (aname, NULL);
+       } else {
+               assembly = mono_image_get_assembly (mono_get_corlib ());
+       }
        if (!cname && (graphtype == GRAPH_TYPES))
                cname = "System.Object";
 
-       assembly = mono_assembly_open (aname, NULL);
        if (!assembly) {
                g_print ("cannot open assembly %s\n", aname);
                exit (1);