ae93b5538844063af91e84ccee36355f2b5dc306
[mono.git] / mono / mini / driver.c
1 /*
2  * driver.c: The new mono JIT compiler.
3  *
4  * Author:
5  *   Paolo Molaro (lupus@ximian.com)
6  *   Dietmar Maurer (dietmar@ximian.com)
7  *
8  * (C) 2002 Ximian, Inc.
9  */
10
11 #include <config.h>
12 #include <signal.h>
13 #include <unistd.h>
14
15 #include <mono/metadata/assembly.h>
16 #include <mono/metadata/loader.h>
17 #include <mono/metadata/cil-coff.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/class.h>
20 #include <mono/metadata/object.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/metadata/opcodes.h>
23 #include <mono/metadata/mono-endian.h>
24 #include <mono/metadata/tokentype.h>
25 #include <mono/metadata/tabledefs.h>
26 #include <mono/metadata/threads.h>
27 #include <mono/metadata/marshal.h>
28 #include <mono/metadata/socket-io.h>
29 #include <mono/metadata/appdomain.h>
30 #include <mono/metadata/debug-helpers.h>
31 #include <mono/io-layer/io-layer.h>
32 #include "mono/metadata/profiler.h"
33 #include <mono/metadata/profiler-private.h>
34 #include <mono/metadata/mono-config.h>
35 #include <mono/metadata/environment.h>
36 #include <mono/metadata/mono-debug.h>
37 #include <mono/metadata/mono-debug-debugger.h>
38
39 #include "mini.h"
40 #include <string.h>
41 #include <ctype.h>
42 #include "inssel.h"
43
44 static FILE *mini_stats_fd = NULL;
45
46 static void mini_usage (void);
47
48 typedef void (*OptFunc) (const char *p);
49
50 /* keep in sync with enum in mini.h */
51 typedef struct {
52         const char* name;
53         const char* desc;
54         const OptFunc func;
55 } OptName;
56
57 static const OptName 
58 opt_names [] = {
59         {"peephole", "Peephole postpass"},
60         {"branch",   "Branch optimizations"},
61         {"inline",   "Inline method calls"},
62         {"cfold",    "Constant folding"},
63         {"consprop", "Constant propagation"},
64         {"copyprop", "Copy propagation"},
65         {"deadce",   "Dead code elimination"},
66         {"linears",  "Linear scan global reg allocation"},
67         {"cmov",     "Conditional moves"},
68         {"shared",   "Emit per-domain code"},
69         {"sched",    "Instruction scheduling"},
70         {"instrins", "Intrinsic method implementations"},
71         {"tailc",    "Tail recursion and tail calls"},
72         {"loop",     "Loop related optimizations"},
73         {"fcmov",    "Fast x86 FP compares"}
74 };
75
76 static guint32
77 parse_optimizations (const char* p)
78 {
79         /* the default value */
80         guint32 opt = MONO_OPT_PEEPHOLE | MONO_OPT_CFOLD /* | MONO_OPT_CONSPROP | MONO_OPT_INLINE*/ | MONO_OPT_BRANCH | /* | MONO_OPT_SAHRED |*/ MONO_OPT_LINEARS;
81         guint32 exclude = 0;
82         const char *n;
83         int i, invert, len;
84
85         /* call out to cpu detection code here that sets the defaults ... */
86         opt |= mono_arch_cpu_optimizazions (&exclude);
87         opt &= ~exclude;
88         if (!p)
89                 return opt;
90
91         while (*p) {
92                 if (*p == '-') {
93                         p++;
94                         invert = TRUE;
95                 } else {
96                         invert = FALSE;
97                 }
98                 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
99                         n = opt_names [i].name;
100                         len = strlen (n);
101                         if (strncmp (p, n, len) == 0) {
102                                 if (invert)
103                                         opt &= ~ (1 << i);
104                                 else
105                                         opt |= 1 << i;
106                                 p += len;
107                                 if (*p == ',') {
108                                         p++;
109                                         break;
110                                 } else if (*p == '=') {
111                                         p++;
112                                         if (opt_names [i].func)
113                                                 opt_names [i].func (p);
114                                         while (*p && *p++ != ',');
115                                         break;
116                                 }
117                                 /* error out */
118                                 break;
119                         }
120                 }
121                 if (i == G_N_ELEMENTS (opt_names)) {
122                         if (strncmp (p, "all", 3) == 0) {
123                                 if (invert)
124                                         opt = 0;
125                                 else
126                                         opt = ~(MONO_OPT_SAHRED | exclude);
127                                 p += 3;
128                                 if (*p == ',')
129                                         p++;
130                         } else {
131                                 fprintf (stderr, "Invalid optimization name `%s'\n", p);
132                                 exit (1);
133                         }
134                 }
135         }
136         return opt;
137 }
138
139 typedef struct {
140         const char* name;
141         const char* desc;
142         MonoGraphOptions value;
143 } GraphName;
144
145 static const GraphName 
146 graph_names [] = {
147         {"cfg",      "Control Flow Graph (CFG)" ,               MONO_GRAPH_CFG},
148         {"dtree",    "Dominator Tree",                          MONO_GRAPH_DTREE},
149         {"code",     "CFG showing code",                        MONO_GRAPH_CFG_CODE},
150         {"ssa",      "CFG showing code after SSA translation",  MONO_GRAPH_CFG_SSA},
151         {"optcode",  "CFG showing code after IR optimizations", MONO_GRAPH_CFG_OPTCODE}
152 };
153
154 static MonoGraphOptions
155 mono_parse_graph_options (const char* p)
156 {
157         const char *n;
158         int i, len;
159
160         for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
161                 n = graph_names [i].name;
162                 len = strlen (n);
163                 if (strncmp (p, n, len) == 0)
164                         return graph_names [i].value;
165         }
166
167         fprintf (stderr, "Invalid graph name provided: %s\n", p);
168         exit (1);
169 }
170
171 int
172 mini_parse_default_optimizations (const char* p)
173 {
174         guint32 opt;
175
176         opt = parse_optimizations (p);
177         return opt;
178 }
179
180 static char*
181 opt_descr (guint32 flags) {
182         GString *str = g_string_new ("");
183         int i, need_comma;
184
185         need_comma = 0;
186         for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
187                 if (flags & (1 << i)) {
188                         if (need_comma)
189                                 g_string_append_c (str, ',');
190                         g_string_append (str, opt_names [i].name);
191                         need_comma = 1;
192                 }
193         }
194         return g_string_free (str, FALSE);
195 }
196
197 static const guint32
198 opt_sets [] = {
199        0,
200        MONO_OPT_PEEPHOLE,
201        MONO_OPT_BRANCH,
202        MONO_OPT_CFOLD,
203        MONO_OPT_FCMOV,
204        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE,
205        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
206        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
207        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_CFOLD,
208        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
209        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE
210 };
211
212 typedef int (*TestMethod) (void);
213
214 #if 0
215 static void
216 domain_dump_native_code (MonoDomain *domain) {
217         // need to poke into the domain, move to metadata/domain.c
218         // need to empty jit_info_table and code_mp
219 }
220 #endif
221
222 static int
223 mini_regression (MonoImage *image, int verbose, int *total_run) {
224         guint32 i, opt, opt_flags;
225         MonoMethod *method;
226         MonoCompile *cfg;
227         char *n;
228         int result, expected, failed, cfailed, run, code_size, total;
229         TestMethod func;
230         GTimer *timer = g_timer_new ();
231
232         if (mini_stats_fd) {
233                 fprintf (mini_stats_fd, "$stattitle = \'Mono Benchmark Results (various optimizations)\';\n");
234
235                 fprintf (mini_stats_fd, "$graph->set_legend(qw(");
236                 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); opt++) {
237                         opt_flags = opt_sets [opt];
238                         n = opt_descr (opt_flags);
239                         if (!n [0])
240                                 n = (char *)"none";
241                         if (opt)
242                                 fprintf (mini_stats_fd, " ");
243                         fprintf (mini_stats_fd, "%s", n);
244                 
245
246                 }
247                 fprintf (mini_stats_fd, "));\n");
248
249                 fprintf (mini_stats_fd, "@data = (\n");
250                 fprintf (mini_stats_fd, "[");
251         }
252
253         /* load the metadata */
254         for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
255                 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
256                 mono_class_init (method->klass);
257
258                 if (!strncmp (method->name, "test_", 5) && mini_stats_fd) {
259                         fprintf (mini_stats_fd, "\"%s\",", method->name);
260                 }
261         }
262         if (mini_stats_fd)
263                 fprintf (mini_stats_fd, "],\n");
264
265
266         total = 0;
267         *total_run = 0;
268         for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
269                 double elapsed, comp_time, start_time;
270                 opt_flags = opt_sets [opt];
271                 mini_set_defaults (verbose, opt_flags);
272                 n = opt_descr (opt_flags);
273                 g_print ("Test run: image=%s, opts=%s\n", image->name, n);
274                 g_free (n);
275                 cfailed = failed = run = code_size = 0;
276                 comp_time = elapsed = 0.0;
277
278                 /* fixme: ugly hack - delete all previously compiled methods */
279                 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
280                         method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
281                         method->info = NULL;
282                 }
283
284                 g_timer_start (timer);
285                 if (mini_stats_fd)
286                         fprintf (mini_stats_fd, "[");
287                 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
288                         method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
289                         if (strncmp (method->name, "test_", 5) == 0) {
290                                 expected = atoi (method->name + 5);
291                                 run++;
292                                 start_time = g_timer_elapsed (timer, NULL);
293                                 comp_time -= start_time; 
294                                 cfg = mini_method_compile (method, opt_flags, mono_root_domain, 0);
295                                 comp_time += g_timer_elapsed (timer, NULL);
296                                 if (cfg) {
297                                         if (verbose >= 2)
298                                                 g_print ("Running '%s' ...\n", method->name);
299 #ifdef MONO_USE_AOT_COMPILER
300                                         if (!(func = mono_aot_get_method (method)))
301 #endif
302                                                 func = (TestMethod)cfg->native_code;
303                                         result = func ();
304                                         if (result != expected) {
305                                                 failed++;
306                                                 if (verbose)
307                                                         g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
308                                         }
309                                         code_size += cfg->code_len;
310                                         mono_destroy_compile (cfg);
311
312                                         if (mono_trace_coverage) {
313                                                 MonoCoverageInfo *cov = mono_get_coverage_info (method);
314
315                                                 if (cov) {
316                                                         int k;
317                                                         printf ("COVERAGE INFO %s\n", mono_method_full_name (method, TRUE));
318                                                 
319                                                         for (k = 0; k < cov->entries; k++) {
320                                                                 printf ("  BBLOCK %3d %d\n", cov->data [k].iloffset, cov->data [k].count);
321                                                         }
322                                                 }
323                                         }
324
325                                 } else {
326                                         cfailed++;
327                                         if (verbose)
328                                                 g_print ("Test '%s' failed compilation.\n", method->name);
329                                 }
330                                 if (mini_stats_fd)
331                                         fprintf (mini_stats_fd, "%f, ", 
332                                                  g_timer_elapsed (timer, NULL) - start_time);
333                         }
334                 }
335                 if (mini_stats_fd)
336                         fprintf (mini_stats_fd, "],\n");
337                 g_timer_stop (timer);
338                 elapsed = g_timer_elapsed (timer, NULL);
339                 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n", 
340                         run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
341                 g_print ("Elapsed time: %f secs (%f, %f), Code size: %d\n\n", elapsed, 
342                          elapsed - comp_time, comp_time, code_size);
343                 total += failed + cfailed;
344                 *total_run += run;
345         }
346
347         if (mini_stats_fd) {
348                 fprintf (mini_stats_fd, ");\n");
349                 fflush (mini_stats_fd);
350         }
351
352         g_timer_destroy (timer);
353         return total;
354 }
355
356 static int
357 mini_regression_list (int verbose, int count, char *images [])
358 {
359         int i, total, total_run, run;
360         MonoAssembly *ass;
361         
362         total_run =  total = 0;
363         for (i = 0; i < count; ++i) {
364                 ass = mono_assembly_open (images [i], NULL);
365                 if (!ass) {
366                         g_warning ("failed to load assembly: %s", images [i]);
367                         continue;
368                 }
369                 total += mini_regression (ass->image, verbose, &run);
370                 total_run += run;
371                 mono_assembly_close (ass);
372         }
373         g_print ("Overall results: tests: %d, failed: %d, opt combinations: %d (pass: %.2f%%)\n", 
374                 total_run, total, G_N_ELEMENTS (opt_sets), 100.0*(total_run-total)/total_run);
375         return total;
376 }
377
378 enum {
379         DO_BENCH,
380         DO_REGRESSION,
381         DO_COMPILE,
382         DO_EXEC,
383         DO_DRAW,
384 };
385
386 static void
387 compile_all_methods (MonoAssembly *ass, int verbose)
388 {
389         MonoImage *image = ass->image;
390         MonoMethod *method;
391         int i, count = 0;
392
393         for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
394                 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
395                 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
396                         continue;
397                 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
398                         continue;
399
400                 count++;
401                 if (verbose) {
402                         char * desc = mono_method_full_name (method, TRUE);
403                         g_print ("Compiling %d %s\n", count, desc + 3);
404                         g_free (desc);
405                 }
406                 mono_compile_method (method);
407         }
408
409 }
410
411 /**
412  * mono_jit_exec:
413  * @assembly: reference to an assembly
414  * @argc: argument count
415  * @argv: argument vector
416  *
417  * Start execution of a program.
418  */
419 int 
420 mono_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
421 {
422         MonoImage *image = assembly->image;
423         MonoMethod *method;
424         guint32 entry = mono_image_get_entry_point (image);
425
426         if (!entry) {
427                 g_print ("Assembly '%s' doesn't have an entry point.\n", image->name);
428                 /* FIXME: remove this silly requirement. */
429                 mono_environment_exitcode_set (1);
430                 return 1;
431         }
432
433         method = mono_get_method (image, entry, NULL);
434
435         return mono_runtime_run_main (method, argc, argv, NULL);
436 }
437
438 typedef struct 
439 {
440         MonoDomain *domain;
441         const char *file;
442         int argc;
443         char **argv;
444         guint32 opts;
445 } MainThreadArgs;
446
447 static void main_thread_handler (gpointer user_data)
448 {
449         MainThreadArgs *main_args = user_data;
450         MonoAssembly *assembly;
451         
452         assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
453         if (!assembly){
454                 fprintf (stderr, "Can not open image %s\n", main_args->file);
455                 exit (1);
456         }
457
458         if (mono_compile_aot) {
459                 int res = mono_compile_assembly (assembly, main_args->opts);
460                 printf ("AOT RESULT %d\n", res);
461         } else {
462                 mono_jit_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
463         }
464 }
465
466 static void
467 mini_usage (void)
468 {
469         int i;
470
471         fprintf (stderr,
472                 "Usage is: mono [options] assembly\n\n"
473                 "Runtime and JIT debugging:\n"
474                 "    --compile METHOD       Just compile METHOD in assembly\n"
475                 "    --ncompile N           Number of times to compile METHOD (default: 1)\n"
476                 "    --regression           Runs the regression test contained in the assembly\n"
477                 "    --print-vtable         Print the vtable of all used classes\n"
478                 "    --trace                Enable tracing\n"
479                 "    --compile-all          Compiles all the methods in the assembly\n"
480                 "    --breakonex            Inserts a breakpoint on exceptions\n"
481                 "    --break METHOD         Inserts a breakpoint at METHOD entry\n"
482                 "    --debug                Enable debugging support\n"
483                 "\n"
484                 "Development:\n"
485                 "    --statfile FILE        Sets the stat file to FILE\n"
486                 "    --aot                  Compiles the assembly to native code\n"
487                 "    --coverage             Performs coverage analysis\n"
488                 "    --profile              Runs in profiling mode\n"
489                 "    --graph[=TYPE] METHOD  Draws a graph of the specified method:\n");
490         
491         for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
492                 fprintf (stderr, "                           %-10s %s\n", graph_names [i].name, graph_names [i].desc);
493         }
494
495         fprintf (stderr,
496                 "\n"
497                 "Runtime:\n"
498                 "    --config FILE          Loads FILE as the Mono config\n"
499                 "    --verbose, -v          Increases the verbosity level\n"
500                 "    --help, -h             Show usage information\n"
501                 "    --version, -V          Show version information\n"
502                 "    --optimize=OPT         Turns on a specific optimization:\n");
503
504         for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
505                 fprintf (stderr, "                           %-10s %s\n", opt_names [i].name, opt_names [i].desc);
506 }
507
508 int
509 mono_main (int argc, char* argv[]) {
510         MainThreadArgs main_args;
511         MonoAssembly *assembly;
512         MonoMethodDesc *desc;
513         MonoMethod *method;
514         MonoCompile *cfg;
515         MonoDomain *domain;
516         const char* aname, *mname = NULL;
517         char *config_file = NULL;
518         int i, count = 1;
519         int enable_debugging = FALSE;
520         guint32 opt, action = DO_EXEC;
521         MonoGraphOptions mono_graph_options = 0;
522         int mini_verbose = 0;
523
524         opt = parse_optimizations (NULL);
525
526         for (i = 1; i < argc; ++i) {
527                 if (argv [i] [0] != '-')
528                         break;
529                 if (strcmp (argv [i], "--regression") == 0) {
530                         action = DO_REGRESSION;
531                 } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
532                         mini_verbose++;
533                 } else if (strcmp (argv [i], "--version") == 0 || strcmp (argv [i], "-V") == 0) {
534                         g_print ("Mono JIT compiler version %s, (C) 2002, 2003 Ximian, Inc.\n", VERSION);
535                         return 0;
536                 } else if (strcmp (argv [i], "--help") == 0 || strcmp (argv [i], "-h") == 0) {
537                         mini_usage ();
538                         return 0;
539                 } else if (strncmp (argv [i], "--statfile", 10) == 0) {
540                         mini_stats_fd = fopen (argv [++i], "w+");
541                 } else if (strncmp (argv [i], "--optimize=", 11) == 0) {
542                         opt = parse_optimizations (argv [i] + 11);
543                 } else if (strncmp (argv [i], "-O=", 3) == 0) {
544                         opt = parse_optimizations (argv [i] + 3);
545                 } else if (strcmp (argv [i], "--config") == 0) {
546                         config_file = argv [++i];
547                 } else if (strcmp (argv [i], "--ncompile") == 0) {
548                         count = atoi (argv [++i]);
549                         action = DO_BENCH;
550                 } else if (strcmp (argv [i], "--trace") == 0) {
551                         mono_jit_trace_calls = TRUE;
552                 } else if (strcmp (argv [i], "--breakonex") == 0) {
553                         mono_break_on_exc = TRUE;
554                 } else if (strcmp (argv [i], "--break") == 0) {
555                         if (!mono_debugger_insert_breakpoint (argv [++i], FALSE))
556                                 g_error ("Invalid method name '%s'", argv [i]);
557                 } else if (strcmp (argv [i], "--print-vtable") == 0) {
558                         mono_print_vtable = TRUE;
559                 } else if (strcmp (argv [i], "--stats") == 0) {
560                         mono_jit_stats.enabled = TRUE;
561                 } else if (strcmp (argv [i], "--aot") == 0) {
562                         mono_compile_aot = TRUE;
563                 } else if (strcmp (argv [i], "--coverage") == 0) {
564                         mono_trace_coverage = TRUE;
565                 } else if (strcmp (argv [i], "--compile-all") == 0) {
566                         action = DO_COMPILE;
567                 } else if (strcmp (argv [i], "--profile") == 0) {
568                         mono_jit_profile = TRUE;
569                         mono_profiler_install_simple ();
570                 } else if (strcmp (argv [i], "--compile") == 0) {
571                         mname = argv [++i];
572                         action = DO_BENCH;
573                 } else if (strncmp (argv [i], "--graph=", 8) == 0) {
574                         mono_graph_options = mono_parse_graph_options (argv [i] + 8);
575                         mname = argv [++i];
576                         action = DO_DRAW;
577                 } else if (strcmp (argv [i], "--graph") == 0) {
578                         mname = argv [++i];
579                         mono_graph_options = MONO_GRAPH_CFG;
580                         action = DO_DRAW;
581                 } else if (strcmp (argv [i], "--debug") == 0) {
582                         enable_debugging = TRUE;
583                 } else {
584                         fprintf (stderr, "Unknown command line option: %s\n", argv [i]);
585                         return 1;
586                 }
587         }
588
589         if (!argv [i]) {
590                 mini_usage ();
591                 return 1;
592         }
593
594         mini_set_defaults (mini_verbose, opt);
595         domain = mini_init (argv [i]);
596
597         switch (action) {
598         case DO_REGRESSION:
599                 if (mini_regression_list (mini_verbose, argc -i, argv + i)) {
600                         g_print ("Regression ERRORS!\n");
601                         mini_cleanup (domain);
602                         return 1;
603                 }
604                 mini_cleanup (domain);
605                 return 0;
606         case DO_BENCH:
607                 if (argc - i != 1 || mname == NULL) {
608                         g_print ("Usage: mini --ncompile num --compile method assembly\n");
609                         mini_cleanup (domain);
610                         return 1;
611                 }
612                 aname = argv [i];
613                 break;
614         case DO_COMPILE:
615                 if (argc - i != 1) {
616                         mini_usage ();
617                         mini_cleanup (domain);
618                         return 1;
619                 }
620                 aname = argv [i];
621                 break;
622         case DO_DRAW:
623                 if (argc - i != 1 || mname == NULL) {
624                         mini_usage ();
625                         mini_cleanup (domain);
626                         return 1;
627                 }
628                 aname = argv [i];
629                 break;
630         default:
631                 if (argc - i < 1) {
632                         mini_usage ();
633                         mini_cleanup (domain);
634                         return 1;
635                 }
636                 aname = argv [i];
637                 break;
638         }
639
640         if (enable_debugging)
641                 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
642
643         assembly = mono_assembly_open (aname, NULL);
644         if (!assembly) {
645                 fprintf (stderr, "cannot open assembly %s\n", aname);
646                 mini_cleanup (domain);
647                 return 2;
648         }
649
650         if (enable_debugging)
651                 mono_debug_init_2 (assembly);
652
653         if (mono_compile_aot || action == DO_EXEC) {
654                 g_set_prgname (aname);
655                 mono_config_parse (config_file);
656                 //mono_set_rootdir ();
657
658                 main_args.domain = domain;
659                 main_args.file = aname;         
660                 main_args.argc = argc - i;
661                 main_args.argv = argv + i;
662                 main_args.opts = opt;
663              
664                 mono_runtime_exec_managed_code (domain, main_thread_handler, &main_args);
665                 mini_cleanup (domain);
666                 /* Look up return value from System.Environment.ExitCode */
667                 i = mono_environment_exitcode_get ();
668                 return i;
669         } else if (action == DO_COMPILE) {
670                 compile_all_methods (assembly, mini_verbose);
671                 mini_cleanup (domain);
672                 return 0;
673         }
674         desc = mono_method_desc_new (mname, 0);
675         if (!desc) {
676                 g_print ("Invalid method name %s\n", mname);
677                 mini_cleanup (domain);
678                 return 3;
679         }
680         method = mono_method_desc_search_in_image (desc, assembly->image);
681         if (!method) {
682                 g_print ("Cannot find method %s\n", mname);
683                 mini_cleanup (domain);
684                 return 3;
685         }
686
687         if (action == DO_DRAW) {
688                 int part = 0;
689
690                 switch (mono_graph_options) {
691                 case MONO_GRAPH_DTREE:
692                         part = 1;
693                         opt |= MONO_OPT_LOOP;
694                         break;
695                 case MONO_GRAPH_CFG_CODE:
696                         part = 1;
697                         break;
698                 case MONO_GRAPH_CFG_SSA:
699                         part = 2;
700                         break;
701                 case MONO_GRAPH_CFG_OPTCODE:
702                         part = 3;
703                         break;
704                 default:
705                         break;
706                 }
707
708                 cfg = mini_method_compile (method, opt, mono_root_domain, part);
709                 if ((mono_graph_options & MONO_GRAPH_CFG_SSA) && !(cfg->comp_done & MONO_COMP_SSA)) {
710                         g_warning ("no SSA info available (use -O=deadce)");
711                         return 1;
712                 }
713                 mono_draw_graph (cfg, mono_graph_options);
714                 mono_destroy_compile (cfg);
715
716         } else if (action == DO_BENCH) {
717                 if (mini_stats_fd) {
718                         const char *n;
719                         double no_opt_time = 0.0;
720                         GTimer *timer = g_timer_new ();
721                         fprintf (mini_stats_fd, "$stattitle = \'Compilations times for %s\';\n", 
722                                  mono_method_full_name (method, TRUE));
723                         fprintf (mini_stats_fd, "@data = (\n");
724                         fprintf (mini_stats_fd, "[");
725                         for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
726                                 opt = opt_sets [i];
727                                 n = opt_descr (opt);
728                                 if (!n [0])
729                                         n = "none";
730                                 fprintf (mini_stats_fd, "\"%s\",", n);
731                         }
732                         fprintf (mini_stats_fd, "],\n[");
733
734                         for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
735                                 int j;
736                                 double elapsed;
737                                 opt = opt_sets [i];
738                                 g_timer_start (timer);
739                                 for (j = 0; j < count; ++j) {
740                                         cfg = mini_method_compile (method, opt, mono_root_domain, 0);
741                                         mono_destroy_compile (cfg);
742                                 }
743                                 g_timer_stop (timer);
744                                 elapsed = g_timer_elapsed (timer, NULL);
745                                 if (!opt)
746                                         no_opt_time = elapsed;
747                                 fprintf (mini_stats_fd, "%f, ", elapsed);
748                         }
749                         fprintf (mini_stats_fd, "]");
750                         if (no_opt_time > 0.0) {
751                                 fprintf (mini_stats_fd, ", \n[");
752                                 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) 
753                                         fprintf (mini_stats_fd, "%f,", no_opt_time);
754                                 fprintf (mini_stats_fd, "]");
755                         }
756                         fprintf (mini_stats_fd, ");\n");
757                 } else {
758                         for (i = 0; i < count; ++i) {
759                                 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
760                                 mono_destroy_compile (cfg);
761                         }
762                 }
763         } else {
764                 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
765                 mono_destroy_compile (cfg);
766         }
767
768         mini_cleanup (domain);
769         return 0;
770 }
771
772 MonoDomain * 
773 mono_jit_init (const char *file)
774 {
775         return mini_init (file);
776 }
777
778 void        
779 mono_jit_cleanup (MonoDomain *domain)
780 {
781         mini_cleanup (domain);
782 }
783
784