1 /*
2  * Copyright (C) 2015 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 extends UnresolvedSuperClass {
18 
19   /// CHECK-START: void Main.callInvokeUnresolvedStatic() register (before)
20   /// CHECK:        InvokeUnresolved invoke_type:static
callInvokeUnresolvedStatic()21   static public void callInvokeUnresolvedStatic() {
22     UnresolvedClass.staticMethod();
23   }
24 
25   /// CHECK-START: void Main.callInvokeUnresolvedVirtual(UnresolvedClass) register (before)
26   /// CHECK:        InvokeUnresolved invoke_type:virtual
callInvokeUnresolvedVirtual(UnresolvedClass c)27   static public void callInvokeUnresolvedVirtual(UnresolvedClass c) {
28     c.virtualMethod();
29   }
30 
31   /// CHECK-START: void Main.callInvokeUnresolvedInterface(UnresolvedInterface) register (before)
32   /// CHECK:        InvokeUnresolved invoke_type:interface
callInvokeUnresolvedInterface(UnresolvedInterface c)33   static public void callInvokeUnresolvedInterface(UnresolvedInterface c) {
34     c.interfaceMethod();
35   }
36 
callInvokeUnresolvedSuper(Main c)37   static public void callInvokeUnresolvedSuper(Main c) {
38     c.superMethod();
39   }
40 
41   /// CHECK-START: void Main.superMethod() register (before)
42   /// CHECK:        InvokeUnresolved invoke_type:super
superMethod()43   public void superMethod() {
44     super.superMethod();
45   }
46 
47   /// CHECK-START: void Main.callUnresolvedStaticFieldAccess() register (before)
48   /// CHECK:        UnresolvedStaticFieldSet field_type:Int8
49   /// CHECK:        UnresolvedStaticFieldSet field_type:Uint16
50   /// CHECK:        UnresolvedStaticFieldSet field_type:Int32
51   /// CHECK:        UnresolvedStaticFieldSet field_type:Int64
52   /// CHECK:        UnresolvedStaticFieldSet field_type:Float32
53   /// CHECK:        UnresolvedStaticFieldSet field_type:Float64
54   /// CHECK:        UnresolvedStaticFieldSet field_type:Reference
55 
56   /// CHECK:        UnresolvedStaticFieldGet field_type:Int8
57   /// CHECK:        UnresolvedStaticFieldGet field_type:Uint16
58   /// CHECK:        UnresolvedStaticFieldGet field_type:Int32
59   /// CHECK:        UnresolvedStaticFieldGet field_type:Int64
60   /// CHECK:        UnresolvedStaticFieldGet field_type:Float32
61   /// CHECK:        UnresolvedStaticFieldGet field_type:Float64
62   /// CHECK:        UnresolvedStaticFieldGet field_type:Reference
callUnresolvedStaticFieldAccess()63   static public void callUnresolvedStaticFieldAccess() {
64     Object o = new Object();
65     UnresolvedClass.staticByte = (byte)1;
66     UnresolvedClass.staticChar = '1';
67     UnresolvedClass.staticInt = 123456789;
68     UnresolvedClass.staticLong = 123456789123456789l;
69     UnresolvedClass.staticFloat = 123456789123456789f;
70     UnresolvedClass.staticDouble = 123456789123456789d;
71     UnresolvedClass.staticObject = o;
72 
73     expectEquals((byte)1, UnresolvedClass.staticByte);
74     expectEquals('1', UnresolvedClass.staticChar);
75     expectEquals(123456789, UnresolvedClass.staticInt);
76     expectEquals(123456789123456789l, UnresolvedClass.staticLong);
77     expectEquals(123456789123456789f, UnresolvedClass.staticFloat);
78     expectEquals(123456789123456789d, UnresolvedClass.staticDouble);
79     expectEquals(o, UnresolvedClass.staticObject);
80 
81     // Check "large" values.
82 
83     UnresolvedClass.staticByte = (byte)-1;
84     UnresolvedClass.staticChar = (char)32768;
85     UnresolvedClass.staticInt = -1;
86 
87     expectEquals((byte)-1, UnresolvedClass.staticByte);
88     expectEquals((char)32768, UnresolvedClass.staticChar);
89     expectEquals(-1, UnresolvedClass.staticInt);
90   }
91 
92   /// CHECK-START: void Main.callUnresolvedInstanceFieldAccess(UnresolvedClass) register (before)
93   /// CHECK:        UnresolvedInstanceFieldSet field_type:Int8
94   /// CHECK:        UnresolvedInstanceFieldSet field_type:Uint16
95   /// CHECK:        UnresolvedInstanceFieldSet field_type:Int32
96   /// CHECK:        UnresolvedInstanceFieldSet field_type:Int64
97   /// CHECK:        UnresolvedInstanceFieldSet field_type:Float32
98   /// CHECK:        UnresolvedInstanceFieldSet field_type:Float64
99   /// CHECK:        UnresolvedInstanceFieldSet field_type:Reference
100 
101   /// CHECK:        UnresolvedInstanceFieldGet field_type:Int8
102   /// CHECK:        UnresolvedInstanceFieldGet field_type:Uint16
103   /// CHECK:        UnresolvedInstanceFieldGet field_type:Int32
104   /// CHECK:        UnresolvedInstanceFieldGet field_type:Int64
105   /// CHECK:        UnresolvedInstanceFieldGet field_type:Float32
106   /// CHECK:        UnresolvedInstanceFieldGet field_type:Float64
107   /// CHECK:        UnresolvedInstanceFieldGet field_type:Reference
callUnresolvedInstanceFieldAccess(UnresolvedClass c)108   static public void callUnresolvedInstanceFieldAccess(UnresolvedClass c) {
109     Object o = new Object();
110     c.instanceByte = (byte)1;
111     c.instanceChar = '1';
112     c.instanceInt = 123456789;
113     c.instanceLong = 123456789123456789l;
114     c.instanceFloat = 123456789123456789f;
115     c.instanceDouble = 123456789123456789d;
116     c.instanceObject = o;
117 
118     expectEquals((byte)1, c.instanceByte);
119     expectEquals('1', c.instanceChar);
120     expectEquals(123456789, c.instanceInt);
121     expectEquals(123456789123456789l, c.instanceLong);
122     expectEquals(123456789123456789f, c.instanceFloat);
123     expectEquals(123456789123456789d, c.instanceDouble);
124     expectEquals(o, c.instanceObject);
125 
126     // Check "large" values.
127 
128     c.instanceByte = (byte)-1;
129     c.instanceChar = (char)32768;
130     c.instanceInt = -1;
131 
132     expectEquals((byte)-1, c.instanceByte);
133     expectEquals((char)32768, c.instanceChar);
134     expectEquals(-1, c.instanceInt);
135   }
136 
137   /// CHECK-START: void Main.callUnresolvedNull(UnresolvedClass) register (before)
138   /// CHECK-NOT: NullCheck
callUnresolvedNull(UnresolvedClass c)139   static public void callUnresolvedNull(UnresolvedClass c) {
140     int x = 0;
141     try {
142       x = c.instanceInt;
143       throw new Error("Expected NPE");
144     } catch (NullPointerException e) {
145       x -= 1;
146     }
147     expectEquals(-1, x);
148     try {
149       c.instanceInt = -1;
150       throw new Error("Expected NPE");
151     } catch (NullPointerException e) {
152       x -= 1;
153     }
154     expectEquals(-2, x);
155     try {
156       c.virtualMethod();
157       throw new Error("Expected NPE");
158     } catch (NullPointerException e) {
159       x -= 1;
160     }
161     expectEquals(-3, x);
162   }
163 
testInstanceOf(Object o)164   static public void testInstanceOf(Object o) {
165     if (o instanceof UnresolvedSuperClass) {
166       System.out.println("instanceof ok");
167     }
168   }
169 
testCheckCast(Object o)170   static public UnresolvedSuperClass testCheckCast(Object o) {
171     UnresolvedSuperClass c = (UnresolvedSuperClass) o;
172     System.out.println("checkcast ok");
173     return c;
174   }
175   /// CHECK-START: void Main.main(java.lang.String[]) register (before)
176   /// CHECK:        InvokeUnresolved invoke_type:direct
main(String[] args)177   static public void main(String[] args) {
178     UnresolvedClass c = new UnresolvedClass();
179     Main m = new Main();
180     callInvokeUnresolvedStatic();
181     callInvokeUnresolvedVirtual(c);
182     callInvokeUnresolvedInterface(c);
183     callInvokeUnresolvedSuper(m);
184     callUnresolvedStaticFieldAccess();
185     callUnresolvedInstanceFieldAccess(c);
186     callUnresolvedNull(null);
187     testInstanceOf(m);
188     testCheckCast(m);
189     testLicm(2);
190   }
191 
192   /// CHECK-START: void Main.testLicm(int) licm (before)
193   /// CHECK:      <<Class:l\d+>>        LoadClass                                     loop:<<LoopLabel:B\d+>>
194   /// CHECK-NEXT: <<Clinit:l\d+>>       ClinitCheck [<<Class>>]                       loop:<<LoopLabel>>
195   /// CHECK-NEXT: <<New:l\d+>>          NewInstance [<<Clinit>>]                      loop:<<LoopLabel>>
196   /// CHECK-NEXT:                       ConstructorFence [<<New>>]                    loop:<<LoopLabel>>
197   /// CHECK-NEXT:                       InvokeUnresolved [<<New>>]                    loop:<<LoopLabel>>
198 
199   /// CHECK-START: void Main.testLicm(int) licm (after)
200   /// CHECK:      <<Class:l\d+>>        LoadClass                                     loop:none
201   /// CHECK-NEXT: <<Clinit:l\d+>>       ClinitCheck [<<Class>>]                       loop:none
202   /// CHECK:      <<New:l\d+>>          NewInstance [<<Clinit>>]                      loop:<<LoopLabel:B\d+>>
203   /// CHECK-NEXT:                       ConstructorFence [<<New>>]                    loop:<<LoopLabel>>
204   /// CHECK-NEXT:                       InvokeUnresolved [<<New>>]                    loop:<<LoopLabel>>
testLicm(int count)205   static public void testLicm(int count) {
206     // Test to make sure we keep the initialization check after loading an unresolved class.
207     UnresolvedClass c;
208     int i = 0;
209     do {
210       c = new UnresolvedClass();
211     } while (i++ != count);
212   }
213 
expectEquals(byte expected, byte result)214   public static void expectEquals(byte expected, byte result) {
215     if (expected != result) {
216       throw new Error("Expected: " + expected + ", found: " + result);
217     }
218   }
219 
expectEquals(char expected, char result)220   public static void expectEquals(char expected, char result) {
221     if (expected != result) {
222       throw new Error("Expected: " + expected + ", found: " + result);
223     }
224   }
225 
expectEquals(int expected, int result)226   public static void expectEquals(int expected, int result) {
227     if (expected != result) {
228       throw new Error("Expected: " + expected + ", found: " + result);
229     }
230   }
231 
expectEquals(long expected, long result)232   public static void expectEquals(long expected, long result) {
233     if (expected != result) {
234       throw new Error("Expected: " + expected + ", found: " + result);
235     }
236   }
237 
expectEquals(float expected, float result)238   public static void expectEquals(float expected, float result) {
239     if (expected != result) {
240       throw new Error("Expected: " + expected + ", found: " + result);
241     }
242   }
243 
expectEquals(double expected, double result)244   public static void expectEquals(double expected, double result) {
245     if (expected != result) {
246       throw new Error("Expected: " + expected + ", found: " + result);
247     }
248   }
249 
expectEquals(Object expected, Object result)250   public static void expectEquals(Object expected, Object result) {
251     if (expected != result) {
252       throw new Error("Expected: " + expected + ", found: " + result);
253     }
254   }
255 }
256