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

7903190: jextract runtime helper issues #33

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -127,6 +127,8 @@ The `jextract` tool includes several customization options. Users can select in
| `--source` | generate java sources instead of classfiles |
| `--dump-includes <String>` | dump included symbols into specified file (see below) |
| `--include-[function,macro,struct,union,typedef,var]<String>` | Include a symbol of the given name and kind in the generated bindings (see below). When one of these options is specified, any symbol that is not matched by any specified filters is omitted from the generated bindings. |
| '--version` | print version information and exit |


#### Additional clang options

23 changes: 19 additions & 4 deletions src/main/java/org/openjdk/jextract/impl/ConstantBuilder.java
Original file line number Diff line number Diff line change
@@ -140,6 +140,18 @@ String fieldName(String javaName) {
this.kind = kind;
}

String className() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who uses these?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just added all getters for uniformity..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only kind and fieldName are used to get the name of the $MH getter name (in wrapper)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok just wanted to make sure I wasn't missing anything

return className;
}

String javaName() {
return javaName;
}

Kind kind() {
return kind;
}

List<String> getterNameParts() {
return List.of(className, javaName, kind.nameSuffix);
}
@@ -190,7 +202,13 @@ private Constant emitMethodHandleField(String javaName, String nativeName, Funct
String fieldName = Constant.Kind.METHOD_HANDLE.fieldName(javaName);
indent();
append(memberMods() + "MethodHandle ");
append(fieldName + " = RuntimeHelper.downcallHandle(\n");
append(fieldName + " = RuntimeHelper.");
if (isVarargs) {
append("downcallHandleVariadic");
} else {
append("downcallHandle");
}
append("(\n");
incrAlign();
indent();
if (!virtual) {
@@ -199,9 +217,6 @@ private Constant emitMethodHandleField(String javaName, String nativeName, Funct
indent();
}
append(functionDesc.accessExpression());
append(", ");
// isVariadic
append(isVarargs);
append("\n");
decrAlign();
indent();
Original file line number Diff line number Diff line change
@@ -95,8 +95,8 @@ private void emitFunctionalFactories() {
append(MEMBER_MODS + " NativeSymbol allocate(" + className() + " fi, ResourceScope scope) {\n");
incrAlign();
indent();
append("return RuntimeHelper.upcallStub(" + className() + ".class, fi, " + functionDesc.accessExpression() + ", " +
"\"" + fiType.toMethodDescriptorString() + "\", scope);\n");
append("return RuntimeHelper.upcallStub(" + className() + ".class, fi, " +
functionDesc.accessExpression() + ", scope);\n");
decrAlign();
indent();
append("}\n");
Original file line number Diff line number Diff line change
@@ -130,11 +130,9 @@ private void emitFunctionWrapper(Constant mhConstant, String javaName, String na
append(" {\n");
incrAlign();
indent();
append("var mh$ = RuntimeHelper.requireNonNull(");
append(mhConstant.accessExpression());
append(", \"");
append(nativeName);
append("\");\n");
append("var mh$ = ");
append(mhConstant.kind().fieldName(javaName));
append("();\n");
indent();
append("try {\n");
incrAlign();
Original file line number Diff line number Diff line change
@@ -55,26 +55,25 @@ final class RuntimeHelper {
return SYMBOL_LOOKUP.lookup(name).map(symbol -> MemorySegment.ofAddress(symbol.address(), layout.byteSize(), ResourceScope.newSharedScope())).orElse(null);
}

static final MethodHandle downcallHandle(String name, FunctionDescriptor fdesc, boolean variadic) {
return SYMBOL_LOOKUP.lookup(name).map(
addr -> {
return variadic ?
VarargsInvoker.make(addr, fdesc) :
LINKER.downcallHandle(addr, fdesc);
}).orElse(null);
static final MethodHandle downcallHandle(String name, FunctionDescriptor fdesc) {
return SYMBOL_LOOKUP.lookup(name).
map(addr -> LINKER.downcallHandle(addr, fdesc)).
orElse(null);
}

static final MethodHandle downcallHandle(FunctionDescriptor fdesc, boolean variadic) {
if (variadic) {
throw new AssertionError("Cannot get here!");
}
static final MethodHandle downcallHandle(FunctionDescriptor fdesc) {
return LINKER.downcallHandle(fdesc);
}

static final <Z> NativeSymbol upcallStub(Class<Z> fi, Z z, FunctionDescriptor fdesc, String mtypeDesc, ResourceScope scope) {
static final MethodHandle downcallHandleVariadic(String name, FunctionDescriptor fdesc) {
return SYMBOL_LOOKUP.lookup(name).
map(addr -> VarargsInvoker.make(addr, fdesc)).
orElse(null);
}

static final <Z> NativeSymbol upcallStub(Class<Z> fi, Z z, FunctionDescriptor fdesc, ResourceScope scope) {
try {
MethodHandle handle = MH_LOOKUP.findVirtual(fi, "apply",
MethodType.fromMethodDescriptorString(mtypeDesc, LOADER));
MethodHandle handle = MH_LOOKUP.findVirtual(fi, "apply", CLinker.upcallType(fdesc));
handle = handle.bindTo(z);
return LINKER.upcallStub(handle, fdesc, scope);
} catch (Throwable ex) {