* src/vm/builtin.c (builtin_throw_exception): Changed return type and
[cacao.git] / src / vm / jit / alpha / md.c
index cb56383056cfddf53635a63fa06cb58b98c97763..d5152672c313cf934406012c755f3c65ea481ee6 100644 (file)
@@ -1,9 +1,9 @@
 /* src/vm/jit/alpha/md.c - machine dependent Alpha functions
 
-   Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
-   R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
-   C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
-   Institut f. Computersprachen - TU Wien
+   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
+   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
+   J. Wenninger, Institut f. Computersprachen - TU Wien
 
    This file is part of CACAO.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
+   Contact: cacao@cacaojvm.org
 
    Authors: Andreas Krall
             Reinhard Grafl
 
    Changes: Joseph Wenninger
             Christian Thalinger
+                       Edwin Steiner
 
-   $Id: md.c 3039 2005-07-13 19:04:25Z twisti $
+   $Id: md.c 4673 2006-03-22 15:30:06Z edwin $
 
 */
 
 
+#include "config.h"
+
 #include <assert.h>
 #include <ucontext.h>
 
-#include "config.h"
+#if defined(__LINUX__)
+# include <asm/fpu.h>
+
+extern unsigned long ieee_get_fp_control();
+extern void ieee_set_fp_control(unsigned long fp_control);
+#endif
+
+#include "vm/types.h"
 
 #include "vm/jit/alpha/md-abi.h"
-#include "vm/jit/alpha/types.h"
 
 #include "vm/exceptions.h"
 #include "vm/stringlocal.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
+
+
+/* global variables ***********************************************************/
+
+bool has_ext_instr_set = false;             /* has instruction set extensions */
+
 
 /* md_init *********************************************************************
 
 
 void md_init(void)
 {
-#if 0
-       /* XXX TWISTI: do we really need this? fptest's seem to work fine */
-
-#if defined(__LINUX__)
-/* Linux on Digital Alpha needs an initialisation of the ieee floating point
-       control for IEEE compliant arithmetic (option -mieee of GCC). Under
-       Digital Unix this is done automatically.
-*/
+       /* check for extended instruction set */
 
-#include <asm/fpu.h>
+       has_ext_instr_set = !asm_md_init();
 
-extern unsigned long ieee_get_fp_control();
-extern void ieee_set_fp_control(unsigned long fp_control);
+#if defined(__LINUX__)
+       /* Linux on Digital Alpha needs an initialisation of the ieee
+          floating point control for IEEE compliant arithmetic (option
+          -mieee of GCC). Under Digital Unix this is done
+          automatically. */
 
        /* initialize floating point control */
 
@@ -79,133 +94,217 @@ extern void ieee_set_fp_control(unsigned long fp_control);
 /*                                             & ~IEEE_TRAP_ENABLE_UNF   we dont want underflow */
                                                & ~IEEE_TRAP_ENABLE_OVF);
 #endif
-#endif
-
-       /* nothing to do */
 }
 
 
-/* signal_handler_sigsegv ******************************************************
+/* md_stacktrace_get_returnaddress *********************************************
 
-   NullPointerException signal handler for hardware null pointer check.
+   Returns the return address of the current stackframe, specified by
+   the passed stack pointer and the stack frame size.
 
 *******************************************************************************/
 
