1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/ci/ciStreams.hpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,441 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef SHARE_VM_CI_CISTREAMS_HPP 1.29 +#define SHARE_VM_CI_CISTREAMS_HPP 1.30 + 1.31 +#include "ci/ciClassList.hpp" 1.32 +#include "ci/ciExceptionHandler.hpp" 1.33 +#include "ci/ciInstanceKlass.hpp" 1.34 +#include "ci/ciMethod.hpp" 1.35 +#include "interpreter/bytecode.hpp" 1.36 + 1.37 +// ciBytecodeStream 1.38 +// 1.39 +// The class is used to iterate over the bytecodes of a method. 1.40 +// It hides the details of constant pool structure/access by 1.41 +// providing accessors for constant pool items. It returns only pure 1.42 +// Java bytecodes; VM-internal _fast bytecodes are translated back to 1.43 +// their original form during iteration. 1.44 +class ciBytecodeStream : StackObj { 1.45 +private: 1.46 + // Handling for the weird bytecodes 1.47 + Bytecodes::Code next_wide_or_table(Bytecodes::Code); // Handle _wide & complicated inline table 1.48 + 1.49 + static Bytecodes::Code check_java(Bytecodes::Code c) { 1.50 + assert(Bytecodes::is_java_code(c), "should not return _fast bytecodes"); 1.51 + return c; 1.52 + } 1.53 + 1.54 + static Bytecodes::Code check_defined(Bytecodes::Code c) { 1.55 + assert(Bytecodes::is_defined(c), ""); 1.56 + return c; 1.57 + } 1.58 + 1.59 + ciMethod* _method; // the method 1.60 + ciInstanceKlass* _holder; 1.61 + address _bc_start; // Start of current bytecode for table 1.62 + address _was_wide; // Address past last wide bytecode 1.63 + jint* _table_base; // Aligned start of last table or switch 1.64 + 1.65 + address _start; // Start of bytecodes 1.66 + address _end; // Past end of bytecodes 1.67 + address _pc; // Current PC 1.68 + Bytecodes::Code _bc; // Current bytecode 1.69 + Bytecodes::Code _raw_bc; // Current bytecode, raw form 1.70 + 1.71 + void reset( address base, unsigned int size ) { 1.72 + _bc_start =_was_wide = 0; 1.73 + _start = _pc = base; _end = base + size; 1.74 + } 1.75 + 1.76 + void assert_wide(bool require_wide) const { 1.77 + if (require_wide) 1.78 + { assert(is_wide(), "must be a wide instruction"); } 1.79 + else { assert(!is_wide(), "must not be a wide instruction"); } 1.80 + } 1.81 + 1.82 + Bytecode bytecode() const { return Bytecode(this, _bc_start); } 1.83 + Bytecode next_bytecode() const { return Bytecode(this, _pc); } 1.84 + 1.85 +public: 1.86 + // End-Of-Bytecodes 1.87 + static Bytecodes::Code EOBC() { 1.88 + return Bytecodes::_illegal; 1.89 + } 1.90 + 1.91 + ciBytecodeStream(ciMethod* m) { 1.92 + reset_to_method(m); 1.93 + } 1.94 + 1.95 + ciBytecodeStream() { 1.96 + reset_to_method(NULL); 1.97 + } 1.98 + 1.99 + ciMethod* method() const { return _method; } 1.100 + 1.101 + void reset_to_method(ciMethod* m) { 1.102 + _method = m; 1.103 + if (m == NULL) { 1.104 + _holder = NULL; 1.105 + reset(NULL, 0); 1.106 + } else { 1.107 + _holder = m->holder(); 1.108 + reset(m->code(), m->code_size()); 1.109 + } 1.110 + } 1.111 + 1.112 + void reset_to_bci( int bci ); 1.113 + 1.114 + // Force the iterator to report a certain bci. 1.115 + void force_bci(int bci); 1.116 + 1.117 + void set_max_bci( int max ) { 1.118 + _end = _start + max; 1.119 + } 1.120 + 1.121 + address cur_bcp() const { return _bc_start; } // Returns bcp to current instruction 1.122 + int next_bci() const { return _pc - _start; } 1.123 + int cur_bci() const { return _bc_start - _start; } 1.124 + int instruction_size() const { return _pc - _bc_start; } 1.125 + 1.126 + Bytecodes::Code cur_bc() const{ return check_java(_bc); } 1.127 + Bytecodes::Code cur_bc_raw() const { return check_defined(_raw_bc); } 1.128 + Bytecodes::Code next_bc() { return Bytecodes::java_code((Bytecodes::Code)* _pc); } 1.129 + 1.130 + // Return current ByteCode and increment PC to next bytecode, skipping all 1.131 + // intermediate constants. Returns EOBC at end. 1.132 + // Expected usage: 1.133 + // ciBytecodeStream iter(m); 1.134 + // while (iter.next() != ciBytecodeStream::EOBC()) { ... } 1.135 + Bytecodes::Code next() { 1.136 + _bc_start = _pc; // Capture start of bc 1.137 + if( _pc >= _end ) return EOBC(); // End-Of-Bytecodes 1.138 + 1.139 + // Fetch Java bytecode 1.140 + // All rewritten bytecodes maintain the size of original bytecode. 1.141 + _bc = Bytecodes::java_code(_raw_bc = (Bytecodes::Code)*_pc); 1.142 + int csize = Bytecodes::length_for(_bc); // Expected size 1.143 + _pc += csize; // Bump PC past bytecode 1.144 + if (csize == 0) { 1.145 + _bc = next_wide_or_table(_bc); 1.146 + } 1.147 + return check_java(_bc); 1.148 + } 1.149 + 1.150 + bool is_wide() const { return ( _pc == _was_wide ); } 1.151 + 1.152 + // Does this instruction contain an index which refes into the CP cache? 1.153 + bool has_cache_index() const { return Bytecodes::uses_cp_cache(cur_bc_raw()); } 1.154 + 1.155 + bool has_optional_appendix() { return Bytecodes::has_optional_appendix(cur_bc_raw()); } 1.156 + 1.157 + int get_index_u1() const { 1.158 + return bytecode().get_index_u1(cur_bc_raw()); 1.159 + } 1.160 + 1.161 + int get_index_u1_cpcache() const { 1.162 + return bytecode().get_index_u1_cpcache(cur_bc_raw()); 1.163 + } 1.164 + 1.165 + // Get a byte index following this bytecode. 1.166 + // If prefixed with a wide bytecode, get a wide index. 1.167 + int get_index() const { 1.168 + assert(!has_cache_index(), "else use cpcache variant"); 1.169 + return (_pc == _was_wide) // was widened? 1.170 + ? get_index_u2(true) // yes, return wide index 1.171 + : get_index_u1(); // no, return narrow index 1.172 + } 1.173 + 1.174 + // Get 2-byte index (byte swapping depending on which bytecode) 1.175 + int get_index_u2(bool is_wide = false) const { 1.176 + return bytecode().get_index_u2(cur_bc_raw(), is_wide); 1.177 + } 1.178 + 1.179 + // Get 2-byte index in native byte order. (Rewriter::rewrite makes these.) 1.180 + int get_index_u2_cpcache() const { 1.181 + return bytecode().get_index_u2_cpcache(cur_bc_raw()); 1.182 + } 1.183 + 1.184 + // Get 4-byte index, for invokedynamic. 1.185 + int get_index_u4() const { 1.186 + return bytecode().get_index_u4(cur_bc_raw()); 1.187 + } 1.188 + 1.189 + bool has_index_u4() const { 1.190 + return bytecode().has_index_u4(cur_bc_raw()); 1.191 + } 1.192 + 1.193 + // Get dimensions byte (multinewarray) 1.194 + int get_dimensions() const { return *(unsigned char*)(_pc-1); } 1.195 + 1.196 + // Sign-extended index byte/short, no widening 1.197 + int get_constant_u1() const { return bytecode().get_constant_u1(instruction_size()-1, cur_bc_raw()); } 1.198 + int get_constant_u2(bool is_wide = false) const { return bytecode().get_constant_u2(instruction_size()-2, cur_bc_raw(), is_wide); } 1.199 + 1.200 + // Get a byte signed constant for "iinc". Invalid for other bytecodes. 1.201 + // If prefixed with a wide bytecode, get a wide constant 1.202 + int get_iinc_con() const {return (_pc==_was_wide) ? (jshort) get_constant_u2(true) : (jbyte) get_constant_u1();} 1.203 + 1.204 + // 2-byte branch offset from current pc 1.205 + int get_dest() const { 1.206 + return cur_bci() + bytecode().get_offset_s2(cur_bc_raw()); 1.207 + } 1.208 + 1.209 + // 2-byte branch offset from next pc 1.210 + int next_get_dest() const { 1.211 + assert(_pc < _end, ""); 1.212 + return next_bci() + next_bytecode().get_offset_s2(Bytecodes::_ifeq); 1.213 + } 1.214 + 1.215 + // 4-byte branch offset from current pc 1.216 + int get_far_dest() const { 1.217 + return cur_bci() + bytecode().get_offset_s4(cur_bc_raw()); 1.218 + } 1.219 + 1.220 + // For a lookup or switch table, return target destination 1.221 + int get_int_table( int index ) const { 1.222 + return Bytes::get_Java_u4((address)&_table_base[index]); } 1.223 + 1.224 + // For tableswitch - get length of offset part 1.225 + int get_tableswitch_length() { return get_int_table(2)-get_int_table(1)+1; } 1.226 + 1.227 + int get_dest_table( int index ) const { 1.228 + return cur_bci() + get_int_table(index); } 1.229 + 1.230 + // --- Constant pool access --- 1.231 + int get_constant_raw_index() const; 1.232 + int get_constant_pool_index() const; 1.233 + int get_constant_cache_index() const; 1.234 + int get_field_index(); 1.235 + int get_method_index(); 1.236 + 1.237 + // If this bytecode is a new, newarray, multianewarray, instanceof, 1.238 + // or checkcast, get the referenced klass. 1.239 + ciKlass* get_klass(bool& will_link); 1.240 + int get_klass_index() const; 1.241 + 1.242 + // If this bytecode is one of the ldc variants, get the referenced 1.243 + // constant. Do not attempt to resolve it, since that would require 1.244 + // execution of Java code. If it is not resolved, return an unloaded 1.245 + // object (ciConstant.as_object()->is_loaded() == false). 1.246 + ciConstant get_constant(); 1.247 + constantTag get_constant_pool_tag(int index) const; 1.248 + 1.249 + // True if the klass-using bytecode points to an unresolved klass 1.250 + bool is_unresolved_klass() const { 1.251 + constantTag tag = get_constant_pool_tag(get_klass_index()); 1.252 + return tag.is_unresolved_klass(); 1.253 + } 1.254 + 1.255 + // If this bytecode is one of get_field, get_static, put_field, 1.256 + // or put_static, get the referenced field. 1.257 + ciField* get_field(bool& will_link); 1.258 + 1.259 + ciInstanceKlass* get_declared_field_holder(); 1.260 + int get_field_holder_index(); 1.261 + int get_field_signature_index(); 1.262 + 1.263 + ciMethod* get_method(bool& will_link, ciSignature* *declared_signature_result); 1.264 + bool has_appendix(); 1.265 + ciObject* get_appendix(); 1.266 + bool has_method_type(); 1.267 + ciMethodType* get_method_type(); 1.268 + ciKlass* get_declared_method_holder(); 1.269 + int get_method_holder_index(); 1.270 + int get_method_signature_index(); 1.271 + 1.272 + // Get the resolved references arrays from the constant pool 1.273 + ciObjArray* get_resolved_references(); 1.274 +}; 1.275 + 1.276 + 1.277 +// ciSignatureStream 1.278 +// 1.279 +// The class is used to iterate over the elements of a method signature. 1.280 +class ciSignatureStream : public StackObj { 1.281 +private: 1.282 + ciSignature* _sig; 1.283 + int _pos; 1.284 + // holder is a method's holder 1.285 + ciKlass* _holder; 1.286 +public: 1.287 + ciSignatureStream(ciSignature* signature, ciKlass* holder = NULL) { 1.288 + _sig = signature; 1.289 + _pos = 0; 1.290 + _holder = holder; 1.291 + } 1.292 + 1.293 + bool at_return_type() { return _pos == _sig->count(); } 1.294 + 1.295 + bool is_done() { return _pos > _sig->count(); } 1.296 + 1.297 + void next() { 1.298 + if (_pos <= _sig->count()) { 1.299 + _pos++; 1.300 + } 1.301 + } 1.302 + 1.303 + ciType* type() { 1.304 + if (at_return_type()) { 1.305 + return _sig->return_type(); 1.306 + } else { 1.307 + return _sig->type_at(_pos); 1.308 + } 1.309 + } 1.310 + 1.311 + // next klass in the signature 1.312 + ciKlass* next_klass() { 1.313 + ciKlass* sig_k; 1.314 + if (_holder != NULL) { 1.315 + sig_k = _holder; 1.316 + _holder = NULL; 1.317 + } else { 1.318 + while (!type()->is_klass()) { 1.319 + next(); 1.320 + } 1.321 + assert(!at_return_type(), "passed end of signature"); 1.322 + sig_k = type()->as_klass(); 1.323 + next(); 1.324 + } 1.325 + return sig_k; 1.326 + } 1.327 +}; 1.328 + 1.329 + 1.330 +// ciExceptionHandlerStream 1.331 +// 1.332 +// The class is used to iterate over the exception handlers of 1.333 +// a method. 1.334 +class ciExceptionHandlerStream : public StackObj { 1.335 +private: 1.336 + // The method whose handlers we are traversing 1.337 + ciMethod* _method; 1.338 + 1.339 + // Our current position in the list of handlers 1.340 + int _pos; 1.341 + int _end; 1.342 + 1.343 + ciInstanceKlass* _exception_klass; 1.344 + int _bci; 1.345 + bool _is_exact; 1.346 + 1.347 +public: 1.348 + ciExceptionHandlerStream(ciMethod* method) { 1.349 + _method = method; 1.350 + 1.351 + // Force loading of method code and handlers. 1.352 + _method->code(); 1.353 + 1.354 + _pos = 0; 1.355 + _end = _method->_handler_count; 1.356 + _exception_klass = NULL; 1.357 + _bci = -1; 1.358 + _is_exact = false; 1.359 + } 1.360 + 1.361 + ciExceptionHandlerStream(ciMethod* method, int bci, 1.362 + ciInstanceKlass* exception_klass = NULL, 1.363 + bool is_exact = false) { 1.364 + _method = method; 1.365 + 1.366 + // Force loading of method code and handlers. 1.367 + _method->code(); 1.368 + 1.369 + _pos = -1; 1.370 + _end = _method->_handler_count + 1; // include the rethrow handler 1.371 + _exception_klass = (exception_klass != NULL && exception_klass->is_loaded() 1.372 + ? exception_klass 1.373 + : NULL); 1.374 + _bci = bci; 1.375 + assert(_bci >= 0, "bci out of range"); 1.376 + _is_exact = is_exact; 1.377 + next(); 1.378 + } 1.379 + 1.380 + // These methods are currently implemented in an odd way. 1.381 + // Count the number of handlers the iterator has ever produced 1.382 + // or will ever produce. Do not include the final rethrow handler. 1.383 + // That is, a trivial exception handler stream will have a count 1.384 + // of zero and produce just the rethrow handler. 1.385 + int count(); 1.386 + 1.387 + // Count the number of handlers this stream will produce from now on. 1.388 + // Include the current handler, and the final rethrow handler. 1.389 + // The remaining count will be zero iff is_done() is true, 1.390 + int count_remaining(); 1.391 + 1.392 + bool is_done() { 1.393 + return (_pos >= _end); 1.394 + } 1.395 + 1.396 + void next() { 1.397 + _pos++; 1.398 + if (_bci != -1) { 1.399 + // We are not iterating over all handlers... 1.400 + while (!is_done()) { 1.401 + ciExceptionHandler* handler = _method->_exception_handlers[_pos]; 1.402 + if (handler->is_in_range(_bci)) { 1.403 + if (handler->is_catch_all()) { 1.404 + // Found final active catch block. 1.405 + _end = _pos+1; 1.406 + return; 1.407 + } else if (_exception_klass == NULL || !handler->catch_klass()->is_loaded()) { 1.408 + // We cannot do any type analysis here. Must conservatively assume 1.409 + // catch block is reachable. 1.410 + return; 1.411 + } else if (_exception_klass->is_subtype_of(handler->catch_klass())) { 1.412 + // This catch clause will definitely catch the exception. 1.413 + // Final candidate. 1.414 + _end = _pos+1; 1.415 + return; 1.416 + } else if (!_is_exact && 1.417 + handler->catch_klass()->is_subtype_of(_exception_klass)) { 1.418 + // This catch block may be reachable. 1.419 + return; 1.420 + } 1.421 + } 1.422 + 1.423 + // The catch block was not pertinent. Go on. 1.424 + _pos++; 1.425 + } 1.426 + } else { 1.427 + // This is an iteration over all handlers. 1.428 + return; 1.429 + } 1.430 + } 1.431 + 1.432 + ciExceptionHandler* handler() { 1.433 + return _method->_exception_handlers[_pos]; 1.434 + } 1.435 +}; 1.436 + 1.437 + 1.438 + 1.439 +// Implementation for declarations in bytecode.hpp 1.440 +Bytecode::Bytecode(const ciBytecodeStream* stream, address bcp): _bcp(bcp != NULL ? bcp : stream->cur_bcp()), _code(Bytecodes::code_at(NULL, addr_at(0))) {} 1.441 +Bytecode_lookupswitch::Bytecode_lookupswitch(const ciBytecodeStream* stream): Bytecode(stream) { verify(); } 1.442 +Bytecode_tableswitch::Bytecode_tableswitch(const ciBytecodeStream* stream): Bytecode(stream) { verify(); } 1.443 + 1.444 +#endif // SHARE_VM_CI_CISTREAMS_HPP