Working towards better make dist
[cacao.git] / main.c
diff --git a/main.c b/main.c
index aee43a89d7b7ca571c51e4a48cdac6b4eacdf512..0836e2a6cf210814d9041a1c4f4f27ea29e0fda9 100644 (file)
--- a/main.c
+++ b/main.c
@@ -4,19 +4,19 @@
 
        See file COPYRIGHT for information on usage and disclaimer of warranties
 
-       Enthaelt die Funktion main() und die Variablen fuer die 
-       globalen Optionen.
-       Dieser Modul erledigt folgende Aufgaben:
-          - Bearbeiten der command-line-options
-          - Aufrufen aller Initialisierungsroutinen
-          - Aufrufen des Classloaders
-          - Starten der main - Methode
+       Contains main() and variables for the global options.
+       This module does the following tasks:
+          - Command line option handling
+          - Calling initialization routines
+          - Calling the class loader
+          - Running the main method
 
        Authors: Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at
        Changes: Andi Krall          EMAIL: cacao@complang.tuwien.ac.at
                 Mark Probst         EMAIL: cacao@complang.tuwien.ac.at
+                        Philipp Tomsich     EMAIL: cacao@complang.tuwien.ac.at
 
-       Last Change: 1997/10/29
+       Last Change: $Id: main.c 175 2002-12-04 13:33:31Z stefan $
 
 *******************************************************************************/
 
@@ -44,7 +44,7 @@ bool new_gc = false;
 
 static bool showmethods = false;
 static bool showconstantpool = false;
-static bool showunicode = false;
+static bool showutf = false;
 static classinfo *topclass;
 
 #ifndef USE_THREADS
@@ -88,6 +88,7 @@ void **stackbottom = 0;
 #define OPT_GC1         22
 #define OPT_GC2         23
 #endif
+#define OPT_OLOOP       24
 
 struct {char *name; bool arg; int value;} opts[] = {
        {"classpath",   true,   OPT_CLASSPATH},
@@ -120,6 +121,7 @@ struct {char *name; bool arg; int value;} opts[] = {
        {"gc1",         false,  OPT_GC1},
        {"gc2",         false,  OPT_GC2},
 #endif
+       {"oloop",       false,  OPT_OLOOP},
        {NULL,  false, 0}
 };
 
@@ -172,9 +174,9 @@ static int get_opt (int argc, char **argv)
 
 
 
-/******************** interne Funktion: print_usage ************************
+/******************** interne Function: print_usage ************************
 
-Gibt die richtige Aufrufsyntax des JavaVM-Compilers auf stdout aus.
+Prints the correct usage syntax to stdout.
 
 ***************************************************************************/
 
@@ -197,6 +199,7 @@ static void print_usage()
        printf ("          -log logfile ......... specify a name for the logfile\n");
        printf ("          -c(heck)b(ounds) ..... don't check array bounds\n");
        printf ("                  s(ync) ....... don't check for synchronization\n");
+       printf ("          -oloop ............... optimize array accesses in loops\n"); 
        printf ("          -l ................... don't start the class after loading\n");
        printf ("          -all ................. compile all methods, no execution\n");
 #ifdef OLD_COMPILER
@@ -216,14 +219,14 @@ static void print_usage()
 #ifdef OLD_COMPILER
        printf ("                 s(tack) ....... show stack for every javaVM-command\n");
 #endif
-       printf ("                 u(nicode) ..... show the unicode - hash\n");
+       printf ("                 u(tf) ......... show the utf - hash\n");
 }   
 
 
 
-/***************************** Funktion: print_times *********************
+/***************************** Function: print_times *********************
 
-       gibt eine Aufstellung der verwendeten CPU-Zeit aus
+       Prints a summary of CPU time usage.
 
 **************************************************************************/
 
@@ -251,7 +254,7 @@ static void print_times()
 
 
 
