Merged trunk and subtype.
[cacao.git] / src / vm / jit / alpha / md.c
index 41c63662af286dc32c159e11ee69cd4534084a0d..9ad99b7474b6f982d1c66ea557be178a04f453a7 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/alpha/md.c - machine dependent Alpha functions
 
-   Copyright (C) 1996-2005, 2006, 2007 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, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: md.c 7304 2007-02-09 10:35:26Z twisti $
-
 */
 
 
 #include "config.h"
 
-#include <assert.h>
+#include <stdint.h>
 #include <ucontext.h>
 
 #if defined(__LINUX__)
@@ -39,20 +35,12 @@ 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/exceptions.h"
-#include "vm/stringlocal.h"
+#include "vm/jit/alpha/codegen.h"
+#include "vm/jit/alpha/md.h"
 
 #include "vm/jit/asmpart.h"
-#include "vm/jit/stacktrace.h"
-
-#if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
-#include "vmcore/options.h" /* XXX debug */
-#include "vm/jit/disass.h" /* XXX debug */
-#endif
+#include "vm/jit/jit.hpp"
+#include "vm/jit/trap.h"
 
 
 /* global variables ***********************************************************/
@@ -68,6 +56,10 @@ bool has_ext_instr_set = false;             /* has instruction set extensions */
 
 void md_init(void)
 {
+#if defined(__LINUX__)
+       unsigned long int fpcw;
+#endif
+
        /* check for extended instruction set */
 
        has_ext_instr_set = !asm_md_init();
@@ -80,71 +72,29 @@ void md_init(void)
 
        /* initialize floating point control */
 
-       ieee_set_fp_control(ieee_get_fp_control()
-                                               & ~IEEE_TRAP_ENABLE_INV
-                                               & ~IEEE_TRAP_ENABLE_DZE
-/*                                             & ~IEEE_TRAP_ENABLE_UNF   we dont want underflow */
-                                               & ~IEEE_TRAP_ENABLE_OVF);
-#endif
-}
-
-
-/* md_codegen_patch_branch *****************************************************
-
-   Back-patches a branch instruction.
-
-*******************************************************************************/
-
-void md_codegen_patch_branch(codegendata *cd, s4 branchmpc, s4 targetmpc)
-{
-       s4 *mcodeptr;
-       s4  mcode;
-       s4  disp;                           /* branch displacement                */
-
-       /* calculate the patch position */
-
-       mcodeptr = (s4 *) (cd->mcodebase + branchmpc);
-
-       /* get the instruction before the exception point */
-
-       mcode = mcodeptr[-1];
-
-       /* Calculate the branch displacement.  For branches we need a
-          displacement relative and shifted to the branch PC. */
-
-       disp = (targetmpc - branchmpc) >> 2;
-
-       /* check branch displacement */
+       fpcw = ieee_get_fp_control();
 
-       if ((disp < (s4) 0xffe00000) || (disp > (s4) 0x001fffff))
-               vm_abort("branch displacement is out of range: %d > +/-%d", disp, 0x001fffff);
+       fpcw = fpcw
+               & ~IEEE_TRAP_ENABLE_INV
+               & ~IEEE_TRAP_ENABLE_DZE
+               /* We dont want underflow. */
+/*             & ~IEEE_TRAP_ENABLE_UNF */
+               & ~IEEE_TRAP_ENABLE_OVF;
 
-       /* patch the branch instruction before the mcodeptr */
+/*     fpcw = fpcw */
+/*             | IEEE_TRAP_ENABLE_INV */
+/*             | IEEE_TRAP_ENABLE_DZE */
+/*             | IEEE_TRAP_ENABLE_OVF */
+/*             | IEEE_TRAP_ENABLE_UNF */
+/*             | IEEE_TRAP_ENABLE_INE */
+/*             | IEEE_TRAP_ENABLE_DNO; */
 
-       mcodeptr[-1] |= (disp & 0x001fffff);
-}
-
-
-/* md_stacktrace_get_returnaddress *********************************************
-
-   Returns the return address of the current stackframe, specified by
-   the passed stack pointer and the stack frame size.
-
-*******************************************************************************/
-
-u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
-{
-       u1 *ra;
-
-       /* on Alpha the return address is located on the top of the stackframe */
-
-       ra = *((u1 **) (sp + framesize - SIZEOF_VOID_P));
-
-       return ra;
+       ieee_set_fp_control(fpcw);
+#endif
 }
 
 
-/* md_get_method_patch_address *************************************************
+/* md_jit_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
@@ -170,148 +120,72 @@ u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
 
 *******************************************************************************/
 
