Mercurial > hg > truffle
comparison graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/tutorial/StaticAnalysisTests.java @ 19041:9a659d65bddd
Examples for Graal tutorial
author | Christian Wimmer <christian.wimmer@oracle.com> |
---|---|
date | Thu, 29 Jan 2015 16:33:33 -0800 |
parents | |
children | 75da87c96605 |
comparison
equal
deleted
inserted
replaced
19040:a143c9cafe16 | 19041:9a659d65bddd |
---|---|
1 /* | |
2 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.graal.compiler.test.tutorial; | |
24 | |
25 import java.lang.reflect.*; | |
26 import java.util.*; | |
27 | |
28 import org.junit.*; | |
29 | |
30 import com.oracle.graal.api.meta.*; | |
31 import com.oracle.graal.api.runtime.*; | |
32 import com.oracle.graal.compiler.target.*; | |
33 import com.oracle.graal.compiler.test.tutorial.StaticAnalysis.MethodState; | |
34 import com.oracle.graal.compiler.test.tutorial.StaticAnalysis.TypeFlow; | |
35 import com.oracle.graal.phases.util.*; | |
36 import com.oracle.graal.runtime.*; | |
37 | |
38 public class StaticAnalysisTests { | |
39 | |
40 static class A { | |
41 Object foo(Object arg) { | |
42 return arg; | |
43 } | |
44 } | |
45 | |
46 static class B extends A { | |
47 @Override | |
48 Object foo(Object arg) { | |
49 if (arg instanceof Data) { | |
50 return ((Data) arg).f; | |
51 } else { | |
52 return super.foo(arg); | |
53 } | |
54 } | |
55 } | |
56 | |
57 static class Data { | |
58 Object f; | |
59 } | |
60 | |
61 private MetaAccessProvider metaAccess; | |
62 | |
63 public StaticAnalysisTests() { | |
64 Backend backend = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend(); | |
65 Providers providers = backend.getProviders(); | |
66 this.metaAccess = providers.getMetaAccess(); | |
67 } | |
68 | |
69 static void test01Entry() { | |
70 A a = new A(); | |
71 a.foo(null); | |
72 } | |
73 | |
74 @Test | |
75 public void test01() { | |
76 StaticAnalysis sa = new StaticAnalysis(metaAccess); | |
77 sa.addMethod(findMethod(StaticAnalysisTests.class, "test01Entry")); | |
78 sa.finish(); | |
79 | |
80 assertEquals(sa.getResults().getAllInstantiatedTypes(), t(A.class)); | |
81 assertEquals(f(sa, Data.class, "f")); | |
82 assertEquals(m(sa, A.class, "foo").getFormalParameters()[0], t(A.class)); | |
83 assertEquals(m(sa, A.class, "foo").getFormalParameters()[1]); | |
84 assertEquals(m(sa, A.class, "foo").getFormalReturn()); | |
85 } | |
86 | |
87 static void test02Entry() { | |
88 A a = new A(); | |
89 a.foo(new Data()); | |
90 | |
91 B b = new B(); | |
92 b.foo(null); | |
93 } | |
94 | |
95 @Test | |
96 public void test02() { | |
97 StaticAnalysis sa = new StaticAnalysis(metaAccess); | |
98 sa.addMethod(findMethod(StaticAnalysisTests.class, "test02Entry")); | |
99 sa.finish(); | |
100 | |
101 assertEquals(sa.getResults().getAllInstantiatedTypes(), t(A.class), t(B.class), t(Data.class)); | |
102 assertEquals(f(sa, Data.class, "f")); | |
103 assertEquals(m(sa, A.class, "foo").getFormalParameters()[0], t(A.class), t(B.class)); | |
104 assertEquals(m(sa, A.class, "foo").getFormalParameters()[1], t(Data.class)); | |
105 assertEquals(m(sa, A.class, "foo").getFormalReturn(), t(Data.class)); | |
106 assertEquals(m(sa, B.class, "foo").getFormalParameters()[0], t(B.class)); | |
107 assertEquals(m(sa, B.class, "foo").getFormalParameters()[1]); | |
108 assertEquals(m(sa, B.class, "foo").getFormalReturn(), t(Data.class)); | |
109 } | |
110 | |
111 static void test03Entry() { | |
112 Data data = new Data(); | |
113 data.f = new Integer(42); | |
114 | |
115 A a = new A(); | |
116 a.foo(new Data()); | |
117 | |
118 B b = new B(); | |
119 b.foo(null); | |
120 } | |
121 | |
122 @Test | |
123 public void test03() { | |
124 StaticAnalysis sa = new StaticAnalysis(metaAccess); | |
125 sa.addMethod(findMethod(StaticAnalysisTests.class, "test03Entry")); | |
126 sa.finish(); | |
127 | |
128 assertEquals(sa.getResults().getAllInstantiatedTypes(), t(A.class), t(B.class), t(Data.class), t(Integer.class)); | |
129 assertEquals(f(sa, Data.class, "f"), t(Integer.class)); | |
130 assertEquals(m(sa, A.class, "foo").getFormalParameters()[0], t(A.class), t(B.class)); | |
131 assertEquals(m(sa, A.class, "foo").getFormalParameters()[1], t(Data.class)); | |
132 assertEquals(m(sa, A.class, "foo").getFormalReturn(), t(Data.class)); | |
133 assertEquals(m(sa, B.class, "foo").getFormalParameters()[0], t(B.class)); | |
134 assertEquals(m(sa, B.class, "foo").getFormalParameters()[1]); | |
135 assertEquals(m(sa, B.class, "foo").getFormalReturn(), t(Data.class), t(Integer.class)); | |
136 } | |
137 | |
138 static void test04Entry() { | |
139 Data data = null; | |
140 for (int i = 0; i < 2; i++) { | |
141 if (i == 0) { | |
142 data = new Data(); | |
143 } else if (i == 1) { | |
144 data.f = new Integer(42); | |
145 } | |
146 } | |
147 | |
148 A a = new A(); | |
149 a.foo(data); | |
150 } | |
151 | |
152 @Test | |
153 public void test04() { | |
154 StaticAnalysis sa = new StaticAnalysis(metaAccess); | |
155 sa.addMethod(findMethod(StaticAnalysisTests.class, "test04Entry")); | |
156 sa.finish(); | |
157 | |
158 assertEquals(sa.getResults().getAllInstantiatedTypes(), t(A.class), t(Data.class), t(Integer.class)); | |
159 assertEquals(f(sa, Data.class, "f"), t(Integer.class)); | |
160 assertEquals(m(sa, A.class, "foo").getFormalParameters()[0], t(A.class)); | |
161 assertEquals(m(sa, A.class, "foo").getFormalParameters()[1], t(Data.class)); | |
162 assertEquals(m(sa, A.class, "foo").getFormalReturn(), t(Data.class)); | |
163 } | |
164 | |
165 private MethodState m(StaticAnalysis sa, Class<?> declaringClass, String name) { | |
166 return sa.getResults().lookupMethod(findMethod(declaringClass, name)); | |
167 } | |
168 | |
169 private TypeFlow f(StaticAnalysis sa, Class<?> declaringClass, String name) { | |
170 return sa.getResults().lookupField(findField(declaringClass, name)); | |
171 } | |
172 | |
173 private static void assertEquals(TypeFlow actual, Object... expected) { | |
174 Collection<?> actualTypes = actual.getTypes(); | |
175 if (actualTypes.size() != expected.length || !actualTypes.containsAll(Arrays.asList(expected))) { | |
176 Assert.fail(actualTypes + " != " + Arrays.asList(expected)); | |
177 } | |
178 } | |
179 | |
180 private ResolvedJavaType t(Class<?> clazz) { | |
181 return metaAccess.lookupJavaType(clazz); | |
182 } | |
183 | |
184 private ResolvedJavaMethod findMethod(Class<?> declaringClass, String name) { | |
185 Method reflectionMethod = null; | |
186 for (Method m : declaringClass.getDeclaredMethods()) { | |
187 if (m.getName().equals(name)) { | |
188 assert reflectionMethod == null : "More than one method with name " + name + " in class " + declaringClass.getName(); | |
189 reflectionMethod = m; | |
190 } | |
191 } | |
192 assert reflectionMethod != null : "No method with name " + name + " in class " + declaringClass.getName(); | |
193 return metaAccess.lookupJavaMethod(reflectionMethod); | |
194 } | |
195 | |
196 private ResolvedJavaField findField(Class<?> declaringClass, String name) { | |
197 Field reflectionField; | |
198 try { | |
199 reflectionField = declaringClass.getDeclaredField(name); | |
200 } catch (NoSuchFieldException | SecurityException ex) { | |
201 throw new AssertionError(ex); | |
202 } | |
203 return metaAccess.lookupJavaField(reflectionField); | |
204 } | |
205 } |