* src/vm/jit/verify/typecheck.c (verify_fieldaccess): New function.
[cacao.git] / src / vm / linker.c
index 42d6db75e1ff9fe2754b6811f6858b8ff2333cd0..2f7bb70102b5f53f38771416072f18850a638323 100644 (file)
@@ -32,7 +32,7 @@
             Edwin Steiner
             Christian Thalinger
 
-   $Id: linker.c 4768 2006-04-13 16:58:05Z edwin $
+   $Id: linker.c 5444 2006-09-09 19:25:24Z edwin $
 
 */
 
 
 #include "mm/memory.h"
 #include "native/native.h"
-#include "vm/builtin.h"
+
+#if defined(ENABLE_THREADS)
+# include "threads/native/lock.h"
+#else
+# include "threads/none/lock.h"
+#endif
+
 #include "vm/class.h"
 #include "vm/classcache.h"
 #include "vm/exceptions.h"
@@ -56,6 +62,8 @@
 #include "vm/stringlocal.h"
 #include "vm/access.h"
 #include "vm/rt-timing.h"
+#include "vm/vm.h"
+#include "vm/jit/asmpart.h"
 
 
 /* global variables ***********************************************************/
@@ -98,7 +106,7 @@ static classinfo *link_class_intern(classinfo *c);
 static arraydescriptor *link_array(classinfo *c);
 static void linker_compute_class_values(classinfo *c);
 static void linker_compute_subclasses(classinfo *c);
-static void linker_addinterface(classinfo *c, classinfo *ic);
+static bool linker_addinterface(classinfo *c, classinfo *ic);
 static s4 class_highestinterface(classinfo *c);
 
 
@@ -189,6 +197,9 @@ bool linker_init(void)
        if (!link_class(class_java_lang_ThreadGroup))
                return false;
 
+       if (!link_class(class_java_lang_VMSystem))
+               return false;
+
        if (!link_class(class_java_lang_VMThread))
                return false;
 
@@ -364,23 +375,17 @@ classinfo *link_class(classinfo *c)
 
        RT_TIMING_GET_TIME(time_start);
 
-       if (!c) {
+       if (c == NULL) {
                exceptions_throw_nullpointerexception();
                return NULL;
        }
 
-#if defined(USE_THREADS)
-       /* enter a monitor on the class */
-
-       builtin_monitorenter((java_objectheader *) c);
-#endif
+       LOCK_MONITOR_ENTER(c);
 
        /* maybe the class is already linked */
 
        if (c->state & CLASS_LINKED) {
-#if defined(USE_THREADS)
-               builtin_monitorexit((java_objectheader *) c);
-#endif
+               LOCK_MONITOR_EXIT(c);
 
                return c;
        }
@@ -388,10 +393,10 @@ classinfo *link_class(classinfo *c)
 #if defined(ENABLE_STATISTICS)
        /* measure time */
 
-       if (getcompilingtime)
+       if (opt_getcompilingtime)
                compilingtime_stop();
 
-       if (getloadingtime)
+       if (opt_getloadingtime)
                loadingtime_start();
 #endif
 
@@ -407,18 +412,14 @@ classinfo *link_class(classinfo *c)
 #if defined(ENABLE_STATISTICS)
        /* measure time */
 
-       if (getloadingtime)
+       if (opt_getloadingtime)
                loadingtime_stop();
 
-       if (getcompilingtime)
+       if (opt_getcompilingtime)
                compilingtime_start();
 #endif
 
-#if defined(USE_THREADS)
-       /* leave the monitor */
-
-       builtin_monitorexit((java_objectheader *) c);
-#endif
+       LOCK_MONITOR_EXIT(c);
 
        RT_TIMING_GET_TIME(time_end);
 
@@ -446,6 +447,14 @@ static classinfo *link_class_intern(classinfo *c)
        vftbl_t *v;                   /* vftbl of current class                   */
        s4 i,j;                       /* interface/method/field counter           */
        arraydescriptor *arraydesc;   /* descriptor for array classes             */
+#if defined(ENABLE_RT_TIMING)
+       struct timespec time_start, time_resolving, time_compute_vftbl,
+                                       time_abstract, time_compute_iftbl, time_fill_vftbl,
+                                       time_offsets, time_fill_iftbl, time_finalizer,
+                                       time_exceptions, time_subclasses;
+#endif
+
+       RT_TIMING_GET_TIME(time_start);
 
        /* the class is already linked */
 
@@ -557,11 +566,17 @@ static classinfo *link_class_intern(classinfo *c)
                                                                          "Cannot inherit from final class");
                        return NULL;
                }
