1 /*
2  * Copyright (C) 2014 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 // Note that $opt$ is a marker for the optimizing compiler to ensure
18 // it does compile the method.
19 
20 public class Main {
main(String[] args)21   public static void main(String[] args) {
22     Error error = null;
23     try {
24       $opt$TestInvokeStatic();
25     } catch (Error e) {
26       error = e;
27     }
28     System.out.println(error);
29 
30     $opt$TestInvokeNew();
31 
32     int result = $opt$TestInvokeIntParameter(42);
33     if (result != 42) {
34       throw new Error("Different value returned: " + result);
35     }
36 
37 
38     $opt$TestInvokeObjectParameter(new Object());
39 
40     Object a = new Object();
41     Object b = $opt$TestInvokeObjectParameter(a);
42     if (a != b) {
43       throw new Error("Different object returned " + a + " " + b);
44     }
45 
46     result = $opt$TestInvokeWith2Parameters(10, 9);
47     if (result != 1) {
48       throw new Error("Unexpected result: " + result);
49     }
50 
51     result = $opt$TestInvokeWith3Parameters(10, 9, 1);
52     if (result != 0) {
53       throw new Error("Unexpected result: " + result);
54     }
55 
56     result = $opt$TestInvokeWith5Parameters(10000, 1000, 100, 10, 1);
57     if (result != 8889) {
58       throw new Error("Unexpected result: " + result);
59     }
60 
61     result = $opt$TestInvokeWith7Parameters(100, 6, 5, 4, 3, 2, 1);
62     if (result != 79) {
63       throw new Error("Unexpected result: " + result);
64     }
65 
66     Main m = new Main();
67     if (m.$opt$TestThisParameter(m) != m) {
68       throw new Error("Unexpected value returned");
69     }
70 
71     if (m.$opt$TestOtherParameter(new Main()) == m) {
72       throw new Error("Unexpected value returned");
73     }
74 
75     if (m.$opt$TestReturnNewObject(m) == m) {
76       throw new Error("Unexpected value returned");
77     }
78 
79     // Loop enough iterations to hope for a crash if no write barrier
80     // is emitted.
81     for (int j = 0; j < 3; j++) {
82       Main m1 = new Main();
83       $opt$SetFieldInOldObject(m1);
84       for (int i = 0; i < 1000; ++i) {
85         Object o = new byte[1024];
86       }
87     }
88 
89     // Test that we do NPE checks on invokedirect.
90     Exception exception = null;
91     try {
92       invokePrivate();
93     } catch (NullPointerException e) {
94       exception = e;
95     }
96 
97     if (exception == null) {
98       throw new Error("Missing NullPointerException");
99     }
100   }
101 
invokePrivate()102   public static void invokePrivate() {
103     Main m = null;
104     m.privateMethod();
105   }
106 
privateMethod()107   private void privateMethod() {
108     Object o = new Object();
109   }
110 
$opt$TestInvokeIntParameter(int param)111   static int $opt$TestInvokeIntParameter(int param) {
112     return param;
113   }
114 
$opt$TestInvokeObjectParameter(Object a)115   static Object $opt$TestInvokeObjectParameter(Object a) {
116     forceGCStaticMethod();
117     return a;
118   }
119 
$opt$TestInvokeWith2Parameters(int a, int b)120   static int $opt$TestInvokeWith2Parameters(int a, int b) {
121     return a - b;
122   }
123 
$opt$TestInvokeWith3Parameters(int a, int b, int c)124   static int $opt$TestInvokeWith3Parameters(int a, int b, int c) {
125     return a - b - c;
126   }
127 
$opt$TestInvokeWith5Parameters(int a, int b, int c, int d, int e)128   static int $opt$TestInvokeWith5Parameters(int a, int b, int c, int d, int e) {
129     return a - b - c - d - e;
130   }
131 
$opt$TestInvokeWith7Parameters(int a, int b, int c, int d, int e, int f, int g)132   static int $opt$TestInvokeWith7Parameters(int a, int b, int c, int d, int e, int f, int g) {
133     return a - b - c - d - e - f - g;
134   }
135 
$opt$TestThisParameter(Object other)136   Object $opt$TestThisParameter(Object other) {
137     forceGCStaticMethod();
138     return other;
139   }
140 
$opt$TestOtherParameter(Object other)141   Object $opt$TestOtherParameter(Object other) {
142     forceGCStaticMethod();
143     return other;
144   }
145 
$opt$TestReturnNewObject(Object other)146   Object $opt$TestReturnNewObject(Object other) {
147     Object o = new Object();
148     forceGCStaticMethod();
149     return o;
150   }
151 
$opt$TestInvokeStatic()152   public static void $opt$TestInvokeStatic() {
153     printStaticMethod();
154     printStaticMethodWith2Args(1, 2);
155     printStaticMethodWith5Args(1, 2, 3, 4, 5);
156     printStaticMethodWith7Args(1, 2, 3, 4, 5, 6, 7);
157     forceGCStaticMethod();
158     throwStaticMethod();
159   }
160 
$opt$TestInvokeNew()161   public static void $opt$TestInvokeNew() {
162     Object o = new Object();
163     forceGCStaticMethod();
164     printStaticMethodWithObjectArg(o);
165     forceGCStaticMethod();
166   }
167 
printStaticMethod()168   public static void printStaticMethod() {
169     System.out.println("In static method");
170   }
171 
printStaticMethodWith2Args(int a, int b)172   public static void printStaticMethodWith2Args(int a, int b) {
173     System.out.println("In static method with 2 args " + a + " " + b);
174   }
175 
printStaticMethodWith5Args(int a, int b, int c, int d, int e)176   public static void printStaticMethodWith5Args(int a, int b, int c, int d, int e) {
177     System.out.println("In static method with 5 args "
178         + a + " " + b + " " + c + " " + d + " " + e);
179   }
180 
printStaticMethodWith7Args(int a, int b, int c, int d, int e, int f, int g)181   public static void printStaticMethodWith7Args(int a, int b, int c, int d, int e, int f, int g) {
182     System.out.println("In static method with 7 args "
183         + a + " " + b + " " + c + " " + d + " " + e + " " + f + " " + g);
184   }
185 
printStaticMethodWithObjectArg(Object a)186   public static void printStaticMethodWithObjectArg(Object a) {
187     System.out.println("In static method with object arg " + a.getClass());
188   }
189 
forceGCStaticMethod()190   public static void forceGCStaticMethod() {
191     Runtime.getRuntime().gc();
192     Runtime.getRuntime().gc();
193     Runtime.getRuntime().gc();
194     Runtime.getRuntime().gc();
195     Runtime.getRuntime().gc();
196     Runtime.getRuntime().gc();
197     System.out.println("Forced GC");
198   }
199 
throwStaticMethod()200   public static void throwStaticMethod() {
201     throw new Error("Error");
202   }
203 
$opt$SetFieldInOldObject(Main m)204   public static void $opt$SetFieldInOldObject(Main m) {
205     m.o = new Main();
206   }
207 
208   Object o;
209 }
210