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