Skip to content

Commit 3c40337

Browse files
committed
Make parse arguments nodes independent of C API.
1 parent e689e10 commit 3c40337

File tree

5 files changed

+127
-110
lines changed

5 files changed

+127
-110
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PythonCextBuiltins.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@
7979
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
8080
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
8181
import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
82-
import com.oracle.graal.python.builtins.objects.cext.CAPIConversionNodeSupplier;
8382
import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CByteArrayWrapper;
8483
import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CStringWrapper;
85-
import com.oracle.graal.python.builtins.objects.cext.CExtModsupportNodes;
84+
import com.oracle.graal.python.builtins.objects.cext.common.CExtParseArgumentsNode;
8685
import com.oracle.graal.python.builtins.objects.cext.CExtNodes;
8786
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.CastToJavaDoubleNode;
8887
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.GetNativeNullNode;
@@ -112,7 +111,10 @@
112111
import com.oracle.graal.python.builtins.objects.cext.PythonNativeWrapper;
113112
import com.oracle.graal.python.builtins.objects.cext.PythonNativeWrapperLibrary;
114113
import com.oracle.graal.python.builtins.objects.cext.UnicodeObjectNodes.UnicodeAsWideCharNode;
115-
import com.oracle.graal.python.builtins.objects.cext.VaListWrapper;
114+
import com.oracle.graal.python.builtins.objects.cext.common.VaListWrapper;
115+
import com.oracle.graal.python.builtins.objects.cext.common.CExtAsPythonObjectNode;
116+
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.PCallCExtFunction;
117+
import com.oracle.graal.python.builtins.objects.cext.common.CExtContext;
116118
import com.oracle.graal.python.builtins.objects.code.PCode;
117119
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
118120
import com.oracle.graal.python.builtins.objects.common.IndexNodes.NormalizeIndexNode;
@@ -2971,25 +2973,22 @@ static boolean isEmptyDict(CExtNodes.AsPythonObjectNode asPythonObjectNode, Hash
29712973
}
29722974
}
29732975

