Skip to content

Commit e946fda

Browse files
committed
Refactor 'object.c'.
1 parent 3c903bc commit e946fda

File tree

14 files changed

+273
-160
lines changed

14 files changed

+273
-160
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,9 @@ PyObject* to_sulong(void *o) {
203203
return explicit_cast(truffle_invoke(PY_TRUFFLE_CEXT, "to_sulong", o));
204204
}
205205

206+
/** to be used from Java code only; reads native 'ob_type' field */
206207
void* get_ob_type(PyObject* obj) {
207-
return to_java_type(obj->ob_type);
208+
return native_to_java(obj->ob_type);
208209
}
209210

210211
typedef struct PyObjectHandle {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,16 @@ void* handle_exception(void* val);
9696

9797
// TODO cache landing function ?
9898
#define PY_TRUFFLE_LANDING ((PyObject*(*)(void *rcv, void* name, ...))polyglot_get_member(PY_TRUFFLE_CEXT, polyglot_from_string("PyTruffle_Upcall", SRC_CS)))
99+
#define PY_TRUFFLE_LANDING_L ((PyObject*(*)(void *rcv, void* name, ...))polyglot_get_member(PY_TRUFFLE_CEXT, polyglot_from_string("PyTruffle_Upcall_l", SRC_CS)))
100+
#define PY_TRUFFLE_LANDING_D ((PyObject*(*)(void *rcv, void* name, ...))polyglot_get_member(PY_TRUFFLE_CEXT, polyglot_from_string("PyTruffle_Upcall_d", SRC_CS)))
99101
#define PY_TRUFFLE_CEXT_LANDING ((PyObject*(*)(void* name, ...))polyglot_get_member(PY_TRUFFLE_CEXT, polyglot_from_string("PyTruffle_Cext_Upcall", SRC_CS)))
100102
#define PY_TRUFFLE_CEXT_LANDING_L ((uint64_t (*)(void* name, ...))polyglot_get_member(PY_TRUFFLE_CEXT, polyglot_from_string("PyTruffle_Cext_Upcall_l", SRC_CS)))
101103
#define PY_TRUFFLE_CEXT_LANDING_D ((double (*)(void* name, ...))polyglot_get_member(PY_TRUFFLE_CEXT, polyglot_from_string("PyTruffle_Cext_Upcall_d", SRC_CS)))
102-
#define UPCALL_O(__recv__, __name__, ...) handle_exception_and_cast(PY_TRUFFLE_LANDING((__recv__), polyglot_from_string((__name__), SRC_CS), __VA_ARGS__))
104+
#define UPCALL_O(__recv__, __name__, ...) handle_exception_and_cast(PY_TRUFFLE_LANDING((__recv__), polyglot_from_string((__name__), SRC_CS), ##__VA_ARGS__))
105+
#define UPCALL_P(__recv__, __name__, ...) (PY_TRUFFLE_LANDING_L((__recv__), polyglot_from_string((__name__), SRC_CS), ##__VA_ARGS__))
106+
#define UPCALL_I(__recv__, __name__, ...) UPCALL_P(__recv__, __name__, ##__VA_ARGS__)
107+
#define UPCALL_L(__recv__, __name__, ...) UPCALL_P(__recv__, __name__, ##__VA_ARGS__)
108+
#define UPCALL_D(__recv__, __name__, ...) handle_exception_and_cast(PY_TRUFFLE_LANDING_D((__recv__), polyglot_from_string((__name__), SRC_CS), ##__VA_ARGS__))
103109
#define UPCALL_CEXT_O(__name__, ...) handle_exception_and_cast(PY_TRUFFLE_CEXT_LANDING(polyglot_from_string((__name__), SRC_CS), ##__VA_ARGS__))
104110
#define UPCALL_CEXT_VOID(__name__, ...) (PY_TRUFFLE_CEXT_LANDING(polyglot_from_string((__name__), SRC_CS), ##__VA_ARGS__))
105111
#define UPCALL_CEXT_PTR(__name__, ...) handle_exception(PY_TRUFFLE_CEXT_LANDING(polyglot_from_string((__name__), SRC_CS), ##__VA_ARGS__))
@@ -114,7 +120,7 @@ extern void* to_java_type(PyTypeObject* cls);
114120
void* native_to_java(PyObject* obj);
115121
extern PyObject* to_sulong(void *o);
116122
extern PyObject* explicit_cast(PyObject* cobj);
117-
#define as_char_pointer(obj) polyglot_invoke(PY_TRUFFLE_CEXT, "to_char_pointer", to_java(obj))
123+
#define as_char_pointer(obj) UPCALL_CEXT_O("to_char_pointer", native_to_java(obj))
118124
#define as_long(obj) ((long)polyglot_as_i64(polyglot_invoke(PY_TRUFFLE_CEXT, "to_long", to_java(obj))))
119125
#define as_long_long(obj) ((long long)polyglot_as_i64(polyglot_invoke(PY_TRUFFLE_CEXT, "PyLong_AsPrimitive", to_java(obj), 1, sizeof(long long), polyglot_from_string("long long", "utf-8"))))
120126
#define as_unsigned_long_long(obj) ((unsigned long long)polyglot_as_i64(polyglot_invoke(PY_TRUFFLE_CEXT, "PyLong_AsPrimitive", to_java(obj), 0, sizeof(unsigned long long), polyglot_from_string("unsigned long long", "utf-8"))))

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

Lines changed: 82 additions & 105 deletions
Large diffs are not rendered by default.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ long _PyHASH_NAN;
4747
long _PyHASH_IMAG;
4848

4949
void initialize_hashes() {
50-
_PyHASH_INF = truffle_invoke_l(PY_BUILTIN, "hash", INFINITY);
51-
_PyHASH_NAN = truffle_invoke_l(PY_BUILTIN, "hash", NAN);
52-
_PyHASH_IMAG = truffle_invoke_l(PY_TRUFFLE_CEXT, "PyHash_Imag");
50+
_PyHASH_INF = UPCALL_L(PY_BUILTIN, "hash", INFINITY);
51+
_PyHASH_NAN = UPCALL_L(PY_BUILTIN, "hash", NAN);
52+
_PyHASH_IMAG = UPCALL_L(PY_TRUFFLE_CEXT, "PyHash_Imag");
5353
}

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import com.oracle.graal.python.nodes.PBaseNode;
7979
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
8080
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
81+
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
8182
import com.oracle.graal.python.runtime.PythonCore;
8283
import com.oracle.graal.python.runtime.exception.PException;
8384
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -299,7 +300,8 @@ int getPid() {
299300
@Builtin(name = "fstat", fixedNumOfArguments = 1)
300301
@GenerateNodeFactory
301302
public abstract static class FstatNode extends PythonFileNode {
302-
@Child private StatNode statNode = StatNodeFactory.create(null);
303+
304+
protected abstract Object executeWith(Object fd);
303305

304306
@Specialization(guards = {"fd >= 0", "fd <= 2"})
305307
@TruffleBoundary
@@ -320,9 +322,24 @@ Object fstatStd(@SuppressWarnings("unused") int fd) {
320322

321323
@Specialization(guards = "fd > 2")
322324
@TruffleBoundary
323-
Object fstat(int fd) {
325+
Object fstat(int fd,
326+
@Cached("createStatNode()") StatNode statNode) {
324327
return statNode.executeWith(getFilePath(fd), PNone.NO_VALUE);
325328
}
329+
330+
@Specialization
331+
Object fstatPInt(PInt fd,
332+
@Cached("create()") FstatNode recursive) {
333+
return recursive.executeWith(fd.intValue());
334+
}
335+
336+
protected static StatNode createStatNode() {
337+
return StatNode.create();
338+
}
339+
340+
protected static FstatNode create() {
341+
return PosixModuleBuiltinsFactory.FstatNodeFactory.create(null);
342+
}
326343
}
327344

328345
@Builtin(name = "set_inheritable", fixedNumOfArguments = 2)
@@ -349,7 +366,7 @@ Object setInheritable(int fd, @SuppressWarnings("unused") Object inheritable) {
349366

350367
@Builtin(name = "stat", minNumOfArguments = 1, maxNumOfArguments = 2)
351368
@GenerateNodeFactory
352-
public abstract static class StatNode extends PythonBuiltinNode {
369+
public abstract static class StatNode extends PythonUnaryBuiltinNode {
353370
private static final int S_IFIFO = 0010000;
354371
private static final int S_IFCHR = 0020000;
355372
private static final int S_IFBLK = 0060000;
@@ -475,6 +492,10 @@ Object stat(String path, boolean followSymlinks) {
475492
ctime,
476493
});
477494
}
495+
496+
protected static StatNode create() {
497+
return StatNodeFactory.create(null);
498+
}
478499
}
479500

480501
@Builtin(name = "listdir", fixedNumOfArguments = 1)
@@ -665,6 +686,9 @@ Object mkdir(String path, @SuppressWarnings("unused") int mode, @SuppressWarning
665686
@Builtin(name = "write", fixedNumOfArguments = 2)
666687
@GenerateNodeFactory
667688
public abstract static class WriteNode extends PythonFileNode {
689+
690+
public abstract Object executeWith(Object fd, Object data);
691+
668692
@Specialization(guards = {"fd <= 2", "fd > 0"})
669693
@TruffleBoundary
670694
Object writeStd(int fd, byte[] data) {
@@ -738,6 +762,16 @@ Object write(int fd, PByteArray data) {
738762
Object writeStd(int fd, PByteArray data) {
739763
return writeStd(fd, data.getBytesExact());
740764
}
765+
766+
@Specialization
767+
Object writePInt(PInt fd, Object data,
768+
@Cached("create()") WriteNode recursive) {
769+
return recursive.executeWith(fd.intValue(), data);
770+
}
771+
772+
protected WriteNode create() {
773+
return PosixModuleBuiltinsFactory.WriteNodeFactory.create(null);
774+
}
741775
}
742776

743777
@Builtin(name = "read", fixedNumOfArguments = 2)

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonClassNativeWrapper;
6666
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
6767
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonObjectNativeWrapper;
68+
import com.oracle.graal.python.builtins.objects.cext.PythonNativeClass;
6869
import com.oracle.graal.python.builtins.objects.cext.UnicodeObjectNodes.UnicodeAsWideCharNode;
6970
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
7071
import com.oracle.graal.python.builtins.objects.complex.PComplex;
@@ -499,7 +500,14 @@ private HashingStorageNodes.GetItemNode getGetItemNode() {
499500
}
500501

501502
@Specialization
502-
PythonClass run(TruffleObject typestruct, PythonClass metaClass, PTuple baseClasses, PDict nativeMembers) {
503+
Object run(Object typestruct, PythonObjectNativeWrapper metaClass, PythonObjectNativeWrapper baseClasses, PythonObjectNativeWrapper nativeMembers,
504+
@Cached("create()") CExtNodes.ToJavaNode toJavaNode) {
505+
// TODO(fa) use recursive node
506+
return run(typestruct, (PythonClass) toJavaNode.execute(metaClass), (PTuple) toJavaNode.execute(baseClasses), (PDict) toJavaNode.execute(nativeMembers));
507+
}
508+
509+
@Specialization
510+
Object run(Object typestruct, PythonClass metaClass, PTuple baseClasses, PDict nativeMembers) {
503511
Object[] array = baseClasses.getArray();
504512
PythonClass[] bases = new PythonClass[array.length];
505513
for (int i = 0; i < array.length; i++) {
@@ -508,10 +516,14 @@ PythonClass run(TruffleObject typestruct, PythonClass metaClass, PTuple baseClas
508516

509517
String name = getStringItem(nativeMembers, "tp_name");
510518
String doc = getStringItem(nativeMembers, "tp_doc");
511-
PythonClass cclass = factory().createNativeClassWrapper(typestruct, metaClass, name, bases);
519+
String module = getStringItem(nativeMembers, SpecialAttributeNames.__MODULE__);
520+
PythonNativeClass cclass = factory().createNativeClassWrapper(typestruct, metaClass, name, bases);
512521
writeNode.execute(cclass, SpecialAttributeNames.__DOC__, doc);
513522
writeNode.execute(cclass, SpecialAttributeNames.__BASICSIZE__, getLongItem(nativeMembers, "tp_basicsize"));
514-
return cclass;
523+
if (module != null) {
524+
writeNode.execute(cclass, SpecialAttributeNames.__MODULE__, module);
525+
}
526+
return PythonClassNativeWrapper.wrap(cclass);
515527
}
516528

517529
private String getStringItem(PDict nativeMembers, String key) {
@@ -1176,7 +1188,7 @@ private GetClassNode getClassNode() {
11761188
abstract static class PyTruffle_Set_Ptr extends NativeBuiltin {
11771189

11781190
@Specialization
1179-
PythonObjectNativeWrapper doPythonObject(PythonObjectNativeWrapper nativeWrapper, Object ptr) {
1191+
PythonObjectNativeWrapper doPythonObject(PythonObjectNativeWrapper nativeWrapper, TruffleObject ptr) {
11801192
nativeWrapper.setNativePointer(ptr);
11811193
return nativeWrapper;
11821194
}

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,54 @@ public static FromCharPointerNode create() {
365365
return new FromCharPointerNode();
366366
}
367367
}
368+
369+
public static class GetNativeClassNode extends PBaseNode {
370+
371+
@Child PCallNativeNode callGetObTypeNode;
372+
@Child ToSulongNode toSulongNode;
373+
@Child ToJavaNode toJavaNode;
374+
375+
@CompilationFinal private TruffleObject func;
376+
377+
public PythonClass execute(PythonNativeObject object) {
378+
Object[] args = new Object[]{getToSulongNode().execute(object.object)};
379+
return (PythonClass) getToJavaNode().execute(getCallGetObTypeNode().execute(getObTypeFunction(), args));
380+
}
381+
382+
private ToJavaNode getToJavaNode() {
383+
if (toJavaNode == null) {
384+
CompilerDirectives.transferToInterpreterAndInvalidate();
385+
toJavaNode = insert(ToJavaNode.create());
386+
}
387+
return toJavaNode;
388+
}
389+
390+
private ToSulongNode getToSulongNode() {
391+
if (toSulongNode == null) {
392+
CompilerDirectives.transferToInterpreterAndInvalidate();
393+
toSulongNode = insert(ToSulongNode.create());
394+
}
395+
return toSulongNode;
396+
}
397+
398+
private PCallNativeNode getCallGetObTypeNode() {
399+
if (callGetObTypeNode == null) {
400+
CompilerDirectives.transferToInterpreterAndInvalidate();
401+
callGetObTypeNode = insert(PCallNativeNode.create(1));
402+
}
403+
return callGetObTypeNode;
404+
}
405+
406+
TruffleObject getObTypeFunction() {
407+
if (func == null) {
408+
CompilerDirectives.transferToInterpreterAndInvalidate();
409+
func = (TruffleObject) getContext().getEnv().importSymbol(NativeCAPISymbols.FUN_GET_OB_TYPE);
410+
}
411+
return func;
412+
}
413+
414+
public static GetNativeClassNode create() {
415+
return new GetNativeClassNode();
416+
}
417+
}
368418
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ public abstract class NativeCAPISymbols {
4949
public static final String FUN_WHCAR_SIZE = "PyTruffle_Wchar_Size";
5050
public static final String FUN_PY_TRUFFLE_CSTR_TO_STRING = "PyTruffle_CstrToString";
5151
public static final String FUN_PY_TRUFFLE_BYTE_ARRAY_TO_NATIVE = "PyTruffle_ByteArrayToNative";
52+
public static final String FUN_GET_OB_TYPE = "get_ob_type";
5253
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,7 @@ abstract static class ToNativeNode extends Node {
215215

216216
Object access(PySequenceArrayWrapper obj) {
217217
if (!obj.isNative()) {
218-
// TODO
219-
Object ptr = toPyObjectNode.execute(obj);
220-
obj.setNativePointer(ptr);
218+
obj.setNativePointer(toPyObjectNode.execute(obj));
221219
}
222220
return obj;
223221
}

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,17 +493,26 @@ abstract static class ToNativeNode extends Node {
493493

494494
Object access(PythonNativeWrapper obj) {
495495
if (!obj.isNative()) {
496-
Object ptr = toPyObjectNode.execute(obj.getDelegate());
497-
obj.setNativePointer(ptr);
496+
obj.setNativePointer(toPyObjectNode.execute(obj.getDelegate()));
498497
}
499498
return obj;
500499
}
501500
}
502501

503502
@Resolve(message = "IS_POINTER")
504503
abstract static class IsPointerNode extends Node {
505-
Object access(PythonNativeWrapper obj) {
506-
return obj.isNative();
504+
@Child private Node isPointerNode;
505+
506+
Object access(PythonObjectNativeWrapper obj) {
507+
return obj.isNative() && (!(obj.getNativePointer() instanceof TruffleObject) || ForeignAccess.sendIsPointer(getIsPointerNode(), (TruffleObject) obj.getNativePointer()));
508+
}
509+
510+
private Node getIsPointerNode() {
511+
if (isPointerNode == null) {
512+
CompilerDirectives.transferToInterpreterAndInvalidate();
513+
isPointerNode = insert(Message.IS_POINTER.createNode());
514+
}
515+
return isPointerNode;
507516
}
508517
}
509518

0 commit comments

Comments
 (0)