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