Skip to content

Commit 3434cec

Browse files
committed
[GR-9246] Various C API fixes for Managed Sulong.
PullRequest: graalpython/197
2 parents 81fd559 + a9d592f commit 3434cec

File tree

8 files changed

+120
-63
lines changed

8 files changed

+120
-63
lines changed

graalpython/com.oracle.graal.python.cext/src/longobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow) {
7070
return result;
7171
}
7272

73-
unsigned long long PyLong_AsUnsignedLonglong(PyObject *obj) {
73+
unsigned long long PyLong_AsUnsignedLongLong(PyObject *obj) {
7474
return as_unsigned_long_long(obj);
7575
}
7676

graalpython/com.oracle.graal.python.cext/src/methodobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ PyObject* PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) {
5252
get_method_flags_cwrapper(ml->ml_flags),
5353
get_method_flags_wrapper(ml->ml_flags),
5454
self,
55-
module,
55+
native_to_java(module),
5656
polyglot_from_string((const char*)(ml->ml_doc ? ml->ml_doc : ""), SRC_CS)));
5757
}

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

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,17 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.cext;
4242

43+
import com.oracle.graal.python.builtins.objects.cext.CArrayWrapperMRFactory.GetTypeIDNodeGen;
4344
import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CArrayWrapper;
4445
import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CByteArrayWrapper;
4546
import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CStringWrapper;
47+
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.CExtBaseNode;
48+
import com.oracle.graal.python.builtins.objects.ints.PInt;
49+
import com.oracle.graal.python.nodes.SpecialMethodNames;
4650
import com.oracle.truffle.api.CompilerDirectives;
51+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
52+
import com.oracle.truffle.api.dsl.ImportStatic;
53+
import com.oracle.truffle.api.dsl.Specialization;
4754
import com.oracle.truffle.api.interop.ForeignAccess;
4855
import com.oracle.truffle.api.interop.Message;
4956
import com.oracle.truffle.api.interop.MessageResolution;
@@ -58,19 +65,24 @@ public class CArrayWrapperMR {
5865

5966
@Resolve(message = "READ")
6067
abstract static class ReadNode extends Node {
61-
6268
public char access(CStringWrapper object, int idx) {
6369
String s = object.getDelegate();
6470
if (idx >= 0 && idx < s.length()) {
6571
return s.charAt(idx);
6672
} else if (idx == s.length()) {
6773
return '\0';
6874
}
75+
CompilerDirectives.transferToInterpreter();
6976
throw UnknownIdentifierException.raise(Integer.toString(idx));
7077
}
7178

72-
public byte access(CByteArrayWrapper object, long idx) {
73-
return access(object, (int) idx);
79+
public char access(CStringWrapper object, long idx) {
80+
try {
81+
return access(object, PInt.intValueExact(idx));
82+
} catch (ArithmeticException e) {
83+
CompilerDirectives.transferToInterpreter();
84+
throw UnknownIdentifierException.raise(Long.toString(idx));
85+
}
7486
}
7587

7688
public byte access(CByteArrayWrapper object, int idx) {
@@ -80,8 +92,51 @@ public byte access(CByteArrayWrapper object, int idx) {
8092
} else if (idx == arr.length) {
8193
return (byte) 0;
8294
}
95+
CompilerDirectives.transferToInterpreter();
8396
throw UnknownIdentifierException.raise(Integer.toString(idx));
8497
}
98+
99+
public byte access(CByteArrayWrapper object, long idx) {
100+
try {
101+
return access(object, PInt.intValueExact(idx));
102+
} catch (ArithmeticException e) {
103+
CompilerDirectives.transferToInterpreter();
104+
throw UnknownIdentifierException.raise(Long.toString(idx));
105+
}
106+
}
107+
}
108+
109+
@SuppressWarnings("unknown-message")
110+
@Resolve(message = "com.oracle.truffle.llvm.spi.GetDynamicType")
111+
abstract static class GetDynamicTypeNode extends Node {
112+
@Child private GetTypeIDNode getTypeId = GetTypeIDNodeGen.create();
113+
114+
public Object access(CStringWrapper object) {
115+
return getTypeId.execute(object);
116+
}
117+
}
118+
119+
@ImportStatic(SpecialMethodNames.class)
120+
abstract static class GetTypeIDNode extends CExtBaseNode {
121+
122+
@Child private PCallNativeNode callUnaryNode = PCallNativeNode.create();
123+
124+
@CompilationFinal private TruffleObject funGetByteArrayTypeID;
125+
126+
public abstract Object execute(Object delegate);
127+
128+
@Specialization
129+
Object doTuple(CStringWrapper object) {
130+
return callGetByteArrayTypeID(object.getDelegate().length());
131+
}
132+
133+
private Object callGetByteArrayTypeID(long len) {
134+
if (funGetByteArrayTypeID == null) {
135+
CompilerDirectives.transferToInterpreterAndInvalidate();
136+
funGetByteArrayTypeID = importCAPISymbol(NativeCAPISymbols.FUN_GET_BYTE_ARRAY_TYPE_ID);
137+
}
138+
return callUnaryNode.execute(funGetByteArrayTypeID, new Object[]{len});
139+
}
85140
}
86141

87142
@Resolve(message = "HAS_SIZE")

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public final class NativeMemberNames {
9595
public static final String BUF_DELEGATE = "buf_delegate";
9696
public static final String BUF_READONLY = "readonly";
9797
public static final String NB_ADD = "nb_add";
98+
public static final String NB_AND = "nb_and";
9899
public static final String NB_INDEX = "nb_index";
99100
public static final String NB_POW = "nb_power";
100101
public static final String NB_TRUE_DIVIDE = "nb_true_divide";

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

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,28 @@
4141
package com.oracle.graal.python.builtins.objects.cext;
4242

4343
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_ADD;
44+
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_AND;
4445
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_INDEX;
45-
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_MULTIPLY;
4646
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_INPLACE_MULTIPLY;
47+
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_MULTIPLY;
4748
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_POW;
4849
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_TRUE_DIVIDE;
4950
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ADD__;
51+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__AND__;
52+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__IMUL__;
5053
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INDEX__;
51-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__POW__;
5254
import static com.oracle.graal.python.nodes.SpecialMethodNames.__MUL__;
53-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__IMUL__;
55+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__POW__;
5456
import static com.oracle.graal.python.nodes.SpecialMethodNames.__TRUEDIV__;
5557

5658
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ToSulongNode;
59+
import com.oracle.graal.python.builtins.objects.cext.PyNumberMethodsWrapperMRFactory.ReadMethodNodeGen;
5760
import com.oracle.graal.python.builtins.objects.type.PythonClass;
61+
import com.oracle.graal.python.nodes.PNodeWithContext;
5862
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
5963
import com.oracle.truffle.api.CompilerDirectives;
64+
import com.oracle.truffle.api.dsl.Cached;
65+
import com.oracle.truffle.api.dsl.Specialization;
6066
import com.oracle.truffle.api.interop.MessageResolution;
6167
import com.oracle.truffle.api.interop.Resolve;
6268
import com.oracle.truffle.api.interop.UnknownIdentifierException;
@@ -67,73 +73,65 @@ public class PyNumberMethodsWrapperMR {
6773

6874
@Resolve(message = "READ")
6975
abstract static class ReadNode extends Node {
70-
@Child private LookupAttributeInMRONode getAddAttributeNode;
71-
@Child private LookupAttributeInMRONode getIndexAttributeNode;
72-
@Child private LookupAttributeInMRONode getPowAttributeNode;
73-
@Child private LookupAttributeInMRONode getMulAttributeNode;
74-
@Child private LookupAttributeInMRONode getTrueDivAttributeNode;
76+
@Child private ReadMethodNode readMethodNode = ReadMethodNodeGen.create();
7577
@Child private ToSulongNode toSulongNode;
7678

7779
public Object access(PyNumberMethodsWrapper object, String key) {
7880
// translate key to attribute name
7981
PythonClass delegate = object.getDelegate();
80-
Object result;
82+
return getToSulongNode().execute(readMethodNode.execute(delegate, key));
83+
}
84+
85+
private ToSulongNode getToSulongNode() {
86+
if (toSulongNode == null) {
87+
CompilerDirectives.transferToInterpreterAndInvalidate();
88+
toSulongNode = insert(ToSulongNode.create());
89+
}
90+
return toSulongNode;
91+
}
92+
}
93+
94+
abstract static class ReadMethodNode extends PNodeWithContext {
95+
96+
public abstract Object execute(PythonClass clazz, String key);
97+
98+
@Specialization(limit = "99", guards = {"eq(cachedKey, key)"})
99+
Object getMethod(PythonClass clazz, @SuppressWarnings("unused") String key,
100+
@Cached("key") @SuppressWarnings("unused") String cachedKey,
101+
@Cached("createLookupNode(cachedKey)") LookupAttributeInMRONode lookupNode) {
102+
if (lookupNode != null) {
103+
return lookupNode.execute(clazz);
104+
}
105+
// TODO extend list
106+
CompilerDirectives.transferToInterpreter();
107+
throw UnknownIdentifierException.raise(key);
108+
}
109+
110+
protected LookupAttributeInMRONode createLookupNode(String key) {
81111
switch (key) {
82112
case NB_ADD:
83-
if (getAddAttributeNode == null) {
84-
CompilerDirectives.transferToInterpreterAndInvalidate();
85-
getAddAttributeNode = insert(LookupAttributeInMRONode.create(__ADD__));
86-
}
87-
result = getAddAttributeNode.execute(delegate);
88-
break;
113+
return LookupAttributeInMRONode.create(__ADD__);
114+
case NB_AND:
115+
return LookupAttributeInMRONode.create(__AND__);
89116
case NB_INDEX:
90-
if (getIndexAttributeNode == null) {
91-
CompilerDirectives.transferToInterpreterAndInvalidate();
92-
getIndexAttributeNode = insert(LookupAttributeInMRONode.create(__INDEX__));
93-
}
94-
result = getIndexAttributeNode.execute(delegate);
95-
break;
117+
return LookupAttributeInMRONode.create(__INDEX__);
96118
case NB_POW:
97-
if (getPowAttributeNode == null) {
98-
CompilerDirectives.transferToInterpreterAndInvalidate();
99-
getPowAttributeNode = insert(LookupAttributeInMRONode.create(__POW__));
100-
}
101-
result = getPowAttributeNode.execute(delegate);
102-
break;
119+
return LookupAttributeInMRONode.create(__POW__);
103120
case NB_TRUE_DIVIDE:
104-
if (getTrueDivAttributeNode == null) {
105-
CompilerDirectives.transferToInterpreterAndInvalidate();
106-
getTrueDivAttributeNode = insert(LookupAttributeInMRONode.create(__TRUEDIV__));
107-
}
108-
result = getTrueDivAttributeNode.execute(delegate);
109-
break;
121+
return LookupAttributeInMRONode.create(__TRUEDIV__);
110122
case NB_MULTIPLY:
111-
if (getMulAttributeNode == null) {
112-
CompilerDirectives.transferToInterpreterAndInvalidate();
113-
getMulAttributeNode = insert(LookupAttributeInMRONode.create(__MUL__));
114-
}
115-
result = getMulAttributeNode.execute(delegate);
116-
break;
123+
return LookupAttributeInMRONode.create(__MUL__);
117124
case NB_INPLACE_MULTIPLY:
118-
if (getMulAttributeNode == null) {
119-
CompilerDirectives.transferToInterpreterAndInvalidate();
120-
getMulAttributeNode = insert(LookupAttributeInMRONode.create(__IMUL__));
121-
}
122-
result = getMulAttributeNode.execute(delegate);
123-
break;
125+
return LookupAttributeInMRONode.create(__IMUL__);
124126
default:
125127
// TODO extend list
126128
throw UnknownIdentifierException.raise(key);
127129
}
128-
return getToSulongNode().execute(result);
129130
}
130131

131-
private ToSulongNode getToSulongNode() {
132-
if (toSulongNode == null) {
133-
CompilerDirectives.transferToInterpreterAndInvalidate();
134-
toSulongNode = insert(ToSulongNode.create());
135-
}
136-
return toSulongNode;
132+
protected static boolean eq(String expected, String actual) {
133+
return expected.equals(actual);
137134
}
138135
}
136+
139137
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
4646
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
47+
import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
4748
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.CExtBaseNode;
4849
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ToSulongNode;
4950
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PySequenceArrayWrapper;
@@ -86,6 +87,7 @@
8687
import com.oracle.truffle.api.interop.UnsupportedMessageException;
8788
import com.oracle.truffle.api.nodes.Node;
8889
import com.oracle.truffle.api.nodes.UnexpectedResultException;
90+
import com.oracle.truffle.api.profiles.ValueProfile;
8991

9092
@MessageResolution(receiverType = PySequenceArrayWrapper.class)
9193
public class PySequenceArrayWrapperMR {
@@ -162,17 +164,19 @@ Object doTuple(PList list, long idx,
162164
* {@code uint64_t} since we do not know how many bytes are requested.
163165
*/
164166
@Specialization
165-
long doBytesI64(PBytes bytes, long byteIdx,
167+
long doBytesI64(PIBytesLike bytesLike, long byteIdx,
168+
@Cached("createClassProfile()") ValueProfile profile,
166169
@Cached("create()") SequenceStorageNodes.LenNode lenNode,
167170
@Cached("create()") SequenceStorageNodes.GetItemNode getItemNode) {
168-
int len = lenNode.execute(bytes.getSequenceStorage());
171+
PIBytesLike profiled = profile.profile(bytesLike);
172+
int len = lenNode.execute(profiled.getSequenceStorage());
169173
// simulate sentinel value
170174
if (byteIdx == len) {
171175
return 0L;
172176
}
173177
int i = (int) byteIdx;
174178
long result = 0;
175-
SequenceStorage store = bytes.getSequenceStorage();
179+
SequenceStorage store = profiled.getSequenceStorage();
176180
result |= getItemNode.executeInt(store, i);
177181
if (i + 1 < len)
178182
result |= ((long) getItemNode.executeInt(store, i + 1) << 8L) & 0xFF00L;

graalpython/lib-graalpython/python_cext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def AddFunction(primary, name, cfunc, cwrapper, wrapper, doc, isclass=False, iss
704704
owner = to_java(primary)
705705
if isinstance(owner, moduletype):
706706
# module case, we create the bound function-or-method
707-
func = PyCFunction_NewEx(name, cfunc, cwrapper, wrapper, owner, owner, doc)
707+
func = PyCFunction_NewEx(name, cfunc, cwrapper, wrapper, owner, owner.__name__, doc)
708708
object.__setattr__(owner, name, func)
709709
else:
710710
func = wrapper(CreateFunction(name, cfunc, cwrapper, owner))
@@ -728,7 +728,7 @@ def PyCFunction_NewEx(name, cfunc, cwrapper, wrapper, self, module, doc):
728728
PyTruffle_SetAttr(func, "__name__", name)
729729
PyTruffle_SetAttr(func, "__doc__", doc)
730730
method = PyTruffle_BuiltinMethod(self, func)
731-
PyTruffle_SetAttr(method, "__module__", module.__name__)
731+
PyTruffle_SetAttr(method, "__module__", to_java(module))
732732
return method
733733

734734

mx.graalpython/copyrights/overrides

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/basic/
130130
graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/builtin/BuiltinFunctionTests.java,zippy.copyright
131131
graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/builtin/ImportTests.java,zippy.copyright
132132
graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/builtin/IteratorTests.java,zippy.copyright
133-
graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/builtin/RegularExpressionTests.java,zippy.copyright
134133
graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/builtin/SpecialMethodTests.java,zippy.copyright
135134
graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/datatype/DictTests.java,zippy.copyright
136135
graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/datatype/FrameTest.java,zippy.copyright

0 commit comments

Comments
 (0)