0
|
1 /*
|
|
2 * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 * have any questions.
|
|
22 *
|
|
23 */
|
|
24
|
|
25 package sun.jvm.hotspot.oops;
|
|
26
|
|
27 import java.io.*;
|
|
28 import java.util.*;
|
|
29 import sun.jvm.hotspot.code.*;
|
|
30 import sun.jvm.hotspot.debugger.*;
|
|
31 import sun.jvm.hotspot.interpreter.*;
|
|
32 import sun.jvm.hotspot.memory.*;
|
|
33 import sun.jvm.hotspot.runtime.*;
|
|
34 import sun.jvm.hotspot.types.*;
|
|
35 import sun.jvm.hotspot.utilities.*;
|
|
36
|
|
37 public class ConstMethod extends Oop {
|
|
38 static {
|
|
39 VM.registerVMInitializedObserver(new Observer() {
|
|
40 public void update(Observable o, Object data) {
|
|
41 initialize(VM.getVM().getTypeDataBase());
|
|
42 }
|
|
43 });
|
|
44 }
|
|
45
|
|
46 // anon-enum constants for _flags.
|
|
47 private static int HAS_LINENUMBER_TABLE;
|
|
48 private static int HAS_CHECKED_EXCEPTIONS;
|
|
49 private static int HAS_LOCALVARIABLE_TABLE;
|
|
50
|
|
51 private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {
|
|
52 Type type = db.lookupType("constMethodOopDesc");
|
|
53 // Backpointer to non-const methodOop
|
|
54 method = new OopField(type.getOopField("_method"), 0);
|
|
55 // The exception handler table. 4-tuples of ints [start_pc, end_pc,
|
|
56 // handler_pc, catch_type index] For methods with no exceptions the
|
|
57 // table is pointing to Universe::the_empty_int_array
|
|
58 exceptionTable = new OopField(type.getOopField("_exception_table"), 0);
|
|
59 constMethodSize = new CIntField(type.getCIntegerField("_constMethod_size"), 0);
|
|
60 flags = new ByteField(type.getJByteField("_flags"), 0);
|
|
61
|
|
62 // enum constants for flags
|
|
63 HAS_LINENUMBER_TABLE = db.lookupIntConstant("constMethodOopDesc::_has_linenumber_table").intValue();
|
|
64 HAS_CHECKED_EXCEPTIONS = db.lookupIntConstant("constMethodOopDesc::_has_checked_exceptions").intValue();
|
|
65 HAS_LOCALVARIABLE_TABLE = db.lookupIntConstant("constMethodOopDesc::_has_localvariable_table").intValue();
|
|
66
|
|
67 // Size of Java bytecodes allocated immediately after constMethodOop.
|
|
68 codeSize = new CIntField(type.getCIntegerField("_code_size"), 0);
|
|
69 nameIndex = new CIntField(type.getCIntegerField("_name_index"), 0);
|
|
70 signatureIndex = new CIntField(type.getCIntegerField("_signature_index"), 0);
|
|
71 genericSignatureIndex = new CIntField(type.getCIntegerField("_generic_signature_index"),0);
|
|
72
|
|
73 // start of byte code
|
|
74 bytecodeOffset = type.getSize();
|
|
75
|
|
76 type = db.lookupType("CheckedExceptionElement");
|
|
77 checkedExceptionElementSize = type.getSize();
|
|
78
|
|
79 type = db.lookupType("LocalVariableTableElement");
|
|
80 localVariableTableElementSize = type.getSize();
|
|
81 }
|
|
82
|
|
83 ConstMethod(OopHandle handle, ObjectHeap heap) {
|
|
84 super(handle, heap);
|
|
85 }
|
|
86
|
|
87 // Fields
|
|
88 private static OopField method;
|
|
89 private static OopField exceptionTable;
|
|
90 private static CIntField constMethodSize;
|
|
91 private static ByteField flags;
|
|
92 private static CIntField codeSize;
|
|
93 private static CIntField nameIndex;
|
|
94 private static CIntField signatureIndex;
|
|
95 private static CIntField genericSignatureIndex;
|
|
96
|
|
97 // start of bytecode
|
|
98 private static long bytecodeOffset;
|
|
99
|
|
100 private static long checkedExceptionElementSize;
|
|
101 private static long localVariableTableElementSize;
|
|
102
|
|
103 // Accessors for declared fields
|
|
104 public Method getMethod() {
|
|
105 return (Method) method.getValue(this);
|
|
106 }
|
|
107
|
|
108 public TypeArray getExceptionTable() {
|
|
109 return (TypeArray) exceptionTable.getValue(this);
|
|
110 }
|
|
111
|
|
112 public long getConstMethodSize() {
|
|
113 return constMethodSize.getValue(this);
|
|
114 }
|
|
115
|
|
116 public byte getFlags() {
|
|
117 return flags.getValue(this);
|
|
118 }
|
|
119
|
|
120 public long getCodeSize() {
|
|
121 return codeSize.getValue(this);
|
|
122 }
|
|
123
|
|
124 public long getNameIndex() {
|
|
125 return nameIndex.getValue(this);
|
|
126 }
|
|
127
|
|
128 public long getSignatureIndex() {
|
|
129 return signatureIndex.getValue(this);
|
|
130 }
|
|
131
|
|
132 public long getGenericSignatureIndex() {
|
|
133 return genericSignatureIndex.getValue(this);
|
|
134 }
|
|
135
|
|
136 public Symbol getName() {
|
|
137 return getMethod().getName();
|
|
138 }
|
|
139
|
|
140 public Symbol getSignature() {
|
|
141 return getMethod().getSignature();
|
|
142 }
|
|
143
|
|
144 public Symbol getGenericSignature() {
|
|
145 return getMethod().getGenericSignature();
|
|
146 }
|
|
147
|
|
148 // bytecode accessors
|
|
149
|
|
150 /** Get a bytecode or breakpoint at the given bci */
|
|
151 public int getBytecodeOrBPAt(int bci) {
|
|
152 return getHandle().getJByteAt(bytecodeOffset + bci) & 0xFF;
|
|
153 }
|
|
154
|
|
155 public byte getBytecodeByteArg(int bci) {
|
|
156 return (byte) getBytecodeOrBPAt(bci);
|
|
157 }
|
|
158
|
|
159 /** Fetches a 16-bit big-endian ("Java ordered") value from the
|
|
160 bytecode stream */
|
|
161 public short getBytecodeShortArg(int bci) {
|
|
162 int hi = getBytecodeOrBPAt(bci);
|
|
163 int lo = getBytecodeOrBPAt(bci + 1);
|
|
164 return (short) ((hi << 8) | lo);
|
|
165 }
|
|
166
|
|
167 /** Fetches a 32-bit big-endian ("Java ordered") value from the
|
|
168 bytecode stream */
|
|
169 public int getBytecodeIntArg(int bci) {
|
|
170 int b4 = getBytecodeOrBPAt(bci);
|
|
171 int b3 = getBytecodeOrBPAt(bci + 1);
|
|
172 int b2 = getBytecodeOrBPAt(bci + 2);
|
|
173 int b1 = getBytecodeOrBPAt(bci + 3);
|
|
174
|
|
175 return (b4 << 24) | (b3 << 16) | (b2 << 8) | b1;
|
|
176 }
|
|
177
|
|
178 public byte[] getByteCode() {
|
|
179 byte[] bc = new byte[ (int) getCodeSize() ];
|
|
180 for( int i=0; i < bc.length; i++ )
|
|
181 {
|
|
182 long offs = bytecodeOffset + i;
|
|
183 bc[i] = getHandle().getJByteAt( offs );
|
|
184 }
|
|
185 return bc;
|
|
186 }
|
|
187
|
|
188 public long getObjectSize() {
|
|
189 return getConstMethodSize() * getHeap().getOopSize();
|
|
190 }
|
|
191
|
|
192 public void printValueOn(PrintStream tty) {
|
|
193 tty.print("ConstMethod " + getName().asString() + getSignature().asString() + "@" + getHandle());
|
|
194 }
|
|
195
|
|
196 public void iterateFields(OopVisitor visitor, boolean doVMFields) {
|
|
197 super.iterateFields(visitor, doVMFields);
|
|
198 if (doVMFields) {
|
|
199 visitor.doOop(method, true);
|
|
200 visitor.doOop(exceptionTable, true);
|
|
201 visitor.doCInt(constMethodSize, true);
|
|
202 visitor.doByte(flags, true);
|
|
203 visitor.doCInt(codeSize, true);
|
|
204 visitor.doCInt(nameIndex, true);
|
|
205 visitor.doCInt(signatureIndex, true);
|
|
206 visitor.doCInt(genericSignatureIndex, true);
|
|
207 visitor.doCInt(codeSize, true);
|
|
208 }
|
|
209 }
|
|
210
|
|
211 // Accessors
|
|
212
|
|
213 public boolean hasLineNumberTable() {
|
|
214 return (getFlags() & HAS_LINENUMBER_TABLE) != 0;
|
|
215 }
|
|
216
|
|
217 public int getLineNumberFromBCI(int bci) {
|
|
218 if (!VM.getVM().isCore()) {
|
|
219 if (bci == DebugInformationRecorder.SYNCHRONIZATION_ENTRY_BCI) bci = 0;
|
|
220 }
|
|
221
|
|
222 if (isNative()) {
|
|
223 return -1;
|
|
224 }
|
|
225
|
|
226 if (Assert.ASSERTS_ENABLED) {
|
|
227 Assert.that(bci == 0 || 0 <= bci && bci < getCodeSize(), "illegal bci");
|
|
228 }
|
|
229 int bestBCI = 0;
|
|
230 int bestLine = -1;
|
|
231 if (hasLineNumberTable()) {
|
|
232 // The line numbers are a short array of 2-tuples [start_pc, line_number].
|
|
233 // Not necessarily sorted and not necessarily one-to-one.
|
|
234 CompressedLineNumberReadStream stream =
|
|
235 new CompressedLineNumberReadStream(getHandle(), (int) offsetOfCompressedLineNumberTable());
|
|
236 while (stream.readPair()) {
|
|
237 if (stream.bci() == bci) {
|
|
238 // perfect match
|
|
239 return stream.line();
|
|
240 } else {
|
|
241 // update best_bci/line
|
|
242 if (stream.bci() < bci && stream.bci() >= bestBCI) {
|
|
243 bestBCI = stream.bci();
|
|
244 bestLine = stream.line();
|
|
245 }
|
|
246 }
|
|
247 }
|
|
248 }
|
|
249 return bestLine;
|
|
250 }
|
|
251
|
|
252 public LineNumberTableElement[] getLineNumberTable() {
|
|
253 if (Assert.ASSERTS_ENABLED) {
|
|
254 Assert.that(hasLineNumberTable(),
|
|
255 "should only be called if table is present");
|
|
256 }
|
|
257 int len = getLineNumberTableLength();
|
|
258 CompressedLineNumberReadStream stream =
|
|
259 new CompressedLineNumberReadStream(getHandle(), (int) offsetOfCompressedLineNumberTable());
|
|
260 LineNumberTableElement[] ret = new LineNumberTableElement[len];
|
|
261
|
|
262 for (int idx = 0; idx < len; idx++) {
|
|
263 stream.readPair();
|
|
264 ret[idx] = new LineNumberTableElement(stream.bci(), stream.line());
|
|
265 }
|
|
266 return ret;
|
|
267 }
|
|
268
|
|
269 public boolean hasLocalVariableTable() {
|
|
270 return (getFlags() & HAS_LOCALVARIABLE_TABLE) != 0;
|
|
271 }
|
|
272
|
|
273 public Symbol getLocalVariableName(int bci, int slot) {
|
|
274 return getMethod().getLocalVariableName(bci, slot);
|
|
275 }
|
|
276
|
|
277 /** Should only be called if table is present */
|
|
278 public LocalVariableTableElement[] getLocalVariableTable() {
|
|
279 if (Assert.ASSERTS_ENABLED) {
|
|
280 Assert.that(hasLocalVariableTable(), "should only be called if table is present");
|
|
281 }
|
|
282 LocalVariableTableElement[] ret = new LocalVariableTableElement[getLocalVariableTableLength()];
|
|
283 long offset = offsetOfLocalVariableTable();
|
|
284 for (int i = 0; i < ret.length; i++) {
|
|
285 ret[i] = new LocalVariableTableElement(getHandle(), offset);
|
|
286 offset += localVariableTableElementSize;
|
|
287 }
|
|
288 return ret;
|
|
289 }
|
|
290
|
|
291 public boolean hasCheckedExceptions() {
|
|
292 return (getFlags() & HAS_CHECKED_EXCEPTIONS) != 0;
|
|
293 }
|
|
294
|
|
295 public CheckedExceptionElement[] getCheckedExceptions() {
|
|
296 if (Assert.ASSERTS_ENABLED) {
|
|
297 Assert.that(hasCheckedExceptions(), "should only be called if table is present");
|
|
298 }
|
|
299 CheckedExceptionElement[] ret = new CheckedExceptionElement[getCheckedExceptionsLength()];
|
|
300 long offset = offsetOfCheckedExceptions();
|
|
301 for (int i = 0; i < ret.length; i++) {
|
|
302 ret[i] = new CheckedExceptionElement(getHandle(), offset);
|
|
303 offset += checkedExceptionElementSize;
|
|
304 }
|
|
305 return ret;
|
|
306 }
|
|
307
|
|
308
|
|
309 //---------------------------------------------------------------------------
|
|
310 // Internals only below this point
|
|
311 //
|
|
312
|
|
313 private boolean isNative() {
|
|
314 return getMethod().isNative();
|
|
315 }
|
|
316
|
|
317 // Offset of end of code
|
|
318 private long offsetOfCodeEnd() {
|
|
319 return bytecodeOffset + getCodeSize();
|
|
320 }
|
|
321
|
|
322 // Offset of start of compressed line number table (see methodOop.hpp)
|
|
323 private long offsetOfCompressedLineNumberTable() {
|
|
324 return offsetOfCodeEnd() + (isNative() ? 2 * VM.getVM().getAddressSize() : 0);
|
|
325 }
|
|
326
|
|
327 // Offset of last short in methodOop
|
|
328 private long offsetOfLastU2Element() {
|
|
329 return getObjectSize() - 2;
|
|
330 }
|
|
331
|
|
332 private long offsetOfCheckedExceptionsLength() {
|
|
333 return offsetOfLastU2Element();
|
|
334 }
|
|
335
|
|
336 private int getCheckedExceptionsLength() {
|
|
337 if (hasCheckedExceptions()) {
|
|
338 return (int) getHandle().getCIntegerAt(offsetOfCheckedExceptionsLength(), 2, true);
|
|
339 } else {
|
|
340 return 0;
|
|
341 }
|
|
342 }
|
|
343
|
|
344 // Offset of start of checked exceptions
|
|
345 private long offsetOfCheckedExceptions() {
|
|
346 long offset = offsetOfCheckedExceptionsLength();
|
|
347 long length = getCheckedExceptionsLength();
|
|
348 if (Assert.ASSERTS_ENABLED) {
|
|
349 Assert.that(length > 0, "should only be called if table is present");
|
|
350 }
|
|
351 offset -= length * checkedExceptionElementSize;
|
|
352 return offset;
|
|
353 }
|
|
354
|
|
355 private int getLineNumberTableLength() {
|
|
356 int len = 0;
|
|
357 if (hasLineNumberTable()) {
|
|
358 CompressedLineNumberReadStream stream =
|
|
359 new CompressedLineNumberReadStream(getHandle(), (int) offsetOfCompressedLineNumberTable());
|
|
360 while (stream.readPair()) {
|
|
361 len += 1;
|
|
362 }
|
|
363 }
|
|
364 return len;
|
|
365 }
|
|
366
|
|
367 private int getLocalVariableTableLength() {
|
|
368 if (hasLocalVariableTable()) {
|
|
369 return (int) getHandle().getCIntegerAt(offsetOfLocalVariableTableLength(), 2, true);
|
|
370 } else {
|
|
371 return 0;
|
|
372 }
|
|
373 }
|
|
374
|
|
375 // Offset of local variable table length
|
|
376 private long offsetOfLocalVariableTableLength() {
|
|
377 if (Assert.ASSERTS_ENABLED) {
|
|
378 Assert.that(hasLocalVariableTable(), "should only be called if table is present");
|
|
379 }
|
|
380 if (hasCheckedExceptions()) {
|
|
381 return offsetOfCheckedExceptions() - 2;
|
|
382 } else {
|
|
383 return offsetOfLastU2Element();
|
|
384 }
|
|
385 }
|
|
386
|
|
387 private long offsetOfLocalVariableTable() {
|
|
388 long offset = offsetOfLocalVariableTableLength();
|
|
389 long length = getLocalVariableTableLength();
|
|
390 if (Assert.ASSERTS_ENABLED) {
|
|
391 Assert.that(length > 0, "should only be called if table is present");
|
|
392 }
|
|
393 offset -= length * localVariableTableElementSize;
|
|
394 return offset;
|
|
395 }
|
|
396
|
|
397 }
|