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 */
23
24
25 package org.graalvm.compiler.replacements.amd64;
26
27 import org.graalvm.compiler.api.replacements.ClassSubstitution;
28 import org.graalvm.compiler.api.replacements.Fold;
29 import org.graalvm.compiler.api.replacements.Fold.InjectedParameter;
30 import org.graalvm.compiler.api.replacements.MethodSubstitution;
31 import org.graalvm.compiler.nodes.DeoptimizeNode;
32 import org.graalvm.compiler.replacements.nodes.ArrayCompareToNode;
33 import org.graalvm.compiler.word.Word;
34 import jdk.internal.vm.compiler.word.Pointer;
35
36 import jdk.vm.ci.meta.DeoptimizationAction;
37 import jdk.vm.ci.meta.DeoptimizationReason;
38 import jdk.vm.ci.meta.JavaKind;
39 import jdk.vm.ci.meta.MetaAccessProvider;
40
41 // JaCoCo Exclude
42
43 /**
44 * Substitutions for {@code java.lang.StringLatin1} methods.
45 *
46 * Since JDK 9.
47 */
48 @ClassSubstitution(className = "java.lang.StringLatin1", optional = true)
49 public class AMD64StringLatin1Substitutions {
50
51 @Fold
52 static int byteArrayBaseOffset(@InjectedParameter MetaAccessProvider metaAccess) {
72 static final MetaAccessProvider INJECTED = null;
73
74 /**
75 * @param value is byte[]
76 * @param other is byte[]
77 */
78 @MethodSubstitution
79 public static int compareTo(byte[] value, byte[] other) {
80 return ArrayCompareToNode.compareTo(value, other, value.length, other.length, JavaKind.Byte, JavaKind.Byte);
81 }
82
83 /**
84 * @param value is byte[]
85 * @param other is char[]
86 */
87 @MethodSubstitution
88 public static int compareToUTF16(byte[] value, byte[] other) {
89 return ArrayCompareToNode.compareTo(value, other, value.length, other.length, JavaKind.Byte, JavaKind.Char);
90 }
91
92 @MethodSubstitution(optional = true)
93 public static int indexOf(byte[] value, int ch, int origFromIndex) {
94 int fromIndex = origFromIndex;
95 if (ch >>> 8 != 0) {
96 // search value must be a byte value
97 return -1;
98 }
99 int length = value.length;
100 if (fromIndex < 0) {
101 fromIndex = 0;
102 } else if (fromIndex >= length) {
103 // Note: fromIndex might be near -1>>>1.
104 return -1;
105 }
106 Pointer sourcePointer = Word.objectToTrackedPointer(value).add(byteArrayBaseOffset(INJECTED)).add(fromIndex);
107 int result = AMD64ArrayIndexOf.indexOf1Byte(sourcePointer, length - fromIndex, (byte) ch);
108 if (result != -1) {
109 return result + fromIndex;
110 }
111 return result;
112 }
113
114 /**
115 * Intrinsic for {@code java.lang.StringLatin1.inflate([BI[CII)V}.
116 *
117 * <pre>
118 * @HotSpotIntrinsicCandidate
119 * public static void inflate(byte[] src, int src_indx, char[] dst, int dst_indx, int len)
120 * </pre>
121 */
122 @MethodSubstitution
123 public static void inflate(byte[] src, int srcIndex, char[] dest, int destIndex, int len) {
124 if (len < 0 || srcIndex < 0 || (srcIndex + len > src.length) || destIndex < 0 || (destIndex + len > dest.length)) {
125 DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.BoundsCheckException);
126 }
127
128 // Offset calc. outside of the actual intrinsic.
129 Pointer srcPointer = Word.objectToTrackedPointer(src).add(byteArrayBaseOffset(INJECTED)).add(srcIndex * byteArrayIndexScale(INJECTED));
130 Pointer destPointer = Word.objectToTrackedPointer(dest).add(charArrayBaseOffset(INJECTED)).add(destIndex * charArrayIndexScale(INJECTED));
131 AMD64StringLatin1InflateNode.inflate(srcPointer, destPointer, len, JavaKind.Char);
|
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 */
23
24
25 package org.graalvm.compiler.replacements.amd64;
26
27 import org.graalvm.compiler.api.replacements.ClassSubstitution;
28 import org.graalvm.compiler.api.replacements.Fold;
29 import org.graalvm.compiler.api.replacements.Fold.InjectedParameter;
30 import org.graalvm.compiler.api.replacements.MethodSubstitution;
31 import org.graalvm.compiler.nodes.DeoptimizeNode;
32 import org.graalvm.compiler.replacements.nodes.ArrayCompareToNode;
33 import org.graalvm.compiler.replacements.nodes.ArrayRegionEqualsNode;
34 import org.graalvm.compiler.word.Word;
35 import jdk.internal.vm.compiler.word.Pointer;
36
37 import jdk.vm.ci.meta.DeoptimizationAction;
38 import jdk.vm.ci.meta.DeoptimizationReason;
39 import jdk.vm.ci.meta.JavaKind;
40 import jdk.vm.ci.meta.MetaAccessProvider;
41
42 // JaCoCo Exclude
43
44 /**
45 * Substitutions for {@code java.lang.StringLatin1} methods.
46 *
47 * Since JDK 9.
48 */
49 @ClassSubstitution(className = "java.lang.StringLatin1", optional = true)
50 public class AMD64StringLatin1Substitutions {
51
52 @Fold
53 static int byteArrayBaseOffset(@InjectedParameter MetaAccessProvider metaAccess) {
73 static final MetaAccessProvider INJECTED = null;
74
75 /**
76 * @param value is byte[]
77 * @param other is byte[]
78 */
79 @MethodSubstitution
80 public static int compareTo(byte[] value, byte[] other) {
81 return ArrayCompareToNode.compareTo(value, other, value.length, other.length, JavaKind.Byte, JavaKind.Byte);
82 }
83
84 /**
85 * @param value is byte[]
86 * @param other is char[]
87 */
88 @MethodSubstitution
89 public static int compareToUTF16(byte[] value, byte[] other) {
90 return ArrayCompareToNode.compareTo(value, other, value.length, other.length, JavaKind.Byte, JavaKind.Char);
91 }
92
93 private static Word pointer(byte[] target) {
94 return Word.objectToTrackedPointer(target).add(byteArrayBaseOffset(INJECTED));
95 }
96
97 private static Word byteOffsetPointer(byte[] source, int offset) {
98 return pointer(source).add(offset * byteArrayIndexScale(INJECTED));
99 }
100
101 @MethodSubstitution
102 public static int indexOf(byte[] value, int ch, int origFromIndex) {
103 int fromIndex = origFromIndex;
104 if (ch >>> 8 != 0) {
105 // search value must be a byte value
106 return -1;
107 }
108 int length = value.length;
109 if (fromIndex < 0) {
110 fromIndex = 0;
111 } else if (fromIndex >= length) {
112 // Note: fromIndex might be near -1>>>1.
113 return -1;
114 }
115 Pointer sourcePointer = byteOffsetPointer(value, fromIndex);
116 int result = AMD64ArrayIndexOf.indexOf1Byte(sourcePointer, length - fromIndex, (byte) ch);
117 if (result != -1) {
118 return result + fromIndex;
119 }
120 return result;
121 }
122
123 @MethodSubstitution
124 public static int indexOf(byte[] source, int sourceCount, byte[] target, int targetCount, int origFromIndex) {
125 int fromIndex = origFromIndex;
126 if (fromIndex >= sourceCount) {
127 return (targetCount == 0 ? sourceCount : -1);
128 }
129 if (fromIndex < 0) {
130 fromIndex = 0;
131 }
132 if (targetCount == 0) {
133 // The empty string is in every string.
134 return fromIndex;
135 }
136 if (sourceCount - fromIndex < targetCount) {
137 // The empty string contains nothing except the empty string.
138 return -1;
139 }
140 int totalOffset = fromIndex;
141 if (targetCount == 1) {
142 Pointer sourcePointer = byteOffsetPointer(source, totalOffset);
143 int indexOfResult = AMD64ArrayIndexOf.indexOf1Byte(sourcePointer, sourceCount - fromIndex, target[0]);
144 if (indexOfResult >= 0) {
145 return indexOfResult + totalOffset;
146 }
147 return indexOfResult;
148 } else if (targetCount == 2) {
149 Pointer sourcePointer = byteOffsetPointer(source, totalOffset);
150 int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveBytes(sourcePointer, sourceCount - fromIndex, target[0], target[1]);
151 if (indexOfResult >= 0) {
152 return indexOfResult + totalOffset;
153 }
154 return indexOfResult;
155 } else {
156 int haystackLength = sourceCount - (fromIndex + (targetCount - 2));
157 while (haystackLength > 0) {
158 Pointer sourcePointer = byteOffsetPointer(source, totalOffset);
159 int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveBytes(sourcePointer, haystackLength, target[0], target[1]);
160 if (indexOfResult < 0) {
161 return -1;
162 }
163 totalOffset += indexOfResult;
164 haystackLength -= (indexOfResult + 1);
165 Pointer cmpSourcePointer = byteOffsetPointer(source, totalOffset);
166 Pointer targetPointer = pointer(target);
167 if (ArrayRegionEqualsNode.regionEquals(cmpSourcePointer, targetPointer, targetCount, JavaKind.Byte)) {
168 return totalOffset;
169 }
170 totalOffset++;
171 }
172 return -1;
173 }
174 }
175
176 /**
177 * Intrinsic for {@code java.lang.StringLatin1.inflate([BI[CII)V}.
178 *
179 * <pre>
180 * @HotSpotIntrinsicCandidate
181 * public static void inflate(byte[] src, int src_indx, char[] dst, int dst_indx, int len)
182 * </pre>
183 */
184 @MethodSubstitution
185 public static void inflate(byte[] src, int srcIndex, char[] dest, int destIndex, int len) {
186 if (len < 0 || srcIndex < 0 || (srcIndex + len > src.length) || destIndex < 0 || (destIndex + len > dest.length)) {
187 DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.BoundsCheckException);
188 }
189
190 // Offset calc. outside of the actual intrinsic.
191 Pointer srcPointer = Word.objectToTrackedPointer(src).add(byteArrayBaseOffset(INJECTED)).add(srcIndex * byteArrayIndexScale(INJECTED));
192 Pointer destPointer = Word.objectToTrackedPointer(dest).add(charArrayBaseOffset(INJECTED)).add(destIndex * charArrayIndexScale(INJECTED));
193 AMD64StringLatin1InflateNode.inflate(srcPointer, destPointer, len, JavaKind.Char);
|