Skip to content

Commit 015d59e

Browse files
committed
Use PyNumberFloat where appropriate
1 parent d171e5d commit 015d59e

File tree

4 files changed

+23
-45
lines changed

4 files changed

+23
-45
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -888,11 +888,9 @@ public Object reversed(VirtualFrame frame, Object cls, Object sequence,
888888
@Builtin(name = FLOAT, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, constructsClass = PythonBuiltinClassType.PFloat)
889889
@GenerateNodeFactory
890890
@ReportPolymorphism
891-
public abstract static class FloatNode extends PythonBinaryBuiltinNode {
891+
abstract static class FloatNode extends PythonBinaryBuiltinNode {
892892
@Child private IsBuiltinClassProfile isPrimitiveProfile = IsBuiltinClassProfile.create();
893893

894-
public abstract Object executeWith(VirtualFrame frame, Object cls, Object arg);
895-
896894
// Used for the recursive call
897895
protected abstract double executeDouble(VirtualFrame frame, PythonBuiltinClassType cls, Object arg) throws UnexpectedResultException;
898896

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

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@
140140
import com.oracle.graal.python.builtins.objects.cext.capi.UnicodeObjectNodesFactory.UnicodeAsWideCharNodeGen;
141141
import com.oracle.graal.python.builtins.objects.cext.common.CExtAsPythonObjectNode;
142142
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes;
143-
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.AsNativeDoubleNode;
144143
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.Charsets;
145144
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.CheckFunctionResultNode;
146145
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.ConvertPIntToPrimitiveNode;
@@ -193,7 +192,9 @@
193192
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
194193
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetMroStorageNode;
195194
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
195+
import com.oracle.graal.python.lib.PyFloatAsDoubleNode;
196196
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
197+
import com.oracle.graal.python.lib.PyNumberFloatNode;
197198
import com.oracle.graal.python.nodes.BuiltinNames;
198199
import com.oracle.graal.python.nodes.ErrorMessages;
199200
import com.oracle.graal.python.nodes.PGuards;
@@ -2375,25 +2376,13 @@ static double doDoubleNativeWrapper(DynamicObjectNativeWrapper.PrimitiveNativeWr
23752376
return object.getDouble();
23762377
}
23772378

2378-
@Specialization(rewriteOn = PException.class)
2379-
double doGeneric(VirtualFrame frame, Object object,
2380-
@Shared("asPythonObjectNode") @Cached AsPythonObjectNode asPythonObjectNode,
2381-
@Shared("asDoubleNode") @Cached AsNativeDoubleNode asDoubleNode) {
2382-
Object state = IndirectCallContext.enter(frame, getContext(), this);
2383-
try {
2384-
return asDoubleNode.executeDouble(asPythonObjectNode.execute(object));
2385-
} finally {
2386-
IndirectCallContext.exit(frame, getContext(), state);
2387-
}
2388-
}
2389-
2390-
@Specialization(replaces = "doGeneric")
2379+
@Specialization
23912380
double doGenericErr(VirtualFrame frame, Object object,
2392-
@Shared("asPythonObjectNode") @Cached AsPythonObjectNode asPythonObjectNode,
2393-
@Shared("asDoubleNode") @Cached AsNativeDoubleNode asDoubleNode,
2381+
@Cached AsPythonObjectNode asPythonObjectNode,
2382+
@Cached PyFloatAsDoubleNode asDoubleNode,
23942383
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
23952384
try {
2396-
return doGeneric(frame, object, asPythonObjectNode, asDoubleNode);
2385+
return asDoubleNode.execute(frame, asPythonObjectNode.execute(object));
23972386
} catch (PException e) {
23982387
transformExceptionToNativeNode.execute(frame, e);
23992388
return -1.0;
@@ -2406,8 +2395,6 @@ static double doDoubleNativeWrapper(DynamicObjectNativeWrapper.PrimitiveNativeWr
24062395
@GenerateNodeFactory
24072396
abstract static class PyNumberFloat extends NativeBuiltin {
24082397

2409-
@Child private BuiltinConstructors.FloatNode floatNode;
2410-
24112398
@Specialization(guards = "object.isDouble()")
24122399
static Object doDoubleNativeWrapper(@SuppressWarnings("unused") Object module, PrimitiveNativeWrapper object,
24132400
@Cached AddRefCntNode refCntNode) {
@@ -2420,24 +2407,14 @@ static Object doLongNativeWrapper(@SuppressWarnings("unused") Object module, Pri
24202407
return primitiveToSulongNode.execute((double) object.getLong());
24212408
}
24222409

2423-
@Specialization(rewriteOn = PException.class)
2424-
Object doGeneric(VirtualFrame frame, @SuppressWarnings("unused") Object module, Object object,
2425-
@Shared("toNewRefNode") @Cached ToNewRefNode toNewRefNode,
2426-
@Shared("asPythonObjectNode") @Cached AsPythonObjectNode asPythonObjectNode) {
2427-
if (floatNode == null) {
2428-
CompilerDirectives.transferToInterpreterAndInvalidate();
2429-
floatNode = insert(BuiltinConstructorsFactory.FloatNodeFactory.create());
2430-
}
2431-
return toNewRefNode.execute(floatNode.executeWith(frame, PythonBuiltinClassType.PFloat, asPythonObjectNode.execute(object)));
2432-
}
2433-
2434-
@Specialization(replaces = "doGeneric")
2435-
Object doGenericErr(VirtualFrame frame, Object module, Object object,
2436-
@Exclusive @Cached GetNativeNullNode getNativeNullNode,
2437-
@Shared("toNewRefNode") @Cached ToNewRefNode toNewRefNode,
2438-
@Shared("asPythonObjectNode") @Cached AsPythonObjectNode asPythonObjectNode) {
2410+
@Specialization
2411+
Object doGeneric(VirtualFrame frame, Object module, Object object,
2412+
@Cached GetNativeNullNode getNativeNullNode,
2413+
@Cached ToNewRefNode toNewRefNode,
2414+
@Cached AsPythonObjectNode asPythonObjectNode,
2415+
@Cached PyNumberFloatNode pyNumberFloat) {
24392416
try {
2440-
return doGeneric(frame, module, object, toNewRefNode, asPythonObjectNode);
2417+
return toNewRefNode.execute(pyNumberFloat.execute(frame, asPythonObjectNode.execute(object)));
24412418
} catch (PException e) {
24422419
transformToNative(frame, e);
24432420
return toNewRefNode.execute(getNativeNullNode.execute(module));

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@
5757
import com.oracle.graal.python.builtins.CoreFunctions;
5858
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5959
import com.oracle.graal.python.builtins.PythonBuiltins;
60-
import com.oracle.graal.python.builtins.modules.BuiltinConstructors;
61-
import com.oracle.graal.python.builtins.modules.BuiltinConstructorsFactory;
6260
import com.oracle.graal.python.builtins.modules.MathGuards;
6361
import com.oracle.graal.python.builtins.objects.PNone;
6462
import com.oracle.graal.python.builtins.objects.PNotImplemented;
@@ -78,6 +76,7 @@
7876
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
7977
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
8078
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
79+
import com.oracle.graal.python.lib.PyNumberFloatNode;
8180
import com.oracle.graal.python.nodes.ErrorMessages;
8281
import com.oracle.graal.python.nodes.PGuards;
8382
import com.oracle.graal.python.nodes.PRaiseNode;
@@ -2559,7 +2558,7 @@ abstract static class ReprNode extends StrNode {
25592558
@ArgumentClinic(name = "format_spec", conversion = ClinicConversion.String)
25602559
@GenerateNodeFactory
25612560
abstract static class FormatNode extends FormatNodeBase {
2562-
@Child private BuiltinConstructors.FloatNode floatNode;
2561+
@Child private PyNumberFloatNode floatNode;
25632562

25642563
@Override
25652564
protected ArgumentClinicProvider getArgumentClinic() {
@@ -2605,10 +2604,10 @@ Object formatPI(VirtualFrame frame, PInt self, String formatString) {
26052604
private double asDouble(VirtualFrame frame, Object self) {
26062605
if (floatNode == null) {
26072606
CompilerDirectives.transferToInterpreterAndInvalidate();
2608-
floatNode = insert(BuiltinConstructorsFactory.FloatNodeFactory.create());
2607+
floatNode = insert(PyNumberFloatNode.create());
26092608
}
2610-
// We cannot use asJavaDouble, because this should have the semantics of PyNumber_Float
2611-
return (double) floatNode.executeWith(frame, PythonBuiltinClassType.PFloat, self);
2609+
// This should have the semantics of PyNumber_Float
2610+
return floatNode.execute(frame, self);
26122611
}
26132612

26142613
private static Spec getSpec(String formatString, PRaiseNode raiseNode) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyNumberFloatNode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,8 @@ public abstract class PyNumberFloatNode extends PNodeWithContext {
130130
}
131131
return fromString.execute(frame, object);
132132
}
133+
134+
public static PyNumberFloatNode create() {
135+
return PyNumberFloatNodeGen.create();
136+
}
133137
}

0 commit comments

Comments
 (0)