Search in sources :

Example 76 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class StructTests method test_add3BoolStructsWithXor_returnStruct.

@Test
public void test_add3BoolStructsWithXor_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"), C_INT.withName("elem3"));
    VarHandle boolHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
    VarHandle boolHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
    VarHandle boolHandle3 = structLayout.varHandle(int.class, PathElement.groupElement("elem3"));
    MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add3BoolStructsWithXor_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    boolHandle1.set(structSegmt1, 1);
    boolHandle2.set(structSegmt1, 0);
    boolHandle3.set(structSegmt1, 1);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    boolHandle1.set(structSegmt2, 1);
    boolHandle2.set(structSegmt2, 1);
    boolHandle3.set(structSegmt2, 0);
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals(boolHandle1.get(resultSegmt), 0);
    Assert.assertEquals(boolHandle2.get(resultSegmt), 1);
    Assert.assertEquals(boolHandle3.get(resultSegmt), 1);
    structSegmt1.close();
    structSegmt2.close();
    resultSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 77 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class StructTests method test_addIntAndIntShortFromStruct.

@Test
public void test_addIntAndIntShortFromStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_SHORT.withName("elem2"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize()));
    VarHandle elemHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
    VarHandle elemHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addIntAndIntShortFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    elemHandle1.set(structSegmt, 11223344);
    elemHandle2.set(structSegmt, (short) 32766);
    int result = (int) mh.invokeExact(22334455, structSegmt);
    Assert.assertEquals(result, 33590565);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 78 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class PrimitiveTypeTests method test_addDoubleAndDoubleFromPointer.

@Test
public void test_addDoubleAndDoubleFromPointer() throws Throwable {
    MethodType mt = MethodType.methodType(double.class, MemoryAddress.class, double.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_POINTER, C_DOUBLE);
    Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoubleFromPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment doubleSegmt = MemorySegment.allocateNative(C_DOUBLE);
    MemoryAccess.setDouble(doubleSegmt, 159.748d);
    double result = (double) mh.invokeExact(doubleSegmt.address(), 262.795d);
    doubleSegmt.close();
    Assert.assertEquals(result, 422.543d, 0.001d);
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_DOUBLE.withName("double"), C_POINTER.withName("pointer"), C_DOUBLE.withName("double"));
    mh = clinker.downcallHandle(functionSymbol, mt, fd2);
    doubleSegmt = MemorySegment.allocateNative(C_DOUBLE.withName("double"));
    MemoryAccess.setDouble(doubleSegmt, 1159.748d);
    result = (double) mh.invokeExact(doubleSegmt.address(), 1262.795d);
    doubleSegmt.close();
    Assert.assertEquals(result, 2422.543d, 0.001d);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 79 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class PrimitiveTypeTests method test_addTwoBytes.

@Test
public void test_addTwoBytes() throws Throwable {
    MethodType mt = MethodType.methodType(byte.class, byte.class, byte.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR);
    Symbol functionSymbol = nativeLib.lookup("add2Bytes").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    byte result = (byte) mh.invokeExact((byte) 6, (byte) 3);
    Assert.assertEquals(result, (byte) 9);
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_CHAR.withName("char"), C_CHAR.withName("char"), C_CHAR.withName("char"));
    mh = clinker.downcallHandle(functionSymbol, mt, fd2);
    result = (byte) mh.invokeExact((byte) 8, (byte) 9);
    Assert.assertEquals(result, (byte) 17);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 80 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class PrimitiveTypeTests method test_printfFromDefaultLibWithMemAddr.

@Test
public void test_printfFromDefaultLibWithMemAddr() throws Throwable {
    Symbol functionSymbol = defaultLib.lookup("printf").get();
    MethodType mt = MethodType.methodType(int.class, MemoryAddress.class, int.class, int.class, int.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, C_INT, C_INT, C_INT);
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment formatMemSegment = CLinker.toCString("\n%d + %d = %d\n");
    mh.invoke(formatMemSegment.address(), 15, 27, 42);
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_POINTER.withName("pointer"), C_INT.withName("int"), C_INT.withName("int"), C_INT.withName("int"));
    mh = clinker.downcallHandle(functionSymbol, mt, fd2);
    mh.invoke(formatMemSegment.address(), 115, 127, 242);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)939 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)939 Test (org.testng.annotations.Test)939 MemorySegment (jdk.incubator.foreign.MemorySegment)754 GroupLayout (jdk.incubator.foreign.GroupLayout)675 MethodType (java.lang.invoke.MethodType)605 ResourceScope (jdk.incubator.foreign.ResourceScope)549 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)541 Addressable (jdk.incubator.foreign.Addressable)432 NativeSymbol (jdk.incubator.foreign.NativeSymbol)334 VarHandle (java.lang.invoke.VarHandle)318 SequenceLayout (jdk.incubator.foreign.SequenceLayout)240 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)173 MemoryAddress (jdk.incubator.foreign.MemoryAddress)127 VaList (jdk.incubator.foreign.VaList)4