Wed Apr 17 10:58:37 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / jit / mono.c
1
2 #include "jit.h"
3 #include "regset.h"
4 #include "codegen.h"
5 #include "debug.h"
6 #include "mono/metadata/debug-helpers.h"
7 #include "mono/metadata/verify.h"
8 #include "mono/metadata/profiler.h"
9
10 /**
11  * mono_jit_assembly:
12  * @assembly: reference to an assembly
13  *
14  * JIT compilation of all methods in the assembly. Prints debugging
15  * information on stdout.
16  */
17 static void
18 mono_jit_assembly (MonoAssembly *assembly)
19 {
20         MonoImage *image = assembly->image;
21         MonoMethod *method;
22         MonoTableInfo *t = &image->tables [MONO_TABLE_METHOD];
23         int i;
24
25         for (i = 0; i < t->rows; i++) {
26
27                 method = mono_get_method (image, 
28                                           (MONO_TABLE_METHOD << 24) | (i + 1), 
29                                           NULL);
30
31                 printf ("\nMethod: %s\n\n", method->name);
32
33                 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
34                         printf ("ABSTARCT\n");
35                 else
36                         arch_compile_method (method);
37
38         }
39
40 }
41
42 static void
43 usage (char *name)
44 {
45         fprintf (stderr,
46                  "%s %s, the Mono ECMA CLI JIT Compiler, (C) 2001 Ximian, Inc.\n\n"
47                  "Usage is: %s [options] executable args...\n", name,  VERSION, name);
48         fprintf (stderr,
49                  "Valid Options are:\n"
50                  "-d               debug the jit, show disassembler output.\n"
51                  "--dump-asm       dumps the assembly code generated\n"
52                  "--dump-forest    dumps the reconstructed forest\n"
53                  "--trace          printf function call trace\n"
54                  "--profile        record and dump profile info\n"
55                  "--share-code     force jit to produce shared code\n"
56                  "--print-vtable   print the VTable of all used classes\n"
57                  "--workers n      maximum number of worker threads\n"
58                  "--stabs          write stabs debug information\n"
59                  "--dwarf          write dwarf2 debug information\n"
60                  "--dwarf-plus     write extended dwarf2 debug information\n"
61                  "--stats          print statistics about the jit operations\n"
62                  "--compile cname  compile methods in given class (namespace.name[:methodname])\n"
63                  "--ncompile num   compile methods num times (default: 1000)\n"
64                  "--debug name     insert a breakpoint at the start of method name\n"
65                  "--help           print this help message\n");
66         exit (1);
67 }
68
69
70 int 
71 main (int argc, char *argv [])
72 {
73         MonoDomain *domain;
74         MonoAssembly *assembly;
75         int retval = 0, i;
76         int compile_times = 1000;
77         char *compile_class = NULL;
78         char *file, *error;
79         gboolean testjit = FALSE;
80         int stack, verbose = FALSE;
81
82         mono_end_of_stack = &stack; /* a pointer to a local variable is always < BP */
83
84         if (argc < 2)
85                 usage (argv [0]);
86
87         for (i = 1; i < argc && argv [i][0] == '-'; i++){
88                 if (strcmp (argv [i], "--help") == 0) {
89                         usage (argv [0]);
90                 } else if (strcmp (argv [i], "-d") == 0) {
91                         testjit = TRUE;
92                         mono_jit_dump_asm = TRUE;
93                         mono_jit_dump_forest = TRUE;
94                 } else if (strcmp (argv [i], "--dump-asm") == 0)
95                         mono_jit_dump_asm = TRUE;
96                 else if (strcmp (argv [i], "--dump-forest") == 0)
97                         mono_jit_dump_forest = TRUE;
98                 else if (strcmp (argv [i], "--trace") == 0)
99                         mono_jit_trace_calls = TRUE;
100                 else if (strcmp (argv [i], "--share-code") == 0)
101                         mono_jit_share_code = TRUE;
102                 else if (strcmp (argv [i], "--print-vtable") == 0)
103                         mono_print_vtable = TRUE;
104                 else if (strcmp (argv [i], "--debug") == 0) {
105                         MonoMethodDesc *desc = mono_method_desc_new (argv [++i], FALSE);
106                         if (!desc)
107                                 g_error ("Invalid method name '%s'", argv [i]);
108                         mono_debug_methods = g_list_append (mono_debug_methods, desc);
109                 } else if (strcmp (argv [i], "--count") == 0) {
110                         compile_times = atoi (argv [++i]);
111                 } else if (strcmp (argv [i], "--workers") == 0) {
112                         mono_worker_threads = atoi (argv [++i]);
113                         if (mono_worker_threads < 1)
114                                 mono_worker_threads = 1;
115                 } else if (strcmp (argv [i], "--profile") == 0) {
116                         mono_jit_profile = TRUE;
117                         mono_profiler_install_simple ();
118                 } else if (strcmp (argv [i], "--compile") == 0) {
119                         compile_class = argv [++i];
120                 } else if (strcmp (argv [i], "--ncompile") == 0) {
121                         compile_times = atoi (argv [++i]);
122                 } else if (strcmp (argv [i], "--stats") == 0) {
123                         memset (&mono_jit_stats, 0, sizeof (MonoJitStats));
124                         mono_jit_stats.enabled = TRUE;
125                 } else if (strcmp (argv [i], "--stabs") == 0) {
126                         if (mono_debug_handle)
127                                 g_error ("You can use either --stabs or --dwarf, but not both.");
128                         mono_debug_handle = mono_debug_open_file ("", MONO_DEBUG_FORMAT_STABS);
129                 } else if (strcmp (argv [i], "--dwarf") == 0) {
130                         if (mono_debug_handle)
131                                 g_error ("You can use either --stabs or --dwarf, but not both.");
132                         mono_debug_handle = mono_debug_open_file ("", MONO_DEBUG_FORMAT_DWARF2);
133                 } else if (strcmp (argv [i], "--dwarf-plus") == 0) {
134                         if (mono_debug_handle)
135                                 g_error ("You can use either --stabs or --dwarf, but not both.");
136                         mono_debug_handle = mono_debug_open_file ("", MONO_DEBUG_FORMAT_DWARF2_PLUS);
137                 } else if (strcmp (argv [i], "--verbose") == 0) {
138                         verbose = TRUE;;
139                 } else
140                         usage (argv [0]);
141         }
142         
143         file = argv [i];
144
145         if (!file)
146                 usage (argv [0]);
147
148         domain = mono_jit_init (file);
149
150         error = mono_verify_corlib ();
151         if (error) {
152                 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
153                 exit (1);
154         }
155
156         assembly = mono_domain_assembly_open (domain, file);
157         if (!assembly){
158                 fprintf (stderr, "Can not open image %s\n", file);
159                 exit (1);
160         }
161
162         if (testjit) {
163                 mono_jit_assembly (assembly);
164         } else if (compile_class) {
165                 const char *cmethod = strrchr (compile_class, ':');
166                 char *cname;
167                 char *code;
168                 int j;
169                 MonoClass *class;
170
171                 if (cmethod) {
172                         MonoMethodDesc *mdesc;
173                         MonoMethod *m;
174                         mdesc = mono_method_desc_new (compile_class, FALSE);
175                         if (!mdesc)
176                                 g_error ("Invalid method name '%s'", compile_class);
177                         m = mono_method_desc_search_in_image (mdesc, assembly->image);
178                         if (!m)
179                                 g_error ("Cannot find method '%s'", compile_class);
180                         for (j = 0; j < compile_times; ++j) {
181                                 code = arch_compile_method (m);
182                                 g_free (code);
183                         }
184                 } else {
185                         cname = strrchr (compile_class, '.');
186                         if (cname)
187                                 *cname++ = 0;
188                         else {
189                                 cname = compile_class;
190                                 compile_class = (char *)"";
191                         }
192                         class = mono_class_from_name (assembly->image, compile_class, cname);
193                         if (!class)
194                                 g_error ("Cannot find class %s.%s", compile_class, cname);
195                         mono_class_init (class);
196                         for (j = 0; j < compile_times; ++j) {
197                                 for (i = 0; i < class->method.count; ++i) {
198                                         if (class->methods [i]->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
199                                                 continue;
200                                         if (class->methods [i]->flags & METHOD_ATTRIBUTE_ABSTRACT)
201                                                 continue;
202                                         if (verbose)
203                                                 g_print ("Compiling: %s\n", class->methods [i]->name);
204                                         code = arch_compile_method (class->methods [i]);
205                                         g_free (code);
206                                 }
207                         }
208                 }
209         } else {
210                 /*
211                  * skip the program name from the args.
212                  */
213                 ++i;
214                 retval = mono_jit_exec (domain, assembly, argc - i, argv + i);
215                 printf ("RESULT: %d\n", retval);
216         }
217
218         mono_profiler_shutdown ();
219         mono_jit_cleanup (domain);
220
221         return retval;
222 }
223
224