1 /* main.c - contains main() and variables for the global options
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 Institut f. Computersprachen, TU Wien
5 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser, M. Probst,
6 S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich,
9 This file is part of CACAO.
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2, or (at
14 your option) any later version.
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 Contact: cacao@complang.tuwien.ac.at
28 Authors: Reinhard Grafl
34 This module does the following tasks:
35 - Command line option handling
36 - Calling initialization routines
37 - Calling the class loader
38 - Running the main method
40 $Id: main.c 582 2003-11-09 19:09:11Z twisti $
56 #include "threads/thread.h"
57 #include "toolbox/loging.h"
58 #include "toolbox/memory.h"
59 #include "parseRTstats.h"
62 bool compileall = false;
64 bool runverbose = false;
65 bool collectverbose = false;
68 static bool showmethods = false;
69 static bool showconstantpool = false;
70 static bool showutf = false;
71 static classinfo *topclass;
74 void **stackbottom = 0;
78 /* internal function: get_opt *************************************************
80 decodes the next command line option
82 ******************************************************************************/
88 #define OPT_CLASSPATH 2
92 #define OPT_VERBOSE1 6
94 #define OPT_VERBOSEGC 8
95 #define OPT_VERBOSECALL 9
97 #define OPT_SOFTNULL 11
103 #define OPT_METHOD 17
104 #define OPT_SIGNATURE 18
108 #define OPT_INLINING 25
114 struct {char *name; bool arg; int value;} opts[] = {
115 {"classpath", true, OPT_CLASSPATH},
117 {"ms", true, OPT_MS},
118 {"mx", true, OPT_MX},
119 {"noasyncgc", false, OPT_IGNORE},
120 {"noverify", false, OPT_IGNORE},
121 {"oss", true, OPT_IGNORE},
122 {"ss", true, OPT_IGNORE},
123 {"v", false, OPT_VERBOSE1},
124 {"verbose", false, OPT_VERBOSE},
125 {"verbosegc", false, OPT_VERBOSEGC},
126 {"verbosecall", false, OPT_VERBOSECALL},
127 #if defined(__ALPHA__)
128 {"noieee", false, OPT_NOIEEE},
130 {"softnull", false, OPT_SOFTNULL},
131 {"time", false, OPT_TIME},
132 {"stat", false, OPT_STAT},
133 {"log", true, OPT_LOG},
134 {"c", true, OPT_CHECK},
135 {"l", false, OPT_LOAD},
136 {"m", true, OPT_METHOD},
137 {"sig", true, OPT_SIGNATURE},
138 {"s", true, OPT_SHOW},
139 {"all", false, OPT_ALL},
140 {"oloop", false, OPT_OLOOP},
141 {"i", true, OPT_INLINING},
142 {"rt", false, OPT_RT},
143 {"xta", false, OPT_XTA},
144 {"vta", false, OPT_VTA},
148 static int opt_ind = 1;
149 static char *opt_arg;
152 static int get_opt(int argc, char **argv)
157 if (opt_ind >= argc) return OPT_DONE;
160 if (a[0] != '-') return OPT_DONE;
162 for (i = 0; opts[i].name; i++) {
164 if (strcmp(a + 1, opts[i].name) == 0) { /* boolean option found */
166 return opts[i].value;
170 if (strcmp(a + 1, opts[i].name) == 0) { /* parameter option found */
172 if (opt_ind < argc) {
173 opt_arg = argv[opt_ind];
175 return opts[i].value;
180 size_t l = strlen(opts[i].name);
181 if (strlen(a + 1) > l) {
182 if (memcmp(a + 1, opts[i].name, l) == 0) {
185 return opts[i].value;
198 /******************** interne Function: print_usage ************************
200 Prints the correct usage syntax to stdout.
202 ***************************************************************************/
204 static void print_usage()
206 printf("USAGE: cacao [options] classname [program arguments]\n");
207 printf("Options:\n");
208 printf(" -classpath path ...... specify a path to look for classes\n");
209 printf(" -Dpropertyname=value . add an entry to the property list\n");
210 printf(" -mx maxmem[k|m] ...... specify the size for the heap\n");
211 printf(" -ms initmem[k|m] ..... specify the initial size for the heap\n");
212 printf(" -v ................... write state-information\n");
213 printf(" -verbose ............. write more information\n");
214 printf(" -verbosegc ........... write message for each GC\n");
215 printf(" -verbosecall ......... write message for each call\n");
216 #if defined(__ALPHA__)
217 printf(" -noieee .............. don't use ieee compliant arithmetic\n");
219 printf(" -softnull ............ use software nullpointer check\n");
220 printf(" -time ................ measure the runtime\n");
221 printf(" -stat ................ detailed compiler statistics\n");
222 printf(" -log logfile ......... specify a name for the logfile\n");
223 printf(" -c(heck)b(ounds) ..... don't check array bounds\n");
224 printf(" s(ync) ....... don't check for synchronization\n");
225 printf(" -oloop ............... optimize array accesses in loops\n");
226 printf(" -l ................... don't start the class after loading\n");
227 printf(" -all ................. compile all methods, no execution\n");
228 printf(" -m ................... compile only a specific method\n");
229 printf(" -sig ................. specify signature for a specific method\n");
230 printf(" -s(how)a(ssembler) ... show disassembled listing\n");
231 printf(" c(onstants) ... show the constant pool\n");
232 printf(" d(atasegment).. show data segment listing\n");
233 printf(" i(ntermediate). show intermediate representation\n");
234 printf(" m(ethods)...... show class fields and methods\n");
235 printf(" u(tf) ......... show the utf - hash\n");
236 printf(" -i n ............. activate inlining\n");
237 printf(" v ............. inline virtual methods\n");
238 printf(" e ............. inline methods with exceptions\n");
239 printf(" p ............. optimize argument renaming\n");
240 printf(" o ............. inline methods of foreign classes\n");
241 printf(" -rt .................. use rapid type analysis\n");
242 printf(" -xta ................. use x type analysis\n");
243 printf(" -vta ................. use variable type analysis\n");
248 /***************************** Function: print_times *********************
250 Prints a summary of CPU time usage.
252 **************************************************************************/
254 static void print_times()
256 long int totaltime = getcputime();
257 long int runtime = totaltime - loadingtime - compilingtime;
259 sprintf(logtext, "Time for loading classes: %ld secs, %ld millis",
260 loadingtime / 1000000, (loadingtime % 1000000) / 1000);
262 sprintf(logtext, "Time for compiling code: %ld secs, %ld millis",
263 compilingtime / 1000000, (compilingtime % 1000000) / 1000);
265 sprintf(logtext, "Time for running program: %ld secs, %ld millis",
266 runtime / 1000000, (runtime % 1000000) / 1000);
268 sprintf(logtext, "Total time: %ld secs, %ld millis",
269 totaltime / 1000000, (totaltime % 1000000) / 1000);
278 /***************************** Function: print_stats *********************
280 outputs detailed compiler statistics
282 **************************************************************************/
284 static void print_stats()
286 sprintf(logtext, "Number of JitCompiler Calls: %d", count_jit_calls);
288 sprintf(logtext, "Number of compiled Methods: %d", count_methods);
290 sprintf(logtext, "Number of max basic blocks per method: %d", count_max_basic_blocks);
292 sprintf(logtext, "Number of compiled basic blocks: %d", count_basic_blocks);
294 sprintf(logtext, "Number of max JavaVM-Instructions per method: %d", count_max_javainstr);
296 sprintf(logtext, "Number of compiled JavaVM-Instructions: %d", count_javainstr);
298 sprintf(logtext, "Size of compiled JavaVM-Instructions: %d(%d)", count_javacodesize,
299 count_javacodesize - count_methods * 18);
301 sprintf(logtext, "Size of compiled Exception Tables: %d", count_javaexcsize);
303 sprintf(logtext, "Value of extended instruction set var: %d", has_ext_instr_set);
305 sprintf(logtext, "Number of Machine-Instructions: %d", count_code_len >> 2);
307 sprintf(logtext, "Number of Spills: %d", count_spills);
309 sprintf(logtext, "Number of Activ Pseudocommands: %5d", count_pcmd_activ);
311 sprintf(logtext, "Number of Drop Pseudocommands: %5d", count_pcmd_drop);
313 sprintf(logtext, "Number of Const Pseudocommands: %5d (zero:%5d)", count_pcmd_load, count_pcmd_zero);
315 sprintf(logtext, "Number of ConstAlu Pseudocommands: %5d (cmp: %5d, store:%5d)", count_pcmd_const_alu, count_pcmd_const_bra, count_pcmd_const_store);
317 sprintf(logtext, "Number of Move Pseudocommands: %5d", count_pcmd_move);
319 sprintf(logtext, "Number of Load Pseudocommands: %5d", count_load_instruction);
321 sprintf(logtext, "Number of Store Pseudocommands: %5d (combined: %5d)", count_pcmd_store, count_pcmd_store - count_pcmd_store_comb);
323 sprintf(logtext, "Number of OP Pseudocommands: %5d", count_pcmd_op);
325 sprintf(logtext, "Number of DUP Pseudocommands: %5d", count_dup_instruction);
327 sprintf(logtext, "Number of Mem Pseudocommands: %5d", count_pcmd_mem);
329 sprintf(logtext, "Number of Method Pseudocommands: %5d", count_pcmd_met);
331 sprintf(logtext, "Number of Branch Pseudocommands: %5d (rets:%5d, Xrets: %5d)",
332 count_pcmd_bra, count_pcmd_return, count_pcmd_returnx);
334 sprintf(logtext, "Number of Table Pseudocommands: %5d", count_pcmd_table);
336 sprintf(logtext, "Number of Useful Pseudocommands: %5d", count_pcmd_table +
337 count_pcmd_bra + count_pcmd_load + count_pcmd_mem + count_pcmd_op);
339 sprintf(logtext, "Number of Null Pointer Checks: %5d", count_check_null);
341 sprintf(logtext, "Number of Array Bound Checks: %5d", count_check_bound);
343 sprintf(logtext, "Number of Try-Blocks: %d", count_tryblocks);
345 sprintf(logtext, "Maximal count of stack elements: %d", count_max_new_stack);
347 sprintf(logtext, "Upper bound of max stack elements: %d", count_upper_bound_new_stack);
349 sprintf(logtext, "Distribution of stack sizes at block boundary");
351 sprintf(logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
353 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_block_stack[0],
354 count_block_stack[1], count_block_stack[2], count_block_stack[3], count_block_stack[4],
355 count_block_stack[5], count_block_stack[6], count_block_stack[7], count_block_stack[8],
356 count_block_stack[9], count_block_stack[10]);
358 sprintf(logtext, "Distribution of store stack depth");
360 sprintf(logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
362 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_depth[0],
363 count_store_depth[1], count_store_depth[2], count_store_depth[3], count_store_depth[4],
364 count_store_depth[5], count_store_depth[6], count_store_depth[7], count_store_depth[8],
365 count_store_depth[9], count_store_depth[10]);
367 sprintf(logtext, "Distribution of store creator chains first part");
369 sprintf(logtext, " 0 1 2 3 4 5 6 7 8 9 ");
371 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[0],
372 count_store_length[1], count_store_length[2], count_store_length[3], count_store_length[4],
373 count_store_length[5], count_store_length[6], count_store_length[7], count_store_length[8],
374 count_store_length[9]);
376 sprintf(logtext, "Distribution of store creator chains second part");
378 sprintf(logtext, " 10 11 12 13 14 15 16 17 18 19 >=20");
380 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[10],
381 count_store_length[11], count_store_length[12], count_store_length[13], count_store_length[14],
382 count_store_length[15], count_store_length[16], count_store_length[17], count_store_length[18],
383 count_store_length[19], count_store_length[20]);
385 sprintf(logtext, "Distribution of analysis iterations");
387 sprintf(logtext, " 1 2 3 4 >=5");
389 sprintf(logtext, "%5d%5d%5d%5d%5d", count_analyse_iterations[0], count_analyse_iterations[1],
390 count_analyse_iterations[2], count_analyse_iterations[3], count_analyse_iterations[4]);
392 sprintf(logtext, "Distribution of basic blocks per method");
394 sprintf(logtext, " <= 5 <=10 <=15 <=20 <=30 <=40 <=50 <=75 >75");
396 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_method_bb_distribution[0],
397 count_method_bb_distribution[1], count_method_bb_distribution[2], count_method_bb_distribution[3],
398 count_method_bb_distribution[4], count_method_bb_distribution[5], count_method_bb_distribution[6],
399 count_method_bb_distribution[7], count_method_bb_distribution[8]);
401 sprintf(logtext, "Distribution of basic block sizes");
404 " 0 1 2 3 4 5 6 7 8 9 <13 <15 <17 <19 <21 <26 <31 >30");
406 sprintf(logtext, "%3d%5d%5d%5d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d",
407 count_block_size_distribution[0], count_block_size_distribution[1], count_block_size_distribution[2],
408 count_block_size_distribution[3], count_block_size_distribution[4], count_block_size_distribution[5],
409 count_block_size_distribution[6], count_block_size_distribution[7], count_block_size_distribution[8],
410 count_block_size_distribution[9], count_block_size_distribution[10], count_block_size_distribution[11],
411 count_block_size_distribution[12], count_block_size_distribution[13], count_block_size_distribution[14],
412 count_block_size_distribution[15], count_block_size_distribution[16], count_block_size_distribution[17]);
414 sprintf(logtext, "Size of Code Area (Kb): %10.3f", (float) count_code_len / 1024);
416 sprintf(logtext, "Size of data Area (Kb): %10.3f", (float) count_data_len / 1024);
418 sprintf(logtext, "Size of Class Infos (Kb):%10.3f", (float) (count_class_infos) / 1024);
420 sprintf(logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_utf_len) / 1024);
422 sprintf(logtext, "Size of Vftbl (Kb): %10.3f", (float) count_vftbl_len / 1024);
424 sprintf(logtext, "Size of comp stub (Kb): %10.3f", (float) count_cstub_len / 1024);
426 sprintf(logtext, "Size of native stub (Kb):%10.3f", (float) count_nstub_len / 1024);
428 sprintf(logtext, "Size of Utf (Kb): %10.3f", (float) count_utf_len / 1024);
430 sprintf(logtext, "Size of VMCode (Kb): %10.3f(%d)", (float) count_vmcode_len / 1024,
431 count_vmcode_len - 18 * count_all_methods);
433 sprintf(logtext, "Size of ExTable (Kb): %10.3f", (float) count_extable_len / 1024);
435 sprintf(logtext, "Number of class loads: %d", count_class_loads);
437 sprintf(logtext, "Number of class inits: %d", count_class_inits);
439 sprintf(logtext, "Number of loaded Methods: %d\n\n", count_all_methods);
442 sprintf(logtext, "Calls of utf_new: %22d", count_utf_new);
444 sprintf(logtext, "Calls of utf_new (element found): %6d\n\n", count_utf_new_found);
450 /********** Function: class_compile_methods (debugging only) ********/
452 void class_compile_methods ()
458 c = list_first (&linkedclasses);
460 for (i = 0; i < c -> methodscount; i++) {
461 m = &(c->methods[i]);
463 (void) jit_compile(m);
466 c = list_next (&linkedclasses, c);
473 * void exit_handler(void)
474 * -----------------------
475 * The exit_handler function is called upon program termination to shutdown
476 * the various subsystems and release the resources allocated to the VM.
478 void exit_handler(void)
480 /********************* Print debug tables ************************/
482 if (showmethods) class_showmethods(topclass);
483 if (showconstantpool) class_showconstantpool(topclass);
484 if (showutf) utf_show();
487 clear_thread_flags(); /* restores standard file descriptor
491 /************************ Free all resources *******************/
493 heap_close(); /* must be called before compiler_close and
494 loader_close because finalization occurs
498 tables_close(literalstring_free);
500 if (verbose || getcompilingtime || statistics) {
501 log_text ("CACAO terminated");
504 if (getcompilingtime)
512 /************************** Function: main *******************************
516 **************************************************************************/
518 int main(int argc, char **argv)
522 java_objectheader *local_exceptionptr = 0;
525 /********** interne (nur fuer main relevante Optionen) **************/
527 char logfilename[200] = "";
528 u4 heapsize = 64000000;
529 u4 heapstartsize = 200000;
530 char classpath[500] = ".:/usr/local/lib/java/classes";
532 char *specificmethodname = NULL;
533 char *specificsignature = NULL;
536 stackbottom = &dummy;
539 if (0 != atexit(exit_handler))
540 panic("unable to register exit_handler");
542 /************ Collect info from the environment ************************/
544 cp = getenv("CLASSPATH");
546 strcpy(classpath, cp);
549 /***************** Interpret the command line *****************/
554 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);
566 int l = strlen(opt_arg);
567 for (n = 0; n < l; n++) {
568 if (opt_arg[n]=='=') {
570 attach_property(opt_arg, opt_arg + n + 1);
583 if (opt_arg[strlen(opt_arg) - 1] == 'k') {
584 j = 1024 * atoi(opt_arg);
586 else if (opt_arg[strlen(opt_arg) - 1] == 'm') {
587 j = 1024 * 1024 * atoi(opt_arg);
589 else j = atoi(opt_arg);
591 if (i == OPT_MX) heapsize = j;
592 else heapstartsize = j;
603 compileverbose = true;
607 collectverbose = true;
610 case OPT_VERBOSECALL:
623 getcompilingtime = true;
624 getloadingtime = true;
632 strcpy(logfilename, opt_arg);
636 for (j = 0; j < strlen(opt_arg); j++) {
637 switch (opt_arg[j]) {
653 makeinitializations = false;
658 specificmethodname = opt_arg;
659 makeinitializations = false;
663 specificsignature = opt_arg;
669 makeinitializations = false;
672 case OPT_SHOW: /* Display options */
673 for (j = 0; j < strlen(opt_arg); j++) {
674 switch (opt_arg[j]) {
676 showdisassemble = true;
680 showconstantpool = true;
683 showddatasegment = true;
686 showintermediate = true;
687 compileverbose = true;
707 for (j = 0; j < strlen(opt_arg); j++) {
708 switch (opt_arg[j]) {
713 inlinevirtuals = true;
716 inlineexceptions = true;
719 inlineparamopt = true;
722 inlineoutsiders = true;
736 /***opt_xta = true; not yet **/
740 /***opt_vta = true; not yet **/
750 if (opt_ind >= argc) {
756 /**************************** Program start *****************************/
758 log_init(logfilename);
760 log_text("CACAO started -------------------------------------------------------");
763 suck_init(classpath);
764 native_setclasspath(classpath);
767 heap_init(heapsize, heapstartsize, &dummy);
771 native_loadclasses();
774 /*********************** Load JAVA classes ***************************/
776 cp = argv[opt_ind++];
777 for (i = strlen(cp) - 1; i >= 0; i--) { /* Transform dots into slashes */
778 if (cp[i] == '.') cp[i] = '/'; /* in the class name */
781 topclass = loader_load(utf_new_char(cp));
783 if (exceptionptr != 0) {
784 printf("#### Class loader has thrown: ");
785 utf_display(exceptionptr->vftbl->class->name);
792 printf("#### Could not find top class - exiting\n");
799 initThreads((u1*) &dummy); /* schani */
802 /************************* Start worker routines ********************/
805 methodinfo *mainmethod;
808 heap_addreference((void**) &a);
810 mainmethod = class_findmethod (
812 utf_new_char ("main"),
813 utf_new_char ("([Ljava/lang/String;)V")
815 if (!mainmethod) panic("Can not find method 'void main(String[])'");
816 if ((mainmethod->flags & ACC_STATIC) != ACC_STATIC) panic("main is not static!");
818 a = builtin_anewarray(argc - opt_ind, class_java_lang_String);
819 for (i = opt_ind; i < argc; i++) {
820 a->data[i - opt_ind] = javastring_new(utf_new_char(argv[i]));
822 local_exceptionptr = asm_calljavamethod(mainmethod, a, NULL, NULL, NULL);
824 if (local_exceptionptr) {
825 printf("Exception in thread \"main\" ");
826 utf_display(local_exceptionptr->vftbl->class->name);
829 /*RTAprint*/ if ((pCallgraph >= 1) && (opt_rt)) {
830 /*RTAprint*/ printCallgraph (); }
832 /*RTprint*/ if ((pClassHeir >= 1) && (opt_rt)) {
833 /*RTprint*/ printf("Last RTA Info -");
834 /*RTprint*/ printRThierarchyInfo(mainmethod);
836 /*RTprint*/ printObjectClassHeirarchy1( );
840 killThread(currentThread);
842 fprintf(stderr, "still here\n");
845 /************* If requested, compile all methods ********************/
848 class_compile_methods();
852 /******** If requested, compile a specific method ***************/
854 if (specificmethodname) {
856 if (specificsignature)
857 m = class_findmethod(topclass,
858 utf_new_char(specificmethodname),
859 utf_new_char(specificsignature));
861 m = class_findmethod(topclass,
862 utf_new_char(specificmethodname), NULL);
863 if (!m) panic ("Specific method not found");
864 (void) jit_compile(m);
872 /************************************ Shutdown function *********************************
874 Terminates the system immediately without freeing memory explicitly (to be
875 used only for abnormal termination)
877 *****************************************************************************************/
879 void cacao_shutdown(s4 status)
881 if ((pCallgraph >= 1) && (opt_rt)) {
882 printCallgraph(NULL);
885 if ((pClassHeir >= 1) && (opt_rt)) {
886 printf("RTA Information -");
887 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