+
+               /* link the superclass if necessary */
                
                if (!(super->state & CLASS_LINKED))
                        if (!link_class(super))
                                return NULL;
 
+               /* OR the ACC_CLASS_HAS_POINTERS flag */
+
+               c->flags |= (super->flags & ACC_CLASS_HAS_POINTERS);
+
                /* handle array classes */
 
                if (c->name->text[0] == '[')
@@ -574,11 +589,12 @@ static classinfo *link_class_intern(classinfo *c)
                        c->index = super->index + 1;
                
                c->instancesize = super->instancesize;
-               
+
                vftbllength = supervftbllength = super->vftbl->vftbllength;
                
                c->finalizer = super->finalizer;
        }
+       RT_TIMING_GET_TIME(time_resolving);
 
 
        /* compute vftbl length */
@@ -613,6 +629,21 @@ static classinfo *link_class_intern(classinfo *c)
                                                        return NULL;
                                                }
 
+                                               /* method m overwrites method j of class tc */
+
+#if defined(ENABLE_VERIFIER)
+                                               /* Add loading constraints (for the more general    */
+                                               /* types of method tc->methods[j]). --              */
+                                               /* Not for <init>,  as it is not invoked virtually. */
+                                               if ((m->name != utf_init)
+                                                       && !classcache_add_constraints_for_params(
+                                                                       c->classloader, tc->classloader,
+                                                                       &(tc->methods[j])))
+                                               {
+                                                       return NULL;
+                                               }
+#endif
+
                                                m->vftblindex = tc->methods[j].vftblindex;
                                                goto foundvftblindex;
                                        }
@@ -626,13 +657,15 @@ static classinfo *link_class_intern(classinfo *c)
                foundvftblindex:
                        ;
                }
-       }       
+       }
+       RT_TIMING_GET_TIME(time_compute_vftbl);
 
 
-       /* Check all interfaces of an abtract class (maybe be an interface
-          too) for unimplemented methods.  Such methods are called
-          miranda-methods and are marked with the ACC_MIRANDA flag.
-          VMClass.getDeclaredMethods does not return such methods. */
+       /* Check all interfaces of an abstract class (maybe be an
+          interface too) for unimplemented methods.  Such methods are
+          called miranda-methods and are marked with the ACC_MIRANDA
+          flag.  VMClass.getDeclaredMethods does not return such
+          methods. */
 
        if (c->flags & ACC_ABSTRACT) {
                classinfo  *ic;
@@ -643,7 +676,7 @@ static classinfo *link_class_intern(classinfo *c)
 
                abstractmethodscount = 0;
 
-               /* check all interfaces of the abtract class */
+               /* check all interfaces of the abstract class */
 
                for (i = 0; i < c->interfacescount; i++) {
                        ic = c->interfaces[i].cls;
@@ -714,6 +747,7 @@ static classinfo *link_class_intern(classinfo *c)
                        }
                }
        }
+       RT_TIMING_GET_TIME(time_abstract);
 
 
 #if defined(ENABLE_STATISTICS)
@@ -725,15 +759,16 @@ static classinfo *link_class_intern(classinfo *c)
        /* compute interfacetable length */
 
        interfacetablelength = 0;
-       tc = c;
-       while (tc) {
+
+       for (tc = c; tc != NULL; tc = tc->super.cls) {
                for (i = 0; i < tc->interfacescount; i++) {
                        s4 h = class_highestinterface(tc->interfaces[i].cls) + 1;
+
                        if (h > interfacetablelength)
                                interfacetablelength = h;
                }
-               tc = tc->super.cls;
        }
+       RT_TIMING_GET_TIME(time_compute_iftbl);
 
        /* allocate virtual function table */
 
@@ -742,11 +777,12 @@ static classinfo *link_class_intern(classinfo *c)
                                                          sizeof(methodptr*) * (interfacetablelength - (interfacetablelength > 0)));
        v = (vftbl_t *) (((methodptr *) v) +
                                         (interfacetablelength - 1) * (interfacetablelength > 1));
