- maybe we should always use `const char *' in function declarations
[cacao.git] / main.c
diff --git a/main.c b/main.c
index 9bae68c79d3cc9ba880a7676bd7ae246bc918d16..11b54349bb80ed0992feed516a2d8904af35ae6e 100644 (file)
--- a/main.c
+++ b/main.c
@@ -37,7 +37,7 @@
      - Calling the class loader
      - Running the main method
 
-   $Id: main.c 1465 2004-11-08 11:09:01Z twisti $
+   $Id: main.c 1549 2004-11-19 13:17:33Z twisti $
 
 */
 
@@ -81,15 +81,7 @@ void **stackbottom = 0;
 #endif
 
 
-/* internal function: get_opt *************************************************
-       
-       decodes the next command line option
-       
-******************************************************************************/
-
-#define OPT_DONE       -1
-#define OPT_ERROR       0
-#define OPT_IGNORE      1
+/* define command line options ************************************************/
 
 #define OPT_CLASSPATH   2
 #define OPT_D           3
@@ -122,7 +114,7 @@ void **stackbottom = 0;
 #define OPT_EAGER            33
 
 
-struct {char *name; bool arg; int value;} opts[] = {
+opt_struct opts[] = {
        {"classpath",        true,   OPT_CLASSPATH},
        {"cp",               true,   OPT_CLASSPATH},
        {"D",                true,   OPT_D},
@@ -165,53 +157,6 @@ struct {char *name; bool arg; int value;} opts[] = {
        {NULL,               false,  0}
 };
 
-static int opt_ind = 1;
-static char *opt_arg;
-
-
-static int get_opt(int argc, char **argv)
-{
-       char *a;
-       int i;
-       
-       if (opt_ind >= argc) return OPT_DONE;
-       
-       a = argv[opt_ind];
-       if (a[0] != '-') return OPT_DONE;
-
-       for (i = 0; opts[i].name; i++) {
-               if (!opts[i].arg) {
-                       if (strcmp(a + 1, opts[i].name) == 0) { /* boolean option found */
-                               opt_ind++;
-                               return opts[i].value;
-                       }
-
-               } else {
-                       if (strcmp(a + 1, opts[i].name) == 0) { /* parameter option found */
-                               opt_ind++;
-                               if (opt_ind < argc) {
-                                       opt_arg = argv[opt_ind];
-                                       opt_ind++;
-                                       return opts[i].value;
-                               }
-                               return OPT_ERROR;
-
-                       } else {
-                               size_t l = strlen(opts[i].name);
-                               if (strlen(a + 1) > l) {
-                                       if (memcmp(a + 1, opts[i].name, l) == 0) {
-                                               opt_ind++;
-                                               opt_arg = a + 1 + l;
-                                               return opts[i].value;
-                                       }
-                               }
-                       }
-               }
-       } /* end for */ 
-
-       return OPT_ERROR;
-}
-
 
 /******************** interne Function: print_usage ************************
 
@@ -219,7 +164,7 @@ Prints the correct usage syntax to stdout.
 
 ***************************************************************************/
 
-static void print_usage()
+static void usage()
 {
        printf("USAGE: cacao [options] classname [program arguments]\n");
        printf("Options:\n");
@@ -263,12 +208,17 @@ static void print_usage()
        printf("                 u(tf) ......... show the utf - hash\n");
        printf("          -i     n ............. activate inlining\n");
        printf("                 v ............. inline virtual methods\n");
+       printf("                                 uses/turns rt option on\n");
        printf("                 e ............. inline methods with exceptions\n");
        printf("                 p ............. optimize argument renaming\n");
        printf("                 o ............. inline methods of foreign classes\n");
        printf("          -rt .................. use rapid type analysis\n");
        printf("          -xta ................. use x type analysis\n");
        printf("          -vta ................. use variable type analysis\n");
+
+       /* exit with error code */
+
+       exit(1);
 }   
 
 
@@ -303,7 +253,7 @@ void exit_handler(void)
 
        MFREE(classpath, u1, strlen(classpath));
 
-       if (verbose || getcompilingtime || opt_stat) {
+       if (opt_verbose || getcompilingtime || opt_stat) {
                log_text("CACAO terminated");
                if (opt_stat) {
                        print_stats();
@@ -370,9 +320,10 @@ int main(int argc, char **argv)
        checknull = false;
        opt_noieee = false;
 
-       while ((i = get_opt(argc, argv)) != OPT_DONE) {
+       while ((i = get_opt(argc, argv, opts)) != OPT_DONE) {
                switch (i) {
-               case OPT_IGNORE: break;
+               case OPT_IGNORE:
+                       break;
                        
                case OPT_CLASSPATH:
                        /* forget old classpath and set the argument as new classpath */
@@ -393,8 +344,7 @@ int main(int argc, char **argv)
                                                goto didit;
                                        }
                                }
-                               print_usage();
-                               exit(10);
+                               usage();
                                        
                        didit: ;
                        }       
@@ -420,11 +370,11 @@ int main(int argc, char **argv)
                        break;
 
                case OPT_VERBOSE1:
-                       verbose = true;
+                       opt_verbose = true;
                        break;
 
                case OPT_VERBOSE:
-                       verbose = true;
+                       opt_verbose = true;
                        loadverbose = true;
                        linkverbose = true;
                        initverbose = true;
@@ -488,8 +438,7 @@ int main(int argc, char **argv)
                                        checksync = false;
                                        break;
                                default:
-                                       print_usage();
-                                       exit(10);
+                                       usage();
                                }
                        }
                        break;
@@ -543,8 +492,7 @@ int main(int argc, char **argv)
                                        showutf = true;
                                        break;
                                default:
-                                       print_usage();
-                                       exit(10);
+                                       usage();
                                }
                        }
                        break;
@@ -557,10 +505,17 @@ int main(int argc, char **argv)
                        for (j = 0; j < strlen(opt_arg); j++) {         
                                switch (opt_arg[j]) {
                                case 'n':
+                                    /* define in options.h; Used in main.c, jit.c & inline.c */
+                                    #ifdef INAFTERMAIN
+                                       useinliningm = true;
+                                       useinlining = false;
+                                    #else
                                        useinlining = true;
+                                    #endif
                                        break;
                                case 'v':
                                        inlinevirtuals = true;
+                                       opt_rt = true;
                                        break;
                                case 'e':
                                        inlineexceptions = true;
@@ -572,8 +527,7 @@ int main(int argc, char **argv)
                                        inlineoutsiders = true;
                                        break;
                                default:
-                                       print_usage();
-                                       exit(10);
+                                       usage();
                                }
                        }
                        break;
@@ -591,15 +545,25 @@ int main(int argc, char **argv)
                        break;
 
                default:
-                       print_usage();
-                       exit(10);
+                       usage();
                }
        }
 
-       if (opt_ind >= argc) {
-               print_usage();
-               exit(10);
+       if (opt_ind >= argc)
+               usage();
+
+#if 0
+       {
+               char *gnucp = "/home/twisti/src/cacao/cacaodev/classpath/lib/:";
+               cp = classpath;
+
+               classpath = MNEW(char, strlen(cp) + strlen(classpath) + 1);
+               strcpy(classpath, gnucp);
+               strcat(classpath, cp);
+
+               MFREE(cp, char, strlen(cp));
        }
+#endif
 
        mainstring = argv[opt_ind++];
        for (i = strlen(mainstring) - 1; i >= 0; i--) {     /* Transform dots into slashes */
@@ -610,7 +574,7 @@ int main(int argc, char **argv)
        /**************************** Program start *****************************/
 
        log_init(logfilename);
-       if (verbose)
+       if (opt_verbose)
                log_text("CACAO started -------------------------------------------------------");
 
        /* initialize the garbage collector */
@@ -636,10 +600,9 @@ int main(int argc, char **argv)
 
        loader_init((u1 *) &dummy);
 
-       jit_init();
-
        /* initialize exceptions used in the system */
-       init_system_exceptions();
+       if (!init_system_exceptions())
+               throw_main_exception_exit();
 
        native_loadclasses();
 
@@ -659,7 +622,7 @@ int main(int argc, char **argv)
 
        
        
-        jni_init();
+/*        jni_init(); */
        cacao_initializing = false;
 
        /************************* Start worker routines ********************/
@@ -667,6 +630,10 @@ int main(int argc, char **argv)
        if (startit) {
                methodinfo *mainmethod;
                java_objectarray *a; 
+               s4 status;
+
+               /* set return value to OK */
+               status = 0;
 
                /* create, load and link the main class */
                mainclass = class_new(utf_new_char(mainstring));
@@ -714,8 +681,10 @@ int main(int argc, char **argv)
                asm_calljavafunction(mainmethod, a, NULL, NULL, NULL);
 
                /* exception occurred? */
-               if (*exceptionptr)
+               if (*exceptionptr) {
                        throw_main_exception();
+                       status = 1;
+               }
 
 #if defined(USE_THREADS)
 #if defined(NATIVE_THREADS)
@@ -727,7 +696,7 @@ int main(int argc, char **argv)
 
                /* now exit the JavaVM */
 
-               cacao_exit(0);
+               cacao_exit(status);
        }
 
        /************* If requested, compile all methods ********************/
@@ -830,11 +799,10 @@ void cacao_exit(s4 status)
 {
        classinfo *c;
        methodinfo *m;
-       java_lang_Runtime *rt;
 
        /* class should already be loaded, but who knows... */
 
-       c = class_new(utf_new_char("java/lang/Runtime"));
+       c = class_new(utf_new_char("java/lang/System"));
 
        if (!class_load(c))
                throw_main_exception_exit();
@@ -842,29 +810,7 @@ void cacao_exit(s4 status)
        if (!class_link(c))
                throw_main_exception_exit();
 
-       /* first call Runtime.getRuntime()Ljava.lang.Runtime; */
-
-       m = class_resolveclassmethod(c,
-                                                                utf_new_char("getRuntime"),
-                                                                utf_new_char("()Ljava/lang/Runtime;"),
-                                                                class_java_lang_Object,
-                                                                true);
-
-       if (!m)
-               throw_main_exception_exit();
-
-       rt = (java_lang_Runtime *) asm_calljavafunction(m,
-                                                                                                       (void *) 0,
-                                                                                                       NULL,
-                                                                                                       NULL,
-                                                                                                       NULL);
-
-       /* exception occurred? */
-
-       if (*exceptionptr)
-               throw_main_exception_exit();
-
-       /* then call Runtime.exit(I)V */
+       /* call System.exit(I)V */
 
        m = class_resolveclassmethod(c,
                                                                 utf_new_char("exit"),
@@ -875,12 +821,25 @@ void cacao_exit(s4 status)
        if (!m)
                throw_main_exception_exit();
 
-       asm_calljavafunction(m, rt, (void *) 0, NULL, NULL);
+       /* call the exit function with passed exit status */
+
+       asm_calljavafunction(m,
+#if POINTERSIZE == 8
+                                                (void *) (s8) status,
+#else
+                                                (void *) status,
+#endif
+                                                NULL,
+                                                NULL,
+                                                NULL);
 
        /* this should never happen */
 
+       if (*exceptionptr)
+               throw_exception_exit();
+
        throw_cacao_exception_exit(string_java_lang_InternalError,
-                                                          "Problems with Runtime.exit(I)V");
+                                                          "System.exit(I)V returned without exception");
 }
 
 
@@ -893,7 +852,7 @@ void cacao_exit(s4 status)
 
 void cacao_shutdown(s4 status)
 {
-       if (verbose || getcompilingtime || opt_stat) {
+       if (opt_verbose || getcompilingtime || opt_stat) {
                log_text("CACAO terminated by shutdown");
                dolog("Exit status: %d\n", (s4) status);
        }