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