-void signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
+u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
 {
-       ucontext_t  *_uc;
-       mcontext_t  *_mc;
-       u4           instr;
-       ptrint       addr;
-       u1          *pv;
-       u1          *sp;
-       functionptr  ra;
-       functionptr  xpc;
+       u1 *ra;
 
-       _uc = (ucontext_t *) _p;
-       _mc = &_uc->uc_mcontext;
+       /* on Alpha the return address is located on the top of the stackframe */
 
-       instr = *((s4 *) (_mc->sc_pc));
-       addr = _mc->sc_regs[(instr >> 16) & 0x1f];
+       ra = *((u1 **) (sp + framesize - SIZEOF_VOID_P));
 
-       if (addr == 0) {
-               pv  = (u1 *) _mc->sc_regs[REG_PV];
-               sp  = (u1 *) _mc->sc_regs[REG_SP];
-               ra  = (functionptr) _mc->sc_regs[REG_RA]; /* this is correct for leafs*/
-               xpc = (functionptr) _mc->sc_pc;
+       return ra;
+}
 
-               _mc->sc_regs[REG_ITMP1_XPTR] =
-                       (ptrint) stacktrace_hardware_nullpointerexception(pv, sp, ra, xpc);
 
-               _mc->sc_regs[REG_ITMP2_XPC] = (ptrint) xpc;
-               _mc->sc_pc = (ptrint) asm_handle_exception;
+/* md_assembler_get_patch_address **********************************************
 
-       } else {
-               addr += (long) ((instr << 16) >> 16);
+   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).
 
-               throw_cacao_exception_exit(string_java_lang_InternalError,
-                                                                  "Segmentation fault: 0x%016lx at 0x%016lx\n",
-                                                                  addr, _mc->sc_pc);
-       }
-}
+   Machine code:
 
+   a77bffb8    ldq     pv,-72(pv)
+   6b5b4000    jsr     (pv)
+
+   or
+
+   a77c0000    ldq     pv,0(at)
+   6b5b4000    jsr     (pv)
+
+*******************************************************************************/
 
-#if defined(USE_THREADS) && defined(NATIVE_THREADS)
-void thread_restartcriticalsection(ucontext_t *uc)
+u1 *md_assembler_get_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
 {
-       void *critical;
+       u4  mcode;
+       s4  offset;
+       u1 *pa;                             /* patch address                      */
 
-       critical = thread_checkcritical((void *) uc->uc_mcontext.sc_pc);
+       /* go back to the actual load instruction (2 instructions on Alpha) */
 
-       if (critical)
-               uc->uc_mcontext.sc_pc = (ptrint) critical;
-}
-#endif
+       ra = ra - 2 * 4;
 
+       /* get first instruction word on current PC */
 
-/* md_stacktrace_get_returnaddress *********************************************
+       mcode = *((u4 *) ra);
 
-   Returns the return address of the current stackframe, specified by
-   the passed stack pointer and the stack frame size.
+       /* check if we have 2 instructions (lui) */
 
-*******************************************************************************/
+       if ((mcode >> 16) == 0x3c19) {
+               /* XXX write a regression for this */
+               assert(0);
 
-functionptr md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
-{
-       functionptr ra;
+               /* get displacement of first instruction (lui) */
 
-       /* on Alpha the return address is located on the top of the stackframe */
+               offset = (s4) (mcode << 16);
 
-       ra = (functionptr) *((u1 **) (sp + framesize - SIZEOF_VOID_P));
+               /* get displacement of second instruction (daddiu) */
 
-       return ra;
+               mcode = *((u4 *) (ra + 1 * 4));
+
+               assert((mcode >> 16) == 0x6739);
+
+               offset += (s2) (mcode & 0x0000ffff);
+
+       } else {
+               /* get first instruction (ldq) */
+
+               mcode = *((u4 *) ra);
+
+               /* get the offset from the instruction */
+
+               offset = (s2) (mcode & 0x0000ffff);
+
+               /* check for call with REG_METHODPTR: ldq pv,0(at) */
+
+               if ((mcode >> 16) == 0xa77c) {
+                       /* in this case we use the passed method pointer */
+
+                       pa = mptr + offset;
+
+               } else {
+                       /* in the normal case we check for a `ldq pv,-72(pv)' instruction */
+
+                       assert((mcode >> 16) == 0xa77b);
+
+                       /* and get the final data segment address */
+
+                       pa = sfi->pv + offset;
+               }
+       }
+
+       return pa;
 }
 
 
-/* codegen_findmethod **********************************************************
+/* md_codegen_findmethod *******************************************************
 
    Machine code:
 
    6b5b4000    jsr     (pv)
-   237affe8    lda     pv,-24(ra)
+   277afffe    ldah    pv,-2(ra)
+   237ba61c    lda     pv,-23012(pv)
 
 *******************************************************************************/
 
-functionptr codegen_findmethod(functionptr pc)
+u1 *md_codegen_findmethod(u1 *ra)
 {
-       u1 *ra;
        u1 *pv;
        u4  mcode;
-       s2  offset;
+       s4  offset;
 
-       ra = (u1 *) pc;
        pv = ra;
 
-       /* get offset of first instruction (lda) */
+       /* get first instruction word after jump */
 
        mcode = *((u4 *) ra);
 
-       if ((mcode >> 16) != 0x237a) {
-               log_text("No `lda pv,x(ra)' instruction found on return address!");
-               assert(0);
-       }
+       /* check if we have 2 instructions (ldah, lda) */
+
+       if ((mcode >> 16) == 0x277a) {
+               /* get displacement of first instruction (ldah) */
 
-       offset = (s2) (mcode & 0x0000ffff);
-       pv += offset;
+               offset = (s4) (mcode << 16);
+               pv += offset;
 
-       /* check for second instruction (ldah) */
+               /* get displacement of second instruction (lda) */
+
+               mcode = *((u4 *) (ra + 1 * 4));
+
+               assert((mcode >> 16) == 0x237b);
+
+               offset = (s2) (mcode & 0x0000ffff);
+               pv += offset;
+
+       } else {
+               /* get displacement of first instruction (lda) */
 
-       mcode = *((u4 *) (ra + 1 * 4));
+               assert((mcode >> 16) == 0x237a);
 
-       if ((mcode >> 16) == 0x177b) {
-               offset = (s2) (mcode << 16);
+               offset = (s2) (mcode & 0x0000ffff);
                pv += offset;
        }
 
-       return (functionptr) pv;
+       return pv;
 }
 
 
+/* md_cacheflush ***************************************************************
+
+   Calls the system's function to flush the instruction and data
+   cache.
+
+*******************************************************************************/
+
+void md_cacheflush(u1 *addr, s4 nbytes)
+{
+       asm_cacheflush(addr, nbytes);
+}
+
+
+/* md_icacheflush **************************************************************
+
+   Calls the system's function to flush the instruction cache.
+
+*******************************************************************************/
+
+void md_icacheflush(u1 *addr, s4 nbytes)
+{
+       asm_cacheflush(addr, nbytes);
+}
+
+
+/* md_patch_replacement_point **************************************************
+
+   Patch the given replacement point.
+
+*******************************************************************************/
+
+void md_patch_replacement_point(rplpoint *rp)
+{
+    u8 mcode;
+
+       /* save the current machine code */
+       mcode = *(u4*)rp->pc;
+
+       /* write the new machine code */
+    *(u4*)(rp->pc) = (u4) rp->mcode;
+
+       /* store saved mcode */
+       rp->mcode = mcode;
+       
+#if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
+       {
+               u1* u1ptr = rp->pc;
+               DISASSINSTR(u1ptr);
+               fflush(stdout);
+       }
+#endif
+                       
+       /* flush instruction cache */
+    md_icacheflush(rp->pc,4);
+}
+
 /*
  * These are local overrides for various environment variables in Emacs.
  * Please do not remove this and leave it at the end of the file, where
@@ -217,4 +316,5 @@ functionptr codegen_findmethod(functionptr pc)
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */