src/share/vm/ci/ciStreams.hpp

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 435
a61af66fc99e
child 1161
be93aad57795
permissions
-rw-r--r--

Initial load

duke@435 1 /*
duke@435 2 * Copyright 1999-2005 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 // ciBytecodeStream
duke@435 26 //
duke@435 27 // The class is used to iterate over the bytecodes of a method.
duke@435 28 // It hides the details of constant pool structure/access by
duke@435 29 // providing accessors for constant pool items. It returns only pure
duke@435 30 // Java bytecodes; VM-internal _fast bytecodes are translated back to
duke@435 31 // their original form during iteration.
duke@435 32 class ciBytecodeStream : StackObj {
duke@435 33 private:
duke@435 34 // Handling for the weird bytecodes
duke@435 35 Bytecodes::Code wide(); // Handle wide bytecode
duke@435 36 Bytecodes::Code table(Bytecodes::Code); // Handle complicated inline table
duke@435 37
duke@435 38 static Bytecodes::Code check_java(Bytecodes::Code c) {
duke@435 39 assert(Bytecodes::is_java_code(c), "should not return _fast bytecodes");
duke@435 40 return c;
duke@435 41 }
duke@435 42
duke@435 43 ciMethod* _method; // the method
duke@435 44 ciInstanceKlass* _holder;
duke@435 45 address _bc_start; // Start of current bytecode for table
duke@435 46 address _was_wide; // Address past last wide bytecode
duke@435 47 jint* _table_base; // Aligned start of last table or switch
duke@435 48
duke@435 49 address _start; // Start of bytecodes
duke@435 50 address _end; // Past end of bytecodes
duke@435 51 address _pc; // Current PC
duke@435 52 Bytecodes::Code _bc; // Current bytecode
duke@435 53
duke@435 54 void reset( address base, unsigned int size ) {
duke@435 55 _bc_start =_was_wide = 0;
duke@435 56 _start = _pc = base; _end = base + size; }
duke@435 57
duke@435 58 public:
duke@435 59 // End-Of-Bytecodes
duke@435 60 static Bytecodes::Code EOBC() {
duke@435 61 return Bytecodes::_illegal;
duke@435 62 }
duke@435 63
duke@435 64 ciBytecodeStream(ciMethod* m) {
duke@435 65 reset_to_method(m);
duke@435 66 }
duke@435 67
duke@435 68 ciBytecodeStream() {
duke@435 69 reset_to_method(NULL);
duke@435 70 }
duke@435 71
duke@435 72 ciMethod* method() const { return _method; }
duke@435 73
duke@435 74 void reset_to_method(ciMethod* m) {
duke@435 75 _method = m;
duke@435 76 if (m == NULL) {
duke@435 77 _holder = NULL;
duke@435 78 reset(NULL, 0);
duke@435 79 } else {
duke@435 80 _holder = m->holder();
duke@435 81 reset(m->code(), m->code_size());
duke@435 82 }
duke@435 83 }
duke@435 84
duke@435 85 void reset_to_bci( int bci );
duke@435 86
duke@435 87 // Force the iterator to report a certain bci.
duke@435 88 void force_bci(int bci);
duke@435 89
duke@435 90 void set_max_bci( int max ) {
duke@435 91 _end = _start + max;
duke@435 92 }
duke@435 93
duke@435 94 address cur_bcp() { return _bc_start; } // Returns bcp to current instruction
duke@435 95 int next_bci() const { return _pc -_start; }
duke@435 96 int cur_bci() const { return _bc_start - _start; }
duke@435 97
duke@435 98 Bytecodes::Code cur_bc() const{ return check_java(_bc); }
duke@435 99 Bytecodes::Code next_bc() { return Bytecodes::java_code((Bytecodes::Code)* _pc); }
duke@435 100
duke@435 101 // Return current ByteCode and increment PC to next bytecode, skipping all
duke@435 102 // intermediate constants. Returns EOBC at end.
duke@435 103 // Expected usage:
duke@435 104 // while( (bc = iter.next()) != EOBC() ) { ... }
duke@435 105 Bytecodes::Code next() {
duke@435 106 _bc_start = _pc; // Capture start of bc
duke@435 107 if( _pc >= _end ) return EOBC(); // End-Of-Bytecodes
duke@435 108
duke@435 109 // Fetch Java bytecode
duke@435 110 // All rewritten bytecodes maintain the size of original bytecode.
duke@435 111 _bc = Bytecodes::java_code((Bytecodes::Code)*_pc);
duke@435 112 int csize = Bytecodes::length_for(_bc); // Expected size
duke@435 113
duke@435 114 if( _bc == Bytecodes::_wide ) {
duke@435 115 _bc=wide(); // Handle wide bytecode
duke@435 116 } else if( csize == 0 ) {
duke@435 117 _bc=table(_bc); // Handle inline tables
duke@435 118 } else {
duke@435 119 _pc += csize; // Bump PC past bytecode
duke@435 120 }
duke@435 121 return check_java(_bc);
duke@435 122 }
duke@435 123
duke@435 124 bool is_wide() { return ( _pc == _was_wide ); }
duke@435 125
duke@435 126 // Get a byte index following this bytecode.
duke@435 127 // If prefixed with a wide bytecode, get a wide index.
duke@435 128 int get_index() const {
duke@435 129 return (_pc == _was_wide) // was widened?
duke@435 130 ? Bytes::get_Java_u2(_bc_start+2) // yes, return wide index
duke@435 131 : _bc_start[1]; // no, return narrow index
duke@435 132 }
duke@435 133
duke@435 134 // Set a byte index following this bytecode.
duke@435 135 // If prefixed with a wide bytecode, get a wide index.
duke@435 136 void put_index(int idx) {
duke@435 137 if (_pc == _was_wide) // was widened?
duke@435 138 Bytes::put_Java_u2(_bc_start+2,idx); // yes, set wide index
duke@435 139 else
duke@435 140 _bc_start[1]=idx; // no, set narrow index
duke@435 141 }
duke@435 142
duke@435 143 // Get 2-byte index (getfield/putstatic/etc)
duke@435 144 int get_index_big() const { return Bytes::get_Java_u2(_bc_start+1); }
duke@435 145
duke@435 146 // Get dimensions byte (multinewarray)
duke@435 147 int get_dimensions() const { return *(unsigned char*)(_pc-1); }
duke@435 148
duke@435 149 // Get unsigned index fast
duke@435 150 int get_index_fast() const { return Bytes::get_native_u2(_pc-2); }
duke@435 151
duke@435 152 // Sign-extended index byte/short, no widening
duke@435 153 int get_byte() const { return (int8_t)(_pc[-1]); }
duke@435 154 int get_short() const { return (int16_t)Bytes::get_Java_u2(_pc-2); }
duke@435 155 int get_long() const { return (int32_t)Bytes::get_Java_u4(_pc-4); }
duke@435 156
duke@435 157 // Get a byte signed constant for "iinc". Invalid for other bytecodes.
duke@435 158 // If prefixed with a wide bytecode, get a wide constant
duke@435 159 int get_iinc_con() const {return (_pc==_was_wide) ? get_short() :get_byte();}
duke@435 160
duke@435 161 // 2-byte branch offset from current pc
duke@435 162 int get_dest( ) const {
duke@435 163 assert( Bytecodes::length_at(_bc_start) == sizeof(jshort)+1, "get_dest called with bad bytecode" );
duke@435 164 return _bc_start-_start + (short)Bytes::get_Java_u2(_pc-2);
duke@435 165 }
duke@435 166
duke@435 167 // 2-byte branch offset from next pc
duke@435 168 int next_get_dest( ) const {
duke@435 169 address next_bc_start = _pc;
duke@435 170 assert( _pc < _end, "" );
duke@435 171 Bytecodes::Code next_bc = (Bytecodes::Code)*_pc;
duke@435 172 assert( next_bc != Bytecodes::_wide, "");
duke@435 173 int next_csize = Bytecodes::length_for(next_bc);
duke@435 174 assert( next_csize != 0, "" );
duke@435 175 assert( next_bc <= Bytecodes::_jsr_w, "");
duke@435 176 address next_pc = _pc + next_csize;
duke@435 177 assert( Bytecodes::length_at(next_bc_start) == sizeof(jshort)+1, "next_get_dest called with bad bytecode" );
duke@435 178 return next_bc_start-_start + (short)Bytes::get_Java_u2(next_pc-2);
duke@435 179 }
duke@435 180
duke@435 181 // 4-byte branch offset from current pc
duke@435 182 int get_far_dest( ) const {
duke@435 183 assert( Bytecodes::length_at(_bc_start) == sizeof(jint)+1, "dest4 called with bad bytecode" );
duke@435 184 return _bc_start-_start + (int)Bytes::get_Java_u4(_pc-4);
duke@435 185 }
duke@435 186
duke@435 187 // For a lookup or switch table, return target destination
duke@435 188 int get_int_table( int index ) const {
duke@435 189 return Bytes::get_Java_u4((address)&_table_base[index]); }
duke@435 190
duke@435 191 // For tableswitch - get length of offset part
duke@435 192 int get_tableswitch_length() { return get_int_table(2)-get_int_table(1)+1; }
duke@435 193
duke@435 194 int get_dest_table( int index ) const {
duke@435 195 return cur_bci() + get_int_table(index); }
duke@435 196
duke@435 197 // --- Constant pool access ---
duke@435 198 int get_constant_index() const;
duke@435 199 int get_field_index();
duke@435 200 int get_method_index();
duke@435 201
duke@435 202 // If this bytecode is a new, newarray, multianewarray, instanceof,
duke@435 203 // or checkcast, get the referenced klass.
duke@435 204 ciKlass* get_klass(bool& will_link);
duke@435 205 int get_klass_index() const;
duke@435 206
duke@435 207 // If this bytecode is one of the ldc variants, get the referenced
duke@435 208 // constant
duke@435 209 ciConstant get_constant();
duke@435 210 // True if the ldc variant points to an unresolved string
duke@435 211 bool is_unresolved_string() const;
duke@435 212 // True if the ldc variant points to an unresolved klass
duke@435 213 bool is_unresolved_klass() const;
duke@435 214
duke@435 215 // If this bytecode is one of get_field, get_static, put_field,
duke@435 216 // or put_static, get the referenced field.
duke@435 217 ciField* get_field(bool& will_link);
duke@435 218
duke@435 219 ciInstanceKlass* get_declared_field_holder();
duke@435 220 int get_field_holder_index();
duke@435 221 int get_field_signature_index();
duke@435 222
duke@435 223 // If this is a method invocation bytecode, get the invoked method.
duke@435 224 ciMethod* get_method(bool& will_link);
duke@435 225 ciKlass* get_declared_method_holder();
duke@435 226 int get_method_holder_index();
duke@435 227 int get_method_signature_index();
duke@435 228 };
duke@435 229
duke@435 230
duke@435 231 // ciSignatureStream
duke@435 232 //
duke@435 233 // The class is used to iterate over the elements of a method signature.
duke@435 234 class ciSignatureStream : public StackObj {
duke@435 235 private:
duke@435 236 ciSignature* _sig;
duke@435 237 int _pos;
duke@435 238 public:
duke@435 239 ciSignatureStream(ciSignature* signature) {
duke@435 240 _sig = signature;
duke@435 241 _pos = 0;
duke@435 242 }
duke@435 243
duke@435 244 bool at_return_type() { return _pos == _sig->count(); }
duke@435 245
duke@435 246 bool is_done() { return _pos > _sig->count(); }
duke@435 247
duke@435 248 void next() {
duke@435 249 if (_pos <= _sig->count()) {
duke@435 250 _pos++;
duke@435 251 }
duke@435 252 }
duke@435 253
duke@435 254 ciType* type() {
duke@435 255 if (at_return_type()) {
duke@435 256 return _sig->return_type();
duke@435 257 } else {
duke@435 258 return _sig->type_at(_pos);
duke@435 259 }
duke@435 260 }
duke@435 261 };
duke@435 262
duke@435 263
duke@435 264 // ciExceptionHandlerStream
duke@435 265 //
duke@435 266 // The class is used to iterate over the exception handlers of
duke@435 267 // a method.
duke@435 268 class ciExceptionHandlerStream : public StackObj {
duke@435 269 private:
duke@435 270 // The method whose handlers we are traversing
duke@435 271 ciMethod* _method;
duke@435 272
duke@435 273 // Our current position in the list of handlers
duke@435 274 int _pos;
duke@435 275 int _end;
duke@435 276
duke@435 277 ciInstanceKlass* _exception_klass;
duke@435 278 int _bci;
duke@435 279 bool _is_exact;
duke@435 280
duke@435 281 public:
duke@435 282 ciExceptionHandlerStream(ciMethod* method) {
duke@435 283 _method = method;
duke@435 284
duke@435 285 // Force loading of method code and handlers.
duke@435 286 _method->code();
duke@435 287
duke@435 288 _pos = 0;
duke@435 289 _end = _method->_handler_count;
duke@435 290 _exception_klass = NULL;
duke@435 291 _bci = -1;
duke@435 292 _is_exact = false;
duke@435 293 }
duke@435 294
duke@435 295 ciExceptionHandlerStream(ciMethod* method, int bci,
duke@435 296 ciInstanceKlass* exception_klass = NULL,
duke@435 297 bool is_exact = false) {
duke@435 298 _method = method;
duke@435 299
duke@435 300 // Force loading of method code and handlers.
duke@435 301 _method->code();
duke@435 302
duke@435 303 _pos = -1;
duke@435 304 _end = _method->_handler_count + 1; // include the rethrow handler
duke@435 305 _exception_klass = (exception_klass != NULL && exception_klass->is_loaded()
duke@435 306 ? exception_klass
duke@435 307 : NULL);
duke@435 308 _bci = bci;
duke@435 309 assert(_bci >= 0, "bci out of range");
duke@435 310 _is_exact = is_exact;
duke@435 311 next();
duke@435 312 }
duke@435 313
duke@435 314 // These methods are currently implemented in an odd way.
duke@435 315 // Count the number of handlers the iterator has ever produced
duke@435 316 // or will ever produce. Do not include the final rethrow handler.
duke@435 317 // That is, a trivial exception handler stream will have a count
duke@435 318 // of zero and produce just the rethrow handler.
duke@435 319 int count();
duke@435 320
duke@435 321 // Count the number of handlers this stream will produce from now on.
duke@435 322 // Include the current handler, and the final rethrow handler.
duke@435 323 // The remaining count will be zero iff is_done() is true,
duke@435 324 int count_remaining();
duke@435 325
duke@435 326 bool is_done() {
duke@435 327 return (_pos >= _end);
duke@435 328 }
duke@435 329
duke@435 330 void next() {
duke@435 331 _pos++;
duke@435 332 if (_bci != -1) {
duke@435 333 // We are not iterating over all handlers...
duke@435 334 while (!is_done()) {
duke@435 335 ciExceptionHandler* handler = _method->_exception_handlers[_pos];
duke@435 336 if (handler->is_in_range(_bci)) {
duke@435 337 if (handler->is_catch_all()) {
duke@435 338 // Found final active catch block.
duke@435 339 _end = _pos+1;
duke@435 340 return;
duke@435 341 } else if (_exception_klass == NULL || !handler->catch_klass()->is_loaded()) {
duke@435 342 // We cannot do any type analysis here. Must conservatively assume
duke@435 343 // catch block is reachable.
duke@435 344 return;
duke@435 345 } else if (_exception_klass->is_subtype_of(handler->catch_klass())) {
duke@435 346 // This catch clause will definitely catch the exception.
duke@435 347 // Final candidate.
duke@435 348 _end = _pos+1;
duke@435 349 return;
duke@435 350 } else if (!_is_exact &&
duke@435 351 handler->catch_klass()->is_subtype_of(_exception_klass)) {
duke@435 352 // This catch block may be reachable.
duke@435 353 return;
duke@435 354 }
duke@435 355 }
duke@435 356
duke@435 357 // The catch block was not pertinent. Go on.
duke@435 358 _pos++;
duke@435 359 }
duke@435 360 } else {
duke@435 361 // This is an iteration over all handlers.
duke@435 362 return;
duke@435 363 }
duke@435 364 }
duke@435 365
duke@435 366 ciExceptionHandler* handler() {
duke@435 367 return _method->_exception_handlers[_pos];
duke@435 368 }
duke@435 369 };

mercurial