Mercurial > hg > truffle
comparison graal/com.oracle.max.cri/src/com/sun/cri/ci/CiAssumptions.java @ 3733:e233f5660da4
Added Java files from Maxine project.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Sat, 17 Dec 2011 19:59:18 +0100 |
parents | |
children | bc8527f3071c |
comparison
equal
deleted
inserted
replaced
3732:3e2e8b8abdaf | 3733:e233f5660da4 |
---|---|
1 /* | |
2 * Copyright (c) 2011, 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.sun.cri.ci; | |
24 | |
25 import java.io.*; | |
26 import java.util.*; | |
27 | |
28 import com.sun.cri.ri.*; | |
29 | |
30 /** | |
31 * Class for recording optimistic assumptions made during compilation. | |
32 * Recorded assumption can be visited for subsequent processing using | |
33 * an implementation of the {@link CiAssumptionProcessor} interface. | |
34 */ | |
35 public final class CiAssumptions implements Serializable, Iterable<CiAssumptions.Assumption> { | |
36 | |
37 public abstract static class Assumption implements Serializable { | |
38 } | |
39 | |
40 /** | |
41 * An assumption about a unique subtype of a given type. | |
42 */ | |
43 public static final class ConcreteSubtype extends Assumption { | |
44 /** | |
45 * Type the assumption is made about. | |
46 */ | |
47 public final RiResolvedType context; | |
48 | |
49 /** | |
50 * Assumed unique concrete sub-type of the context type. | |
51 */ | |
52 public final RiResolvedType subtype; | |
53 | |
54 public ConcreteSubtype(RiResolvedType context, RiResolvedType subtype) { | |
55 this.context = context; | |
56 this.subtype = subtype; | |
57 } | |
58 | |
59 @Override | |
60 public int hashCode() { | |
61 final int prime = 31; | |
62 int result = 1; | |
63 result = prime * result + context.hashCode(); | |
64 result = prime * result + subtype.hashCode(); | |
65 return result; | |
66 } | |
67 | |
68 @Override | |
69 public boolean equals(Object obj) { | |
70 if (obj instanceof ConcreteSubtype) { | |
71 ConcreteSubtype other = (ConcreteSubtype) obj; | |
72 return other.context == context && other.subtype == subtype; | |
73 } | |
74 return false; | |
75 } | |
76 } | |
77 | |
78 /** | |
79 * An assumption about a unique implementation of a virtual method. | |
80 */ | |
81 public static final class ConcreteMethod extends Assumption { | |
82 | |
83 /** | |
84 * A virtual (or interface) method whose unique implementation for the receiver type | |
85 * in {@link #context} is {@link #impl}. | |
86 */ | |
87 public final RiResolvedMethod method; | |
88 | |
89 /** | |
90 * A receiver type. | |
91 */ | |
92 public final RiResolvedType context; | |
93 | |
94 /** | |
95 * The unique implementation of {@link #method} for {@link #context}. | |
96 */ | |
97 public final RiResolvedMethod impl; | |
98 | |
99 public ConcreteMethod(RiResolvedMethod method, RiResolvedType context, RiResolvedMethod impl) { | |
100 this.method = method; | |
101 this.context = context; | |
102 this.impl = impl; | |
103 } | |
104 | |
105 @Override | |
106 public int hashCode() { | |
107 final int prime = 31; | |
108 int result = 1; | |
109 result = prime * result + method.hashCode(); | |
110 result = prime * result + context.hashCode(); | |
111 result = prime * result + impl.hashCode(); | |
112 return result; | |
113 } | |
114 | |
115 @Override | |
116 public boolean equals(Object obj) { | |
117 if (obj instanceof ConcreteMethod) { | |
118 ConcreteMethod other = (ConcreteMethod) obj; | |
119 return other.method == method && other.context == context && other.impl == impl; | |
120 } | |
121 return false; | |
122 } | |
123 } | |
124 | |
125 /** | |
126 * Array with the assumptions. This field is directly accessed from C++ code in the Graal/HotSpot implementation. | |
127 */ | |
128 private Assumption[] list; | |
129 | |
130 private int count; | |
131 | |
132 /** | |
133 * Returns whether any assumptions have been registered. | |
134 * @return {@code true} if at least one assumption has been registered, {@code false} otherwise. | |
135 */ | |
136 public boolean isEmpty() { | |
137 return count == 0; | |
138 } | |
139 | |
140 @Override | |
141 public Iterator<Assumption> iterator() { | |
142 return new Iterator<CiAssumptions.Assumption>() { | |
143 int index; | |
144 public void remove() { | |
145 throw new UnsupportedOperationException(); | |
146 } | |
147 public Assumption next() { | |
148 if (index >= count) { | |
149 throw new NoSuchElementException(); | |
150 } | |
151 return list[index++]; | |
152 } | |
153 public boolean hasNext() { | |
154 return index < count; | |
155 } | |
156 }; | |
157 } | |
158 | |
159 /** | |
160 * Records an assumption that the specified type has no finalizable subclasses. | |
161 * | |
162 * @param receiverType the type that is assumed to have no finalizable subclasses | |
163 * @return {@code true} if the assumption was recorded and can be assumed; {@code false} otherwise | |
164 */ | |
165 public boolean recordNoFinalizableSubclassAssumption(RiResolvedType receiverType) { | |
166 return false; | |
167 } | |
168 | |
169 /** | |
170 * Records that {@code subtype} is the only concrete subtype in the class hierarchy below {@code context}. | |
171 * @param context the root of the subtree of the class hierarchy that this assumptions is about | |
172 * @param subtype the one concrete subtype | |
173 */ | |
174 public void recordConcreteSubtype(RiResolvedType context, RiResolvedType subtype) { | |
175 record(new ConcreteSubtype(context, subtype)); | |
176 } | |
177 | |
178 /** | |
179 * Records that {@code impl} is the only possible concrete target for a virtual call to | |
180 * {@code method} with a receiver of type {@code context}. | |
181 * | |
182 * @param method a method that is the target of a virtual call | |
183 * @param context the receiver type of a call to {@code method} | |
184 * @param impl the concrete method that is the only possible target for the virtual call | |
185 */ | |
186 public void recordConcreteMethod(RiResolvedMethod method, RiResolvedType context, RiResolvedMethod impl) { | |
187 record(new ConcreteMethod(method, context, impl)); | |
188 } | |
189 | |
190 private void record(Assumption assumption) { | |
191 if (list == null) { | |
192 list = new Assumption[4]; | |
193 } else { | |
194 for (int i = 0; i < count; ++i) { | |
195 if (assumption.equals(list[i])) { | |
196 return; | |
197 } | |
198 } | |
199 } | |
200 if (list.length == count) { | |
201 Assumption[] newList = new Assumption[list.length * 2]; | |
202 for (int i = 0; i < list.length; ++i) { | |
203 newList[i] = list[i]; | |
204 } | |
205 list = newList; | |
206 } | |
207 list[count] = assumption; | |
208 count++; | |
209 } | |
210 | |
211 } |