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