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