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/security-manager.h>
40 #include <mono/os/gc_wrapper.h>
41 #include "mono/utils/mono-counters.h"
50 static FILE *mini_stats_fd = NULL;
52 static void mini_usage (void);
54 extern int mini_wapi_hps (int argc, char **argv);
55 extern int mini_wapi_semdel (int argc, char **argv);
56 extern int mini_wapi_seminfo (int argc, char **argv);
58 /* This turns off command line globbing under win32 */
63 typedef void (*OptFunc) (const char *p);
65 /* keep in sync with enum in mini.h */
74 {"peephole", "Peephole postpass"},
75 {"branch", "Branch optimizations"},
76 {"inline", "Inline method calls"},
77 {"cfold", "Constant folding"},
78 {"consprop", "Constant propagation"},
79 {"copyprop", "Copy propagation"},
80 {"deadce", "Dead code elimination"},
81 {"linears", "Linear scan global reg allocation"},
82 {"cmov", "Conditional moves"},
83 {"shared", "Emit per-domain code"},
84 {"sched", "Instruction scheduling"},
85 {"intrins", "Intrinsic method implementations"},
86 {"tailc", "Tail recursion and tail calls"},
87 {"loop", "Loop related optimizations"},
88 {"fcmov", "Fast x86 FP compares"},
89 {"leaf", "Leaf procedures optimizations"},
90 {"aot", "Usage of Ahead Of Time compiled code"},
91 {"precomp", "Precompile all methods before executing Main"},
92 {"abcrem", "Array bound checks removal"},
93 {"ssapre", "SSA based Partial Redundancy Elimination"},
94 {"exception", "Optimize exception catch blocks"},
95 {"ssa", "Use plain SSA form"},
96 {"treeprop", "Tree propagation"}
99 #define DEFAULT_OPTIMIZATIONS ( \
100 MONO_OPT_PEEPHOLE | \
106 MONO_OPT_EXCEPTION | \
109 #define EXCLUDED_FROM_ALL (MONO_OPT_SHARED | MONO_OPT_PRECOMP)
112 parse_optimizations (const char* p)
114 /* the default value */
115 guint32 opt = DEFAULT_OPTIMIZATIONS;
120 /* call out to cpu detection code here that sets the defaults ... */
121 opt |= mono_arch_cpu_optimizazions (&exclude);
133 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
134 n = opt_names [i].name;
136 if (strncmp (p, n, len) == 0) {
145 } else if (*p == '=') {
147 if (opt_names [i].func)
148 opt_names [i].func (p);
149 while (*p && *p++ != ',');
156 if (i == G_N_ELEMENTS (opt_names)) {
157 if (strncmp (p, "all", 3) == 0) {
161 opt = ~(EXCLUDED_FROM_ALL | exclude);
166 fprintf (stderr, "Invalid optimization name `%s'\n", p);
177 MonoGraphOptions value;
180 static const GraphName
182 {"cfg", "Control Flow Graph (CFG)" , MONO_GRAPH_CFG},
183 {"dtree", "Dominator Tree", MONO_GRAPH_DTREE},
184 {"code", "CFG showing code", MONO_GRAPH_CFG_CODE},
185 {"ssa", "CFG showing code after SSA translation", MONO_GRAPH_CFG_SSA},
186 {"optcode", "CFG showing code after IR optimizations", MONO_GRAPH_CFG_OPTCODE}
189 static MonoGraphOptions
190 mono_parse_graph_options (const char* p)
195 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
196 n = graph_names [i].name;
198 if (strncmp (p, n, len) == 0)
199 return graph_names [i].value;
202 fprintf (stderr, "Invalid graph name provided: %s\n", p);
207 mono_parse_default_optimizations (const char* p)
211 opt = parse_optimizations (p);
216 opt_descr (guint32 flags) {
217 GString *str = g_string_new ("");
221 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
222 if (flags & (1 << i)) {
224 g_string_append_c (str, ',');
225 g_string_append (str, opt_names [i].name);
229 return g_string_free (str, FALSE);
239 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_INTRINS,
240 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
241 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
242 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_CFOLD,
243 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
244 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,
245 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_SSA,
246 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_EXCEPTION,
247 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_EXCEPTION | MONO_OPT_ABCREM,
248 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_EXCEPTION | MONO_OPT_ABCREM | MONO_OPT_SSAPRE,
249 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,
250 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_TREEPROP,
251 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,
252 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
255 typedef int (*TestMethod) (void);
259 domain_dump_native_code (MonoDomain *domain) {
260 // need to poke into the domain, move to metadata/domain.c
261 // need to empty jit_info_table and code_mp
266 mini_regression (MonoImage *image, int verbose, int *total_run) {
267 guint32 i, opt, opt_flags;
271 int result, expected, failed, cfailed, run, code_size, total;
273 GTimer *timer = g_timer_new ();
276 fprintf (mini_stats_fd, "$stattitle = \'Mono Benchmark Results (various optimizations)\';\n");
278 fprintf (mini_stats_fd, "$graph->set_legend(qw(");
279 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); opt++) {
280 opt_flags = opt_sets [opt];
281 n = opt_descr (opt_flags);
285 fprintf (mini_stats_fd, " ");
286 fprintf (mini_stats_fd, "%s", n);
290 fprintf (mini_stats_fd, "));\n");
292 fprintf (mini_stats_fd, "@data = (\n");
293 fprintf (mini_stats_fd, "[");
296 /* load the metadata */
297 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
298 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
299 mono_class_init (method->klass);
301 if (!strncmp (method->name, "test_", 5) && mini_stats_fd) {
302 fprintf (mini_stats_fd, "\"%s\",", method->name);
306 fprintf (mini_stats_fd, "],\n");
311 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
312 double elapsed, comp_time, start_time;
315 opt_flags = opt_sets [opt];
316 mono_set_defaults (verbose, opt_flags);
317 n = opt_descr (opt_flags);
318 g_print ("Test run: image=%s, opts=%s\n", mono_image_get_filename (image), n);
320 cfailed = failed = run = code_size = 0;
321 comp_time = elapsed = 0.0;
323 /* fixme: ugly hack - delete all previously compiled methods */
324 g_hash_table_destroy (mono_domain_get ()->jit_trampoline_hash);
325 mono_domain_get ()->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
326 g_hash_table_destroy (mono_domain_get ()->jit_code_hash);
327 mono_domain_get ()->jit_code_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
329 g_timer_start (timer);
331 fprintf (mini_stats_fd, "[");
332 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
333 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
334 if (strncmp (method->name, "test_", 5) == 0) {
335 expected = atoi (method->name + 5);
337 start_time = g_timer_elapsed (timer, NULL);
338 comp_time -= start_time;
339 cfg = mini_method_compile (method, opt_flags, mono_get_root_domain (), TRUE, FALSE, 0);
340 comp_time += g_timer_elapsed (timer, NULL);
341 if (cfg->exception_type == MONO_EXCEPTION_NONE) {
343 g_print ("Running '%s' ...\n", method->name);
344 #ifdef MONO_USE_AOT_COMPILER
345 if ((jinfo = mono_aot_get_method (mono_get_root_domain (), method)))
346 func = jinfo->code_start;
349 func = (TestMethod)(gpointer)cfg->native_code;
350 func = (TestMethod)mono_create_ftnptr (mono_get_root_domain (), func);
352 if (result != expected) {
354 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
356 code_size += cfg->code_len;
357 mono_destroy_compile (cfg);
362 g_print ("Test '%s' failed compilation.\n", method->name);
365 fprintf (mini_stats_fd, "%f, ",
366 g_timer_elapsed (timer, NULL) - start_time);
370 fprintf (mini_stats_fd, "],\n");
371 g_timer_stop (timer);
372 elapsed = g_timer_elapsed (timer, NULL);
373 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
374 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
375 g_print ("Elapsed time: %f secs (%f, %f), Code size: %d\n\n", elapsed,
376 elapsed - comp_time, comp_time, code_size);
377 total += failed + cfailed;
382 fprintf (mini_stats_fd, ");\n");
383 fflush (mini_stats_fd);
386 g_timer_destroy (timer);
391 mini_regression_list (int verbose, int count, char *images [])
393 int i, total, total_run, run;
396 total_run = total = 0;
397 for (i = 0; i < count; ++i) {
398 ass = mono_assembly_open (images [i], NULL);
400 g_warning ("failed to load assembly: %s", images [i]);
403 total += mini_regression (mono_assembly_get_image (ass), verbose, &run);
406 g_print ("Overall results: tests: %d, failed: %d, opt combinations: %d (pass: %.2f%%)\n",
407 total_run, total, (int)G_N_ELEMENTS (opt_sets), 100.0*(total_run-total)/total_run);
420 typedef struct CompileAllThreadArgs {
424 } CompileAllThreadArgs;
427 compile_all_methods_thread_main (CompileAllThreadArgs *args)
429 MonoAssembly *ass = args->ass;
430 int verbose = args->verbose;
431 MonoImage *image = mono_assembly_get_image (ass);
436 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
437 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
438 MonoMethodSignature *sig;
440 if (mono_metadata_has_generic_params (image, token))
443 method = mono_get_method (image, token, NULL);
444 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
445 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
446 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
447 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
450 if (method->klass->generic_container)
452 sig = mono_method_signature (method);
453 if (sig->has_type_parameters)
458 char * desc = mono_method_full_name (method, TRUE);
459 g_print ("Compiling %d %s\n", count, desc);
462 cfg = mini_method_compile (method, args->opts, mono_get_root_domain (), FALSE, FALSE, 0);
463 mono_destroy_compile (cfg);
469 compile_all_methods (MonoAssembly *ass, int verbose, guint32 opts)
471 CompileAllThreadArgs args;
474 args.verbose = verbose;
478 * Need to create a mono thread since compilation might trigger
479 * running of managed code.
481 mono_thread_create (mono_domain_get (), compile_all_methods_thread_main, &args);
483 mono_thread_manage ();
488 * @assembly: reference to an assembly
489 * @argc: argument count
490 * @argv: argument vector
492 * Start execution of a program.
495 mono_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
497 MonoImage *image = mono_assembly_get_image (assembly);
499 guint32 entry = mono_image_get_entry_point (image);
502 g_print ("Assembly '%s' doesn't have an entry point.\n", mono_image_get_filename (image));
503 /* FIXME: remove this silly requirement. */
504 mono_environment_exitcode_set (1);
508 method = mono_get_method (image, entry, NULL);
510 return mono_runtime_run_main (method, argc, argv, NULL);
523 static void main_thread_handler (gpointer user_data)
525 MainThreadArgs *main_args = user_data;
526 MonoAssembly *assembly;
528 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
530 fprintf (stderr, "Can not open image %s\n", main_args->file);
534 if (mono_compile_aot) {
535 int res = mono_compile_assembly (assembly, main_args->opts, main_args->aot_options);
536 printf ("AOT RESULT %d\n", res);
539 * This must be done in a thread managed by mono since it can invoke
542 if (main_args->opts & MONO_OPT_PRECOMP)
543 mono_precompile_assemblies ();
545 mono_jit_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
550 mini_usage_jitdeveloper (void)
555 "Runtime and JIT debugging options:\n"
556 " --breakonex Inserts a breakpoint on exceptions\n"
557 " --break METHOD Inserts a breakpoint at METHOD entry\n"
558 " --compile METHOD Just compile METHOD in assembly\n"
559 " --compile-all Compiles all the methods in the assembly\n"
560 " --ncompile N Number of times to compile METHOD (default: 1)\n"
561 " --print-vtable Print the vtable of all used classes\n"
562 " --regression Runs the regression test contained in the assembly\n"
563 " --statfile FILE Sets the stat file to FILE\n"
564 " --stats Print statistics about the JIT operations\n"
565 " --wapi=hps|semdel IO-layer maintenance\n"
568 " --graph[=TYPE] METHOD Draws a graph of the specified method:\n");
570 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
571 fprintf (stdout, " %-10s %s\n", graph_names [i].name, graph_names [i].desc);
576 mini_usage_list_opt (void)
580 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
581 fprintf (stdout, " %-10s %s\n", opt_names [i].name, opt_names [i].desc);
588 "Usage is: mono [options] program [program-options]\n"
591 " --aot Compiles the assembly to native code\n"
592 " --debug Enable debugging support\n"
593 " --profile[=profiler] Runs in profiling mode with the specified profiler module\n"
594 " --trace[=EXPR] Enable tracing, use --help-trace for details\n"
595 " --help-devel Shows more options available to developers\n"
598 " --config FILE Loads FILE as the Mono config\n"
599 " --verbose, -v Increases the verbosity level\n"
600 " --help, -h Show usage information\n"
601 " --version, -V Show version information\n"
602 " --optimize=OPT Turns on or off a specific optimization\n"
603 " Use --list-opt to get a list of optimizations\n"
604 " --security Turns on the security manager (unsupported, default is off)\n");
608 mini_trace_usage (void)
612 " --trace[=EXPR] Trace every call, optional EXPR controls the scope\n"
614 "EXPR is composed of:\n"
615 " all All assemblies\n"
616 " none No assemblies\n"
617 " program Entry point assembly\n"
618 " assembly Specifies an assembly\n"
619 " M:Type:Method Specifies a method\n"
620 " N:Namespace Specifies a namespace\n"
621 " T:Type Specifies a type\n"
622 " +EXPR Includes expression\n"
623 " -EXPR Excludes expression\n"
624 " disabled Don't print any output until toggled via SIGUSR2\n");
627 static const char info[] =
628 #ifdef HAVE_KW_THREAD
632 #endif /* HAVE_KW_THREAD */
633 "\tGC: " USED_GC_NAME "\n"
634 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
635 "\tSIGSEGV: altstack\n"
637 "\tSIGSEGV: normal\n"
639 "\tDisabled: " DISABLED_FEATURES "\n"
643 mono_main (int argc, char* argv[])
645 MainThreadArgs main_args;
646 MonoAssembly *assembly;
647 MonoMethodDesc *desc;
651 const char* aname, *mname = NULL;
652 char *config_file = NULL;
654 int enable_debugging = FALSE;
655 guint32 opt, action = DO_EXEC;
656 MonoGraphOptions mono_graph_options = 0;
657 int mini_verbose = 0;
658 gboolean enable_profile = FALSE;
659 char *trace_options = NULL;
660 char *profile_options = NULL;
661 char *aot_options = NULL;
663 setlocale (LC_ALL, "");
665 if (mono_running_on_valgrind () && getenv ("MONO_VALGRIND_LEAK_CHECK")) {
666 GMemVTable mem_vtable;
669 * Instruct glib to use the system allocation functions so valgrind
670 * can track the memory allocated by the g_... functions.
672 memset (&mem_vtable, 0, sizeof (mem_vtable));
673 mem_vtable.malloc = malloc;
674 mem_vtable.realloc = realloc;
675 mem_vtable.free = free;
676 mem_vtable.calloc = calloc;
678 g_mem_set_vtable (&mem_vtable);
681 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
682 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
684 opt = parse_optimizations (NULL);
686 for (i = 1; i < argc; ++i) {
687 if (argv [i] [0] != '-')
689 if (strcmp (argv [i], "--regression") == 0) {
690 action = DO_REGRESSION;
691 } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
693 } else if (strcmp (argv [i], "--version") == 0 || strcmp (argv [i], "-V") == 0) {
694 g_print ("Mono JIT compiler version %s, (C) 2002-2005 Novell, Inc and Contributors. www.mono-project.com\n", VERSION);
698 const char *clibpath;
700 cerror = mono_check_corlib_version ();
701 clibpath = mono_defaults.corlib? mono_image_get_filename (mono_defaults.corlib): "unknown";
703 g_print ("The currently installed mscorlib doesn't match this runtime version.\n");
704 g_print ("The error is: %s\n", cerror);
705 g_print ("mscorlib.dll loaded at: %s\n", clibpath);
710 } else if (strcmp (argv [i], "--help") == 0 || strcmp (argv [i], "-h") == 0) {
713 } else if (strcmp (argv [i], "--help-trace") == 0){
716 } else if (strcmp (argv [i], "--help-devel") == 0){
717 mini_usage_jitdeveloper ();
719 } else if (strcmp (argv [i], "--list-opt") == 0){
720 mini_usage_list_opt ();
722 } else if (strncmp (argv [i], "--statfile", 10) == 0) {
724 fprintf (stderr, "error: --statfile requires a filename argument\n");
727 mini_stats_fd = fopen (argv [++i], "w+");
728 } else if (strncmp (argv [i], "--optimize=", 11) == 0) {
729 opt = parse_optimizations (argv [i] + 11);
730 } else if (strncmp (argv [i], "-O=", 3) == 0) {
731 opt = parse_optimizations (argv [i] + 3);
732 } else if (strcmp (argv [i], "--config") == 0) {
734 fprintf (stderr, "error: --config requires a filename argument\n");
737 config_file = argv [++i];
738 } else if (strcmp (argv [i], "--ncompile") == 0) {
740 fprintf (stderr, "error: --ncompile requires an argument\n");
743 count = atoi (argv [++i]);
745 } else if (strcmp (argv [i], "--trace") == 0) {
746 trace_options = (char*)"";
747 } else if (strncmp (argv [i], "--trace=", 8) == 0) {
748 trace_options = &argv [i][8];
749 } else if (strcmp (argv [i], "--breakonex") == 0) {
750 mono_break_on_exc = TRUE;
751 } else if (strcmp (argv [i], "--break") == 0) {
753 fprintf (stderr, "Missing method name in --break command line option\n");
757 if (!mono_debugger_insert_breakpoint (argv [++i], FALSE))
758 fprintf (stderr, "Error: invalid method name '%s'\n", argv [i]);
759 } else if (strcmp (argv [i], "--print-vtable") == 0) {
760 mono_print_vtable = TRUE;
761 } else if (strcmp (argv [i], "--stats") == 0) {
762 mono_counters_enable (-1);
763 mono_stats.enabled = TRUE;
764 mono_jit_stats.enabled = TRUE;
766 } else if (strcmp (argv [i], "--aot") == 0) {
767 mono_compile_aot = TRUE;
768 } else if (strncmp (argv [i], "--aot=", 6) == 0) {
769 mono_compile_aot = TRUE;
770 aot_options = &argv [i][6];
772 } else if (strcmp (argv [i], "--compile-all") == 0) {
774 } else if (strcmp (argv [i], "--profile") == 0) {
775 enable_profile = TRUE;
776 profile_options = NULL;
777 } else if (strncmp (argv [i], "--profile=", 10) == 0) {
778 enable_profile = TRUE;
779 profile_options = argv [i] + 10;
780 } else if (strcmp (argv [i], "--compile") == 0) {
782 fprintf (stderr, "error: --compile option requires a method name argument\n");
788 } else if (strncmp (argv [i], "--graph=", 8) == 0) {
790 fprintf (stderr, "error: --graph option requires a method name argument\n");
794 mono_graph_options = mono_parse_graph_options (argv [i] + 8);
797 } else if (strcmp (argv [i], "--graph") == 0) {
799 fprintf (stderr, "error: --graph option requires a method name argument\n");
804 mono_graph_options = MONO_GRAPH_CFG;
806 } else if (strcmp (argv [i], "--debug") == 0) {
807 enable_debugging = TRUE;
808 } else if (strcmp (argv [i], "--security") == 0) {
809 mono_use_security_manager = TRUE;
810 mono_activate_security_manager ();
811 } else if (strcmp (argv [i], "--desktop") == 0) {
812 #if defined (HAVE_BOEHM_GC)
815 /* Put desktop-specific optimizations here */
816 } else if (strcmp (argv [i], "--server") == 0){
817 /* Put server-specific optimizations here */
818 } else if (strcmp (argv [i], "--inside-mdb") == 0) {
819 action = DO_DEBUGGER;
820 } else if (strncmp (argv [i], "--wapi=", 7) == 0) {
821 if (strcmp (argv [i] + 7, "hps") == 0) {
822 return mini_wapi_hps (argc - i, argv + i);
823 } else if (strcmp (argv [i] + 7, "semdel") == 0) {
824 return mini_wapi_semdel (argc - i, argv + i);
825 } else if (strcmp (argv [i] + 7, "seminfo") == 0) {
826 return mini_wapi_seminfo (argc - i, argv + i);
828 fprintf (stderr, "Invalid --wapi suboption: '%s'\n", argv [i]);
832 fprintf (stderr, "Unknown command line option: '%s'\n", argv [i]);
842 if (mono_compile_aot || action == DO_EXEC || action == DO_DEBUGGER) {
843 g_set_prgname (argv[i]);
846 if (enable_profile) {
847 /* Needed because of TLS accesses in mono_profiler_load () */
849 mono_profiler_load (profile_options);
852 if (trace_options != NULL){
854 * Need to call this before mini_init () so we can trace methods
855 * compiled there too.
857 mono_jit_trace_calls = mono_trace_parse_options (trace_options);
858 if (mono_jit_trace_calls == NULL)
862 if (action == DO_DEBUGGER) {
863 opt |= MONO_OPT_SHARED;
864 enable_debugging = TRUE;
866 #ifdef MONO_DEBUGGER_SUPPORTED
867 mono_debug_init (MONO_DEBUG_FORMAT_DEBUGGER);
868 mono_debugger_init ();
870 g_print ("The Mono Debugger is not supported on this platform.\n");
875 mono_set_defaults (mini_verbose, opt);
876 domain = mini_init (argv [i]);
880 if (mini_regression_list (mini_verbose, argc -i, argv + i)) {
881 g_print ("Regression ERRORS!\n");
882 mini_cleanup (domain);
885 mini_cleanup (domain);
888 if (argc - i != 1 || mname == NULL) {
889 g_print ("Usage: mini --ncompile num --compile method assembly\n");
890 mini_cleanup (domain);
898 mini_cleanup (domain);
904 if (argc - i != 1 || mname == NULL) {
906 mini_cleanup (domain);
914 mini_cleanup (domain);
921 if (action == DO_DEBUGGER)
922 mono_debug_init_1 (domain);
923 else if (enable_debugging) {
924 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
925 mono_debug_init_1 (domain);
928 /* Parse gac loading options before loading assemblies. */
929 if (mono_compile_aot || action == DO_EXEC || action == DO_DEBUGGER) {
930 mono_config_parse (config_file);
933 assembly = mono_assembly_open (aname, NULL);
935 fprintf (stderr, "cannot open assembly %s\n", aname);
936 mini_cleanup (domain);
940 if (trace_options != NULL)
941 mono_trace_set_assembly (assembly);
943 if (enable_debugging)
944 mono_debug_init_2 (assembly);
946 if (mono_compile_aot || action == DO_EXEC) {
949 //mono_set_rootdir ();
951 error = mono_check_corlib_version ();
953 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
954 fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
958 #ifdef PLATFORM_WIN32
959 /* Detach console when executing IMAGE_SUBSYSTEM_WINDOWS_GUI on win32 */
960 if (!mono_compile_aot && ((MonoCLIImageInfo*)(mono_assembly_get_image (assembly)->image_info))->cli_header.nt.pe_subsys_required == IMAGE_SUBSYSTEM_WINDOWS_GUI)
964 main_args.domain = domain;
965 main_args.file = aname;
966 main_args.argc = argc - i;
967 main_args.argv = argv + i;
968 main_args.opts = opt;
969 main_args.aot_options = aot_options;
971 mono_runtime_exec_managed_code (domain, main_thread_handler, &main_args);
973 main_thread_handler (&main_args);
974 mono_thread_manage ();
976 mini_cleanup (domain);
978 /* Look up return value from System.Environment.ExitCode */
979 i = mono_environment_exitcode_get ();
981 } else if (action == DO_COMPILE) {
982 compile_all_methods (assembly, mini_verbose, opt);
983 mini_cleanup (domain);
985 } else if (action == DO_DEBUGGER) {
986 #ifdef MONO_DEBUGGER_SUPPORTED
989 error = mono_check_corlib_version ();
991 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
992 fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
996 mono_debugger_main (domain, assembly, argc, argv);
997 mini_cleanup (domain);
1003 desc = mono_method_desc_new (mname, 0);
1005 g_print ("Invalid method name %s\n", mname);
1006 mini_cleanup (domain);
1009 method = mono_method_desc_search_in_image (desc, mono_assembly_get_image (assembly));
1011 g_print ("Cannot find method %s\n", mname);
1012 mini_cleanup (domain);
1016 if (action == DO_DRAW) {
1019 switch (mono_graph_options) {
1020 case MONO_GRAPH_DTREE:
1022 opt |= MONO_OPT_LOOP;
1024 case MONO_GRAPH_CFG_CODE:
1027 case MONO_GRAPH_CFG_SSA:
1030 case MONO_GRAPH_CFG_OPTCODE:
1037 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1038 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
1040 nm = mono_marshal_get_native_wrapper (method);
1041 cfg = mini_method_compile (nm, opt, mono_get_root_domain (), FALSE, FALSE, part);
1044 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, part);
1045 if ((mono_graph_options & MONO_GRAPH_CFG_SSA) && !(cfg->comp_done & MONO_COMP_SSA)) {
1046 g_warning ("no SSA info available (use -O=deadce)");
1049 mono_draw_graph (cfg, mono_graph_options);
1050 mono_destroy_compile (cfg);
1052 } else if (action == DO_BENCH) {
1053 if (mini_stats_fd) {
1055 double no_opt_time = 0.0;
1056 GTimer *timer = g_timer_new ();
1057 fprintf (mini_stats_fd, "$stattitle = \'Compilations times for %s\';\n",
1058 mono_method_full_name (method, TRUE));
1059 fprintf (mini_stats_fd, "@data = (\n");
1060 fprintf (mini_stats_fd, "[");
1061 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
1063 n = opt_descr (opt);
1066 fprintf (mini_stats_fd, "\"%s\",", n);
1068 fprintf (mini_stats_fd, "],\n[");
1070 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
1074 g_timer_start (timer);
1075 for (j = 0; j < count; ++j) {
1076 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1077 mono_destroy_compile (cfg);
1079 g_timer_stop (timer);
1080 elapsed = g_timer_elapsed (timer, NULL);
1082 no_opt_time = elapsed;
1083 fprintf (mini_stats_fd, "%f, ", elapsed);
1085 fprintf (mini_stats_fd, "]");
1086 if (no_opt_time > 0.0) {
1087 fprintf (mini_stats_fd, ", \n[");
1088 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++)
1089 fprintf (mini_stats_fd, "%f,", no_opt_time);
1090 fprintf (mini_stats_fd, "]");
1092 fprintf (mini_stats_fd, ");\n");
1094 for (i = 0; i < count; ++i) {
1095 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1096 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
1097 method = mono_marshal_get_native_wrapper (method);
1099 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1100 mono_destroy_compile (cfg);
1104 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1105 mono_destroy_compile (cfg);
1108 mini_cleanup (domain);
1113 mono_jit_init (const char *file)
1115 return mini_init (file);
1119 mono_jit_cleanup (MonoDomain *domain)
1121 mini_cleanup (domain);