Merged revisions 7766-7796 via svnmerge from
[cacao.git] / src / vm / jit / codegen-common.c
index 188c8e7c68545f9e3b46cb8277a36d8a63864769..bc63d9550039f1f0ab6d44868716813a00264928 100644 (file)
@@ -39,7 +39,7 @@
    memory. All functions writing values into the data area return the offset
    relative the begin of the code area (start of procedure).   
 
-   $Id: codegen-common.c 7693 2007-04-12 14:56:49Z michi $
+   $Id: codegen-common.c 7797 2007-04-23 20:12:39Z michi $
 
 */
 
@@ -72,6 +72,7 @@
 
 #include "threads/threads-common.h"
 
+#include "vm/builtin.h"
 #include "vm/exceptions.h"
 #include "vm/stringlocal.h"
 
 
 # include "vmcore/statistics.h"
 
+#if defined(ENABLE_VMLOG)
+#include <vmlog_cacao.h>
+#endif
+
 
 /* in this tree we store all method addresses *********************************/
 
@@ -200,8 +205,6 @@ void codegen_setup(jitdata *jd)
        cd->linenumbertablestartpos = 0;
        cd->linenumbertab = 0;
        
-       cd->maxstack = m->maxstack;
-
 #if defined(ENABLE_THREADS)
        cd->threadcritcurrent.next = NULL;
        cd->threadcritcount = 0;
@@ -492,7 +495,7 @@ void codegen_resolve_branchrefs(codegendata *cd, basicblock *bptr)
 
 void codegen_branch_label_add(codegendata *cd, s4 label, s4 condition, s4 reg, u4 options)
 {
-       list               *list;
+       list_t             *list;
        branch_label_ref_t *br;
        s4                  mpc;
 
@@ -649,6 +652,10 @@ u1 *codegen_get_pv_from_pc(u1 *pc)
        if (mte == NULL) {
                /* No method was found.  Let's dump a stacktrace. */
 
+#if defined(ENABLE_VMLOG)
+               vmlog_cacao_signl("SIGSEGV");
+#endif
+
                log_println("We received a SIGSEGV and tried to handle it, but we were");
                log_println("unable to find a Java method at:");
                log_println("");
@@ -662,7 +669,7 @@ u1 *codegen_get_pv_from_pc(u1 *pc)
 
 #if defined(ENABLE_THREADS)
                /* XXX michi: This should be available even without threads! */
-               threads_print_stacktrace(THREADOBJECT);
+               threads_print_stacktrace();
 #endif
 
                vm_abort("Exiting...");
@@ -1019,6 +1026,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)
+               count_nstub_len += code->mcodelength;
+#endif
+
+       /* release memory */
+
+       dump_release(dumpsize);
+}
+
+
 /* codegen_generate_stub_native ************************************************
 
    Wrapper for codegen_emit_stub_native.
@@ -1120,15 +1193,15 @@ codeinfo *codegen_generate_stub_native(methodinfo *m, functionptr f)
        intrp_createnativestub(f, jd, nmd);
 #endif
 
+       /* reallocate the memory and finish the code generation */
+
+       codegen_finish(jd);
+
 #if defined(ENABLE_STATISTICS)
        if (opt_stat)
                count_nstub_len += code->mcodelength;
 #endif
 
-       /* reallocate the memory and finish the code generation */
-
-       codegen_finish(jd);
-
 #if !defined(NDEBUG)
        /* disassemble native stub */
 
@@ -1177,6 +1250,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:
@@ -1254,16 +1398,16 @@ java_objectheader *codegen_finish_native_call(u1 *datasp)
 {
        stackframeinfo     *sfi;
        stackframeinfo    **psfi;
+#if defined(ENABLE_JAVASE)
        localref_table     *lrt;
        localref_table     *plrt;
        s4                  localframes;
+#endif
        java_objectheader  *e;
 
        /* get data structures from stack */
 
        sfi = (stackframeinfo *) (datasp - sizeof(stackframeinfo));
-       lrt = (localref_table *) (datasp - sizeof(stackframeinfo) - 
-                                                         sizeof(localref_table));
 
 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
        /* clear the native world flag */