Skip to content

Commit 5ea152e

Browse files
committed
move getting the class and getting the object dict into the library for native objects
1 parent 40808ce commit 5ea152e

File tree

9 files changed

+143
-139
lines changed

9 files changed

+143
-139
lines changed

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

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

43-
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_GET_OB_TYPE;
4443
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_NATIVE_LONG_TO_JAVA;
4544
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_NATIVE_TO_JAVA;
4645
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_PTR_COMPARE;
4746
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_PY_FLOAT_AS_DOUBLE;
48-
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_PY_OBJECT_GENERIC_GET_DICT;
4947
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_PY_TRUFFLE_BYTE_ARRAY_TO_NATIVE;
5048
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_PY_TRUFFLE_CSTR_TO_STRING;
5149
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_PY_TRUFFLE_STRING_TO_CSTR;
@@ -66,7 +64,6 @@
6664
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.AsLongNodeGen;
6765
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.CextUpcallNodeGen;
6866
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.DirectUpcallNodeGen;
69-
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.GetNativeClassNodeFactory.GetNativeClassCachedNodeGen;
7067
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.GetTypeMemberNodeGen;
7168
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.IsPointerNodeGen;
7269
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.ObjectUpcallNodeGen;
@@ -873,76 +870,6 @@ public String execute(Object charPtr,
873870
}
874871
}
875872

876-
// -----------------------------------------------------------------------------------------------------------------
877-
public abstract static class GetNativeClassNode extends CExtBaseNode {
878-
public abstract PythonAbstractClass execute(PythonNativeObject object);
879-
880-
abstract static class GetNativeClassCachedNode extends GetNativeClassNode {
881-
@Specialization(guards = "object == cachedObject", limit = "1")
882-
static PythonAbstractClass getNativeClassCachedIdentity(@SuppressWarnings("unused") PythonNativeObject object,
883-
@Exclusive @Cached("object") @SuppressWarnings("unused") PythonNativeObject cachedObject,
884-
@Exclusive @Cached(value = "getNativeClassUncached(cachedObject)", allowUncached = true) PythonAbstractClass cachedClass) {
885-
// TODO: (tfel) is this really something we can do? It's so rare for this class to
886-
// change that it shouldn't be worth the effort, but in native code, anything can
887-
// happen. OTOH, CPython also has caches that can become invalid when someone just
888-
// goes and changes the ob_type of an object.
889-
return cachedClass;
890-
}
891-
892-
@Specialization(guards = "cachedObject.equals(object)", limit = "1", assumptions = "singleContextAssumption()")
893-
static PythonAbstractClass getNativeClassCached(@SuppressWarnings("unused") PythonNativeObject object,
894-
@Exclusive @Cached("object") @SuppressWarnings("unused") PythonNativeObject cachedObject,
895-
@Exclusive @Cached(value = "getNativeClassUncached(cachedObject)", allowUncached = true) PythonAbstractClass cachedClass) {
896-
// TODO same as for 'getNativeClassCachedIdentity'
897-
return cachedClass;
898-
}
899-
900-
@Specialization(replaces = "getNativeClassCached")
901-
static PythonAbstractClass getNativeClass(PythonNativeObject object,
902-
@Exclusive @Cached PCallCapiFunction callGetObTypeNode,
903-
@Exclusive @Cached ToJavaNode toJavaNode) {
904-
// do not convert wrap 'object.object' since that is really the native pointer
905-
// object
906-
return (PythonAbstractClass) toJavaNode.execute(callGetObTypeNode.call(FUN_GET_OB_TYPE, object.getPtr()));
907-
}
908-
909-
protected static PythonAbstractClass getNativeClassUncached(PythonNativeObject object) {
910-
// do not convert wrap 'object.object' since that is really the native pointer
911-
// object
912-
return getNativeClass(object, PCallCapiFunction.getUncached(), ToJavaNode.getUncached());
913-
}
914-
}
915-
916-
private static final class Uncached extends GetNativeClassNode {
917-
private static final Uncached INSTANCE = new Uncached();
918-
919-
@TruffleBoundary
920-
@Override
921-
public PythonAbstractClass execute(PythonNativeObject object) {
922-
return GetNativeClassCachedNode.getNativeClassUncached(object);
923-
}
924-
925-
@Override
926-
public NodeCost getCost() {
927-
return NodeCost.MEGAMORPHIC;
928-
}
929-
930-
@Override
931-
public boolean isAdoptable() {
932-
return false;
933-
}
934-
935-
}
936-
937-
public static GetNativeClassNode create() {
938-
return GetNativeClassCachedNodeGen.create();
939-
}
940-
941-
public static GetNativeClassNode getUncached() {
942-
return Uncached.INSTANCE;
943-
}
944-
}
945-
946873
// -----------------------------------------------------------------------------------------------------------------
947874
@GenerateUncached
948875
public abstract static class SizeofWCharNode extends CExtBaseNode {
@@ -1732,35 +1659,6 @@ void doGeneric(PythonAbstractObject obj, Object ptr,
17321659
}
17331660
}
17341661