-       c->vftbl = v;
-       v->class = c;
-       v->vftbllength = vftbllength;
+
+       c->vftbl                = v;
+       v->class                = c;
+       v->vftbllength          = vftbllength;
        v->interfacetablelength = interfacetablelength;
-       v->arraydesc = arraydesc;
+       v->arraydesc            = arraydesc;
 
        /* store interface index in vftbl */
 
@@ -757,31 +793,49 @@ static classinfo *link_class_intern(classinfo *c)
 
        for (i = 0; i < supervftbllength; i++) 
                v->table[i] = super->vftbl->table[i];
-       
+
+       /* Fill the remaining vftbl slots with the AbstractMethodError
+          stub (all after the super class slots, because they are already
+          initialized). */
+
+       for (; i < vftbllength; i++)
+               v->table[i] = (methodptr) (ptrint) &asm_abstractmethoderror;
+
        /* add method stubs into virtual function table */
 
        for (i = 0; i < c->methodscount; i++) {
                methodinfo *m = &(c->methods[i]);
 
-               /* Methods in ABSTRACT classes from interfaces maybe already
-                  have a stubroutine. */
+               assert(m->stubroutine == NULL);
+
+               /* Don't create a compiler stub for abstract methods as they
+                  throw an AbstractMethodError with the default stub in the
+                  vftbl.  This entry is simply copied by sub-classes. */
+
+               if (m->flags & ACC_ABSTRACT)
+                       continue;
 
-               if (!m->stubroutine) {
 #if defined(ENABLE_JIT)
 # if defined(ENABLE_INTRP)
-                       if (opt_intrp)
-                               m->stubroutine = intrp_createcompilerstub(m);
-                       else
+               if (opt_intrp)
+                       m->stubroutine = intrp_createcompilerstub(m);
+               else
 #endif
-                               m->stubroutine = createcompilerstub(m);
+                       m->stubroutine = createcompilerstub(m);
 #else
-                       m->stubroutine = intrp_createcompilerstub(m);
+               m->stubroutine = intrp_createcompilerstub(m);
 #endif
-               }
 
-               if (!(m->flags & ACC_STATIC))
-                       v->table[m->vftblindex] = (methodptr) (ptrint) m->stubroutine;
+               /* static methods are not in the vftbl */
+
+               if (m->flags & ACC_STATIC)
+                       continue;
+
+               /* insert the stubroutine into the vftbl */
+
+               v->table[m->vftblindex] = (methodptr) (ptrint) m->stubroutine;
        }
+       RT_TIMING_GET_TIME(time_fill_vftbl);
 
        /* compute instance size and offset of each field */
        
@@ -791,14 +845,26 @@ static classinfo *link_class_intern(classinfo *c)
                
                if (!(f->flags & ACC_STATIC)) {
                        dsize = descriptor_typesize(f->parseddesc);
+
+                       /* On i386 we only align to 4 bytes even for double and s8.    */
+                       /* This matches what gcc does for struct members. We must      */
+                       /* do the same as gcc here because the offsets in native       */
+                       /* header structs like java_lang_Double must match the offsets */
+                       /* of the Java fields (eg. java.lang.Double.value).            */
+#if defined(__I386__)
+                       c->instancesize = ALIGN(c->instancesize, 4);
+#else
                        c->instancesize = ALIGN(c->instancesize, dsize);
+#endif
+
                        f->offset = c->instancesize;
                        c->instancesize += dsize;
                }
        }
+       RT_TIMING_GET_TIME(time_offsets);
 
        /* initialize interfacetable and interfacevftbllength */
-       
+
        v->interfacevftbllength = MNEW(s4, interfacetablelength);
 
 #if defined(ENABLE_STATISTICS)
@@ -810,12 +876,15 @@ static classinfo *link_class_intern(classinfo *c)
                v->interfacevftbllength[i] = 0;
                v->interfacetable[-i] = NULL;
        }
-       
+
        /* add interfaces */
-       
+
        for (tc = c; tc != NULL; tc = tc->super.cls)
                for (i = 0; i < tc->interfacescount; i++)
-                       linker_addinterface(c, tc->interfaces[i].cls);
+                       if (!linker_addinterface(c, tc->interfaces[i].cls))
+                               return NULL;
+
+       RT_TIMING_GET_TIME(time_fill_iftbl);
 
        /* add finalizer method (not for java.lang.Object) */
 
