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