* src/vm/jit/x86_64/codegen.c (codegen): Use code variables.
[cacao.git] / src / vm / jit / x86_64 / md.c
index 9eb2ea5354c25b49adeb187eef20411249a98abb..e2c68ac5ee755011b6e8babe6e545e77e447e094 100644 (file)
@@ -28,7 +28,7 @@
 
    Changes: Edwin Steiner
 
-   $Id: md.c 4637 2006-03-16 15:36:49Z twisti $
+   $Id: md.c 5051 2006-06-28 15:39:22Z twisti $
 
 */
 
 
 #include "config.h"
 
+#include <assert.h>
 #include <stdlib.h>
 #include <ucontext.h>
 
 #include "vm/jit/x86_64/md-abi.h"
 
+#if defined(ENABLE_THREADS)
+# include "threads/native/threads.h"
+#endif
+
 #include "vm/exceptions.h"
 #include "vm/signallocal.h"
 #include "vm/jit/asmpart.h"
 #include "vm/jit/stacktrace.h"
+
+#if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
 #include "vm/options.h" /* XXX debug */
 #include "vm/jit/disass.h" /* XXX debug */
+#endif
 
 
 /* md_init *********************************************************************
@@ -80,8 +88,8 @@ void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
        _uc = (ucontext_t *) _p;
        _mc = &_uc->uc_mcontext;
 
-       /* ATTENTION: don't use CACAO internal REG_* defines as they are          */
-       /* different to the ones in <ucontext.h>                                  */
+       /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
+          different to the ones in <ucontext.h>. */
 
        sp  = (u1 *) _mc->gregs[REG_RSP];
        xpc = (u1 *) _mc->gregs[REG_RIP];
@@ -113,8 +121,8 @@ void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
        _uc = (ucontext_t *) _p;
        _mc = &_uc->uc_mcontext;
 
-       /* ATTENTION: don't use CACAO internal REG_* defines as they are          */
-       /* different to the ones in <ucontext.h>                                  */
+       /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
+          different to the ones in <ucontext.h>. */
 
        sp  = (u1 *) _mc->gregs[REG_RSP];
        xpc = (u1 *) _mc->gregs[REG_RIP];
@@ -128,15 +136,46 @@ void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
 }
 
 
-#if defined(USE_THREADS) && defined(NATIVE_THREADS)
-void thread_restartcriticalsection(ucontext_t *uc)
+/* md_signal_handler_sigusr2 ***************************************************
+
+   NullPointerException signal handler for hardware null pointer
+   check.
+
+*******************************************************************************/
+
+void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
 {
-       void *critical;
+       threadobject *t;
+       ucontext_t   *_uc;
+       mcontext_t   *_mc;
+       u1           *pc;
 
-       critical = thread_checkcritical((void *) uc->uc_mcontext.gregs[REG_RIP]);
+       t = THREADOBJECT;
 
-       if (critical)
-               uc->uc_mcontext.gregs[REG_RIP] = (ptrint) critical;
+       _uc = (ucontext_t *) _p;
+       _mc = &_uc->uc_mcontext;
+
+       /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
+          different to the ones in <ucontext.h>. */
+
+       pc = (u1 *) _mc->gregs[REG_RIP];
+
+       t->pc = pc;
+}
+
+
+#if defined(ENABLE_THREADS)
+void thread_restartcriticalsection(ucontext_t *_uc)
+{
+       mcontext_t *_mc;
+       void       *pc;
+
+       _mc = &_uc->uc_mcontext;
+
+       pc = critical_find_restart_point((void *) _mc->gregs[REG_RIP]);
+
+       if (pc != NULL)
+               _mc->gregs[REG_RIP] = (ptrint) pc;
 }
 #endif
 
@@ -160,6 +199,77 @@ u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
 }
 
 
+/* md_get_method_patch_address *************************************************
+
+   Gets the patch address of the currently compiled method. The offset
+   is extracted from the load instruction(s) before the jump and added
+   to the right base address (PV or REG_METHODPTR).
+
+   INVOKESTATIC/SPECIAL:
+
+   49 ba 98 3a ed ab aa 2a 00 00    mov    $0x2aaaabed3a98,%r10
+   49 ff d2                         rex64Z callq  *%r10
+
+   INVOKEVIRTUAL:
+
+   4c 8b 17                         mov    (%rdi),%r10
+   49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
+   48 ff d3                         rex64 callq  *%rax
+
+   INVOKEINTERFACE:
+
+   4c 8b 17                         mov    (%rdi),%r10
+   4d 8b 92 00 00 00 00             mov    0x0(%r10),%r10
+   49 8b 82 00 00 00 00             mov    0x0(%r10),%rax
+   48 ff d3                         rex64 callq  *%r11
+
+*******************************************************************************/
+
+u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
+{
+       u1  mcode;
+       s4  offset;
+       u1 *pa;                             /* patch address                      */
+
+       /* go back to the actual call instruction (3-bytes) */
+
+       ra = ra - 3;
+
+       /* get the last byte of the call */
+
+       mcode = ra[2];
+
+       /* check for the different calls */
+
+       /* INVOKESTATIC/SPECIAL */
+
+       if (mcode == 0xd2) {
+               /* patch address is 8-bytes before the call instruction */
+
+               pa = ra - 8;
+
+       } else if (mcode == 0xd3) {
+               /* INVOKEVIRTUAL/INTERFACE */
+
+               /* Get the offset from the instruction (the offset address is
+                  4-bytes before the call instruction). */
+
+               offset = *((s4 *) (ra - 4));
+
+               /* add the offset to the method pointer */
+
+               pa = mptr + offset;
+
+       } else {
+               /* catch any problems */
+
+               assert(0);
+       }
+
+       return pa;
+}
+
+
 /* md_codegen_findmethod *******************************************************
 
    On this architecture just a wrapper function to codegen_findmethod.
@@ -192,6 +302,30 @@ void md_cacheflush(u1 *addr, s4 nbytes)
 }
 
 
+/* md_icacheflush **************************************************************
+
+   Calls the system's function to flush the instruction cache.
+
+*******************************************************************************/
+
+void md_icacheflush(u1 *addr, s4 nbytes)
+{
+       /* do nothing */
+}
+
+
+/* md_dcacheflush **************************************************************
+
+   Calls the system's function to flush the data cache.
+
+*******************************************************************************/
+
+void md_dcacheflush(u1 *addr, s4 nbytes)
+{
+       /* do nothing */
+}
+
+
 /* md_patch_replacement_point **************************************************
 
    Patch the given replacement point.
@@ -219,11 +353,13 @@ void md_patch_replacement_point(rplpoint *rp)
        /* store saved mcode */
        rp->mcode = mcode;
        
+#if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
        {
                u1* u1ptr = rp->pc;
                DISASSINSTR(u1ptr);
                fflush(stdout);
        }
+#endif
                        
     /* XXX if required asm_cacheflush(rp->pc,8); */
 }