Merged new changes from default (manually: src/vm/jit/i386/codegen.c).
[cacao.git] / src / vm / jit / builtin.cpp
index bf9b656611d6b07949c4f765af8c85c1ed31fb66..06e2d2626779a359e7786a6dbf9adaf0115a37c7 100644 (file)
 
 #include "vm/array.hpp"
 #include "vm/jit/builtin.hpp"
-#include "vm/class.h"
+#include "vm/class.hpp"
 #include "vm/cycles-stats.h"
 #include "vm/exceptions.hpp"
 #include "vm/global.h"
 #include "vm/globals.hpp"
-#include "vm/initialize.h"
+#include "vm/initialize.hpp"
 #include "vm/linker.h"
 #include "vm/loader.hpp"
 #include "vm/options.h"
@@ -267,6 +267,81 @@ bool builtin_init(void)
 }
 
 
+/* builtintable_get_by_key *****************************************************
+
+   Returns a key for the given builtintable_entry object which is suitable
+   for retrieving the instance again by calling builtintable_get_by_key.
+
+   The key can be regarded fixed between multiple runs of the JVM.
+
+*******************************************************************************/
+
+s4 builtintable_get_key(builtintable_entry *bte)
+{
+       s4 entries;
+/*
+       int i;
+       entries = sizeof(builtintable_internal) / sizeof(builtintable_entry) - 1;
+       for (i = 0; i < entries; i++)
+               if (&builtintable_internal[i] == bte)
+                       return i + 1;
+
+       entries = sizeof(builtintable_automatic) / sizeof(builtintable_entry) - 1;
+       for (i = 0; i < entries; i++)
+               if (&builtintable_automatic[i] == bte)
+                       return -i;
+
+       entries = sizeof(builtintable_function) / sizeof(builtintable_entry) - 1;
+       for (i = 0; i < entries; i++)
+               if (&builtintable_function[i] == bte)
+                       return -1000 - i;
+*/
+
+       entries = sizeof(builtintable_internal) / sizeof(builtintable_entry) - 1;
+       if (&builtintable_internal[0] <= bte
+               && &builtintable_internal[entries - 1] >= bte)
+       {
+               return (s4) (bte - &builtintable_internal[0]) + 1;
+       }
+
+       entries = sizeof(builtintable_automatic) / sizeof(builtintable_entry) - 1;
+       if (&builtintable_automatic[0] <= bte
+               && &builtintable_automatic[entries - 1] >= bte)
+       {
+               return -(s4) (bte - &builtintable_automatic[0]);
+       }
+
+       entries = sizeof(builtintable_function) / sizeof(builtintable_entry) - 1;
+       if (&builtintable_function[0] <= bte
+               && &builtintable_function[entries - 1] >= bte)
+       {
+               return -1000 - (s4) (bte - &builtintable_function[0]);
+       }
+
+       /* builtintable_entry is not in our tables. */
+       assert (0);
+
+       return 0;
+}
+
+/* builtintable_get_by_key *****************************************************
+
+   Retrieves an entry in the internal and automatic builtin functions tables
+   using a key that was retrived previously with builtintable_get_key()
+
+*******************************************************************************/
+
+builtintable_entry *builtintable_get_by_key(s4 key)
+{
+       /* If key is positive it is the index into builtintable_internal. If it is
+        * negative it is the index into builtintable_automatic. If it is <= -1000
+     * it is the index into builtintable_function.
+     */
+       return (key > 0)
+               ? &builtintable_internal[key - 1]
+               : (key > -1000 ? &builtintable_automatic[-key] : &builtintable_function[-(1000 + key)]);
+}
+
 /* builtintable_get_internal ***************************************************
 
    Finds an entry in the builtintable for internal functions and
@@ -653,6 +728,15 @@ bool builtin_canstore(java_handle_objectarray_t *oa, java_handle_t *o)
 
 *******************************************************************************/
 
+bool fast_subtype_check(struct _vftbl *s, struct _vftbl *t)
+{
+       if (s->subtype_display[t->subtype_depth] == t)
+               return true;
+       if (t->subtype_offset != OFFSET(vftbl_t, subtype_display[DISPLAY_SIZE]))
+               return false;
+       return s->subtype_depth >= t->subtype_depth && s->subtype_overflow[t->subtype_depth - DISPLAY_SIZE] == t;
+}
+
 bool builtin_fast_canstore(java_objectarray_t *oa, java_object_t *o)
 {
        arraydescriptor *desc;
@@ -685,8 +769,6 @@ bool builtin_fast_canstore(java_objectarray_t *oa, java_object_t *o)
                if (valuevftbl == componentvftbl)
                        return 1;
 
-               linker_classrenumber_mutex->lock();
-
                baseval = componentvftbl->baseval;
 
                if (baseval <= 0) {
@@ -696,11 +778,8 @@ bool builtin_fast_canstore(java_objectarray_t *oa, java_object_t *o)
                                          (valuevftbl->interfacetable[baseval] != NULL));
                }
                else {
-                       diffval = valuevftbl->baseval - componentvftbl->baseval;
-                       result  = diffval <= (uint32_t) componentvftbl->diffval;
+                       result = fast_subtype_check(valuevftbl, componentvftbl);
                }
