AMD64StringSubstitutions.java revision 13083:b9a173f12fe6
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 */
23package org.graalvm.compiler.replacements.amd64;
24
25import org.graalvm.api.word.Pointer;
26import org.graalvm.compiler.api.replacements.ClassSubstitution;
27import org.graalvm.compiler.api.replacements.MethodSubstitution;
28import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
29import org.graalvm.compiler.word.Word;
30
31import sun.misc.Unsafe;
32
33// JaCoCo Exclude
34
35/**
36 * Substitutions for {@link java.lang.String} methods.
37 */
38@ClassSubstitution(String.class)
39public class AMD64StringSubstitutions {
40
41    // Only exists in JDK <= 8
42    @MethodSubstitution(isStatic = true, optional = true)
43    public static int indexOf(char[] source, int sourceOffset, int sourceCount,
44                    @ConstantNodeParameter char[] target, int targetOffset, int targetCount,
45                    int origFromIndex) {
46        int fromIndex = origFromIndex;
47        if (fromIndex >= sourceCount) {
48            return (targetCount == 0 ? sourceCount : -1);
49        }
50        if (fromIndex < 0) {
51            fromIndex = 0;
52        }
53        if (targetCount == 0) {
54            // The empty string is in every string.
55            return fromIndex;
56        }
57
58        int totalOffset = sourceOffset + fromIndex;
59        if (sourceCount - fromIndex < targetCount) {
60            // The empty string contains nothing except the empty string.
61            return -1;
62        }
63        assert sourceCount - fromIndex > 0 && targetCount > 0;
64
65        Pointer sourcePointer = Word.objectToTrackedPointer(source).add(Unsafe.ARRAY_CHAR_BASE_OFFSET).add(totalOffset * Unsafe.ARRAY_CHAR_INDEX_SCALE);
66        Pointer targetPointer = Word.objectToTrackedPointer(target).add(Unsafe.ARRAY_CHAR_BASE_OFFSET).add(targetOffset * Unsafe.ARRAY_CHAR_INDEX_SCALE);
67        int result = AMD64StringIndexOfNode.optimizedStringIndexPointer(sourcePointer, sourceCount - fromIndex, targetPointer, targetCount);
68        if (result >= 0) {
69            return result + totalOffset;
70        }
71        return result;
72    }
73}
74