1735-
// -----------------------------------------------------------------------------------------------------------------
1736-
@GenerateUncached
1737-
public abstract static class GetObjectDictNode extends CExtBaseNode {
1738-
public abstract Object execute(Object self);
1739-
1740-
@Specialization
1741-
public Object execute(Object self,
1742-
@Exclusive @Cached ToSulongNode toSulong,
1743-
@Exclusive @Cached ToJavaNode toJava,
1744-
@CachedLibrary(limit = "1") InteropLibrary interopLibrary,
1745-
@Exclusive @Cached ImportCAPISymbolNode importCAPISymbolNode) {
1746-
try {
1747-
Object func = importCAPISymbolNode.execute(FUN_PY_OBJECT_GENERIC_GET_DICT);
1748-
return toJava.execute(interopLibrary.execute(func, toSulong.execute(self)));
1749-
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
1750-
CompilerDirectives.transferToInterpreter();
1751-
throw new IllegalStateException("could not run our core function to get the dict of a native object", e);
1752-
}
1753-
}
1754-
1755-
public static GetObjectDictNode create() {
1756-
return CExtNodesFactory.GetObjectDictNodeGen.create();
1757-
}
1758-
1759-
public static GetObjectDictNode getUncached() {
1760-
return CExtNodesFactory.GetObjectDictNodeGen.getUncached();
1761-
}
1762-
}
1763-
17641662
@GenerateUncached
17651663
@TypeSystemReference(PythonTypes.class)
17661664
public abstract static class GetTypeMemberNode extends CExtBaseNode {

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,42 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.cext;
4242

43+
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_GET_OB_TYPE;
44+
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_PY_OBJECT_GENERIC_GET_DICT;
45+
4346
import java.util.Objects;
4447

48+
import com.oracle.graal.python.PythonLanguage;
49+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4550
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
51+
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ImportCAPISymbolNode;
52+
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.PCallCapiFunction;
53+
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ToJavaNode;
54+
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ToSulongNode;
55+
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
56+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
57+
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
58+
import com.oracle.graal.python.nodes.PRaiseNode;
59+
import com.oracle.truffle.api.Assumption;
4660
import com.oracle.truffle.api.CompilerAsserts;
4761
import com.oracle.truffle.api.CompilerDirectives;
62+
import com.oracle.truffle.api.dsl.Cached;
63+
import com.oracle.truffle.api.dsl.Cached.Exclusive;
64+
import com.oracle.truffle.api.dsl.Cached.Shared;
65+
import com.oracle.truffle.api.dsl.GenerateUncached;
66+
import com.oracle.truffle.api.dsl.Specialization;
67+
import com.oracle.truffle.api.interop.ArityException;
68+
import com.oracle.truffle.api.interop.InteropLibrary;
4869
import com.oracle.truffle.api.interop.TruffleObject;
70+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
71+
import com.oracle.truffle.api.interop.UnsupportedTypeException;
72+
import com.oracle.truffle.api.library.CachedLibrary;
73+
import com.oracle.truffle.api.library.ExportLibrary;
74+
import com.oracle.truffle.api.library.ExportMessage;
4975
import com.oracle.truffle.api.object.Shape;
5076
import com.oracle.truffle.api.profiles.ValueProfile;
5177

78+
@ExportLibrary(PythonObjectLibrary.class)
5279
public class PythonAbstractNativeObject extends PythonAbstractObject implements PythonNativeObject, PythonNativeClass {
5380

5481
public final TruffleObject object;
@@ -111,4 +138,81 @@ public String toString() {
111138
CompilerAsserts.neverPartOfCompilation();
112139
return String.format("PythonAbstractNativeObject(%s)", object);
113140
}
141+
142+
@ExportMessage
143+
@SuppressWarnings("static-method")
144+
public boolean hasDict() {
145+
return true;
146+
}
147+
148+
@ExportMessage
149+
@GenerateUncached
150+
public static abstract class GetDict {
151+
@Specialization
152+
public PHashingCollection getNativeDictionary(Object self,
153+
@Cached PRaiseNode raiseNode,
154+
@Exclusive @Cached ToSulongNode toSulong,
155+
@Exclusive @Cached ToJavaNode toJava,
156+
@CachedLibrary(limit = "1") InteropLibrary interopLibrary,
157+
@Exclusive @Cached ImportCAPISymbolNode importCAPISymbolNode) {
158+
try {
159+
Object func = importCAPISymbolNode.execute(FUN_PY_OBJECT_GENERIC_GET_DICT);
160+
Object javaDict = toJava.execute(interopLibrary.execute(func, toSulong.execute(self)));
161+
if (javaDict instanceof PHashingCollection) {
162+
return (PHashingCollection) javaDict;
163+
} else {
164+
throw raiseNode.raise(PythonBuiltinClassType.TypeError, "__dict__ must have been set to a dictionary, not a '%p'", javaDict);
165+
}
166+
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
167+
CompilerDirectives.transferToInterpreter();
168+
throw new IllegalStateException("could not run our core function to get the dict of a native object", e);
169+
}
170+
}
171+
}
172+
173+
@ExportMessage
174+
@GenerateUncached
175+
@SuppressWarnings("unused")
176+
public static abstract class GetLazyPythonClass {
177+
public static Assumption getSingleContextAssumption() {
178+
return PythonLanguage.getCurrent().singleContextAssumption;
179+
}
180+
181+
@Specialization(guards = "object == cachedObject", limit = "1", assumptions = "singleContextAssumption")
182+
public static PythonAbstractClass getNativeClassCachedIdentity(PythonAbstractNativeObject object,
183+
@Shared("assumption") @Cached(value = "getSingleContextAssumption()") Assumption singleContextAssumption,
184+
@Exclusive @Cached("object") PythonNativeObject cachedObject,
185+
@Exclusive @Cached("getNativeClassUncached(cachedObject)") PythonAbstractClass cachedClass) {
186+
// TODO: (tfel) is this really something we can do? It's so rare for this class to
187+
// change that it shouldn't be worth the effort, but in native code, anything can
188+
// happen. OTOH, CPython also has caches that can become invalid when someone just
189+
// goes and changes the ob_type of an object.
190+
return cachedClass;
191+
}
192+
193+
@Specialization(guards = "cachedObject.equals(object)", limit = "1", assumptions = "singleContextAssumption")
194+
public static PythonAbstractClass getNativeClassCached(PythonAbstractNativeObject object,
195+
@Shared("assumption") @Cached(value = "getSingleContextAssumption()") Assumption singleContextAssumption,
196+
@Exclusive @Cached("object") PythonNativeObject cachedObject,
197+
@Exclusive @Cached("getNativeClassUncached(cachedObject)") PythonAbstractClass cachedClass) {
198+
// TODO same as for 'getNativeClassCachedIdentity'
199+
return cachedClass;
200+
}
201+
202+
@Specialization(replaces = {"getNativeClassCached", "getNativeClassCachedIdentity"})
203+
public static PythonAbstractClass getNativeClass(PythonAbstractNativeObject object,
204+
@Exclusive @Cached PCallCapiFunction callGetObTypeNode,
205+
@Exclusive @Cached ToJavaNode toJavaNode) {
206+
// do not convert wrap 'object.object' since that is really the native pointer
207+
// object
208+
return (PythonAbstractClass) toJavaNode.execute(callGetObTypeNode.call(FUN_GET_OB_TYPE, object.getPtr()));
209+
}
210+
211+
@Specialization(replaces = "getNativeClass")
212+
public static PythonAbstractClass getNativeClassUncached(PythonAbstractNativeObject object) {
213+
// do not convert wrap 'object.object' since that is really the native pointer
214+
// object
215+
return getNativeClass(object, PCallCapiFunction.getUncached(), ToJavaNode.getUncached());
216+
}
217+
}
114218
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/ObjectBuiltins.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
import com.oracle.graal.python.builtins.objects.cext.CExtNodes;
6363
import com.oracle.graal.python.builtins.objects.cext.PythonAbstractNativeObject;
6464
import com.oracle.graal.python.builtins.objects.cext.PythonNativeClass;
65-
import com.oracle.graal.python.builtins.objects.cext.PythonNativeObject;
6665
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
6766
import com.oracle.graal.python.builtins.objects.dict.PDict;
6867
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
@@ -598,11 +597,11 @@ Object dict(PythonObject self, PDict dict,
598597
return PNone.NONE;
599598
}
600599

601-
@Specialization(guards = "isNoValue(none)")
602-
Object dict(PythonNativeObject self, @SuppressWarnings("unused") PNone none,
603-
@Cached("create()") CExtNodes.GetObjectDictNode getDictNode) {
604-
Object dict = getDictNode.execute(self);
605-
if (dict == PNone.NO_VALUE) {
600+
@Specialization(guards = "isNoValue(none)", limit = "1")
601+
Object dict(PythonAbstractNativeObject self, @SuppressWarnings("unused") PNone none,
602+
@CachedLibrary("self") PythonObjectLibrary lib) {
603+
PHashingCollection dict = lib.getDict(self);
604+
if (dict == null) {
606605
raise(self, none);
607606
}
608607
return dict;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/PythonObject.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
3838
import com.oracle.graal.python.builtins.objects.type.PythonManagedClass;
3939
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
40+
import com.oracle.graal.python.nodes.PRaiseNode;
4041
import com.oracle.graal.python.nodes.attributes.WriteAttributeToDynamicObjectNode;
4142
import com.oracle.truffle.api.Assumption;
4243
import com.oracle.truffle.api.CompilerAsserts;
@@ -54,6 +55,7 @@
5455
import com.oracle.truffle.api.object.Property;
5556
import com.oracle.truffle.api.object.Shape;
5657
import com.oracle.truffle.api.object.dsl.Layout;
58+
import com.oracle.truffle.api.profiles.BranchProfile;
5759

5860
@ExportLibrary(PythonObjectLibrary.class)
5961
public class PythonObject extends PythonAbstractObject {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/PythonObjectLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void setDict(PythonAbstractObject receiver, PHashingCollection dict) thro
6666
throw UnsupportedMessageException.create();
6767
}
6868

69-
public abstract void getLazyPythonClass(PythonAbstractObject receiver);
69+
public abstract LazyPythonClass getLazyPythonClass(PythonAbstractObject receiver);
7070

7171
public abstract void setLazyPythonClass(PythonAbstractObject receiver, LazyPythonClass cls);
7272

0 commit comments

Comments
 (0)