-
-               linker_classrenumber_mutex->unlock();
        }
        else if (valuedesc == NULL) {
                /* {oa has dimension > 1} */
@@ -752,8 +831,6 @@ bool builtin_fast_canstore_onedim(java_objectarray_t *a, java_object_t *o)
        if (valuevftbl == elementvftbl)
                return 1;
 
-       linker_classrenumber_mutex->lock();
-
        baseval = elementvftbl->baseval;
 
        if (baseval <= 0) {
@@ -762,12 +839,9 @@ bool builtin_fast_canstore_onedim(java_objectarray_t *a, java_object_t *o)
                                  (valuevftbl->interfacetable[baseval] != NULL));
        }
        else {
-               diffval = valuevftbl->baseval - elementvftbl->baseval;
-               result  = diffval <= (uint32_t) elementvftbl->diffval;
+               result = fast_subtype_check(valuevftbl, elementvftbl);
        }
 
-       linker_classrenumber_mutex->unlock();
-
        return result;
 }
 
@@ -801,12 +875,7 @@ bool builtin_fast_canstore_onedim_class(java_objectarray_t *a, java_object_t *o)
        if (valuevftbl == elementvftbl)
                return 1;
 
-       linker_classrenumber_mutex->lock();
-
-       diffval = valuevftbl->baseval - elementvftbl->baseval;
-       result  = diffval <= (uint32_t) elementvftbl->diffval;
-
-       linker_classrenumber_mutex->unlock();
+       result = fast_subtype_check(valuevftbl, elementvftbl);
 
        return result;
 }
@@ -878,7 +947,7 @@ java_handle_t *builtin_new(classinfo *c)
        LLNI_vftbl_direct(o) = c->vftbl;
 
 #if defined(ENABLE_THREADS)
-       lock_init_object_lock(LLNI_DIRECT(o));
+       LLNI_DIRECT(o)->lockword.init();
 #endif
 
        CYCLES_STATS_GET(cycles_end);
@@ -960,7 +1029,7 @@ java_handle_t *builtin_tlh_new(classinfo *c)
        LLNI_vftbl_direct(o) = c->vftbl;
 
 # if defined(ENABLE_THREADS)
-       lock_init_object_lock(LLNI_DIRECT(o));
+       LLNI_DIRECT(o)->lockword.init();
 # endif
 
        CYCLES_STATS_GET(cycles_end);
@@ -1039,7 +1108,7 @@ java_object_t *builtin_fast_new(classinfo *c)
        o->vftbl = c->vftbl;
 
 #if defined(ENABLE_THREADS)
-       lock_init_object_lock(o);
+       LLNI_DIRECT(o)->lockword.init();
 #endif
 
        CYCLES_STATS_GET(cycles_end);
@@ -1109,7 +1178,7 @@ java_handle_t *builtin_newarray(int32_t size, classinfo *arrayclass)
        LLNI_vftbl_direct(a) = arrayclass->vftbl;
 
 #if defined(ENABLE_THREADS)
-       lock_init_object_lock(LLNI_DIRECT(a));
+       LLNI_DIRECT(a)->lockword.init();
 #endif
 
        LLNI_array_size(a) = size;
@@ -1391,11 +1460,7 @@ s8 builtin_ladd(s8 a, s8 b)
 {
        s8 c;
 
-#if U8_AVAILABLE
        c = a + b; 
-#else
-       c = builtin_i2l(0);
-#endif
 
        return c;
 }
@@ -1404,11 +1469,7 @@ s8 builtin_lsub(s8 a, s8 b)
 {
        s8 c;
 
-#if U8_AVAILABLE
        c = a - b; 
-#else
-       c = builtin_i2l(0);
-#endif
 
        return c;
 }
@@ -1417,11 +1478,7 @@ s8 builtin_lneg(s8 a)
 {
        s8 c;
 
-#if U8_AVAILABLE
        c = -a;
-#else
-       c = builtin_i2l(0);
-#endif
 
        return c;
 }
@@ -1433,11 +1490,7 @@ s8 builtin_lmul(s8 a, s8 b)
 {
        s8 c;
 
-#if U8_AVAILABLE
        c = a * b; 
-#else
-       c = builtin_i2l(0);
-#endif
 
        return c;
 }
@@ -1449,11 +1502,7 @@ s8 builtin_ldiv(s8 a, s8 b)
 {
        s8 c;
 
-#if U8_AVAILABLE
        c = a / b; 
-#else
-       c = builtin_i2l(0);
-#endif
 
        return c;
 }
