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
19 Last Change: 1997/10/29
21 *******************************************************************************/
36 #include "threads/thread.h"
38 bool compileall = false;
39 int newcompiler = true;
45 static bool showmethods = false;
46 static bool showconstantpool = false;
47 static bool showunicode = false;
48 static classinfo *topclass;
51 void **stackbottom = 0;
55 /* internal function: get_opt *************************************************
57 decodes the next command line option
59 ******************************************************************************/
65 #define OPT_CLASSPATH 2
69 #define OPT_VERBOSE1 6
71 #define OPT_VERBOSEGC 8
72 #define OPT_VERBOSECALL 9
74 #define OPT_SOFTNULL 11
81 #define OPT_SIGNATURE 18
92 struct {char *name; bool arg; int value;} opts[] = {
93 {"classpath", true, OPT_CLASSPATH},
97 {"noasyncgc", false, OPT_IGNORE},
98 {"noverify", false, OPT_IGNORE},
99 {"oss", true, OPT_IGNORE},
100 {"ss", true, OPT_IGNORE},
101 {"v", false, OPT_VERBOSE1},
102 {"verbose", false, OPT_VERBOSE},
103 {"verbosegc", false, OPT_VERBOSEGC},
104 {"verbosecall", false, OPT_VERBOSECALL},
105 {"ieee", false, OPT_IEEE},
106 {"softnull", false, OPT_SOFTNULL},
107 {"time", false, OPT_TIME},
108 {"stat", false, OPT_STAT},
109 {"log", true, OPT_LOG},
110 {"c", true, OPT_CHECK},
111 {"l", false, OPT_LOAD},
112 {"m", true, OPT_METHOD},
113 {"sig", true, OPT_SIGNATURE},
114 {"s", true, OPT_SHOW},
115 {"all", false, OPT_ALL},
117 {"old", false, OPT_OLD},
120 {"gc1", false, OPT_GC1},
121 {"gc2", false, OPT_GC2},
126 static int opt_ind = 1;
127 static char *opt_arg;
129 static int get_opt (int argc, char **argv)
134 if (opt_ind >= argc) return OPT_DONE;
137 if (a[0] != '-') return OPT_DONE;
139 for (i=0; opts[i].name; i++) {
141 if (strcmp(a+1, opts[i].name) == 0) { /* boolean option found */
143 return opts[i].value;
147 if (strcmp(a+1, opts[i].name) == 0) { /* parameter option found */
149 if (opt_ind < argc) {
150 opt_arg = argv[opt_ind];
152 return opts[i].value;
157 size_t l = strlen(opts[i].name);
158 if (strlen(a+1) > l) {
159 if (memcmp (a+1, opts[i].name, l)==0) {
162 return opts[i].value;
175 /******************** interne Funktion: print_usage ************************
177 Gibt die richtige Aufrufsyntax des JavaVM-Compilers auf stdout aus.
179 ***************************************************************************/
181 static void print_usage()
183 printf ("USAGE: cacao [options] classname [program arguments\n");
184 printf ("Options:\n");
185 printf (" -classpath path ...... specify a path to look for classes\n");
186 printf (" -Dpropertyname=value . add an entry to the property list\n");
187 printf (" -mx maxmem[k|m] ...... specify the size for the heap\n");
188 printf (" -ms initmem[k|m] ..... specify the initial size for the heap\n");
189 printf (" -v ................... write state-information\n");
190 printf (" -verbose ............. write more information\n");
191 printf (" -verbosegc ........... write message for each GC\n");
192 printf (" -verbosecall ......... write message for each call\n");
193 printf (" -ieee ................ use ieee compliant arithmetic\n");
194 printf (" -softnull ............ use software nullpointer check\n");
195 printf (" -time ................ measure the runtime\n");
196 printf (" -stat ................ detailed compiler statistics\n");
197 printf (" -log logfile ......... specify a name for the logfile\n");
198 printf (" -c(heck)b(ounds) ..... don't check array bounds\n");
199 printf (" s(ync) ....... don't check for synchronization\n");
200 printf (" -l ................... don't start the class after loading\n");
201 printf (" -all ................. compile all methods, no execution\n");
203 printf (" -old ................. use old JIT compiler\n");
206 printf (" -gc1 ................. use the old garbage collector (default)\n");
207 printf (" -gc2 ................. use the new garbage collector\n");
209 printf (" -m ................... compile only a specific method\n");
210 printf (" -sig ................. specify signature for a specific method\n");
211 printf (" -s(how)a(ssembler) ... show disassembled listing\n");
212 printf (" c(onstants) ... show the constant pool\n");
213 printf (" d(atasegment).. show data segment listing\n");
214 printf (" i(ntermediate). show intermediate representation\n");
215 printf (" m(ethods)...... show class fields and methods\n");
217 printf (" s(tack) ....... show stack for every javaVM-command\n");
219 printf (" u(nicode) ..... show the unicode - hash\n");
224 /***************************** Funktion: print_times *********************
226 gibt eine Aufstellung der verwendeten CPU-Zeit aus
228 **************************************************************************/
230 static void print_times()
232 long int totaltime = getcputime();
233 long int runtime = totaltime - loadingtime - compilingtime;
235 sprintf (logtext, "Time for loading classes: %ld secs, %ld millis",
236 loadingtime / 1000000, (loadingtime % 1000000) / 1000);
238 sprintf (logtext, "Time for compiling code: %ld secs, %ld millis",
239 compilingtime / 1000000, (compilingtime % 1000000) / 1000);
241 sprintf (logtext, "Time for running program: %ld secs, %ld millis",
242 runtime / 1000000, (runtime % 1000000) / 1000);
244 sprintf (logtext, "Total time: %ld secs, %ld millis",
245 totaltime / 1000000, (totaltime % 1000000) / 1000);
254 /***************************** Funktion: print_stats *********************
256 outputs detailed compiler statistics
258 **************************************************************************/
260 static void print_stats()
262 sprintf (logtext, "Number of JitCompiler Calls: %d", count_jit_calls);
264 sprintf (logtext, "Number of compiled Methods: %d", count_methods);
266 sprintf (logtext, "Number of max basic blocks per method: %d", count_max_basic_blocks);
268 sprintf (logtext, "Number of compiled basic blocks: %d", count_basic_blocks);
270 sprintf (logtext, "Number of max JavaVM-Instructions per method: %d", count_max_javainstr);
272 sprintf (logtext, "Number of compiled JavaVM-Instructions: %d", count_javainstr);
274 sprintf (logtext, "Size of compiled JavaVM-Instructions: %d(%d)", count_javacodesize,
275 count_javacodesize - count_methods * 18);
277 sprintf (logtext, "Size of compiled Exception Tables: %d", count_javaexcsize);
279 sprintf (logtext, "Value of extended instruction set var: %d", has_ext_instr_set);
281 sprintf (logtext, "Number of Alpha-Instructions: %d", count_code_len >> 2);
283 sprintf (logtext, "Number of Spills: %d", count_spills);
285 sprintf (logtext, "Number of Activ Pseudocommands: %5d", count_pcmd_activ);
287 sprintf (logtext, "Number of Drop Pseudocommands: %5d", count_pcmd_drop);
289 sprintf (logtext, "Number of Const Pseudocommands: %5d (zero:%5d)", count_pcmd_load, count_pcmd_zero);
291 sprintf (logtext, "Number of ConstAlu Pseudocommands: %5d (cmp: %5d, store:%5d)", count_pcmd_const_alu, count_pcmd_const_bra, count_pcmd_const_store);
293 sprintf (logtext, "Number of Move Pseudocommands: %5d", count_pcmd_move);
295 sprintf (logtext, "Number of Load Pseudocommands: %5d", count_load_instruction);
297 sprintf (logtext, "Number of Store Pseudocommands: %5d (combined: %5d)", count_pcmd_store, count_pcmd_store - count_pcmd_store_comb);
299 sprintf (logtext, "Number of OP Pseudocommands: %5d", count_pcmd_op);
301 sprintf (logtext, "Number of DUP Pseudocommands: %5d", count_dup_instruction);
303 sprintf (logtext, "Number of Mem Pseudocommands: %5d", count_pcmd_mem);
305 sprintf (logtext, "Number of Method Pseudocommands: %5d", count_pcmd_met);
307 sprintf (logtext, "Number of Branch Pseudocommands: %5d (rets:%5d, Xrets: %5d)",
308 count_pcmd_bra, count_pcmd_return, count_pcmd_returnx);
310 sprintf (logtext, "Number of Table Pseudocommands: %5d", count_pcmd_table);
312 sprintf (logtext, "Number of Useful Pseudocommands: %5d", count_pcmd_table +
313 count_pcmd_bra + count_pcmd_load + count_pcmd_mem + count_pcmd_op);
315 sprintf (logtext, "Number of Null Pointer Checks: %5d", count_check_null);
317 sprintf (logtext, "Number of Array Bound Checks: %5d", count_check_bound);
319 sprintf (logtext, "Number of Try-Blocks: %d", count_tryblocks);
321 sprintf (logtext, "Maximal count of stack elements: %d", count_max_new_stack);
323 sprintf (logtext, "Upper bound of max stack elements: %d", count_upper_bound_new_stack);
325 sprintf (logtext, "Distribution of stack sizes at block boundary");
327 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
329 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_block_stack[0],
330 count_block_stack[1],count_block_stack[2],count_block_stack[3],count_block_stack[4],
331 count_block_stack[5],count_block_stack[6],count_block_stack[7],count_block_stack[8],
332 count_block_stack[9],count_block_stack[10]);
334 sprintf (logtext, "Distribution of store stack depth");
336 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
338 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_depth[0],
339 count_store_depth[1],count_store_depth[2],count_store_depth[3],count_store_depth[4],
340 count_store_depth[5],count_store_depth[6],count_store_depth[7],count_store_depth[8],
341 count_store_depth[9],count_store_depth[10]);
343 sprintf (logtext, "Distribution of store creator chains first part");
345 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 ");
347 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[0],
348 count_store_length[1],count_store_length[2],count_store_length[3],count_store_length[4],
349 count_store_length[5],count_store_length[6],count_store_length[7],count_store_length[8],
350 count_store_length[9]);
352 sprintf (logtext, "Distribution of store creator chains second part");
354 sprintf (logtext, " 10 11 12 13 14 15 16 17 18 19 >=20");
356 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[10],
357 count_store_length[11],count_store_length[12],count_store_length[13],count_store_length[14],
358 count_store_length[15],count_store_length[16],count_store_length[17],count_store_length[18],
359 count_store_length[19],count_store_length[20]);
361 sprintf (logtext, "Distribution of analysis iterations");
363 sprintf (logtext, " 1 2 3 4 >=5");
365 sprintf (logtext, "%5d%5d%5d%5d%5d", count_analyse_iterations[0],count_analyse_iterations[1],
366 count_analyse_iterations[2],count_analyse_iterations[3],count_analyse_iterations[4]);
368 sprintf (logtext, "Distribution of basic blocks per method");
370 sprintf (logtext, " <= 5 <=10 <=15 <=20 <=30 <=40 <=50 <=75 >75");
372 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_method_bb_distribution[0],
373 count_method_bb_distribution[1],count_method_bb_distribution[2],count_method_bb_distribution[3],
374 count_method_bb_distribution[4],count_method_bb_distribution[5],count_method_bb_distribution[6],
375 count_method_bb_distribution[7],count_method_bb_distribution[8]);
377 sprintf (logtext, "Distribution of basic block sizes");
380 " 1 2 3 4 5 6 7 8 9 10 <13 <15 <17 <19 <21 <26 <31 >30");
382 sprintf (logtext, "%3d%5d%5d%5d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d",
383 count_block_size_distribution[0], count_block_size_distribution[1], count_block_size_distribution[2],
384 count_block_size_distribution[3], count_block_size_distribution[4], count_block_size_distribution[5],
385 count_block_size_distribution[6], count_block_size_distribution[7], count_block_size_distribution[8],
386 count_block_size_distribution[9], count_block_size_distribution[10],count_block_size_distribution[11],
387 count_block_size_distribution[12],count_block_size_distribution[13],count_block_size_distribution[14],
388 count_block_size_distribution[15],count_block_size_distribution[16],count_block_size_distribution[17]);
390 sprintf (logtext, "Size of Code Area (Kb): %10.3f", (float) count_code_len / 1024);
392 sprintf (logtext, "Size of data Area (Kb): %10.3f", (float) count_data_len / 1024);
394 sprintf (logtext, "Size of Class Infos (Kb):%10.3f", (float) (count_class_infos) / 1024);
396 sprintf (logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_unicode_len) / 1024);
398 sprintf (logtext, "Size of Vftbl (Kb): %10.3f", (float) count_vftbl_len / 1024);
400 sprintf (logtext, "Size of comp stub (Kb): %10.3f", (float) count_cstub_len / 1024);
402 sprintf (logtext, "Size of native stub (Kb):%10.3f", (float) count_nstub_len / 1024);
404 sprintf (logtext, "Size of Unicode (Kb): %10.3f", (float) count_unicode_len / 1024);
406 sprintf (logtext, "Size of VMCode (Kb): %10.3f(%d)", (float) count_vmcode_len / 1024,
407 count_vmcode_len - 18 * count_all_methods);
409 sprintf (logtext, "Size of ExTable (Kb): %10.3f", (float) count_extable_len / 1024);
411 sprintf (logtext, "Number of loaded Methods: %d\n\n", count_all_methods);
416 /********** Funktion: class_compile_methods (nur f"ur Debug-Zwecke) ********/
418 void class_compile_methods ()
424 c = list_first (&linkedclasses);
426 for (i = 0; i < c -> methodscount; i++) {
427 m = &(c->methods[i]);
432 (void) jit_compile(m);
435 (void) compiler_compile(m);
439 c = list_next (&linkedclasses, c);
444 * void exit_handler(void)
445 * -----------------------
446 * The exit_handler function is called upon program termination to shutdown
447 * the various subsystems and release the resources allocated to the VM.
450 void exit_handler(void)
452 /********************* Debug-Tabellen ausgeben ************************/
454 if (showmethods) class_showmethods (topclass);
455 if (showconstantpool) class_showconstantpool (topclass);
456 if (showunicode) unicode_show ();
459 clear_thread_flags(); /* restores standard file descriptor
463 /************************ Freigeben aller Resourcen *******************/
465 heap_close (); /* must be called before compiler_close and
466 loader_close because finalization occurs
473 unicode_close ( literalstring_free );
475 if (verbose || getcompilingtime || statistics) {
476 log_text ("CACAO terminated");
479 if (getcompilingtime)
485 /************************** Funktion: main *******************************
488 Wird vom System zu Programstart aufgerufen (eh klar).
490 **************************************************************************/
492 int main(int argc, char **argv)
496 java_objectheader *exceptionptr;
499 /********** interne (nur fuer main relevante Optionen) **************/
501 char logfilename[200] = "";
502 u4 heapsize = 16000000;
503 u4 heapstartsize = 200000;
504 char classpath[500] = ".:/usr/local/lib/java/classes";
506 char *specificmethodname = NULL;
507 char *specificsignature = NULL;
510 stackbottom = &dummy;
513 if (0 != atexit(exit_handler))
514 panic("unable to register exit_handler");
516 /************ Infos aus der Environment lesen ************************/
518 cp = getenv ("CLASSPATH");
520 strcpy (classpath, cp);
523 /***************** Interpretieren der Kommandozeile *****************/
528 while ( (i = get_opt(argc,argv)) != OPT_DONE) {
531 case OPT_IGNORE: break;
534 strcpy (classpath + strlen(classpath), ":");
535 strcpy (classpath + strlen(classpath), opt_arg);
540 int n,l=strlen(opt_arg);
541 for (n=0; n<l; n++) {
542 if (opt_arg[n]=='=') {
544 attach_property (opt_arg, opt_arg+n+1);
557 if (opt_arg[strlen(opt_arg)-1] == 'k') {
558 j = 1024 * atoi(opt_arg);
560 else if (opt_arg[strlen(opt_arg)-1] == 'm') {
561 j = 1024 * 1024 * atoi(opt_arg);
563 else j = atoi(opt_arg);
565 if (i==OPT_MX) heapsize = j;
566 else heapstartsize = j;
577 compileverbose = true;
581 collectverbose = true;
584 case OPT_VERBOSECALL:
597 getcompilingtime = true;
598 getloadingtime = true;
606 strcpy (logfilename, opt_arg);
611 for (j=0; j<strlen(opt_arg); j++) {
612 switch (opt_arg[j]) {
613 case 'b': checkbounds=false; break;
614 case 's': checksync=false; break;
615 default: print_usage();
623 makeinitializations = false;
628 specificmethodname = opt_arg;
629 makeinitializations = false;
633 specificsignature = opt_arg;
639 makeinitializations = false;
659 case OPT_SHOW: /* Anzeigeoptionen */
660 for (j=0; j<strlen(opt_arg); j++) {
661 switch (opt_arg[j]) {
662 case 'a': showdisassemble=true; compileverbose=true; break;
663 case 'c': showconstantpool=true; break;
664 case 'd': showddatasegment=true; break;
665 case 'i': showintermediate=true; compileverbose=true; break;
666 case 'm': showmethods=true; break;
668 case 's': showstack=true; compileverbose=true; break;
670 case 'u': showunicode=true; break;
671 default: print_usage();
686 if (opt_ind >= argc) {
692 /**************************** Programmstart *****************************/
694 log_init (logfilename);
697 "CACAO started -------------------------------------------------------");
700 suck_init (classpath);
701 native_setclasspath (classpath);
704 heap_init(heapsize, heapstartsize, &dummy);
711 native_loadclasses ();
714 /*********************** JAVA-Klassen laden ***************************/
716 cp = argv[opt_ind++];
717 for (i=strlen(cp)-1; i>=0; i--) { /* Punkte im Klassennamen */
718 if (cp[i]=='.') cp[i]='/'; /* auf slashes umbauen */
721 topclass = loader_load ( unicode_new_char (cp) );
723 loader_compute_subclasses();
728 initThreads((u1*)&dummy); /* schani */
731 /************************* Arbeitsroutinen starten ********************/
734 methodinfo *mainmethod;
737 heap_addreference((void**) &a);
739 mainmethod = class_findmethod (
741 unicode_new_char ("main"),
742 unicode_new_char ("([Ljava/lang/String;)V")
744 if (!mainmethod) panic ("Can not find method 'void main(String[])'");
745 if ((mainmethod->flags & ACC_STATIC) != ACC_STATIC) panic ("main is not static!");
747 a = builtin_anewarray (argc - opt_ind, class_java_lang_String);
748 for (i=opt_ind; i<argc; i++) {
749 a->data[i-opt_ind] = javastring_new (unicode_new_char (argv[i]) );
751 exceptionptr = asm_calljavamethod (mainmethod, a, NULL,NULL,NULL );
754 printf ("#### Program has thrown: ");
755 unicode_display (exceptionptr->vftbl->class->name);
760 killThread(currentThread);
762 fprintf(stderr, "still here\n");
765 /************* Auf Wunsch alle Methode "ubersetzen ********************/
768 class_compile_methods();
772 /******** Auf Wunsch eine spezielle Methode "ubersetzen ***************/
774 if (specificmethodname) {
776 if (specificsignature)
777 m = class_findmethod(topclass,
778 unicode_new_char(specificmethodname),
779 unicode_new_char(specificsignature));
781 m = class_findmethod(topclass,
782 unicode_new_char(specificmethodname), NULL);
783 if (!m) panic ("Specific method not found");
787 (void) jit_compile(m);
790 (void) compiler_compile(m);
799 /************************************ SHUTDOWN-Funktion *********************************
801 Terminiert das System augenblicklich, ohne den Speicher
802 explizit freizugeben (eigentlich nur f"ur abnorme
803 Programmterminierung)
805 *****************************************************************************************/
807 void cacao_shutdown(s4 status)
809 if (verbose || getcompilingtime || statistics) {
810 log_text ("CACAO terminated by shutdown");
813 if (getcompilingtime)
816 sprintf (logtext, "Exit status: %d\n", (int) status);
825 * These are local overrides for various environment variables in Emacs.
826 * Please do not remove this and leave it at the end of the file, where
827 * Emacs will automagically detect them.
828 * ---------------------------------------------------------------------
831 * indent-tabs-mode: t