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 467 2003-09-26 01:55:25Z didi $
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
92 #define OPT_INLINING 25
97 struct {char *name; bool arg; int value;} opts[] = {
98 {"classpath", true, OPT_CLASSPATH},
100 {"ms", true, OPT_MS},
101 {"mx", true, OPT_MX},
102 {"noasyncgc", false, OPT_IGNORE},
103 {"noverify", false, OPT_IGNORE},
104 {"oss", true, OPT_IGNORE},
105 {"ss", true, OPT_IGNORE},
106 {"v", false, OPT_VERBOSE1},
107 {"verbose", false, OPT_VERBOSE},
108 {"verbosegc", false, OPT_VERBOSEGC},
109 {"verbosecall", false, OPT_VERBOSECALL},
110 {"ieee", false, OPT_IEEE},
111 {"softnull", false, OPT_SOFTNULL},
112 {"time", false, OPT_TIME},
113 {"stat", false, OPT_STAT},
114 {"log", true, OPT_LOG},
115 {"c", true, OPT_CHECK},
116 {"l", false, OPT_LOAD},
117 {"m", true, OPT_METHOD},
118 {"sig", true, OPT_SIGNATURE},
119 {"s", true, OPT_SHOW},
120 {"all", false, OPT_ALL},
122 {"old", false, OPT_OLD},
125 {"gc1", false, OPT_GC1},
126 {"gc2", false, OPT_GC2},
128 {"oloop", false, OPT_OLOOP},
129 {"i", true, OPT_INLINING},
130 {"rt", false, OPT_RT},
131 {"xta", false, OPT_XTA},
135 static int opt_ind = 1;
136 static char *opt_arg;
138 static int get_opt (int argc, char **argv)
143 if (opt_ind >= argc) return OPT_DONE;
146 if (a[0] != '-') return OPT_DONE;
148 for (i=0; opts[i].name; i++) {
150 if (strcmp(a+1, opts[i].name) == 0) { /* boolean option found */
152 return opts[i].value;
156 if (strcmp(a+1, opts[i].name) == 0) { /* parameter option found */
158 if (opt_ind < argc) {
159 opt_arg = argv[opt_ind];
161 return opts[i].value;
166 size_t l = strlen(opts[i].name);
167 if (strlen(a+1) > l) {
168 if (memcmp (a+1, opts[i].name, l)==0) {
171 return opts[i].value;
184 /******************** interne Function: print_usage ************************
186 Prints the correct usage syntax to stdout.
188 ***************************************************************************/
190 static void print_usage()
192 printf ("USAGE: cacao [options] classname [program arguments]\n");
193 printf ("Options:\n");
194 printf (" -classpath path ...... specify a path to look for classes\n");
195 printf (" -Dpropertyname=value . add an entry to the property list\n");
196 printf (" -mx maxmem[k|m] ...... specify the size for the heap\n");
197 printf (" -ms initmem[k|m] ..... specify the initial size for the heap\n");
198 printf (" -v ................... write state-information\n");
199 printf (" -verbose ............. write more information\n");
200 printf (" -verbosegc ........... write message for each GC\n");
201 printf (" -verbosecall ......... write message for each call\n");
202 printf (" -ieee ................ use ieee compliant arithmetic\n");
203 printf (" -softnull ............ use software nullpointer check\n");
204 printf (" -time ................ measure the runtime\n");
205 printf (" -stat ................ detailed compiler statistics\n");
206 printf (" -log logfile ......... specify a name for the logfile\n");
207 printf (" -c(heck)b(ounds) ..... don't check array bounds\n");
208 printf (" s(ync) ....... don't check for synchronization\n");
209 printf (" -oloop ............... optimize array accesses in loops\n");
210 printf (" -l ................... don't start the class after loading\n");
211 printf (" -all ................. compile all methods, no execution\n");
213 printf (" -old ................. use old JIT compiler\n");
216 printf (" -gc1 ................. use the old garbage collector (default)\n");
217 printf (" -gc2 ................. use the new garbage collector\n");
219 printf (" -m ................... compile only a specific method\n");
220 printf (" -sig ................. specify signature for a specific method\n");
221 printf (" -s(how)a(ssembler) ... show disassembled listing\n");
222 printf (" c(onstants) ... show the constant pool\n");
223 printf (" d(atasegment).. show data segment listing\n");
224 printf (" i(ntermediate). show intermediate representation\n");
225 printf (" m(ethods)...... show class fields and methods\n");
227 printf (" s(tack) ....... show stack for every javaVM-command\n");
229 printf (" u(tf) ......... show the utf - hash\n");
230 printf (" -i n ............. activate inlining\n");
231 printf (" v ............. inline virtual methods\n");
232 printf (" e ............. inline methods with exceptions\n");
233 printf (" p ............. optimize argument renaming\n");
234 printf (" o ............. inline methods of foreign classes\n");
235 printf (" -rt .................. use rapid type analysis\n");
236 printf (" -xta ................. use xta\n");
241 /***************************** Function: print_times *********************
243 Prints a summary of CPU time usage.
245 **************************************************************************/
247 static void print_times()
249 long int totaltime = getcputime();
250 long int runtime = totaltime - loadingtime - compilingtime;
252 sprintf (logtext, "Time for loading classes: %ld secs, %ld millis",
253 loadingtime / 1000000, (loadingtime % 1000000) / 1000);
255 sprintf (logtext, "Time for compiling code: %ld secs, %ld millis",
256 compilingtime / 1000000, (compilingtime % 1000000) / 1000);
258 sprintf (logtext, "Time for running program: %ld secs, %ld millis",
259 runtime / 1000000, (runtime % 1000000) / 1000);
261 sprintf (logtext, "Total time: %ld secs, %ld millis",
262 totaltime / 1000000, (totaltime % 1000000) / 1000);
271 /***************************** Function: print_stats *********************
273 outputs detailed compiler statistics
275 **************************************************************************/
277 static void print_stats()
279 sprintf (logtext, "Number of JitCompiler Calls: %d", count_jit_calls);
281 sprintf (logtext, "Number of compiled Methods: %d", count_methods);
283 sprintf (logtext, "Number of max basic blocks per method: %d", count_max_basic_blocks);
285 sprintf (logtext, "Number of compiled basic blocks: %d", count_basic_blocks);
287 sprintf (logtext, "Number of max JavaVM-Instructions per method: %d", count_max_javainstr);
289 sprintf (logtext, "Number of compiled JavaVM-Instructions: %d", count_javainstr);
291 sprintf (logtext, "Size of compiled JavaVM-Instructions: %d(%d)", count_javacodesize,
292 count_javacodesize - count_methods * 18);
294 sprintf (logtext, "Size of compiled Exception Tables: %d", count_javaexcsize);
296 sprintf (logtext, "Value of extended instruction set var: %d", has_ext_instr_set);
298 sprintf (logtext, "Number of Machine-Instructions: %d", count_code_len >> 2);
300 sprintf (logtext, "Number of Spills: %d", count_spills);
302 sprintf (logtext, "Number of Activ Pseudocommands: %5d", count_pcmd_activ);
304 sprintf (logtext, "Number of Drop Pseudocommands: %5d", count_pcmd_drop);
306 sprintf (logtext, "Number of Const Pseudocommands: %5d (zero:%5d)", count_pcmd_load, count_pcmd_zero);
308 sprintf (logtext, "Number of ConstAlu Pseudocommands: %5d (cmp: %5d, store:%5d)", count_pcmd_const_alu, count_pcmd_const_bra, count_pcmd_const_store);
310 sprintf (logtext, "Number of Move Pseudocommands: %5d", count_pcmd_move);
312 sprintf (logtext, "Number of Load Pseudocommands: %5d", count_load_instruction);
314 sprintf (logtext, "Number of Store Pseudocommands: %5d (combined: %5d)", count_pcmd_store, count_pcmd_store - count_pcmd_store_comb);
316 sprintf (logtext, "Number of OP Pseudocommands: %5d", count_pcmd_op);
318 sprintf (logtext, "Number of DUP Pseudocommands: %5d", count_dup_instruction);
320 sprintf (logtext, "Number of Mem Pseudocommands: %5d", count_pcmd_mem);
322 sprintf (logtext, "Number of Method Pseudocommands: %5d", count_pcmd_met);
324 sprintf (logtext, "Number of Branch Pseudocommands: %5d (rets:%5d, Xrets: %5d)",
325 count_pcmd_bra, count_pcmd_return, count_pcmd_returnx);
327 sprintf (logtext, "Number of Table Pseudocommands: %5d", count_pcmd_table);
329 sprintf (logtext, "Number of Useful Pseudocommands: %5d", count_pcmd_table +
330 count_pcmd_bra + count_pcmd_load + count_pcmd_mem + count_pcmd_op);
332 sprintf (logtext, "Number of Null Pointer Checks: %5d", count_check_null);
334 sprintf (logtext, "Number of Array Bound Checks: %5d", count_check_bound);
336 sprintf (logtext, "Number of Try-Blocks: %d", count_tryblocks);
338 sprintf (logtext, "Maximal count of stack elements: %d", count_max_new_stack);
340 sprintf (logtext, "Upper bound of max stack elements: %d", count_upper_bound_new_stack);
342 sprintf (logtext, "Distribution of stack sizes at block boundary");
344 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
346 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_block_stack[0],
347 count_block_stack[1],count_block_stack[2],count_block_stack[3],count_block_stack[4],
348 count_block_stack[5],count_block_stack[6],count_block_stack[7],count_block_stack[8],
349 count_block_stack[9],count_block_stack[10]);
351 sprintf (logtext, "Distribution of store stack depth");
353 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
355 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_depth[0],
356 count_store_depth[1],count_store_depth[2],count_store_depth[3],count_store_depth[4],
357 count_store_depth[5],count_store_depth[6],count_store_depth[7],count_store_depth[8],
358 count_store_depth[9],count_store_depth[10]);
360 sprintf (logtext, "Distribution of store creator chains first part");
362 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 ");
364 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[0],
365 count_store_length[1],count_store_length[2],count_store_length[3],count_store_length[4],
366 count_store_length[5],count_store_length[6],count_store_length[7],count_store_length[8],
367 count_store_length[9]);
369 sprintf (logtext, "Distribution of store creator chains second part");
371 sprintf (logtext, " 10 11 12 13 14 15 16 17 18 19 >=20");
373 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[10],
374 count_store_length[11],count_store_length[12],count_store_length[13],count_store_length[14],
375 count_store_length[15],count_store_length[16],count_store_length[17],count_store_length[18],
376 count_store_length[19],count_store_length[20]);
378 sprintf (logtext, "Distribution of analysis iterations");
380 sprintf (logtext, " 1 2 3 4 >=5");
382 sprintf (logtext, "%5d%5d%5d%5d%5d", count_analyse_iterations[0],count_analyse_iterations[1],
383 count_analyse_iterations[2],count_analyse_iterations[3],count_analyse_iterations[4]);
385 sprintf (logtext, "Distribution of basic blocks per method");
387 sprintf (logtext, " <= 5 <=10 <=15 <=20 <=30 <=40 <=50 <=75 >75");
389 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_method_bb_distribution[0],
390 count_method_bb_distribution[1],count_method_bb_distribution[2],count_method_bb_distribution[3],
391 count_method_bb_distribution[4],count_method_bb_distribution[5],count_method_bb_distribution[6],
392 count_method_bb_distribution[7],count_method_bb_distribution[8]);
394 sprintf (logtext, "Distribution of basic block sizes");
397 " 0 1 2 3 4 5 6 7 8 9 <13 <15 <17 <19 <21 <26 <31 >30");
399 sprintf (logtext, "%3d%5d%5d%5d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d",
400 count_block_size_distribution[0], count_block_size_distribution[1], count_block_size_distribution[2],
401 count_block_size_distribution[3], count_block_size_distribution[4], count_block_size_distribution[5],
402 count_block_size_distribution[6], count_block_size_distribution[7], count_block_size_distribution[8],
403 count_block_size_distribution[9], count_block_size_distribution[10],count_block_size_distribution[11],
404 count_block_size_distribution[12],count_block_size_distribution[13],count_block_size_distribution[14],
405 count_block_size_distribution[15],count_block_size_distribution[16],count_block_size_distribution[17]);
407 sprintf (logtext, "Size of Code Area (Kb): %10.3f", (float) count_code_len / 1024);
409 sprintf (logtext, "Size of data Area (Kb): %10.3f", (float) count_data_len / 1024);
411 sprintf (logtext, "Size of Class Infos (Kb):%10.3f", (float) (count_class_infos) / 1024);
413 sprintf (logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_utf_len) / 1024);
415 sprintf (logtext, "Size of Vftbl (Kb): %10.3f", (float) count_vftbl_len / 1024);
417 sprintf (logtext, "Size of comp stub (Kb): %10.3f", (float) count_cstub_len / 1024);
419 sprintf (logtext, "Size of native stub (Kb):%10.3f", (float) count_nstub_len / 1024);
421 sprintf (logtext, "Size of Utf (Kb): %10.3f", (float) count_utf_len / 1024);
423 sprintf (logtext, "Size of VMCode (Kb): %10.3f(%d)", (float) count_vmcode_len / 1024,
424 count_vmcode_len - 18 * count_all_methods);
426 sprintf (logtext, "Size of ExTable (Kb): %10.3f", (float) count_extable_len / 1024);
428 sprintf (logtext, "Number of class loads: %d", count_class_loads);
430 sprintf (logtext, "Number of class inits: %d", count_class_inits);
432 sprintf (logtext, "Number of loaded Methods: %d\n\n", count_all_methods);
435 sprintf (logtext, "Calls of utf_new: %22d", count_utf_new);
437 sprintf (logtext, "Calls of utf_new (element found): %6d\n\n", count_utf_new_found);
442 /********** Function: class_compile_methods (debugging only) ********/
444 void class_compile_methods ()
450 c = list_first (&linkedclasses);
452 for (i = 0; i < c -> methodscount; i++) {
453 m = &(c->methods[i]);
458 (void) jit_compile(m);
461 (void) compiler_compile(m);
465 c = list_next (&linkedclasses, c);
470 * void exit_handler(void)
471 * -----------------------
472 * The exit_handler function is called upon program termination to shutdown
473 * the various subsystems and release the resources allocated to the VM.
476 void exit_handler(void)
478 /********************* Print debug tables ************************/
480 if (showmethods) class_showmethods (topclass);
481 if (showconstantpool) class_showconstantpool (topclass);
482 if (showutf) utf_show ();
485 clear_thread_flags(); /* restores standard file descriptor
489 /************************ Free all resources *******************/
491 heap_close (); /* must be called before compiler_close and
492 loader_close because finalization occurs
499 tables_close ( literalstring_free );
501 if (verbose || getcompilingtime || statistics) {
502 log_text ("CACAO terminated");
505 if (getcompilingtime)
511 /************************** Function: main *******************************
515 **************************************************************************/
517 int main(int argc, char **argv)
521 java_objectheader *local_exceptionptr = 0;
524 /********** interne (nur fuer main relevante Optionen) **************/
526 char logfilename[200] = "";
527 u4 heapsize = 64000000;
528 u4 heapstartsize = 200000;
529 char classpath[500] = ".:/usr/local/lib/java/classes";
531 char *specificmethodname = NULL;
532 char *specificsignature = NULL;
535 stackbottom = &dummy;
538 if (0 != atexit(exit_handler))
539 panic("unable to register exit_handler");
541 /************ Collect info from the environment ************************/
543 cp = getenv ("CLASSPATH");
545 strcpy (classpath, cp);
548 /***************** Interpret the command line *****************/
553 while ( (i = get_opt(argc,argv)) != OPT_DONE) {
556 case OPT_IGNORE: break;
559 strcpy (classpath + strlen(classpath), ":");
560 strcpy (classpath + strlen(classpath), opt_arg);
565 int n,l=strlen(opt_arg);
566 for (n=0; n<l; n++) {
567 if (opt_arg[n]=='=') {
569 attach_property (opt_arg, opt_arg+n+1);
582 if (opt_arg[strlen(opt_arg)-1] == 'k') {
583 j = 1024 * atoi(opt_arg);
585 else if (opt_arg[strlen(opt_arg)-1] == 'm') {
586 j = 1024 * 1024 * atoi(opt_arg);
588 else j = atoi(opt_arg);
590 if (i==OPT_MX) heapsize = j;
591 else heapstartsize = j;
602 compileverbose = true;
606 collectverbose = true;
609 case OPT_VERBOSECALL:
622 getcompilingtime = true;
623 getloadingtime = true;
631 strcpy (logfilename, opt_arg);
636 for (j=0; j<strlen(opt_arg); j++) {
637 switch (opt_arg[j]) {
638 case 'b': checkbounds=false; break;
639 case 's': checksync=false; break;
640 default: print_usage();
648 makeinitializations = false;
653 specificmethodname = opt_arg;
654 makeinitializations = false;
658 specificsignature = opt_arg;
664 makeinitializations = false;
684 case OPT_SHOW: /* Display options */
685 for (j=0; j<strlen(opt_arg); j++) {
686 switch (opt_arg[j]) {
687 case 'a': showdisassemble=true; compileverbose=true; break;
688 case 'c': showconstantpool=true; break;
689 case 'd': showddatasegment=true; break;
690 case 'i': showintermediate=true; compileverbose=true; break;
691 case 'm': showmethods=true; break;
693 case 's': showstack=true; compileverbose=true; break;
695 case 'u': showutf=true; break;
696 default: print_usage();
708 for (j=0; j<strlen(opt_arg); j++) {
709 switch (opt_arg[j]) {
710 case 'n': useinlining = true; break;
711 case 'v': inlinevirtuals = true; break;
712 case 'e': inlineexceptions = true; break;
713 case 'p': inlineparamopt = true; break;
714 case 'o': inlineoutsiders = true; break;
715 default: print_usage();
739 if (opt_ind >= argc) {
745 /**************************** Program start *****************************/
747 log_init (logfilename);
750 "CACAO started -------------------------------------------------------");
753 suck_init (classpath);
754 native_setclasspath (classpath);
757 heap_init(heapsize, heapstartsize, &dummy);
764 native_loadclasses ();
767 /*********************** Load JAVA classes ***************************/
769 cp = argv[opt_ind++];
770 for (i=strlen(cp)-1; i>=0; i--) { /* Transform dots into slashes */
771 if (cp[i]=='.') cp[i]='/'; /* in the class name */
774 topclass = loader_load ( utf_new_char (cp) );
776 if (exceptionptr != 0)
778 printf ("#### Class loader has thrown: ");
779 utf_display (exceptionptr->vftbl->class->name);
787 printf("#### Could not find top class - exiting\n");
794 initThreads((u1*)&dummy); /* schani */
797 /************************* Start worker routines ********************/
800 methodinfo *mainmethod;
803 heap_addreference((void**) &a);
805 mainmethod = class_findmethod (
807 utf_new_char ("main"),
808 utf_new_char ("([Ljava/lang/String;)V")
810 if (!mainmethod) panic ("Can not find method 'void main(String[])'");
811 if ((mainmethod->flags & ACC_STATIC) != ACC_STATIC) panic ("main is not static!");
813 a = builtin_anewarray (argc - opt_ind, class_java_lang_String);
814 for (i=opt_ind; i<argc; i++) {
815 a->data[i-opt_ind] = javastring_new (utf_new_char (argv[i]) );
817 local_exceptionptr = asm_calljavamethod (mainmethod, a, NULL,NULL,NULL );
819 if (local_exceptionptr) {
820 printf ("#### Program has thrown: ");
821 utf_display (local_exceptionptr->vftbl->class->name);
824 /*RTAprint*/ if ((pCallgraph >= 1) && (opt_rt)) {
825 /*RTAprint*/ printCallgraph (); }
827 /*RTprint*/ if ((pClassHeir >= 1) && (opt_rt)) {
828 /*RTprint*/ printf("Last RTA Class Heirarchy -");
829 /*RTprint*/ printRThierarchyInfo(mainmethod);
831 /*RTprint*/ printObjectClassHeirarchy1( );
835 killThread(currentThread);
837 fprintf(stderr, "still here\n");
840 /************* If requested, compile all methods ********************/
843 class_compile_methods();
847 /******** If requested, compile a specific method ***************/
849 if (specificmethodname) {
851 if (specificsignature)
852 m = class_findmethod(topclass,
853 utf_new_char(specificmethodname),
854 utf_new_char(specificsignature));
856 m = class_findmethod(topclass,
857 utf_new_char(specificmethodname), NULL);
858 if (!m) panic ("Specific method not found");
862 (void) jit_compile(m);
865 (void) compiler_compile(m);
874 /************************************ Shutdown function *********************************
876 Terminates the system immediately without freeing memory explicitly (to be
877 used only for abnormal termination)
879 *****************************************************************************************/
881 void cacao_shutdown(s4 status)
883 /*RTAprint*/ if ((pCallgraph >= 1) && (opt_rt)) {
884 /*RTAprint*/ printCallgraph (NULL); }
886 /*RTprint*/ if ((pClassHeir >= 1) && (opt_rt)) {
887 /*RTprint*/ printf("Last RTA Class Heirarchy -");
888 /*RTprint*/ printRThierarchyInfo(NULL); }
890 if (verbose || getcompilingtime || statistics) {
891 log_text ("CACAO terminated by shutdown");
894 if (getcompilingtime)
897 sprintf (logtext, "Exit status: %d\n", (int) status);
906 * These are local overrides for various environment variables in Emacs.
907 * Please do not remove this and leave it at the end of the file, where
908 * Emacs will automagically detect them.
909 * ---------------------------------------------------------------------
912 * indent-tabs-mode: t