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 typedef void (*OptFunc) (const char *p);
56 /* keep in sync with enum in mini.h */
65 {"peephole", "Peephole postpass"},
66 {"branch", "Branch optimizations"},
67 {"inline", "Inline method calls"},
68 {"cfold", "Constant folding"},
69 {"consprop", "Constant propagation"},
70 {"copyprop", "Copy propagation"},
71 {"deadce", "Dead code elimination"},
72 {"linears", "Linear scan global reg allocation"},
73 {"cmov", "Conditional moves"},
74 {"shared", "Emit per-domain code"},
75 {"sched", "Instruction scheduling"},
76 {"intrins", "Intrinsic method implementations"},
77 {"tailc", "Tail recursion and tail calls"},
78 {"loop", "Loop related optimizations"},
79 {"fcmov", "Fast x86 FP compares"},
80 {"leaf", "Leaf procedures optimizations"},
81 {"aot", "Usage of Ahead Of Time compiled code"},
82 {"precomp", "Precompile all methods before executing Main"},
83 {"abcrem", "Array bound checks removal"},
84 {"ssapre", "SSA based Partial Redundancy Elimination"}
87 #define DEFAULT_OPTIMIZATIONS ( \
96 /* SSAPRE does not work correctly on non x86 platforms (bug #70637) */
97 #define EXCLUDED_FROM_ALL (MONO_OPT_SHARED | MONO_OPT_PRECOMP)
100 parse_optimizations (const char* p)
102 /* the default value */
103 guint32 opt = DEFAULT_OPTIMIZATIONS;
108 /* call out to cpu detection code here that sets the defaults ... */
109 opt |= mono_arch_cpu_optimizazions (&exclude);
121 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
122 n = opt_names [i].name;
124 if (strncmp (p, n, len) == 0) {
133 } else if (*p == '=') {
135 if (opt_names [i].func)
136 opt_names [i].func (p);
137 while (*p && *p++ != ',');
144 if (i == G_N_ELEMENTS (opt_names)) {
145 if (strncmp (p, "all", 3) == 0) {
149 opt = ~(EXCLUDED_FROM_ALL | exclude);
154 fprintf (stderr, "Invalid optimization name `%s'\n", p);
165 MonoGraphOptions value;
168 static const GraphName
170 {"cfg", "Control Flow Graph (CFG)" , MONO_GRAPH_CFG},
171 {"dtree", "Dominator Tree", MONO_GRAPH_DTREE},
172 {"code", "CFG showing code", MONO_GRAPH_CFG_CODE},
173 {"ssa", "CFG showing code after SSA translation", MONO_GRAPH_CFG_SSA},
174 {"optcode", "CFG showing code after IR optimizations", MONO_GRAPH_CFG_OPTCODE}
177 static MonoGraphOptions
178 mono_parse_graph_options (const char* p)
183 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
184 n = graph_names [i].name;
186 if (strncmp (p, n, len) == 0)
187 return graph_names [i].value;
190 fprintf (stderr, "Invalid graph name provided: %s\n", p);
195 mono_parse_default_optimizations (const char* p)
199 opt = parse_optimizations (p);
204 opt_descr (guint32 flags) {
205 GString *str = g_string_new ("");
209 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
210 if (flags & (1 << i)) {
212 g_string_append_c (str, ',');
213 g_string_append (str, opt_names [i].name);
217 return g_string_free (str, FALSE);
227 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_INTRINS,
228 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
229 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
230 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_CFOLD,
231 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
232 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,
233 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,
234 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,
235 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
238 typedef int (*TestMethod) (void);
242 domain_dump_native_code (MonoDomain *domain) {
243 // need to poke into the domain, move to metadata/domain.c
244 // need to empty jit_info_table and code_mp
249 mini_regression (MonoImage *image, int verbose, int *total_run) {
250 guint32 i, opt, opt_flags;
254 int result, expected, failed, cfailed, run, code_size, total;
256 GTimer *timer = g_timer_new ();
259 fprintf (mini_stats_fd, "$stattitle = \'Mono Benchmark Results (various optimizations)\';\n");
261 fprintf (mini_stats_fd, "$graph->set_legend(qw(");
262 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); opt++) {
263 opt_flags = opt_sets [opt];
264 n = opt_descr (opt_flags);
268 fprintf (mini_stats_fd, " ");
269 fprintf (mini_stats_fd, "%s", n);
273 fprintf (mini_stats_fd, "));\n");
275 fprintf (mini_stats_fd, "@data = (\n");
276 fprintf (mini_stats_fd, "[");
279 /* load the metadata */
280 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
281 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
282 mono_class_init (method->klass);
284 if (!strncmp (method->name, "test_", 5) && mini_stats_fd) {
285 fprintf (mini_stats_fd, "\"%s\",", method->name);
289 fprintf (mini_stats_fd, "],\n");
294 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
295 double elapsed, comp_time, start_time;
298 opt_flags = opt_sets [opt];
299 mono_set_defaults (verbose, opt_flags);
300 n = opt_descr (opt_flags);
301 g_print ("Test run: image=%s, opts=%s\n", mono_image_get_filename (image), n);
303 cfailed = failed = run = code_size = 0;
304 comp_time = elapsed = 0.0;
306 /* fixme: ugly hack - delete all previously compiled methods */
307 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
308 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
312 g_timer_start (timer);
314 fprintf (mini_stats_fd, "[");
315 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
316 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
317 if (strncmp (method->name, "test_", 5) == 0) {
318 expected = atoi (method->name + 5);
320 start_time = g_timer_elapsed (timer, NULL);
321 comp_time -= start_time;
322 cfg = mini_method_compile (method, opt_flags, mono_get_root_domain (), TRUE, FALSE, 0);
323 comp_time += g_timer_elapsed (timer, NULL);
326 g_print ("Running '%s' ...\n", method->name);
327 #ifdef MONO_USE_AOT_COMPILER
328 if ((jinfo = mono_aot_get_method (mono_get_root_domain (), method)))
329 func = jinfo->code_start;
332 func = (TestMethod)cfg->native_code;
334 if (result != expected) {
337 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
339 code_size += cfg->code_len;
340 mono_destroy_compile (cfg);
345 g_print ("Test '%s' failed compilation.\n", method->name);
348 fprintf (mini_stats_fd, "%f, ",
349 g_timer_elapsed (timer, NULL) - start_time);
353 fprintf (mini_stats_fd, "],\n");
354 g_timer_stop (timer);
355 elapsed = g_timer_elapsed (timer, NULL);
356 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
357 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
358 g_print ("Elapsed time: %f secs (%f, %f), Code size: %d\n\n", elapsed,
359 elapsed - comp_time, comp_time, code_size);
360 total += failed + cfailed;
365 fprintf (mini_stats_fd, ");\n");
366 fflush (mini_stats_fd);
369 g_timer_destroy (timer);
374 mini_regression_list (int verbose, int count, char *images [])
376 int i, total, total_run, run;
379 total_run = total = 0;
380 for (i = 0; i < count; ++i) {
381 ass = mono_assembly_open (images [i], NULL);
383 g_warning ("failed to load assembly: %s", images [i]);
386 total += mini_regression (mono_assembly_get_image (ass), verbose, &run);
389 g_print ("Overall results: tests: %d, failed: %d, opt combinations: %d (pass: %.2f%%)\n",
390 total_run, total, (int)G_N_ELEMENTS (opt_sets), 100.0*(total_run-total)/total_run);
402 typedef struct CompileAllThreadArgs {
405 } CompileAllThreadArgs;
408 compile_all_methods_thread_main (CompileAllThreadArgs *args)
410 MonoAssembly *ass = args->ass;
411 int verbose = args->verbose;
412 MonoImage *image = mono_assembly_get_image (ass);
417 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
418 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
419 MonoMethodSignature *sig;
421 if (mono_metadata_has_generic_params (image, token))
424 method = mono_get_method (image, token, NULL);
425 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
426 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
427 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
428 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
431 if (method->klass->generic_container)
433 sig = mono_method_signature (method);
434 if (sig->has_type_parameters)
439 char * desc = mono_method_full_name (method, TRUE);
440 g_print ("Compiling %d %s\n", count, desc);
443 cfg = mini_method_compile (method, DEFAULT_OPTIMIZATIONS, mono_get_root_domain (), FALSE, FALSE, 0);
444 mono_destroy_compile (cfg);
450 compile_all_methods (MonoAssembly *ass, int verbose)
452 CompileAllThreadArgs args;
455 args.verbose = verbose;
458 * Need to create a mono thread since compilation might trigger
459 * running of managed code.
461 mono_thread_create (mono_domain_get (), compile_all_methods_thread_main, &args);
463 mono_thread_manage ();
468 * @assembly: reference to an assembly
469 * @argc: argument count
470 * @argv: argument vector
472 * Start execution of a program.
475 mono_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
477 MonoImage *image = mono_assembly_get_image (assembly);
479 guint32 entry = mono_image_get_entry_point (image);
482 g_print ("Assembly '%s' doesn't have an entry point.\n", mono_image_get_filename (image));
483 /* FIXME: remove this silly requirement. */
484 mono_environment_exitcode_set (1);
488 method = mono_get_method (image, entry, NULL);
490 return mono_runtime_run_main (method, argc, argv, NULL);
503 static void main_thread_handler (gpointer user_data)
505 MainThreadArgs *main_args = user_data;
506 MonoAssembly *assembly;
508 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
510 fprintf (stderr, "Can not open image %s\n", main_args->file);
514 if (mono_compile_aot) {
515 int res = mono_compile_assembly (assembly, main_args->opts, main_args->aot_options);
516 printf ("AOT RESULT %d\n", res);
519 * This must be done in a thread managed by mono since it can invoke
522 if (main_args->opts & MONO_OPT_PRECOMP)
523 mono_precompile_assemblies ();
525 mono_jit_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
530 mini_usage_jitdeveloper (void)
535 "Runtime and JIT debugging options:\n"
536 " --breakonex Inserts a breakpoint on exceptions\n"
537 " --break METHOD Inserts a breakpoint at METHOD entry\n"
538 " --compile METHOD Just compile METHOD in assembly\n"
539 " --compile-all Compiles all the methods in the assembly\n"
540 " --ncompile N Number of times to compile METHOD (default: 1)\n"
541 " --print-vtable Print the vtable of all used classes\n"
542 " --regression Runs the regression test contained in the assembly\n"
543 " --statfile FILE Sets the stat file to FILE\n"
544 " --stats Print statistics about the JIT operations\n"
547 " --graph[=TYPE] METHOD Draws a graph of the specified method:\n");
549 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
550 fprintf (stdout, " %-10s %s\n", graph_names [i].name, graph_names [i].desc);
555 mini_usage_list_opt (void)
559 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
560 fprintf (stdout, " %-10s %s\n", opt_names [i].name, opt_names [i].desc);
567 "Usage is: mono [options] program [program-options]\n"
570 " --aot Compiles the assembly to native code\n"
571 " --debug Enable debugging support\n"
572 " --profile[=profiler] Runs in profiling mode with the specified profiler module\n"
573 " --trace[=EXPR] Enable tracing, use --help-trace for details\n"
574 " --help-devel Shows more options available to developers\n"
577 " --config FILE Loads FILE as the Mono config\n"
578 " --verbose, -v Increases the verbosity level\n"
579 " --help, -h Show usage information\n"
580 " --version, -V Show version information\n"
581 " --optimize=OPT Turns on or off a specific optimization\n"
582 " Use --list-opt to get a list of optimizations\n"
583 " --security Turns on the security manager (unsupported, default is off)\n");
587 mini_trace_usage (void)
591 " --trace[=EXPR] Trace every call, optional EXPR controls the scope\n"
593 "EXPR is composed of:\n"
594 " all All assemblies\n"
595 " none No assemblies\n"
596 " program Entry point assembly\n"
597 " assembly Specifies an assembly\n"
598 " M:Type:Method Specifies a method\n"
599 " N:Namespace Specifies a namespace\n"
600 " T:Type Specifies a type\n"
601 " +EXPR Includes expression\n"
602 " -EXPR Excludes expression\n");
605 static const char *info = ""
606 #ifdef HAVE_KW_THREAD
610 #endif /* HAVE_KW_THREAD */
612 #ifdef USE_INCLUDED_LIBGC
613 "\tGC: Included Boehm (with typed GC)\n"
615 #if HAVE_GC_GCJ_MALLOC
616 "\tGC: System Boehm (with typed GC)\n"
618 "\tGC: System Boehm (no typed GC available)\n"
623 #endif /* HAVE_BOEHM_GC */
624 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
625 "\tSIGSEGV : altstack\n"
627 "\tSIGSEGV : normal\n"
630 "\tGlobalization: ICU\n"
632 "\tGlobalization: none\n"
633 #endif /* HAVE_ICU */
637 mono_main (int argc, char* argv[])
639 MainThreadArgs main_args;
640 MonoAssembly *assembly;
641 MonoMethodDesc *desc;
645 const char* aname, *mname = NULL;
646 char *config_file = NULL;
648 int enable_debugging = FALSE;
649 guint32 opt, action = DO_EXEC;
650 MonoGraphOptions mono_graph_options = 0;
651 int mini_verbose = 0;
652 gboolean enable_profile = FALSE;
653 char *trace_options = NULL;
654 char *profile_options = NULL;
655 char *aot_options = NULL;
657 setlocale (LC_ALL, "");
659 if (mono_running_on_valgrind () && getenv ("MONO_VALGRIND_LEAK_CHECK")) {
660 GMemVTable mem_vtable;
663 * Instruct glib to use the system allocation functions so valgrind
664 * can track the memory allocated by the g_... functions.
666 memset (&mem_vtable, 0, sizeof (mem_vtable));
667 mem_vtable.malloc = malloc;
668 mem_vtable.realloc = realloc;
669 mem_vtable.free = free;
670 mem_vtable.calloc = calloc;
672 g_mem_set_vtable (&mem_vtable);
675 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
676 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
678 opt = parse_optimizations (NULL);
680 for (i = 1; i < argc; ++i) {
681 if (argv [i] [0] != '-')
683 if (strcmp (argv [i], "--regression") == 0) {
684 action = DO_REGRESSION;
685 } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
687 } else if (strcmp (argv [i], "--version") == 0 || strcmp (argv [i], "-V") == 0) {
688 g_print ("Mono JIT compiler version %s, (C) 2002-2004 Novell, Inc and Contributors. www.go-mono.com\n", VERSION);
691 const guchar *cerror;
692 const guchar *clibpath;
694 cerror = mono_check_corlib_version ();
695 clibpath = mono_defaults.corlib? mono_image_get_filename (mono_defaults.corlib): "unknown";
697 g_print ("The currently installed mscorlib doesn't match this runtime version.\n");
698 g_print ("The error is: %s\n", cerror);
699 g_print ("mscorlib.dll loaded at: %s\n", clibpath);
704 } else if (strcmp (argv [i], "--help") == 0 || strcmp (argv [i], "-h") == 0) {
707 } else if (strcmp (argv [i], "--help-trace") == 0){
710 } else if (strcmp (argv [i], "--help-devel") == 0){
711 mini_usage_jitdeveloper ();
713 } else if (strcmp (argv [i], "--list-opt") == 0){
714 mini_usage_list_opt ();
716 } else if (strncmp (argv [i], "--statfile", 10) == 0) {
717 mini_stats_fd = fopen (argv [++i], "w+");
718 } else if (strncmp (argv [i], "--optimize=", 11) == 0) {
719 opt = parse_optimizations (argv [i] + 11);
720 } else if (strncmp (argv [i], "-O=", 3) == 0) {
721 opt = parse_optimizations (argv [i] + 3);
722 } else if (strcmp (argv [i], "--config") == 0) {
723 config_file = argv [++i];
724 } else if (strcmp (argv [i], "--ncompile") == 0) {
725 count = atoi (argv [++i]);
727 } else if (strcmp (argv [i], "--trace") == 0) {
728 trace_options = (char*)"";
729 } else if (strncmp (argv [i], "--trace=", 8) == 0) {
730 trace_options = &argv [i][8];
731 } else if (strcmp (argv [i], "--breakonex") == 0) {
732 mono_break_on_exc = TRUE;
733 } else if (strcmp (argv [i], "--break") == 0) {
734 if (!mono_debugger_insert_breakpoint (argv [++i], FALSE))
735 g_error ("Invalid method name '%s'", argv [i]);
736 } else if (strcmp (argv [i], "--print-vtable") == 0) {
737 mono_print_vtable = TRUE;
738 } else if (strcmp (argv [i], "--stats") == 0) {
739 mono_stats.enabled = TRUE;
740 mono_jit_stats.enabled = TRUE;
742 } else if (strcmp (argv [i], "--aot") == 0) {
743 mono_compile_aot = TRUE;
744 } else if (strncmp (argv [i], "--aot=", 6) == 0) {
745 mono_compile_aot = TRUE;
746 aot_options = &argv [i][6];
748 } else if (strcmp (argv [i], "--compile-all") == 0) {
750 } else if (strcmp (argv [i], "--profile") == 0) {
751 enable_profile = TRUE;
752 profile_options = NULL;
753 } else if (strncmp (argv [i], "--profile=", 10) == 0) {
754 enable_profile = TRUE;
755 profile_options = argv [i] + 10;
756 } else if (strcmp (argv [i], "--compile") == 0) {
759 } else if (strncmp (argv [i], "--graph=", 8) == 0) {
760 mono_graph_options = mono_parse_graph_options (argv [i] + 8);
763 } else if (strcmp (argv [i], "--graph") == 0) {
765 mono_graph_options = MONO_GRAPH_CFG;
767 } else if (strcmp (argv [i], "--debug") == 0) {
768 enable_debugging = TRUE;
769 } else if (strcmp (argv [i], "--security") == 0) {
770 mono_use_security_manager = TRUE;
771 mono_activate_security_manager ();
773 fprintf (stderr, "Unknown command line option: '%s'\n", argv [i]);
783 if (mono_compile_aot || action == DO_EXEC) {
784 g_set_prgname (argv[i]);
787 if (enable_profile) {
788 /* Needed because of TLS accesses in mono_profiler_load () */
790 mono_profiler_load (profile_options);
793 if (trace_options != NULL){
795 * Need to call this before mini_init () so we can trace methods
796 * compiled there too.
798 mono_jit_trace_calls = mono_trace_parse_options (trace_options);
799 if (mono_jit_trace_calls == NULL)
803 mono_set_defaults (mini_verbose, opt);
804 domain = mini_init (argv [i]);
808 if (mini_regression_list (mini_verbose, argc -i, argv + i)) {
809 g_print ("Regression ERRORS!\n");
810 mini_cleanup (domain);
813 mini_cleanup (domain);
816 if (argc - i != 1 || mname == NULL) {
817 g_print ("Usage: mini --ncompile num --compile method assembly\n");
818 mini_cleanup (domain);
826 mini_cleanup (domain);
832 if (argc - i != 1 || mname == NULL) {
834 mini_cleanup (domain);
842 mini_cleanup (domain);
849 if (enable_debugging) {
850 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
851 mono_debug_init_1 (domain);
854 /* Parse gac loading options before loading assemblies. */
855 if (mono_compile_aot || action == DO_EXEC) {
856 mono_config_parse (config_file);
859 assembly = mono_assembly_open (aname, NULL);
861 fprintf (stderr, "cannot open assembly %s\n", aname);
862 mini_cleanup (domain);
866 if (trace_options != NULL)
867 mono_trace_set_assembly (assembly);
869 if (enable_debugging)
870 mono_debug_init_2 (assembly);
872 if (mono_compile_aot || action == DO_EXEC) {
875 //mono_set_rootdir ();
877 error = mono_check_corlib_version ();
879 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
880 fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
884 main_args.domain = domain;
885 main_args.file = aname;
886 main_args.argc = argc - i;
887 main_args.argv = argv + i;
888 main_args.opts = opt;
889 main_args.aot_options = aot_options;
891 mono_runtime_exec_managed_code (domain, main_thread_handler, &main_args);
893 main_thread_handler (&main_args);
894 mono_thread_manage ();
896 mini_cleanup (domain);
898 /* Look up return value from System.Environment.ExitCode */
899 i = mono_environment_exitcode_get ();
901 } else if (action == DO_COMPILE) {
902 compile_all_methods (assembly, mini_verbose);
903 mini_cleanup (domain);
906 desc = mono_method_desc_new (mname, 0);
908 g_print ("Invalid method name %s\n", mname);
909 mini_cleanup (domain);
912 method = mono_method_desc_search_in_image (desc, mono_assembly_get_image (assembly));
914 g_print ("Cannot find method %s\n", mname);
915 mini_cleanup (domain);
919 if (action == DO_DRAW) {
922 switch (mono_graph_options) {
923 case MONO_GRAPH_DTREE:
925 opt |= MONO_OPT_LOOP;
927 case MONO_GRAPH_CFG_CODE:
930 case MONO_GRAPH_CFG_SSA:
933 case MONO_GRAPH_CFG_OPTCODE:
940 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
941 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
943 nm = mono_marshal_get_native_wrapper (method);
944 cfg = mini_method_compile (nm, opt, mono_get_root_domain (), FALSE, FALSE, part);
947 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, part);
948 if ((mono_graph_options & MONO_GRAPH_CFG_SSA) && !(cfg->comp_done & MONO_COMP_SSA)) {
949 g_warning ("no SSA info available (use -O=deadce)");
952 mono_draw_graph (cfg, mono_graph_options);
953 mono_destroy_compile (cfg);
955 } else if (action == DO_BENCH) {
958 double no_opt_time = 0.0;
959 GTimer *timer = g_timer_new ();
960 fprintf (mini_stats_fd, "$stattitle = \'Compilations times for %s\';\n",
961 mono_method_full_name (method, TRUE));
962 fprintf (mini_stats_fd, "@data = (\n");
963 fprintf (mini_stats_fd, "[");
964 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
969 fprintf (mini_stats_fd, "\"%s\",", n);
971 fprintf (mini_stats_fd, "],\n[");
973 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
977 g_timer_start (timer);
978 for (j = 0; j < count; ++j) {
979 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
980 mono_destroy_compile (cfg);
982 g_timer_stop (timer);
983 elapsed = g_timer_elapsed (timer, NULL);
985 no_opt_time = elapsed;
986 fprintf (mini_stats_fd, "%f, ", elapsed);
988 fprintf (mini_stats_fd, "]");
989 if (no_opt_time > 0.0) {
990 fprintf (mini_stats_fd, ", \n[");
991 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++)
992 fprintf (mini_stats_fd, "%f,", no_opt_time);
993 fprintf (mini_stats_fd, "]");
995 fprintf (mini_stats_fd, ");\n");
997 for (i = 0; i < count; ++i) {
998 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
999 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
1000 method = mono_marshal_get_native_wrapper (method);
1002 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1003 mono_destroy_compile (cfg);
1007 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1008 mono_destroy_compile (cfg);
1011 mini_cleanup (domain);
1016 mono_jit_init (const char *file)
1018 return mini_init (file);
1022 mono_jit_cleanup (MonoDomain *domain)
1024 mini_cleanup (domain);