46344
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
|
46344
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
50858
|
23 |
|
|
24 |
|
46344
|
25 |
package org.graalvm.compiler.replacements.amd64;
|
|
26 |
|
|
27 |
import org.graalvm.compiler.api.replacements.ClassSubstitution;
|
48861
|
28 |
import org.graalvm.compiler.api.replacements.Fold;
|
|
29 |
import org.graalvm.compiler.api.replacements.Fold.InjectedParameter;
|
46344
|
30 |
import org.graalvm.compiler.api.replacements.MethodSubstitution;
|
49451
|
31 |
import org.graalvm.compiler.core.common.SuppressFBWarnings;
|
46344
|
32 |
import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
|
49451
|
33 |
import org.graalvm.compiler.replacements.StringSubstitutions;
|
|
34 |
import org.graalvm.compiler.replacements.nodes.ArrayCompareToNode;
|
52578
|
35 |
import org.graalvm.compiler.replacements.nodes.ArrayRegionEqualsNode;
|
46344
|
36 |
import org.graalvm.compiler.word.Word;
|
49873
|
37 |
import jdk.internal.vm.compiler.word.Pointer;
|
46344
|
38 |
|
48861
|
39 |
import jdk.vm.ci.meta.JavaKind;
|
52578
|
40 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
46344
|
41 |
|
|
42 |
// JaCoCo Exclude
|
|
43 |
|
|
44 |
/**
|
|
45 |
* Substitutions for {@link java.lang.String} methods.
|
|
46 |
*/
|
|
47 |
@ClassSubstitution(String.class)
|
|
48 |
public class AMD64StringSubstitutions {
|
|
49 |
|
48861
|
50 |
@Fold
|
52578
|
51 |
static int charArrayBaseOffset(@InjectedParameter MetaAccessProvider metaAccess) {
|
|
52 |
return metaAccess.getArrayBaseOffset(JavaKind.Char);
|
48861
|
53 |
}
|
|
54 |
|
|
55 |
@Fold
|
52578
|
56 |
static int charArrayIndexScale(@InjectedParameter MetaAccessProvider metaAccess) {
|
|
57 |
return metaAccess.getArrayIndexScale(JavaKind.Char);
|
48861
|
58 |
}
|
|
59 |
|
|
60 |
/** Marker value for the {@link InjectedParameter} injected parameter. */
|
52578
|
61 |
static final MetaAccessProvider INJECTED = null;
|
48861
|
62 |
|
46344
|
63 |
// Only exists in JDK <= 8
|
|
64 |
@MethodSubstitution(isStatic = true, optional = true)
|
|
65 |
public static int indexOf(char[] source, int sourceOffset, int sourceCount,
|
|
66 |
@ConstantNodeParameter char[] target, int targetOffset, int targetCount,
|
|
67 |
int origFromIndex) {
|
|
68 |
int fromIndex = origFromIndex;
|
|
69 |
if (fromIndex >= sourceCount) {
|
|
70 |
return (targetCount == 0 ? sourceCount : -1);
|
|
71 |
}
|
|
72 |
if (fromIndex < 0) {
|
|
73 |
fromIndex = 0;
|
|
74 |
}
|
|
75 |
if (targetCount == 0) {
|
|
76 |
// The empty string is in every string.
|
|
77 |
return fromIndex;
|
|
78 |
}
|
|
79 |
|
|
80 |
int totalOffset = sourceOffset + fromIndex;
|
|
81 |
if (sourceCount - fromIndex < targetCount) {
|
|
82 |
// The empty string contains nothing except the empty string.
|
|
83 |
return -1;
|
|
84 |
}
|
|
85 |
|
52578
|
86 |
if (targetCount == 1) {
|
55509
|
87 |
return AMD64ArrayIndexOf.indexOf1Char(source, sourceCount, totalOffset, target[targetOffset]);
|
52578
|
88 |
} else {
|
55509
|
89 |
int haystackLength = sourceCount - (targetCount - 2);
|
|
90 |
while (totalOffset < haystackLength) {
|
|
91 |
int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveChars(source, haystackLength, totalOffset, target[targetOffset], target[targetOffset + 1]);
|
52578
|
92 |
if (indexOfResult < 0) {
|
|
93 |
return -1;
|
|
94 |
}
|
55509
|
95 |
totalOffset = indexOfResult;
|
58299
|
96 |
if (targetCount == 2) {
|
52578
|
97 |
return totalOffset;
|
58299
|
98 |
} else {
|
|
99 |
Pointer cmpSourcePointer = Word.objectToTrackedPointer(source).add(charArrayBaseOffset(INJECTED)).add(totalOffset * charArrayIndexScale(INJECTED));
|
|
100 |
Pointer targetPointer = Word.objectToTrackedPointer(target).add(charArrayBaseOffset(INJECTED)).add(targetOffset * charArrayIndexScale(INJECTED));
|
|
101 |
if (ArrayRegionEqualsNode.regionEquals(cmpSourcePointer, targetPointer, targetCount, JavaKind.Char)) {
|
|
102 |
return totalOffset;
|
|
103 |
}
|
52578
|
104 |
}
|
|
105 |
totalOffset++;
|
|
106 |
}
|
|
107 |
return -1;
|
46344
|
108 |
}
|
|
109 |
}
|
49451
|
110 |
|
51436
|
111 |
// Only exists in JDK <= 8
|
|
112 |
@MethodSubstitution(isStatic = false, optional = true)
|
|
113 |
public static int indexOf(String source, int ch, int origFromIndex) {
|
|
114 |
int fromIndex = origFromIndex;
|
|
115 |
final int sourceCount = source.length();
|
|
116 |
if (fromIndex >= sourceCount) {
|
|
117 |
// Note: fromIndex might be near -1>>>1.
|
|
118 |
return -1;
|
|
119 |
}
|
|
120 |
if (fromIndex < 0) {
|
|
121 |
fromIndex = 0;
|
|
122 |
}
|
|
123 |
|
|
124 |
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
|
|
125 |
char[] sourceArray = StringSubstitutions.getValue(source);
|
55509
|
126 |
return AMD64ArrayIndexOf.indexOf1Char(sourceArray, sourceCount, fromIndex, (char) ch);
|
51436
|
127 |
} else {
|
|
128 |
return indexOf(source, ch, origFromIndex);
|
|
129 |
}
|
|
130 |
}
|
|
131 |
|
49451
|
132 |
@MethodSubstitution(isStatic = false)
|
|
133 |
@SuppressFBWarnings(value = "ES_COMPARING_PARAMETER_STRING_WITH_EQ", justification = "reference equality on the receiver is what we want")
|
|
134 |
public static int compareTo(String receiver, String anotherString) {
|
|
135 |
if (receiver == anotherString) {
|
|
136 |
return 0;
|
|
137 |
}
|
|
138 |
char[] value = StringSubstitutions.getValue(receiver);
|
|
139 |
char[] other = StringSubstitutions.getValue(anotherString);
|
|
140 |
return ArrayCompareToNode.compareTo(value, other, value.length << 1, other.length << 1, JavaKind.Char, JavaKind.Char);
|
|
141 |
}
|
|
142 |
|
46344
|
143 |
}
|