1 /*
   2  * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
   4  * Copyright (c) 2020, 2023, Huawei Technologies Co., Ltd. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #include "precompiled.hpp"
  28 #include "asm/macroAssembler.inline.hpp"
  29 #include "gc/shared/barrierSetAssembler.hpp"
  30 #include "gc/shared/collectedHeap.hpp"
  31 #include "gc/shared/tlab_globals.hpp"
  32 #include "interpreter/interp_masm.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "interpreter/interpreterRuntime.hpp"
  35 #include "interpreter/templateTable.hpp"
  36 #include "memory/universe.hpp"
  37 #include "oops/method.inline.hpp"
  38 #include "oops/methodData.hpp"
  39 #include "oops/objArrayKlass.hpp"
  40 #include "oops/oop.inline.hpp"
  41 #include "oops/resolvedFieldEntry.hpp"
  42 #include "oops/resolvedIndyEntry.hpp"
  43 #include "oops/resolvedMethodEntry.hpp"
  44 #include "prims/jvmtiExport.hpp"
  45 #include "prims/methodHandles.hpp"
  46 #include "runtime/frame.inline.hpp"
  47 #include "runtime/sharedRuntime.hpp"
  48 #include "runtime/stubRoutines.hpp"
  49 #include "runtime/synchronizer.hpp"
  50 #include "utilities/powerOfTwo.hpp"
  51 
  52 #define __ _masm->
  53 
  54 // Address computation: local variables
  55 
  56 static inline Address iaddress(int n) {
  57   return Address(xlocals, Interpreter::local_offset_in_bytes(n));
  58 }
  59 
  60 static inline Address laddress(int n) {
  61   return iaddress(n + 1);
  62 }
  63 
  64 static inline Address faddress(int n) {
  65   return iaddress(n);
  66 }
  67 
  68 static inline Address daddress(int n) {
  69   return laddress(n);
  70 }
  71 
  72 static inline Address aaddress(int n) {
  73   return iaddress(n);
  74 }
  75 
  76 static inline Address iaddress(Register r, Register temp, InterpreterMacroAssembler* _masm) {
  77   _masm->shadd(temp, r, xlocals, temp, 3);
  78   return Address(temp, 0);
  79 }
  80 
  81 static inline Address laddress(Register r, Register temp, InterpreterMacroAssembler* _masm) {
  82   _masm->shadd(temp, r, xlocals, temp, 3);
  83   return Address(temp, Interpreter::local_offset_in_bytes(1));;
  84 }
  85 
  86 static inline Address faddress(Register r, Register temp, InterpreterMacroAssembler* _masm) {
  87   return iaddress(r, temp, _masm);
  88 }
  89 
  90 static inline Address daddress(Register r, Register temp, InterpreterMacroAssembler* _masm) {
  91   return laddress(r, temp, _masm);
  92 }
  93 
  94 static inline Address aaddress(Register r, Register temp, InterpreterMacroAssembler* _masm) {
  95   return iaddress(r, temp, _masm);
  96 }
  97 
  98 static inline Address at_rsp() {
  99   return Address(esp, 0);
 100 }
 101 
 102 // At top of Java expression stack which may be different than esp().  It
 103 // isn't for category 1 objects.
 104 static inline Address at_tos   () {
 105   return Address(esp,  Interpreter::expr_offset_in_bytes(0));
 106 }
 107 
 108 static inline Address at_tos_p1() {
 109   return Address(esp, Interpreter::expr_offset_in_bytes(1));
 110 }
 111 
 112 static inline Address at_tos_p2() {
 113   return Address(esp, Interpreter::expr_offset_in_bytes(2));
 114 }
 115 
 116 static inline Address at_tos_p3() {
 117   return Address(esp, Interpreter::expr_offset_in_bytes(3));
 118 }
 119 
 120 static inline Address at_tos_p4() {
 121   return Address(esp, Interpreter::expr_offset_in_bytes(4));
 122 }
 123 
 124 static inline Address at_tos_p5() {
 125   return Address(esp, Interpreter::expr_offset_in_bytes(5));
 126 }
 127 
 128 // Miscellaneous helper routines
 129 // Store an oop (or null) at the Address described by obj.
 130 // If val == noreg this means store a null
 131 static void do_oop_store(InterpreterMacroAssembler* _masm,
 132                          Address dst,
 133                          Register val,
 134                          DecoratorSet decorators) {
 135   assert(val == noreg || val == x10, "parameter is just for looks");
 136   __ store_heap_oop(dst, val, x28, x29, x13, decorators);
 137 }
 138 
 139 static void do_oop_load(InterpreterMacroAssembler* _masm,
 140                         Address src,
 141                         Register dst,
 142                         DecoratorSet decorators) {
 143   __ load_heap_oop(dst, src, x28, x29, decorators);
 144 }
 145 
 146 Address TemplateTable::at_bcp(int offset) {
 147   assert(_desc->uses_bcp(), "inconsistent uses_bcp information");
 148   return Address(xbcp, offset);
 149 }
 150 
 151 void TemplateTable::patch_bytecode(Bytecodes::Code bc, Register bc_reg,
 152                                    Register temp_reg, bool load_bc_into_bc_reg /*=true*/,
 153                                    int byte_no) {
 154   if (!RewriteBytecodes) { return; }
 155   Label L_patch_done;
 156 
 157   switch (bc) {
 158     case Bytecodes::_fast_aputfield:  // fall through
 159     case Bytecodes::_fast_bputfield:  // fall through
 160     case Bytecodes::_fast_zputfield:  // fall through
 161     case Bytecodes::_fast_cputfield:  // fall through
 162     case Bytecodes::_fast_dputfield:  // fall through
 163     case Bytecodes::_fast_fputfield:  // fall through
 164     case Bytecodes::_fast_iputfield:  // fall through
 165     case Bytecodes::_fast_lputfield:  // fall through
 166     case Bytecodes::_fast_sputfield: {
 167       // We skip bytecode quickening for putfield instructions when
 168       // the put_code written to the constant pool cache is zero.
 169       // This is required so that every execution of this instruction
 170       // calls out to InterpreterRuntime::resolve_get_put to do
 171       // additional, required work.
 172       assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
 173       assert(load_bc_into_bc_reg, "we use bc_reg as temp");
 174       __ load_field_entry(temp_reg, bc_reg);
 175       if (byte_no == f1_byte) {
 176         __ la(temp_reg, Address(temp_reg, in_bytes(ResolvedFieldEntry::get_code_offset())));
 177       } else {
 178         __ la(temp_reg, Address(temp_reg, in_bytes(ResolvedFieldEntry::put_code_offset())));
 179       }
 180       // Load-acquire the bytecode to match store-release in ResolvedFieldEntry::fill_in()
 181       __ lbu(temp_reg, Address(temp_reg, 0));
 182       __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
 183       __ mv(bc_reg, bc);
 184       __ beqz(temp_reg, L_patch_done);
 185       break;
 186     }
 187     default:
 188       assert(byte_no == -1, "sanity");
 189       // the pair bytecodes have already done the load.
 190       if (load_bc_into_bc_reg) {
 191         __ mv(bc_reg, bc);
 192       }
 193   }
 194 
 195   if (JvmtiExport::can_post_breakpoint()) {
 196     Label L_fast_patch;
 197     // if a breakpoint is present we can't rewrite the stream directly
 198     __ load_unsigned_byte(temp_reg, at_bcp(0));
 199     __ addi(temp_reg, temp_reg, -Bytecodes::_breakpoint); // temp_reg is temporary register.
 200     __ bnez(temp_reg, L_fast_patch);
 201     // Let breakpoint table handling rewrite to quicker bytecode
 202     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::set_original_bytecode_at), xmethod, xbcp, bc_reg);
 203     __ j(L_patch_done);
 204     __ bind(L_fast_patch);
 205   }
 206 
 207 #ifdef ASSERT
 208   Label L_okay;
 209   __ load_unsigned_byte(temp_reg, at_bcp(0));
 210   __ beq(temp_reg, bc_reg, L_okay);
 211   __ addi(temp_reg, temp_reg, -(int) Bytecodes::java_code(bc));
 212   __ beqz(temp_reg, L_okay);
 213   __ stop("patching the wrong bytecode");
 214   __ bind(L_okay);
 215 #endif
 216 
 217   // patch bytecode
 218   __ sb(bc_reg, at_bcp(0));
 219   __ bind(L_patch_done);
 220 }
 221 
 222 // Individual instructions
 223 
 224 void TemplateTable::nop() {
 225   transition(vtos, vtos);
 226   // nothing to do
 227 }
 228 
 229 void TemplateTable::shouldnotreachhere() {
 230   transition(vtos, vtos);
 231   __ stop("should not reach here bytecode");
 232 }
 233 
 234 void TemplateTable::aconst_null() {
 235   transition(vtos, atos);
 236   __ mv(x10, zr);
 237 }
 238 
 239 void TemplateTable::iconst(int value) {
 240   transition(vtos, itos);
 241   __ mv(x10, value);
 242 }
 243 
 244 void TemplateTable::lconst(int value) {
 245   transition(vtos, ltos);
 246   __ mv(x10, value);
 247 }
 248 
 249 void TemplateTable::fconst(int value) {
 250   transition(vtos, ftos);
 251   static float fBuf[2] = {1.0, 2.0};
 252   __ mv(t0, (intptr_t)fBuf);
 253   switch (value) {
 254     case 0:
 255       __ fmv_w_x(f10, zr);
 256       break;
 257     case 1:
 258       __ flw(f10, Address(t0, 0));
 259       break;
 260     case 2:
 261       __ flw(f10, Address(t0, sizeof(float)));
 262       break;
 263     default:
 264       ShouldNotReachHere();
 265   }
 266 }
 267 
 268 void TemplateTable::dconst(int value) {
 269   transition(vtos, dtos);
 270   static double dBuf[2] = {1.0, 2.0};
 271   __ mv(t0, (intptr_t)dBuf);
 272   switch (value) {
 273     case 0:
 274       __ fmv_d_x(f10, zr);
 275       break;
 276     case 1:
 277       __ fld(f10, Address(t0, 0));
 278       break;
 279     case 2:
 280       __ fld(f10, Address(t0, sizeof(double)));
 281       break;
 282     default:
 283       ShouldNotReachHere();
 284   }
 285 }
 286 
 287 void TemplateTable::bipush() {
 288   transition(vtos, itos);
 289   __ load_signed_byte(x10, at_bcp(1));
 290 }
 291 
 292 void TemplateTable::sipush() {
 293   transition(vtos, itos);
 294   if (AvoidUnalignedAccesses) {
 295     __ load_signed_byte(x10, at_bcp(1));
 296     __ load_unsigned_byte(t1, at_bcp(2));
 297     __ slli(x10, x10, 8);
 298     __ add(x10, x10, t1);
 299   } else {
 300     __ load_unsigned_short(x10, at_bcp(1));
 301     __ revb_h_h(x10, x10); // reverse bytes in half-word and sign-extend
 302   }
 303 }
 304 
 305 void TemplateTable::ldc(LdcType type) {
 306   transition(vtos, vtos);
 307   Label call_ldc, notFloat, notClass, notInt, Done;
 308 
 309   if (is_ldc_wide(type)) {
 310    __ get_unsigned_2_byte_index_at_bcp(x11, 1);
 311   } else {
 312    __ load_unsigned_byte(x11, at_bcp(1));
 313   }
 314   __ get_cpool_and_tags(x12, x10);
 315 
 316   const int base_offset = ConstantPool::header_size() * wordSize;
 317   const int tags_offset = Array<u1>::base_offset_in_bytes();
 318 
 319   // get type
 320   __ addi(x13, x11, tags_offset);
 321   __ add(x13, x10, x13);
 322   __ lbu(x13, Address(x13, 0));
 323   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
 324 
 325   // unresolved class - get the resolved class
 326   __ mv(t1, (u1)JVM_CONSTANT_UnresolvedClass);
 327   __ beq(x13, t1, call_ldc);
 328 
 329   // unresolved class in error state - call into runtime to throw the error
 330   // from the first resolution attempt
 331   __ mv(t1, (u1)JVM_CONSTANT_UnresolvedClassInError);
 332   __ beq(x13, t1, call_ldc);
 333 
 334   // resolved class - need to call vm to get java mirror of the class
 335   __ mv(t1, (u1)JVM_CONSTANT_Class);
 336   __ bne(x13, t1, notClass);
 337 
 338   __ bind(call_ldc);
 339   __ mv(c_rarg1, is_ldc_wide(type) ? 1 : 0);
 340   call_VM(x10, CAST_FROM_FN_PTR(address, InterpreterRuntime::ldc), c_rarg1);
 341   __ push_ptr(x10);
 342   __ verify_oop(x10);
 343   __ j(Done);
 344 
 345   __ bind(notClass);
 346   __ mv(t1, (u1)JVM_CONSTANT_Float);
 347   __ bne(x13, t1, notFloat);
 348 
 349   // ftos
 350   __ shadd(x11, x11, x12, x11, 3);
 351   __ flw(f10, Address(x11, base_offset));
 352   __ push_f(f10);
 353   __ j(Done);
 354 
 355   __ bind(notFloat);
 356 
 357   __ mv(t1, (u1)JVM_CONSTANT_Integer);
 358   __ bne(x13, t1, notInt);
 359 
 360   // itos
 361   __ shadd(x11, x11, x12, x11, 3);
 362   __ lw(x10, Address(x11, base_offset));
 363   __ push_i(x10);
 364   __ j(Done);
 365 
 366   __ bind(notInt);
 367   condy_helper(Done);
 368 
 369   __ bind(Done);
 370 }
 371 
 372 // Fast path for caching oop constants.
 373 void TemplateTable::fast_aldc(LdcType type) {
 374   transition(vtos, atos);
 375 
 376   const Register result = x10;
 377   const Register tmp = x11;
 378   const Register rarg = x12;
 379 
 380   const int index_size = is_ldc_wide(type) ? sizeof(u2) : sizeof(u1);
 381 
 382   Label resolved;
 383 
 384   // We are resolved if the resolved reference cache entry contains a
 385   // non-null object (String, MethodType, etc.)
 386   assert_different_registers(result, tmp);
 387   // register result is trashed by next load, let's use it as temporary register
 388   __ get_cache_index_at_bcp(tmp, result, 1, index_size);
 389   __ load_resolved_reference_at_index(result, tmp);
 390   __ bnez(result, resolved);
 391 
 392   const address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_ldc);
 393 
 394   // first time invocation - must resolve first
 395   __ mv(rarg, (int)bytecode());
 396   __ call_VM(result, entry, rarg);
 397 
 398   __ bind(resolved);
 399 
 400   { // Check for the null sentinel.
 401     // If we just called the VM, it already did the mapping for us,
 402     // but it's harmless to retry.
 403     Label notNull;
 404 
 405     // Stash null_sentinel address to get its value later
 406     int32_t offset = 0;
 407     __ mv(rarg, Universe::the_null_sentinel_addr(), offset);
 408     __ ld(tmp, Address(rarg, offset));
 409     __ resolve_oop_handle(tmp, x15, t1);
 410     __ bne(result, tmp, notNull);
 411     __ mv(result, zr);  // null object reference
 412     __ bind(notNull);
 413   }
 414 
 415   if (VerifyOops) {
 416     // Safe to call with 0 result
 417     __ verify_oop(result);
 418   }
 419 }
 420 
 421 void TemplateTable::ldc2_w() {
 422     transition(vtos, vtos);
 423     Label notDouble, notLong, Done;
 424     __ get_unsigned_2_byte_index_at_bcp(x10, 1);
 425 
 426     __ get_cpool_and_tags(x11, x12);
 427     const int base_offset = ConstantPool::header_size() * wordSize;
 428     const int tags_offset = Array<u1>::base_offset_in_bytes();
 429 
 430     // get type
 431     __ add(x12, x12, x10);
 432     __ load_unsigned_byte(x12, Address(x12, tags_offset));
 433     __ mv(t1, JVM_CONSTANT_Double);
 434     __ bne(x12, t1, notDouble);
 435 
 436     // dtos
 437     __ shadd(x12, x10, x11, x12, 3);
 438     __ fld(f10, Address(x12, base_offset));
 439     __ push_d(f10);
 440     __ j(Done);
 441 
 442     __ bind(notDouble);
 443     __ mv(t1, (int)JVM_CONSTANT_Long);
 444     __ bne(x12, t1, notLong);
 445 
 446     // ltos
 447     __ shadd(x10, x10, x11, x10, 3);
 448     __ ld(x10, Address(x10, base_offset));
 449     __ push_l(x10);
 450     __ j(Done);
 451 
 452     __ bind(notLong);
 453     condy_helper(Done);
 454     __ bind(Done);
 455 }
 456 
 457 void TemplateTable::condy_helper(Label& Done) {
 458   const Register obj = x10;
 459   const Register rarg = x11;
 460   const Register flags = x12;
 461   const Register off = x13;
 462 
 463   const address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_ldc);
 464 
 465   __ mv(rarg, (int) bytecode());
 466   __ call_VM(obj, entry, rarg);
 467 
 468   __ get_vm_result_2(flags, xthread);
 469 
 470   // VMr = obj = base address to find primitive value to push
 471   // VMr2 = flags = (tos, off) using format of CPCE::_flags
 472   __ mv(off, flags);
 473   __ mv(t0, ConstantPoolCache::field_index_mask);
 474   __ andrw(off, off, t0);
 475 
 476   __ add(off, obj, off);
 477   const Address field(off, 0); // base + R---->base + offset
 478 
 479   __ slli(flags, flags, XLEN - (ConstantPoolCache::tos_state_shift + ConstantPoolCache::tos_state_bits));
 480   __ srli(flags, flags, XLEN - ConstantPoolCache::tos_state_bits); // (1 << 5) - 4 --> 28~31==> flags:0~3
 481 
 482   switch (bytecode()) {
 483     case Bytecodes::_ldc:   // fall through
 484     case Bytecodes::_ldc_w: {
 485       // tos in (itos, ftos, stos, btos, ctos, ztos)
 486       Label notInt, notFloat, notShort, notByte, notChar, notBool;
 487       __ mv(t1, itos);
 488       __ bne(flags, t1, notInt);
 489       // itos
 490       __ lw(x10, field);
 491       __ push(itos);
 492       __ j(Done);
 493 
 494       __ bind(notInt);
 495       __ mv(t1, ftos);
 496       __ bne(flags, t1, notFloat);
 497       // ftos
 498       __ load_float(field);
 499       __ push(ftos);
 500       __ j(Done);
 501 
 502       __ bind(notFloat);
 503       __ mv(t1, stos);
 504       __ bne(flags, t1, notShort);
 505       // stos
 506       __ load_signed_short(x10, field);
 507       __ push(stos);
 508       __ j(Done);
 509 
 510       __ bind(notShort);
 511       __ mv(t1, btos);
 512       __ bne(flags, t1, notByte);
 513       // btos
 514       __ load_signed_byte(x10, field);
 515       __ push(btos);
 516       __ j(Done);
 517 
 518       __ bind(notByte);
 519       __ mv(t1, ctos);
 520       __ bne(flags, t1, notChar);
 521       // ctos
 522       __ load_unsigned_short(x10, field);
 523       __ push(ctos);
 524       __ j(Done);
 525 
 526       __ bind(notChar);
 527       __ mv(t1, ztos);
 528       __ bne(flags, t1, notBool);
 529       // ztos
 530       __ load_signed_byte(x10, field);
 531       __ push(ztos);
 532       __ j(Done);
 533 
 534       __ bind(notBool);
 535       break;
 536     }
 537 
 538     case Bytecodes::_ldc2_w: {
 539       Label notLong, notDouble;
 540       __ mv(t1, ltos);
 541       __ bne(flags, t1, notLong);
 542       // ltos
 543       __ ld(x10, field);
 544       __ push(ltos);
 545       __ j(Done);
 546 
 547       __ bind(notLong);
 548       __ mv(t1, dtos);
 549       __ bne(flags, t1, notDouble);
 550       // dtos
 551       __ load_double(field);
 552       __ push(dtos);
 553       __ j(Done);
 554 
 555       __ bind(notDouble);
 556       break;
 557     }
 558 
 559     default:
 560       ShouldNotReachHere();
 561   }
 562 
 563   __ stop("bad ldc/condy");
 564 }
 565 
 566 void TemplateTable::locals_index(Register reg, int offset) {
 567   __ lbu(reg, at_bcp(offset));
 568   __ neg(reg, reg);
 569 }
 570 
 571 void TemplateTable::iload() {
 572   iload_internal();
 573 }
 574 
 575 void TemplateTable::nofast_iload() {
 576   iload_internal(may_not_rewrite);
 577 }
 578 
 579 void TemplateTable::iload_internal(RewriteControl rc) {
 580   transition(vtos, itos);
 581   if (RewriteFrequentPairs && rc == may_rewrite) {
 582     Label rewrite, done;
 583     const Register bc = x14;
 584 
 585     // get next bytecode
 586     __ load_unsigned_byte(x11, at_bcp(Bytecodes::length_for(Bytecodes::_iload)));
 587 
 588     // if _iload, wait to rewrite to iload2.  We only want to rewrite the
 589     // last two iloads in a pair.  Comparing against fast_iload means that
 590     // the next bytecode is neither an iload or a caload, and therefore
 591     // an iload pair.
 592     __ mv(t1, Bytecodes::_iload);
 593     __ beq(x11, t1, done);
 594 
 595     // if _fast_iload rewrite to _fast_iload2
 596     __ mv(t1, Bytecodes::_fast_iload);
 597     __ mv(bc, Bytecodes::_fast_iload2);
 598     __ beq(x11, t1, rewrite);
 599 
 600     // if _caload rewrite to _fast_icaload
 601     __ mv(t1, Bytecodes::_caload);
 602     __ mv(bc, Bytecodes::_fast_icaload);
 603     __ beq(x11, t1, rewrite);
 604 
 605     // else rewrite to _fast_iload
 606     __ mv(bc, Bytecodes::_fast_iload);
 607 
 608     // rewrite
 609     // bc: new bytecode
 610     __ bind(rewrite);
 611     patch_bytecode(Bytecodes::_iload, bc, x11, false);
 612     __ bind(done);
 613 
 614   }
 615 
 616   // do iload, get the local value into tos
 617   locals_index(x11);
 618   __ lw(x10, iaddress(x11, x10, _masm));
 619 }
 620 
 621 void TemplateTable::fast_iload2() {
 622   transition(vtos, itos);
 623   locals_index(x11);
 624   __ lw(x10, iaddress(x11, x10, _masm));
 625   __ push(itos);
 626   locals_index(x11, 3);
 627   __ lw(x10, iaddress(x11, x10, _masm));
 628 }
 629 
 630 void TemplateTable::fast_iload() {
 631   transition(vtos, itos);
 632   locals_index(x11);
 633   __ lw(x10, iaddress(x11, x10, _masm));
 634 }
 635 
 636 void TemplateTable::lload() {
 637   transition(vtos, ltos);
 638   __ lbu(x11, at_bcp(1));
 639   __ slli(x11, x11, LogBytesPerWord);
 640   __ sub(x11, xlocals, x11);
 641   __ ld(x10, Address(x11, Interpreter::local_offset_in_bytes(1)));
 642 }
 643 
 644 void TemplateTable::fload() {
 645   transition(vtos, ftos);
 646   locals_index(x11);
 647   __ flw(f10, faddress(x11, t0, _masm));
 648 }
 649 
 650 void TemplateTable::dload() {
 651   transition(vtos, dtos);
 652   __ lbu(x11, at_bcp(1));
 653   __ slli(x11, x11, LogBytesPerWord);
 654   __ sub(x11, xlocals, x11);
 655   __ fld(f10, Address(x11, Interpreter::local_offset_in_bytes(1)));
 656 }
 657 
 658 void TemplateTable::aload() {
 659   transition(vtos, atos);
 660   locals_index(x11);
 661   __ ld(x10, iaddress(x11, x10, _masm));
 662 }
 663 
 664 void TemplateTable::locals_index_wide(Register reg) {
 665   __ lhu(reg, at_bcp(2));
 666   __ revb_h_h_u(reg, reg); // reverse bytes in half-word and zero-extend
 667   __ neg(reg, reg);
 668 }
 669 
 670 void TemplateTable::wide_iload() {
 671   transition(vtos, itos);
 672   locals_index_wide(x11);
 673   __ lw(x10, iaddress(x11, t0, _masm));
 674 }
 675 
 676 void TemplateTable::wide_lload() {
 677   transition(vtos, ltos);
 678   __ lhu(x11, at_bcp(2));
 679   __ revb_h_h_u(x11, x11); // reverse bytes in half-word and zero-extend
 680   __ slli(x11, x11, LogBytesPerWord);
 681   __ sub(x11, xlocals, x11);
 682   __ ld(x10, Address(x11, Interpreter::local_offset_in_bytes(1)));
 683 }
 684 
 685 void TemplateTable::wide_fload() {
 686   transition(vtos, ftos);
 687   locals_index_wide(x11);
 688   __ flw(f10, faddress(x11, t0, _masm));
 689 }
 690 
 691 void TemplateTable::wide_dload() {
 692   transition(vtos, dtos);
 693   __ lhu(x11, at_bcp(2));
 694   __ revb_h_h_u(x11, x11); // reverse bytes in half-word and zero-extend
 695   __ slli(x11, x11, LogBytesPerWord);
 696   __ sub(x11, xlocals, x11);
 697   __ fld(f10, Address(x11, Interpreter::local_offset_in_bytes(1)));
 698 }
 699 
 700 void TemplateTable::wide_aload() {
 701   transition(vtos, atos);
 702   locals_index_wide(x11);
 703   __ ld(x10, aaddress(x11, t0, _masm));
 704 }
 705 
 706 void TemplateTable::index_check(Register array, Register index) {
 707   // destroys x11, t0
 708   // sign extend index for use by indexed load
 709   // check index
 710   const Register length = t0;
 711   __ lwu(length, Address(array, arrayOopDesc::length_offset_in_bytes()));
 712   if (index != x11) {
 713     assert(x11 != array, "different registers");
 714     __ mv(x11, index);
 715   }
 716   Label ok;
 717   __ sign_extend(index, index, 32);
 718   __ bltu(index, length, ok);
 719   __ mv(x13, array);
 720   __ mv(t0, Interpreter::_throw_ArrayIndexOutOfBoundsException_entry);
 721   __ jr(t0);
 722   __ bind(ok);
 723 }
 724 
 725 void TemplateTable::iaload() {
 726   transition(itos, itos);
 727   __ mv(x11, x10);
 728   __ pop_ptr(x10);
 729   // x10: array
 730   // x11: index
 731   index_check(x10, x11); // leaves index in x11
 732   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_INT) >> 2);
 733   __ shadd(x10, x11, x10, t0, 2);
 734   __ access_load_at(T_INT, IN_HEAP | IS_ARRAY, x10, Address(x10), noreg, noreg);
 735   __ sign_extend(x10, x10, 32);
 736 }
 737 
 738 void TemplateTable::laload() {
 739   transition(itos, ltos);
 740   __ mv(x11, x10);
 741   __ pop_ptr(x10);
 742   // x10: array
 743   // x11: index
 744   index_check(x10, x11); // leaves index in x11
 745   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_LONG) >> 3);
 746   __ shadd(x10, x11, x10, t0, 3);
 747   __ access_load_at(T_LONG, IN_HEAP | IS_ARRAY, x10, Address(x10), noreg, noreg);
 748 }
 749 
 750 void TemplateTable::faload() {
 751   transition(itos, ftos);
 752   __ mv(x11, x10);
 753   __ pop_ptr(x10);
 754   // x10: array
 755   // x11: index
 756   index_check(x10, x11); // leaves index in x11
 757   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_FLOAT) >> 2);
 758   __ shadd(x10, x11, x10, t0, 2);
 759   __ access_load_at(T_FLOAT, IN_HEAP | IS_ARRAY, x10, Address(x10), noreg, noreg);
 760 }
 761 
 762 void TemplateTable::daload() {
 763   transition(itos, dtos);
 764   __ mv(x11, x10);
 765   __ pop_ptr(x10);
 766   // x10: array
 767   // x11: index
 768   index_check(x10, x11); // leaves index in x11
 769   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_DOUBLE) >> 3);
 770   __ shadd(x10, x11, x10, t0, 3);
 771   __ access_load_at(T_DOUBLE, IN_HEAP | IS_ARRAY, x10, Address(x10), noreg, noreg);
 772 }
 773 
 774 void TemplateTable::aaload() {
 775   transition(itos, atos);
 776   __ mv(x11, x10);
 777   __ pop_ptr(x10);
 778   // x10: array
 779   // x11: index
 780   index_check(x10, x11); // leaves index in x11
 781   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_OBJECT) >> LogBytesPerHeapOop);
 782   __ shadd(x10, x11, x10, t0, LogBytesPerHeapOop);
 783   do_oop_load(_masm, Address(x10), x10, IS_ARRAY);
 784 }
 785 
 786 void TemplateTable::baload() {
 787   transition(itos, itos);
 788   __ mv(x11, x10);
 789   __ pop_ptr(x10);
 790   // x10: array
 791   // x11: index
 792   index_check(x10, x11); // leaves index in x11
 793   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_BYTE) >> 0);
 794   __ shadd(x10, x11, x10, t0, 0);
 795   __ access_load_at(T_BYTE, IN_HEAP | IS_ARRAY, x10, Address(x10), noreg, noreg);
 796 }
 797 
 798 void TemplateTable::caload() {
 799   transition(itos, itos);
 800   __ mv(x11, x10);
 801   __ pop_ptr(x10);
 802   // x10: array
 803   // x11: index
 804   index_check(x10, x11); // leaves index in x11
 805   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_CHAR) >> 1);
 806   __ shadd(x10, x11, x10, t0, 1);
 807   __ access_load_at(T_CHAR, IN_HEAP | IS_ARRAY, x10, Address(x10), noreg, noreg);
 808 }
 809 
 810 // iload followed by caload frequent pair
 811 void TemplateTable::fast_icaload() {
 812   transition(vtos, itos);
 813   // load index out of locals
 814   locals_index(x12);
 815   __ lw(x11, iaddress(x12, x11, _masm));
 816   __ pop_ptr(x10);
 817 
 818   // x10: array
 819   // x11: index
 820   index_check(x10, x11); // leaves index in x11, kills t0
 821   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_CHAR) >> 1); // addi, max imm is 2^11
 822   __ shadd(x10, x11, x10, t0, 1);
 823   __ access_load_at(T_CHAR, IN_HEAP | IS_ARRAY, x10, Address(x10), noreg, noreg);
 824 }
 825 
 826 void TemplateTable::saload() {
 827   transition(itos, itos);
 828   __ mv(x11, x10);
 829   __ pop_ptr(x10);
 830   // x10: array
 831   // x11: index
 832   index_check(x10, x11); // leaves index in x11, kills t0
 833   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_SHORT) >> 1);
 834   __ shadd(x10, x11, x10, t0, 1);
 835   __ access_load_at(T_SHORT, IN_HEAP | IS_ARRAY, x10, Address(x10), noreg, noreg);
 836 }
 837 
 838 void TemplateTable::iload(int n) {
 839   transition(vtos, itos);
 840   __ lw(x10, iaddress(n));
 841 }
 842 
 843 void TemplateTable::lload(int n) {
 844   transition(vtos, ltos);
 845   __ ld(x10, laddress(n));
 846 }
 847 
 848 void TemplateTable::fload(int n) {
 849   transition(vtos, ftos);
 850   __ flw(f10, faddress(n));
 851 }
 852 
 853 void TemplateTable::dload(int n) {
 854   transition(vtos, dtos);
 855   __ fld(f10, daddress(n));
 856 }
 857 
 858 void TemplateTable::aload(int n) {
 859   transition(vtos, atos);
 860   __ ld(x10, iaddress(n));
 861 }
 862 
 863 void TemplateTable::aload_0() {
 864   aload_0_internal();
 865 }
 866 
 867 void TemplateTable::nofast_aload_0() {
 868   aload_0_internal(may_not_rewrite);
 869 }
 870 
 871 void TemplateTable::aload_0_internal(RewriteControl rc) {
 872   // According to bytecode histograms, the pairs:
 873   //
 874   // _aload_0, _fast_igetfield
 875   // _aload_0, _fast_agetfield
 876   // _aload_0, _fast_fgetfield
 877   //
 878   // occur frequently. If RewriteFrequentPairs is set, the (slow)
 879   // _aload_0 bytecode checks if the next bytecode is either
 880   // _fast_igetfield, _fast_agetfield or _fast_fgetfield and then
 881   // rewrites the current bytecode into a pair bytecode; otherwise it
 882   // rewrites the current bytecode into _fast_aload_0 that doesn't do
 883   // the pair check anymore.
 884   //
 885   // Note: If the next bytecode is _getfield, the rewrite must be
 886   //       delayed, otherwise we may miss an opportunity for a pair.
 887   //
 888   // Also rewrite frequent pairs
 889   //   aload_0, aload_1
 890   //   aload_0, iload_1
 891   // These bytecodes with a small amount of code are most profitable
 892   // to rewrite
 893   if (RewriteFrequentPairs && rc == may_rewrite) {
 894     Label rewrite, done;
 895     const Register bc = x14;
 896 
 897     // get next bytecode
 898     __ load_unsigned_byte(x11, at_bcp(Bytecodes::length_for(Bytecodes::_aload_0)));
 899 
 900     // if _getfield then wait with rewrite
 901     __ mv(t1, Bytecodes::Bytecodes::_getfield);
 902     __ beq(x11, t1, done);
 903 
 904     // if _igetfield then rewrite to _fast_iaccess_0
 905     assert(Bytecodes::java_code(Bytecodes::_fast_iaccess_0) == Bytecodes::_aload_0, "fix bytecode definition");
 906     __ mv(t1, Bytecodes::_fast_igetfield);
 907     __ mv(bc, Bytecodes::_fast_iaccess_0);
 908     __ beq(x11, t1, rewrite);
 909 
 910     // if _agetfield then rewrite to _fast_aaccess_0
 911     assert(Bytecodes::java_code(Bytecodes::_fast_aaccess_0) == Bytecodes::_aload_0, "fix bytecode definition");
 912     __ mv(t1, Bytecodes::_fast_agetfield);
 913     __ mv(bc, Bytecodes::_fast_aaccess_0);
 914     __ beq(x11, t1, rewrite);
 915 
 916     // if _fgetfield then rewrite to _fast_faccess_0
 917     assert(Bytecodes::java_code(Bytecodes::_fast_faccess_0) == Bytecodes::_aload_0, "fix bytecode definition");
 918     __ mv(t1, Bytecodes::_fast_fgetfield);
 919     __ mv(bc, Bytecodes::_fast_faccess_0);
 920     __ beq(x11, t1, rewrite);
 921 
 922     // else rewrite to _fast_aload0
 923     assert(Bytecodes::java_code(Bytecodes::_fast_aload_0) == Bytecodes::_aload_0, "fix bytecode definition");
 924     __ mv(bc, Bytecodes::Bytecodes::_fast_aload_0);
 925 
 926     // rewrite
 927     // bc: new bytecode
 928     __ bind(rewrite);
 929     patch_bytecode(Bytecodes::_aload_0, bc, x11, false);
 930 
 931     __ bind(done);
 932   }
 933 
 934   // Do actual aload_0 (must do this after patch_bytecode which might call VM and GC might change oop).
 935   aload(0);
 936 }
 937 
 938 void TemplateTable::istore() {
 939   transition(itos, vtos);
 940   locals_index(x11);
 941   __ sw(x10, iaddress(x11, t0, _masm));
 942 }
 943 
 944 void TemplateTable::lstore() {
 945   transition(ltos, vtos);
 946   locals_index(x11);
 947   __ sd(x10, laddress(x11, t0, _masm));
 948 }
 949 
 950 void TemplateTable::fstore() {
 951   transition(ftos, vtos);
 952   locals_index(x11);
 953   __ fsw(f10, iaddress(x11, t0, _masm));
 954 }
 955 
 956 void TemplateTable::dstore() {
 957   transition(dtos, vtos);
 958   locals_index(x11);
 959   __ fsd(f10, daddress(x11, t0, _masm));
 960 }
 961 
 962 void TemplateTable::astore() {
 963   transition(vtos, vtos);
 964   __ pop_ptr(x10);
 965   locals_index(x11);
 966   __ sd(x10, aaddress(x11, t0, _masm));
 967 }
 968 
 969 void TemplateTable::wide_istore() {
 970   transition(vtos, vtos);
 971   __ pop_i();
 972   locals_index_wide(x11);
 973   __ sw(x10, iaddress(x11, t0, _masm));
 974 }
 975 
 976 void TemplateTable::wide_lstore() {
 977   transition(vtos, vtos);
 978   __ pop_l();
 979   locals_index_wide(x11);
 980   __ sd(x10, laddress(x11, t0, _masm));
 981 }
 982 
 983 void TemplateTable::wide_fstore() {
 984   transition(vtos, vtos);
 985   __ pop_f();
 986   locals_index_wide(x11);
 987   __ fsw(f10, faddress(x11, t0, _masm));
 988 }
 989 
 990 void TemplateTable::wide_dstore() {
 991   transition(vtos, vtos);
 992   __ pop_d();
 993   locals_index_wide(x11);
 994   __ fsd(f10, daddress(x11, t0, _masm));
 995 }
 996 
 997 void TemplateTable::wide_astore() {
 998   transition(vtos, vtos);
 999   __ pop_ptr(x10);
1000   locals_index_wide(x11);
1001   __ sd(x10, aaddress(x11, t0, _masm));
1002 }
1003 
1004 void TemplateTable::iastore() {
1005   transition(itos, vtos);
1006   __ pop_i(x11);
1007   __ pop_ptr(x13);
1008   // x10: value
1009   // x11: index
1010   // x13: array
1011   index_check(x13, x11); // prefer index in x11
1012   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_INT) >> 2);
1013   __ shadd(t0, x11, x13, t0, 2);
1014   __ access_store_at(T_INT, IN_HEAP | IS_ARRAY, Address(t0, 0), x10, noreg, noreg, noreg);
1015 }
1016 
1017 void TemplateTable::lastore() {
1018   transition(ltos, vtos);
1019   __ pop_i(x11);
1020   __ pop_ptr(x13);
1021   // x10: value
1022   // x11: index
1023   // x13: array
1024   index_check(x13, x11); // prefer index in x11
1025   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_LONG) >> 3);
1026   __ shadd(t0, x11, x13, t0, 3);
1027   __ access_store_at(T_LONG, IN_HEAP | IS_ARRAY, Address(t0, 0), x10, noreg, noreg, noreg);
1028 }
1029 
1030 void TemplateTable::fastore() {
1031   transition(ftos, vtos);
1032   __ pop_i(x11);
1033   __ pop_ptr(x13);
1034   // f10: value
1035   // x11:  index
1036   // x13:  array
1037   index_check(x13, x11); // prefer index in x11
1038   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_FLOAT) >> 2);
1039   __ shadd(t0, x11, x13, t0, 2);
1040   __ access_store_at(T_FLOAT, IN_HEAP | IS_ARRAY, Address(t0, 0), noreg /* ftos */, noreg, noreg, noreg);
1041 }
1042 
1043 void TemplateTable::dastore() {
1044   transition(dtos, vtos);
1045   __ pop_i(x11);
1046   __ pop_ptr(x13);
1047   // f10: value
1048   // x11:  index
1049   // x13:  array
1050   index_check(x13, x11); // prefer index in x11
1051   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_DOUBLE) >> 3);
1052   __ shadd(t0, x11, x13, t0, 3);
1053   __ access_store_at(T_DOUBLE, IN_HEAP | IS_ARRAY, Address(t0, 0), noreg /* dtos */, noreg, noreg, noreg);
1054 }
1055 
1056 void TemplateTable::aastore() {
1057   Label is_null, ok_is_subtype, done;
1058   transition(vtos, vtos);
1059   // stack: ..., array, index, value
1060   __ ld(x10, at_tos());    // value
1061   __ ld(x12, at_tos_p1()); // index
1062   __ ld(x13, at_tos_p2()); // array
1063 
1064   index_check(x13, x12);     // kills x11
1065   __ add(x14, x12, arrayOopDesc::base_offset_in_bytes(T_OBJECT) >> LogBytesPerHeapOop);
1066   __ shadd(x14, x14, x13, x14, LogBytesPerHeapOop);
1067 
1068   Address element_address(x14, 0);
1069 
1070   // do array store check - check for null value first
1071   __ beqz(x10, is_null);
1072 
1073   // Move subklass into x11
1074   __ load_klass(x11, x10);
1075   // Move superklass into x10
1076   __ load_klass(x10, x13);
1077   __ ld(x10, Address(x10,
1078                      ObjArrayKlass::element_klass_offset()));
1079   // Compress array + index * oopSize + 12 into a single register.  Frees x12.
1080 
1081   // Generate subtype check.  Blows x12, x15
1082   // Superklass in x10.  Subklass in x11.
1083   __ gen_subtype_check(x11, ok_is_subtype);
1084 
1085   // Come here on failure
1086   // object is at TOS
1087   __ j(Interpreter::_throw_ArrayStoreException_entry);
1088 
1089   // Come here on success
1090   __ bind(ok_is_subtype);
1091 
1092   // Get the value we will store
1093   __ ld(x10, at_tos());
1094   // Now store using the appropriate barrier
1095   do_oop_store(_masm, element_address, x10, IS_ARRAY);
1096   __ j(done);
1097 
1098   // Have a null in x10, x13=array, x12=index.  Store null at ary[idx]
1099   __ bind(is_null);
1100   __ profile_null_seen(x12);
1101 
1102   // Store a null
1103   do_oop_store(_masm, element_address, noreg, IS_ARRAY);
1104 
1105   // Pop stack arguments
1106   __ bind(done);
1107   __ add(esp, esp, 3 * Interpreter::stackElementSize);
1108 }
1109 
1110 void TemplateTable::bastore() {
1111   transition(itos, vtos);
1112   __ pop_i(x11);
1113   __ pop_ptr(x13);
1114   // x10: value
1115   // x11: index
1116   // x13: array
1117   index_check(x13, x11); // prefer index in x11
1118 
1119   // Need to check whether array is boolean or byte
1120   // since both types share the bastore bytecode.
1121   __ load_klass(x12, x13);
1122   __ lwu(x12, Address(x12, Klass::layout_helper_offset()));
1123   Label L_skip;
1124   __ test_bit(t0, x12, exact_log2(Klass::layout_helper_boolean_diffbit()));
1125   __ beqz(t0, L_skip);
1126   __ andi(x10, x10, 1);  // if it is a T_BOOLEAN array, mask the stored value to 0/1
1127   __ bind(L_skip);
1128 
1129   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_BYTE) >> 0);
1130 
1131   __ add(x11, x13, x11);
1132   __ access_store_at(T_BYTE, IN_HEAP | IS_ARRAY, Address(x11, 0), x10, noreg, noreg, noreg);
1133 }
1134 
1135 void TemplateTable::castore() {
1136   transition(itos, vtos);
1137   __ pop_i(x11);
1138   __ pop_ptr(x13);
1139   // x10: value
1140   // x11: index
1141   // x13: array
1142   index_check(x13, x11); // prefer index in x11
1143   __ add(x11, x11, arrayOopDesc::base_offset_in_bytes(T_CHAR) >> 1);
1144   __ shadd(t0, x11, x13, t0, 1);
1145   __ access_store_at(T_CHAR, IN_HEAP | IS_ARRAY, Address(t0, 0), x10, noreg, noreg, noreg);
1146 }
1147 
1148 void TemplateTable::sastore() {
1149   castore();
1150 }
1151 
1152 void TemplateTable::istore(int n) {
1153   transition(itos, vtos);
1154   __ sd(x10, iaddress(n));
1155 }
1156 
1157 void TemplateTable::lstore(int n) {
1158   transition(ltos, vtos);
1159   __ sd(x10, laddress(n));
1160 }
1161 
1162 void TemplateTable::fstore(int n) {
1163   transition(ftos, vtos);
1164   __ fsw(f10, faddress(n));
1165 }
1166 
1167 void TemplateTable::dstore(int n) {
1168   transition(dtos, vtos);
1169   __ fsd(f10, daddress(n));
1170 }
1171 
1172 void TemplateTable::astore(int n) {
1173   transition(vtos, vtos);
1174   __ pop_ptr(x10);
1175   __ sd(x10, iaddress(n));
1176 }
1177 
1178 void TemplateTable::pop() {
1179   transition(vtos, vtos);
1180   __ addi(esp, esp, Interpreter::stackElementSize);
1181 }
1182 
1183 void TemplateTable::pop2() {
1184   transition(vtos, vtos);
1185   __ addi(esp, esp, 2 * Interpreter::stackElementSize);
1186 }
1187 
1188 void TemplateTable::dup() {
1189   transition(vtos, vtos);
1190   __ ld(x10, Address(esp, 0));
1191   __ push_reg(x10);
1192   // stack: ..., a, a
1193 }
1194 
1195 void TemplateTable::dup_x1() {
1196   transition(vtos, vtos);
1197   // stack: ..., a, b
1198   __ ld(x10, at_tos());  // load b
1199   __ ld(x12, at_tos_p1());  // load a
1200   __ sd(x10, at_tos_p1());  // store b
1201   __ sd(x12, at_tos());  // store a
1202   __ push_reg(x10);                  // push b
1203   // stack: ..., b, a, b
1204 }
1205 
1206 void TemplateTable::dup_x2() {
1207   transition(vtos, vtos);
1208   // stack: ..., a, b, c
1209   __ ld(x10, at_tos());  // load c
1210   __ ld(x12, at_tos_p2());  // load a
1211   __ sd(x10, at_tos_p2());  // store c in a
1212   __ push_reg(x10);      // push c
1213   // stack: ..., c, b, c, c
1214   __ ld(x10, at_tos_p2());  // load b
1215   __ sd(x12, at_tos_p2());  // store a in b
1216   // stack: ..., c, a, c, c
1217   __ sd(x10, at_tos_p1());  // store b in c
1218   // stack: ..., c, a, b, c
1219 }
1220 
1221 void TemplateTable::dup2() {
1222   transition(vtos, vtos);
1223   // stack: ..., a, b
1224   __ ld(x10, at_tos_p1());  // load a
1225   __ push_reg(x10);                  // push a
1226   __ ld(x10, at_tos_p1());  // load b
1227   __ push_reg(x10);                  // push b
1228   // stack: ..., a, b, a, b
1229 }
1230 
1231 void TemplateTable::dup2_x1() {
1232   transition(vtos, vtos);
1233   // stack: ..., a, b, c
1234   __ ld(x12, at_tos());     // load c
1235   __ ld(x10, at_tos_p1());  // load b
1236   __ push_reg(x10);             // push b
1237   __ push_reg(x12);             // push c
1238   // stack: ..., a, b, c, b, c
1239   __ sd(x12, at_tos_p3());  // store c in b
1240   // stack: ..., a, c, c, b, c
1241   __ ld(x12, at_tos_p4());  // load a
1242   __ sd(x12, at_tos_p2());  // store a in 2nd c
1243   // stack: ..., a, c, a, b, c
1244   __ sd(x10, at_tos_p4());  // store b in a
1245   // stack: ..., b, c, a, b, c
1246 }
1247 
1248 void TemplateTable::dup2_x2() {
1249   transition(vtos, vtos);
1250   // stack: ..., a, b, c, d
1251   __ ld(x12, at_tos());     // load d
1252   __ ld(x10, at_tos_p1());  // load c
1253   __ push_reg(x10);             // push c
1254   __ push_reg(x12);             // push d
1255   // stack: ..., a, b, c, d, c, d
1256   __ ld(x10, at_tos_p4());  // load b
1257   __ sd(x10, at_tos_p2());  // store b in d
1258   __ sd(x12, at_tos_p4());  // store d in b
1259   // stack: ..., a, d, c, b, c, d
1260   __ ld(x12, at_tos_p5());  // load a
1261   __ ld(x10, at_tos_p3());  // load c
1262   __ sd(x12, at_tos_p3());  // store a in c
1263   __ sd(x10, at_tos_p5());  // store c in a
1264   // stack: ..., c, d, a, b, c, d
1265 }
1266 
1267 void TemplateTable::swap() {
1268   transition(vtos, vtos);
1269   // stack: ..., a, b
1270   __ ld(x12, at_tos_p1());  // load a
1271   __ ld(x10, at_tos());     // load b
1272   __ sd(x12, at_tos());     // store a in b
1273   __ sd(x10, at_tos_p1());  // store b in a
1274   // stack: ..., b, a
1275 }
1276 
1277 void TemplateTable::iop2(Operation op) {
1278   transition(itos, itos);
1279   // x10 <== x11 op x10
1280   __ pop_i(x11);
1281   switch (op) {
1282     case add  : __ addw(x10, x11, x10);  break;
1283     case sub  : __ subw(x10, x11, x10);  break;
1284     case mul  : __ mulw(x10, x11, x10);  break;
1285     case _and : __ andrw(x10, x11, x10); break;
1286     case _or  : __ orrw(x10, x11, x10);  break;
1287     case _xor : __ xorrw(x10, x11, x10); break;
1288     case shl  : __ sllw(x10, x11, x10);  break;
1289     case shr  : __ sraw(x10, x11, x10);  break;
1290     case ushr : __ srlw(x10, x11, x10);  break;
1291     default   : ShouldNotReachHere();
1292   }
1293 }
1294 
1295 void TemplateTable::lop2(Operation op) {
1296   transition(ltos, ltos);
1297   // x10 <== x11 op x10
1298   __ pop_l(x11);
1299   switch (op) {
1300     case add  : __ add(x10, x11, x10);  break;
1301     case sub  : __ sub(x10, x11, x10);  break;
1302     case mul  : __ mul(x10, x11, x10);  break;
1303     case _and : __ andr(x10, x11, x10); break;
1304     case _or  : __ orr(x10, x11, x10);  break;
1305     case _xor : __ xorr(x10, x11, x10); break;
1306     default   : ShouldNotReachHere();
1307   }
1308 }
1309 
1310 void TemplateTable::idiv() {
1311   transition(itos, itos);
1312   // explicitly check for div0
1313   Label no_div0;
1314   __ bnez(x10, no_div0);
1315   __ mv(t0, Interpreter::_throw_ArithmeticException_entry);
1316   __ jr(t0);
1317   __ bind(no_div0);
1318   __ pop_i(x11);
1319   // x10 <== x11 idiv x10
1320   __ corrected_idivl(x10, x11, x10, /* want_remainder */ false, /* is_signed */ true);
1321 }
1322 
1323 void TemplateTable::irem() {
1324   transition(itos, itos);
1325   // explicitly check for div0
1326   Label no_div0;
1327   __ bnez(x10, no_div0);
1328   __ mv(t0, Interpreter::_throw_ArithmeticException_entry);
1329   __ jr(t0);
1330   __ bind(no_div0);
1331   __ pop_i(x11);
1332   // x10 <== x11 irem x10
1333   __ corrected_idivl(x10, x11, x10, /* want_remainder */ true, /* is_signed */ true);
1334 }
1335 
1336 void TemplateTable::lmul() {
1337   transition(ltos, ltos);
1338   __ pop_l(x11);
1339   __ mul(x10, x10, x11);
1340 }
1341 
1342 void TemplateTable::ldiv() {
1343   transition(ltos, ltos);
1344   // explicitly check for div0
1345   Label no_div0;
1346   __ bnez(x10, no_div0);
1347   __ mv(t0, Interpreter::_throw_ArithmeticException_entry);
1348   __ jr(t0);
1349   __ bind(no_div0);
1350   __ pop_l(x11);
1351   // x10 <== x11 ldiv x10
1352   __ corrected_idivq(x10, x11, x10, /* want_remainder */ false, /* is_signed */ true);
1353 }
1354 
1355 void TemplateTable::lrem() {
1356   transition(ltos, ltos);
1357   // explicitly check for div0
1358   Label no_div0;
1359   __ bnez(x10, no_div0);
1360   __ mv(t0, Interpreter::_throw_ArithmeticException_entry);
1361   __ jr(t0);
1362   __ bind(no_div0);
1363   __ pop_l(x11);
1364   // x10 <== x11 lrem x10
1365   __ corrected_idivq(x10, x11, x10, /* want_remainder */ true, /* is_signed */ true);
1366 }
1367 
1368 void TemplateTable::lshl() {
1369   transition(itos, ltos);
1370   // shift count is in x10
1371   __ pop_l(x11);
1372   __ sll(x10, x11, x10);
1373 }
1374 
1375 void TemplateTable::lshr() {
1376   transition(itos, ltos);
1377   // shift count is in x10
1378   __ pop_l(x11);
1379   __ sra(x10, x11, x10);
1380 }
1381 
1382 void TemplateTable::lushr() {
1383   transition(itos, ltos);
1384   // shift count is in x10
1385   __ pop_l(x11);
1386   __ srl(x10, x11, x10);
1387 }
1388 
1389 void TemplateTable::fop2(Operation op) {
1390   transition(ftos, ftos);
1391   switch (op) {
1392     case add:
1393       __ pop_f(f11);
1394       __ fadd_s(f10, f11, f10);
1395       break;
1396     case sub:
1397       __ pop_f(f11);
1398       __ fsub_s(f10, f11, f10);
1399       break;
1400     case mul:
1401       __ pop_f(f11);
1402       __ fmul_s(f10, f11, f10);
1403       break;
1404     case div:
1405       __ pop_f(f11);
1406       __ fdiv_s(f10, f11, f10);
1407       break;
1408     case rem:
1409       __ fmv_s(f11, f10);
1410       __ pop_f(f10);
1411       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::frem));
1412       break;
1413     default:
1414       ShouldNotReachHere();
1415   }
1416 }
1417 
1418 void TemplateTable::dop2(Operation op) {
1419   transition(dtos, dtos);
1420   switch (op) {
1421     case add:
1422       __ pop_d(f11);
1423       __ fadd_d(f10, f11, f10);
1424       break;
1425     case sub:
1426       __ pop_d(f11);
1427       __ fsub_d(f10, f11, f10);
1428       break;
1429     case mul:
1430       __ pop_d(f11);
1431       __ fmul_d(f10, f11, f10);
1432       break;
1433     case div:
1434       __ pop_d(f11);
1435       __ fdiv_d(f10, f11, f10);
1436       break;
1437     case rem:
1438       __ fmv_d(f11, f10);
1439       __ pop_d(f10);
1440       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::drem));
1441       break;
1442     default:
1443       ShouldNotReachHere();
1444   }
1445 }
1446 
1447 void TemplateTable::ineg() {
1448   transition(itos, itos);
1449   __ negw(x10, x10);
1450 }
1451 
1452 void TemplateTable::lneg() {
1453   transition(ltos, ltos);
1454   __ neg(x10, x10);
1455 }
1456 
1457 void TemplateTable::fneg() {
1458   transition(ftos, ftos);
1459   __ fneg_s(f10, f10);
1460 }
1461 
1462 void TemplateTable::dneg() {
1463   transition(dtos, dtos);
1464   __ fneg_d(f10, f10);
1465 }
1466 
1467 void TemplateTable::iinc() {
1468   transition(vtos, vtos);
1469   __ load_signed_byte(x11, at_bcp(2)); // get constant
1470   locals_index(x12);
1471   __ ld(x10, iaddress(x12, x10, _masm));
1472   __ addw(x10, x10, x11);
1473   __ sd(x10, iaddress(x12, t0, _masm));
1474 }
1475 
1476 void TemplateTable::wide_iinc() {
1477   transition(vtos, vtos);
1478   __ lwu(x11, at_bcp(2)); // get constant and index
1479   __ revb_h_w_u(x11, x11); // reverse bytes in half-word (32bit) and zero-extend
1480   __ zero_extend(x12, x11, 16);
1481   __ neg(x12, x12);
1482   __ slli(x11, x11, 32);
1483   __ srai(x11, x11, 48);
1484   __ ld(x10, iaddress(x12, t0, _masm));
1485   __ addw(x10, x10, x11);
1486   __ sd(x10, iaddress(x12, t0, _masm));
1487 }
1488 
1489 void TemplateTable::convert() {
1490   // Checking
1491 #ifdef ASSERT
1492   {
1493     TosState tos_in  = ilgl;
1494     TosState tos_out = ilgl;
1495     switch (bytecode()) {
1496       case Bytecodes::_i2l: // fall through
1497       case Bytecodes::_i2f: // fall through
1498       case Bytecodes::_i2d: // fall through
1499       case Bytecodes::_i2b: // fall through
1500       case Bytecodes::_i2c: // fall through
1501       case Bytecodes::_i2s: tos_in = itos; break;
1502       case Bytecodes::_l2i: // fall through
1503       case Bytecodes::_l2f: // fall through
1504       case Bytecodes::_l2d: tos_in = ltos; break;
1505       case Bytecodes::_f2i: // fall through
1506       case Bytecodes::_f2l: // fall through
1507       case Bytecodes::_f2d: tos_in = ftos; break;
1508       case Bytecodes::_d2i: // fall through
1509       case Bytecodes::_d2l: // fall through
1510       case Bytecodes::_d2f: tos_in = dtos; break;
1511       default             : ShouldNotReachHere();
1512     }
1513     switch (bytecode()) {
1514       case Bytecodes::_l2i: // fall through
1515       case Bytecodes::_f2i: // fall through
1516       case Bytecodes::_d2i: // fall through
1517       case Bytecodes::_i2b: // fall through
1518       case Bytecodes::_i2c: // fall through
1519       case Bytecodes::_i2s: tos_out = itos; break;
1520       case Bytecodes::_i2l: // fall through
1521       case Bytecodes::_f2l: // fall through
1522       case Bytecodes::_d2l: tos_out = ltos; break;
1523       case Bytecodes::_i2f: // fall through
1524       case Bytecodes::_l2f: // fall through
1525       case Bytecodes::_d2f: tos_out = ftos; break;
1526       case Bytecodes::_i2d: // fall through
1527       case Bytecodes::_l2d: // fall through
1528       case Bytecodes::_f2d: tos_out = dtos; break;
1529       default             : ShouldNotReachHere();
1530     }
1531     transition(tos_in, tos_out);
1532   }
1533 #endif // ASSERT
1534 
1535   // Conversion
1536   switch (bytecode()) {
1537     case Bytecodes::_i2l:
1538       __ sign_extend(x10, x10, 32);
1539       break;
1540     case Bytecodes::_i2f:
1541       __ fcvt_s_w(f10, x10);
1542       break;
1543     case Bytecodes::_i2d:
1544       __ fcvt_d_w(f10, x10);
1545       break;
1546     case Bytecodes::_i2b:
1547       __ sign_extend(x10, x10, 8);
1548       break;
1549     case Bytecodes::_i2c:
1550       __ zero_extend(x10, x10, 16);
1551       break;
1552     case Bytecodes::_i2s:
1553       __ sign_extend(x10, x10, 16);
1554       break;
1555     case Bytecodes::_l2i:
1556       __ sign_extend(x10, x10, 32);
1557       break;
1558     case Bytecodes::_l2f:
1559       __ fcvt_s_l(f10, x10);
1560       break;
1561     case Bytecodes::_l2d:
1562       __ fcvt_d_l(f10, x10);
1563       break;
1564     case Bytecodes::_f2i:
1565       __ fcvt_w_s_safe(x10, f10);
1566       break;
1567     case Bytecodes::_f2l:
1568       __ fcvt_l_s_safe(x10, f10);
1569       break;
1570     case Bytecodes::_f2d:
1571       __ fcvt_d_s(f10, f10);
1572       break;
1573     case Bytecodes::_d2i:
1574       __ fcvt_w_d_safe(x10, f10);
1575       break;
1576     case Bytecodes::_d2l:
1577       __ fcvt_l_d_safe(x10, f10);
1578       break;
1579     case Bytecodes::_d2f:
1580       __ fcvt_s_d(f10, f10);
1581       break;
1582     default:
1583       ShouldNotReachHere();
1584   }
1585 }
1586 
1587 void TemplateTable::lcmp() {
1588   transition(ltos, itos);
1589   __ pop_l(x11);
1590   __ cmp_l2i(t0, x11, x10);
1591   __ mv(x10, t0);
1592 }
1593 
1594 void TemplateTable::float_cmp(bool is_float, int unordered_result) {
1595   // For instruction feq, flt and fle, the result is 0 if either operand is NaN
1596   if (is_float) {
1597     __ pop_f(f11);
1598     // if unordered_result < 0:
1599     //   we want -1 for unordered or less than, 0 for equal and 1 for
1600     //   greater than.
1601     // else:
1602     //   we want -1 for less than, 0 for equal and 1 for unordered or
1603     //   greater than.
1604     // f11 primary, f10 secondary
1605     __ float_compare(x10, f11, f10, unordered_result);
1606   } else {
1607     __ pop_d(f11);
1608     // if unordered_result < 0:
1609     //   we want -1 for unordered or less than, 0 for equal and 1 for
1610     //   greater than.
1611     // else:
1612     //   we want -1 for less than, 0 for equal and 1 for unordered or
1613     //   greater than.
1614     // f11 primary, f10 secondary
1615     __ double_compare(x10, f11, f10, unordered_result);
1616   }
1617 }
1618 
1619 void TemplateTable::branch(bool is_jsr, bool is_wide) {
1620   __ profile_taken_branch(x10, x11);
1621   const ByteSize be_offset = MethodCounters::backedge_counter_offset() +
1622                              InvocationCounter::counter_offset();
1623   const ByteSize inv_offset = MethodCounters::invocation_counter_offset() +
1624                               InvocationCounter::counter_offset();
1625 
1626   // load branch displacement
1627   if (!is_wide) {
1628     if (AvoidUnalignedAccesses) {
1629       __ lb(x12, at_bcp(1));
1630       __ lbu(t1, at_bcp(2));
1631       __ slli(x12, x12, 8);
1632       __ add(x12, x12, t1);
1633     } else {
1634       __ lhu(x12, at_bcp(1));
1635       __ revb_h_h(x12, x12); // reverse bytes in half-word and sign-extend
1636     }
1637   } else {
1638     __ lwu(x12, at_bcp(1));
1639     __ revb_w_w(x12, x12); // reverse bytes in word and sign-extend
1640   }
1641 
1642   // Handle all the JSR stuff here, then exit.
1643   // It's much shorter and cleaner than intermingling with the non-JSR
1644   // normal-branch stuff occurring below.
1645 
1646   if (is_jsr) {
1647     // compute return address as bci
1648     __ ld(t1, Address(xmethod, Method::const_offset()));
1649     __ add(t1, t1,
1650            in_bytes(ConstMethod::codes_offset()) - (is_wide ? 5 : 3));
1651     __ sub(x11, xbcp, t1);
1652     __ push_i(x11);
1653     // Adjust the bcp by the 16-bit displacement in x12
1654     __ add(xbcp, xbcp, x12);
1655     __ load_unsigned_byte(t0, Address(xbcp, 0));
1656     // load the next target bytecode into t0, it is the argument of dispatch_only
1657     __ dispatch_only(vtos, /*generate_poll*/true);
1658     return;
1659   }
1660 
1661   // Normal (non-jsr) branch handling
1662 
1663   // Adjust the bcp by the displacement in x12
1664   __ add(xbcp, xbcp, x12);
1665 
1666   assert(UseLoopCounter || !UseOnStackReplacement,
1667          "on-stack-replacement requires loop counters");
1668   Label backedge_counter_overflow;
1669   Label dispatch;
1670   if (UseLoopCounter) {
1671     // increment backedge counter for backward branches
1672     // x10: MDO
1673     // x11: MDO bumped taken-count
1674     // x12: target offset
1675     __ bgtz(x12, dispatch); // count only if backward branch
1676 
1677     // check if MethodCounters exists
1678     Label has_counters;
1679     __ ld(t0, Address(xmethod, Method::method_counters_offset()));
1680     __ bnez(t0, has_counters);
1681     __ push_reg(x10);
1682     __ push_reg(x11);
1683     __ push_reg(x12);
1684     __ call_VM(noreg, CAST_FROM_FN_PTR(address,
1685             InterpreterRuntime::build_method_counters), xmethod);
1686     __ pop_reg(x12);
1687     __ pop_reg(x11);
1688     __ pop_reg(x10);
1689     __ ld(t0, Address(xmethod, Method::method_counters_offset()));
1690     __ beqz(t0, dispatch); // No MethodCounters allocated, OutOfMemory
1691     __ bind(has_counters);
1692 
1693     Label no_mdo;
1694     int increment = InvocationCounter::count_increment;
1695     if (ProfileInterpreter) {
1696       // Are we profiling?
1697       __ ld(x11, Address(xmethod, in_bytes(Method::method_data_offset())));
1698       __ beqz(x11, no_mdo);
1699       // Increment the MDO backedge counter
1700       const Address mdo_backedge_counter(x11, in_bytes(MethodData::backedge_counter_offset()) +
1701                                          in_bytes(InvocationCounter::counter_offset()));
1702       const Address mask(x11, in_bytes(MethodData::backedge_mask_offset()));
1703       __ increment_mask_and_jump(mdo_backedge_counter, increment, mask,
1704                                  x10, t0, false,
1705                                  UseOnStackReplacement ? &backedge_counter_overflow : &dispatch);
1706       __ j(dispatch);
1707     }
1708     __ bind(no_mdo);
1709     // Increment backedge counter in MethodCounters*
1710     __ ld(t0, Address(xmethod, Method::method_counters_offset()));
1711     const Address mask(t0, in_bytes(MethodCounters::backedge_mask_offset()));
1712     __ increment_mask_and_jump(Address(t0, be_offset), increment, mask,
1713                                x10, t1, false,
1714                                UseOnStackReplacement ? &backedge_counter_overflow : &dispatch);
1715     __ bind(dispatch);
1716   }
1717 
1718   // Pre-load the next target bytecode into t0
1719   __ load_unsigned_byte(t0, Address(xbcp, 0));
1720 
1721   // continue with the bytecode @ target
1722   // t0: target bytecode
1723   // xbcp: target bcp
1724   __ dispatch_only(vtos, /*generate_poll*/true);
1725 
1726   if (UseLoopCounter && UseOnStackReplacement) {
1727     // invocation counter overflow
1728     __ bind(backedge_counter_overflow);
1729     __ neg(x12, x12);
1730     __ add(x12, x12, xbcp);     // branch xbcp
1731     // IcoResult frequency_counter_overflow([JavaThread*], address branch_bcp)
1732     __ call_VM(noreg,
1733                CAST_FROM_FN_PTR(address,
1734                                 InterpreterRuntime::frequency_counter_overflow),
1735                x12);
1736     __ load_unsigned_byte(x11, Address(xbcp, 0));  // restore target bytecode
1737 
1738     // x10: osr nmethod (osr ok) or null (osr not possible)
1739     // w11: target bytecode
1740     // x12: temporary
1741     __ beqz(x10, dispatch);     // test result -- no osr if null
1742     // nmethod may have been invalidated (VM may block upon call_VM return)
1743     __ lbu(x12, Address(x10, nmethod::state_offset()));
1744     if (nmethod::in_use != 0) {
1745       __ sub(x12, x12, nmethod::in_use);
1746     }
1747     __ bnez(x12, dispatch);
1748 
1749     // We have the address of an on stack replacement routine in x10
1750     // We need to prepare to execute the OSR method. First we must
1751     // migrate the locals and monitors off of the stack.
1752 
1753     __ mv(x9, x10);                             // save the nmethod
1754 
1755     call_VM(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin));
1756 
1757     // x10 is OSR buffer, move it to expected parameter location
1758     __ mv(j_rarg0, x10);
1759 
1760     // remove activation
1761     // get sender esp
1762     __ ld(esp,
1763         Address(fp, frame::interpreter_frame_sender_sp_offset * wordSize));
1764     // remove frame anchor
1765     __ leave();
1766     // Ensure compiled code always sees stack at proper alignment
1767     __ andi(sp, esp, -16);
1768 
1769     // and begin the OSR nmethod
1770     __ ld(t0, Address(x9, nmethod::osr_entry_point_offset()));
1771     __ jr(t0);
1772   }
1773 }
1774 
1775 void TemplateTable::if_0cmp(Condition cc) {
1776   transition(itos, vtos);
1777   // assume branch is more often taken than not (loops use backward branches)
1778   Label not_taken;
1779 
1780   __ sign_extend(x10, x10, 32);
1781   switch (cc) {
1782     case equal:
1783       __ bnez(x10, not_taken);
1784       break;
1785     case not_equal:
1786       __ beqz(x10, not_taken);
1787       break;
1788     case less:
1789       __ bgez(x10, not_taken);
1790       break;
1791     case less_equal:
1792       __ bgtz(x10, not_taken);
1793       break;
1794     case greater:
1795       __ blez(x10, not_taken);
1796       break;
1797     case greater_equal:
1798       __ bltz(x10, not_taken);
1799       break;
1800     default:
1801       break;
1802   }
1803 
1804   branch(false, false);
1805   __ bind(not_taken);
1806   __ profile_not_taken_branch(x10);
1807 }
1808 
1809 void TemplateTable::if_icmp(Condition cc) {
1810   transition(itos, vtos);
1811   // assume branch is more often taken than not (loops use backward branches)
1812   Label not_taken;
1813   __ pop_i(x11);
1814   __ sign_extend(x10, x10, 32);
1815   switch (cc) {
1816     case equal:
1817       __ bne(x11, x10, not_taken);
1818       break;
1819     case not_equal:
1820       __ beq(x11, x10, not_taken);
1821       break;
1822     case less:
1823       __ bge(x11, x10, not_taken);
1824       break;
1825     case less_equal:
1826       __ bgt(x11, x10, not_taken);
1827       break;
1828     case greater:
1829       __ ble(x11, x10, not_taken);
1830       break;
1831     case greater_equal:
1832       __ blt(x11, x10, not_taken);
1833       break;
1834     default:
1835       break;
1836   }
1837 
1838   branch(false, false);
1839   __ bind(not_taken);
1840   __ profile_not_taken_branch(x10);
1841 }
1842 
1843 void TemplateTable::if_nullcmp(Condition cc) {
1844   transition(atos, vtos);
1845   // assume branch is more often taken than not (loops use backward branches)
1846   Label not_taken;
1847   if (cc == equal) {
1848     __ bnez(x10, not_taken);
1849   } else {
1850     __ beqz(x10, not_taken);
1851   }
1852   branch(false, false);
1853   __ bind(not_taken);
1854   __ profile_not_taken_branch(x10);
1855 }
1856 
1857 void TemplateTable::if_acmp(Condition cc) {
1858   transition(atos, vtos);
1859   // assume branch is more often taken than not (loops use backward branches)
1860   Label not_taken;
1861   __ pop_ptr(x11);
1862 
1863   if (cc == equal) {
1864     __ bne(x11, x10, not_taken);
1865   } else if (cc == not_equal) {
1866     __ beq(x11, x10, not_taken);
1867   }
1868   branch(false, false);
1869   __ bind(not_taken);
1870   __ profile_not_taken_branch(x10);
1871 }
1872 
1873 void TemplateTable::ret() {
1874   transition(vtos, vtos);
1875   locals_index(x11);
1876   __ ld(x11, aaddress(x11, t1, _masm)); // get return bci, compute return bcp
1877   __ profile_ret(x11, x12);
1878   __ ld(xbcp, Address(xmethod, Method::const_offset()));
1879   __ add(xbcp, xbcp, x11);
1880   __ addi(xbcp, xbcp, in_bytes(ConstMethod::codes_offset()));
1881   __ dispatch_next(vtos, 0, /*generate_poll*/true);
1882 }
1883 
1884 void TemplateTable::wide_ret() {
1885   transition(vtos, vtos);
1886   locals_index_wide(x11);
1887   __ ld(x11, aaddress(x11, t0, _masm)); // get return bci, compute return bcp
1888   __ profile_ret(x11, x12);
1889   __ ld(xbcp, Address(xmethod, Method::const_offset()));
1890   __ add(xbcp, xbcp, x11);
1891   __ add(xbcp, xbcp, in_bytes(ConstMethod::codes_offset()));
1892   __ dispatch_next(vtos, 0, /*generate_poll*/true);
1893 }
1894 
1895 void TemplateTable::tableswitch() {
1896   Label default_case, continue_execution;
1897   transition(itos, vtos);
1898   // align xbcp
1899   __ la(x11, at_bcp(BytesPerInt));
1900   __ andi(x11, x11, -BytesPerInt);
1901   // load lo & hi
1902   __ lwu(x12, Address(x11, BytesPerInt));
1903   __ lwu(x13, Address(x11, 2 * BytesPerInt));
1904   __ revb_w_w(x12, x12); // reverse bytes in word (32bit) and sign-extend
1905   __ revb_w_w(x13, x13); // reverse bytes in word (32bit) and sign-extend
1906   // check against lo & hi
1907   __ blt(x10, x12, default_case);
1908   __ bgt(x10, x13, default_case);
1909   // lookup dispatch offset
1910   __ subw(x10, x10, x12);
1911   __ shadd(x13, x10, x11, t0, 2);
1912   __ lwu(x13, Address(x13, 3 * BytesPerInt));
1913   __ profile_switch_case(x10, x11, x12);
1914   // continue execution
1915   __ bind(continue_execution);
1916   __ revb_w_w(x13, x13); // reverse bytes in word (32bit) and sign-extend
1917   __ add(xbcp, xbcp, x13);
1918   __ load_unsigned_byte(t0, Address(xbcp));
1919   __ dispatch_only(vtos, /*generate_poll*/true);
1920   // handle default
1921   __ bind(default_case);
1922   __ profile_switch_default(x10);
1923   __ lwu(x13, Address(x11, 0));
1924   __ j(continue_execution);
1925 }
1926 
1927 void TemplateTable::lookupswitch() {
1928   transition(itos, itos);
1929   __ stop("lookupswitch bytecode should have been rewritten");
1930 }
1931 
1932 void TemplateTable::fast_linearswitch() {
1933   transition(itos, vtos);
1934   Label loop_entry, loop, found, continue_execution;
1935   // bswap x10 so we can avoid bswapping the table entries
1936   __ revb_w_w(x10, x10); // reverse bytes in word (32bit) and sign-extend
1937   // align xbcp
1938   __ la(x9, at_bcp(BytesPerInt)); // btw: should be able to get rid of
1939                                     // this instruction (change offsets
1940                                     // below)
1941   __ andi(x9, x9, -BytesPerInt);
1942   // set counter
1943   __ lwu(x11, Address(x9, BytesPerInt));
1944   __ revb_w(x11, x11);
1945   __ j(loop_entry);
1946   // table search
1947   __ bind(loop);
1948   __ shadd(t0, x11, x9, t0, 3);
1949   __ lw(t0, Address(t0, 2 * BytesPerInt));
1950   __ beq(x10, t0, found);
1951   __ bind(loop_entry);
1952   __ addi(x11, x11, -1);
1953   __ bgez(x11, loop);
1954   // default case
1955   __ profile_switch_default(x10);
1956   __ lwu(x13, Address(x9, 0));
1957   __ j(continue_execution);
1958   // entry found -> get offset
1959   __ bind(found);
1960   __ shadd(t0, x11, x9, t0, 3);
1961   __ lwu(x13, Address(t0, 3 * BytesPerInt));
1962   __ profile_switch_case(x11, x10, x9);
1963   // continue execution
1964   __ bind(continue_execution);
1965   __ revb_w_w(x13, x13); // reverse bytes in word (32bit) and sign-extend
1966   __ add(xbcp, xbcp, x13);
1967   __ lbu(t0, Address(xbcp, 0));
1968   __ dispatch_only(vtos, /*generate_poll*/true);
1969 }
1970 
1971 void TemplateTable::fast_binaryswitch() {
1972   transition(itos, vtos);
1973   // Implementation using the following core algorithm:
1974   //
1975   // int binary_search(int key, LookupswitchPair* array, int n)
1976   //   binary_search start:
1977   //   #Binary search according to "Methodik des Programmierens" by
1978   //   # Edsger W. Dijkstra and W.H.J. Feijen, Addison Wesley Germany 1985.
1979   //   int i = 0;
1980   //   int j = n;
1981   //   while (i + 1 < j) do
1982   //     # invariant P: 0 <= i < j <= n and (a[i] <= key < a[j] or Q)
1983   //     # with      Q: for all i: 0 <= i < n: key < a[i]
1984   //     # where a stands for the array and assuming that the (inexisting)
1985   //     # element a[n] is infinitely big.
1986   //     int h = (i + j) >> 1
1987   //     # i < h < j
1988   //     if (key < array[h].fast_match())
1989   //     then [j = h]
1990   //     else [i = h]
1991   //   end
1992   //   # R: a[i] <= key < a[i+1] or Q
1993   //   # (i.e., if key is within array, i is the correct index)
1994   //   return i
1995   // binary_search end
1996 
1997 
1998   // Register allocation
1999   const Register key   = x10; // already set (tosca)
2000   const Register array = x11;
2001   const Register i     = x12;
2002   const Register j     = x13;
2003   const Register h     = x14;
2004   const Register temp  = x15;
2005 
2006   // Find array start
2007   __ la(array, at_bcp(3 * BytesPerInt));  // btw: should be able to
2008                                           // get rid of this
2009                                           // instruction (change
2010                                           // offsets below)
2011   __ andi(array, array, -BytesPerInt);
2012 
2013   // Initialize i & j
2014   __ mv(i, zr);                            // i = 0
2015   __ lwu(j, Address(array, -BytesPerInt)); // j = length(array)
2016 
2017   // Convert j into native byteordering
2018   __ revb_w(j, j);
2019 
2020   // And start
2021   Label entry;
2022   __ j(entry);
2023 
2024   // binary search loop
2025   {
2026     Label loop;
2027     __ bind(loop);
2028     __ addw(h, i, j);                           // h = i + j
2029     __ srliw(h, h, 1);                          // h = (i + j) >> 1
2030     // if [key < array[h].fast_match()]
2031     // then [j = h]
2032     // else [i = h]
2033     // Convert array[h].match to native byte-ordering before compare
2034     __ shadd(temp, h, array, temp, 3);
2035     __ lwu(temp, Address(temp, 0));
2036     __ revb_w_w(temp, temp); // reverse bytes in word (32bit) and sign-extend
2037 
2038     Label L_done, L_greater;
2039     __ bge(key, temp, L_greater);
2040     // if [key < array[h].fast_match()] then j = h
2041     __ mv(j, h);
2042     __ j(L_done);
2043     __ bind(L_greater);
2044     // if [key >= array[h].fast_match()] then i = h
2045     __ mv(i, h);
2046     __ bind(L_done);
2047 
2048     // while [i + 1 < j]
2049     __ bind(entry);
2050     __ addiw(h, i, 1);         // i + 1
2051     __ blt(h, j, loop);        // i + 1 < j
2052   }
2053 
2054   // end of binary search, result index is i (must check again!)
2055   Label default_case;
2056   // Convert array[i].match to native byte-ordering before compare
2057   __ shadd(temp, i, array, temp, 3);
2058   __ lwu(temp, Address(temp, 0));
2059   __ revb_w_w(temp, temp); // reverse bytes in word (32bit) and sign-extend
2060   __ bne(key, temp, default_case);
2061 
2062   // entry found -> j = offset
2063   __ shadd(temp, i, array, temp, 3);
2064   __ lwu(j, Address(temp, BytesPerInt));
2065   __ profile_switch_case(i, key, array);
2066   __ revb_w_w(j, j); // reverse bytes in word (32bit) and sign-extend
2067 
2068   __ add(temp, xbcp, j);
2069   __ load_unsigned_byte(t0, Address(temp, 0));
2070 
2071   __ add(xbcp, xbcp, j);
2072   __ la(xbcp, Address(xbcp, 0));
2073   __ dispatch_only(vtos, /*generate_poll*/true);
2074 
2075   // default case -> j = default offset
2076   __ bind(default_case);
2077   __ profile_switch_default(i);
2078   __ lwu(j, Address(array, -2 * BytesPerInt));
2079   __ revb_w_w(j, j); // reverse bytes in word (32bit) and sign-extend
2080 
2081   __ add(temp, xbcp, j);
2082   __ load_unsigned_byte(t0, Address(temp, 0));
2083 
2084   __ add(xbcp, xbcp, j);
2085   __ la(xbcp, Address(xbcp, 0));
2086   __ dispatch_only(vtos, /*generate_poll*/true);
2087 }
2088 
2089 void TemplateTable::_return(TosState state) {
2090   transition(state, state);
2091   assert(_desc->calls_vm(),
2092          "inconsistent calls_vm information"); // call in remove_activation
2093 
2094   if (_desc->bytecode() == Bytecodes::_return_register_finalizer) {
2095     assert(state == vtos, "only valid state");
2096 
2097     __ ld(c_rarg1, aaddress(0));
2098     __ load_klass(x13, c_rarg1);
2099     __ lbu(x13, Address(x13, Klass::misc_flags_offset()));
2100     Label skip_register_finalizer;
2101     __ test_bit(t0, x13, exact_log2(KlassFlags::_misc_has_finalizer));
2102     __ beqz(t0, skip_register_finalizer);
2103 
2104     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::register_finalizer), c_rarg1);
2105 
2106     __ bind(skip_register_finalizer);
2107   }
2108 
2109   // Issue a StoreStore barrier after all stores but before return
2110   // from any constructor for any class with a final field. We don't
2111   // know if this is a finalizer, so we always do so.
2112   if (_desc->bytecode() == Bytecodes::_return) {
2113     __ membar(MacroAssembler::StoreStore);
2114   }
2115 
2116   if (_desc->bytecode() != Bytecodes::_return_register_finalizer) {
2117     Label no_safepoint;
2118     __ ld(t0, Address(xthread, JavaThread::polling_word_offset()));
2119     __ test_bit(t0, t0, exact_log2(SafepointMechanism::poll_bit()));
2120     __ beqz(t0, no_safepoint);
2121     __ push(state);
2122     __ push_cont_fastpath(xthread);
2123     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint));
2124     __ pop_cont_fastpath(xthread);
2125     __ pop(state);
2126     __ bind(no_safepoint);
2127   }
2128 
2129   // Narrow result if state is itos but result type is smaller.
2130   // Need to narrow in the return bytecode rather than in generate_return_entry
2131   // since compiled code callers expect the result to already be narrowed.
2132   if (state == itos) {
2133     __ narrow(x10);
2134   }
2135 
2136   __ remove_activation(state);
2137   __ ret();
2138 }
2139 
2140 
2141 // ----------------------------------------------------------------------------
2142 // Volatile variables demand their effects be made known to all CPU's
2143 // in order.  Store buffers on most chips allow reads & writes to
2144 // reorder; the JMM's ReadAfterWrite.java test fails in -Xint mode
2145 // without some kind of memory barrier (i.e., it's not sufficient that
2146 // the interpreter does not reorder volatile references, the hardware
2147 // also must not reorder them).
2148 //
2149 // According to the new Java Memory Model (JMM):
2150 // (1) All volatiles are serialized wrt to each other.  ALSO reads &
2151 //     writes act as acquire & release, so:
2152 // (2) A read cannot let unrelated NON-volatile memory refs that
2153 //     happen after the read float up to before the read.  It's OK for
2154 //     non-volatile memory refs that happen before the volatile read to
2155 //     float down below it.
2156 // (3) Similar a volatile write cannot let unrelated NON-volatile
2157 //     memory refs that happen BEFORE the write float down to after the
2158 //     write.  It's OK for non-volatile memory refs that happen after the
2159 //     volatile write to float up before it.
2160 //
2161 // We only put in barriers around volatile refs (they are expensive),
2162 // not _between_ memory refs (that would require us to track the
2163 // flavor of the previous memory refs).  Requirements (2) and (3)
2164 // require some barriers before volatile stores and after volatile
2165 // loads.  These nearly cover requirement (1) but miss the
2166 // volatile-store-volatile-load case.  This final case is placed after
2167 // volatile-stores although it could just as well go before
2168 // volatile-loads.
2169 
2170 void TemplateTable::resolve_cache_and_index_for_method(int byte_no,
2171                                                        Register Rcache,
2172                                                        Register index) {
2173   const Register temp = x9;
2174   assert_different_registers(Rcache, index, temp);
2175   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2176 
2177   Label resolved, clinit_barrier_slow;
2178 
2179   Bytecodes::Code code = bytecode();
2180   __ load_method_entry(Rcache, index);
2181   switch(byte_no) {
2182     case f1_byte:
2183       __ add(temp, Rcache, in_bytes(ResolvedMethodEntry::bytecode1_offset()));
2184       break;
2185     case f2_byte:
2186       __ add(temp, Rcache, in_bytes(ResolvedMethodEntry::bytecode2_offset()));
2187       break;
2188   }
2189   // Load-acquire the bytecode to match store-release in InterpreterRuntime
2190   __ lbu(temp, Address(temp, 0));
2191   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
2192 
2193   __ mv(t0, (int) code);
2194   __ beq(temp, t0, resolved);  // have we resolved this bytecode?
2195 
2196   // resolve first time through
2197   // Class initialization barrier slow path lands here as well.
2198   __ bind(clinit_barrier_slow);
2199 
2200   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2201   __ mv(temp, (int) code);
2202   __ call_VM(noreg, entry, temp);
2203 
2204   // Update registers with resolved info
2205   __ load_method_entry(Rcache, index);
2206   // n.b. unlike x86 Rcache is now rcpool plus the indexed offset
2207   // so all clients ofthis method must be modified accordingly
2208   __ bind(resolved);
2209 
2210   // Class initialization barrier for static methods
2211   if (VM_Version::supports_fast_class_init_checks() && bytecode() == Bytecodes::_invokestatic) {
2212     __ ld(temp, Address(Rcache, in_bytes(ResolvedMethodEntry::method_offset())));
2213     __ load_method_holder(temp, temp);
2214     __ clinit_barrier(temp, t0, nullptr, &clinit_barrier_slow);
2215   }
2216 }
2217 
2218 void TemplateTable::resolve_cache_and_index_for_field(int byte_no,
2219                                             Register Rcache,
2220                                             Register index) {
2221   const Register temp = x9;
2222   assert_different_registers(Rcache, index, temp);
2223 
2224   Label resolved;
2225 
2226   Bytecodes::Code code = bytecode();
2227   switch (code) {
2228   case Bytecodes::_nofast_getfield: code = Bytecodes::_getfield; break;
2229   case Bytecodes::_nofast_putfield: code = Bytecodes::_putfield; break;
2230   default: break;
2231   }
2232 
2233   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2234   __ load_field_entry(Rcache, index);
2235   if (byte_no == f1_byte) {
2236     __ la(temp, Address(Rcache, in_bytes(ResolvedFieldEntry::get_code_offset())));
2237   } else {
2238     __ la(temp, Address(Rcache, in_bytes(ResolvedFieldEntry::put_code_offset())));
2239   }
2240   // Load-acquire the bytecode to match store-release in ResolvedFieldEntry::fill_in()
2241   __ lbu(temp, Address(temp, 0));
2242   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
2243   __ mv(t0, (int) code);  // have we resolved this bytecode?
2244   __ beq(temp, t0, resolved);
2245 
2246   // resolve first time through
2247   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2248   __ mv(temp, (int) code);
2249   __ call_VM(noreg, entry, temp);
2250 
2251   // Update registers with resolved info
2252   __ load_field_entry(Rcache, index);
2253   __ bind(resolved);
2254 }
2255 
2256 void TemplateTable::load_resolved_field_entry(Register obj,
2257                                               Register cache,
2258                                               Register tos_state,
2259                                               Register offset,
2260                                               Register flags,
2261                                               bool is_static = false) {
2262   assert_different_registers(cache, tos_state, flags, offset);
2263 
2264   // Field offset
2265   __ load_sized_value(offset, Address(cache, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
2266 
2267   // Flags
2268   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedFieldEntry::flags_offset())));
2269 
2270   // TOS state
2271   if (tos_state != noreg) {
2272     __ load_unsigned_byte(tos_state, Address(cache, in_bytes(ResolvedFieldEntry::type_offset())));
2273   }
2274 
2275   // Klass overwrite register
2276   if (is_static) {
2277     __ ld(obj, Address(cache, ResolvedFieldEntry::field_holder_offset()));
2278     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
2279     __ ld(obj, Address(obj, mirror_offset));
2280     __ resolve_oop_handle(obj, x15, t1);
2281   }
2282 }
2283 
2284 void TemplateTable::load_resolved_method_entry_special_or_static(Register cache,
2285                                                                  Register method,
2286                                                                  Register flags) {
2287 
2288   // setup registers
2289   const Register index = flags;
2290   assert_different_registers(method, cache, flags);
2291 
2292   // determine constant pool cache field offsets
2293   resolve_cache_and_index_for_method(f1_byte, cache, index);
2294   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2295   __ ld(method, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2296 }
2297 
2298 void TemplateTable::load_resolved_method_entry_handle(Register cache,
2299                                                       Register method,
2300                                                       Register ref_index,
2301                                                       Register flags) {
2302   // setup registers
2303   const Register index = ref_index;
2304   assert_different_registers(method, flags);
2305   assert_different_registers(method, cache, index);
2306 
2307   // determine constant pool cache field offsets
2308   resolve_cache_and_index_for_method(f1_byte, cache, index);
2309   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2310 
2311   // maybe push appendix to arguments (just before return address)
2312   Label L_no_push;
2313   __ test_bit(t0, flags, ResolvedMethodEntry::has_appendix_shift);
2314   __ beqz(t0, L_no_push);
2315   // invokehandle uses an index into the resolved references array
2316   __ load_unsigned_short(ref_index, Address(cache, in_bytes(ResolvedMethodEntry::resolved_references_index_offset())));
2317   // Push the appendix as a trailing parameter.
2318   // This must be done before we get the receiver,
2319   // since the parameter_size includes it.
2320   Register appendix = method;
2321   __ load_resolved_reference_at_index(appendix, ref_index);
2322   __ push_reg(appendix); // push appendix (MethodType, CallSite, etc.)
2323   __ bind(L_no_push);
2324 
2325   __ ld(method, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2326 }
2327 
2328 void TemplateTable::load_resolved_method_entry_interface(Register cache,
2329                                                          Register klass,
2330                                                          Register method_or_table_index,
2331                                                          Register flags) {
2332   // setup registers
2333   const Register index = method_or_table_index;
2334   assert_different_registers(method_or_table_index, cache, flags);
2335 
2336   // determine constant pool cache field offsets
2337   resolve_cache_and_index_for_method(f1_byte, cache, index);
2338   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2339 
2340   // Invokeinterface can behave in different ways:
2341   // If calling a method from java.lang.Object, the forced virtual flag is true so the invocation will
2342   // behave like an invokevirtual call. The state of the virtual final flag will determine whether a method or
2343   // vtable index is placed in the register.
2344   // Otherwise, the registers will be populated with the klass and method.
2345 
2346   Label NotVirtual; Label NotVFinal; Label Done;
2347   __ test_bit(t0, flags, ResolvedMethodEntry::is_forced_virtual_shift);
2348   __ beqz(t0, NotVirtual);
2349   __ test_bit(t0, flags, ResolvedMethodEntry::is_vfinal_shift);
2350   __ beqz(t0, NotVFinal);
2351   __ ld(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2352   __ j(Done);
2353 
2354   __ bind(NotVFinal);
2355   __ load_unsigned_short(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::table_index_offset())));
2356   __ j(Done);
2357 
2358   __ bind(NotVirtual);
2359   __ ld(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2360   __ ld(klass, Address(cache, in_bytes(ResolvedMethodEntry::klass_offset())));
2361   __ bind(Done);
2362 }
2363 
2364 void TemplateTable::load_resolved_method_entry_virtual(Register cache,
2365                                                        Register method_or_table_index,
2366                                                        Register flags) {
2367   // setup registers
2368   const Register index = flags;
2369   assert_different_registers(method_or_table_index, cache, flags);
2370 
2371   // determine constant pool cache field offsets
2372   resolve_cache_and_index_for_method(f2_byte, cache, index);
2373   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2374 
2375   // method_or_table_index can either be an itable index or a method depending on the virtual final flag
2376   Label NotVFinal; Label Done;
2377   __ test_bit(t0, flags, ResolvedMethodEntry::is_vfinal_shift);
2378   __ beqz(t0, NotVFinal);
2379   __ ld(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2380   __ j(Done);
2381 
2382   __ bind(NotVFinal);
2383   __ load_unsigned_short(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::table_index_offset())));
2384   __ bind(Done);
2385 }
2386 
2387 // The xmethod register is input and overwritten to be the adapter method for the
2388 // indy call. Return address (ra) is set to the return address for the adapter and
2389 // an appendix may be pushed to the stack. Registers x10-x13 are clobbered.
2390 void TemplateTable::load_invokedynamic_entry(Register method) {
2391   // setup registers
2392   const Register appendix = x10;
2393   const Register cache = x12;
2394   const Register index = x13;
2395   assert_different_registers(method, appendix, cache, index, xcpool);
2396 
2397   __ save_bcp();
2398 
2399   Label resolved;
2400 
2401   __ load_resolved_indy_entry(cache, index);
2402   __ ld(method, Address(cache, in_bytes(ResolvedIndyEntry::method_offset())));
2403   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
2404 
2405   // Compare the method to zero
2406   __ bnez(method, resolved);
2407 
2408   Bytecodes::Code code = bytecode();
2409 
2410   // Call to the interpreter runtime to resolve invokedynamic
2411   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2412   __ mv(method, code); // this is essentially Bytecodes::_invokedynamic
2413   __ call_VM(noreg, entry, method);
2414   // Update registers with resolved info
2415   __ load_resolved_indy_entry(cache, index);
2416   __ ld(method, Address(cache, in_bytes(ResolvedIndyEntry::method_offset())));
2417   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
2418 
2419 #ifdef ASSERT
2420   __ bnez(method, resolved);
2421   __ stop("Should be resolved by now");
2422 #endif // ASSERT
2423   __ bind(resolved);
2424 
2425   Label L_no_push;
2426   // Check if there is an appendix
2427   __ load_unsigned_byte(index, Address(cache, in_bytes(ResolvedIndyEntry::flags_offset())));
2428   __ test_bit(t0, index, ResolvedIndyEntry::has_appendix_shift);
2429   __ beqz(t0, L_no_push);
2430 
2431   // Get appendix
2432   __ load_unsigned_short(index, Address(cache, in_bytes(ResolvedIndyEntry::resolved_references_index_offset())));
2433   // Push the appendix as a trailing parameter
2434   // since the parameter_size includes it.
2435   __ push_reg(method);
2436   __ mv(method, index);
2437   __ load_resolved_reference_at_index(appendix, method);
2438   __ verify_oop(appendix);
2439   __ pop_reg(method);
2440   __ push_reg(appendix);  // push appendix (MethodType, CallSite, etc.)
2441   __ bind(L_no_push);
2442 
2443   // compute return type
2444   __ load_unsigned_byte(index, Address(cache, in_bytes(ResolvedIndyEntry::result_type_offset())));
2445   // load return address
2446   // Return address is loaded into ra and not pushed to the stack like x86
2447   {
2448     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
2449     __ mv(t0, table_addr);
2450     __ shadd(t0, index, t0, index, 3);
2451     __ ld(ra, Address(t0, 0));
2452   }
2453 }
2454 
2455 // The registers cache and index expected to be set before call.
2456 // Correct values of the cache and index registers are preserved.
2457 void TemplateTable::jvmti_post_field_access(Register cache, Register index,
2458                                             bool is_static, bool has_tos) {
2459   // do the JVMTI work here to avoid disturbing the register state below
2460   // We use c_rarg registers here because we want to use the register used in
2461   // the call to the VM
2462   if (JvmtiExport::can_post_field_access()) {
2463     // Check to see if a field access watch has been set before we
2464     // take the time to call into the VM.
2465     Label L1;
2466     assert_different_registers(cache, index, x10);
2467     ExternalAddress target((address) JvmtiExport::get_field_access_count_addr());
2468     __ relocate(target.rspec(), [&] {
2469       int32_t offset;
2470       __ la(t0, target.target(), offset);
2471       __ lwu(x10, Address(t0, offset));
2472     });
2473 
2474     __ beqz(x10, L1);
2475 
2476     __ load_field_entry(c_rarg2, index);
2477 
2478     if (is_static) {
2479       __ mv(c_rarg1, zr); // null object reference
2480     } else {
2481       __ ld(c_rarg1, at_tos()); // get object pointer without popping it
2482       __ verify_oop(c_rarg1);
2483     }
2484     // c_rarg1: object pointer or null
2485     // c_rarg2: cache entry pointer
2486     __ call_VM(noreg, CAST_FROM_FN_PTR(address,
2487                                        InterpreterRuntime::post_field_access),
2488                                        c_rarg1, c_rarg2);
2489     __ load_field_entry(cache, index);
2490     __ bind(L1);
2491   }
2492 }
2493 
2494 void TemplateTable::pop_and_check_object(Register r) {
2495   __ pop_ptr(r);
2496   __ null_check(r);  // for field access must check obj.
2497   __ verify_oop(r);
2498 }
2499 
2500 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2501   const Register cache     = x14;
2502   const Register obj       = x14;
2503   const Register index     = x13;
2504   const Register tos_state = x13;
2505   const Register off       = x9;
2506   const Register flags     = x16;
2507   const Register bc        = x14; // uses same reg as obj, so don't mix them
2508 
2509   resolve_cache_and_index_for_field(byte_no, cache, index);
2510   jvmti_post_field_access(cache, index, is_static, false);
2511   load_resolved_field_entry(obj, cache, tos_state, off, flags, is_static);
2512 
2513   if (!is_static) {
2514     // obj is on the stack
2515     pop_and_check_object(obj);
2516   }
2517 
2518   __ add(off, obj, off);
2519   const Address field(off);
2520 
2521   Label Done, notByte, notBool, notInt, notShort, notChar,
2522               notLong, notFloat, notObj, notDouble;
2523 
2524   assert(btos == 0, "change code, btos != 0");
2525   __ bnez(tos_state, notByte);
2526 
2527   // Don't rewrite getstatic, only getfield
2528   if (is_static) {
2529     rc = may_not_rewrite;
2530   }
2531 
2532   // btos
2533   __ access_load_at(T_BYTE, IN_HEAP, x10, field, noreg, noreg);
2534   __ push(btos);
2535   // Rewrite bytecode to be faster
2536   if (rc == may_rewrite) {
2537     patch_bytecode(Bytecodes::_fast_bgetfield, bc, x11);
2538   }
2539   __ j(Done);
2540 
2541   __ bind(notByte);
2542   __ sub(t0, tos_state, (u1)ztos);
2543   __ bnez(t0, notBool);
2544 
2545   // ztos (same code as btos)
2546   __ access_load_at(T_BOOLEAN, IN_HEAP, x10, field, noreg, noreg);
2547   __ push(ztos);
2548   // Rewrite bytecode to be faster
2549   if (rc == may_rewrite) {
2550     // uses btos rewriting, no truncating to t/f bit is needed for getfield
2551     patch_bytecode(Bytecodes::_fast_bgetfield, bc, x11);
2552   }
2553   __ j(Done);
2554 
2555   __ bind(notBool);
2556   __ sub(t0, tos_state, (u1)atos);
2557   __ bnez(t0, notObj);
2558   // atos
2559   do_oop_load(_masm, field, x10, IN_HEAP);
2560   __ push(atos);
2561   if (rc == may_rewrite) {
2562     patch_bytecode(Bytecodes::_fast_agetfield, bc, x11);
2563   }
2564   __ j(Done);
2565 
2566   __ bind(notObj);
2567   __ sub(t0, tos_state, (u1)itos);
2568   __ bnez(t0, notInt);
2569   // itos
2570   __ access_load_at(T_INT, IN_HEAP, x10, field, noreg, noreg);
2571   __ sign_extend(x10, x10, 32);
2572   __ push(itos);
2573   // Rewrite bytecode to be faster
2574   if (rc == may_rewrite) {
2575     patch_bytecode(Bytecodes::_fast_igetfield, bc, x11);
2576   }
2577   __ j(Done);
2578 
2579   __ bind(notInt);
2580   __ sub(t0, tos_state, (u1)ctos);
2581   __ bnez(t0, notChar);
2582   // ctos
2583   __ access_load_at(T_CHAR, IN_HEAP, x10, field, noreg, noreg);
2584   __ push(ctos);
2585   // Rewrite bytecode to be faster
2586   if (rc == may_rewrite) {
2587     patch_bytecode(Bytecodes::_fast_cgetfield, bc, x11);
2588   }
2589   __ j(Done);
2590 
2591   __ bind(notChar);
2592   __ sub(t0, tos_state, (u1)stos);
2593   __ bnez(t0, notShort);
2594   // stos
2595   __ access_load_at(T_SHORT, IN_HEAP, x10, field, noreg, noreg);
2596   __ push(stos);
2597   // Rewrite bytecode to be faster
2598   if (rc == may_rewrite) {
2599     patch_bytecode(Bytecodes::_fast_sgetfield, bc, x11);
2600   }
2601   __ j(Done);
2602 
2603   __ bind(notShort);
2604   __ sub(t0, tos_state, (u1)ltos);
2605   __ bnez(t0, notLong);
2606   // ltos
2607   __ access_load_at(T_LONG, IN_HEAP, x10, field, noreg, noreg);
2608   __ push(ltos);
2609   // Rewrite bytecode to be faster
2610   if (rc == may_rewrite) {
2611     patch_bytecode(Bytecodes::_fast_lgetfield, bc, x11);
2612   }
2613   __ j(Done);
2614 
2615   __ bind(notLong);
2616   __ sub(t0, tos_state, (u1)ftos);
2617   __ bnez(t0, notFloat);
2618   // ftos
2619   __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
2620   __ push(ftos);
2621   // Rewrite bytecode to be faster
2622   if (rc == may_rewrite) {
2623     patch_bytecode(Bytecodes::_fast_fgetfield, bc, x11);
2624   }
2625   __ j(Done);
2626 
2627   __ bind(notFloat);
2628 #ifdef ASSERT
2629   __ sub(t0, tos_state, (u1)dtos);
2630   __ bnez(t0, notDouble);
2631 #endif
2632   // dtos
2633   __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
2634   __ push(dtos);
2635   // Rewrite bytecode to be faster
2636   if (rc == may_rewrite) {
2637     patch_bytecode(Bytecodes::_fast_dgetfield, bc, x11);
2638   }
2639 #ifdef ASSERT
2640   __ j(Done);
2641 
2642   __ bind(notDouble);
2643   __ stop("Bad state");
2644 #endif
2645 
2646   __ bind(Done);
2647 
2648   Label notVolatile;
2649   __ test_bit(t0, flags, ResolvedFieldEntry::is_volatile_shift);
2650   __ beqz(t0, notVolatile);
2651   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
2652   __ bind(notVolatile);
2653 }
2654 
2655 void TemplateTable::getfield(int byte_no) {
2656   getfield_or_static(byte_no, false);
2657 }
2658 
2659 void TemplateTable::nofast_getfield(int byte_no) {
2660   getfield_or_static(byte_no, false, may_not_rewrite);
2661 }
2662 
2663 void TemplateTable::getstatic(int byte_no)
2664 {
2665   getfield_or_static(byte_no, true);
2666 }
2667 
2668 // The registers cache and index expected to be set before call.
2669 // The function may destroy various registers, just not the cache and index registers.
2670 void TemplateTable::jvmti_post_field_mod(Register cache, Register index, bool is_static) {
2671   transition(vtos, vtos);
2672 
2673   if (JvmtiExport::can_post_field_modification()) {
2674     // Check to see if a field modification watch has been set before
2675     // we take the time to call into the VM.
2676     Label L1;
2677     assert_different_registers(cache, index, x10);
2678     ExternalAddress target((address)JvmtiExport::get_field_modification_count_addr());
2679     __ relocate(target.rspec(), [&] {
2680       int32_t offset;
2681       __ la(t0, target.target(), offset);
2682       __ lwu(x10, Address(t0, offset));
2683     });
2684     __ beqz(x10, L1);
2685 
2686     __ mv(c_rarg2, cache);
2687 
2688     if (is_static) {
2689       // Life is simple. Null out the object pointer.
2690       __ mv(c_rarg1, zr);
2691     } else {
2692       // Life is harder. The stack holds the value on top, followed by
2693       // the object. We don't know the size of the value, though; it
2694       // could be one or two words depending on its type. As a result,
2695       // we must find the type to determine where the object is.
2696       __ load_unsigned_byte(c_rarg3, Address(c_rarg2, in_bytes(ResolvedFieldEntry::type_offset())));
2697       Label nope2, done, ok;
2698       __ ld(c_rarg1, at_tos_p1());   // initially assume a one word jvalue
2699       __ sub(t0, c_rarg3, ltos);
2700       __ beqz(t0, ok);
2701       __ sub(t0, c_rarg3, dtos);
2702       __ bnez(t0, nope2);
2703       __ bind(ok);
2704       __ ld(c_rarg1, at_tos_p2());  // ltos (two word jvalue);
2705       __ bind(nope2);
2706     }
2707     // object (tos)
2708     __ mv(c_rarg3, esp);
2709     // c_rarg1: object pointer set up above (null if static)
2710     // c_rarg2: cache entry pointer
2711     // c_rarg3: jvalue object on  the stack
2712     __ call_VM(noreg,
2713                CAST_FROM_FN_PTR(address,
2714                                 InterpreterRuntime::post_field_modification),
2715                                 c_rarg1, c_rarg2, c_rarg3);
2716     __ load_field_entry(cache, index);
2717     __ bind(L1);
2718   }
2719 }
2720 
2721 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2722   transition(vtos, vtos);
2723 
2724   const Register cache     = x12;
2725   const Register index     = x13;
2726   const Register tos_state = x13;
2727   const Register obj       = x12;
2728   const Register off       = x9;
2729   const Register flags     = x10;
2730   const Register bc        = x14;
2731 
2732   resolve_cache_and_index_for_field(byte_no, cache, index);
2733   jvmti_post_field_mod(cache, index, is_static);
2734   load_resolved_field_entry(obj, cache, tos_state, off, flags, is_static);
2735 
2736   Label Done;
2737   __ mv(x15, flags);
2738 
2739   {
2740     Label notVolatile;
2741     __ test_bit(t0, x15, ResolvedFieldEntry::is_volatile_shift);
2742     __ beqz(t0, notVolatile);
2743     __ membar(MacroAssembler::StoreStore | MacroAssembler::LoadStore);
2744     __ bind(notVolatile);
2745   }
2746 
2747   Label notByte, notBool, notInt, notShort, notChar,
2748         notLong, notFloat, notObj, notDouble;
2749 
2750   assert(btos == 0, "change code, btos != 0");
2751   __ bnez(tos_state, notByte);
2752 
2753   // Don't rewrite putstatic, only putfield
2754   if (is_static) {
2755     rc = may_not_rewrite;
2756   }
2757 
2758   // btos
2759   {
2760     __ pop(btos);
2761     // field address
2762     if (!is_static) {
2763       pop_and_check_object(obj);
2764     }
2765     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2766     const Address field(off, 0); // off register as temparator register.
2767     __ access_store_at(T_BYTE, IN_HEAP, field, x10, noreg, noreg, noreg);
2768     if (rc == may_rewrite) {
2769       patch_bytecode(Bytecodes::_fast_bputfield, bc, x11, true, byte_no);
2770     }
2771     __ j(Done);
2772   }
2773 
2774   __ bind(notByte);
2775   __ sub(t0, tos_state, (u1)ztos);
2776   __ bnez(t0, notBool);
2777 
2778   // ztos
2779   {
2780     __ pop(ztos);
2781     // field address
2782     if (!is_static) {
2783       pop_and_check_object(obj);
2784     }
2785     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2786     const Address field(off, 0);
2787     __ access_store_at(T_BOOLEAN, IN_HEAP, field, x10, noreg, noreg, noreg);
2788     if (rc == may_rewrite) {
2789       patch_bytecode(Bytecodes::_fast_zputfield, bc, x11, true, byte_no);
2790     }
2791     __ j(Done);
2792   }
2793 
2794   __ bind(notBool);
2795   __ sub(t0, tos_state, (u1)atos);
2796   __ bnez(t0, notObj);
2797 
2798   // atos
2799   {
2800     __ pop(atos);
2801     // field address
2802     if (!is_static) {
2803       pop_and_check_object(obj);
2804     }
2805     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2806     const Address field(off, 0);
2807     // Store into the field
2808     do_oop_store(_masm, field, x10, IN_HEAP);
2809     if (rc == may_rewrite) {
2810       patch_bytecode(Bytecodes::_fast_aputfield, bc, x11, true, byte_no);
2811     }
2812     __ j(Done);
2813   }
2814 
2815   __ bind(notObj);
2816   __ sub(t0, tos_state, (u1)itos);
2817   __ bnez(t0, notInt);
2818 
2819   // itos
2820   {
2821     __ pop(itos);
2822     // field address
2823     if (!is_static) {
2824       pop_and_check_object(obj);
2825     }
2826     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2827     const Address field(off, 0);
2828     __ access_store_at(T_INT, IN_HEAP, field, x10, noreg, noreg, noreg);
2829     if (rc == may_rewrite) {
2830       patch_bytecode(Bytecodes::_fast_iputfield, bc, x11, true, byte_no);
2831     }
2832     __ j(Done);
2833   }
2834 
2835   __ bind(notInt);
2836   __ sub(t0, tos_state, (u1)ctos);
2837   __ bnez(t0, notChar);
2838 
2839   // ctos
2840   {
2841     __ pop(ctos);
2842     // field address
2843     if (!is_static) {
2844       pop_and_check_object(obj);
2845     }
2846     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2847     const Address field(off, 0);
2848     __ access_store_at(T_CHAR, IN_HEAP, field, x10, noreg, noreg, noreg);
2849     if (rc == may_rewrite) {
2850       patch_bytecode(Bytecodes::_fast_cputfield, bc, x11, true, byte_no);
2851     }
2852     __ j(Done);
2853   }
2854 
2855   __ bind(notChar);
2856   __ sub(t0, tos_state, (u1)stos);
2857   __ bnez(t0, notShort);
2858 
2859   // stos
2860   {
2861     __ pop(stos);
2862     // field address
2863     if (!is_static) {
2864       pop_and_check_object(obj);
2865     }
2866     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2867     const Address field(off, 0);
2868     __ access_store_at(T_SHORT, IN_HEAP, field, x10, noreg, noreg, noreg);
2869     if (rc == may_rewrite) {
2870       patch_bytecode(Bytecodes::_fast_sputfield, bc, x11, true, byte_no);
2871     }
2872     __ j(Done);
2873   }
2874 
2875   __ bind(notShort);
2876   __ sub(t0, tos_state, (u1)ltos);
2877   __ bnez(t0, notLong);
2878 
2879   // ltos
2880   {
2881     __ pop(ltos);
2882     // field address
2883     if (!is_static) {
2884       pop_and_check_object(obj);
2885     }
2886     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2887     const Address field(off, 0);
2888     __ access_store_at(T_LONG, IN_HEAP, field, x10, noreg, noreg, noreg);
2889     if (rc == may_rewrite) {
2890       patch_bytecode(Bytecodes::_fast_lputfield, bc, x11, true, byte_no);
2891     }
2892     __ j(Done);
2893   }
2894 
2895   __ bind(notLong);
2896   __ sub(t0, tos_state, (u1)ftos);
2897   __ bnez(t0, notFloat);
2898 
2899   // ftos
2900   {
2901     __ pop(ftos);
2902     // field address
2903     if (!is_static) {
2904       pop_and_check_object(obj);
2905     }
2906     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2907     const Address field(off, 0);
2908     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos */, noreg, noreg, noreg);
2909     if (rc == may_rewrite) {
2910       patch_bytecode(Bytecodes::_fast_fputfield, bc, x11, true, byte_no);
2911     }
2912     __ j(Done);
2913   }
2914 
2915   __ bind(notFloat);
2916 #ifdef ASSERT
2917   __ sub(t0, tos_state, (u1)dtos);
2918   __ bnez(t0, notDouble);
2919 #endif
2920 
2921   // dtos
2922   {
2923     __ pop(dtos);
2924     // field address
2925     if (!is_static) {
2926       pop_and_check_object(obj);
2927     }
2928     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2929     const Address field(off, 0);
2930     __ access_store_at(T_DOUBLE, IN_HEAP, field, noreg /* dtos */, noreg, noreg, noreg);
2931     if (rc == may_rewrite) {
2932       patch_bytecode(Bytecodes::_fast_dputfield, bc, x11, true, byte_no);
2933     }
2934   }
2935 
2936 #ifdef ASSERT
2937   __ j(Done);
2938 
2939   __ bind(notDouble);
2940   __ stop("Bad state");
2941 #endif
2942 
2943   __ bind(Done);
2944 
2945   {
2946     Label notVolatile;
2947     __ test_bit(t0, x15, ResolvedFieldEntry::is_volatile_shift);
2948     __ beqz(t0, notVolatile);
2949     __ membar(MacroAssembler::StoreLoad | MacroAssembler::StoreStore);
2950     __ bind(notVolatile);
2951   }
2952 }
2953 
2954 void TemplateTable::putfield(int byte_no) {
2955   putfield_or_static(byte_no, false);
2956 }
2957 
2958 void TemplateTable::nofast_putfield(int byte_no) {
2959   putfield_or_static(byte_no, false, may_not_rewrite);
2960 }
2961 
2962 void TemplateTable::putstatic(int byte_no) {
2963   putfield_or_static(byte_no, true);
2964 }
2965 
2966 void TemplateTable::jvmti_post_fast_field_mod() {
2967   if (JvmtiExport::can_post_field_modification()) {
2968     // Check to see if a field modification watch has been set before
2969     // we take the time to call into the VM.
2970     Label L2;
2971     ExternalAddress target((address)JvmtiExport::get_field_modification_count_addr());
2972     __ relocate(target.rspec(), [&] {
2973       int32_t offset;
2974       __ la(t0, target.target(), offset);
2975       __ lwu(c_rarg3, Address(t0, offset));
2976     });
2977     __ beqz(c_rarg3, L2);
2978     __ pop_ptr(x9);                  // copy the object pointer from tos
2979     __ verify_oop(x9);
2980     __ push_ptr(x9);                 // put the object pointer back on tos
2981     // Save tos values before call_VM() clobbers them. Since we have
2982     // to do it for every data type, we use the saved values as the
2983     // jvalue object.
2984     switch (bytecode()) {          // load values into the jvalue object
2985       case Bytecodes::_fast_aputfield: __ push_ptr(x10); break;
2986       case Bytecodes::_fast_bputfield: // fall through
2987       case Bytecodes::_fast_zputfield: // fall through
2988       case Bytecodes::_fast_sputfield: // fall through
2989       case Bytecodes::_fast_cputfield: // fall through
2990       case Bytecodes::_fast_iputfield: __ push_i(x10); break;
2991       case Bytecodes::_fast_dputfield: __ push_d(); break;
2992       case Bytecodes::_fast_fputfield: __ push_f(); break;
2993       case Bytecodes::_fast_lputfield: __ push_l(x10); break;
2994 
2995       default:
2996         ShouldNotReachHere();
2997     }
2998     __ mv(c_rarg3, esp);             // points to jvalue on the stack
2999     // access constant pool cache entry
3000     __ load_field_entry(c_rarg2, x10);
3001     __ verify_oop(x9);
3002     // x9: object pointer copied above
3003     // c_rarg2: cache entry pointer
3004     // c_rarg3: jvalue object on the stack
3005     __ call_VM(noreg,
3006                CAST_FROM_FN_PTR(address,
3007                                 InterpreterRuntime::post_field_modification),
3008                x9, c_rarg2, c_rarg3);
3009 
3010     switch (bytecode()) {             // restore tos values
3011       case Bytecodes::_fast_aputfield: __ pop_ptr(x10); break;
3012       case Bytecodes::_fast_bputfield: // fall through
3013       case Bytecodes::_fast_zputfield: // fall through
3014       case Bytecodes::_fast_sputfield: // fall through
3015       case Bytecodes::_fast_cputfield: // fall through
3016       case Bytecodes::_fast_iputfield: __ pop_i(x10); break;
3017       case Bytecodes::_fast_dputfield: __ pop_d(); break;
3018       case Bytecodes::_fast_fputfield: __ pop_f(); break;
3019       case Bytecodes::_fast_lputfield: __ pop_l(x10); break;
3020       default: break;
3021     }
3022     __ bind(L2);
3023   }
3024 }
3025 
3026 void TemplateTable::fast_storefield(TosState state) {
3027   transition(state, vtos);
3028 
3029   ByteSize base = ConstantPoolCache::base_offset();
3030 
3031   jvmti_post_fast_field_mod();
3032 
3033   // access constant pool cache
3034   __ load_field_entry(x12, x11);
3035 
3036   // X11: field offset, X12: field holder, X13: flags
3037   load_resolved_field_entry(x12, x12, noreg, x11, x13);
3038 
3039   {
3040     Label notVolatile;
3041     __ test_bit(t0, x13, ResolvedFieldEntry::is_volatile_shift);
3042     __ beqz(t0, notVolatile);
3043     __ membar(MacroAssembler::StoreStore | MacroAssembler::LoadStore);
3044     __ bind(notVolatile);
3045   }
3046 
3047   // Get object from stack
3048   pop_and_check_object(x12);
3049 
3050   // field address
3051   __ add(x11, x12, x11);
3052   const Address field(x11, 0);
3053 
3054   // access field
3055   switch (bytecode()) {
3056     case Bytecodes::_fast_aputfield:
3057       do_oop_store(_masm, field, x10, IN_HEAP);
3058       break;
3059     case Bytecodes::_fast_lputfield:
3060       __ access_store_at(T_LONG, IN_HEAP, field, x10, noreg, noreg, noreg);
3061       break;
3062     case Bytecodes::_fast_iputfield:
3063       __ access_store_at(T_INT, IN_HEAP, field, x10, noreg, noreg, noreg);
3064       break;
3065     case Bytecodes::_fast_zputfield:
3066       __ access_store_at(T_BOOLEAN, IN_HEAP, field, x10, noreg, noreg, noreg);
3067       break;
3068     case Bytecodes::_fast_bputfield:
3069       __ access_store_at(T_BYTE, IN_HEAP, field, x10, noreg, noreg, noreg);
3070       break;
3071     case Bytecodes::_fast_sputfield:
3072       __ access_store_at(T_SHORT, IN_HEAP, field, x10, noreg, noreg, noreg);
3073       break;
3074     case Bytecodes::_fast_cputfield:
3075       __ access_store_at(T_CHAR, IN_HEAP, field, x10, noreg, noreg, noreg);
3076       break;
3077     case Bytecodes::_fast_fputfield:
3078       __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos */, noreg, noreg, noreg);
3079       break;
3080     case Bytecodes::_fast_dputfield:
3081       __ access_store_at(T_DOUBLE, IN_HEAP, field, noreg /* dtos */, noreg, noreg, noreg);
3082       break;
3083     default:
3084       ShouldNotReachHere();
3085   }
3086 
3087   {
3088     Label notVolatile;
3089     __ test_bit(t0, x13, ResolvedFieldEntry::is_volatile_shift);
3090     __ beqz(t0, notVolatile);
3091     __ membar(MacroAssembler::StoreLoad | MacroAssembler::StoreStore);
3092     __ bind(notVolatile);
3093   }
3094 }
3095 
3096 void TemplateTable::fast_accessfield(TosState state) {
3097   transition(atos, state);
3098   // Do the JVMTI work here to avoid disturbing the register state below
3099   if (JvmtiExport::can_post_field_access()) {
3100     // Check to see if a field access watch has been set before we
3101     // take the time to call into the VM.
3102     Label L1;
3103     ExternalAddress target((address)JvmtiExport::get_field_access_count_addr());
3104     __ relocate(target.rspec(), [&] {
3105       int32_t offset;
3106       __ la(t0, target.target(), offset);
3107       __ lwu(x12, Address(t0, offset));
3108     });
3109     __ beqz(x12, L1);
3110     // access constant pool cache entry
3111     __ load_field_entry(c_rarg2, t1);
3112     __ verify_oop(x10);
3113     __ push_ptr(x10);  // save object pointer before call_VM() clobbers it
3114     __ mv(c_rarg1, x10);
3115     // c_rarg1: object pointer copied above
3116     // c_rarg2: cache entry pointer
3117     __ call_VM(noreg,
3118                CAST_FROM_FN_PTR(address,
3119                                 InterpreterRuntime::post_field_access),
3120                c_rarg1, c_rarg2);
3121     __ pop_ptr(x10); // restore object pointer
3122     __ bind(L1);
3123   }
3124 
3125   // access constant pool cache
3126   __ load_field_entry(x12, x11);
3127 
3128   __ load_sized_value(x11, Address(x12, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
3129   __ load_unsigned_byte(x13, Address(x12, in_bytes(ResolvedFieldEntry::flags_offset())));
3130 
3131   // x10: object
3132   __ verify_oop(x10);
3133   __ null_check(x10);
3134   __ add(x11, x10, x11);
3135   const Address field(x11, 0);
3136 
3137   // access field
3138   switch (bytecode()) {
3139     case Bytecodes::_fast_agetfield:
3140       do_oop_load(_masm, field, x10, IN_HEAP);
3141       __ verify_oop(x10);
3142       break;
3143     case Bytecodes::_fast_lgetfield:
3144       __ access_load_at(T_LONG, IN_HEAP, x10, field, noreg, noreg);
3145       break;
3146     case Bytecodes::_fast_igetfield:
3147       __ access_load_at(T_INT, IN_HEAP, x10, field, noreg, noreg);
3148       __ sign_extend(x10, x10, 32);
3149       break;
3150     case Bytecodes::_fast_bgetfield:
3151       __ access_load_at(T_BYTE, IN_HEAP, x10, field, noreg, noreg);
3152       break;
3153     case Bytecodes::_fast_sgetfield:
3154       __ access_load_at(T_SHORT, IN_HEAP, x10, field, noreg, noreg);
3155       break;
3156     case Bytecodes::_fast_cgetfield:
3157       __ access_load_at(T_CHAR, IN_HEAP, x10, field, noreg, noreg);
3158       break;
3159     case Bytecodes::_fast_fgetfield:
3160       __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3161       break;
3162     case Bytecodes::_fast_dgetfield:
3163       __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* dtos */, field, noreg, noreg);
3164       break;
3165     default:
3166       ShouldNotReachHere();
3167   }
3168   {
3169     Label notVolatile;
3170     __ test_bit(t0, x13, ResolvedFieldEntry::is_volatile_shift);
3171     __ beqz(t0, notVolatile);
3172     __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
3173     __ bind(notVolatile);
3174   }
3175 }
3176 
3177 void TemplateTable::fast_xaccess(TosState state) {
3178   transition(vtos, state);
3179 
3180   // get receiver
3181   __ ld(x10, aaddress(0));
3182   // access constant pool cache
3183   __ load_field_entry(x12, x13, 2);
3184   __ load_sized_value(x11, Address(x12, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
3185 
3186   // make sure exception is reported in correct bcp range (getfield is
3187   // next instruction)
3188   __ addi(xbcp, xbcp, 1);
3189   __ null_check(x10);
3190   switch (state) {
3191     case itos:
3192       __ add(x10, x10, x11);
3193       __ access_load_at(T_INT, IN_HEAP, x10, Address(x10, 0), noreg, noreg);
3194       __ sign_extend(x10, x10, 32);
3195       break;
3196     case atos:
3197       __ add(x10, x10, x11);
3198       do_oop_load(_masm, Address(x10, 0), x10, IN_HEAP);
3199       __ verify_oop(x10);
3200       break;
3201     case ftos:
3202       __ add(x10, x10, x11);
3203       __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, Address(x10), noreg, noreg);
3204       break;
3205     default:
3206       ShouldNotReachHere();
3207   }
3208 
3209   {
3210     Label notVolatile;
3211     __ load_unsigned_byte(x13, Address(x12, in_bytes(ResolvedFieldEntry::flags_offset())));
3212     __ test_bit(t0, x13, ResolvedFieldEntry::is_volatile_shift);
3213     __ beqz(t0, notVolatile);
3214     __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
3215     __ bind(notVolatile);
3216   }
3217 
3218   __ sub(xbcp, xbcp, 1);
3219 }
3220 
3221 //-----------------------------------------------------------------------------
3222 // Calls
3223 
3224 void TemplateTable::prepare_invoke(Register cache, Register recv) {
3225 
3226   Bytecodes::Code code = bytecode();
3227   const bool load_receiver       = (code != Bytecodes::_invokestatic) && (code != Bytecodes::_invokedynamic);
3228 
3229   // save 'interpreter return address'
3230   __ save_bcp();
3231 
3232   // Load TOS state for later
3233   __ load_unsigned_byte(t1, Address(cache, in_bytes(ResolvedMethodEntry::type_offset())));
3234 
3235   // load receiver if needed (note: no return address pushed yet)
3236   if (load_receiver) {
3237     __ load_unsigned_short(recv, Address(cache, in_bytes(ResolvedMethodEntry::num_parameters_offset())));
3238     __ shadd(t0, recv, esp, t0, 3);
3239     __ ld(recv, Address(t0, -Interpreter::expr_offset_in_bytes(1)));
3240     __ verify_oop(recv);
3241   }
3242 
3243   // load return address
3244   {
3245     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
3246     __ mv(t0, table_addr);
3247     __ shadd(t0, t1, t0, t1, 3);
3248     __ ld(ra, Address(t0, 0));
3249   }
3250 }
3251 
3252 void TemplateTable::invokevirtual_helper(Register index,
3253                                          Register recv,
3254                                          Register flags) {
3255   // Uses temporary registers x10, x13
3256   assert_different_registers(index, recv, x10, x13);
3257   // Test for an invoke of a final method
3258   Label notFinal;
3259   __ test_bit(t0, flags, ResolvedMethodEntry::is_vfinal_shift);
3260   __ beqz(t0, notFinal);
3261 
3262   const Register method = index;  // method must be xmethod
3263   assert(method == xmethod, "Method must be xmethod for interpreter calling convention");
3264 
3265   // do the call - the index is actually the method to call
3266   // that is, f2 is a vtable index if !is_vfinal, else f2 is a Method*
3267 
3268   // It's final, need a null check here!
3269   __ null_check(recv);
3270 
3271   // profile this call
3272   __ profile_final_call(x10);
3273   __ profile_arguments_type(x10, method, x14, true);
3274 
3275   __ jump_from_interpreted(method);
3276 
3277   __ bind(notFinal);
3278 
3279   // get receiver klass
3280   __ load_klass(x10, recv);
3281 
3282   // profile this call
3283   __ profile_virtual_call(x10, xlocals, x13);
3284 
3285   // get target Method & entry point
3286   __ lookup_virtual_method(x10, index, method);
3287   __ profile_arguments_type(x13, method, x14, true);
3288   __ jump_from_interpreted(method);
3289 }
3290 
3291 void TemplateTable::invokevirtual(int byte_no) {
3292   transition(vtos, vtos);
3293   assert(byte_no == f2_byte, "use this argument");
3294 
3295   load_resolved_method_entry_virtual(x12,      // ResolvedMethodEntry*
3296                                      xmethod, // Method* or itable index
3297                                      x13);     // flags
3298   prepare_invoke(x12, x12); // recv
3299 
3300   // xmethod: index (actually a Method*)
3301   // x12: receiver
3302   // x13: flags
3303 
3304   invokevirtual_helper(xmethod, x12, x13);
3305 }
3306 
3307 void TemplateTable::invokespecial(int byte_no) {
3308   transition(vtos, vtos);
3309   assert(byte_no == f1_byte, "use this argument");
3310 
3311   load_resolved_method_entry_special_or_static(x12,      // ResolvedMethodEntry*
3312                                                xmethod, // Method*
3313                                                x13);     // flags
3314   prepare_invoke(x12, x12);  // get receiver also for null check
3315 
3316   __ verify_oop(x12);
3317   __ null_check(x12);
3318   // do the call
3319   __ profile_call(x10);
3320   __ profile_arguments_type(x10, xmethod, xbcp, false);
3321   __ jump_from_interpreted(xmethod);
3322 }
3323 
3324 void TemplateTable::invokestatic(int byte_no) {
3325   transition(vtos, vtos);
3326   assert(byte_no == f1_byte, "use this argument");
3327 
3328   load_resolved_method_entry_special_or_static(x12,      // ResolvedMethodEntry*
3329                                                xmethod, // Method*
3330                                                x13);     // flags
3331   prepare_invoke(x12, x12);  // get receiver also for null check
3332 
3333   // do the call
3334   __ profile_call(x10);
3335   __ profile_arguments_type(x10, xmethod, x14, false);
3336   __ jump_from_interpreted(xmethod);
3337 }
3338 
3339 void TemplateTable::fast_invokevfinal(int byte_no) {
3340   __ call_Unimplemented();
3341 }
3342 
3343 void TemplateTable::invokeinterface(int byte_no) {
3344   transition(vtos, vtos);
3345   assert(byte_no == f1_byte, "use this argument");
3346 
3347   load_resolved_method_entry_interface(x12,      // ResolvedMethodEntry*
3348                                        x10,      // Klass*
3349                                        xmethod, // Method* or itable/vtable index
3350                                        x13);     // flags
3351   prepare_invoke(x12, x12); // receiver
3352 
3353   // x10: interface klass (from f1)
3354   // xmethod: method (from f2)
3355   // x12: receiver
3356   // x13: flags
3357 
3358   // First check for Object case, then private interface method,
3359   // then regular interface method.
3360 
3361   // Special case of invokeinterface called for virtual method of
3362   // java.lang.Object. See cpCache.cpp for details
3363   Label notObjectMethod;
3364   __ test_bit(t0, x13, ResolvedMethodEntry::is_forced_virtual_shift);
3365   __ beqz(t0, notObjectMethod);
3366 
3367   invokevirtual_helper(xmethod, x12, x13);
3368   __ bind(notObjectMethod);
3369 
3370   Label no_such_interface;
3371 
3372   // Check for private method invocation - indicated by vfinal
3373   Label notVFinal;
3374   __ test_bit(t0, x13, ResolvedMethodEntry::is_vfinal_shift);
3375   __ beqz(t0, notVFinal);
3376 
3377   // Check receiver klass into x13
3378   __ load_klass(x13, x12);
3379 
3380   Label subtype;
3381   __ check_klass_subtype(x13, x10, x14, subtype);
3382   // If we get here the typecheck failed
3383   __ j(no_such_interface);
3384   __ bind(subtype);
3385 
3386   __ profile_final_call(x10);
3387   __ profile_arguments_type(x10, xmethod, x14, true);
3388   __ jump_from_interpreted(xmethod);
3389 
3390   __ bind(notVFinal);
3391 
3392   // Get receiver klass into x13
3393   __ restore_locals();
3394   __ load_klass(x13, x12);
3395 
3396   Label no_such_method;
3397 
3398   // Preserve method for the throw_AbstractMethodErrorVerbose.
3399   __ mv(x28, xmethod);
3400   // Receiver subtype check against REFC.
3401   // Superklass in x10. Subklass in x13. Blows t1, x30
3402   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3403                              x13, x10, noreg,
3404                              // outputs: scan temp. reg, scan temp. reg
3405                              t1, x30,
3406                              no_such_interface,
3407                              /*return_method=*/false);
3408 
3409   // profile this call
3410   __ profile_virtual_call(x13, x30, x9);
3411 
3412   // Get declaring interface class from method, and itable index
3413   __ load_method_holder(x10, xmethod);
3414   __ lwu(xmethod, Address(xmethod, Method::itable_index_offset()));
3415   __ subw(xmethod, xmethod, Method::itable_index_max);
3416   __ negw(xmethod, xmethod);
3417 
3418   // Preserve recvKlass for throw_AbstractMethodErrorVerbose
3419   __ mv(xlocals, x13);
3420   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3421                              xlocals, x10, xmethod,
3422                              // outputs: method, scan temp. reg
3423                              xmethod, x30,
3424                              no_such_interface);
3425 
3426   // xmethod: Method to call
3427   // x12: receiver
3428   // Check for abstract method error
3429   // Note: This should be done more efficiently via a throw_abstract_method_error
3430   //       interpreter entry point and a conditional jump to it in case of a null
3431   //       method.
3432   __ beqz(xmethod, no_such_method);
3433 
3434   __ profile_arguments_type(x13, xmethod, x30, true);
3435 
3436   // do the call
3437   // x12: receiver
3438   // xmethod: Method
3439   __ jump_from_interpreted(xmethod);
3440   __ should_not_reach_here();
3441 
3442   // exception handling code follows ...
3443   // note: must restore interpreter registers to canonical
3444   //       state for exception handling to work correctly!
3445 
3446   __ bind(no_such_method);
3447   // throw exception
3448   __ restore_bcp();    // bcp must be correct for exception handler   (was destroyed)
3449   __ restore_locals(); // make sure locals pointer is correct as well (was destroyed)
3450   // Pass arguments for generating a verbose error message.
3451   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorVerbose), x13, x28);
3452   // the call_VM checks for exception, so we should never return here.
3453   __ should_not_reach_here();
3454 
3455   __ bind(no_such_interface);
3456   // throw exceptiong
3457   __ restore_bcp();    // bcp must be correct for exception handler   (was destroyed)
3458   __ restore_locals(); // make sure locals pointer is correct as well (was destroyed)
3459   // Pass arguments for generating a verbose error message.
3460   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
3461                                      InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose), x13, x10);
3462   // the call_VM checks for exception, so we should never return here.
3463   __ should_not_reach_here();
3464   return;
3465 }
3466 
3467 void TemplateTable::invokehandle(int byte_no) {
3468   transition(vtos, vtos);
3469   assert(byte_no == f1_byte, "use this argument");
3470 
3471   load_resolved_method_entry_handle(x12,      // ResolvedMethodEntry*
3472                                     xmethod, // Method*
3473                                     x10,      // Resolved reference
3474                                     x13);     // flags
3475   prepare_invoke(x12, x12);
3476 
3477   __ verify_method_ptr(x12);
3478   __ verify_oop(x12);
3479   __ null_check(x12);
3480 
3481   // FIXME: profile the LambdaForm also
3482 
3483   // x30 is safe to use here as a temp reg because it is about to
3484   // be clobbered by jump_from_interpreted().
3485   __ profile_final_call(x30);
3486   __ profile_arguments_type(x30, xmethod, x14, true);
3487 
3488   __ jump_from_interpreted(xmethod);
3489 }
3490 
3491 void TemplateTable::invokedynamic(int byte_no) {
3492   transition(vtos, vtos);
3493   assert(byte_no == f1_byte, "use this argument");
3494 
3495   load_invokedynamic_entry(xmethod);
3496 
3497   // x10: CallSite object (from cpool->resolved_references[])
3498   // xmethod: MH.linkToCallSite method
3499 
3500   // Note: x10_callsite is already pushed
3501 
3502   // %%% should make a type profile for any invokedynamic that takes a ref argument
3503   // profile this call
3504   __ profile_call(xbcp);
3505   __ profile_arguments_type(x13, xmethod, x30, false);
3506 
3507   __ verify_oop(x10);
3508 
3509   __ jump_from_interpreted(xmethod);
3510 }
3511 
3512 //-----------------------------------------------------------------------------
3513 // Allocation
3514 
3515 void TemplateTable::_new() {
3516   transition(vtos, atos);
3517 
3518   __ get_unsigned_2_byte_index_at_bcp(x13, 1);
3519   Label slow_case;
3520   Label done;
3521   Label initialize_header;
3522 
3523   __ get_cpool_and_tags(x14, x10);
3524   // Make sure the class we're about to instantiate has been resolved.
3525   // This is done before loading InstanceKlass to be consistent with the order
3526   // how Constant Pool is update (see ConstantPool::klass_at_put)
3527   const int tags_offset = Array<u1>::base_offset_in_bytes();
3528   __ add(t0, x10, x13);
3529   __ la(t0, Address(t0, tags_offset));
3530   __ lbu(t0, t0);
3531   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
3532   __ sub(t1, t0, (u1)JVM_CONSTANT_Class);
3533   __ bnez(t1, slow_case);
3534 
3535   // get InstanceKlass
3536   __ load_resolved_klass_at_offset(x14, x13, x14, t0);
3537 
3538   // make sure klass is initialized
3539   assert(VM_Version::supports_fast_class_init_checks(),
3540          "Optimization requires support for fast class initialization checks");
3541   __ clinit_barrier(x14, t0, nullptr /*L_fast_path*/, &slow_case);
3542 
3543   // get instance_size in InstanceKlass (scaled to a count of bytes)
3544   __ lwu(x13, Address(x14, Klass::layout_helper_offset()));
3545   // test to see if is malformed in some way
3546   __ test_bit(t0, x13, exact_log2(Klass::_lh_instance_slow_path_bit));
3547   __ bnez(t0, slow_case);
3548 
3549   // Allocate the instance:
3550   //  If TLAB is enabled:
3551   //    Try to allocate in the TLAB.
3552   //    If fails, go to the slow path.
3553   //    Initialize the allocation.
3554   //    Exit.
3555   //  Go to slow path.
3556 
3557   if (UseTLAB) {
3558     __ tlab_allocate(x10, x13, 0, noreg, x11, slow_case);
3559 
3560     if (ZeroTLAB) {
3561       // the fields have been already cleared
3562       __ j(initialize_header);
3563     }
3564 
3565     // The object is initialized before the header. If the object size is
3566     // zero, go directly to the header initialization.
3567     __ sub(x13, x13, sizeof(oopDesc));
3568     __ beqz(x13, initialize_header);
3569 
3570     // Initialize object fields
3571     {
3572       __ add(x12, x10, sizeof(oopDesc));
3573       Label loop;
3574       __ bind(loop);
3575       __ sd(zr, Address(x12));
3576       __ add(x12, x12, BytesPerLong);
3577       __ sub(x13, x13, BytesPerLong);
3578       __ bnez(x13, loop);
3579     }
3580 
3581     // initialize object hader only.
3582     __ bind(initialize_header);
3583     __ mv(t0, (intptr_t)markWord::prototype().value());
3584     __ sd(t0, Address(x10, oopDesc::mark_offset_in_bytes()));
3585     __ store_klass_gap(x10, zr);   // zero klass gap for compressed oops
3586     __ store_klass(x10, x14);      // store klass last
3587 
3588     if (DTraceAllocProbes) {
3589       // Trigger dtrace event for fastpath
3590       __ push(atos); // save the return value
3591       __ call_VM_leaf(CAST_FROM_FN_PTR(address, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc)), x10);
3592       __ pop(atos); // restore the return value
3593     }
3594     __ j(done);
3595   }
3596 
3597   // slow case
3598   __ bind(slow_case);
3599   __ get_constant_pool(c_rarg1);
3600   __ get_unsigned_2_byte_index_at_bcp(c_rarg2, 1);
3601   call_VM(x10, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), c_rarg1, c_rarg2);
3602   __ verify_oop(x10);
3603 
3604   // continue
3605   __ bind(done);
3606   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3607   __ membar(MacroAssembler::StoreStore);
3608 }
3609 
3610 void TemplateTable::newarray() {
3611   transition(itos, atos);
3612   __ load_unsigned_byte(c_rarg1, at_bcp(1));
3613   __ mv(c_rarg2, x10);
3614   call_VM(x10, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
3615           c_rarg1, c_rarg2);
3616   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3617   __ membar(MacroAssembler::StoreStore);
3618 }
3619 
3620 void TemplateTable::anewarray() {
3621   transition(itos, atos);
3622   __ get_unsigned_2_byte_index_at_bcp(c_rarg2, 1);
3623   __ get_constant_pool(c_rarg1);
3624   __ mv(c_rarg3, x10);
3625   call_VM(x10, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
3626           c_rarg1, c_rarg2, c_rarg3);
3627   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3628   __ membar(MacroAssembler::StoreStore);
3629 }
3630 
3631 void TemplateTable::arraylength() {
3632   transition(atos, itos);
3633   __ lwu(x10, Address(x10, arrayOopDesc::length_offset_in_bytes()));
3634 }
3635 
3636 void TemplateTable::checkcast() {
3637   transition(atos, atos);
3638   Label done, is_null, ok_is_subtype, quicked, resolved;
3639   __ beqz(x10, is_null);
3640 
3641   // Get cpool & tags index
3642   __ get_cpool_and_tags(x12, x13); // x12=cpool, x13=tags array
3643   __ get_unsigned_2_byte_index_at_bcp(x9, 1); // x9=index
3644   // See if bytecode has already been quicked
3645   __ add(t0, x13, Array<u1>::base_offset_in_bytes());
3646   __ add(x11, t0, x9);
3647   __ lbu(x11, x11);
3648   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
3649   __ sub(t0, x11, (u1)JVM_CONSTANT_Class);
3650   __ beqz(t0, quicked);
3651 
3652   __ push(atos); // save receiver for result, and for GC
3653   call_VM(x10, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3654   // vm_result_2 has metadata result
3655   __ get_vm_result_2(x10, xthread);
3656   __ pop_reg(x13); // restore receiver
3657   __ j(resolved);
3658 
3659   // Get superklass in x10 and subklass in x13
3660   __ bind(quicked);
3661   __ mv(x13, x10); // Save object in x13; x10 needed for subtype check
3662   __ load_resolved_klass_at_offset(x12, x9, x10, t0); // x10 = klass
3663 
3664   __ bind(resolved);
3665   __ load_klass(x9, x13);
3666 
3667   // Generate subtype check.  Blows x12, x15.  Object in x13.
3668   // Superklass in x10.  Subklass in x9.
3669   __ gen_subtype_check(x9, ok_is_subtype);
3670 
3671   // Come here on failure
3672   __ push_reg(x13);
3673   // object is at TOS
3674   __ j(Interpreter::_throw_ClassCastException_entry);
3675 
3676   // Come here on success
3677   __ bind(ok_is_subtype);
3678   __ mv(x10, x13); // Restore object in x13
3679 
3680   // Collect counts on whether this test sees nulls a lot or not.
3681   if (ProfileInterpreter) {
3682     __ j(done);
3683     __ bind(is_null);
3684     __ profile_null_seen(x12);
3685   } else {
3686     __ bind(is_null);   // same as 'done'
3687   }
3688   __ bind(done);
3689 }
3690 
3691 void TemplateTable::instanceof() {
3692   transition(atos, itos);
3693   Label done, is_null, ok_is_subtype, quicked, resolved;
3694   __ beqz(x10, is_null);
3695 
3696   // Get cpool & tags index
3697   __ get_cpool_and_tags(x12, x13); // x12=cpool, x13=tags array
3698   __ get_unsigned_2_byte_index_at_bcp(x9, 1); // x9=index
3699   // See if bytecode has already been quicked
3700   __ add(t0, x13, Array<u1>::base_offset_in_bytes());
3701   __ add(x11, t0, x9);
3702   __ lbu(x11, x11);
3703   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
3704   __ sub(t0, x11, (u1)JVM_CONSTANT_Class);
3705   __ beqz(t0, quicked);
3706 
3707   __ push(atos); // save receiver for result, and for GC
3708   call_VM(x10, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3709   // vm_result_2 has metadata result
3710   __ get_vm_result_2(x10, xthread);
3711   __ pop_reg(x13); // restore receiver
3712   __ verify_oop(x13);
3713   __ load_klass(x13, x13);
3714   __ j(resolved);
3715 
3716   // Get superklass in x10 and subklass in x13
3717   __ bind(quicked);
3718   __ load_klass(x13, x10);
3719   __ load_resolved_klass_at_offset(x12, x9, x10, t0);
3720 
3721   __ bind(resolved);
3722 
3723   // Generate subtype check.  Blows x12, x15
3724   // Superklass in x10.  Subklass in x13.
3725   __ gen_subtype_check(x13, ok_is_subtype);
3726 
3727   // Come here on failure
3728   __ mv(x10, zr);
3729   __ j(done);
3730   // Come here on success
3731   __ bind(ok_is_subtype);
3732   __ mv(x10, 1);
3733 
3734   // Collect counts on whether this test sees nulls a lot or not.
3735   if (ProfileInterpreter) {
3736     __ j(done);
3737     __ bind(is_null);
3738     __ profile_null_seen(x12);
3739   } else {
3740     __ bind(is_null);   // same as 'done'
3741   }
3742   __ bind(done);
3743   // x10 = 0: obj is    null or  obj is not an instanceof the specified klass
3744   // x10 = 1: obj isn't null and obj is     an instanceof the specified klass
3745 }
3746 
3747 //-----------------------------------------------------------------------------
3748 // Breakpoints
3749 
3750 void TemplateTable::_breakpoint() {
3751   // Note: We get here even if we are single stepping..
3752   // jbug inists on setting breakpoints at every bytecode
3753   // even if we are in single step mode.
3754 
3755   transition(vtos, vtos);
3756 
3757   // get the unpatched byte code
3758   __ get_method(c_rarg1);
3759   __ call_VM(noreg,
3760              CAST_FROM_FN_PTR(address,
3761                               InterpreterRuntime::get_original_bytecode_at),
3762              c_rarg1, xbcp);
3763   __ mv(x9, x10);
3764 
3765   // post the breakpoint event
3766   __ call_VM(noreg,
3767              CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint),
3768              xmethod, xbcp);
3769 
3770   // complete the execution of original bytecode
3771   __ mv(t0, x9);
3772   __ dispatch_only_normal(vtos);
3773 }
3774 
3775 //-----------------------------------------------------------------------------
3776 // Exceptions
3777 
3778 void TemplateTable::athrow() {
3779   transition(atos, vtos);
3780   __ null_check(x10);
3781   __ j(Interpreter::throw_exception_entry());
3782 }
3783 
3784 //-----------------------------------------------------------------------------
3785 // Synchronization
3786 //
3787 // Note: monitorenter & exit are symmetric routines; which is reflected
3788 //       in the assembly code structure as well
3789 //
3790 // Stack layout:
3791 //
3792 // [expressions  ] <--- esp               = expression stack top
3793 // ..
3794 // [expressions  ]
3795 // [monitor entry] <--- monitor block top = expression stack bot
3796 // ..
3797 // [monitor entry]
3798 // [frame data   ] <--- monitor block bot
3799 // ...
3800 // [saved fp     ] <--- fp
3801 
3802 void TemplateTable::monitorenter() {
3803   transition(atos, vtos);
3804 
3805    // check for null object
3806    __ null_check(x10);
3807 
3808    const Address monitor_block_top(
3809          fp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
3810    const Address monitor_block_bot(
3811          fp, frame::interpreter_frame_initial_sp_offset * wordSize);
3812    const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
3813 
3814    Label allocated;
3815 
3816    // initialize entry pointer
3817    __ mv(c_rarg1, zr); // points to free slot or null
3818 
3819    // find a free slot in the monitor block (result in c_rarg1)
3820    {
3821      Label entry, loop, exit, notUsed;
3822      __ ld(c_rarg3, monitor_block_top); // derelativize pointer
3823      __ shadd(c_rarg3, c_rarg3, fp, c_rarg3, LogBytesPerWord);
3824      // Now c_rarg3 points to current entry, starting with top-most entry
3825 
3826      __ la(c_rarg2, monitor_block_bot); // points to word before bottom
3827 
3828      __ j(entry);
3829 
3830      __ bind(loop);
3831      // check if current entry is used
3832      // if not used then remember entry in c_rarg1
3833      __ ld(t0, Address(c_rarg3, BasicObjectLock::obj_offset()));
3834      __ bnez(t0, notUsed);
3835      __ mv(c_rarg1, c_rarg3);
3836      __ bind(notUsed);
3837      // check if current entry is for same object
3838      // if same object then stop searching
3839      __ beq(x10, t0, exit);
3840      // otherwise advance to next entry
3841      __ add(c_rarg3, c_rarg3, entry_size);
3842      __ bind(entry);
3843      // check if bottom reached
3844      // if not at bottom then check this entry
3845      __ bne(c_rarg3, c_rarg2, loop);
3846      __ bind(exit);
3847    }
3848 
3849    __ bnez(c_rarg1, allocated); // check if a slot has been found and
3850                              // if found, continue with that on
3851 
3852    // allocate one if there's no free slot
3853    {
3854      Label entry, loop;
3855      // 1. compute new pointers            // esp: old expression stack top
3856 
3857      __ check_extended_sp();
3858      __ sub(sp, sp, entry_size);           // make room for the monitor
3859      __ sub(t0, sp, fp);
3860      __ srai(t0, t0, Interpreter::logStackElementSize);
3861      __ sd(t0, Address(fp, frame::interpreter_frame_extended_sp_offset * wordSize));
3862 
3863      __ ld(c_rarg1, monitor_block_bot);    // derelativize pointer
3864      __ shadd(c_rarg1, c_rarg1, fp, c_rarg1, LogBytesPerWord);
3865      // Now c_rarg1 points to the old expression stack bottom
3866 
3867      __ sub(esp, esp, entry_size);         // move expression stack top
3868      __ sub(c_rarg1, c_rarg1, entry_size); // move expression stack bottom
3869      __ mv(c_rarg3, esp);                  // set start value for copy loop
3870      __ sub(t0, c_rarg1, fp);              // relativize pointer
3871      __ srai(t0, t0, Interpreter::logStackElementSize);
3872      __ sd(t0, monitor_block_bot);         // set new monitor block bottom
3873 
3874      __ j(entry);
3875      // 2. move expression stack contents
3876      __ bind(loop);
3877      __ ld(c_rarg2, Address(c_rarg3, entry_size)); // load expression stack
3878                                                    // word from old location
3879      __ sd(c_rarg2, Address(c_rarg3, 0));          // and store it at new location
3880      __ add(c_rarg3, c_rarg3, wordSize);           // advance to next word
3881      __ bind(entry);
3882      __ bne(c_rarg3, c_rarg1, loop);    // check if bottom reached.if not at bottom
3883                                         // then copy next word
3884    }
3885 
3886    // call run-time routine
3887    // c_rarg1: points to monitor entry
3888    __ bind(allocated);
3889 
3890    // Increment bcp to point to the next bytecode, so exception
3891    // handling for async. exceptions work correctly.
3892    // The object has already been popped from the stack, so the
3893    // expression stack looks correct.
3894    __ addi(xbcp, xbcp, 1);
3895 
3896    // store object
3897    __ sd(x10, Address(c_rarg1, BasicObjectLock::obj_offset()));
3898 
3899    __ lock_object(c_rarg1);
3900 
3901    // check to make sure this monitor doesn't cause stack overflow after locking
3902    __ save_bcp();  // in case of exception
3903    __ generate_stack_overflow_check(0);
3904 
3905    // The bcp has already been incremented. Just need to dispatch to
3906    // next instruction.
3907    __ dispatch_next(vtos);
3908 }
3909 
3910 void TemplateTable::monitorexit() {
3911   transition(atos, vtos);
3912 
3913   // check for null object
3914   __ null_check(x10);
3915 
3916   const Address monitor_block_top(
3917         fp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
3918   const Address monitor_block_bot(
3919         fp, frame::interpreter_frame_initial_sp_offset * wordSize);
3920   const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
3921 
3922   Label found;
3923 
3924   // find matching slot
3925   {
3926     Label entry, loop;
3927     __ ld(c_rarg1, monitor_block_top); // derelativize pointer
3928     __ shadd(c_rarg1, c_rarg1, fp, c_rarg1, LogBytesPerWord);
3929     // Now c_rarg1 points to current entry, starting with top-most entry
3930 
3931     __ la(c_rarg2, monitor_block_bot); // points to word before bottom
3932                                         // of monitor block
3933     __ j(entry);
3934 
3935     __ bind(loop);
3936     // check if current entry is for same object
3937     __ ld(t0, Address(c_rarg1, BasicObjectLock::obj_offset()));
3938     // if same object then stop searching
3939     __ beq(x10, t0, found);
3940     // otherwise advance to next entry
3941     __ add(c_rarg1, c_rarg1, entry_size);
3942     __ bind(entry);
3943     // check if bottom reached
3944     // if not at bottom then check this entry
3945     __ bne(c_rarg1, c_rarg2, loop);
3946   }
3947 
3948   // error handling. Unlocking was not block-structured
3949   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
3950              InterpreterRuntime::throw_illegal_monitor_state_exception));
3951   __ should_not_reach_here();
3952 
3953   // call run-time routine
3954   __ bind(found);
3955   __ push_ptr(x10); // make sure object is on stack (contract with oopMaps)
3956   __ unlock_object(c_rarg1);
3957   __ pop_ptr(x10); // discard object
3958 }
3959 
3960 // Wide instructions
3961 void TemplateTable::wide() {
3962   __ load_unsigned_byte(x9, at_bcp(1));
3963   __ mv(t0, (address)Interpreter::_wentry_point);
3964   __ shadd(t0, x9, t0, t1, 3);
3965   __ ld(t0, Address(t0));
3966   __ jr(t0);
3967 }
3968 
3969 // Multi arrays
3970 void TemplateTable::multianewarray() {
3971   transition(vtos, atos);
3972   __ load_unsigned_byte(x10, at_bcp(3)); // get number of dimensions
3973   // last dim is on top of stack; we want address of first one:
3974   // first_addr = last_addr + (ndims - 1) * wordSize
3975   __ shadd(c_rarg1, x10, esp, c_rarg1, 3);
3976   __ sub(c_rarg1, c_rarg1, wordSize);
3977   call_VM(x10,
3978           CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray),
3979           c_rarg1);
3980   __ load_unsigned_byte(x11, at_bcp(3));
3981   __ shadd(esp, x11, esp, t0, 3);
3982 }