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