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 *******************************************************************************/
36 #include "threads/thread.h" /* schani */
38 bool compileall = false;
39 int newcompiler = true;
46 void **stackbottom = 0;
50 /* internal function: get_opt *************************************************
52 decodes the next command line option
54 ******************************************************************************/
60 #define OPT_CLASSPATH 2
64 #define OPT_VERBOSE1 6
66 #define OPT_VERBOSEGC 8
67 #define OPT_VERBOSECALL 9
69 #define OPT_SOFTNULL 11
76 #define OPT_SIGNATURE 18
87 struct {char *name; bool arg; int value;} opts[] = {
88 {"classpath", true, OPT_CLASSPATH},
92 {"noasyncgc", false, OPT_IGNORE},
93 {"noverify", false, OPT_IGNORE},
94 {"oss", true, OPT_IGNORE},
95 {"ss", true, OPT_IGNORE},
96 {"v", false, OPT_VERBOSE1},
97 {"verbose", false, OPT_VERBOSE},
98 {"verbosegc", false, OPT_VERBOSEGC},
99 {"verbosecall", false, OPT_VERBOSECALL},
100 {"ieee", false, OPT_IEEE},
101 {"softnull", false, OPT_SOFTNULL},
102 {"time", false, OPT_TIME},
103 {"stat", false, OPT_STAT},
104 {"log", true, OPT_LOG},
105 {"c", true, OPT_CHECK},
106 {"l", false, OPT_LOAD},
107 {"m", true, OPT_METHOD},
108 {"sig", true, OPT_SIGNATURE},
109 {"s", true, OPT_SHOW},
110 {"all", false, OPT_ALL},
112 {"old", false, OPT_OLD},
115 {"gc1", false, OPT_GC1},
116 {"gc2", false, OPT_GC2},
121 static int opt_ind = 1;
122 static char *opt_arg;
124 static int get_opt (int argc, char **argv)
129 if (opt_ind >= argc) return OPT_DONE;
132 if (a[0] != '-') return OPT_DONE;
134 for (i=0; opts[i].name; i++) {
136 if (strcmp(a+1, opts[i].name) == 0) { /* boolean option found */
138 return opts[i].value;
142 if (strcmp(a+1, opts[i].name) == 0) { /* parameter option found */
144 if (opt_ind < argc) {
145 opt_arg = argv[opt_ind];
147 return opts[i].value;
152 size_t l = strlen(opts[i].name);
153 if (strlen(a+1) > l) {
154 if (memcmp (a+1, opts[i].name, l)==0) {
157 return opts[i].value;
170 /******************** interne Funktion: print_usage ************************
172 Gibt die richtige Aufrufsyntax des JavaVM-Compilers auf stdout aus.
174 ***************************************************************************/
176 static void print_usage()
178 printf ("USAGE: cacao [options] classname [program arguments\n");
179 printf ("Options:\n");
180 printf (" -classpath path ...... specify a path to look for classes\n");
181 printf (" -Dpropertyname=value . add an entry to the property list\n");
182 printf (" -mx maxmem[k|m] ...... specify the size for the heap\n");
183 printf (" -ms initmem[k|m] ..... specify the initial size for the heap\n");
184 printf (" -v ................... write state-information\n");
185 printf (" -verbose ............. write more information\n");
186 printf (" -verbosegc ........... write message for each GC\n");
187 printf (" -verbosecall ......... write message for each call\n");
188 printf (" -ieee ................ use ieee compliant arithmetic\n");
189 printf (" -softnull ............ use software nullpointer check\n");
190 printf (" -time ................ measure the runtime\n");
191 printf (" -stat ................ detailed compiler statistics\n");
192 printf (" -log logfile ......... specify a name for the logfile\n");
193 printf (" -c(heck) b(ounds...... don't check array bounds\n");
194 printf (" s(ync) ...... don't check for synchronization\n");
195 printf (" -l ................... don't start the class after loading\n");
196 printf (" -all ................. compile all methods, no execution\n");
198 printf (" -old ................. use old JIT compiler\n");
201 printf (" -gc1 ................. use the old garbage collector (default)\n");
202 printf (" -gc2 ................. use the new garbage collector\n");
204 printf (" -m ................... compile only a specific method\n");
205 printf (" -sig ................. specify signature for a specific method\n");
206 printf (" -s(how)m(ethods) ..... show all methods&fields of a class\n");
207 printf (" a(ssembler) ... show disassembled listing\n");
208 printf (" c(onstants) ... show the constant pool\n");
209 printf (" d(atasegment).. show data segment listing\n");
210 printf (" i(ntermediate). show intermediate representation\n");
211 printf (" m(ethods)...... show class fields and methods\n");
213 printf (" s(tack) ....... show stack for every javaVM-command\n");
215 printf (" u(nicode) ..... show the unicode - hash\n");
220 /***************************** Funktion: print_times *********************
222 gibt eine Aufstellung der verwendeten CPU-Zeit aus
224 **************************************************************************/
226 static void print_times()
228 long int totaltime = getcputime();
229 long int runtime = totaltime - loadingtime - compilingtime;
231 sprintf (logtext, "Time for loading classes: %ld secs, %ld millis",
232 loadingtime / 1000000, (loadingtime % 1000000) / 1000);
234 sprintf (logtext, "Time for compiling code: %ld secs, %ld millis",
235 compilingtime / 1000000, (compilingtime % 1000000) / 1000);
237 sprintf (logtext, "Time for running program: %ld secs, %ld millis",
238 runtime / 1000000, (runtime % 1000000) / 1000);
240 sprintf (logtext, "Total time: %ld secs, %ld millis",
241 totaltime / 1000000, (totaltime % 1000000) / 1000);
250 /***************************** Funktion: print_stats *********************
252 outputs detailed compiler statistics
254 **************************************************************************/
256 static void print_stats()
258 sprintf (logtext, "Number of JitCompiler Calls: %d", count_jit_calls);
260 sprintf (logtext, "Number of compiled Methods: %d", count_methods);
262 sprintf (logtext, "Number of max basic blocks per method: %d", count_max_basic_blocks);
264 sprintf (logtext, "Number of compiled basic blocks: %d", count_basic_blocks);
266 sprintf (logtext, "Number of max JavaVM-Instructions per method: %d", count_max_javainstr);
268 sprintf (logtext, "Number of compiled JavaVM-Instructions: %d", count_javainstr);
270 sprintf (logtext, "Size of compiled JavaVM-Instructions: %d(%d)", count_javacodesize,
271 count_javacodesize - count_methods * 18);
273 sprintf (logtext, "Size of compiled Exception Tables: %d", count_javaexcsize);
275 sprintf (logtext, "Value of extended instruction set var: %d", has_ext_instr_set);
277 sprintf (logtext, "Number of Alpha-Instructions: %d", count_code_len >> 2);
279 sprintf (logtext, "Number of Spills: %d", count_spills);
281 sprintf (logtext, "Number of Activ Pseudocommands: %5d", count_pcmd_activ);
283 sprintf (logtext, "Number of Drop Pseudocommands: %5d", count_pcmd_drop);
285 sprintf (logtext, "Number of Const Pseudocommands: %5d (zero:%5d)", count_pcmd_load, count_pcmd_zero);
287 sprintf (logtext, "Number of ConstAlu Pseudocommands: %5d (cmp: %5d, store:%5d)", count_pcmd_const_alu, count_pcmd_const_bra, count_pcmd_const_store);
289 sprintf (logtext, "Number of Move Pseudocommands: %5d", count_pcmd_move);
291 sprintf (logtext, "Number of Load Pseudocommands: %5d", count_load_instruction);
293 sprintf (logtext, "Number of Store Pseudocommands: %5d (combined: %5d)", count_pcmd_store, count_pcmd_store - count_pcmd_store_comb);
295 sprintf (logtext, "Number of OP Pseudocommands: %5d", count_pcmd_op);
297 sprintf (logtext, "Number of DUP Pseudocommands: %5d", count_dup_instruction);
299 sprintf (logtext, "Number of Mem Pseudocommands: %5d", count_pcmd_mem);
301 sprintf (logtext, "Number of Method Pseudocommands: %5d", count_pcmd_met);
303 sprintf (logtext, "Number of Branch Pseudocommands: %5d (rets:%5d, Xrets: %5d)",
304 count_pcmd_bra, count_pcmd_return, count_pcmd_returnx);
306 sprintf (logtext, "Number of Table Pseudocommands: %5d", count_pcmd_table);
308 sprintf (logtext, "Number of Useful Pseudocommands: %5d", count_pcmd_table +
309 count_pcmd_bra + count_pcmd_load + count_pcmd_mem + count_pcmd_op);
311 sprintf (logtext, "Number of Null Pointer Checks: %5d", count_check_null);
313 sprintf (logtext, "Number of Array Bound Checks: %5d", count_check_bound);
315 sprintf (logtext, "Number of Try-Blocks: %d", count_tryblocks);
317 sprintf (logtext, "Maximal count of stack elements: %d", count_max_new_stack);
319 sprintf (logtext, "Upper bound of max stack elements: %d", count_upper_bound_new_stack);
321 sprintf (logtext, "Distribution of stack sizes at block boundary");
323 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
325 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_block_stack[0],
326 count_block_stack[1],count_block_stack[2],count_block_stack[3],count_block_stack[4],
327 count_block_stack[5],count_block_stack[6],count_block_stack[7],count_block_stack[8],
328 count_block_stack[9],count_block_stack[10]);
330 sprintf (logtext, "Distribution of store stack depth");
332 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
334 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_depth[0],
335 count_store_depth[1],count_store_depth[2],count_store_depth[3],count_store_depth[4],
336 count_store_depth[5],count_store_depth[6],count_store_depth[7],count_store_depth[8],
337 count_store_depth[9],count_store_depth[10]);
339 sprintf (logtext, "Distribution of store creator chains first part");
341 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 ");
343 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[0],
344 count_store_length[1],count_store_length[2],count_store_length[3],count_store_length[4],
345 count_store_length[5],count_store_length[6],count_store_length[7],count_store_length[8],
346 count_store_length[9]);
348 sprintf (logtext, "Distribution of store creator chains second part");
350 sprintf (logtext, " 10 11 12 13 14 15 16 17 18 19 >=20");
352 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[10],
353 count_store_length[11],count_store_length[12],count_store_length[13],count_store_length[14],
354 count_store_length[15],count_store_length[16],count_store_length[17],count_store_length[18],
355 count_store_length[19],count_store_length[20]);
357 sprintf (logtext, "Distribution of analysis iterations");
359 sprintf (logtext, " 1 2 3 4 >=5");
361 sprintf (logtext, "%5d%5d%5d%5d%5d", count_analyse_iterations[0],count_analyse_iterations[1],
362 count_analyse_iterations[2],count_analyse_iterations[3],count_analyse_iterations[4]);
364 sprintf (logtext, "Distribution of basic blocks per method");
366 sprintf (logtext, " <= 5 <=10 <=15 <=20 <=30 <=40 <=50 <=75 >75");
368 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_method_bb_distribution[0],
369 count_method_bb_distribution[1],count_method_bb_distribution[2],count_method_bb_distribution[3],
370 count_method_bb_distribution[4],count_method_bb_distribution[5],count_method_bb_distribution[6],
371 count_method_bb_distribution[7],count_method_bb_distribution[8]);
373 sprintf (logtext, "Distribution of basic block sizes");
376 " 1 2 3 4 5 6 7 8 9 10 <13 <15 <17 <19 <21 <26 <31 >30");
378 sprintf (logtext, "%3d%5d%5d%5d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d",
379 count_block_size_distribution[0], count_block_size_distribution[1], count_block_size_distribution[2],
380 count_block_size_distribution[3], count_block_size_distribution[4], count_block_size_distribution[5],
381 count_block_size_distribution[6], count_block_size_distribution[7], count_block_size_distribution[8],
382 count_block_size_distribution[9], count_block_size_distribution[10],count_block_size_distribution[11],
383 count_block_size_distribution[12],count_block_size_distribution[13],count_block_size_distribution[14],
384 count_block_size_distribution[15],count_block_size_distribution[16],count_block_size_distribution[17]);
386 sprintf (logtext, "Size of Code Area (Kb): %10.3f", (float) count_code_len / 1024);
388 sprintf (logtext, "Size of data Area (Kb): %10.3f", (float) count_data_len / 1024);
390 sprintf (logtext, "Size of Class Infos (Kb):%10.3f", (float) (count_class_infos) / 1024);
392 sprintf (logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_unicode_len) / 1024);
394 sprintf (logtext, "Size of Vftbl (Kb): %10.3f", (float) count_vftbl_len / 1024);
396 sprintf (logtext, "Size of comp stub (Kb): %10.3f", (float) count_cstub_len / 1024);
398 sprintf (logtext, "Size of native stub (Kb):%10.3f", (float) count_nstub_len / 1024);
400 sprintf (logtext, "Size of Unicode (Kb): %10.3f", (float) count_unicode_len / 1024);
402 sprintf (logtext, "Size of VMCode (Kb): %10.3f(%d)", (float) count_vmcode_len / 1024,
403 count_vmcode_len - 18 * count_all_methods);
405 sprintf (logtext, "Size of ExTable (Kb): %10.3f", (float) count_extable_len / 1024);
407 sprintf (logtext, "Number of loaded Methods: %d\n\n", count_all_methods);
412 /********** Funktion: class_compile_methods (nur f"ur Debug-Zwecke) ********/
414 void class_compile_methods ()
420 c = list_first (&linkedclasses);
422 for (i = 0; i < c -> methodscount; i++) {
423 m = &(c->methods[i]);
428 (void) jit_compile(m);
431 (void) compiler_compile(m);
435 c = list_next (&linkedclasses, c);
440 /************************** Funktion: main *******************************
443 Wird vom System zu Programstart aufgerufen (eh klar).
445 **************************************************************************/
448 int main(int argc, char **argv)
453 java_objectheader *exceptionptr;
456 /********** interne (nur fuer main relevante Optionen) **************/
458 char logfilename[200] = "";
459 u4 heapsize = 16000000;
460 u4 heapstartsize = 200000;
461 char classpath[500] = ".:/usr/local/lib/java/classes";
462 bool showmethods = false;
463 bool showconstantpool = false;
464 bool showunicode = false;
466 char *specificmethodname = NULL;
467 char *specificsignature = NULL;
470 stackbottom = &dummy;
475 atexit(clear_thread_flags);
478 /************ Infos aus der Environment lesen ************************/
480 cp = getenv ("CLASSPATH");
482 strcpy (classpath, cp);
485 /***************** Interpretieren der Kommandozeile *****************/
490 while ( (i = get_opt(argc,argv)) != OPT_DONE) {
493 case OPT_IGNORE: break;
496 strcpy (classpath + strlen(classpath), ":");
497 strcpy (classpath + strlen(classpath), opt_arg);
502 int n,l=strlen(opt_arg);
503 for (n=0; n<l; n++) {
504 if (opt_arg[n]=='=') {
506 attach_property (opt_arg, opt_arg+n+1);
519 if (opt_arg[strlen(opt_arg)-1] == 'k') {
520 j = 1024 * atoi(opt_arg);
522 else if (opt_arg[strlen(opt_arg)-1] == 'm') {
523 j = 1024 * 1024 * atoi(opt_arg);
525 else j = atoi(opt_arg);
527 if (i==OPT_MX) heapsize = j;
528 else heapstartsize = j;
539 compileverbose = true;
543 collectverbose = true;
546 case OPT_VERBOSECALL:
559 getcompilingtime = true;
560 getloadingtime = true;
568 strcpy (logfilename, opt_arg);
573 for (j=0; j<strlen(opt_arg); j++) {
574 switch (opt_arg[j]) {
575 case 'b': checkbounds=false; break;
576 case 's': checksync=false; break;
577 default: print_usage();
585 makeinitializations = false;
590 specificmethodname = opt_arg;
591 makeinitializations = false;
595 specificsignature = opt_arg;
601 makeinitializations = false;
621 case OPT_SHOW: /* Anzeigeoptionen */
622 for (j=0; j<strlen(opt_arg); j++) {
623 switch (opt_arg[j]) {
624 case 'a': showdisassemble=true; compileverbose=true; break;
625 case 'c': showconstantpool=true; break;
626 case 'd': showddatasegment=true; break;
627 case 'i': showintermediate=true; compileverbose=true; break;
628 case 'm': showmethods=true; break;
630 case 's': showstack=true; compileverbose=true; break;
632 case 'u': showunicode=true; break;
633 default: print_usage();
648 if (opt_ind >= argc) {
654 /**************************** Programmstart *****************************/
656 log_init (logfilename);
659 "CACAO started -------------------------------------------------------");
662 suck_init (classpath);
663 native_setclasspath (classpath);
666 heap_init(heapsize, heapstartsize, &dummy);
673 native_loadclasses ();
676 /*********************** JAVA-Klassen laden ***************************/
678 cp = argv[opt_ind++];
679 for (i=strlen(cp)-1; i>=0; i--) { /* Punkte im Klassennamen */
680 if (cp[i]=='.') cp[i]='/'; /* auf slashes umbauen */
683 topclass = loader_load ( unicode_new_char (cp) );
685 loader_compute_subclasses();
690 initThreads((u1*)&dummy); /* schani */
693 /************************* Arbeitsroutinen starten ********************/
696 methodinfo *mainmethod;
699 heap_addreference((void**) &a);
701 mainmethod = class_findmethod (
703 unicode_new_char ("main"),
704 unicode_new_char ("([Ljava/lang/String;)V")
706 if (!mainmethod) panic ("Can not find method 'void main(String[])'");
707 if ((mainmethod->flags & ACC_STATIC) != ACC_STATIC) panic ("main is not static!");
709 a = builtin_anewarray (argc - opt_ind, class_java_lang_String);
710 for (i=opt_ind; i<argc; i++) {
711 a->data[i-opt_ind] = javastring_new (unicode_new_char (argv[i]) );
713 exceptionptr = asm_calljavamethod (mainmethod, a, NULL,NULL,NULL );
716 printf ("#### Program has thrown: ");
717 unicode_display (exceptionptr->vftbl->class->name);
721 /* killThread(currentThread); */
725 /************* Auf Wunsch alle Methode "ubersetzen ********************/
728 class_compile_methods();
732 /******** Auf Wunsch eine spezielle Methode "ubersetzen ***************/
734 if (specificmethodname) {
736 if (specificsignature)
737 m = class_findmethod(topclass,
738 unicode_new_char(specificmethodname),
739 unicode_new_char(specificsignature));
741 m = class_findmethod(topclass,
742 unicode_new_char(specificmethodname), NULL);
743 if (!m) panic ("Specific method not found");
747 (void) jit_compile(m);
750 (void) compiler_compile(m);
754 /********************* Debug-Tabellen ausgeben ************************/
756 if (showmethods) class_showmethods (topclass);
757 if (showconstantpool) class_showconstantpool (topclass);
758 if (showunicode) unicode_show ();
762 /************************ Freigeben aller Resourcen *******************/
764 heap_close (); /* must be called before compiler_close and
765 loader_close because finalization occurs
771 unicode_close ( literalstring_free );
774 /* Endemeldung ausgeben und mit entsprechendem exit-Status terminieren */
776 if (verbose || getcompilingtime || statistics) {
777 log_text ("CACAO terminated");
780 if (getcompilingtime)
791 /************************************ SHUTDOWN-Funktion *********************************
793 Terminiert das System augenblicklich, ohne den Speicher
794 explizit freizugeben (eigentlich nur f"ur abnorme
795 Programmterminierung)
797 *****************************************************************************************/
799 void cacao_shutdown(s4 status)
801 if (verbose || getcompilingtime || statistics) {
802 log_text ("CACAO terminated by shutdown");
805 if (getcompilingtime)
808 sprintf (logtext, "Exit status: %d\n", (int) status);
817 * These are local overrides for various environment variables in Emacs.
818 * Please do not remove this and leave it at the end of the file, where
819 * Emacs will automagically detect them.
820 * ---------------------------------------------------------------------
823 * indent-tabs-mode: t