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