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 #include "precompiled.hpp" 26 #include "ci/ciConstant.hpp" 27 #include "ci/ciField.hpp" 28 #include "ci/ciKlass.hpp" 29 #include "ci/ciObjArrayKlass.hpp" 30 #include "ci/ciStreams.hpp" 31 #include "ci/ciSymbols.hpp" 32 #include "ci/ciUtilities.inline.hpp" 33 #include "runtime/handles.inline.hpp" 34 35 // ciExceptionHandlerStream 36 // 37 // Walk over some selected set of a methods exception handlers. 38 39 // ------------------------------------------------------------------ 40 // ciExceptionHandlerStream::count 41 // 42 // How many exception handlers are there in this stream? 43 // 44 // Implementation note: Compiler2 needs this functionality, so I had 45 int ciExceptionHandlerStream::count() { 46 int save_pos = _pos; 47 int save_end = _end; 48 49 int count = 0; 50 51 _pos = -1; 52 _end = _method->_handler_count; 53 54 55 next(); 56 while (!is_done()) { 57 count++; 58 next(); 59 } 60 61 _pos = save_pos; 62 _end = save_end; 63 64 return count; 65 } 66 67 int ciExceptionHandlerStream::count_remaining() { 68 int save_pos = _pos; 69 int save_end = _end; 70 71 int count = 0; 72 73 while (!is_done()) { 74 count++; 75 next(); 76 } 77 78 _pos = save_pos; 79 _end = save_end; 80 81 return count; 82 } 83 84 // ciBytecodeStream 85 // 86 // The class is used to iterate over the bytecodes of a method. 87 // It hides the details of constant pool structure/access by 88 // providing accessors for constant pool items. 89 90 // ------------------------------------------------------------------ 91 // ciBytecodeStream::next_wide_or_table 92 // 93 // Special handling for switch ops 94 Bytecodes::Code ciBytecodeStream::next_wide_or_table(Bytecodes::Code bc) { 95 switch (bc) { // Check for special bytecode handling 96 case Bytecodes::_wide: 97 // Special handling for the wide bytcode 98 // Get following bytecode; do not return wide 99 assert(Bytecodes::Code(_pc[0]) == Bytecodes::_wide, ""); 100 bc = Bytecodes::java_code(_raw_bc = (Bytecodes::Code)_pc[1]); 101 assert(Bytecodes::wide_length_for(bc) > 2, "must make progress"); 102 _pc += Bytecodes::wide_length_for(bc); 103 _was_wide = _pc; // Flag last wide bytecode found 104 assert(is_wide(), "accessor works right"); 105 break; 106 107 case Bytecodes::_lookupswitch: 108 _pc++; // Skip wide bytecode 109 _pc += (_start-_pc)&3; // Word align 110 _table_base = (jint*)_pc; // Capture for later usage 111 // table_base[0] is default far_dest 112 // Table has 2 lead elements (default, length), then pairs of u4 values. 113 // So load table length, and compute address at end of table 114 _pc = (address)&_table_base[2+ 2*Bytes::get_Java_u4((address)&_table_base[1])]; 115 break; 116 117 case Bytecodes::_tableswitch: { 118 _pc++; // Skip wide bytecode 119 _pc += (_start-_pc)&3; // Word align 120 _table_base = (jint*)_pc; // Capture for later usage 121 // table_base[0] is default far_dest 122 int lo = Bytes::get_Java_u4((address)&_table_base[1]);// Low bound 123 int hi = Bytes::get_Java_u4((address)&_table_base[2]);// High bound 124 int len = hi - lo + 1; // Dense table size 125 _pc = (address)&_table_base[3+len]; // Skip past table 126 break; 127 } 128 129 default: 130 fatal("unhandled bytecode"); 131 } 132 return bc; 133 } 134 135 // ------------------------------------------------------------------ 136 // ciBytecodeStream::reset_to_bci 137 void ciBytecodeStream::reset_to_bci( int bci ) { 138 _bc_start=_was_wide=0; 139 _pc = _start+bci; 140 } 141 142 // ------------------------------------------------------------------ 143 // ciBytecodeStream::force_bci 144 void ciBytecodeStream::force_bci(int bci) { 145 if (bci < 0) { 146 reset_to_bci(0); 147 _bc_start = _start + bci; 148 _bc = EOBC(); 149 } else { 150 reset_to_bci(bci); 151 next(); 152 } 153 } 154 155 156 // ------------------------------------------------------------------ 157 // Constant pool access 158 // ------------------------------------------------------------------ 159 160 // ------------------------------------------------------------------ 161 // ciBytecodeStream::get_klass_index 162 // 163 // If this bytecodes references a klass, return the index of the 164 // referenced klass. 165 int ciBytecodeStream::get_klass_index() const { 166 switch(cur_bc()) { 167 case Bytecodes::_ldc: 168 return get_index_u1(); 169 case Bytecodes::_ldc_w: 170 case Bytecodes::_ldc2_w: 171 case Bytecodes::_checkcast: 172 case Bytecodes::_instanceof: 173 case Bytecodes::_anewarray: 174 case Bytecodes::_multianewarray: 175 case Bytecodes::_new: 176 case Bytecodes::_aconst_init: 177 case Bytecodes::_newarray: 178 return get_index_u2(); 179 default: 180 ShouldNotReachHere(); 181 return 0; 182 } 183 } 184 185 // ------------------------------------------------------------------ 186 // ciBytecodeStream::get_klass 187 // 188 // If this bytecode is a new, newarray, multianewarray, instanceof, 189 // or checkcast, get the referenced klass. 190 ciKlass* ciBytecodeStream::get_klass(bool& will_link) { 191 VM_ENTRY_MARK; 192 constantPoolHandle cpool(THREAD, _method->get_Method()->constants()); 193 return CURRENT_ENV->get_klass_by_index(cpool, get_klass_index(), will_link, _holder); 194 } 195 196 // ciBytecodeStream::get_klass 197 // 198 // If this bytecode is a new, newarray, multianewarray, instanceof, 199 // or checkcast, get the referenced klass. Retuns an unloaded ciKlass 200 // if the referenced klass is not accessible. 201 ciKlass* ciBytecodeStream::get_klass() { 202 bool will_link; 203 ciKlass* klass = get_klass(will_link); 204 if (!will_link && klass->is_loaded()) { // klass not accessible 205 VM_ENTRY_MARK; 206 klass = CURRENT_ENV->get_unloaded_klass(_holder, klass->name()); 207 } 208 return klass; 209 } 210 211 // ------------------------------------------------------------------ 212 // ciBytecodeStream::is_inline_klass 213 // 214 // Check if the klass is an inline klass. 215 bool ciBytecodeStream::has_Q_signature() const { 216 VM_ENTRY_MARK; 217 constantPoolHandle cpool(THREAD, _method->get_Method()->constants()); 218 return CURRENT_ENV->has_Q_signature(cpool, get_klass_index()); 219 } 220 221 // ------------------------------------------------------------------ 222 // ciBytecodeStream::get_constant_raw_index 223 // 224 // If this bytecode is one of the ldc variants, get the index of the 225 // referenced constant. 226 int ciBytecodeStream::get_constant_raw_index() const { 227 // work-alike for Bytecode_loadconstant::raw_index() 228 switch (cur_bc()) { 229 case Bytecodes::_ldc: 230 return get_index_u1(); 231 case Bytecodes::_ldc_w: 232 case Bytecodes::_ldc2_w: 233 return get_index_u2(); 234 default: 235 ShouldNotReachHere(); 236 return 0; 237 } 238 } 239 240 // ------------------------------------------------------------------ 241 // ciBytecodeStream::get_constant_pool_index 242 // Decode any reference index into a regular pool index. 243 int ciBytecodeStream::get_constant_pool_index() const { 244 // work-alike for Bytecode_loadconstant::pool_index() 245 int index = get_constant_raw_index(); 246 if (has_cache_index()) { 247 VM_ENTRY_MARK; 248 constantPoolHandle cpool(THREAD, _method->get_Method()->constants()); 249 return cpool->object_to_cp_index(index); 250 } 251 return index; 252 } 253 254 // ------------------------------------------------------------------ 255 // ciBytecodeStream::get_constant 256 // 257 // If this bytecode is one of the ldc variants, get the referenced 258 // constant. 259 ciConstant ciBytecodeStream::get_constant() { 260 VM_ENTRY_MARK; 261 constantPoolHandle cpool(THREAD, _method->get_Method()->constants()); 262 int pool_index = get_constant_raw_index(); 263 int cache_index = -1; 264 if (has_cache_index()) { 265 cache_index = pool_index; 266 pool_index = cpool->object_to_cp_index(cache_index); 267 } else if (cpool->tag_at(pool_index).is_dynamic_constant() || 268 cpool->tag_at(pool_index).is_dynamic_constant_in_error()) { 269 // Condy with primitive type is not quickened, so the index into resolved reference cache should be reconstructed. 270 assert(is_java_primitive(cpool->basic_type_for_constant_at(pool_index)), "not quickened"); 271 cache_index = cpool->cp_to_object_index(pool_index); 272 } 273 return CURRENT_ENV->get_constant_by_index(cpool, pool_index, cache_index, _holder); 274 } 275 276 // ------------------------------------------------------------------ 277 // ciBytecodeStream::get_constant_pool_tag 278 // 279 // If this bytecode is one of the ldc variants, get the referenced 280 // constant. 281 constantTag ciBytecodeStream::get_constant_pool_tag(int index) const { 282 VM_ENTRY_MARK; 283 return _method->get_Method()->constants()->constant_tag_at(index); 284 } 285 286 // ------------------------------------------------------------------ 287 // ciBytecodeStream::get_raw_pool_tag 288 // 289 constantTag ciBytecodeStream::get_raw_pool_tag_at(int index) const { 290 VM_ENTRY_MARK; 291 return _method->get_Method()->constants()->tag_at(index); 292 } 293 294 // ------------------------------------------------------------------ 295 // ciBytecodeStream::get_basic_type_for_constant_at 296 // 297 BasicType ciBytecodeStream::get_basic_type_for_constant_at(int index) const { 298 VM_ENTRY_MARK; 299 return _method->get_Method()->constants()->basic_type_for_constant_at(index); 300 } 301 302 // ------------------------------------------------------------------ 303 // ciBytecodeStream::get_field_index 304 // 305 // If this is a field access bytecode, get the constant pool 306 // index of the referenced field. 307 int ciBytecodeStream::get_field_index() { 308 assert(cur_bc() == Bytecodes::_getfield || 309 cur_bc() == Bytecodes::_putfield || 310 cur_bc() == Bytecodes::_getstatic || 311 cur_bc() == Bytecodes::_putstatic || 312 cur_bc() == Bytecodes::_withfield, "wrong bc"); 313 return get_index_u2_cpcache(); 314 } 315 316 317 // ------------------------------------------------------------------ 318 // ciBytecodeStream::get_field 319 // 320 // If this bytecode is one of get_field, get_static, put_field, 321 // or put_static, get the referenced field. 322 ciField* ciBytecodeStream::get_field(bool& will_link) { 323 ciField* f = CURRENT_ENV->get_field_by_index(_holder, get_field_index()); 324 will_link = f->will_link(_method, _bc); 325 return f; 326 } 327 328 329 // ------------------------------------------------------------------ 330 // ciBytecodeStream::get_declared_field_holder 331 // 332 // Get the declared holder of the currently referenced field. 333 // 334 // Usage note: the holder() of a ciField class returns the canonical 335 // holder of the field, rather than the holder declared in the 336 // bytecodes. 337 // 338 // There is no "will_link" result passed back. The user is responsible 339 // for checking linkability when retrieving the associated field. 340 ciInstanceKlass* ciBytecodeStream::get_declared_field_holder() { 341 VM_ENTRY_MARK; 342 constantPoolHandle cpool(THREAD, _method->get_Method()->constants()); 343 int holder_index = get_field_holder_index(); 344 bool ignore; 345 return CURRENT_ENV->get_klass_by_index(cpool, holder_index, ignore, _holder) 346 ->as_instance_klass(); 347 } 348 349 // ------------------------------------------------------------------ 350 // ciBytecodeStream::get_field_holder_index 351 // 352 // Get the constant pool index of the declared holder of the field 353 // referenced by the current bytecode. Used for generating 354 // deoptimization information. 355 int ciBytecodeStream::get_field_holder_index() { 356 GUARDED_VM_ENTRY( 357 ConstantPool* cpool = _holder->get_instanceKlass()->constants(); 358 return cpool->klass_ref_index_at(get_field_index()); 359 ) 360 } 361 362 // ------------------------------------------------------------------ 363 // ciBytecodeStream::get_method_index 364 // 365 // If this is a method invocation bytecode, get the constant pool 366 // index of the invoked method. 367 int ciBytecodeStream::get_method_index() { 368 assert(Bytecodes::is_invoke(cur_bc()), "invalid bytecode: %s", Bytecodes::name(cur_bc())); 369 if (has_index_u4()) 370 return get_index_u4(); // invokedynamic 371 return get_index_u2_cpcache(); 372 } 373 374 // ------------------------------------------------------------------ 375 // ciBytecodeStream::get_method 376 // 377 // If this is a method invocation bytecode, get the invoked method. 378 // Additionally return the declared signature to get more concrete 379 // type information if required (Cf. invokedynamic and invokehandle). 380 ciMethod* ciBytecodeStream::get_method(bool& will_link, ciSignature* *declared_signature_result) { 381 VM_ENTRY_MARK; 382 ciEnv* env = CURRENT_ENV; 383 constantPoolHandle cpool(THREAD, _method->get_Method()->constants()); 384 ciMethod* m = env->get_method_by_index(cpool, get_method_index(), cur_bc(), _holder); 385 will_link = m->is_loaded(); 386 387 // Use the signature stored in the CP cache to create a signature 388 // with correct types (in respect to class loaders). 389 // 390 // In classic Java (before Java 7) there is never the slightest 391 // difference between the signature at the call site and that of the 392 // method. Such a difference would have been a type error in the 393 // JVM. 394 // 395 // Now there are a few circumstances where the signature of a call 396 // site (which controls the outgoing stacked arguments) can differ 397 // from the signature of the method (which controls the receipt of 398 // those arguments at the method entry point). 399 // 400 // A. The signatures can differ if the callee is a static method and 401 // the caller thinks it is calling a non-static method (VH.get). 402 // This requires the method signature to have an explicit leading 403 // argument for the implicit 'this', not present at the call site. 404 // 405 // B. The call site can have less specific parameter types than the 406 // method, allowing loosely-typed code to handle strongly-typed 407 // methods. This happens with linkToStatic and related linker 408 // commands. Obviously the loosely-typed code has to ensure that 409 // the strongly typed method's invariants are respected, and this is 410 // done by issuing dynamic casts. 411 // 412 // C. The call site can have more specific parameter types than the 413 // method, allowing loosely-typed methods to handle strongly-typed 414 // requests. 415 // 416 // D. There are corresponding effects with return values, such as 417 // boolean method returning an int to an int-receiving call site, 418 // even though the method thought it returned just a boolean. 419 // 420 // E. The calling sequence at a particular call site may add an 421 // "appendix" argument not mentioned in the call site signature. It 422 // is expected by the method signature, though, and this adds to the 423 // method's arity, even after 'this' parameter effects (A) are 424 // discounted. Appendixes are used by invokehandle and 425 // invokedynamic instructions. 426 // 427 // F. A linker method (linkToStatic, etc.) can also take an extra 428 // argument, a MemberName which routes the call to a concrete 429 // strongly-typed method. In this case the linker method may also 430 // differ in any of the ways A-D. The eventual method will ignore 431 // the presence of the extra argument. 432 // 433 // None of these changes to calling sequences requires an argument 434 // to be moved or reformatted in any way. This works because all 435 // references look alike to the JVM, as do all primitives (except 436 // float/long/double). Another required property of the JVM is 437 // that, if a trailing argument is added or dropped, the placement 438 // of other arguments does not change. This allows cases E and F to 439 // work smoothly, against without any moving or reformatting, 440 // despite the arity change. 441 // 442 if (has_local_signature()) { 443 Symbol* local_signature = cpool->symbol_at(get_method_signature_index(cpool)); 444 ciSymbol* sig_sym = env->get_symbol(local_signature); 445 ciKlass* pool_holder = env->get_klass(cpool->pool_holder()); 446 ciSignature* call_site_sig = new (env->arena()) ciSignature(pool_holder, cpool, sig_sym); 447 // Examples of how the call site signature can differ from the method's own signature: 448 // 449 // meth = static jboolean java.lang.invoke.VarHandleGuards.guard_LII_Z(jobject, jobject, jint, jint, jobject) 450 // msig = (Ljava/lang/invoke/VarHandle;Ljava/lang/Object;IILjava/lang/invoke/VarHandle$AccessDescriptor;)Z 451 // call = (Ljava/util/concurrent/locks/AbstractQueuedSynchronizer;II)Z 452 // 453 // meth = static jobject java.lang.invoke.LambdaForm$MH/0x0000000800066840.linkToTargetMethod(jobject, jobject) 454 // msig = (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; 455 // call = (Ljava/lang/String;)Ljava/util/function/Predicate; 456 // 457 (*declared_signature_result) = call_site_sig; 458 459 } else { 460 // We can just use the method's own signature. It may differ from the call site, but not by much. 461 // 462 // Examples of how the call site signature can differ from the method's signature: 463 // 464 // meth = static final native jint java.lang.invoke.MethodHandle.linkToStatic(jobject, jobject, jint, jint, jobject) 465 // msig = (Ljava/lang/Object;Ljava/lang/Object;IILjava/lang/invoke/MemberName;)I 466 // call = (Ljava/lang/invoke/VarHandle;Ljava/lang/Object;IILjava/lang/invoke/MemberName;)Z 467 // 468 // meth = final native jint java.lang.invoke.MethodHandle.invokeBasic(jobject, jobject, jint, jint) 469 // msig = (Ljava/lang/Object;Ljava/lang/Object;II)I 470 // call = (Ljava/lang/invoke/VarHandle;Ljava/lang/Object;II)Z 471 // 472 (*declared_signature_result) = m->signature(); 473 } 474 return m; 475 } 476 477 // ------------------------------------------------------------------ 478 // ciBytecodeStream::has_appendix 479 // 480 // Returns true if there is an appendix argument stored in the 481 // constant pool cache at the current bci. 482 bool ciBytecodeStream::has_appendix() { 483 VM_ENTRY_MARK; 484 constantPoolHandle cpool(THREAD, _method->get_Method()->constants()); 485 return ConstantPool::has_appendix_at_if_loaded(cpool, get_method_index()); 486 } 487 488 // ------------------------------------------------------------------ 489 // ciBytecodeStream::get_appendix 490 // 491 // Return the appendix argument stored in the constant pool cache at 492 // the current bci. 493 ciObject* ciBytecodeStream::get_appendix() { 494 VM_ENTRY_MARK; 495 constantPoolHandle cpool(THREAD, _method->get_Method()->constants()); 496 oop appendix_oop = ConstantPool::appendix_at_if_loaded(cpool, get_method_index()); 497 return CURRENT_ENV->get_object(appendix_oop); 498 } 499 500 // ------------------------------------------------------------------ 501 // ciBytecodeStream::has_local_signature 502 // 503 // Returns true if the method stored in the constant 504 // pool cache at the current bci has a local signature. 505 bool ciBytecodeStream::has_local_signature() { 506 GUARDED_VM_ENTRY( 507 constantPoolHandle cpool(Thread::current(), _method->get_Method()->constants()); 508 return ConstantPool::has_local_signature_at_if_loaded(cpool, get_method_index()); 509 ) 510 } 511 512 // ------------------------------------------------------------------ 513 // ciBytecodeStream::get_declared_method_holder 514 // 515 // Get the declared holder of the currently referenced method. 516 // 517 // Usage note: the holder() of a ciMethod class returns the canonical 518 // holder of the method, rather than the holder declared in the 519 // bytecodes. 520 // 521 // There is no "will_link" result passed back. The user is responsible 522 // for checking linkability when retrieving the associated method. 523 ciKlass* ciBytecodeStream::get_declared_method_holder() { 524 VM_ENTRY_MARK; 525 constantPoolHandle cpool(THREAD, _method->get_Method()->constants()); 526 bool ignore; 527 // report as MethodHandle for invokedynamic, which is syntactically classless 528 if (cur_bc() == Bytecodes::_invokedynamic) { 529 return CURRENT_ENV->MethodHandle_klass(); 530 } 531 return CURRENT_ENV->get_klass_by_index(cpool, get_method_holder_index(), ignore, _holder); 532 } 533 534 // ------------------------------------------------------------------ 535 // ciBytecodeStream::get_method_holder_index 536 // 537 // Get the constant pool index of the declared holder of the method 538 // referenced by the current bytecode. Used for generating 539 // deoptimization information. 540 int ciBytecodeStream::get_method_holder_index() { 541 ConstantPool* cpool = _method->get_Method()->constants(); 542 return cpool->klass_ref_index_at(get_method_index()); 543 } 544 545 // ------------------------------------------------------------------ 546 // ciBytecodeStream::get_method_signature_index 547 // 548 // Get the constant pool index of the signature of the method 549 // referenced by the current bytecode. Used for generating 550 // deoptimization information. 551 int ciBytecodeStream::get_method_signature_index(const constantPoolHandle& cpool) { 552 GUARDED_VM_ENTRY( 553 const int method_index = get_method_index(); 554 const int name_and_type_index = cpool->name_and_type_ref_index_at(method_index); 555 return cpool->signature_ref_index_at(name_and_type_index); 556 ) 557 } 558