Thu Jul 26 15:31:01 CEST 2001 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / loader.c
1 /*
2  * loader.c: Image Loader 
3  *
4  * Authors:
5  *   Paolo Molaro (lupus@ximian.com)
6  *   Miguel de Icaza (miguel@ximian.com)
7  *
8  * (C) 2001 Ximian, Inc.
9  *
10  * This file is used by the interpreter and the JIT engine to locate
11  * assemblies.  Used to load AssemblyRef and later to resolve various
12  * kinds of `Refs'.
13  *
14  * TODO:
15  *   This should keep track of the assembly versions that we are loading.
16  *
17  */
18 #include <config.h>
19 #include <glib.h>
20 #include <stdio.h>
21 #include <mono/metadata/metadata.h>
22 #include <mono/metadata/image.h>
23 #include <mono/metadata/cil-coff.h>
24 #include "cli.h"
25
26 MonoMethod *
27 mono_get_method (MonoImage *image, guint32 token)
28 {
29         MonoMethod *result;
30         int table = mono_metadata_token_table (token);
31         int index = mono_metadata_token_index (token);
32         metadata_tableinfo_t *tables = image->metadata.tables;
33         const char *loc;
34         const char *sig = NULL;
35         int size;
36         guint32 cols[6];
37
38         if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
39                         return result;
40         
41         result = g_new0 (MonoMethod, 1);
42         result->image = image;
43         /*
44          * We need a context with cli_image_info_t for this module and the assemblies
45          * loaded later to support method refs...
46          */
47         if (table != META_TABLE_METHOD) {
48                 g_assert (table == META_TABLE_MEMBERREF);
49                 mono_metadata_decode_row (&tables [table], index-1, cols, 3);
50                 g_print ("methodref: 0x%x 0x%x", cols [0] & 7, cols [0] >> 3);
51                 index = cols [0] >> 3;
52                 sig = mono_metadata_blob_heap (&image->metadata, cols [2]);
53                 result->name_idx = cols [1];
54                 /* Need to finish this code ... */
55                 switch ((cols [0] & 0x07)) {
56                 case 1:
57                         mono_metadata_decode_row (&tables [META_TABLE_TYPEREF], index, cols, 3);
58                         g_assert (0);
59                         break;
60                 default:
61                         g_assert_not_reached ();
62                 }
63                 table = META_TABLE_METHOD;
64         }
65         
66         mono_metadata_decode_row (&tables [table], index - 1, cols, 6);
67         result->name_idx = cols [3];
68         /* if this is a methodref from another module/assembly, this fails */
69         loc = cli_rva_map ((cli_image_info_t *)image->image_info, cols [0]);
70         g_assert (loc);
71         result->header = mono_metadata_parse_mh (&image->metadata, loc);
72         if (!sig) /* already taken from the methodref */
73                 sig = mono_metadata_blob_heap (&image->metadata, cols [4]);
74         size = mono_metadata_decode_blob_size (sig, &sig);
75         result->signature = mono_metadata_parse_method_signature (&image->metadata, 0, sig, NULL);
76
77         g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
78
79         return result;
80 }
81
82 void
83 mono_free_method  (MonoMethod *method)
84 {
85         mono_metadata_free_method_signature (method->signature);
86         mono_metadata_free_mh (method->header);
87         g_free (method);
88 }