Skip to content

Commit

Permalink
JDK-8244270: reorganize ABI-dependent layout constants (second attempt)
Browse files Browse the repository at this point in the history
Reviewed-by: jvernee
  • Loading branch information
mcimadamore authored and JornVernee committed May 6, 2020
1 parent 4303213 commit aeec1cb
Show file tree
Hide file tree
Showing 27 changed files with 1,587 additions and 1,704 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.MemoryLayout;
import jdk.incubator.foreign.SystemABI;
import jdk.internal.jextract.impl.LayoutUtils;
import jdk.internal.jextract.impl.TypeImpl;

Expand Down Expand Up @@ -90,89 +91,94 @@ enum Kind {
/**
* {@code void} type.
*/
Void("void"),
Void("void", null),
/**
* {@code Bool} type.
*/
Bool("_Bool"),
Bool("_Bool", SystemABI.C_BOOL),
/**
* {@code char} type.
*/
Char("char"),
Char("char", SystemABI.C_CHAR),
/**
* {@code char16} type.
*/
Char16("char16"),
Char16("char16", null),
/**
* {@code char32} type.
*/
Char32("char32"),
Char32("char32", null),
/**
* {@code short} type.
*/
Short("short"),
Short("short", SystemABI.C_SHORT),
/**
* {@code int} type.
*/
Int("int"),
Int("int", SystemABI.C_INT),
/**
* {@code long} type.
*/
Long("long"),
Long("long", SystemABI.C_LONG),
/**
* {@code long long} type.
*/
LongLong("long long"),
LongLong("long long", SystemABI.C_LONGLONG),
/**
* {@code int128} type.
*/
Int128("__int128"),
Int128("__int128", null),
/**
* {@code float} type.
*/
Float("float"),
Float("float", SystemABI.C_FLOAT),
/**
* {@code double} type.
*/
Double("double"),
Double("double",SystemABI.C_DOUBLE),
/**
* {@code long double} type.
*/
LongDouble("long double"),
LongDouble("long double", SystemABI.C_LONGDOUBLE),
/**
* {@code float128} type.
*/
Float128("float128"),
Float128("float128", null),
/**
* {@code float16} type.
*/
HalfFloat("__fp16"),
HalfFloat("__fp16", null),
/**
* {@code wchar} type.
*/
WChar("wchar_t");
WChar("wchar_t", null);

private String typeName;
Kind(String typeName) {
private final String typeName;
private final MemoryLayout layout;

Kind(String typeName, MemoryLayout layout) {
this.typeName = typeName;
this.layout = layout;
}

public String typeName() {
return typeName;
}

/**
* The primitive type (optional) layout.
* @return The primitive type (optional) layout.
*/
public Optional<MemoryLayout> layout() {
return Optional.ofNullable(layout);
}
}

/**
* The primitive type kind.
* @return The primitive type kind.
*/
Kind kind();

/**
* The primitive type (optional) layout.
* @return The primitive type (optional) layout.
*/
Optional<MemoryLayout> layout();
}

