Skip to content

Commit 970f54f

Browse files
committed
Refactor str.__lt/le/gt/ge__
1 parent a6cad3e commit 970f54f

File tree

2 files changed

+85
-37
lines changed

2 files changed

+85
-37
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringBuiltins.java

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@
121121
import com.oracle.graal.python.builtins.objects.slice.SliceNodes.ComputeIndices;
122122
import com.oracle.graal.python.builtins.objects.str.StringBuiltinsClinicProviders.FormatNodeClinicProviderGen;
123123
import com.oracle.graal.python.builtins.objects.str.StringBuiltinsClinicProviders.SplitNodeClinicProviderGen;
124-
import com.oracle.graal.python.builtins.objects.str.StringBuiltinsFactory.LtNodeFactory;
125124
import com.oracle.graal.python.builtins.objects.str.StringNodes.CastToJavaStringCheckedNode;
126125
import com.oracle.graal.python.builtins.objects.str.StringNodes.CastToTruffleStringCheckedNode;
127126
import com.oracle.graal.python.builtins.objects.str.StringNodes.JoinInternalNode;
@@ -164,6 +163,7 @@
164163
import com.oracle.graal.python.runtime.formatting.StringFormatProcessor;
165164
import com.oracle.graal.python.runtime.formatting.TextFormatter;
166165
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
166+
import com.oracle.graal.python.util.IntPredicate;
167167
import com.oracle.truffle.api.CompilerAsserts;
168168
import com.oracle.truffle.api.CompilerDirectives;
169169
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
@@ -177,7 +177,6 @@
177177
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
178178
import com.oracle.truffle.api.dsl.GenerateUncached;
179179
import com.oracle.truffle.api.dsl.ImportStatic;
180-
import com.oracle.truffle.api.dsl.NeverDefault;
181180
import com.oracle.truffle.api.dsl.Specialization;
182181
import com.oracle.truffle.api.dsl.TypeSystemReference;
183182
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -385,20 +384,23 @@ static Object doGeneric(Node inliningTarget, Object self, Object other, boolean
385384
}
386385
}
387386

