Threads now close down even if main thread
[cacao.git] / main.c
1 /* main.c **********************************************************************
2
3         Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
4
5         See file COPYRIGHT for information on usage and disclaimer of warranties
6
7         Enthaelt die Funktion main() und die Variablen fuer die 
8         globalen Optionen.
9         Dieser Modul erledigt folgende Aufgaben:
10            - Bearbeiten der command-line-options
11            - Aufrufen aller Initialisierungsroutinen
12            - Aufrufen des Classloaders
13            - Starten der main - Methode
14
15         Authors: Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at
16         Changes: Andi Krall          EMAIL: cacao@complang.tuwien.ac.at
17                  Mark Probst         EMAIL: cacao@complang.tuwien.ac.at
18
19         Last Change: 1997/10/29
20
21 *******************************************************************************/
22
23 #include "global.h"
24
25 #include "tables.h"
26 #include "loader.h"
27 #include "jit.h"
28 #ifdef OLD_COMPILER
29 #include "compiler.h"
30 #endif
31
32 #include "asmpart.h"
33 #include "builtin.h"
34 #include "native.h"
35
36 #include "threads/thread.h"
37
38 bool compileall = false;
39 int  newcompiler = true;                
40 bool verbose =  false;
41 #ifdef NEW_GC
42 bool new_gc = false;
43 #endif
44
45 static bool showmethods = false;
46 static bool showconstantpool = false;
47 static bool showunicode = false;
48 static classinfo *topclass;
49
50 #ifndef USE_THREADS
51 void **stackbottom = 0;
52 #endif
53
54
55 /* internal function: get_opt *************************************************
56         
57         decodes the next command line option
58         
59 ******************************************************************************/
60
61 #define OPT_DONE  -1
62 #define OPT_ERROR  0
63 #define OPT_IGNORE 1
64
65 #define OPT_CLASSPATH   2
66 #define OPT_D           3
67 #define OPT_MS          4
68 #define OPT_MX          5
69 #define OPT_VERBOSE1    6
70 #define OPT_VERBOSE     7
71 #define OPT_VERBOSEGC   8
72 #define OPT_VERBOSECALL 9
73 #define OPT_IEEE        10
74 #define OPT_SOFTNULL    11
75 #define OPT_TIME        12
76 #define OPT_STAT        13
77 #define OPT_LOG         14
78 #define OPT_CHECK       15
79 #define OPT_LOAD        16
80 #define OPT_METHOD      17
81 #define OPT_SIGNATURE   18
82 #define OPT_SHOW        19
83 #define OPT_ALL         20
84 #ifdef OLD_COMPILER
85 #define OPT_OLD         21
86 #endif
87 #ifdef NEW_GC
88 #define OPT_GC1         22
89 #define OPT_GC2         23
90 #endif
91
92 struct {char *name; bool arg; int value;} opts[] = {
93         {"classpath",   true,   OPT_CLASSPATH},
94         {"D",           true,   OPT_D},
95         {"ms",          true,   OPT_MS},
96         {"mx",          true,   OPT_MX},
97         {"noasyncgc",   false,  OPT_IGNORE},
98         {"noverify",    false,  OPT_IGNORE},
99         {"oss",         true,   OPT_IGNORE},
100         {"ss",          true,   OPT_IGNORE},
101         {"v",           false,  OPT_VERBOSE1},
102         {"verbose",     false,  OPT_VERBOSE},
103         {"verbosegc",   false,  OPT_VERBOSEGC},
104         {"verbosecall", false,  OPT_VERBOSECALL},
105         {"ieee",        false,  OPT_IEEE},
106         {"softnull",    false,  OPT_SOFTNULL},
107         {"time",        false,  OPT_TIME},
108         {"stat",        false,  OPT_STAT},
109         {"log",         true,   OPT_LOG},
110         {"c",           true,   OPT_CHECK},
111         {"l",           false,  OPT_LOAD},
112         {"m",           true,   OPT_METHOD},
113         {"sig",         true,   OPT_SIGNATURE},
114         {"s",           true,   OPT_SHOW},
115         {"all",         false,  OPT_ALL},
116 #ifdef OLD_COMPILER
117         {"old",         false,  OPT_OLD},
118 #endif
119 #ifdef NEW_GC
120         {"gc1",         false,  OPT_GC1},
121         {"gc2",         false,  OPT_GC2},
122 #endif
123         {NULL,  false, 0}
124 };
125
126 static int opt_ind = 1;
127 static char *opt_arg;
128
129 static int get_opt (int argc, char **argv) 
130 {
131         char *a;
132         int i;
133         
134         if (opt_ind >= argc) return OPT_DONE;
135         
136         a = argv[opt_ind];
137         if (a[0] != '-') return OPT_DONE;
138
139         for (i=0; opts[i].name; i++) {
140                 if (! opts[i].arg) {
141                         if (strcmp(a+1, opts[i].name) == 0) {  /* boolean option found */
142                                 opt_ind++;
143                                 return opts[i].value;
144                         }
145                 }
146                 else {
147                         if (strcmp(a+1, opts[i].name) == 0) { /* parameter option found */
148                                 opt_ind++;
149                                 if (opt_ind < argc) {
150                                         opt_arg = argv[opt_ind];
151                                         opt_ind++;
152                                         return opts[i].value;
153                                 }
154                                 return OPT_ERROR;
155                         }
156                         else {
157                                 size_t l = strlen(opts[i].name);
158                                 if (strlen(a+1) > l) {
159                                         if (memcmp (a+1, opts[i].name, l)==0) {
160                                                 opt_ind++;
161                                                 opt_arg = a+1+l;
162                                                 return opts[i].value;
163                                         }
164                                 }
165                         }
166                 }
167         } /* end for */ 
168
169         return OPT_ERROR;
170 }
171
172
173
174
175 /******************** interne Funktion: print_usage ************************
176
177 Gibt die richtige Aufrufsyntax des JavaVM-Compilers auf stdout aus.
178
179 ***************************************************************************/
180
181 static void print_usage()
182 {
183         printf ("USAGE: cacao [options] classname [program arguments\n");
184         printf ("Options:\n");
185         printf ("          -classpath path ...... specify a path to look for classes\n");
186         printf ("          -Dpropertyname=value . add an entry to the property list\n");
187         printf ("          -mx maxmem[k|m] ...... specify the size for the heap\n");
188         printf ("          -ms initmem[k|m] ..... specify the initial size for the heap\n");
189         printf ("          -v ................... write state-information\n");
190         printf ("          -verbose ............. write more information\n");
191         printf ("          -verbosegc ........... write message for each GC\n");
192         printf ("          -verbosecall ......... write message for each call\n");
193         printf ("          -ieee ................ use ieee compliant arithmetic\n");
194         printf ("          -softnull ............ use software nullpointer check\n");
195         printf ("          -time ................ measure the runtime\n");
196         printf ("          -stat ................ detailed compiler statistics\n");
197         printf ("          -log logfile ......... specify a name for the logfile\n");
198         printf ("          -c(heck) b(ounds...... don't check array bounds\n");
199         printf ("                   s(ync) ...... don't check for synchronization\n");
200         printf ("          -l ................... don't start the class after loading\n");
201         printf ("          -all ................. compile all methods, no execution\n");
202 #ifdef OLD_COMPILER
203         printf ("          -old ................. use old JIT compiler\n");
204 #endif
205 #ifdef NEW_GC
206         printf ("          -gc1 ................. use the old garbage collector (default)\n");
207         printf ("          -gc2 ................. use the new garbage collector\n");
208 #endif
209         printf ("          -m ................... compile only a specific method\n");
210         printf ("          -sig ................. specify signature for a specific method\n");
211         printf ("          -s(how)m(ethods) ..... show all methods&fields of a class\n");
212         printf ("                 a(ssembler) ... show disassembled listing\n");
213         printf ("                 c(onstants) ... show the constant pool\n");
214         printf ("                 d(atasegment).. show data segment listing\n");
215         printf ("                 i(ntermediate). show intermediate representation\n");
216         printf ("                 m(ethods)...... show class fields and methods\n");
217 #ifdef OLD_COMPILER
218         printf ("                 s(tack) ....... show stack for every javaVM-command\n");
219 #endif
220         printf ("                 u(nicode) ..... show the unicode - hash\n");
221 }   
222
223
224
225 /***************************** Funktion: print_times *********************
226
227         gibt eine Aufstellung der verwendeten CPU-Zeit aus
228
229 **************************************************************************/
230
231 static void print_times()
232 {
233         long int totaltime = getcputime();
234         long int runtime = totaltime - loadingtime - compilingtime;
235
236         sprintf (logtext, "Time for loading classes: %ld secs, %ld millis",
237              loadingtime / 1000000, (loadingtime % 1000000) / 1000);
238         dolog();
239         sprintf (logtext, "Time for compiling code:  %ld secs, %ld millis",
240              compilingtime / 1000000, (compilingtime % 1000000) / 1000);
241         dolog();
242         sprintf (logtext, "Time for running program: %ld secs, %ld millis",
243              runtime / 1000000, (runtime % 1000000) / 1000);
244         dolog();
245         sprintf (logtext, "Total time: %ld secs, %ld millis",
246              totaltime / 1000000, (totaltime % 1000000) / 1000);
247         dolog();
248 }
249
250
251
252
253
254
255 /***************************** Funktion: print_stats *********************
256
257         outputs detailed compiler statistics
258
259 **************************************************************************/
260
261 static void print_stats()
262 {
263         sprintf (logtext, "Number of JitCompiler Calls: %d", count_jit_calls);
264         dolog();
265         sprintf (logtext, "Number of compiled Methods: %d", count_methods);
266         dolog();
267         sprintf (logtext, "Number of max basic blocks per method: %d", count_max_basic_blocks);
268         dolog();
269         sprintf (logtext, "Number of compiled basic blocks: %d", count_basic_blocks);
270         dolog();
271         sprintf (logtext, "Number of max JavaVM-Instructions per method: %d", count_max_javainstr);
272         dolog();
273         sprintf (logtext, "Number of compiled JavaVM-Instructions: %d", count_javainstr);
274         dolog();
275         sprintf (logtext, "Size of compiled JavaVM-Instructions:   %d(%d)", count_javacodesize,
276                                                       count_javacodesize - count_methods * 18);
277         dolog();
278         sprintf (logtext, "Size of compiled Exception Tables:      %d", count_javaexcsize);
279         dolog();
280         sprintf (logtext, "Value of extended instruction set var:  %d", has_ext_instr_set);
281         dolog();
282         sprintf (logtext, "Number of Alpha-Instructions: %d", count_code_len >> 2);
283         dolog();
284         sprintf (logtext, "Number of Spills: %d", count_spills);
285         dolog();
286         sprintf (logtext, "Number of Activ    Pseudocommands: %5d", count_pcmd_activ);
287         dolog();
288         sprintf (logtext, "Number of Drop     Pseudocommands: %5d", count_pcmd_drop);
289         dolog();
290         sprintf (logtext, "Number of Const    Pseudocommands: %5d (zero:%5d)", count_pcmd_load, count_pcmd_zero);
291         dolog();
292         sprintf (logtext, "Number of ConstAlu Pseudocommands: %5d (cmp: %5d, store:%5d)", count_pcmd_const_alu, count_pcmd_const_bra, count_pcmd_const_store);
293         dolog();
294         sprintf (logtext, "Number of Move     Pseudocommands: %5d", count_pcmd_move);
295         dolog();
296         sprintf (logtext, "Number of Load     Pseudocommands: %5d", count_load_instruction);
297         dolog();
298         sprintf (logtext, "Number of Store    Pseudocommands: %5d (combined: %5d)", count_pcmd_store, count_pcmd_store - count_pcmd_store_comb);
299         dolog();
300         sprintf (logtext, "Number of OP       Pseudocommands: %5d", count_pcmd_op);
301         dolog();
302         sprintf (logtext, "Number of DUP      Pseudocommands: %5d", count_dup_instruction);
303         dolog();
304         sprintf (logtext, "Number of Mem      Pseudocommands: %5d", count_pcmd_mem);
305         dolog();
306         sprintf (logtext, "Number of Method   Pseudocommands: %5d", count_pcmd_met);
307         dolog();
308         sprintf (logtext, "Number of Branch   Pseudocommands: %5d (rets:%5d, Xrets: %5d)",
309                           count_pcmd_bra, count_pcmd_return, count_pcmd_returnx);
310         dolog();
311         sprintf (logtext, "Number of Table    Pseudocommands: %5d", count_pcmd_table);
312         dolog();
313         sprintf (logtext, "Number of Useful   Pseudocommands: %5d", count_pcmd_table +
314                  count_pcmd_bra + count_pcmd_load + count_pcmd_mem + count_pcmd_op);
315         dolog();
316         sprintf (logtext, "Number of Null Pointer Checks:     %5d", count_check_null);
317         dolog();
318         sprintf (logtext, "Number of Array Bound Checks:      %5d", count_check_bound);
319         dolog();
320         sprintf (logtext, "Number of Try-Blocks: %d", count_tryblocks);
321         dolog();
322         sprintf (logtext, "Maximal count of stack elements:   %d", count_max_new_stack);
323         dolog();
324         sprintf (logtext, "Upper bound of max stack elements: %d", count_upper_bound_new_stack);
325         dolog();
326         sprintf (logtext, "Distribution of stack sizes at block boundary");
327         dolog();
328         sprintf (logtext, "    0    1    2    3    4    5    6    7    8    9    >=10");
329         dolog();
330         sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_block_stack[0],
331                 count_block_stack[1],count_block_stack[2],count_block_stack[3],count_block_stack[4],
332                 count_block_stack[5],count_block_stack[6],count_block_stack[7],count_block_stack[8],
333                 count_block_stack[9],count_block_stack[10]);
334         dolog();
335         sprintf (logtext, "Distribution of store stack depth");
336         dolog();
337         sprintf (logtext, "    0    1    2    3    4    5    6    7    8    9    >=10");
338         dolog();
339         sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_depth[0],
340                 count_store_depth[1],count_store_depth[2],count_store_depth[3],count_store_depth[4],
341                 count_store_depth[5],count_store_depth[6],count_store_depth[7],count_store_depth[8],
342                 count_store_depth[9],count_store_depth[10]);
343         dolog();
344         sprintf (logtext, "Distribution of store creator chains first part");
345         dolog();
346         sprintf (logtext, "    0    1    2    3    4    5    6    7    8    9  ");
347         dolog();
348         sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[0],
349                 count_store_length[1],count_store_length[2],count_store_length[3],count_store_length[4],
350                 count_store_length[5],count_store_length[6],count_store_length[7],count_store_length[8],
351                 count_store_length[9]);
352         dolog();
353         sprintf (logtext, "Distribution of store creator chains second part");
354         dolog();
355         sprintf (logtext, "   10   11   12   13   14   15   16   17   18   19  >=20");
356         dolog();
357         sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[10],
358                 count_store_length[11],count_store_length[12],count_store_length[13],count_store_length[14],
359                 count_store_length[15],count_store_length[16],count_store_length[17],count_store_length[18],
360                 count_store_length[19],count_store_length[20]);
361         dolog();
362         sprintf (logtext, "Distribution of analysis iterations");
363         dolog();
364         sprintf (logtext, "    1    2    3    4    >=5");
365         dolog();
366         sprintf (logtext, "%5d%5d%5d%5d%5d", count_analyse_iterations[0],count_analyse_iterations[1],
367                 count_analyse_iterations[2],count_analyse_iterations[3],count_analyse_iterations[4]);
368         dolog();
369         sprintf (logtext, "Distribution of basic blocks per method");
370         dolog();
371         sprintf (logtext, " <= 5 <=10 <=15 <=20 <=30 <=40 <=50 <=75  >75");
372         dolog();
373         sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_method_bb_distribution[0],
374                 count_method_bb_distribution[1],count_method_bb_distribution[2],count_method_bb_distribution[3],
375                 count_method_bb_distribution[4],count_method_bb_distribution[5],count_method_bb_distribution[6],
376                 count_method_bb_distribution[7],count_method_bb_distribution[8]);
377         dolog();
378         sprintf (logtext, "Distribution of basic block sizes");
379         dolog();
380         sprintf (logtext,
381         "  1    2    3    4   5   6   7   8   9  10 <13 <15 <17 <19 <21 <26 <31 >30");
382         dolog();
383         sprintf (logtext, "%3d%5d%5d%5d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d",
384                 count_block_size_distribution[0], count_block_size_distribution[1], count_block_size_distribution[2],
385                 count_block_size_distribution[3], count_block_size_distribution[4], count_block_size_distribution[5],
386                 count_block_size_distribution[6], count_block_size_distribution[7], count_block_size_distribution[8],
387                 count_block_size_distribution[9], count_block_size_distribution[10],count_block_size_distribution[11],
388                 count_block_size_distribution[12],count_block_size_distribution[13],count_block_size_distribution[14],
389                 count_block_size_distribution[15],count_block_size_distribution[16],count_block_size_distribution[17]);
390         dolog();
391         sprintf (logtext, "Size of Code Area (Kb):  %10.3f", (float) count_code_len / 1024);
392         dolog();
393         sprintf (logtext, "Size of data Area (Kb):  %10.3f", (float) count_data_len / 1024);
394         dolog();
395         sprintf (logtext, "Size of Class Infos (Kb):%10.3f", (float) (count_class_infos) / 1024);
396         dolog();
397         sprintf (logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_unicode_len) / 1024);
398         dolog();
399         sprintf (logtext, "Size of Vftbl (Kb):      %10.3f", (float) count_vftbl_len / 1024);
400         dolog();
401         sprintf (logtext, "Size of comp stub (Kb):  %10.3f", (float) count_cstub_len / 1024);
402         dolog();
403         sprintf (logtext, "Size of native stub (Kb):%10.3f", (float) count_nstub_len / 1024);
404         dolog();
405         sprintf (logtext, "Size of Unicode (Kb):    %10.3f", (float) count_unicode_len / 1024);
406         dolog();
407         sprintf (logtext, "Size of VMCode (Kb):     %10.3f(%d)", (float) count_vmcode_len / 1024,
408                                                       count_vmcode_len - 18 * count_all_methods);
409         dolog();
410         sprintf (logtext, "Size of ExTable (Kb):    %10.3f", (float) count_extable_len / 1024);
411         dolog();
412         sprintf (logtext, "Number of loaded Methods: %d\n\n", count_all_methods);
413         dolog();
414 }
415
416
417 /********** Funktion: class_compile_methods   (nur f"ur Debug-Zwecke) ********/
418
419 void class_compile_methods ()
420 {
421         int        i;
422         classinfo  *c;
423         methodinfo *m;
424         
425         c = list_first (&linkedclasses);
426         while (c) {
427                 for (i = 0; i < c -> methodscount; i++) {
428                         m = &(c->methods[i]);
429                         if (m->jcode) {
430 #ifdef OLD_COMPILER
431                                 if (newcompiler)
432 #endif
433                                         (void) jit_compile(m);
434 #ifdef OLD_COMPILER
435                                 else
436                                         (void) compiler_compile(m);
437 #endif
438                                 }
439                         }
440                 c = list_next (&linkedclasses, c);
441                 }
442 }
443
444 /*
445  * void exit_handler(void)
446  * -----------------------
447  * The exit_handler function is called upon program termination to shutdown
448  * the various subsystems and release the resources allocated to the VM.
449  */
450
451 void exit_handler(void)
452 {
453         /********************* Debug-Tabellen ausgeben ************************/
454                                 
455         if (showmethods) class_showmethods (topclass);
456         if (showconstantpool)  class_showconstantpool (topclass);
457         if (showunicode)       unicode_show ();
458
459 #ifdef USE_THREADS
460         clear_thread_flags();           /* restores standard file descriptor
461                                                                    flags */
462 #endif
463
464         /************************ Freigeben aller Resourcen *******************/
465
466         heap_close ();                          /* must be called before compiler_close and
467                                                                    loader_close because finalization occurs
468                                                                    here */
469
470 #ifdef OLD_COMPILER
471         compiler_close ();
472 #endif
473         loader_close ();
474         unicode_close ( literalstring_free );
475
476
477         if (verbose || getcompilingtime || statistics) {
478                 log_text ("CACAO terminated");
479                 if (statistics)
480                         print_stats ();
481                 if (getcompilingtime)
482                         print_times ();
483                 mem_usagelog(1);
484         }
485 }
486
487 /************************** Funktion: main *******************************
488
489    Das Hauptprogramm.
490    Wird vom System zu Programstart aufgerufen (eh klar).
491    
492 **************************************************************************/
493
494 int main(int argc, char **argv)
495 {
496         s4 i,j;
497         char *cp;
498         java_objectheader *exceptionptr;
499         void *dummy;
500         
501         /********** interne (nur fuer main relevante Optionen) **************/
502    
503         char logfilename[200] = "";
504         u4 heapsize = 16000000;
505         u4 heapstartsize = 200000;
506         char classpath[500] = ".:/usr/local/lib/java/classes";
507         bool startit = true;
508         char *specificmethodname = NULL;
509         char *specificsignature = NULL;
510
511 #ifndef USE_THREADS
512         stackbottom = &dummy;
513 #endif
514
515         if (0 != atexit(exit_handler))
516                 panic("unable to register exit_handler");
517
518         /************ Infos aus der Environment lesen ************************/
519
520         cp = getenv ("CLASSPATH");
521         if (cp) {
522                 strcpy (classpath, cp);
523         }
524
525         /***************** Interpretieren der Kommandozeile *****************/
526    
527         checknull = false;
528         checkfloats = false;
529
530         while ( (i = get_opt(argc,argv)) != OPT_DONE) {
531
532                 switch (i) {
533                 case OPT_IGNORE: break;
534                         
535                 case OPT_CLASSPATH:    
536                         strcpy (classpath + strlen(classpath), ":");
537                         strcpy (classpath + strlen(classpath), opt_arg);
538                         break;
539                                 
540                 case OPT_D:
541                         {
542                                 int n,l=strlen(opt_arg);
543                                 for (n=0; n<l; n++) {
544                                         if (opt_arg[n]=='=') {
545                                                 opt_arg[n] = '\0';
546                                                 attach_property (opt_arg, opt_arg+n+1);
547                                                 goto didit;
548                                         }
549                                 }
550                                 print_usage();
551                                 exit(10);
552                                         
553                         didit: ;
554                         }       
555                 break;
556                                 
557                 case OPT_MS:
558                 case OPT_MX:
559                         if (opt_arg[strlen(opt_arg)-1] == 'k') {
560                                 j = 1024 * atoi(opt_arg);
561                         }
562                         else if (opt_arg[strlen(opt_arg)-1] == 'm') {
563                                 j = 1024 * 1024 * atoi(opt_arg);
564                         }
565                         else j = atoi(opt_arg);
566                                 
567                         if (i==OPT_MX) heapsize = j;
568                         else heapstartsize = j;
569                         break;
570
571                 case OPT_VERBOSE1:
572                         verbose = true;
573                         break;
574                                                                 
575                 case OPT_VERBOSE:
576                         verbose = true;
577                         loadverbose = true;
578                         initverbose = true;
579                         compileverbose = true;
580                         break;
581                                 
582                 case OPT_VERBOSEGC:
583                         collectverbose = true;
584                         break;
585                                 
586                 case OPT_VERBOSECALL:
587                         runverbose = true;
588                         break;
589                                 
590                 case OPT_IEEE:
591                         checkfloats = true;
592                         break;
593
594                 case OPT_SOFTNULL:
595                         checknull = true;
596                         break;
597
598                 case OPT_TIME:
599                         getcompilingtime = true;
600                         getloadingtime = true;
601                         break;
602                                         
603                 case OPT_STAT:
604                         statistics = true;
605                         break;
606                                         
607                 case OPT_LOG:
608                         strcpy (logfilename, opt_arg);
609                         break;
610                         
611                         
612                 case OPT_CHECK:
613                         for (j=0; j<strlen(opt_arg); j++) {
614                                 switch (opt_arg[j]) {
615                                 case 'b': checkbounds=false; break;
616                                 case 's': checksync=false; break;
617                                 default:  print_usage();
618                                         exit(10);
619                                 }
620                         }
621                         break;
622                         
623                 case OPT_LOAD:
624                         startit = false;
625                         makeinitializations = false;
626                         break;
627
628                 case OPT_METHOD:
629                         startit = false;
630                         specificmethodname = opt_arg;                   
631                         makeinitializations = false;
632                         break;
633                         
634                 case OPT_SIGNATURE:
635                         specificsignature = opt_arg;                    
636                         break;
637                         
638                 case OPT_ALL:
639                         compileall = true;              
640                         startit = false;
641                         makeinitializations = false;
642                         break;
643                         
644 #ifdef OLD_COMPILER
645                 case OPT_OLD:
646                         newcompiler = false;                    
647                         checknull = true;
648                         break;
649 #endif
650
651 #ifdef NEW_GC
652                 case OPT_GC2:
653                         new_gc = true;
654                         break;
655
656                 case OPT_GC1:
657                         new_gc = false;
658                         break;
659 #endif
660                         
661                 case OPT_SHOW:       /* Anzeigeoptionen */
662                         for (j=0; j<strlen(opt_arg); j++) {             
663                                 switch (opt_arg[j]) {
664                                 case 'a':  showdisassemble=true; compileverbose=true; break;
665                                 case 'c':  showconstantpool=true; break;
666                                 case 'd':  showddatasegment=true; break;
667                                 case 'i':  showintermediate=true; compileverbose=true; break;
668                                 case 'm':  showmethods=true; break;
669 #ifdef OLD_COMPILER
670                                 case 's':  showstack=true; compileverbose=true; break;
671 #endif
672                                 case 'u':  showunicode=true; break;
673                                 default:   print_usage();
674                                         exit(10);
675                                 }
676                         }
677                         break;
678                         
679                 default:
680                         print_usage();
681                         exit(10);
682                 }
683                         
684                         
685         }
686    
687    
688         if (opt_ind >= argc) {
689                 print_usage ();
690                 exit(10);
691         }
692
693
694         /**************************** Programmstart *****************************/
695
696         log_init (logfilename);
697         if (verbose) {
698                 log_text (
699                                   "CACAO started -------------------------------------------------------");
700         }
701         
702         suck_init (classpath);
703         native_setclasspath (classpath);
704                 
705         unicode_init();
706         heap_init(heapsize, heapstartsize, &dummy);
707         loader_init();
708 #ifdef OLD_COMPILER
709         compiler_init();
710 #endif
711         jit_init();
712
713         native_loadclasses ();
714
715
716         /*********************** JAVA-Klassen laden  ***************************/
717    
718         cp = argv[opt_ind++];
719         for (i=strlen(cp)-1; i>=0; i--) {     /* Punkte im Klassennamen */
720                 if (cp[i]=='.') cp[i]='/';        /* auf slashes umbauen */
721         }
722
723         topclass = loader_load ( unicode_new_char (cp) );
724
725         loader_compute_subclasses();
726
727         gc_init();
728
729 #ifdef USE_THREADS
730         initThreads((u1*)&dummy);                   /* schani */
731 #endif
732
733         /************************* Arbeitsroutinen starten ********************/
734
735         if (startit) {
736                 methodinfo *mainmethod;
737                 java_objectarray *a; 
738
739                 heap_addreference((void**) &a);
740
741                 mainmethod = class_findmethod (
742                                                                            topclass,
743                                                                            unicode_new_char ("main"), 
744                                                                            unicode_new_char ("([Ljava/lang/String;)V")
745                                                                            );
746                 if (!mainmethod) panic ("Can not find method 'void main(String[])'");
747                 if ((mainmethod->flags & ACC_STATIC) != ACC_STATIC) panic ("main is not static!");
748                         
749                 a = builtin_anewarray (argc - opt_ind, class_java_lang_String);
750                 for (i=opt_ind; i<argc; i++) {
751                         a->data[i-opt_ind] = javastring_new (unicode_new_char (argv[i]) );
752                 }
753                 exceptionptr = asm_calljavamethod (mainmethod, a, NULL,NULL,NULL );
754         
755                 if (exceptionptr) {
756                         printf ("#### Program has thrown: ");
757                         unicode_display (exceptionptr->vftbl->class->name);
758                         printf ("\n");
759                 }
760
761                 killThread(currentThread);
762                 fprintf(stderr, "still here\n");
763         }
764
765         /************* Auf Wunsch alle Methode "ubersetzen ********************/
766
767         if (compileall) {
768                 class_compile_methods();
769         }
770
771
772         /******** Auf Wunsch eine spezielle Methode "ubersetzen ***************/
773
774         if (specificmethodname) {
775                 methodinfo *m;
776                 if (specificsignature)
777                         m = class_findmethod(topclass, 
778                                                                  unicode_new_char(specificmethodname),
779                                                                  unicode_new_char(specificsignature));
780                 else
781                         m = class_findmethod(topclass, 
782                                                                  unicode_new_char(specificmethodname), NULL);
783                 if (!m) panic ("Specific method not found");
784 #ifdef OLD_COMPILER
785                 if (newcompiler)
786 #endif
787                         (void) jit_compile(m);
788 #ifdef OLD_COMPILER
789                 else
790                         (void) compiler_compile(m);
791 #endif
792         }
793
794         exit(0);
795 }
796
797
798
799 /************************************ SHUTDOWN-Funktion *********************************
800
801         Terminiert das System augenblicklich, ohne den Speicher
802         explizit freizugeben (eigentlich nur f"ur abnorme 
803         Programmterminierung)
804         
805 *****************************************************************************************/
806
807 void cacao_shutdown(s4 status)
808 {
809         if (verbose || getcompilingtime || statistics) {
810                 log_text ("CACAO terminated by shutdown");
811                 if (statistics)
812                         print_stats ();
813                 if (getcompilingtime)
814                         print_times ();
815                 mem_usagelog(0);
816                 sprintf (logtext, "Exit status: %d\n", (int) status);
817                 dolog();
818                 }
819
820         exit(status);
821 }
822
823
824 /*
825  * These are local overrides for various environment variables in Emacs.
826  * Please do not remove this and leave it at the end of the file, where
827  * Emacs will automagically detect them.
828  * ---------------------------------------------------------------------
829  * Local variables:
830  * mode: c
831  * indent-tabs-mode: t
832  * c-basic-offset: 4
833  * tab-width: 4
834  * End:
835  */