2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / monograph / monograph.c
index 5a81c616e6b10ce20d839979c7bf3da1ee9f168d..11f220226c193c838a00db9c566a7dacbf49038f 100644 (file)
@@ -1,6 +1,6 @@
 #include <glib.h>
 #include <string.h>
-#include "mono/metadata/class.h"
+#include "mono/metadata/class-internals.h"
 #include "mono/metadata/assembly.h"
 #include "mono/metadata/tokentype.h"
 #include "mono/metadata/opcodes.h"
@@ -32,14 +32,14 @@ print_subtypes (MonoImage *image, MonoClass *class, int depth) {
        if (depth++ > max_depth)
                return;
 
-       t = &image->tables [MONO_TABLE_TYPEDEF];
+       t = mono_image_get_table_info (image, MONO_TABLE_TYPEDEF);
        
        token = mono_metadata_token_index (class->type_token);
-       token <<= TYPEDEFORREF_BITS;
-       token |= TYPEDEFORREF_TYPEDEF;
+       token <<= MONO_TYPEDEFORREF_BITS;
+       token |= MONO_TYPEDEFORREF_TYPEDEF;
 
        /* use a subgraph? */
-       for (i = 0; i < t->rows; ++i) {
+       for (i = 0; i < mono_table_info_get_rows (t); ++i) {
                if (token == mono_metadata_decode_row_col (t, i, MONO_TYPEDEF_EXTENDS)) {
                        child = mono_class_get (image, MONO_TOKEN_TYPE_DEF | (i + 1));
                        output_type_edge (class, child);
@@ -91,7 +91,7 @@ interface_graph (MonoImage *image, const char* cname) {
        char *p;
        guint32 cols [MONO_INTERFACEIMPL_SIZE];
        guint32 token, i, count = 0;
-       MonoTableInfo *intf = &image->tables [MONO_TABLE_INTERFACEIMPL];
+       MonoTableInfo *intf = mono_image_get_table_info (image, MONO_TABLE_INTERFACEIMPL);
 
        cname = g_strdup (cname);
        p = strrchr (cname, '.');
@@ -112,9 +112,9 @@ interface_graph (MonoImage *image, const char* cname) {
        fprintf (output, "%s", graph_properties);
        /* TODO: handle inetrface defined in one image and class defined in another. */
        token = mono_metadata_token_index (class->type_token);
-       token <<= TYPEDEFORREF_BITS;
-       token |= TYPEDEFORREF_TYPEDEF;
-       for (i = 0; i < intf->rows; ++i) {
+       token <<= MONO_TYPEDEFORREF_BITS;
+       token |= MONO_TYPEDEFORREF_TYPEDEF;
+       for (i = 0; i < mono_table_info_get_rows (intf); ++i) {
                mono_metadata_decode_row (intf, i, cols, MONO_INTERFACEIMPL_SIZE);
                /*g_print ("index: %d [%d implements %d]\n", index, cols [MONO_INTERFACEIMPL_CLASS], cols [MONO_INTERFACEIMPL_INTERFACE]);*/
                if (token == cols [MONO_INTERFACEIMPL_INTERFACE]) {
@@ -165,6 +165,7 @@ static int has_switch = 0;
 static int num_switch = 0;
 static int max_switch = 0;
 static int cast_sealed = 0;
+static int cast_iface = 0;
 static int total_cast = 0;
 static int nonvirt_callvirt = 0;
 static int total_callvirt = 0;
@@ -183,7 +184,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;
@@ -235,10 +236,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;
@@ -248,6 +249,8 @@ method_stats (MonoMethod *method) {
                                MonoClass *k = mono_class_get (method->klass->image, token);
                                if (k && k->flags & TYPE_ATTRIBUTE_SEALED)
                                        cast_sealed++;
+                               if (k && k->flags & TYPE_ATTRIBUTE_INTERFACE)
+                                       cast_iface++;
                                total_cast++;
                        }
                        ip += 5;
@@ -277,16 +280,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;
@@ -298,7 +301,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;
@@ -308,7 +311,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;
@@ -399,32 +402,69 @@ method_stats (MonoMethod *method) {
        return;
 }
 
+static int num_pdepth = 0;
+static int max_pdepth = 0;
+static int num_ifaces = 0;
+
+static void
+type_stats (MonoClass *klass) {
+       MonoClass *parent;
+       int depth = 0;
+
+       if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
+               num_ifaces++;
+               return;
+       }
+       parent = klass->parent;
+       while (parent) {
+               depth++;
+               parent = parent->parent;
+       }
+       num_pdepth += depth;
+       if (max_pdepth < depth)
+               max_pdepth = depth;
+}
+
 static void
 stats (MonoImage *image, const char *name) {
-       int i;
+       int i, num_methods, num_types;
        MonoMethod *method;
+       MonoClass *klass;
        
-       for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
+       num_methods = mono_image_get_table_rows (image, MONO_TABLE_METHOD);
+       for (i = 0; i < num_methods; ++i) {
                method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
                method_stats (method);
        }
+       num_types = mono_image_get_table_rows (image, MONO_TABLE_TYPEDEF);
+       for (i = 0; i < num_types; ++i) {
+               klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | (i + 1));
+               type_stats (klass);
+       }
+
+       g_print ("Methods and code stats:\n");
        g_print ("back branch waste: %d\n", back_branch_waste);
        g_print ("branch waste: %d\n", branch_waste);
        g_print ("var waste: %d\n", var_waste);
        g_print ("int waste: %d\n", int_waste);
        g_print ("nop waste: %d\n", nop_waste);
-       g_print ("has exceptions: %d/%d, total: %d, max: %d, mean: %f\n", has_exceptions, i, num_exceptions, max_exceptions, num_exceptions/(double)has_exceptions);
-       g_print ("has locals: %d/%d, total: %d, max: %d, mean: %f\n", has_locals, i, num_locals, max_locals, num_locals/(double)has_locals);
-       g_print ("has args: %d/%d, total: %d, max: %d, mean: %f\n", has_args, i, num_args, max_args, num_args/(double)has_args);
-       g_print ("has maxstack: %d/%d, total: %d, max: %d, mean: %f\n", has_maxstack, i, num_maxstack, max_maxstack, num_maxstack/(double)i);
-       g_print ("has code: %d/%d, total: %d, max: %d, mean: %f\n", has_code, i, num_code, max_code, num_code/(double)has_code);
-       g_print ("has branch: %d/%d, total: %d, max: %d, mean: %f\n", has_branch, i, num_branch, max_branch, num_branch/(double)has_branch);
-       g_print ("has condbranch: %d/%d, total: %d, max: %d, mean: %f\n", has_condbranch, i, num_condbranch, max_condbranch, num_condbranch/(double)has_condbranch);
-       g_print ("has switch: %d/%d, total: %d, max: %d, mean: %f\n", has_switch, i, num_switch, max_switch, num_switch/(double)has_switch);
-       g_print ("has calls: %d/%d, total: %d, max: %d, mean: %f\n", has_calls, i, num_calls, max_calls, num_calls/(double)has_calls);
-       g_print ("has throw: %d/%d, total: %d, max: %d, mean: %f\n", has_throw, i, num_throw, max_throw, num_throw/(double)has_throw);
+       g_print ("has exceptions: %d/%d, total: %d, max: %d, mean: %f\n", has_exceptions, num_methods, num_exceptions, max_exceptions, num_exceptions/(double)has_exceptions);
+       g_print ("has locals: %d/%d, total: %d, max: %d, mean: %f\n", has_locals, num_methods, num_locals, max_locals, num_locals/(double)has_locals);
+       g_print ("has args: %d/%d, total: %d, max: %d, mean: %f\n", has_args, num_methods, num_args, max_args, num_args/(double)has_args);
+       g_print ("has maxstack: %d/%d, total: %d, max: %d, mean: %f\n", has_maxstack, num_methods, num_maxstack, max_maxstack, num_maxstack/(double)i);
+       g_print ("has code: %d/%d, total: %d, max: %d, mean: %f\n", has_code, num_methods, num_code, max_code, num_code/(double)has_code);
+       g_print ("has branch: %d/%d, total: %d, max: %d, mean: %f\n", has_branch, num_methods, num_branch, max_branch, num_branch/(double)has_branch);
+       g_print ("has condbranch: %d/%d, total: %d, max: %d, mean: %f\n", has_condbranch, num_methods, num_condbranch, max_condbranch, num_condbranch/(double)has_condbranch);
+       g_print ("has switch: %d/%d, total: %d, max: %d, mean: %f\n", has_switch, num_methods, num_switch, max_switch, num_switch/(double)has_switch);
+       g_print ("has calls: %d/%d, total: %d, max: %d, mean: %f\n", has_calls, num_methods, num_calls, max_calls, num_calls/(double)has_calls);
+       g_print ("has throw: %d/%d, total: %d, max: %d, mean: %f\n", has_throw, num_methods, num_throw, max_throw, num_throw/(double)has_throw);
        g_print ("sealed type cast: %d/%d\n", cast_sealed, total_cast);
+       g_print ("interface type cast: %d/%d\n", cast_iface, total_cast);
        g_print ("non virtual callvirt: %d/%d\n", nonvirt_callvirt, total_callvirt);
+       
+       g_print ("\nType stats:\n");
+       g_print ("interface types: %d/%d\n", num_ifaces, num_types);
+       g_print ("parent depth: max: %d, mean: %d\n", max_pdepth, num_pdepth/(num_types - num_ifaces));
 }
 
 static char *
@@ -488,7 +528,7 @@ print_method (MonoMethod *method, int depth) {
        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);
@@ -568,7 +608,7 @@ method_graph (MonoImage *image, const char *name) {
        if (!name) {
                guint32 token = mono_image_get_entry_point (image);
                if (!token || !(method = mono_get_method (image, token, NULL))) {
-                       g_print ("Cannot find entry point in %s: specify an explict method name.\n", image->name);
+                       g_print ("Cannot find entry point in %s: specify an explict method name.\n", mono_image_get_filename (image));
                        exit (1);
                }
        } else {
@@ -615,10 +655,10 @@ link_bblock (MonoBasicBlock *from, MonoBasicBlock* to)
 }
 
 static int
-compare_bblock (void *a, void *b)
+compare_bblock (const void *a, const void *b)
 {
-       MonoBasicBlock **ab = a;
-       MonoBasicBlock **bb = b;
+       MonoBasicBlock * const *ab = a;
+       MonoBasicBlock * const *bb = b;
 
        return (*ab)->cil_code - (*bb)->cil_code;
 }
@@ -755,7 +795,7 @@ mono_method_find_bblocks (MonoMethodHeader *header)
                                        target = g_new0 (MonoBasicBlock, 1);
                                        target->cil_code = itarget;
                                        g_ptr_array_add (result, target);
-                                       g_hash_table_insert (table, itarget, target);
+                                       g_hash_table_insert (table, (gpointer) itarget, target);
                                }
                                link_bblock (bb, target);
                        }
@@ -834,7 +874,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);
@@ -879,7 +919,7 @@ method_cfg (MonoImage *image, const char *name) {
        if (!name) {
                guint32 token = mono_image_get_entry_point (image);
                if (!token || !(method = mono_get_method (image, token, NULL))) {
-                       g_print ("Cannot find entry point in %s: specify an explict method name.\n", image->name);
+                       g_print ("Cannot find entry point in %s: specify an explict method name.\n", mono_image_get_filename (image));
                        exit (1);
                }
        } else {
@@ -909,7 +949,7 @@ static void
 usage (void) {
        printf ("monograph 0.2 Copyright (c) 2002 Ximian, Inc\n");
        printf ("Create call graph or type hierarchy information from CIL assemblies.\n");
-       printf ("Usage: monograph [options] [assembly [typename|methodname]]\n");
+       printf ("Usage: monograph [options] [assembly [typename|methodname]]\n\n");
        printf ("Valid options are:\n");
        printf ("\t-c|--call             output call graph instead of type hierarchy\n");
        printf ("\t-C|--control-flow     output control flow of methodname\n");
@@ -918,14 +958,16 @@ usage (void) {
        printf ("\t-o|--output filename  write graph to file filename (default: stdout)\n");
        printf ("\t-f|--fullname         include namespace in type and method names\n");
        printf ("\t-n|--neato            invoke neato directly\n");
-       printf ("\t-v|--verbose          verbose operation\n");
-       printf ("The default assembly is corlib.dll. The default method for\n");
-       printf ("the --call and --control-flow options is the entrypoint.\n");
+       printf ("\t-v|--verbose          verbose operation\n\n");
+       printf ("The default assembly is mscorlib.dll. The default method for\n");
+       printf ("the --call and --control-flow options is the entrypoint.\n\n");
        printf ("When the --neato option is used the output type info is taken\n");
-       printf ("from the output filename extension. You need the graphviz package installed\n");
-       printf ("to be able to use this option.\n");
+       printf ("from the output filename extension. You need the graphviz package\n");
+       printf ("installed to be able to use this option and build bitmap files.\n");
+       printf ("Without --neato, monograph will create .dot files, a description\n");
+       printf ("file for a graph.\n\n");
        printf ("Sample runs:\n");
-       printf ("\tmonograph -n -o vt.png corlib.dll System.ValueType\n");
+       printf ("\tmonograph -n -o vt.png mscorlib.dll System.ValueType\n");
        printf ("\tmonograph -n -o expr.png mcs.exe Mono.CSharp.Expression\n");
        printf ("\tmonograph -n -o cfg.png -C mcs.exe Driver:Main\n");
        printf ("\tmonograph -d 3 -n -o callgraph.png -c mis.exe\n");
@@ -957,6 +999,7 @@ enum {
 int
 main (int argc, char *argv[]) {
        MonoAssembly *assembly;
+       MonoImage *image;
        const char *cname = NULL;
        const char *aname = NULL;
        char *outputfile = NULL;
@@ -1002,7 +1045,7 @@ main (int argc, char *argv[]) {
        if (argc > i + 1)
                cname = argv [i + 1];
        if (!aname)
-               aname = "corlib.dll";
+               aname = "mscorlib";
        if (!cname && (graphtype == GRAPH_TYPES))
                cname = "System.Object";
 
@@ -1038,21 +1081,22 @@ main (int argc, char *argv[]) {
        if (cname && strchr (cname, ':') && graphtype == GRAPH_TYPES)
                graphtype = GRAPH_CALL;
 
+       image = mono_assembly_get_image (assembly);
        switch (graphtype) {
        case GRAPH_TYPES:
-               type_graph (assembly->image, cname);
+               type_graph (image, cname);
                break;
        case GRAPH_CALL:
-               method_graph (assembly->image, cname);
+               method_graph (image, cname);
                break;
        case GRAPH_INTERFACE:
-               interface_graph (assembly->image, cname);
+               interface_graph (image, cname);
                break;
        case GRAPH_CONTROL_FLOW:
-               method_cfg (assembly->image, cname);
+               method_cfg (image, cname);
                break;
        case GRAPH_STATS:
-               stats (assembly->image, cname);
+               stats (image, cname);
                break;
        default:
                g_error ("wrong graph type");