* Merged with 3aaaa9f60c5d.
[cacao.git] / src / vm / builtin.c
index 190ea15e9c96bb5961dc3c05aec3379df64d9494..23588740155e7dfbcec5a2bee2948b2fdd371632 100644 (file)
@@ -28,8 +28,6 @@
    calls instead of machine instructions, using the C calling
    convention.
 
-   $Id: builtin.c 8348 2007-08-19 09:27:03Z pm $
-
 */
 
 
 
 #include "native/jni.h"
 #include "native/llni.h"
-#include "native/include/java_lang_String.h"
-#include "native/include/java_lang_Throwable.h"
 
 #include "threads/lock-common.h"
-#if defined(ENABLE_THREADS)
-#include "threads/native/threads.h"
-#else
-#include "threads/none/threads.h"
-#endif
+#include "threads/threads-common.h"
 
 #include "toolbox/logging.h"
 #include "toolbox/util.h"
@@ -79,6 +71,7 @@
 #include "vm/stringlocal.h"
 
 #include "vm/jit/asmpart.h"
+#include "vm/jit/trace.h"
 
 #include "vmcore/class.h"
 #include "vmcore/linker.h"
@@ -184,6 +177,11 @@ static bool builtintable_init(void)
                                                                                                        bte->descriptor,
                                                                                                        ACC_STATIC | ACC_METHOD_BUILTIN,
                                                                                                        NULL);
+
+               /* generate a builtin stub if we need one */
+
+               if (bte->flags & BUILTINTABLE_FLAG_STUB)
+                       codegen_generate_stub_builtin(bte);
        }
 
        for (bte = builtintable_automatic; bte->fp != NULL; bte++) {
@@ -192,6 +190,10 @@ static bool builtintable_init(void)
                                                                                                        bte->descriptor,
                                                                                                        ACC_STATIC | ACC_METHOD_BUILTIN,
                                                                                                        NULL);
+
+               /* no stubs should be needed for this table */
+
+               assert(!bte->flags & BUILTINTABLE_FLAG_STUB);
        }
 
        for (bte = builtintable_function; bte->fp != NULL; bte++) {
@@ -200,6 +202,11 @@ static bool builtintable_init(void)
                                                                                                        bte->descriptor,
                                                                                                        ACC_STATIC | ACC_METHOD_BUILTIN,
                                                                                                        NULL);
+
+               /* generate a builtin stub if we need one */
+
+               if (bte->flags & BUILTINTABLE_FLAG_STUB)
+                       codegen_generate_stub_builtin(bte);
        }
 
        /* release dump area */
@@ -317,7 +324,8 @@ builtintable_entry *builtintable_get_automatic(s4 opcode)
                if (middle->opcode < opcode) {
                        first = middle + 1;
                        entries -= half + 1;
-               } else
+               }
+               else
                        entries = half;
        }
 
