Search in sources :

Example 1 with FloatingPointMachineRegister

use of org.jikesrvm.ia32.RegisterConstants.FloatingPointMachineRegister in project JikesRVM by JikesRVM.

the class JNICompiler method generateEpilogForJNIMethod.

/**
 * Handles the C to Java transition:  JNI methods in JNIFunctions.java.
 * Creates an epilogue for the baseline compiler.
 *
 * @param asm the assembler to use
 * @param method the method that's being compiled
 */
public static void generateEpilogForJNIMethod(Assembler asm, RVMMethod method) {
    if (VM.BuildFor32Addr) {
        // if returning long, switch the order of the hi/lo word in T0 and T1
        if (method.getReturnType().isLongType()) {
            asm.emitPUSH_Reg(T1);
            asm.emitMOV_Reg_Reg(T1, T0);
            asm.emitPOP_Reg(T0);
        } else {
            if (SSE2_FULL && VM.BuildFor32Addr) {
                // Marshall from XMM0 -> FP0
                if (method.getReturnType().isDoubleType()) {
                    if (VM.VerifyAssertions)
                        VM._assert(VM.BuildFor32Addr);
                    asm.emitMOVSD_RegDisp_Reg(THREAD_REGISTER, Entrypoints.scratchStorageField.getOffset(), XMM0);
                    asm.emitFLD_Reg_RegDisp_Quad(FP0, THREAD_REGISTER, Entrypoints.scratchStorageField.getOffset());
                } else if (method.getReturnType().isFloatType()) {
                    if (VM.VerifyAssertions)
                        VM._assert(VM.BuildFor32Addr);
                    asm.emitMOVSS_RegDisp_Reg(THREAD_REGISTER, Entrypoints.scratchStorageField.getOffset(), XMM0);
                    asm.emitFLD_Reg_RegDisp(FP0, THREAD_REGISTER, Entrypoints.scratchStorageField.getOffset());
                }
            }
        }
    }
    // S0 <- JNIEnvironment
    if (VM.BuildFor32Addr) {
        asm.emitMOV_Reg_RegDisp(S0, THREAD_REGISTER, Entrypoints.jniEnvField.getOffset());
    } else {
        asm.emitMOV_Reg_RegDisp_Quad(S0, THREAD_REGISTER, Entrypoints.jniEnvField.getOffset());
    }
    // set jniEnv TopJavaFP using value saved in frame in prolog
    if (VM.BuildFor32Addr) {
        // EDI<-saved TopJavaFP (offset)
        asm.emitMOV_Reg_RegDisp(EDI, EBP, SAVED_JAVA_FP_OFFSET);
        // change offset from FP into address
        asm.emitADD_Reg_Reg(EDI, EBP);
        // jniEnv.TopJavaFP <- EDI
        asm.emitMOV_RegDisp_Reg(S0, Entrypoints.JNITopJavaFPField.getOffset(), EDI);
    } else {
        // EDI<-saved TopJavaFP (offset)
        asm.emitMOV_Reg_RegDisp_Quad(EDI, EBP, SAVED_JAVA_FP_OFFSET);
        // change offset from FP into address
        asm.emitADD_Reg_Reg_Quad(EDI, EBP);
        // jniEnv.TopJavaFP <- EDI
        asm.emitMOV_RegDisp_Reg_Quad(S0, Entrypoints.JNITopJavaFPField.getOffset(), EDI);
    }
    // NOTE: we could save the TR in the JNI env, but no need, that would have
    // already been done.
    // what's going on here:
    // - SP and EBP have important stuff in them, but that's fine, since
    // a call will restore SP and EBP is non-volatile for RVM code
    // - TR still refers to the thread
    // save return values
    asm.emitPUSH_Reg(T0);
    asm.emitPUSH_Reg(T1);
    // attempt to change the thread state to IN_JNI
    asm.emitMOV_Reg_Imm(T0, RVMThread.IN_JAVA);
    asm.emitMOV_Reg_Imm(T1, RVMThread.IN_JNI);
    asm.emitLockNextInstruction();
    asm.emitCMPXCHG_RegDisp_Reg(THREAD_REGISTER, Entrypoints.execStatusField.getOffset(), T1);
    // if success, skip the slow path call
    ForwardReference doneEnterJNIRef = asm.forwardJcc(EQ);
    // fast path failed, make the call
    asm.generateJTOCcall(Entrypoints.enterJNIBlockedFromJNIFunctionCallMethod.getOffset());
    // OK - we reach here when we have set the state to IN_JNI
    doneEnterJNIRef.resolve(asm);
    // restore return values
    asm.emitPOP_Reg(T1);
    asm.emitPOP_Reg(T0);
    // reload native/C nonvolatile regs - saved in prolog
    for (FloatingPointMachineRegister r : NATIVE_NONVOLATILE_FPRS) {
        // TODO: we assume non-volatile will hold at most a double
        if (r instanceof XMM) {
            asm.emitMOVSD_Reg_RegInd((XMM) r, SP);
        } else {
            // NB this will fail for anything other than FPR0
            asm.emitFLD_Reg_RegInd_Quad((FPR) r, SP);
        }
        // adjust space for double
        asm.emitPOP_Reg(T0);
        asm.emitPOP_Reg(T0);
    }
    // nonvolatile push as the 1st instruction of the prologue
    for (int i = NATIVE_NONVOLATILE_GPRS.length - 1; i >= 0; i--) {
        GPR r = NATIVE_NONVOLATILE_GPRS[i];
        asm.emitPOP_Reg(r);
    }
    // Discard JNIEnv, CMID and outer most native frame pointer
    if (VM.BuildFor32Addr) {
        // discard current stack frame
        asm.emitADD_Reg_Imm(SP, 3 * WORDSIZE);
    } else {
        // discard current stack frame
        asm.emitADD_Reg_Imm_Quad(SP, 3 * WORDSIZE);
    }
    // return to caller
    asm.emitRET();
}
Also used : ForwardReference(org.jikesrvm.compilers.common.assembler.ForwardReference) FloatingPointMachineRegister(org.jikesrvm.ia32.RegisterConstants.FloatingPointMachineRegister) GPR(org.jikesrvm.ia32.RegisterConstants.GPR) XMM(org.jikesrvm.ia32.RegisterConstants.XMM)

