Skip to content

Commit 14ba3fa

Browse files
committed
[GR-11176] Remove obsolete Sulong workarounds.
PullRequest: graalpython/164
2 parents 37efdd4 + ce333f2 commit 14ba3fa

File tree

8 files changed

+58
-152
lines changed

8 files changed

+58
-152
lines changed

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,9 @@
4141
#include "capi.h"
4242

4343

44-
MUST_INLINE static void force_to_native(void* obj) {
45-
if (polyglot_is_value(obj)) {
46-
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_Set_Ptr", obj, truffle_deref_handle_for_managed(obj));
47-
}
48-
}
49-
5044
static void initialize_type_structure(PyTypeObject* structure, const char* typname, void* typeid) {
5145
PyTypeObject* ptype = (PyTypeObject*)UPCALL_CEXT_O("PyTruffle_Type", polyglot_from_string(typname, SRC_CS));
5246

53-
// We eagerly create a native pointer for all builtin types. This is necessary for pointer comparisons to work correctly.
54-
// TODO Remove this as soon as this is properly supported.
55-
force_to_native(ptype);
56-
5747
// Store the Sulong struct type id to be used for instances of this class
5848
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_Set_SulongType", ptype, typeid);
5949

@@ -130,30 +120,24 @@ POLYGLOT_DECLARE_TYPE(PyObjectPtr);
130120
static void initialize_globals() {
131121
// None
132122
PyObject* jnone = UPCALL_CEXT_O("Py_None");
133-
force_to_native(jnone);
134123
truffle_assign_managed(&_Py_NoneStruct, jnone);
135124

136125
// NotImplemented
137126
void *jnotimpl = UPCALL_CEXT_O("Py_NotImplemented");
138-
force_to_native(jnotimpl);
139127
truffle_assign_managed(&_Py_NotImplementedStruct, jnotimpl);
140128

141129
// Ellipsis
142130
void *jellipsis = UPCALL_CEXT_O("Py_Ellipsis");
143-
force_to_native(jellipsis);
144131
truffle_assign_managed(&_Py_EllipsisObject, jellipsis);
145132

146133
// True, False
147134
void *jtrue = UPCALL_CEXT_O("Py_True");
148-
force_to_native(jtrue);
149135
truffle_assign_managed(&_Py_TrueStruct, jtrue);
150136
void *jfalse = UPCALL_CEXT_O("Py_False");
151-
force_to_native(jfalse);
152137
truffle_assign_managed(&_Py_FalseStruct, jfalse);
153138

154139
// error marker
155140
void *jerrormarker = UPCALL_CEXT_PTR("Py_ErrorHandler");
156-
force_to_native(jerrormarker);
157141
truffle_assign_managed(&marker_struct, jerrormarker);
158142
}
159143

@@ -177,6 +161,10 @@ inline void* handle_exception(void* val) {
177161
return val == ERROR_MARKER ? NULL : val;
178162
}
179163

164+
// Heuristic to test if some value is a pointer object
165+
// TODO we need a reliable solution for that
166+
#define IS_POINTER(__val__) (polyglot_is_value(__val__) && !polyglot_fits_in_i64(__val__))
167+
180168
void* native_to_java(PyObject* obj) {
181169
if (obj == Py_None) {
182170
return Py_None;
@@ -188,6 +176,8 @@ void* native_to_java(PyObject* obj) {
188176
return truffle_managed_from_handle(obj);
189177
} else if (truffle_is_handle_to_managed(obj->ob_refcnt)) {
190178
return truffle_managed_from_handle(obj->ob_refcnt);
179+
} else if (IS_POINTER(obj->ob_refcnt)) {
180+
return obj->ob_refcnt;
191181
}
192182
return obj;
193183
}

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,19 @@ int PyType_IsSubtype(PyTypeObject* a, PyTypeObject* b) {
5757
}
5858

5959
static int add_subclass(PyTypeObject *base, PyTypeObject *type) {
60-
void* key = PyLong_FromVoidPtr((void *) type);
60+
void* key = (void *) type;
6161
if (key == NULL) {
6262
return -1;
6363
}
64-
if (polyglot_is_value(base)) {
65-
return polyglot_as_i32(polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_Add_Subclass", native_to_java((PyObject*)base), native_to_java(key), native_to_java((PyObject*)type)));
66-
} else {
67-
PyObject *dict = base->tp_subclasses;
64+
PyObject *dict = base->tp_subclasses;
65+
if (dict == NULL) {
66+
base->tp_subclasses = dict = PyDict_New();
6867
if (dict == NULL) {
69-
base->tp_subclasses = dict = PyDict_New();
70-
if (dict == NULL) {
71-
return -1;
72-
}
68+
return -1;
7369
}
74-
// TODO value should be a weak reference !
75-
return PyDict_SetItem(base->tp_subclasses, key, (PyObject*)type);
7670
}
77-
return -1;
71+
// TODO value should be a weak reference !
72+
return PyDict_SetItem(base->tp_subclasses, key, (PyObject*)type);
7873
}
7974

8075
/* Special C landing functions that convert some arguments to primitives. */
@@ -250,7 +245,7 @@ int PyType_Ready(PyTypeObject* cls) {
250245
native_to_java(native_members));
251246

252247
// remember the managed wrapper
253-
((PyObject*)cls)->ob_refcnt = truffle_handle_for_managed(javacls);
248+
((PyObject*)cls)->ob_refcnt = javacls;
254249
if (cls->tp_dict != NULL) {
255250
javacls->tp_dict = native_to_java(cls->tp_dict);
256251
} else {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CStringWrapper;
7171
import com.oracle.graal.python.builtins.objects.cext.CExtNodes;
7272
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PySequenceArrayWrapper;
73+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonClassInitNativeWrapper;
7374
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonClassNativeWrapper;
7475
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
7576
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonObjectNativeWrapper;
@@ -574,7 +575,7 @@ Object run(Object typestruct, PythonClass metaClass, PTuple baseClasses, PDict n
574575
if (module != null) {
575576
writeNode.execute(cclass, SpecialAttributeNames.__MODULE__, module);
576577
}
577-
return PythonClassNativeWrapper.wrap(cclass);
578+
return new PythonClassInitNativeWrapper(cclass);
578579
}
579580

580581
private String getStringItem(PDict nativeMembers, String key) {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,23 @@ public String toString() {
188188
}
189189
}
190190

191+
/**
192+
* Used to wrap {@link PythonClass} just for the time when a natively defined type is processed
193+
* in {@code PyType_Ready} and we need to pass the mirroring managed class to native to marry
194+
* these two objects.
195+
*/
196+
public static class PythonClassInitNativeWrapper extends PythonObjectNativeWrapper {
197+
198+
public PythonClassInitNativeWrapper(PythonClass object) {
199+
super(object);
200+
}
201+
202+
@Override
203+
public String toString() {
204+
return String.format("PythonClassNativeInitWrapper(%s, isNative=%s)", getPythonObject(), isNative());
205+
}
206+
}
207+
191208
/**
192209
* Wraps a sequence object (like a list) such that it behaves like a bare C array.
193210
*/

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ Object access(PySequenceArrayWrapper obj) {
284284
}
285285
}
286286

287-
abstract static class ToNativeArrayNode extends TransformToNativeNode {
287+
abstract static class ToNativeArrayNode extends PBaseNode {
288288
@CompilationFinal private TruffleObject PyObjectHandle_FromJavaObject;
289289
@Child private PCallNativeNode callNativeBinary;
290290
@Child private ToNativeStorageNode toNativeStorageNode;
@@ -306,7 +306,7 @@ Object doPSequence(PySequenceArrayWrapper object) {
306306
@Fallback
307307
Object doGeneric(PySequenceArrayWrapper object) {
308308
// TODO correct element size
309-
return ensureIsPointer(callBinaryIntoCapi(getNativeHandleForArray(), object, 8L));
309+
return callBinaryIntoCapi(getNativeHandleForArray(), object, 8L);
310310
}
311311

312312
private TruffleObject getNativeHandleForArray() {
@@ -449,7 +449,7 @@ public static GetTypeIDNode create() {
449449
}
450450
}
451451

452-
static abstract class ToNativeStorageNode extends TransformToNativeNode {
452+
static abstract class ToNativeStorageNode extends PBaseNode {
453453
@Child private StorageToNativeNode storageToNativeNode;
454454

455455
public abstract NativeSequenceStorage execute(SequenceStorage object);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMR.ToPyObjectNode;
5353
import com.oracle.graal.python.builtins.objects.cext.UnicodeObjectNodes.UnicodeAsWideCharNode;
5454
import com.oracle.graal.python.builtins.objects.str.PString;
55+
import com.oracle.graal.python.nodes.PBaseNode;
5556
import com.oracle.truffle.api.CompilerDirectives;
5657
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
5758
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -179,7 +180,7 @@ long access(PyUnicodeWrapper obj) {
179180
}
180181
}
181182

182-
abstract static class PyUnicodeToNativeNode extends TransformToNativeNode {
183+
abstract static class PyUnicodeToNativeNode extends PBaseNode {
183184
@CompilationFinal private TruffleObject derefHandleIntrinsic;
184185
@Child private PCallNativeNode callNativeUnary;
185186
@Child private PCallNativeNode callNativeBinary;
@@ -189,7 +190,7 @@ abstract static class PyUnicodeToNativeNode extends TransformToNativeNode {
189190

190191
@Specialization
191192
Object doUnicodeWrapper(PyUnicodeWrapper object) {
192-
return ensureIsPointer(callUnaryIntoCapi(getPyObjectHandle_ForJavaType(), object));
193+
return callUnaryIntoCapi(getPyObjectHandle_ForJavaType(), object);
193194
}
194195

195196
private TruffleObject getPyObjectHandle_ForJavaType() {

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PySequenceArrayWrapper;
5454
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PyUnicodeData;
5555
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PyUnicodeState;
56+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonClassInitNativeWrapper;
5657
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
5758
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonObjectNativeWrapper;
5859
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMRFactory.PAsPointerNodeGen;
@@ -790,7 +791,15 @@ public Object access(Object object) {
790791
abstract static class ToNativeNode extends Node {
791792
@Child private ToPyObjectNode toPyObjectNode = ToPyObjectNode.create();
792793

794+
Object access(PythonClassInitNativeWrapper obj) {
795+
if (!obj.isNative()) {
796+
obj.setNativePointer(toPyObjectNode.getHandleForObject(obj.getDelegate(), 0));
797+
}
798+
return obj;
799+
}
800+
793801
Object access(PythonNativeWrapper obj) {
802+
assert !(obj instanceof PythonClassInitNativeWrapper);
794803
if (!obj.isNative()) {
795804
obj.setNativePointer(toPyObjectNode.execute(obj.getDelegate()));
796805
}
@@ -800,18 +809,8 @@ Object access(PythonNativeWrapper obj) {
800809

801810
@Resolve(message = "IS_POINTER")
802811
abstract static class IsPointerNode extends Node {
803-
@Child private Node isPointerNode;
804-
805812
boolean access(PythonNativeWrapper obj) {
806-
return obj.isNative() && (!(obj.getNativePointer() instanceof TruffleObject) || ForeignAccess.sendIsPointer(getIsPointerNode(), (TruffleObject) obj.getNativePointer()));
807-
}
808-
809-
private Node getIsPointerNode() {
810-
if (isPointerNode == null) {
811-
CompilerDirectives.transferToInterpreterAndInvalidate();
812-
isPointerNode = insert(Message.IS_POINTER.createNode());
813-
}
814-
return isPointerNode;
813+
return obj.isNative();
815814
}
816815
}
817816

@@ -867,7 +866,7 @@ public static PAsPointerNode create() {
867866

868867
}
869868

870-
abstract static class ToPyObjectNode extends TransformToNativeNode {
869+
abstract static class ToPyObjectNode extends PBaseNode {
871870
@CompilationFinal private TruffleObject PyObjectHandle_FromJavaObject;
872871
@CompilationFinal private TruffleObject PyObjectHandle_FromJavaType;
873872
@CompilationFinal private TruffleObject PyNoneHandle;
@@ -880,23 +879,27 @@ abstract static class ToPyObjectNode extends TransformToNativeNode {
880879

881880
@Specialization
882881
Object runNativeClass(PythonNativeClass object) {
883-
return ensureIsPointer(object.object);
882+
return object.object;
884883
}
885884

886885
@Specialization
887886
Object runNativeObject(PythonNativeObject object) {
888-
return ensureIsPointer(object.object);
887+
return object.object;
889888
}
890889

891890
@Specialization(guards = "isManagedPythonClass(object)")
892891
Object runClass(PythonClass object) {
893-
return ensureIsPointer(callUnaryIntoCapi(getPyObjectHandle_ForJavaType(), getToSulongNode().execute(object)));
892+
return callUnaryIntoCapi(getPyObjectHandle_ForJavaType(), getToSulongNode().execute(object));
894893
}
895894

896895
@Fallback
897896
Object runObject(Object object) {
898897
PythonClass clazz = getClassNode().execute(object);
899-
return ensureIsPointer(callBinaryIntoCapi(getPyObjectHandle_ForJavaObject(), getToSulongNode().execute(object), clazz.getFlags()));
898+
return callBinaryIntoCapi(getPyObjectHandle_ForJavaObject(), getToSulongNode().execute(object), clazz.getFlags());
899+
}
900+
901+
Object getHandleForObject(Object object, long flags) {
902+
return callBinaryIntoCapi(getPyObjectHandle_ForJavaObject(), object, flags);
900903
}
901904

902905
private TruffleObject getPyObjectHandle_ForJavaType() {

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

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)