1 /******************************* main.c ****************************************
3 Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
5 See file COPYRIGHT for information on usage and disclaimer of warranties
7 Enthaelt die Funktion main() und die Variablen fuer die
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
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
19 Last Change: 1997/10/29
21 *******************************************************************************/
27 #include "ncomp/ncomp.h"
34 #include "threads/thread.h" /* schani */
36 bool compileall = false;
37 int newcompiler = true;
41 void **stackbottom = 0;
45 /********************* interne Funktion: get_opt *****************************
47 liest die n"achste Option aus der Kommandozeile
49 ******************************************************************************/
55 #define OPT_CLASSPATH 2
59 #define OPT_VERBOSE1 6
61 #define OPT_VERBOSEGC 8
62 #define OPT_VERBOSECALL 9
64 #define OPT_SOFTNULL 11
71 #define OPT_SIGNATURE 18
76 struct { char *name; bool arg; int value; } opts[] = {
77 { "classpath", true, OPT_CLASSPATH },
79 { "ms", true, OPT_MS },
80 { "mx", true, OPT_MX },
81 { "noasyncgc", false, OPT_IGNORE },
82 { "noverify", false, OPT_IGNORE },
83 { "oss", true, OPT_IGNORE },
84 { "ss", true, OPT_IGNORE },
85 { "v", false, OPT_VERBOSE1 },
86 { "verbose", false, OPT_VERBOSE },
87 { "verbosegc", false, OPT_VERBOSEGC },
88 { "verbosecall", false, OPT_VERBOSECALL },
89 { "ieee", false, OPT_IEEE },
90 { "softnull", false, OPT_SOFTNULL },
91 { "time", false, OPT_TIME },
92 { "stat", false, OPT_STAT },
93 { "log", true, OPT_LOG },
94 { "c", true, OPT_CHECK },
95 { "l", false, OPT_LOAD },
96 { "m", true, OPT_METHOD },
97 { "sig", true, OPT_SIGNATURE },
98 { "s", true, OPT_SHOW },
99 { "all", false, OPT_ALL },
100 { "old", false, OPT_OLD },
104 static int opt_ind = 1;
105 static char *opt_arg;
107 static int get_opt (int argc, char **argv)
112 if (opt_ind >= argc) return OPT_DONE;
115 if (a[0] != '-') return OPT_DONE;
117 for (i=0; opts[i].name; i++) {
119 if (strcmp(a+1, opts[i].name) == 0) { /* boolean option found */
121 return opts[i].value;
125 if (strcmp(a+1, opts[i].name) == 0) { /* parameter option found */
127 if (opt_ind < argc) {
128 opt_arg = argv[opt_ind];
130 return opts[i].value;
135 size_t l = strlen(opts[i].name);
136 if (strlen(a+1) > l) {
137 if (memcmp (a+1, opts[i].name, l)==0) {
140 return opts[i].value;
153 /******************** interne Funktion: print_usage ************************
155 Gibt die richtige Aufrufsyntax des JavaVM-Compilers auf stdout aus.
157 ***************************************************************************/
159 static void print_usage()
161 printf ("USAGE: cacao [options] classname [program arguments\n");
162 printf ("Options:\n");
163 printf (" -classpath path ...... specify a path to look for classes\n");
164 printf (" -Dpropertyname=value . add an entry to the property list\n");
165 printf (" -mx maxmem[k|m] ...... specify the size for the heap\n");
166 printf (" -ms initmem[k|m] ..... specify the initial size for the heap\n");
167 printf (" -v ................... write state-information\n");
168 printf (" -verbose ............. write more information\n");
169 printf (" -verbosegc ........... write message for each GC\n");
170 printf (" -verbosecall ......... write message for each call\n");
171 printf (" -ieee ................ use ieee compliant arithmetic\n");
172 printf (" -softnull ............ use software nullpointer check\n");
173 printf (" -time ................ measure the runtime\n");
174 printf (" -stat ................ detailed compiler statistics\n");
175 printf (" -log logfile ......... specify a name for the logfile\n");
176 printf (" -c(heck) b(ounds...... don't check array bounds\n");
177 printf (" s(ync) ...... don't check for synchronization\n");
178 printf (" -l ................... don't start the class after loading\n");
179 printf (" -all ................. compile all methods, no execution\n");
180 printf (" -old ................. use old JIT compiler\n");
181 printf (" -m ................... compile only a specific method\n");
182 printf (" -sig ................. specify signature for a specific method\n");
183 printf (" -s(how)m(ethods) ..... show all methods&fields of a class\n");
184 printf (" c(onstants) ... show the constant pool\n");
185 printf (" a(ssembler) ... show disassembled listing\n");
186 printf (" s(tack) ....... show stack for every javaVM-command\n");
187 printf (" i(ntermediate). show intermediate representation\n");
188 printf (" u(nicode) ..... show the unicode - hash\n");
193 /***************************** Funktion: print_times *********************
195 gibt eine Aufstellung der verwendeten CPU-Zeit aus
197 **************************************************************************/
199 static void print_times()
201 long int totaltime = getcputime();
202 long int runtime = totaltime - loadingtime - compilingtime;
204 sprintf (logtext, "Time for loading classes: %ld secs, %ld millis",
205 loadingtime / 1000000, (loadingtime % 1000000) / 1000);
207 sprintf (logtext, "Time for compiling code: %ld secs, %ld millis",
208 compilingtime / 1000000, (compilingtime % 1000000) / 1000);
210 sprintf (logtext, "Time for running program: %ld secs, %ld millis",
211 runtime / 1000000, (runtime % 1000000) / 1000);
213 sprintf (logtext, "Total time: %ld secs, %ld millis",
214 totaltime / 1000000, (totaltime % 1000000) / 1000);
223 /***************************** Funktion: print_stats *********************
225 outputs detailed compiler statistics
227 **************************************************************************/
229 static void print_stats()
231 sprintf (logtext, "Number of JitCompiler Calls: %d", count_jit_calls);
233 sprintf (logtext, "Number of compiled Methods: %d", count_methods);
235 sprintf (logtext, "Number of max basic blocks per method: %d", count_max_basic_blocks);
237 sprintf (logtext, "Number of compiled basic blocks: %d", count_basic_blocks);
239 sprintf (logtext, "Number of max JavaVM-Instructions per method: %d", count_max_javainstr);
241 sprintf (logtext, "Number of compiled JavaVM-Instructions: %d", count_javainstr);
243 sprintf (logtext, "Size of compiled JavaVM-Instructions: %d(%d)", count_javacodesize,
244 count_javacodesize - count_methods * 18);
246 sprintf (logtext, "Size of compiled Exception Tables: %d", count_javaexcsize);
248 sprintf (logtext, "Value of extended instruction set var: %d", has_ext_instr_set);
250 sprintf (logtext, "Number of Alpha-Instructions: %d", count_code_len >> 2);
252 sprintf (logtext, "Number of Spills: %d", count_spills);
254 sprintf (logtext, "Number of Activ Pseudocommands: %5d", count_pcmd_activ);
256 sprintf (logtext, "Number of Drop Pseudocommands: %5d", count_pcmd_drop);
258 sprintf (logtext, "Number of Const Pseudocommands: %5d (zero:%5d)", count_pcmd_load, count_pcmd_zero);
260 sprintf (logtext, "Number of ConstAlu Pseudocommands: %5d (cmp: %5d, store:%5d)", count_pcmd_const_alu, count_pcmd_const_bra, count_pcmd_const_store);
262 sprintf (logtext, "Number of Move Pseudocommands: %5d", count_pcmd_move);
264 sprintf (logtext, "Number of Load Pseudocommands: %5d", count_load_instruction);
266 sprintf (logtext, "Number of Store Pseudocommands: %5d (combined: %5d)", count_pcmd_store, count_pcmd_store - count_pcmd_store_comb);
268 sprintf (logtext, "Number of OP Pseudocommands: %5d", count_pcmd_op);
270 sprintf (logtext, "Number of DUP Pseudocommands: %5d", count_dup_instruction);
272 sprintf (logtext, "Number of Mem Pseudocommands: %5d", count_pcmd_mem);
274 sprintf (logtext, "Number of Method Pseudocommands: %5d", count_pcmd_met);
276 sprintf (logtext, "Number of Branch Pseudocommands: %5d (rets:%5d, Xrets: %5d)",
277 count_pcmd_bra, count_pcmd_return, count_pcmd_returnx);
279 sprintf (logtext, "Number of Table Pseudocommands: %5d", count_pcmd_table);
281 sprintf (logtext, "Number of Useful Pseudocommands: %5d", count_pcmd_table +
282 count_pcmd_bra + count_pcmd_load + count_pcmd_mem + count_pcmd_op);
284 sprintf (logtext, "Number of Null Pointer Checks: %5d", count_check_null);
286 sprintf (logtext, "Number of Array Bound Checks: %5d", count_check_bound);
288 sprintf (logtext, "Number of Try-Blocks: %d", count_tryblocks);
290 sprintf (logtext, "Maximal count of stack elements: %d", count_max_new_stack);
292 sprintf (logtext, "Upper bound of max stack elements: %d", count_upper_bound_new_stack);
294 sprintf (logtext, "Distribution of stack sizes at block boundary");
296 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
298 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_block_stack[0],
299 count_block_stack[1],count_block_stack[2],count_block_stack[3],count_block_stack[4],
300 count_block_stack[5],count_block_stack[6],count_block_stack[7],count_block_stack[8],
301 count_block_stack[9],count_block_stack[10]);
303 sprintf (logtext, "Distribution of store stack depth");
305 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
307 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_depth[0],
308 count_store_depth[1],count_store_depth[2],count_store_depth[3],count_store_depth[4],
309 count_store_depth[5],count_store_depth[6],count_store_depth[7],count_store_depth[8],
310 count_store_depth[9],count_store_depth[10]);
312 sprintf (logtext, "Distribution of store creator chains first part");
314 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 ");
316 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[0],
317 count_store_length[1],count_store_length[2],count_store_length[3],count_store_length[4],
318 count_store_length[5],count_store_length[6],count_store_length[7],count_store_length[8],
319 count_store_length[9]);
321 sprintf (logtext, "Distribution of store creator chains second part");
323 sprintf (logtext, " 10 11 12 13 14 15 16 17 18 19 >=20");
325 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[10],
326 count_store_length[11],count_store_length[12],count_store_length[13],count_store_length[14],
327 count_store_length[15],count_store_length[16],count_store_length[17],count_store_length[18],
328 count_store_length[19],count_store_length[20]);
330 sprintf (logtext, "Distribution of analysis iterations");
332 sprintf (logtext, " 1 2 3 4 >=5");
334 sprintf (logtext, "%5d%5d%5d%5d%5d", count_analyse_iterations[0],count_analyse_iterations[1],
335 count_analyse_iterations[2],count_analyse_iterations[3],count_analyse_iterations[4]);
337 sprintf (logtext, "Distribution of basic blocks per method");
339 sprintf (logtext, " <= 5 <=10 <=15 <=20 <=30 <=40 <=50 <=75 >75");
341 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_method_bb_distribution[0],
342 count_method_bb_distribution[1],count_method_bb_distribution[2],count_method_bb_distribution[3],
343 count_method_bb_distribution[4],count_method_bb_distribution[5],count_method_bb_distribution[6],
344 count_method_bb_distribution[7],count_method_bb_distribution[8]);
346 sprintf (logtext, "Distribution of basic block sizes");
349 " 1 2 3 4 5 6 7 8 9 10 <13 <15 <17 <19 <21 <26 <31 >30");
351 sprintf (logtext, "%3d%5d%5d%5d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d",
352 count_block_size_distribution[0], count_block_size_distribution[1], count_block_size_distribution[2],
353 count_block_size_distribution[3], count_block_size_distribution[4], count_block_size_distribution[5],
354 count_block_size_distribution[6], count_block_size_distribution[7], count_block_size_distribution[8],
355 count_block_size_distribution[9], count_block_size_distribution[10],count_block_size_distribution[11],
356 count_block_size_distribution[12],count_block_size_distribution[13],count_block_size_distribution[14],
357 count_block_size_distribution[15],count_block_size_distribution[16],count_block_size_distribution[17]);
359 sprintf (logtext, "Size of Code Area (Kb): %10.3f", (float) count_code_len / 1024);
361 sprintf (logtext, "Size of data Area (Kb): %10.3f", (float) count_data_len / 1024);
363 sprintf (logtext, "Size of Class Infos (Kb):%10.3f", (float) (count_class_infos) / 1024);
365 sprintf (logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_unicode_len) / 1024);
367 sprintf (logtext, "Size of Vftbl (Kb): %10.3f", (float) count_vftbl_len / 1024);
369 sprintf (logtext, "Size of comp stub (Kb): %10.3f", (float) count_cstub_len / 1024);
371 sprintf (logtext, "Size of native stub (Kb):%10.3f", (float) count_nstub_len / 1024);
373 sprintf (logtext, "Size of Unicode (Kb): %10.3f", (float) count_unicode_len / 1024);
375 sprintf (logtext, "Size of VMCode (Kb): %10.3f(%d)", (float) count_vmcode_len / 1024,
376 count_vmcode_len - 18 * count_all_methods);
378 sprintf (logtext, "Size of ExTable (Kb): %10.3f", (float) count_extable_len / 1024);
380 sprintf (logtext, "Number of loaded Methods: %d\n\n", count_all_methods);
385 /********** Funktion: class_compile_methods (nur f"ur Debug-Zwecke) ********/
387 void class_compile_methods ()
393 c = list_first (&linkedclasses);
395 for (i = 0; i < c -> methodscount; i++) {
396 m = &(c->methods[i]);
399 (void) new_compile(m);
401 (void) compiler_compile(m);
404 c = list_next (&linkedclasses, c);
409 /************************** Funktion: main *******************************
412 Wird vom System zu Programstart aufgerufen (eh klar).
414 **************************************************************************/
417 int main(int argc, char **argv)
422 java_objectheader *exceptionptr;
426 /********** interne (nur fuer main relevante Optionen) **************/
428 char logfilename[200] = "";
429 u4 heapsize = 16000000;
430 u4 heapstartsize = 200000;
431 char classpath[500] = ".:/usr/local/lib/java/classes";
432 bool showmethods = false;
433 bool showconstantpool = false;
434 bool showunicode = false;
436 char *specificmethodname = NULL;
437 char *specificsignature = NULL;
440 stackbottom = &dummy;
444 /************ Infos aus der Environment lesen ************************/
446 cp = getenv ("CLASSPATH");
448 strcpy (classpath, cp);
451 /***************** Interpretieren der Kommandozeile *****************/
456 while ( (i = get_opt(argc,argv)) != OPT_DONE) {
459 case OPT_IGNORE: break;
462 strcpy (classpath + strlen(classpath), ":");
463 strcpy (classpath + strlen(classpath), opt_arg);
468 int n,l=strlen(opt_arg);
469 for (n=0; n<l; n++) {
470 if (opt_arg[n]=='=') {
472 attach_property (opt_arg, opt_arg+n+1);
485 if (opt_arg[strlen(opt_arg)-1] == 'k') {
486 j = 1024 * atoi(opt_arg);
488 else if (opt_arg[strlen(opt_arg)-1] == 'm') {
489 j = 1024 * 1024 * atoi(opt_arg);
491 else j = atoi(opt_arg);
493 if (i==OPT_MX) heapsize = j;
494 else heapstartsize = j;
505 compileverbose = true;
509 collectverbose = true;
512 case OPT_VERBOSECALL:
525 getcompilingtime = true;
526 getloadingtime = true;
534 strcpy (logfilename, opt_arg);
539 for (j=0; j<strlen(opt_arg); j++) {
540 switch (opt_arg[j]) {
541 case 'b': checkbounds=false; break;
542 case 's': checksync=false; break;
543 default: print_usage();
551 makeinitializations = false;
556 specificmethodname = opt_arg;
557 makeinitializations = false;
561 specificsignature = opt_arg;
567 makeinitializations = false;
575 case OPT_SHOW: /* Anzeigeoptionen */
576 for (j=0; j<strlen(opt_arg); j++) {
577 switch (opt_arg[j]) {
578 case 'm': showmethods=true; break;
579 case 'c': showconstantpool=true; break;
580 case 'a': showdisassemble=true; compileverbose=true; break;
581 case 's': showstack=true; compileverbose=true; break;
582 case 'i': showintermediate=true; compileverbose=true; break;
583 case 'u': showunicode=true; break;
584 default: print_usage();
599 if (opt_ind >= argc) {
605 /**************************** Programmstart *****************************/
607 log_init (logfilename);
610 "CACAO started -------------------------------------------------------");
613 suck_init (classpath);
614 native_setclasspath (classpath);
617 heap_init(heapsize, heapstartsize, &dummy);
622 native_loadclasses ();
625 /*********************** JAVA-Klassen laden ***************************/
627 cp = argv[opt_ind++];
628 for (i=strlen(cp)-1; i>=0; i--) { /* Punkte im Klassennamen */
629 if (cp[i]=='.') cp[i]='/'; /* auf slashes umbauen */
632 topclass = loader_load ( unicode_new_char (cp) );
637 initThreads((u1*)&dummy); /* schani */
640 /************************* Arbeitsroutinen starten ********************/
643 methodinfo *mainmethod;
646 mainmethod = class_findmethod (
648 unicode_new_char ("main"),
649 unicode_new_char ("([Ljava/lang/String;)V")
651 if (!mainmethod) panic ("Can not find method 'void main(String[])'");
652 if ((mainmethod->flags & ACC_STATIC) != ACC_STATIC) panic ("main is not static!");
654 a = builtin_anewarray (argc - opt_ind, class_java_lang_String);
655 for (i=opt_ind; i<argc; i++) {
656 a->data[i-opt_ind] = javastring_new (unicode_new_char (argv[i]) );
658 exceptionptr = asm_calljavamethod (mainmethod, a, NULL,NULL,NULL );
661 printf ("#### Program has thrown: ");
662 unicode_display (exceptionptr->vftbl->class->name);
666 /* killThread(currentThread); */
670 /************* Auf Wunsch alle Methode "ubersetzen ********************/
673 class_compile_methods();
677 /******** Auf Wunsch eine spezielle Methode "ubersetzen ***************/
679 if (specificmethodname) {
681 if (specificsignature)
682 m = class_findmethod(topclass,
683 unicode_new_char(specificmethodname),
684 unicode_new_char(specificsignature));
686 m = class_findmethod(topclass,
687 unicode_new_char(specificmethodname), NULL);
688 if (!m) panic ("Specific method not found");
690 (void) new_compile(m);
692 (void) compiler_compile(m);
695 /********************* Debug-Tabellen ausgeben ************************/
697 if (showmethods) class_showmethods (topclass);
698 if (showconstantpool) class_showconstantpool (topclass);
699 if (showunicode) unicode_show ();
703 /************************ Freigeben aller Resourcen *******************/
708 unicode_close ( literalstring_free );
711 /* Endemeldung ausgeben und mit entsprechendem exit-Status terminieren */
713 if (verbose || getcompilingtime || statistics) {
714 log_text ("CACAO terminated");
717 if (getcompilingtime)
728 /************************************ SHUTDOWN-Funktion *********************************
730 Terminiert das System augenblicklich, ohne den Speicher
731 explizit freizugeben (eigentlich nur f"ur abnorme
732 Programmterminierung)
734 *****************************************************************************************/
736 void cacao_shutdown(s4 status)
738 if (verbose || getcompilingtime || statistics) {
739 log_text ("CACAO terminated by shutdown");
742 if (getcompilingtime)
745 sprintf (logtext, "Exit status: %d\n", (int) status);