Example 2 with FloatingPointMachineRegister

use of org.jikesrvm.ia32.RegisterConstants.FloatingPointMachineRegister in project JikesRVM by JikesRVM.

the class JNICompiler method generateGlueCodeForJNIMethod.

/**
 * Handles the C to Java transition:  JNI methods in JNIFunctions.java.
 * Creates a prologue for the baseline compiler.
 * <pre>
 * NOTE:
 *   -We need THREAD_REGISTER to access Java environment; we can get it from
 *    the JNIEnv* (which is an interior pointer to the JNIEnvironment)
 *   -Unlike the powerPC scheme which has a special prolog preceding
 *    the normal Java prolog, the Intel scheme replaces the Java prolog
 *    completely with the special prolog
 *
 *            Stack on entry            Stack at end of prolog after call
 *             high memory                       high memory
 *            |            |                   |            |
 *    EBP -&gt;  |saved FP    |                   |saved FP    |
 *            |  ...       |                   |  ...       |
 *            |            |                   |            |
 *            |arg n-1     |                   |arg n-1     |
 * native     |  ...       |                   |  ...       |
 * caller     |arg 0       | JNIEnv*           |arg 0       | JNIEnvironment
 *    ESP -&gt;  |return addr |                   |return addr |
 *            |            |           EBP -&gt;  |saved FP    | outer most native frame pointer
 *            |            |                   |methodID    | normal MethodID for JNI function
 *            |            |                   |saved JavaFP| offset to preceeding java frame
 *            |            |                   |saved nonvol| to be used for nonvolatile storage
 *            |            |                   |  ...       |   including ebp on entry
 *            |            |                   |arg 0       | copied in reverse order (JNIEnvironment)
 *            |            |                   |  ...       |
 *            |            |           ESP -&gt;  |arg n-1     |
 *            |            |                   |            | normally compiled Java code continue
 *            |            |                   |            |
 *            |            |                   |            |
 *            |            |                   |            |
 *             low memory                        low memory
 * </pre>
 *
 * @param asm the assembler to use
 * @param method the method that's being compiled (i.e. the method which is a bridge
 *  from native).
 * @param methodID the id of the compiled method
 */
