Skip to content

Commit 0ca9b77

Browse files
committed
Leverage wrapper type in 'GetDynamicTypeNode'.
1 parent ba49d28 commit 0ca9b77

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public static class PrimitiveNativeWrapper extends DynamicObjectNativeWrapper {
167167
public static final byte PRIMITIVE_STATE_BYTE = 0b00000010;
168168
public static final byte PRIMITIVE_STATE_INT = 0b00000100;
169169
public static final byte PRIMITIVE_STATE_LONG = 0b00001000;
170-
public static final byte PRIMITIVE_STATE_DOUBLE = 0b00001000;
170+
public static final byte PRIMITIVE_STATE_DOUBLE = 0b00010000;
171171

172172
private final byte state;
173173
private final long value;
@@ -230,6 +230,10 @@ public boolean isDouble() {
230230
return state == PRIMITIVE_STATE_DOUBLE;
231231
}
232232

233+
public boolean isIntLike() {
234+
return (state & (PRIMITIVE_STATE_BYTE | PRIMITIVE_STATE_INT | PRIMITIVE_STATE_LONG)) != 0;
235+
}
236+
233237
// this method exists just for readability
234238
public Object getMaterializedObject() {
235239
return getDelegate();

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

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.MaterializeDelegateNode;
5454
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ToJavaNode;
5555
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ToSulongNode;
56-
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.BoolNativeWrapper;
5756
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.DynamicObjectNativeWrapper;
57+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PrimitiveNativeWrapper;
5858
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PySequenceArrayWrapper;
5959
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PyUnicodeData;
6060
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PyUnicodeState;
@@ -65,6 +65,7 @@
6565
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMRFactory.GetSulongTypeNodeGen;
6666
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMRFactory.InvalidateNativeObjectsAllManagedNodeGen;
6767
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMRFactory.PAsPointerNodeGen;
68+
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMRFactory.PGetDynamicTypeNodeGen;
6869
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMRFactory.ReadNativeMemberNodeGen;
6970
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMRFactory.ToPyObjectNodeGen;
7071
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMRFactory.WriteNativeMemberNodeGen;
@@ -140,12 +141,57 @@ public class PythonObjectNativeWrapperMR {
140141
@SuppressWarnings("unknown-message")
141142
@Resolve(message = "com.oracle.truffle.llvm.spi.GetDynamicType")
142143
abstract static class GetDynamicTypeNode extends Node {
144+
@Child private PGetDynamicTypeNode getDynamicTypeNode = PGetDynamicTypeNode.create();
145+
146+
public Object access(PythonNativeWrapper object) {
147+
return getDynamicTypeNode.execute(object);
148+
}
149+
}
150+
151+
abstract static class PGetDynamicTypeNode extends PNodeWithContext {
143152
@Child private GetLazyClassNode getLazyClassNode = GetLazyClassNode.create();
144153
@Child private GetSulongTypeNode getSulongTypeNode = GetSulongTypeNode.create();
145154
@Child private AsPythonObjectNode getDelegate = AsPythonObjectNode.create();
146155

147-
public Object access(PythonNativeWrapper object) {
148-
return getSulongTypeNode.execute(getLazyClassNode.execute(getDelegate.execute(object)));
156+
public abstract Object execute(PythonNativeWrapper obj);
157+
158+
@Specialization(guards = "obj.isIntLike()")
159+
Object doIntLike(@SuppressWarnings("unused") PrimitiveNativeWrapper obj,
160+
@Cached("getLongobjectType()") Object cachedSulongType) {
161+
return cachedSulongType;
162+
}
163+
164+
@Specialization(guards = "obj.isBool()")
165+
Object doBool(@SuppressWarnings("unused") PrimitiveNativeWrapper obj,
166+
@Cached("getBoolobjectType()") Object cachedSulongType) {
167+
return cachedSulongType;
168+
}
169+
170+
@Specialization(guards = "obj.isDouble()")
171+
Object doDouble(@SuppressWarnings("unused") PrimitiveNativeWrapper obj,
172+
@Cached("getFloatobjectType()") Object cachedSulongType) {
173+
return cachedSulongType;
174+
}
175+
176+
@Specialization
177+
Object doGeneric(PythonNativeWrapper obj) {
178+
return getSulongTypeNode.execute(getLazyClassNode.execute(getDelegate.execute(obj)));
179+
}
180+
181+
protected Object getLongobjectType() {
182+
return getSulongTypeNode.execute(PythonBuiltinClassType.PInt);
183+
}
184+
185+
protected Object getBoolobjectType() {
186+
return getSulongTypeNode.execute(PythonBuiltinClassType.Boolean);
187+
}
188+
189+
protected Object getFloatobjectType() {
190+
return getSulongTypeNode.execute(PythonBuiltinClassType.PFloat);
191+
}
192+
193+
public static PGetDynamicTypeNode create() {
194+
return PGetDynamicTypeNodeGen.create();
149195
}
150196
}
151197

@@ -973,17 +1019,17 @@ abstract static class PAsPointerNode extends PNodeWithContext {
9731019

9741020
public abstract long execute(PythonNativeWrapper o);
9751021

976-
@Specialization(guards = "!obj.isNative()")
977-
long doBoolNotNative(BoolNativeWrapper obj,
1022+
@Specialization(guards = {"obj.isBool()", "!obj.isNative()"})
1023+
long doBoolNotNative(PrimitiveNativeWrapper obj,
9781024
@Cached("create()") MaterializeDelegateNode materializeNode) {
9791025
// special case for True and False singletons
9801026
PInt boxed = (PInt) materializeNode.execute(obj);
9811027
assert obj.getNativePointer() == boxed.getNativeWrapper().getNativePointer();
9821028
return doFast(obj);
9831029
}
9841030

985-
@Specialization(guards = "obj.isNative()")
986-
long doBoolNative(BoolNativeWrapper obj) {
1031+
@Specialization(guards = {"obj.isBool()", "obj.isNative()"})
1032+
long doBoolNative(PrimitiveNativeWrapper obj) {
9871033
return doFast(obj);
9881034
}
9891035

@@ -1011,7 +1057,7 @@ private long ensureLong(Object nativePointer) {
10111057
}
10121058

10131059
protected static boolean isBoolNativeWrapper(Object obj) {
1014-
return obj instanceof BoolNativeWrapper;
1060+
return obj instanceof PrimitiveNativeWrapper && ((PrimitiveNativeWrapper) obj).isBool();
10151061
}
10161062

10171063
public static PAsPointerNode create() {

0 commit comments

Comments
 (0)