Skip to content

Commit 0b0ddc4

Browse files
committed
Refactoring of real and imag nodes.
1 parent 39bb7d4 commit 0b0ddc4

File tree

2 files changed

+41
-63
lines changed

2 files changed

+41
-63
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats/FloatBuiltins.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
7676
import com.oracle.graal.python.runtime.JavaTypeConversions;
7777
import com.oracle.graal.python.runtime.exception.PythonErrorType;
78+
import com.oracle.truffle.api.CompilerDirectives;
7879
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7980
import com.oracle.truffle.api.dsl.Cached;
8081
import com.oracle.truffle.api.dsl.Fallback;
@@ -879,23 +880,29 @@ abstract static class NegNode extends PythonUnaryBuiltinNode {
879880
@GenerateNodeFactory
880881
@Builtin(name = "real", fixedNumOfArguments = 1, isGetter = true, doc = "the real part of a complex number")
881882
static abstract class RealNode extends PythonBuiltinNode {
882-
@Specialization
883-
float get(float self) {
884-
return self;
883+
884+
@Child private GetClassNode getClassNode;
885+
886+
protected PythonClass getClass(Object value) {
887+
if (getClassNode == null) {
888+
CompilerDirectives.transferToInterpreterAndInvalidate();
889+
getClassNode = insert(GetClassNode.create());
890+
}
891+
return getClassNode.execute(value);
885892
}
886-
893+
887894
@Specialization
888895
double get(double self) {
889896
return self;
890897
}
891898

892-
@Specialization
893-
PFloat get(PFloat self,
894-
@Cached("create()") GetClassNode clazzNode) {
895-
PythonClass clazz = clazzNode.execute(self);
896-
if (clazz.isBuiltin()) {
897-
return self;
898-
}
899+
@Specialization (guards ="cannotBeOverridden(getClass(self))")
900+
PFloat getPFloat(PFloat self) {
901+
return self;
902+
}
903+
904+
@Specialization (guards ="!cannotBeOverridden(getClass(self))")
905+
PFloat getPFloatOverriden(PFloat self) {
899906
return factory().createFloat(self.getValue());
900907
}
901908
}
@@ -905,19 +912,10 @@ PFloat get(PFloat self,
905912
static abstract class ImagNode extends PythonBuiltinNode {
906913

907914
@Specialization
908-
float get(@SuppressWarnings("unused") float self) {
909-
return 0;
910-
}
911-
912-
@Specialization
913-
double get(@SuppressWarnings("unused") double self) {
914-
return 0;
915-
}
916-
917-
@Specialization
918-
double get(@SuppressWarnings("unused") PFloat self) {
915+
double get(@SuppressWarnings("unused") Object self) {
919916
return 0;
920917
}
918+
921919
}
922920

923921
@Builtin(name = __GETFORMAT__, fixedNumOfArguments = 2)

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

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
5757
import com.oracle.graal.python.runtime.ArithmeticUtil;
5858
import com.oracle.graal.python.runtime.exception.PythonErrorType;
59+
import com.oracle.truffle.api.CompilerDirectives;
5960
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
60-
import com.oracle.truffle.api.dsl.Cached;
6161
import com.oracle.truffle.api.dsl.Fallback;
6262
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6363
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -1855,6 +1855,17 @@ int bitLength(PInt argument) {
18551855
@GenerateNodeFactory
18561856
@Builtin(name = "real", fixedNumOfArguments = 1, isGetter = true, doc = "the real part of a complex number")
18571857
static abstract class RealNode extends PythonBuiltinNode {
1858+
1859+
@Child private GetClassNode getClassNode;
1860+
1861+
protected PythonClass getClass(Object value) {
1862+
if (getClassNode == null) {
1863+
CompilerDirectives.transferToInterpreterAndInvalidate();
1864+
getClassNode = insert(GetClassNode.create());
1865+
}
1866+
return getClassNode.execute(value);
1867+
}
1868+
18581869
@Specialization
18591870
int get(boolean self) {
18601871
return self ? 1 : 0;
@@ -1870,13 +1881,13 @@ long get(long self) {
18701881
return self;
18711882
}
18721883

1873-
@Specialization
1874-
PInt get(PInt self,
1875-
@Cached("create()") GetClassNode clazzNode) {
1876-
PythonClass clazz = clazzNode.execute(self);
1877-
if (clazz.isBuiltin()) {
1878-
return self;
1879-
}
1884+
@Specialization(guards ="cannotBeOverridden(getClass(self))")
1885+
PInt getPInt(PInt self) {
1886+
return self;
1887+
}
1888+
1889+
@Specialization(guards ="!cannotBeOverridden(getClass(self))")
1890+
PInt getPIntOverriden(PInt self) {
18801891
return factory().createInt(self.getValue());
18811892
}
18821893
}
@@ -1885,22 +1896,7 @@ PInt get(PInt self,
18851896
@Builtin(name = "imag", fixedNumOfArguments = 1, isGetter = true, doc = "the imaginary part of a complex number")
18861897
static abstract class ImagNode extends PythonBuiltinNode {
18871898
@Specialization
1888-
int get(@SuppressWarnings("unused") boolean self) {
1889-
return 0;
1890-
}
1891-
1892-
@Specialization
1893-
int get(@SuppressWarnings("unused") int self) {
1894-
return 0;
1895-
}
1896-
1897-
@Specialization
1898-
int get(@SuppressWarnings("unused") long self) {
1899-
return 0;
1900-
}
1901-
1902-
@Specialization
1903-
int get(@SuppressWarnings("unused") PInt self) {
1899+
int get(@SuppressWarnings("unused") Object self) {
19041900
return 0;
19051901
}
19061902
}
@@ -1915,27 +1911,11 @@ static abstract class NumeratorNode extends RealNode {
19151911
@Builtin(name = "denominator", fixedNumOfArguments = 1, isGetter = true, doc = "the denominator of a rational number in lowest terms")
19161912
static abstract class DenominatorNode extends PythonBuiltinNode {
19171913
@Specialization
1918-
int get(@SuppressWarnings("unused") boolean self) {
1919-
return 1;
1920-
}
1921-
1922-
@Specialization
1923-
int get(@SuppressWarnings("unused") int self) {
1924-
return 1;
1925-
}
1926-
1927-
@Specialization
1928-
int get(@SuppressWarnings("unused") long self) {
1929-
return 1;
1930-
}
1931-
1932-
@Specialization
1933-
int get(@SuppressWarnings("unused") PInt self) {
1914+
int get(@SuppressWarnings("unused") Object self) {
19341915
return 1;
19351916
}
19361917
}
19371918

1938-
19391919
@Builtin(name = SpecialMethodNames.__INT__, fixedNumOfArguments = 1)
19401920
@GenerateNodeFactory
19411921
abstract static class IntNode extends PythonBuiltinNode {

0 commit comments

Comments
 (0)