xref: /aosp_15_r20/art/test/537-checker-arraycopy/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker 
18*795d594fSAndroid Build Coastguard Worker public class Main {
main(String[] args)19*795d594fSAndroid Build Coastguard Worker   public static void main(String[] args) {
20*795d594fSAndroid Build Coastguard Worker     arraycopy();
21*795d594fSAndroid Build Coastguard Worker     try {
22*795d594fSAndroid Build Coastguard Worker       arraycopy(new Object());
23*795d594fSAndroid Build Coastguard Worker       throw new Error("Should not be here");
24*795d594fSAndroid Build Coastguard Worker     } catch (ArrayStoreException ase) {
25*795d594fSAndroid Build Coastguard Worker       // Ignore.
26*795d594fSAndroid Build Coastguard Worker     }
27*795d594fSAndroid Build Coastguard Worker     try {
28*795d594fSAndroid Build Coastguard Worker       arraycopy(null);
29*795d594fSAndroid Build Coastguard Worker       throw new Error("Should not be here");
30*795d594fSAndroid Build Coastguard Worker     } catch (NullPointerException npe) {
31*795d594fSAndroid Build Coastguard Worker       // Ignore.
32*795d594fSAndroid Build Coastguard Worker     }
33*795d594fSAndroid Build Coastguard Worker 
34*795d594fSAndroid Build Coastguard Worker     try {
35*795d594fSAndroid Build Coastguard Worker       arraycopy(new Object[1]);
36*795d594fSAndroid Build Coastguard Worker       throw new Error("Should not be here");
37*795d594fSAndroid Build Coastguard Worker     } catch (ArrayIndexOutOfBoundsException aiooe) {
38*795d594fSAndroid Build Coastguard Worker       // Ignore.
39*795d594fSAndroid Build Coastguard Worker     }
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker     arraycopy(new Object[2]);
42*795d594fSAndroid Build Coastguard Worker     arraycopy(new Object[2], 0);
43*795d594fSAndroid Build Coastguard Worker 
44*795d594fSAndroid Build Coastguard Worker     try {
45*795d594fSAndroid Build Coastguard Worker       arraycopy(new Object[1], 1);
46*795d594fSAndroid Build Coastguard Worker       throw new Error("Should not be here");
47*795d594fSAndroid Build Coastguard Worker     } catch (ArrayIndexOutOfBoundsException aiooe) {
48*795d594fSAndroid Build Coastguard Worker       // Ignore.
49*795d594fSAndroid Build Coastguard Worker     }
50*795d594fSAndroid Build Coastguard Worker   }
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker   /// CHECK-START-X86_64: void Main.arraycopy() disassembly (after)
53*795d594fSAndroid Build Coastguard Worker   /// CHECK:          InvokeStaticOrDirect intrinsic:SystemArrayCopy
54*795d594fSAndroid Build Coastguard Worker   /// CHECK-NOT:      test {{^[^\[].*}}, {{^[^\[].*}}
55*795d594fSAndroid Build Coastguard Worker   /// CHECK-NOT:      call
56*795d594fSAndroid Build Coastguard Worker   /// CHECK:          ReturnVoid
57*795d594fSAndroid Build Coastguard Worker   // Checks that the call is intrinsified and that there is no register test instruction
58*795d594fSAndroid Build Coastguard Worker   // when we know the source and destination are not null.
arraycopy()59*795d594fSAndroid Build Coastguard Worker   public static void arraycopy() {
60*795d594fSAndroid Build Coastguard Worker     Object[] obj = new Object[4];
61*795d594fSAndroid Build Coastguard Worker     System.arraycopy(obj, 1, obj, 0, 1);
62*795d594fSAndroid Build Coastguard Worker   }
63*795d594fSAndroid Build Coastguard Worker 
arraycopy(Object obj)64*795d594fSAndroid Build Coastguard Worker   public static void arraycopy(Object obj) {
65*795d594fSAndroid Build Coastguard Worker     System.arraycopy(obj, 1, obj, 0, 1);
66*795d594fSAndroid Build Coastguard Worker   }
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker   // Test case for having enough registers on x86 for the arraycopy intrinsic.
69*795d594fSAndroid Build Coastguard Worker   /// CHECK-START-X86: void Main.arraycopy(java.lang.Object[], int) disassembly (after)
70*795d594fSAndroid Build Coastguard Worker   /// CHECK:          InvokeStaticOrDirect intrinsic:SystemArrayCopy
71*795d594fSAndroid Build Coastguard Worker   /// CHECK-NOT:      mov {{[a-z]+}}, [esp + {{[0-9]+}}]
72*795d594fSAndroid Build Coastguard Worker   /// CHECK:          ReturnVoid
arraycopy(Object[] obj, int pos)73*795d594fSAndroid Build Coastguard Worker   public static void arraycopy(Object[] obj, int pos) {
74*795d594fSAndroid Build Coastguard Worker     System.arraycopy(obj, pos, obj, 0, obj.length);
75*795d594fSAndroid Build Coastguard Worker   }
76*795d594fSAndroid Build Coastguard Worker 
77*795d594fSAndroid Build Coastguard Worker   // Test case for having enough registers on x86 for the arraycopy intrinsic
78*795d594fSAndroid Build Coastguard Worker   // when an input is passed twice.
79*795d594fSAndroid Build Coastguard Worker   /// CHECK-START-X86: int Main.arraycopy2(java.lang.Object[], int) disassembly (after)
80*795d594fSAndroid Build Coastguard Worker   /// CHECK:          InvokeStaticOrDirect intrinsic:SystemArrayCopy
81*795d594fSAndroid Build Coastguard Worker   /// CHECK-NOT:      mov {{[a-z]+}}, [esp + {{[0-9]+}}]
82*795d594fSAndroid Build Coastguard Worker   /// CHECK:          Return
arraycopy2(Object[] obj, int pos)83*795d594fSAndroid Build Coastguard Worker   public static int arraycopy2(Object[] obj, int pos) {
84*795d594fSAndroid Build Coastguard Worker     System.arraycopy(obj, pos, obj, pos - 1, obj.length);
85*795d594fSAndroid Build Coastguard Worker     return pos;
86*795d594fSAndroid Build Coastguard Worker   }
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker   // Test case for not having enough registers on x86. The arraycopy intrinsic
89*795d594fSAndroid Build Coastguard Worker   // will ask for length to be in stack and load it.
90*795d594fSAndroid Build Coastguard Worker   /// CHECK-START-X86: int Main.arraycopy3(java.lang.Object[], java.lang.Object[], int, int, int) disassembly (after)
91*795d594fSAndroid Build Coastguard Worker   /// CHECK:          InvokeStaticOrDirect intrinsic:SystemArrayCopy
92*795d594fSAndroid Build Coastguard Worker   /// CHECK:          mov {{[a-z]+}}, [esp + {{[0-9]+}}]
93*795d594fSAndroid Build Coastguard Worker   /// CHECK:          Return
arraycopy3(Object[] obj1, Object[] obj2, int input1, int input3, int input4)94*795d594fSAndroid Build Coastguard Worker   public static int arraycopy3(Object[] obj1, Object[] obj2, int input1, int input3, int input4) {
95*795d594fSAndroid Build Coastguard Worker     System.arraycopy(obj1, input1, obj2, input3, input4);
96*795d594fSAndroid Build Coastguard Worker     System.out.println(obj1);
97*795d594fSAndroid Build Coastguard Worker     System.out.println(obj2);
98*795d594fSAndroid Build Coastguard Worker     return input1 + input3 + input4;
99*795d594fSAndroid Build Coastguard Worker   }
100*795d594fSAndroid Build Coastguard Worker }
101