Search in sources :

Example 16 with Address

use of org.vmmagic.unboxed.Address in project JikesRVM by JikesRVM.

the class JNIHelpers method invokeWithDotDotVarArg.

/**
 * Common code shared by the JNI functions CallStatic<type>Method
 * (static method invocation)
 * @param methodID the method ID
 * @param expectReturnType the return type of the method to be invoked
 * @return an object that may be the return object or a wrapper for the primitive return value
 */
@NoInline
public static Object invokeWithDotDotVarArg(int methodID, TypeReference expectReturnType) throws Exception {
    MethodReference mr = MemberReference.getMethodRef(methodID);
    if (VM.BuildForPower64ELF_ABI) {
        Address varargAddress = pushVarArgToSpillArea(methodID, false);
        Object[] argObjectArray = packageParameterFromVarArg(mr, varargAddress);
        return callMethod(null, mr, argObjectArray, expectReturnType, true);
    } else {
        if (VM.VerifyAssertions)
            VM._assert(VM.BuildForSVR4ABI);
        Address glueFP = Magic.getCallerFramePointer(Magic.getCallerFramePointer(Magic.getCallerFramePointer(Magic.getFramePointer())));
        Object[] argObjectArray = packageParameterFromDotArgSVR4(mr, glueFP, false);
        return callMethod(null, mr, argObjectArray, expectReturnType, true);
    }
}
Also used : Address(org.vmmagic.unboxed.Address) MethodReference(org.jikesrvm.classloader.MethodReference) NoInline(org.vmmagic.pragma.NoInline)

Example 17 with Address

use of org.vmmagic.unboxed.Address in project JikesRVM by JikesRVM.

the class JNIHelpers method pushVarArgToSpillArea.

/**
 * This method supports var args passed from C.<p>
 *
 * TODO update for AIX removal
 *
 * In the AIX C convention, the caller keeps the first 8 words in registers and
 * the rest in the spill area in the caller frame.  The callee will push the values
 * in registers out to the spill area of the caller frame and use the beginning
 * address of this spill area as the var arg address.<p>
 *
 * For the JNI functions that takes var args, their prolog code will save the
 * var arg in the glue frame because the values in the register may be lost by
 * subsequent calls.<p>
 *
 * This method copies the var arg values that were saved earlier in glue frame into
 * the spill area of the original caller, thereby doing the work that the callee
 * normally performs in the AIX C convention..<p>
 *
 * NOTE:  this method assumes that it is immediately above the
 * invokeWithDotDotVarArg frame, the JNI frame, the glue frame and
 * the C caller frame in the respective order.
 * Therefore, this method will not work if called from anywhere else
 * <pre>
 *
 *   |  fp  | <- JNIEnvironment.pushVarArgToSpillArea
 *   | mid  |
 *   | xxx  |
 *   |      |
 *   |      |
 *   |------|
 *   |  fp  | <- JNIEnvironment.invokeWithDotDotVarArg frame
 *   | mid  |
 *   | xxx  |
 *   |      |
 *   |      |
 *   |      |
 *   |------|
 *   |  fp  | <- JNI method frame
 *   | mid  |
 *   | xxx  |
 *   |      |
 *   |      |
 *   |      |
 *   |------|
 *   |  fp  | <- glue frame
 *   | mid  |
 *   + xxx  +
 *   | r3   |   volatile save area
 *   | r4   |
 *   | r5   |
 *   | r6   |   vararg GPR[6-10]save area   <- VARARG_AREA_OFFSET
 *   | r7   |
 *   | r8   |
 *   | r9   |
 *   | r10  |
 *   | fpr1 |   vararg FPR[1-3] save area (also used as volatile FPR[1-6] save area)
 *   | fpr2 |
 *   | fpr3 |
 *   | fpr4 |
 *   | fpr5 |
 *   + fpr6 +
 *   | r13  |   nonvolatile GPR[13-31] save area
 *   | ...  |
 *   + r31  +
 *   | fpr14|   nonvolatile FPR[14-31] save area
 *   | ...  |
 *   | fpr31|
 *   |topjav|   offset to preceding Java to C glue frame
 *   |------|
 *   | fp   | <- Native C caller frame
 *   | cr   |
 *   | lr   |
 *   | resv |
 *   | resv |
 *   + toc  +
 *   |   0  |    spill area initially not filled
 *   |   1  |    to be filled by this method
 *   |   2  |
 *   |   3  |
 *   |   4  |
 *   |   5  |
 *   |   6  |
 *   |   7  |
 *   |   8  |    spill area already filled by caller
 *   |   9  |
 *   |      |
 *   |      |
 *   |      |
 * </pre>
 *
 * @param methodID a MemberReference id
 * @param skip4Args if true, the calling JNI function has 4 args before the vararg
 *                  if false, the calling JNI function has 3 args before the vararg
 * @return the starting address of the vararg in the caller stack frame
 */
