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>
43 static FILE *mini_stats_fd = NULL;
45 static void mini_usage (void);
47 typedef void (*OptFunc) (const char *p);
49 /* keep in sync with enum in mini.h */
58 {"peephole", "Peephole postpass"},
59 {"branch", "Branch optimizations"},
60 {"inline", "Inline method calls"},
61 {"cfold", "Constant folding"},
62 {"consprop", "Constant propagation"},
63 {"copyprop", "Copy propagation"},
64 {"deadce", "Dead code elimination"},
65 {"linears", "Linear scan global reg allocation"},
66 {"cmov", "Conditional moves"},
67 {"shared", "Emit per-domain code"},
68 {"sched", "Instruction scheduling"},
69 {"instrins", "Intrinsic method implementations"},
70 {"tailc", "Tail recursion and tail calls"},
71 {"loop", "Loop related optimizations"},
72 {"fcmov", "Fast x86 FP compares"}
76 parse_optimizations (const char* p)
78 /* the default value */
79 guint32 opt = MONO_OPT_PEEPHOLE | MONO_OPT_CFOLD /* | MONO_OPT_CONSPROP | MONO_OPT_INLINE*/ | MONO_OPT_BRANCH | /* | MONO_OPT_SAHRED |*/ MONO_OPT_LINEARS;
83 /* call out to cpu detection code here that sets the defaults ... */
84 opt |= mono_arch_cpu_optimizazions ();
95 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
96 n = opt_names [i].name;
98 if (strncmp (p, n, len) == 0) {
107 } else if (*p == '=') {
109 if (opt_names [i].func)
110 opt_names [i].func (p);
111 while (*p && *p++ != ',');
118 if (i == G_N_ELEMENTS (opt_names)) {
119 if (strncmp (p, "all", 3) == 0) {
123 opt = ~(MONO_OPT_SAHRED);
128 fprintf (stderr, "Invalid optimization name `%s'\n", p);
139 MonoGraphOptions value;
142 static const GraphName
144 {"cfg", "Control Flow Graph (CFG)" , MONO_GRAPH_CFG},
145 {"dtree", "Dominator Tree", MONO_GRAPH_DTREE},
146 {"code", "CFG showing code", MONO_GRAPH_CFG_CODE},
147 {"ssa", "CFG showing code after SSA translation", MONO_GRAPH_CFG_SSA},
148 {"optcode", "CFG showing code after IR optimizations", MONO_GRAPH_CFG_OPTCODE}
151 static MonoGraphOptions
152 mono_parse_graph_options (const char* p)
157 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
158 n = graph_names [i].name;
160 if (strncmp (p, n, len) == 0)
161 return graph_names [i].value;
164 fprintf (stderr, "Invalid graph name provided: %s\n", p);
169 mini_parse_default_optimizations (const char* p)
173 opt = parse_optimizations (p);
178 opt_descr (guint32 flags) {
179 GString *str = g_string_new ("");
183 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
184 if (flags & (1 << i)) {
186 g_string_append_c (str, ',');
187 g_string_append (str, opt_names [i].name);
191 return g_string_free (str, FALSE);
201 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE,
202 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
203 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
204 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_CFOLD,
205 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
206 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP
209 typedef int (*TestMethod) (void);
213 domain_dump_native_code (MonoDomain *domain) {
214 // need to poke into the domain, move to metadata/domain.c
215 // need to empty jit_info_table and code_mp
220 mini_regression (MonoImage *image, int verbose, int *total_run) {
221 guint32 i, opt, opt_flags;
225 int result, expected, failed, cfailed, run, code_size, total;
227 GTimer *timer = g_timer_new ();
230 fprintf (mini_stats_fd, "$stattitle = \'Mono Benchmark Results (various optimizations)\';\n");
232 fprintf (mini_stats_fd, "$graph->set_legend(qw(");
233 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); opt++) {
234 opt_flags = opt_sets [opt];
235 n = opt_descr (opt_flags);
239 fprintf (mini_stats_fd, " ");
240 fprintf (mini_stats_fd, "%s", n);
244 fprintf (mini_stats_fd, "));\n");
246 fprintf (mini_stats_fd, "@data = (\n");
247 fprintf (mini_stats_fd, "[");
250 /* load the metadata */
251 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
252 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
253 mono_class_init (method->klass);
255 if (!strncmp (method->name, "test_", 5) && mini_stats_fd) {
256 fprintf (mini_stats_fd, "\"%s\",", method->name);
260 fprintf (mini_stats_fd, "],\n");
265 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
266 double elapsed, comp_time, start_time;
267 opt_flags = opt_sets [opt];
268 mini_set_defaults (verbose, opt_flags);
269 n = opt_descr (opt_flags);
270 g_print ("Test run: image=%s, opts=%s\n", image->name, n);
272 cfailed = failed = run = code_size = 0;
273 comp_time = elapsed = 0.0;
275 /* fixme: ugly hack - delete all previously compiled methods */
276 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
277 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
281 g_timer_start (timer);
283 fprintf (mini_stats_fd, "[");
284 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
285 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
286 if (strncmp (method->name, "test_", 5) == 0) {
287 expected = atoi (method->name + 5);
289 start_time = g_timer_elapsed (timer, NULL);
290 comp_time -= start_time;
291 cfg = mini_method_compile (method, opt_flags, mono_root_domain, 0);
292 comp_time += g_timer_elapsed (timer, NULL);
295 g_print ("Running '%s' ...\n", method->name);
296 #ifdef MONO_USE_AOT_COMPILER
297 if (!(func = mono_aot_get_method (method)))
299 func = (TestMethod)cfg->native_code;
301 if (result != expected) {
304 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
306 code_size += cfg->code_len;
307 mono_destroy_compile (cfg);
309 if (mono_trace_coverage) {
310 MonoCoverageInfo *cov = mono_get_coverage_info (method);
314 printf ("COVERAGE INFO %s\n", mono_method_full_name (method, TRUE));
316 for (k = 0; k < cov->entries; k++) {
317 printf (" BBLOCK %3d %d\n", cov->data [k].iloffset, cov->data [k].count);
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: mini [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"
481 " --statfile FILE Sets the stat file to FILE\n"
482 " --aot Compiles the assembly to native code\n"
483 " --coverage Performs coverage analysis\n"
484 " --profile Runs in profiling mode\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 " --optimize=OPT Turns on a specific optimization:\n");
499 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
500 fprintf (stderr, " %-10s %s\n", opt_names [i].name, opt_names [i].desc);
504 mini_main (int argc, char* argv[]) {
505 MainThreadArgs main_args;
506 MonoAssembly *assembly;
507 MonoMethodDesc *desc;
511 const char* aname, *mname = NULL;
512 char *config_file = NULL;
514 guint32 opt, action = DO_EXEC;
515 MonoGraphOptions mono_graph_options = 0;
516 int mini_verbose = 0;
518 opt = parse_optimizations (NULL);
520 for (i = 1; i < argc; ++i) {
521 if (argv [i] [0] != '-')
523 if (strcmp (argv [i], "--regression") == 0) {
524 action = DO_REGRESSION;
525 } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
527 } else if (strcmp (argv [i], "--help") == 0 || strcmp (argv [i], "-h") == 0) {
530 } else if (strncmp (argv [i], "--statfile", 10) == 0) {
531 mini_stats_fd = fopen (argv [++i], "w+");
532 } else if (strncmp (argv [i], "--optimize=", 11) == 0) {
533 opt = parse_optimizations (argv [i] + 11);
534 } else if (strncmp (argv [i], "-O=", 3) == 0) {
535 opt = parse_optimizations (argv [i] + 3);
536 } else if (strcmp (argv [i], "--config") == 0) {
537 config_file = argv [++i];
538 } else if (strcmp (argv [i], "--ncompile") == 0) {
539 count = atoi (argv [++i]);
541 } else if (strcmp (argv [i], "--trace") == 0) {
542 mono_jit_trace_calls = TRUE;
543 } else if (strcmp (argv [i], "--breakonex") == 0) {
544 mono_break_on_exc = TRUE;
545 } else if (strcmp (argv [i], "--break") == 0) {
546 if (!mono_insert_breakpoint (argv [++i], FALSE))
547 g_error ("Invalid method name '%s'", argv [i]);
548 } else if (strcmp (argv [i], "--print-vtable") == 0) {
549 mono_print_vtable = TRUE;
550 } else if (strcmp (argv [i], "--stats") == 0) {
551 mono_jit_stats.enabled = TRUE;
552 } else if (strcmp (argv [i], "--aot") == 0) {
553 mono_compile_aot = TRUE;
554 } else if (strcmp (argv [i], "--coverage") == 0) {
555 mono_trace_coverage = TRUE;
556 } else if (strcmp (argv [i], "--compile-all") == 0) {
558 } else if (strcmp (argv [i], "--profile") == 0) {
559 mono_jit_profile = TRUE;
560 mono_profiler_install_simple ();
561 } else if (strcmp (argv [i], "--compile") == 0) {
564 } else if (strncmp (argv [i], "--graph=", 8) == 0) {
565 mono_graph_options = mono_parse_graph_options (argv [i] + 8);
568 } else if (strcmp (argv [i], "--graph") == 0) {
570 mono_graph_options = MONO_GRAPH_CFG;
573 fprintf (stderr, "Unknown command line option: %s\n", argv [i]);
578 mini_set_defaults (mini_verbose, opt);
579 domain = mini_init (argv [0]);
583 if (mini_regression_list (mini_verbose, argc -i, argv + i)) {
584 g_print ("Regression ERRORS!\n");
585 mini_cleanup (domain);
588 mini_cleanup (domain);
591 if (argc - i != 1 || mname == NULL) {
592 g_print ("Usage: mini --ncompile num --compile method assembly\n");
593 mini_cleanup (domain);
600 fprintf (stderr, "Missing assembly name in --compile-all");
601 mini_cleanup (domain);
607 if (argc - i != 1 || mname == NULL) {
608 fprintf (stderr, "Usage: mini --graph[=TYPE] method assembly\n");
609 mini_cleanup (domain);
617 mini_cleanup (domain);
624 assembly = mono_assembly_open (aname, NULL);
626 fprintf (stderr, "cannot open assembly %s\n", aname);
627 mini_cleanup (domain);
631 if (mono_compile_aot || action == DO_EXEC) {
632 g_set_prgname (aname);
633 mono_config_parse (config_file);
634 //mono_set_rootdir ();
636 main_args.domain = domain;
637 main_args.file = aname;
638 main_args.argc = argc - i;
639 main_args.argv = argv + i;
640 main_args.opts = opt;
642 mono_runtime_exec_managed_code (domain, main_thread_handler, &main_args);
643 mini_cleanup (domain);
644 /* Look up return value from System.Environment.ExitCode */
645 i = mono_environment_exitcode_get ();
647 } else if (action == DO_COMPILE) {
648 compile_all_methods (assembly, mini_verbose);
649 mini_cleanup (domain);
652 desc = mono_method_desc_new (mname, 0);
654 g_print ("Invalid method name %s\n", mname);
655 mini_cleanup (domain);
658 method = mono_method_desc_search_in_image (desc, assembly->image);
660 g_print ("Cannot find method %s\n", mname);
661 mini_cleanup (domain);
665 if (action == DO_DRAW) {
668 switch (mono_graph_options) {
669 case MONO_GRAPH_DTREE:
671 opt |= MONO_OPT_LOOP;
673 case MONO_GRAPH_CFG_CODE:
676 case MONO_GRAPH_CFG_SSA:
679 case MONO_GRAPH_CFG_OPTCODE:
686 cfg = mini_method_compile (method, opt, mono_root_domain, part);
687 if ((mono_graph_options & MONO_GRAPH_CFG_SSA) && !(cfg->comp_done & MONO_COMP_SSA)) {
688 g_warning ("no SSA info available (use -O=deadce)");
691 mono_draw_graph (cfg, mono_graph_options);
692 mono_destroy_compile (cfg);
694 } else if (action == DO_BENCH) {
697 double no_opt_time = 0.0;
698 GTimer *timer = g_timer_new ();
699 fprintf (mini_stats_fd, "$stattitle = \'Compilations times for %s\';\n",
700 mono_method_full_name (method, TRUE));
701 fprintf (mini_stats_fd, "@data = (\n");
702 fprintf (mini_stats_fd, "[");
703 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
708 fprintf (mini_stats_fd, "\"%s\",", n);
710 fprintf (mini_stats_fd, "],\n[");
712 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
716 g_timer_start (timer);
717 for (j = 0; j < count; ++j) {
718 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
719 mono_destroy_compile (cfg);
721 g_timer_stop (timer);
722 elapsed = g_timer_elapsed (timer, NULL);
724 no_opt_time = elapsed;
725 fprintf (mini_stats_fd, "%f, ", elapsed);
727 fprintf (mini_stats_fd, "]");
728 if (no_opt_time > 0.0) {
729 fprintf (mini_stats_fd, ", \n[");
730 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++)
731 fprintf (mini_stats_fd, "%f,", no_opt_time);
732 fprintf (mini_stats_fd, "]");
734 fprintf (mini_stats_fd, ");\n");
736 for (i = 0; i < count; ++i) {
737 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
738 mono_destroy_compile (cfg);
742 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
743 mono_destroy_compile (cfg);
746 mini_cleanup (domain);