Skip to content

Commit 21967c7

Browse files
committed
generalize many builtins
1 parent e611352 commit 21967c7

File tree

13 files changed

+297
-570
lines changed

13 files changed

+297
-570
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,19 @@ public static boolean fitLong(double value) {
5050
return Long.MIN_VALUE <= value && value <= Long.MAX_VALUE;
5151
}
5252

53+
public static boolean fitInt(double value) {
54+
return Integer.MIN_VALUE <= value && value <= Integer.MAX_VALUE;
55+
}
56+
5357
public static boolean isNumber(Object value) {
5458
return isInteger(value) || value instanceof Float || value instanceof Double || value instanceof PFloat;
5559
}
5660

5761
public static boolean isInteger(Object value) {
5862
return value instanceof Integer || value instanceof Long || value instanceof PInt || value instanceof Boolean;
5963
}
64+
65+
public static boolean isFloat(Object value) {
66+
return value instanceof Double || value instanceof PFloat;
67+
}
6068
}

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

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import java.math.BigDecimal;
3434
import java.math.BigInteger;
35+
import java.math.MathContext;
3536
import java.util.List;
3637

3738
import com.oracle.graal.python.builtins.Builtin;
@@ -41,7 +42,6 @@
4142
import com.oracle.graal.python.builtins.objects.floats.PFloat;
4243
import com.oracle.graal.python.builtins.objects.ints.PInt;
4344
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
44-
import com.oracle.graal.python.nodes.PNode;
4545
import com.oracle.graal.python.nodes.SpecialMethodNames;
4646
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
4747
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
@@ -62,7 +62,6 @@
6262
import com.oracle.truffle.api.dsl.Specialization;
6363
import com.oracle.truffle.api.dsl.TypeSystemReference;
6464
import com.oracle.truffle.api.profiles.ConditionProfile;
65-
import java.math.MathContext;
6665

6766
@CoreFunctions(defineModule = "math")
6867
public class MathModuleBuiltins extends PythonBuiltins {
@@ -153,7 +152,7 @@ public double sqrt(Object value,
153152
}
154153

155154
public static SqrtNode create() {
156-
return MathModuleBuiltinsFactory.SqrtNodeFactory.create(new PNode[0]);
155+
return MathModuleBuiltinsFactory.SqrtNodeFactory.create();
157156
}
158157
}
159158

@@ -200,10 +199,7 @@ public long ceil(long value) {
200199

201200
@Specialization
202201
public int ceil(boolean value) {
203-
if (value) {
204-
return 1;
205-
}
206-
return 0;
202+
return value ? 1 : 0;
207203
}
208204

209205
@Specialization
@@ -212,10 +208,10 @@ public Object ceil(PFloat value,
212208
@Cached("create(__CEIL__)") LookupAndCallUnaryNode dispatchCeil) {
213209
Object result = dispatchCeil.executeObject(value);
214210
if (PNone.NO_VALUE.equals(result)) {
215-
if (value.getValue() <= Long.MAX_VALUE) {
216-
result = Math.ceil(value.getValue());
211+
if (MathGuards.fitLong(value.getValue())) {
212+
return ceilLong(value.getValue());
217213
} else {
218-
result = factory().createInt(BigDecimal.valueOf(Math.ceil(value.getValue())).toBigInteger());
214+
return ceil(value.getValue());
219215
}
220216
}
221217
return result;
@@ -745,7 +741,7 @@ public boolean isinf(Object value,
745741
}
746742

747743
protected static IsNanNode create() {
748-
return MathModuleBuiltinsFactory.IsNanNodeFactory.create(new PNode[0]);
744+
return MathModuleBuiltinsFactory.IsNanNodeFactory.create();
749745
}
750746
}
751747

@@ -940,7 +936,7 @@ public double acos(Object value,
940936
}
941937

942938
protected static AcosNode create() {
943-
return MathModuleBuiltinsFactory.AcosNodeFactory.create(new PNode[0]);
939+
return MathModuleBuiltinsFactory.AcosNodeFactory.create();
944940
}
945941
}
946942

@@ -993,7 +989,7 @@ public double acosh(Object value,
993989
}
994990

995991
protected static AcoshNode create() {
996-
return MathModuleBuiltinsFactory.AcoshNodeFactory.create(new PNode[0]);
992+
return MathModuleBuiltinsFactory.AcoshNodeFactory.create();
997993
}
998994
}
999995

@@ -1039,7 +1035,7 @@ public double asin(Object value,
10391035
}
10401036

10411037
protected static AsinNode create() {
1042-
return MathModuleBuiltinsFactory.AsinNodeFactory.create(new PNode[0]);
1038+
return MathModuleBuiltinsFactory.AsinNodeFactory.create();
10431039
}
10441040
}
10451041

