2 * mono.c: Main driver for the Mono JIT engine
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
13 #include "mono/metadata/debug-helpers.h"
14 #include "mono/metadata/verify.h"
15 #include "mono/metadata/profiler.h"
16 #include <mono/os/util.h>
20 * @image: reference to an image
21 * @verbose: If true, print debugging information on stdout.
23 * JIT compilation of all methods in the image.
26 mono_jit_compile_image (MonoImage *image, int verbose)
29 MonoTableInfo *t = &image->tables [MONO_TABLE_METHOD];
32 for (i = 0; i < t->rows; i++) {
34 method = mono_get_method (image,
35 (MONO_TABLE_METHOD << 24) | (i + 1),
39 g_print ("Compiling: %s:%s\n\n", image->assembly_name, method->name);
41 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT) {
43 printf ("ABSTARCT\n");
45 arch_compile_method (method);
52 find_class_in_assembly (MonoAssembly *assembly, const char *namespace, const char *name)
57 class = mono_class_from_name (assembly->image, namespace, name);
61 for (ptr = assembly->image->references; ptr && *ptr; ptr++) {
62 class = find_class_in_assembly (*ptr, namespace, name);
71 find_method_in_assembly (MonoAssembly *assembly, MonoMethodDesc *mdesc)
76 method = mono_method_desc_search_in_image (mdesc, assembly->image);
80 for (ptr = assembly->image->references; ptr && *ptr; ptr++) {
81 method = find_method_in_assembly (*ptr, mdesc);
90 * mono_jit_compile_class:
91 * @assembly: Lookup things in this assembly
92 * @compile_class: Name of the image/class/method to compile
93 * @compile_times: Compile it that many times
94 * @verbose: If true, print debugging information on stdout.
96 * JIT compilation of the image/class/method.
98 * @compile_class can be one of:
100 * - an image name (`@corlib')
101 * - a class name (`System.Int32')
102 * - a method name (`System.Int32:Parse')
105 mono_jit_compile_class (MonoAssembly *assembly, char *compile_class,
106 int compile_times, int verbose)
108 const char *cmethod = strrchr (compile_class, ':');
114 if (compile_class [0] == '@') {
115 MonoImage *image = mono_image_loaded (compile_class + 1);
117 g_error ("Cannot find image %s", compile_class + 1);
119 g_print ("Compiling image %s\n", image->name);
120 for (j = 0; j < compile_times; ++j)
121 mono_jit_compile_image (image, verbose);
122 } else if (cmethod) {
123 MonoMethodDesc *mdesc;
125 mdesc = mono_method_desc_new (compile_class, FALSE);
127 g_error ("Invalid method name '%s'", compile_class);
128 m = find_method_in_assembly (assembly, mdesc);
130 g_error ("Cannot find method '%s'", compile_class);
131 for (j = 0; j < compile_times; ++j) {
132 code = arch_compile_method (m);
136 cname = strrchr (compile_class, '.');
140 cname = compile_class;
141 compile_class = (char *)"";
143 class = find_class_in_assembly (assembly, compile_class, cname);
145 g_error ("Cannot find class %s.%s", compile_class, cname);
146 mono_class_init (class);
147 for (j = 0; j < compile_times; ++j) {
148 for (i = 0; i < class->method.count; ++i) {
149 if (class->methods [i]->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
151 if (class->methods [i]->flags & METHOD_ATTRIBUTE_ABSTRACT)
154 g_print ("Compiling: %s.%s:%s\n",
155 compile_class, cname, class->methods [i]->name);
156 code = arch_compile_method (class->methods [i]);
167 "%s %s, the Mono ECMA CLI JIT Compiler, (C) 2001, 2002 Ximian, Inc.\n\n"
168 "Usage is: %s [options] executable args...\n\n", name, VERSION, name);
170 "Runtime Debugging:\n"
171 " -d debug the jit, show disassembler output.\n"
172 " --dump-asm dumps the assembly code generated\n"
173 " --dump-forest dumps the reconstructed forest\n"
174 " --print-vtable print the VTable of all used classes\n"
175 " --stats print statistics about the jit operations\n"
176 " --trace printf function call trace\n"
177 " --compile NAME compile NAME, then exit.\n"
178 " NAME is in one of the following formats:\n"
179 " namespace.name compile the given class\n"
180 " namespace.name:method compile the given method\n"
181 " @imagename compile the given image\n"
182 " --ncompile NUM compile methods NUM times (default: 1000)\n"
185 " --debug[=FORMAT] write a debugging file. FORMAT is one of:\n"
186 " stabs to write stabs information\n"
187 " dwarf to write dwarf2 information\n"
188 " dwarf-plus to write extended dwarf2 information\n"
189 " --debug-args ARGS comma-separated list of additional arguments for the\n"
190 " symbol writer. See the manpage for details.\n"
191 " --profile record and dump profile info\n"
192 " --break NAME insert a breakpoint at the start of method NAME\n"
193 " (NAME is in `namespace.name:methodname' format)\n"
194 " --precomile name precompile NAME before executing the main application:\n"
195 " NAME is in one of the following formats:\n"
196 " namespace.name compile the given class\n"
197 " namespace.name:method compile the given method\n"
198 " @imagename compile the given image\n"
201 " --fast-iconv Use fast floating point integer conversion\n"
202 " --noinline Disable code inliner\n"
203 " --nols disable linear scan register allocation\n"
204 " --share-code force jit to produce shared code\n"
205 " --workers n maximum number of worker threads\n"
211 main (int argc, char *argv [])
214 MonoAssembly *assembly;
216 int compile_times = 1000;
217 char *compile_class = NULL;
218 char *debug_args = NULL;
220 gboolean testjit = FALSE;
221 int stack, verbose = FALSE;
222 GList *precompile_classes = NULL;
224 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
225 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
230 for (i = 1; i < argc && argv [i][0] == '-'; i++){
231 if (strcmp (argv [i], "--help") == 0) {
233 } else if (strcmp (argv [i], "-d") == 0) {
235 mono_jit_dump_asm = TRUE;
236 mono_jit_dump_forest = TRUE;
237 } else if (strcmp (argv [i], "--dump-asm") == 0)
238 mono_jit_dump_asm = TRUE;
239 else if (strcmp (argv [i], "--dump-forest") == 0)
240 mono_jit_dump_forest = TRUE;
241 else if (strcmp (argv [i], "--trace") == 0)
242 mono_jit_trace_calls = TRUE;
243 else if (strcmp (argv [i], "--share-code") == 0)
244 mono_jit_share_code = TRUE;
245 else if (strcmp (argv [i], "--noinline") == 0)
246 mono_jit_inline_code = FALSE;
247 else if (strcmp (argv [i], "--nols") == 0)
248 mono_use_linear_scan = FALSE;
249 else if (strcmp (argv [i], "--print-vtable") == 0)
250 mono_print_vtable = TRUE;
251 else if (strcmp (argv [i], "--break") == 0) {
252 MonoMethodDesc *desc = mono_method_desc_new (argv [++i], FALSE);
254 g_error ("Invalid method name '%s'", argv [i]);
255 mono_debug_methods = g_list_append (mono_debug_methods, desc);
256 } else if (strcmp (argv [i], "--count") == 0) {
257 compile_times = atoi (argv [++i]);
258 } else if (strcmp (argv [i], "--workers") == 0) {
259 mono_worker_threads = atoi (argv [++i]);
260 if (mono_worker_threads < 1)
261 mono_worker_threads = 1;
262 } else if (strcmp (argv [i], "--profile") == 0) {
263 mono_jit_profile = TRUE;
264 mono_profiler_install_simple ();
265 } else if (strcmp (argv [i], "--compile") == 0) {
266 compile_class = argv [++i];
267 } else if (strcmp (argv [i], "--ncompile") == 0) {
268 compile_times = atoi (argv [++i]);
269 } else if (strcmp (argv [i], "--stats") == 0) {
270 memset (&mono_jit_stats, 0, sizeof (MonoJitStats));
271 mono_jit_stats.enabled = TRUE;
272 } else if (strncmp (argv [i], "--debug=", 8) == 0) {
273 const char *format = &argv [i][8];
275 if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
276 g_error ("You can only use one debugging format.");
277 if (strcmp (format, "stabs") == 0)
278 mono_debug_format = MONO_DEBUG_FORMAT_STABS;
279 else if (strcmp (format, "dwarf") == 0)
280 mono_debug_format = MONO_DEBUG_FORMAT_DWARF2;
281 else if (strcmp (format, "dwarf-plus") == 0)
282 mono_debug_format = MONO_DEBUG_FORMAT_DWARF2_PLUS;
284 g_error ("Unknown debugging format: %s", argv [i] + 8);
285 } else if (strcmp (argv [i], "--debug") == 0) {
286 if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
287 g_error ("You can only use one debugging format.");
288 mono_debug_format = MONO_DEBUG_FORMAT_DWARF2_PLUS;
289 } else if (strcmp (argv [i], "--debug-args") == 0) {
291 g_error ("You can use --debug-args only once.");
292 debug_args = argv [++i];
293 } else if (strcmp (argv [i], "--precompile") == 0) {
294 precompile_classes = g_list_append (precompile_classes, argv [++i]);
295 } else if (strcmp (argv [i], "--verbose") == 0) {
297 } else if (strcmp (argv [i], "--fast-iconv") == 0) {
298 mono_use_fast_iconv = TRUE;
308 mono_set_rootdir (argv [0]);
309 domain = mono_jit_init (file);
311 error = mono_verify_corlib ();
313 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
317 assembly = mono_domain_assembly_open (domain, file);
319 fprintf (stderr, "Can not open image %s\n", file);
323 if (mono_debug_format != MONO_DEBUG_FORMAT_NONE) {
326 args = g_strsplit (debug_args ? debug_args : "", ",", -1);
327 mono_debug_open (assembly, mono_debug_format, (const char **) args);
332 mono_jit_compile_image (assembly->image, TRUE);
333 } else if (compile_class) {
334 mono_jit_compile_class (assembly, compile_class, compile_times, TRUE);
338 for (tmp = precompile_classes; tmp; tmp = tmp->next)
339 mono_jit_compile_class (assembly, tmp->data, 1, verbose);
341 retval = mono_jit_exec (domain, assembly, argc - i, argv + i);
342 printf ("RESULT: %d\n", retval);
345 mono_profiler_shutdown ();
346 mono_jit_cleanup (domain);