|
116 | 116 | import com.oracle.graal.python.builtins.Python3Core;
|
117 | 117 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
118 | 118 | 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; |
121 | 119 | import com.oracle.graal.python.builtins.modules.WarningsModuleBuiltins.WarnNode;
|
122 | 120 | import com.oracle.graal.python.builtins.modules.ast.AstModuleBuiltins;
|
123 | 121 | import com.oracle.graal.python.builtins.modules.io.IOModuleBuiltins;
|
@@ -572,158 +570,147 @@ static boolean doObject(VirtualFrame frame, Object object,
|
572 | 570 | }
|
573 | 571 | }
|
574 | 572 |
|
575 |
| - // bin(object) |
576 |
| - @Builtin(name = J_BIN, minNumOfPositionalArgs = 1) |
| 573 | + @GenerateInline |
| 574 | + @GenerateCached(false) |
577 | 575 | @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 { |
583 | 577 |
|
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); |
593 | 581 | }
|
594 | 582 |
|
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); |
598 | 584 |
|
599 | 585 | @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(); |
602 | 594 | }
|
603 | 595 |
|
604 | 596 | @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(); |
607 | 599 | }
|
608 | 600 |
|
609 | 601 | @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); |
612 | 604 | }
|
613 | 605 |
|
614 | 606 | @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, |
617 | 608 | @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) { |
621 | 610 | 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)); |
623 | 612 | }
|
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)); |
625 | 614 | }
|
626 | 615 |
|
627 | 616 | @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) { |
630 | 620 | throw raise.raiseIntegerInterpretationError(x);
|
631 | 621 | }
|
632 | 622 |
|
633 | 623 | @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) { |
638 | 626 | 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)); |
640 | 628 | }
|
641 | 629 |
|
642 | 630 | @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, |
646 | 632 | @Exclusive @Cached InlinedConditionProfile isMinLong,
|
647 | 633 | @Cached PyNumberIndexNode indexNode,
|
648 | 634 | @Cached PyNumberAsSizeNode asSizeNode,
|
649 | 635 | @Cached InlinedBranchProfile isInt,
|
650 | 636 | @Cached InlinedBranchProfile isLong,
|
651 | 637 | @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) { |
656 | 639 | Object index = indexNode.execute(frame, inliningTarget, x);
|
657 | 640 | if (index instanceof Boolean || index instanceof Integer) {
|
658 | 641 | 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); |
660 | 643 | } else if (index instanceof Long) {
|
661 | 644 | isLong.enter(inliningTarget);
|
662 |
| - return doL((long) index, inliningTarget, isMinLong, fromJavaStringNode, appendStringNode, toStringNode); |
| 645 | + return doL(inliningTarget, (long) index, prefix, radix, longToString, isMinLong, fromJavaStringNode); |
663 | 646 | } else if (index instanceof PInt) {
|
664 | 647 | isPInt.enter(inliningTarget);
|
665 |
| - return doPI((PInt) index, fromJavaStringNode, appendStringNode, toStringNode); |
| 648 | + return doPI((PInt) index, prefix, radix, longToString, fromJavaStringNode); |
666 | 649 | } else {
|
667 | 650 | 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")); |
669 | 652 | }
|
670 | 653 | }
|
671 | 654 | }
|
672 | 655 |
|
673 |
| - // oct(object) |
674 |
| - @Builtin(name = J_OCT, minNumOfPositionalArgs = 1) |
| 656 | + // bin(object) |
| 657 | + @Builtin(name = J_BIN, minNumOfPositionalArgs = 1) |
675 | 658 | @TypeSystemReference(PythonArithmeticTypes.class)
|
676 | 659 | @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); |
682 | 668 | }
|
683 | 669 |
|
684 |
| - @Override |
685 | 670 | @TruffleBoundary
|
686 |
| - protected String longToString(long x) { |
687 |
| - return Long.toOctalString(x); |
| 671 | + private static String longToString(long x) { |
| 672 | + return Long.toBinaryString(x); |
688 | 673 | }
|
| 674 | + } |
689 | 675 |
|
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); |
693 | 688 | }
|
694 | 689 |
|
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); |
698 | 693 | }
|
699 | 694 | }
|
700 | 695 |
|
701 | 696 | // hex(object)
|
702 | 697 | @Builtin(name = J_HEX, minNumOfPositionalArgs = 1)
|
703 | 698 | @TypeSystemReference(PythonArithmeticTypes.class)
|
704 | 699 | @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); |
710 | 708 | }
|
711 | 709 |
|
712 |
| - @Override |
713 | 710 | @TruffleBoundary
|
714 |
| - protected String longToString(long x) { |
| 711 | + private static String longToString(long x) { |
715 | 712 | return Long.toHexString(x);
|
716 | 713 | }
|
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 |
| - } |
727 | 714 | }
|
728 | 715 |
|
729 | 716 | // callable(object)
|
@@ -1657,7 +1644,7 @@ protected final BinaryComparisonNode createComparison() {
|
1657 | 1644 | }
|
1658 | 1645 |
|
1659 | 1646 | @Specialization(guards = "args.length == 0")
|
1660 |
| - @SuppressWarnings("truffle-static-method") |
| 1647 | + @SuppressWarnings("truffle-static-method") // TODO: inh |
1661 | 1648 | Object minmaxSequenceWithKey(VirtualFrame frame, Object arg1, @SuppressWarnings("unused") Object[] args, Object keywordArgIn, Object defaultVal,
|
1662 | 1649 | @Bind("this") Node inliningTarget,
|
1663 | 1650 | @Exclusive @Cached PyObjectGetIter getIter,
|
@@ -1726,7 +1713,7 @@ private String getName() {
|
1726 | 1713 | }
|
1727 | 1714 |
|
1728 | 1715 | @Specialization(guards = {"args.length != 0"})
|
1729 |
| - @SuppressWarnings("truffle-static-method") |
| 1716 | + @SuppressWarnings("truffle-static-method") // TODO: inh |
1730 | 1717 | Object minmaxBinaryWithKey(VirtualFrame frame, Object arg1, Object[] args, Object keywordArgIn, Object defaultVal,
|
1731 | 1718 | @Bind("this") Node inliningTarget,
|
1732 | 1719 | @Shared @Cached("createComparison()") BinaryComparisonNode compare,
|
|
0 commit comments