2 * mono.c: Main driver for the Mono JIT engine
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
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>
22 * @image: reference to an image
23 * @verbose: If true, print debugging information on stdout.
25 * JIT compilation of all methods in the image.
28 mono_jit_compile_image (MonoImage *image, int verbose)
31 MonoTableInfo *t = &image->tables [MONO_TABLE_METHOD];
34 for (i = 0; i < t->rows; i++) {
36 method = mono_get_method (image,
37 (MONO_TABLE_METHOD << 24) | (i + 1),
41 g_print ("Compiling: %s:%s\n\n", image->assembly_name, method->name);
43 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT) {
45 printf ("ABSTARCT\n");
47 mono_compile_method (method);
54 find_class_in_assembly (MonoAssembly *assembly, const char *namespace, const char *name)
59 class = mono_class_from_name (assembly->image, namespace, name);
63 for (ptr = assembly->image->references; ptr && *ptr; ptr++) {
64 class = find_class_in_assembly (*ptr, namespace, name);
73 find_method_in_assembly (MonoAssembly *assembly, MonoMethodDesc *mdesc)
78 method = mono_method_desc_search_in_image (mdesc, assembly->image);
82 for (ptr = assembly->image->references; ptr && *ptr; ptr++) {
83 method = find_method_in_assembly (*ptr, mdesc);
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.
98 * JIT compilation of the image/class/method.
100 * @compile_class can be one of:
102 * - an image name (`@corlib')
103 * - a class name (`System.Int32')
104 * - a method name (`System.Int32:Parse')
107 mono_jit_compile_class (MonoAssembly *assembly, char *compile_class,
108 int compile_times, int verbose)
110 const char *cmethod = strrchr (compile_class, ':');
115 MonoDomain *domain = mono_domain_get ();
117 if (compile_class [0] == '@') {
118 MonoImage *image = mono_image_loaded (compile_class + 1);
120 g_error ("Cannot find image %s", compile_class + 1);
122 g_print ("Compiling image %s\n", image->name);
123 for (j = 0; j < compile_times; ++j)
124 mono_jit_compile_image (image, verbose);
125 } else if (cmethod) {
126 MonoMethodDesc *mdesc;
128 mdesc = mono_method_desc_new (compile_class, FALSE);
130 g_error ("Invalid method name '%s'", compile_class);
131 m = find_method_in_assembly (assembly, mdesc);
133 g_error ("Cannot find method '%s'", compile_class);
134 for (j = 0; j < compile_times; ++j) {
135 code = mono_compile_method (m);
137 g_hash_table_remove (domain->jit_code_hash, m);
140 cname = strrchr (compile_class, '.');
144 cname = compile_class;
145 compile_class = (char *)"";
147 class = find_class_in_assembly (assembly, compile_class, cname);
149 g_error ("Cannot find class %s.%s", compile_class, cname);
150 mono_class_init (class);
151 for (j = 0; j < compile_times; ++j) {
152 for (i = 0; i < class->method.count; ++i) {
153 if (class->methods [i]->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
155 if (class->methods [i]->flags & METHOD_ATTRIBUTE_ABSTRACT)
158 g_print ("Compiling: %s.%s:%s\n",
159 compile_class, cname, class->methods [i]->name);
160 code = mono_compile_method (class->methods [i]);
161 g_hash_table_remove (domain->jit_code_hash, class->methods [i]);
172 "%s %s, the Mono ECMA CLI JIT Compiler, (C) 2001, 2002 Ximian, Inc.\n\n"
173 "Usage is: %s [options] executable args...\n\n", name, VERSION, name);
175 "Runtime Debugging:\n"
176 " -d debug the jit, show disassembler output.\n"
177 " --dump-asm dumps the assembly code generated\n"
178 " --dump-forest dumps the reconstructed forest\n"
179 " --print-vtable print the VTable of all used classes\n"
180 " --stats print statistics about the jit operations\n"
181 " --trace printf function call trace\n"
182 " --compile NAME compile NAME, then exit.\n"
183 " NAME is in one of the following formats:\n"
184 " namespace.name compile the given class\n"
185 " namespace.name:method compile the given method\n"
186 " @imagename compile the given image\n"
187 " --ncompile NUM compile methods NUM times (default: 1000)\n"
190 " --debug[=FORMAT] write a debugging file. FORMAT is one of:\n"
191 " stabs to write stabs information\n"
192 " dwarf to write dwarf2 information\n"
193 " dwarf-plus to write extended dwarf2 information\n"
194 " --debug-args ARGS comma-separated list of additional arguments for the\n"
195 " symbol writer. See the manpage for details.\n"
196 " --profile record and dump profile info\n"
197 " --breakonex set a breakpoint for unhandled exception\n"
198 " --break NAME insert a breakpoint at the start of method NAME\n"
199 " (NAME is in `namespace.name:methodname' format)\n"
200 " --precompile name precompile NAME before executing the main application:\n"
201 " NAME is in one of the following formats:\n"
202 " namespace.name compile the given class\n"
203 " namespace.name:method compile the given method\n"
204 " @imagename compile the given image\n"
207 " --fast-iconv Use fast floating point integer conversion\n"
208 " --noinline Disable code inliner\n"
209 " --nols disable linear scan register allocation\n"
210 " --share-code force jit to produce shared code\n"
211 " --workers n maximum number of worker threads\n"
217 main (int argc, char *argv [])
220 MonoAssembly *assembly;
222 int compile_times = 1000;
223 char *compile_class = NULL;
224 char *debug_args = NULL;
226 gboolean testjit = FALSE;
228 GList *precompile_classes = NULL;
230 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
231 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
236 for (i = 1; i < argc && argv [i][0] == '-'; i++){
237 if (strcmp (argv [i], "--help") == 0) {
239 } else if (strcmp (argv [i], "-d") == 0) {
241 mono_jit_dump_asm = TRUE;
242 mono_jit_dump_forest = TRUE;
243 } else if (strcmp (argv [i], "--dump-asm") == 0)
244 mono_jit_dump_asm = TRUE;
245 else if (strcmp (argv [i], "--dump-forest") == 0)
246 mono_jit_dump_forest = TRUE;
247 else if (strcmp (argv [i], "--trace") == 0)
248 mono_jit_trace_calls = TRUE;
249 else if (strcmp (argv [i], "--share-code") == 0)
250 mono_jit_share_code = TRUE;
251 else if (strcmp (argv [i], "--noinline") == 0)
252 mono_jit_inline_code = FALSE;
253 else if (strcmp (argv [i], "--nols") == 0)
254 mono_use_linear_scan = FALSE;
255 else if (strcmp (argv [i], "--breakonex") == 0)
256 mono_break_on_exc = TRUE;
257 else if (strcmp (argv [i], "--print-vtable") == 0)
258 mono_print_vtable = TRUE;
259 else if (strcmp (argv [i], "--break") == 0) {
260 MonoMethodDesc *desc = mono_method_desc_new (argv [++i], FALSE);
262 g_error ("Invalid method name '%s'", argv [i]);
263 mono_debug_methods = g_list_append (mono_debug_methods, desc);
264 } else if (strcmp (argv [i], "--count") == 0) {
265 compile_times = atoi (argv [++i]);
266 } else if (strcmp (argv [i], "--workers") == 0) {
267 mono_worker_threads = atoi (argv [++i]);
268 if (mono_worker_threads < 1)
269 mono_worker_threads = 1;
270 } else if (strcmp (argv [i], "--profile") == 0) {
271 mono_jit_profile = TRUE;
272 mono_profiler_install_simple ();
273 } else if (strcmp (argv [i], "--compile") == 0) {
274 compile_class = argv [++i];
275 } else if (strcmp (argv [i], "--ncompile") == 0) {
276 compile_times = atoi (argv [++i]);
277 } else if (strcmp (argv [i], "--stats") == 0) {
278 memset (&mono_jit_stats, 0, sizeof (MonoJitStats));
279 mono_jit_stats.enabled = TRUE;
280 } else if (strncmp (argv [i], "--debug=", 8) == 0) {
281 const char *format = &argv [i][8];
283 if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
284 g_error ("You can only use one debugging format.");
285 if (strcmp (format, "stabs") == 0)
286 mono_debug_format = MONO_DEBUG_FORMAT_STABS;
287 else if (strcmp (format, "dwarf") == 0)
288 mono_debug_format = MONO_DEBUG_FORMAT_DWARF2;
289 else if (strcmp (format, "dwarf-plus") == 0)
290 mono_debug_format = MONO_DEBUG_FORMAT_DWARF2_PLUS;
292 g_error ("Unknown debugging format: %s", argv [i] + 8);
293 } else if (strcmp (argv [i], "--debug") == 0) {
294 if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
295 g_error ("You can only use one debugging format.");
296 mono_debug_format = MONO_DEBUG_FORMAT_DWARF2_PLUS;
297 } else if (strcmp (argv [i], "--debug-args") == 0) {
299 g_error ("You can use --debug-args only once.");
300 debug_args = argv [++i];
301 } else if (strcmp (argv [i], "--precompile") == 0) {
302 precompile_classes = g_list_append (precompile_classes, argv [++i]);
303 } else if (strcmp (argv [i], "--verbose") == 0) {
305 } else if (strcmp (argv [i], "--fast-iconv") == 0) {
306 mono_use_fast_iconv = TRUE;
316 mono_set_rootdir (argv [0]);
317 domain = mono_jit_init (file);
319 error = mono_verify_corlib ();
321 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
325 assembly = mono_domain_assembly_open (domain, file);
327 fprintf (stderr, "Can not open image %s\n", file);
331 if (mono_debug_format != MONO_DEBUG_FORMAT_NONE) {
332 MonoDebugHandle *debug;
335 args = g_strsplit (debug_args ? debug_args : "", ",", -1);
336 debug = mono_debug_open (assembly, mono_debug_format, (const char **) args);
337 mono_debug_add_image (debug, assembly->image);
342 mono_jit_compile_image (assembly->image, TRUE);
343 } else if (compile_class) {
344 mono_jit_compile_class (assembly, compile_class, compile_times, TRUE);
348 for (tmp = precompile_classes; tmp; tmp = tmp->next)
349 mono_jit_compile_class (assembly, tmp->data, 1, verbose);
351 retval = mono_jit_exec (domain, assembly, argc - i, argv + i);
352 printf ("RESULT: %d\n", retval);
355 mono_profiler_shutdown ();
356 mono_jit_cleanup (domain);