2 * driver.c: The new mono JIT compiler.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002-2003 Ximian, Inc.
9 * (C) 2003-2004 Novell, Inc.
16 #include <mono/metadata/assembly.h>
17 #include <mono/metadata/loader.h>
18 #include <mono/metadata/cil-coff.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/class.h>
21 #include <mono/metadata/object.h>
22 #include <mono/metadata/exception.h>
23 #include <mono/metadata/opcodes.h>
24 #include <mono/metadata/mono-endian.h>
25 #include <mono/metadata/tokentype.h>
26 #include <mono/metadata/tabledefs.h>
27 #include <mono/metadata/threads.h>
28 #include <mono/metadata/marshal.h>
29 #include <mono/metadata/socket-io.h>
30 #include <mono/metadata/appdomain.h>
31 #include <mono/metadata/debug-helpers.h>
32 #include <mono/io-layer/io-layer.h>
33 #include "mono/metadata/profiler.h"
34 #include <mono/metadata/profiler-private.h>
35 #include <mono/metadata/mono-config.h>
36 #include <mono/metadata/environment.h>
37 #include <mono/metadata/verify.h>
38 #include <mono/metadata/mono-debug.h>
39 #include <mono/metadata/mono-debug-debugger.h>
40 #include <mono/metadata/security-manager.h>
41 #include <mono/os/gc_wrapper.h>
50 static FILE *mini_stats_fd = NULL;
52 static void mini_usage (void);
54 /* This turns off command line globbing under win32 */
59 typedef void (*OptFunc) (const char *p);
61 /* keep in sync with enum in mini.h */
70 {"peephole", "Peephole postpass"},
71 {"branch", "Branch optimizations"},
72 {"inline", "Inline method calls"},
73 {"cfold", "Constant folding"},
74 {"consprop", "Constant propagation"},
75 {"copyprop", "Copy propagation"},
76 {"deadce", "Dead code elimination"},
77 {"linears", "Linear scan global reg allocation"},
78 {"cmov", "Conditional moves"},
79 {"shared", "Emit per-domain code"},
80 {"sched", "Instruction scheduling"},
81 {"intrins", "Intrinsic method implementations"},
82 {"tailc", "Tail recursion and tail calls"},
83 {"loop", "Loop related optimizations"},
84 {"fcmov", "Fast x86 FP compares"},
85 {"leaf", "Leaf procedures optimizations"},
86 {"aot", "Usage of Ahead Of Time compiled code"},
87 {"precomp", "Precompile all methods before executing Main"},
88 {"abcrem", "Array bound checks removal"},
89 {"ssapre", "SSA based Partial Redundancy Elimination"}
92 #define DEFAULT_OPTIMIZATIONS ( \
101 #define EXCLUDED_FROM_ALL (MONO_OPT_SHARED | MONO_OPT_PRECOMP)
104 parse_optimizations (const char* p)
106 /* the default value */
107 guint32 opt = DEFAULT_OPTIMIZATIONS;
112 /* call out to cpu detection code here that sets the defaults ... */
113 opt |= mono_arch_cpu_optimizazions (&exclude);
125 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
126 n = opt_names [i].name;
128 if (strncmp (p, n, len) == 0) {
137 } else if (*p == '=') {
139 if (opt_names [i].func)
140 opt_names [i].func (p);
141 while (*p && *p++ != ',');
148 if (i == G_N_ELEMENTS (opt_names)) {
149 if (strncmp (p, "all", 3) == 0) {
153 opt = ~(EXCLUDED_FROM_ALL | exclude);
158 fprintf (stderr, "Invalid optimization name `%s'\n", p);
169 MonoGraphOptions value;
172 static const GraphName
174 {"cfg", "Control Flow Graph (CFG)" , MONO_GRAPH_CFG},
175 {"dtree", "Dominator Tree", MONO_GRAPH_DTREE},
176 {"code", "CFG showing code", MONO_GRAPH_CFG_CODE},
177 {"ssa", "CFG showing code after SSA translation", MONO_GRAPH_CFG_SSA},
178 {"optcode", "CFG showing code after IR optimizations", MONO_GRAPH_CFG_OPTCODE}
181 static MonoGraphOptions
182 mono_parse_graph_options (const char* p)
187 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
188 n = graph_names [i].name;
190 if (strncmp (p, n, len) == 0)
191 return graph_names [i].value;
194 fprintf (stderr, "Invalid graph name provided: %s\n", p);
199 mono_parse_default_optimizations (const char* p)
203 opt = parse_optimizations (p);
208 opt_descr (guint32 flags) {
209 GString *str = g_string_new ("");
213 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
214 if (flags & (1 << i)) {
216 g_string_append_c (str, ',');
217 g_string_append (str, opt_names [i].name);
221 return g_string_free (str, FALSE);
231 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_INTRINS,
232 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
233 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
234 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_CFOLD,
235 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
236 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,
237 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 | MONO_OPT_ABCREM,
238 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 | MONO_OPT_SSAPRE,
239 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 | MONO_OPT_ABCREM | MONO_OPT_SHARED
242 typedef int (*TestMethod) (void);
246 domain_dump_native_code (MonoDomain *domain) {
247 // need to poke into the domain, move to metadata/domain.c
248 // need to empty jit_info_table and code_mp
253 mini_regression (MonoImage *image, int verbose, int *total_run) {
254 guint32 i, opt, opt_flags;
258 int result, expected, failed, cfailed, run, code_size, total;
260 GTimer *timer = g_timer_new ();
263 fprintf (mini_stats_fd, "$stattitle = \'Mono Benchmark Results (various optimizations)\';\n");
265 fprintf (mini_stats_fd, "$graph->set_legend(qw(");
266 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); opt++) {
267 opt_flags = opt_sets [opt];
268 n = opt_descr (opt_flags);
272 fprintf (mini_stats_fd, " ");
273 fprintf (mini_stats_fd, "%s", n);
277 fprintf (mini_stats_fd, "));\n");
279 fprintf (mini_stats_fd, "@data = (\n");
280 fprintf (mini_stats_fd, "[");
283 /* load the metadata */
284 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
285 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
286 mono_class_init (method->klass);
288 if (!strncmp (method->name, "test_", 5) && mini_stats_fd) {
289 fprintf (mini_stats_fd, "\"%s\",", method->name);
293 fprintf (mini_stats_fd, "],\n");
298 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
299 double elapsed, comp_time, start_time;
302 opt_flags = opt_sets [opt];
303 mono_set_defaults (verbose, opt_flags);
304 n = opt_descr (opt_flags);
305 g_print ("Test run: image=%s, opts=%s\n", mono_image_get_filename (image), n);
307 cfailed = failed = run = code_size = 0;
308 comp_time = elapsed = 0.0;
310 /* fixme: ugly hack - delete all previously compiled methods */
311 g_hash_table_destroy (mono_domain_get ()->jit_trampoline_hash);
312 mono_domain_get ()->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
313 g_hash_table_destroy (mono_domain_get ()->jit_code_hash);
314 mono_domain_get ()->jit_code_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
316 g_timer_start (timer);
318 fprintf (mini_stats_fd, "[");
319 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
320 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
321 if (strncmp (method->name, "test_", 5) == 0) {
322 expected = atoi (method->name + 5);
324 start_time = g_timer_elapsed (timer, NULL);
325 comp_time -= start_time;
326 cfg = mini_method_compile (method, opt_flags, mono_get_root_domain (), TRUE, FALSE, 0);
327 comp_time += g_timer_elapsed (timer, NULL);
330 g_print ("Running '%s' ...\n", method->name);
331 #ifdef MONO_USE_AOT_COMPILER
332 if ((jinfo = mono_aot_get_method (mono_get_root_domain (), method)))
333 func = jinfo->code_start;
336 func = (TestMethod)(gpointer)cfg->native_code;
337 func = (TestMethod)mono_create_ftnptr (mono_get_root_domain (), func);
339 if (result != expected) {
341 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
343 code_size += cfg->code_len;
344 mono_destroy_compile (cfg);
349 g_print ("Test '%s' failed compilation.\n", method->name);
352 fprintf (mini_stats_fd, "%f, ",
353 g_timer_elapsed (timer, NULL) - start_time);
357 fprintf (mini_stats_fd, "],\n");
358 g_timer_stop (timer);
359 elapsed = g_timer_elapsed (timer, NULL);
360 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
361 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
362 g_print ("Elapsed time: %f secs (%f, %f), Code size: %d\n\n", elapsed,
363 elapsed - comp_time, comp_time, code_size);
364 total += failed + cfailed;
369 fprintf (mini_stats_fd, ");\n");
370 fflush (mini_stats_fd);
373 g_timer_destroy (timer);
378 mini_regression_list (int verbose, int count, char *images [])
380 int i, total, total_run, run;
383 total_run = total = 0;
384 for (i = 0; i < count; ++i) {
385 ass = mono_assembly_open (images [i], NULL);
387 g_warning ("failed to load assembly: %s", images [i]);
390 total += mini_regression (mono_assembly_get_image (ass), verbose, &run);
393 g_print ("Overall results: tests: %d, failed: %d, opt combinations: %d (pass: %.2f%%)\n",
394 total_run, total, (int)G_N_ELEMENTS (opt_sets), 100.0*(total_run-total)/total_run);
406 typedef struct CompileAllThreadArgs {
409 } CompileAllThreadArgs;
412 compile_all_methods_thread_main (CompileAllThreadArgs *args)
414 MonoAssembly *ass = args->ass;
415 int verbose = args->verbose;
416 MonoImage *image = mono_assembly_get_image (ass);
421 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
422 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
423 MonoMethodSignature *sig;
425 if (mono_metadata_has_generic_params (image, token))
428 method = mono_get_method (image, token, NULL);
429 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
430 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
431 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
432 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
435 if (method->klass->generic_container)
437 sig = mono_method_signature (method);
438 if (sig->has_type_parameters)
443 char * desc = mono_method_full_name (method, TRUE);
444 g_print ("Compiling %d %s\n", count, desc);
447 cfg = mini_method_compile (method, DEFAULT_OPTIMIZATIONS, mono_get_root_domain (), FALSE, FALSE, 0);
448 mono_destroy_compile (cfg);
454 compile_all_methods (MonoAssembly *ass, int verbose)
456 CompileAllThreadArgs args;
459 args.verbose = verbose;
462 * Need to create a mono thread since compilation might trigger
463 * running of managed code.
465 mono_thread_create (mono_domain_get (), compile_all_methods_thread_main, &args);
467 mono_thread_manage ();
472 * @assembly: reference to an assembly
473 * @argc: argument count
474 * @argv: argument vector
476 * Start execution of a program.
479 mono_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
481 MonoImage *image = mono_assembly_get_image (assembly);
483 guint32 entry = mono_image_get_entry_point (image);
486 g_print ("Assembly '%s' doesn't have an entry point.\n", mono_image_get_filename (image));
487 /* FIXME: remove this silly requirement. */
488 mono_environment_exitcode_set (1);
492 method = mono_get_method (image, entry, NULL);
494 return mono_runtime_run_main (method, argc, argv, NULL);
507 static void main_thread_handler (gpointer user_data)
509 MainThreadArgs *main_args = user_data;
510 MonoAssembly *assembly;
512 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
514 fprintf (stderr, "Can not open image %s\n", main_args->file);
518 if (mono_compile_aot) {
519 int res = mono_compile_assembly (assembly, main_args->opts, main_args->aot_options);
520 printf ("AOT RESULT %d\n", res);
523 * This must be done in a thread managed by mono since it can invoke
526 if (main_args->opts & MONO_OPT_PRECOMP)
527 mono_precompile_assemblies ();
529 mono_jit_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
534 mini_usage_jitdeveloper (void)
539 "Runtime and JIT debugging options:\n"
540 " --breakonex Inserts a breakpoint on exceptions\n"
541 " --break METHOD Inserts a breakpoint at METHOD entry\n"
542 " --compile METHOD Just compile METHOD in assembly\n"
543 " --compile-all Compiles all the methods in the assembly\n"
544 " --ncompile N Number of times to compile METHOD (default: 1)\n"
545 " --print-vtable Print the vtable of all used classes\n"
546 " --regression Runs the regression test contained in the assembly\n"
547 " --statfile FILE Sets the stat file to FILE\n"
548 " --stats Print statistics about the JIT operations\n"
551 " --graph[=TYPE] METHOD Draws a graph of the specified method:\n");
553 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
554 fprintf (stdout, " %-10s %s\n", graph_names [i].name, graph_names [i].desc);
559 mini_usage_list_opt (void)
563 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
564 fprintf (stdout, " %-10s %s\n", opt_names [i].name, opt_names [i].desc);
571 "Usage is: mono [options] program [program-options]\n"
574 " --aot Compiles the assembly to native code\n"
575 " --debug Enable debugging support\n"
576 " --profile[=profiler] Runs in profiling mode with the specified profiler module\n"
577 " --trace[=EXPR] Enable tracing, use --help-trace for details\n"
578 " --help-devel Shows more options available to developers\n"
581 " --config FILE Loads FILE as the Mono config\n"
582 " --verbose, -v Increases the verbosity level\n"
583 " --help, -h Show usage information\n"
584 " --version, -V Show version information\n"
585 " --optimize=OPT Turns on or off a specific optimization\n"
586 " Use --list-opt to get a list of optimizations\n"
587 " --security Turns on the security manager (unsupported, default is off)\n");
591 mini_trace_usage (void)
595 " --trace[=EXPR] Trace every call, optional EXPR controls the scope\n"
597 "EXPR is composed of:\n"
598 " all All assemblies\n"
599 " none No assemblies\n"
600 " program Entry point assembly\n"
601 " assembly Specifies an assembly\n"
602 " M:Type:Method Specifies a method\n"
603 " N:Namespace Specifies a namespace\n"
604 " T:Type Specifies a type\n"
605 " +EXPR Includes expression\n"
606 " -EXPR Excludes expression\n"
607 " disabled Don't print any output until toggled via SIGUSR2\n");
610 static const char *info = ""
611 #ifdef HAVE_KW_THREAD
615 #endif /* HAVE_KW_THREAD */
617 #ifdef USE_INCLUDED_LIBGC
618 "\tGC: Included Boehm (with typed GC)\n"
620 #if HAVE_GC_GCJ_MALLOC
621 "\tGC: System Boehm (with typed GC)\n"
623 "\tGC: System Boehm (no typed GC available)\n"
628 #endif /* HAVE_BOEHM_GC */
629 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
630 "\tSIGSEGV : altstack\n"
632 "\tSIGSEGV : normal\n"
635 "\tGlobalization: ICU (unsupported, don't use)\n"
637 "\tGlobalization: normal\n"
638 #endif /* HAVE_ICU */
642 mono_main (int argc, char* argv[])
644 MainThreadArgs main_args;
645 MonoAssembly *assembly;
646 MonoMethodDesc *desc;
650 const char* aname, *mname = NULL;
651 char *config_file = NULL;
653 int enable_debugging = FALSE;
654 guint32 opt, action = DO_EXEC;
655 MonoGraphOptions mono_graph_options = 0;
656 int mini_verbose = 0;
657 gboolean enable_profile = FALSE;
658 char *trace_options = NULL;
659 char *profile_options = NULL;
660 char *aot_options = NULL;
662 setlocale (LC_ALL, "");
664 if (mono_running_on_valgrind () && getenv ("MONO_VALGRIND_LEAK_CHECK")) {
665 GMemVTable mem_vtable;
668 * Instruct glib to use the system allocation functions so valgrind
669 * can track the memory allocated by the g_... functions.
671 memset (&mem_vtable, 0, sizeof (mem_vtable));
672 mem_vtable.malloc = malloc;
673 mem_vtable.realloc = realloc;
674 mem_vtable.free = free;
675 mem_vtable.calloc = calloc;
677 g_mem_set_vtable (&mem_vtable);
680 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
681 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
683 opt = parse_optimizations (NULL);
685 for (i = 1; i < argc; ++i) {
686 if (argv [i] [0] != '-')
688 if (strcmp (argv [i], "--regression") == 0) {
689 action = DO_REGRESSION;
690 } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
692 } else if (strcmp (argv [i], "--version") == 0 || strcmp (argv [i], "-V") == 0) {
693 g_print ("Mono JIT compiler version %s, (C) 2002-2005 Novell, Inc and Contributors. www.mono-project.com\n", VERSION);
697 const char *clibpath;
699 cerror = mono_check_corlib_version ();
700 clibpath = mono_defaults.corlib? mono_image_get_filename (mono_defaults.corlib): "unknown";
702 g_print ("The currently installed mscorlib doesn't match this runtime version.\n");
703 g_print ("The error is: %s\n", cerror);
704 g_print ("mscorlib.dll loaded at: %s\n", clibpath);
709 } else if (strcmp (argv [i], "--help") == 0 || strcmp (argv [i], "-h") == 0) {
712 } else if (strcmp (argv [i], "--help-trace") == 0){
715 } else if (strcmp (argv [i], "--help-devel") == 0){
716 mini_usage_jitdeveloper ();
718 } else if (strcmp (argv [i], "--list-opt") == 0){
719 mini_usage_list_opt ();
721 } else if (strncmp (argv [i], "--statfile", 10) == 0) {
723 fprintf (stderr, "error: --statfile requires a filename argument\n");
726 mini_stats_fd = fopen (argv [++i], "w+");
727 } else if (strncmp (argv [i], "--optimize=", 11) == 0) {
728 opt = parse_optimizations (argv [i] + 11);
729 } else if (strncmp (argv [i], "-O=", 3) == 0) {
730 opt = parse_optimizations (argv [i] + 3);
731 } else if (strcmp (argv [i], "--config") == 0) {
733 fprintf (stderr, "error: --config requires a filename argument\n");
736 config_file = argv [++i];
737 } else if (strcmp (argv [i], "--ncompile") == 0) {
739 fprintf (stderr, "error: --ncompile requires an argument\n");
742 count = atoi (argv [++i]);
744 } else if (strcmp (argv [i], "--trace") == 0) {
745 trace_options = (char*)"";
746 } else if (strncmp (argv [i], "--trace=", 8) == 0) {
747 trace_options = &argv [i][8];
748 } else if (strcmp (argv [i], "--breakonex") == 0) {
749 mono_break_on_exc = TRUE;
750 } else if (strcmp (argv [i], "--break") == 0) {
752 fprintf (stderr, "Missing method name in --break command line option\n");
756 if (!mono_debugger_insert_breakpoint (argv [++i], FALSE))
757 fprintf (stderr, "Error: invalid method name '%s'\n", argv [i]);
758 } else if (strcmp (argv [i], "--print-vtable") == 0) {
759 mono_print_vtable = TRUE;
760 } else if (strcmp (argv [i], "--stats") == 0) {
761 mono_stats.enabled = TRUE;
762 mono_jit_stats.enabled = TRUE;
764 } else if (strcmp (argv [i], "--aot") == 0) {
765 mono_compile_aot = TRUE;
766 } else if (strncmp (argv [i], "--aot=", 6) == 0) {
767 mono_compile_aot = TRUE;
768 aot_options = &argv [i][6];
770 } else if (strcmp (argv [i], "--compile-all") == 0) {
772 } else if (strcmp (argv [i], "--profile") == 0) {
773 enable_profile = TRUE;
774 profile_options = NULL;
775 } else if (strncmp (argv [i], "--profile=", 10) == 0) {
776 enable_profile = TRUE;
777 profile_options = argv [i] + 10;
778 } else if (strcmp (argv [i], "--compile") == 0) {
780 fprintf (stderr, "error: --compile option requires a method name argument\n");
786 } else if (strncmp (argv [i], "--graph=", 8) == 0) {
788 fprintf (stderr, "error: --graph option requires a method name argument\n");
792 mono_graph_options = mono_parse_graph_options (argv [i] + 8);
795 } else if (strcmp (argv [i], "--graph") == 0) {
797 fprintf (stderr, "error: --graph option requires a method name argument\n");
802 mono_graph_options = MONO_GRAPH_CFG;
804 } else if (strcmp (argv [i], "--debug") == 0) {
805 enable_debugging = TRUE;
806 } else if (strcmp (argv [i], "--security") == 0) {
807 mono_use_security_manager = TRUE;
808 mono_activate_security_manager ();
810 fprintf (stderr, "Unknown command line option: '%s'\n", argv [i]);
820 if (mono_compile_aot || action == DO_EXEC) {
821 g_set_prgname (argv[i]);
824 if (enable_profile) {
825 /* Needed because of TLS accesses in mono_profiler_load () */
827 mono_profiler_load (profile_options);
830 if (trace_options != NULL){
832 * Need to call this before mini_init () so we can trace methods
833 * compiled there too.
835 mono_jit_trace_calls = mono_trace_parse_options (trace_options);
836 if (mono_jit_trace_calls == NULL)
840 mono_set_defaults (mini_verbose, opt);
841 domain = mini_init (argv [i]);
845 if (mini_regression_list (mini_verbose, argc -i, argv + i)) {
846 g_print ("Regression ERRORS!\n");
847 mini_cleanup (domain);
850 mini_cleanup (domain);
853 if (argc - i != 1 || mname == NULL) {
854 g_print ("Usage: mini --ncompile num --compile method assembly\n");
855 mini_cleanup (domain);
863 mini_cleanup (domain);
869 if (argc - i != 1 || mname == NULL) {
871 mini_cleanup (domain);
879 mini_cleanup (domain);
886 if (enable_debugging) {
887 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
888 mono_debug_init_1 (domain);
891 /* Parse gac loading options before loading assemblies. */
892 if (mono_compile_aot || action == DO_EXEC) {
893 mono_config_parse (config_file);
896 assembly = mono_assembly_open (aname, NULL);
898 fprintf (stderr, "cannot open assembly %s\n", aname);
899 mini_cleanup (domain);
903 if (trace_options != NULL)
904 mono_trace_set_assembly (assembly);
906 if (enable_debugging)
907 mono_debug_init_2 (assembly);
909 if (mono_compile_aot || action == DO_EXEC) {
912 //mono_set_rootdir ();
914 error = mono_check_corlib_version ();
916 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
917 fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
921 #ifdef PLATFORM_WIN32
922 /* Detach console when executing IMAGE_SUBSYSTEM_WINDOWS_GUI on win32 */
923 if (!mono_compile_aot && ((MonoCLIImageInfo*)(mono_assembly_get_image (assembly)->image_info))->cli_header.nt.pe_subsys_required == IMAGE_SUBSYSTEM_WINDOWS_GUI)
927 main_args.domain = domain;
928 main_args.file = aname;
929 main_args.argc = argc - i;
930 main_args.argv = argv + i;
931 main_args.opts = opt;
932 main_args.aot_options = aot_options;
934 mono_runtime_exec_managed_code (domain, main_thread_handler, &main_args);
936 main_thread_handler (&main_args);
937 mono_thread_manage ();
939 mini_cleanup (domain);
941 /* Look up return value from System.Environment.ExitCode */
942 i = mono_environment_exitcode_get ();
944 } else if (action == DO_COMPILE) {
945 compile_all_methods (assembly, mini_verbose);
946 mini_cleanup (domain);
949 desc = mono_method_desc_new (mname, 0);
951 g_print ("Invalid method name %s\n", mname);
952 mini_cleanup (domain);
955 method = mono_method_desc_search_in_image (desc, mono_assembly_get_image (assembly));
957 g_print ("Cannot find method %s\n", mname);
958 mini_cleanup (domain);
962 if (action == DO_DRAW) {
965 switch (mono_graph_options) {
966 case MONO_GRAPH_DTREE:
968 opt |= MONO_OPT_LOOP;
970 case MONO_GRAPH_CFG_CODE:
973 case MONO_GRAPH_CFG_SSA:
976 case MONO_GRAPH_CFG_OPTCODE:
983 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
984 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
986 nm = mono_marshal_get_native_wrapper (method);
987 cfg = mini_method_compile (nm, opt, mono_get_root_domain (), FALSE, FALSE, part);
990 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, part);
991 if ((mono_graph_options & MONO_GRAPH_CFG_SSA) && !(cfg->comp_done & MONO_COMP_SSA)) {
992 g_warning ("no SSA info available (use -O=deadce)");
995 mono_draw_graph (cfg, mono_graph_options);
996 mono_destroy_compile (cfg);
998 } else if (action == DO_BENCH) {
1001 double no_opt_time = 0.0;
1002 GTimer *timer = g_timer_new ();
1003 fprintf (mini_stats_fd, "$stattitle = \'Compilations times for %s\';\n",
1004 mono_method_full_name (method, TRUE));
1005 fprintf (mini_stats_fd, "@data = (\n");
1006 fprintf (mini_stats_fd, "[");
1007 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
1009 n = opt_descr (opt);
1012 fprintf (mini_stats_fd, "\"%s\",", n);
1014 fprintf (mini_stats_fd, "],\n[");
1016 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
1020 g_timer_start (timer);
1021 for (j = 0; j < count; ++j) {
1022 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1023 mono_destroy_compile (cfg);
1025 g_timer_stop (timer);
1026 elapsed = g_timer_elapsed (timer, NULL);
1028 no_opt_time = elapsed;
1029 fprintf (mini_stats_fd, "%f, ", elapsed);
1031 fprintf (mini_stats_fd, "]");
1032 if (no_opt_time > 0.0) {
1033 fprintf (mini_stats_fd, ", \n[");
1034 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++)
1035 fprintf (mini_stats_fd, "%f,", no_opt_time);
1036 fprintf (mini_stats_fd, "]");
1038 fprintf (mini_stats_fd, ");\n");
1040 for (i = 0; i < count; ++i) {
1041 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1042 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
1043 method = mono_marshal_get_native_wrapper (method);
1045 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1046 mono_destroy_compile (cfg);
1050 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1051 mono_destroy_compile (cfg);
1054 mini_cleanup (domain);
1059 mono_jit_init (const char *file)
1061 return mini_init (file);
1065 mono_jit_cleanup (MonoDomain *domain)
1067 mini_cleanup (domain);