|
59 | 59 | import com.oracle.graal.python.builtins.objects.PNone;
|
60 | 60 | import com.oracle.graal.python.builtins.objects.bytes.BytesCommonBuiltins;
|
61 | 61 | import com.oracle.graal.python.builtins.objects.cext.PythonNativeVoidPtr;
|
| 62 | +import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext; |
62 | 63 | import com.oracle.graal.python.builtins.objects.cext.capi.CApiGuards;
|
63 | 64 | import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.FromCharPointerNode;
|
| 65 | +import com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbol; |
64 | 66 | import com.oracle.graal.python.builtins.objects.cext.capi.PrimitiveNativeWrapper;
|
65 | 67 | import com.oracle.graal.python.builtins.objects.cext.common.CArrayWrappers.CByteArrayWrapper;
|
66 | 68 | import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodesFactory.GetIndexNodeGen;
|
|
93 | 95 | import com.oracle.graal.python.runtime.object.PythonObjectFactory;
|
94 | 96 | import com.oracle.graal.python.util.OverflowException;
|
95 | 97 | import com.oracle.graal.python.util.PythonUtils;
|
96 |
| -import com.oracle.truffle.api.CompilerAsserts; |
97 | 98 | import com.oracle.truffle.api.CompilerDirectives;
|
98 | 99 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
99 | 100 | import com.oracle.truffle.api.HostCompilerDirectives.InliningCutoff;
|
|
108 | 109 | import com.oracle.truffle.api.dsl.Specialization;
|
109 | 110 | import com.oracle.truffle.api.interop.InteropException;
|
110 | 111 | import com.oracle.truffle.api.interop.InteropLibrary;
|
111 |
| -import com.oracle.truffle.api.interop.UnknownIdentifierException; |
112 | 112 | import com.oracle.truffle.api.interop.UnsupportedMessageException;
|
113 | 113 | import com.oracle.truffle.api.library.CachedLibrary;
|
114 | 114 | import com.oracle.truffle.api.nodes.Node;
|
115 | 115 | import com.oracle.truffle.api.nodes.UnexpectedResultException;
|
116 |
| -import com.oracle.truffle.api.object.DynamicObject; |
117 |
| -import com.oracle.truffle.api.object.DynamicObjectLibrary; |
118 | 116 | import com.oracle.truffle.api.profiles.InlinedBranchProfile;
|
119 | 117 | import com.oracle.truffle.api.profiles.InlinedConditionProfile;
|
120 | 118 | import com.oracle.truffle.api.strings.TruffleString;
|
@@ -155,55 +153,22 @@ public static void fatalError(Node location, PythonContext context, String prefi
|
155 | 153 | @GenerateCached(false)
|
156 | 154 | public abstract static class ImportCExtSymbolNode extends PNodeWithContext {
|
157 | 155 |
|
158 |
| - public abstract Object execute(Node inliningTarget, CExtContext nativeContext, NativeCExtSymbol symbol); |
159 |
| - |
160 |
| - @Specialization(guards = {"isSingleContext()", "cachedSymbol == symbol"}, limit = "1") |
161 |
| - static Object doSymbolCached(@SuppressWarnings("unused") Node inliningTarget, @SuppressWarnings("unused") CExtContext nativeContext, @SuppressWarnings("unused") NativeCExtSymbol symbol, |
162 |
| - @Cached("symbol") @SuppressWarnings("unused") NativeCExtSymbol cachedSymbol, |
163 |
| - @Cached("importCAPISymbolUncached(inliningTarget, nativeContext, symbol)") Object llvmSymbol) { |
164 |
| - return llvmSymbol; |
| 156 | + public final Object execute(Node inliningTarget, NativeCAPISymbol symbol) { |
| 157 | + return execute(inliningTarget, PythonContext.get(inliningTarget).getCApiContext(), symbol); |
165 | 158 | }
|
166 | 159 |
|
167 |
| - // n.b. if 'singleContextAssumption' is valid, we may also cache the native context |
168 |
| - @Specialization(guards = {"isSingleContext()", "nativeContext == cachedNativeContext"}, limit = "1", // |
169 |
| - replaces = "doSymbolCached") |
170 |
| - static Object doWithSymbolCacheSingleContext(Node inliningTarget, @SuppressWarnings("unused") CExtContext nativeContext, NativeCExtSymbol symbol, |
171 |
| - @Cached("nativeContext") CExtContext cachedNativeContext, |
172 |
| - @Cached("nativeContext.getSymbolCache()") DynamicObject cachedSymbolCache, |
173 |
| - @CachedLibrary("cachedSymbolCache") DynamicObjectLibrary dynamicObjectLib) { |
174 |
| - return doWithSymbolCache(inliningTarget, cachedNativeContext, symbol, cachedSymbolCache, dynamicObjectLib); |
175 |
| - } |
176 |
| - |
177 |
| - @Specialization(replaces = {"doSymbolCached", "doWithSymbolCacheSingleContext"}, limit = "1") |
178 |
| - static Object doWithSymbolCache(Node inliningTarget, CExtContext nativeContext, NativeCExtSymbol symbol, |
179 |
| - @Bind("nativeContext.getSymbolCache()") DynamicObject symbolCache, |
180 |
| - @CachedLibrary("symbolCache") DynamicObjectLibrary dynamicObjectLib) { |
181 |
| - Object nativeSymbol = dynamicObjectLib.getOrDefault(symbolCache, symbol, PNone.NO_VALUE); |
182 |
| - if (nativeSymbol == PNone.NO_VALUE) { |
183 |
| - nativeSymbol = importCAPISymbolUncached(inliningTarget, nativeContext, symbol, symbolCache, dynamicObjectLib); |
184 |
| - } |
185 |
| - return nativeSymbol; |
186 |
| - } |
| 160 | + public abstract Object execute(Node inliningTarget, CApiContext nativeContext, NativeCExtSymbol symbol); |
187 | 161 |
|
188 |
| - protected static Object importCAPISymbolUncached(Node location, CExtContext nativeContext, NativeCExtSymbol symbol) { |
189 |
| - CompilerAsserts.neverPartOfCompilation(); |
190 |
| - return importCAPISymbolUncached(location, nativeContext, symbol, nativeContext.getSymbolCache(), DynamicObjectLibrary.getUncached()); |
| 162 | + @Specialization(guards = {"isSingleContext()", "cachedSymbol == symbol"}, limit = "1") |
| 163 | + static Object doCached(@SuppressWarnings("unused") Node inliningTarget, @SuppressWarnings("unused") CApiContext nativeContext, @SuppressWarnings("unused") NativeCAPISymbol symbol, |
| 164 | + @Cached("symbol") @SuppressWarnings("unused") NativeCAPISymbol cachedSymbol, |
| 165 | + @Cached(value = "nativeContext.getNativeSymbol(symbol)", weak = true) Object llvmSymbol) { |
| 166 | + return llvmSymbol; |
191 | 167 | }
|
192 | 168 |
|
193 |
| - @TruffleBoundary |
194 |
| - protected static Object importCAPISymbolUncached(Node location, CExtContext nativeContext, NativeCExtSymbol symbol, DynamicObject symbolCache, DynamicObjectLibrary dynamicObjectLib) { |
195 |
| - Object llvmLibrary = nativeContext.getLLVMLibrary(); |
196 |
| - String name = symbol.getName(); |
197 |
| - try { |
198 |
| - Object nativeSymbol = InteropLibrary.getUncached().readMember(llvmLibrary, name); |
199 |
| - nativeSymbol = CExtContext.ensureExecutable(nativeSymbol, symbol); |
200 |
| - dynamicObjectLib.put(symbolCache, symbol, nativeSymbol); |
201 |
| - return nativeSymbol; |
202 |
| - } catch (UnknownIdentifierException e) { |
203 |
| - throw PRaiseNode.raiseUncached(location, PythonBuiltinClassType.SystemError, ErrorMessages.INVALID_CAPI_FUNC, symbol.getTsName()); |
204 |
| - } catch (UnsupportedMessageException e) { |
205 |
| - throw PRaiseNode.raiseUncached(location, PythonBuiltinClassType.SystemError, ErrorMessages.CORRUPTED_CAPI_LIB_OBJ, llvmLibrary); |
206 |
| - } |
| 169 | + @Specialization(replaces = "doCached") |
| 170 | + static Object doGeneric(CApiContext nativeContext, NativeCAPISymbol symbol) { |
| 171 | + return nativeContext.getNativeSymbol(symbol); |
207 | 172 | }
|
208 | 173 | }
|
209 | 174 |
|
|
0 commit comments