-u1 *md_get_method_patch_address(u1 *ra, stackframeinfo *sfi, u1 *mptr)
+void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
 {
-       u4  mcode;
-       s4  offset;
-       u1 *pa;                             /* patch address                      */
+       uint32_t *pc;
+       uint32_t  mcode;
+       int       opcode;
+       int       base;
+       int32_t   disp;
+       void     *pa;                       /* patch address                      */
 
-       /* go back to the actual load instruction (2 instructions on Alpha) */
+       /* Go back to the load instruction (2 instructions). */
 
-       ra = ra - 2 * 4;
+       pc = ((uint32_t *) ra) - 2;
 
-       /* get first instruction word on current PC */
+       /* Get first instruction word. */
 
-       mcode = *((u4 *) ra);
+       mcode = pc[0];
 
-       /* check if we have 2 instructions (lui) */
+       /* Get opcode, base register and displacement. */
 
-       if ((mcode >> 16) == 0x3c19) {
-               /* XXX write a regression for this */
-               assert(0);
-
-               /* get displacement of first instruction (lui) */
-
-               offset = (s4) (mcode << 16);
-
-               /* get displacement of second instruction (daddiu) */
-
-               mcode = *((u4 *) (ra + 1 * 4));
-
-               assert((mcode >> 16) == 0x6739);
+       opcode = M_MEM_GET_Opcode(mcode);
+       base   = M_MEM_GET_Rb(mcode);
+       disp   = M_MEM_GET_Memory_disp(mcode);
 
-               offset += (s2) (mcode & 0x0000ffff);
+       /* Check for short or long load (2 instructions). */
 
-       } else {
-               /* get first instruction (ldq) */
+       switch (opcode) {
+       case 0x29: /* LDQ: TODO use define */
+               switch (base) {
+               case REG_PV:
+                       /* Calculate the data segment address. */
 
-               mcode = *((u4 *) ra);
+                       pa = ((uint8_t *) pv) + disp;
+                       break;
 
-               /* 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 */
-
-                       /* return NULL if no mptr was specified (used for replacement) */
+               case REG_METHODPTR:
+                       /* Return NULL if no mptr was specified (used for
+                          replacement). */
 
                        if (mptr == NULL)
                                return NULL;
 
-                       pa = mptr + offset;
-
-               } else {
-                       /* in the normal case we check for a `ldq pv,-72(pv)' instruction */
+                       /* Calculate the address in the vftbl. */
 
-                       assert((mcode >> 16) == 0xa77b);
+                       pa = ((uint8_t *) mptr) + disp;
+                       break;
 
-                       /* and get the final data segment address */
-
-                       pa = sfi->pv + offset;
+               default:
+                       vm_abort_disassemble(pc, 2, "md_jit_method_patch_address: unknown instruction %x", mcode);
+                       return NULL;
                }
-       }
-
-       return pa;
-}
-
-
-/* md_codegen_get_pv_from_pc ***************************************************
-
-   Machine code:
-
-   6b5b4000    jsr     (pv)
-   277afffe    ldah    pv,-2(ra)
-   237ba61c    lda     pv,-23012(pv)
-
-*******************************************************************************/
+               break;
 
-u1 *md_codegen_get_pv_from_pc(u1 *ra)
-{
-       u1 *pv;
-       u4  mcode;
-       s4  offset;
-
-       pv = ra;
-
-       /* get first instruction word after jump */
-
-       mcode = *((u4 *) ra);
-
-       /* check if we have 2 instructions (ldah, lda) */
-
-       if ((mcode >> 16) == 0x277a) {
-               /* get displacement of first instruction (ldah) */
-
-               offset = (s4) (mcode << 16);
-               pv += offset;
-
-               /* get displacement of second instruction (lda) */
-
-               mcode = *((u4 *) (ra + 1 * 4));
-
-               assert((mcode >> 16) == 0x237b);
+       case 0x09: /* LDAH: TODO use define */
+               /* XXX write a regression for this */
 
-               offset = (s2) (mcode & 0x0000ffff);
-               pv += offset;
-       }
-       else {
-               /* get displacement of first instruction (lda) */
+               vm_abort("md_jit_method_patch_address: IMPLEMENT ME!");
 
-               assert((mcode >> 16) == 0x237a);
+               pa = NULL;
+               break;
 
-               offset = (s2) (mcode & 0x0000ffff);
-               pv += offset;
+       default:
+               vm_abort_disassemble(pc, 2, "md_jit_method_patch_address: unknown instruction %x", mcode);
+               return NULL;
        }
 
-       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);
+       return pa;
 }
 
 
@@ -322,44 +196,31 @@ void md_icacheflush(u1 *addr, s4 nbytes)
 *******************************************************************************/
 
 #if defined(ENABLE_REPLACEMENT)
-void md_patch_replacement_point(codeinfo *code, s4 index, rplpoint *rp, u1 *savedmcode)
+void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
 {
-       s4 disp;
        u4 mcode;
 
-       if (index < 0) {
+       if (revert) {
                /* restore the patched-over instruction */
-               *(u4*)(rp->pc) = *(u4*)(savedmcode);
+               *(u4*)(pc) = *(u4*)(savedmcode);
        }
        else {
                /* save the current machine code */
-               *(u4*)(savedmcode) = *(u4*)(rp->pc);
+               *(u4*)(savedmcode) = *(u4*)(pc);
 
                /* build the machine code for the patch */
-               disp = ((u4*)code->replacementstubs - (u4*)rp->pc)
-                          + index * REPLACEMENT_STUB_SIZE
-                          - 1;
-
-               /* BR */
-        mcode = (((s4) (0x30)) << 26) | ((REG_ZERO) << 21) | ((disp) & 0x1fffff);
+               mcode = (0xa41f0000 | (TRAP_PATCHER));
 
                /* write the new machine code */
-               *(u4*)(rp->pc) = mcode;
+               *(u4*)(pc) = mcode;
        }
        
-#if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER) && 0
-       {
-               u1* u1ptr = rp->pc;
-               DISASSINSTR(u1ptr);
-               fflush(stdout);
-       }
-#endif
-                       
        /* flush instruction cache */
-    md_icacheflush(rp->pc,4);
+    md_icacheflush(pc,4);
 }
 #endif /* defined(ENABLE_REPLACEMENT) */
 
+
 /*
  * 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