Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 public class Main {
     18 
     19   // Note that this is testing we haven't intrinsified the byte[] arraycopy version.
     20   // Once we eventually start doing it, we will need to re-adjust this test.
     21 
     22   /// CHECK-START-X86: void Main.typedCopy(java.lang.Object, byte[]) disassembly (after)
     23   /// CHECK: InvokeStaticOrDirect method_name:java.lang.System.arraycopy intrinsic:SystemArrayCopy
     24   /// CHECK-NOT:    call
     25   /// CHECK: InvokeStaticOrDirect method_name:java.lang.System.arraycopy intrinsic:SystemArrayCopy
     26   /// CHECK:        call
     27   /// CHECK: ReturnVoid
     28   public static void typedCopy(Object o, byte[] foo) {
     29     System.arraycopy(o, 1, o, 0, 1);
     30     System.arraycopy((Object)foo, 1, (Object)foo, 0, 1);  // Don't use the @hide byte[] overload.
     31   }
     32 
     33   public static void untypedCopy(Object o, Object foo) {
     34     System.arraycopy(o, 1, o, 0, 1);
     35     System.arraycopy(foo, 1, foo, 0, 1);
     36   }
     37 
     38   // Test that we still do the optimization after inlining.
     39 
     40   /// CHECK-START-X86: void Main.untypedCopyCaller(java.lang.Object, byte[]) disassembly (after)
     41   /// CHECK: InvokeStaticOrDirect method_name:java.lang.System.arraycopy intrinsic:SystemArrayCopy
     42   /// CHECK-NOT:    call
     43   /// CHECK: InvokeStaticOrDirect method_name:java.lang.System.arraycopy intrinsic:SystemArrayCopy
     44   /// CHECK:        call
     45   /// CHECK: ReturnVoid
     46   public static void untypedCopyCaller(Object o, byte[] array) {
     47     untypedCopy(o, array);
     48   }
     49 
     50   public static void assertEquals(Object one, Object two) {
     51     if (one != two) {
     52       throw new Error("Expected " + one + ", got " + two);
     53     }
     54   }
     55 
     56   public static void main(String[] args) {
     57     // Simple sanity checks.
     58     byte[] a = new byte[2];
     59     Object[] o = new Object[2];
     60 
     61     o[0] = a;
     62     o[1] = o;
     63     a[0] = 1;
     64     a[1] = 2;
     65 
     66     untypedCopyCaller(o, a);
     67     assertEquals(o[0], o);
     68     assertEquals(o[1], o);
     69     assertEquals(a[0], (byte)2);
     70     assertEquals(a[1], (byte)2);
     71 
     72     o[0] = a;
     73     o[1] = o;
     74     a[0] = 1;
     75     a[1] = 2;
     76 
     77     typedCopy(o, a);
     78     assertEquals(o[0], o);
     79     assertEquals(o[1], o);
     80     assertEquals(a[0], (byte)2);
     81     assertEquals(a[1], (byte)2);
     82   }
     83 }
     84