Skip to content

Commit

Permalink
Merge foreign-abi
Browse files Browse the repository at this point in the history
  • Loading branch information
J. Duke authored and JornVernee committed May 6, 2020
2 parents 6f69981 + 50e5acd commit 4303213
Show file tree
Hide file tree
Showing 24 changed files with 359 additions and 758 deletions.

Large diffs are not rendered by default.

Expand Up @@ -34,12 +34,13 @@

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import java.nio.ByteOrder;
import java.util.Optional;

/**
* This class models a system application binary interface (ABI).
*
* Instances of this class can be obtained by calling {@link Foreign#getSystemABI()}
* Instances of this class can be obtained by calling {@link SystemABI#getSystemABI()}
*/
public interface SystemABI {
/**
Expand Down Expand Up @@ -100,113 +101,317 @@ default void freeUpcallStub(MemoryAddress address) {
*/
String name();

enum Type {
/**
* The {@code _Bool} native type.
*/
ValueLayout C_BOOL = Utils.pick(SysV.C_BOOL, Win64.C_BOOL, AArch64.C_BOOL);

/**
* The {@code char} native type.
*/
ValueLayout C_CHAR = Utils.pick(SysV.C_CHAR, Win64.C_CHAR, AArch64.C_CHAR);

/**
* The {@code short} native type.
*/
ValueLayout C_SHORT = Utils.pick(SysV.C_SHORT, Win64.C_SHORT, AArch64.C_SHORT);

/**
* The {@code int} native type.
*/
ValueLayout C_INT = Utils.pick(SysV.C_INT, Win64.C_INT, AArch64.C_INT);

/**
* The {@code long} native type.
*/
ValueLayout C_LONG = Utils.pick(SysV.C_LONG, Win64.C_LONG, AArch64.C_LONG);

/**
* The {@code long long} native type.
*/
ValueLayout C_LONGLONG = Utils.pick(SysV.C_LONGLONG, Win64.C_LONGLONG, AArch64.C_LONGLONG);

/**
* The {@code float} native type.
*/
ValueLayout C_FLOAT = Utils.pick(SysV.C_FLOAT, Win64.C_FLOAT, AArch64.C_FLOAT);

/**
* The {@code double} native type.
*/
ValueLayout C_DOUBLE = Utils.pick(SysV.C_DOUBLE, Win64.C_DOUBLE, AArch64.C_DOUBLE);

/**
* The {@code long double} native type.
*/
ValueLayout C_LONGDOUBLE = Utils.pick(SysV.C_LONGDOUBLE, Win64.C_LONGDOUBLE, AArch64.C_LONGDOUBLE);

/**
* The {@code T*} native type.
*/
ValueLayout C_POINTER = Utils.pick(SysV.C_POINTER, Win64.C_POINTER, AArch64.C_POINTER);

/**
* This class defines layout constants modelling standard primitive types supported by the x64 SystemV ABI.
*/
final class SysV {
private SysV() {
//just the one
}

/**
* The name of the SysV ABI
*/
public static final String NAME = "SysV";

public final static String CLASS_ATTRIBUTE_NAME = "abi/sysv/class";

public enum ArgumentClass {
INTEGER,
SSE,
X87,
COMPLEX_87,
POINTER;
}

/**
* The {@code _Bool} native type.
*/
BOOL,
public static final ValueLayout C_BOOL = MemoryLayouts.BITS_8_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code unsigned char} native type.
* The {@code char} native type.
*/
UNSIGNED_CHAR,
public static final ValueLayout C_CHAR = MemoryLayouts.BITS_8_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code signed char} native type.
* The {@code short} native type.
*/
SIGNED_CHAR,
public static final ValueLayout C_SHORT = MemoryLayouts.BITS_16_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code char} native type.
* The {@code int} native type.
*/
CHAR,
public static final ValueLayout C_INT = MemoryLayouts.BITS_32_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code short} native type.
* The {@code long} native type.
*/
SHORT,
public static final ValueLayout C_LONG = MemoryLayouts.BITS_64_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code unsigned short} native type.
* The {@code long long} native type.
*/
UNSIGNED_SHORT,
public static final ValueLayout C_LONGLONG = MemoryLayouts.BITS_64_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code int} native type.
* The {@code float} native type.
*/
INT,
public static final ValueLayout C_FLOAT = MemoryLayouts.BITS_32_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.SSE);

/**
* The {@code unsigned int} native type.
* The {@code double} native type.
*/
UNSIGNED_INT,
public static final ValueLayout C_DOUBLE = MemoryLayouts.BITS_64_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.SSE);

/**
* The {@code long} native type.
* The {@code long double} native type.
*/
LONG,
public static final ValueLayout C_LONGDOUBLE = MemoryLayout.ofValueBits(128, ByteOrder.LITTLE_ENDIAN)
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.X87);

/**
* The {@code unsigned long} native type.
* The {@code complex long double} native type.
*/
UNSIGNED_LONG,
public static final GroupLayout C_COMPLEX_LONGDOUBLE = MemoryLayout.ofStruct(C_LONGDOUBLE, C_LONGDOUBLE)
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.COMPLEX_87);

