src/share/vm/ci/ciStreams.hpp

Wed, 02 Jun 2010 22:45:42 -0700

author
jrose
date
Wed, 02 Jun 2010 22:45:42 -0700
changeset 1934
e9ff18c4ace7
parent 1907
c18cbe5936b8
parent 1920
ab102d5d923e
child 1957
136b78722a08
permissions
-rw-r--r--

Merge

duke@435 1 /*
jrose@1934 2 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. 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 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * 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:
jrose@1920 34 // Handling for the weird bytecodes
jrose@1920 35 Bytecodes::Code next_wide_or_table(Bytecodes::Code); // Handle _wide & complicated inline table
duke@435 36
duke@435 37 static Bytecodes::Code check_java(Bytecodes::Code c) {
duke@435 38 assert(Bytecodes::is_java_code(c), "should not return _fast bytecodes");
duke@435 39 return c;
duke@435 40 }
duke@435 41
jrose@1920 42 static Bytecodes::Code check_defined(Bytecodes::Code c) {
jrose@1920 43 assert(Bytecodes::is_defined(c), "");
jrose@1920 44 return c;
jrose@1920 45 }
jrose@1920 46
duke@435 47 ciMethod* _method; // the method
duke@435 48 ciInstanceKlass* _holder;
duke@435 49 address _bc_start; // Start of current bytecode for table
duke@435 50 address _was_wide; // Address past last wide bytecode
duke@435 51 jint* _table_base; // Aligned start of last table or switch
duke@435 52
duke@435 53 address _start; // Start of bytecodes
duke@435 54 address _end; // Past end of bytecodes
duke@435 55 address _pc; // Current PC
duke@435 56 Bytecodes::Code _bc; // Current bytecode
jrose@1920 57 Bytecodes::Code _raw_bc; // Current bytecode, raw form
duke@435 58
duke@435 59 void reset( address base, unsigned int size ) {
duke@435 60 _bc_start =_was_wide = 0;
duke@435 61 _start = _pc = base; _end = base + size; }
duke@435 62
jrose@1920 63 void assert_wide(bool require_wide) const {
jrose@1920 64 if (require_wide)
jrose@1920 65 { assert(is_wide(), "must be a wide instruction"); }
jrose@1920 66 else { assert(!is_wide(), "must not be a wide instruction"); }
jrose@1920 67 }
jrose@1920 68
jrose@1920 69 Bytecode* bytecode() const { return Bytecode_at(_bc_start); }
jrose@1920 70 Bytecode* next_bytecode() const { return Bytecode_at(_pc); }
jrose@1920 71
duke@435 72 public:
duke@435 73 // End-Of-Bytecodes
duke@435 74 static Bytecodes::Code EOBC() {
duke@435 75 return Bytecodes::_illegal;
duke@435 76 }
duke@435 77
duke@435 78 ciBytecodeStream(ciMethod* m) {
duke@435 79 reset_to_method(m);
duke@435 80 }
duke@435 81
duke@435 82 ciBytecodeStream() {
duke@435 83 reset_to_method(NULL);
duke@435 84 }
duke@435 85
duke@435 86 ciMethod* method() const { return _method; }
duke@435 87
duke@435 88 void reset_to_method(ciMethod* m) {
duke@435 89 _method = m;
duke@435 90 if (m == NULL) {
duke@435 91 _holder = NULL;
duke@435 92 reset(NULL, 0);
duke@435 93 } else {
duke@435 94 _holder = m->holder();
duke@435 95 reset(m->code(), m->code_size());
duke@435 96 }
duke@435 97 }
duke@435 98
duke@435 99 void reset_to_bci( int bci );
duke@435 100
duke@435 101 // Force the iterator to report a certain bci.
duke@435 102 void force_bci(int bci);
duke@435 103
duke@435 104 void set_max_bci( int max ) {
duke@435 105 _end = _start + max;
duke@435 106 }
duke@435 107
jrose@1161 108 address cur_bcp() const { return _bc_start; } // Returns bcp to current instruction
jrose@1920 109 int next_bci() const { return _pc - _start; }
duke@435 110 int cur_bci() const { return _bc_start - _start; }
jrose@1161 111 int instruction_size() const { return _pc - _bc_start; }
duke@435 112
duke@435 113 Bytecodes::Code cur_bc() const{ return check_java(_bc); }
jrose@1920 114 Bytecodes::Code cur_bc_raw() const { return check_defined(_raw_bc); }
duke@435 115 Bytecodes::Code next_bc() { return Bytecodes::java_code((Bytecodes::Code)* _pc); }
duke@435 116
duke@435 117 // Return current ByteCode and increment PC to next bytecode, skipping all
duke@435 118 // intermediate constants. Returns EOBC at end.
duke@435 119 // Expected usage:
duke@435 120 // while( (bc = iter.next()) != EOBC() ) { ... }
duke@435 121 Bytecodes::Code next() {
duke@435 122 _bc_start = _pc; // Capture start of bc
duke@435 123 if( _pc >= _end ) return EOBC(); // End-Of-Bytecodes
duke@435 124
duke@435 125 // Fetch Java bytecode
duke@435 126 // All rewritten bytecodes maintain the size of original bytecode.
jrose@1920 127 _bc = Bytecodes::java_code(_raw_bc = (Bytecodes::Code)*_pc);
duke@435 128 int csize = Bytecodes::length_for(_bc); // Expected size
jrose@1920 129 _pc += csize; // Bump PC past bytecode
jrose@1920 130 if (csize == 0) {
jrose@1920 131 _bc = next_wide_or_table(_bc);
duke@435 132 }
duke@435 133 return check_java(_bc);
duke@435 134 }
duke@435 135
jrose@1161 136 bool is_wide() const { return ( _pc == _was_wide ); }
duke@435 137
jrose@1920 138 // Does this instruction contain an index which refes into the CP cache?
jrose@1920 139 bool uses_cp_cache() const { return Bytecodes::uses_cp_cache(cur_bc_raw()); }
jrose@1920 140
jrose@1920 141 int get_index_u1() const {
jrose@1920 142 return bytecode()->get_index_u1(cur_bc_raw());
jrose@1920 143 }
jrose@1920 144
duke@435 145 // Get a byte index following this bytecode.
duke@435 146 // If prefixed with a wide bytecode, get a wide index.
duke@435 147 int get_index() const {
duke@435 148 return (_pc == _was_wide) // was widened?
jrose@1920 149 ? get_index_u2(true) // yes, return wide index
jrose@1920 150 : get_index_u1(); // no, return narrow index
duke@435 151 }
duke@435 152
jrose@1920 153 // Get 2-byte index (byte swapping depending on which bytecode)
jrose@1920 154 int get_index_u2(bool is_wide = false) const {
jrose@1920 155 return bytecode()->get_index_u2(cur_bc_raw(), is_wide);
duke@435 156 }
duke@435 157
jrose@1920 158 // Get 2-byte index in native byte order. (Rewriter::rewrite makes these.)
jrose@1920 159 int get_index_u2_cpcache() const {
jrose@1920 160 return bytecode()->get_index_u2_cpcache(cur_bc_raw());
jrose@1161 161 }
jrose@1161 162
jrose@1161 163 // Get 4-byte index, for invokedynamic.
jrose@1920 164 int get_index_u4() const {
jrose@1920 165 return bytecode()->get_index_u4(cur_bc_raw());
jrose@1161 166 }
jrose@1161 167
jrose@1920 168 bool has_index_u4() const {
jrose@1920 169 return bytecode()->has_index_u4(cur_bc_raw());
jrose@1920 170 }
duke@435 171
duke@435 172 // Get dimensions byte (multinewarray)
duke@435 173 int get_dimensions() const { return *(unsigned char*)(_pc-1); }
duke@435 174
duke@435 175 // Sign-extended index byte/short, no widening
jrose@1920 176 int get_constant_u1() const { return bytecode()->get_constant_u1(instruction_size()-1, cur_bc_raw()); }
jrose@1920 177 int get_constant_u2(bool is_wide = false) const { return bytecode()->get_constant_u2(instruction_size()-2, cur_bc_raw(), is_wide); }
duke@435 178
duke@435 179 // Get a byte signed constant for "iinc". Invalid for other bytecodes.
duke@435 180 // If prefixed with a wide bytecode, get a wide constant
jrose@1920 181 int get_iinc_con() const {return (_pc==_was_wide) ? (jshort) get_constant_u2(true) : (jbyte) get_constant_u1();}
duke@435 182
duke@435 183 // 2-byte branch offset from current pc
jrose@1920 184 int get_dest() const {
jrose@1920 185 return cur_bci() + bytecode()->get_offset_s2(cur_bc_raw());
duke@435 186 }
duke@435 187
duke@435 188 // 2-byte branch offset from next pc
jrose@1920 189 int next_get_dest() const {
jrose@1920 190 assert(_pc < _end, "");
jrose@1920 191 return next_bci() + next_bytecode()->get_offset_s2(Bytecodes::_ifeq);
duke@435 192 }
duke@435 193
duke@435 194 // 4-byte branch offset from current pc
jrose@1920 195 int get_far_dest() const {
jrose@1920 196 return cur_bci() + bytecode()->get_offset_s4(cur_bc_raw());
duke@435 197 }
duke@435 198
duke@435 199 // For a lookup or switch table, return target destination
duke@435 200 int get_int_table( int index ) const {
duke@435 201 return Bytes::get_Java_u4((address)&_table_base[index]); }
duke@435 202
duke@435 203 // For tableswitch - get length of offset part
duke@435 204 int get_tableswitch_length() { return get_int_table(2)-get_int_table(1)+1; }
duke@435 205
duke@435 206 int get_dest_table( int index ) const {
duke@435 207 return cur_bci() + get_int_table(index); }
duke@435 208
duke@435 209 // --- Constant pool access ---
duke@435 210 int get_constant_index() const;
duke@435 211 int get_field_index();
duke@435 212 int get_method_index();
duke@435 213
duke@435 214 // If this bytecode is a new, newarray, multianewarray, instanceof,
duke@435 215 // or checkcast, get the referenced klass.
duke@435 216 ciKlass* get_klass(bool& will_link);
duke@435 217 int get_klass_index() const;
duke@435 218
duke@435 219 // If this bytecode is one of the ldc variants, get the referenced
duke@435 220 // constant
duke@435 221 ciConstant get_constant();
duke@435 222 // True if the ldc variant points to an unresolved string
duke@435 223 bool is_unresolved_string() const;
duke@435 224 // True if the ldc variant points to an unresolved klass
duke@435 225 bool is_unresolved_klass() const;
duke@435 226
duke@435 227 // If this bytecode is one of get_field, get_static, put_field,
duke@435 228 // or put_static, get the referenced field.
duke@435 229 ciField* get_field(bool& will_link);
duke@435 230
duke@435 231 ciInstanceKlass* get_declared_field_holder();
duke@435 232 int get_field_holder_index();
duke@435 233 int get_field_signature_index();
duke@435 234
duke@435 235 // If this is a method invocation bytecode, get the invoked method.
duke@435 236 ciMethod* get_method(bool& will_link);
duke@435 237 ciKlass* get_declared_method_holder();
duke@435 238 int get_method_holder_index();
duke@435 239 int get_method_signature_index();
jrose@1161 240
twisti@1573 241 ciCPCache* get_cpcache();
twisti@1573 242 ciCallSite* get_call_site();
duke@435 243 };
duke@435 244
duke@435 245
duke@435 246 // ciSignatureStream
duke@435 247 //
duke@435 248 // The class is used to iterate over the elements of a method signature.
duke@435 249 class ciSignatureStream : public StackObj {
duke@435 250 private:
duke@435 251 ciSignature* _sig;
duke@435 252 int _pos;
duke@435 253 public:
duke@435 254 ciSignatureStream(ciSignature* signature) {
duke@435 255 _sig = signature;
duke@435 256 _pos = 0;
duke@435 257 }
duke@435 258
duke@435 259 bool at_return_type() { return _pos == _sig->count(); }
duke@435 260
duke@435 261 bool is_done() { return _pos > _sig->count(); }
duke@435 262
duke@435 263 void next() {
duke@435 264 if (_pos <= _sig->count()) {
duke@435 265 _pos++;
duke@435 266 }
duke@435 267 }
duke@435 268
duke@435 269 ciType* type() {
duke@435 270 if (at_return_type()) {
duke@435 271 return _sig->return_type();
duke@435 272 } else {
duke@435 273 return _sig->type_at(_pos);
duke@435 274 }
duke@435 275 }
duke@435 276 };
duke@435 277
duke@435 278
duke@435 279 // ciExceptionHandlerStream
duke@435 280 //
duke@435 281 // The class is used to iterate over the exception handlers of
duke@435 282 // a method.
duke@435 283 class ciExceptionHandlerStream : public StackObj {
duke@435 284 private:
duke@435 285 // The method whose handlers we are traversing
duke@435 286 ciMethod* _method;
duke@435 287
duke@435 288 // Our current position in the list of handlers
duke@435 289 int _pos;
duke@435 290 int _end;
duke@435 291
duke@435 292 ciInstanceKlass* _exception_klass;
duke@435 293 int _bci;
duke@435 294 bool _is_exact;
duke@435 295
duke@435 296 public:
duke@435 297 ciExceptionHandlerStream(ciMethod* method) {
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 = 0;
duke@435 304 _end = _method->_handler_count;
duke@435 305 _exception_klass = NULL;
duke@435 306 _bci = -1;
duke@435 307 _is_exact = false;
duke@435 308 }
duke@435 309
duke@435 310 ciExceptionHandlerStream(ciMethod* method, int bci,
duke@435 311 ciInstanceKlass* exception_klass = NULL,
duke@435 312 bool is_exact = false) {
duke@435 313 _method = method;
duke@435 314
duke@435 315 // Force loading of method code and handlers.
duke@435 316 _method->code();
duke@435 317
duke@435 318 _pos = -1;
duke@435 319 _end = _method->_handler_count + 1; // include the rethrow handler
duke@435 320 _exception_klass = (exception_klass != NULL && exception_klass->is_loaded()
duke@435 321 ? exception_klass
duke@435 322 : NULL);
duke@435 323 _bci = bci;
duke@435 324 assert(_bci >= 0, "bci out of range");
duke@435 325 _is_exact = is_exact;
duke@435 326 next();
duke@435 327 }
duke@435 328
duke@435 329 // These methods are currently implemented in an odd way.
duke@435 330 // Count the number of handlers the iterator has ever produced
duke@435 331 // or will ever produce. Do not include the final rethrow handler.
duke@435 332 // That is, a trivial exception handler stream will have a count
duke@435 333 // of zero and produce just the rethrow handler.
duke@435 334 int count();
duke@435 335
duke@435 336 // Count the number of handlers this stream will produce from now on.
duke@435 337 // Include the current handler, and the final rethrow handler.
duke@435 338 // The remaining count will be zero iff is_done() is true,
duke@435 339 int count_remaining();
duke@435 340
duke@435 341 bool is_done() {
duke@435 342 return (_pos >= _end);
duke@435 343 }
duke@435 344
duke@435 345 void next() {
duke@435 346 _pos++;
duke@435 347 if (_bci != -1) {
duke@435 348 // We are not iterating over all handlers...
duke@435 349 while (!is_done()) {
duke@435 350 ciExceptionHandler* handler = _method->_exception_handlers[_pos];
duke@435 351 if (handler->is_in_range(_bci)) {
duke@435 352 if (handler->is_catch_all()) {
duke@435 353 // Found final active catch block.
duke@435 354 _end = _pos+1;
duke@435 355 return;
duke@435 356 } else if (_exception_klass == NULL || !handler->catch_klass()->is_loaded()) {
duke@435 357 // We cannot do any type analysis here. Must conservatively assume
duke@435 358 // catch block is reachable.
duke@435 359 return;
duke@435 360 } else if (_exception_klass->is_subtype_of(handler->catch_klass())) {
duke@435 361 // This catch clause will definitely catch the exception.
duke@435 362 // Final candidate.
duke@435 363 _end = _pos+1;
duke@435 364 return;
duke@435 365 } else if (!_is_exact &&
duke@435 366 handler->catch_klass()->is_subtype_of(_exception_klass)) {
duke@435 367 // This catch block may be reachable.
duke@435 368 return;
duke@435 369 }
duke@435 370 }
duke@435 371
duke@435 372 // The catch block was not pertinent. Go on.
duke@435 373 _pos++;
duke@435 374 }
duke@435 375 } else {
duke@435 376 // This is an iteration over all handlers.
duke@435 377 return;
duke@435 378 }
duke@435 379 }
duke@435 380
duke@435 381 ciExceptionHandler* handler() {
duke@435 382 return _method->_exception_handlers[_pos];
duke@435 383 }
duke@435 384 };

mercurial