@NoInline
private static Address pushVarArgToSpillArea(int methodID, boolean skip4Args) throws Exception {
    if (!(VM.BuildForPower64ELF_ABI || VM.BuildForSVR4ABI)) {
        if (VM.VerifyAssertions)
            VM._assert(VM.NOT_REACHED);
        return Address.zero();
    }
    int glueFrameSize = JNI_GLUE_FRAME_SIZE;
    // get the FP for this stack frame and traverse 3 frames to get to the glue frame
    Address gluefp = // *.ppc.JNIHelpers.invoke*
    Magic.getFramePointer().plus(StackFrameLayout.getStackFramePointerOffset()).loadAddress();
    // architecture.JNIHelpers.invoke*
    gluefp = gluefp.plus(StackFrameLayout.getStackFramePointerOffset()).loadAddress();
    // JNIFunctions
    gluefp = gluefp.plus(StackFrameLayout.getStackFramePointerOffset()).loadAddress();
    // glue frame
    gluefp = gluefp.plus(StackFrameLayout.getStackFramePointerOffset()).loadAddress();
    // compute the offset into the area where the vararg GPR[6-10] and FPR[1-3] are saved
    // skipping the args which are not part of the arguments for the target method
    // For Call<type>Method functions and NewObject, skip 3 args
    // For CallNonvirtual<type>Method functions, skip 4 args
    Offset varargGPROffset = Offset.fromIntSignExtend(VARARG_AREA_OFFSET + (skip4Args ? BYTES_IN_ADDRESS : 0));
    Offset varargFPROffset = varargGPROffset.plus(5 * BYTES_IN_ADDRESS);
    // compute the offset into the spill area of the native caller frame,
    // skipping the args which are not part of the arguments for the target method
    // For Call<type>Method functions, skip 3 args
    // For CallNonvirtual<type>Method functions, skip 4 args
    Offset spillAreaLimit = Offset.fromIntSignExtend(glueFrameSize + NATIVE_FRAME_HEADER_SIZE + 8 * BYTES_IN_ADDRESS);
    Offset spillAreaOffset = Offset.fromIntSignExtend(glueFrameSize + NATIVE_FRAME_HEADER_SIZE + (skip4Args ? 4 * BYTES_IN_ADDRESS : 3 * BYTES_IN_ADDRESS));
    // address to return pointing to the var arg list
    Address varargAddress = gluefp.plus(spillAreaOffset);
    // VM.sysWrite("pushVarArgToSpillArea:  var arg at " +
    // Services.intAsHexString(varargAddress) + "\n");
    RVMMethod targetMethod = MemberReference.getMethodRef(methodID).resolve();
    TypeReference[] argTypes = targetMethod.getParameterTypes();
    int argCount = argTypes.length;
    for (int i = 0; i < argCount && spillAreaOffset.sLT(spillAreaLimit); i++) {
        Word hiword, loword;
        if (argTypes[i].isFloatingPointType()) {
            // move 2 words from the vararg FPR save area into the spill area of the caller
            hiword = gluefp.loadWord(varargFPROffset);
            varargFPROffset = varargFPROffset.plus(BYTES_IN_ADDRESS);
            if (VM.BuildFor32Addr) {
                loword = gluefp.loadWord(varargFPROffset);
                varargFPROffset = varargFPROffset.plus(BYTES_IN_ADDRESS);
            }
            gluefp.store(hiword, spillAreaOffset);
            spillAreaOffset = spillAreaOffset.plus(BYTES_IN_ADDRESS);
            if (VM.BuildFor32Addr) {
                gluefp.store(loword, spillAreaOffset);
                spillAreaOffset = spillAreaOffset.plus(BYTES_IN_ADDRESS);
            }
        } else if (argTypes[i].isLongType()) {
            // move 2 words from the vararg GPR save area into the spill area of the caller
            hiword = gluefp.loadWord(varargGPROffset);
            varargGPROffset = varargGPROffset.plus(BYTES_IN_ADDRESS);
            gluefp.store(hiword, spillAreaOffset);
            spillAreaOffset = spillAreaOffset.plus(BYTES_IN_ADDRESS);
            // this covers the case when the long value straddles the spill boundary
            if (VM.BuildFor32Addr && spillAreaOffset.sLT(spillAreaLimit)) {
                loword = gluefp.loadWord(varargGPROffset);
                varargGPROffset = varargGPROffset.plus(BYTES_IN_ADDRESS);
                gluefp.store(loword, spillAreaOffset);
                spillAreaOffset = spillAreaOffset.plus(BYTES_IN_ADDRESS);
            }
        } else {
            hiword = gluefp.loadWord(varargGPROffset);
            varargGPROffset = varargGPROffset.plus(BYTES_IN_ADDRESS);
            gluefp.store(hiword, spillAreaOffset);
            spillAreaOffset = spillAreaOffset.plus(BYTES_IN_ADDRESS);
        }
    }
    // return the address of the beginning of the vararg to use in invoking the target method
    return varargAddress;
}
Also used : RVMMethod(org.jikesrvm.classloader.RVMMethod) Word(org.vmmagic.unboxed.Word) Address(org.vmmagic.unboxed.Address) TypeReference(org.jikesrvm.classloader.TypeReference) Offset(org.vmmagic.unboxed.Offset) NoInline(org.vmmagic.pragma.NoInline)

