- added comments
[cacao.git] / main.c
diff --git a/main.c b/main.c
index 07818664e0c4628144913f6aa696312c897b7149..108651b793c3a4c75d544bc0319a1e4c32122c80 100644 (file)
--- a/main.c
+++ b/main.c
@@ -37,7 +37,7 @@
      - Calling the class loader
      - Running the main method
 
-   $Id: main.c 730 2003-12-11 21:23:31Z edwin $
+   $Id: main.c 841 2004-01-05 00:43:03Z twisti $
 
 */
 
 #include "toolbox/memory.h"
 #include "parseRTstats.h"
 #include "typeinfo.h" /* XXX remove debug */
+#include "nat/java_lang_Throwable.h"
+
+
+/* command line option */
 
-bool compileall = false;
 bool verbose =  false;
-bool runverbose = false;
+bool compileall = false;
+bool runverbose = false;       /* trace all method invocation                */
 bool collectverbose = false;
 
+bool loadverbose = false;
+bool linkverbose = false;
+bool initverbose = false;
+
+bool opt_rt = false;           /* true if RTA parse should be used     RT-CO */
+bool opt_xta = false;          /* true if XTA parse should be used    XTA-CO */
+bool opt_vta = false;          /* true if VTA parse should be used    VTA-CO */
+
+bool showmethods = false;
+bool showconstantpool = false;
+bool showutf = false;
+
+bool compileverbose =  false;  /* trace compiler actions                     */
+bool showstack = false;
+bool showdisassemble = false;  /* generate disassembler listing              */
+bool showddatasegment = false; /* generate data segment listing              */
+bool showintermediate = false; /* generate intermediate code listing         */
+
+bool useinlining = false;      /* use method inlining                        */
+bool inlinevirtuals = false;   /* inline unique virtual methods              */
+bool inlineexceptions = false; /* inline methods, that contain excptions     */
+bool inlineparamopt = false;   /* optimize parameter passing to inlined methods */
+bool inlineoutsiders = false;  /* inline methods, that are not member of the invoker's class */
+
+bool checkbounds = true;       /* check array bounds                         */
+bool checknull = true;         /* check null pointers                        */
+bool opt_noieee = false;       /* don't implement ieee compliant floats      */
+bool checksync = true;         /* do synchronization                         */
+bool opt_loops = false;        /* optimize array accesses in loops           */
+
+bool makeinitializations = true;
+
+bool getloadingtime = false;   /* to measure the runtime                     */
+s8 loadingtime = 0;
+
+bool getcompilingtime = false; /* compute compile time                       */
+s8 compilingtime = 0;          /* accumulated compile time                   */
+
+int has_ext_instr_set = 0;     /* has instruction set extensions */
+
+bool statistics = false;
+
+bool opt_verify = true;        /* true if classfiles should be verified      */
+
 
-static bool showmethods = false;
-static bool showconstantpool = false;
-static bool showutf = false;
 static classinfo *topclass;
 
 #ifndef USE_THREADS
@@ -110,6 +155,7 @@ void **stackbottom = 0;
 #define OPT_XTA         27 
 #define OPT_VTA         28
 #define OPT_VERBOSETC   29
