Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename SystemABI to ForeignLinker, and move C support to a separate class. #174

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -33,7 +33,7 @@
/**
* A set of utilities for working with libraries using the C language/ABI
*/
public class C {
public class CSupport {
/**
* Obtain a linker that uses the de facto C ABI of the current system to do it's linking.
* <p>
@@ -45,7 +45,7 @@
* {@code permit}, {@code warn} or {@code debug} (the default value is set to {@code deny}).
*/
public static ForeignLinker getSystemLinker() {
Utils.checkRestrictedAccess("C.getSystemLinker");
Utils.checkRestrictedAccess("CSupport.getSystemLinker");
return SharedUtils.getSystemLinker();
}

@@ -32,7 +32,7 @@
* This class can be used to link native functions as a {@link MethodHandle}, or to link Java
* methods as a native function pointer (modelled as a {@link MemorySegment}).
*
* Instances of this interface can be obtained for instance by calling {@link C#getSystemLinker()}
* Instances of this interface can be obtained for instance by calling {@link CSupport#getSystemLinker()}
*/
public interface ForeignLinker {
/**
@@ -26,7 +26,7 @@

package jdk.internal.foreign;

import jdk.incubator.foreign.C;
import jdk.incubator.foreign.CSupport;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryHandles;
import jdk.incubator.foreign.MemoryLayout;
@@ -109,9 +109,9 @@ private static void throwIllegalAccessError(String value, String method) {
public static <Z extends MemoryLayout> Z pick(Z sysv, Z win64, Z aarch64) {
ForeignLinker abi = SharedUtils.getSystemLinker();
return switch (abi.name()) {
case C.SysV.NAME -> sysv;
case C.Win64.NAME -> win64;
case C.AArch64.NAME -> aarch64;
case CSupport.SysV.NAME -> sysv;
case CSupport.Win64.NAME -> win64;
case CSupport.AArch64.NAME -> aarch64;
default -> throw new ExceptionInInitializerError("Unexpected ABI: " + abi.name());
};
}
@@ -35,7 +35,7 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;

import static jdk.incubator.foreign.C.*;
import static jdk.incubator.foreign.CSupport.*;

/**
* ABI implementation based on ARM document "Procedure Call Standard for
@@ -46,7 +46,7 @@
import java.util.List;
import java.util.Optional;

import static jdk.incubator.foreign.C.*;
import static jdk.incubator.foreign.CSupport.*;
import static jdk.internal.foreign.abi.aarch64.AArch64Architecture.*;

/**
@@ -53,7 +53,7 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static jdk.incubator.foreign.C.*;
import static jdk.incubator.foreign.CSupport.*;
import static jdk.internal.foreign.abi.Binding.*;
import static jdk.internal.foreign.abi.x64.X86_64Architecture.*;
import static jdk.internal.foreign.abi.x64.sysv.SysVx64Linker.MAX_INTEGER_ARGUMENT_REGISTERS;
@@ -35,7 +35,7 @@
import java.lang.invoke.MethodType;
import java.util.Optional;

import static jdk.incubator.foreign.C.*;
import static jdk.incubator.foreign.CSupport.*;

/**
* ABI implementation based on System V ABI AMD64 supplement v.0.99.6
@@ -46,8 +46,8 @@
import java.util.List;
import java.util.Optional;

import static jdk.incubator.foreign.C.*;
import static jdk.incubator.foreign.C.Win64.VARARGS_ATTRIBUTE_NAME;
import static jdk.incubator.foreign.CSupport.*;
import static jdk.incubator.foreign.CSupport.Win64.VARARGS_ATTRIBUTE_NAME;
import static jdk.internal.foreign.abi.x64.X86_64Architecture.*;

/**
@@ -34,7 +34,7 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;

import static jdk.incubator.foreign.C.*;
import static jdk.incubator.foreign.CSupport.*;

/**
* ABI implementation based on Windows ABI AMD64 supplement v.0.99.6
@@ -38,7 +38,7 @@

import org.testng.annotations.*;

import static jdk.incubator.foreign.C.*;
import static jdk.incubator.foreign.CSupport.*;
import static org.testng.Assert.*;

public class CallGeneratorHelper extends NativeTestHelper {
@@ -27,7 +27,7 @@
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryLayout;
import jdk.incubator.foreign.MemorySegment;
import static jdk.incubator.foreign.C.C_CHAR;
import static jdk.incubator.foreign.CSupport.C_CHAR;

public final class Cstring {
// don't create!
@@ -22,34 +22,34 @@
*
*/

import jdk.incubator.foreign.C;
import jdk.incubator.foreign.CSupport;
import jdk.incubator.foreign.ForeignLinker;
import jdk.incubator.foreign.MemoryLayout;
import jdk.incubator.foreign.ValueLayout;

public class NativeTestHelper {

public static final ForeignLinker ABI = C.getSystemLinker();
public static final ForeignLinker ABI = CSupport.getSystemLinker();

public static boolean isIntegral(MemoryLayout layout) {
return switch (ABI.name()) {
case C.SysV.NAME -> layout.attribute(C.SysV.CLASS_ATTRIBUTE_NAME).get() == C.SysV.ArgumentClass.INTEGER;
case C.Win64.NAME -> layout.attribute(C.Win64.CLASS_ATTRIBUTE_NAME).get() == C.Win64.ArgumentClass.INTEGER;
case C.AArch64.NAME -> layout.attribute(C.AArch64.CLASS_ATTRIBUTE_NAME).get() == C.AArch64.ArgumentClass.INTEGER;
case CSupport.SysV.NAME -> layout.attribute(CSupport.SysV.CLASS_ATTRIBUTE_NAME).get() == CSupport.SysV.ArgumentClass.INTEGER;
case CSupport.Win64.NAME -> layout.attribute(CSupport.Win64.CLASS_ATTRIBUTE_NAME).get() == CSupport.Win64.ArgumentClass.INTEGER;
case CSupport.AArch64.NAME -> layout.attribute(CSupport.AArch64.CLASS_ATTRIBUTE_NAME).get() == CSupport.AArch64.ArgumentClass.INTEGER;
default -> throw new AssertionError("unexpected ABI: " + ABI.name());
};
}

public static boolean isPointer(MemoryLayout layout) {
return switch (ABI.name()) {
case C.SysV.NAME -> layout.attribute(C.SysV.CLASS_ATTRIBUTE_NAME).get() == C.SysV.ArgumentClass.POINTER;
case C.Win64.NAME -> layout.attribute(C.Win64.CLASS_ATTRIBUTE_NAME).get() == C.Win64.ArgumentClass.POINTER;
case C.AArch64.NAME -> layout.attribute(C.AArch64.CLASS_ATTRIBUTE_NAME).get() == C.AArch64.ArgumentClass.POINTER;
case CSupport.SysV.NAME -> layout.attribute(CSupport.SysV.CLASS_ATTRIBUTE_NAME).get() == CSupport.SysV.ArgumentClass.POINTER;
case CSupport.Win64.NAME -> layout.attribute(CSupport.Win64.CLASS_ATTRIBUTE_NAME).get() == CSupport.Win64.ArgumentClass.POINTER;
case CSupport.AArch64.NAME -> layout.attribute(CSupport.AArch64.CLASS_ATTRIBUTE_NAME).get() == CSupport.AArch64.ArgumentClass.POINTER;
default -> throw new AssertionError("unexpected ABI: " + ABI.name());
};
}

public static ValueLayout asVarArg(ValueLayout layout) {
return ABI.name().equals(C.Win64.NAME) ? C.Win64.asVarArg(layout) : layout;
return ABI.name().equals(CSupport.Win64.NAME) ? CSupport.Win64.asVarArg(layout) : layout;
}
}
@@ -51,7 +51,7 @@
import java.util.stream.LongStream;
import java.util.stream.Stream;

import jdk.incubator.foreign.C;
import jdk.incubator.foreign.CSupport;
import jdk.incubator.foreign.ForeignLinker;
import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.LibraryLookup;
@@ -62,13 +62,13 @@
import jdk.incubator.foreign.SequenceLayout;
import org.testng.annotations.*;

import static jdk.incubator.foreign.C.*;
import static jdk.incubator.foreign.CSupport.*;
import static org.testng.Assert.*;

@Test
public class StdLibTest extends NativeTestHelper {

final static ForeignLinker abi = C.getSystemLinker();
final static ForeignLinker abi = CSupport.getSystemLinker();

final static VarHandle byteHandle = MemoryHandles.varHandle(byte.class, ByteOrder.nativeOrder());
final static VarHandle intHandle = MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());
@@ -27,7 +27,7 @@
* @run testng/othervm TestCircularInit1
*/

import jdk.incubator.foreign.C;
import jdk.incubator.foreign.CSupport;
import org.testng.annotations.Test;

import static org.testng.Assert.assertNotNull;
@@ -36,8 +36,8 @@

@Test
public void testCircularInit() {
System.out.println(C.C_BOOL); // trigger clinit
assertNotNull(C.C_BOOL); // should not be null
System.out.println(CSupport.C_BOOL); // trigger clinit
assertNotNull(CSupport.C_BOOL); // should not be null
}

}
@@ -27,7 +27,7 @@
* @run testng/othervm TestCircularInit2
*/

import jdk.incubator.foreign.C;
import jdk.incubator.foreign.CSupport;
import org.testng.annotations.Test;

import static org.testng.Assert.assertNotNull;
@@ -36,10 +36,10 @@

@Test
public void testCircularInit() {
System.out.println(C.C_BOOL); // trigger clinit
assertNotNull(C.C_BOOL);
assertNotNull(C.C_BOOL);
assertNotNull(C.C_BOOL);
System.out.println(CSupport.C_BOOL); // trigger clinit
assertNotNull(CSupport.C_BOOL);
assertNotNull(CSupport.C_BOOL);
assertNotNull(CSupport.C_BOOL);
}

}
@@ -33,7 +33,7 @@
* @run testng/othervm -Dforeign.restricted=permit TestDowncall
*/

import jdk.incubator.foreign.C;
import jdk.incubator.foreign.CSupport;
import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.LibraryLookup;
import jdk.incubator.foreign.MemoryAddress;
@@ -51,7 +51,7 @@
public class TestDowncall extends CallGeneratorHelper {

static LibraryLookup lib = LibraryLookup.ofLibrary("TestDowncall");
static ForeignLinker abi = C.getSystemLinker();
static ForeignLinker abi = CSupport.getSystemLinker();


@Test(dataProvider="functions", dataProviderClass=CallGeneratorHelper.class)
@@ -28,7 +28,7 @@
* @run testng/othervm -Dforeign.restricted=permit TestIllegalLink
*/

import jdk.incubator.foreign.C;
import jdk.incubator.foreign.CSupport;
import jdk.incubator.foreign.ForeignLinker;
import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.MemoryAddress;
@@ -40,14 +40,14 @@

import java.lang.invoke.MethodType;

import static jdk.incubator.foreign.C.C_INT;
import static jdk.incubator.foreign.CSupport.C_INT;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

public class TestIllegalLink {

private static final MemoryAddress dummyTarget = MemoryAddress.NULL;
private static final ForeignLinker ABI = C.getSystemLinker();
private static final ForeignLinker ABI = CSupport.getSystemLinker();

@Test(dataProvider = "types")
public void testTypeMismatch(MethodType mt, FunctionDescriptor desc, String expectedExceptionMessage) {
@@ -33,7 +33,7 @@
* @run testng/othervm -Dforeign.restricted=permit TestUpcall
*/

import jdk.incubator.foreign.C;
import jdk.incubator.foreign.CSupport;
import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.GroupLayout;
import jdk.incubator.foreign.LibraryLookup;
@@ -57,14 +57,14 @@
import java.util.stream.Collectors;

import static java.lang.invoke.MethodHandles.insertArguments;
import static jdk.incubator.foreign.C.C_POINTER;
import static jdk.incubator.foreign.CSupport.C_POINTER;
import static org.testng.Assert.assertEquals;


public class TestUpcall extends CallGeneratorHelper {

static LibraryLookup lib = LibraryLookup.ofLibrary("TestUpcall");
static ForeignLinker abi = C.getSystemLinker();
static ForeignLinker abi = CSupport.getSystemLinker();

static MethodHandle DUMMY;
static MethodHandle PASS_AND_SAVE;
@@ -27,7 +27,7 @@
* @run testng/othervm -Dforeign.restricted=permit TestUpcallStubs
*/

import jdk.incubator.foreign.C;
import jdk.incubator.foreign.CSupport;
import jdk.incubator.foreign.ForeignLinker;
import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.MemoryAddress;
@@ -44,7 +44,7 @@

public class TestUpcallStubs {

static final ForeignLinker abi = C.getSystemLinker();
static final ForeignLinker abi = CSupport.getSystemLinker();
static final MethodHandle MH_dummy;

static {
@@ -31,7 +31,7 @@
* @run testng/othervm -Dforeign.restricted=permit TestVarArgs
*/

import jdk.incubator.foreign.C;
import jdk.incubator.foreign.CSupport;
import jdk.incubator.foreign.ForeignLinker;
import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.LibraryLookup;
@@ -48,7 +48,7 @@
import java.util.ArrayList;
import java.util.List;

import static jdk.incubator.foreign.C.*;
import static jdk.incubator.foreign.CSupport.*;
import static jdk.incubator.foreign.MemoryLayout.PathElement.*;
import static org.testng.Assert.assertEquals;

@@ -63,7 +63,7 @@

static final VarHandle VH_IntArray = MemoryLayout.ofSequence(C_INT).varHandle(int.class, sequenceElement());

static final ForeignLinker abi = C.getSystemLinker();
static final ForeignLinker abi = CSupport.getSystemLinker();
static final MemoryAddress varargsAddr;

static {
@@ -44,7 +44,7 @@

import java.lang.invoke.MethodType;

import static jdk.incubator.foreign.C.AArch64.*;
import static jdk.incubator.foreign.CSupport.AArch64.*;
import static jdk.internal.foreign.abi.Binding.*;
import static jdk.internal.foreign.abi.aarch64.AArch64Architecture.*;
import static org.testng.Assert.assertEquals;
@@ -45,7 +45,7 @@

import java.lang.invoke.MethodType;

import static jdk.incubator.foreign.C.SysV.*;
import static jdk.incubator.foreign.CSupport.SysV.*;
import static jdk.internal.foreign.abi.Binding.*;
import static jdk.internal.foreign.abi.x64.X86_64Architecture.*;
import static org.testng.Assert.assertEquals;
@@ -44,7 +44,7 @@

import java.lang.invoke.MethodType;

import static jdk.incubator.foreign.C.Win64.*;
import static jdk.incubator.foreign.CSupport.Win64.*;
import static jdk.internal.foreign.abi.Binding.*;
import static jdk.internal.foreign.abi.Binding.copy;
import static jdk.internal.foreign.abi.x64.X86_64Architecture.*;
ProTip! Use n and p to navigate between commits in a pull request.