Example 18 with Address

use of org.vmmagic.unboxed.Address in project JikesRVM by JikesRVM.

the class AlignmentEncoding method adjustRegion.

/**
 * Adjust a region address so that the object pointer of an object that starts at this address
 * will be aligned so as to encode the specified value.
 *
 * @param alignCode Value to encode
 * @param region The initial region
 * @return the aligned address
 */
public static Address adjustRegion(int alignCode, Address region) {
    assertSanity(alignCode);
    if (alignCode == ALIGN_CODE_NONE)
        return region;
    // Now fake the region address to encode our data
    final Address limit = region.plus(padding(alignCode));
    if (VERBOSE) {
        VM.sysWrite("Allocating TIB: region = ", region, " tib code = ", getTibCodeForRegion(region));
        VM.sysWriteln(", requested = ", alignCode);
    }
    while (getTibCodeForRegion(region) != alignCode) {
        Address next = region.plus(ALIGNMENT_INCREMENT);
        // Hack to allow alignment, but no alignment filling during boot
        while (region.LT(next)) {
            if (VM.runningVM) {
                region.store(ALIGNMENT_VALUE);
            }
            region = region.plus(BYTES_IN_INT);
        }
        if (region.GT(limit)) {
            VM.sysFail("Tib alignment fail");
        }
    }
    if (VERBOSE) {
        VM.sysWrite("           TIB: region = ", region, " tib code = ", getTibCodeForRegion(region));
        VM.sysWriteln(", requested = ", alignCode);
    }
    return region;
}
Also used : Address(org.vmmagic.unboxed.Address)

Example 19 with Address

use of org.vmmagic.unboxed.Address in project JikesRVM by JikesRVM.

the class JNIFunctions method RegisterNatives.

/**
 * RegisterNatives: registers implementation of native methods
 * @param env A JREF index for the JNI environment object
 * @param classJREF a JREF index for the class to register native methods in
 * @param methodsAddress the address of an array of native methods to be registered
 * @param nmethods the number of native methods in the array
 * @return 0 is successful -1 if failed
 * @throws NoSuchMethodError if a specified method cannot be found or is not native
 */
private static int RegisterNatives(JNIEnvironment env, int classJREF, Address methodsAddress, int nmethods) {
    if (traceJNI)
        VM.sysWriteln("JNI called: RegisterNatives");
    RuntimeEntrypoints.checkJNICountDownToGC();
    try {
        // get the target class
        Class<?> jcls = (Class<?>) env.getJNIRef(classJREF);
        RVMType type = java.lang.JikesRVMSupport.getTypeForClass(jcls);
        if (!type.isClassType()) {
            env.recordException(new NoSuchMethodError());
            return 0;
        }
        RVMClass klass = type.asClass();
        if (!klass.isInitialized()) {
            RuntimeEntrypoints.initializeClassForDynamicLink(klass);
        }
        // Create list of methods and verify them to avoid partial success
        NativeMethod[] methods = new NativeMethod[nmethods];
        AddressArray symbols = AddressArray.create(nmethods);
        Address curMethod = methodsAddress;
        for (int i = 0; i < nmethods; i++) {
            String methodString = JNIGenericHelpers.createStringFromC(curMethod.loadAddress());
            Atom methodName = Atom.findOrCreateAsciiAtom(methodString);
            String sigString = JNIGenericHelpers.createStringFromC(curMethod.loadAddress(Offset.fromIntSignExtend(BYTES_IN_ADDRESS)));
            Atom sigName = Atom.findOrCreateAsciiAtom(sigString);
            // Find the target method
            RVMMethod meth = klass.findDeclaredMethod(methodName, sigName);
            if (meth == null || !meth.isNative()) {
                env.recordException(new NoSuchMethodError(klass + ": " + methodName + " " + sigName));
                return -1;
            }
            methods[i] = (NativeMethod) meth;
            symbols.set(i, curMethod.loadAddress(Offset.fromIntSignExtend(BYTES_IN_ADDRESS * 2)));
            curMethod = curMethod.plus(3 * BYTES_IN_ADDRESS);
        }
        // Register methods
        for (int i = 0; i < nmethods; i++) {
            methods[i].registerNativeSymbol(symbols.get(i));
        }
        return 0;
    } catch (Throwable unexpected) {
        if (traceJNI)
            unexpected.printStackTrace(System.err);
        env.recordException(unexpected);
        return -1;
    }
}
Also used : AddressArray(org.vmmagic.unboxed.AddressArray) Address(org.vmmagic.unboxed.Address) RVMType(org.jikesrvm.classloader.RVMType) Atom(org.jikesrvm.classloader.Atom) RVMClass(org.jikesrvm.classloader.RVMClass) RVMMethod(org.jikesrvm.classloader.RVMMethod) NativeMethod(org.jikesrvm.classloader.NativeMethod) RVMClass(org.jikesrvm.classloader.RVMClass)