388-
abstract static class StringCmpOpBaseNode extends PythonBinaryBuiltinNode {
387+
@GenerateInline
388+
@GenerateCached(false)
389+
abstract static class StringCmpOpHelperNode extends Node {
390+
391+
abstract Object execute(Node inliningTarget, Object self, Object other, IntPredicate resultProcessor);
392+
389393
@Specialization
390-
boolean doStrings(TruffleString self, TruffleString other,
391-
@Shared("compareIntsUtf32") @Cached TruffleString.CompareIntsUTF32Node compareIntsUTF32Node) {
392-
return processResult(StringUtils.compareStrings(self, other, compareIntsUTF32Node));
394+
static boolean doStrings(TruffleString self, TruffleString other, IntPredicate resultProcessor,
395+
@Shared @Cached(inline = false) TruffleString.CompareIntsUTF32Node compareIntsUTF32Node) {
396+
return resultProcessor.test(StringUtils.compareStrings(self, other, compareIntsUTF32Node));
393397
}
394398

395399
@Specialization
396-
@SuppressWarnings("truffle-static-method")
397-
Object doGeneric(Object self, Object other,
398-
@Bind("this") Node inliningTarget,
400+
static Object doGeneric(Node inliningTarget, Object self, Object other, IntPredicate resultProcessor,
399401
@Cached CastToTruffleStringCheckedNode castSelfNode,
400402
@Cached CastToTruffleStringNode castOtherNode,
401-
@Shared("compareIntsUtf32") @Cached TruffleString.CompareIntsUTF32Node compareIntsUTF32Node,
403+
@Shared @Cached(inline = false) TruffleString.CompareIntsUTF32Node compareIntsUTF32Node,
402404
@Cached InlinedBranchProfile noStringBranch) {
403405
TruffleString selfStr = castSelfNode.cast(inliningTarget, self, ErrorMessages.REQUIRES_STR_OBJECT_BUT_RECEIVED_P, T___EQ__, self);
404406
TruffleString otherStr;
@@ -408,13 +410,7 @@ Object doGeneric(Object self, Object other,
408410
noStringBranch.enter(inliningTarget);
409411
return PNotImplemented.NOT_IMPLEMENTED;
410412
}
411-
return doStrings(selfStr, otherStr, compareIntsUTF32Node);
412-
}
413-
414-
@SuppressWarnings("unused")
415-
boolean processResult(int cmpResult) {
416-
CompilerAsserts.neverPartOfCompilation();
417-
throw new IllegalStateException("should not be reached");
413+
return doStrings(selfStr, otherStr, resultProcessor, compareIntsUTF32Node);
418414
}
419415
}
420416

@@ -461,42 +457,49 @@ static Object doIt(Object self, Object other,
461457

462458
@Builtin(name = J___LT__, minNumOfPositionalArgs = 2)
463459
@GenerateNodeFactory
464-
public abstract static class LtNode extends StringCmpOpBaseNode {
465-
@Override
466-
boolean processResult(int cmpResult) {
467-
return cmpResult < 0;
468-
}
460+
public abstract static class LtNode extends PythonBinaryBuiltinNode {
469461

470-
@NeverDefault
471-
public static LtNode create() {
472-
return LtNodeFactory.create();
462+
@Specialization
463+
static Object doIt(Object self, Object other,
464+
@Bind("this") Node inliningTarget,
465+
@Cached StringCmpOpHelperNode stringCmpOpHelperNode) {
466+
return stringCmpOpHelperNode.execute(inliningTarget, self, other, r -> r < 0);
473467
}
474468
}
475469

476470
@Builtin(name = J___LE__, minNumOfPositionalArgs = 2)
477471
@GenerateNodeFactory
478-
public abstract static class LeNode extends StringCmpOpBaseNode {
479-
@Override
480-
boolean processResult(int cmpResult) {
481-
return cmpResult <= 0;
472+
public abstract static class LeNode extends PythonBinaryBuiltinNode {
473+
474+
@Specialization
475+
static Object doIt(Object self, Object other,
476+
@Bind("this") Node inliningTarget,
477+
@Cached StringCmpOpHelperNode stringCmpOpHelperNode) {
478+
return stringCmpOpHelperNode.execute(inliningTarget, self, other, r -> r <= 0);
482479
}
483480
}
484481

485482
@Builtin(name = J___GT__, minNumOfPositionalArgs = 2)
486483
@GenerateNodeFactory
487-
public abstract static class GtNode extends StringCmpOpBaseNode {
488-
@Override
489-
boolean processResult(int cmpResult) {
490-
return cmpResult > 0;
484+
public abstract static class GtNode extends PythonBinaryBuiltinNode {
485+
486+
@Specialization
487+
static Object doIt(Object self, Object other,
488+
@Bind("this") Node inliningTarget,
489+
@Cached StringCmpOpHelperNode stringCmpOpHelperNode) {
490+
return stringCmpOpHelperNode.execute(inliningTarget, self, other, r -> r > 0);
491491
}
492492
}
493493

494494
@Builtin(name = J___GE__, minNumOfPositionalArgs = 2)
495495
@GenerateNodeFactory
496-
public abstract static class GeNode extends StringCmpOpBaseNode {
497-
@Override
498-
boolean processResult(int cmpResult) {
499-
return cmpResult >= 0;
496+
public abstract static class GeNode extends PythonBinaryBuiltinNode {
497+
498+
@Specialization
499+
static Object doIt(Object self, Object other,
500+
@Bind("this") Node inliningTarget,
501+
@Cached StringCmpOpHelperNode stringCmpOpHelperNode) {
502+
return stringCmpOpHelperNode.execute(inliningTarget, self, other, r -> r >= 0);
500503
}
501504
}
502505

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.util;
42+
43+
@FunctionalInterface
44+
public interface IntPredicate extends java.util.function.IntPredicate {
45+
}

0 commit comments

Comments
 (0)