Tue, 05 Jan 2010 13:05:58 +0100
6829187: compiler optimizations required for JSR 292
Summary: C2 implementation for invokedynamic support.
Reviewed-by: kvn, never
duke@435 | 1 | /* |
xdono@1279 | 2 | * Copyright 1999-2009 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 | |
jrose@1161 | 94 | address cur_bcp() const { 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; } |
jrose@1161 | 97 | int instruction_size() const { return _pc - _bc_start; } |
duke@435 | 98 | |
duke@435 | 99 | Bytecodes::Code cur_bc() const{ return check_java(_bc); } |
duke@435 | 100 | Bytecodes::Code next_bc() { return Bytecodes::java_code((Bytecodes::Code)* _pc); } |
duke@435 | 101 | |
duke@435 | 102 | // Return current ByteCode and increment PC to next bytecode, skipping all |
duke@435 | 103 | // intermediate constants. Returns EOBC at end. |
duke@435 | 104 | // Expected usage: |
duke@435 | 105 | // while( (bc = iter.next()) != EOBC() ) { ... } |
duke@435 | 106 | Bytecodes::Code next() { |
duke@435 | 107 | _bc_start = _pc; // Capture start of bc |
duke@435 | 108 | if( _pc >= _end ) return EOBC(); // End-Of-Bytecodes |
duke@435 | 109 | |
duke@435 | 110 | // Fetch Java bytecode |
duke@435 | 111 | // All rewritten bytecodes maintain the size of original bytecode. |
duke@435 | 112 | _bc = Bytecodes::java_code((Bytecodes::Code)*_pc); |
duke@435 | 113 | int csize = Bytecodes::length_for(_bc); // Expected size |
duke@435 | 114 | |
duke@435 | 115 | if( _bc == Bytecodes::_wide ) { |
duke@435 | 116 | _bc=wide(); // Handle wide bytecode |
duke@435 | 117 | } else if( csize == 0 ) { |
duke@435 | 118 | _bc=table(_bc); // Handle inline tables |
duke@435 | 119 | } else { |
duke@435 | 120 | _pc += csize; // Bump PC past bytecode |
duke@435 | 121 | } |
duke@435 | 122 | return check_java(_bc); |
duke@435 | 123 | } |
duke@435 | 124 | |
jrose@1161 | 125 | bool is_wide() const { return ( _pc == _was_wide ); } |
duke@435 | 126 | |
duke@435 | 127 | // Get a byte index following this bytecode. |
duke@435 | 128 | // If prefixed with a wide bytecode, get a wide index. |
duke@435 | 129 | int get_index() const { |
jrose@1161 | 130 | assert_index_size(is_wide() ? 2 : 1); |
duke@435 | 131 | return (_pc == _was_wide) // was widened? |
duke@435 | 132 | ? Bytes::get_Java_u2(_bc_start+2) // yes, return wide index |
duke@435 | 133 | : _bc_start[1]; // no, return narrow index |
duke@435 | 134 | } |
duke@435 | 135 | |
jrose@1161 | 136 | // Get 2-byte index (getfield/putstatic/etc) |
jrose@1161 | 137 | int get_index_big() const { |
jrose@1161 | 138 | assert_index_size(2); |
jrose@1161 | 139 | return Bytes::get_Java_u2(_bc_start+1); |
duke@435 | 140 | } |
duke@435 | 141 | |
jrose@1161 | 142 | // Get 2-byte index (or 4-byte, for invokedynamic) |
jrose@1161 | 143 | int get_index_int() const { |
jrose@1161 | 144 | return has_giant_index() ? get_index_giant() : get_index_big(); |
jrose@1161 | 145 | } |
jrose@1161 | 146 | |
jrose@1161 | 147 | // Get 4-byte index, for invokedynamic. |
jrose@1161 | 148 | int get_index_giant() const { |
jrose@1161 | 149 | assert_index_size(4); |
jrose@1161 | 150 | return Bytes::get_native_u4(_bc_start+1); |
jrose@1161 | 151 | } |
jrose@1161 | 152 | |
jrose@1161 | 153 | bool has_giant_index() const { return (cur_bc() == Bytecodes::_invokedynamic); } |
duke@435 | 154 | |
duke@435 | 155 | // Get dimensions byte (multinewarray) |
duke@435 | 156 | int get_dimensions() const { return *(unsigned char*)(_pc-1); } |
duke@435 | 157 | |
duke@435 | 158 | // Sign-extended index byte/short, no widening |
duke@435 | 159 | int get_byte() const { return (int8_t)(_pc[-1]); } |
duke@435 | 160 | int get_short() const { return (int16_t)Bytes::get_Java_u2(_pc-2); } |
duke@435 | 161 | int get_long() const { return (int32_t)Bytes::get_Java_u4(_pc-4); } |
duke@435 | 162 | |
duke@435 | 163 | // Get a byte signed constant for "iinc". Invalid for other bytecodes. |
duke@435 | 164 | // If prefixed with a wide bytecode, get a wide constant |
duke@435 | 165 | int get_iinc_con() const {return (_pc==_was_wide) ? get_short() :get_byte();} |
duke@435 | 166 | |
duke@435 | 167 | // 2-byte branch offset from current pc |
duke@435 | 168 | int get_dest( ) const { |
duke@435 | 169 | assert( Bytecodes::length_at(_bc_start) == sizeof(jshort)+1, "get_dest called with bad bytecode" ); |
duke@435 | 170 | return _bc_start-_start + (short)Bytes::get_Java_u2(_pc-2); |
duke@435 | 171 | } |
duke@435 | 172 | |
duke@435 | 173 | // 2-byte branch offset from next pc |
duke@435 | 174 | int next_get_dest( ) const { |
duke@435 | 175 | address next_bc_start = _pc; |
duke@435 | 176 | assert( _pc < _end, "" ); |
duke@435 | 177 | Bytecodes::Code next_bc = (Bytecodes::Code)*_pc; |
duke@435 | 178 | assert( next_bc != Bytecodes::_wide, ""); |
duke@435 | 179 | int next_csize = Bytecodes::length_for(next_bc); |
duke@435 | 180 | assert( next_csize != 0, "" ); |
duke@435 | 181 | assert( next_bc <= Bytecodes::_jsr_w, ""); |
duke@435 | 182 | address next_pc = _pc + next_csize; |
duke@435 | 183 | assert( Bytecodes::length_at(next_bc_start) == sizeof(jshort)+1, "next_get_dest called with bad bytecode" ); |
duke@435 | 184 | return next_bc_start-_start + (short)Bytes::get_Java_u2(next_pc-2); |
duke@435 | 185 | } |
duke@435 | 186 | |
duke@435 | 187 | // 4-byte branch offset from current pc |
duke@435 | 188 | int get_far_dest( ) const { |
duke@435 | 189 | assert( Bytecodes::length_at(_bc_start) == sizeof(jint)+1, "dest4 called with bad bytecode" ); |
duke@435 | 190 | return _bc_start-_start + (int)Bytes::get_Java_u4(_pc-4); |
duke@435 | 191 | } |
duke@435 | 192 | |
duke@435 | 193 | // For a lookup or switch table, return target destination |
duke@435 | 194 | int get_int_table( int index ) const { |
duke@435 | 195 | return Bytes::get_Java_u4((address)&_table_base[index]); } |
duke@435 | 196 | |
duke@435 | 197 | // For tableswitch - get length of offset part |
duke@435 | 198 | int get_tableswitch_length() { return get_int_table(2)-get_int_table(1)+1; } |
duke@435 | 199 | |
duke@435 | 200 | int get_dest_table( int index ) const { |
duke@435 | 201 | return cur_bci() + get_int_table(index); } |
duke@435 | 202 | |
duke@435 | 203 | // --- Constant pool access --- |
duke@435 | 204 | int get_constant_index() const; |
duke@435 | 205 | int get_field_index(); |
duke@435 | 206 | int get_method_index(); |
duke@435 | 207 | |
duke@435 | 208 | // If this bytecode is a new, newarray, multianewarray, instanceof, |
duke@435 | 209 | // or checkcast, get the referenced klass. |
duke@435 | 210 | ciKlass* get_klass(bool& will_link); |
duke@435 | 211 | int get_klass_index() const; |
duke@435 | 212 | |
duke@435 | 213 | // If this bytecode is one of the ldc variants, get the referenced |
duke@435 | 214 | // constant |
duke@435 | 215 | ciConstant get_constant(); |
duke@435 | 216 | // True if the ldc variant points to an unresolved string |
duke@435 | 217 | bool is_unresolved_string() const; |
duke@435 | 218 | // True if the ldc variant points to an unresolved klass |
duke@435 | 219 | bool is_unresolved_klass() const; |
duke@435 | 220 | |
duke@435 | 221 | // If this bytecode is one of get_field, get_static, put_field, |
duke@435 | 222 | // or put_static, get the referenced field. |
duke@435 | 223 | ciField* get_field(bool& will_link); |
duke@435 | 224 | |
duke@435 | 225 | ciInstanceKlass* get_declared_field_holder(); |
duke@435 | 226 | int get_field_holder_index(); |
duke@435 | 227 | int get_field_signature_index(); |
duke@435 | 228 | |
duke@435 | 229 | // If this is a method invocation bytecode, get the invoked method. |
duke@435 | 230 | ciMethod* get_method(bool& will_link); |
duke@435 | 231 | ciKlass* get_declared_method_holder(); |
duke@435 | 232 | int get_method_holder_index(); |
duke@435 | 233 | int get_method_signature_index(); |
jrose@1161 | 234 | |
twisti@1572 | 235 | ciCPCache* get_cpcache(); |
twisti@1572 | 236 | |
jrose@1161 | 237 | private: |
jrose@1161 | 238 | void assert_index_size(int required_size) const { |
jrose@1161 | 239 | #ifdef ASSERT |
jrose@1161 | 240 | int isize = instruction_size() - (is_wide() ? 1 : 0) - 1; |
jrose@1161 | 241 | if (isize == 2 && cur_bc() == Bytecodes::_iinc) |
jrose@1161 | 242 | isize = 1; |
jrose@1161 | 243 | else if (isize <= 2) |
jrose@1161 | 244 | ; // no change |
jrose@1161 | 245 | else if (has_giant_index()) |
jrose@1161 | 246 | isize = 4; |
jrose@1161 | 247 | else |
jrose@1161 | 248 | isize = 2; |
jrose@1161 | 249 | assert(isize = required_size, "wrong index size"); |
jrose@1161 | 250 | #endif |
jrose@1161 | 251 | } |
duke@435 | 252 | }; |
duke@435 | 253 | |
duke@435 | 254 | |
duke@435 | 255 | // ciSignatureStream |
duke@435 | 256 | // |
duke@435 | 257 | // The class is used to iterate over the elements of a method signature. |
duke@435 | 258 | class ciSignatureStream : public StackObj { |
duke@435 | 259 | private: |
duke@435 | 260 | ciSignature* _sig; |
duke@435 | 261 | int _pos; |
duke@435 | 262 | public: |
duke@435 | 263 | ciSignatureStream(ciSignature* signature) { |
duke@435 | 264 | _sig = signature; |
duke@435 | 265 | _pos = 0; |
duke@435 | 266 | } |
duke@435 | 267 | |
duke@435 | 268 | bool at_return_type() { return _pos == _sig->count(); } |
duke@435 | 269 | |
duke@435 | 270 | bool is_done() { return _pos > _sig->count(); } |
duke@435 | 271 | |
duke@435 | 272 | void next() { |
duke@435 | 273 | if (_pos <= _sig->count()) { |
duke@435 | 274 | _pos++; |
duke@435 | 275 | } |
duke@435 | 276 | } |
duke@435 | 277 | |
duke@435 | 278 | ciType* type() { |
duke@435 | 279 | if (at_return_type()) { |
duke@435 | 280 | return _sig->return_type(); |
duke@435 | 281 | } else { |
duke@435 | 282 | return _sig->type_at(_pos); |
duke@435 | 283 | } |
duke@435 | 284 | } |
duke@435 | 285 | }; |
duke@435 | 286 | |
duke@435 | 287 | |
duke@435 | 288 | // ciExceptionHandlerStream |
duke@435 | 289 | // |
duke@435 | 290 | // The class is used to iterate over the exception handlers of |
duke@435 | 291 | // a method. |
duke@435 | 292 | class ciExceptionHandlerStream : public StackObj { |
duke@435 | 293 | private: |
duke@435 | 294 | // The method whose handlers we are traversing |
duke@435 | 295 | ciMethod* _method; |
duke@435 | 296 | |
duke@435 | 297 | // Our current position in the list of handlers |
duke@435 | 298 | int _pos; |
duke@435 | 299 | int _end; |
duke@435 | 300 | |
duke@435 | 301 | ciInstanceKlass* _exception_klass; |
duke@435 | 302 | int _bci; |
duke@435 | 303 | bool _is_exact; |
duke@435 | 304 | |
duke@435 | 305 | public: |
duke@435 | 306 | ciExceptionHandlerStream(ciMethod* method) { |
duke@435 | 307 | _method = method; |
duke@435 | 308 | |
duke@435 | 309 | // Force loading of method code and handlers. |
duke@435 | 310 | _method->code(); |
duke@435 | 311 | |
duke@435 | 312 | _pos = 0; |
duke@435 | 313 | _end = _method->_handler_count; |
duke@435 | 314 | _exception_klass = NULL; |
duke@435 | 315 | _bci = -1; |
duke@435 | 316 | _is_exact = false; |
duke@435 | 317 | } |
duke@435 | 318 | |
duke@435 | 319 | ciExceptionHandlerStream(ciMethod* method, int bci, |
duke@435 | 320 | ciInstanceKlass* exception_klass = NULL, |
duke@435 | 321 | bool is_exact = false) { |
duke@435 | 322 | _method = method; |
duke@435 | 323 | |
duke@435 | 324 | // Force loading of method code and handlers. |
duke@435 | 325 | _method->code(); |
duke@435 | 326 | |
duke@435 | 327 | _pos = -1; |
duke@435 | 328 | _end = _method->_handler_count + 1; // include the rethrow handler |
duke@435 | 329 | _exception_klass = (exception_klass != NULL && exception_klass->is_loaded() |
duke@435 | 330 | ? exception_klass |
duke@435 | 331 | : NULL); |
duke@435 | 332 | _bci = bci; |
duke@435 | 333 | assert(_bci >= 0, "bci out of range"); |
duke@435 | 334 | _is_exact = is_exact; |
duke@435 | 335 | next(); |
duke@435 | 336 | } |
duke@435 | 337 | |
duke@435 | 338 | // These methods are currently implemented in an odd way. |
duke@435 | 339 | // Count the number of handlers the iterator has ever produced |
duke@435 | 340 | // or will ever produce. Do not include the final rethrow handler. |
duke@435 | 341 | // That is, a trivial exception handler stream will have a count |
duke@435 | 342 | // of zero and produce just the rethrow handler. |
duke@435 | 343 | int count(); |
duke@435 | 344 | |
duke@435 | 345 | // Count the number of handlers this stream will produce from now on. |
duke@435 | 346 | // Include the current handler, and the final rethrow handler. |
duke@435 | 347 | // The remaining count will be zero iff is_done() is true, |
duke@435 | 348 | int count_remaining(); |
duke@435 | 349 | |
duke@435 | 350 | bool is_done() { |
duke@435 | 351 | return (_pos >= _end); |
duke@435 | 352 | } |
duke@435 | 353 | |
duke@435 | 354 | void next() { |
duke@435 | 355 | _pos++; |
duke@435 | 356 | if (_bci != -1) { |
duke@435 | 357 | // We are not iterating over all handlers... |
duke@435 | 358 | while (!is_done()) { |
duke@435 | 359 | ciExceptionHandler* handler = _method->_exception_handlers[_pos]; |
duke@435 | 360 | if (handler->is_in_range(_bci)) { |
duke@435 | 361 | if (handler->is_catch_all()) { |
duke@435 | 362 | // Found final active catch block. |
duke@435 | 363 | _end = _pos+1; |
duke@435 | 364 | return; |
duke@435 | 365 | } else if (_exception_klass == NULL || !handler->catch_klass()->is_loaded()) { |
duke@435 | 366 | // We cannot do any type analysis here. Must conservatively assume |
duke@435 | 367 | // catch block is reachable. |
duke@435 | 368 | return; |
duke@435 | 369 | } else if (_exception_klass->is_subtype_of(handler->catch_klass())) { |
duke@435 | 370 | // This catch clause will definitely catch the exception. |
duke@435 | 371 | // Final candidate. |
duke@435 | 372 | _end = _pos+1; |
duke@435 | 373 | return; |
duke@435 | 374 | } else if (!_is_exact && |
duke@435 | 375 | handler->catch_klass()->is_subtype_of(_exception_klass)) { |
duke@435 | 376 | // This catch block may be reachable. |
duke@435 | 377 | return; |
duke@435 | 378 | } |
duke@435 | 379 | } |
duke@435 | 380 | |
duke@435 | 381 | // The catch block was not pertinent. Go on. |
duke@435 | 382 | _pos++; |
duke@435 | 383 | } |
duke@435 | 384 | } else { |
duke@435 | 385 | // This is an iteration over all handlers. |
duke@435 | 386 | return; |
duke@435 | 387 | } |
duke@435 | 388 | } |
duke@435 | 389 | |
duke@435 | 390 | ciExceptionHandler* handler() { |
duke@435 | 391 | return _method->_exception_handlers[_pos]; |
duke@435 | 392 | } |
duke@435 | 393 | }; |