1 /*
   2  * Copyright (c) 2013, 2015, 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.core.common.spi.ArrayOffsetProvider;
  33 import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
  34 import org.graalvm.compiler.replacements.StringSubstitutions;
  35 import org.graalvm.compiler.replacements.nodes.ArrayCompareToNode;
  36 import org.graalvm.compiler.word.Word;
  37 import jdk.internal.vm.compiler.word.Pointer;
  38 
  39 import jdk.vm.ci.meta.JavaKind;
  40 
  41 // JaCoCo Exclude
  42 
  43 /**
  44  * Substitutions for {@link java.lang.String} methods.
  45  */
  46 @ClassSubstitution(String.class)
  47 public class AMD64StringSubstitutions {
  48 
  49     @Fold
  50     static int charArrayBaseOffset(@InjectedParameter ArrayOffsetProvider arrayOffsetProvider) {
  51         return arrayOffsetProvider.arrayBaseOffset(JavaKind.Char);
  52     }
  53 
  54     @Fold
  55     static int charArrayIndexScale(@InjectedParameter ArrayOffsetProvider arrayOffsetProvider) {
  56         return arrayOffsetProvider.arrayScalingFactor(JavaKind.Char);
  57     }
  58 
  59     /** Marker value for the {@link InjectedParameter} injected parameter. */
  60     static final ArrayOffsetProvider INJECTED = null;
  61 
  62     // Only exists in JDK <= 8
  63     @MethodSubstitution(isStatic = true, optional = true)
  64     public static int indexOf(char[] source, int sourceOffset, int sourceCount,
  65                     @ConstantNodeParameter char[] target, int targetOffset, int targetCount,
  66                     int origFromIndex) {
  67         int fromIndex = origFromIndex;
  68         if (fromIndex >= sourceCount) {
  69             return (targetCount == 0 ? sourceCount : -1);
  70         }
  71         if (fromIndex < 0) {
  72             fromIndex = 0;
  73         }
  74         if (targetCount == 0) {
  75             // The empty string is in every string.
  76             return fromIndex;
  77         }
  78 
  79         int totalOffset = sourceOffset + fromIndex;
  80         if (sourceCount - fromIndex < targetCount) {
  81             // The empty string contains nothing except the empty string.
  82             return -1;
  83         }
  84         assert sourceCount - fromIndex > 0 && targetCount > 0;
  85 
  86         Pointer sourcePointer = Word.objectToTrackedPointer(source).add(charArrayBaseOffset(INJECTED)).add(totalOffset * charArrayIndexScale(INJECTED));
  87         Pointer targetPointer = Word.objectToTrackedPointer(target).add(charArrayBaseOffset(INJECTED)).add(targetOffset * charArrayIndexScale(INJECTED));
  88         int result = AMD64StringIndexOfNode.optimizedStringIndexPointer(sourcePointer, sourceCount - fromIndex, targetPointer, targetCount);
  89         if (result >= 0) {
  90             return result + totalOffset;
  91         }
  92         return result;
  93     }
  94 
  95     // Only exists in JDK <= 8
  96     @MethodSubstitution(isStatic = false, optional = true)
  97     public static int indexOf(String source, int ch, int origFromIndex) {
  98         int fromIndex = origFromIndex;
  99         final int sourceCount = source.length();
 100         if (fromIndex >= sourceCount) {
 101             // Note: fromIndex might be near -1>>>1.
 102             return -1;
 103         }
 104         if (fromIndex < 0) {
 105             fromIndex = 0;
 106         }
 107 
 108         if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
 109             char[] sourceArray = StringSubstitutions.getValue(source);
 110 
 111             Pointer sourcePointer = Word.objectToTrackedPointer(sourceArray).add(charArrayBaseOffset(INJECTED)).add(fromIndex * charArrayIndexScale(INJECTED));
 112             int result = AMD64ArrayIndexOfNode.optimizedArrayIndexOf(sourcePointer, sourceCount - fromIndex, (char) ch, JavaKind.Char);
 113             if (result != -1) {
 114                 return result + fromIndex;
 115             }
 116             return result;
 117         } else {
 118             return indexOf(source, ch, origFromIndex);
 119         }
 120     }
 121 
 122     @MethodSubstitution(isStatic = false)
 123     @SuppressFBWarnings(value = "ES_COMPARING_PARAMETER_STRING_WITH_EQ", justification = "reference equality on the receiver is what we want")
 124     public static int compareTo(String receiver, String anotherString) {
 125         if (receiver == anotherString) {
 126             return 0;
 127         }
 128         char[] value = StringSubstitutions.getValue(receiver);
 129         char[] other = StringSubstitutions.getValue(anotherString);
 130         return ArrayCompareToNode.compareTo(value, other, value.length << 1, other.length << 1, JavaKind.Char, JavaKind.Char);
 131     }
 132 
 133 }