* Merged with default branch at rev 16f3633aaa5a.
[cacao.git] / src / vm / jit / codegen-common.c
index aa7a158fea7f967b7691fe73183e8e0bee8017fd..8789e282c3f5092cc07eb6ace928a7ea4525c85a 100644 (file)
@@ -66,6 +66,7 @@
 
 #include "threads/threads-common.h"
 
+#include "vm/builtin.h"
 #include "vm/exceptions.h"
 #include "vm/stringlocal.h"
 
@@ -1182,6 +1183,72 @@ u1 *codegen_generate_stub_compiler(methodinfo *m)
 }
 
 
+/* codegen_generate_stub_builtin ***********************************************
+
+   Wrapper for codegen_emit_stub_builtin.
+
+   Returns:
+       Pointer to the entrypoint of the stub.
+
+*******************************************************************************/
+
+void codegen_generate_stub_builtin(builtintable_entry *bte)
+{
+       jitdata  *jd;
+       codeinfo *code;
+       s4        dumpsize;
+
+       /* mark dump memory */
+
+       dumpsize = dump_size();
+
+       jd = DNEW(jitdata);
+
+       jd->m     = NULL;
+       jd->cd    = DNEW(codegendata);
+       jd->rd    = NULL;
+       jd->flags = 0;
+
+       /* Allocate codeinfo memory from the heap as we need to keep them. */
+
+       jd->code  = code_codeinfo_new(NULL);
+
+       /* get required compiler data */
+
+       code = jd->code;
+
+       /* setup code generation stuff */
+
+       codegen_setup(jd);
+
+       /* generate the code */
+
+#if defined(ENABLE_JIT)
+# if defined(ENABLE_INTRP)
+       if (!opt_intrp)
+# endif
+               codegen_emit_stub_builtin(jd, bte);
+#endif
+
+       /* reallocate the memory and finish the code generation */
+
+       codegen_finish(jd);
+
+       /* set the stub entry point in the builtin table */
+
+       bte->stub = code->entrypoint;
+
+#if defined(ENABLE_STATISTICS)
+       if (opt_stat)
+               size_stub_native += code->mcodelength;
+#endif
+
+       /* release memory */
+
+       dump_release(dumpsize);
+}
+
+
 /* codegen_generate_stub_native ************************************************
 
    Wrapper for codegen_emit_stub_native.
@@ -1347,6 +1414,77 @@ void codegen_disassemble_nativestub(methodinfo *m, u1 *start, u1 *end)
 #endif
 
 
+/* codegen_stub_builtin_enter **************************************************
+
+   Prepares the stuff required for a builtin function call:
+
+   - adds a stackframe info structure to the chain, for stacktraces
+
+   The layout of the builtin stub stackframe should look like this:
+
+   +---------------------------+ <- SP (of parent Java function)
+   | return address            |
+   +---------------------------+
+   |                           |
+   | stackframe info structure |
+   |                           |
+   +---------------------------+
+   |                           |
+   | arguments (if any)        |
+   |                           |
+   +---------------------------+ <- SP (native stub)
+
+*******************************************************************************/
+
+void codegen_stub_builtin_enter(u1 *datasp, u1 *pv, u1 *sp, u1 *ra)
+{
+       stackframeinfo *sfi;
+
+       /* get data structures from stack */
+
+       sfi = (stackframeinfo *) (datasp - sizeof(stackframeinfo));
+
+       /* add a stackframeinfo to the chain */
+
+       stacktrace_create_native_stackframeinfo(sfi, pv, sp, ra);
+
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+       /* set the native world flag */
+
+       THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE;
+#endif
+}
+
+
+/* codegen_stub_builtin_exit ***************************************************
+
+   Removes the stuff required for a builtin function call.
+
+*******************************************************************************/
+
+void codegen_stub_builtin_exit(u1 *datasp)
+{
+       stackframeinfo     *sfi;
+       stackframeinfo    **psfi;
+
+       /* get data structures from stack */
+
+       sfi = (stackframeinfo *) (datasp - sizeof(stackframeinfo));
+
+       /* remove current stackframeinfo from chain */
+
+       psfi = &STACKFRAMEINFO;
+
+       *psfi = sfi->prev;
+
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+       /* clear the native world flag */
+
+       THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE;
+#endif
+}
+
+
 /* codegen_start_native_call ***************************************************
 
    Prepares the stuff required for a native (JNI) function call:
@@ -1396,6 +1534,12 @@ void codegen_start_native_call(u1 *datasp, u1 *pv, u1 *sp, u1 *ra)
 
        localref_table_add(lrt);
 #endif
+
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+       /* set the native world flag */
+
+       THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE;
+#endif
 }
 
 
@@ -1417,6 +1561,12 @@ java_object_t *codegen_finish_native_call(u1 *datasp)
 
        sfi = (stackframeinfo *) (datasp - sizeof(stackframeinfo));
 
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+       /* clear the native world flag */
+
+       THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE;
+#endif
+
        /* remove current stackframeinfo from chain */
 
        psfi = &STACKFRAMEINFO;