Skip to content
Permalink
Browse files

JDK-8244270: reorganize ABI-dependent layout constants (second attempt)

Reviewed-by: jvernee
  • Loading branch information
mcimadamore authored and JornVernee committed May 6, 2020
1 parent 4303213 commit aeec1cba55de61044a0ad631015c7430d4126922
Showing with 1,587 additions and 1,704 deletions.
  1. +34 −29 src/jdk.incubator.jextract/share/classes/jdk/incubator/jextract/Type.java
  2. +10 −15 src/jdk.incubator.jextract/share/classes/jdk/incubator/jextract/tool/OutputFactory.java
  3. +14 −22 src/jdk.incubator.jextract/share/classes/jdk/incubator/jextract/tool/TypeTranslator.java
  4. +1 −1 src/jdk.incubator.jextract/share/classes/jdk/incubator/jextract/tool/resources/Cstring.java.template
  5. +3 −4 src/jdk.incubator.jextract/share/classes/jdk/internal/clang/Index.java
  6. +1 −1 src/jdk.incubator.jextract/share/classes/jdk/internal/clang/LibClang.java
  7. +5 −4 src/jdk.incubator.jextract/share/classes/jdk/internal/clang/SourceLocation.java
  8. +3 −2 src/jdk.incubator.jextract/share/classes/jdk/internal/clang/TranslationUnit.java
  9. +9 −8 src/jdk.incubator.jextract/share/classes/jdk/internal/clang/Utils.java
  10. +1,402 −1,400 src/jdk.incubator.jextract/share/classes/jdk/internal/clang/libclang/Index_h.java
  11. +25 −126 src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/LayoutUtils.java
  12. +1 −1 src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/MacroParserImpl.java
  13. +1 −1 src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/PrettyPrinter.java
  14. +5 −1 src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/StructLayoutComputer.java
  15. +0 −16 src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/TypeImpl.java
  16. +15 −15 src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/TypeMaker.java
  17. +1 −2 test/jdk/java/jextract/JextractApiTestBase.java
  18. +1 −3 test/jdk/java/jextract/Test8240372.java
  19. +1 −1 test/jdk/java/jextract/TestAttributes.java
  20. +2 −1 test/jdk/java/jextract/TestMacros.java
  21. +0 −1 test/jdk/tools/jextract/ConstantsTest.java
  22. +7 −6 test/jdk/tools/jextract/JextractToolRunner.java
  23. +8 −7 test/jdk/tools/jextract/RepeatedDeclsTest.java
  24. +11 −10 test/jdk/tools/jextract/Test8240811.java
  25. +4 −3 test/jdk/tools/jextract/UniondeclTest.java
  26. +6 −6 test/jdk/tools/jextract/testClassGen/TestClassGeneration.java
  27. +17 −18 test/jdk/tools/jextract/testStruct/LibStructTest.java
@@ -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;

@@ -90,89 +91,94 @@
/**
* {@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();
}

/**
@@ -398,13 +404,12 @@ public String typeName() {
}

/**
* 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);
}

/**
@@ -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;
@@ -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;

@@ -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);
}
@@ -182,14 +181,10 @@ private JavaFileObject getCstringFile(String pkgName) throws IOException, URISyn

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).
@@ -201,9 +196,9 @@ private JavaFileObject getCstringFile(String pkgName) throws IOException, URISyn
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);
@@ -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;
@@ -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));
}
}

@@ -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);
};
}

@@ -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!
@@ -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;
@@ -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;
@@ -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(),
@@ -55,7 +55,7 @@
"_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);
@@ -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;

@@ -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());
@@ -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;
@@ -119,8 +120,8 @@ public void reparse(Consumer<Diagnostic> dh, Index.UnsavedFile... inMemoryFiles)
}

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;

0 comments on commit aeec1cb

Please sign in to comment.