Example 20 with Address

use of org.vmmagic.unboxed.Address in project JikesRVM by JikesRVM.

the class JNIFunctions method GetDoubleArrayElements.

/**
 * GetDoubleArrayElements: get all the elements of a double array
 * @param env A JREF index for the JNI environment object
 * @param arrayJREF a JREF index for the source array
 * @param isCopyAddress address of a flag to indicate whether the returned array is a copy or a direct pointer
 * @return A pointer to the double array and the isCopy flag is set to true if it's a copy
 *         or false if it's a direct pointer
 * @throws OutOfMemoryError if the system runs out of memory
 */
private static Address GetDoubleArrayElements(JNIEnvironment env, int arrayJREF, Address isCopyAddress) {
    if (traceJNI)
        VM.sysWriteln("JNI called: GetDoubleArrayElements");
    RuntimeEntrypoints.checkJNICountDownToGC();
    try {
        double[] sourceArray = (double[]) env.getJNIRef(arrayJREF);
        int size = sourceArray.length;
        if (MemoryManager.willNeverMove(sourceArray)) {
            JNIGenericHelpers.setBoolStar(isCopyAddress, false);
            return Magic.objectAsAddress(sourceArray);
        } else {
            // alloc non moving buffer in C heap for a copy of string contents
            Address copyBuffer = sysCall.sysMalloc(size << LOG_BYTES_IN_DOUBLE);
            if (copyBuffer.isZero()) {
                env.recordException(new OutOfMemoryError());
                return Address.zero();
            }
            Memory.memcopy(copyBuffer, Magic.objectAsAddress(sourceArray), size << LOG_BYTES_IN_DOUBLE);
            /* Set caller's isCopy boolean to true, if we got a valid (non-null)
         address */
            JNIGenericHelpers.setBoolStar(isCopyAddress, true);
            return copyBuffer;
        }
    } catch (Throwable unexpected) {
        if (traceJNI)
            unexpected.printStackTrace(System.err);
        env.recordException(unexpected);
        return Address.zero();
    }
}
Also used : Address(org.vmmagic.unboxed.Address)

Aggregations

Address (org.vmmagic.unboxed.Address)281 Offset (org.vmmagic.unboxed.Offset)48 CompiledMethod (org.jikesrvm.compilers.common.CompiledMethod)30 NoInline (org.vmmagic.pragma.NoInline)30 Test (org.junit.Test)24 Entrypoint (org.vmmagic.pragma.Entrypoint)22 TypeReference (org.jikesrvm.classloader.TypeReference)21 OptCompiledMethod (org.jikesrvm.compilers.opt.runtimesupport.OptCompiledMethod)17 RVMType (org.jikesrvm.classloader.RVMType)16 Inline (org.vmmagic.pragma.Inline)15 Uninterruptible (org.vmmagic.pragma.Uninterruptible)14 Word (org.vmmagic.unboxed.Word)14 BaseMMTkTest (org.mmtk.harness.tests.BaseMMTkTest)13 Unpreemptible (org.vmmagic.pragma.Unpreemptible)12 ObjectReference (org.vmmagic.unboxed.ObjectReference)12 Interruptible (org.vmmagic.pragma.Interruptible)11 Extent (org.vmmagic.unboxed.Extent)11 RVMClass (org.jikesrvm.classloader.RVMClass)9 AddressConstantOperand (org.jikesrvm.compilers.opt.ir.operand.AddressConstantOperand)8 IntConstantOperand (org.jikesrvm.compilers.opt.ir.operand.IntConstantOperand)8