1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/ci/ciStreams.hpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,369 @@ 1.4 +/* 1.5 + * Copyright 1999-2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +// ciBytecodeStream 1.29 +// 1.30 +// The class is used to iterate over the bytecodes of a method. 1.31 +// It hides the details of constant pool structure/access by 1.32 +// providing accessors for constant pool items. It returns only pure 1.33 +// Java bytecodes; VM-internal _fast bytecodes are translated back to 1.34 +// their original form during iteration. 1.35 +class ciBytecodeStream : StackObj { 1.36 +private: 1.37 + // Handling for the weird bytecodes 1.38 + Bytecodes::Code wide(); // Handle wide bytecode 1.39 + Bytecodes::Code table(Bytecodes::Code); // Handle complicated inline table 1.40 + 1.41 + static Bytecodes::Code check_java(Bytecodes::Code c) { 1.42 + assert(Bytecodes::is_java_code(c), "should not return _fast bytecodes"); 1.43 + return c; 1.44 + } 1.45 + 1.46 + ciMethod* _method; // the method 1.47 + ciInstanceKlass* _holder; 1.48 + address _bc_start; // Start of current bytecode for table 1.49 + address _was_wide; // Address past last wide bytecode 1.50 + jint* _table_base; // Aligned start of last table or switch 1.51 + 1.52 + address _start; // Start of bytecodes 1.53 + address _end; // Past end of bytecodes 1.54 + address _pc; // Current PC 1.55 + Bytecodes::Code _bc; // Current bytecode 1.56 + 1.57 + void reset( address base, unsigned int size ) { 1.58 + _bc_start =_was_wide = 0; 1.59 + _start = _pc = base; _end = base + size; } 1.60 + 1.61 +public: 1.62 + // End-Of-Bytecodes 1.63 + static Bytecodes::Code EOBC() { 1.64 + return Bytecodes::_illegal; 1.65 + } 1.66 + 1.67 + ciBytecodeStream(ciMethod* m) { 1.68 + reset_to_method(m); 1.69 + } 1.70 + 1.71 + ciBytecodeStream() { 1.72 + reset_to_method(NULL); 1.73 + } 1.74 + 1.75 + ciMethod* method() const { return _method; } 1.76 + 1.77 + void reset_to_method(ciMethod* m) { 1.78 + _method = m; 1.79 + if (m == NULL) { 1.80 + _holder = NULL; 1.81 + reset(NULL, 0); 1.82 + } else { 1.83 + _holder = m->holder(); 1.84 + reset(m->code(), m->code_size()); 1.85 + } 1.86 + } 1.87 + 1.88 + void reset_to_bci( int bci ); 1.89 + 1.90 + // Force the iterator to report a certain bci. 1.91 + void force_bci(int bci); 1.92 + 1.93 + void set_max_bci( int max ) { 1.94 + _end = _start + max; 1.95 + } 1.96 + 1.97 + address cur_bcp() { return _bc_start; } // Returns bcp to current instruction 1.98 + int next_bci() const { return _pc -_start; } 1.99 + int cur_bci() const { return _bc_start - _start; } 1.100 + 1.101 + Bytecodes::Code cur_bc() const{ return check_java(_bc); } 1.102 + Bytecodes::Code next_bc() { return Bytecodes::java_code((Bytecodes::Code)* _pc); } 1.103 + 1.104 + // Return current ByteCode and increment PC to next bytecode, skipping all 1.105 + // intermediate constants. Returns EOBC at end. 1.106 + // Expected usage: 1.107 + // while( (bc = iter.next()) != EOBC() ) { ... } 1.108 + Bytecodes::Code next() { 1.109 + _bc_start = _pc; // Capture start of bc 1.110 + if( _pc >= _end ) return EOBC(); // End-Of-Bytecodes 1.111 + 1.112 + // Fetch Java bytecode 1.113 + // All rewritten bytecodes maintain the size of original bytecode. 1.114 + _bc = Bytecodes::java_code((Bytecodes::Code)*_pc); 1.115 + int csize = Bytecodes::length_for(_bc); // Expected size 1.116 + 1.117 + if( _bc == Bytecodes::_wide ) { 1.118 + _bc=wide(); // Handle wide bytecode 1.119 + } else if( csize == 0 ) { 1.120 + _bc=table(_bc); // Handle inline tables 1.121 + } else { 1.122 + _pc += csize; // Bump PC past bytecode 1.123 + } 1.124 + return check_java(_bc); 1.125 + } 1.126 + 1.127 + bool is_wide() { return ( _pc == _was_wide ); } 1.128 + 1.129 + // Get a byte index following this bytecode. 1.130 + // If prefixed with a wide bytecode, get a wide index. 1.131 + int get_index() const { 1.132 + return (_pc == _was_wide) // was widened? 1.133 + ? Bytes::get_Java_u2(_bc_start+2) // yes, return wide index 1.134 + : _bc_start[1]; // no, return narrow index 1.135 + } 1.136 + 1.137 + // Set a byte index following this bytecode. 1.138 + // If prefixed with a wide bytecode, get a wide index. 1.139 + void put_index(int idx) { 1.140 + if (_pc == _was_wide) // was widened? 1.141 + Bytes::put_Java_u2(_bc_start+2,idx); // yes, set wide index 1.142 + else 1.143 + _bc_start[1]=idx; // no, set narrow index 1.144 + } 1.145 + 1.146 + // Get 2-byte index (getfield/putstatic/etc) 1.147 + int get_index_big() const { return Bytes::get_Java_u2(_bc_start+1); } 1.148 + 1.149 + // Get dimensions byte (multinewarray) 1.150 + int get_dimensions() const { return *(unsigned char*)(_pc-1); } 1.151 + 1.152 + // Get unsigned index fast 1.153 + int get_index_fast() const { return Bytes::get_native_u2(_pc-2); } 1.154 + 1.155 + // Sign-extended index byte/short, no widening 1.156 + int get_byte() const { return (int8_t)(_pc[-1]); } 1.157 + int get_short() const { return (int16_t)Bytes::get_Java_u2(_pc-2); } 1.158 + int get_long() const { return (int32_t)Bytes::get_Java_u4(_pc-4); } 1.159 + 1.160 + // Get a byte signed constant for "iinc". Invalid for other bytecodes. 1.161 + // If prefixed with a wide bytecode, get a wide constant 1.162 + int get_iinc_con() const {return (_pc==_was_wide) ? get_short() :get_byte();} 1.163 + 1.164 + // 2-byte branch offset from current pc 1.165 + int get_dest( ) const { 1.166 + assert( Bytecodes::length_at(_bc_start) == sizeof(jshort)+1, "get_dest called with bad bytecode" ); 1.167 + return _bc_start-_start + (short)Bytes::get_Java_u2(_pc-2); 1.168 + } 1.169 + 1.170 + // 2-byte branch offset from next pc 1.171 + int next_get_dest( ) const { 1.172 + address next_bc_start = _pc; 1.173 + assert( _pc < _end, "" ); 1.174 + Bytecodes::Code next_bc = (Bytecodes::Code)*_pc; 1.175 + assert( next_bc != Bytecodes::_wide, ""); 1.176 + int next_csize = Bytecodes::length_for(next_bc); 1.177 + assert( next_csize != 0, "" ); 1.178 + assert( next_bc <= Bytecodes::_jsr_w, ""); 1.179 + address next_pc = _pc + next_csize; 1.180 + assert( Bytecodes::length_at(next_bc_start) == sizeof(jshort)+1, "next_get_dest called with bad bytecode" ); 1.181 + return next_bc_start-_start + (short)Bytes::get_Java_u2(next_pc-2); 1.182 + } 1.183 + 1.184 + // 4-byte branch offset from current pc 1.185 + int get_far_dest( ) const { 1.186 + assert( Bytecodes::length_at(_bc_start) == sizeof(jint)+1, "dest4 called with bad bytecode" ); 1.187 + return _bc_start-_start + (int)Bytes::get_Java_u4(_pc-4); 1.188 + } 1.189 + 1.190 + // For a lookup or switch table, return target destination 1.191 + int get_int_table( int index ) const { 1.192 + return Bytes::get_Java_u4((address)&_table_base[index]); } 1.193 + 1.194 + // For tableswitch - get length of offset part 1.195 + int get_tableswitch_length() { return get_int_table(2)-get_int_table(1)+1; } 1.196 + 1.197 + int get_dest_table( int index ) const { 1.198 + return cur_bci() + get_int_table(index); } 1.199 + 1.200 + // --- Constant pool access --- 1.201 + int get_constant_index() const; 1.202 + int get_field_index(); 1.203 + int get_method_index(); 1.204 + 1.205 + // If this bytecode is a new, newarray, multianewarray, instanceof, 1.206 + // or checkcast, get the referenced klass. 1.207 + ciKlass* get_klass(bool& will_link); 1.208 + int get_klass_index() const; 1.209 + 1.210 + // If this bytecode is one of the ldc variants, get the referenced 1.211 + // constant 1.212 + ciConstant get_constant(); 1.213 + // True if the ldc variant points to an unresolved string 1.214 + bool is_unresolved_string() const; 1.215 + // True if the ldc variant points to an unresolved klass 1.216 + bool is_unresolved_klass() const; 1.217 + 1.218 + // If this bytecode is one of get_field, get_static, put_field, 1.219 + // or put_static, get the referenced field. 1.220 + ciField* get_field(bool& will_link); 1.221 + 1.222 + ciInstanceKlass* get_declared_field_holder(); 1.223 + int get_field_holder_index(); 1.224 + int get_field_signature_index(); 1.225 + 1.226 + // If this is a method invocation bytecode, get the invoked method. 1.227 + ciMethod* get_method(bool& will_link); 1.228 + ciKlass* get_declared_method_holder(); 1.229 + int get_method_holder_index(); 1.230 + int get_method_signature_index(); 1.231 +}; 1.232 + 1.233 + 1.234 +// ciSignatureStream 1.235 +// 1.236 +// The class is used to iterate over the elements of a method signature. 1.237 +class ciSignatureStream : public StackObj { 1.238 +private: 1.239 + ciSignature* _sig; 1.240 + int _pos; 1.241 +public: 1.242 + ciSignatureStream(ciSignature* signature) { 1.243 + _sig = signature; 1.244 + _pos = 0; 1.245 + } 1.246 + 1.247 + bool at_return_type() { return _pos == _sig->count(); } 1.248 + 1.249 + bool is_done() { return _pos > _sig->count(); } 1.250 + 1.251 + void next() { 1.252 + if (_pos <= _sig->count()) { 1.253 + _pos++; 1.254 + } 1.255 + } 1.256 + 1.257 + ciType* type() { 1.258 + if (at_return_type()) { 1.259 + return _sig->return_type(); 1.260 + } else { 1.261 + return _sig->type_at(_pos); 1.262 + } 1.263 + } 1.264 +}; 1.265 + 1.266 + 1.267 +// ciExceptionHandlerStream 1.268 +// 1.269 +// The class is used to iterate over the exception handlers of 1.270 +// a method. 1.271 +class ciExceptionHandlerStream : public StackObj { 1.272 +private: 1.273 + // The method whose handlers we are traversing 1.274 + ciMethod* _method; 1.275 + 1.276 + // Our current position in the list of handlers 1.277 + int _pos; 1.278 + int _end; 1.279 + 1.280 + ciInstanceKlass* _exception_klass; 1.281 + int _bci; 1.282 + bool _is_exact; 1.283 + 1.284 +public: 1.285 + ciExceptionHandlerStream(ciMethod* method) { 1.286 + _method = method; 1.287 + 1.288 + // Force loading of method code and handlers. 1.289 + _method->code(); 1.290 + 1.291 + _pos = 0; 1.292 + _end = _method->_handler_count; 1.293 + _exception_klass = NULL; 1.294 + _bci = -1; 1.295 + _is_exact = false; 1.296 + } 1.297 + 1.298 + ciExceptionHandlerStream(ciMethod* method, int bci, 1.299 + ciInstanceKlass* exception_klass = NULL, 1.300 + bool is_exact = false) { 1.301 + _method = method; 1.302 + 1.303 + // Force loading of method code and handlers. 1.304 + _method->code(); 1.305 + 1.306 + _pos = -1; 1.307 + _end = _method->_handler_count + 1; // include the rethrow handler 1.308 + _exception_klass = (exception_klass != NULL && exception_klass->is_loaded() 1.309 + ? exception_klass 1.310 + : NULL); 1.311 + _bci = bci; 1.312 + assert(_bci >= 0, "bci out of range"); 1.313 + _is_exact = is_exact; 1.314 + next(); 1.315 + } 1.316 + 1.317 + // These methods are currently implemented in an odd way. 1.318 + // Count the number of handlers the iterator has ever produced 1.319 + // or will ever produce. Do not include the final rethrow handler. 1.320 + // That is, a trivial exception handler stream will have a count 1.321 + // of zero and produce just the rethrow handler. 1.322 + int count(); 1.323 + 1.324 + // Count the number of handlers this stream will produce from now on. 1.325 + // Include the current handler, and the final rethrow handler. 1.326 + // The remaining count will be zero iff is_done() is true, 1.327 + int count_remaining(); 1.328 + 1.329 + bool is_done() { 1.330 + return (_pos >= _end); 1.331 + } 1.332 + 1.333 + void next() { 1.334 + _pos++; 1.335 + if (_bci != -1) { 1.336 + // We are not iterating over all handlers... 1.337 + while (!is_done()) { 1.338 + ciExceptionHandler* handler = _method->_exception_handlers[_pos]; 1.339 + if (handler->is_in_range(_bci)) { 1.340 + if (handler->is_catch_all()) { 1.341 + // Found final active catch block. 1.342 + _end = _pos+1; 1.343 + return; 1.344 + } else if (_exception_klass == NULL || !handler->catch_klass()->is_loaded()) { 1.345 + // We cannot do any type analysis here. Must conservatively assume 1.346 + // catch block is reachable. 1.347 + return; 1.348 + } else if (_exception_klass->is_subtype_of(handler->catch_klass())) { 1.349 + // This catch clause will definitely catch the exception. 1.350 + // Final candidate. 1.351 + _end = _pos+1; 1.352 + return; 1.353 + } else if (!_is_exact && 1.354 + handler->catch_klass()->is_subtype_of(_exception_klass)) { 1.355 + // This catch block may be reachable. 1.356 + return; 1.357 + } 1.358 + } 1.359 + 1.360 + // The catch block was not pertinent. Go on. 1.361 + _pos++; 1.362 + } 1.363 + } else { 1.364 + // This is an iteration over all handlers. 1.365 + return; 1.366 + } 1.367 + } 1.368 + 1.369 + ciExceptionHandler* handler() { 1.370 + return _method->_exception_handlers[_pos]; 1.371 + } 1.372 +};