/**
* The {@code long long} native type.
* The {@code T*} native type.
*/
LONG_LONG,
public static final ValueLayout C_POINTER = MemoryLayouts.BITS_64_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.POINTER);
}

/**
* This class defines layout constants modelling standard primitive types supported by the x64 Windows ABI.
*/
final class Win64 {

private Win64() {
//just the one
}

/**
* The {@code unsigned long long} native type.
* The name of the Windows ABI
*/
UNSIGNED_LONG_LONG,
public final static String NAME = "Windows";

public final static String VARARGS_ATTRIBUTE_NAME = "abi/windows/varargs";

public final static String CLASS_ATTRIBUTE_NAME = "abi/windows/class";

public enum ArgumentClass {
INTEGER,
FLOAT,
POINTER;
}

/**
* The {@code float} native type.
* The {@code _Bool} native type.
*/
FLOAT,
public static final ValueLayout C_BOOL = MemoryLayouts.BITS_8_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code double} native type.
* The {@code char} native type.
*/
DOUBLE,
public static final ValueLayout C_CHAR = MemoryLayouts.BITS_8_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code long double} native type.
* The {@code short} native type.
*/
LONG_DOUBLE,
public static final ValueLayout C_SHORT = MemoryLayouts.BITS_16_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code complex long double} native type.
* The {@code int} native type.
*/
COMPLEX_LONG_DOUBLE,
public static final ValueLayout C_INT = MemoryLayouts.BITS_32_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code T*} native type.
* The {@code long} native type.
*/
public static final ValueLayout C_LONG = MemoryLayouts.BITS_32_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code long long} native type.
*/
public static final ValueLayout C_LONGLONG = MemoryLayouts.BITS_64_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code float} native type.
*/
public static final ValueLayout C_FLOAT = MemoryLayouts.BITS_32_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.FLOAT);

/**
* The {@code double} native type.
*/
public static final ValueLayout C_DOUBLE = MemoryLayouts.BITS_64_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.FLOAT);

/**
* The {@code long double} native type.
*/
POINTER;
public static final ValueLayout C_LONGDOUBLE = MemoryLayouts.BITS_64_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.FLOAT);

/**
* Retrieve the ABI type attached to the given layout,
* or throw an {@code IllegalArgumentException} if there is none
*
* @param ml the layout to retrieve the ABI type of
* @return the retrieved ABI type
* @throws IllegalArgumentException if the given layout does not have an ABI type attribute
* The {@code T*} native type.
*/
public static Type fromLayout(MemoryLayout ml) throws IllegalArgumentException {
return ml.attribute(NATIVE_TYPE)
.map(SystemABI.Type.class::cast)
.orElseThrow(() -> new IllegalArgumentException("No ABI attribute present"));
public static final ValueLayout C_POINTER = MemoryLayouts.BITS_64_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.POINTER);

public static ValueLayout asVarArg(ValueLayout l) {
return l.withAttribute(VARARGS_ATTRIBUTE_NAME, "true");
}
}

/**
* Returns memory layout for the given native type if supported by the platform ABI.
* @param type the native type for which the layout is to be retrieved.
* @return the layout (if any) associated with {@code type}
* This class defines layout constants modelling standard primitive types supported by the AArch64 ABI.
*/
Optional<MemoryLayout> layoutFor(Type type);
final class AArch64 {

private AArch64() {
//just the one
}

/**
* The name of the AArch64 ABI
*/
public final static String NAME = "AArch64";

public static final String CLASS_ATTRIBUTE_NAME = "abi/aarch64/class";

public enum ArgumentClass {
INTEGER,
VECTOR,
POINTER;
}

/**
* The {@code _Bool} native type.
*/
public static final ValueLayout C_BOOL = MemoryLayouts.BITS_8_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code char} native type.
*/
public static final ValueLayout C_CHAR = MemoryLayouts.BITS_8_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code short} native type.
*/
public static final ValueLayout C_SHORT = MemoryLayouts.BITS_16_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code int} native type.
*/
public static final ValueLayout C_INT = MemoryLayouts.BITS_32_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code long} native type.
*/
public static final ValueLayout C_LONG = MemoryLayouts.BITS_64_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code long long} native type.
*/
public static final ValueLayout C_LONGLONG = MemoryLayouts.BITS_64_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.INTEGER);

/**
* The {@code float} native type.
*/
public static final ValueLayout C_FLOAT = MemoryLayouts.BITS_32_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.VECTOR);

/**
* The {@code double} native type.
*/
public static final ValueLayout C_DOUBLE = MemoryLayouts.BITS_64_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.VECTOR);

/**
* The {@code long double} native type.
*/
public static final ValueLayout C_LONGDOUBLE = MemoryLayout.ofValueBits(128, ByteOrder.LITTLE_ENDIAN)
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.VECTOR);

/**
* The {@code T*} native type.
*/
public static final ValueLayout C_POINTER = MemoryLayouts.BITS_64_LE
.withAttribute(CLASS_ATTRIBUTE_NAME, ArgumentClass.POINTER);
}

/**
* Obtain an instance of the system ABI.
Expand Down

0 comments on commit 4303213

Please sign in to comment.