2003-09-04 Sebastien Pouliot <spouliot@videotron.ca>
[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 "jit.h"
41 #include <string.h>
42 #include <ctype.h>
43 #include "inssel.h"
44 #include <locale.h>
45
46 static FILE *mini_stats_fd = NULL;
47
48 static void mini_usage (void);
49
50 typedef void (*OptFunc) (const char *p);
51
52 /* keep in sync with enum in mini.h */
53 typedef struct {
54         const char* name;
55         const char* desc;
56         const OptFunc func;
57 } OptName;
58
59 static const OptName 
60 opt_names [] = {
61         {"peephole", "Peephole postpass"},
62         {"branch",   "Branch optimizations"},
63         {"inline",   "Inline method calls"},
64         {"cfold",    "Constant folding"},
65         {"consprop", "Constant propagation"},
66         {"copyprop", "Copy propagation"},
67         {"deadce",   "Dead code elimination"},
68         {"linears",  "Linear scan global reg allocation"},
69         {"cmov",     "Conditional moves"},
70         {"shared",   "Emit per-domain code"},
71         {"sched",    "Instruction scheduling"},
72         {"intrins",  "Intrinsic method implementations"},
73         {"tailc",    "Tail recursion and tail calls"},
74         {"loop",     "Loop related optimizations"},
75         {"fcmov",    "Fast x86 FP compares"},
76         {"leaf",     "Leaf procedures optimizations"}
77 };
78
79 #define DEFAULT_OPTIMIZATIONS ( \
80         MONO_OPT_PEEPHOLE |     \
81         MONO_OPT_CFOLD |        \
82         MONO_OPT_BRANCH |       \
83         MONO_OPT_LINEARS |      \
84         MONO_OPT_INTRINS)
85
86 static guint32
87 parse_optimizations (const char* p)
88 {
89         /* the default value */
90         guint32 opt = DEFAULT_OPTIMIZATIONS;
91         guint32 exclude = 0;
92         const char *n;
93         int i, invert, len;
94
95         /* call out to cpu detection code here that sets the defaults ... */
96         opt |= mono_arch_cpu_optimizazions (&exclude);
97         opt &= ~exclude;
98         if (!p)
99                 return opt;
100
101         while (*p) {
102                 if (*p == '-') {
103                         p++;
104                         invert = TRUE;
105                 } else {
106                         invert = FALSE;
107                 }
108                 for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
109                         n = opt_names [i].name;
110                         len = strlen (n);
111                         if (strncmp (p, n, len) == 0) {
112                                 if (invert)
113                                         opt &= ~ (1 << i);
114                                 else
115                                         opt |= 1 << i;
116                                 p += len;
117                                 if (*p == ',') {
118                                         p++;
119                                         break;
120                                 } else if (*p == '=') {
121                                         p++;
122                                         if (opt_names [i].func)
123                                                 opt_names [i].func (p);
124                                         while (*p && *p++ != ',');
125                                         break;
126                                 }
127                                 /* error out */
128                                 break;
129                         }
130                 }
131                 if (i == G_N_ELEMENTS (opt_names)) {
132                         if (strncmp (p, "all", 3) == 0) {
133                                 if (invert)
134                                         opt = 0;
135                                 else
136                                         opt = ~(MONO_OPT_SHARED | exclude);
137                                 p += 3;
138                                 if (*p == ',')
139                                         p++;
140                         } else {
141                                 fprintf (stderr, "Invalid optimization name `%s'\n", p);
142                                 exit (1);
143                         }
144                 }
145         }
146         return opt;
147 }
148
149 typedef struct {
150         const char* name;
151         const char* desc;
152         MonoGraphOptions value;
153 } GraphName;
154
155 static const GraphName 
156 graph_names [] = {
157         {"cfg",      "Control Flow Graph (CFG)" ,               MONO_GRAPH_CFG},
158         {"dtree",    "Dominator Tree",                          MONO_GRAPH_DTREE},
159         {"code",     "CFG showing code",                        MONO_GRAPH_CFG_CODE},
160         {"ssa",      "CFG showing code after SSA translation",  MONO_GRAPH_CFG_SSA},
161         {"optcode",  "CFG showing code after IR optimizations", MONO_GRAPH_CFG_OPTCODE}
162 };
163
164 static MonoGraphOptions
165 mono_parse_graph_options (const char* p)
166 {
167         const char *n;
168         int i, len;
169
170         for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
171                 n = graph_names [i].name;
172                 len = strlen (n);
173                 if (strncmp (p, n, len) == 0)
174                         return graph_names [i].value;
175         }
176
177         fprintf (stderr, "Invalid graph name provided: %s\n", p);
178         exit (1);
179 }
180
181 int
182 mono_parse_default_optimizations (const char* p)
183 {
184         guint32 opt;
185
186         opt = parse_optimizations (p);
187         return opt;
188 }
189
190 static char*
191 opt_descr (guint32 flags) {
192         GString *str = g_string_new ("");
193         int i, need_comma;
194
195         need_comma = 0;
196         for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
197                 if (flags & (1 << i)) {
198                         if (need_comma)
199                                 g_string_append_c (str, ',');
200                         g_string_append (str, opt_names [i].name);
201                         need_comma = 1;
202                 }
203         }
204         return g_string_free (str, FALSE);
205 }
206
207 static const guint32
208 opt_sets [] = {
209        0,
210        MONO_OPT_PEEPHOLE,
211        MONO_OPT_BRANCH,
212        MONO_OPT_CFOLD,
213        MONO_OPT_FCMOV,
214        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_INTRINS,
215        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
216        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
217        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_CFOLD,
218        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
219        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS
220 };
221
222 typedef int (*TestMethod) (void);
223
224 #if 0
225 static void
226 domain_dump_native_code (MonoDomain *domain) {
227         // need to poke into the domain, move to metadata/domain.c
228         // need to empty jit_info_table and code_mp
229 }
230 #endif
231
232 static int
233 mini_regression (MonoImage *image, int verbose, int *total_run) {
234         guint32 i, opt, opt_flags;
235         MonoMethod *method;
236         MonoCompile *cfg;
237         char *n;
238         int result, expected, failed, cfailed, run, code_size, total;
239         TestMethod func;
240         GTimer *timer = g_timer_new ();
241
242         if (mini_stats_fd) {
243                 fprintf (mini_stats_fd, "$stattitle = \'Mono Benchmark Results (various optimizations)\';\n");
244
245                 fprintf (mini_stats_fd, "$graph->set_legend(qw(");
246                 for (opt = 0; opt < G_N_ELEMENTS (opt_sets); opt++) {
247                         opt_flags = opt_sets [opt];
248                         n = opt_descr (opt_flags);
249                         if (!n [0])
250                                 n = (char *)"none";
251                         if (opt)
252                                 fprintf (mini_stats_fd, " ");
253                         fprintf (mini_stats_fd, "%s", n);
254                 
255
256                 }
257                 fprintf (mini_stats_fd, "));\n");
258
259                 fprintf (mini_stats_fd, "@data = (\n");
260                 fprintf (mini_stats_fd, "[");
261         }
262
263         /* load the metadata */
264         for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
265                 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
266                 mono_class_init (method->klass);
267
268                 if (!strncmp (method->name, "test_", 5) && mini_stats_fd) {
269                         fprintf (mini_stats_fd, "\"%s\",", method->name);
270                 }
271         }
272         if (mini_stats_fd)
273                 fprintf (mini_stats_fd, "],\n");
274
275
276         total = 0;
277         *total_run = 0;
278         for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
279                 double elapsed, comp_time, start_time;
280                 opt_flags = opt_sets [opt];
281                 mono_set_defaults (verbose, opt_flags);
282                 n = opt_descr (opt_flags);
283                 g_print ("Test run: image=%s, opts=%s\n", image->name, n);
284                 g_free (n);
285                 cfailed = failed = run = code_size = 0;
286                 comp_time = elapsed = 0.0;
287
288                 /* fixme: ugly hack - delete all previously compiled methods */
289                 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
290                         method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
291                         method->info = NULL;
292                 }
293
294                 g_timer_start (timer);
295                 if (mini_stats_fd)
296                         fprintf (mini_stats_fd, "[");
297                 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
298                         method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
299                         if (strncmp (method->name, "test_", 5) == 0) {
300                                 expected = atoi (method->name + 5);
301                                 run++;
302                                 start_time = g_timer_elapsed (timer, NULL);
303                                 comp_time -= start_time; 
304                                 cfg = mini_method_compile (method, opt_flags, mono_root_domain, 0);
305                                 comp_time += g_timer_elapsed (timer, NULL);
306                                 if (cfg) {
307                                         if (verbose >= 2)
308                                                 g_print ("Running '%s' ...\n", method->name);
309 #ifdef MONO_USE_AOT_COMPILER
310                                         if (!(func = mono_aot_get_method (mono_root_domain, method)))
311 #endif
312                                                 func = (TestMethod)cfg->native_code;
313                                         result = func ();
314                                         if (result != expected) {
315                                                 failed++;
316                                                 if (verbose)
317                                                         g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
318                                         }
319                                         code_size += cfg->code_len;
320                                         mono_destroy_compile (cfg);
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 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: mono [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                 "    --debug                Enable debugging support\n"
480             "    --stats                Print statistics about the JIT operations\n"
481             "    --no-aot               Disable loading of AOT code\n"
482                 "\n"
483                 "Development:\n"
484                 "    --statfile FILE        Sets the stat file to FILE\n"
485                 "    --aot                  Compiles the assembly to native code\n"
486                 "    --profile[=profiler]   Runs in profiling mode with the specified profiler module\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                 "    --version, -V          Show version information\n"
500                 "    --optimize=OPT         Turns on a specific optimization:\n");
501
502         for (i = 0; i < G_N_ELEMENTS (opt_names); ++i)
503                 fprintf (stderr, "                           %-10s %s\n", opt_names [i].name, opt_names [i].desc);
504 }
505
506 int
507 mono_main (int argc, char* argv[]) {
508         MainThreadArgs main_args;
509         MonoAssembly *assembly;
510         MonoMethodDesc *desc;
511         MonoMethod *method;
512         MonoCompile *cfg;
513         MonoDomain *domain;
514         const char* aname, *mname = NULL;
515         char *config_file = NULL;
516         int i, count = 1;
517         int enable_debugging = FALSE;
518         guint32 opt, action = DO_EXEC;
519         MonoGraphOptions mono_graph_options = 0;
520         int mini_verbose = 0;
521
522         setlocale (LC_ALL, "");
523         g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
524         g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
525         
526         opt = parse_optimizations (NULL);
527
528         for (i = 1; i < argc; ++i) {
529                 if (argv [i] [0] != '-')
530                         break;
531                 if (strcmp (argv [i], "--regression") == 0) {
532                         action = DO_REGRESSION;
533                 } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
534                         mini_verbose++;
535                 } else if (strcmp (argv [i], "--version") == 0 || strcmp (argv [i], "-V") == 0) {
536                         g_print ("Mono JIT compiler version %s, (C) 2002, 2003 Ximian, Inc.\n", VERSION);
537                         return 0;
538                 } else if (strcmp (argv [i], "--help") == 0 || strcmp (argv [i], "-h") == 0) {
539                         mini_usage ();
540                         return 0;
541                 } else if (strncmp (argv [i], "--statfile", 10) == 0) {
542                         mini_stats_fd = fopen (argv [++i], "w+");
543                 } else if (strncmp (argv [i], "--optimize=", 11) == 0) {
544                         opt = parse_optimizations (argv [i] + 11);
545                 } else if (strncmp (argv [i], "-O=", 3) == 0) {
546                         opt = parse_optimizations (argv [i] + 3);
547                 } else if (strcmp (argv [i], "--config") == 0) {
548                         config_file = argv [++i];
549                 } else if (strcmp (argv [i], "--ncompile") == 0) {
550                         count = atoi (argv [++i]);
551                         action = DO_BENCH;
552                 } else if (strcmp (argv [i], "--trace") == 0) {
553                         mono_jit_trace_calls = TRUE;
554                 } else if (strcmp (argv [i], "--breakonex") == 0) {
555                         mono_break_on_exc = TRUE;
556                 } else if (strcmp (argv [i], "--break") == 0) {
557                         if (!mono_debugger_insert_breakpoint (argv [++i], FALSE))
558                                 g_error ("Invalid method name '%s'", argv [i]);
559                 } else if (strcmp (argv [i], "--print-vtable") == 0) {
560                         mono_print_vtable = TRUE;
561                 } else if (strcmp (argv [i], "--stats") == 0) {
562                         mono_jit_stats.enabled = TRUE;
563                 } else if (strcmp (argv [i], "--no-aot") == 0) {
564                         mono_no_aot = TRUE;
565                 } else if (strcmp (argv [i], "--aot") == 0) {
566                         mono_compile_aot = TRUE;
567                 } else if (strcmp (argv [i], "--compile-all") == 0) {
568                         action = DO_COMPILE;
569                 } else if (strcmp (argv [i], "--profile") == 0) {
570                         mono_profiler_load (NULL);
571                 } else if (strncmp (argv [i], "--profile=", 10) == 0) {
572                         mono_profiler_load (argv [i] + 10);
573                 } else if (strcmp (argv [i], "--compile") == 0) {
574                         mname = argv [++i];
575                         action = DO_BENCH;
576                 } else if (strncmp (argv [i], "--graph=", 8) == 0) {
577                         mono_graph_options = mono_parse_graph_options (argv [i] + 8);
578                         mname = argv [++i];
579                         action = DO_DRAW;
580                 } else if (strcmp (argv [i], "--graph") == 0) {
581                         mname = argv [++i];
582                         mono_graph_options = MONO_GRAPH_CFG;
583                         action = DO_DRAW;
584                 } else if (strcmp (argv [i], "--debug") == 0) {
585                         enable_debugging = TRUE;
586                 } else {
587                         fprintf (stderr, "Unknown command line option: '%s'\n", argv [i]);
588                         return 1;
589                 }
590         }
591
592         if (!argv [i]) {
593                 mini_usage ();
594                 return 1;
595         }
596
597         if (mono_compile_aot || action == DO_EXEC) {
598                 g_set_prgname (argv[i]);
599         }
600
601         mono_set_defaults (mini_verbose, opt);
602         domain = mini_init (argv [i]);
603
604         switch (action) {
605         case DO_REGRESSION:
606                 if (mini_regression_list (mini_verbose, argc -i, argv + i)) {
607                         g_print ("Regression ERRORS!\n");
608                         mini_cleanup (domain);
609                         return 1;
610                 }
611                 mini_cleanup (domain);
612                 return 0;
613         case DO_BENCH:
614                 if (argc - i != 1 || mname == NULL) {
615                         g_print ("Usage: mini --ncompile num --compile method assembly\n");
616                         mini_cleanup (domain);
617                         return 1;
618                 }
619                 aname = argv [i];
620                 break;
621         case DO_COMPILE:
622                 if (argc - i != 1) {
623                         mini_usage ();
624                         mini_cleanup (domain);
625                         return 1;
626                 }
627                 aname = argv [i];
628                 break;
629         case DO_DRAW:
630                 if (argc - i != 1 || mname == NULL) {
631                         mini_usage ();
632                         mini_cleanup (domain);
633                         return 1;
634                 }
635                 aname = argv [i];
636                 break;
637         default:
638                 if (argc - i < 1) {
639                         mini_usage ();
640                         mini_cleanup (domain);
641                         return 1;
642                 }
643                 aname = argv [i];
644                 break;
645         }
646
647         if (enable_debugging)
648                 mono_debug_init (domain, MONO_DEBUG_FORMAT_MONO);
649
650         assembly = mono_assembly_open (aname, NULL);
651         if (!assembly) {
652                 fprintf (stderr, "cannot open assembly %s\n", aname);
653                 mini_cleanup (domain);
654                 return 2;
655         }
656
657         if (enable_debugging)
658                 mono_debug_init_2 (assembly);
659
660         if (mono_compile_aot || action == DO_EXEC) {
661                 mono_config_parse (config_file);
662                 //mono_set_rootdir ();
663
664                 main_args.domain = domain;
665                 main_args.file = aname;         
666                 main_args.argc = argc - i;
667                 main_args.argv = argv + i;
668                 main_args.opts = opt;
669              
670                 mono_runtime_exec_managed_code (domain, main_thread_handler, &main_args);
671                 mini_cleanup (domain);
672                 /* Look up return value from System.Environment.ExitCode */
673                 i = mono_environment_exitcode_get ();
674                 return i;
675         } else if (action == DO_COMPILE) {
676                 compile_all_methods (assembly, mini_verbose);
677                 mini_cleanup (domain);
678                 return 0;
679         }
680         desc = mono_method_desc_new (mname, 0);
681         if (!desc) {
682                 g_print ("Invalid method name %s\n", mname);
683                 mini_cleanup (domain);
684                 return 3;
685         }
686         method = mono_method_desc_search_in_image (desc, assembly->image);
687         if (!method) {
688                 g_print ("Cannot find method %s\n", mname);
689                 mini_cleanup (domain);
690                 return 3;
691         }
692
693         if (action == DO_DRAW) {
694                 int part = 0;
695
696                 switch (mono_graph_options) {
697                 case MONO_GRAPH_DTREE:
698                         part = 1;
699                         opt |= MONO_OPT_LOOP;
700                         break;
701                 case MONO_GRAPH_CFG_CODE:
702                         part = 1;
703                         break;
704                 case MONO_GRAPH_CFG_SSA:
705                         part = 2;
706                         break;
707                 case MONO_GRAPH_CFG_OPTCODE:
708                         part = 3;
709                         break;
710                 default:
711                         break;
712                 }
713
714                 cfg = mini_method_compile (method, opt, mono_root_domain, part);
715                 if ((mono_graph_options & MONO_GRAPH_CFG_SSA) && !(cfg->comp_done & MONO_COMP_SSA)) {
716                         g_warning ("no SSA info available (use -O=deadce)");
717                         return 1;
718                 }
719                 mono_draw_graph (cfg, mono_graph_options);
720                 mono_destroy_compile (cfg);
721
722         } else if (action == DO_BENCH) {
723                 if (mini_stats_fd) {
724                         const char *n;
725                         double no_opt_time = 0.0;
726                         GTimer *timer = g_timer_new ();
727                         fprintf (mini_stats_fd, "$stattitle = \'Compilations times for %s\';\n", 
728                                  mono_method_full_name (method, TRUE));
729                         fprintf (mini_stats_fd, "@data = (\n");
730                         fprintf (mini_stats_fd, "[");
731                         for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
732                                 opt = opt_sets [i];
733                                 n = opt_descr (opt);
734                                 if (!n [0])
735                                         n = "none";
736                                 fprintf (mini_stats_fd, "\"%s\",", n);
737                         }
738                         fprintf (mini_stats_fd, "],\n[");
739
740                         for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) {
741                                 int j;
742                                 double elapsed;
743                                 opt = opt_sets [i];
744                                 g_timer_start (timer);
745                                 for (j = 0; j < count; ++j) {
746                                         cfg = mini_method_compile (method, opt, mono_root_domain, 0);
747                                         mono_destroy_compile (cfg);
748                                 }
749                                 g_timer_stop (timer);
750                                 elapsed = g_timer_elapsed (timer, NULL);
751                                 if (!opt)
752                                         no_opt_time = elapsed;
753                                 fprintf (mini_stats_fd, "%f, ", elapsed);
754                         }
755                         fprintf (mini_stats_fd, "]");
756                         if (no_opt_time > 0.0) {
757                                 fprintf (mini_stats_fd, ", \n[");
758                                 for (i = 0; i < G_N_ELEMENTS (opt_sets); i++) 
759                                         fprintf (mini_stats_fd, "%f,", no_opt_time);
760                                 fprintf (mini_stats_fd, "]");
761                         }
762                         fprintf (mini_stats_fd, ");\n");
763                 } else {
764                         for (i = 0; i < count; ++i) {
765                                 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
766                                 mono_destroy_compile (cfg);
767                         }
768                 }
769         } else {
770                 cfg = mini_method_compile (method, opt, mono_root_domain, 0);
771                 mono_destroy_compile (cfg);
772         }
773
774         mini_cleanup (domain);
775         return 0;
776 }
777
778 MonoDomain * 
779 mono_jit_init (const char *file)
780 {
781         return mini_init (file);
782 }
783
784 void        
785 mono_jit_cleanup (MonoDomain *domain)
786 {
787         mini_cleanup (domain);
788 }
789
790