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