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