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