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>
44 static FILE *mini_stats_fd = NULL;
46 static void mini_usage (void);
48 typedef void (*OptFunc) (const char *p);
50 /* keep in sync with enum in mini.h */
59 {"peephole", "Peephole postpass"},
60 {"branch", "Branch optimizations"},
61 {"inline", "Inline method calls"},
62 {"cfold", "Constant folding"},
63 {"consprop", "Constant propagation"},
64 {"copyprop", "Copy propagation"},
65 {"deadce", "Dead code elimination"},
66 {"linears", "Linear scan global reg allocation"},
67 {"cmov", "Conditional moves"},
68 {"shared", "Emit per-domain code"},
69 {"sched", "Instruction scheduling"},
70 {"instrins", "Intrinsic method implementations"},
71 {"tailc", "Tail recursion and tail calls"},
72 {"loop", "Loop related optimizations"},
73 {"fcmov", "Fast x86 FP compares"}
77 parse_optimizations (const char* p)
79 /* the default value */
80 guint32 opt = MONO_OPT_PEEPHOLE | MONO_OPT_CFOLD /* | MONO_OPT_CONSPROP | MONO_OPT_INLINE*/ | MONO_OPT_BRANCH | /* | MONO_OPT_SAHRED |*/ MONO_OPT_LINEARS;
84 /* call out to cpu detection code here that sets the defaults ... */
85 opt |= mono_arch_cpu_optimizazions ();
96 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
97 n = opt_names [i].name;
99 if (strncmp (p, n, len) == 0) {
108 } else if (*p == '=') {
110 if (opt_names [i].func)
111 opt_names [i].func (p);
112 while (*p && *p++ != ',');
119 if (i == G_N_ELEMENTS (opt_names)) {
120 if (strncmp (p, "all", 3) == 0) {
124 opt = ~(MONO_OPT_SAHRED);
129 fprintf (stderr, "Invalid optimization name `%s'\n", p);
140 MonoGraphOptions value;
143 static const GraphName
145 {"cfg", "Control Flow Graph (CFG)" , MONO_GRAPH_CFG},
146 {"dtree", "Dominator Tree", MONO_GRAPH_DTREE},
147 {"code", "CFG showing code", MONO_GRAPH_CFG_CODE},
148 {"ssa", "CFG showing code after SSA translation", MONO_GRAPH_CFG_SSA},
149 {"optcode", "CFG showing code after IR optimizations", MONO_GRAPH_CFG_OPTCODE}
152 static MonoGraphOptions
153 mono_parse_graph_options (const char* p)
158 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
159 n = graph_names [i].name;
161 if (strncmp (p, n, len) == 0)
162 return graph_names [i].value;
165 fprintf (stderr, "Invalid graph name provided: %s\n", p);
170 mini_parse_default_optimizations (const char* p)
174 opt = parse_optimizations (p);
179 opt_descr (guint32 flags) {
180 GString *str = g_string_new ("");
184 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
185 if (flags & (1 << i)) {
187 g_string_append_c (str, ',');
188 g_string_append (str, opt_names [i].name);
192 return g_string_free (str, FALSE);
202 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE,
203 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
204 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
205 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_CFOLD,
206 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
207 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP
210 typedef int (*TestMethod) (void);
214 domain_dump_native_code (MonoDomain *domain) {
215 // need to poke into the domain, move to metadata/domain.c
216 // need to empty jit_info_table and code_mp
221 mini_regression (MonoImage *image, int verbose, int *total_run) {
222 guint32 i, opt, opt_flags;
226 int result, expected, failed, cfailed, run, code_size, total;
228 GTimer *timer = g_timer_new ();
231 fprintf (mini_stats_fd, "$stattitle = \'Mono Benchmark Results (various optimizations)\';\n");
233 fprintf (mini_stats_fd, "$graph->set_legend(qw(");
234 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); opt++) {
235 opt_flags = opt_sets [opt];
236 n = opt_descr (opt_flags);
240 fprintf (mini_stats_fd, " ");
241 fprintf (mini_stats_fd, "%s", n);
245 fprintf (mini_stats_fd, "));\n");
247 fprintf (mini_stats_fd, "@data = (\n");
248 fprintf (mini_stats_fd, "[");
251 /* load the metadata */
252 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
253 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
254 mono_class_init (method->klass);
256 if (!strncmp (method->name, "test_", 5) && mini_stats_fd) {
257 fprintf (mini_stats_fd, "\"%s\",", method->name);
261 fprintf (mini_stats_fd, "],\n");
266 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
267 double elapsed, comp_time, start_time;
268 opt_flags = opt_sets [opt];
269 mini_set_defaults (verbose, opt_flags);
270 n = opt_descr (opt_flags);
271 g_print ("Test run: image=%s, opts=%s\n", image->name, n);
273 cfailed = failed = run = code_size = 0;
274 comp_time = elapsed = 0.0;
276 /* fixme: ugly hack - delete all previously compiled methods */
277 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
278 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
282 g_timer_start (timer);
284 fprintf (mini_stats_fd, "[");
285 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
286 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
287 if (strncmp (method->name, "test_", 5) == 0) {
288 expected = atoi (method->name + 5);
290 start_time = g_timer_elapsed (timer, NULL);
291 comp_time -= start_time;
292 cfg = mini_method_compile (method, opt_flags, mono_root_domain, 0);
293 comp_time += g_timer_elapsed (timer, NULL);
296 g_print ("Running '%s' ...\n", method->name);
297 #ifdef MONO_USE_AOT_COMPILER
298 if (!(func = mono_aot_get_method (method)))
300 func = (TestMethod)cfg->native_code;
302 if (result != expected) {
305 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
307 code_size += cfg->code_len;
308 mono_destroy_compile (cfg);
310 if (mono_trace_coverage) {
311 MonoCoverageInfo *cov = mono_get_coverage_info (method);
315 printf ("COVERAGE INFO %s\n", mono_method_full_name (method, TRUE));
317 for (k = 0; k < cov->entries; k++) {
318 printf (" BBLOCK %3d %d\n", cov->data [k].iloffset, cov->data [k].count);
326 g_print ("Test '%s' failed compilation.\n", method->name);
329 fprintf (mini_stats_fd, "%f, ",
330 g_timer_elapsed (timer, NULL) - start_time);
334 fprintf (mini_stats_fd, "],\n");
335 g_timer_stop (timer);
336 elapsed = g_timer_elapsed (timer, NULL);
337 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
338 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
339 g_print ("Elapsed time: %f secs (%f, %f), Code size: %d\n\n", elapsed,
340 elapsed - comp_time, comp_time, code_size);
341 total += failed + cfailed;
346 fprintf (mini_stats_fd, ");\n");
347 fflush (mini_stats_fd);
350 g_timer_destroy (timer);
355 mini_regression_list (int verbose, int count, char *images [])
357 int i, total, total_run, run;
360 total_run = total = 0;
361 for (i = 0; i < count; ++i) {
362 ass = mono_assembly_open (images [i], NULL);
364 g_warning ("failed to load assembly: %s", images [i]);
367 total += mini_regression (ass->image, verbose, &run);
369 mono_assembly_close (ass);
371 g_print ("Overall results: tests: %d, failed: %d, opt combinations: %d (pass: %.2f%%)\n",
372 total_run, total, G_N_ELEMENTS (opt_sets), 100.0*(total_run-total)/total_run);
385 compile_all_methods (MonoAssembly *ass, int verbose)
387 MonoImage *image = ass->image;
391 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
392 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
393 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
395 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
400 char * desc = mono_method_full_name (method, TRUE);
401 g_print ("Compiling %d %s\n", count, desc + 3);
404 mono_compile_method (method);
411 * @assembly: reference to an assembly
412 * @argc: argument count
413 * @argv: argument vector
415 * Start execution of a program.
418 mono_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
420 MonoImage *image = assembly->image;
422 guint32 entry = mono_image_get_entry_point (image);
425 g_print ("Assembly '%s' doesn't have an entry point.\n", image->name);
426 /* FIXME: remove this silly requirement. */
427 mono_environment_exitcode_set (1);
431 method = mono_get_method (image, entry, NULL);
433 return mono_runtime_run_main (method, argc, argv, NULL);
445 static void main_thread_handler (gpointer user_data)
447 MainThreadArgs *main_args = user_data;
448 MonoAssembly *assembly;
450 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
452 fprintf (stderr, "Can not open image %s\n", main_args->file);
456 if (mono_compile_aot) {
457 int res = mono_compile_assembly (assembly, main_args->opts);
458 printf ("AOT RESULT %d\n", res);
460 mono_jit_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
470 "Usage is: mini [options] assembly\n\n"
471 "Runtime and JIT debugging:\n"
472 " --compile METHOD Just compile METHOD in assembly\n"
473 " --ncompile N Number of times to compile METHOD (default: 1)\n"
474 " --regression Runs the regression test contained in the assembly\n"
475 " --print-vtable Print the vtable of all used classes\n"
476 " --trace Enable tracing\n"
477 " --compile-all Compiles all the methods in the assembly\n"
478 " --breakonex Inserts a breakpoint on exceptions\n"
479 " --break METHOD Inserts a breakpoint at METHOD entry\n"
480 " --debug Enable debugging support\n"
483 " --statfile FILE Sets the stat file to FILE\n"
484 " --aot Compiles the assembly to native code\n"
485 " --coverage Performs coverage analysis\n"
486 " --profile Runs in profiling mode\n"
487 " --graph[=TYPE] METHOD Draws a graph of the specified method:\n");
489 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
490 fprintf (stderr, " %-10s %s\n", graph_names [i].name, graph_names [i].desc);
496 " --config FILE Loads FILE as the Mono config\n"
497 " --verbose, -v Increases the verbosity level\n"
498 " --help, -h Show usage information\n"
499 " --optimize=OPT Turns on a specific optimization:\n");
501 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
502 fprintf (stderr, " %-10s %s\n", opt_names [i].name, opt_names [i].desc);
506 mini_main (int argc, char* argv[]) {
507 MainThreadArgs main_args;
508 MonoAssembly *assembly;
509 MonoMethodDesc *desc;
513 const char* aname, *mname = NULL;
514 char *config_file = NULL;
516 int enable_debugging = FALSE;
517 guint32 opt, action = DO_EXEC;
518 MonoGraphOptions mono_graph_options = 0;
519 int mini_verbose = 0;
521 opt = parse_optimizations (NULL);
523 for (i = 1; i < argc; ++i) {
524 if (argv [i] [0] != '-')
526 if (strcmp (argv [i], "--regression") == 0) {
527 action = DO_REGRESSION;
528 } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
530 } else if (strcmp (argv [i], "--help") == 0 || strcmp (argv [i], "-h") == 0) {
533 } else if (strncmp (argv [i], "--statfile", 10) == 0) {
534 mini_stats_fd = fopen (argv [++i], "w+");
535 } else if (strncmp (argv [i], "--optimize=", 11) == 0) {
536 opt = parse_optimizations (argv [i] + 11);
537 } else if (strncmp (argv [i], "-O=", 3) == 0) {
538 opt = parse_optimizations (argv [i] + 3);
539 } else if (strcmp (argv [i], "--config") == 0) {
540 config_file = argv [++i];
541 } else if (strcmp (argv [i], "--ncompile") == 0) {
542 count = atoi (argv [++i]);
544 } else if (strcmp (argv [i], "--trace") == 0) {
545 mono_jit_trace_calls = TRUE;
546 } else if (strcmp (argv [i], "--breakonex") == 0) {
547 mono_break_on_exc = TRUE;
548 } else if (strcmp (argv [i], "--break") == 0) {
549 if (!mono_debugger_insert_breakpoint (argv [++i], FALSE))
550 g_error ("Invalid method name '%s'", argv [i]);
551 } else if (strcmp (argv [i], "--print-vtable") == 0) {
552 mono_print_vtable = TRUE;
553 } else if (strcmp (argv [i], "--stats") == 0) {
554 mono_jit_stats.enabled = TRUE;
555 } else if (strcmp (argv [i], "--aot") == 0) {
556 mono_compile_aot = TRUE;
557 } else if (strcmp (argv [i], "--coverage") == 0) {
558 mono_trace_coverage = TRUE;
559 } else if (strcmp (argv [i], "--compile-all") == 0) {
561 } else if (strcmp (argv [i], "--profile") == 0) {
562 mono_jit_profile = TRUE;
563 mono_profiler_install_simple ();
564 } else if (strcmp (argv [i], "--compile") == 0) {
567 } else if (strncmp (argv [i], "--graph=", 8) == 0) {
568 mono_graph_options = mono_parse_graph_options (argv [i] + 8);
571 } else if (strcmp (argv [i], "--graph") == 0) {
573 mono_graph_options = MONO_GRAPH_CFG;
575 } else if (strcmp (argv [i], "--debug") == 0) {
576 enable_debugging = TRUE;
578 fprintf (stderr, "Unknown command line option: %s\n", argv [i]);
583 mini_set_defaults (mini_verbose, opt);
584 domain = mini_init (argv [0]);
588 if (mini_regression_list (mini_verbose, argc -i, argv + i)) {
589 g_print ("Regression ERRORS!\n");
590 mini_cleanup (domain);
593 mini_cleanup (domain);
596 if (argc - i != 1 || mname == NULL) {
597 g_print ("Usage: mini --ncompile num --compile method assembly\n");
598 mini_cleanup (domain);
605 fprintf (stderr, "Missing assembly name in --compile-all");
606 mini_cleanup (domain);
612 if (argc - i != 1 || mname == NULL) {
613 fprintf (stderr, "Usage: mini --graph[=TYPE] method assembly\n");
614 mini_cleanup (domain);
622 mini_cleanup (domain);
629 if (enable_debugging)
630 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
632 assembly = mono_assembly_open (aname, NULL);
634 fprintf (stderr, "cannot open assembly %s\n", aname);
635 mini_cleanup (domain);
639 if (enable_debugging)
640 mono_debug_init_2 (assembly);
642 if (mono_compile_aot || action == DO_EXEC) {
643 g_set_prgname (aname);
644 mono_config_parse (config_file);
645 //mono_set_rootdir ();
647 main_args.domain = domain;
648 main_args.file = aname;
649 main_args.argc = argc - i;
650 main_args.argv = argv + i;
651 main_args.opts = opt;
653 mono_runtime_exec_managed_code (domain, main_thread_handler, &main_args);
654 mini_cleanup (domain);
655 /* Look up return value from System.Environment.ExitCode */
656 i = mono_environment_exitcode_get ();
658 } else if (action == DO_COMPILE) {
659 compile_all_methods (assembly, mini_verbose);
660 mini_cleanup (domain);
663 desc = mono_method_desc_new (mname, 0);
665 g_print ("Invalid method name %s\n", mname);
666 mini_cleanup (domain);
669 method = mono_method_desc_search_in_image (desc, assembly->image);
671 g_print ("Cannot find method %s\n", mname);
672 mini_cleanup (domain);
676 if (action == DO_DRAW) {
679 switch (mono_graph_options) {
680 case MONO_GRAPH_DTREE:
682 opt |= MONO_OPT_LOOP;
684 case MONO_GRAPH_CFG_CODE:
687 case MONO_GRAPH_CFG_SSA:
690 case MONO_GRAPH_CFG_OPTCODE:
697 cfg = mini_method_compile (method, opt, mono_root_domain, part);
698 if ((mono_graph_options & MONO_GRAPH_CFG_SSA) && !(cfg->comp_done & MONO_COMP_SSA)) {
699 g_warning ("no SSA info available (use -O=deadce)");
702 mono_draw_graph (cfg, mono_graph_options);
703 mono_destroy_compile (cfg);
705 } else if (action == DO_BENCH) {
708 double no_opt_time = 0.0;
709 GTimer *timer = g_timer_new ();
710 fprintf (mini_stats_fd, "$stattitle = \'Compilations times for %s\';\n",
711 mono_method_full_name (method, TRUE));
712 fprintf (mini_stats_fd, "@data = (\n");
713 fprintf (mini_stats_fd, "[");
714 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
719 fprintf (mini_stats_fd, "\"%s\",", n);
721 fprintf (mini_stats_fd, "],\n[");
723 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
727 g_timer_start (timer);
728 for (j = 0; j < count; ++j) {
729 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
730 mono_destroy_compile (cfg);
732 g_timer_stop (timer);
733 elapsed = g_timer_elapsed (timer, NULL);
735 no_opt_time = elapsed;
736 fprintf (mini_stats_fd, "%f, ", elapsed);
738 fprintf (mini_stats_fd, "]");
739 if (no_opt_time > 0.0) {
740 fprintf (mini_stats_fd, ", \n[");
741 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++)
742 fprintf (mini_stats_fd, "%f,", no_opt_time);
743 fprintf (mini_stats_fd, "]");
745 fprintf (mini_stats_fd, ");\n");
747 for (i = 0; i < count; ++i) {
748 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
749 mono_destroy_compile (cfg);
753 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
754 mono_destroy_compile (cfg);
757 mini_cleanup (domain);