*** empty log message ***
[cacao.git] / main.c
1 /* main.c **********************************************************************
2
3         Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
4
5         See file COPYRIGHT for information on usage and disclaimer of warranties
6
7         Contains main() and variables for the global options.
8         This module does the following tasks:
9            - Command line option handling
10            - Calling initialization routines
11            - Calling the class loader
12            - Running the main method
13
14         Authors: Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at
15         Changes: Andi Krall          EMAIL: cacao@complang.tuwien.ac.at
16                  Mark Probst         EMAIL: cacao@complang.tuwien.ac.at
17                          Philipp Tomsich     EMAIL: cacao@complang.tuwien.ac.at
18
19         Last Change: $Id: main.c 285 2003-05-12 20:05:38Z carolyn $
20
21 *******************************************************************************/
22
23 #include "global.h"
24
25 #include "tables.h"
26 #include "loader.h"
27 #include "jit.h"
28 #ifdef OLD_COMPILER
29 #include "compiler.h"
30 #endif
31
32 #include "asmpart.h"
33 #include "builtin.h"
34 #include "native.h"
35
36 #include "threads/thread.h"
37
38 bool compileall = false;
39 int  newcompiler = true;                
40 bool verbose =  false;
41 #ifdef NEW_GC
42 bool new_gc = false;
43 #endif
44
45 static bool showmethods = false;
46 static bool showconstantpool = false;
47 static bool showutf = false;
48 static classinfo *topclass;
49
50 #ifndef USE_THREADS
51 void **stackbottom = 0;
52 #endif
53
54
55 /* internal function: get_opt *************************************************
56         
57         decodes the next command line option
58         
59 ******************************************************************************/
60
61 #define OPT_DONE  -1
62 #define OPT_ERROR  0
63 #define OPT_IGNORE 1
64
65 #define OPT_CLASSPATH   2
66 #define OPT_D           3
67 #define OPT_MS          4
68 #define OPT_MX          5
69 #define OPT_VERBOSE1    6
70 #define OPT_VERBOSE     7
71 #define OPT_VERBOSEGC   8
72 #define OPT_VERBOSECALL 9
73 #define OPT_IEEE        10
74 #define OPT_SOFTNULL    11
75 #define OPT_TIME        12
76 #define OPT_STAT        13
77 #define OPT_LOG         14
78 #define OPT_CHECK       15
79 #define OPT_LOAD        16
80 #define OPT_METHOD      17
81 #define OPT_SIGNATURE   18
82 #define OPT_SHOW        19
83 #define OPT_ALL         20
84 #ifdef OLD_COMPILER
85 #define OPT_OLD         21
86 #endif
87 #ifdef NEW_GC
88 #define OPT_GC1         22
89 #define OPT_GC2         23
90 #endif
91 #define OPT_OLOOP       24
92 #define OPT_RT          25
93 #define OPT_XTA         26 
94
95 struct {char *name; bool arg; int value;} opts[] = {
96         {"classpath",   true,   OPT_CLASSPATH},
97         {"D",           true,   OPT_D},
98         {"ms",          true,   OPT_MS},
99         {"mx",          true,   OPT_MX},
100         {"noasyncgc",   false,  OPT_IGNORE},
101         {"noverify",    false,  OPT_IGNORE},
102         {"oss",         true,   OPT_IGNORE},
103         {"ss",          true,   OPT_IGNORE},
104         {"v",           false,  OPT_VERBOSE1},
105         {"verbose",     false,  OPT_VERBOSE},
106         {"verbosegc",   false,  OPT_VERBOSEGC},
107         {"verbosecall", false,  OPT_VERBOSECALL},
108         {"ieee",        false,  OPT_IEEE},
109         {"softnull",    false,  OPT_SOFTNULL},
110         {"time",        false,  OPT_TIME},
111         {"stat",        false,  OPT_STAT},
112         {"log",         true,   OPT_LOG},
113         {"c",           true,   OPT_CHECK},
114         {"l",           false,  OPT_LOAD},
115         {"m",           true,   OPT_METHOD},
116         {"sig",         true,   OPT_SIGNATURE},
117         {"s",           true,   OPT_SHOW},
118         {"all",         false,  OPT_ALL},
119 #ifdef OLD_COMPILER
120         {"old",         false,  OPT_OLD},
121 #endif
122 #ifdef NEW_GC
123         {"gc1",         false,  OPT_GC1},
124         {"gc2",         false,  OPT_GC2},
125 #endif
126         {"oloop",       false,  OPT_OLOOP},
127         {"rt",          false,  OPT_RT},
128         {"xta",         false,  OPT_XTA},
129         {NULL,  false, 0}
130 };
131
132 static int opt_ind = 1;
133 static char *opt_arg;
134
135 static int get_opt (int argc, char **argv) 
136 {
137         char *a;
138         int i;
139         
140         if (opt_ind >= argc) return OPT_DONE;
141         
142         a = argv[opt_ind];
143         if (a[0] != '-') return OPT_DONE;
144
145         for (i=0; opts[i].name; i++) {
146                 if (! opts[i].arg) {
147                         if (strcmp(a+1, opts[i].name) == 0) {  /* boolean option found */
148                                 opt_ind++;
149                                 return opts[i].value;
150                         }
151                 }
152                 else {
153                         if (strcmp(a+1, opts[i].name) == 0) { /* parameter option found */
154                                 opt_ind++;
155                                 if (opt_ind < argc) {
156                                         opt_arg = argv[opt_ind];
157                                         opt_ind++;
158                                         return opts[i].value;
159                                 }
160                                 return OPT_ERROR;
161                         }
162                         else {
163                                 size_t l = strlen(opts[i].name);
164                                 if (strlen(a+1) > l) {
165                                         if (memcmp (a+1, opts[i].name, l)==0) {
166                                                 opt_ind++;
167                                                 opt_arg = a+1+l;
168                                                 return opts[i].value;
169                                         }
170                                 }
171                         }
172                 }
173         } /* end for */ 
174
175         return OPT_ERROR;
176 }
177
178
179
180
181 /******************** interne Function: print_usage ************************
182
183 Prints the correct usage syntax to stdout.
184
185 ***************************************************************************/
186
187 static void print_usage()
188 {
189         printf ("USAGE: cacao [options] classname [program arguments\n");
190         printf ("Options:\n");
191         printf ("          -classpath path ...... specify a path to look for classes\n");
192         printf ("          -Dpropertyname=value . add an entry to the property list\n");
193         printf ("          -mx maxmem[k|m] ...... specify the size for the heap\n");
194         printf ("          -ms initmem[k|m] ..... specify the initial size for the heap\n");
195         printf ("          -v ................... write state-information\n");
196         printf ("          -verbose ............. write more information\n");
197         printf ("          -verbosegc ........... write message for each GC\n");
198         printf ("          -verbosecall ......... write message for each call\n");
199         printf ("          -ieee ................ use ieee compliant arithmetic\n");
200         printf ("          -softnull ............ use software nullpointer check\n");
201         printf ("          -time ................ measure the runtime\n");
202         printf ("          -stat ................ detailed compiler statistics\n");
203         printf ("          -log logfile ......... specify a name for the logfile\n");
204         printf ("          -c(heck)b(ounds) ..... don't check array bounds\n");
205         printf ("                  s(ync) ....... don't check for synchronization\n");
206         printf ("          -oloop ............... optimize array accesses in loops\n"); 
207         printf ("          -l ................... don't start the class after loading\n");
208         printf ("          -all ................. compile all methods, no execution\n");
209 #ifdef OLD_COMPILER
210         printf ("          -old ................. use old JIT compiler\n");
211 #endif
212 #ifdef NEW_GC
213         printf ("          -gc1 ................. use the old garbage collector (default)\n");
214         printf ("          -gc2 ................. use the new garbage collector\n");
215 #endif
216         printf ("          -m ................... compile only a specific method\n");
217         printf ("          -sig ................. specify signature for a specific method\n");
218         printf ("          -s(how)a(ssembler) ... show disassembled listing\n");
219         printf ("                 c(onstants) ... show the constant pool\n");
220         printf ("                 d(atasegment).. show data segment listing\n");
221         printf ("                 i(ntermediate). show intermediate representation\n");
222         printf ("                 m(ethods)...... show class fields and methods\n");
223 #ifdef OLD_COMPILER
224         printf ("                 s(tack) ....... show stack for every javaVM-command\n");
225 #endif
226         printf ("                 u(tf) ......... show the utf - hash\n");
227         printf ("          -rt .................. use rapid type analysis\n");
228         printf ("          -xta ................. use xta\n");
229 }   
230
231
232
233 /***************************** Function: print_times *********************
234
235         Prints a summary of CPU time usage.
236
237 **************************************************************************/
238
239 static void print_times()
240 {
241         long int totaltime = getcputime();
242         long int runtime = totaltime - loadingtime - compilingtime;
243
244         sprintf (logtext, "Time for loading classes: %ld secs, %ld millis",
245              loadingtime / 1000000, (loadingtime % 1000000) / 1000);
246         dolog();
247         sprintf (logtext, "Time for compiling code:  %ld secs, %ld millis",
248              compilingtime / 1000000, (compilingtime % 1000000) / 1000);
249         dolog();
250         sprintf (logtext, "Time for running program: %ld secs, %ld millis",
251              runtime / 1000000, (runtime % 1000000) / 1000);
252         dolog();
253         sprintf (logtext, "Total time: %ld secs, %ld millis",
254              totaltime / 1000000, (totaltime % 1000000) / 1000);
255         dolog();
256 }
257
258
259
260
261
262
263 /***************************** Function: print_stats *********************
264
265         outputs detailed compiler statistics
266
267 **************************************************************************/
268
269 static void print_stats()
270 {
271         sprintf (logtext, "Number of JitCompiler Calls: %d", count_jit_calls);
272         dolog();
273         sprintf (logtext, "Number of compiled Methods: %d", count_methods);
274         dolog();
275         sprintf (logtext, "Number of max basic blocks per method: %d", count_max_basic_blocks);
276         dolog();
277         sprintf (logtext, "Number of compiled basic blocks: %d", count_basic_blocks);
278         dolog();
279         sprintf (logtext, "Number of max JavaVM-Instructions per method: %d", count_max_javainstr);
280         dolog();
281         sprintf (logtext, "Number of compiled JavaVM-Instructions: %d", count_javainstr);
282         dolog();
283         sprintf (logtext, "Size of compiled JavaVM-Instructions:   %d(%d)", count_javacodesize,
284                                                       count_javacodesize - count_methods * 18);
285         dolog();
286         sprintf (logtext, "Size of compiled Exception Tables:      %d", count_javaexcsize);
287         dolog();
288         sprintf (logtext, "Value of extended instruction set var:  %d", has_ext_instr_set);
289         dolog();
290         sprintf (logtext, "Number of Alpha-Instructions: %d", count_code_len >> 2);
291         dolog();
292         sprintf (logtext, "Number of Spills: %d", count_spills);
293         dolog();
294         sprintf (logtext, "Number of Activ    Pseudocommands: %5d", count_pcmd_activ);
295         dolog();
296         sprintf (logtext, "Number of Drop     Pseudocommands: %5d", count_pcmd_drop);
297         dolog();
298         sprintf (logtext, "Number of Const    Pseudocommands: %5d (zero:%5d)", count_pcmd_load, count_pcmd_zero);
299         dolog();
300         sprintf (logtext, "Number of ConstAlu Pseudocommands: %5d (cmp: %5d, store:%5d)", count_pcmd_const_alu, count_pcmd_const_bra, count_pcmd_const_store);
301         dolog();
302         sprintf (logtext, "Number of Move     Pseudocommands: %5d", count_pcmd_move);
303         dolog();
304         sprintf (logtext, "Number of Load     Pseudocommands: %5d", count_load_instruction);
305         dolog();
306         sprintf (logtext, "Number of Store    Pseudocommands: %5d (combined: %5d)", count_pcmd_store, count_pcmd_store - count_pcmd_store_comb);
307         dolog();
308         sprintf (logtext, "Number of OP       Pseudocommands: %5d", count_pcmd_op);
309         dolog();
310         sprintf (logtext, "Number of DUP      Pseudocommands: %5d", count_dup_instruction);
311         dolog();
312         sprintf (logtext, "Number of Mem      Pseudocommands: %5d", count_pcmd_mem);
313         dolog();
314         sprintf (logtext, "Number of Method   Pseudocommands: %5d", count_pcmd_met);
315         dolog();
316         sprintf (logtext, "Number of Branch   Pseudocommands: %5d (rets:%5d, Xrets: %5d)",
317                           count_pcmd_bra, count_pcmd_return, count_pcmd_returnx);
318         dolog();
319         sprintf (logtext, "Number of Table    Pseudocommands: %5d", count_pcmd_table);
320         dolog();
321         sprintf (logtext, "Number of Useful   Pseudocommands: %5d", count_pcmd_table +
322                  count_pcmd_bra + count_pcmd_load + count_pcmd_mem + count_pcmd_op);
323         dolog();
324         sprintf (logtext, "Number of Null Pointer Checks:     %5d", count_check_null);
325         dolog();
326         sprintf (logtext, "Number of Array Bound Checks:      %5d", count_check_bound);
327         dolog();
328         sprintf (logtext, "Number of Try-Blocks: %d", count_tryblocks);
329         dolog();
330         sprintf (logtext, "Maximal count of stack elements:   %d", count_max_new_stack);
331         dolog();
332         sprintf (logtext, "Upper bound of max stack elements: %d", count_upper_bound_new_stack);
333         dolog();
334         sprintf (logtext, "Distribution of stack sizes at block boundary");
335         dolog();
336         sprintf (logtext, "    0    1    2    3    4    5    6    7    8    9    >=10");
337         dolog();
338         sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_block_stack[0],
339                 count_block_stack[1],count_block_stack[2],count_block_stack[3],count_block_stack[4],
340                 count_block_stack[5],count_block_stack[6],count_block_stack[7],count_block_stack[8],
341                 count_block_stack[9],count_block_stack[10]);
342         dolog();
343         sprintf (logtext, "Distribution of store stack depth");
344         dolog();
345         sprintf (logtext, "    0    1    2    3    4    5    6    7    8    9    >=10");
346         dolog();
347         sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_depth[0],
348                 count_store_depth[1],count_store_depth[2],count_store_depth[3],count_store_depth[4],
349                 count_store_depth[5],count_store_depth[6],count_store_depth[7],count_store_depth[8],
350                 count_store_depth[9],count_store_depth[10]);
351         dolog();
352         sprintf (logtext, "Distribution of store creator chains first part");
353         dolog();
354         sprintf (logtext, "    0    1    2    3    4    5    6    7    8    9  ");
355         dolog();
356         sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[0],
357                 count_store_length[1],count_store_length[2],count_store_length[3],count_store_length[4],
358                 count_store_length[5],count_store_length[6],count_store_length[7],count_store_length[8],
359                 count_store_length[9]);
360         dolog();
361         sprintf (logtext, "Distribution of store creator chains second part");
362         dolog();
363         sprintf (logtext, "   10   11   12   13   14   15   16   17   18   19  >=20");
364         dolog();
365         sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[10],
366                 count_store_length[11],count_store_length[12],count_store_length[13],count_store_length[14],
367                 count_store_length[15],count_store_length[16],count_store_length[17],count_store_length[18],
368                 count_store_length[19],count_store_length[20]);
369         dolog();
370         sprintf (logtext, "Distribution of analysis iterations");
371         dolog();
372         sprintf (logtext, "    1    2    3    4    >=5");
373         dolog();
374         sprintf (logtext, "%5d%5d%5d%5d%5d", count_analyse_iterations[0],count_analyse_iterations[1],
375                 count_analyse_iterations[2],count_analyse_iterations[3],count_analyse_iterations[4]);
376         dolog();
377         sprintf (logtext, "Distribution of basic blocks per method");
378         dolog();
379         sprintf (logtext, " <= 5 <=10 <=15 <=20 <=30 <=40 <=50 <=75  >75");
380         dolog();
381         sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_method_bb_distribution[0],
382                 count_method_bb_distribution[1],count_method_bb_distribution[2],count_method_bb_distribution[3],
383                 count_method_bb_distribution[4],count_method_bb_distribution[5],count_method_bb_distribution[6],
384                 count_method_bb_distribution[7],count_method_bb_distribution[8]);
385         dolog();
386         sprintf (logtext, "Distribution of basic block sizes");
387         dolog();
388         sprintf (logtext,
389         "  0    1    2    3    4   5   6   7   8   9 <13 <15 <17 <19 <21 <26 <31 >30");
390         dolog();
391         sprintf (logtext, "%3d%5d%5d%5d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d",
392                 count_block_size_distribution[0], count_block_size_distribution[1], count_block_size_distribution[2],
393                 count_block_size_distribution[3], count_block_size_distribution[4], count_block_size_distribution[5],
394                 count_block_size_distribution[6], count_block_size_distribution[7], count_block_size_distribution[8],
395                 count_block_size_distribution[9], count_block_size_distribution[10],count_block_size_distribution[11],
396                 count_block_size_distribution[12],count_block_size_distribution[13],count_block_size_distribution[14],
397                 count_block_size_distribution[15],count_block_size_distribution[16],count_block_size_distribution[17]);
398         dolog();
399         sprintf (logtext, "Size of Code Area (Kb):  %10.3f", (float) count_code_len / 1024);
400         dolog();
401         sprintf (logtext, "Size of data Area (Kb):  %10.3f", (float) count_data_len / 1024);
402         dolog();
403         sprintf (logtext, "Size of Class Infos (Kb):%10.3f", (float) (count_class_infos) / 1024);
404         dolog();
405         sprintf (logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_utf_len) / 1024);
406         dolog();
407         sprintf (logtext, "Size of Vftbl (Kb):      %10.3f", (float) count_vftbl_len / 1024);
408         dolog();
409         sprintf (logtext, "Size of comp stub (Kb):  %10.3f", (float) count_cstub_len / 1024);
410         dolog();
411         sprintf (logtext, "Size of native stub (Kb):%10.3f", (float) count_nstub_len / 1024);
412         dolog();
413         sprintf (logtext, "Size of Utf (Kb):        %10.3f", (float) count_utf_len / 1024);
414         dolog();
415         sprintf (logtext, "Size of VMCode (Kb):     %10.3f(%d)", (float) count_vmcode_len / 1024,
416                                                       count_vmcode_len - 18 * count_all_methods);
417         dolog();
418         sprintf (logtext, "Size of ExTable (Kb):    %10.3f", (float) count_extable_len / 1024);
419         dolog();
420         sprintf (logtext, "Number of class loads:   %d", count_class_loads);
421         dolog();
422         sprintf (logtext, "Number of class inits:   %d", count_class_inits);
423         dolog();
424         sprintf (logtext, "Number of loaded Methods: %d\n\n", count_all_methods);
425         dolog();
426
427         sprintf (logtext, "Calls of utf_new: %22d", count_utf_new);
428         dolog();
429         sprintf (logtext, "Calls of utf_new (element found): %6d\n\n", count_utf_new_found);
430         dolog();
431 }
432
433
434 /********** Function: class_compile_methods   (debugging only) ********/
435
436 void class_compile_methods ()
437 {
438         int        i;
439         classinfo  *c;
440         methodinfo *m;
441         
442         c = list_first (&linkedclasses);
443         while (c) {
444                 for (i = 0; i < c -> methodscount; i++) {
445                         m = &(c->methods[i]);
446                         if (m->jcode) {
447 #ifdef OLD_COMPILER
448                                 if (newcompiler)
449 #endif
450                                         (void) jit_compile(m);
451 #ifdef OLD_COMPILER
452                                 else
453                                         (void) compiler_compile(m);
454 #endif
455                                 }
456                         }
457                 c = list_next (&linkedclasses, c);
458                 }
459 }
460
461 /*
462  * void exit_handler(void)
463  * -----------------------
464  * The exit_handler function is called upon program termination to shutdown
465  * the various subsystems and release the resources allocated to the VM.
466  */
467
468 void exit_handler(void)
469 {
470         /********************* Print debug tables ************************/
471                                 
472         if (showmethods) class_showmethods (topclass);
473         if (showconstantpool)  class_showconstantpool (topclass);
474         if (showutf)           utf_show ();
475
476 #ifdef USE_THREADS
477         clear_thread_flags();           /* restores standard file descriptor
478                                                                    flags */
479 #endif
480
481         /************************ Free all resources *******************/
482
483         heap_close ();                          /* must be called before compiler_close and
484                                                                    loader_close because finalization occurs
485                                                                    here */
486
487 #ifdef OLD_COMPILER
488         compiler_close ();
489 #endif
490         loader_close ();
491         tables_close ( literalstring_free );
492
493         if (verbose || getcompilingtime || statistics) {
494                 log_text ("CACAO terminated");
495                 if (statistics)
496                         print_stats ();
497                 if (getcompilingtime)
498                         print_times ();
499                 mem_usagelog(1);
500         }
501 }
502
503 /************************** Function: main *******************************
504
505    The main program.
506    
507 **************************************************************************/
508
509 int main(int argc, char **argv)
510 {
511         s4 i,j;
512         char *cp;
513         java_objectheader *local_exceptionptr = 0;
514         void *dummy;
515         
516         /********** interne (nur fuer main relevante Optionen) **************/
517    
518         char logfilename[200] = "";
519         u4 heapsize = 64000000;
520         u4 heapstartsize = 200000;
521         char classpath[500] = ".:/usr/local/lib/java/classes";
522         bool startit = true;
523         char *specificmethodname = NULL;
524         char *specificsignature = NULL;
525
526 #ifndef USE_THREADS
527         stackbottom = &dummy;
528 #endif
529         
530         if (0 != atexit(exit_handler))
531                 panic("unable to register exit_handler");
532
533         /************ Collect info from the environment ************************/
534
535         cp = getenv ("CLASSPATH");
536         if (cp) {
537                 strcpy (classpath, cp);
538         }
539
540         /***************** Interpret the command line *****************/
541    
542         checknull = false;
543         checkfloats = false;
544
545         while ( (i = get_opt(argc,argv)) != OPT_DONE) {
546
547                 switch (i) {
548                 case OPT_IGNORE: break;
549                         
550                 case OPT_CLASSPATH:    
551                         strcpy (classpath + strlen(classpath), ":");
552                         strcpy (classpath + strlen(classpath), opt_arg);
553                         break;
554                                 
555                 case OPT_D:
556                         {
557                                 int n,l=strlen(opt_arg);
558                                 for (n=0; n<l; n++) {
559                                         if (opt_arg[n]=='=') {
560                                                 opt_arg[n] = '\0';
561                                                 attach_property (opt_arg, opt_arg+n+1);
562                                                 goto didit;
563                                         }
564                                 }
565                                 print_usage();
566                                 exit(10);
567                                         
568                         didit: ;
569                         }       
570                 break;
571                                 
572                 case OPT_MS:
573                 case OPT_MX:
574                         if (opt_arg[strlen(opt_arg)-1] == 'k') {
575                                 j = 1024 * atoi(opt_arg);
576                         }
577                         else if (opt_arg[strlen(opt_arg)-1] == 'm') {
578                                 j = 1024 * 1024 * atoi(opt_arg);
579                         }
580                         else j = atoi(opt_arg);
581                                 
582                         if (i==OPT_MX) heapsize = j;
583                         else heapstartsize = j;
584                         break;
585
586                 case OPT_VERBOSE1:
587                         verbose = true;
588                         break;
589                                                                 
590                 case OPT_VERBOSE:
591                         verbose = true;
592                         loadverbose = true;
593                         initverbose = true;
594                         compileverbose = true;
595                         break;
596                                 
597                 case OPT_VERBOSEGC:
598                         collectverbose = true;
599                         break;
600                                 
601                 case OPT_VERBOSECALL:
602                         runverbose = true;
603                         break;
604                                 
605                 case OPT_IEEE:
606                         checkfloats = true;
607                         break;
608
609                 case OPT_SOFTNULL:
610                         checknull = true;
611                         break;
612
613                 case OPT_TIME:
614                         getcompilingtime = true;
615                         getloadingtime = true;
616                         break;
617                                         
618                 case OPT_STAT:
619                         statistics = true;
620                         break;
621                                         
622                 case OPT_LOG:
623                         strcpy (logfilename, opt_arg);
624                         break;
625                         
626                         
627                 case OPT_CHECK:
628                         for (j=0; j<strlen(opt_arg); j++) {
629                                 switch (opt_arg[j]) {
630                                 case 'b': checkbounds=false; break;
631                                 case 's': checksync=false; break;
632                                 default:  print_usage();
633                                         exit(10);
634                                 }
635                         }
636                         break;
637                         
638                 case OPT_LOAD:
639                         startit = false;
640                         makeinitializations = false;
641                         break;
642
643                 case OPT_METHOD:
644                         startit = false;
645                         specificmethodname = opt_arg;                   
646                         makeinitializations = false;
647                         break;
648                         
649                 case OPT_SIGNATURE:
650                         specificsignature = opt_arg;                    
651                         break;
652                         
653                 case OPT_ALL:
654                         compileall = true;              
655                         startit = false;
656                         makeinitializations = false;
657                         break;
658                         
659 #ifdef OLD_COMPILER
660                 case OPT_OLD:
661                         newcompiler = false;                    
662                         checknull = true;
663                         break;
664 #endif
665
666 #ifdef NEW_GC
667                 case OPT_GC2:
668                         new_gc = true;
669                         break;
670
671                 case OPT_GC1:
672                         new_gc = false;
673                         break;
674 #endif
675                         
676                 case OPT_SHOW:       /* Display options */
677                         for (j=0; j<strlen(opt_arg); j++) {             
678                                 switch (opt_arg[j]) {
679                                 case 'a':  showdisassemble=true; compileverbose=true; break;
680                                 case 'c':  showconstantpool=true; break;
681                                 case 'd':  showddatasegment=true; break;
682                                 case 'i':  showintermediate=true; compileverbose=true; break;
683                                 case 'm':  showmethods=true; break;
684 #ifdef OLD_COMPILER
685                                 case 's':  showstack=true; compileverbose=true; break;
686 #endif
687                                 case 'u':  showutf=true; break;
688                                 default:   print_usage();
689                                         exit(10);
690                                 }
691                         }
692                         break;
693                         
694                 case OPT_OLOOP:
695                         opt_loops = true;
696                         break;
697
698                case OPT_RT:
699                         opt_rt = true;
700                         break;
701
702                case OPT_XTA:
703                         opt_xta = true;
704                         break;
705
706                 default:
707                         print_usage();
708                         exit(10);
709                 }
710                         
711                         
712         }
713    
714    
715         if (opt_ind >= argc) {
716                 print_usage ();
717                 exit(10);
718         }
719
720
721         /**************************** Program start *****************************/
722
723         log_init (logfilename);
724         if (verbose) {
725                 log_text (
726                                   "CACAO started -------------------------------------------------------");
727         }
728         
729         suck_init (classpath);
730         native_setclasspath (classpath);
731                 
732         tables_init();
733         heap_init(heapsize, heapstartsize, &dummy);
734 #ifdef OLD_COMPILER
735         compiler_init();
736 #endif
737         jit_init();
738         loader_init();
739
740         native_loadclasses ();
741
742
743         /*********************** Load JAVA classes  ***************************/
744    
745         cp = argv[opt_ind++];
746         for (i=strlen(cp)-1; i>=0; i--) {     /* Transform dots into slashes */
747                 if (cp[i]=='.') cp[i]='/';        /* in the class name */
748         }
749
750         topclass = loader_load ( utf_new_char (cp) );
751
752         if (exceptionptr != 0)
753         {
754                 printf ("#### Class loader has thrown: ");
755                 utf_display (exceptionptr->vftbl->class->name);
756                 printf ("\n");
757
758                 exceptionptr = 0;
759         }
760
761         if (topclass == 0)
762         {
763                 printf("#### Could not find top class - exiting\n");
764                 exit(1);
765         }
766
767         gc_init();
768
769 #ifdef USE_THREADS
770         initThreads((u1*)&dummy);                   /* schani */
771 #endif
772
773         /************************* Start worker routines ********************/
774
775         if (startit) {
776                 methodinfo *mainmethod;
777                 java_objectarray *a; 
778
779                 heap_addreference((void**) &a);
780
781                 mainmethod = class_findmethod (
782                                                                            topclass,
783                                                                            utf_new_char ("main"), 
784                                                                            utf_new_char ("([Ljava/lang/String;)V")
785                                                                            );
786                 if (!mainmethod) panic ("Can not find method 'void main(String[])'");
787                 if ((mainmethod->flags & ACC_STATIC) != ACC_STATIC) panic ("main is not static!");
788                         
789                 a = builtin_anewarray (argc - opt_ind, class_java_lang_String);
790                 for (i=opt_ind; i<argc; i++) {
791                         a->data[i-opt_ind] = javastring_new (utf_new_char (argv[i]) );
792                 }
793                 local_exceptionptr = asm_calljavamethod (mainmethod, a, NULL,NULL,NULL );
794         
795                 if (local_exceptionptr) {
796                         printf ("#### Program has thrown: ");
797                         utf_display (local_exceptionptr->vftbl->class->name);
798                         printf ("\n");
799                 }
800                                         /*RTAprint*/ if ((pCallgraph >= 1) && (opt_rt)) {
801                                         /*RTAprint*/    printCallgraph (); }
802
803                                         /*RTprint*/ if ((pClassHeir >= 1) && (opt_rt)) {
804                                         /*RTprint*/     printf("Last RTA Class Heirarchy -");
805                                         /*RTprint*/     printRThierarchyInfo(mainmethod); }
806
807 #ifdef USE_THREADS
808                 killThread(currentThread);
809 #endif
810                 fprintf(stderr, "still here\n");
811         }
812
813         /************* If requested, compile all methods ********************/
814
815         if (compileall) {
816                 class_compile_methods();
817         }
818
819
820         /******** If requested, compile a specific method ***************/
821
822         if (specificmethodname) {
823                 methodinfo *m;
824                 if (specificsignature)
825                         m = class_findmethod(topclass, 
826                                                                  utf_new_char(specificmethodname),
827                                                                  utf_new_char(specificsignature));
828                 else
829                         m = class_findmethod(topclass, 
830                                                                  utf_new_char(specificmethodname), NULL);
831                 if (!m) panic ("Specific method not found");
832 #ifdef OLD_COMPILER
833                 if (newcompiler)
834 #endif
835                         (void) jit_compile(m);
836 #ifdef OLD_COMPILER
837                 else
838                         (void) compiler_compile(m);
839 #endif
840         }
841
842         exit(0);
843 }
844
845
846
847 /************************************ Shutdown function *********************************
848
849         Terminates the system immediately without freeing memory explicitly (to be
850         used only for abnormal termination)
851         
852 *****************************************************************************************/
853
854 void cacao_shutdown(s4 status)
855 {
856                                         /*RTAprint*/ if ((pCallgraph >= 1) && (opt_rt)) {
857                                         /*RTAprint*/    printCallgraph (NULL); }
858
859                                         /*RTprint*/ if ((pClassHeir >= 1) && (opt_rt)) {
860                                         /*RTprint*/     printf("Last RTA Class Heirarchy -");
861                                         /*RTprint*/     printRThierarchyInfo(NULL); }
862
863         if (verbose || getcompilingtime || statistics) {
864                 log_text ("CACAO terminated by shutdown");
865                 if (statistics)
866                         print_stats ();
867                 if (getcompilingtime)
868                         print_times ();
869                 mem_usagelog(0);
870                 sprintf (logtext, "Exit status: %d\n", (int) status);
871                 dolog();
872                 }
873
874         exit(status);
875 }
876
877
878 /*
879  * These are local overrides for various environment variables in Emacs.
880  * Please do not remove this and leave it at the end of the file, where
881  * Emacs will automagically detect them.
882  * ---------------------------------------------------------------------
883  * Local variables:
884  * mode: c
885  * indent-tabs-mode: t
886  * c-basic-offset: 4
887  * tab-width: 4
888  * End:
889  */