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