/
RuntimeHelper.java.template
207 lines (180 loc) · 7.98 KB
/
RuntimeHelper.java.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Generated by jextract
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 java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Optional;
import static ${C_LANG}.*;
public class RuntimeHelper {
private final static SystemABI ABI = SystemABI.getSystemABI();
private final static ClassLoader LOADER = RuntimeHelper.class.getClassLoader();
private final static MethodHandles.Lookup MH_LOOKUP = MethodHandles.lookup();
static final LibraryLookup[] libraries(String... libNames) {
if (libNames.length == 0) {
return new LibraryLookup[] { LibraryLookup.ofDefault() };
} else {
return Arrays.stream(libNames)
.map(libName -> LibraryLookup.ofLibrary(libName))
.toArray(LibraryLookup[]::new);
}
}
private static final Optional<MemoryAddress> lookup(LibraryLookup[] LIBRARIES, String sym) {
for (LibraryLookup l : LIBRARIES) {
try {
return Optional.of(l.lookup(sym));
} catch (Throwable t) {
}
}
try {
return Optional.of(LibraryLookup.ofDefault().lookup(sym));
} catch (Throwable t) {
return Optional.empty();
}
}
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) {
return lookup(LIBRARIES, name).map(
addr -> {
MethodType mt = MethodType.fromMethodDescriptorString(desc, LOADER);
return variadic ?
VarargsInvoker.make(addr, mt, fdesc) :
ABI.downcallHandle(addr, mt, fdesc);
}).orElse(null);
}
public static final MemorySegment upcallStub(MethodHandle handle, FunctionDescriptor fdesc) {
return ABI.upcallStub(handle, fdesc);
}
public static final <Z> MemorySegment upcallStub(Class<Z> fi, Z z, FunctionDescriptor fdesc, String mtypeDesc) {
try {
MethodHandle handle = MH_LOOKUP.findVirtual(fi, "apply",
MethodType.fromMethodDescriptorString(mtypeDesc, LOADER));
handle = handle.bindTo(z);
return upcallStub(handle, fdesc);
} catch (Throwable ex) {
throw new AssertionError(ex);
}
}
private static class VarargsInvoker {
private static final MethodHandle INVOKE_MH;
private final MemoryAddress symbol;
private final MethodType varargs;
private final FunctionDescriptor function;
private VarargsInvoker(MemoryAddress symbol, MethodType type, FunctionDescriptor function) {
this.symbol = symbol;
this.varargs = type;
this.function = function;
}
static {
try {
INVOKE_MH = MethodHandles.lookup().findVirtual(VarargsInvoker.class, "invoke", MethodType.methodType(Object.class, Object[].class));
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
static MethodHandle make(MemoryAddress symbol, MethodType type, FunctionDescriptor function) {
VarargsInvoker invoker = new VarargsInvoker(symbol, type, function);
return INVOKE_MH.bindTo(invoker).asCollector(Object[].class, type.parameterCount())
.asType(type);
}
private Object invoke(Object[] args) throws Throwable {
// one trailing Object[]
int nNamedArgs = function.argumentLayouts().size();
assert(args.length == nNamedArgs + 1);
// The last argument is the array of vararg collector
Object[] unnamedArgs = (Object[]) args[args.length - 1];
int argsCount = nNamedArgs + unnamedArgs.length;
Class<?>[] argTypes = new Class<?>[argsCount];
MemoryLayout[] argLayouts = new MemoryLayout[nNamedArgs + unnamedArgs.length];
int pos = 0;
for (pos = 0; pos < nNamedArgs; pos++) {
argTypes[pos] = varargs.parameterType(pos);
argLayouts[pos] = function.argumentLayouts().get(pos);
}
assert pos == nNamedArgs;
for (Object o: unnamedArgs) {
argTypes[pos] = normalize(o.getClass());
argLayouts[pos] = variadicLayout(argTypes[pos]);
pos++;
}
assert pos == argsCount;
MethodType mt = MethodType.methodType(varargs.returnType(), argTypes);
FunctionDescriptor f = (function.returnLayout().isEmpty()) ?
FunctionDescriptor.ofVoid(argLayouts) :
FunctionDescriptor.of(function.returnLayout().get(), argLayouts);
MethodHandle mh = ABI.downcallHandle(symbol, mt, f);
// flatten argument list so that it can be passed to an asSpreader MH
Object[] allArgs = new Object[nNamedArgs + unnamedArgs.length];
System.arraycopy(args, 0, allArgs, 0, nNamedArgs);
System.arraycopy(unnamedArgs, 0, allArgs, nNamedArgs, unnamedArgs.length);
return mh.asSpreader(Object[].class, argsCount).invoke(allArgs);
}
private static Class<?> unboxIfNeeded(Class<?> clazz) {
if (clazz == Boolean.class) {
return boolean.class;
} else if (clazz == Void.class) {
return void.class;
} else if (clazz == Byte.class) {
return byte.class;
} else if (clazz == Character.class) {
return char.class;
} else if (clazz == Short.class) {
return short.class;
} else if (clazz == Integer.class) {
return int.class;
} else if (clazz == Long.class) {
return long.class;
} else if (clazz == Float.class) {
return float.class;
} else if (clazz == Double.class) {
return double.class;
} else {
return clazz;
}
}
private Class<?> promote(Class<?> c) {
if (c == byte.class || c == char.class || c == short.class || c == int.class) {
return long.class;
} else if (c == float.class) {
return double.class;
} else {
return c;
}
}
private Class<?> normalize(Class<?> c) {
c = unboxIfNeeded(c);
if (c.isPrimitive()) {
return promote(c);
}
if (MemoryAddress.class.isAssignableFrom(c)) {
return MemoryAddress.class;
}
if (MemorySegment.class.isAssignableFrom(c)) {
return MemorySegment.class;
}
throw new IllegalArgumentException("Invalid type for ABI: " + c.getTypeName());
}
private MemoryLayout variadicLayout(Class<?> c) {
if (c == long.class) {
return C_LONGLONG;
} else if (c == double.class) {
return C_DOUBLE;
} else if (MemoryAddress.class.isAssignableFrom(c)) {
return C_POINTER;
} else {
throw new IllegalArgumentException("Unhandled variadic argument class: " + c);
}
}
}
}