-/***************************** Funktion: print_stats *********************
+/***************************** Function: print_stats *********************
 
        outputs detailed compiler statistics
 
@@ -377,7 +380,7 @@ static void print_stats()
        sprintf (logtext, "Distribution of basic block sizes");
        dolog();
        sprintf (logtext,
-       "  1    2    3    4   5   6   7   8   9  10 <13 <15 <17 <19 <21 <26 <31 >30");
+       "  0    1    2    3    4   5   6   7   8   9 <13 <15 <17 <19 <21 <26 <31 >30");
        dolog();
        sprintf (logtext, "%3d%5d%5d%5d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d",
                count_block_size_distribution[0], count_block_size_distribution[1], count_block_size_distribution[2],
@@ -393,7 +396,7 @@ static void print_stats()
        dolog();
        sprintf (logtext, "Size of Class Infos (Kb):%10.3f", (float) (count_class_infos) / 1024);
        dolog();
-       sprintf (logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_unicode_len) / 1024);
+       sprintf (logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_utf_len) / 1024);
        dolog();
        sprintf (logtext, "Size of Vftbl (Kb):      %10.3f", (float) count_vftbl_len / 1024);
        dolog();
@@ -401,19 +404,28 @@ static void print_stats()
        dolog();
        sprintf (logtext, "Size of native stub (Kb):%10.3f", (float) count_nstub_len / 1024);
        dolog();
-       sprintf (logtext, "Size of Unicode (Kb):    %10.3f", (float) count_unicode_len / 1024);
+       sprintf (logtext, "Size of Utf (Kb):        %10.3f", (float) count_utf_len / 1024);
        dolog();
        sprintf (logtext, "Size of VMCode (Kb):     %10.3f(%d)", (float) count_vmcode_len / 1024,
                                                      count_vmcode_len - 18 * count_all_methods);
        dolog();
        sprintf (logtext, "Size of ExTable (Kb):    %10.3f", (float) count_extable_len / 1024);
        dolog();
+       sprintf (logtext, "Number of class loads:   %d", count_class_loads);
+       dolog();
+       sprintf (logtext, "Number of class inits:   %d", count_class_inits);
+       dolog();
        sprintf (logtext, "Number of loaded Methods: %d\n\n", count_all_methods);
        dolog();
+
+       sprintf (logtext, "Calls of utf_new: %22d", count_utf_new);
+       dolog();
+       sprintf (logtext, "Calls of utf_new (element found): %6d\n\n", count_utf_new_found);
+       dolog();
 }
 
 
-/********** Funktion: class_compile_methods   (nur f"ur Debug-Zwecke) ********/
+/********** Function: class_compile_methods   (debugging only) ********/
 
 void class_compile_methods ()
 {
@@ -449,18 +461,18 @@ void class_compile_methods ()
 
 void exit_handler(void)
 {
-       /********************* Debug-Tabellen ausgeben ************************/
+       /********************* Print debug tables ************************/
                                
        if (showmethods) class_showmethods (topclass);
        if (showconstantpool)  class_showconstantpool (topclass);
-       if (showunicode)       unicode_show ();
+       if (showutf)           utf_show ();
 
 #ifdef USE_THREADS
        clear_thread_flags();           /* restores standard file descriptor
                                                                   flags */
 #endif
 
-       /************************ Freigeben aller Resourcen *******************/
+       /************************ Free all resources *******************/
 
        heap_close ();                          /* must be called before compiler_close and
                                                                   loader_close because finalization occurs
@@ -470,7 +482,7 @@ void exit_handler(void)
        compiler_close ();
 #endif
        loader_close ();
-       unicode_close ( literalstring_free );
+       tables_close ( literalstring_free );
 
        if (verbose || getcompilingtime || statistics) {
                log_text ("CACAO terminated");
@@ -482,10 +494,9 @@ void exit_handler(void)
        }
 }
 
-/************************** Funktion: main *******************************
+/************************** Function: main *******************************
 
-   Das Hauptprogramm.
-   Wird vom System zu Programstart aufgerufen (eh klar).
+   The main program.
    
 **************************************************************************/
 
@@ -493,7 +504,7 @@ int main(int argc, char **argv)
 {
        s4 i,j;
        char *cp;
-       java_objectheader *exceptionptr;
+       java_objectheader *local_exceptionptr = 0;
        void *dummy;
        
        /********** interne (nur fuer main relevante Optionen) **************/
@@ -513,14 +524,14 @@ int main(int argc, char **argv)
        if (0 != atexit(exit_handler))
                panic("unable to register exit_handler");
 
-       /************ Infos aus der Environment lesen ************************/
+       /************ Collect info from the environment ************************/
 
        cp = getenv ("CLASSPATH");
        if (cp) {
                strcpy (classpath, cp);
        }
 
-       /***************** Interpretieren der Kommandozeile *****************/
+       /***************** Interpret the command line *****************/
    
        checknull = false;
        checkfloats = false;
@@ -656,7 +667,7 @@ int main(int argc, char **argv)
                        break;
 #endif
                        
-               case OPT_SHOW:       /* Anzeigeoptionen */
+               case OPT_SHOW:       /* Display options */
                        for (j=0; j<strlen(opt_arg); j++) {             
                                switch (opt_arg[j]) {
                                case 'a':  showdisassemble=true; compileverbose=true; break;
@@ -667,13 +678,17 @@ int main(int argc, char **argv)
 #ifdef OLD_COMPILER
                                case 's':  showstack=true; compileverbose=true; break;
 #endif
-                               case 'u':  showunicode=true; break;
+                               case 'u':  showutf=true; break;
                                default:   print_usage();
                                        exit(10);
                                }
                        }
                        break;
                        
+               case OPT_OLOOP:
+                       opt_loops = true;
+                       break;
+
                default:
                        print_usage();
                        exit(10);
@@ -689,7 +704,7 @@ int main(int argc, char **argv)
        }
 
 
-       /**************************** Programmstart *****************************/
+       /**************************** Program start *****************************/
 
        log_init (logfilename);
        if (verbose) {
@@ -700,7 +715,7 @@ int main(int argc, char **argv)
        suck_init (classpath);
        native_setclasspath (classpath);
                
-       unicode_init();
+       tables_init();
        heap_init(heapsize, heapstartsize, &dummy);
        loader_init();
 #ifdef OLD_COMPILER
@@ -711,16 +726,29 @@ int main(int argc, char **argv)
        native_loadclasses ();
 
 
-       /*********************** JAVA-Klassen laden  ***************************/
+       /*********************** Load JAVA classes  ***************************/
    
        cp = argv[opt_ind++];
-       for (i=strlen(cp)-1; i>=0; i--) {     /* Punkte im Klassennamen */
-               if (cp[i]=='.') cp[i]='/';        /* auf slashes umbauen */
+       for (i=strlen(cp)-1; i>=0; i--) {     /* Transform dots into slashes */
+               if (cp[i]=='.') cp[i]='/';        /* in the class name */
        }
 
-       topclass = loader_load ( unicode_new_char (cp) );
+       topclass = loader_load ( utf_new_char (cp) );
 
-       loader_compute_subclasses();
+       if (exceptionptr != 0)
+       {
+               printf ("#### Class loader has thrown: ");
+               utf_display (exceptionptr->vftbl->class->name);
+               printf ("\n");
+
+               exceptionptr = 0;
+       }
+
+       if (topclass == 0)
+       {
+               printf("#### Could not find top class - exiting\n");
+               exit(1);
+       }
 
        gc_init();
 
@@ -728,7 +756,7 @@ int main(int argc, char **argv)
        initThreads((u1*)&dummy);                   /* schani */
 #endif
 
-       /************************* Arbeitsroutinen starten ********************/
+       /************************* Start worker routines ********************/
 
        if (startit) {
                methodinfo *mainmethod;
@@ -738,46 +766,48 @@ int main(int argc, char **argv)
 
                mainmethod = class_findmethod (
                                                                           topclass,
-                                                                          unicode_new_char ("main"), 
-                                                                          unicode_new_char ("([Ljava/lang/String;)V")
+                                                                          utf_new_char ("main"), 
+                                                                          utf_new_char ("([Ljava/lang/String;)V")
                                                                           );
                if (!mainmethod) panic ("Can not find method 'void main(String[])'");
                if ((mainmethod->flags & ACC_STATIC) != ACC_STATIC) panic ("main is not static!");
                        
                a = builtin_anewarray (argc - opt_ind, class_java_lang_String);
                for (i=opt_ind; i<argc; i++) {
-                       a->data[i-opt_ind] = javastring_new (unicode_new_char (argv[i]) );
+                       a->data[i-opt_ind] = javastring_new (utf_new_char (argv[i]) );
                }
-               exceptionptr = asm_calljavamethod (mainmethod, a, NULL,NULL,NULL );
+               local_exceptionptr = asm_calljavamethod (mainmethod, a, NULL,NULL,NULL );
        
-               if (exceptionptr) {
+               if (local_exceptionptr) {
                        printf ("#### Program has thrown: ");
-                       unicode_display (exceptionptr->vftbl->class->name);
+                       utf_display (local_exceptionptr->vftbl->class->name);
                        printf ("\n");
                }
 
+#ifdef USE_THREADS
                killThread(currentThread);
+#endif
                fprintf(stderr, "still here\n");
        }
 
-       /************* Auf Wunsch alle Methode "ubersetzen ********************/
+       /************* If requested, compile all methods ********************/
 
        if (compileall) {
                class_compile_methods();
        }
 
 
-       /******** Auf Wunsch eine spezielle Methode "ubersetzen ***************/
+       /******** If requested, compile a specific method ***************/
 
        if (specificmethodname) {
                methodinfo *m;
                if (specificsignature)
                        m = class_findmethod(topclass, 
-                                                                unicode_new_char(specificmethodname),
-                                                                unicode_new_char(specificsignature));
+                                                                utf_new_char(specificmethodname),
+                                                                utf_new_char(specificsignature));
                else
                        m = class_findmethod(topclass, 
-                                                                unicode_new_char(specificmethodname), NULL);
+                                                                utf_new_char(specificmethodname), NULL);
                if (!m) panic ("Specific method not found");
 #ifdef OLD_COMPILER
                if (newcompiler)
@@ -794,11 +824,10 @@ int main(int argc, char **argv)
 
 
 
-/************************************ SHUTDOWN-Funktion *********************************
+/************************************ Shutdown function *********************************
 
-       Terminiert das System augenblicklich, ohne den Speicher
-       explizit freizugeben (eigentlich nur f"ur abnorme 
-       Programmterminierung)
+       Terminates the system immediately without freeing memory explicitly (to be
+       used only for abnormal termination)
        
 *****************************************************************************************/