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 483 2003-10-14 17:08:38Z twisti $
21 *******************************************************************************/
33 #include "threads/thread.h"
35 bool compileall = false;
41 static bool showmethods = false;
42 static bool showconstantpool = false;
43 static bool showutf = false;
44 static classinfo *topclass;
47 void **stackbottom = 0;
51 /* internal function: get_opt *************************************************
53 decodes the next command line option
55 ******************************************************************************/
61 #define OPT_CLASSPATH 2
65 #define OPT_VERBOSE1 6
67 #define OPT_VERBOSEGC 8
68 #define OPT_VERBOSECALL 9
70 #define OPT_SOFTNULL 11
77 #define OPT_SIGNATURE 18
85 #define OPT_INLINING 25
91 struct {char *name; bool arg; int value;} opts[] = {
92 {"classpath", true, OPT_CLASSPATH},
96 {"noasyncgc", false, OPT_IGNORE},
97 {"noverify", false, OPT_IGNORE},
98 {"oss", true, OPT_IGNORE},
99 {"ss", true, OPT_IGNORE},
100 {"v", false, OPT_VERBOSE1},
101 {"verbose", false, OPT_VERBOSE},
102 {"verbosegc", false, OPT_VERBOSEGC},
103 {"verbosecall", false, OPT_VERBOSECALL},
104 {"ieee", false, OPT_IEEE},
105 {"softnull", false, OPT_SOFTNULL},
106 {"time", false, OPT_TIME},
107 {"stat", false, OPT_STAT},
108 {"log", true, OPT_LOG},
109 {"c", true, OPT_CHECK},
110 {"l", false, OPT_LOAD},
111 {"m", true, OPT_METHOD},
112 {"sig", true, OPT_SIGNATURE},
113 {"s", true, OPT_SHOW},
114 {"all", false, OPT_ALL},
116 {"gc1", false, OPT_GC1},
117 {"gc2", false, OPT_GC2},
119 {"oloop", false, OPT_OLOOP},
120 {"i", true, OPT_INLINING},
121 {"rt", false, OPT_RT},
122 {"xta", false, OPT_XTA},
123 {"vta", false, OPT_VTA},
127 static int opt_ind = 1;
128 static char *opt_arg;
130 static int get_opt (int argc, char **argv)
135 if (opt_ind >= argc) return OPT_DONE;
138 if (a[0] != '-') return OPT_DONE;
140 for (i=0; opts[i].name; i++) {
142 if (strcmp(a+1, opts[i].name) == 0) { /* boolean option found */
144 return opts[i].value;
148 if (strcmp(a+1, opts[i].name) == 0) { /* parameter option found */
150 if (opt_ind < argc) {
151 opt_arg = argv[opt_ind];
153 return opts[i].value;
158 size_t l = strlen(opts[i].name);
159 if (strlen(a+1) > l) {
160 if (memcmp (a+1, opts[i].name, l)==0) {
163 return opts[i].value;
176 /******************** interne Function: print_usage ************************
178 Prints the correct usage syntax to stdout.
180 ***************************************************************************/
182 static void print_usage()
184 printf ("USAGE: cacao [options] classname [program arguments]\n");
185 printf ("Options:\n");
186 printf (" -classpath path ...... specify a path to look for classes\n");
187 printf (" -Dpropertyname=value . add an entry to the property list\n");
188 printf (" -mx maxmem[k|m] ...... specify the size for the heap\n");
189 printf (" -ms initmem[k|m] ..... specify the initial size for the heap\n");
190 printf (" -v ................... write state-information\n");
191 printf (" -verbose ............. write more information\n");
192 printf (" -verbosegc ........... write message for each GC\n");
193 printf (" -verbosecall ......... write message for each call\n");
194 printf (" -ieee ................ use ieee compliant arithmetic\n");
195 printf (" -softnull ............ use software nullpointer check\n");
196 printf (" -time ................ measure the runtime\n");
197 printf (" -stat ................ detailed compiler statistics\n");
198 printf (" -log logfile ......... specify a name for the logfile\n");
199 printf (" -c(heck)b(ounds) ..... don't check array bounds\n");
200 printf (" s(ync) ....... don't check for synchronization\n");
201 printf (" -oloop ............... optimize array accesses in loops\n");
202 printf (" -l ................... don't start the class after loading\n");
203 printf (" -all ................. compile all methods, no execution\n");
205 printf (" -gc1 ................. use the old garbage collector (default)\n");
206 printf (" -gc2 ................. use the new garbage collector\n");
208 printf (" -m ................... compile only a specific method\n");
209 printf (" -sig ................. specify signature for a specific method\n");
210 printf (" -s(how)a(ssembler) ... show disassembled listing\n");
211 printf (" c(onstants) ... show the constant pool\n");
212 printf (" d(atasegment).. show data segment listing\n");
213 printf (" i(ntermediate). show intermediate representation\n");
214 printf (" m(ethods)...... show class fields and methods\n");
215 printf (" u(tf) ......... show the utf - hash\n");
216 printf (" -i n ............. activate inlining\n");
217 printf (" v ............. inline virtual methods\n");
218 printf (" e ............. inline methods with exceptions\n");
219 printf (" p ............. optimize argument renaming\n");
220 printf (" o ............. inline methods of foreign classes\n");
221 printf (" -rt .................. use rapid type analysis\n");
222 printf (" -xta ................. use x type analysis\n");
223 printf (" -vta ................. use variable type analysis\n");
228 /***************************** Function: print_times *********************
230 Prints a summary of CPU time usage.
232 **************************************************************************/
234 static void print_times()
236 long int totaltime = getcputime();
237 long int runtime = totaltime - loadingtime - compilingtime;
239 sprintf (logtext, "Time for loading classes: %ld secs, %ld millis",
240 loadingtime / 1000000, (loadingtime % 1000000) / 1000);
242 sprintf (logtext, "Time for compiling code: %ld secs, %ld millis",
243 compilingtime / 1000000, (compilingtime % 1000000) / 1000);
245 sprintf (logtext, "Time for running program: %ld secs, %ld millis",
246 runtime / 1000000, (runtime % 1000000) / 1000);
248 sprintf (logtext, "Total time: %ld secs, %ld millis",
249 totaltime / 1000000, (totaltime % 1000000) / 1000);
258 /***************************** Function: print_stats *********************
260 outputs detailed compiler statistics
262 **************************************************************************/
264 static void print_stats()
266 sprintf (logtext, "Number of JitCompiler Calls: %d", count_jit_calls);
268 sprintf (logtext, "Number of compiled Methods: %d", count_methods);
270 sprintf (logtext, "Number of max basic blocks per method: %d", count_max_basic_blocks);
272 sprintf (logtext, "Number of compiled basic blocks: %d", count_basic_blocks);
274 sprintf (logtext, "Number of max JavaVM-Instructions per method: %d", count_max_javainstr);
276 sprintf (logtext, "Number of compiled JavaVM-Instructions: %d", count_javainstr);
278 sprintf (logtext, "Size of compiled JavaVM-Instructions: %d(%d)", count_javacodesize,
279 count_javacodesize - count_methods * 18);
281 sprintf (logtext, "Size of compiled Exception Tables: %d", count_javaexcsize);
283 sprintf (logtext, "Value of extended instruction set var: %d", has_ext_instr_set);
285 sprintf (logtext, "Number of Machine-Instructions: %d", count_code_len >> 2);
287 sprintf (logtext, "Number of Spills: %d", count_spills);
289 sprintf (logtext, "Number of Activ Pseudocommands: %5d", count_pcmd_activ);
291 sprintf (logtext, "Number of Drop Pseudocommands: %5d", count_pcmd_drop);
293 sprintf (logtext, "Number of Const Pseudocommands: %5d (zero:%5d)", count_pcmd_load, count_pcmd_zero);
295 sprintf (logtext, "Number of ConstAlu Pseudocommands: %5d (cmp: %5d, store:%5d)", count_pcmd_const_alu, count_pcmd_const_bra, count_pcmd_const_store);
297 sprintf (logtext, "Number of Move Pseudocommands: %5d", count_pcmd_move);
299 sprintf (logtext, "Number of Load Pseudocommands: %5d", count_load_instruction);
301 sprintf (logtext, "Number of Store Pseudocommands: %5d (combined: %5d)", count_pcmd_store, count_pcmd_store - count_pcmd_store_comb);
303 sprintf (logtext, "Number of OP Pseudocommands: %5d", count_pcmd_op);
305 sprintf (logtext, "Number of DUP Pseudocommands: %5d", count_dup_instruction);
307 sprintf (logtext, "Number of Mem Pseudocommands: %5d", count_pcmd_mem);
309 sprintf (logtext, "Number of Method Pseudocommands: %5d", count_pcmd_met);
311 sprintf (logtext, "Number of Branch Pseudocommands: %5d (rets:%5d, Xrets: %5d)",
312 count_pcmd_bra, count_pcmd_return, count_pcmd_returnx);
314 sprintf (logtext, "Number of Table Pseudocommands: %5d", count_pcmd_table);
316 sprintf (logtext, "Number of Useful Pseudocommands: %5d", count_pcmd_table +
317 count_pcmd_bra + count_pcmd_load + count_pcmd_mem + count_pcmd_op);
319 sprintf (logtext, "Number of Null Pointer Checks: %5d", count_check_null);
321 sprintf (logtext, "Number of Array Bound Checks: %5d", count_check_bound);
323 sprintf (logtext, "Number of Try-Blocks: %d", count_tryblocks);
325 sprintf (logtext, "Maximal count of stack elements: %d", count_max_new_stack);
327 sprintf (logtext, "Upper bound of max stack elements: %d", count_upper_bound_new_stack);
329 sprintf (logtext, "Distribution of stack sizes at block boundary");
331 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
333 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_block_stack[0],
334 count_block_stack[1],count_block_stack[2],count_block_stack[3],count_block_stack[4],
335 count_block_stack[5],count_block_stack[6],count_block_stack[7],count_block_stack[8],
336 count_block_stack[9],count_block_stack[10]);
338 sprintf (logtext, "Distribution of store stack depth");
340 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
342 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_depth[0],
343 count_store_depth[1],count_store_depth[2],count_store_depth[3],count_store_depth[4],
344 count_store_depth[5],count_store_depth[6],count_store_depth[7],count_store_depth[8],
345 count_store_depth[9],count_store_depth[10]);
347 sprintf (logtext, "Distribution of store creator chains first part");
349 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 ");
351 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[0],
352 count_store_length[1],count_store_length[2],count_store_length[3],count_store_length[4],
353 count_store_length[5],count_store_length[6],count_store_length[7],count_store_length[8],
354 count_store_length[9]);
356 sprintf (logtext, "Distribution of store creator chains second part");
358 sprintf (logtext, " 10 11 12 13 14 15 16 17 18 19 >=20");
360 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[10],
361 count_store_length[11],count_store_length[12],count_store_length[13],count_store_length[14],
362 count_store_length[15],count_store_length[16],count_store_length[17],count_store_length[18],
363 count_store_length[19],count_store_length[20]);
365 sprintf (logtext, "Distribution of analysis iterations");
367 sprintf (logtext, " 1 2 3 4 >=5");
369 sprintf (logtext, "%5d%5d%5d%5d%5d", count_analyse_iterations[0],count_analyse_iterations[1],
370 count_analyse_iterations[2],count_analyse_iterations[3],count_analyse_iterations[4]);
372 sprintf (logtext, "Distribution of basic blocks per method");
374 sprintf (logtext, " <= 5 <=10 <=15 <=20 <=30 <=40 <=50 <=75 >75");
376 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_method_bb_distribution[0],
377 count_method_bb_distribution[1],count_method_bb_distribution[2],count_method_bb_distribution[3],
378 count_method_bb_distribution[4],count_method_bb_distribution[5],count_method_bb_distribution[6],
379 count_method_bb_distribution[7],count_method_bb_distribution[8]);
381 sprintf (logtext, "Distribution of basic block sizes");
384 " 0 1 2 3 4 5 6 7 8 9 <13 <15 <17 <19 <21 <26 <31 >30");
386 sprintf (logtext, "%3d%5d%5d%5d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d",
387 count_block_size_distribution[0], count_block_size_distribution[1], count_block_size_distribution[2],
388 count_block_size_distribution[3], count_block_size_distribution[4], count_block_size_distribution[5],
389 count_block_size_distribution[6], count_block_size_distribution[7], count_block_size_distribution[8],
390 count_block_size_distribution[9], count_block_size_distribution[10],count_block_size_distribution[11],
391 count_block_size_distribution[12],count_block_size_distribution[13],count_block_size_distribution[14],
392 count_block_size_distribution[15],count_block_size_distribution[16],count_block_size_distribution[17]);
394 sprintf (logtext, "Size of Code Area (Kb): %10.3f", (float) count_code_len / 1024);
396 sprintf (logtext, "Size of data Area (Kb): %10.3f", (float) count_data_len / 1024);
398 sprintf (logtext, "Size of Class Infos (Kb):%10.3f", (float) (count_class_infos) / 1024);
400 sprintf (logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_utf_len) / 1024);
402 sprintf (logtext, "Size of Vftbl (Kb): %10.3f", (float) count_vftbl_len / 1024);
404 sprintf (logtext, "Size of comp stub (Kb): %10.3f", (float) count_cstub_len / 1024);
406 sprintf (logtext, "Size of native stub (Kb):%10.3f", (float) count_nstub_len / 1024);
408 sprintf (logtext, "Size of Utf (Kb): %10.3f", (float) count_utf_len / 1024);
410 sprintf (logtext, "Size of VMCode (Kb): %10.3f(%d)", (float) count_vmcode_len / 1024,
411 count_vmcode_len - 18 * count_all_methods);
413 sprintf (logtext, "Size of ExTable (Kb): %10.3f", (float) count_extable_len / 1024);
415 sprintf (logtext, "Number of class loads: %d", count_class_loads);
417 sprintf (logtext, "Number of class inits: %d", count_class_inits);
419 sprintf (logtext, "Number of loaded Methods: %d\n\n", count_all_methods);
422 sprintf (logtext, "Calls of utf_new: %22d", count_utf_new);
424 sprintf (logtext, "Calls of utf_new (element found): %6d\n\n", count_utf_new_found);
429 /********** Function: class_compile_methods (debugging only) ********/
431 void class_compile_methods ()
437 c = list_first (&linkedclasses);
439 for (i = 0; i < c -> methodscount; i++) {
440 m = &(c->methods[i]);
442 (void) jit_compile(m);
445 c = list_next (&linkedclasses, c);
450 * void exit_handler(void)
451 * -----------------------
452 * The exit_handler function is called upon program termination to shutdown
453 * the various subsystems and release the resources allocated to the VM.
456 void exit_handler(void)
458 /********************* Print debug tables ************************/
460 if (showmethods) class_showmethods (topclass);
461 if (showconstantpool) class_showconstantpool (topclass);
462 if (showutf) utf_show ();
465 clear_thread_flags(); /* restores standard file descriptor
469 /************************ Free all resources *******************/
471 heap_close (); /* must be called before compiler_close and
472 loader_close because finalization occurs
476 tables_close ( literalstring_free );
478 if (verbose || getcompilingtime || statistics) {
479 log_text ("CACAO terminated");
482 if (getcompilingtime)
488 /************************** Function: main *******************************
492 **************************************************************************/
494 int main(int argc, char **argv)
498 java_objectheader *local_exceptionptr = 0;
501 /********** interne (nur fuer main relevante Optionen) **************/
503 char logfilename[200] = "";
504 u4 heapsize = 64000000;
505 u4 heapstartsize = 200000;
506 char classpath[500] = ".:/usr/local/lib/java/classes";
508 char *specificmethodname = NULL;
509 char *specificsignature = NULL;
512 stackbottom = &dummy;
515 if (0 != atexit(exit_handler))
516 panic("unable to register exit_handler");
518 /************ Collect info from the environment ************************/
520 cp = getenv ("CLASSPATH");
522 strcpy (classpath, cp);
525 /***************** Interpret the command line *****************/
530 while ( (i = get_opt(argc,argv)) != OPT_DONE) {
533 case OPT_IGNORE: break;
536 strcpy (classpath + strlen(classpath), ":");
537 strcpy (classpath + strlen(classpath), opt_arg);
542 int n,l=strlen(opt_arg);
543 for (n=0; n<l; n++) {
544 if (opt_arg[n]=='=') {
546 attach_property (opt_arg, opt_arg+n+1);
559 if (opt_arg[strlen(opt_arg)-1] == 'k') {
560 j = 1024 * atoi(opt_arg);
562 else if (opt_arg[strlen(opt_arg)-1] == 'm') {
563 j = 1024 * 1024 * atoi(opt_arg);
565 else j = atoi(opt_arg);
567 if (i==OPT_MX) heapsize = j;
568 else heapstartsize = j;
579 compileverbose = true;
583 collectverbose = true;
586 case OPT_VERBOSECALL:
599 getcompilingtime = true;
600 getloadingtime = true;
608 strcpy (logfilename, opt_arg);
613 for (j=0; j<strlen(opt_arg); j++) {
614 switch (opt_arg[j]) {
615 case 'b': checkbounds=false; break;
616 case 's': checksync=false; break;
617 default: print_usage();
625 makeinitializations = false;
630 specificmethodname = opt_arg;
631 makeinitializations = false;
635 specificsignature = opt_arg;
641 makeinitializations = false;
654 case OPT_SHOW: /* Display options */
655 for (j=0; j<strlen(opt_arg); j++) {
656 switch (opt_arg[j]) {
657 case 'a': showdisassemble=true; compileverbose=true; break;
658 case 'c': showconstantpool=true; break;
659 case 'd': showddatasegment=true; break;
660 case 'i': showintermediate=true; compileverbose=true; break;
661 case 'm': showmethods=true; break;
662 case 'u': showutf=true; break;
663 default: print_usage();
675 for (j=0; j<strlen(opt_arg); j++) {
676 switch (opt_arg[j]) {
677 case 'n': useinlining = true; break;
678 case 'v': inlinevirtuals = true; break;
679 case 'e': inlineexceptions = true; break;
680 case 'p': inlineparamopt = true; break;
681 case 'o': inlineoutsiders = true; break;
682 default: print_usage();
694 /***opt_xta = true; not yet **/
698 /***opt_vta = true; not yet **/
710 if (opt_ind >= argc) {
716 /**************************** Program start *****************************/
718 log_init (logfilename);
721 "CACAO started -------------------------------------------------------");
724 suck_init (classpath);
725 native_setclasspath (classpath);
728 heap_init(heapsize, heapstartsize, &dummy);
732 native_loadclasses ();
735 /*********************** Load JAVA classes ***************************/
737 cp = argv[opt_ind++];
738 for (i=strlen(cp)-1; i>=0; i--) { /* Transform dots into slashes */
739 if (cp[i]=='.') cp[i]='/'; /* in the class name */
742 topclass = loader_load ( utf_new_char (cp) );
744 if (exceptionptr != 0)
746 printf ("#### Class loader has thrown: ");
747 utf_display (exceptionptr->vftbl->class->name);
755 printf("#### Could not find top class - exiting\n");
762 initThreads((u1*)&dummy); /* schani */
765 /************************* Start worker routines ********************/
768 methodinfo *mainmethod;
771 heap_addreference((void**) &a);
773 mainmethod = class_findmethod (
775 utf_new_char ("main"),
776 utf_new_char ("([Ljava/lang/String;)V")
778 if (!mainmethod) panic ("Can not find method 'void main(String[])'");
779 if ((mainmethod->flags & ACC_STATIC) != ACC_STATIC) panic ("main is not static!");
781 a = builtin_anewarray (argc - opt_ind, class_java_lang_String);
782 for (i=opt_ind; i<argc; i++) {
783 a->data[i-opt_ind] = javastring_new (utf_new_char (argv[i]) );
785 local_exceptionptr = asm_calljavamethod (mainmethod, a, NULL,NULL,NULL );
787 if (local_exceptionptr) {
788 printf ("#### Program has thrown: ");
789 utf_display (local_exceptionptr->vftbl->class->name);
792 /*RTAprint*/ if ((pCallgraph >= 1) && (opt_rt)) {
793 /*RTAprint*/ printCallgraph (); }
795 /*RTprint*/ if ((pClassHeir >= 1) && (opt_rt)) {
796 /*RTprint*/ printf("Last RTA Info -");
797 /*RTprint*/ printRThierarchyInfo(mainmethod);
799 /*RTprint*/ printObjectClassHeirarchy1( );
803 killThread(currentThread);
805 fprintf(stderr, "still here\n");
808 /************* If requested, compile all methods ********************/
811 class_compile_methods();
815 /******** If requested, compile a specific method ***************/
817 if (specificmethodname) {
819 if (specificsignature)
820 m = class_findmethod(topclass,
821 utf_new_char(specificmethodname),
822 utf_new_char(specificsignature));
824 m = class_findmethod(topclass,
825 utf_new_char(specificmethodname), NULL);
826 if (!m) panic ("Specific method not found");
827 (void) jit_compile(m);
835 /************************************ Shutdown function *********************************
837 Terminates the system immediately without freeing memory explicitly (to be
838 used only for abnormal termination)
840 *****************************************************************************************/
842 void cacao_shutdown(s4 status)
844 /*RTAprint*/ if ((pCallgraph >= 1) && (opt_rt)) {
845 /*RTAprint*/ printCallgraph (NULL); }
847 /*RTprint*/ if ((pClassHeir >= 1) && (opt_rt)) {
848 /*RTprint*/ printf("RTA Information -");
849 /*RTprint*/ printRThierarchyInfo(NULL); }
851 if (verbose || getcompilingtime || statistics) {
852 log_text ("CACAO terminated by shutdown");
855 if (getcompilingtime)
858 sprintf (logtext, "Exit status: %d\n", (int) status);
867 * These are local overrides for various environment variables in Emacs.
868 * Please do not remove this and leave it at the end of the file, where
869 * Emacs will automagically detect them.
870 * ---------------------------------------------------------------------
873 * indent-tabs-mode: t