@@ -828,27 +897,39 @@ static classinfo *link_class_intern(classinfo *c)
                        if (!(fi->flags & ACC_STATIC))
                                c->finalizer = fi;
        }
+       RT_TIMING_GET_TIME(time_finalizer);
 
        /* resolve exception class references */
 
        for (i = 0; i < c->methodscount; i++) {
                methodinfo *m = &(c->methods[i]);
+               classinfo *exclass;
 
                for (j = 0; j < m->exceptiontablelength; j++) {
+                       /* skip NULL (catch all) entries */
                        if (!m->exceptiontable[j].catchtype.any)
                                continue;
-                       if (!resolve_classref_or_classinfo(NULL,
+
+                       /* try to resolve the class reference lazily */
+                       if (!resolve_classref_or_classinfo(m,
                                                                                           m->exceptiontable[j].catchtype,
-                                                                                          resolveEager, true, false,
-                                                                                          &(m->exceptiontable[j].catchtype.cls)))
+                                                                                          resolveLazy, true, false,
+                                                                                          &exclass))
                                return NULL;
+
+                       /* if resolved, enter the result of resolution in the table */
+                       if (exclass != NULL)
+                               m->exceptiontable[j].catchtype.cls = exclass;
                }
        }
+       RT_TIMING_GET_TIME(time_exceptions);
        
        /* final tasks */
 
        linker_compute_subclasses(c);
 
+       RT_TIMING_GET_TIME(time_subclasses);
+
        /* revert the linking state and class is linked */
 
        c->state = (c->state & ~CLASS_LINKING) | CLASS_LINKED;
@@ -858,6 +939,17 @@ static classinfo *link_class_intern(classinfo *c)
                log_message_class("Linking done class: ", c);
 #endif
 
+       RT_TIMING_TIME_DIFF(time_start        ,time_resolving    ,RT_TIMING_LINK_RESOLVE);
+       RT_TIMING_TIME_DIFF(time_resolving    ,time_compute_vftbl,RT_TIMING_LINK_C_VFTBL);
+       RT_TIMING_TIME_DIFF(time_compute_vftbl,time_abstract     ,RT_TIMING_LINK_ABSTRACT);
+       RT_TIMING_TIME_DIFF(time_abstract     ,time_compute_iftbl,RT_TIMING_LINK_C_IFTBL);
+       RT_TIMING_TIME_DIFF(time_compute_iftbl,time_fill_vftbl   ,RT_TIMING_LINK_F_VFTBL);
+       RT_TIMING_TIME_DIFF(time_fill_vftbl   ,time_offsets      ,RT_TIMING_LINK_OFFSETS);
+       RT_TIMING_TIME_DIFF(time_offsets      ,time_fill_iftbl   ,RT_TIMING_LINK_F_IFTBL);
+       RT_TIMING_TIME_DIFF(time_fill_iftbl   ,time_finalizer    ,RT_TIMING_LINK_FINALIZER);
+       RT_TIMING_TIME_DIFF(time_finalizer    ,time_exceptions   ,RT_TIMING_LINK_EXCEPTS);
+       RT_TIMING_TIME_DIFF(time_exceptions   ,time_subclasses   ,RT_TIMING_LINK_SUBCLASS);
+
        /* just return c to show that we didn't had a problem */
 
        return c;
