* Merged with michi branch at rev 21fd42e049a3.
[cacao.git] / src / vm / builtin.c
index c8f1b7aaa0dbd48945f02c6f2ebcd452ddcdb7b3..4a435205cf9bb84238db5ded869e6c3ba18af5a9 100644 (file)
 
 #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"
@@ -77,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"
@@ -112,6 +107,7 @@ static bool builtintable_init(void)
        descriptor_pool    *descpool;
        s4                  dumpsize;
        builtintable_entry *bte;
+       methodinfo         *m;
 
        /* mark start of dump memory area */
 
@@ -132,8 +128,7 @@ static bool builtintable_init(void)
        /* first add all descriptors to the pool */
 
        for (bte = builtintable_internal; bte->fp != NULL; bte++) {
-               /* create a utf8 string from descriptor */
-
+               bte->name       = utf_new_char(bte->cname);
                bte->descriptor = utf_new_char(bte->cdescriptor);
 
                if (!descriptor_pool_add(descpool, bte->descriptor, NULL)) {
@@ -185,8 +180,10 @@ static bool builtintable_init(void)
 
                /* generate a builtin stub if we need one */
 
-               if (bte->flags & BUILTINTABLE_FLAG_STUB)
-                       codegen_generate_stub_builtin(bte);
+               if (bte->flags & BUILTINTABLE_FLAG_STUB) {
+                       m = method_new_builtin(bte);
+                       codegen_generate_stub_builtin(m, bte);
+               }
        }
 
        for (bte = builtintable_automatic; bte->fp != NULL; bte++) {
@@ -210,8 +207,10 @@ static bool builtintable_init(void)
 
                /* generate a builtin stub if we need one */
 
-               if (bte->flags & BUILTINTABLE_FLAG_STUB)
-                       codegen_generate_stub_builtin(bte);
+               if (bte->flags & BUILTINTABLE_FLAG_STUB) {
+                       m = method_new_builtin(bte);
+                       codegen_generate_stub_builtin(m, bte);
+               }
        }
 
        /* release dump area */
@@ -542,67 +541,10 @@ s4 builtin_arrayinstanceof(java_handle_t *o, classinfo *targetclass)
 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);
-
-                       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 */
+       /* print exception trace */
 
-               dump_release(dumpsize);
-       }
+       if (opt_TraceExceptions)
+               trace_exception_builtin(xptr);
 #endif /* !defined(NDEBUG) */
 
        /* actually set the exception */
@@ -1254,7 +1196,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);
 }
 
 
@@ -1266,12 +1209,6 @@ java_handle_objectarray_t *builtin_multianewarray(int n, classinfo *arrayclass,
        
 *****************************************************************************/
 
-#if !defined(NDEBUG)
-static s4 methodindent = 0;
-static u4 callcount = 0;
-#endif /* !defined(NDEBUG) */
-
-
 /* builtin_print_argument ******************************************************
 
    Prints arguments and return values for the call trace.
@@ -1419,6 +1356,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;
@@ -1431,6 +1370,8 @@ void builtin_verbosecall_enter(s8 a0, s8 a1,
 
        md = m->parseddesc;
 
+       methodindent = TRACEJAVACALLINDENT;
+
        /* calculate message length */
 
        logtextlen =
@@ -1473,7 +1414,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);
@@ -1577,7 +1518,7 @@ void builtin_verbosecall_enter(s8 a0, s8 a1,
 
        dump_release(dumpsize);
 
-       methodindent++;
+       TRACEJAVACALLINDENT++;
 
 }
 #endif
@@ -1600,6 +1541,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;
@@ -1612,6 +1554,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 =
@@ -1635,13 +1586,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, "           ");
@@ -2525,12 +2469,9 @@ float builtin_d2f(double a)
 
    Builtin for java.lang.System.arraycopy.
 
-   ATTENTION: This builtin function returns a boolean value to signal
-   the ICMD_BUILTIN if there was an exception.
-
 *******************************************************************************/
 
-bool builtin_arraycopy(java_handle_t *src, s4 srcStart,
+void builtin_arraycopy(java_handle_t *src, s4 srcStart,
                                           java_handle_t *dest, s4 destStart, s4 len)
 {
        arraydescriptor *sdesc;
@@ -2539,7 +2480,7 @@ bool builtin_arraycopy(java_handle_t *src, s4 srcStart,
 
        if ((src == NULL) || (dest == NULL)) { 
                exceptions_throw_nullpointerexception();
-               return false;
+               return;
        }
 
        sdesc = LLNI_vftbl_direct(src)->arraydesc;
@@ -2547,7 +2488,7 @@ bool builtin_arraycopy(java_handle_t *src, s4 srcStart,
 
        if (!sdesc || !ddesc || (sdesc->arraytype != ddesc->arraytype)) {
                exceptions_throw_arraystoreexception();
-               return false;
+               return;
        }
 
        /* we try to throw exception with the same message as SUN does */
@@ -2556,7 +2497,7 @@ bool builtin_arraycopy(java_handle_t *src, s4 srcStart,
                (srcStart  + len < 0) || (srcStart  + len > LLNI_array_size(src)) ||
                (destStart + len < 0) || (destStart + len > LLNI_array_size(dest))) {
                exceptions_throw_arrayindexoutofboundsexception();
-               return false;
+               return;
        }
 
        if (sdesc->componentvftbl == ddesc->componentvftbl) {
@@ -2582,7 +2523,7 @@ bool builtin_arraycopy(java_handle_t *src, s4 srcStart,
                                LLNI_objectarray_element_get(oas, srcStart + i, o);
 
                                if (!builtin_canstore(oad, o))
-                                       return false;
+                                       return;
 
                                LLNI_objectarray_element_set(oad, destStart + i, o);
                        }
@@ -2600,14 +2541,12 @@ bool builtin_arraycopy(java_handle_t *src, s4 srcStart,
                                LLNI_objectarray_element_get(oas, srcStart + i, o);
 
                                if (!builtin_canstore(oad, o))
-                                       return false;
+                                       return;
 
                                LLNI_objectarray_element_set(oad, destStart + i, o);
                        }
                }
        }
-
-       return true;
 }