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" /* schani */
38 bool compileall = false;
39 int newcompiler = true;
43 void **stackbottom = 0;
47 /* internal function: get_opt *************************************************
49 decodes the next command line option
51 ******************************************************************************/
57 #define OPT_CLASSPATH 2
61 #define OPT_VERBOSE1 6
63 #define OPT_VERBOSEGC 8
64 #define OPT_VERBOSECALL 9
66 #define OPT_SOFTNULL 11
73 #define OPT_SIGNATURE 18
80 struct {char *name; bool arg; int value;} opts[] = {
81 {"classpath", true, OPT_CLASSPATH},
85 {"noasyncgc", false, OPT_IGNORE},
86 {"noverify", false, OPT_IGNORE},
87 {"oss", true, OPT_IGNORE},
88 {"ss", true, OPT_IGNORE},
89 {"v", false, OPT_VERBOSE1},
90 {"verbose", false, OPT_VERBOSE},
91 {"verbosegc", false, OPT_VERBOSEGC},
92 {"verbosecall", false, OPT_VERBOSECALL},
93 {"ieee", false, OPT_IEEE},
94 {"softnull", false, OPT_SOFTNULL},
95 {"time", false, OPT_TIME},
96 {"stat", false, OPT_STAT},
97 {"log", true, OPT_LOG},
98 {"c", true, OPT_CHECK},
99 {"l", false, OPT_LOAD},
100 {"m", true, OPT_METHOD},
101 {"sig", true, OPT_SIGNATURE},
102 {"s", true, OPT_SHOW},
103 {"all", false, OPT_ALL},
105 {"old", false, OPT_OLD},
110 static int opt_ind = 1;
111 static char *opt_arg;
113 static int get_opt (int argc, char **argv)
118 if (opt_ind >= argc) return OPT_DONE;
121 if (a[0] != '-') return OPT_DONE;
123 for (i=0; opts[i].name; i++) {
125 if (strcmp(a+1, opts[i].name) == 0) { /* boolean option found */
127 return opts[i].value;
131 if (strcmp(a+1, opts[i].name) == 0) { /* parameter option found */
133 if (opt_ind < argc) {
134 opt_arg = argv[opt_ind];
136 return opts[i].value;
141 size_t l = strlen(opts[i].name);
142 if (strlen(a+1) > l) {
143 if (memcmp (a+1, opts[i].name, l)==0) {
146 return opts[i].value;
159 /******************** interne Funktion: print_usage ************************
161 Gibt die richtige Aufrufsyntax des JavaVM-Compilers auf stdout aus.
163 ***************************************************************************/
165 static void print_usage()
167 printf ("USAGE: cacao [options] classname [program arguments\n");
168 printf ("Options:\n");
169 printf (" -classpath path ...... specify a path to look for classes\n");
170 printf (" -Dpropertyname=value . add an entry to the property list\n");
171 printf (" -mx maxmem[k|m] ...... specify the size for the heap\n");
172 printf (" -ms initmem[k|m] ..... specify the initial size for the heap\n");
173 printf (" -v ................... write state-information\n");
174 printf (" -verbose ............. write more information\n");
175 printf (" -verbosegc ........... write message for each GC\n");
176 printf (" -verbosecall ......... write message for each call\n");
177 printf (" -ieee ................ use ieee compliant arithmetic\n");
178 printf (" -softnull ............ use software nullpointer check\n");
179 printf (" -time ................ measure the runtime\n");
180 printf (" -stat ................ detailed compiler statistics\n");
181 printf (" -log logfile ......... specify a name for the logfile\n");
182 printf (" -c(heck) b(ounds...... don't check array bounds\n");
183 printf (" s(ync) ...... don't check for synchronization\n");
184 printf (" -l ................... don't start the class after loading\n");
185 printf (" -all ................. compile all methods, no execution\n");
187 printf (" -old ................. use old JIT compiler\n");
189 printf (" -m ................... compile only a specific method\n");
190 printf (" -sig ................. specify signature for a specific method\n");
191 printf (" -s(how)m(ethods) ..... show all methods&fields of a class\n");
192 printf (" a(ssembler) ... show disassembled listing\n");
193 printf (" c(onstants) ... show the constant pool\n");
194 printf (" d(atasegment).. show data segment listing\n");
195 printf (" i(ntermediate). show intermediate representation\n");
196 printf (" m(ethods)...... show class fields and methods\n");
198 printf (" s(tack) ....... show stack for every javaVM-command\n");
200 printf (" u(nicode) ..... show the unicode - hash\n");
205 /***************************** Funktion: print_times *********************
207 gibt eine Aufstellung der verwendeten CPU-Zeit aus
209 **************************************************************************/
211 static void print_times()
213 long int totaltime = getcputime();
214 long int runtime = totaltime - loadingtime - compilingtime;
216 sprintf (logtext, "Time for loading classes: %ld secs, %ld millis",
217 loadingtime / 1000000, (loadingtime % 1000000) / 1000);
219 sprintf (logtext, "Time for compiling code: %ld secs, %ld millis",
220 compilingtime / 1000000, (compilingtime % 1000000) / 1000);
222 sprintf (logtext, "Time for running program: %ld secs, %ld millis",
223 runtime / 1000000, (runtime % 1000000) / 1000);
225 sprintf (logtext, "Total time: %ld secs, %ld millis",
226 totaltime / 1000000, (totaltime % 1000000) / 1000);
235 /***************************** Funktion: print_stats *********************
237 outputs detailed compiler statistics
239 **************************************************************************/
241 static void print_stats()
243 sprintf (logtext, "Number of JitCompiler Calls: %d", count_jit_calls);
245 sprintf (logtext, "Number of compiled Methods: %d", count_methods);
247 sprintf (logtext, "Number of max basic blocks per method: %d", count_max_basic_blocks);
249 sprintf (logtext, "Number of compiled basic blocks: %d", count_basic_blocks);
251 sprintf (logtext, "Number of max JavaVM-Instructions per method: %d", count_max_javainstr);
253 sprintf (logtext, "Number of compiled JavaVM-Instructions: %d", count_javainstr);
255 sprintf (logtext, "Size of compiled JavaVM-Instructions: %d(%d)", count_javacodesize,
256 count_javacodesize - count_methods * 18);
258 sprintf (logtext, "Size of compiled Exception Tables: %d", count_javaexcsize);
260 sprintf (logtext, "Value of extended instruction set var: %d", has_ext_instr_set);
262 sprintf (logtext, "Number of Alpha-Instructions: %d", count_code_len >> 2);
264 sprintf (logtext, "Number of Spills: %d", count_spills);
266 sprintf (logtext, "Number of Activ Pseudocommands: %5d", count_pcmd_activ);
268 sprintf (logtext, "Number of Drop Pseudocommands: %5d", count_pcmd_drop);
270 sprintf (logtext, "Number of Const Pseudocommands: %5d (zero:%5d)", count_pcmd_load, count_pcmd_zero);
272 sprintf (logtext, "Number of ConstAlu Pseudocommands: %5d (cmp: %5d, store:%5d)", count_pcmd_const_alu, count_pcmd_const_bra, count_pcmd_const_store);
274 sprintf (logtext, "Number of Move Pseudocommands: %5d", count_pcmd_move);
276 sprintf (logtext, "Number of Load Pseudocommands: %5d", count_load_instruction);
278 sprintf (logtext, "Number of Store Pseudocommands: %5d (combined: %5d)", count_pcmd_store, count_pcmd_store - count_pcmd_store_comb);
280 sprintf (logtext, "Number of OP Pseudocommands: %5d", count_pcmd_op);
282 sprintf (logtext, "Number of DUP Pseudocommands: %5d", count_dup_instruction);
284 sprintf (logtext, "Number of Mem Pseudocommands: %5d", count_pcmd_mem);
286 sprintf (logtext, "Number of Method Pseudocommands: %5d", count_pcmd_met);
288 sprintf (logtext, "Number of Branch Pseudocommands: %5d (rets:%5d, Xrets: %5d)",
289 count_pcmd_bra, count_pcmd_return, count_pcmd_returnx);
291 sprintf (logtext, "Number of Table Pseudocommands: %5d", count_pcmd_table);
293 sprintf (logtext, "Number of Useful Pseudocommands: %5d", count_pcmd_table +
294 count_pcmd_bra + count_pcmd_load + count_pcmd_mem + count_pcmd_op);
296 sprintf (logtext, "Number of Null Pointer Checks: %5d", count_check_null);
298 sprintf (logtext, "Number of Array Bound Checks: %5d", count_check_bound);
300 sprintf (logtext, "Number of Try-Blocks: %d", count_tryblocks);
302 sprintf (logtext, "Maximal count of stack elements: %d", count_max_new_stack);
304 sprintf (logtext, "Upper bound of max stack elements: %d", count_upper_bound_new_stack);
306 sprintf (logtext, "Distribution of stack sizes at block boundary");
308 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
310 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_block_stack[0],
311 count_block_stack[1],count_block_stack[2],count_block_stack[3],count_block_stack[4],
312 count_block_stack[5],count_block_stack[6],count_block_stack[7],count_block_stack[8],
313 count_block_stack[9],count_block_stack[10]);
315 sprintf (logtext, "Distribution of store stack depth");
317 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 >=10");
319 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_depth[0],
320 count_store_depth[1],count_store_depth[2],count_store_depth[3],count_store_depth[4],
321 count_store_depth[5],count_store_depth[6],count_store_depth[7],count_store_depth[8],
322 count_store_depth[9],count_store_depth[10]);
324 sprintf (logtext, "Distribution of store creator chains first part");
326 sprintf (logtext, " 0 1 2 3 4 5 6 7 8 9 ");
328 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[0],
329 count_store_length[1],count_store_length[2],count_store_length[3],count_store_length[4],
330 count_store_length[5],count_store_length[6],count_store_length[7],count_store_length[8],
331 count_store_length[9]);
333 sprintf (logtext, "Distribution of store creator chains second part");
335 sprintf (logtext, " 10 11 12 13 14 15 16 17 18 19 >=20");
337 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[10],
338 count_store_length[11],count_store_length[12],count_store_length[13],count_store_length[14],
339 count_store_length[15],count_store_length[16],count_store_length[17],count_store_length[18],
340 count_store_length[19],count_store_length[20]);
342 sprintf (logtext, "Distribution of analysis iterations");
344 sprintf (logtext, " 1 2 3 4 >=5");
346 sprintf (logtext, "%5d%5d%5d%5d%5d", count_analyse_iterations[0],count_analyse_iterations[1],
347 count_analyse_iterations[2],count_analyse_iterations[3],count_analyse_iterations[4]);
349 sprintf (logtext, "Distribution of basic blocks per method");
351 sprintf (logtext, " <= 5 <=10 <=15 <=20 <=30 <=40 <=50 <=75 >75");
353 sprintf (logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_method_bb_distribution[0],
354 count_method_bb_distribution[1],count_method_bb_distribution[2],count_method_bb_distribution[3],
355 count_method_bb_distribution[4],count_method_bb_distribution[5],count_method_bb_distribution[6],
356 count_method_bb_distribution[7],count_method_bb_distribution[8]);
358 sprintf (logtext, "Distribution of basic block sizes");
361 " 1 2 3 4 5 6 7 8 9 10 <13 <15 <17 <19 <21 <26 <31 >30");
363 sprintf (logtext, "%3d%5d%5d%5d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d",
364 count_block_size_distribution[0], count_block_size_distribution[1], count_block_size_distribution[2],
365 count_block_size_distribution[3], count_block_size_distribution[4], count_block_size_distribution[5],
366 count_block_size_distribution[6], count_block_size_distribution[7], count_block_size_distribution[8],
367 count_block_size_distribution[9], count_block_size_distribution[10],count_block_size_distribution[11],
368 count_block_size_distribution[12],count_block_size_distribution[13],count_block_size_distribution[14],
369 count_block_size_distribution[15],count_block_size_distribution[16],count_block_size_distribution[17]);
371 sprintf (logtext, "Size of Code Area (Kb): %10.3f", (float) count_code_len / 1024);
373 sprintf (logtext, "Size of data Area (Kb): %10.3f", (float) count_data_len / 1024);
375 sprintf (logtext, "Size of Class Infos (Kb):%10.3f", (float) (count_class_infos) / 1024);
377 sprintf (logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_unicode_len) / 1024);
379 sprintf (logtext, "Size of Vftbl (Kb): %10.3f", (float) count_vftbl_len / 1024);
381 sprintf (logtext, "Size of comp stub (Kb): %10.3f", (float) count_cstub_len / 1024);
383 sprintf (logtext, "Size of native stub (Kb):%10.3f", (float) count_nstub_len / 1024);
385 sprintf (logtext, "Size of Unicode (Kb): %10.3f", (float) count_unicode_len / 1024);
387 sprintf (logtext, "Size of VMCode (Kb): %10.3f(%d)", (float) count_vmcode_len / 1024,
388 count_vmcode_len - 18 * count_all_methods);
390 sprintf (logtext, "Size of ExTable (Kb): %10.3f", (float) count_extable_len / 1024);
392 sprintf (logtext, "Number of loaded Methods: %d\n\n", count_all_methods);
397 /********** Funktion: class_compile_methods (nur f"ur Debug-Zwecke) ********/
399 void class_compile_methods ()
405 c = list_first (&linkedclasses);
407 for (i = 0; i < c -> methodscount; i++) {
408 m = &(c->methods[i]);
413 (void) jit_compile(m);
416 (void) compiler_compile(m);
420 c = list_next (&linkedclasses, c);
425 /************************** Funktion: main *******************************
428 Wird vom System zu Programstart aufgerufen (eh klar).
430 **************************************************************************/
433 int main(int argc, char **argv)
438 java_objectheader *exceptionptr;
441 /********** interne (nur fuer main relevante Optionen) **************/
443 char logfilename[200] = "";
444 u4 heapsize = 16000000;
445 u4 heapstartsize = 200000;
446 char classpath[500] = ".:/usr/local/lib/java/classes";
447 bool showmethods = false;
448 bool showconstantpool = false;
449 bool showunicode = false;
451 char *specificmethodname = NULL;
452 char *specificsignature = NULL;
455 stackbottom = &dummy;
460 atexit(clear_thread_flags);
463 /************ Infos aus der Environment lesen ************************/
465 cp = getenv ("CLASSPATH");
467 strcpy (classpath, cp);
470 /***************** Interpretieren der Kommandozeile *****************/
475 while ( (i = get_opt(argc,argv)) != OPT_DONE) {
478 case OPT_IGNORE: break;
481 strcpy (classpath + strlen(classpath), ":");
482 strcpy (classpath + strlen(classpath), opt_arg);
487 int n,l=strlen(opt_arg);
488 for (n=0; n<l; n++) {
489 if (opt_arg[n]=='=') {
491 attach_property (opt_arg, opt_arg+n+1);
504 if (opt_arg[strlen(opt_arg)-1] == 'k') {
505 j = 1024 * atoi(opt_arg);
507 else if (opt_arg[strlen(opt_arg)-1] == 'm') {
508 j = 1024 * 1024 * atoi(opt_arg);
510 else j = atoi(opt_arg);
512 if (i==OPT_MX) heapsize = j;
513 else heapstartsize = j;
524 compileverbose = true;
528 collectverbose = true;
531 case OPT_VERBOSECALL:
544 getcompilingtime = true;
545 getloadingtime = true;
553 strcpy (logfilename, opt_arg);
558 for (j=0; j<strlen(opt_arg); j++) {
559 switch (opt_arg[j]) {
560 case 'b': checkbounds=false; break;
561 case 's': checksync=false; break;
562 default: print_usage();
570 makeinitializations = false;
575 specificmethodname = opt_arg;
576 makeinitializations = false;
580 specificsignature = opt_arg;
586 makeinitializations = false;
596 case OPT_SHOW: /* Anzeigeoptionen */
597 for (j=0; j<strlen(opt_arg); j++) {
598 switch (opt_arg[j]) {
599 case 'a': showdisassemble=true; compileverbose=true; break;
600 case 'c': showconstantpool=true; break;
601 case 'd': showddatasegment=true; break;
602 case 'i': showintermediate=true; compileverbose=true; break;
603 case 'm': showmethods=true; break;
605 case 's': showstack=true; compileverbose=true; break;
607 case 'u': showunicode=true; break;
608 default: print_usage();
623 if (opt_ind >= argc) {
629 /**************************** Programmstart *****************************/
631 log_init (logfilename);
634 "CACAO started -------------------------------------------------------");
637 suck_init (classpath);
638 native_setclasspath (classpath);
641 heap_init(heapsize, heapstartsize, &dummy);
648 native_loadclasses ();
651 /*********************** JAVA-Klassen laden ***************************/
653 cp = argv[opt_ind++];
654 for (i=strlen(cp)-1; i>=0; i--) { /* Punkte im Klassennamen */
655 if (cp[i]=='.') cp[i]='/'; /* auf slashes umbauen */
658 topclass = loader_load ( unicode_new_char (cp) );
660 loader_compute_subclasses();
665 initThreads((u1*)&dummy); /* schani */
668 /************************* Arbeitsroutinen starten ********************/
671 methodinfo *mainmethod;
674 heap_addreference((void**) &a);
676 mainmethod = class_findmethod (
678 unicode_new_char ("main"),
679 unicode_new_char ("([Ljava/lang/String;)V")
681 if (!mainmethod) panic ("Can not find method 'void main(String[])'");
682 if ((mainmethod->flags & ACC_STATIC) != ACC_STATIC) panic ("main is not static!");
684 a = builtin_anewarray (argc - opt_ind, class_java_lang_String);
685 for (i=opt_ind; i<argc; i++) {
686 a->data[i-opt_ind] = javastring_new (unicode_new_char (argv[i]) );
688 exceptionptr = asm_calljavamethod (mainmethod, a, NULL,NULL,NULL );
691 printf ("#### Program has thrown: ");
692 unicode_display (exceptionptr->vftbl->class->name);
696 /* killThread(currentThread); */
700 /************* Auf Wunsch alle Methode "ubersetzen ********************/
703 class_compile_methods();
707 /******** Auf Wunsch eine spezielle Methode "ubersetzen ***************/
709 if (specificmethodname) {
711 if (specificsignature)
712 m = class_findmethod(topclass,
713 unicode_new_char(specificmethodname),
714 unicode_new_char(specificsignature));
716 m = class_findmethod(topclass,
717 unicode_new_char(specificmethodname), NULL);
718 if (!m) panic ("Specific method not found");
722 (void) jit_compile(m);
725 (void) compiler_compile(m);
729 /********************* Debug-Tabellen ausgeben ************************/
731 if (showmethods) class_showmethods (topclass);
732 if (showconstantpool) class_showconstantpool (topclass);
733 if (showunicode) unicode_show ();
737 /************************ Freigeben aller Resourcen *******************/
739 heap_close (); /* must be called before compiler_close and
740 loader_close because finalization occurs
746 unicode_close ( literalstring_free );
749 /* Endemeldung ausgeben und mit entsprechendem exit-Status terminieren */
751 if (verbose || getcompilingtime || statistics) {
752 log_text ("CACAO terminated");
755 if (getcompilingtime)
766 /************************************ SHUTDOWN-Funktion *********************************
768 Terminiert das System augenblicklich, ohne den Speicher
769 explizit freizugeben (eigentlich nur f"ur abnorme
770 Programmterminierung)
772 *****************************************************************************************/
774 void cacao_shutdown(s4 status)
776 if (verbose || getcompilingtime || statistics) {
777 log_text ("CACAO terminated by shutdown");
780 if (getcompilingtime)
783 sprintf (logtext, "Exit status: %d\n", (int) status);
792 * These are local overrides for various environment variables in Emacs.
793 * Please do not remove this and leave it at the end of the file, where
794 * Emacs will automagically detect them.
795 * ---------------------------------------------------------------------
798 * indent-tabs-mode: t