1 /*
2  * Copyright (c) 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.core.common.spi.ForeignCallDescriptor;
28 import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
29 import org.graalvm.compiler.nodes.extended.ForeignCallNode;
30 import jdk.internal.vm.compiler.word.Pointer;
31 
32 import static org.graalvm.compiler.graph.Node.NodeIntrinsic;
33 
34 public class AMD64ArrayIndexOf {
35 
36     public static final ForeignCallDescriptor STUB_INDEX_OF_TWO_CONSECUTIVE_BYTES = new ForeignCallDescriptor(
37                     "indexOfTwoConsecutiveBytes", int.class, Pointer.class, int.class, int.class);
38     public static final ForeignCallDescriptor STUB_INDEX_OF_TWO_CONSECUTIVE_CHARS = new ForeignCallDescriptor(
39                     "indexOfTwoConsecutiveChars", int.class, Pointer.class, int.class, int.class);
40     public static final ForeignCallDescriptor STUB_INDEX_OF_1_BYTE = new ForeignCallDescriptor(
41                     "indexOf1Byte", int.class, Pointer.class, int.class, byte.class);
42     public static final ForeignCallDescriptor STUB_INDEX_OF_2_BYTES = new ForeignCallDescriptor(
43                     "indexOf2Bytes", int.class, Pointer.class, int.class, byte.class, byte.class);
44     public static final ForeignCallDescriptor STUB_INDEX_OF_3_BYTES = new ForeignCallDescriptor(
45                     "indexOf3Bytes", int.class, Pointer.class, int.class, byte.class, byte.class, byte.class);
46     public static final ForeignCallDescriptor STUB_INDEX_OF_4_BYTES = new ForeignCallDescriptor(
47                     "indexOf4Bytes", int.class, Pointer.class, int.class, byte.class, byte.class, byte.class, byte.class);
48     public static final ForeignCallDescriptor STUB_INDEX_OF_1_CHAR = new ForeignCallDescriptor(
49                     "indexOf1Char", int.class, Pointer.class, int.class, char.class);
50     public static final ForeignCallDescriptor STUB_INDEX_OF_2_CHARS = new ForeignCallDescriptor(
51                     "indexOf2Chars", int.class, Pointer.class, int.class, char.class, char.class);
52     public static final ForeignCallDescriptor STUB_INDEX_OF_3_CHARS = new ForeignCallDescriptor(
53                     "indexOf3Chars", int.class, Pointer.class, int.class, char.class, char.class, char.class);
54     public static final ForeignCallDescriptor STUB_INDEX_OF_4_CHARS = new ForeignCallDescriptor(
55                     "indexOf4Chars", int.class, Pointer.class, int.class, char.class, char.class, char.class, char.class);
56 
indexOfTwoConsecutiveBytes(Pointer arrayPointer, int arrayLength, byte b1, byte b2)57     public static int indexOfTwoConsecutiveBytes(Pointer arrayPointer, int arrayLength, byte b1, byte b2) {
58         int searchValue = (Byte.toUnsignedInt(b2) << Byte.SIZE) | Byte.toUnsignedInt(b1);
59         return callInt(STUB_INDEX_OF_TWO_CONSECUTIVE_BYTES, arrayPointer, arrayLength, searchValue);
60     }
61 
indexOfTwoConsecutiveChars(Pointer arrayPointer, int arrayLength, char c1, char c2)62     public static int indexOfTwoConsecutiveChars(Pointer arrayPointer, int arrayLength, char c1, char c2) {
63         int searchValue = (c2 << Character.SIZE) | c1;
64         return callInt(STUB_INDEX_OF_TWO_CONSECUTIVE_CHARS, arrayPointer, arrayLength, searchValue);
65     }
66 
indexOf1Byte(Pointer arrayPointer, int arrayLength, byte b)67     public static int indexOf1Byte(Pointer arrayPointer, int arrayLength, byte b) {
68         return callByte1(STUB_INDEX_OF_1_BYTE, arrayPointer, arrayLength, b);
69     }
70 
indexOf2Bytes(Pointer arrayPointer, int arrayLength, byte b1, byte b2)71     public static int indexOf2Bytes(Pointer arrayPointer, int arrayLength, byte b1, byte b2) {
72         return callByte2(STUB_INDEX_OF_2_BYTES, arrayPointer, arrayLength, b1, b2);
73     }
74 
indexOf3Bytes(Pointer arrayPointer, int arrayLength, byte b1, byte b2, byte b3)75     public static int indexOf3Bytes(Pointer arrayPointer, int arrayLength, byte b1, byte b2, byte b3) {
76         return callByte3(STUB_INDEX_OF_3_BYTES, arrayPointer, arrayLength, b1, b2, b3);
77     }
78 
indexOf4Bytes(Pointer arrayPointer, int arrayLength, byte b1, byte b2, byte b3, byte b4)79     public static int indexOf4Bytes(Pointer arrayPointer, int arrayLength, byte b1, byte b2, byte b3, byte b4) {
80         return callByte4(STUB_INDEX_OF_4_BYTES, arrayPointer, arrayLength, b1, b2, b3, b4);
81     }
82 
indexOf1Char(Pointer arrayPointer, int arrayLength, char c)83     public static int indexOf1Char(Pointer arrayPointer, int arrayLength, char c) {
84         return callChar1(STUB_INDEX_OF_1_CHAR, arrayPointer, arrayLength, c);
85     }
86 
indexOf2Chars(Pointer arrayPointer, int arrayLength, char c1, char c2)87     public static int indexOf2Chars(Pointer arrayPointer, int arrayLength, char c1, char c2) {
88         return callChar2(STUB_INDEX_OF_2_CHARS, arrayPointer, arrayLength, c1, c2);
89     }
90 
indexOf3Chars(Pointer arrayPointer, int arrayLength, char c1, char c2, char c3)91     public static int indexOf3Chars(Pointer arrayPointer, int arrayLength, char c1, char c2, char c3) {
92         return callChar3(STUB_INDEX_OF_3_CHARS, arrayPointer, arrayLength, c1, c2, c3);
93     }
94 
indexOf4Chars(Pointer arrayPointer, int arrayLength, char c1, char c2, char c3, char c4)95     public static int indexOf4Chars(Pointer arrayPointer, int arrayLength, char c1, char c2, char c3, char c4) {
96         return callChar4(STUB_INDEX_OF_4_CHARS, arrayPointer, arrayLength, c1, c2, c3, c4);
97     }
98 
99     @NodeIntrinsic(value = ForeignCallNode.class)
callInt(@onstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, int v1)100     private static native int callInt(@ConstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, int v1);
101 
102     @NodeIntrinsic(value = ForeignCallNode.class)
callByte1(@onstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, byte v1)103     private static native int callByte1(@ConstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, byte v1);
104 
105     @NodeIntrinsic(value = ForeignCallNode.class)
callByte2(@onstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, byte v1, byte v2)106     private static native int callByte2(@ConstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, byte v1, byte v2);
107 
108     @NodeIntrinsic(value = ForeignCallNode.class)
callByte3(@onstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, byte v1, byte v2, byte v3)109     private static native int callByte3(@ConstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, byte v1, byte v2, byte v3);
110 
111     @NodeIntrinsic(value = ForeignCallNode.class)
callByte4(@onstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, byte v1, byte v2, byte v3, byte v4)112     private static native int callByte4(@ConstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, byte v1, byte v2, byte v3, byte v4);
113 
114     @NodeIntrinsic(value = ForeignCallNode.class)
callChar1(@onstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, char v1)115     private static native int callChar1(@ConstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, char v1);
116 
117     @NodeIntrinsic(value = ForeignCallNode.class)
callChar2(@onstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, char v1, char v2)118     private static native int callChar2(@ConstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, char v1, char v2);
119 
120     @NodeIntrinsic(value = ForeignCallNode.class)
callChar3(@onstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, char v1, char v2, char v3)121     private static native int callChar3(@ConstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, char v1, char v2, char v3);
122 
123     @NodeIntrinsic(value = ForeignCallNode.class)
callChar4(@onstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, char v1, char v2, char v3, char v4)124     private static native int callChar4(@ConstantNodeParameter ForeignCallDescriptor descriptor, Pointer arrayPointer, int arrayLength, char v1, char v2, char v3, char v4);
125 }
126