public static void generateGlueCodeForJNIMethod(Assembler asm, NormalMethod method, int methodID) {
    // Variable tracking the depth of the stack as we generate the prologue
    int stackDepth = 0;
    // 2nd word of header = space for frame pointer
    if (VM.VerifyAssertions)
        VM._assert(STACKFRAME_FRAME_POINTER_OFFSET.toInt() == stackDepth << LG_WORDSIZE);
    asm.emitPUSH_Reg(EBP);
    stackDepth--;
    // start new frame:  set FP to point to the new frame
    if (VM.BuildFor32Addr) {
        asm.emitMOV_Reg_Reg(EBP, SP);
    } else {
        asm.emitMOV_Reg_Reg_Quad(EBP, SP);
    }
    // set 3rd word of header: method ID
    if (VM.VerifyAssertions)
        VM._assert(STACKFRAME_METHOD_ID_OFFSET.toInt() == stackDepth << LG_WORDSIZE);
    asm.emitPUSH_Imm(methodID);
    stackDepth--;
    // buy space for the SAVED_JAVA_FP
    if (VM.VerifyAssertions)
        VM._assert(STACKFRAME_BODY_OFFSET.toInt() == stackDepth << LG_WORDSIZE);
    asm.emitPUSH_Reg(T0);
    stackDepth--;
    // store non-volatiles
    for (GPR r : NATIVE_NONVOLATILE_GPRS) {
        if (r != EBP) {
            asm.emitPUSH_Reg(r);
        } else {
            // save original EBP value
            asm.emitPUSH_RegInd(EBP);
        }
        stackDepth--;
    }
    for (FloatingPointMachineRegister r : NATIVE_NONVOLATILE_FPRS) {
        // TODO: we assume non-volatile will hold at most a double
        // adjust space for double
        asm.emitPUSH_Reg(T0);
        asm.emitPUSH_Reg(T0);
        stackDepth -= 2;
        if (r instanceof XMM) {
            asm.emitMOVSD_RegInd_Reg(SP, (XMM) r);
        } else {
            // NB this will fail for anything other than FPR0
            asm.emitFST_RegInd_Reg_Quad(SP, (FPR) r);
        }
    }
    if (VM.VerifyAssertions) {
        boolean b = stackDepth << LG_WORDSIZE == STACKFRAME_BODY_OFFSET.toInt() - (SAVED_GPRS_FOR_JNI << LG_WORDSIZE);
        if (!b) {
            String msg = "of2fp=" + stackDepth + " sg4j=" + SAVED_GPRS_FOR_JNI;
            VM._assert(VM.NOT_REACHED, msg);
        }
    }
    // Adjust first param from JNIEnv* to JNIEnvironment.
    final Offset firstStackArgOffset = Offset.fromIntSignExtend(2 * WORDSIZE);
    if (jniExternalFunctionsFieldOffset != 0) {
        if (NATIVE_PARAMETER_GPRS.length > 0) {
            if (VM.BuildFor32Addr) {
                asm.emitSUB_Reg_Imm(NATIVE_PARAMETER_GPRS[0], jniExternalFunctionsFieldOffset);
            } else {
                asm.emitSUB_Reg_Imm_Quad(NATIVE_PARAMETER_GPRS[0], jniExternalFunctionsFieldOffset);
            }
        } else {
            if (VM.BuildFor32Addr) {
                asm.emitSUB_RegDisp_Imm(EBP, firstStackArgOffset, jniExternalFunctionsFieldOffset);
            } else {
                asm.emitSUB_RegDisp_Imm_Quad(EBP, firstStackArgOffset, jniExternalFunctionsFieldOffset);
            }
        }
    }
    // copy the arguments in reverse order
    // does NOT include implicit this or class ptr
    final TypeReference[] argTypes = method.getParameterTypes();
    Offset stackArgOffset = firstStackArgOffset;
    // negative value relative to EBP
    final int startOfStackedArgs = stackDepth + 1;
    int argGPR = 0;
    int argFPR = 0;
    for (TypeReference argType : argTypes) {
        if (argType.isFloatType()) {
            if (argFPR < NATIVE_PARAMETER_FPRS.length) {
                // adjust stack
                asm.emitPUSH_Reg(T0);
                if (VM.BuildForSSE2) {
                    asm.emitMOVSS_RegInd_Reg(SP, (XMM) NATIVE_PARAMETER_FPRS[argFPR]);
                } else {
                    asm.emitFSTP_RegInd_Reg(SP, FP0);
                }
                argFPR++;
            } else {
                asm.emitPUSH_RegDisp(EBP, stackArgOffset);
                stackArgOffset = stackArgOffset.plus(WORDSIZE);
            }
            stackDepth--;
        } else if (argType.isDoubleType()) {
            if (argFPR < NATIVE_PARAMETER_FPRS.length) {
                // adjust stack
                asm.emitPUSH_Reg(T0);
                asm.emitPUSH_Reg(T0);
                if (VM.BuildForSSE2) {
                    asm.emitMOVSD_RegInd_Reg(SP, (XMM) NATIVE_PARAMETER_FPRS[argFPR]);
                } else {
                    asm.emitFSTP_RegInd_Reg_Quad(SP, FP0);
                }
                argFPR++;
            } else {
                if (VM.BuildFor32Addr) {
                    asm.emitPUSH_RegDisp(EBP, stackArgOffset.plus(WORDSIZE));
                    asm.emitPUSH_RegDisp(EBP, stackArgOffset);
                    stackArgOffset = stackArgOffset.plus(2 * WORDSIZE);
                } else {
                    // adjust stack
                    asm.emitPUSH_Reg(T0);
                    asm.emitPUSH_RegDisp(EBP, stackArgOffset);
                    stackArgOffset = stackArgOffset.plus(WORDSIZE);
                }
            }
            stackDepth -= 2;
        } else if (argType.isLongType()) {
            if (VM.BuildFor32Addr) {
                if (argGPR + 1 < NATIVE_PARAMETER_GPRS.length) {
                    asm.emitPUSH_Reg(NATIVE_PARAMETER_GPRS[argGPR]);
                    asm.emitPUSH_Reg(NATIVE_PARAMETER_GPRS[argGPR + 1]);
                    argGPR += 2;
                } else if (argGPR < NATIVE_PARAMETER_GPRS.length) {
                    asm.emitPUSH_RegDisp(EBP, stackArgOffset);
                    asm.emitPUSH_Reg(NATIVE_PARAMETER_GPRS[argGPR]);
                    argGPR++;
                    stackArgOffset = stackArgOffset.plus(WORDSIZE);
                } else {
                    asm.emitPUSH_RegDisp(EBP, stackArgOffset.plus(WORDSIZE));
                    asm.emitPUSH_RegDisp(EBP, stackArgOffset);
                    stackArgOffset = stackArgOffset.plus(WORDSIZE * 2);
                }
                stackDepth -= 2;
            } else {
                // adjust stack
                asm.emitPUSH_Reg(T0);
                if (argGPR < NATIVE_PARAMETER_GPRS.length) {
                    asm.emitPUSH_Reg(NATIVE_PARAMETER_GPRS[argGPR]);
                    argGPR++;
                } else {
                    asm.emitPUSH_RegDisp(EBP, stackArgOffset);
                    stackDepth -= 2;
                    stackArgOffset = stackArgOffset.plus(WORDSIZE);
                }
                stackDepth -= 2;
            }
        } else {
            // expect integer arguments
            if (argGPR < NATIVE_PARAMETER_GPRS.length) {
                asm.emitPUSH_Reg(NATIVE_PARAMETER_GPRS[argGPR]);
                argGPR++;
            } else {
                asm.emitPUSH_RegDisp(EBP, stackArgOffset);
                stackArgOffset = stackArgOffset.plus(WORDSIZE);
            }
            stackDepth--;
        }
    }
    // Restore JTOC register
    if (JTOC_REGISTER != null) {
        asm.emitMOV_Reg_Imm_Quad(JTOC_REGISTER, BootRecord.the_boot_record.tocRegister.toLong());
    }
    // START of code sequence to atomically change thread status from
    // IN_JNI to IN_JAVA, looping in a call to
    // RVMThread.leaveJNIBlockedFromJNIFunctionCallMethod if
    // BLOCKED_IN_NATIVE
    // backward branch label
    int retryLabel = asm.getMachineCodeIndex();
    // Restore THREAD_REGISTER from JNIEnvironment
    if (VM.BuildFor32Addr) {
        // pick up arg 0 (from our frame)
        asm.emitMOV_Reg_RegDisp(EBX, EBP, Offset.fromIntSignExtend((startOfStackedArgs - 1) * WORDSIZE));
        asm.emitMOV_Reg_RegDisp(THREAD_REGISTER, EBX, Entrypoints.JNIEnvSavedTRField.getOffset());
    } else {
        // pick up arg 0 (from our frame)
        asm.emitMOV_Reg_RegDisp_Quad(EBX, EBP, Offset.fromIntSignExtend((startOfStackedArgs - 1) * WORDSIZE));
        asm.emitMOV_Reg_RegDisp_Quad(THREAD_REGISTER, EBX, Entrypoints.JNIEnvSavedTRField.getOffset());
    }
    // what we need to keep in mind at this point:
    // - EBX has JNI env (but it's nonvolatile)
    // - EBP has the FP (but it's nonvolatile)
    // - stack has the args but not the locals
    // - TR has been restored
    // attempt to change the thread state to IN_JAVA
    asm.emitMOV_Reg_Imm(T0, RVMThread.IN_JNI);
    asm.emitMOV_Reg_Imm(T1, RVMThread.IN_JAVA);
    asm.emitLockNextInstruction();
    asm.emitCMPXCHG_RegDisp_Reg(THREAD_REGISTER, Entrypoints.execStatusField.getOffset(), T1);
    // if we succeeded, move on, else go into slow path
    ForwardReference doneLeaveJNIRef = asm.forwardJcc(EQ);
    // make the slow call
    asm.generateJTOCcall(Entrypoints.leaveJNIBlockedFromJNIFunctionCallMethod.getOffset());
    // arrive here when we've switched to IN_JAVA
    doneLeaveJNIRef.resolve(asm);
    // END of code sequence to change state from IN_JNI to IN_JAVA
    // status is now IN_JAVA. GC can not occur while we execute on a processor
    // in this state, so it is safe to access fields of objects.
    // RVM TR register has been restored and EBX contains a pointer to
    // the thread's JNIEnvironment.
    // done saving, bump SP to reserve room for the local variables
    // SP should now be at the point normally marked as emptyStackOffset
    int numLocalVariables = method.getLocalWords() - method.getParameterWords();
    // TODO: optimize this space adjustment
    if (VM.BuildFor32Addr) {
        asm.emitSUB_Reg_Imm(SP, (numLocalVariables << LG_WORDSIZE));
    } else {
        asm.emitSUB_Reg_Imm_Quad(SP, (numLocalVariables << LG_WORDSIZE));
    }
    // frame of JNIFunction
    if (VM.BuildFor32Addr) {
        asm.emitMOV_Reg_RegDisp(S0, EBX, Entrypoints.JNITopJavaFPField.getOffset());
    } else {
        asm.emitMOV_Reg_RegDisp_Quad(S0, EBX, Entrypoints.JNITopJavaFPField.getOffset());
    }
    // get offset from current FP and save in hdr of current frame
    if (VM.BuildFor32Addr) {
        asm.emitSUB_Reg_Reg(S0, EBP);
        asm.emitMOV_RegDisp_Reg(EBP, SAVED_JAVA_FP_OFFSET, S0);
    } else {
        asm.emitSUB_Reg_Reg_Quad(S0, EBP);
        asm.emitMOV_RegDisp_Reg_Quad(EBP, SAVED_JAVA_FP_OFFSET, S0);
    }
    // clobber the saved frame pointer with that from the JNIEnvironment (work around for omit-frame-pointer)
    if (VM.BuildFor32Addr) {
        asm.emitMOV_Reg_RegDisp(S0, EBX, Entrypoints.JNIEnvBasePointerOnEntryToNative.getOffset());
        asm.emitMOV_RegInd_Reg(EBP, S0);
    } else {
        asm.emitMOV_Reg_RegDisp_Quad(S0, EBX, Entrypoints.JNIEnvBasePointerOnEntryToNative.getOffset());
        asm.emitMOV_RegInd_Reg_Quad(EBP, S0);
    }
    // put framePointer in Thread following Jikes RVM conventions.
    if (VM.BuildFor32Addr) {
        asm.emitMOV_RegDisp_Reg(THREAD_REGISTER, ArchEntrypoints.framePointerField.getOffset(), EBP);
    } else {
        asm.emitMOV_RegDisp_Reg_Quad(THREAD_REGISTER, ArchEntrypoints.framePointerField.getOffset(), EBP);
    }
// at this point: TR has been restored &
// processor status = IN_JAVA,
// arguments for the call have been setup, space on the stack for locals
// has been acquired.
// finally proceed with the normal Java compiled code
// skip the thread switch test for now, see BaselineCompilerImpl.genThreadSwitchTest(true)
// asm.emitNOP(1); // end of prologue marker
}
Also used : ForwardReference(org.jikesrvm.compilers.common.assembler.ForwardReference) FloatingPointMachineRegister(org.jikesrvm.ia32.RegisterConstants.FloatingPointMachineRegister) GPR(org.jikesrvm.ia32.RegisterConstants.GPR) XMM(org.jikesrvm.ia32.RegisterConstants.XMM) TypeReference(org.jikesrvm.classloader.TypeReference) Offset(org.vmmagic.unboxed.Offset)

Aggregations

ForwardReference (org.jikesrvm.compilers.common.assembler.ForwardReference)2 FloatingPointMachineRegister (org.jikesrvm.ia32.RegisterConstants.FloatingPointMachineRegister)2 GPR (org.jikesrvm.ia32.RegisterConstants.GPR)2 XMM (org.jikesrvm.ia32.RegisterConstants.XMM)2 TypeReference (org.jikesrvm.classloader.TypeReference)1 Offset (org.vmmagic.unboxed.Offset)1