@@ -1462,11 +1511,7 @@ s8 builtin_lrem(s8 a, s8 b)
 {
        s8 c;
 
-#if U8_AVAILABLE
        c = a % b; 
-#else
-       c = builtin_i2l(0);
-#endif
 
        return c;
 }
@@ -1478,11 +1523,7 @@ s8 builtin_lshl(s8 a, s4 b)
 {
        s8 c;
 
-#if U8_AVAILABLE
        c = a << (b & 63);
-#else
-       c = builtin_i2l(0);
-#endif
 
        return c;
 }
@@ -1491,11 +1532,7 @@ s8 builtin_lshr(s8 a, s4 b)
 {
        s8 c;
 
-#if U8_AVAILABLE
        c = a >> (b & 63);
-#else
-       c = builtin_i2l(0);
-#endif
 
        return c;
 }
@@ -1504,11 +1541,7 @@ s8 builtin_lushr(s8 a, s4 b)
 {
        s8 c;
 
-#if U8_AVAILABLE
        c = ((u8) a) >> (b & 63);
-#else
-       c = builtin_i2l(0);
-#endif
 
        return c;
 }
@@ -1520,11 +1553,7 @@ s8 builtin_land(s8 a, s8 b)
 {
        s8 c;
 
-#if U8_AVAILABLE
        c = a & b; 
-#else
-       c = builtin_i2l(0);
-#endif
 
        return c;
 }
@@ -1533,11 +1562,7 @@ s8 builtin_lor(s8 a, s8 b)
 {
        s8 c;
 
-#if U8_AVAILABLE
        c = a | b; 
-#else
-       c = builtin_i2l(0);
-#endif
 
        return c;
 }
@@ -1546,11 +1571,7 @@ s8 builtin_lxor(s8 a, s8 b)
 {
        s8 c;
 
-#if U8_AVAILABLE
        c = a ^ b; 
-#else
-       c = builtin_i2l(0);
-#endif
 
        return c;
 }
@@ -1560,7 +1581,6 @@ s8 builtin_lxor(s8 a, s8 b)
 #if !(SUPPORT_LONG && SUPPORT_LONG_CMP)
 s4 builtin_lcmp(s8 a, s8 b)
 { 
-#if U8_AVAILABLE
        if (a < b)
                return -1;
 
@@ -1568,9 +1588,6 @@ s4 builtin_lcmp(s8 a, s8 b)
                return 1;
 
        return 0;
-#else
-       return 0;
-#endif
 }
 #endif /* !(SUPPORT_LONG && SUPPORT_LONG_CMP) */
 
@@ -1968,30 +1985,6 @@ double builtin_drem(double a, double b)
 
 /* conversion operations ******************************************************/
 
-#if 0
-s8 builtin_i2l(s4 i)
-{
-#if U8_AVAILABLE
-       return i;
-#else
-       s8 v;
-       v.high = 0;
-       v.low = i;
-       return v;
-#endif
-}
-
-s4 builtin_l2i(s8 l)
-{
-#if U8_AVAILABLE
-       return (s4) l;
-#else
-       return l.low;
-#endif
-}
-#endif
-
-
 #if !(SUPPORT_FLOAT && SUPPORT_I2F)
 float builtin_i2f(s4 a)
 {
@@ -2013,12 +2006,8 @@ double builtin_i2d(s4 a)
 #if !(SUPPORT_LONG && SUPPORT_FLOAT && SUPPORT_L2F)
 float builtin_l2f(s8 a)
 {
-#if U8_AVAILABLE
        float f = (float) a;
        return f;
-#else
-       return 0.0;
-#endif
 }
 #endif /* !(SUPPORT_LONG && SUPPORT_FLOAT && SUPPORT_L2F) */
 
@@ -2026,12 +2015,8 @@ float builtin_l2f(s8 a)
 #if !(SUPPORT_LONG && SUPPORT_DOUBLE && SUPPORT_L2D)
 double builtin_l2d(s8 a)
 {
-#if U8_AVAILABLE
        double d = (double) a;
        return d;
-#else
-       return 0.0;
-#endif
 }
 #endif /* !(SUPPORT_LONG && SUPPORT_DOUBLE && SUPPORT_L2D) */
 
@@ -2346,7 +2331,7 @@ java_handle_t *builtin_clone(void *env, java_handle_t *o)
 #endif
 
 #if defined(ENABLE_THREADS)
-               lock_init_object_lock(LLNI_DIRECT(co));
+               LLNI_DIRECT(co)->lockword.init();
 #endif
 
                LLNI_CRITICAL_END;
@@ -2381,7 +2366,7 @@ java_handle_t *builtin_clone(void *env, java_handle_t *o)
 #endif
 
 #if defined(ENABLE_THREADS)
-       lock_init_object_lock(LLNI_DIRECT(co));
+       LLNI_DIRECT(co)->lockword.init();
 #endif
 
        LLNI_CRITICAL_END;