2974-
abstract static class ParseTupleAndKeywordsBaseNode extends PythonVarargsBuiltinNode {
2976+
public abstract static class ParseTupleAndKeywordsBaseNode extends PythonVarargsBuiltinNode {
29752977

29762978
@Override
29772979
public Object varArgExecute(VirtualFrame frame, Object self, Object[] arguments, PKeyword[] keywords) throws VarargsBuiltinDirectInvocationNotSupported {
29782980
return execute(frame, self, arguments, PKeyword.EMPTY_KEYWORDS);
29792981
}
29802982

2981-
static int doConvert(Object cextModule, Object argv, Object nativeKwds, Object nativeFormat, Object nativeKwdnames, Object nativeVarargs,
2983+
public static int doConvert(CExtContext nativeContext, Object nativeNull, Object argv, Object nativeKwds, Object nativeFormat, Object nativeKwdnames, Object nativeVarargs,
29822984
ReferenceLibrary kwdsRefLib,
29832985
ReferenceLibrary kwdnamesRefLib,
29842986
ValueProfile kwdsProfile,
29852987
ConditionProfile kwdnamesProfile,
29862988
ConditionProfile functionNameProfile,
2987-
CExtNodes.AsPythonObjectNode kwdsToJavaNode,
2989+
CExtAsPythonObjectNode kwdsToJavaNode,
29882990
CastToJavaStringNode castToStringNode,
2989-
CExtNodes.ToSulongNode nativeNullToSulongNode,
2990-
GetNativeNullNode getNativeNullNode,
2991-
CExtModsupportNodes.ParseTupleAndKeywordsNode parseTupleAndKeywordsNode) {
2992-
Object nativeNull = nativeNullToSulongNode.execute(getNativeNullNode.execute(cextModule));
2991+
CExtParseArgumentsNode.ParseTupleAndKeywordsNode parseTupleAndKeywordsNode) {
29932992

29942993
// force 'format' to be a String
29952994
String format = castToStringNode.execute(nativeFormat);
@@ -3010,13 +3009,13 @@ static int doConvert(Object cextModule, Object argv, Object nativeKwds, Object n
30103009
if (kwdsRefLib.isSame(nativeKwds, nativeNull)) {
30113010
kwds = null;
30123011
} else {
3013-
kwds = kwdsToJavaNode.execute(nativeKwds);
3012+
kwds = kwdsToJavaNode.execute(nativeContext, nativeKwds);
30143013
}
30153014

30163015
// sort out if kwdnames is native NULL
30173016
Object kwdnames = kwdnamesProfile.profile(kwdnamesRefLib.isSame(nativeKwdnames, nativeNull)) ? null : nativeKwdnames;
30183017

3019-
return parseTupleAndKeywordsNode.execute(functionName, argv, kwdsProfile.profile(kwds), format, kwdnames, nativeVarargs, CAPIConversionNodeSupplier.INSTANCE);
3018+
return parseTupleAndKeywordsNode.execute(functionName, argv, kwdsProfile.profile(kwds), format, kwdnames, nativeVarargs, nativeContext);
30203019
}
30213020

30223021
static Object getKwds(Object[] arguments) {
@@ -3034,6 +3033,7 @@ abstract static class ParseTupleAndKeywordsNode extends ParseTupleAndKeywordsBas
30343033

30353034
@Specialization(guards = "arguments.length == 5", limit = "2")
30363035
static int doConvert(Object cextModule, Object[] arguments, @SuppressWarnings("unused") PKeyword[] keywords,
3036+
@CachedContext(PythonLanguage.class) PythonContext context,
30373037
@CachedLibrary("getKwds(arguments)") ReferenceLibrary kwdsRefLib,
30383038
@CachedLibrary("getKwdnames(arguments)") ReferenceLibrary kwdnamesRefLib,
30393039
@Cached("createIdentityProfile()") ValueProfile kwdsProfile,
@@ -3044,10 +3044,12 @@ static int doConvert(Object cextModule, Object[] arguments, @SuppressWarnings("u
30443044
@Cached CastToJavaStringNode castToStringNode,
30453045
@Cached CExtNodes.ToSulongNode nativeNullToSulongNode,
30463046
@Cached GetNativeNullNode getNativeNullNode,
3047-
@Cached CExtModsupportNodes.ParseTupleAndKeywordsNode parseTupleAndKeywordsNode) {
3047+
@Cached CExtParseArgumentsNode.ParseTupleAndKeywordsNode parseTupleAndKeywordsNode) {
3048+
CExtContext nativeContext = context.getCApiContext();
30483049
Object argv = argvToJavaNode.execute(arguments[0]);
3049-
return ParseTupleAndKeywordsBaseNode.doConvert(cextModule, argv, arguments[1], arguments[2], arguments[3], arguments[4], kwdsRefLib, kwdnamesRefLib, kwdsProfile, kwdnamesProfile,
3050-
functionNameProfile, kwdsToJavaNode, castToStringNode, nativeNullToSulongNode, getNativeNullNode, parseTupleAndKeywordsNode);
3050+
Object nativeNull = nativeNullToSulongNode.execute(getNativeNullNode.execute(cextModule));
3051+
return ParseTupleAndKeywordsBaseNode.doConvert(nativeContext, nativeNull, argv, arguments[1], arguments[2], arguments[3], arguments[4], kwdsRefLib, kwdnamesRefLib, kwdsProfile,
3052+
kwdnamesProfile, functionNameProfile, kwdsToJavaNode, castToStringNode, parseTupleAndKeywordsNode);
30513053
}
30523054

30533055
}
@@ -3058,6 +3060,7 @@ abstract static class ParseStackAndKeywordsNode extends ParseTupleAndKeywordsBas
30583060

30593061
@Specialization(guards = "arguments.length == 5", limit = "2")
30603062
int doConvert(VirtualFrame frame, Object cextModule, Object[] arguments, @SuppressWarnings("unused") PKeyword[] keywords,
3063+
@CachedContext(PythonLanguage.class) PythonContext context,
30613064
@CachedLibrary("getArgsArray(arguments)") InteropLibrary argsArrayLib,
30623065
@CachedLibrary("getKwds(arguments)") ReferenceLibrary kwdsRefLib,
30633066
@CachedLibrary("getKwdnames(arguments)") ReferenceLibrary kwdnamesRefLib,
@@ -3069,18 +3072,20 @@ int doConvert(VirtualFrame frame, Object cextModule, Object[] arguments, @Suppre
30693072
@Cached CastToJavaStringNode castToStringNode,
30703073
@Cached CExtNodes.ToSulongNode nativeNullToSulongNode,
30713074
@Cached GetNativeNullNode getNativeNullNode,
3072-
@Cached CExtModsupportNodes.ParseTupleAndKeywordsNode parseTupleAndKeywordsNode,
3075+
@Cached CExtParseArgumentsNode.ParseTupleAndKeywordsNode parseTupleAndKeywordsNode,
30733076
@Cached PRaiseNativeNode raiseNode) {
30743077
try {
3078+
CExtContext nativeContext = context.getCApiContext();
30753079
Object argsArray = arguments[0];
30763080
int n = PInt.intValueExact(argsArrayLib.getArraySize(argsArray));
30773081
Object[] args = new Object[n];
30783082
for (int i = 0; i < args.length; i++) {
30793083
args[i] = argvToJavaNode.execute(argsArrayLib.readArrayElement(argsArray, i));
30803084
}
3085+
Object nativeNull = nativeNullToSulongNode.execute(getNativeNullNode.execute(cextModule));
30813086
PTuple argv = factory().createTuple(args);
3082-
return ParseTupleAndKeywordsBaseNode.doConvert(cextModule, argv, arguments[1], arguments[2], arguments[3], arguments[4], kwdsRefLib, kwdnamesRefLib, kwdsProfile, kwdnamesProfile,
3083-
functionNameProfile, kwdsToJavaNode, castToStringNode, nativeNullToSulongNode, getNativeNullNode, parseTupleAndKeywordsNode);
3087+
return ParseTupleAndKeywordsBaseNode.doConvert(nativeContext, nativeNull, argv, arguments[1], arguments[2], arguments[3], arguments[4], kwdsRefLib, kwdnamesRefLib, kwdsProfile,
3088+
kwdnamesProfile, functionNameProfile, kwdsToJavaNode, castToStringNode, parseTupleAndKeywordsNode);
30843089
} catch (InteropException e) {
30853090
CompilerDirectives.transferToInterpreter();
30863091
return raiseNode.raiseInt(frame, 0, SystemError, "error when reading native argument stack: %s", e);
@@ -3098,22 +3103,25 @@ abstract static class ParseTupleAndKeywordsVaListNode extends ParseTupleAndKeywo
30983103

30993104
@Specialization(guards = "arguments.length == 5", limit = "2")
31003105
static int doConvert(Object cextModule, Object[] arguments, @SuppressWarnings("unused") PKeyword[] keywords,
3106+
@CachedContext(PythonLanguage.class) PythonContext context,
31013107
@CachedLibrary("getKwds(arguments)") ReferenceLibrary kwdsRefLib,
31023108
@CachedLibrary("getKwdnames(arguments)") ReferenceLibrary kwdnamesRefLib,
31033109
@Cached("createIdentityProfile()") ValueProfile kwdsProfile,
31043110
@Cached("createBinaryProfile()") ConditionProfile kwdnamesProfile,
31053111
@Cached("createBinaryProfile()") ConditionProfile functionNameProfile,
3106-
@Cached PCallCapiFunction callMallocOutVarPtr,
3112+
@Cached PCallCExtFunction callMallocOutVarPtr,
31073113
@Cached CExtNodes.AsPythonObjectNode argvToJavaNode,
31083114
@Cached CExtNodes.AsPythonObjectNode kwdsToJavaNode,
31093115
@Cached CastToJavaStringNode castToStringNode,
31103116
@Cached CExtNodes.ToSulongNode nativeNullToSulongNode,
31113117
@Cached GetNativeNullNode getNativeNullNode,
3112-
@Cached CExtModsupportNodes.ParseTupleAndKeywordsNode parseTupleAndKeywordsNode) {
3118+
@Cached CExtParseArgumentsNode.ParseTupleAndKeywordsNode parseTupleAndKeywordsNode) {
3119+
CExtContext nativeContext = context.getCApiContext();
3120+
Object nativeNull = nativeNullToSulongNode.execute(getNativeNullNode.execute(cextModule));
31133121
Object argv = argvToJavaNode.execute(arguments[0]);
3114-
VaListWrapper varargs = new VaListWrapper(arguments[4], callMallocOutVarPtr.call(NativeCAPISymbols.FUN_ALLOCATE_OUTVAR));
3115-
return ParseTupleAndKeywordsBaseNode.doConvert(cextModule, argv, arguments[1], arguments[2], arguments[3], varargs, kwdsRefLib, kwdnamesRefLib, kwdsProfile, kwdnamesProfile,
3116-
functionNameProfile, kwdsToJavaNode, castToStringNode, nativeNullToSulongNode, getNativeNullNode, parseTupleAndKeywordsNode);
3122+
VaListWrapper varargs = new VaListWrapper(nativeContext, arguments[4], callMallocOutVarPtr.call(nativeContext, NativeCAPISymbols.FUN_ALLOCATE_OUTVAR));
3123+
return ParseTupleAndKeywordsBaseNode.doConvert(nativeContext, nativeNull, argv, arguments[1], arguments[2], arguments[3], varargs, kwdsRefLib, kwdnamesRefLib, kwdsProfile, kwdnamesProfile,
3124+
functionNameProfile, kwdsToJavaNode, castToStringNode, parseTupleAndKeywordsNode);
31173125
}
31183126
}
31193127

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.cext.capi;
4242

43+
import com.oracle.graal.python.builtins.objects.cext.CAPIConversionNodeSupplier;
4344
import com.oracle.graal.python.builtins.objects.cext.common.CExtContext;
4445
import com.oracle.graal.python.runtime.PythonContext;
4546

4647
public final class CApiContext extends CExtContext {
4748
public CApiContext(PythonContext context, Object hpyLibrary) {
48-
super(context, hpyLibrary);
49+
super(context, hpyLibrary, CAPIConversionNodeSupplier.INSTANCE);
4950
}
5051
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/common/CExtContext.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -44,17 +44,21 @@
4444

4545
public abstract class CExtContext {
4646

47-
public static CExtContext LAZY_CONTEXT = new CExtContext(null, null) {
47+
public static CExtContext LAZY_CONTEXT = new CExtContext(null, null, null) {
4848
};
4949

5050
private final PythonContext context;
5151

5252
/** The LLVM bitcode library object representing 'libpython.*.so' or similar. */
5353
private final Object llvmLibrary;
5454

55-
public CExtContext(PythonContext context, Object llvmLibrary) {
55+
/** A factory for creating context-specific conversion nodes. */
56+
private final ConversionNodeSupplier supplier;
57+
58+
public CExtContext(PythonContext context, Object llvmLibrary, ConversionNodeSupplier supplier) {
5659
this.context = context;
5760
this.llvmLibrary = llvmLibrary;
61+
this.supplier = supplier;
5862
}
5963

6064
public final PythonContext getContext() {
@@ -64,4 +68,8 @@ public final PythonContext getContext() {
6468
public final Object getLLVMLibrary() {
6569
return llvmLibrary;
6670
}
71+
72+
public final ConversionNodeSupplier getSupplier() {
73+
return supplier;
74+
}
6775
}

0 commit comments

Comments
 (0)