blob: f2e451854b1e22ea76932cfd8d20a33a0ab2e6ac [file] [log] [blame]
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001/*
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
Roland Levillainc2abe2f2015-08-03 15:20:02 +010017// Note that $opt$ is a marker for the optimizing compiler to test
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000018// it does compile the method.
19
20public class Main {
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);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +010029
30 $opt$TestInvokeNew();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010031
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 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +010074
75 if (m.$opt$TestReturnNewObject(m) == m) {
76 throw new Error("Unexpected value returned");
77 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010078
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 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010088
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
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000097 // Test that we do NPE checks on array length.
98 exception = null;
99 try {
100 $opt$ArrayLengthOfNull(null);
101 } catch (NullPointerException e) {
102 exception = e;
103 }
104
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100105 if (exception == null) {
106 throw new Error("Missing NullPointerException");
107 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100108
109 result = $opt$InvokeVirtualMethod();
110 if (result != 42) {
111 throw new Error("Unexpected result: " + result);
112 }
Jeff Hao7d925a92015-06-23 17:34:04 -0700113
114 String s = $opt$StringInit();
115 if (!s.equals("hello world")) {
116 throw new Error("Unexpected string: " + s);
117 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100118 }
119
120 public static void invokePrivate() {
121 Main m = null;
122 m.privateMethod();
123 }
124
125 private void privateMethod() {
126 Object o = new Object();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100127 }
128
129 static int $opt$TestInvokeIntParameter(int param) {
130 return param;
131 }
132
133 static Object $opt$TestInvokeObjectParameter(Object a) {
134 forceGCStaticMethod();
135 return a;
136 }
137
138 static int $opt$TestInvokeWith2Parameters(int a, int b) {
139 return a - b;
140 }
141
142 static int $opt$TestInvokeWith3Parameters(int a, int b, int c) {
143 return a - b - c;
144 }
145
146 static int $opt$TestInvokeWith5Parameters(int a, int b, int c, int d, int e) {
147 return a - b - c - d - e;
148 }
149
150 static int $opt$TestInvokeWith7Parameters(int a, int b, int c, int d, int e, int f, int g) {
151 return a - b - c - d - e - f - g;
152 }
153
154 Object $opt$TestThisParameter(Object other) {
155 forceGCStaticMethod();
156 return other;
157 }
158
159 Object $opt$TestOtherParameter(Object other) {
160 forceGCStaticMethod();
161 return other;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000162 }
163
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100164 Object $opt$TestReturnNewObject(Object other) {
165 Object o = new Object();
166 forceGCStaticMethod();
167 return o;
168 }
169
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000170 public static void $opt$TestInvokeStatic() {
171 printStaticMethod();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100172 printStaticMethodWith2Args(1, 2);
173 printStaticMethodWith5Args(1, 2, 3, 4, 5);
174 printStaticMethodWith7Args(1, 2, 3, 4, 5, 6, 7);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000175 forceGCStaticMethod();
176 throwStaticMethod();
177 }
178
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100179 public static void $opt$TestInvokeNew() {
180 Object o = new Object();
181 forceGCStaticMethod();
182 printStaticMethodWithObjectArg(o);
183 forceGCStaticMethod();
184 }
185
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000186 public static void printStaticMethod() {
187 System.out.println("In static method");
188 }
189
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100190 public static void printStaticMethodWith2Args(int a, int b) {
191 System.out.println("In static method with 2 args " + a + " " + b);
192 }
193
194 public static void printStaticMethodWith5Args(int a, int b, int c, int d, int e) {
195 System.out.println("In static method with 5 args "
196 + a + " " + b + " " + c + " " + d + " " + e);
197 }
198
199 public static void printStaticMethodWith7Args(int a, int b, int c, int d, int e, int f, int g) {
200 System.out.println("In static method with 7 args "
201 + a + " " + b + " " + c + " " + d + " " + e + " " + f + " " + g);
202 }
203
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100204 public static void printStaticMethodWithObjectArg(Object a) {
205 System.out.println("In static method with object arg " + a.getClass());
206 }
207
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000208 public static void forceGCStaticMethod() {
209 Runtime.getRuntime().gc();
210 Runtime.getRuntime().gc();
211 Runtime.getRuntime().gc();
212 Runtime.getRuntime().gc();
213 Runtime.getRuntime().gc();
214 Runtime.getRuntime().gc();
215 System.out.println("Forced GC");
216 }
217
218 public static void throwStaticMethod() {
219 throw new Error("Error");
220 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +0100221
222 public static void $opt$SetFieldInOldObject(Main m) {
223 m.o = new Main();
224 }
225
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100226 public static int $opt$InvokeVirtualMethod() {
227 return new Main().virtualMethod();
228 }
229
230 public int virtualMethod() {
231 return 42;
232 }
233
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000234 public static int $opt$ArrayLengthOfNull(int[] array) {
235 return array.length;
236 }
237
Jeff Hao7d925a92015-06-23 17:34:04 -0700238 public static String $opt$StringInit() {
239 return new String("hello world");
240 }
241
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +0100242 Object o;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000243}