@@ -1079,7 +1075,7 @@ public double cosh(Object value,
10791075
}
10801076

10811077
protected static CosNode create() {
1082-
return MathModuleBuiltinsFactory.CosNodeFactory.create(new PNode[0]);
1078+
return MathModuleBuiltinsFactory.CosNodeFactory.create();
10831079
}
10841080
}
10851081

@@ -1124,7 +1120,7 @@ public double coshO(Object value,
11241120
}
11251121

11261122
protected static CoshNode create() {
1127-
return MathModuleBuiltinsFactory.CoshNodeFactory.create(new PNode[0]);
1123+
return MathModuleBuiltinsFactory.CoshNodeFactory.create();
11281124
}
11291125
}
11301126

@@ -1164,7 +1160,7 @@ public double cosh(Object value,
11641160
}
11651161

11661162
protected static SinNode create() {
1167-
return MathModuleBuiltinsFactory.SinNodeFactory.create(new PNode[0]);
1163+
return MathModuleBuiltinsFactory.SinNodeFactory.create();
11681164
}
11691165
}
11701166

@@ -1209,7 +1205,7 @@ public double sinhO(Object value,
12091205
}
12101206

12111207
protected static SinhNode create() {
1212-
return MathModuleBuiltinsFactory.SinhNodeFactory.create(new PNode[0]);
1208+
return MathModuleBuiltinsFactory.SinhNodeFactory.create();
12131209
}
12141210
}
12151211

@@ -1249,7 +1245,7 @@ public double tanO(Object value,
12491245
}
12501246

12511247
protected static TanNode create() {
1252-
return MathModuleBuiltinsFactory.TanNodeFactory.create(new PNode[0]);
1248+
return MathModuleBuiltinsFactory.TanNodeFactory.create();
12531249
}
12541250
}
12551251

@@ -1289,7 +1285,7 @@ public double tanhO(Object value,
12891285
}
12901286

12911287
protected static TanhNode create() {
1292-
return MathModuleBuiltinsFactory.TanhNodeFactory.create(new PNode[0]);
1288+
return MathModuleBuiltinsFactory.TanhNodeFactory.create();
12931289
}
12941290
}
12951291

@@ -1329,7 +1325,7 @@ public double atanO(Object value,
13291325
}
13301326

13311327
protected static AtanNode create() {
1332-
return MathModuleBuiltinsFactory.AtanNodeFactory.create(new PNode[0]);
1328+
return MathModuleBuiltinsFactory.AtanNodeFactory.create();
13331329
}
13341330
}
13351331

@@ -1376,7 +1372,7 @@ public double atanhO(Object value,
13761372
}
13771373

13781374
protected static AtanhNode create() {
1379-
return MathModuleBuiltinsFactory.AtanhNodeFactory.create(new PNode[0]);
1375+
return MathModuleBuiltinsFactory.AtanhNodeFactory.create();
13801376
}
13811377
}
13821378

@@ -1420,7 +1416,7 @@ public double asinhO(Object value,
14201416
}
14211417

14221418
protected static AsinhNode create() {
1423-
return MathModuleBuiltinsFactory.AsinhNodeFactory.create(new PNode[0]);
1419+
return MathModuleBuiltinsFactory.AsinhNodeFactory.create();
14241420
}
14251421
}
14261422

@@ -1459,7 +1455,7 @@ public boolean isinf(Object value,
14591455
}
14601456

14611457
protected static IsFiniteNode create() {
1462-
return MathModuleBuiltinsFactory.IsFiniteNodeFactory.create(new PNode[0]);
1458+
return MathModuleBuiltinsFactory.IsFiniteNodeFactory.create();
14631459
}
14641460
}
14651461

@@ -1498,15 +1494,15 @@ public boolean isinf(Object value,
14981494
}
14991495

15001496
protected static IsInfNode create() {
1501-
return MathModuleBuiltinsFactory.IsInfNodeFactory.create(new PNode[0]);
1497+
return MathModuleBuiltinsFactory.IsInfNodeFactory.create();
15021498
}
15031499
}
15041500

