1 /*
   2  * Copyright (c) 2017, 2019, 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         return AMD64ArrayIndexOf.indexOf1Byte(value, length, fromIndex, (byte) ch);
 116     }
 117 
 118     @MethodSubstitution
 119     public static int indexOf(byte[] source, int sourceCount, byte[] target, int targetCount, int origFromIndex) {
 120         int fromIndex = origFromIndex;
 121         if (fromIndex >= sourceCount) {
 122             return (targetCount == 0 ? sourceCount : -1);
 123         }
 124         if (fromIndex < 0) {
 125             fromIndex = 0;
 126         }
 127         if (targetCount == 0) {
 128             // The empty string is in every string.
 129             return fromIndex;
 130         }
 131         if (sourceCount - fromIndex < targetCount) {
 132             // The empty string contains nothing except the empty string.
 133             return -1;
 134         }
 135         if (targetCount == 1) {
 136             return AMD64ArrayIndexOf.indexOf1Byte(source, sourceCount, fromIndex, target[0]);
 137         } else {
 138             int haystackLength = sourceCount - (targetCount - 2);
 139             int offset = fromIndex;
 140             while (offset < haystackLength) {
 141                 int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveBytes(source, haystackLength, offset, target[0], target[1]);
 142                 if (indexOfResult < 0) {
 143                     return -1;
 144                 }
 145                 offset = indexOfResult;
 146                 Pointer cmpSourcePointer = byteOffsetPointer(source, offset);
 147                 Pointer targetPointer = pointer(target);
 148                 if (targetCount == 2 || ArrayRegionEqualsNode.regionEquals(cmpSourcePointer, targetPointer, targetCount, JavaKind.Byte)) {
 149                     return offset;
 150                 }
 151                 offset++;
 152             }
 153             return -1;
 154         }
 155     }
 156 
 157     /**
 158      * Intrinsic for {@code java.lang.StringLatin1.inflate([BI[CII)V}.
 159      *
 160      * <pre>
 161      * @HotSpotIntrinsicCandidate
 162      * public static void inflate(byte[] src, int src_indx, char[] dst, int dst_indx, int len)
 163      * </pre>
 164      */
 165     @MethodSubstitution
 166     public static void inflate(byte[] src, int srcIndex, char[] dest, int destIndex, int len) {
 167         if (len < 0 || srcIndex < 0 || (srcIndex + len > src.length) || destIndex < 0 || (destIndex + len > dest.length)) {
 168             DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.BoundsCheckException);
 169         }
 170 
 171         // Offset calc. outside of the actual intrinsic.
 172         Pointer srcPointer = Word.objectToTrackedPointer(src).add(byteArrayBaseOffset(INJECTED)).add(srcIndex * byteArrayIndexScale(INJECTED));
 173         Pointer destPointer = Word.objectToTrackedPointer(dest).add(charArrayBaseOffset(INJECTED)).add(destIndex * charArrayIndexScale(INJECTED));
 174         AMD64StringLatin1InflateNode.inflate(srcPointer, destPointer, len, JavaKind.Char);
 175     }
 176 
 177     /**
 178      * Intrinsic for {@code }java.lang.StringLatin1.inflate([BI[BII)V}.
 179      *
 180      * <pre>
 181      * @HotSpotIntrinsicCandidate
 182      * public static void inflate(byte[] src, int src_indx, byte[] dst, int dst_indx, int len)
 183      * </pre>
 184      *
 185      * In this variant {@code dest} refers to a byte array containing 2 byte per char so
 186      * {@code destIndex} and {@code len} are in terms of char elements and have to be scaled by 2
 187      * when referring to {@code dest}
 188      */
 189     @MethodSubstitution
 190     public static void inflate(byte[] src, int srcIndex, byte[] dest, int destIndex, int len) {
 191         if (len < 0 || srcIndex < 0 || (srcIndex + len > src.length) || destIndex < 0 || (destIndex * 2 + len * 2 > dest.length)) {
 192             DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.BoundsCheckException);
 193         }
 194 
 195         // Offset calc. outside of the actual intrinsic.
 196         Pointer srcPointer = Word.objectToTrackedPointer(src).add(byteArrayBaseOffset(INJECTED)).add(srcIndex * byteArrayIndexScale(INJECTED));
 197         Pointer destPointer = Word.objectToTrackedPointer(dest).add(byteArrayBaseOffset(INJECTED)).add(destIndex * 2 * byteArrayIndexScale(INJECTED));
 198         AMD64StringLatin1InflateNode.inflate(srcPointer, destPointer, len, JavaKind.Byte);
 199     }
 200 
 201 }