Search in sources :

Example 76 with VarHandle

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

the class StructTests1 method test_add2LongStructs_returnStruct_1.

@Test
public void test_add2LongStructs_returnStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(longLayout.withName("elem1"), longLayout.withName("elem2"));
    VarHandle longHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1"));
    VarHandle longHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("add2LongStructs_returnStruct").get();
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        longHandle1.set(structSegmt1, 987654321987L);
        longHandle2.set(structSegmt1, 123456789123L);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        longHandle1.set(structSegmt2, 224466880022L);
        longHandle2.set(structSegmt2, 113355779911L);
        MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
        Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L);
        Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 77 with VarHandle

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

the class StructTests1 method test_addCharAndCharsFromStructPointer_1.

@Test
public void test_addCharAndCharsFromStructPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(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(char.class, char.class, MemoryAddress.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_POINTER);
    Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        charHandle1.set(structSegmt, 'H');
        charHandle2.set(structSegmt, 'I');
        char result = (char) mh.invokeExact('G', structSegmt.address());
        Assert.assertEquals(result, 'V');
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 78 with VarHandle

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

the class StructTests1 method test_add3ShortStructs_returnStruct_1.

@Test
public void test_add3ShortStructs_returnStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"), C_SHORT.withName("elem3"), MemoryLayout.paddingLayout(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);
    Addressable functionSymbol = nativeLibLookup.lookup("add3ShortStructs_returnStruct").get();
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        shortHandle1.set(structSegmt1, (short) 25);
        shortHandle2.set(structSegmt1, (short) 26);
        shortHandle3.set(structSegmt1, (short) 27);
        MemorySegment structSegmt2 = allocator.allocate(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);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 79 with VarHandle

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

the class StructTests1 method test_addBoolAndBoolsFromStructWithXor_1.

@Test
public void test_addBoolAndBoolsFromStructWithXor_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    VarHandle boolHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
    VarHandle boolHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithXor").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        boolHandle1.set(structSegmt, (byte) 0);
        boolHandle2.set(structSegmt, (byte) 1);
        boolean result = (boolean) mh.invokeExact(false, structSegmt);
        Assert.assertEquals(result, true);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 80 with VarHandle

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

the class StructTests1 method test_addLongFromPointerAndLongsFromStruct_1.

@Test
public void test_addLongFromPointerAndLongsFromStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(longLayout.withName("elem1"), longLayout.withName("elem2"));
    VarHandle longHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1"));
    VarHandle longHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(long.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_POINTER, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment longSegmt = allocator.allocate(longLayout);
        MemoryAccess.setLong(longSegmt, 1111111111L);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        longHandle1.set(structSegmt, 3333333333L);
        longHandle2.set(structSegmt, 5555555555L);
        long result = (long) mh.invokeExact(longSegmt.address(), structSegmt);
        Assert.assertEquals(result, 9999999999L);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) 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