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;
85 /* call out to cpu detection code here that sets the defaults ... */
86 opt |= mono_arch_cpu_optimizazions (&exclude);
98 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
99 n = opt_names [i].name;
101 if (strncmp (p, n, len) == 0) {
110 } else if (*p == '=') {
112 if (opt_names [i].func)
113 opt_names [i].func (p);
114 while (*p && *p++ != ',');
121 if (i == G_N_ELEMENTS (opt_names)) {
122 if (strncmp (p, "all", 3) == 0) {
126 opt = ~(MONO_OPT_SAHRED | exclude);
131 fprintf (stderr, "Invalid optimization name `%s'\n", p);
142 MonoGraphOptions value;
145 static const GraphName
147 {"cfg", "Control Flow Graph (CFG)" , MONO_GRAPH_CFG},
148 {"dtree", "Dominator Tree", MONO_GRAPH_DTREE},
149 {"code", "CFG showing code", MONO_GRAPH_CFG_CODE},
150 {"ssa", "CFG showing code after SSA translation", MONO_GRAPH_CFG_SSA},
151 {"optcode", "CFG showing code after IR optimizations", MONO_GRAPH_CFG_OPTCODE}
154 static MonoGraphOptions
155 mono_parse_graph_options (const char* p)
160 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
161 n = graph_names [i].name;
163 if (strncmp (p, n, len) == 0)
164 return graph_names [i].value;
167 fprintf (stderr, "Invalid graph name provided: %s\n", p);
172 mono_parse_default_optimizations (const char* p)
176 opt = parse_optimizations (p);
181 opt_descr (guint32 flags) {
182 GString *str = g_string_new ("");
186 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
187 if (flags & (1 << i)) {
189 g_string_append_c (str, ',');
190 g_string_append (str, opt_names [i].name);
194 return g_string_free (str, FALSE);
204 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE,
205 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
206 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
207 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_CFOLD,
208 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
209 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE
212 typedef int (*TestMethod) (void);
216 domain_dump_native_code (MonoDomain *domain) {
217 // need to poke into the domain, move to metadata/domain.c
218 // need to empty jit_info_table and code_mp
223 mini_regression (MonoImage *image, int verbose, int *total_run) {
224 guint32 i, opt, opt_flags;
228 int result, expected, failed, cfailed, run, code_size, total;
230 GTimer *timer = g_timer_new ();
233 fprintf (mini_stats_fd, "$stattitle = \'Mono Benchmark Results (various optimizations)\';\n");
235 fprintf (mini_stats_fd, "$graph->set_legend(qw(");
236 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); opt++) {
237 opt_flags = opt_sets [opt];
238 n = opt_descr (opt_flags);
242 fprintf (mini_stats_fd, " ");
243 fprintf (mini_stats_fd, "%s", n);
247 fprintf (mini_stats_fd, "));\n");
249 fprintf (mini_stats_fd, "@data = (\n");
250 fprintf (mini_stats_fd, "[");
253 /* load the metadata */
254 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
255 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
256 mono_class_init (method->klass);
258 if (!strncmp (method->name, "test_", 5) && mini_stats_fd) {
259 fprintf (mini_stats_fd, "\"%s\",", method->name);
263 fprintf (mini_stats_fd, "],\n");
268 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
269 double elapsed, comp_time, start_time;
270 opt_flags = opt_sets [opt];
271 mini_set_defaults (verbose, opt_flags);
272 n = opt_descr (opt_flags);
273 g_print ("Test run: image=%s, opts=%s\n", image->name, n);
275 cfailed = failed = run = code_size = 0;
276 comp_time = elapsed = 0.0;
278 /* fixme: ugly hack - delete all previously compiled methods */
279 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
280 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
284 g_timer_start (timer);
286 fprintf (mini_stats_fd, "[");
287 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
288 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
289 if (strncmp (method->name, "test_", 5) == 0) {
290 expected = atoi (method->name + 5);
292 start_time = g_timer_elapsed (timer, NULL);
293 comp_time -= start_time;
294 cfg = mini_method_compile (method, opt_flags, mono_root_domain, 0);
295 comp_time += g_timer_elapsed (timer, NULL);
298 g_print ("Running '%s' ...\n", method->name);
299 #ifdef MONO_USE_AOT_COMPILER
300 if (!(func = mono_aot_get_method (method)))
302 func = (TestMethod)cfg->native_code;
304 if (result != expected) {
307 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
309 code_size += cfg->code_len;
310 mono_destroy_compile (cfg);
312 if (mono_trace_coverage) {
313 MonoCoverageInfo *cov = mono_get_coverage_info (method);
317 printf ("COVERAGE INFO %s\n", mono_method_full_name (method, TRUE));
319 for (k = 0; k < cov->entries; k++) {
320 printf (" BBLOCK %3d %d\n", cov->data [k].iloffset, cov->data [k].count);
328 g_print ("Test '%s' failed compilation.\n", method->name);
331 fprintf (mini_stats_fd, "%f, ",
332 g_timer_elapsed (timer, NULL) - start_time);
336 fprintf (mini_stats_fd, "],\n");
337 g_timer_stop (timer);
338 elapsed = g_timer_elapsed (timer, NULL);
339 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
340 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
341 g_print ("Elapsed time: %f secs (%f, %f), Code size: %d\n\n", elapsed,
342 elapsed - comp_time, comp_time, code_size);
343 total += failed + cfailed;
348 fprintf (mini_stats_fd, ");\n");
349 fflush (mini_stats_fd);
352 g_timer_destroy (timer);
357 mini_regression_list (int verbose, int count, char *images [])
359 int i, total, total_run, run;
362 total_run = total = 0;
363 for (i = 0; i < count; ++i) {
364 ass = mono_assembly_open (images [i], NULL);
366 g_warning ("failed to load assembly: %s", images [i]);
369 total += mini_regression (ass->image, verbose, &run);
371 mono_assembly_close (ass);
373 g_print ("Overall results: tests: %d, failed: %d, opt combinations: %d (pass: %.2f%%)\n",
374 total_run, total, G_N_ELEMENTS (opt_sets), 100.0*(total_run-total)/total_run);
387 compile_all_methods (MonoAssembly *ass, int verbose)
389 MonoImage *image = ass->image;
393 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
394 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
395 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
397 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
402 char * desc = mono_method_full_name (method, TRUE);
403 g_print ("Compiling %d %s\n", count, desc + 3);
406 mono_compile_method (method);
413 * @assembly: reference to an assembly
414 * @argc: argument count
415 * @argv: argument vector
417 * Start execution of a program.
420 mono_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
422 MonoImage *image = assembly->image;
424 guint32 entry = mono_image_get_entry_point (image);
427 g_print ("Assembly '%s' doesn't have an entry point.\n", image->name);
428 /* FIXME: remove this silly requirement. */
429 mono_environment_exitcode_set (1);
433 method = mono_get_method (image, entry, NULL);
435 return mono_runtime_run_main (method, argc, argv, NULL);
447 static void main_thread_handler (gpointer user_data)
449 MainThreadArgs *main_args = user_data;
450 MonoAssembly *assembly;
452 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
454 fprintf (stderr, "Can not open image %s\n", main_args->file);
458 if (mono_compile_aot) {
459 int res = mono_compile_assembly (assembly, main_args->opts);
460 printf ("AOT RESULT %d\n", res);
462 mono_jit_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
472 "Usage is: mono [options] assembly\n\n"
473 "Runtime and JIT debugging:\n"
474 " --compile METHOD Just compile METHOD in assembly\n"
475 " --ncompile N Number of times to compile METHOD (default: 1)\n"
476 " --regression Runs the regression test contained in the assembly\n"
477 " --print-vtable Print the vtable of all used classes\n"
478 " --trace Enable tracing\n"
479 " --compile-all Compiles all the methods in the assembly\n"
480 " --breakonex Inserts a breakpoint on exceptions\n"
481 " --break METHOD Inserts a breakpoint at METHOD entry\n"
482 " --debug Enable debugging support\n"
485 " --statfile FILE Sets the stat file to FILE\n"
486 " --aot Compiles the assembly to native code\n"
487 " --coverage Performs coverage analysis\n"
488 " --profile Runs in profiling mode\n"
489 " --graph[=TYPE] METHOD Draws a graph of the specified method:\n");
491 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
492 fprintf (stderr, " %-10s %s\n", graph_names [i].name, graph_names [i].desc);
498 " --config FILE Loads FILE as the Mono config\n"
499 " --verbose, -v Increases the verbosity level\n"
500 " --help, -h Show usage information\n"
501 " --version, -V Show version information\n"
502 " --optimize=OPT Turns on a specific optimization:\n");
504 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
505 fprintf (stderr, " %-10s %s\n", opt_names [i].name, opt_names [i].desc);
509 mono_main (int argc, char* argv[]) {
510 MainThreadArgs main_args;
511 MonoAssembly *assembly;
512 MonoMethodDesc *desc;
516 const char* aname, *mname = NULL;
517 char *config_file = NULL;
519 int enable_debugging = FALSE;
520 guint32 opt, action = DO_EXEC;
521 MonoGraphOptions mono_graph_options = 0;
522 int mini_verbose = 0;
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], "--coverage") == 0) {
564 mono_trace_coverage = TRUE;
565 } else if (strcmp (argv [i], "--compile-all") == 0) {
567 } else if (strcmp (argv [i], "--profile") == 0) {
568 mono_jit_profile = TRUE;
569 mono_profiler_install_simple ();
570 } else if (strcmp (argv [i], "--compile") == 0) {
573 } else if (strncmp (argv [i], "--graph=", 8) == 0) {
574 mono_graph_options = mono_parse_graph_options (argv [i] + 8);
577 } else if (strcmp (argv [i], "--graph") == 0) {
579 mono_graph_options = MONO_GRAPH_CFG;
581 } else if (strcmp (argv [i], "--debug") == 0) {
582 enable_debugging = TRUE;
584 fprintf (stderr, "Unknown command line option: %s\n", argv [i]);
594 mini_set_defaults (mini_verbose, opt);
595 domain = mini_init (argv [i]);
599 if (mini_regression_list (mini_verbose, argc -i, argv + i)) {
600 g_print ("Regression ERRORS!\n");
601 mini_cleanup (domain);
604 mini_cleanup (domain);
607 if (argc - i != 1 || mname == NULL) {
608 g_print ("Usage: mini --ncompile num --compile method assembly\n");
609 mini_cleanup (domain);
617 mini_cleanup (domain);
623 if (argc - i != 1 || mname == NULL) {
625 mini_cleanup (domain);
633 mini_cleanup (domain);
640 if (enable_debugging)
641 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
643 assembly = mono_assembly_open (aname, NULL);
645 fprintf (stderr, "cannot open assembly %s\n", aname);
646 mini_cleanup (domain);
650 if (enable_debugging)
651 mono_debug_init_2 (assembly);
653 if (mono_compile_aot || action == DO_EXEC) {
654 g_set_prgname (aname);
655 mono_config_parse (config_file);
656 //mono_set_rootdir ();
658 main_args.domain = domain;
659 main_args.file = aname;
660 main_args.argc = argc - i;
661 main_args.argv = argv + i;
662 main_args.opts = opt;
664 mono_runtime_exec_managed_code (domain, main_thread_handler, &main_args);
665 mini_cleanup (domain);
666 /* Look up return value from System.Environment.ExitCode */
667 i = mono_environment_exitcode_get ();
669 } else if (action == DO_COMPILE) {
670 compile_all_methods (assembly, mini_verbose);
671 mini_cleanup (domain);
674 desc = mono_method_desc_new (mname, 0);
676 g_print ("Invalid method name %s\n", mname);
677 mini_cleanup (domain);
680 method = mono_method_desc_search_in_image (desc, assembly->image);
682 g_print ("Cannot find method %s\n", mname);
683 mini_cleanup (domain);
687 if (action == DO_DRAW) {
690 switch (mono_graph_options) {
691 case MONO_GRAPH_DTREE:
693 opt |= MONO_OPT_LOOP;
695 case MONO_GRAPH_CFG_CODE:
698 case MONO_GRAPH_CFG_SSA:
701 case MONO_GRAPH_CFG_OPTCODE:
708 cfg = mini_method_compile (method, opt, mono_root_domain, part);
709 if ((mono_graph_options & MONO_GRAPH_CFG_SSA) && !(cfg->comp_done & MONO_COMP_SSA)) {
710 g_warning ("no SSA info available (use -O=deadce)");
713 mono_draw_graph (cfg, mono_graph_options);
714 mono_destroy_compile (cfg);
716 } else if (action == DO_BENCH) {
719 double no_opt_time = 0.0;
720 GTimer *timer = g_timer_new ();
721 fprintf (mini_stats_fd, "$stattitle = \'Compilations times for %s\';\n",
722 mono_method_full_name (method, TRUE));
723 fprintf (mini_stats_fd, "@data = (\n");
724 fprintf (mini_stats_fd, "[");
725 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
730 fprintf (mini_stats_fd, "\"%s\",", n);
732 fprintf (mini_stats_fd, "],\n[");
734 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
738 g_timer_start (timer);
739 for (j = 0; j < count; ++j) {
740 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
741 mono_destroy_compile (cfg);
743 g_timer_stop (timer);
744 elapsed = g_timer_elapsed (timer, NULL);
746 no_opt_time = elapsed;
747 fprintf (mini_stats_fd, "%f, ", elapsed);
749 fprintf (mini_stats_fd, "]");
750 if (no_opt_time > 0.0) {
751 fprintf (mini_stats_fd, ", \n[");
752 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++)
753 fprintf (mini_stats_fd, "%f,", no_opt_time);
754 fprintf (mini_stats_fd, "]");
756 fprintf (mini_stats_fd, ");\n");
758 for (i = 0; i < count; ++i) {
759 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
760 mono_destroy_compile (cfg);
764 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
765 mono_destroy_compile (cfg);
768 mini_cleanup (domain);
773 mono_jit_init (const char *file)
775 return mini_init (file);
779 mono_jit_cleanup (MonoDomain *domain)
781 mini_cleanup (domain);