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"}
79 #define DEFAULT_OPTIMIZATIONS ( \
87 parse_optimizations (const char* p)
89 /* the default value */
90 guint32 opt = DEFAULT_OPTIMIZATIONS;
95 /* call out to cpu detection code here that sets the defaults ... */
96 opt |= mono_arch_cpu_optimizazions (&exclude);
108 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
109 n = opt_names [i].name;
111 if (strncmp (p, n, len) == 0) {
120 } else if (*p == '=') {
122 if (opt_names [i].func)
123 opt_names [i].func (p);
124 while (*p && *p++ != ',');
131 if (i == G_N_ELEMENTS (opt_names)) {
132 if (strncmp (p, "all", 3) == 0) {
136 opt = ~(MONO_OPT_SHARED | exclude);
141 fprintf (stderr, "Invalid optimization name `%s'\n", p);
152 MonoGraphOptions value;
155 static const GraphName
157 {"cfg", "Control Flow Graph (CFG)" , MONO_GRAPH_CFG},
158 {"dtree", "Dominator Tree", MONO_GRAPH_DTREE},
159 {"code", "CFG showing code", MONO_GRAPH_CFG_CODE},
160 {"ssa", "CFG showing code after SSA translation", MONO_GRAPH_CFG_SSA},
161 {"optcode", "CFG showing code after IR optimizations", MONO_GRAPH_CFG_OPTCODE}
164 static MonoGraphOptions
165 mono_parse_graph_options (const char* p)
170 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
171 n = graph_names [i].name;
173 if (strncmp (p, n, len) == 0)
174 return graph_names [i].value;
177 fprintf (stderr, "Invalid graph name provided: %s\n", p);
182 mono_parse_default_optimizations (const char* p)
186 opt = parse_optimizations (p);
191 opt_descr (guint32 flags) {
192 GString *str = g_string_new ("");
196 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
197 if (flags & (1 << i)) {
199 g_string_append_c (str, ',');
200 g_string_append (str, opt_names [i].name);
204 return g_string_free (str, FALSE);
214 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_INTRINS,
215 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
216 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
217 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_CFOLD,
218 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
219 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
222 typedef int (*TestMethod) (void);
226 domain_dump_native_code (MonoDomain *domain) {
227 // need to poke into the domain, move to metadata/domain.c
228 // need to empty jit_info_table and code_mp
233 mini_regression (MonoImage *image, int verbose, int *total_run) {
234 guint32 i, opt, opt_flags;
238 int result, expected, failed, cfailed, run, code_size, total;
240 GTimer *timer = g_timer_new ();
243 fprintf (mini_stats_fd, "$stattitle = \'Mono Benchmark Results (various optimizations)\';\n");
245 fprintf (mini_stats_fd, "$graph->set_legend(qw(");
246 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); opt++) {
247 opt_flags = opt_sets [opt];
248 n = opt_descr (opt_flags);
252 fprintf (mini_stats_fd, " ");
253 fprintf (mini_stats_fd, "%s", n);
257 fprintf (mini_stats_fd, "));\n");
259 fprintf (mini_stats_fd, "@data = (\n");
260 fprintf (mini_stats_fd, "[");
263 /* load the metadata */
264 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
265 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
266 mono_class_init (method->klass);
268 if (!strncmp (method->name, "test_", 5) && mini_stats_fd) {
269 fprintf (mini_stats_fd, "\"%s\",", method->name);
273 fprintf (mini_stats_fd, "],\n");
278 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
279 double elapsed, comp_time, start_time;
280 opt_flags = opt_sets [opt];
281 mono_set_defaults (verbose, opt_flags);
282 n = opt_descr (opt_flags);
283 g_print ("Test run: image=%s, opts=%s\n", image->name, n);
285 cfailed = failed = run = code_size = 0;
286 comp_time = elapsed = 0.0;
288 /* fixme: ugly hack - delete all previously compiled methods */
289 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
290 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
294 g_timer_start (timer);
296 fprintf (mini_stats_fd, "[");
297 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
298 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
299 if (strncmp (method->name, "test_", 5) == 0) {
300 expected = atoi (method->name + 5);
302 start_time = g_timer_elapsed (timer, NULL);
303 comp_time -= start_time;
304 cfg = mini_method_compile (method, opt_flags, mono_root_domain, 0);
305 comp_time += g_timer_elapsed (timer, NULL);
308 g_print ("Running '%s' ...\n", method->name);
309 #ifdef MONO_USE_AOT_COMPILER
310 if (!(func = mono_aot_get_method (method)))
312 func = (TestMethod)cfg->native_code;
314 if (result != expected) {
317 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
319 code_size += cfg->code_len;
320 mono_destroy_compile (cfg);
325 g_print ("Test '%s' failed compilation.\n", method->name);
328 fprintf (mini_stats_fd, "%f, ",
329 g_timer_elapsed (timer, NULL) - start_time);
333 fprintf (mini_stats_fd, "],\n");
334 g_timer_stop (timer);
335 elapsed = g_timer_elapsed (timer, NULL);
336 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
337 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
338 g_print ("Elapsed time: %f secs (%f, %f), Code size: %d\n\n", elapsed,
339 elapsed - comp_time, comp_time, code_size);
340 total += failed + cfailed;
345 fprintf (mini_stats_fd, ");\n");
346 fflush (mini_stats_fd);
349 g_timer_destroy (timer);
354 mini_regression_list (int verbose, int count, char *images [])
356 int i, total, total_run, run;
359 total_run = total = 0;
360 for (i = 0; i < count; ++i) {
361 ass = mono_assembly_open (images [i], NULL);
363 g_warning ("failed to load assembly: %s", images [i]);
366 total += mini_regression (ass->image, verbose, &run);
368 mono_assembly_close (ass);
370 g_print ("Overall results: tests: %d, failed: %d, opt combinations: %d (pass: %.2f%%)\n",
371 total_run, total, G_N_ELEMENTS (opt_sets), 100.0*(total_run-total)/total_run);
384 compile_all_methods (MonoAssembly *ass, int verbose)
386 MonoImage *image = ass->image;
390 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
391 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
392 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
394 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
399 char * desc = mono_method_full_name (method, TRUE);
400 g_print ("Compiling %d %s\n", count, desc + 3);
403 mono_compile_method (method);
410 * @assembly: reference to an assembly
411 * @argc: argument count
412 * @argv: argument vector
414 * Start execution of a program.
417 mono_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
419 MonoImage *image = assembly->image;
421 guint32 entry = mono_image_get_entry_point (image);
424 g_print ("Assembly '%s' doesn't have an entry point.\n", image->name);
425 /* FIXME: remove this silly requirement. */
426 mono_environment_exitcode_set (1);
430 method = mono_get_method (image, entry, NULL);
432 return mono_runtime_run_main (method, argc, argv, NULL);
444 static void main_thread_handler (gpointer user_data)
446 MainThreadArgs *main_args = user_data;
447 MonoAssembly *assembly;
449 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
451 fprintf (stderr, "Can not open image %s\n", main_args->file);
455 if (mono_compile_aot) {
456 int res = mono_compile_assembly (assembly, main_args->opts);
457 printf ("AOT RESULT %d\n", res);
459 mono_jit_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
469 "Usage is: mono [options] assembly\n\n"
470 "Runtime and JIT debugging:\n"
471 " --compile METHOD Just compile METHOD in assembly\n"
472 " --ncompile N Number of times to compile METHOD (default: 1)\n"
473 " --regression Runs the regression test contained in the assembly\n"
474 " --print-vtable Print the vtable of all used classes\n"
475 " --trace Enable tracing\n"
476 " --compile-all Compiles all the methods in the assembly\n"
477 " --breakonex Inserts a breakpoint on exceptions\n"
478 " --break METHOD Inserts a breakpoint at METHOD entry\n"
479 " --debug Enable debugging support\n"
482 " --statfile FILE Sets the stat file to FILE\n"
483 " --aot Compiles the assembly to native code\n"
484 " --profile[=profiler] Runs in profiling mode with the specified profiler module\n"
485 " --graph[=TYPE] METHOD Draws a graph of the specified method:\n");
487 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
488 fprintf (stderr, " %-10s %s\n", graph_names [i].name, graph_names [i].desc);
494 " --config FILE Loads FILE as the Mono config\n"
495 " --verbose, -v Increases the verbosity level\n"
496 " --help, -h Show usage information\n"
497 " --version, -V Show version information\n"
498 " --optimize=OPT Turns on a specific optimization:\n");
500 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
501 fprintf (stderr, " %-10s %s\n", opt_names [i].name, opt_names [i].desc);
505 mono_main (int argc, char* argv[]) {
506 MainThreadArgs main_args;
507 MonoAssembly *assembly;
508 MonoMethodDesc *desc;
512 const char* aname, *mname = NULL;
513 char *config_file = NULL;
515 int enable_debugging = FALSE;
516 guint32 opt, action = DO_EXEC;
517 MonoGraphOptions mono_graph_options = 0;
518 int mini_verbose = 0;
520 setlocale (LC_ALL, "");
521 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
522 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
524 opt = parse_optimizations (NULL);
526 for (i = 1; i < argc; ++i) {
527 if (argv [i] [0] != '-')
529 if (strcmp (argv [i], "--regression") == 0) {
530 action = DO_REGRESSION;
531 } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
533 } else if (strcmp (argv [i], "--version") == 0 || strcmp (argv [i], "-V") == 0) {
534 g_print ("Mono JIT compiler version %s, (C) 2002, 2003 Ximian, Inc.\n", VERSION);
536 } else if (strcmp (argv [i], "--help") == 0 || strcmp (argv [i], "-h") == 0) {
539 } else if (strncmp (argv [i], "--statfile", 10) == 0) {
540 mini_stats_fd = fopen (argv [++i], "w+");
541 } else if (strncmp (argv [i], "--optimize=", 11) == 0) {
542 opt = parse_optimizations (argv [i] + 11);
543 } else if (strncmp (argv [i], "-O=", 3) == 0) {
544 opt = parse_optimizations (argv [i] + 3);
545 } else if (strcmp (argv [i], "--config") == 0) {
546 config_file = argv [++i];
547 } else if (strcmp (argv [i], "--ncompile") == 0) {
548 count = atoi (argv [++i]);
550 } else if (strcmp (argv [i], "--trace") == 0) {
551 mono_jit_trace_calls = TRUE;
552 } else if (strcmp (argv [i], "--breakonex") == 0) {
553 mono_break_on_exc = TRUE;
554 } else if (strcmp (argv [i], "--break") == 0) {
555 if (!mono_debugger_insert_breakpoint (argv [++i], FALSE))
556 g_error ("Invalid method name '%s'", argv [i]);
557 } else if (strcmp (argv [i], "--print-vtable") == 0) {
558 mono_print_vtable = TRUE;
559 } else if (strcmp (argv [i], "--stats") == 0) {
560 mono_jit_stats.enabled = TRUE;
561 } else if (strcmp (argv [i], "--aot") == 0) {
562 mono_compile_aot = TRUE;
563 } else if (strcmp (argv [i], "--compile-all") == 0) {
565 } else if (strcmp (argv [i], "--profile") == 0) {
566 mono_profiler_load (NULL);
567 } else if (strncmp (argv [i], "--profile=", 10) == 0) {
568 mono_profiler_load (argv [i] + 10);
569 } else if (strcmp (argv [i], "--compile") == 0) {
572 } else if (strncmp (argv [i], "--graph=", 8) == 0) {
573 mono_graph_options = mono_parse_graph_options (argv [i] + 8);
576 } else if (strcmp (argv [i], "--graph") == 0) {
578 mono_graph_options = MONO_GRAPH_CFG;
580 } else if (strcmp (argv [i], "--debug") == 0) {
581 enable_debugging = TRUE;
583 fprintf (stderr, "Unknown command line option: '%s'\n", argv [i]);
593 if (mono_compile_aot || action == DO_EXEC) {
594 g_set_prgname (argv[i]);
597 mono_set_defaults (mini_verbose, opt);
598 domain = mini_init (argv [i]);
602 if (mini_regression_list (mini_verbose, argc -i, argv + i)) {
603 g_print ("Regression ERRORS!\n");
604 mini_cleanup (domain);
607 mini_cleanup (domain);
610 if (argc - i != 1 || mname == NULL) {
611 g_print ("Usage: mini --ncompile num --compile method assembly\n");
612 mini_cleanup (domain);
620 mini_cleanup (domain);
626 if (argc - i != 1 || mname == NULL) {
628 mini_cleanup (domain);
636 mini_cleanup (domain);
643 if (enable_debugging)
644 mono_debug_init (domain, MONO_DEBUG_FORMAT_MONO);
646 assembly = mono_assembly_open (aname, NULL);
648 fprintf (stderr, "cannot open assembly %s\n", aname);
649 mini_cleanup (domain);
653 if (enable_debugging)
654 mono_debug_init_2 (assembly);
656 if (mono_compile_aot || action == DO_EXEC) {
657 mono_config_parse (config_file);
658 //mono_set_rootdir ();
660 main_args.domain = domain;
661 main_args.file = aname;
662 main_args.argc = argc - i;
663 main_args.argv = argv + i;
664 main_args.opts = opt;
666 mono_runtime_exec_managed_code (domain, main_thread_handler, &main_args);
667 mini_cleanup (domain);
668 /* Look up return value from System.Environment.ExitCode */
669 i = mono_environment_exitcode_get ();
671 } else if (action == DO_COMPILE) {
672 compile_all_methods (assembly, mini_verbose);
673 mini_cleanup (domain);
676 desc = mono_method_desc_new (mname, 0);
678 g_print ("Invalid method name %s\n", mname);
679 mini_cleanup (domain);
682 method = mono_method_desc_search_in_image (desc, assembly->image);
684 g_print ("Cannot find method %s\n", mname);
685 mini_cleanup (domain);
689 if (action == DO_DRAW) {
692 switch (mono_graph_options) {
693 case MONO_GRAPH_DTREE:
695 opt |= MONO_OPT_LOOP;
697 case MONO_GRAPH_CFG_CODE:
700 case MONO_GRAPH_CFG_SSA:
703 case MONO_GRAPH_CFG_OPTCODE:
710 cfg = mini_method_compile (method, opt, mono_root_domain, part);
711 if ((mono_graph_options & MONO_GRAPH_CFG_SSA) && !(cfg->comp_done & MONO_COMP_SSA)) {
712 g_warning ("no SSA info available (use -O=deadce)");
715 mono_draw_graph (cfg, mono_graph_options);
716 mono_destroy_compile (cfg);
718 } else if (action == DO_BENCH) {
721 double no_opt_time = 0.0;
722 GTimer *timer = g_timer_new ();
723 fprintf (mini_stats_fd, "$stattitle = \'Compilations times for %s\';\n",
724 mono_method_full_name (method, TRUE));
725 fprintf (mini_stats_fd, "@data = (\n");
726 fprintf (mini_stats_fd, "[");
727 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
732 fprintf (mini_stats_fd, "\"%s\",", n);
734 fprintf (mini_stats_fd, "],\n[");
736 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
740 g_timer_start (timer);
741 for (j = 0; j < count; ++j) {
742 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
743 mono_destroy_compile (cfg);
745 g_timer_stop (timer);
746 elapsed = g_timer_elapsed (timer, NULL);
748 no_opt_time = elapsed;
749 fprintf (mini_stats_fd, "%f, ", elapsed);
751 fprintf (mini_stats_fd, "]");
752 if (no_opt_time > 0.0) {
753 fprintf (mini_stats_fd, ", \n[");
754 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++)
755 fprintf (mini_stats_fd, "%f,", no_opt_time);
756 fprintf (mini_stats_fd, "]");
758 fprintf (mini_stats_fd, ");\n");
760 for (i = 0; i < count; ++i) {
761 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
762 mono_destroy_compile (cfg);
766 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
767 mono_destroy_compile (cfg);
770 mini_cleanup (domain);
775 mono_jit_init (const char *file)
777 return mini_init (file);
781 mono_jit_cleanup (MonoDomain *domain)
783 mini_cleanup (domain);