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