Thu Nov 23 20:01:12 CET 2006 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / monograph / monograph.c
index 7fb66c17e04372ed6c9a244c401d79fbab51d77b..6c30820b8f9d0c3e1dd1f2bf19c8e88aee90c16a 100644 (file)
@@ -1,5 +1,6 @@
 #include <glib.h>
 #include <string.h>
+#include <math.h>
 #include "mono/metadata/class-internals.h"
 #include "mono/metadata/assembly.h"
 #include "mono/metadata/tokentype.h"
@@ -169,13 +170,15 @@ static int cast_sealed = 0;
 static int cast_iface = 0;
 static int total_cast = 0;
 static int nonvirt_callvirt = 0;
+static int iface_callvirt = 0;
 static int total_callvirt = 0;
 
 static void
 method_stats (MonoMethod *method) {
        const MonoOpcode *opcode;
        MonoMethodHeader *header;
-       const unsigned char *ip;
+       MonoMethodSignature *sig;
+       const unsigned char *ip, *il_code_end;
        int i, n;
        int local_branch = 0, local_condbranch = 0, local_throw = 0, local_calls = 0;
        gint64 l;
@@ -186,24 +189,29 @@ method_stats (MonoMethod *method) {
                return;
 
        header = mono_method_get_header (method);
-       if (header->num_clauses)
+       n = mono_method_header_get_num_clauses (header);
+       if (n)
                has_exceptions++;
-       num_exceptions += header->num_clauses;
-       if (max_exceptions < header->num_clauses)
-               max_exceptions = header->num_clauses;
-       if (header->num_locals)
+       num_exceptions += n;
+       if (max_exceptions < n)
+               max_exceptions = n;
+       mono_method_header_get_locals (header, &n, NULL);
+       if (n)
                has_locals++;
-       num_locals += header->num_locals;
-       if (max_locals < header->num_locals)
-               max_locals = header->num_locals;
-
-       if (max_maxstack < header->max_stack)
-               max_maxstack = header->max_stack;
-       num_maxstack += header->max_stack;
-       if (header->max_stack != 8) /* just a guess */
+       num_locals += n;
+       if (max_locals < n)
+               max_locals = n;
+
+       ip = mono_method_header_get_code (header, &n, &i);
+       il_code_end = ip + n;
+       if (max_maxstack < i)
+               max_maxstack = i;
+       num_maxstack += i;
+       if (i != 8) /* just a guess */
                has_maxstack++;
 
-       n = method->signature->hasthis + method->signature->param_count;
+       sig = mono_method_signature (method);
+       n = sig->hasthis + sig->param_count;
        if (max_args < n)
                max_args = n;
        num_args += n;
@@ -211,13 +219,11 @@ method_stats (MonoMethod *method) {
                has_args++;
 
        has_code++;
-       if (max_code < header->code_size)
-               max_code = header->code_size;
-       num_code += header->code_size;
-
-       ip = header->code;
+       if (max_code < il_code_end - ip)
+               max_code = il_code_end - ip;
+       num_code += il_code_end - ip;
 
-       while (ip < (header->code + header->code_size)) {
+       while (ip < il_code_end) {
                if (*ip == 0xfe) {
                        ++ip;
                        i = *ip + 256;
@@ -281,16 +287,16 @@ method_stats (MonoMethod *method) {
                                        case MONO_CEE_LDLOC:
                                        case MONO_CEE_STLOC:
                                                var_waste += 3;
-                                               g_print ("%s %d\n", mono_opcode_name (i), n);
+                                               /*g_print ("%s %d\n", mono_opcode_name (i), n);*/
                                                break;
                                        default:
                                                var_waste += 2;
-                                               g_print ("%s %d\n", mono_opcode_name (i), n);
+                                               /*g_print ("%s %d\n", mono_opcode_name (i), n);*/
                                                break;
                                        }
                                } else {
                                        var_waste += 2;
-                                       g_print ("%s %d\n", mono_opcode_name (i), n);
+                                       /*g_print ("%s %d\n", mono_opcode_name (i), n);*/
                                }
                        }
                        ip += 3;
@@ -302,7 +308,7 @@ method_stats (MonoMethod *method) {
                                case MONO_CEE_LDLOC_S:
                                case MONO_CEE_STLOC_S:
                                        var_waste++;
-                                       g_print ("%s %d\n", mono_opcode_name (i), (signed char)ip [1]);
+                                       /*g_print ("%s %d\n", mono_opcode_name (i), (signed char)ip [1]);*/
                                        break;
                                default:
                                        break;
@@ -312,7 +318,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_name (i), (signed char)ip [1]);
+                               /*g_print ("%s %d\n", mono_opcode_name (i), (signed char)ip [1]);*/
                                int_waste ++;
                        }
                        ip += 2;
@@ -352,6 +358,8 @@ method_stats (MonoMethod *method) {
                                MonoMethod *cm = mono_get_method (method->klass->image, read32 (ip + 1), NULL);
                                if (cm && !(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
                                        nonvirt_callvirt++;
+                               if (cm && (cm->klass->flags & TYPE_ATTRIBUTE_INTERFACE))
+                                       iface_callvirt++;
                                total_callvirt++;
                        }
                        ip += 5;
@@ -405,12 +413,16 @@ method_stats (MonoMethod *method) {
 
 static int num_pdepth = 0;
 static int max_pdepth = 0;
+static int num_pdepth_ovf = 0;
 static int num_ifaces = 0;
+static int *pdepth_array = NULL;
+static int pdepth_array_size = 0;
+static int pdepth_array_next = 0;
 
 static void
 type_stats (MonoClass *klass) {
        MonoClass *parent;
-       int depth = 0;
+       int depth = 1;
 
        if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
                num_ifaces++;
@@ -421,9 +433,20 @@ type_stats (MonoClass *klass) {
                depth++;
                parent = parent->parent;
        }
+       if (pdepth_array_next >= pdepth_array_size) {
+               pdepth_array_size *= 2;
+               if (!pdepth_array_size)
+                       pdepth_array_size = 128;
+               pdepth_array = g_realloc (pdepth_array, pdepth_array_size * sizeof (int));
+       }
+       pdepth_array [pdepth_array_next++] = depth;
        num_pdepth += depth;
        if (max_pdepth < depth)
                max_pdepth = depth;
+       if (depth > MONO_DEFAULT_SUPERTABLE_SIZE) {
+               /*g_print ("overflow parent depth: %s.%s\n", klass->name_space, klass->name);*/
+               num_pdepth_ovf++;
+       }
 }
 
 static void
@@ -462,10 +485,23 @@ stats (MonoImage *image, const char *name) {
        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 ("interface callvirt: %d/%d\n", iface_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));
+       {
+               double mean = 0;
+               double stddev = 0;
+               if (pdepth_array_next) {
+                       int i;
+                       mean = (double)num_pdepth/pdepth_array_next;
+                       for (i = 0; i < pdepth_array_next; ++i) {
+                               stddev += (pdepth_array [i] - mean) * (pdepth_array [i] - mean);
+                       }
+                       stddev = sqrt (stddev/pdepth_array_next);
+               }
+               g_print ("parent depth: max: %d, mean: %f, sttdev: %f, overflowing: %d\n", max_pdepth, mean, stddev, num_pdepth_ovf);
+       }
 }
 
 static char *
@@ -482,7 +518,7 @@ get_signature (MonoMethod *method) {
        res = g_string_new ("");
        if (include_namespace && *(method->klass->name_space))
                g_string_sprintfa (res, "%s.", method->klass->name_space);
-       result = mono_signature_get_desc (method->signature, include_namespace);
+       result = mono_signature_get_desc (mono_method_signature (method), include_namespace);
        g_string_sprintfa (res, "%s:%s(%s)", method->klass->name, method->name, result);
        g_free (result);
        g_hash_table_insert (hash, method, res->str);
@@ -516,7 +552,7 @@ print_method (MonoMethod *method, int depth) {
        MonoMethodHeader *header;
        GHashTable *hash;
        static GHashTable *visited = NULL;
-       const unsigned char *ip;
+       const unsigned char *ip, *il_code_end;
        int i;
 
        if (depth++ > max_depth)
@@ -536,11 +572,12 @@ print_method (MonoMethod *method, int depth) {
                return;
 
        header = mono_method_get_header (method);
-       ip = header->code;
+       ip = mono_method_header_get_code (header, &i, NULL);
+       il_code_end = ip + i;
 
        hash = g_hash_table_new (g_direct_hash, g_direct_equal);
        
-       while (ip < (header->code + header->code_size)) {
+       while (ip < il_code_end) {
                if (*ip == 0xfe) {
                        ++ip;
                        i = *ip + 256;
@@ -680,8 +717,8 @@ mono_method_find_bblocks (MonoMethodHeader *header)
        GHashTable *table = g_hash_table_new (g_direct_hash, g_direct_equal);
        MonoBasicBlock *entry_bb, *end_bb, *bb, *target;
 
-       ip = header->code;
-       end = ip + header->code_size;
+       ip = mono_method_header_get_code (header, &i, NULL);
+       end = ip + i;
        debug_start = ip;
 
        entry_bb = g_new0 (MonoBasicBlock, 1);
@@ -831,7 +868,7 @@ mono_method_find_bblocks (MonoMethodHeader *header)
                target = bb;
                /*fprintf (stderr, "bblock %d at IL_%04x:\n", i, bb->cil_code - header->code);*/
        }
-       bb->cil_length = header->code + header->code_size - bb->cil_code;
+       bb->cil_length = end - bb->cil_code;
        return result;
 }
 
@@ -880,8 +917,10 @@ print_method_cfg (MonoMethod *method) {
        MonoMethodHeader *header;
        int i, dfn;
        char *code;
+       const unsigned char *il_code;
 
        header = mono_method_get_header (method);
+       il_code = mono_method_header_get_code (header, NULL, NULL);
        bblocks = mono_method_find_bblocks (header);
        for (i = 0; i < bblocks->len; ++i) {
                bb = (MonoBasicBlock*)g_ptr_array_index (bblocks, i);
@@ -891,7 +930,7 @@ print_method_cfg (MonoMethod *method) {
                        fprintf (output, "\tB%p [shape=record,label=\"end\"]\n", bb);
                else {
                        code = mono_disasm_code (&graph_dh, method, bb->cil_code, bb->cil_code + bb->cil_length);
-                       fprintf (output, "\tB%p [shape=record,label=\"IL_%04x\\n%s\"]\n", bb, bb->cil_code - header->code, code);
+                       fprintf (output, "\tB%p [shape=record,label=\"IL_%04x\\n%s\"]\n", bb, bb->cil_code - il_code, code);
                        g_free (code);
                }
        }
@@ -910,7 +949,7 @@ print_method_cfg (MonoMethod *method) {
        dfn = 0;
        for (i = 0; i < bblocks->len; ++i) {
                bb = (MonoBasicBlock*)g_ptr_array_index (bblocks, i);
-               df_visit (bb, &dfn, header->code);
+               df_visit (bb, &dfn, il_code);
        }
 #endif
 }
@@ -1014,7 +1053,6 @@ main (int argc, char *argv[]) {
        int callneato = 0;
        int i;
        
-       mono_init (argv [0]);
        output = stdout;
 
        for (i = 1; i < argc; ++i) {
@@ -1052,8 +1090,10 @@ main (int argc, char *argv[]) {
        if (argc > i + 1)
                cname = argv [i + 1];
        if (aname) {
+               mono_init_from_assembly (argv [0], aname);
                assembly = mono_assembly_open (aname, NULL);
        } else {
+               mono_init (argv [0]);
                assembly = mono_image_get_assembly (mono_get_corlib ());
        }
        if (!cname && (graphtype == GRAPH_TYPES))