Search in sources :

Example 41 with VarHandle

use of java.lang.invoke.VarHandle in project openj9 by eclipse.

the class StructTests method test_add2CharStructs_returnStruct.

@Test
public void test_add2CharStructs_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
    VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1"));
    VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add2CharStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    charHandle1.set(structSegmt1, 'A');
    charHandle2.set(structSegmt1, 'B');
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    charHandle1.set(structSegmt2, 'C');
    charHandle2.set(structSegmt2, 'D');
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals(charHandle1.get(resultSegmt), 'C');
    Assert.assertEquals(charHandle2.get(resultSegmt), 'E');
    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 42 with VarHandle

use of java.lang.invoke.VarHandle in project openj9 by eclipse.

the class StructTests method test_add3ShortStructs_returnStruct.

@Test
public void test_add3ShortStructs_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"), C_SHORT.withName("elem3"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize()));
    VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1"));
    VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2"));
    VarHandle shortHandle3 = structLayout.varHandle(short.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("add3ShortStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    shortHandle1.set(structSegmt1, (short) 25);
    shortHandle2.set(structSegmt1, (short) 26);
    shortHandle3.set(structSegmt1, (short) 27);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    shortHandle1.set(structSegmt2, (short) 34);
    shortHandle2.set(structSegmt2, (short) 35);
    shortHandle3.set(structSegmt2, (short) 36);
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals((short) shortHandle1.get(resultSegmt), (short) 59);
    Assert.assertEquals((short) shortHandle2.get(resultSegmt), (short) 61);
    Assert.assertEquals((short) shortHandle3.get(resultSegmt), (short) 63);
    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 43 with VarHandle

use of java.lang.invoke.VarHandle in project openj9 by eclipse.

the class StructTests method test_add2FloatStructs_returnStructPointer.

@Test
public void test_add2FloatStructs_returnStructPointer() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
    VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1"));
    VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add2FloatStructs_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    floatHandle1.set(structSegmt1, 25.12F);
    floatHandle2.set(structSegmt1, 11.23F);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    floatHandle1.set(structSegmt2, 24.34F);
    floatHandle2.set(structSegmt2, 13.45F);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
    Assert.assertEquals((float) floatHandle1.get(resultSegmt), 49.46F, 0.01F);
    Assert.assertEquals((float) floatHandle2.get(resultSegmt), 24.68F, 0.01F);
    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) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 44 with VarHandle

use of java.lang.invoke.VarHandle in project openj9 by eclipse.

the class StructTests method test_add3ByteStructs_returnStruct.

@Test
public void test_add3ByteStructs_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"), C_CHAR.withName("elem3"), MemoryLayout.ofPaddingBits(C_CHAR.bitSize()));
    VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
    VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
    VarHandle byteHandle3 = structLayout.varHandle(byte.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("add3ByteStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    byteHandle1.set(structSegmt1, (byte) 25);
    byteHandle2.set(structSegmt1, (byte) 11);
    byteHandle3.set(structSegmt1, (byte) 12);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    byteHandle1.set(structSegmt2, (byte) 24);
    byteHandle2.set(structSegmt2, (byte) 13);
    byteHandle3.set(structSegmt2, (byte) 16);
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals((byte) byteHandle1.get(resultSegmt), (byte) 49);
    Assert.assertEquals((byte) byteHandle2.get(resultSegmt), (byte) 24);
    Assert.assertEquals((byte) byteHandle3.get(resultSegmt), (byte) 28);
    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 45 with VarHandle

use of java.lang.invoke.VarHandle in project openj9 by eclipse.

the class StructTests method test_add3CharStructs_returnStruct.

@Test
public void test_add3CharStructs_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"), C_SHORT.withName("elem3"));
    VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1"));
    VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2"));
    VarHandle charHandle3 = structLayout.varHandle(char.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("add3CharStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    charHandle1.set(structSegmt1, 'A');
    charHandle2.set(structSegmt1, 'B');
    charHandle3.set(structSegmt1, 'C');
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    charHandle1.set(structSegmt2, 'B');
    charHandle2.set(structSegmt2, 'C');
    charHandle3.set(structSegmt2, 'D');
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals(charHandle1.get(resultSegmt), 'B');
    Assert.assertEquals(charHandle2.get(resultSegmt), 'D');
    Assert.assertEquals(charHandle3.get(resultSegmt), 'F');
    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)

Aggregations

VarHandle (java.lang.invoke.VarHandle)400 Test (org.testng.annotations.Test)388 MethodHandle (java.lang.invoke.MethodHandle)319 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)318 GroupLayout (jdk.incubator.foreign.GroupLayout)318 MemorySegment (jdk.incubator.foreign.MemorySegment)318 ResourceScope (jdk.incubator.foreign.ResourceScope)254 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)254 MethodType (java.lang.invoke.MethodType)192 Addressable (jdk.incubator.foreign.Addressable)128 NativeSymbol (jdk.incubator.foreign.NativeSymbol)126 MemoryAddress (jdk.incubator.foreign.MemoryAddress)78 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)64 Test (org.junit.Test)8 VarHandleDesc (java.lang.invoke.VarHandle.VarHandleDesc)2 ClassDesc (java.lang.constant.ClassDesc)1 Field (java.lang.reflect.Field)1