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