@@ -1021,12 +1113,8 @@ static arraydescriptor *link_array(classinfo *c)
 
 static void linker_compute_subclasses(classinfo *c)
 {
-#if defined(USE_THREADS)
-#if defined(NATIVE_THREADS)
+#if defined(ENABLE_THREADS)
        compiler_lock();
-#else
-       intsDisable();
-#endif
 #endif
 
        if (!(c->flags & ACC_INTERFACE)) {
@@ -1045,12 +1133,8 @@ static void linker_compute_subclasses(classinfo *c)
 
        linker_compute_class_values(class_java_lang_Object);
 
-#if defined(USE_THREADS)
-#if defined(NATIVE_THREADS)
+#if defined(ENABLE_THREADS)
        compiler_unlock();
-#else
-       intsRestore();
-#endif
 #endif
 }
 
@@ -1084,30 +1168,39 @@ static void linker_compute_class_values(classinfo *c)
    Is needed by link_class for adding a VTBL to a class. All
    interfaces implemented by ic are added as well.
 
+   RETURN VALUE:
+      true.........everything ok
+         false........an exception has been thrown
+
 *******************************************************************************/
 
-static void linker_addinterface(classinfo *c, classinfo *ic)
+static bool linker_addinterface(classinfo *c, classinfo *ic)
 {
-       s4     j, m;
-       s4     i   = ic->index;
-       vftbl_t *v = c->vftbl;
+       s4          j, k;
+       vftbl_t    *v;
+       s4          i;
+       classinfo  *sc;
+       methodinfo *m;
 
-       if (i >= v->interfacetablelength) {
-               log_text("Inernal error: interfacetable overflow");
-               assert(0);
-       }
+       v = c->vftbl;
+       i = ic->index;
 
-       if (v->interfacetable[-i])
-               return;
+       if (i >= v->interfacetablelength)
+               vm_abort("Internal error: interfacetable overflow");
+
+       /* if this interface has already been added, return immediately */
+
+       if (v->interfacetable[-i] != NULL)
+               return true;
 
        if (ic->methodscount == 0) {  /* fake entry needed for subtype test */
                v->interfacevftbllength[i] = 1;
-               v->interfacetable[-i] = MNEW(methodptr, 1);
-               v->interfacetable[-i][0] = NULL;
-
-       else {
+               v->interfacetable[-i]      = MNEW(methodptr, 1);
+               v->interfacetable[-i][0]   = NULL;
+       }
+       else {
                v->interfacevftbllength[i] = ic->methodscount;
-               v->interfacetable[-i] = MNEW(methodptr, ic->methodscount);
+               v->interfacetable[-i]      = MNEW(methodptr, ic->methodscount);
 
 #if defined(ENABLE_STATISTICS)
                if (opt_stat)
@@ -1116,26 +1209,58 @@ static void linker_addinterface(classinfo *c, classinfo *ic)
 #endif
 
                for (j = 0; j < ic->methodscount; j++) {
-                       classinfo *sc = c;
+                       for (sc = c; sc != NULL; sc = sc->super.cls) {
+                               for (k = 0; k < sc->methodscount; k++) {
+                                       m = &(sc->methods[k]);
+
+                                       if (method_canoverwrite(m, &(ic->methods[j]))) {
+                                               /* method m overwrites the (abstract) method */
+#if defined(ENABLE_VERIFIER)
+                                               /* Add loading constraints (for the more
+                                                  general types of the method
+                                                  ic->methods[j]).  */
+                                               if (!classcache_add_constraints_for_params(
+                                                                       c->classloader, ic->classloader,
+                                                                       &(ic->methods[j])))
+                                               {
+                                                       return false;
+                                               }
+#endif
 
-                       while (sc) {
-                               for (m = 0; m < sc->methodscount; m++) {
-                                       methodinfo *mi = &(sc->methods[m]);
+                                               /* XXX taken from gcj */
+                                               /* check for ACC_STATIC: IncompatibleClassChangeError */
 
-                                       if (method_canoverwrite(mi, &(ic->methods[j]))) {
-                                               v->interfacetable[-i][j] = v->table[mi->vftblindex];
+                                               /* check for !ACC_PUBLIC: IllegalAccessError */
+
+                                               /* check for ACC_ABSTRACT: AbstracMethodError,
+                                                  not sure about that one */
+
+                                               v->interfacetable[-i][j] = v->table[m->vftblindex];
                                                goto foundmethod;
                                        }
                                }
-                               sc = sc->super.cls;
                        }
+
+                       /* If no method was found, insert the AbstractMethodError
+                          stub. */
+
+                       v->interfacetable[-i][j] =
+                               (methodptr) (ptrint) &asm_abstractmethoderror;
+
                foundmethod:
                        ;
                }
        }
 
-       for (j = 0; j < ic->interfacescount; j++) 
-               linker_addinterface(c, ic->interfaces[j].cls);
+       /* add superinterfaces of this interface */
+
+       for (j = 0; j < ic->interfacescount; j++)
+               if (!linker_addinterface(c, ic->interfaces[j].cls))
+                       return false;
+
+       /* everything ok */
+
+       return true;
 }