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: cacao.c 689 2003-12-05 18:03:47Z stefan $
56 #include "threads/thread.h"
57 #include "toolbox/loging.h"
58 #include "toolbox/memory.h"
59 #include "parseRTstats.h"
60 #include "typeinfo.h" /* XXX remove debug */
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;
258 char logtext[MAXLOGTEXT];
260 sprintf(logtext, "Time for loading classes: %ld secs, %ld millis",
261 loadingtime / 1000000, (loadingtime % 1000000) / 1000);
263 sprintf(logtext, "Time for compiling code: %ld secs, %ld millis",
264 compilingtime / 1000000, (compilingtime % 1000000) / 1000);
266 sprintf(logtext, "Time for running program: %ld secs, %ld millis",
267 runtime / 1000000, (runtime % 1000000) / 1000);
269 sprintf(logtext, "Total time: %ld secs, %ld millis",
270 totaltime / 1000000, (totaltime % 1000000) / 1000);
279 /***************************** Function: print_stats *********************
281 outputs detailed compiler statistics
283 **************************************************************************/
285 static void print_stats()
287 char logtext[MAXLOGTEXT];
289 sprintf(logtext, "Number of JitCompiler Calls: %d", count_jit_calls);
291 sprintf(logtext, "Number of compiled Methods: %d", count_methods);
293 sprintf(logtext, "Number of max basic blocks per method: %d", count_max_basic_blocks);
295 sprintf(logtext, "Number of compiled basic blocks: %d", count_basic_blocks);
297 sprintf(logtext, "Number of max JavaVM-Instructions per method: %d", count_max_javainstr);
299 sprintf(logtext, "Number of compiled JavaVM-Instructions: %d", count_javainstr);
301 sprintf(logtext, "Size of compiled JavaVM-Instructions: %d(%d)", count_javacodesize,
302 count_javacodesize - count_methods * 18);
304 sprintf(logtext, "Size of compiled Exception Tables: %d", count_javaexcsize);
306 sprintf(logtext, "Value of extended instruction set var: %d", has_ext_instr_set);
308 sprintf(logtext, "Number of Machine-Instructions: %d", count_code_len >> 2);
310 sprintf(logtext, "Number of Spills: %d", count_spills);
312 sprintf(logtext, "Number of Activ Pseudocommands: %5d", count_pcmd_activ);
314 sprintf(logtext, "Number of Drop Pseudocommands: %5d", count_pcmd_drop);
316 sprintf(logtext, "Number of Const Pseudocommands: %5d (zero:%5d)", count_pcmd_load, count_pcmd_zero);
318 sprintf(logtext, "Number of ConstAlu Pseudocommands: %5d (cmp: %5d, store:%5d)", count_pcmd_const_alu, count_pcmd_const_bra, count_pcmd_const_store);
320 sprintf(logtext, "Number of Move Pseudocommands: %5d", count_pcmd_move);
322 sprintf(logtext, "Number of Load Pseudocommands: %5d", count_load_instruction);
324 sprintf(logtext, "Number of Store Pseudocommands: %5d (combined: %5d)", count_pcmd_store, count_pcmd_store - count_pcmd_store_comb);
326 sprintf(logtext, "Number of OP Pseudocommands: %5d", count_pcmd_op);
328 sprintf(logtext, "Number of DUP Pseudocommands: %5d", count_dup_instruction);
330 sprintf(logtext, "Number of Mem Pseudocommands: %5d", count_pcmd_mem);
332 sprintf(logtext, "Number of Method Pseudocommands: %5d", count_pcmd_met);
334 sprintf(logtext, "Number of Branch Pseudocommands: %5d (rets:%5d, Xrets: %5d)",
335 count_pcmd_bra, count_pcmd_return, count_pcmd_returnx);
337 sprintf(logtext, "Number of Table Pseudocommands: %5d", count_pcmd_table);
339 sprintf(logtext, "Number of Useful Pseudocommands: %5d", count_pcmd_table +
340 count_pcmd_bra + count_pcmd_load + count_pcmd_mem + count_pcmd_op);
342 sprintf(logtext, "Number of Null Pointer Checks: %5d", count_check_null);
344 sprintf(logtext, "Number of Array Bound Checks: %5d", count_check_bound);
346 sprintf(logtext, "Number of Try-Blocks: %d", count_tryblocks);
348 sprintf(logtext, "Maximal count of stack elements: %d", count_max_new_stack);
350 sprintf(logtext, "Upper bound of max stack elements: %d", count_upper_bound_new_stack);
352 sprintf(logtext, "Distribution of stack sizes at block boundary");
354 sprintf(logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
356 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_block_stack[0],
357 count_block_stack[1], count_block_stack[2], count_block_stack[3], count_block_stack[4],
358 count_block_stack[5], count_block_stack[6], count_block_stack[7], count_block_stack[8],
359 count_block_stack[9], count_block_stack[10]);
361 sprintf(logtext, "Distribution of store stack depth");
363 sprintf(logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
365 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_depth[0],
366 count_store_depth[1], count_store_depth[2], count_store_depth[3], count_store_depth[4],
367 count_store_depth[5], count_store_depth[6], count_store_depth[7], count_store_depth[8],
368 count_store_depth[9], count_store_depth[10]);
370 sprintf(logtext, "Distribution of store creator chains first part");
372 sprintf(logtext, " 0 1 2 3 4 5 6 7 8 9 ");
374 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[0],
375 count_store_length[1], count_store_length[2], count_store_length[3], count_store_length[4],
376 count_store_length[5], count_store_length[6], count_store_length[7], count_store_length[8],
377 count_store_length[9]);
379 sprintf(logtext, "Distribution of store creator chains second part");
381 sprintf(logtext, " 10 11 12 13 14 15 16 17 18 19 >=20");
383 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[10],
384 count_store_length[11], count_store_length[12], count_store_length[13], count_store_length[14],
385 count_store_length[15], count_store_length[16], count_store_length[17], count_store_length[18],
386 count_store_length[19], count_store_length[20]);
388 sprintf(logtext, "Distribution of analysis iterations");
390 sprintf(logtext, " 1 2 3 4 >=5");
392 sprintf(logtext, "%5d%5d%5d%5d%5d", count_analyse_iterations[0], count_analyse_iterations[1],
393 count_analyse_iterations[2], count_analyse_iterations[3], count_analyse_iterations[4]);
395 sprintf(logtext, "Distribution of basic blocks per method");
397 sprintf(logtext, " <= 5 <=10 <=15 <=20 <=30 <=40 <=50 <=75 >75");
399 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_method_bb_distribution[0],
400 count_method_bb_distribution[1], count_method_bb_distribution[2], count_method_bb_distribution[3],
401 count_method_bb_distribution[4], count_method_bb_distribution[5], count_method_bb_distribution[6],
402 count_method_bb_distribution[7], count_method_bb_distribution[8]);
404 sprintf(logtext, "Distribution of basic block sizes");
407 " 0 1 2 3 4 5 6 7 8 9 <13 <15 <17 <19 <21 <26 <31 >30");
409 sprintf(logtext, "%3d%5d%5d%5d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d",
410 count_block_size_distribution[0], count_block_size_distribution[1], count_block_size_distribution[2],
411 count_block_size_distribution[3], count_block_size_distribution[4], count_block_size_distribution[5],
412 count_block_size_distribution[6], count_block_size_distribution[7], count_block_size_distribution[8],
413 count_block_size_distribution[9], count_block_size_distribution[10], count_block_size_distribution[11],
414 count_block_size_distribution[12], count_block_size_distribution[13], count_block_size_distribution[14],
415 count_block_size_distribution[15], count_block_size_distribution[16], count_block_size_distribution[17]);
417 sprintf(logtext, "Size of Code Area (Kb): %10.3f", (float) count_code_len / 1024);
419 sprintf(logtext, "Size of data Area (Kb): %10.3f", (float) count_data_len / 1024);
421 sprintf(logtext, "Size of Class Infos (Kb):%10.3f", (float) (count_class_infos) / 1024);
423 sprintf(logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_utf_len) / 1024);
425 sprintf(logtext, "Size of Vftbl (Kb): %10.3f", (float) count_vftbl_len / 1024);
427 sprintf(logtext, "Size of comp stub (Kb): %10.3f", (float) count_cstub_len / 1024);
429 sprintf(logtext, "Size of native stub (Kb):%10.3f", (float) count_nstub_len / 1024);
431 sprintf(logtext, "Size of Utf (Kb): %10.3f", (float) count_utf_len / 1024);
433 sprintf(logtext, "Size of VMCode (Kb): %10.3f(%d)", (float) count_vmcode_len / 1024,
434 count_vmcode_len - 18 * count_all_methods);
436 sprintf(logtext, "Size of ExTable (Kb): %10.3f", (float) count_extable_len / 1024);
438 sprintf(logtext, "Number of class loads: %d", count_class_loads);
440 sprintf(logtext, "Number of class inits: %d", count_class_inits);
442 sprintf(logtext, "Number of loaded Methods: %d\n\n", count_all_methods);
445 sprintf(logtext, "Calls of utf_new: %22d", count_utf_new);
447 sprintf(logtext, "Calls of utf_new (element found): %6d\n\n", count_utf_new_found);
453 /********** Function: class_compile_methods (debugging only) ********/
455 void class_compile_methods ()
461 c = list_first (&linkedclasses);
463 for (i = 0; i < c -> methodscount; i++) {
464 m = &(c->methods[i]);
466 (void) jit_compile(m);
469 c = list_next (&linkedclasses, c);
476 * void exit_handler(void)
477 * -----------------------
478 * The exit_handler function is called upon program termination to shutdown
479 * the various subsystems and release the resources allocated to the VM.
481 void exit_handler(void)
483 /********************* Print debug tables ************************/
485 if (showmethods) class_showmethods(topclass);
486 if (showconstantpool) class_showconstantpool(topclass);
487 if (showutf) utf_show();
490 clear_thread_flags(); /* restores standard file descriptor
494 /************************ Free all resources *******************/
496 heap_close(); /* must be called before compiler_close and
497 loader_close because finalization occurs
501 tables_close(literalstring_free);
503 if (verbose || getcompilingtime || statistics) {
504 log_text ("CACAO terminated");
507 if (getcompilingtime)
515 /************************** Function: main *******************************
519 **************************************************************************/
521 int main(int argc, char **argv)
525 java_objectheader *local_exceptionptr = 0;
528 /********** interne (nur fuer main relevante Optionen) **************/
530 char logfilename[200] = "";
531 u4 heapsize = 64000000;
532 u4 heapstartsize = 200000;
533 char classpath[500] = ".:/usr/local/lib/java/classes";
535 char *specificmethodname = NULL;
536 char *specificsignature = NULL;
539 stackbottom = &dummy;
542 if (0 != atexit(exit_handler))
543 panic("unable to register exit_handler");
545 /************ Collect info from the environment ************************/
547 cp = getenv("CLASSPATH");
549 strcpy(classpath, cp);
552 /***************** Interpret the command line *****************/
557 while ((i = get_opt(argc, argv)) != OPT_DONE) {
559 case OPT_IGNORE: break;
562 strcpy(classpath + strlen(classpath), ":");
563 strcpy(classpath + strlen(classpath), opt_arg);
569 int l = strlen(opt_arg);
570 for (n = 0; n < l; n++) {
571 if (opt_arg[n]=='=') {
573 attach_property(opt_arg, opt_arg + n + 1);
586 if (opt_arg[strlen(opt_arg) - 1] == 'k') {
587 j = 1024 * atoi(opt_arg);
589 else if (opt_arg[strlen(opt_arg) - 1] == 'm') {
590 j = 1024 * 1024 * atoi(opt_arg);
592 else j = atoi(opt_arg);
594 if (i == OPT_MX) heapsize = j;
595 else heapstartsize = j;
606 compileverbose = true;
610 collectverbose = true;
613 case OPT_VERBOSECALL:
626 getcompilingtime = true;
627 getloadingtime = true;
635 strcpy(logfilename, opt_arg);
639 for (j = 0; j < strlen(opt_arg); j++) {
640 switch (opt_arg[j]) {
656 makeinitializations = false;
661 specificmethodname = opt_arg;
662 makeinitializations = false;
666 specificsignature = opt_arg;
672 makeinitializations = false;
675 case OPT_SHOW: /* Display options */
676 for (j = 0; j < strlen(opt_arg); j++) {
677 switch (opt_arg[j]) {
679 showdisassemble = true;
683 showconstantpool = true;
686 showddatasegment = true;
689 showintermediate = true;
690 compileverbose = true;
710 for (j = 0; j < strlen(opt_arg); j++) {
711 switch (opt_arg[j]) {
716 inlinevirtuals = true;
719 inlineexceptions = true;
722 inlineparamopt = true;
725 inlineoutsiders = true;
739 opt_xta = false; /**not yet **/
743 /***opt_vta = true; not yet **/
753 if (opt_ind >= argc) {
759 /**************************** Program start *****************************/
761 log_init(logfilename);
763 log_text("CACAO started -------------------------------------------------------");
766 suck_init (classpath);
767 native_setclasspath (classpath);
770 heap_init(heapsize, heapstartsize, &dummy);
774 log_text("calling jit_init");
779 log_text("calling loader_init");
781 loader_init((u1*)&dummy);
783 log_text("calling native_loadclasses");
784 native_loadclasses ();
787 /*********************** Load JAVA classes ***************************/
789 cp = argv[opt_ind++];
790 for (i = strlen(cp) - 1; i >= 0; i--) { /* Transform dots into slashes */
791 if (cp[i] == '.') cp[i] = '/'; /* in the class name */
794 topclass = loader_load(utf_new_char(cp));
796 if (exceptionptr != 0) {
797 printf("#### Class loader has thrown: ");
798 utf_display(exceptionptr->vftbl->class->name);
805 printf("#### Could not find top class - exiting\n");
813 initThreads((u1*) &dummy); /* schani */
817 /************************* Start worker routines ********************/
820 methodinfo *mainmethod;
823 heap_addreference((void**) &a);
825 mainmethod = class_findmethod (
827 utf_new_char ("main"),
828 utf_new_char ("([Ljava/lang/String;)V")
830 if (!mainmethod) panic("Can not find method 'void main(String[])'");
831 if ((mainmethod->flags & ACC_STATIC) != ACC_STATIC) panic("main is not static!");
833 a = builtin_anewarray(argc - opt_ind, class_java_lang_String);
834 for (i = opt_ind; i < argc; i++) {
835 a->data[i - opt_ind] = javastring_new(utf_new_char(argv[i]));
839 typeinfo_test(); /* XXX remove debug */
841 /*class_showmethods(currentThread->group->header.vftbl->class); */
843 local_exceptionptr = asm_calljavamethod (mainmethod, a, NULL, NULL, NULL );
845 if (local_exceptionptr) {
846 printf("Exception in thread \"main\" ");
847 utf_display(local_exceptionptr->vftbl->class->name);
853 killThread(currentThread);
855 fprintf(stderr, "still here\n");
858 /************* If requested, compile all methods ********************/
861 class_compile_methods();
865 /******** If requested, compile a specific method ***************/
867 if (specificmethodname) {
869 if (specificsignature)
870 m = class_findmethod(topclass,
871 utf_new_char(specificmethodname),
872 utf_new_char(specificsignature));
874 m = class_findmethod(topclass,
875 utf_new_char(specificmethodname), NULL);
876 if (!m) panic ("Specific method not found");
877 (void) jit_compile(m);
885 /************************************ Shutdown function *********************************
887 Terminates the system immediately without freeing memory explicitly (to be
888 used only for abnormal termination)
890 *****************************************************************************************/
892 void cacao_shutdown(s4 status)
896 if (verbose || getcompilingtime || statistics) {
897 log_text ("CACAO terminated by shutdown");
900 if (getcompilingtime)
903 dolog("Exit status: %d\n", (int) status);
911 * These are local overrides for various environment variables in Emacs.
912 * Please do not remove this and leave it at the end of the file, where
913 * Emacs will automagically detect them.
914 * ---------------------------------------------------------------------
917 * indent-tabs-mode: t