2002-09-03 Martin Baulig <martin@gnome.org>
[mono.git] / mono / jit / mono.c
1 /*
2  * mono.c: Main driver for the Mono JIT engine
3  *
4  * Author:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *
7  * (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
8  */
9 #include "jit.h"
10 #include "regset.h"
11 #include "codegen.h"
12 #include "debug.h"
13 #include "mono/metadata/debug-helpers.h"
14 #include "mono/metadata/verify.h"
15 #include "mono/metadata/profiler.h"
16 #include "mono/metadata/threadpool.h"
17 #include "mono/metadata/mono-config.h"
18 #include <mono/metadata/profiler-private.h>
19 #include <mono/os/util.h>
20
21 static MonoClass *
22 find_class_in_assembly (MonoAssembly *assembly, const char *namespace, const char *name)
23 {
24         MonoAssembly **ptr;
25         MonoClass *class;
26
27         class = mono_class_from_name (assembly->image, namespace, name);
28         if (class)
29                 return class;
30
31         for (ptr = assembly->image->references; ptr && *ptr; ptr++) {
32                 class = find_class_in_assembly (*ptr, namespace, name);
33                 if (class)
34                         return class;
35         }
36
37         return NULL;
38 }
39
40 static MonoMethod *
41 find_method_in_assembly (MonoAssembly *assembly, MonoMethodDesc *mdesc)
42 {
43         MonoAssembly **ptr;
44         MonoMethod *method;
45
46         method = mono_method_desc_search_in_image (mdesc, assembly->image);
47         if (method)
48                 return method;
49
50         for (ptr = assembly->image->references; ptr && *ptr; ptr++) {
51                 method = find_method_in_assembly (*ptr, mdesc);
52                 if (method)
53                         return method;
54         }
55
56         return NULL;
57 }
58
59 /**
60  * mono_jit_compile_class:
61  * @assembly: Lookup things in this assembly
62  * @compile_class: Name of the image/class/method to compile
63  * @compile_times: Compile it that many times
64  * @verbose: If true, print debugging information on stdout.
65  *
66  * JIT compilation of the image/class/method.
67  *
68  * @compile_class can be one of:
69  *
70  * - an image name (`@corlib')
71  * - a class name (`System.Int32')
72  * - a method name (`System.Int32:Parse')
73  */
74 void
75 mono_jit_compile_class (MonoAssembly *assembly, char *compile_class,
76                         int compile_times, int verbose)
77 {
78         const char *cmethod = strrchr (compile_class, ':');
79         char *cname;
80         char *code;
81         int i, j;
82         MonoClass *class;
83         MonoDomain *domain = mono_domain_get ();
84
85         if (compile_class [0] == '@') {
86                 MonoImage *image = mono_image_loaded (compile_class + 1);
87                 if (!image)
88                         g_error ("Cannot find image %s", compile_class + 1);
89                 if (verbose)
90                         g_print ("Compiling image %s\n", image->name);
91                 for (j = 0; j < compile_times; ++j)
92                         mono_jit_compile_image (image, verbose);
93         } else if (cmethod) {
94                 MonoMethodDesc *mdesc;
95                 MonoMethod *m;
96                 mdesc = mono_method_desc_new (compile_class, FALSE);
97                 if (!mdesc)
98                         g_error ("Invalid method name '%s'", compile_class);
99                 m = find_method_in_assembly (assembly, mdesc);
100                 if (!m)
101                         g_error ("Cannot find method '%s'", compile_class);
102                 for (j = 0; j < compile_times; ++j) {
103                         code = mono_compile_method (m);
104                         // g_free (code);
105                         g_hash_table_remove (domain->jit_code_hash, m);
106                 }
107         } else {
108                 cname = strrchr (compile_class, '.');
109                 if (cname)
110                         *cname++ = 0;
111                 else {
112                         cname = compile_class;
113                         compile_class = (char *)"";
114                 }
115                 class = find_class_in_assembly (assembly, compile_class, cname);
116                 if (!class)
117                         g_error ("Cannot find class %s.%s", compile_class, cname);
118                 mono_class_init (class);
119                 for (j = 0; j < compile_times; ++j) {
120                         for (i = 0; i < class->method.count; ++i) {
121                                 if (class->methods [i]->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
122                                         continue;
123                                 if (class->methods [i]->flags & METHOD_ATTRIBUTE_ABSTRACT)
124                                         continue;
125                                 if (verbose)
126                                         g_print ("Compiling: %s.%s:%s\n",
127                                                  compile_class, cname, class->methods [i]->name);
128                                 code = mono_compile_method (class->methods [i]);
129                                 g_hash_table_remove (domain->jit_code_hash, class->methods [i]);
130                                 // g_free (code);
131                         }
132                 }
133         }
134 }
135
136 static void
137 usage (char *name)
138 {
139         fprintf (stderr,
140                  "mono %s, the Mono ECMA CLI JIT Compiler, (C) 2001, 2002 Ximian, Inc.\n\n"
141                  "Usage is: %s [options] executable args...\n\n",  VERSION, name);
142         fprintf (stderr,
143                  "Runtime Debugging:\n"
144                  "    -d                 debug the jit, show disassembler output.\n"
145                  "    --dump-asm         dumps the assembly code generated\n"
146                  "    --dump-forest      dumps the reconstructed forest\n"
147                  "    --print-vtable     print the VTable of all used classes\n"
148                  "    --stats            print statistics about the jit operations\n"
149                  "    --trace            printf function call trace\n"
150                  "    --compile NAME     compile NAME, then exit.\n"
151                  "                       NAME is in one of the following formats:\n"
152                  "                         namespace.name          compile the given class\n"
153                  "                         namespace.name:method   compile the given method\n"
154                  "                         @imagename              compile the given image\n"
155                  "    --ncompile NUM     compile methods NUM times (default: 1000)\n"
156                  "\n"
157                  "Development:\n"
158                  "    --debug[=FORMAT]   write a debugging file.  FORMAT is one of:\n"
159                  "                         stabs        to write stabs information\n"
160                  "                         dwarf        to write dwarf2 information\n"
161                  "                         dwarf-plus   to write extended dwarf2 information\n"
162                  "    --debug-args ARGS  comma-separated list of additional arguments for the\n"
163                  "                       symbol writer.  See the manpage for details.\n"
164                  "    --profile          record and dump profile info\n"
165                  "    --breakonex        set a breakpoint for unhandled exception\n"
166                  "    --break NAME       insert a breakpoint at the start of method NAME\n"
167                  "                       (NAME is in `namespace.name:methodname' format\n"
168                  "                       or `Main' to break on the application's main method)\n"
169                  "    --precompile name  precompile NAME before executing the main application:\n"
170                  "                       NAME is in one of the following formats:\n"
171                  "                         namespace.name          compile the given class\n"
172                  "                         namespace.name:method   compile the given method\n"
173                  "                         @imagename              compile the given image\n"
174                  "\n"
175                  "Runtime:\n"
176                  "    --config filename  Load specified config file instead of the default.\n"
177                  "    --fast-iconv       Use fast floating point integer conversion\n"
178                  "    --noinline         Disable code inliner\n"
179                  "    --nointrinsic      Disable memcopy inliner\n"
180                  "    --nols             disable linear scan register allocation\n"
181                  "    --share-code       force jit to produce shared code\n"
182                  "    --workers n        maximum number of worker threads\n"
183                 );
184         exit (1);
185 }
186
187 int 
188 main (int argc, char *argv [])
189 {
190         MonoDomain *domain;
191         MonoAssembly *assembly;
192         int retval = 0, i;
193         int compile_times = 1000;
194         char *compile_class = NULL;
195         char *debug_args = NULL;
196         char *file, *error, *config_file = NULL;
197         gboolean testjit = FALSE;
198         int verbose = FALSE;
199         GList *precompile_classes = NULL;
200         int break_on_main = FALSE;
201         MonoDebugHandle *debug = NULL;
202
203         g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
204         g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
205         
206         if (argc < 2)
207                 usage (argv [0]);
208
209         for (i = 1; i < argc && argv [i][0] == '-'; i++){
210                 if (strcmp (argv [i], "--help") == 0) {
211                         usage (argv [0]);
212                 } else if (strcmp (argv [i], "-d") == 0) {
213                         testjit = TRUE;
214                         mono_jit_dump_asm = TRUE;
215                         mono_jit_dump_forest = TRUE;
216                 } else if (strcmp (argv [i], "--dump-asm") == 0)
217                         mono_jit_dump_asm = TRUE;
218                 else if (strcmp (argv [i], "--dump-forest") == 0)
219                         mono_jit_dump_forest = TRUE;
220                 else if (strcmp (argv [i], "--trace") == 0)
221                         mono_jit_trace_calls = TRUE;
222                 else if (strcmp (argv [i], "--share-code") == 0)
223                         mono_jit_share_code = TRUE;
224                 else if (strcmp (argv [i], "--noinline") == 0)
225                         mono_jit_inline_code = FALSE;
226                 else if (strcmp (argv [i], "--nointrinsic") == 0)
227                         mono_inline_memcpy = FALSE;
228                 else if (strcmp (argv [i], "--nols") == 0)
229                         mono_use_linear_scan = FALSE;
230                 else if (strcmp (argv [i], "--breakonex") == 0)
231                         mono_break_on_exc = TRUE;
232                 else if (strcmp (argv [i], "--print-vtable") == 0)
233                         mono_print_vtable = TRUE;
234                 else if (strcmp (argv [i], "--break") == 0) {
235                         if (!strcmp (argv [i+1], "Main")) {
236                                 break_on_main = TRUE;
237                                 i++;
238                         } else {
239                                 MonoMethodDesc *desc = mono_method_desc_new (argv [++i], FALSE);
240                                 if (!desc)
241                                         g_error ("Invalid method name '%s'", argv [i]);
242                                 mono_debug_methods = g_list_append (mono_debug_methods, desc);
243                         }
244                 } else if (strcmp (argv [i], "--count") == 0) {
245                         compile_times = atoi (argv [++i]);
246                 } else if (strcmp (argv [i], "--config") == 0) {
247                         config_file = argv [++i];
248                 } else if (strcmp (argv [i], "--workers") == 0) {
249                         mono_worker_threads = atoi (argv [++i]);
250                         if (mono_worker_threads < 1)
251                                 mono_worker_threads = 1;
252                 } else if (strcmp (argv [i], "--profile") == 0) {
253                         mono_jit_profile = TRUE;
254                         mono_profiler_install_simple ();
255                 } else if (strcmp (argv [i], "--compile") == 0) {
256                         compile_class = argv [++i];
257                 } else if (strcmp (argv [i], "--ncompile") == 0) {
258                         compile_times = atoi (argv [++i]);
259                 } else if (strcmp (argv [i], "--stats") == 0) {
260                         memset (&mono_jit_stats, 0, sizeof (MonoJitStats));
261                         mono_jit_stats.enabled = TRUE;
262                 } else if (strncmp (argv [i], "--debug=", 8) == 0) {
263                         const char *format = &argv [i][8];
264                                 
265                         if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
266                                 g_error ("You can only use one debugging format.");
267                         if (strcmp (format, "stabs") == 0)
268                                 mono_debug_format = MONO_DEBUG_FORMAT_STABS;
269                         else if (strcmp (format, "dwarf") == 0)
270                                 mono_debug_format = MONO_DEBUG_FORMAT_DWARF2;
271                         else if (strcmp (format, "mono") == 0)
272                                 mono_debug_format = MONO_DEBUG_FORMAT_MONO;
273                         else
274                                 g_error ("Unknown debugging format: %s", argv [i] + 8);
275                 } else if (strcmp (argv [i], "--debug") == 0) {
276                         if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
277                                 g_error ("You can only use one debugging format.");
278                         mono_debug_format = MONO_DEBUG_FORMAT_MONO;
279                 } else if (strcmp (argv [i], "--debug-args") == 0) {
280                         if (debug_args)
281                                 g_error ("You can use --debug-args only once.");
282                         debug_args = argv [++i];
283                 } else if (strcmp (argv [i], "--precompile") == 0) {
284                         precompile_classes = g_list_append (precompile_classes, argv [++i]);
285                 } else if (strcmp (argv [i], "--verbose") == 0) {
286                         verbose = TRUE;;
287                 } else if (strcmp (argv [i], "--fast-iconv") == 0) {
288                         mono_use_fast_iconv = TRUE;
289                 } else
290                         usage (argv [0]);
291         }
292         
293         file = argv [i];
294
295         if (!file)
296                 usage (argv [0]);
297
298         mono_config_parse (config_file);
299         mono_set_rootdir ();
300
301         if (mono_debug_format != MONO_DEBUG_FORMAT_NONE) {
302                 gchar **args;
303
304                 args = g_strsplit (debug_args ? debug_args : "", ",", -1);
305                 debug = mono_debug_open (file, mono_debug_format, (const char **) args);
306                 g_strfreev (args);
307         }
308
309         domain = mono_jit_init (file);
310
311         error = mono_verify_corlib ();
312         if (error) {
313                 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
314                 exit (1);
315         }
316
317         assembly = mono_domain_assembly_open (domain, file);
318         if (!assembly){
319                 fprintf (stderr, "Can not open image %s\n", file);
320                 exit (1);
321         }
322
323         if (debug)
324                 mono_debug_add_image (debug, assembly->image);
325
326         if (testjit) {
327                 mono_jit_compile_image (assembly->image, TRUE);
328         } else if (compile_class) {
329                 mono_jit_compile_class (assembly, compile_class, compile_times, TRUE);
330         } else {
331                 GList *tmp;
332
333                 for (tmp = precompile_classes; tmp; tmp = tmp->next)
334                         mono_jit_compile_class (assembly, tmp->data, 1, verbose);
335
336                 if (break_on_main) {
337                         MonoImage *image = assembly->image;
338                         MonoMethodDesc *desc;
339                         MonoMethod *method;
340
341                         method = mono_get_method (image, mono_image_get_entry_point (image), NULL);
342                         desc = mono_method_desc_from_method (method);
343                         mono_debug_methods = g_list_append (mono_debug_methods, desc);
344                 }
345
346                 retval = mono_jit_exec (domain, assembly, argc - i, argv + i);
347         }
348
349         mono_profiler_shutdown ();
350         mono_jit_cleanup (domain);
351
352         return retval;
353 }
354
355