+#define OPT_NOVERIFY    30
 
 
 struct {char *name; bool arg; int value;} opts[] = {
@@ -118,7 +164,7 @@ struct {char *name; bool arg; int value;} opts[] = {
        {"ms",          true,   OPT_MS},
        {"mx",          true,   OPT_MX},
        {"noasyncgc",   false,  OPT_IGNORE},
-       {"noverify",    false,  OPT_IGNORE},
+       {"noverify",    false,  OPT_NOVERIFY},
        {"oss",         true,   OPT_IGNORE},
        {"ss",          true,   OPT_IGNORE},
        {"v",           false,  OPT_VERBOSE1},
@@ -223,6 +269,7 @@ static void print_usage()
 #if defined(__ALPHA__)
        printf("          -noieee .............. don't use ieee compliant arithmetic\n");
 #endif
+       printf("          -noverify ............ don't verify classfiles\n");
        printf("          -softnull ............ use software nullpointer check\n");
        printf("          -time ................ measure the runtime\n");
        printf("          -stat ................ detailed compiler statistics\n");
@@ -260,20 +307,39 @@ static void print_usage()
 
 static void print_times()
 {
-       long int totaltime = getcputime();
-       long int runtime = totaltime - loadingtime - compilingtime;
+       s8 totaltime = getcputime();
+       s8 runtime = totaltime - loadingtime - compilingtime;
        char logtext[MAXLOGTEXT];
 
+#if defined(__I386__)
+       sprintf(logtext, "Time for loading classes: %lld secs, %lld millis",
+#else
        sprintf(logtext, "Time for loading classes: %ld secs, %ld millis",
+#endif
                        loadingtime / 1000000, (loadingtime % 1000000) / 1000);
        log_text(logtext);
+
+#if defined(__I386__)
+       sprintf(logtext, "Time for compiling code:  %lld secs, %lld millis",
+#else
        sprintf(logtext, "Time for compiling code:  %ld secs, %ld millis",
+#endif
                        compilingtime / 1000000, (compilingtime % 1000000) / 1000);
        log_text(logtext);
+
+#if defined(__I386__)
+       sprintf(logtext, "Time for running program: %lld secs, %lld millis",
+#else
        sprintf(logtext, "Time for running program: %ld secs, %ld millis",
+#endif
                        runtime / 1000000, (runtime % 1000000) / 1000);
        log_text(logtext);
+
+#if defined(__I386__)
+       sprintf(logtext, "Total time: %lld secs, %lld millis",
+#else
        sprintf(logtext, "Total time: %ld secs, %ld millis",
+#endif
                        totaltime / 1000000, (totaltime % 1000000) / 1000);
        log_text(logtext);
 }
@@ -456,16 +522,15 @@ static void print_stats()
 }
 
 
-
 /********** Function: class_compile_methods   (debugging only) ********/
 
-void class_compile_methods ()
+void class_compile_methods()
 {
        int        i;
        classinfo  *c;
        methodinfo *m;
        
-       c = list_first (&linkedclasses);
+       c = list_first(&linkedclasses);
        while (c) {
                for (i = 0; i < c -> methodscount; i++) {
                        m = &(c->methods[i]);
@@ -473,12 +538,11 @@ void class_compile_methods ()
                                (void) jit_compile(m);
                        }
                }
-               c = list_next (&linkedclasses, c);
+               c = list_next(&linkedclasses, c);
        }
 }
 
 
-
 /*
  * void exit_handler(void)
  * -----------------------
@@ -490,8 +554,8 @@ void exit_handler(void)
        /********************* Print debug tables ************************/
                                
        if (showmethods) class_showmethods(topclass);
-       if (showconstantpool)  class_showconstantpool(topclass);
-       if (showutf)           utf_show();
+       if (showconstantpool) class_showconstantpool(topclass);
+       if (showutf) utf_show();
 
 #ifdef USE_THREADS
        clear_thread_flags();           /* restores standard file descriptor
@@ -508,7 +572,7 @@ void exit_handler(void)
        tables_close(literalstring_free);
 
        if (verbose || getcompilingtime || statistics) {
-               log_text ("CACAO terminated");
+               log_text("CACAO terminated");
                if (statistics)
                        print_stats();
                if (getcompilingtime)
@@ -518,7 +582,6 @@ void exit_handler(void)
 }
 
 
-
 /************************** Function: main *******************************
 
    The main program.
@@ -527,9 +590,8 @@ void exit_handler(void)
 
 int main(int argc, char **argv)
 {
-       s4 i,j;
+       s4 i, j;
        char *cp;
-       java_objectheader *local_exceptionptr = 0;
        void *dummy;
        
        /********** interne (nur fuer main relevante Optionen) **************/
@@ -537,7 +599,7 @@ int main(int argc, char **argv)
        char logfilename[200] = "";
        u4 heapsize = 64000000;
        u4 heapstartsize = 200000;
-       char classpath[500] = ".:/usr/local/lib/java/classes";
+       char classpath[500] = ".";
        bool startit = true;
        char *specificmethodname = NULL;
        char *specificsignature = NULL;
@@ -575,7 +637,7 @@ int main(int argc, char **argv)
                                int n;
                                int l = strlen(opt_arg);
                                for (n = 0; n < l; n++) {
-                                       if (opt_arg[n]=='=') {
+                                       if (opt_arg[n] == '=') {
                                                opt_arg[n] = '\0';
                                                attach_property(opt_arg, opt_arg + n + 1);
                                                goto didit;
@@ -631,6 +693,10 @@ int main(int argc, char **argv)
                        opt_noieee = true;
                        break;
 
+               case OPT_NOVERIFY:
+                       opt_verify = false;
+                       break;
+
                case OPT_SOFTNULL:
                        checknull = true;
                        break;
@@ -776,25 +842,21 @@ int main(int argc, char **argv)
                log_text("CACAO started -------------------------------------------------------");
        }
 
-       suck_init (classpath);
-       native_setclasspath (classpath);
+       native_setclasspath(classpath);
                
        tables_init();
+       suck_init(classpath);
+
        heap_init(heapsize, heapstartsize, &dummy);
 
-       
-       
        log_text("calling jit_init");
        jit_init();
 
-
-
        log_text("calling loader_init");
-
-       loader_init((u1*)&dummy);
+       loader_init((u1 *) &dummy);
 
        log_text("calling native_loadclasses");
-       native_loadclasses ();
+       native_loadclasses();
 
 
        /*********************** Load JAVA classes  ***************************/
@@ -804,26 +866,30 @@ int main(int argc, char **argv)
                if (cp[i] == '.') cp[i] = '/';          /* in the class name */
        }
 
+       /*printf("-------------------->%s\n",cp);*/
        topclass = loader_load(utf_new_char(cp));
+       /*class_showmethods(topclass);  */
 
        if (exceptionptr != 0) {
-               printf("#### Class loader has thrown: ");
+               printf("Exception in thread \"main\" ");
                utf_display(exceptionptr->vftbl->class->name);
+               printf(": ");
+               utf_display(javastring_toutf(((java_lang_Throwable *) exceptionptr)->detailMessage, false));
                printf("\n");
 
                exceptionptr = 0;
        }
 
        if (topclass == 0) {
-               printf("#### Could not find top class - exiting\n");
+               /* should we print out something? we already have the exception */
                exit(1);
        }
 
+       /* initialize the garbage collector */
+       gc_init();
 
-
-       gc_init();      
 #ifdef USE_THREADS
-       initThreads((u1*) &dummy);                   /* schani */
+       initThreads((u1*) &dummy);
 #endif
 
 
@@ -833,19 +899,23 @@ int main(int argc, char **argv)
                methodinfo *mainmethod;
                java_objectarray *a; 
 
-               heap_addreference((void**) &a);
+/*             heap_addreference((void**) &a); */
+
+               mainmethod = class_findmethod(topclass,
+                                                                         utf_new_char("main"), 
+                                                                         utf_new_char("([Ljava/lang/String;)V")
+                                                                         );
+
+               /* there is no main method or it isn't static */
+               if (!mainmethod || !(mainmethod->flags & ACC_STATIC)) {
+                       printf("Exception in thread \"main\" java.lang.NoSuchMethodError: main\n");
+                       exit(1);
+               }
 
-               mainmethod = class_findmethod (
-                                                                          topclass,
-                                                                          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(utf_new_char(argv[i]));
+                       a->data[i - opt_ind] = 
+                               (java_objectheader *) javastring_new(utf_new_char(argv[i]));
                }
 
 #ifdef TYPEINFO_DEBUG_TEST
@@ -853,18 +923,24 @@ int main(int argc, char **argv)
                typeinfo_test();
 #endif
                /*class_showmethods(currentThread->group->header.vftbl->class); */
+
+               /* here we go... */
+               asm_calljavafunction(mainmethod, a, NULL, NULL, NULL);
        
-               local_exceptionptr = asm_calljavamethod (mainmethod, a, NULL, NULL, NULL );
-       
-               if (local_exceptionptr) {
+               if (exceptionptr) {
                        printf("Exception in thread \"main\" ");
-                       utf_display(local_exceptionptr->vftbl->class->name);
+                       utf_display(exceptionptr->vftbl->class->name);
+
+                       /* do we have a detail message? */
+                       if (((java_lang_Throwable *) exceptionptr)->detailMessage) {
+                               printf(": ");
+                               utf_display(javastring_toutf(((java_lang_Throwable *) exceptionptr)->detailMessage, false));
+                       }
                        printf("\n");
                }
-               /*---RTAprint---*/
 
 #ifdef USE_THREADS
-               killThread(currentThread);
+               killThread(currentThread);
 #endif
                fprintf(stderr, "still here\n");
        }
@@ -880,15 +956,20 @@ int main(int argc, char **argv)
 
        if (specificmethodname) {
                methodinfo *m;
-               if (specificsignature)
+               if (specificsignature) {
                        m = class_findmethod(topclass, 
                                                                 utf_new_char(specificmethodname),
                                                                 utf_new_char(specificsignature));
-               else
+               } else {
                        m = class_findmethod(topclass, 
-                                                                utf_new_char(specificmethodname), NULL);
-               if (!m) panic ("Specific method not found");
-               (void) jit_compile(m);
+                                                                utf_new_char(specificmethodname),
+                                                                NULL);
+               }
+
+               if (!m)
+                       panic("Specific method not found");
+               
+               jit_compile(m);
        }
 
        exit(0);