author | twisti |
Tue, 24 Jul 2012 10:51:00 -0700 | |
changeset 13391 | 30245956af37 |
parent 10506 | 575ad9bccff5 |
child 13522 | 5ad4627e792a |
permissions | -rw-r--r-- |
1 | 1 |
/* |
7913 | 2 |
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. |
1 | 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 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
4567
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
4567
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
4567
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
7397 | 25 |
#ifndef SHARE_VM_CI_CISTREAMS_HPP |
26 |
#define SHARE_VM_CI_CISTREAMS_HPP |
|
27 |
||
28 |
#include "ci/ciClassList.hpp" |
|
29 |
#include "ci/ciExceptionHandler.hpp" |
|
30 |
#include "ci/ciInstanceKlass.hpp" |
|
31 |
#include "ci/ciMethod.hpp" |
|
32 |
#include "interpreter/bytecode.hpp" |
|
33 |
||
1 | 34 |
// ciBytecodeStream |
35 |
// |
|
36 |
// The class is used to iterate over the bytecodes of a method. |
|
37 |
// It hides the details of constant pool structure/access by |
|
38 |
// providing accessors for constant pool items. It returns only pure |
|
39 |
// Java bytecodes; VM-internal _fast bytecodes are translated back to |
|
40 |
// their original form during iteration. |
|
41 |
class ciBytecodeStream : StackObj { |
|
42 |
private: |
|
5688 | 43 |
// Handling for the weird bytecodes |
44 |
Bytecodes::Code next_wide_or_table(Bytecodes::Code); // Handle _wide & complicated inline table |
|
1 | 45 |
|
46 |
static Bytecodes::Code check_java(Bytecodes::Code c) { |
|
47 |
assert(Bytecodes::is_java_code(c), "should not return _fast bytecodes"); |
|
48 |
return c; |
|
49 |
} |
|
50 |
||
5688 | 51 |
static Bytecodes::Code check_defined(Bytecodes::Code c) { |
52 |
assert(Bytecodes::is_defined(c), ""); |
|
53 |
return c; |
|
54 |
} |
|
55 |
||
1 | 56 |
ciMethod* _method; // the method |
57 |
ciInstanceKlass* _holder; |
|
5882 | 58 |
ciCPCache* _cpcache; |
1 | 59 |
address _bc_start; // Start of current bytecode for table |
60 |
address _was_wide; // Address past last wide bytecode |
|
61 |
jint* _table_base; // Aligned start of last table or switch |
|
62 |
||
63 |
address _start; // Start of bytecodes |
|
64 |
address _end; // Past end of bytecodes |
|
65 |
address _pc; // Current PC |
|
66 |
Bytecodes::Code _bc; // Current bytecode |
|
5688 | 67 |
Bytecodes::Code _raw_bc; // Current bytecode, raw form |
1 | 68 |
|
69 |
void reset( address base, unsigned int size ) { |
|
70 |
_bc_start =_was_wide = 0; |
|
5882 | 71 |
_start = _pc = base; _end = base + size; |
72 |
_cpcache = NULL; |
|
73 |
} |
|
1 | 74 |
|
5688 | 75 |
void assert_wide(bool require_wide) const { |
76 |
if (require_wide) |
|
77 |
{ assert(is_wide(), "must be a wide instruction"); } |
|
78 |
else { assert(!is_wide(), "must not be a wide instruction"); } |
|
79 |
} |
|
80 |
||
7913 | 81 |
Bytecode bytecode() const { return Bytecode(this, _bc_start); } |
82 |
Bytecode next_bytecode() const { return Bytecode(this, _pc); } |
|
5688 | 83 |
|
1 | 84 |
public: |
85 |
// End-Of-Bytecodes |
|
86 |
static Bytecodes::Code EOBC() { |
|
87 |
return Bytecodes::_illegal; |
|
88 |
} |
|
89 |
||
90 |
ciBytecodeStream(ciMethod* m) { |
|
91 |
reset_to_method(m); |
|
92 |
} |
|
93 |
||
94 |
ciBytecodeStream() { |
|
95 |
reset_to_method(NULL); |
|
96 |
} |
|
97 |
||
98 |
ciMethod* method() const { return _method; } |
|
99 |
||
100 |
void reset_to_method(ciMethod* m) { |
|
101 |
_method = m; |
|
102 |
if (m == NULL) { |
|
103 |
_holder = NULL; |
|
104 |
reset(NULL, 0); |
|
105 |
} else { |
|
106 |
_holder = m->holder(); |
|
107 |
reset(m->code(), m->code_size()); |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
void reset_to_bci( int bci ); |
|
112 |
||
113 |
// Force the iterator to report a certain bci. |
|
114 |
void force_bci(int bci); |
|
115 |
||
116 |
void set_max_bci( int max ) { |
|
117 |
_end = _start + max; |
|
118 |
} |
|
119 |
||
2570
ecc7862946d4
6655646: dynamic languages need dynamically linked call sites
jrose
parents:
1
diff
changeset
|
120 |
address cur_bcp() const { return _bc_start; } // Returns bcp to current instruction |
5688 | 121 |
int next_bci() const { return _pc - _start; } |
1 | 122 |
int cur_bci() const { return _bc_start - _start; } |
2570
ecc7862946d4
6655646: dynamic languages need dynamically linked call sites
jrose
parents:
1
diff
changeset
|
123 |
int instruction_size() const { return _pc - _bc_start; } |
1 | 124 |
|
125 |
Bytecodes::Code cur_bc() const{ return check_java(_bc); } |
|
5688 | 126 |
Bytecodes::Code cur_bc_raw() const { return check_defined(_raw_bc); } |
1 | 127 |
Bytecodes::Code next_bc() { return Bytecodes::java_code((Bytecodes::Code)* _pc); } |
128 |
||
129 |
// Return current ByteCode and increment PC to next bytecode, skipping all |
|
130 |
// intermediate constants. Returns EOBC at end. |
|
131 |
// Expected usage: |
|
10506
575ad9bccff5
7078382: JSR 292: don't count method handle adapters against inlining budgets
twisti
parents:
7913
diff
changeset
|
132 |
// ciBytecodeStream iter(m); |
575ad9bccff5
7078382: JSR 292: don't count method handle adapters against inlining budgets
twisti
parents:
7913
diff
changeset
|
133 |
// while (iter.next() != ciBytecodeStream::EOBC()) { ... } |
1 | 134 |
Bytecodes::Code next() { |
135 |
_bc_start = _pc; // Capture start of bc |
|
136 |
if( _pc >= _end ) return EOBC(); // End-Of-Bytecodes |
|
137 |
||
138 |
// Fetch Java bytecode |
|
139 |
// All rewritten bytecodes maintain the size of original bytecode. |
|
5688 | 140 |
_bc = Bytecodes::java_code(_raw_bc = (Bytecodes::Code)*_pc); |
1 | 141 |
int csize = Bytecodes::length_for(_bc); // Expected size |
5688 | 142 |
_pc += csize; // Bump PC past bytecode |
143 |
if (csize == 0) { |
|
144 |
_bc = next_wide_or_table(_bc); |
|
1 | 145 |
} |
146 |
return check_java(_bc); |
|
147 |
} |
|
148 |
||
2570
ecc7862946d4
6655646: dynamic languages need dynamically linked call sites
jrose
parents:
1
diff
changeset
|
149 |
bool is_wide() const { return ( _pc == _was_wide ); } |
1 | 150 |
|
5688 | 151 |
// Does this instruction contain an index which refes into the CP cache? |
5882 | 152 |
bool has_cache_index() const { return Bytecodes::uses_cp_cache(cur_bc_raw()); } |
5688 | 153 |
|
154 |
int get_index_u1() const { |
|
7913 | 155 |
return bytecode().get_index_u1(cur_bc_raw()); |
5688 | 156 |
} |
157 |
||
5882 | 158 |
int get_index_u1_cpcache() const { |
7913 | 159 |
return bytecode().get_index_u1_cpcache(cur_bc_raw()); |
5882 | 160 |
} |
161 |
||
1 | 162 |
// Get a byte index following this bytecode. |
163 |
// If prefixed with a wide bytecode, get a wide index. |
|
164 |
int get_index() const { |
|
5882 | 165 |
assert(!has_cache_index(), "else use cpcache variant"); |
1 | 166 |
return (_pc == _was_wide) // was widened? |
5688 | 167 |
? get_index_u2(true) // yes, return wide index |
168 |
: get_index_u1(); // no, return narrow index |
|
1 | 169 |
} |
170 |
||
5688 | 171 |
// Get 2-byte index (byte swapping depending on which bytecode) |
172 |
int get_index_u2(bool is_wide = false) const { |
|
7913 | 173 |
return bytecode().get_index_u2(cur_bc_raw(), is_wide); |
1 | 174 |
} |
175 |
||
5688 | 176 |
// Get 2-byte index in native byte order. (Rewriter::rewrite makes these.) |
177 |
int get_index_u2_cpcache() const { |
|
7913 | 178 |
return bytecode().get_index_u2_cpcache(cur_bc_raw()); |
2570
ecc7862946d4
6655646: dynamic languages need dynamically linked call sites
jrose
parents:
1
diff
changeset
|
179 |
} |
ecc7862946d4
6655646: dynamic languages need dynamically linked call sites
jrose
parents:
1
diff
changeset
|
180 |
|
ecc7862946d4
6655646: dynamic languages need dynamically linked call sites
jrose
parents:
1
diff
changeset
|
181 |
// Get 4-byte index, for invokedynamic. |
5688 | 182 |
int get_index_u4() const { |
7913 | 183 |
return bytecode().get_index_u4(cur_bc_raw()); |
2570
ecc7862946d4
6655646: dynamic languages need dynamically linked call sites
jrose
parents:
1
diff
changeset
|
184 |
} |
ecc7862946d4
6655646: dynamic languages need dynamically linked call sites
jrose
parents:
1
diff
changeset
|
185 |
|
5688 | 186 |
bool has_index_u4() const { |
7913 | 187 |
return bytecode().has_index_u4(cur_bc_raw()); |
5688 | 188 |
} |
1 | 189 |
|
190 |
// Get dimensions byte (multinewarray) |
|
191 |
int get_dimensions() const { return *(unsigned char*)(_pc-1); } |
|
192 |
||
193 |
// Sign-extended index byte/short, no widening |
|
7913 | 194 |
int get_constant_u1() const { return bytecode().get_constant_u1(instruction_size()-1, cur_bc_raw()); } |
195 |
int get_constant_u2(bool is_wide = false) const { return bytecode().get_constant_u2(instruction_size()-2, cur_bc_raw(), is_wide); } |
|
1 | 196 |
|
197 |
// Get a byte signed constant for "iinc". Invalid for other bytecodes. |
|
198 |
// If prefixed with a wide bytecode, get a wide constant |
|
5688 | 199 |
int get_iinc_con() const {return (_pc==_was_wide) ? (jshort) get_constant_u2(true) : (jbyte) get_constant_u1();} |
1 | 200 |
|
201 |
// 2-byte branch offset from current pc |
|
5688 | 202 |
int get_dest() const { |
7913 | 203 |
return cur_bci() + bytecode().get_offset_s2(cur_bc_raw()); |
1 | 204 |
} |
205 |
||
206 |
// 2-byte branch offset from next pc |
|
5688 | 207 |
int next_get_dest() const { |
208 |
assert(_pc < _end, ""); |
|
7913 | 209 |
return next_bci() + next_bytecode().get_offset_s2(Bytecodes::_ifeq); |
1 | 210 |
} |
211 |
||
212 |
// 4-byte branch offset from current pc |
|
5688 | 213 |
int get_far_dest() const { |
7913 | 214 |
return cur_bci() + bytecode().get_offset_s4(cur_bc_raw()); |
1 | 215 |
} |
216 |
||
217 |
// For a lookup or switch table, return target destination |
|
218 |
int get_int_table( int index ) const { |
|
219 |
return Bytes::get_Java_u4((address)&_table_base[index]); } |
|
220 |
||
221 |
// For tableswitch - get length of offset part |
|
222 |
int get_tableswitch_length() { return get_int_table(2)-get_int_table(1)+1; } |
|
223 |
||
224 |
int get_dest_table( int index ) const { |
|
225 |
return cur_bci() + get_int_table(index); } |
|
226 |
||
227 |
// --- Constant pool access --- |
|
5882 | 228 |
int get_constant_raw_index() const; |
229 |
int get_constant_pool_index() const; |
|
230 |
int get_constant_cache_index() const; |
|
1 | 231 |
int get_field_index(); |
232 |
int get_method_index(); |
|
233 |
||
234 |
// If this bytecode is a new, newarray, multianewarray, instanceof, |
|
235 |
// or checkcast, get the referenced klass. |
|
236 |
ciKlass* get_klass(bool& will_link); |
|
237 |
int get_klass_index() const; |
|
238 |
||
239 |
// If this bytecode is one of the ldc variants, get the referenced |
|
5882 | 240 |
// constant. Do not attempt to resolve it, since that would require |
241 |
// execution of Java code. If it is not resolved, return an unloaded |
|
242 |
// object (ciConstant.as_object()->is_loaded() == false). |
|
1 | 243 |
ciConstant get_constant(); |
5882 | 244 |
constantTag get_constant_pool_tag(int index) const; |
245 |
||
246 |
// True if the klass-using bytecode points to an unresolved klass |
|
247 |
bool is_unresolved_klass() const { |
|
248 |
constantTag tag = get_constant_pool_tag(get_klass_index()); |
|
249 |
return tag.is_unresolved_klass(); |
|
250 |
} |
|
1 | 251 |
|
252 |
// If this bytecode is one of get_field, get_static, put_field, |
|
253 |
// or put_static, get the referenced field. |
|
254 |
ciField* get_field(bool& will_link); |
|
255 |
||
256 |
ciInstanceKlass* get_declared_field_holder(); |
|
257 |
int get_field_holder_index(); |
|
258 |
int get_field_signature_index(); |
|
259 |
||
260 |
// If this is a method invocation bytecode, get the invoked method. |
|
261 |
ciMethod* get_method(bool& will_link); |
|
13391
30245956af37
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
10506
diff
changeset
|
262 |
bool has_appendix(); |
30245956af37
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
10506
diff
changeset
|
263 |
ciObject* get_appendix(); |
1 | 264 |
ciKlass* get_declared_method_holder(); |
265 |
int get_method_holder_index(); |
|
13391
30245956af37
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
10506
diff
changeset
|
266 |
ciSignature* get_declared_method_signature(); |
1 | 267 |
int get_method_signature_index(); |
2570
ecc7862946d4
6655646: dynamic languages need dynamically linked call sites
jrose
parents:
1
diff
changeset
|
268 |
|
5882 | 269 |
ciCPCache* get_cpcache() const; |
4567
7fc02fbe5c7a
6893268: additional dynamic language related optimizations in C2
twisti
parents:
4566
diff
changeset
|
270 |
ciCallSite* get_call_site(); |
1 | 271 |
}; |
272 |
||
273 |
||
274 |
// ciSignatureStream |
|
275 |
// |
|
276 |
// The class is used to iterate over the elements of a method signature. |
|
277 |
class ciSignatureStream : public StackObj { |
|
278 |
private: |
|
279 |
ciSignature* _sig; |
|
280 |
int _pos; |
|
281 |
public: |
|
282 |
ciSignatureStream(ciSignature* signature) { |
|
283 |
_sig = signature; |
|
284 |
_pos = 0; |
|
285 |
} |
|
286 |
||
287 |
bool at_return_type() { return _pos == _sig->count(); } |
|
288 |
||
289 |
bool is_done() { return _pos > _sig->count(); } |
|
290 |
||
291 |
void next() { |
|
292 |
if (_pos <= _sig->count()) { |
|
293 |
_pos++; |
|
294 |
} |
|
295 |
} |
|
296 |
||
297 |
ciType* type() { |
|
298 |
if (at_return_type()) { |
|
299 |
return _sig->return_type(); |
|
300 |
} else { |
|
301 |
return _sig->type_at(_pos); |
|
302 |
} |
|
303 |
} |
|
304 |
}; |
|
305 |
||
306 |
||
307 |
// ciExceptionHandlerStream |
|
308 |
// |
|
309 |
// The class is used to iterate over the exception handlers of |
|
310 |
// a method. |
|
311 |
class ciExceptionHandlerStream : public StackObj { |
|
312 |
private: |
|
313 |
// The method whose handlers we are traversing |
|
314 |
ciMethod* _method; |
|
315 |
||
316 |
// Our current position in the list of handlers |
|
317 |
int _pos; |
|
318 |
int _end; |
|
319 |
||
320 |
ciInstanceKlass* _exception_klass; |
|
321 |
int _bci; |
|
322 |
bool _is_exact; |
|
323 |
||
324 |
public: |
|
325 |
ciExceptionHandlerStream(ciMethod* method) { |
|
326 |
_method = method; |
|
327 |
||
328 |
// Force loading of method code and handlers. |
|
329 |
_method->code(); |
|
330 |
||
331 |
_pos = 0; |
|
332 |
_end = _method->_handler_count; |
|
333 |
_exception_klass = NULL; |
|
334 |
_bci = -1; |
|
335 |
_is_exact = false; |
|
336 |
} |
|
337 |
||
338 |
ciExceptionHandlerStream(ciMethod* method, int bci, |
|
339 |
ciInstanceKlass* exception_klass = NULL, |
|
340 |
bool is_exact = false) { |
|
341 |
_method = method; |
|
342 |
||
343 |
// Force loading of method code and handlers. |
|
344 |
_method->code(); |
|
345 |
||
346 |
_pos = -1; |
|
347 |
_end = _method->_handler_count + 1; // include the rethrow handler |
|
348 |
_exception_klass = (exception_klass != NULL && exception_klass->is_loaded() |
|
349 |
? exception_klass |
|
350 |
: NULL); |
|
351 |
_bci = bci; |
|
352 |
assert(_bci >= 0, "bci out of range"); |
|
353 |
_is_exact = is_exact; |
|
354 |
next(); |
|
355 |
} |
|
356 |
||
357 |
// These methods are currently implemented in an odd way. |
|
358 |
// Count the number of handlers the iterator has ever produced |
|
359 |
// or will ever produce. Do not include the final rethrow handler. |
|
360 |
// That is, a trivial exception handler stream will have a count |
|
361 |
// of zero and produce just the rethrow handler. |
|
362 |
int count(); |
|
363 |
||
364 |
// Count the number of handlers this stream will produce from now on. |
|
365 |
// Include the current handler, and the final rethrow handler. |
|
366 |
// The remaining count will be zero iff is_done() is true, |
|
367 |
int count_remaining(); |
|
368 |
||
369 |
bool is_done() { |
|
370 |
return (_pos >= _end); |
|
371 |
} |
|
372 |
||
373 |
void next() { |
|
374 |
_pos++; |
|
375 |
if (_bci != -1) { |
|
376 |
// We are not iterating over all handlers... |
|
377 |
while (!is_done()) { |
|
378 |
ciExceptionHandler* handler = _method->_exception_handlers[_pos]; |
|
379 |
if (handler->is_in_range(_bci)) { |
|
380 |
if (handler->is_catch_all()) { |
|
381 |
// Found final active catch block. |
|
382 |
_end = _pos+1; |
|
383 |
return; |
|
384 |
} else if (_exception_klass == NULL || !handler->catch_klass()->is_loaded()) { |
|
385 |
// We cannot do any type analysis here. Must conservatively assume |
|
386 |
// catch block is reachable. |
|
387 |
return; |
|
388 |
} else if (_exception_klass->is_subtype_of(handler->catch_klass())) { |
|
389 |
// This catch clause will definitely catch the exception. |
|
390 |
// Final candidate. |
|
391 |
_end = _pos+1; |
|
392 |
return; |
|
393 |
} else if (!_is_exact && |
|
394 |
handler->catch_klass()->is_subtype_of(_exception_klass)) { |
|
395 |
// This catch block may be reachable. |
|
396 |
return; |
|
397 |
} |
|
398 |
} |
|
399 |
||
400 |
// The catch block was not pertinent. Go on. |
|
401 |
_pos++; |
|
402 |
} |
|
403 |
} else { |
|
404 |
// This is an iteration over all handlers. |
|
405 |
return; |
|
406 |
} |
|
407 |
} |
|
408 |
||
409 |
ciExceptionHandler* handler() { |
|
410 |
return _method->_exception_handlers[_pos]; |
|
411 |
} |
|
412 |
}; |
|
7397 | 413 |
|
7913 | 414 |
|
415 |
||
416 |
// Implementation for declarations in bytecode.hpp |
|
417 |
Bytecode::Bytecode(const ciBytecodeStream* stream, address bcp): _bcp(bcp != NULL ? bcp : stream->cur_bcp()), _code(Bytecodes::code_at(NULL, addr_at(0))) {} |
|
418 |
Bytecode_lookupswitch::Bytecode_lookupswitch(const ciBytecodeStream* stream): Bytecode(stream) { verify(); } |
|
419 |
Bytecode_tableswitch::Bytecode_tableswitch(const ciBytecodeStream* stream): Bytecode(stream) { verify(); } |
|
420 |
||
7397 | 421 |
#endif // SHARE_VM_CI_CISTREAMS_HPP |