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);
66 #ifdef HAVE_ARRAY_ELEM_INIT
67 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
68 #define MSGSTRFIELD1(line) str##line
70 static const struct msgstr_t {
71 #define OPTFLAG(id,shift,name,desc) char MSGSTRFIELD(__LINE__) [sizeof (name) + sizeof (desc)];
72 #include "optflags-def.h"
75 #define OPTFLAG(id,shift,name,desc) name "\0" desc,
76 #include "optflags-def.h"
79 static const gint16 opt_names [] = {
80 #define OPTFLAG(id,shift,name,desc) [(shift)] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
81 #include "optflags-def.h"
85 #define optflag_get_name(id) ((const char*)&opstr + opt_names [(id)])
86 #define optflag_get_desc(id) (optflag_get_name(id) + 1 + strlen (optflag_get_name(id)))
88 #else /* !HAVE_ARRAY_ELEM_INIT */
94 #define OPTFLAG(id,shift,name,desc) {name,desc},
97 #include "optflags-def.h"
100 #define optflag_get_name(id) (opt_names [(id)].name)
101 #define optflag_get_desc(id) (opt_names [(id)].desc)
106 opt_funcs [sizeof (int) * 8] = {
110 #define DEFAULT_OPTIMIZATIONS ( \
111 MONO_OPT_PEEPHOLE | \
114 MONO_OPT_CONSPROP | \
115 MONO_OPT_COPYPROP | \
116 MONO_OPT_TREEPROP | \
122 MONO_OPT_EXCEPTION | \
125 #define EXCLUDED_FROM_ALL (MONO_OPT_SHARED | MONO_OPT_PRECOMP)
128 parse_optimizations (const char* p)
130 /* the default value */
131 guint32 opt = DEFAULT_OPTIMIZATIONS;
136 /* call out to cpu detection code here that sets the defaults ... */
137 opt |= mono_arch_cpu_optimizazions (&exclude);
149 for (i = 0; i < G_N_ELEMENTS (opt_names) && optflag_get_name (i); ++i) {
150 n = optflag_get_name (i);
152 if (strncmp (p, n, len) == 0) {
161 } else if (*p == '=') {
165 while (*p && *p++ != ',');
172 if (i == G_N_ELEMENTS (opt_names) || !optflag_get_name (i)) {
173 if (strncmp (p, "all", 3) == 0) {
177 opt = ~(EXCLUDED_FROM_ALL | exclude);
182 fprintf (stderr, "Invalid optimization name `%s'\n", p);
192 const char desc [18];
193 MonoGraphOptions value;
196 static const GraphName
198 {"cfg", "Control Flow", MONO_GRAPH_CFG},
199 {"dtree", "Dominator Tree", MONO_GRAPH_DTREE},
200 {"code", "CFG showing code", MONO_GRAPH_CFG_CODE},
201 {"ssa", "CFG after SSA", MONO_GRAPH_CFG_SSA},
202 {"optc", "CFG after IR opts", MONO_GRAPH_CFG_OPTCODE}
205 static MonoGraphOptions
206 mono_parse_graph_options (const char* p)
211 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
212 n = graph_names [i].name;
214 if (strncmp (p, n, len) == 0)
215 return graph_names [i].value;
218 fprintf (stderr, "Invalid graph name provided: %s\n", p);
223 mono_parse_default_optimizations (const char* p)
227 opt = parse_optimizations (p);
232 opt_descr (guint32 flags) {
233 GString *str = g_string_new ("");
237 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
238 if (flags & (1 << i)) {
240 g_string_append_c (str, ',');
241 g_string_append (str, optflag_get_name (i));
245 return g_string_free (str, FALSE);
255 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_INTRINS,
256 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
257 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
258 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_CFOLD,
259 MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
260 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,
261 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,
262 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,
263 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,
264 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,
265 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,
266 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,
267 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,
268 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
271 typedef int (*TestMethod) (void);
275 domain_dump_native_code (MonoDomain *domain) {
276 // need to poke into the domain, move to metadata/domain.c
277 // need to empty jit_info_table and code_mp
282 mini_regression (MonoImage *image, int verbose, int *total_run) {
283 guint32 i, opt, opt_flags;
287 int result, expected, failed, cfailed, run, code_size, total;
289 GTimer *timer = g_timer_new ();
292 fprintf (mini_stats_fd, "$stattitle = \'Mono Benchmark Results (various optimizations)\';\n");
294 fprintf (mini_stats_fd, "$graph->set_legend(qw(");
295 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); opt++) {
296 opt_flags = opt_sets [opt];
297 n = opt_descr (opt_flags);
301 fprintf (mini_stats_fd, " ");
302 fprintf (mini_stats_fd, "%s", n);
306 fprintf (mini_stats_fd, "));\n");
308 fprintf (mini_stats_fd, "@data = (\n");
309 fprintf (mini_stats_fd, "[");
312 /* load the metadata */
313 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
314 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
315 mono_class_init (method->klass);
317 if (!strncmp (method->name, "test_", 5) && mini_stats_fd) {
318 fprintf (mini_stats_fd, "\"%s\",", method->name);
322 fprintf (mini_stats_fd, "],\n");
327 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
328 double elapsed, comp_time, start_time;
331 opt_flags = opt_sets [opt];
332 mono_set_defaults (verbose, opt_flags);
333 n = opt_descr (opt_flags);
334 g_print ("Test run: image=%s, opts=%s\n", mono_image_get_filename (image), n);
336 cfailed = failed = run = code_size = 0;
337 comp_time = elapsed = 0.0;
339 /* fixme: ugly hack - delete all previously compiled methods */
340 g_hash_table_destroy (mono_domain_get ()->jit_trampoline_hash);
341 mono_domain_get ()->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
342 g_hash_table_destroy (mono_domain_get ()->jit_code_hash);
343 mono_domain_get ()->jit_code_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
345 g_timer_start (timer);
347 fprintf (mini_stats_fd, "[");
348 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
349 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
350 if (strncmp (method->name, "test_", 5) == 0) {
351 expected = atoi (method->name + 5);
353 start_time = g_timer_elapsed (timer, NULL);
354 comp_time -= start_time;
355 cfg = mini_method_compile (method, opt_flags, mono_get_root_domain (), TRUE, FALSE, 0);
356 comp_time += g_timer_elapsed (timer, NULL);
357 if (cfg->exception_type == MONO_EXCEPTION_NONE) {
359 g_print ("Running '%s' ...\n", method->name);
360 #ifdef MONO_USE_AOT_COMPILER
361 if ((jinfo = mono_aot_get_method (mono_get_root_domain (), method)))
362 func = jinfo->code_start;
365 func = (TestMethod)(gpointer)cfg->native_code;
366 func = (TestMethod)mono_create_ftnptr (mono_get_root_domain (), func);
368 if (result != expected) {
370 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
372 code_size += cfg->code_len;
373 mono_destroy_compile (cfg);
378 g_print ("Test '%s' failed compilation.\n", method->name);
381 fprintf (mini_stats_fd, "%f, ",
382 g_timer_elapsed (timer, NULL) - start_time);
386 fprintf (mini_stats_fd, "],\n");
387 g_timer_stop (timer);
388 elapsed = g_timer_elapsed (timer, NULL);
389 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
390 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
391 g_print ("Elapsed time: %f secs (%f, %f), Code size: %d\n\n", elapsed,
392 elapsed - comp_time, comp_time, code_size);
393 total += failed + cfailed;
398 fprintf (mini_stats_fd, ");\n");
399 fflush (mini_stats_fd);
402 g_timer_destroy (timer);
407 mini_regression_list (int verbose, int count, char *images [])
409 int i, total, total_run, run;
412 total_run = total = 0;
413 for (i = 0; i < count; ++i) {
414 ass = mono_assembly_open (images [i], NULL);
416 g_warning ("failed to load assembly: %s", images [i]);
419 total += mini_regression (mono_assembly_get_image (ass), verbose, &run);
422 g_print ("Overall results: tests: %d, failed: %d, opt combinations: %d (pass: %.2f%%)\n",
423 total_run, total, (int)G_N_ELEMENTS (opt_sets), 100.0*(total_run-total)/total_run);
436 typedef struct CompileAllThreadArgs {
440 } CompileAllThreadArgs;
443 compile_all_methods_thread_main (CompileAllThreadArgs *args)
445 MonoAssembly *ass = args->ass;
446 int verbose = args->verbose;
447 MonoImage *image = mono_assembly_get_image (ass);
452 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
453 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
454 MonoMethodSignature *sig;
456 if (mono_metadata_has_generic_params (image, token))
459 method = mono_get_method (image, token, NULL);
460 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
461 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
462 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
463 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
466 if (method->klass->generic_container)
468 sig = mono_method_signature (method);
469 if (sig->has_type_parameters)
474 char * desc = mono_method_full_name (method, TRUE);
475 g_print ("Compiling %d %s\n", count, desc);
478 cfg = mini_method_compile (method, args->opts, mono_get_root_domain (), FALSE, FALSE, 0);
479 mono_destroy_compile (cfg);
485 compile_all_methods (MonoAssembly *ass, int verbose, guint32 opts)
487 CompileAllThreadArgs args;
490 args.verbose = verbose;
494 * Need to create a mono thread since compilation might trigger
495 * running of managed code.
497 mono_thread_create (mono_domain_get (), compile_all_methods_thread_main, &args);
499 mono_thread_manage ();
504 * @assembly: reference to an assembly
505 * @argc: argument count
506 * @argv: argument vector
508 * Start execution of a program.
511 mono_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
513 MonoImage *image = mono_assembly_get_image (assembly);
515 guint32 entry = mono_image_get_entry_point (image);
518 g_print ("Assembly '%s' doesn't have an entry point.\n", mono_image_get_filename (image));
519 /* FIXME: remove this silly requirement. */
520 mono_environment_exitcode_set (1);
524 method = mono_get_method (image, entry, NULL);
526 g_print ("The entry point method could not be loaded\n");
527 mono_environment_exitcode_set (1);
531 return mono_runtime_run_main (method, argc, argv, NULL);
544 static void main_thread_handler (gpointer user_data)
546 MainThreadArgs *main_args = user_data;
547 MonoAssembly *assembly;
549 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
551 fprintf (stderr, "Can not open image %s\n", main_args->file);
555 if (mono_compile_aot) {
556 int res = mono_compile_assembly (assembly, main_args->opts, main_args->aot_options);
557 printf ("AOT RESULT %d\n", res);
560 * This must be done in a thread managed by mono since it can invoke
563 if (main_args->opts & MONO_OPT_PRECOMP)
564 mono_precompile_assemblies ();
566 mono_jit_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
571 mini_usage_jitdeveloper (void)
576 "Runtime and JIT debugging options:\n"
577 " --breakonex Inserts a breakpoint on exceptions\n"
578 " --break METHOD Inserts a breakpoint at METHOD entry\n"
579 " --compile METHOD Just compile METHOD in assembly\n"
580 " --compile-all Compiles all the methods in the assembly\n"
581 " --ncompile N Number of times to compile METHOD (default: 1)\n"
582 " --print-vtable Print the vtable of all used classes\n"
583 " --regression Runs the regression test contained in the assembly\n"
584 " --statfile FILE Sets the stat file to FILE\n"
585 " --stats Print statistics about the JIT operations\n"
586 " --wapi=hps|semdel IO-layer maintenance\n"
589 " --graph[=TYPE] METHOD Draws a graph of the specified method:\n");
591 for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
592 fprintf (stdout, " %-10s %s\n", graph_names [i].name, graph_names [i].desc);
597 mini_usage_list_opt (void)
601 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
602 fprintf (stdout, " %-10s %s\n", optflag_get_name (i), optflag_get_desc (i));
609 "Usage is: mono [options] program [program-options]\n"
612 " --aot Compiles the assembly to native code\n"
613 " --debug Enable debugging support\n"
614 " --profile[=profiler] Runs in profiling mode with the specified profiler module\n"
615 " --trace[=EXPR] Enable tracing, use --help-trace for details\n"
616 " --help-devel Shows more options available to developers\n"
619 " --config FILE Loads FILE as the Mono config\n"
620 " --verbose, -v Increases the verbosity level\n"
621 " --help, -h Show usage information\n"
622 " --version, -V Show version information\n"
623 " --optimize=OPT Turns on or off a specific optimization\n"
624 " Use --list-opt to get a list of optimizations\n"
625 " --security Turns on the security manager (unsupported, default is off)\n");
629 mini_trace_usage (void)
633 " --trace[=EXPR] Trace every call, optional EXPR controls the scope\n"
635 "EXPR is composed of:\n"
636 " all All assemblies\n"
637 " none No assemblies\n"
638 " program Entry point assembly\n"
639 " assembly Specifies an assembly\n"
640 " M:Type:Method Specifies a method\n"
641 " N:Namespace Specifies a namespace\n"
642 " T:Type Specifies a type\n"
643 " +EXPR Includes expression\n"
644 " -EXPR Excludes expression\n"
645 " disabled Don't print any output until toggled via SIGUSR2\n");
648 static const char info[] =
649 #ifdef HAVE_KW_THREAD
653 #endif /* HAVE_KW_THREAD */
654 "\tGC: " USED_GC_NAME "\n"
655 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
656 "\tSIGSEGV: altstack\n"
658 "\tSIGSEGV: normal\n"
660 "\tDisabled: " DISABLED_FEATURES "\n"
664 mono_main (int argc, char* argv[])
666 MainThreadArgs main_args;
667 MonoAssembly *assembly;
668 MonoMethodDesc *desc;
672 const char* aname, *mname = NULL;
673 char *config_file = NULL;
675 int enable_debugging = FALSE;
676 guint32 opt, action = DO_EXEC;
677 MonoGraphOptions mono_graph_options = 0;
678 int mini_verbose = 0;
679 gboolean enable_profile = FALSE;
680 char *trace_options = NULL;
681 char *profile_options = NULL;
682 char *aot_options = NULL;
684 setlocale (LC_ALL, "");
686 if (mono_running_on_valgrind () && getenv ("MONO_VALGRIND_LEAK_CHECK")) {
687 GMemVTable mem_vtable;
690 * Instruct glib to use the system allocation functions so valgrind
691 * can track the memory allocated by the g_... functions.
693 memset (&mem_vtable, 0, sizeof (mem_vtable));
694 mem_vtable.malloc = malloc;
695 mem_vtable.realloc = realloc;
696 mem_vtable.free = free;
697 mem_vtable.calloc = calloc;
699 g_mem_set_vtable (&mem_vtable);
702 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
703 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
705 opt = parse_optimizations (NULL);
707 for (i = 1; i < argc; ++i) {
708 if (argv [i] [0] != '-')
710 if (strcmp (argv [i], "--regression") == 0) {
711 action = DO_REGRESSION;
712 } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
714 } else if (strcmp (argv [i], "--version") == 0 || strcmp (argv [i], "-V") == 0) {
715 g_print ("Mono JIT compiler version %s, (C) 2002-2006 Novell, Inc and Contributors. www.mono-project.com\n", VERSION);
719 const char *clibpath;
721 cerror = mono_check_corlib_version ();
722 clibpath = mono_defaults.corlib? mono_image_get_filename (mono_defaults.corlib): "unknown";
724 g_print ("The currently installed mscorlib doesn't match this runtime version.\n");
725 g_print ("The error is: %s\n", cerror);
726 g_print ("mscorlib.dll loaded at: %s\n", clibpath);
731 } else if (strcmp (argv [i], "--help") == 0 || strcmp (argv [i], "-h") == 0) {
734 } else if (strcmp (argv [i], "--help-trace") == 0){
737 } else if (strcmp (argv [i], "--help-devel") == 0){
738 mini_usage_jitdeveloper ();
740 } else if (strcmp (argv [i], "--list-opt") == 0){
741 mini_usage_list_opt ();
743 } else if (strncmp (argv [i], "--statfile", 10) == 0) {
745 fprintf (stderr, "error: --statfile requires a filename argument\n");
748 mini_stats_fd = fopen (argv [++i], "w+");
749 } else if (strncmp (argv [i], "--optimize=", 11) == 0) {
750 opt = parse_optimizations (argv [i] + 11);
751 } else if (strncmp (argv [i], "-O=", 3) == 0) {
752 opt = parse_optimizations (argv [i] + 3);
753 } else if (strcmp (argv [i], "--config") == 0) {
755 fprintf (stderr, "error: --config requires a filename argument\n");
758 config_file = argv [++i];
759 } else if (strcmp (argv [i], "--ncompile") == 0) {
761 fprintf (stderr, "error: --ncompile requires an argument\n");
764 count = atoi (argv [++i]);
766 } else if (strcmp (argv [i], "--trace") == 0) {
767 trace_options = (char*)"";
768 } else if (strncmp (argv [i], "--trace=", 8) == 0) {
769 trace_options = &argv [i][8];
770 } else if (strcmp (argv [i], "--breakonex") == 0) {
771 mono_break_on_exc = TRUE;
772 } else if (strcmp (argv [i], "--break") == 0) {
774 fprintf (stderr, "Missing method name in --break command line option\n");
778 if (!mono_debugger_insert_breakpoint (argv [++i], FALSE))
779 fprintf (stderr, "Error: invalid method name '%s'\n", argv [i]);
780 } else if (strcmp (argv [i], "--print-vtable") == 0) {
781 mono_print_vtable = TRUE;
782 } else if (strcmp (argv [i], "--stats") == 0) {
783 mono_counters_enable (-1);
784 mono_stats.enabled = TRUE;
785 mono_jit_stats.enabled = TRUE;
787 } else if (strcmp (argv [i], "--aot") == 0) {
788 mono_compile_aot = TRUE;
789 } else if (strncmp (argv [i], "--aot=", 6) == 0) {
790 mono_compile_aot = TRUE;
791 aot_options = &argv [i][6];
793 } else if (strcmp (argv [i], "--compile-all") == 0) {
795 } else if (strcmp (argv [i], "--profile") == 0) {
796 enable_profile = TRUE;
797 profile_options = NULL;
798 } else if (strncmp (argv [i], "--profile=", 10) == 0) {
799 enable_profile = TRUE;
800 profile_options = argv [i] + 10;
801 } else if (strcmp (argv [i], "--compile") == 0) {
803 fprintf (stderr, "error: --compile option requires a method name argument\n");
809 } else if (strncmp (argv [i], "--graph=", 8) == 0) {
811 fprintf (stderr, "error: --graph option requires a method name argument\n");
815 mono_graph_options = mono_parse_graph_options (argv [i] + 8);
818 } else if (strcmp (argv [i], "--graph") == 0) {
820 fprintf (stderr, "error: --graph option requires a method name argument\n");
825 mono_graph_options = MONO_GRAPH_CFG;
827 } else if (strcmp (argv [i], "--debug") == 0) {
828 enable_debugging = TRUE;
829 } else if (strcmp (argv [i], "--security") == 0) {
830 mono_use_security_manager = TRUE;
831 mono_activate_security_manager ();
832 } else if (strcmp (argv [i], "--desktop") == 0) {
833 #if defined (HAVE_BOEHM_GC)
836 /* Put desktop-specific optimizations here */
837 } else if (strcmp (argv [i], "--server") == 0){
838 /* Put server-specific optimizations here */
839 } else if (strcmp (argv [i], "--inside-mdb") == 0) {
840 action = DO_DEBUGGER;
841 } else if (strncmp (argv [i], "--wapi=", 7) == 0) {
842 if (strcmp (argv [i] + 7, "hps") == 0) {
843 return mini_wapi_hps (argc - i, argv + i);
844 } else if (strcmp (argv [i] + 7, "semdel") == 0) {
845 return mini_wapi_semdel (argc - i, argv + i);
846 } else if (strcmp (argv [i] + 7, "seminfo") == 0) {
847 return mini_wapi_seminfo (argc - i, argv + i);
849 fprintf (stderr, "Invalid --wapi suboption: '%s'\n", argv [i]);
853 fprintf (stderr, "Unknown command line option: '%s'\n", argv [i]);
863 if ((action == DO_EXEC) && g_getenv ("MONO_INSIDE_MDB"))
864 action = DO_DEBUGGER;
866 if (mono_compile_aot || action == DO_EXEC || action == DO_DEBUGGER) {
867 g_set_prgname (argv[i]);
870 if (enable_profile) {
871 /* Needed because of TLS accesses in mono_profiler_load () */
873 mono_profiler_load (profile_options);
876 if (trace_options != NULL){
878 * Need to call this before mini_init () so we can trace methods
879 * compiled there too.
881 mono_jit_trace_calls = mono_trace_parse_options (trace_options);
882 if (mono_jit_trace_calls == NULL)
886 if (action == DO_DEBUGGER) {
887 opt |= MONO_OPT_SHARED;
888 opt &= ~MONO_OPT_INLINE;
889 opt &= ~MONO_OPT_COPYPROP;
890 opt &= ~MONO_OPT_CONSPROP;
891 enable_debugging = TRUE;
893 #ifdef MONO_DEBUGGER_SUPPORTED
894 mono_debug_init (MONO_DEBUG_FORMAT_DEBUGGER);
895 mono_debugger_init ();
897 g_print ("The Mono Debugger is not supported on this platform.\n");
902 mono_set_defaults (mini_verbose, opt);
903 domain = mini_init (argv [i]);
907 if (mini_regression_list (mini_verbose, argc -i, argv + i)) {
908 g_print ("Regression ERRORS!\n");
909 mini_cleanup (domain);
912 mini_cleanup (domain);
915 if (argc - i != 1 || mname == NULL) {
916 g_print ("Usage: mini --ncompile num --compile method assembly\n");
917 mini_cleanup (domain);
925 mini_cleanup (domain);
931 if (argc - i != 1 || mname == NULL) {
933 mini_cleanup (domain);
941 mini_cleanup (domain);
948 if (action == DO_DEBUGGER)
949 mono_debug_init_1 (domain);
950 else if (enable_debugging) {
951 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
952 mono_debug_init_1 (domain);
955 /* Parse gac loading options before loading assemblies. */
956 if (mono_compile_aot || action == DO_EXEC || action == DO_DEBUGGER) {
957 mono_config_parse (config_file);
960 assembly = mono_assembly_open (aname, NULL);
962 fprintf (stderr, "cannot open assembly %s\n", aname);
963 mini_cleanup (domain);
967 if (trace_options != NULL)
968 mono_trace_set_assembly (assembly);
970 if (enable_debugging)
971 mono_debug_init_2 (assembly);
973 if (mono_compile_aot || action == DO_EXEC) {
976 //mono_set_rootdir ();
978 error = mono_check_corlib_version ();
980 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
981 fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
985 #ifdef PLATFORM_WIN32
986 /* Detach console when executing IMAGE_SUBSYSTEM_WINDOWS_GUI on win32 */
987 if (!mono_compile_aot && ((MonoCLIImageInfo*)(mono_assembly_get_image (assembly)->image_info))->cli_header.nt.pe_subsys_required == IMAGE_SUBSYSTEM_WINDOWS_GUI)
991 main_args.domain = domain;
992 main_args.file = aname;
993 main_args.argc = argc - i;
994 main_args.argv = argv + i;
995 main_args.opts = opt;
996 main_args.aot_options = aot_options;
998 mono_runtime_exec_managed_code (domain, main_thread_handler, &main_args);
1000 main_thread_handler (&main_args);
1001 mono_thread_manage ();
1003 mini_cleanup (domain);
1005 /* Look up return value from System.Environment.ExitCode */
1006 i = mono_environment_exitcode_get ();
1008 } else if (action == DO_COMPILE) {
1009 compile_all_methods (assembly, mini_verbose, opt);
1010 mini_cleanup (domain);
1012 } else if (action == DO_DEBUGGER) {
1013 #ifdef MONO_DEBUGGER_SUPPORTED
1016 error = mono_check_corlib_version ();
1018 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
1019 fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
1023 mono_debugger_main (domain, assembly, argc - i, argv + i);
1024 mini_cleanup (domain);
1030 desc = mono_method_desc_new (mname, 0);
1032 g_print ("Invalid method name %s\n", mname);
1033 mini_cleanup (domain);
1036 method = mono_method_desc_search_in_image (desc, mono_assembly_get_image (assembly));
1038 g_print ("Cannot find method %s\n", mname);
1039 mini_cleanup (domain);
1043 if (action == DO_DRAW) {
1046 switch (mono_graph_options) {
1047 case MONO_GRAPH_DTREE:
1049 opt |= MONO_OPT_LOOP;
1051 case MONO_GRAPH_CFG_CODE:
1054 case MONO_GRAPH_CFG_SSA:
1057 case MONO_GRAPH_CFG_OPTCODE:
1064 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1065 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
1067 nm = mono_marshal_get_native_wrapper (method);
1068 cfg = mini_method_compile (nm, opt, mono_get_root_domain (), FALSE, FALSE, part);
1071 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, part);
1072 if ((mono_graph_options & MONO_GRAPH_CFG_SSA) && !(cfg->comp_done & MONO_COMP_SSA)) {
1073 g_warning ("no SSA info available (use -O=deadce)");
1076 mono_draw_graph (cfg, mono_graph_options);
1077 mono_destroy_compile (cfg);
1079 } else if (action == DO_BENCH) {
1080 if (mini_stats_fd) {
1082 double no_opt_time = 0.0;
1083 GTimer *timer = g_timer_new ();
1084 fprintf (mini_stats_fd, "$stattitle = \'Compilations times for %s\';\n",
1085 mono_method_full_name (method, TRUE));
1086 fprintf (mini_stats_fd, "@data = (\n");
1087 fprintf (mini_stats_fd, "[");
1088 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
1090 n = opt_descr (opt);
1093 fprintf (mini_stats_fd, "\"%s\",", n);
1095 fprintf (mini_stats_fd, "],\n[");
1097 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
1101 g_timer_start (timer);
1102 for (j = 0; j < count; ++j) {
1103 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1104 mono_destroy_compile (cfg);
1106 g_timer_stop (timer);
1107 elapsed = g_timer_elapsed (timer, NULL);
1109 no_opt_time = elapsed;
1110 fprintf (mini_stats_fd, "%f, ", elapsed);
1112 fprintf (mini_stats_fd, "]");
1113 if (no_opt_time > 0.0) {
1114 fprintf (mini_stats_fd, ", \n[");
1115 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++)
1116 fprintf (mini_stats_fd, "%f,", no_opt_time);
1117 fprintf (mini_stats_fd, "]");
1119 fprintf (mini_stats_fd, ");\n");
1121 for (i = 0; i < count; ++i) {
1122 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1123 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
1124 method = mono_marshal_get_native_wrapper (method);
1126 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1127 mono_destroy_compile (cfg);
1131 cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
1132 mono_destroy_compile (cfg);
1135 mini_cleanup (domain);
1140 mono_jit_init (const char *file)
1142 return mini_init (file);
1146 mono_jit_cleanup (MonoDomain *domain)
1148 mini_cleanup (domain);