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