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 696 2003-12-06 20:10:05Z edwin $
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
112 #define OPT_VERBOSETC 29
115 struct {char *name; bool arg; int value;} opts[] = {
116 {"classpath", true, OPT_CLASSPATH},
118 {"ms", true, OPT_MS},
119 {"mx", true, OPT_MX},
120 {"noasyncgc", false, OPT_IGNORE},
121 {"noverify", false, OPT_IGNORE},
122 {"oss", true, OPT_IGNORE},
123 {"ss", true, OPT_IGNORE},
124 {"v", false, OPT_VERBOSE1},
125 {"verbose", false, OPT_VERBOSE},
126 {"verbosegc", false, OPT_VERBOSEGC},
127 {"verbosecall", false, OPT_VERBOSECALL},
128 #ifdef TYPECHECK_VERBOSE
129 {"verbosetc", false, OPT_VERBOSETC},
131 #if defined(__ALPHA__)
132 {"noieee", false, OPT_NOIEEE},
134 {"softnull", false, OPT_SOFTNULL},
135 {"time", false, OPT_TIME},
136 {"stat", false, OPT_STAT},
137 {"log", true, OPT_LOG},
138 {"c", true, OPT_CHECK},
139 {"l", false, OPT_LOAD},
140 {"m", true, OPT_METHOD},
141 {"sig", true, OPT_SIGNATURE},
142 {"s", true, OPT_SHOW},
143 {"all", false, OPT_ALL},
144 {"oloop", false, OPT_OLOOP},
145 {"i", true, OPT_INLINING},
146 {"rt", false, OPT_RT},
147 {"xta", false, OPT_XTA},
148 {"vta", false, OPT_VTA},
152 static int opt_ind = 1;
153 static char *opt_arg;
156 static int get_opt(int argc, char **argv)
161 if (opt_ind >= argc) return OPT_DONE;
164 if (a[0] != '-') return OPT_DONE;
166 for (i = 0; opts[i].name; i++) {
168 if (strcmp(a + 1, opts[i].name) == 0) { /* boolean option found */
170 return opts[i].value;
174 if (strcmp(a + 1, opts[i].name) == 0) { /* parameter option found */
176 if (opt_ind < argc) {
177 opt_arg = argv[opt_ind];
179 return opts[i].value;
184 size_t l = strlen(opts[i].name);
185 if (strlen(a + 1) > l) {
186 if (memcmp(a + 1, opts[i].name, l) == 0) {
189 return opts[i].value;
202 /******************** interne Function: print_usage ************************
204 Prints the correct usage syntax to stdout.
206 ***************************************************************************/
208 static void print_usage()
210 printf("USAGE: cacao [options] classname [program arguments]\n");
211 printf("Options:\n");
212 printf(" -classpath path ...... specify a path to look for classes\n");
213 printf(" -Dpropertyname=value . add an entry to the property list\n");
214 printf(" -mx maxmem[k|m] ...... specify the size for the heap\n");
215 printf(" -ms initmem[k|m] ..... specify the initial size for the heap\n");
216 printf(" -v ................... write state-information\n");
217 printf(" -verbose ............. write more information\n");
218 printf(" -verbosegc ........... write message for each GC\n");
219 printf(" -verbosecall ......... write message for each call\n");
220 #ifdef TYPECHECK_VERBOSE
221 printf(" -verbosetc ........... write debug messages while typechecking\n");
223 #if defined(__ALPHA__)
224 printf(" -noieee .............. don't use ieee compliant arithmetic\n");
226 printf(" -softnull ............ use software nullpointer check\n");
227 printf(" -time ................ measure the runtime\n");
228 printf(" -stat ................ detailed compiler statistics\n");
229 printf(" -log logfile ......... specify a name for the logfile\n");
230 printf(" -c(heck)b(ounds) ..... don't check array bounds\n");
231 printf(" s(ync) ....... don't check for synchronization\n");
232 printf(" -oloop ............... optimize array accesses in loops\n");
233 printf(" -l ................... don't start the class after loading\n");
234 printf(" -all ................. compile all methods, no execution\n");
235 printf(" -m ................... compile only a specific method\n");
236 printf(" -sig ................. specify signature for a specific method\n");
237 printf(" -s(how)a(ssembler) ... show disassembled listing\n");
238 printf(" c(onstants) ... show the constant pool\n");
239 printf(" d(atasegment).. show data segment listing\n");
240 printf(" i(ntermediate). show intermediate representation\n");
241 printf(" m(ethods)...... show class fields and methods\n");
242 printf(" u(tf) ......... show the utf - hash\n");
243 printf(" -i n ............. activate inlining\n");
244 printf(" v ............. inline virtual methods\n");
245 printf(" e ............. inline methods with exceptions\n");
246 printf(" p ............. optimize argument renaming\n");
247 printf(" o ............. inline methods of foreign classes\n");
248 printf(" -rt .................. use rapid type analysis\n");
249 printf(" -xta ................. use x type analysis\n");
250 printf(" -vta ................. use variable type analysis\n");
255 /***************************** Function: print_times *********************
257 Prints a summary of CPU time usage.
259 **************************************************************************/
261 static void print_times()
263 long int totaltime = getcputime();
264 long int runtime = totaltime - loadingtime - compilingtime;
265 char logtext[MAXLOGTEXT];
267 sprintf(logtext, "Time for loading classes: %ld secs, %ld millis",
268 loadingtime / 1000000, (loadingtime % 1000000) / 1000);
270 sprintf(logtext, "Time for compiling code: %ld secs, %ld millis",
271 compilingtime / 1000000, (compilingtime % 1000000) / 1000);
273 sprintf(logtext, "Time for running program: %ld secs, %ld millis",
274 runtime / 1000000, (runtime % 1000000) / 1000);
276 sprintf(logtext, "Total time: %ld secs, %ld millis",
277 totaltime / 1000000, (totaltime % 1000000) / 1000);
286 /***************************** Function: print_stats *********************
288 outputs detailed compiler statistics
290 **************************************************************************/
292 static void print_stats()
294 char logtext[MAXLOGTEXT];
296 sprintf(logtext, "Number of JitCompiler Calls: %d", count_jit_calls);
298 sprintf(logtext, "Number of compiled Methods: %d", count_methods);
300 sprintf(logtext, "Number of max basic blocks per method: %d", count_max_basic_blocks);
302 sprintf(logtext, "Number of compiled basic blocks: %d", count_basic_blocks);
304 sprintf(logtext, "Number of max JavaVM-Instructions per method: %d", count_max_javainstr);
306 sprintf(logtext, "Number of compiled JavaVM-Instructions: %d", count_javainstr);
308 sprintf(logtext, "Size of compiled JavaVM-Instructions: %d(%d)", count_javacodesize,
309 count_javacodesize - count_methods * 18);
311 sprintf(logtext, "Size of compiled Exception Tables: %d", count_javaexcsize);
313 sprintf(logtext, "Value of extended instruction set var: %d", has_ext_instr_set);
315 sprintf(logtext, "Number of Machine-Instructions: %d", count_code_len >> 2);
317 sprintf(logtext, "Number of Spills: %d", count_spills);
319 sprintf(logtext, "Number of Activ Pseudocommands: %5d", count_pcmd_activ);
321 sprintf(logtext, "Number of Drop Pseudocommands: %5d", count_pcmd_drop);
323 sprintf(logtext, "Number of Const Pseudocommands: %5d (zero:%5d)", count_pcmd_load, count_pcmd_zero);
325 sprintf(logtext, "Number of ConstAlu Pseudocommands: %5d (cmp: %5d, store:%5d)", count_pcmd_const_alu, count_pcmd_const_bra, count_pcmd_const_store);
327 sprintf(logtext, "Number of Move Pseudocommands: %5d", count_pcmd_move);
329 sprintf(logtext, "Number of Load Pseudocommands: %5d", count_load_instruction);
331 sprintf(logtext, "Number of Store Pseudocommands: %5d (combined: %5d)", count_pcmd_store, count_pcmd_store - count_pcmd_store_comb);
333 sprintf(logtext, "Number of OP Pseudocommands: %5d", count_pcmd_op);
335 sprintf(logtext, "Number of DUP Pseudocommands: %5d", count_dup_instruction);
337 sprintf(logtext, "Number of Mem Pseudocommands: %5d", count_pcmd_mem);
339 sprintf(logtext, "Number of Method Pseudocommands: %5d", count_pcmd_met);
341 sprintf(logtext, "Number of Branch Pseudocommands: %5d (rets:%5d, Xrets: %5d)",
342 count_pcmd_bra, count_pcmd_return, count_pcmd_returnx);
344 sprintf(logtext, "Number of Table Pseudocommands: %5d", count_pcmd_table);
346 sprintf(logtext, "Number of Useful Pseudocommands: %5d", count_pcmd_table +
347 count_pcmd_bra + count_pcmd_load + count_pcmd_mem + count_pcmd_op);
349 sprintf(logtext, "Number of Null Pointer Checks: %5d", count_check_null);
351 sprintf(logtext, "Number of Array Bound Checks: %5d", count_check_bound);
353 sprintf(logtext, "Number of Try-Blocks: %d", count_tryblocks);
355 sprintf(logtext, "Maximal count of stack elements: %d", count_max_new_stack);
357 sprintf(logtext, "Upper bound of max stack elements: %d", count_upper_bound_new_stack);
359 sprintf(logtext, "Distribution of stack sizes at block boundary");
361 sprintf(logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
363 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_block_stack[0],
364 count_block_stack[1], count_block_stack[2], count_block_stack[3], count_block_stack[4],
365 count_block_stack[5], count_block_stack[6], count_block_stack[7], count_block_stack[8],
366 count_block_stack[9], count_block_stack[10]);
368 sprintf(logtext, "Distribution of store stack depth");
370 sprintf(logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
372 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_depth[0],
373 count_store_depth[1], count_store_depth[2], count_store_depth[3], count_store_depth[4],
374 count_store_depth[5], count_store_depth[6], count_store_depth[7], count_store_depth[8],
375 count_store_depth[9], count_store_depth[10]);
377 sprintf(logtext, "Distribution of store creator chains first part");
379 sprintf(logtext, " 0 1 2 3 4 5 6 7 8 9 ");
381 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[0],
382 count_store_length[1], count_store_length[2], count_store_length[3], count_store_length[4],
383 count_store_length[5], count_store_length[6], count_store_length[7], count_store_length[8],
384 count_store_length[9]);
386 sprintf(logtext, "Distribution of store creator chains second part");
388 sprintf(logtext, " 10 11 12 13 14 15 16 17 18 19 >=20");
390 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[10],
391 count_store_length[11], count_store_length[12], count_store_length[13], count_store_length[14],
392 count_store_length[15], count_store_length[16], count_store_length[17], count_store_length[18],
393 count_store_length[19], count_store_length[20]);
395 sprintf(logtext, "Distribution of analysis iterations");
397 sprintf(logtext, " 1 2 3 4 >=5");
399 sprintf(logtext, "%5d%5d%5d%5d%5d", count_analyse_iterations[0], count_analyse_iterations[1],
400 count_analyse_iterations[2], count_analyse_iterations[3], count_analyse_iterations[4]);
402 sprintf(logtext, "Distribution of basic blocks per method");
404 sprintf(logtext, " <= 5 <=10 <=15 <=20 <=30 <=40 <=50 <=75 >75");
406 sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_method_bb_distribution[0],
407 count_method_bb_distribution[1], count_method_bb_distribution[2], count_method_bb_distribution[3],
408 count_method_bb_distribution[4], count_method_bb_distribution[5], count_method_bb_distribution[6],
409 count_method_bb_distribution[7], count_method_bb_distribution[8]);
411 sprintf(logtext, "Distribution of basic block sizes");
414 " 0 1 2 3 4 5 6 7 8 9 <13 <15 <17 <19 <21 <26 <31 >30");
416 sprintf(logtext, "%3d%5d%5d%5d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d",
417 count_block_size_distribution[0], count_block_size_distribution[1], count_block_size_distribution[2],
418 count_block_size_distribution[3], count_block_size_distribution[4], count_block_size_distribution[5],
419 count_block_size_distribution[6], count_block_size_distribution[7], count_block_size_distribution[8],
420 count_block_size_distribution[9], count_block_size_distribution[10], count_block_size_distribution[11],
421 count_block_size_distribution[12], count_block_size_distribution[13], count_block_size_distribution[14],
422 count_block_size_distribution[15], count_block_size_distribution[16], count_block_size_distribution[17]);
424 sprintf(logtext, "Size of Code Area (Kb): %10.3f", (float) count_code_len / 1024);
426 sprintf(logtext, "Size of data Area (Kb): %10.3f", (float) count_data_len / 1024);
428 sprintf(logtext, "Size of Class Infos (Kb):%10.3f", (float) (count_class_infos) / 1024);
430 sprintf(logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_utf_len) / 1024);
432 sprintf(logtext, "Size of Vftbl (Kb): %10.3f", (float) count_vftbl_len / 1024);
434 sprintf(logtext, "Size of comp stub (Kb): %10.3f", (float) count_cstub_len / 1024);
436 sprintf(logtext, "Size of native stub (Kb):%10.3f", (float) count_nstub_len / 1024);
438 sprintf(logtext, "Size of Utf (Kb): %10.3f", (float) count_utf_len / 1024);
440 sprintf(logtext, "Size of VMCode (Kb): %10.3f(%d)", (float) count_vmcode_len / 1024,
441 count_vmcode_len - 18 * count_all_methods);
443 sprintf(logtext, "Size of ExTable (Kb): %10.3f", (float) count_extable_len / 1024);
445 sprintf(logtext, "Number of class loads: %d", count_class_loads);
447 sprintf(logtext, "Number of class inits: %d", count_class_inits);
449 sprintf(logtext, "Number of loaded Methods: %d\n\n", count_all_methods);
452 sprintf(logtext, "Calls of utf_new: %22d", count_utf_new);
454 sprintf(logtext, "Calls of utf_new (element found): %6d\n\n", count_utf_new_found);
460 /********** Function: class_compile_methods (debugging only) ********/
462 void class_compile_methods ()
468 c = list_first (&linkedclasses);
470 for (i = 0; i < c -> methodscount; i++) {
471 m = &(c->methods[i]);
473 (void) jit_compile(m);
476 c = list_next (&linkedclasses, c);
483 * void exit_handler(void)
484 * -----------------------
485 * The exit_handler function is called upon program termination to shutdown
486 * the various subsystems and release the resources allocated to the VM.
488 void exit_handler(void)
490 /********************* Print debug tables ************************/
492 if (showmethods) class_showmethods(topclass);
493 if (showconstantpool) class_showconstantpool(topclass);
494 if (showutf) utf_show();
497 clear_thread_flags(); /* restores standard file descriptor
501 /************************ Free all resources *******************/
503 heap_close(); /* must be called before compiler_close and
504 loader_close because finalization occurs
508 tables_close(literalstring_free);
510 if (verbose || getcompilingtime || statistics) {
511 log_text ("CACAO terminated");
514 if (getcompilingtime)
522 /************************** Function: main *******************************
526 **************************************************************************/
528 int main(int argc, char **argv)
532 java_objectheader *local_exceptionptr = 0;
535 /********** interne (nur fuer main relevante Optionen) **************/
537 char logfilename[200] = "";
538 u4 heapsize = 64000000;
539 u4 heapstartsize = 200000;
540 char classpath[500] = ".:/usr/local/lib/java/classes";
542 char *specificmethodname = NULL;
543 char *specificsignature = NULL;
546 stackbottom = &dummy;
549 if (0 != atexit(exit_handler))
550 panic("unable to register exit_handler");
552 /************ Collect info from the environment ************************/
554 cp = getenv("CLASSPATH");
556 strcpy(classpath, cp);
559 /***************** Interpret the command line *****************/
564 while ((i = get_opt(argc, argv)) != OPT_DONE) {
566 case OPT_IGNORE: break;
569 strcpy(classpath + strlen(classpath), ":");
570 strcpy(classpath + strlen(classpath), opt_arg);
576 int l = strlen(opt_arg);
577 for (n = 0; n < l; n++) {
578 if (opt_arg[n]=='=') {
580 attach_property(opt_arg, opt_arg + n + 1);
593 if (opt_arg[strlen(opt_arg) - 1] == 'k') {
594 j = 1024 * atoi(opt_arg);
596 else if (opt_arg[strlen(opt_arg) - 1] == 'm') {
597 j = 1024 * 1024 * atoi(opt_arg);
599 else j = atoi(opt_arg);
601 if (i == OPT_MX) heapsize = j;
602 else heapstartsize = j;
613 compileverbose = true;
617 collectverbose = true;
620 #ifdef TYPECHECK_VERBOSE
622 typecheckverbose = true;
626 case OPT_VERBOSECALL:
639 getcompilingtime = true;
640 getloadingtime = true;
648 strcpy(logfilename, opt_arg);
652 for (j = 0; j < strlen(opt_arg); j++) {
653 switch (opt_arg[j]) {
669 makeinitializations = false;
674 specificmethodname = opt_arg;
675 makeinitializations = false;
679 specificsignature = opt_arg;
685 makeinitializations = false;
688 case OPT_SHOW: /* Display options */
689 for (j = 0; j < strlen(opt_arg); j++) {
690 switch (opt_arg[j]) {
692 showdisassemble = true;
696 showconstantpool = true;
699 showddatasegment = true;
702 showintermediate = true;
703 compileverbose = true;
723 for (j = 0; j < strlen(opt_arg); j++) {
724 switch (opt_arg[j]) {
729 inlinevirtuals = true;
732 inlineexceptions = true;
735 inlineparamopt = true;
738 inlineoutsiders = true;
752 opt_xta = false; /**not yet **/
756 /***opt_vta = true; not yet **/
766 if (opt_ind >= argc) {
772 /**************************** Program start *****************************/
774 log_init(logfilename);
776 log_text("CACAO started -------------------------------------------------------");
779 suck_init (classpath);
780 native_setclasspath (classpath);
783 heap_init(heapsize, heapstartsize, &dummy);
787 log_text("calling jit_init");
792 log_text("calling loader_init");
794 loader_init((u1*)&dummy);
796 log_text("calling native_loadclasses");
797 native_loadclasses ();
800 /*********************** Load JAVA classes ***************************/
802 cp = argv[opt_ind++];
803 for (i = strlen(cp) - 1; i >= 0; i--) { /* Transform dots into slashes */
804 if (cp[i] == '.') cp[i] = '/'; /* in the class name */
807 topclass = loader_load(utf_new_char(cp));
809 if (exceptionptr != 0) {
810 printf("#### Class loader has thrown: ");
811 utf_display(exceptionptr->vftbl->class->name);
818 printf("#### Could not find top class - exiting\n");
826 initThreads((u1*) &dummy); /* schani */
830 /************************* Start worker routines ********************/
833 methodinfo *mainmethod;
836 heap_addreference((void**) &a);
838 mainmethod = class_findmethod (
840 utf_new_char ("main"),
841 utf_new_char ("([Ljava/lang/String;)V")
843 if (!mainmethod) panic("Can not find method 'void main(String[])'");
844 if ((mainmethod->flags & ACC_STATIC) != ACC_STATIC) panic("main is not static!");
846 a = builtin_anewarray(argc - opt_ind, class_java_lang_String);
847 for (i = opt_ind; i < argc; i++) {
848 a->data[i - opt_ind] = javastring_new(utf_new_char(argv[i]));
851 #ifdef TYPEINFO_DEBUG_TEST
852 /* test the typeinfo system */
855 /*class_showmethods(currentThread->group->header.vftbl->class); */
857 local_exceptionptr = asm_calljavamethod (mainmethod, a, NULL, NULL, NULL );
859 if (local_exceptionptr) {
860 printf("Exception in thread \"main\" ");
861 utf_display(local_exceptionptr->vftbl->class->name);
867 killThread(currentThread);
869 fprintf(stderr, "still here\n");
872 /************* If requested, compile all methods ********************/
875 class_compile_methods();
879 /******** If requested, compile a specific method ***************/
881 if (specificmethodname) {
883 if (specificsignature)
884 m = class_findmethod(topclass,
885 utf_new_char(specificmethodname),
886 utf_new_char(specificsignature));
888 m = class_findmethod(topclass,
889 utf_new_char(specificmethodname), NULL);
890 if (!m) panic ("Specific method not found");
891 (void) jit_compile(m);
899 /************************************ Shutdown function *********************************
901 Terminates the system immediately without freeing memory explicitly (to be
902 used only for abnormal termination)
904 *****************************************************************************************/
906 void cacao_shutdown(s4 status)
910 if (verbose || getcompilingtime || statistics) {
911 log_text ("CACAO terminated by shutdown");
914 if (getcompilingtime)
917 dolog("Exit status: %d\n", (int) status);
925 * These are local overrides for various environment variables in Emacs.
926 * Please do not remove this and leave it at the end of the file, where
927 * Emacs will automagically detect them.
928 * ---------------------------------------------------------------------
931 * indent-tabs-mode: t