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-2006 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 g_print ("The entry point method could not be loaded\n");
511 mono_environment_exitcode_set (1);
515 return mono_runtime_run_main (method, argc, argv, NULL);
528 static void main_thread_handler (gpointer user_data)
530 MainThreadArgs *main_args = user_data;
531 MonoAssembly *assembly;
533 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
535 fprintf (stderr, "Can not open image %s\n", main_args->file);
539 if (mono_compile_aot) {
540 int res = mono_compile_assembly (assembly, main_args->opts, main_args->aot_options);
541 printf ("AOT RESULT %d\n", res);
544 * This must be done in a thread managed by mono since it can invoke
547 if (main_args->opts & MONO_OPT_PRECOMP)
548 mono_precompile_assemblies ();
550 mono_jit_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
555 mini_usage_jitdeveloper (void)
560 "Runtime and JIT debugging options:\n"
561 " --breakonex Inserts a breakpoint on exceptions\n"
562 " --break METHOD Inserts a breakpoint at METHOD entry\n"
563 " --compile METHOD Just compile METHOD in assembly\n"
564 " --compile-all Compiles all the methods in the assembly\n"
565 " --ncompile N Number of times to compile METHOD (default: 1)\n"
566 " --print-vtable Print the vtable of all used classes\n"
567 " --regression Runs the regression test contained in the assembly\n"
568 " --statfile FILE Sets the stat file to FILE\n"
569 " --stats Print statistics about the JIT operations\n"
570 " --wapi=hps|semdel IO-layer maintenance\n"
573 " --graph[=TYPE] METHOD Draws a graph of the specified method:\n");
575 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
576 fprintf (stdout, " %-10s %s\n", graph_names [i].name, graph_names [i].desc);
581 mini_usage_list_opt (void)
585 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
586 fprintf (stdout, " %-10s %s\n", opt_names [i].name, opt_names [i].desc);
593 "Usage is: mono [options] program [program-options]\n"
596 " --aot Compiles the assembly to native code\n"
597 " --debug Enable debugging support\n"
598 " --profile[=profiler] Runs in profiling mode with the specified profiler module\n"
599 " --trace[=EXPR] Enable tracing, use --help-trace for details\n"
600 " --help-devel Shows more options available to developers\n"
603 " --config FILE Loads FILE as the Mono config\n"
604 " --verbose, -v Increases the verbosity level\n"
605 " --help, -h Show usage information\n"
606 " --version, -V Show version information\n"
607 " --optimize=OPT Turns on or off a specific optimization\n"
608 " Use --list-opt to get a list of optimizations\n"
609 " --security Turns on the security manager (unsupported, default is off)\n");
613 mini_trace_usage (void)
617 " --trace[=EXPR] Trace every call, optional EXPR controls the scope\n"
619 "EXPR is composed of:\n"
620 " all All assemblies\n"
621 " none No assemblies\n"
622 " program Entry point assembly\n"
623 " assembly Specifies an assembly\n"
624 " M:Type:Method Specifies a method\n"
625 " N:Namespace Specifies a namespace\n"
626 " T:Type Specifies a type\n"
627 " +EXPR Includes expression\n"
628 " -EXPR Excludes expression\n"
629 " disabled Don't print any output until toggled via SIGUSR2\n");
632 static const char info[] =
633 #ifdef HAVE_KW_THREAD
637 #endif /* HAVE_KW_THREAD */
638 "\tGC: " USED_GC_NAME "\n"
639 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
640 "\tSIGSEGV: altstack\n"
642 "\tSIGSEGV: normal\n"
644 "\tDisabled: " DISABLED_FEATURES "\n"
648 mono_main (int argc, char* argv[])
650 MainThreadArgs main_args;
651 MonoAssembly *assembly;
652 MonoMethodDesc *desc;
656 const char* aname, *mname = NULL;
657 char *config_file = NULL;
659 int enable_debugging = FALSE;
660 guint32 opt, action = DO_EXEC;
661 MonoGraphOptions mono_graph_options = 0;
662 int mini_verbose = 0;
663 gboolean enable_profile = FALSE;
664 char *trace_options = NULL;
665 char *profile_options = NULL;
666 char *aot_options = NULL;
668 setlocale (LC_ALL, "");
670 if (mono_running_on_valgrind () && getenv ("MONO_VALGRIND_LEAK_CHECK")) {
671 GMemVTable mem_vtable;
674 * Instruct glib to use the system allocation functions so valgrind
675 * can track the memory allocated by the g_... functions.
677 memset (&mem_vtable, 0, sizeof (mem_vtable));
678 mem_vtable.malloc = malloc;
679 mem_vtable.realloc = realloc;
680 mem_vtable.free = free;
681 mem_vtable.calloc = calloc;
683 g_mem_set_vtable (&mem_vtable);
686 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
687 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
689 opt = parse_optimizations (NULL);
691 for (i = 1; i < argc; ++i) {
692 if (argv [i] [0] != '-')
694 if (strcmp (argv [i], "--regression") == 0) {
695 action = DO_REGRESSION;
696 } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
698 } else if (strcmp (argv [i], "--version") == 0 || strcmp (argv [i], "-V") == 0) {
699 g_print ("Mono JIT compiler version %s, (C) 2002-2006 Novell, Inc and Contributors. www.mono-project.com\n", VERSION);
703 const char *clibpath;
705 cerror = mono_check_corlib_version ();
706 clibpath = mono_defaults.corlib? mono_image_get_filename (mono_defaults.corlib): "unknown";
708 g_print ("The currently installed mscorlib doesn't match this runtime version.\n");
709 g_print ("The error is: %s\n", cerror);
710 g_print ("mscorlib.dll loaded at: %s\n", clibpath);
715 } else if (strcmp (argv [i], "--help") == 0 || strcmp (argv [i], "-h") == 0) {
718 } else if (strcmp (argv [i], "--help-trace") == 0){
721 } else if (strcmp (argv [i], "--help-devel") == 0){
722 mini_usage_jitdeveloper ();
724 } else if (strcmp (argv [i], "--list-opt") == 0){
725 mini_usage_list_opt ();
727 } else if (strncmp (argv [i], "--statfile", 10) == 0) {
729 fprintf (stderr, "error: --statfile requires a filename argument\n");
732 mini_stats_fd = fopen (argv [++i], "w+");
733 } else if (strncmp (argv [i], "--optimize=", 11) == 0) {
734 opt = parse_optimizations (argv [i] + 11);
735 } else if (strncmp (argv [i], "-O=", 3) == 0) {
736 opt = parse_optimizations (argv [i] + 3);
737 } else if (strcmp (argv [i], "--config") == 0) {
739 fprintf (stderr, "error: --config requires a filename argument\n");
742 config_file = argv [++i];
743 } else if (strcmp (argv [i], "--ncompile") == 0) {
745 fprintf (stderr, "error: --ncompile requires an argument\n");
748 count = atoi (argv [++i]);
750 } else if (strcmp (argv [i], "--trace") == 0) {
751 trace_options = (char*)"";
752 } else if (strncmp (argv [i], "--trace=", 8) == 0) {
753 trace_options = &argv [i][8];
754 } else if (strcmp (argv [i], "--breakonex") == 0) {
755 mono_break_on_exc = TRUE;
756 } else if (strcmp (argv [i], "--break") == 0) {
758 fprintf (stderr, "Missing method name in --break command line option\n");
762 if (!mono_debugger_insert_breakpoint (argv [++i], FALSE))
763 fprintf (stderr, "Error: invalid method name '%s'\n", argv [i]);
764 } else if (strcmp (argv [i], "--print-vtable") == 0) {
765 mono_print_vtable = TRUE;
766 } else if (strcmp (argv [i], "--stats") == 0) {
767 mono_counters_enable (-1);
768 mono_stats.enabled = TRUE;
769 mono_jit_stats.enabled = TRUE;
771 } else if (strcmp (argv [i], "--aot") == 0) {
772 mono_compile_aot = TRUE;
773 } else if (strncmp (argv [i], "--aot=", 6) == 0) {
774 mono_compile_aot = TRUE;
775 aot_options = &argv [i][6];
777 } else if (strcmp (argv [i], "--compile-all") == 0) {
779 } else if (strcmp (argv [i], "--profile") == 0) {
780 enable_profile = TRUE;
781 profile_options = NULL;
782 } else if (strncmp (argv [i], "--profile=", 10) == 0) {
783 enable_profile = TRUE;
784 profile_options = argv [i] + 10;
785 } else if (strcmp (argv [i], "--compile") == 0) {
787 fprintf (stderr, "error: --compile option requires a method name argument\n");
793 } else if (strncmp (argv [i], "--graph=", 8) == 0) {
795 fprintf (stderr, "error: --graph option requires a method name argument\n");
799 mono_graph_options = mono_parse_graph_options (argv [i] + 8);
802 } else if (strcmp (argv [i], "--graph") == 0) {
804 fprintf (stderr, "error: --graph option requires a method name argument\n");
809 mono_graph_options = MONO_GRAPH_CFG;
811 } else if (strcmp (argv [i], "--debug") == 0) {
812 enable_debugging = TRUE;
813 } else if (strcmp (argv [i], "--security") == 0) {
814 mono_use_security_manager = TRUE;
815 mono_activate_security_manager ();
816 } else if (strcmp (argv [i], "--desktop") == 0) {
817 #if defined (HAVE_BOEHM_GC)
820 /* Put desktop-specific optimizations here */
821 } else if (strcmp (argv [i], "--server") == 0){
822 /* Put server-specific optimizations here */
823 } else if (strcmp (argv [i], "--inside-mdb") == 0) {
824 action = DO_DEBUGGER;
825 } else if (strncmp (argv [i], "--wapi=", 7) == 0) {
826 if (strcmp (argv [i] + 7, "hps") == 0) {
827 return mini_wapi_hps (argc - i, argv + i);
828 } else if (strcmp (argv [i] + 7, "semdel") == 0) {
829 return mini_wapi_semdel (argc - i, argv + i);
830 } else if (strcmp (argv [i] + 7, "seminfo") == 0) {
831 return mini_wapi_seminfo (argc - i, argv + i);
833 fprintf (stderr, "Invalid --wapi suboption: '%s'\n", argv [i]);
837 fprintf (stderr, "Unknown command line option: '%s'\n", argv [i]);
847 if ((action == DO_EXEC) && g_getenv ("MONO_INSIDE_MDB"))
848 action = DO_DEBUGGER;
850 if (mono_compile_aot || action == DO_EXEC || action == DO_DEBUGGER) {
851 g_set_prgname (argv[i]);
854 if (enable_profile) {
855 /* Needed because of TLS accesses in mono_profiler_load () */
857 mono_profiler_load (profile_options);
860 if (trace_options != NULL){
862 * Need to call this before mini_init () so we can trace methods
863 * compiled there too.
865 mono_jit_trace_calls = mono_trace_parse_options (trace_options);
866 if (mono_jit_trace_calls == NULL)
870 if (action == DO_DEBUGGER) {
871 opt |= MONO_OPT_SHARED;
872 opt &= ~MONO_OPT_INLINE;
873 opt &= ~MONO_OPT_COPYPROP;
874 opt &= ~MONO_OPT_CONSPROP;
875 enable_debugging = TRUE;
877 #ifdef MONO_DEBUGGER_SUPPORTED
878 mono_debug_init (MONO_DEBUG_FORMAT_DEBUGGER);
879 mono_debugger_init ();
881 g_print ("The Mono Debugger is not supported on this platform.\n");
886 mono_set_defaults (mini_verbose, opt);
887 domain = mini_init (argv [i]);
891 if (mini_regression_list (mini_verbose, argc -i, argv + i)) {
892 g_print ("Regression ERRORS!\n");
893 mini_cleanup (domain);
896 mini_cleanup (domain);
899 if (argc - i != 1 || mname == NULL) {
900 g_print ("Usage: mini --ncompile num --compile method assembly\n");
901 mini_cleanup (domain);
909 mini_cleanup (domain);
915 if (argc - i != 1 || mname == NULL) {
917 mini_cleanup (domain);
925 mini_cleanup (domain);
932 if (action == DO_DEBUGGER)
933 mono_debug_init_1 (domain);
934 else if (enable_debugging) {
935 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
936 mono_debug_init_1 (domain);
939 /* Parse gac loading options before loading assemblies. */
940 if (mono_compile_aot || action == DO_EXEC || action == DO_DEBUGGER) {
941 mono_config_parse (config_file);
944 assembly = mono_assembly_open (aname, NULL);
946 fprintf (stderr, "cannot open assembly %s\n", aname);
947 mini_cleanup (domain);
951 if (trace_options != NULL)
952 mono_trace_set_assembly (assembly);
954 if (enable_debugging)
955 mono_debug_init_2 (assembly);
957 if (mono_compile_aot || action == DO_EXEC) {
960 //mono_set_rootdir ();
962 error = mono_check_corlib_version ();
964 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
965 fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
969 #ifdef PLATFORM_WIN32
970 /* Detach console when executing IMAGE_SUBSYSTEM_WINDOWS_GUI on win32 */
971 if (!mono_compile_aot && ((MonoCLIImageInfo*)(mono_assembly_get_image (assembly)->image_info))->cli_header.nt.pe_subsys_required == IMAGE_SUBSYSTEM_WINDOWS_GUI)
975 main_args.domain = domain;
976 main_args.file = aname;
977 main_args.argc = argc - i;
978 main_args.argv = argv + i;
979 main_args.opts = opt;
980 main_args.aot_options = aot_options;
982 mono_runtime_exec_managed_code (domain, main_thread_handler, &main_args);
984 main_thread_handler (&main_args);
985 mono_thread_manage ();
987 mini_cleanup (domain);
989 /* Look up return value from System.Environment.ExitCode */
990 i = mono_environment_exitcode_get ();
992 } else if (action == DO_COMPILE) {
993 compile_all_methods (assembly, mini_verbose, opt);
994 mini_cleanup (domain);
996 } else if (action == DO_DEBUGGER) {
997 #ifdef MONO_DEBUGGER_SUPPORTED
1000 error = mono_check_corlib_version ();
1002 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
1003 fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
1007 mono_debugger_main (domain, assembly, argc - i, argv + i);
1008 mini_cleanup (domain);
1014 desc = mono_method_desc_new (mname, 0);
1016 g_print ("Invalid method name %s\n", mname);
1017 mini_cleanup (domain);
1020 method = mono_method_desc_search_in_image (desc, mono_assembly_get_image (assembly));
1022 g_print ("Cannot find method %s\n", mname);
1023 mini_cleanup (domain);
1027 if (action == DO_DRAW) {
1030 switch (mono_graph_options) {
1031 case MONO_GRAPH_DTREE:
1033 opt |= MONO_OPT_LOOP;
1035 case MONO_GRAPH_CFG_CODE:
1038 case MONO_GRAPH_CFG_SSA:
1041 case MONO_GRAPH_CFG_OPTCODE:
1048 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1049 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
1051 nm = mono_marshal_get_native_wrapper (method);
1052 cfg = mini_method_compile (nm, opt, mono_get_root_domain (), FALSE, FALSE, part);
1055 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, part);
1056 if ((mono_graph_options & MONO_GRAPH_CFG_SSA) && !(cfg->comp_done & MONO_COMP_SSA)) {
1057 g_warning ("no SSA info available (use -O=deadce)");
1060 mono_draw_graph (cfg, mono_graph_options);
1061 mono_destroy_compile (cfg);
1063 } else if (action == DO_BENCH) {
1064 if (mini_stats_fd) {
1066 double no_opt_time = 0.0;
1067 GTimer *timer = g_timer_new ();
1068 fprintf (mini_stats_fd, "$stattitle = \'Compilations times for %s\';\n",
1069 mono_method_full_name (method, TRUE));
1070 fprintf (mini_stats_fd, "@data = (\n");
1071 fprintf (mini_stats_fd, "[");
1072 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
1074 n = opt_descr (opt);
1077 fprintf (mini_stats_fd, "\"%s\",", n);
1079 fprintf (mini_stats_fd, "],\n[");
1081 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
1085 g_timer_start (timer);
1086 for (j = 0; j < count; ++j) {
1087 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1088 mono_destroy_compile (cfg);
1090 g_timer_stop (timer);
1091 elapsed = g_timer_elapsed (timer, NULL);
1093 no_opt_time = elapsed;
1094 fprintf (mini_stats_fd, "%f, ", elapsed);
1096 fprintf (mini_stats_fd, "]");
1097 if (no_opt_time > 0.0) {
1098 fprintf (mini_stats_fd, ", \n[");
1099 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++)
1100 fprintf (mini_stats_fd, "%f,", no_opt_time);
1101 fprintf (mini_stats_fd, "]");
1103 fprintf (mini_stats_fd, ");\n");
1105 for (i = 0; i < count; ++i) {
1106 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1107 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
1108 method = mono_marshal_get_native_wrapper (method);
1110 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1111 mono_destroy_compile (cfg);
1115 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1116 mono_destroy_compile (cfg);
1119 mini_cleanup (domain);
1124 mono_jit_init (const char *file)
1126 return mini_init (file);
1130 mono_jit_cleanup (MonoDomain *domain)
1132 mini_cleanup (domain);