15051501
@Builtin(name = "log", minNumOfArguments = 1, maxNumOfArguments = 2)
15061502
@TypeSystemReference(PythonArithmeticTypes.class)
15071503
@ImportStatic(MathGuards.class)
15081504
@GenerateNodeFactory
1509-
public abstract static class LogNode extends PythonUnaryBuiltinNode {
1505+
public abstract static class LogNode extends PythonBinaryBuiltinNode {
15101506

15111507
@Child private LookupAndCallUnaryNode valueDispatchNode;
15121508
@Child private LookupAndCallUnaryNode baseDispatchNode;
@@ -1718,13 +1714,13 @@ private Object getRealNumber(Object object, LookupAndCallUnaryNode dispatchNode,
17181714
}
17191715

17201716
public static LogNode create() {
1721-
return MathModuleBuiltinsFactory.LogNodeFactory.create(new PNode[0]);
1717+
return MathModuleBuiltinsFactory.LogNodeFactory.create();
17221718
}
17231719
}
17241720

17251721
@Builtin(name = "fabs", fixedNumOfArguments = 1)
17261722
@GenerateNodeFactory
1727-
public abstract static class fabsNode extends PythonBuiltinNode {
1723+
public abstract static class FabsNode extends PythonBuiltinNode {
17281724

17291725
@Specialization
17301726
public double fabs(int value) {
@@ -1879,7 +1875,7 @@ public abstract static class PowNode extends PythonBinaryBuiltinNode {
18791875
}
18801876

18811877
public static PowNode create() {
1882-
return MathModuleBuiltinsFactory.PowNodeFactory.create(new PNode[0]);
1878+
return MathModuleBuiltinsFactory.PowNodeFactory.create();
18831879
}
18841880
}
18851881

@@ -1969,7 +1965,7 @@ public abstract static class Atan2Node extends PythonBinaryBuiltinNode {
19691965
}
19701966

19711967
protected Atan2Node create() {
1972-
return MathModuleBuiltinsFactory.Atan2NodeFactory.create(new PNode[0]);
1968+
return MathModuleBuiltinsFactory.Atan2NodeFactory.create();
19731969
}
19741970
}
19751971
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
8181
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
8282
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
83-
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
83+
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
8484
import com.oracle.graal.python.runtime.PythonCore;
8585
import com.oracle.graal.python.runtime.exception.PException;
8686
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -373,7 +373,7 @@ Object setInheritable(int fd, @SuppressWarnings("unused") Object inheritable) {
373373

374374
@Builtin(name = "stat", minNumOfArguments = 1, maxNumOfArguments = 2)
375375
@GenerateNodeFactory
376-
public abstract static class StatNode extends PythonUnaryBuiltinNode {
376+
public abstract static class StatNode extends PythonBinaryBuiltinNode {
377377
private static final int S_IFIFO = 0010000;
378378
private static final int S_IFCHR = 0020000;
379379
private static final int S_IFBLK = 0060000;
@@ -501,7 +501,7 @@ Object stat(String path, boolean followSymlinks) {
501501
}
502502

503503
protected static StatNode create() {
504-
return StatNodeFactory.create(null);
504+
return StatNodeFactory.create();
505505
}
506506
}
507507

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@
3737
import com.oracle.graal.python.builtins.objects.PNone;
3838
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
3939
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
40+
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
4041
import com.oracle.truffle.api.CompilerDirectives;
4142
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
4243
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4344
import com.oracle.truffle.api.TruffleOptions;
4445
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
4546
import com.oracle.truffle.api.dsl.Specialization;
47+
import com.oracle.truffle.api.dsl.TypeSystemReference;
4648

4749
@CoreFunctions(defineModule = "time")
4850
public final class TimeModuleBuiltins extends PythonBuiltins {
@@ -81,6 +83,7 @@ private static Object[] getTimeStruct(double seconds, boolean local) {
8183
// time.gmtime([seconds])
8284
@Builtin(name = "__truffle_gmtime_tuple__", fixedNumOfArguments = 1)
8385
@GenerateNodeFactory
86+
@TypeSystemReference(PythonArithmeticTypes.class)
8487
public abstract static class PythonGMTimeNode extends PythonBuiltinNode {
8588
@Specialization
8689
public PTuple gmtime(double seconds) {
@@ -91,6 +94,7 @@ public PTuple gmtime(double seconds) {
9194
// time.localtime([seconds])
9295
@Builtin(name = "__truffle_localtime_tuple__", fixedNumOfArguments = 1)
9396
@GenerateNodeFactory
97+
@TypeSystemReference(PythonArithmeticTypes.class)
9498
public abstract static class PythonLocalTimeNode extends PythonBuiltinNode {
9599
@Specialization
96100
public PTuple localtime(double seconds) {
@@ -157,6 +161,7 @@ public abstract static class PythonClockNode extends PythonBuiltinNode {
157161

158162
@Builtin(name = "sleep", fixedNumOfArguments = 1)
159163
@GenerateNodeFactory
164+
@TypeSystemReference(PythonArithmeticTypes.class)
160165
abstract static class SleepNode extends PythonBuiltinNode {
161166
// see: https://github.com/python/cpython/blob/master/Modules/timemodule.c#L1741
162167

0 commit comments

Comments
 (0)