/**
Expand Down Expand Up @@ -398,13 +404,12 @@ static Type.Primitive void_() {
}

/**
* Creates a new primitive type given kind and layout.
* Creates a new primitive type given kind.
* @param kind the primitive type kind.
* @param layout the primitive type layout.
* @return a new primitive type with given kind and layout.
* @return a new primitive type with given kind.
*/
static Type.Primitive primitive(Type.Primitive.Kind kind, MemoryLayout layout) {
return new TypeImpl.PrimitiveImpl(kind, layout);
static Type.Primitive primitive(Type.Primitive.Kind kind) {
return new TypeImpl.PrimitiveImpl(kind);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import jdk.incubator.foreign.MemoryLayout;
import jdk.incubator.foreign.MemorySegment;
import jdk.incubator.foreign.SystemABI;
import jdk.incubator.jextract.Type.Primitive;
import jdk.internal.foreign.abi.SharedUtils;

import javax.tools.JavaFileObject;
Expand All @@ -45,10 +46,8 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -112,15 +111,15 @@ public OutputFactory(String clsName, String pkgName, HeaderBuilder builder, Cons
}

private static String getCLangConstantsHolder() {
String prefix = "jdk.incubator.foreign.MemoryLayouts.";
String prefix = "jdk.incubator.foreign.SystemABI.";
String abi = SharedUtils.getSystemABI().name();
switch (abi) {
case SystemABI.ABI_SYSV:
return prefix + "SysV";
case SystemABI.ABI_WINDOWS:
return prefix + "WinABI";
return prefix + "Win64";
case SystemABI.ABI_AARCH64:
return prefix + "AArch64ABI";
return prefix + "AArch64";
default:
throw new UnsupportedOperationException("Unsupported ABI: " + abi);
}
Expand Down Expand Up @@ -182,14 +181,10 @@ private List<JavaFileObject> getPrimitiveTypeFiles(String pkgName) throws IOExce

List<JavaFileObject> files = new ArrayList<>();
String pkgPrefix = pkgName.isEmpty()? "" : "package " + pkgName + ";\n";
for (SystemABI.Type type : SystemABI.Type.values()) {
// FIXME: ignore pointer and complex type
if (type == SystemABI.Type.POINTER || type == SystemABI.Type.COMPLEX_LONG_DOUBLE) {
continue;
}

String typeName = type.name().toLowerCase();
MemoryLayout layout = abi.layoutFor(type).get();
for (Primitive.Kind type : Primitive.Kind.values()) {
if (type.layout().isEmpty()) continue;
String typeName = type.typeName().toLowerCase().replace(' ', '_');
MemoryLayout layout = type.layout().get();
String contents = pkgPrefix +
lines.stream().collect(Collectors.joining("\n")).
replace("-X", typeName).
Expand All @@ -201,9 +196,9 @@ private List<JavaFileObject> getPrimitiveTypeFiles(String pkgName) throws IOExce
return files;
}

private static Class<?> classForType(SystemABI.Type type, MemoryLayout layout) {
private static Class<?> classForType(Primitive.Kind type, MemoryLayout layout) {
boolean isFloat = switch(type) {
case FLOAT, DOUBLE, LONG_DOUBLE -> true;
case Float, Double, LongDouble -> true;
default-> false;
};
return TypeTranslator.layoutToClass(isFloat, layout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package jdk.incubator.jextract.tool;

import jdk.incubator.foreign.SystemABI;
import jdk.incubator.jextract.Type.Primitive;
import jdk.incubator.jextract.Type;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryLayout;
Expand All @@ -36,10 +37,10 @@
public class TypeTranslator implements Type.Visitor<Class<?>, Void> {
@Override
public Class<?> visitPrimitive(Type.Primitive t, Void aVoid) {
if (t.layout().isEmpty()) {
if (t.kind().layout().isEmpty()) {
return void.class;
} else {
return layoutToClass(isFloatingPoint(t), t.layout().orElseThrow(UnsupportedOperationException::new));
return layoutToClass(isFloatingPoint(t), t.kind().layout().orElseThrow(UnsupportedOperationException::new));
}
}

Expand All @@ -56,27 +57,18 @@ private boolean isFloatingPoint(Type.Primitive t) {
}
}

static String typeToLayoutName(SystemABI.Type type) {
static String typeToLayoutName(Primitive.Kind type) {
return switch (type) {
case BOOL -> "C_BOOL";
case SIGNED_CHAR -> "C_SCHAR";
case UNSIGNED_CHAR -> "C_UCHAR";
case CHAR -> "C_CHAR";
case SHORT -> "C_SHORT";
case UNSIGNED_SHORT -> "C_USHORT";
case INT -> "C_INT";
case UNSIGNED_INT -> "C_UINT";
case LONG -> "C_LONG";
case UNSIGNED_LONG -> "C_ULONG";
case LONG_LONG -> "C_LONGLONG";
case UNSIGNED_LONG_LONG -> "C_ULONGLONG";
case FLOAT -> "C_FLOAT";
case DOUBLE -> "C_DOUBLE";
case LONG_DOUBLE -> "C_LONGDOUBLE";
case POINTER -> "C_POINTER";
default -> {
throw new RuntimeException("should not reach here: " + type);
}
case Bool -> "C_BOOL";
case Char -> "C_CHAR";
case Short -> "C_SHORT";
case Int -> "C_INT";
case Long -> "C_LONG";
case LongLong -> "C_LONGLONG";
case Float -> "C_FLOAT";
case Double -> "C_DOUBLE";
case LongDouble -> "C_LONGDOUBLE";
default -> throw new RuntimeException("should not reach here: " + type);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import jdk.incubator.foreign.NativeAllocationScope;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryLayout;
import jdk.incubator.foreign.MemorySegment;
import static jdk.incubator.foreign.MemoryLayouts.C_CHAR;
import static jdk.incubator.foreign.SystemABI.C_CHAR;

public final class Cstring {
// don't create!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryHandles;
import jdk.incubator.foreign.MemorySegment;
import jdk.incubator.foreign.SystemABI;
import jdk.internal.clang.libclang.Index_h;

import java.lang.invoke.VarHandle;
Expand All @@ -37,8 +38,6 @@
import java.util.List;
import java.util.function.Consumer;

import static jdk.internal.jextract.impl.LayoutUtils.C_POINTER;

public class Index implements AutoCloseable {
// Pointer to CXIndex
private MemoryAddress ptr;
Expand Down Expand Up @@ -77,13 +76,13 @@ public ParsingFailedException(Path srcFile, ErrorCode code) {
}

private static final VarHandle VH_MemoryAddress =
MemoryHandles.asAddressVarHandle(C_POINTER.varHandle(long.class));
MemoryHandles.asAddressVarHandle(SystemABI.C_POINTER.varHandle(long.class));

public TranslationUnit parseTU(String file, Consumer<Diagnostic> dh, int options, String... args)
throws ParsingFailedException {
try (MemorySegment src = Utils.toNativeString(file) ;
MemorySegment cargs = Utils.toNativeStringArray(args);
MemorySegment outAddress = MemorySegment.allocateNative(C_POINTER)) {
MemorySegment outAddress = MemorySegment.allocateNative(SystemABI.C_POINTER)) {
ErrorCode code = ErrorCode.valueOf(Index_h.clang_parseTranslationUnit2(
ptr,
src.baseAddress(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class LibClang {
"_putenv" : "putenv";
MethodHandle PUT_ENV = abi.downcallHandle(LibraryLookup.ofDefault().lookup(putenv),
MethodType.methodType(int.class, MemoryAddress.class),
FunctionDescriptor.of(LayoutUtils.C_INT, LayoutUtils.C_POINTER));
FunctionDescriptor.of(SystemABI.C_INT, SystemABI.C_POINTER));
int res = (int) PUT_ENV.invokeExact(disableCrashRecovery.baseAddress());
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemorySegment;
import jdk.incubator.foreign.SystemABI;
import jdk.internal.clang.libclang.Index_h;
import jdk.internal.jextract.impl.LayoutUtils;

Expand All @@ -50,10 +51,10 @@ void get(MemorySegment loc, MemoryAddress file,

@SuppressWarnings("unchecked")
private Location getLocation(LocationFactory fn) {
try (MemorySegment file = MemorySegment.allocateNative(LayoutUtils.C_POINTER);
MemorySegment line = MemorySegment.allocateNative(LayoutUtils.C_INT);
MemorySegment col = MemorySegment.allocateNative(LayoutUtils.C_INT);
MemorySegment offset = MemorySegment.allocateNative(LayoutUtils.C_INT)) {
try (MemorySegment file = MemorySegment.allocateNative(SystemABI.C_POINTER);
MemorySegment line = MemorySegment.allocateNative(SystemABI.C_INT);
MemorySegment col = MemorySegment.allocateNative(SystemABI.C_INT);
MemorySegment offset = MemorySegment.allocateNative(SystemABI.C_INT)) {

fn.get(loc, file.baseAddress(), line.baseAddress(), col.baseAddress(), offset.baseAddress());
MemoryAddress fname = Utils.getPointer(file.baseAddress());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryLayout;
import jdk.incubator.foreign.MemorySegment;
import jdk.incubator.foreign.SystemABI;
import jdk.internal.clang.libclang.Index_h;
import jdk.internal.foreign.MemoryAddressImpl;
import jdk.internal.jextract.impl.LayoutUtils;
Expand Down Expand Up @@ -119,8 +120,8 @@ public String[] tokens(SourceRange range) {
}

public Tokens tokenize(SourceRange range) {
MemorySegment p = MemorySegment.allocateNative(LayoutUtils.C_POINTER);
MemorySegment pCnt = MemorySegment.allocateNative(LayoutUtils.C_INT);
MemorySegment p = MemorySegment.allocateNative(SystemABI.C_POINTER);
MemorySegment pCnt = MemorySegment.allocateNative(SystemABI.C_INT);
Index_h.clang_tokenize(tu, range.range, p.baseAddress(), pCnt.baseAddress());
Tokens rv = new Tokens(Utils.getPointer(p.baseAddress()), Utils.getInt(pCnt.baseAddress()));
return rv;
Expand Down
Loading

0 comments on commit aeec1cb

Please sign in to comment.