Search in sources :

Example 51 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class StructTests method test_addIntAndLongIntFromStruct.

@Test
public void test_addIntAndLongIntFromStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), C_INT.withName("elem2"), MemoryLayout.ofPaddingBits(C_INT.bitSize()));
    VarHandle elemHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1"));
    VarHandle elemHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(long.class, int.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_INT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addIntAndLongIntFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    elemHandle1.set(structSegmt, 667788990011L);
    elemHandle2.set(structSegmt, 11223344);
    long result = (long) mh.invokeExact(1234567, structSegmt);
    Assert.assertEquals(result, 667801447922L);
    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 52 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class StructTests method test_addByteAndBytesFromStruct.

@Test
public void test_addByteAndBytesFromStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
    VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    byteHandle1.set(structSegmt, (byte) 8);
    byteHandle2.set(structSegmt, (byte) 9);
    byte result = (byte) mh.invokeExact((byte) 6, structSegmt);
    Assert.assertEquals(result, 23);
    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 53 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class StructTests method test_addByteAndBytesFromNestedStruct_reverseOrder.

@Test
public void test_addByteAndBytesFromNestedStruct_reverseOrder() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), nestedStructLayout.withName("struct_elem2"), MemoryLayout.ofPaddingBits(C_CHAR.bitSize()));
    MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromNestedStruct_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 12);
    MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 24);
    MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 36);
    byte result = (byte) mh.invokeExact((byte) 48, structSegmt);
    Assert.assertEquals(result, 120);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) 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 54 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol 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 55 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol 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)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)173 MethodType (java.lang.invoke.MethodType)173 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)173 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)173 Test (org.testng.annotations.Test)173 MemorySegment (jdk.incubator.foreign.MemorySegment)149 GroupLayout (jdk.incubator.foreign.GroupLayout)135 VarHandle (java.lang.invoke.VarHandle)64 SequenceLayout (jdk.incubator.foreign.SequenceLayout)48 MemoryAddress (jdk.incubator.foreign.MemoryAddress)30