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