1 /* 2 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. 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 */ 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) { 54 return metaAccess.getArrayBaseOffset(JavaKind.Byte); 55 } 56 57 @Fold 58 static int byteArrayIndexScale(@InjectedParameter MetaAccessProvider metaAccess) { 59 return metaAccess.getArrayIndexScale(JavaKind.Byte); 60 } 61 62 @Fold 63 static int charArrayBaseOffset(@InjectedParameter MetaAccessProvider metaAccess) { 64 return metaAccess.getArrayBaseOffset(JavaKind.Char); 65 } 66 67 @Fold 68 static int charArrayIndexScale(@InjectedParameter MetaAccessProvider metaAccess) { 69 return metaAccess.getArrayIndexScale(JavaKind.Char); 70 } 71 72 /** Marker value for the {@link InjectedParameter} injected parameter. */ 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); 194 } 195 196 /** 197 * Intrinsic for {@code }java.lang.StringLatin1.inflate([BI[BII)V}. 198 * 199 * <pre> 200 * @HotSpotIntrinsicCandidate 201 * public static void inflate(byte[] src, int src_indx, byte[] dst, int dst_indx, int len) 202 * </pre> 203 * 204 * In this variant {@code dest} refers to a byte array containing 2 byte per char so 205 * {@code destIndex} and {@code len} are in terms of char elements and have to be scaled by 2 206 * when referring to {@code dest} 207 */ 208 @MethodSubstitution 209 public static void inflate(byte[] src, int srcIndex, byte[] dest, int destIndex, int len) { 210 if (len < 0 || srcIndex < 0 || (srcIndex + len > src.length) || destIndex < 0 || (destIndex * 2 + len * 2 > dest.length)) { 211 DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.BoundsCheckException); 212 } 213 214 // Offset calc. outside of the actual intrinsic. 215 Pointer srcPointer = Word.objectToTrackedPointer(src).add(byteArrayBaseOffset(INJECTED)).add(srcIndex * byteArrayIndexScale(INJECTED)); 216 Pointer destPointer = Word.objectToTrackedPointer(dest).add(byteArrayBaseOffset(INJECTED)).add(destIndex * 2 * byteArrayIndexScale(INJECTED)); 217 AMD64StringLatin1InflateNode.inflate(srcPointer, destPointer, len, JavaKind.Byte); 218 } 219 220 }