Mercurial > hg > truffle
comparison graal/com.oracle.jvmci.code/src/com/oracle/jvmci/code/BytecodeFrame.java @ 21556:48c1ebd24120
renamed com.oracle.graal.api[meta|code] modules to com.oracle.jvmci.[meta|code] (JBS:GRAAL-53)
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Wed, 27 May 2015 00:36:16 +0200 |
parents | graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/BytecodeFrame.java@082417ac43e4 |
children | f5b549811bac |
comparison
equal
deleted
inserted
replaced
21555:d12eaef9af72 | 21556:48c1ebd24120 |
---|---|
1 /* | |
2 * Copyright (c) 2009, 2014, 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.jvmci.code; | |
24 | |
25 import com.oracle.jvmci.meta.Kind; | |
26 import com.oracle.jvmci.meta.ResolvedJavaMethod; | |
27 import com.oracle.jvmci.meta.Value; | |
28 import java.util.*; | |
29 | |
30 | |
31 /** | |
32 * Represents the Java bytecode frame state(s) at a given position including {@link Value locations} | |
33 * where to find the local variables, operand stack values and locked objects of the bytecode | |
34 * frame(s). | |
35 */ | |
36 public class BytecodeFrame extends BytecodePosition { | |
37 | |
38 /** | |
39 * An array of values representing how to reconstruct the state of the Java frame. This is array | |
40 * is partitioned as follows: | |
41 * <p> | |
42 * <table summary="" border="1" cellpadding="5" frame="void" rules="all"> | |
43 * <tr> | |
44 * <th>Start index (inclusive)</th> | |
45 * <th>End index (exclusive)</th> | |
46 * <th>Description</th> | |
47 * </tr> | |
48 * <tr> | |
49 * <td>0</td> | |
50 * <td>numLocals</td> | |
51 * <td>Local variables</td> | |
52 * </tr> | |
53 * <tr> | |
54 * <td>numLocals</td> | |
55 * <td>numLocals + numStack</td> | |
56 * <td>Operand stack</td> | |
57 * </tr> | |
58 * <tr> | |
59 * <td>numLocals + numStack</td> | |
60 * <td>values.length</td> | |
61 * <td>Locked objects</td> | |
62 * </tr> | |
63 * </table> | |
64 * <p> | |
65 * Note that the number of locals and the number of stack slots may be smaller than the maximum | |
66 * number of locals and stack slots as specified in the compiled method. | |
67 */ | |
68 public final Value[] values; | |
69 | |
70 /** | |
71 * The number of locals in the values array. | |
72 */ | |
73 public final int numLocals; | |
74 | |
75 /** | |
76 * The number of stack slots in the values array. | |
77 */ | |
78 public final int numStack; | |
79 | |
80 /** | |
81 * The number of locks in the values array. | |
82 */ | |
83 public final int numLocks; | |
84 | |
85 /** | |
86 * True if this is a position inside an exception handler before the exception object has been | |
87 * consumed. In this case, {@link #numStack} {@code == 1} and {@link #getStackValue(int) | |
88 * getStackValue(0)} is the location of the exception object. If deoptimization happens at this | |
89 * position, the interpreter will rethrow the exception instead of executing the bytecode | |
90 * instruction at this position. | |
91 */ | |
92 public final boolean rethrowException; | |
93 | |
94 public final boolean duringCall; | |
95 | |
96 /** | |
97 * This BCI should be used for frame states that are built for code with no meaningful BCI. | |
98 */ | |
99 public static final int UNKNOWN_BCI = -5; | |
100 | |
101 /** | |
102 * The BCI for exception unwind. This is synthetic code and has no representation in bytecode. | |
103 * In contrast with {@link #AFTER_EXCEPTION_BCI}, at this point, if the method is synchronized, | |
104 * the monitor is still held. | |
105 */ | |
106 public static final int UNWIND_BCI = -1; | |
107 | |
108 /** | |
109 * The BCI for the state before starting to execute a method. Note that if the method is | |
110 * synchronized, the monitor is not yet held. | |
111 */ | |
112 public static final int BEFORE_BCI = -2; | |
113 | |
114 /** | |
115 * The BCI for the state after finishing the execution of a method and returning normally. Note | |
116 * that if the method was synchronized the monitor is already released. | |
117 */ | |
118 public static final int AFTER_BCI = -3; | |
119 | |
120 /** | |
121 * The BCI for exception unwind. This is synthetic code and has no representation in bytecode. | |
122 * In contrast with {@link #UNWIND_BCI}, at this point, if the method is synchronized, the | |
123 * monitor is already released. | |
124 */ | |
125 public static final int AFTER_EXCEPTION_BCI = -4; | |
126 | |
127 /** | |
128 * This BCI should be used for states that cannot be the target of a deoptimization, like | |
129 * snippet frame states. | |
130 */ | |
131 public static final int INVALID_FRAMESTATE_BCI = -6; | |
132 | |
133 /** | |
134 * Determines if a given BCI matches one of the placeholder BCI constants defined in this class. | |
135 */ | |
136 public static boolean isPlaceholderBci(int bci) { | |
137 return bci < 0; | |
138 } | |
139 | |
140 /** | |
141 * Gets the name of a given placeholder BCI. | |
142 */ | |
143 public static String getPlaceholderBciName(int bci) { | |
144 assert isPlaceholderBci(bci); | |
145 if (bci == BytecodeFrame.AFTER_BCI) { | |
146 return "AFTER_BCI"; | |
147 } else if (bci == BytecodeFrame.AFTER_EXCEPTION_BCI) { | |
148 return "AFTER_EXCEPTION_BCI"; | |
149 } else if (bci == BytecodeFrame.INVALID_FRAMESTATE_BCI) { | |
150 return "INVALID_FRAMESTATE_BCI"; | |
151 } else if (bci == BytecodeFrame.BEFORE_BCI) { | |
152 return "BEFORE_BCI"; | |
153 } else if (bci == BytecodeFrame.UNKNOWN_BCI) { | |
154 return "UNKNOWN_BCI"; | |
155 } else { | |
156 assert bci == BytecodeFrame.UNWIND_BCI; | |
157 return "UNWIND_BCI"; | |
158 } | |
159 } | |
160 | |
161 /** | |
162 * Creates a new frame object. | |
163 * | |
164 * @param caller the caller frame (which may be {@code null}) | |
165 * @param method the method | |
166 * @param bci a BCI within the method | |
167 * @param rethrowException specifies if the VM should re-throw the pending exception when | |
168 * deopt'ing using this frame | |
169 * @param values the frame state {@link #values} | |
170 * @param numLocals the number of local variables | |
171 * @param numStack the depth of the stack | |
172 * @param numLocks the number of locked objects | |
173 */ | |
174 public BytecodeFrame(BytecodeFrame caller, ResolvedJavaMethod method, int bci, boolean rethrowException, boolean duringCall, Value[] values, int numLocals, int numStack, int numLocks) { | |
175 super(caller, method, bci); | |
176 assert values != null; | |
177 this.rethrowException = rethrowException; | |
178 this.duringCall = duringCall; | |
179 this.values = values; | |
180 this.numLocals = numLocals; | |
181 this.numStack = numStack; | |
182 this.numLocks = numLocks; | |
183 assert !rethrowException || numStack == 1 : "must have exception on top of the stack"; | |
184 } | |
185 | |
186 /** | |
187 * Ensure that the frame state is formatted as expected by the JVM, with null or Illegal in the | |
188 * slot following a double word item. This should really be checked in FrameState itself but | |
189 * because of Word type rewriting and alternative backends that can't be done. | |
190 */ | |
191 public boolean validateFormat(boolean derivedOk) { | |
192 if (caller() != null) { | |
193 caller().validateFormat(derivedOk); | |
194 } | |
195 for (int i = 0; i < numLocals + numStack; i++) { | |
196 if (values[i] != null) { | |
197 Kind kind = values[i].getKind(); | |
198 if (kind.needsTwoSlots()) { | |
199 assert values.length > i + 1 : String.format("missing second word %s", this); | |
200 assert values[i + 1] == null || values[i + 1].getKind() == Kind.Illegal : this; | |
201 } | |
202 assert derivedOk || ValueUtil.isIllegal(values[i]) || !values[i].getLIRKind().isDerivedReference() : "Unexpected derived value: " + values[i]; | |
203 } | |
204 } | |
205 return true; | |
206 } | |
207 | |
208 /** | |
209 * Gets the value representing the specified local variable. | |
210 * | |
211 * @param i the local variable index | |
212 * @return the value that can be used to reconstruct the local's current value | |
213 */ | |
214 public Value getLocalValue(int i) { | |
215 return values[i]; | |
216 } | |
217 | |
218 /** | |
219 * Gets the value representing the specified stack slot. | |
220 * | |
221 * @param i the stack index | |
222 * @return the value that can be used to reconstruct the stack slot's current value | |
223 */ | |
224 public Value getStackValue(int i) { | |
225 return values[i + numLocals]; | |
226 } | |
227 | |
228 /** | |
229 * Gets the value representing the specified lock. | |
230 * | |
231 * @param i the lock index | |
232 * @return the value that can be used to reconstruct the lock's current value | |
233 */ | |
234 public Value getLockValue(int i) { | |
235 return values[i + numLocals + numStack]; | |
236 } | |
237 | |
238 /** | |
239 * Gets the caller of this frame. | |
240 * | |
241 * @return {@code null} if this frame has no caller | |
242 */ | |
243 public BytecodeFrame caller() { | |
244 return (BytecodeFrame) getCaller(); | |
245 } | |
246 | |
247 @Override | |
248 public boolean equals(Object obj) { | |
249 if (this == obj) { | |
250 return true; | |
251 } | |
252 if (obj instanceof BytecodeFrame && super.equals(obj)) { | |
253 BytecodeFrame that = (BytecodeFrame) obj; | |
254 // @formatter:off | |
255 if (this.duringCall == that.duringCall && | |
256 this.rethrowException == that.rethrowException && | |
257 this.numLocals == that.numLocals && | |
258 this.numLocks == that.numLocks && | |
259 this.numStack == that.numStack && | |
260 Arrays.equals(this.values, that.values)) { | |
261 return true; | |
262 } | |
263 // @formatter:off | |
264 return true; | |
265 } | |
266 return false; | |
267 } | |
268 | |
269 @Override | |
270 public String toString() { | |
271 return CodeUtil.append(new StringBuilder(100), this).toString(); | |
272 } | |
273 } |