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