1 /* 2 * Copyright (c) 1999, 2016, 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 #include "precompiled.hpp" 26 #include "ci/ciCallSite.hpp" 27 #include "ci/ciConstant.hpp" 28 #include "ci/ciField.hpp" 29 #include "ci/ciStreams.hpp" 30 #include "ci/ciUtilities.hpp" 31 32 // ciExceptionHandlerStream 33 // 34 // Walk over some selected set of a methods exception handlers. 35 36 // ------------------------------------------------------------------ 37 // ciExceptionHandlerStream::count 38 // 39 // How many exception handlers are there in this stream? 40 // 41 // Implementation note: Compiler2 needs this functionality, so I had 42 int ciExceptionHandlerStream::count() { 43 int save_pos = _pos; 44 int save_end = _end; 45 46 int count = 0; 47 48 _pos = -1; 49 _end = _method->_handler_count; 50 51 52 next(); 53 while (!is_done()) { 54 count++; 55 next(); 56 } 57 58 _pos = save_pos; 59 _end = save_end; 60 61 return count; 62 } 63 64 int ciExceptionHandlerStream::count_remaining() { 65 int save_pos = _pos; 66 int save_end = _end; 67 68 int count = 0; 69 70 while (!is_done()) { 71 count++; 72 next(); 73 } 74 75 _pos = save_pos; 76 _end = save_end; 77 78 return count; 79 } 80 81 // ciBytecodeStream 82 // 83 // The class is used to iterate over the bytecodes of a method. 84 // It hides the details of constant pool structure/access by 85 // providing accessors for constant pool items. 86 87 // ------------------------------------------------------------------ 88 // ciBytecodeStream::next_wide_or_table 89 // 90 // Special handling for switch ops 91 Bytecodes::Code ciBytecodeStream::next_wide_or_table(Bytecodes::Code bc) { 92 switch (bc) { // Check for special bytecode handling 93 case Bytecodes::_wide: 94 // Special handling for the wide bytcode 95 // Get following bytecode; do not return wide 96 assert(Bytecodes::Code(_pc[0]) == Bytecodes::_wide, ""); 97 bc = Bytecodes::java_code(_raw_bc = (Bytecodes::Code)_pc[1]); 98 assert(Bytecodes::wide_length_for(bc) > 2, "must make progress"); 99 _pc += Bytecodes::wide_length_for(bc); 100 _was_wide = _pc; // Flag last wide bytecode found 101 assert(is_wide(), "accessor works right"); 102 break; 103 104 case Bytecodes::_lookupswitch: 105 _pc++; // Skip wide bytecode 106 _pc += (_start-_pc)&3; // Word align 107 _table_base = (jint*)_pc; // Capture for later usage 108 // table_base[0] is default far_dest 109 // Table has 2 lead elements (default, length), then pairs of u4 values. 110 // So load table length, and compute address at end of table 111 _pc = (address)&_table_base[2+ 2*Bytes::get_Java_u4((address)&_table_base[1])]; 112 break; 113 114 case Bytecodes::_tableswitch: { 115 _pc++; // Skip wide bytecode 116 _pc += (_start-_pc)&3; // Word align 117 _table_base = (jint*)_pc; // Capture for later usage 118 // table_base[0] is default far_dest 119 int lo = Bytes::get_Java_u4((address)&_table_base[1]);// Low bound 120 int hi = Bytes::get_Java_u4((address)&_table_base[2]);// High bound 121 int len = hi - lo + 1; // Dense table size 122 _pc = (address)&_table_base[3+len]; // Skip past table 123 break; 124 } 125 126 default: 127 fatal("unhandled bytecode"); 128 } 129 return bc; 130 } 131 132 // ------------------------------------------------------------------ 133 // ciBytecodeStream::reset_to_bci 134 void ciBytecodeStream::reset_to_bci( int bci ) { 135 _bc_start=_was_wide=0; 136 _pc = _start+bci; 137 } 138 139 // ------------------------------------------------------------------ 140 // ciBytecodeStream::force_bci 141 void ciBytecodeStream::force_bci(int bci) { 142 if (bci < 0) { 143 reset_to_bci(0); 144 _bc_start = _start + bci; 145 _bc = EOBC(); 146 } else { 147 reset_to_bci(bci); 148 next(); 149 } 150 } 151 152 153 // ------------------------------------------------------------------ 154 // Constant pool access 155 // ------------------------------------------------------------------ 156 157 // ------------------------------------------------------------------ 158 // ciBytecodeStream::get_klass_index 159 // 160 // If this bytecodes references a klass, return the index of the 161 // referenced klass. 162 int ciBytecodeStream::get_klass_index() const { 163 switch(cur_bc()) { 164 case Bytecodes::_ldc: 165 return get_index_u1(); 166 case Bytecodes::_ldc_w: 167 case Bytecodes::_ldc2_w: 168 case Bytecodes::_checkcast: 169 case Bytecodes::_instanceof: 170 case Bytecodes::_anewarray: 171 case Bytecodes::_multianewarray: 172 case Bytecodes::_new: 173 case Bytecodes::_defaultvalue: 174 case Bytecodes::_newarray: 175 return get_index_u2(); 176 default: 177 ShouldNotReachHere(); 178 return 0; 179 } 180 } 181 182 // ------------------------------------------------------------------ 183 // ciBytecodeStream::get_klass 184 // 185 // If this bytecode is a new, newarray, multianewarray, instanceof, vbox, 186 // vunbox, or checkcast, get the referenced klass. 187 ciKlass* ciBytecodeStream::get_klass(bool& will_link) { 188 VM_ENTRY_MARK; 189 constantPoolHandle cpool(_method->get_Method()->constants()); 190 return CURRENT_ENV->get_klass_by_index(cpool, get_klass_index(), will_link, _holder); 191 } 192 193 // ------------------------------------------------------------------ 194 // ciBytecodeStream::get_constant_raw_index 195 // 196 // If this bytecode is one of the ldc variants, get the index of the 197 // referenced constant. 198 int ciBytecodeStream::get_constant_raw_index() const { 199 // work-alike for Bytecode_loadconstant::raw_index() 200 switch (cur_bc()) { 201 case Bytecodes::_ldc: 202 return get_index_u1(); 203 case Bytecodes::_ldc_w: 204 case Bytecodes::_ldc2_w: 205 return get_index_u2(); 206 default: 207 ShouldNotReachHere(); 208 return 0; 209 } 210 } 211 212 // ------------------------------------------------------------------ 213 // ciBytecodeStream::get_constant_pool_index 214 // Decode any reference index into a regular pool index. 215 int ciBytecodeStream::get_constant_pool_index() const { 216 // work-alike for Bytecode_loadconstant::pool_index() 217 int index = get_constant_raw_index(); 218 if (has_cache_index()) { 219 VM_ENTRY_MARK; 220 constantPoolHandle cpool(_method->get_Method()->constants()); 221 return cpool->object_to_cp_index(index); 222 } 223 return index; 224 } 225 226 // ------------------------------------------------------------------ 227 // ciBytecodeStream::get_constant_cache_index 228 // Return the CP cache index, or -1 if there isn't any. 229 int ciBytecodeStream::get_constant_cache_index() const { 230 // work-alike for Bytecode_loadconstant::cache_index() 231 return has_cache_index() ? get_constant_raw_index() : -1; 232 } 233 234 // ------------------------------------------------------------------ 235 // ciBytecodeStream::get_constant 236 // 237 // If this bytecode is one of the ldc variants, get the referenced 238 // constant. 239 ciConstant ciBytecodeStream::get_constant() { 240 int pool_index = get_constant_raw_index(); 241 int cache_index = -1; 242 if (has_cache_index()) { 243 cache_index = pool_index; 244 pool_index = -1; 245 } 246 VM_ENTRY_MARK; 247 constantPoolHandle cpool(_method->get_Method()->constants()); 248 return CURRENT_ENV->get_constant_by_index(cpool, pool_index, cache_index, _holder); 249 } 250 251 // ------------------------------------------------------------------ 252 // ciBytecodeStream::get_constant_pool_tag 253 // 254 // If this bytecode is one of the ldc variants, get the referenced 255 // constant. 256 constantTag ciBytecodeStream::get_constant_pool_tag(int index) const { 257 VM_ENTRY_MARK; 258 BasicType bt = _method->get_Method()->constants()->basic_type_for_constant_at(index); 259 return constantTag::ofBasicType(bt); 260 } 261 262 // ------------------------------------------------------------------ 263 // ciBytecodeStream::get_field_index 264 // 265 // If this is a field access bytecode, get the constant pool 266 // index of the referenced field. 267 int ciBytecodeStream::get_field_index() { 268 assert(cur_bc() == Bytecodes::_getfield || 269 cur_bc() == Bytecodes::_putfield || 270 cur_bc() == Bytecodes::_getstatic || 271 cur_bc() == Bytecodes::_putstatic || 272 cur_bc() == Bytecodes::_withfield, "wrong bc"); 273 return get_index_u2_cpcache(); 274 } 275 276 277 // ------------------------------------------------------------------ 278 // ciBytecodeStream::get_field 279 // 280 // If this bytecode is one of get_field, get_static, put_field, 281 // or put_static, get the referenced field. 282 ciField* ciBytecodeStream::get_field(bool& will_link) { 283 ciField* f = CURRENT_ENV->get_field_by_index(_holder, get_field_index()); 284 will_link = f->will_link(_method, _bc); 285 return f; 286 } 287 288 289 // ------------------------------------------------------------------ 290 // ciBytecodeStream::get_declared_field_holder 291 // 292 // Get the declared holder of the currently referenced field. 293 // 294 // Usage note: the holder() of a ciField class returns the canonical 295 // holder of the field, rather than the holder declared in the 296 // bytecodes. 297 // 298 // There is no "will_link" result passed back. The user is responsible 299 // for checking linkability when retrieving the associated field. 300 ciInstanceKlass* ciBytecodeStream::get_declared_field_holder() { 301 VM_ENTRY_MARK; 302 constantPoolHandle cpool(_method->get_Method()->constants()); 303 int holder_index = get_field_holder_index(); 304 bool ignore; 305 return CURRENT_ENV->get_klass_by_index(cpool, holder_index, ignore, _holder) 306 ->as_instance_klass(); 307 } 308 309 // ------------------------------------------------------------------ 310 // ciBytecodeStream::get_field_holder_index 311 // 312 // Get the constant pool index of the declared holder of the field 313 // referenced by the current bytecode. Used for generating 314 // deoptimization information. 315 int ciBytecodeStream::get_field_holder_index() { 316 GUARDED_VM_ENTRY( 317 ConstantPool* cpool = _holder->get_instanceKlass()->constants(); 318 return cpool->klass_ref_index_at(get_field_index()); 319 ) 320 } 321 322 // ------------------------------------------------------------------ 323 // ciBytecodeStream::get_field_signature_index 324 // 325 // Get the constant pool index of the signature of the field 326 // referenced by the current bytecode. Used for generating 327 // deoptimization information. 328 int ciBytecodeStream::get_field_signature_index() { 329 VM_ENTRY_MARK; 330 ConstantPool* cpool = _holder->get_instanceKlass()->constants(); 331 int nt_index = cpool->name_and_type_ref_index_at(get_field_index()); 332 return cpool->signature_ref_index_at(nt_index); 333 } 334 335 // ------------------------------------------------------------------ 336 // ciBytecodeStream::get_method_index 337 // 338 // If this is a method invocation bytecode, get the constant pool 339 // index of the invoked method. 340 int ciBytecodeStream::get_method_index() { 341 #ifdef ASSERT 342 switch (cur_bc()) { 343 case Bytecodes::_invokeinterface: 344 case Bytecodes::_invokevirtual: 345 case Bytecodes::_invokespecial: 346 case Bytecodes::_invokestatic: 347 case Bytecodes::_invokedynamic: 348 break; 349 default: 350 ShouldNotReachHere(); 351 } 352 #endif 353 if (has_index_u4()) 354 return get_index_u4(); // invokedynamic 355 return get_index_u2_cpcache(); 356 } 357 358 // ------------------------------------------------------------------ 359 // ciBytecodeStream::get_method 360 // 361 // If this is a method invocation bytecode, get the invoked method. 362 // Additionally return the declared signature to get more concrete 363 // type information if required (Cf. invokedynamic and invokehandle). 364 ciMethod* ciBytecodeStream::get_method(bool& will_link, ciSignature* *declared_signature_result) { 365 VM_ENTRY_MARK; 366 ciEnv* env = CURRENT_ENV; 367 constantPoolHandle cpool(THREAD, _method->get_Method()->constants()); 368 ciMethod* m = env->get_method_by_index(cpool, get_method_index(), cur_bc(), _holder); 369 will_link = m->is_loaded(); 370 371 // Use the MethodType stored in the CP cache to create a signature 372 // with correct types (in respect to class loaders). 373 if (has_method_type()) { 374 ciSymbol* sig_sym = env->get_symbol(cpool->symbol_at(get_method_signature_index(cpool))); 375 ciKlass* pool_holder = env->get_klass(cpool->pool_holder()); 376 ciMethodType* method_type = get_method_type(); 377 ciSignature* declared_signature = new (env->arena()) ciSignature(pool_holder, sig_sym, method_type); 378 (*declared_signature_result) = declared_signature; 379 } else { 380 (*declared_signature_result) = m->signature(); 381 } 382 return m; 383 } 384 385 // ------------------------------------------------------------------ 386 // ciBytecodeStream::has_appendix 387 // 388 // Returns true if there is an appendix argument stored in the 389 // constant pool cache at the current bci. 390 bool ciBytecodeStream::has_appendix() { 391 VM_ENTRY_MARK; 392 constantPoolHandle cpool(_method->get_Method()->constants()); 393 return ConstantPool::has_appendix_at_if_loaded(cpool, get_method_index()); 394 } 395 396 // ------------------------------------------------------------------ 397 // ciBytecodeStream::get_appendix 398 // 399 // Return the appendix argument stored in the constant pool cache at 400 // the current bci. 401 ciObject* ciBytecodeStream::get_appendix() { 402 VM_ENTRY_MARK; 403 constantPoolHandle cpool(_method->get_Method()->constants()); 404 oop appendix_oop = ConstantPool::appendix_at_if_loaded(cpool, get_method_index()); 405 return CURRENT_ENV->get_object(appendix_oop); 406 } 407 408 // ------------------------------------------------------------------ 409 // ciBytecodeStream::has_method_type 410 // 411 // Returns true if there is a MethodType argument stored in the 412 // constant pool cache at the current bci. 413 bool ciBytecodeStream::has_method_type() { 414 GUARDED_VM_ENTRY( 415 constantPoolHandle cpool(_method->get_Method()->constants()); 416 return ConstantPool::has_method_type_at_if_loaded(cpool, get_method_index()); 417 ) 418 } 419 420 // ------------------------------------------------------------------ 421 // ciBytecodeStream::get_method_type 422 // 423 // Return the MethodType stored in the constant pool cache at 424 // the current bci. 425 ciMethodType* ciBytecodeStream::get_method_type() { 426 GUARDED_VM_ENTRY( 427 constantPoolHandle cpool(_method->get_Method()->constants()); 428 oop method_type_oop = ConstantPool::method_type_at_if_loaded(cpool, get_method_index()); 429 return CURRENT_ENV->get_object(method_type_oop)->as_method_type(); 430 ) 431 } 432 433 // ------------------------------------------------------------------ 434 // ciBytecodeStream::get_declared_method_holder 435 // 436 // Get the declared holder of the currently referenced method. 437 // 438 // Usage note: the holder() of a ciMethod class returns the canonical 439 // holder of the method, rather than the holder declared in the 440 // bytecodes. 441 // 442 // There is no "will_link" result passed back. The user is responsible 443 // for checking linkability when retrieving the associated method. 444 ciKlass* ciBytecodeStream::get_declared_method_holder() { 445 VM_ENTRY_MARK; 446 constantPoolHandle cpool(_method->get_Method()->constants()); 447 bool ignore; 448 // report as MethodHandle for invokedynamic, which is syntactically classless 449 if (cur_bc() == Bytecodes::_invokedynamic) 450 return CURRENT_ENV->get_klass_by_name(_holder, ciSymbol::java_lang_invoke_MethodHandle(), false); 451 return CURRENT_ENV->get_klass_by_index(cpool, get_method_holder_index(), ignore, _holder); 452 } 453 454 // ------------------------------------------------------------------ 455 // ciBytecodeStream::get_method_holder_index 456 // 457 // Get the constant pool index of the declared holder of the method 458 // referenced by the current bytecode. Used for generating 459 // deoptimization information. 460 int ciBytecodeStream::get_method_holder_index() { 461 ConstantPool* cpool = _method->get_Method()->constants(); 462 return cpool->klass_ref_index_at(get_method_index()); 463 } 464 465 // ------------------------------------------------------------------ 466 // ciBytecodeStream::get_method_signature_index 467 // 468 // Get the constant pool index of the signature of the method 469 // referenced by the current bytecode. Used for generating 470 // deoptimization information. 471 int ciBytecodeStream::get_method_signature_index(const constantPoolHandle& cpool) { 472 GUARDED_VM_ENTRY( 473 const int method_index = get_method_index(); 474 const int name_and_type_index = cpool->name_and_type_ref_index_at(method_index); 475 return cpool->signature_ref_index_at(name_and_type_index); 476 ) 477 } 478 479 // ------------------------------------------------------------------ 480 // ciBytecodeStream::get_resolved_references 481 ciObjArray* ciBytecodeStream::get_resolved_references() { 482 VM_ENTRY_MARK; 483 // Get the constant pool. 484 ConstantPool* cpool = _holder->get_instanceKlass()->constants(); 485 486 // Create a resolved references array and return it. 487 return CURRENT_ENV->get_object(cpool->resolved_references())->as_obj_array(); 488 }