comparison graal/com.oracle.max.cri/src/com/oracle/max/cri/ci/CiAssumptions.java @ 4199:aaac4894175c

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