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