@@ -362,14 +370,15 @@ bool builtintable_replace_function(void *iptr_)
 
        for (bte = builtintable_function; bte->fp != NULL; bte++) {
                if ((METHODREF_CLASSNAME(mr) == bte->classname) &&
-                       (mr->name             == bte->name) &&
-                       (mr->descriptor       == bte->descriptor)) {
+                       (mr->name                == bte->name) &&
+                       (mr->descriptor          == bte->descriptor)) {
 
                        /* set the values in the instruction */
 
-                       iptr->opc   = bte->opcode;
+                       iptr->opc           = bte->opcode;
                        iptr->sx.s23.s3.bte = bte;
-                       if (bte->checkexception)
+
+                       if (bte->flags & BUILTINTABLE_FLAG_EXCEPTION)
                                iptr->flags.bits |= INS_FLAG_CHECK;
                        else
                                iptr->flags.bits &= ~INS_FLAG_CHECK;
@@ -400,10 +409,14 @@ bool builtintable_replace_function(void *iptr_)
 
 s4 builtin_instanceof(java_handle_t *o, classinfo *class)
 {
+       classinfo *c;
+
        if (o == NULL)
                return 0;
 
-       return class_isanysubclass(o->vftbl->class, class);
+       LLNI_class_get(o, c);
+
+       return class_isanysubclass(c, class);
 }
 
 
@@ -417,10 +430,14 @@ s4 builtin_instanceof(java_handle_t *o, classinfo *class)
 
 s4 builtin_checkcast(java_handle_t *o, classinfo *class)
 {
+       classinfo *c;
+
        if (o == NULL)
                return 1;
 
-       if (class_isanysubclass(o->vftbl->class, class))
+       LLNI_class_get(o, c);
+
+       if (class_isanysubclass(c, class))
                return 1;
 
        return 0;
@@ -492,7 +509,7 @@ s4 builtin_arraycheckcast(java_handle_t *o, classinfo *targetclass)
        if (o == NULL)
                return 1;
 
-       desc = o->vftbl->arraydesc;
+       desc = LLNI_vftbl_direct(o)->arraydesc;
 
        if (desc == NULL)
                return 0;
@@ -517,70 +534,13 @@ s4 builtin_arrayinstanceof(java_handle_t *o, classinfo *targetclass)
 
 *******************************************************************************/
 
-void *builtin_throw_exception(java_handle_t *xptr)
+void *builtin_throw_exception(java_object_t *xptr)
 {
 #if !defined(NDEBUG)
-    java_lang_Throwable *t;
-       java_lang_String    *s;
-       char                *logtext;
-       s4                   logtextlen;
-       s4                   dumpsize;
-
-       if (opt_verbose) {
-               t = (java_lang_Throwable *) xptr;
-
-               /* get detail message */
-               if (t)
-                       LLNI_field_get_ref(t, detailMessage, s);
-
-               /* calculate message length */
-
-               logtextlen = strlen("Builtin exception thrown: ") + strlen("0");
-
-               if (t) {
-                       logtextlen +=
-                               utf_bytes(xptr->vftbl->class->name);
-                       if (s) {
-                               logtextlen += strlen(": ") +
-                                       u2_utflength(LLNI_field_direct(s, value)->data 
-                                                                       + LLNI_field_direct(s, offset),
-                                                        LLNI_field_direct(s,count));
-                       }
-               } 
-               else {
-                       logtextlen += strlen("(nil)");
-               }
-
-               /* allocate memory */
-
-               dumpsize = dump_size();
-
-               logtext = DMNEW(char, logtextlen);
-
-               strcpy(logtext, "Builtin exception thrown: ");
-
-               if (t) {
-                       utf_cat_classname(logtext, xptr->vftbl->class->name);
+       /* print exception trace */
 
-                       if (s) {
-                               char *buf;
-
-                               buf = javastring_tochar((java_handle_t *) s);
-                               strcat(logtext, ": ");
-                               strcat(logtext, buf);
-                               MFREE(buf, char, strlen(buf) + 1);
-                       }
-
-               } else {
-                       strcat(logtext, "(nil)");
-               }
-
-               log_text(logtext);
-
-               /* release memory */
-
-               dump_release(dumpsize);
-       }
+       if (opt_TraceExceptions)
+               trace_exception_builtin(xptr);
 #endif /* !defined(NDEBUG) */
 
        /* actually set the exception */
@@ -604,6 +564,25 @@ void *builtin_throw_exception(java_handle_t *xptr)
 *******************************************************************************/
 
 s4 builtin_canstore(java_handle_objectarray_t *oa, java_handle_t *o)
+{
+       int result;
+
+       LLNI_CRITICAL_START;
+
+       result = builtin_fast_canstore(LLNI_DIRECT(oa), LLNI_UNWRAP(o));
+
+       LLNI_CRITICAL_END;
+
+       /* if not possible, throw an exception */
+
+       if (result == 0)
+               exceptions_throw_arraystoreexception();
+
+       return result;
+}
+
+
+s4 builtin_fast_canstore(java_objectarray_t *oa, java_object_t *o)
 {
        arraydescriptor *desc;
        arraydescriptor *valuedesc;
@@ -666,11 +645,6 @@ s4 builtin_canstore(java_handle_objectarray_t *oa, java_handle_t *o)
                result = builtin_descriptorscompatible(valuedesc, componentvftbl->arraydesc);
        }
 
-       /* if not possible, throw an exception */
-
-       if (result == 0)
-               exceptions_throw_arraystoreexception();
-
        /* return result */
 
        return result;
@@ -678,7 +652,7 @@ s4 builtin_canstore(java_handle_objectarray_t *oa, java_handle_t *o)
 
 
 /* This is an optimized version where a is guaranteed to be one-dimensional */
-s4 builtin_canstore_onedim (java_handle_objectarray_t *a, java_handle_t *o)
+s4 builtin_canstore_onedim (java_objectarray_t *a, java_object_t *o)
 {
        arraydescriptor *desc;
        vftbl_t         *elementvftbl;
@@ -729,7 +703,7 @@ s4 builtin_canstore_onedim (java_handle_objectarray_t *a, java_handle_t *o)
 
 /* This is an optimized version where a is guaranteed to be a
  * one-dimensional array of a class type */
-s4 builtin_canstore_onedim_class(java_handle_objectarray_t *a, java_handle_t *o)
+s4 builtin_canstore_onedim_class(java_objectarray_t *a, java_object_t *o)
 {
        vftbl_t  *elementvftbl;
        vftbl_t  *valuevftbl;
@@ -773,7 +747,7 @@ s4 builtin_canstore_onedim_class(java_handle_objectarray_t *a, java_handle_t *o)
 
    Return value: pointer to the object or NULL if no memory is
    available
-                       
+
 *******************************************************************************/
 
 java_handle_t *builtin_new(classinfo *c)
@@ -816,8 +790,69 @@ java_handle_t *builtin_new(classinfo *c)
                        return NULL;
        }
 
-       o = heap_allocate(c->instancesize, c->flags & ACC_CLASS_HAS_POINTERS,
-                                         c->finalizer);
+       o = heap_alloc(c->instancesize, c->flags & ACC_CLASS_HAS_POINTERS,
+                                  c->finalizer, true);
+
+       if (!o)
+               return NULL;
+
+       o->vftbl = c->vftbl;
+
+#if defined(ENABLE_THREADS)
+       lock_init_object_lock(o);
+#endif
+
+       CYCLES_STATS_GET(cycles_end);
+       RT_TIMING_GET_TIME(time_end);
+
+       CYCLES_STATS_COUNT(builtin_new,cycles_end - cycles_start);
+       RT_TIMING_TIME_DIFF(time_start, time_end, RT_TIMING_NEW_OBJECT);
+
+       return o;
+}
+
+
+/* builtin_fast_new ************************************************************
+
+   Creates a new instance of class c on the heap.
+
+   Return value: pointer to the object or NULL if no fast return
+   is possible for any reason.
+
+*******************************************************************************/
+
+java_object_t *builtin_fast_new(classinfo *c)
+{
+       java_object_t *o;
+#if defined(ENABLE_RT_TIMING)
+       struct timespec time_start, time_end;
+#endif
+#if defined(ENABLE_CYCLES_STATS)
+       u8 cycles_start, cycles_end;
+#endif
+
+       RT_TIMING_GET_TIME(time_start);
+       CYCLES_STATS_GET(cycles_start);
+
+       /* is the class loaded */
+
+       assert(c->state & CLASS_LOADED);
+
+       /* check if we can instantiate this class */
+
+       if (c->flags & ACC_ABSTRACT)
+               return NULL;
+
+       /* is the class linked */
+
+       if (!(c->state & CLASS_LINKED))
+               return NULL;
+
+       if (!(c->state & CLASS_INITIALIZED))
+               return NULL;
+
+       o = heap_alloc(c->instancesize, c->flags & ACC_CLASS_HAS_POINTERS,
+                                  c->finalizer, false);
 
        if (!o)
                return NULL;
@@ -878,7 +913,7 @@ java_handle_t *builtin_newarray(s4 size, classinfo *arrayclass)
                return NULL;
        }
 
-       a = heap_allocate(actualsize, (desc->arraytype == ARRAYTYPE_OBJECT), NULL);
+       a = heap_alloc(actualsize, (desc->arraytype == ARRAYTYPE_OBJECT), NULL, true);
 
        if (a == NULL)
                return NULL;
@@ -1118,8 +1153,8 @@ static java_handle_t *builtin_multianewarray_intern(int n,
 
                if (!ea)
                        return NULL;
-               
-               ((java_handle_objectarray_t *) a)->data[i] = (java_object_t *) ea;
+
+               LLNI_objectarray_element_set((java_handle_objectarray_t *) a, i, ea);
        }
 
        return a;
@@ -1157,7 +1192,8 @@ java_handle_objectarray_t *builtin_multianewarray(int n, classinfo *arrayclass,
 
        /* now call the real function */
 
-       return builtin_multianewarray_intern(n, arrayclass, dims);
+       return (java_handle_objectarray_t *)
+               builtin_multianewarray_intern(n, arrayclass, dims);
 }
 
 
@@ -1169,156 +1205,6 @@ java_handle_objectarray_t *builtin_multianewarray(int n, classinfo *arrayclass,
        
 *****************************************************************************/
 
-#if !defined(NDEBUG)
-static s4 methodindent = 0;
-static u4 callcount = 0;
-
-java_handle_t *builtin_trace_exception(java_handle_t *xptr,
-                                                                          methodinfo *m,
-                                                                          void *pos,
-                                                                          s4 indent)
-{
-       char *logtext;
-       s4    logtextlen;
-       s4    dumpsize;
-       codeinfo *code;
-
-#if defined(ENABLE_DEBUG_FILTER)
-       if (! show_filters_test_verbosecall_exit(m)) return xptr;
-#endif
-
-#if defined(ENABLE_VMLOG)
-       return xptr;
-#endif
-
-       if (opt_verbosecall && indent)
-#if defined(__S390__)
-               TRACEJAVACALLINDENT--;
-#else
-               methodindent--;
-#endif
-
-       /* calculate message length */
-
-       if (xptr) {
-               logtextlen =
-                       strlen("Exception ") + utf_bytes(xptr->vftbl->class->name);
-       } 
-       else {
-               logtextlen = strlen("Some Throwable");
-       }
-
-       logtextlen += strlen(" thrown in ");
-
-       if (m) {
-               logtextlen +=
-                       utf_bytes(m->class->name) +
-                       strlen(".") +
-                       utf_bytes(m->name) +
-                       utf_bytes(m->descriptor) +
-                       strlen("(NOSYNC,NATIVE");
-
-#if SIZEOF_VOID_P == 8
-               logtextlen +=
-                       strlen(")(0x123456789abcdef0) at position 0x123456789abcdef0 (");
-#else
-               logtextlen += strlen(")(0x12345678) at position 0x12345678 (");
-#endif
-
-               if (m->class->sourcefile == NULL)
-                       logtextlen += strlen("<NO CLASSFILE INFORMATION>");
-               else
-                       logtextlen += utf_bytes(m->class->sourcefile);
-
-               logtextlen += strlen(":65536)");
-
-       } 
-       else {
-               logtextlen += strlen("call_java_method");
-       }
-
-       logtextlen += strlen("0");
-
-       /* allocate memory */
-
-       dumpsize = dump_size();
-
-       logtext = DMNEW(char, logtextlen);
-
-       if (xptr) {
-               strcpy(logtext, "Exception ");
-               utf_cat_classname(logtext, xptr->vftbl->class->name);
-
-       } else {
-               strcpy(logtext, "Some Throwable");
-       }
-
-       strcat(logtext, " thrown in ");
-
-       if (m) {
-               utf_cat_classname(logtext, m->class->name);
-               strcat(logtext, ".");
-               utf_cat(logtext, m->name);
-               utf_cat(logtext, m->descriptor);
-
-               if (m->flags & ACC_SYNCHRONIZED)
-                       strcat(logtext, "(SYNC");
-               else
-                       strcat(logtext, "(NOSYNC");
-
-               if (m->flags & ACC_NATIVE) {
-                       strcat(logtext, ",NATIVE");
-
-                       code = m->code;
-
-#if SIZEOF_VOID_P == 8
-                       sprintf(logtext + strlen(logtext),
-                                       ")(0x%016lx) at position 0x%016lx",
-                                       (ptrint) code->entrypoint, (ptrint) pos);
-#else
-                       sprintf(logtext + strlen(logtext),
-                                       ")(0x%08x) at position 0x%08x",
-                                       (ptrint) code->entrypoint, (ptrint) pos);
-#endif
-
-               } else {
-
-                       /* XXX preliminary: This should get the actual codeinfo */
-                       /* in which the exception happened.                     */
-                       code = m->code;
-                       
-#if SIZEOF_VOID_P == 8
-                       sprintf(logtext + strlen(logtext),
-                                       ")(0x%016lx) at position 0x%016lx (",
-                                       (ptrint) code->entrypoint, (ptrint) pos);
-#else
-                       sprintf(logtext + strlen(logtext),
-                                       ")(0x%08x) at position 0x%08x (",
-                                       (ptrint) code->entrypoint, (ptrint) pos);
-#endif
-
-                       if (m->class->sourcefile == NULL)
-                               strcat(logtext, "<NO CLASSFILE INFORMATION>");
-                       else
-                               utf_cat(logtext, m->class->sourcefile);
-
-                       sprintf(logtext + strlen(logtext), ":%d)", 0);
-               }
-
-       } else
-               strcat(logtext, "call_java_method");
-
-       log_text(logtext);
-
-       /* release memory */
-
-       dump_release(dumpsize);
-
-       return xptr;
-}
-#endif /* !defined(NDEBUG) */
-
-
 /* builtin_print_argument ******************************************************
 
    Prints arguments and return values for the call trace.
@@ -1330,7 +1216,7 @@ static char *builtin_print_argument(char *logtext, s4 *logtextlen,
                                                                        typedesc *paramtype, s8 value)
 {
        imm_union          imu;
-       java_handle_t     *o;
+       java_object_t     *o;
        classinfo         *c;
        utf               *u;
        u4                 len;
@@ -1378,7 +1264,7 @@ static char *builtin_print_argument(char *logtext, s4 *logtextlen,
 
                /* cast to java.lang.Object */
 
-               o = (java_handle_t *) (ptrint) value;
+               o = (java_object_t *) (ptrint) value;
 
                /* check return argument for java.lang.Class or java.lang.String */
 
@@ -1466,6 +1352,8 @@ void builtin_verbosecall_enter(s8 a0, s8 a1,
        s4          dumpsize;
        s4          i;
        s4          pos;
+       int         methodindent;
+       int         callcount;
 
 #if defined(ENABLE_DEBUG_FILTER)
        if (! show_filters_test_verbosecall_enter(m)) return;
@@ -1478,6 +1366,8 @@ void builtin_verbosecall_enter(s8 a0, s8 a1,
 
        md = m->parseddesc;
 
+       methodindent = TRACEJAVACALLINDENT;
+
        /* calculate message length */
 
        logtextlen =
@@ -1520,7 +1410,7 @@ void builtin_verbosecall_enter(s8 a0, s8 a1,
 
        logtext = DMNEW(char, logtextlen);
 
-       callcount++;
+       callcount = ++TRACEJAVACALLCOUNT;
 
        sprintf(logtext, "%10d ", callcount);
        sprintf(logtext + strlen(logtext), "-%d-", methodindent);
@@ -1624,7 +1514,7 @@ void builtin_verbosecall_enter(s8 a0, s8 a1,
 
        dump_release(dumpsize);
 
-       methodindent++;
+       TRACEJAVACALLINDENT++;
 
 }
 #endif
@@ -1647,6 +1537,7 @@ void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m)
        s4          i;
        s4          pos;
        imm_union   val;
+       int         methodindent;
 
 #if defined(ENABLE_DEBUG_FILTER)
        if (! show_filters_test_verbosecall_exit(m)) return;
@@ -1659,6 +1550,15 @@ void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m)
 
        md = m->parseddesc;
 
+       /* outdent the log message */
+
+       if (TRACEJAVACALLINDENT)
+               TRACEJAVACALLINDENT--;
+       else
+               log_text("WARNING: unmatched methodindent--");
+
+       methodindent = TRACEJAVACALLINDENT;
+
        /* calculate message length */
 
        logtextlen =
@@ -1682,13 +1582,6 @@ void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m)
 
        logtext = DMNEW(char, logtextlen);
 
-       /* outdent the log message */
-
-       if (methodindent)
-               methodindent--;
-       else
-               log_text("WARNING: unmatched methodindent--");
-
        /* generate the message */
 
        sprintf(logtext, "           ");
@@ -2621,15 +2514,17 @@ bool builtin_arraycopy(java_handle_t *src, s4 srcStart,
 
                java_handle_objectarray_t *oas = (java_handle_objectarray_t *) src;
                java_handle_objectarray_t *oad = (java_handle_objectarray_t *) dest;
-                
                if (destStart <= srcStart) {
                        for (i = 0; i < len; i++) {
-                               java_handle_t *o = oas->data[srcStart + i];
+                               java_handle_t *o;
+
+                               LLNI_objectarray_element_get(oas, srcStart + i, o);
 
                                if (!builtin_canstore(oad, o))
                                        return false;
 
-                               oad->data[destStart + i] = o;
+                               LLNI_objectarray_element_set(oad, destStart + i, o);
                        }
                }
                else {
@@ -2640,12 +2535,14 @@ bool builtin_arraycopy(java_handle_t *src, s4 srcStart,
                           index have been copied before the throw. */
 
                        for (i = len - 1; i >= 0; i--) {
-                               java_handle_t *o = oas->data[srcStart + i];
+                               java_handle_t *o;
+
+                               LLNI_objectarray_element_get(oas, srcStart + i, o);
 
                                if (!builtin_canstore(oad, o))
                                        return false;
 
-                               oad->data[destStart + i] = o;
+                               LLNI_objectarray_element_set(oad, destStart + i, o);
                        }
                }
        }
@@ -2706,7 +2603,7 @@ java_handle_t *builtin_clone(void *env, java_handle_t *o)
 
        /* get the array descriptor */
 
-       ad = o->vftbl->arraydesc;
+       ad = LLNI_vftbl_direct(o)->arraydesc;
 
        /* we are cloning an array */
 
@@ -2715,7 +2612,7 @@ java_handle_t *builtin_clone(void *env, java_handle_t *o)
 
                size = ad->dataoffset + ad->componentsize * LLNI_array_size(ah);
         
-               co = heap_allocate(size, (ad->arraytype == ARRAYTYPE_OBJECT), NULL);
+               co = heap_alloc(size, (ad->arraytype == ARRAYTYPE_OBJECT), NULL, true);
 
                if (co == NULL)
                        return NULL;
@@ -2742,7 +2639,7 @@ java_handle_t *builtin_clone(void *env, java_handle_t *o)
 
        /* get the class of the object */
 
-    c = o->vftbl->class;
+       LLNI_class_get(o, c);
 
        /* create new object */