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 Enthaelt die Funktion main() und die Variablen fuer die
9 Dieser Modul erledigt folgende Aufgaben:
10 - Bearbeiten der command-line-options
11 - Aufrufen aller Initialisierungsroutinen
12 - Aufrufen des Classloaders
13 - Starten der main - Methode
15 Authors: Reinhard Grafl EMAIL: cacao@complang.tuwien.ac.at
16 Changes: Andi Krall EMAIL: cacao@complang.tuwien.ac.at
17 Mark Probst EMAIL: cacao@complang.tuwien.ac.at
18 Philipp Tomsich EMAIL: cacao@complang.tuwien.ac.at
20 Last Change: $Id: cacao.c 136 1999-11-09 11:33:46Z schani $
22 *******************************************************************************/
37 #include "threads/thread.h"
39 bool compileall = false;
40 int newcompiler = true;
46 static bool showmethods = false;
47 static bool showconstantpool = false;
48 static bool showutf = false;
49 static classinfo *topclass;
52 void **stackbottom = 0;
56 /* internal function: get_opt *************************************************
58 decodes the next command line option
60 ******************************************************************************/
66 #define OPT_CLASSPATH 2
70 #define OPT_VERBOSE1 6
72 #define OPT_VERBOSEGC 8
73 #define OPT_VERBOSECALL 9
75 #define OPT_SOFTNULL 11
82 #define OPT_SIGNATURE 18
94 struct {char *name; bool arg; int value;} opts[] = {
95 {"classpath", true, OPT_CLASSPATH},
99 {"noasyncgc", false, OPT_IGNORE},
100 {"noverify", false, OPT_IGNORE},
101 {"oss", true, OPT_IGNORE},
102 {"ss", true, OPT_IGNORE},
103 {"v", false, OPT_VERBOSE1},
104 {"verbose", false, OPT_VERBOSE},
105 {"verbosegc", false, OPT_VERBOSEGC},
106 {"verbosecall", false, OPT_VERBOSECALL},
107 {"ieee", false, OPT_IEEE},
108 {"softnull", false, OPT_SOFTNULL},
109 {"time", false, OPT_TIME},
110 {"stat", false, OPT_STAT},
111 {"log", true, OPT_LOG},
112 {"c", true, OPT_CHECK},
113 {"l", false, OPT_LOAD},
114 {"m", true, OPT_METHOD},
115 {"sig", true, OPT_SIGNATURE},
116 {"s", true, OPT_SHOW},
117 {"all", false, OPT_ALL},
119 {"old", false, OPT_OLD},
122 {"gc1", false, OPT_GC1},
123 {"gc2", false, OPT_GC2},
125 {"oloop", false, OPT_OLOOP},
129 static int opt_ind = 1;
130 static char *opt_arg;
132 static int get_opt (int argc, char **argv)
137 if (opt_ind >= argc) return OPT_DONE;
140 if (a[0] != '-') return OPT_DONE;
142 for (i=0; opts[i].name; i++) {
144 if (strcmp(a+1, opts[i].name) == 0) { /* boolean option found */
146 return opts[i].value;
150 if (strcmp(a+1, opts[i].name) == 0) { /* parameter option found */
152 if (opt_ind < argc) {
153 opt_arg = argv[opt_ind];
155 return opts[i].value;
160 size_t l = strlen(opts[i].name);
161 if (strlen(a+1) > l) {
162 if (memcmp (a+1, opts[i].name, l)==0) {
165 return opts[i].value;
178 /******************** interne Funktion: print_usage ************************
180 Gibt die richtige Aufrufsyntax des JavaVM-Compilers auf stdout aus.
182 ***************************************************************************/
184 static void print_usage()
186 printf ("USAGE: cacao [options] classname [program arguments\n");
187 printf ("Options:\n");
188 printf (" -classpath path ...... specify a path to look for classes\n");
189 printf (" -Dpropertyname=value . add an entry to the property list\n");
190 printf (" -mx maxmem[k|m] ...... specify the size for the heap\n");
191 printf (" -ms initmem[k|m] ..... specify the initial size for the heap\n");
192 printf (" -v ................... write state-information\n");
193 printf (" -verbose ............. write more information\n");
194 printf (" -verbosegc ........... write message for each GC\n");
195 printf (" -verbosecall ......... write message for each call\n");
196 printf (" -ieee ................ use ieee compliant arithmetic\n");
197 printf (" -softnull ............ use software nullpointer check\n");
198 printf (" -time ................ measure the runtime\n");
199 printf (" -stat ................ detailed compiler statistics\n");
200 printf (" -log logfile ......... specify a name for the logfile\n");
201 printf (" -c(heck)b(ounds) ..... don't check array bounds\n");
202 printf (" s(ync) ....... don't check for synchronization\n");
203 printf (" -oloop ............... optimize array accesses in loops\n");
204 printf (" -l ................... don't start the class after loading\n");
205 printf (" -all ................. compile all methods, no execution\n");
207 printf (" -old ................. use old JIT compiler\n");
210 printf (" -gc1 ................. use the old garbage collector (default)\n");
211 printf (" -gc2 ................. use the new garbage collector\n");
213 printf (" -m ................... compile only a specific method\n");
214 printf (" -sig ................. specify signature for a specific method\n");
215 printf (" -s(how)a(ssembler) ... show disassembled listing\n");
216 printf (" c(onstants) ... show the constant pool\n");
217 printf (" d(atasegment).. show data segment listing\n");
218 printf (" i(ntermediate). show intermediate representation\n");
219 printf (" m(ethods)...... show class fields and methods\n");
221 printf (" s(tack) ....... show stack for every javaVM-command\n");
223 printf (" u(tf) ......... show the utf - hash\n");
228 /***************************** Funktion: print_times *********************
230 gibt eine Aufstellung der verwendeten CPU-Zeit aus
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 /***************************** Funktion: 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 Alpha-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 loaded Methods: %d\n\n", count_all_methods);
418 sprintf (logtext, "Calls of utf_new: %22d", count_utf_new);
420 sprintf (logtext, "Calls of utf_new (element found): %6d\n\n", count_utf_new_found);
425 /********** Funktion: class_compile_methods (nur f"ur Debug-Zwecke) ********/
427 void class_compile_methods ()
433 c = list_first (&linkedclasses);
435 for (i = 0; i < c -> methodscount; i++) {
436 m = &(c->methods[i]);
441 (void) jit_compile(m);
444 (void) compiler_compile(m);
448 c = list_next (&linkedclasses, c);
453 * void exit_handler(void)
454 * -----------------------
455 * The exit_handler function is called upon program termination to shutdown
456 * the various subsystems and release the resources allocated to the VM.
459 void exit_handler(void)
461 /********************* Debug-Tabellen ausgeben ************************/
463 if (showmethods) class_showmethods (topclass);
464 if (showconstantpool) class_showconstantpool (topclass);
465 if (showutf) utf_show ();
468 clear_thread_flags(); /* restores standard file descriptor
472 /************************ Freigeben aller Resourcen *******************/
474 heap_close (); /* must be called before compiler_close and
475 loader_close because finalization occurs
482 tables_close ( literalstring_free );
484 if (verbose || getcompilingtime || statistics) {
485 log_text ("CACAO terminated");
488 if (getcompilingtime)
494 /************************** Funktion: main *******************************
497 Wird vom System zu Programstart aufgerufen (eh klar).
499 **************************************************************************/
501 int main(int argc, char **argv)
505 java_objectheader *exceptionptr;
508 /********** interne (nur fuer main relevante Optionen) **************/
510 char logfilename[200] = "";
511 u4 heapsize = 16000000;
512 u4 heapstartsize = 200000;
513 char classpath[500] = ".:/usr/local/lib/java/classes";
515 char *specificmethodname = NULL;
516 char *specificsignature = NULL;
519 stackbottom = &dummy;
522 if (0 != atexit(exit_handler))
523 panic("unable to register exit_handler");
525 /************ Infos aus der Environment lesen ************************/
527 cp = getenv ("CLASSPATH");
529 strcpy (classpath, cp);
532 /***************** Interpretieren der Kommandozeile *****************/
537 while ( (i = get_opt(argc,argv)) != OPT_DONE) {
540 case OPT_IGNORE: break;
543 strcpy (classpath + strlen(classpath), ":");
544 strcpy (classpath + strlen(classpath), opt_arg);
549 int n,l=strlen(opt_arg);
550 for (n=0; n<l; n++) {
551 if (opt_arg[n]=='=') {
553 attach_property (opt_arg, opt_arg+n+1);
566 if (opt_arg[strlen(opt_arg)-1] == 'k') {
567 j = 1024 * atoi(opt_arg);
569 else if (opt_arg[strlen(opt_arg)-1] == 'm') {
570 j = 1024 * 1024 * atoi(opt_arg);
572 else j = atoi(opt_arg);
574 if (i==OPT_MX) heapsize = j;
575 else heapstartsize = j;
586 compileverbose = true;
590 collectverbose = true;
593 case OPT_VERBOSECALL:
606 getcompilingtime = true;
607 getloadingtime = true;
615 strcpy (logfilename, opt_arg);
620 for (j=0; j<strlen(opt_arg); j++) {
621 switch (opt_arg[j]) {
622 case 'b': checkbounds=false; break;
623 case 's': checksync=false; break;
624 default: print_usage();
632 makeinitializations = false;
637 specificmethodname = opt_arg;
638 makeinitializations = false;
642 specificsignature = opt_arg;
648 makeinitializations = false;
668 case OPT_SHOW: /* Anzeigeoptionen */
669 for (j=0; j<strlen(opt_arg); j++) {
670 switch (opt_arg[j]) {
671 case 'a': showdisassemble=true; compileverbose=true; break;
672 case 'c': showconstantpool=true; break;
673 case 'd': showddatasegment=true; break;
674 case 'i': showintermediate=true; compileverbose=true; break;
675 case 'm': showmethods=true; break;
677 case 's': showstack=true; compileverbose=true; break;
679 case 'u': showutf=true; break;
680 default: print_usage();
699 if (opt_ind >= argc) {
705 /**************************** Programmstart *****************************/
707 log_init (logfilename);
710 "CACAO started -------------------------------------------------------");
713 suck_init (classpath);
714 native_setclasspath (classpath);
717 heap_init(heapsize, heapstartsize, &dummy);
724 native_loadclasses ();
727 /*********************** JAVA-Klassen laden ***************************/
729 cp = argv[opt_ind++];
730 for (i=strlen(cp)-1; i>=0; i--) { /* Punkte im Klassennamen */
731 if (cp[i]=='.') cp[i]='/'; /* auf slashes umbauen */
734 topclass = loader_load ( utf_new_char (cp) );
739 initThreads((u1*)&dummy); /* schani */
742 /************************* Arbeitsroutinen starten ********************/
745 methodinfo *mainmethod;
748 heap_addreference((void**) &a);
750 mainmethod = class_findmethod (
752 utf_new_char ("main"),
753 utf_new_char ("([Ljava/lang/String;)V")
755 if (!mainmethod) panic ("Can not find method 'void main(String[])'");
756 if ((mainmethod->flags & ACC_STATIC) != ACC_STATIC) panic ("main is not static!");
758 a = builtin_anewarray (argc - opt_ind, class_java_lang_String);
759 for (i=opt_ind; i<argc; i++) {
760 a->data[i-opt_ind] = javastring_new (utf_new_char (argv[i]) );
762 exceptionptr = asm_calljavamethod (mainmethod, a, NULL,NULL,NULL );
765 printf ("#### Program has thrown: ");
766 utf_display (exceptionptr->vftbl->class->name);
771 killThread(currentThread);
773 fprintf(stderr, "still here\n");
776 /************* Auf Wunsch alle Methode "ubersetzen ********************/
779 class_compile_methods();
783 /******** Auf Wunsch eine spezielle Methode "ubersetzen ***************/
785 if (specificmethodname) {
787 if (specificsignature)
788 m = class_findmethod(topclass,
789 utf_new_char(specificmethodname),
790 utf_new_char(specificsignature));
792 m = class_findmethod(topclass,
793 utf_new_char(specificmethodname), NULL);
794 if (!m) panic ("Specific method not found");
798 (void) jit_compile(m);
801 (void) compiler_compile(m);
810 /************************************ SHUTDOWN-Funktion *********************************
812 Terminiert das System augenblicklich, ohne den Speicher
813 explizit freizugeben (eigentlich nur f"ur abnorme
814 Programmterminierung)
816 *****************************************************************************************/
818 void cacao_shutdown(s4 status)
820 if (verbose || getcompilingtime || statistics) {
821 log_text ("CACAO terminated by shutdown");
824 if (getcompilingtime)
827 sprintf (logtext, "Exit status: %d\n", (int) status);
836 * These are local overrides for various environment variables in Emacs.
837 * Please do not remove this and leave it at the end of the file, where
838 * Emacs will automagically detect them.
839 * ---------------------------------------------------------------------
842 * indent-tabs-mode: t