Skip to content

Commit 2989f88

Browse files
committed
Refactor bin/oct/hex
1 parent 610c23e commit 2989f88

File tree

1 file changed

+74
-87
lines changed

1 file changed

+74
-87
lines changed

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

Lines changed: 74 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@
116116
import com.oracle.graal.python.builtins.Python3Core;
117117
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
118118
import com.oracle.graal.python.builtins.PythonBuiltins;
119-
import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.HexNodeFactory;
120-
import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.OctNodeFactory;
121119
import com.oracle.graal.python.builtins.modules.WarningsModuleBuiltins.WarnNode;
122120
import com.oracle.graal.python.builtins.modules.ast.AstModuleBuiltins;
123121
import com.oracle.graal.python.builtins.modules.io.IOModuleBuiltins;
@@ -572,158 +570,147 @@ static boolean doObject(VirtualFrame frame, Object object,
572570
}
573571
}
574572

575-
// bin(object)
576-
@Builtin(name = J_BIN, minNumOfPositionalArgs = 1)
573+
@GenerateInline
574+
@GenerateCached(false)
577575
@TypeSystemReference(PythonArithmeticTypes.class)
578-
@GenerateNodeFactory
579-
public abstract static class BinNode extends PythonUnaryBuiltinNode {
580-
static final TruffleString T_BIN_PREFIX = tsLiteral("0b");
581-
static final TruffleString T_HEX_PREFIX = tsLiteral("0x");
582-
static final TruffleString T_OCT_PREFIX = tsLiteral("0o");
576+
abstract static class BinOctHexHelperNode extends Node {
583577

584-
@TruffleBoundary
585-
protected TruffleString buildString(boolean isNegative, TruffleString number, TruffleStringBuilder.AppendStringNode appendStringNode, TruffleStringBuilder.ToStringNode toStringNode) {
586-
TruffleStringBuilder sb = TruffleStringBuilder.create(TS_ENCODING, tsbCapacity(3) + number.byteLength(TS_ENCODING));
587-
if (isNegative) {
588-
appendStringNode.execute(sb, T_MINUS);
589-
}
590-
appendStringNode.execute(sb, prefix());
591-
appendStringNode.execute(sb, number);
592-
return toStringNode.execute(sb);
578+
@FunctionalInterface
579+
interface LongToString {
580+
String convert(long value);
593581
}
594582

595-
protected TruffleString prefix() {
596-
return T_BIN_PREFIX;
597-
}
583+
abstract TruffleString execute(VirtualFrame frame, Node inliningTarget, Object o, TruffleString prefix, int radix, LongToString longToString);
598584

599585
@TruffleBoundary
600-
protected String longToString(long x) {
601-
return Long.toBinaryString(x);
586+
private static TruffleString buildString(boolean isNegative, TruffleString prefix, TruffleString number) {
587+
TruffleStringBuilder sb = TruffleStringBuilder.create(TS_ENCODING, tsbCapacity(3) + number.byteLength(TS_ENCODING));
588+
if (isNegative) {
589+
sb.appendStringUncached(T_MINUS);
590+
}
591+
sb.appendStringUncached(prefix);
592+
sb.appendStringUncached(number);
593+
return sb.toStringUncached();
602594
}
603595

604596
@TruffleBoundary
605-
protected String bigToString(BigInteger x) {
606-
return x.toString(2);
597+
private static BigInteger longMaxPlusOne() {
598+
return BigInteger.valueOf(Long.MIN_VALUE).abs();
607599
}
608600

609601
@TruffleBoundary
610-
protected BigInteger bigAbs(BigInteger x) {
611-
return x.abs();
602+
private static String bigToString(int radix, BigInteger x) {
603+
return x.toString(radix);
612604
}
613605

614606
@Specialization
615-
TruffleString doL(long x,
616-
@Bind("this") Node inliningTarget,
607+
static TruffleString doL(Node inliningTarget, long x, TruffleString prefix, int radix, LongToString longToString,
617608
@Exclusive @Cached InlinedConditionProfile isMinLong,
618-
@Shared @Cached TruffleString.FromJavaStringNode fromJavaStringNode,
619-
@Shared @Cached TruffleStringBuilder.AppendStringNode appendStringNode,
620-
@Shared @Cached TruffleStringBuilder.ToStringNode toStringNode) {
609+
@Shared @Cached(inline = false) TruffleString.FromJavaStringNode fromJavaStringNode) {
621610
if (isMinLong.profile(inliningTarget, x == Long.MIN_VALUE)) {
622-
return buildString(true, fromJavaStringNode.execute(bigToString(bigAbs(PInt.longToBigInteger(x))), TS_ENCODING), appendStringNode, toStringNode);
611+
return buildString(true, prefix, fromJavaStringNode.execute(bigToString(radix, longMaxPlusOne()), TS_ENCODING));
623612
}
624-
return buildString(x < 0, fromJavaStringNode.execute(longToString(Math.abs(x)), TS_ENCODING), appendStringNode, toStringNode);
613+
return buildString(x < 0, prefix, fromJavaStringNode.execute(longToString.convert(Math.abs(x)), TS_ENCODING));
625614
}
626615

627616
@Specialization
628-
TruffleString doD(double x,
629-
@Cached PRaiseNode raise) {
617+
@SuppressWarnings("unused")
618+
static TruffleString doD(double x, TruffleString prefix, int radix, LongToString longToString,
619+
@Cached(inline = false) PRaiseNode raise) {
630620
throw raise.raiseIntegerInterpretationError(x);
631621
}
632622

633623
@Specialization
634-
TruffleString doPI(PInt x,
635-
@Shared @Cached TruffleString.FromJavaStringNode fromJavaStringNode,
636-
@Shared @Cached TruffleStringBuilder.AppendStringNode appendStringNode,
637-
@Shared @Cached TruffleStringBuilder.ToStringNode toStringNode) {
624+
static TruffleString doPI(PInt x, TruffleString prefix, int radix, @SuppressWarnings("unused") LongToString longToString,
625+
@Shared @Cached(inline = false) TruffleString.FromJavaStringNode fromJavaStringNode) {
638626
BigInteger value = x.getValue();
639-
return buildString(value.signum() < 0, fromJavaStringNode.execute(bigToString(PInt.abs(value)), TS_ENCODING), appendStringNode, toStringNode);
627+
return buildString(value.signum() < 0, prefix, fromJavaStringNode.execute(bigToString(radix, PInt.abs(value)), TS_ENCODING));
640628
}
641629

642630
@Specialization(replaces = {"doL", "doD", "doPI"})
643-
@SuppressWarnings("truffle-static-method")
644-
TruffleString doO(VirtualFrame frame, Object x,
645-
@Bind("this") Node inliningTarget,
631+
static TruffleString doO(VirtualFrame frame, Node inliningTarget, Object x, TruffleString prefix, int radix, LongToString longToString,
646632
@Exclusive @Cached InlinedConditionProfile isMinLong,
647633
@Cached PyNumberIndexNode indexNode,
648634
@Cached PyNumberAsSizeNode asSizeNode,
649635
@Cached InlinedBranchProfile isInt,
650636
@Cached InlinedBranchProfile isLong,
651637
@Cached InlinedBranchProfile isPInt,
652-
@Shared @Cached TruffleString.FromJavaStringNode fromJavaStringNode,
653-
@Shared @Cached TruffleStringBuilder.AppendStringNode appendStringNode,
654-
@Shared @Cached TruffleStringBuilder.ToStringNode toStringNode,
655-
@Cached PRaiseNode.Lazy raiseNode) {
638+
@Shared @Cached(inline = false) TruffleString.FromJavaStringNode fromJavaStringNode) {
656639
Object index = indexNode.execute(frame, inliningTarget, x);
657640
if (index instanceof Boolean || index instanceof Integer) {
658641
isInt.enter(inliningTarget);
659-
return doL(asSizeNode.executeExact(frame, inliningTarget, index), inliningTarget, isMinLong, fromJavaStringNode, appendStringNode, toStringNode);
642+
return doL(inliningTarget, asSizeNode.executeExact(frame, inliningTarget, index), prefix, radix, longToString, isMinLong, fromJavaStringNode);
660643
} else if (index instanceof Long) {
661644
isLong.enter(inliningTarget);
662-
return doL((long) index, inliningTarget, isMinLong, fromJavaStringNode, appendStringNode, toStringNode);
645+
return doL(inliningTarget, (long) index, prefix, radix, longToString, isMinLong, fromJavaStringNode);
663646
} else if (index instanceof PInt) {
664647
isPInt.enter(inliningTarget);
665-
return doPI((PInt) index, fromJavaStringNode, appendStringNode, toStringNode);
648+
return doPI((PInt) index, prefix, radix, longToString, fromJavaStringNode);
666649
} else {
667650
CompilerDirectives.transferToInterpreter();
668-
throw raiseNode.get(inliningTarget).raise(PythonBuiltinClassType.NotImplementedError, toTruffleStringUncached("bin/oct/hex with native integer subclasses"));
651+
throw PRaiseNode.raiseUncached(inliningTarget, PythonBuiltinClassType.NotImplementedError, toTruffleStringUncached("bin/oct/hex with native integer subclasses"));
669652
}
670653
}
671654
}
672655

673-
// oct(object)
674-
@Builtin(name = J_OCT, minNumOfPositionalArgs = 1)
656+
// bin(object)
657+
@Builtin(name = J_BIN, minNumOfPositionalArgs = 1)
675658
@TypeSystemReference(PythonArithmeticTypes.class)
676659
@GenerateNodeFactory
677-
public abstract static class OctNode extends BinNode {
678-
@Override
679-
@TruffleBoundary
680-
protected String bigToString(BigInteger x) {
681-
return x.toString(8);
660+
public abstract static class BinNode extends PythonUnaryBuiltinNode {
661+
static final TruffleString T_BIN_PREFIX = tsLiteral("0b");
662+
663+
@Specialization
664+
static TruffleString doIt(VirtualFrame frame, Object x,
665+
@Bind("this") Node inliningTarget,
666+
@Cached BinOctHexHelperNode helperNode) {
667+
return helperNode.execute(frame, inliningTarget, x, T_BIN_PREFIX, 2, BinNode::longToString);
682668
}
683669

684-
@Override
685670
@TruffleBoundary
686-
protected String longToString(long x) {
687-
return Long.toOctalString(x);
671+
private static String longToString(long x) {
672+
return Long.toBinaryString(x);
688673
}
674+
}
689675

690-
@Override
691-
protected TruffleString prefix() {
692-
return T_OCT_PREFIX;
676+
// oct(object)
677+
@Builtin(name = J_OCT, minNumOfPositionalArgs = 1)
678+
@TypeSystemReference(PythonArithmeticTypes.class)
679+
@GenerateNodeFactory
680+
public abstract static class OctNode extends PythonUnaryBuiltinNode {
681+
static final TruffleString T_OCT_PREFIX = tsLiteral("0o");
682+
683+
@Specialization
684+
static TruffleString doIt(VirtualFrame frame, Object x,
685+
@Bind("this") Node inliningTarget,
686+
@Cached BinOctHexHelperNode helperNode) {
687+
return helperNode.execute(frame, inliningTarget, x, T_OCT_PREFIX, 8, OctNode::longToString);
693688
}
694689

695-
@NeverDefault
696-
public static OctNode create() {
697-
return OctNodeFactory.create();
690+
@TruffleBoundary
691+
private static String longToString(long x) {
692+
return Long.toOctalString(x);
698693
}
699694
}
700695

701696
// hex(object)
702697
@Builtin(name = J_HEX, minNumOfPositionalArgs = 1)
703698
@TypeSystemReference(PythonArithmeticTypes.class)
704699
@GenerateNodeFactory
705-
public abstract static class HexNode extends BinNode {
706-
@Override
707-
@TruffleBoundary
708-
protected String bigToString(BigInteger x) {
709-
return x.toString(16);
700+
public abstract static class HexNode extends PythonUnaryBuiltinNode {
701+
static final TruffleString T_HEX_PREFIX = tsLiteral("0x");
702+
703+
@Specialization
704+
static TruffleString doIt(VirtualFrame frame, Object x,
705+
@Bind("this") Node inliningTarget,
706+
@Cached BinOctHexHelperNode helperNode) {
707+
return helperNode.execute(frame, inliningTarget, x, T_HEX_PREFIX, 16, HexNode::longToString);
710708
}
711709

712-
@Override
713710
@TruffleBoundary
714-
protected String longToString(long x) {
711+
private static String longToString(long x) {
715712
return Long.toHexString(x);
716713
}
717-
718-
@Override
719-
protected TruffleString prefix() {
720-
return T_HEX_PREFIX;
721-
}
722-
723-
@NeverDefault
724-
public static HexNode create() {
725-
return HexNodeFactory.create();
726-
}
727714
}
728715

729716
// callable(object)
@@ -1657,7 +1644,7 @@ protected final BinaryComparisonNode createComparison() {
16571644
}
16581645

16591646
@Specialization(guards = "args.length == 0")
1660-
@SuppressWarnings("truffle-static-method")
1647+
@SuppressWarnings("truffle-static-method") // TODO: inh
16611648
Object minmaxSequenceWithKey(VirtualFrame frame, Object arg1, @SuppressWarnings("unused") Object[] args, Object keywordArgIn, Object defaultVal,
16621649
@Bind("this") Node inliningTarget,
16631650
@Exclusive @Cached PyObjectGetIter getIter,
@@ -1726,7 +1713,7 @@ private String getName() {
17261713
}
17271714

17281715
@Specialization(guards = {"args.length != 0"})
1729-
@SuppressWarnings("truffle-static-method")
1716+
@SuppressWarnings("truffle-static-method") // TODO: inh
17301717
Object minmaxBinaryWithKey(VirtualFrame frame, Object arg1, Object[] args, Object keywordArgIn, Object defaultVal,
17311718
@Bind("this") Node inliningTarget,
17321719
@Shared @Cached("createComparison()") BinaryComparisonNode compare,

0 commit comments

Comments
 (0)