1 /* 2 * Copyright (c) 2013, 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.core.common.SuppressFBWarnings; 32 import org.graalvm.compiler.graph.Node.ConstantNodeParameter; 33 import org.graalvm.compiler.replacements.StringSubstitutions; 34 import org.graalvm.compiler.replacements.nodes.ArrayCompareToNode; 35 import org.graalvm.compiler.replacements.nodes.ArrayRegionEqualsNode; 36 import org.graalvm.compiler.word.Word; 37 import jdk.internal.vm.compiler.word.Pointer; 38 39 import jdk.vm.ci.meta.JavaKind; 40 import jdk.vm.ci.meta.MetaAccessProvider; 41 42 // JaCoCo Exclude 43 44 /** 45 * Substitutions for {@link java.lang.String} methods. 46 */ 47 @ClassSubstitution(String.class) 48 public class AMD64StringSubstitutions { 49 50 @Fold 51 static int charArrayBaseOffset(@InjectedParameter MetaAccessProvider metaAccess) { 52 return metaAccess.getArrayBaseOffset(JavaKind.Char); 53 } 54 55 @Fold 56 static int charArrayIndexScale(@InjectedParameter MetaAccessProvider metaAccess) { 57 return metaAccess.getArrayIndexScale(JavaKind.Char); 58 } 59 60 /** Marker value for the {@link InjectedParameter} injected parameter. */ 61 static final MetaAccessProvider INJECTED = null; 62 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 86 if (targetCount == 1) { 87 return AMD64ArrayIndexOf.indexOf1Char(source, sourceCount, totalOffset, target[targetOffset]); 88 } else if (targetCount == 2) { 89 return AMD64ArrayIndexOf.indexOfTwoConsecutiveChars(source, sourceCount, totalOffset, target[targetOffset], target[targetOffset + 1]); 90 } else { 91 int haystackLength = sourceCount - (targetCount - 2); 92 while (totalOffset < haystackLength) { 93 int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveChars(source, haystackLength, totalOffset, target[targetOffset], target[targetOffset + 1]); 94 if (indexOfResult < 0) { 95 return -1; 96 } 97 totalOffset = indexOfResult; 98 Pointer cmpSourcePointer = Word.objectToTrackedPointer(source).add(charArrayBaseOffset(INJECTED)).add(totalOffset * charArrayIndexScale(INJECTED)); 99 Pointer targetPointer = Word.objectToTrackedPointer(target).add(charArrayBaseOffset(INJECTED)).add(targetOffset * charArrayIndexScale(INJECTED)); 100 if (ArrayRegionEqualsNode.regionEquals(cmpSourcePointer, targetPointer, targetCount, JavaKind.Char)) { 101 return totalOffset; 102 } 103 totalOffset++; 104 } 105 return -1; 106 } 107 } 108 109 // Only exists in JDK <= 8 110 @MethodSubstitution(isStatic = false, optional = true) 111 public static int indexOf(String source, int ch, int origFromIndex) { 112 int fromIndex = origFromIndex; 113 final int sourceCount = source.length(); 114 if (fromIndex >= sourceCount) { 115 // Note: fromIndex might be near -1>>>1. 116 return -1; 117 } 118 if (fromIndex < 0) { 119 fromIndex = 0; 120 } 121 122 if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) { 123 char[] sourceArray = StringSubstitutions.getValue(source); 124 return AMD64ArrayIndexOf.indexOf1Char(sourceArray, sourceCount, fromIndex, (char) ch); 125 } else { 126 return indexOf(source, ch, origFromIndex); 127 } 128 } 129 130 @MethodSubstitution(isStatic = false) 131 @SuppressFBWarnings(value = "ES_COMPARING_PARAMETER_STRING_WITH_EQ", justification = "reference equality on the receiver is what we want") 132 public static int compareTo(String receiver, String anotherString) { 133 if (receiver == anotherString) { 134 return 0; 135 } 136 char[] value = StringSubstitutions.getValue(receiver); 137 char[] other = StringSubstitutions.getValue(anotherString); 138 return ArrayCompareToNode.compareTo(value, other, value.length << 1, other.length << 1, JavaKind.Char, JavaKind.Char); 139 } 140 141 }