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 Contains main() and variables for the global options.
8 This module does the following tasks:
9 - Command line option handling
10 - Calling initialization routines
11 - Calling the class loader
12 - Running the main method
14 Authors: Reinhard Grafl EMAIL: cacao@complang.tuwien.ac.at
15 Changes: Andi Krall EMAIL: cacao@complang.tuwien.ac.at
16 Mark Probst EMAIL: cacao@complang.tuwien.ac.at
17 Philipp Tomsich EMAIL: cacao@complang.tuwien.ac.at
19 Last Change: $Id: main.c 285 2003-05-12 20:05:38Z carolyn $
21 *******************************************************************************/
36 #include "threads/thread.h"
38 bool compileall = false;
39 int newcompiler = true;
45 static bool showmethods = false;
46 static bool showconstantpool = false;
47 static bool showutf = false;
48 static classinfo *topclass;
51 void **stackbottom = 0;
55 /* internal function: get_opt *************************************************
57 decodes the next command line option
59 ******************************************************************************/
65 #define OPT_CLASSPATH 2
69 #define OPT_VERBOSE1 6
71 #define OPT_VERBOSEGC 8
72 #define OPT_VERBOSECALL 9
74 #define OPT_SOFTNULL 11
81 #define OPT_SIGNATURE 18
95 struct {char *name; bool arg; int value;} opts[] = {
96 {"classpath", true, OPT_CLASSPATH},
100 {"noasyncgc", false, OPT_IGNORE},
101 {"noverify", false, OPT_IGNORE},
102 {"oss", true, OPT_IGNORE},
103 {"ss", true, OPT_IGNORE},
104 {"v", false, OPT_VERBOSE1},
105 {"verbose", false, OPT_VERBOSE},
106 {"verbosegc", false, OPT_VERBOSEGC},
107 {"verbosecall", false, OPT_VERBOSECALL},
108 {"ieee", false, OPT_IEEE},
109 {"softnull", false, OPT_SOFTNULL},
110 {"time", false, OPT_TIME},
111 {"stat", false, OPT_STAT},
112 {"log", true, OPT_LOG},
113 {"c", true, OPT_CHECK},
114 {"l", false, OPT_LOAD},
115 {"m", true, OPT_METHOD},
116 {"sig", true, OPT_SIGNATURE},
117 {"s", true, OPT_SHOW},
118 {"all", false, OPT_ALL},
120 {"old", false, OPT_OLD},
123 {"gc1", false, OPT_GC1},
124 {"gc2", false, OPT_GC2},
126 {"oloop", false, OPT_OLOOP},
127 {"rt", false, OPT_RT},
128 {"xta", false, OPT_XTA},
132 static int opt_ind = 1;
133 static char *opt_arg;
135 static int get_opt (int argc, char **argv)
140 if (opt_ind >= argc) return OPT_DONE;
143 if (a[0] != '-') return OPT_DONE;
145 for (i=0; opts[i].name; i++) {
147 if (strcmp(a+1, opts[i].name) == 0) { /* boolean option found */
149 return opts[i].value;
153 if (strcmp(a+1, opts[i].name) == 0) { /* parameter option found */
155 if (opt_ind < argc) {
156 opt_arg = argv[opt_ind];
158 return opts[i].value;
163 size_t l = strlen(opts[i].name);
164 if (strlen(a+1) > l) {
165 if (memcmp (a+1, opts[i].name, l)==0) {
168 return opts[i].value;
181 /******************** interne Function: print_usage ************************
183 Prints the correct usage syntax to stdout.
185 ***************************************************************************/
187 static void print_usage()
189 printf ("USAGE: cacao [options] classname [program arguments\n");
190 printf ("Options:\n");
191 printf (" -classpath path ...... specify a path to look for classes\n");
192 printf (" -Dpropertyname=value . add an entry to the property list\n");
193 printf (" -mx maxmem[k|m] ...... specify the size for the heap\n");
194 printf (" -ms initmem[k|m] ..... specify the initial size for the heap\n");
195 printf (" -v ................... write state-information\n");
196 printf (" -verbose ............. write more information\n");
197 printf (" -verbosegc ........... write message for each GC\n");
198 printf (" -verbosecall ......... write message for each call\n");
199 printf (" -ieee ................ use ieee compliant arithmetic\n");
200 printf (" -softnull ............ use software nullpointer check\n");
201 printf (" -time ................ measure the runtime\n");
202 printf (" -stat ................ detailed compiler statistics\n");
203 printf (" -log logfile ......... specify a name for the logfile\n");
204 printf (" -c(heck)b(ounds) ..... don't check array bounds\n");
205 printf (" s(ync) ....... don't check for synchronization\n");
206 printf (" -oloop ............... optimize array accesses in loops\n");
207 printf (" -l ................... don't start the class after loading\n");
208 printf (" -all ................. compile all methods, no execution\n");
210 printf (" -old ................. use old JIT compiler\n");
213 printf (" -gc1 ................. use the old garbage collector (default)\n");
214 printf (" -gc2 ................. use the new garbage collector\n");
216 printf (" -m ................... compile only a specific method\n");
217 printf (" -sig ................. specify signature for a specific method\n");
218 printf (" -s(how)a(ssembler) ... show disassembled listing\n");
219 printf (" c(onstants) ... show the constant pool\n");
220 printf (" d(atasegment).. show data segment listing\n");
221 printf (" i(ntermediate). show intermediate representation\n");
222 printf (" m(ethods)...... show class fields and methods\n");
224 printf (" s(tack) ....... show stack for every javaVM-command\n");
226 printf (" u(tf) ......... show the utf - hash\n");
227 printf (" -rt .................. use rapid type analysis\n");
228 printf (" -xta ................. use xta\n");
233 /***************************** Function: print_times *********************
235 Prints a summary of CPU time usage.
237 **************************************************************************/
239 static void print_times()
241 long int totaltime = getcputime();
242 long int runtime = totaltime - loadingtime - compilingtime;
244 sprintf (logtext, "Time for loading classes: %ld secs, %ld millis",
245 loadingtime / 1000000, (loadingtime % 1000000) / 1000);
247 sprintf (logtext, "Time for compiling code: %ld secs, %ld millis",
248 compilingtime / 1000000, (compilingtime % 1000000) / 1000);
250 sprintf (logtext, "Time for running program: %ld secs, %ld millis",
251 runtime / 1000000, (runtime % 1000000) / 1000);
253 sprintf (logtext, "Total time: %ld secs, %ld millis",
254 totaltime / 1000000, (totaltime % 1000000) / 1000);
263 /***************************** Function: print_stats *********************
265 outputs detailed compiler statistics
267 **************************************************************************/
269 static void print_stats()
271 sprintf (logtext, "Number of JitCompiler Calls: %d", count_jit_calls);
273 sprintf (logtext, "Number of compiled Methods: %d", count_methods);
275 sprintf (logtext, "Number of max basic blocks per method: %d", count_max_basic_blocks);
277 sprintf (logtext, "Number of compiled basic blocks: %d", count_basic_blocks);
279 sprintf (logtext, "Number of max JavaVM-Instructions per method: %d", count_max_javainstr);
281 sprintf (logtext, "Number of compiled JavaVM-Instructions: %d", count_javainstr);
283 sprintf (logtext, "Size of compiled JavaVM-Instructions: %d(%d)", count_javacodesize,
284 count_javacodesize - count_methods * 18);
286 sprintf (logtext, "Size of compiled Exception Tables: %d", count_javaexcsize);
288 sprintf (logtext, "Value of extended instruction set var: %d", has_ext_instr_set);
290 sprintf (logtext, "Number of Alpha-Instructions: %d", count_code_len >> 2);
292 sprintf (logtext, "Number of Spills: %d", count_spills);
294 sprintf (logtext, "Number of Activ Pseudocommands: %5d", count_pcmd_activ);
296 sprintf (logtext, "Number of Drop Pseudocommands: %5d", count_pcmd_drop);
298 sprintf (logtext, "Number of Const Pseudocommands: %5d (zero:%5d)", count_pcmd_load, count_pcmd_zero);
300 sprintf (logtext, "Number of ConstAlu Pseudocommands: %5d (cmp: %5d, store:%5d)", count_pcmd_const_alu, count_pcmd_const_bra, count_pcmd_const_store);
302 sprintf (logtext, "Number of Move Pseudocommands: %5d", count_pcmd_move);
304 sprintf (logtext, "Number of Load Pseudocommands: %5d", count_load_instruction);
306 sprintf (logtext, "Number of Store Pseudocommands: %5d (combined: %5d)", count_pcmd_store, count_pcmd_store - count_pcmd_store_comb);
308 sprintf (logtext, "Number of OP Pseudocommands: %5d", count_pcmd_op);
310 sprintf (logtext, "Number of DUP Pseudocommands: %5d", count_dup_instruction);
312 sprintf (logtext, "Number of Mem Pseudocommands: %5d", count_pcmd_mem);
314 sprintf (logtext, "Number of Method Pseudocommands: %5d", count_pcmd_met);
316 sprintf (logtext, "Number of Branch Pseudocommands: %5d (rets:%5d, Xrets: %5d)",
317 count_pcmd_bra, count_pcmd_return, count_pcmd_returnx);
319 sprintf (logtext, "Number of Table Pseudocommands: %5d", count_pcmd_table);
321 sprintf (logtext, "Number of Useful Pseudocommands: %5d", count_pcmd_table +
322 count_pcmd_bra + count_pcmd_load + count_pcmd_mem + count_pcmd_op);
324 sprintf (logtext, "Number of Null Pointer Checks: %5d", count_check_null);
326 sprintf (logtext, "Number of Array Bound Checks: %5d", count_check_bound);
328 sprintf (logtext, "Number of Try-Blocks: %d", count_tryblocks);
330 sprintf (logtext, "Maximal count of stack elements: %d", count_max_new_stack);
332 sprintf (logtext, "Upper bound of max stack elements: %d", count_upper_bound_new_stack);
334 sprintf (logtext, "Distribution of stack sizes at block boundary");
336 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
338 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_block_stack[0],
339 count_block_stack[1],count_block_stack[2],count_block_stack[3],count_block_stack[4],
340 count_block_stack[5],count_block_stack[6],count_block_stack[7],count_block_stack[8],
341 count_block_stack[9],count_block_stack[10]);
343 sprintf (logtext, "Distribution of store stack depth");
345 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
347 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_depth[0],
348 count_store_depth[1],count_store_depth[2],count_store_depth[3],count_store_depth[4],
349 count_store_depth[5],count_store_depth[6],count_store_depth[7],count_store_depth[8],
350 count_store_depth[9],count_store_depth[10]);
352 sprintf (logtext, "Distribution of store creator chains first part");
354 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 ");
356 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[0],
357 count_store_length[1],count_store_length[2],count_store_length[3],count_store_length[4],
358 count_store_length[5],count_store_length[6],count_store_length[7],count_store_length[8],
359 count_store_length[9]);
361 sprintf (logtext, "Distribution of store creator chains second part");
363 sprintf (logtext, " 10 11 12 13 14 15 16 17 18 19 >=20");
365 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[10],
366 count_store_length[11],count_store_length[12],count_store_length[13],count_store_length[14],
367 count_store_length[15],count_store_length[16],count_store_length[17],count_store_length[18],
368 count_store_length[19],count_store_length[20]);
370 sprintf (logtext, "Distribution of analysis iterations");
372 sprintf (logtext, " 1 2 3 4 >=5");
374 sprintf (logtext, "%5d%5d%5d%5d%5d", count_analyse_iterations[0],count_analyse_iterations[1],
375 count_analyse_iterations[2],count_analyse_iterations[3],count_analyse_iterations[4]);
377 sprintf (logtext, "Distribution of basic blocks per method");
379 sprintf (logtext, " <= 5 <=10 <=15 <=20 <=30 <=40 <=50 <=75 >75");
381 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_method_bb_distribution[0],
382 count_method_bb_distribution[1],count_method_bb_distribution[2],count_method_bb_distribution[3],
383 count_method_bb_distribution[4],count_method_bb_distribution[5],count_method_bb_distribution[6],
384 count_method_bb_distribution[7],count_method_bb_distribution[8]);
386 sprintf (logtext, "Distribution of basic block sizes");
389 " 0 1 2 3 4 5 6 7 8 9 <13 <15 <17 <19 <21 <26 <31 >30");
391 sprintf (logtext, "%3d%5d%5d%5d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d",
392 count_block_size_distribution[0], count_block_size_distribution[1], count_block_size_distribution[2],
393 count_block_size_distribution[3], count_block_size_distribution[4], count_block_size_distribution[5],
394 count_block_size_distribution[6], count_block_size_distribution[7], count_block_size_distribution[8],
395 count_block_size_distribution[9], count_block_size_distribution[10],count_block_size_distribution[11],
396 count_block_size_distribution[12],count_block_size_distribution[13],count_block_size_distribution[14],
397 count_block_size_distribution[15],count_block_size_distribution[16],count_block_size_distribution[17]);
399 sprintf (logtext, "Size of Code Area (Kb): %10.3f", (float) count_code_len / 1024);
401 sprintf (logtext, "Size of data Area (Kb): %10.3f", (float) count_data_len / 1024);
403 sprintf (logtext, "Size of Class Infos (Kb):%10.3f", (float) (count_class_infos) / 1024);
405 sprintf (logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_utf_len) / 1024);
407 sprintf (logtext, "Size of Vftbl (Kb): %10.3f", (float) count_vftbl_len / 1024);
409 sprintf (logtext, "Size of comp stub (Kb): %10.3f", (float) count_cstub_len / 1024);
411 sprintf (logtext, "Size of native stub (Kb):%10.3f", (float) count_nstub_len / 1024);
413 sprintf (logtext, "Size of Utf (Kb): %10.3f", (float) count_utf_len / 1024);
415 sprintf (logtext, "Size of VMCode (Kb): %10.3f(%d)", (float) count_vmcode_len / 1024,
416 count_vmcode_len - 18 * count_all_methods);
418 sprintf (logtext, "Size of ExTable (Kb): %10.3f", (float) count_extable_len / 1024);
420 sprintf (logtext, "Number of class loads: %d", count_class_loads);
422 sprintf (logtext, "Number of class inits: %d", count_class_inits);
424 sprintf (logtext, "Number of loaded Methods: %d\n\n", count_all_methods);
427 sprintf (logtext, "Calls of utf_new: %22d", count_utf_new);
429 sprintf (logtext, "Calls of utf_new (element found): %6d\n\n", count_utf_new_found);
434 /********** Function: class_compile_methods (debugging only) ********/
436 void class_compile_methods ()
442 c = list_first (&linkedclasses);
444 for (i = 0; i < c -> methodscount; i++) {
445 m = &(c->methods[i]);
450 (void) jit_compile(m);
453 (void) compiler_compile(m);
457 c = list_next (&linkedclasses, c);
462 * void exit_handler(void)
463 * -----------------------
464 * The exit_handler function is called upon program termination to shutdown
465 * the various subsystems and release the resources allocated to the VM.
468 void exit_handler(void)
470 /********************* Print debug tables ************************/
472 if (showmethods) class_showmethods (topclass);
473 if (showconstantpool) class_showconstantpool (topclass);
474 if (showutf) utf_show ();
477 clear_thread_flags(); /* restores standard file descriptor
481 /************************ Free all resources *******************/
483 heap_close (); /* must be called before compiler_close and
484 loader_close because finalization occurs
491 tables_close ( literalstring_free );
493 if (verbose || getcompilingtime || statistics) {
494 log_text ("CACAO terminated");
497 if (getcompilingtime)
503 /************************** Function: main *******************************
507 **************************************************************************/
509 int main(int argc, char **argv)
513 java_objectheader *local_exceptionptr = 0;
516 /********** interne (nur fuer main relevante Optionen) **************/
518 char logfilename[200] = "";
519 u4 heapsize = 64000000;
520 u4 heapstartsize = 200000;
521 char classpath[500] = ".:/usr/local/lib/java/classes";
523 char *specificmethodname = NULL;
524 char *specificsignature = NULL;
527 stackbottom = &dummy;
530 if (0 != atexit(exit_handler))
531 panic("unable to register exit_handler");
533 /************ Collect info from the environment ************************/
535 cp = getenv ("CLASSPATH");
537 strcpy (classpath, cp);
540 /***************** Interpret the command line *****************/
545 while ( (i = get_opt(argc,argv)) != OPT_DONE) {
548 case OPT_IGNORE: break;
551 strcpy (classpath + strlen(classpath), ":");
552 strcpy (classpath + strlen(classpath), opt_arg);
557 int n,l=strlen(opt_arg);
558 for (n=0; n<l; n++) {
559 if (opt_arg[n]=='=') {
561 attach_property (opt_arg, opt_arg+n+1);
574 if (opt_arg[strlen(opt_arg)-1] == 'k') {
575 j = 1024 * atoi(opt_arg);
577 else if (opt_arg[strlen(opt_arg)-1] == 'm') {
578 j = 1024 * 1024 * atoi(opt_arg);
580 else j = atoi(opt_arg);
582 if (i==OPT_MX) heapsize = j;
583 else heapstartsize = j;
594 compileverbose = true;
598 collectverbose = true;
601 case OPT_VERBOSECALL:
614 getcompilingtime = true;
615 getloadingtime = true;
623 strcpy (logfilename, opt_arg);
628 for (j=0; j<strlen(opt_arg); j++) {
629 switch (opt_arg[j]) {
630 case 'b': checkbounds=false; break;
631 case 's': checksync=false; break;
632 default: print_usage();
640 makeinitializations = false;
645 specificmethodname = opt_arg;
646 makeinitializations = false;
650 specificsignature = opt_arg;
656 makeinitializations = false;
676 case OPT_SHOW: /* Display options */
677 for (j=0; j<strlen(opt_arg); j++) {
678 switch (opt_arg[j]) {
679 case 'a': showdisassemble=true; compileverbose=true; break;
680 case 'c': showconstantpool=true; break;
681 case 'd': showddatasegment=true; break;
682 case 'i': showintermediate=true; compileverbose=true; break;
683 case 'm': showmethods=true; break;
685 case 's': showstack=true; compileverbose=true; break;
687 case 'u': showutf=true; break;
688 default: print_usage();
715 if (opt_ind >= argc) {
721 /**************************** Program start *****************************/
723 log_init (logfilename);
726 "CACAO started -------------------------------------------------------");
729 suck_init (classpath);
730 native_setclasspath (classpath);
733 heap_init(heapsize, heapstartsize, &dummy);
740 native_loadclasses ();
743 /*********************** Load JAVA classes ***************************/
745 cp = argv[opt_ind++];
746 for (i=strlen(cp)-1; i>=0; i--) { /* Transform dots into slashes */
747 if (cp[i]=='.') cp[i]='/'; /* in the class name */
750 topclass = loader_load ( utf_new_char (cp) );
752 if (exceptionptr != 0)
754 printf ("#### Class loader has thrown: ");
755 utf_display (exceptionptr->vftbl->class->name);
763 printf("#### Could not find top class - exiting\n");
770 initThreads((u1*)&dummy); /* schani */
773 /************************* Start worker routines ********************/
776 methodinfo *mainmethod;
779 heap_addreference((void**) &a);
781 mainmethod = class_findmethod (
783 utf_new_char ("main"),
784 utf_new_char ("([Ljava/lang/String;)V")
786 if (!mainmethod) panic ("Can not find method 'void main(String[])'");
787 if ((mainmethod->flags & ACC_STATIC) != ACC_STATIC) panic ("main is not static!");
789 a = builtin_anewarray (argc - opt_ind, class_java_lang_String);
790 for (i=opt_ind; i<argc; i++) {
791 a->data[i-opt_ind] = javastring_new (utf_new_char (argv[i]) );
793 local_exceptionptr = asm_calljavamethod (mainmethod, a, NULL,NULL,NULL );
795 if (local_exceptionptr) {
796 printf ("#### Program has thrown: ");
797 utf_display (local_exceptionptr->vftbl->class->name);
800 /*RTAprint*/ if ((pCallgraph >= 1) && (opt_rt)) {
801 /*RTAprint*/ printCallgraph (); }
803 /*RTprint*/ if ((pClassHeir >= 1) && (opt_rt)) {
804 /*RTprint*/ printf("Last RTA Class Heirarchy -");
805 /*RTprint*/ printRThierarchyInfo(mainmethod); }
808 killThread(currentThread);
810 fprintf(stderr, "still here\n");
813 /************* If requested, compile all methods ********************/
816 class_compile_methods();
820 /******** If requested, compile a specific method ***************/
822 if (specificmethodname) {
824 if (specificsignature)
825 m = class_findmethod(topclass,
826 utf_new_char(specificmethodname),
827 utf_new_char(specificsignature));
829 m = class_findmethod(topclass,
830 utf_new_char(specificmethodname), NULL);
831 if (!m) panic ("Specific method not found");
835 (void) jit_compile(m);
838 (void) compiler_compile(m);
847 /************************************ Shutdown function *********************************
849 Terminates the system immediately without freeing memory explicitly (to be
850 used only for abnormal termination)
852 *****************************************************************************************/
854 void cacao_shutdown(s4 status)
856 /*RTAprint*/ if ((pCallgraph >= 1) && (opt_rt)) {
857 /*RTAprint*/ printCallgraph (NULL); }
859 /*RTprint*/ if ((pClassHeir >= 1) && (opt_rt)) {
860 /*RTprint*/ printf("Last RTA Class Heirarchy -");
861 /*RTprint*/ printRThierarchyInfo(NULL); }
863 if (verbose || getcompilingtime || statistics) {
864 log_text ("CACAO terminated by shutdown");
867 if (getcompilingtime)
870 sprintf (logtext, "Exit status: %d\n", (int) status);
879 * These are local overrides for various environment variables in Emacs.
880 * Please do not remove this and leave it at the end of the file, where
881 * Emacs will automagically detect them.
882 * ---------------------------------------------------------------------
885 * indent-tabs-mode: t