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