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

8245003: jextract does not generate accessor for MemorySegement typed values #165

Closed
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -146,7 +146,8 @@
methodType(
MemoryAddress.class,
LibraryLookup[].class,
String.class)
String.class,
MemoryLayout.class)
);
this.MH_makeCString = findRuntimeHelperBootstrap(
cString,
@@ -188,8 +189,8 @@ public DirectMethodHandleDesc addMethodHandle(String javaName, String nativeName
return emitCondyGetter(javaName + "$MH", MethodHandle.class, methodHandleDesc(nativeName, mtype, desc, varargs));
}

public DirectMethodHandleDesc addAddress(String javaName, String nativeName) {
return emitCondyGetter(javaName + "$ADDR", MemoryAddress.class, globalVarAddressDesc(nativeName));
public DirectMethodHandleDesc addAddress(String javaName, String nativeName, MemoryLayout layout) {
return emitCondyGetter(javaName + "$ADDR", MemoryAddress.class, globalVarAddressDesc(nativeName, layout));
}

public DirectMethodHandleDesc addFunctionDesc(String javaName, FunctionDescriptor fDesc) {
@@ -419,8 +420,8 @@ private static ConstantDesc varHandleDesc(String javaName, String nativeName, Me
}
}

private ConstantDesc globalVarAddressDesc(String name) {
return DynamicConstantDesc.ofNamed(BSM_INVOKE, "ADDR_" + name, CD_MemoryAddress, MH_lookupGlobalVariable, LIBRARIES, name);
private ConstantDesc globalVarAddressDesc(String name, MemoryLayout layout) {
return DynamicConstantDesc.ofNamed(BSM_INVOKE, "ADDR_" + name, CD_MemoryAddress, MH_lookupGlobalVariable, LIBRARIES, name, desc(layout));
}

private ConstantDesc addressDesc(long value) {
@@ -25,16 +25,12 @@
package jdk.incubator.jextract.tool;

import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryLayout;
import jdk.incubator.foreign.MemorySegment;

import javax.tools.JavaFileObject;
import java.lang.constant.ClassDesc;
import java.lang.constant.DirectMethodHandleDesc;
import java.lang.invoke.MethodType;
import java.util.ArrayList;
import java.util.List;

/**
* Superclass for .java source generator classes.
@@ -119,8 +115,8 @@ public void addMethodHandleGetter(String javaName, String nativeName, MethodType
emitForwardGetter(constantHelper.addMethodHandle(javaName, nativeName, mtype, desc, varargs));
}

public void addAddressGetter(String javaName, String nativeName) {
emitForwardGetter(constantHelper.addAddress(javaName, nativeName));
public void addAddressGetter(String javaName, String nativeName, MemoryLayout layout) {
emitForwardGetter(constantHelper.addAddress(javaName, nativeName, layout));
}

public void addConstantGetter(String javaName, Class<?> type, Object value) {
@@ -130,14 +126,12 @@ public void addConstantGetter(String javaName, Class<?> type, Object value) {
public void addGetter(String javaName, String nativeName, MemoryLayout layout, Class<?> type, MemoryLayout parentLayout) {
incrAlign();
indent();
String param = parentLayout != null ? (MemoryAddress.class.getName() + " addr") : "";
sb.append(PUB_MODS + type.getName() + " " + javaName + "$get(" + param + ") {\n");
sb.append(PUB_MODS + type.getName() + " " + javaName + "$get() {\n");
incrAlign();
indent();
String vhParam = parentLayout != null ?
"addr" : addressGetCallString(javaName, nativeName);
String vhParam = addressGetCallString(javaName, nativeName, layout);
sb.append("return (" + type.getName() + ")"
+ varHandleGetCallString(javaName, nativeName, layout, type, parentLayout) + ".get(" + vhParam + ");\n");
+ varHandleGetCallString(javaName, nativeName, layout, type, null) + ".get(" + vhParam + ");\n");
decrAlign();
indent();
sb.append("}\n");
@@ -147,13 +141,25 @@ public void addGetter(String javaName, String nativeName, MemoryLayout layout, C
public void addSetter(String javaName, String nativeName, MemoryLayout layout, Class<?> type, MemoryLayout parentLayout) {
incrAlign();
indent();
String param = parentLayout != null ? (MemoryAddress.class.getName() + " addr, ") : "";
sb.append(PUB_MODS + "void " + javaName + "$set(" + param + type.getName() + " x) {\n");
sb.append(PUB_MODS + "void " + javaName + "$set(" + type.getName() + " x) {\n");
incrAlign();
indent();
String vhParam = parentLayout != null ?
"addr" : addressGetCallString(javaName, nativeName);
sb.append(varHandleGetCallString(javaName, nativeName, layout, type, parentLayout) + ".set(" + vhParam + ", x);\n");
String vhParam = addressGetCallString(javaName, nativeName, layout);
sb.append(varHandleGetCallString(javaName, nativeName, layout, type, null) + ".set(" + vhParam + ", x);\n");
decrAlign();
indent();
sb.append("}\n");
decrAlign();
}

public void addAddressOf(String javaName, String nativeName, MemoryLayout layout, Class<?> type, MemoryLayout parentLayout) {
DirectMethodHandleDesc desc = constantHelper.addAddress(javaName, nativeName, layout);
incrAlign();
indent();
sb.append(PUB_MODS + "MemoryAddress " + javaName + "$addressof() {\n");
incrAlign();
indent();
sb.append("return " + getCallString(desc) + ";\n");
decrAlign();
indent();
sb.append("}\n");
@@ -215,8 +221,8 @@ protected String varHandleGetCallString(String javaName, String nativeName, Memo
return getCallString(constantHelper.addVarHandle(javaName, nativeName, layout, type, parentLayout));
}

protected String addressGetCallString(String javaName, String nativeName) {
return getCallString(constantHelper.addAddress(javaName, nativeName));
protected String addressGetCallString(String javaName, String nativeName, MemoryLayout layout) {
return getCallString(constantHelper.addAddress(javaName, nativeName, layout));
}

protected void indent() {
@@ -24,13 +24,9 @@
*/
package jdk.incubator.jextract.tool;

import jdk.incubator.foreign.*;
import jdk.incubator.jextract.Declaration;
import jdk.incubator.jextract.Type;
import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.MemoryAddress;
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;

@@ -353,11 +349,6 @@ public Void visitVariable(Declaration.Variable tree, Declaration parent) {
String symbol = tree.name();
assert !symbol.isEmpty();
assert !fieldName.isEmpty();

// FIXME: we need tree transformer. The mangling should be a separate tree transform phase
if (parent == null) {
fieldName = tree.name();
}
fieldName = Utils.javaSafeIdentifier(fieldName);

Type type = tree.type();
@@ -368,23 +359,32 @@ public Void visitVariable(Declaration.Variable tree, Declaration parent) {
}
Class<?> clazz = typeTranslator.getJavaType(type);
if (tree.kind() == Declaration.Variable.Kind.BITFIELD || clazz == MemoryAddress.class ||
clazz == MemorySegment.class || layout.byteSize() > 8) {
(layout instanceof ValueLayout && layout.byteSize() > 8)) {
//skip
return null;
}

boolean isSegment = clazz == MemorySegment.class;
MemoryLayout treeLayout = tree.layout().orElseThrow();
if (parent != null) { //struct field
MemoryLayout parentLayout = parentLayout(parent);
structBuilder.addVarHandleGetter(fieldName, tree.name(), treeLayout, clazz, parentLayout);
structBuilder.addGetter(fieldName, tree.name(), treeLayout, clazz, parentLayout);
structBuilder.addSetter(fieldName, tree.name(), treeLayout, clazz, parentLayout);
if (isSegment) {
structBuilder.addAddressOf(fieldName, tree.name(), treeLayout, clazz, parentLayout);
} else {
structBuilder.addVarHandleGetter(fieldName, tree.name(), treeLayout, clazz, parentLayout);
structBuilder.addGetter(fieldName, tree.name(), treeLayout, clazz, parentLayout);
structBuilder.addSetter(fieldName, tree.name(), treeLayout, clazz, parentLayout);
}
} else {
builder.addLayoutGetter(fieldName, layout);
builder.addVarHandleGetter(fieldName, tree.name(), treeLayout, clazz, null);
builder.addAddressGetter(fieldName, tree.name());
builder.addGetter(fieldName, tree.name(), treeLayout, clazz, null);
builder.addSetter(fieldName, tree.name(), treeLayout, clazz, null);
if (isSegment) {
builder.addAddressOf(fieldName, tree.name(), treeLayout, clazz, null);
} else {
builder.addLayoutGetter(fieldName, layout);
builder.addVarHandleGetter(fieldName, tree.name(), treeLayout, clazz,null);
builder.addAddressGetter(fieldName, tree.name(), treeLayout);
builder.addGetter(fieldName, tree.name(), treeLayout, clazz, null);
builder.addSetter(fieldName, tree.name(), treeLayout, clazz, null);
}
}

return null;
@@ -79,16 +79,56 @@ public void addLayoutGetter(String javaName, MemoryLayout layout) {

@Override
public void addGetter(String javaName, String nativeName, MemoryLayout layout, Class<?> type, MemoryLayout parentLayout) {
super.addGetter(javaName, nativeName, layout, type, parentLayout);
incrAlign();
indent();
sb.append(PUB_MODS + type.getName() + " " + javaName + "$get(MemoryAddress addr) {\n");
incrAlign();
indent();
sb.append("return (" + type.getName() + ")"
+ varHandleGetCallString(javaName, nativeName, layout, type, parentLayout) + ".get(addr);\n");
decrAlign();
indent();
sb.append("}\n");
decrAlign();

addIndexGetter(javaName, nativeName, layout, type, parentLayout);
}

@Override
public void addSetter(String javaName, String nativeName, MemoryLayout layout, Class<?> type, MemoryLayout parentLayout) {
super.addSetter(javaName, nativeName, layout, type, parentLayout);
incrAlign();
indent();
String param = MemoryAddress.class.getName() + " addr";
sb.append(PUB_MODS + "void " + javaName + "$set(" + param + ", " + type.getName() + " x) {\n");
incrAlign();
indent();
sb.append(varHandleGetCallString(javaName, nativeName, layout, type, null) + ".set(addr, x);\n");
decrAlign();
indent();
sb.append("}\n");
decrAlign();

addIndexSetter(javaName, nativeName, layout, type, parentLayout);
}

@Override
public void addAddressOf(String javaName, String nativeName, MemoryLayout layout, Class<?> type, MemoryLayout parentLayout) {
incrAlign();
indent();
sb.append(PUB_MODS + "MemoryAddress " + javaName + "$addressof(MemoryAddress addr) {\n");
incrAlign();
indent();
sb.append("return addr.segment().asSlice(");
sb.append(parentLayout.byteOffset(MemoryLayout.PathElement.groupElement(nativeName)));
sb.append(", ");
sb.append(layout.byteSize());
sb.append(").baseAddress();\n");
decrAlign();
indent();
sb.append("}\n");
decrAlign();
}

private void emitSizeof() {
incrAlign();
indent();
@@ -48,8 +48,11 @@ public class RuntimeHelper {
}
}

public static final MemoryAddress lookupGlobalVariable(LibraryLookup[] LIBRARIES, String name) {
return lookup(LIBRARIES, name).orElse(null);
public static final MemoryAddress lookupGlobalVariable(LibraryLookup[] LIBRARIES, String name, MemoryLayout layout) {
return lookup(LIBRARIES, name).map(a ->
MemorySegment.ofNativeRestricted(
a, layout.byteSize(), null, null, a
).withAccessModes(MemorySegment.READ | MemorySegment.WRITE).baseAddress()).orElse(null);
}

public static final MethodHandle downcallHandle(LibraryLookup[] LIBRARIES, String name, String desc, FunctionDescriptor fdesc, boolean variadic) {
@@ -30,6 +30,8 @@
import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.LibraryLookup;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryLayout;
import jdk.incubator.foreign.MemorySegment;
import jdk.incubator.foreign.SystemABI;
import jdk.internal.foreign.abi.SharedUtils;

@@ -84,8 +86,11 @@
}
}

public static final MemoryAddress lookupGlobalVariable(LibraryLookup[] LIBRARIES, String name) {
return lookup(LIBRARIES, name).orElse(null);
public static final MemoryAddress lookupGlobalVariable(LibraryLookup[] LIBRARIES, String name, MemoryLayout layout) {
return lookup(LIBRARIES, name).map(a ->
MemorySegment.ofNativeRestricted(
a, layout.byteSize(), null, null, a
).withAccessModes(MemorySegment.READ | MemorySegment.WRITE).baseAddress()).orElse(null);
}

public static final MethodHandle downcallHandle(LibraryLookup[] LIBRARIES, String name, String desc, FunctionDescriptor fdesc) {
@@ -26,6 +26,7 @@

package jdk.internal.jextract.impl;

import jdk.incubator.foreign.GroupLayout;
import jdk.incubator.foreign.MemoryLayout;
import jdk.incubator.foreign.ValueLayout;
import jdk.internal.clang.Cursor;
@@ -103,10 +104,12 @@ void addFieldLayout(MemoryLayout MemoryLayout) {
}

void addFieldLayout(long offset, Type parent, Cursor c) {
MemoryLayout MemoryLayout = c.isAnonymousStruct()?
compute(offset, parent, c.type()) :
fieldLayout(c);
addFieldLayout(MemoryLayout);
if (c.isAnonymousStruct()) {
GroupLayout layout = (GroupLayout) compute(offset, parent, c.type());
fieldLayouts.addAll(layout.memberLayouts());
} else {
addFieldLayout(fieldLayout(c));
}
}

MemoryLayout fieldLayout(Cursor c) {
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import org.testng.annotations.Test;
import test.jextract.test8245003.*;
import static org.testng.Assert.assertEquals;
import static test.jextract.test8245003.test8245003_h.*;

/*
* @test
* @bug 8245003
* @summary jextract does not generate accessor for MemorySegement typed values
* @library ..
* @modules jdk.incubator.jextract
* @run driver JtregJextract -l Test8245003 -t test.jextract.test8245003 -- test8245003.h
* @run testng/othervm -Dforeign.restricted=permit Test8245003
*/
public class Test8245003 {
@Test
public void testStructAccessor() {
var addr = special_pt$addressof();
assertEquals(addr.segment().byteSize(), CPoint.sizeof());
assertEquals(CPoint.x$get(addr), 56);
assertEquals(CPoint.y$get(addr), 75);

addr = special_pt3d$addressof();
assertEquals(addr.segment().byteSize(), CPoint3D.sizeof());
assertEquals(CPoint3D.z$get(addr), 35);
var pointAddr = CPoint3D.p$addressof(addr);
assertEquals(pointAddr.segment().byteSize(), CPoint.sizeof());
assertEquals(CPoint.x$get(pointAddr), 43);
assertEquals(CPoint.y$get(pointAddr), 45);
}

@Test
public void testArrayAccessor() {
var addr = iarr$addressof();
assertEquals(addr.segment().byteSize(), Cint.sizeof()*5);
int[] arr = Cint.toJavaArray(addr.segment());
assertEquals(arr.length, 5);
assertEquals(arr[0], 2);
assertEquals(arr[1], -2);
assertEquals(arr[2], 42);
assertEquals(arr[3], -42);
assertEquals(arr[4], 345);

addr = foo$addressof();
assertEquals(addr.segment().byteSize(), CFoo.sizeof());
assertEquals(CFoo.count$get(addr), 37);
var greeting = CFoo.greeting$addressof(addr);
byte[] barr = Cchar.toJavaArray(greeting.segment());
assertEquals(new String(barr), "hello");
}
}
ProTip! Use n and p to navigate between commits in a pull request.