2 * driver.c: The new mono JIT compiler.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
15 #include <mono/metadata/assembly.h>
16 #include <mono/metadata/loader.h>
17 #include <mono/metadata/cil-coff.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/class.h>
20 #include <mono/metadata/object.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/metadata/opcodes.h>
23 #include <mono/metadata/mono-endian.h>
24 #include <mono/metadata/tokentype.h>
25 #include <mono/metadata/tabledefs.h>
26 #include <mono/metadata/threads.h>
27 #include <mono/metadata/marshal.h>
28 #include <mono/metadata/socket-io.h>
29 #include <mono/metadata/appdomain.h>
30 #include <mono/metadata/debug-helpers.h>
31 #include <mono/io-layer/io-layer.h>
32 #include "mono/metadata/profiler.h"
33 #include <mono/metadata/profiler-private.h>
34 #include <mono/metadata/mono-config.h>
35 #include <mono/metadata/environment.h>
36 #include <mono/metadata/mono-debug.h>
37 #include <mono/metadata/mono-debug-debugger.h>
46 static FILE *mini_stats_fd = NULL;
48 static void mini_usage (void);
50 typedef void (*OptFunc) (const char *p);
52 /* keep in sync with enum in mini.h */
61 {"peephole", "Peephole postpass"},
62 {"branch", "Branch optimizations"},
63 {"inline", "Inline method calls"},
64 {"cfold", "Constant folding"},
65 {"consprop", "Constant propagation"},
66 {"copyprop", "Copy propagation"},
67 {"deadce", "Dead code elimination"},
68 {"linears", "Linear scan global reg allocation"},
69 {"cmov", "Conditional moves"},
70 {"shared", "Emit per-domain code"},
71 {"sched", "Instruction scheduling"},
72 {"intrins", "Intrinsic method implementations"},
73 {"tailc", "Tail recursion and tail calls"},
74 {"loop", "Loop related optimizations"},
75 {"fcmov", "Fast x86 FP compares"},
76 {"leaf", "Leaf procedures optimizations"},
77 {"aot", "Usage of Ahead Of Time compiled code"}
80 #define DEFAULT_OPTIMIZATIONS ( \
89 parse_optimizations (const char* p)
91 /* the default value */
92 guint32 opt = DEFAULT_OPTIMIZATIONS;
97 /* call out to cpu detection code here that sets the defaults ... */
98 opt |= mono_arch_cpu_optimizazions (&exclude);
110 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
111 n = opt_names [i].name;
113 if (strncmp (p, n, len) == 0) {
122 } else if (*p == '=') {
124 if (opt_names [i].func)
125 opt_names [i].func (p);
126 while (*p && *p++ != ',');
133 if (i == G_N_ELEMENTS (opt_names)) {
134 if (strncmp (p, "all", 3) == 0) {
138 opt = ~(MONO_OPT_SHARED | exclude);
143 fprintf (stderr, "Invalid optimization name `%s'\n", p);
154 MonoGraphOptions value;
157 static const GraphName
159 {"cfg", "Control Flow Graph (CFG)" , MONO_GRAPH_CFG},
160 {"dtree", "Dominator Tree", MONO_GRAPH_DTREE},
161 {"code", "CFG showing code", MONO_GRAPH_CFG_CODE},
162 {"ssa", "CFG showing code after SSA translation", MONO_GRAPH_CFG_SSA},
163 {"optcode", "CFG showing code after IR optimizations", MONO_GRAPH_CFG_OPTCODE}
166 static MonoGraphOptions
167 mono_parse_graph_options (const char* p)
172 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
173 n = graph_names [i].name;
175 if (strncmp (p, n, len) == 0)
176 return graph_names [i].value;
179 fprintf (stderr, "Invalid graph name provided: %s\n", p);
184 mono_parse_default_optimizations (const char* p)
188 opt = parse_optimizations (p);
193 opt_descr (guint32 flags) {
194 GString *str = g_string_new ("");
198 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
199 if (flags & (1 << i)) {
201 g_string_append_c (str, ',');
202 g_string_append (str, opt_names [i].name);
206 return g_string_free (str, FALSE);
216 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_INTRINS,
217 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
218 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
219 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_CFOLD,
220 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
221 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS
224 typedef int (*TestMethod) (void);
228 domain_dump_native_code (MonoDomain *domain) {
229 // need to poke into the domain, move to metadata/domain.c
230 // need to empty jit_info_table and code_mp
235 mini_regression (MonoImage *image, int verbose, int *total_run) {
236 guint32 i, opt, opt_flags;
240 int result, expected, failed, cfailed, run, code_size, total;
242 GTimer *timer = g_timer_new ();
245 fprintf (mini_stats_fd, "$stattitle = \'Mono Benchmark Results (various optimizations)\';\n");
247 fprintf (mini_stats_fd, "$graph->set_legend(qw(");
248 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); opt++) {
249 opt_flags = opt_sets [opt];
250 n = opt_descr (opt_flags);
254 fprintf (mini_stats_fd, " ");
255 fprintf (mini_stats_fd, "%s", n);
259 fprintf (mini_stats_fd, "));\n");
261 fprintf (mini_stats_fd, "@data = (\n");
262 fprintf (mini_stats_fd, "[");
265 /* load the metadata */
266 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
267 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
268 mono_class_init (method->klass);
270 if (!strncmp (method->name, "test_", 5) && mini_stats_fd) {
271 fprintf (mini_stats_fd, "\"%s\",", method->name);
275 fprintf (mini_stats_fd, "],\n");
280 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
281 double elapsed, comp_time, start_time;
284 opt_flags = opt_sets [opt];
285 mono_set_defaults (verbose, opt_flags);
286 n = opt_descr (opt_flags);
287 g_print ("Test run: image=%s, opts=%s\n", image->name, n);
289 cfailed = failed = run = code_size = 0;
290 comp_time = elapsed = 0.0;
292 /* fixme: ugly hack - delete all previously compiled methods */
293 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
294 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
298 g_timer_start (timer);
300 fprintf (mini_stats_fd, "[");
301 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
302 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
303 if (strncmp (method->name, "test_", 5) == 0) {
304 expected = atoi (method->name + 5);
306 start_time = g_timer_elapsed (timer, NULL);
307 comp_time -= start_time;
308 cfg = mini_method_compile (method, opt_flags, mono_root_domain, 0);
309 comp_time += g_timer_elapsed (timer, NULL);
312 g_print ("Running '%s' ...\n", method->name);
313 #ifdef MONO_USE_AOT_COMPILER
314 if ((jinfo = mono_aot_get_method (mono_root_domain, method)))
315 func = jinfo->code_start;
318 func = (TestMethod)cfg->native_code;
320 if (result != expected) {
323 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
325 code_size += cfg->code_len;
326 mono_destroy_compile (cfg);
331 g_print ("Test '%s' failed compilation.\n", method->name);
334 fprintf (mini_stats_fd, "%f, ",
335 g_timer_elapsed (timer, NULL) - start_time);
339 fprintf (mini_stats_fd, "],\n");
340 g_timer_stop (timer);
341 elapsed = g_timer_elapsed (timer, NULL);
342 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
343 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
344 g_print ("Elapsed time: %f secs (%f, %f), Code size: %d\n\n", elapsed,
345 elapsed - comp_time, comp_time, code_size);
346 total += failed + cfailed;
351 fprintf (mini_stats_fd, ");\n");
352 fflush (mini_stats_fd);
355 g_timer_destroy (timer);
360 mini_regression_list (int verbose, int count, char *images [])
362 int i, total, total_run, run;
365 total_run = total = 0;
366 for (i = 0; i < count; ++i) {
367 ass = mono_assembly_open (images [i], NULL);
369 g_warning ("failed to load assembly: %s", images [i]);
372 total += mini_regression (ass->image, verbose, &run);
374 mono_assembly_close (ass);
376 g_print ("Overall results: tests: %d, failed: %d, opt combinations: %d (pass: %.2f%%)\n",
377 total_run, total, G_N_ELEMENTS (opt_sets), 100.0*(total_run-total)/total_run);
390 compile_all_methods (MonoAssembly *ass, int verbose)
392 MonoImage *image = ass->image;
396 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
397 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
398 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
400 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
405 char * desc = mono_method_full_name (method, TRUE);
406 g_print ("Compiling %d %s\n", count, desc + 3);
409 mono_compile_method (method);
416 * @assembly: reference to an assembly
417 * @argc: argument count
418 * @argv: argument vector
420 * Start execution of a program.
423 mono_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
425 MonoImage *image = assembly->image;
427 guint32 entry = mono_image_get_entry_point (image);
430 g_print ("Assembly '%s' doesn't have an entry point.\n", image->name);
431 /* FIXME: remove this silly requirement. */
432 mono_environment_exitcode_set (1);
436 method = mono_get_method (image, entry, NULL);
438 return mono_runtime_run_main (method, argc, argv, NULL);
450 static void main_thread_handler (gpointer user_data)
452 MainThreadArgs *main_args = user_data;
453 MonoAssembly *assembly;
455 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
457 fprintf (stderr, "Can not open image %s\n", main_args->file);
461 if (mono_compile_aot) {
462 int res = mono_compile_assembly (assembly, main_args->opts);
463 printf ("AOT RESULT %d\n", res);
465 mono_jit_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
475 "Usage is: mono [options] assembly\n\n"
476 "Runtime and JIT debugging:\n"
477 " --compile METHOD Just compile METHOD in assembly\n"
478 " --ncompile N Number of times to compile METHOD (default: 1)\n"
479 " --regression Runs the regression test contained in the assembly\n"
480 " --print-vtable Print the vtable of all used classes\n"
481 " --trace Enable tracing\n"
482 " --compile-all Compiles all the methods in the assembly\n"
483 " --breakonex Inserts a breakpoint on exceptions\n"
484 " --break METHOD Inserts a breakpoint at METHOD entry\n"
485 " --debug Enable debugging support\n"
486 " --stats Print statistics about the JIT operations\n"
489 " --statfile FILE Sets the stat file to FILE\n"
490 " --aot Compiles the assembly to native code\n"
491 " --profile[=profiler] Runs in profiling mode with the specified profiler module\n"
492 " --graph[=TYPE] METHOD Draws a graph of the specified method:\n");
494 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
495 fprintf (stderr, " %-10s %s\n", graph_names [i].name, graph_names [i].desc);
501 " --config FILE Loads FILE as the Mono config\n"
502 " --verbose, -v Increases the verbosity level\n"
503 " --help, -h Show usage information\n"
504 " --version, -V Show version information\n"
505 " --optimize=OPT Turns on a specific optimization:\n");
507 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
508 fprintf (stderr, " %-10s %s\n", opt_names [i].name, opt_names [i].desc);
512 mono_main (int argc, char* argv[]) {
513 MainThreadArgs main_args;
514 MonoAssembly *assembly;
515 MonoMethodDesc *desc;
519 const char* aname, *mname = NULL;
520 char *config_file = NULL;
522 int enable_debugging = FALSE;
523 guint32 opt, action = DO_EXEC;
524 MonoGraphOptions mono_graph_options = 0;
525 int mini_verbose = 0;
527 setlocale (LC_ALL, "");
528 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
529 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
531 opt = parse_optimizations (NULL);
533 for (i = 1; i < argc; ++i) {
534 if (argv [i] [0] != '-')
536 if (strcmp (argv [i], "--regression") == 0) {
537 action = DO_REGRESSION;
538 } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
540 } else if (strcmp (argv [i], "--version") == 0 || strcmp (argv [i], "-V") == 0) {
541 g_print ("Mono JIT compiler version %s, (C) 2002, 2003 Ximian, Inc.\n", VERSION);
543 } else if (strcmp (argv [i], "--help") == 0 || strcmp (argv [i], "-h") == 0) {
546 } else if (strncmp (argv [i], "--statfile", 10) == 0) {
547 mini_stats_fd = fopen (argv [++i], "w+");
548 } else if (strncmp (argv [i], "--optimize=", 11) == 0) {
549 opt = parse_optimizations (argv [i] + 11);
550 } else if (strncmp (argv [i], "-O=", 3) == 0) {
551 opt = parse_optimizations (argv [i] + 3);
552 } else if (strcmp (argv [i], "--config") == 0) {
553 config_file = argv [++i];
554 } else if (strcmp (argv [i], "--ncompile") == 0) {
555 count = atoi (argv [++i]);
557 } else if (strcmp (argv [i], "--trace") == 0) {
558 mono_jit_trace_calls = TRUE;
559 } else if (strcmp (argv [i], "--breakonex") == 0) {
560 mono_break_on_exc = TRUE;
561 } else if (strcmp (argv [i], "--break") == 0) {
562 if (!mono_debugger_insert_breakpoint (argv [++i], FALSE))
563 g_error ("Invalid method name '%s'", argv [i]);
564 } else if (strcmp (argv [i], "--print-vtable") == 0) {
565 mono_print_vtable = TRUE;
566 } else if (strcmp (argv [i], "--stats") == 0) {
567 mono_jit_stats.enabled = TRUE;
568 } else if (strcmp (argv [i], "--aot") == 0) {
569 mono_compile_aot = TRUE;
570 } else if (strcmp (argv [i], "--compile-all") == 0) {
572 } else if (strcmp (argv [i], "--profile") == 0) {
573 mono_profiler_load (NULL);
574 } else if (strncmp (argv [i], "--profile=", 10) == 0) {
575 mono_profiler_load (argv [i] + 10);
576 } else if (strcmp (argv [i], "--compile") == 0) {
579 } else if (strncmp (argv [i], "--graph=", 8) == 0) {
580 mono_graph_options = mono_parse_graph_options (argv [i] + 8);
583 } else if (strcmp (argv [i], "--graph") == 0) {
585 mono_graph_options = MONO_GRAPH_CFG;
587 } else if (strcmp (argv [i], "--debug") == 0) {
588 enable_debugging = TRUE;
590 fprintf (stderr, "Unknown command line option: '%s'\n", argv [i]);
600 if (mono_compile_aot || action == DO_EXEC) {
601 g_set_prgname (argv[i]);
604 mono_set_defaults (mini_verbose, opt);
605 domain = mini_init (argv [i]);
609 if (mini_regression_list (mini_verbose, argc -i, argv + i)) {
610 g_print ("Regression ERRORS!\n");
611 mini_cleanup (domain);
614 mini_cleanup (domain);
617 if (argc - i != 1 || mname == NULL) {
618 g_print ("Usage: mini --ncompile num --compile method assembly\n");
619 mini_cleanup (domain);
627 mini_cleanup (domain);
633 if (argc - i != 1 || mname == NULL) {
635 mini_cleanup (domain);
643 mini_cleanup (domain);
650 if (enable_debugging)
651 mono_debug_init (domain, MONO_DEBUG_FORMAT_MONO);
653 assembly = mono_assembly_open (aname, NULL);
655 fprintf (stderr, "cannot open assembly %s\n", aname);
656 mini_cleanup (domain);
660 if (enable_debugging)
661 mono_debug_init_2 (assembly);
663 if (mono_compile_aot || action == DO_EXEC) {
664 mono_config_parse (config_file);
665 //mono_set_rootdir ();
667 main_args.domain = domain;
668 main_args.file = aname;
669 main_args.argc = argc - i;
670 main_args.argv = argv + i;
671 main_args.opts = opt;
673 mono_runtime_exec_managed_code (domain, main_thread_handler, &main_args);
674 mini_cleanup (domain);
675 /* Look up return value from System.Environment.ExitCode */
676 i = mono_environment_exitcode_get ();
678 } else if (action == DO_COMPILE) {
679 compile_all_methods (assembly, mini_verbose);
680 mini_cleanup (domain);
683 desc = mono_method_desc_new (mname, 0);
685 g_print ("Invalid method name %s\n", mname);
686 mini_cleanup (domain);
689 method = mono_method_desc_search_in_image (desc, assembly->image);
691 g_print ("Cannot find method %s\n", mname);
692 mini_cleanup (domain);
696 if (action == DO_DRAW) {
699 switch (mono_graph_options) {
700 case MONO_GRAPH_DTREE:
702 opt |= MONO_OPT_LOOP;
704 case MONO_GRAPH_CFG_CODE:
707 case MONO_GRAPH_CFG_SSA:
710 case MONO_GRAPH_CFG_OPTCODE:
717 cfg = mini_method_compile (method, opt, mono_root_domain, part);
718 if ((mono_graph_options & MONO_GRAPH_CFG_SSA) && !(cfg->comp_done & MONO_COMP_SSA)) {
719 g_warning ("no SSA info available (use -O=deadce)");
722 mono_draw_graph (cfg, mono_graph_options);
723 mono_destroy_compile (cfg);
725 } else if (action == DO_BENCH) {
728 double no_opt_time = 0.0;
729 GTimer *timer = g_timer_new ();
730 fprintf (mini_stats_fd, "$stattitle = \'Compilations times for %s\';\n",
731 mono_method_full_name (method, TRUE));
732 fprintf (mini_stats_fd, "@data = (\n");
733 fprintf (mini_stats_fd, "[");
734 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
739 fprintf (mini_stats_fd, "\"%s\",", n);
741 fprintf (mini_stats_fd, "],\n[");
743 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
747 g_timer_start (timer);
748 for (j = 0; j < count; ++j) {
749 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
750 mono_destroy_compile (cfg);
752 g_timer_stop (timer);
753 elapsed = g_timer_elapsed (timer, NULL);
755 no_opt_time = elapsed;
756 fprintf (mini_stats_fd, "%f, ", elapsed);
758 fprintf (mini_stats_fd, "]");
759 if (no_opt_time > 0.0) {
760 fprintf (mini_stats_fd, ", \n[");
761 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++)
762 fprintf (mini_stats_fd, "%f,", no_opt_time);
763 fprintf (mini_stats_fd, "]");
765 fprintf (mini_stats_fd, ");\n");
767 for (i = 0; i < count; ++i) {
768 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
769 mono_destroy_compile (cfg);
773 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
774 mono_destroy_compile (cfg);
777 mini_cleanup (domain);
782 mono_jit_init (const char *file)
784 return mini_init (file);
788 mono_jit_cleanup (MonoDomain *domain)
790 mini_cleanup (domain);