1 /*
   2  * Copyright (c) 2003, 2025, 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 (VM_Version::supports_fast_class_init_checks() && bytecode() == Bytecodes::_invokestatic) {
2197     __ bne(temp, t0, L_clinit_barrier_slow);  // have we resolved this bytecode?
2198     __ ld(temp, Address(Rcache, in_bytes(ResolvedMethodEntry::method_offset())));
2199     __ load_method_holder(temp, temp);
2200     __ clinit_barrier(temp, t0, &L_done, /*L_slow_path*/ nullptr);
2201     __ bind(L_clinit_barrier_slow);
2202   } else {
2203     __ beq(temp, t0, L_done);  // have we resolved this bytecode?
2204   }
2205 
2206   // resolve first time through
2207   // Class initialization barrier slow path lands here as well.
2208   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2209   __ mv(temp, (int) code);
2210   __ call_VM(noreg, entry, temp);
2211 
2212   // Update registers with resolved info
2213   __ load_method_entry(Rcache, index);
2214   // n.b. unlike x86 Rcache is now rcpool plus the indexed offset
2215   // so all clients ofthis method must be modified accordingly
2216   __ bind(L_done);
2217 }
2218 
2219 void TemplateTable::resolve_cache_and_index_for_field(int byte_no,
2220                                             Register Rcache,
2221                                             Register index) {
2222   const Register temp = x9;
2223   assert_different_registers(Rcache, index, temp);
2224 
2225   Label L_clinit_barrier_slow, L_done;
2226 
2227   Bytecodes::Code code = bytecode();
2228   switch (code) {
2229     case Bytecodes::_nofast_getfield: code = Bytecodes::_getfield; break;
2230     case Bytecodes::_nofast_putfield: code = Bytecodes::_putfield; break;
2231     default: break;
2232   }
2233 
2234   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2235   __ load_field_entry(Rcache, index);
2236   if (byte_no == f1_byte) {
2237     __ la(temp, Address(Rcache, in_bytes(ResolvedFieldEntry::get_code_offset())));
2238   } else {
2239     __ la(temp, Address(Rcache, in_bytes(ResolvedFieldEntry::put_code_offset())));
2240   }
2241   // Load-acquire the bytecode to match store-release in ResolvedFieldEntry::fill_in()
2242   __ lbu(temp, Address(temp, 0));
2243   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
2244   __ mv(t0, (int) code);  // have we resolved this bytecode?
2245 
2246   // Class initialization barrier for static fields
2247   if (VM_Version::supports_fast_class_init_checks() &&
2248       (bytecode() == Bytecodes::_getstatic || bytecode() == Bytecodes::_putstatic)) {
2249     const Register field_holder = temp;
2250 
2251     __ bne(temp, t0, L_clinit_barrier_slow);
2252     __ ld(field_holder, Address(Rcache, in_bytes(ResolvedFieldEntry::field_holder_offset())));
2253     __ clinit_barrier(field_holder, t0, &L_done, /*L_slow_path*/ nullptr);
2254     __ bind(L_clinit_barrier_slow);
2255   } else {
2256     __ beq(temp, t0, L_done);
2257   }
2258 
2259   // resolve first time through
2260   // Class initialization barrier slow path lands here as well.
2261   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2262   __ mv(temp, (int) code);
2263   __ call_VM(noreg, entry, temp);
2264 
2265   // Update registers with resolved info
2266   __ load_field_entry(Rcache, index);
2267   __ bind(L_done);
2268 }
2269 
2270 void TemplateTable::load_resolved_field_entry(Register obj,
2271                                               Register cache,
2272                                               Register tos_state,
2273                                               Register offset,
2274                                               Register flags,
2275                                               bool is_static = false) {
2276   assert_different_registers(cache, tos_state, flags, offset);
2277 
2278   // Field offset
2279   __ load_sized_value(offset, Address(cache, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
2280 
2281   // Flags
2282   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedFieldEntry::flags_offset())));
2283 
2284   // TOS state
2285   if (tos_state != noreg) {
2286     __ load_unsigned_byte(tos_state, Address(cache, in_bytes(ResolvedFieldEntry::type_offset())));
2287   }
2288 
2289   // Klass overwrite register
2290   if (is_static) {
2291     __ ld(obj, Address(cache, ResolvedFieldEntry::field_holder_offset()));
2292     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
2293     __ ld(obj, Address(obj, mirror_offset));
2294     __ resolve_oop_handle(obj, x15, t1);
2295   }
2296 }
2297 
2298 void TemplateTable::load_resolved_method_entry_special_or_static(Register cache,
2299                                                                  Register method,
2300                                                                  Register flags) {
2301 
2302   // setup registers
2303   const Register index = flags;
2304   assert_different_registers(method, cache, flags);
2305 
2306   // determine constant pool cache field offsets
2307   resolve_cache_and_index_for_method(f1_byte, cache, index);
2308   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2309   __ ld(method, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2310 }
2311 
2312 void TemplateTable::load_resolved_method_entry_handle(Register cache,
2313                                                       Register method,
2314                                                       Register ref_index,
2315                                                       Register flags) {
2316   // setup registers
2317   const Register index = ref_index;
2318   assert_different_registers(method, flags);
2319   assert_different_registers(method, cache, index);
2320 
2321   // determine constant pool cache field offsets
2322   resolve_cache_and_index_for_method(f1_byte, cache, index);
2323   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2324 
2325   // maybe push appendix to arguments (just before return address)
2326   Label L_no_push;
2327   __ test_bit(t0, flags, ResolvedMethodEntry::has_appendix_shift);
2328   __ beqz(t0, L_no_push);
2329   // invokehandle uses an index into the resolved references array
2330   __ load_unsigned_short(ref_index, Address(cache, in_bytes(ResolvedMethodEntry::resolved_references_index_offset())));
2331   // Push the appendix as a trailing parameter.
2332   // This must be done before we get the receiver,
2333   // since the parameter_size includes it.
2334   Register appendix = method;
2335   __ load_resolved_reference_at_index(appendix, ref_index);
2336   __ push_reg(appendix); // push appendix (MethodType, CallSite, etc.)
2337   __ bind(L_no_push);
2338 
2339   __ ld(method, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2340 }
2341 
2342 void TemplateTable::load_resolved_method_entry_interface(Register cache,
2343                                                          Register klass,
2344                                                          Register method_or_table_index,
2345                                                          Register flags) {
2346   // setup registers
2347   const Register index = method_or_table_index;
2348   assert_different_registers(method_or_table_index, cache, flags);
2349 
2350   // determine constant pool cache field offsets
2351   resolve_cache_and_index_for_method(f1_byte, cache, index);
2352   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2353 
2354   // Invokeinterface can behave in different ways:
2355   // If calling a method from java.lang.Object, the forced virtual flag is true so the invocation will
2356   // behave like an invokevirtual call. The state of the virtual final flag will determine whether a method or
2357   // vtable index is placed in the register.
2358   // Otherwise, the registers will be populated with the klass and method.
2359 
2360   Label NotVirtual; Label NotVFinal; Label Done;
2361   __ test_bit(t0, flags, ResolvedMethodEntry::is_forced_virtual_shift);
2362   __ beqz(t0, NotVirtual);
2363   __ test_bit(t0, flags, ResolvedMethodEntry::is_vfinal_shift);
2364   __ beqz(t0, NotVFinal);
2365   __ ld(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2366   __ j(Done);
2367 
2368   __ bind(NotVFinal);
2369   __ load_unsigned_short(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::table_index_offset())));
2370   __ j(Done);
2371 
2372   __ bind(NotVirtual);
2373   __ ld(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2374   __ ld(klass, Address(cache, in_bytes(ResolvedMethodEntry::klass_offset())));
2375   __ bind(Done);
2376 }
2377 
2378 void TemplateTable::load_resolved_method_entry_virtual(Register cache,
2379                                                        Register method_or_table_index,
2380                                                        Register flags) {
2381   // setup registers
2382   const Register index = flags;
2383   assert_different_registers(method_or_table_index, cache, flags);
2384 
2385   // determine constant pool cache field offsets
2386   resolve_cache_and_index_for_method(f2_byte, cache, index);
2387   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2388 
2389   // method_or_table_index can either be an itable index or a method depending on the virtual final flag
2390   Label NotVFinal; Label Done;
2391   __ test_bit(t0, flags, ResolvedMethodEntry::is_vfinal_shift);
2392   __ beqz(t0, NotVFinal);
2393   __ ld(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2394   __ j(Done);
2395 
2396   __ bind(NotVFinal);
2397   __ load_unsigned_short(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::table_index_offset())));
2398   __ bind(Done);
2399 }
2400 
2401 // The xmethod register is input and overwritten to be the adapter method for the
2402 // indy call. Return address (ra) is set to the return address for the adapter and
2403 // an appendix may be pushed to the stack. Registers x10-x13 are clobbered.
2404 void TemplateTable::load_invokedynamic_entry(Register method) {
2405   // setup registers
2406   const Register appendix = x10;
2407   const Register cache = x12;
2408   const Register index = x13;
2409   assert_different_registers(method, appendix, cache, index, xcpool);
2410 
2411   __ save_bcp();
2412 
2413   Label resolved;
2414 
2415   __ load_resolved_indy_entry(cache, index);
2416   __ ld(method, Address(cache, in_bytes(ResolvedIndyEntry::method_offset())));
2417   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
2418 
2419   // Compare the method to zero
2420   __ bnez(method, resolved);
2421 
2422   Bytecodes::Code code = bytecode();
2423 
2424   // Call to the interpreter runtime to resolve invokedynamic
2425   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2426   __ mv(method, code); // this is essentially Bytecodes::_invokedynamic
2427   __ call_VM(noreg, entry, method);
2428   // Update registers with resolved info
2429   __ load_resolved_indy_entry(cache, index);
2430   __ ld(method, Address(cache, in_bytes(ResolvedIndyEntry::method_offset())));
2431   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
2432 
2433 #ifdef ASSERT
2434   __ bnez(method, resolved);
2435   __ stop("Should be resolved by now");
2436 #endif // ASSERT
2437   __ bind(resolved);
2438 
2439   Label L_no_push;
2440   // Check if there is an appendix
2441   __ load_unsigned_byte(index, Address(cache, in_bytes(ResolvedIndyEntry::flags_offset())));
2442   __ test_bit(t0, index, ResolvedIndyEntry::has_appendix_shift);
2443   __ beqz(t0, L_no_push);
2444 
2445   // Get appendix
2446   __ load_unsigned_short(index, Address(cache, in_bytes(ResolvedIndyEntry::resolved_references_index_offset())));
2447   // Push the appendix as a trailing parameter
2448   // since the parameter_size includes it.
2449   __ push_reg(method);
2450   __ mv(method, index);
2451   __ load_resolved_reference_at_index(appendix, method);
2452   __ verify_oop(appendix);
2453   __ pop_reg(method);
2454   __ push_reg(appendix);  // push appendix (MethodType, CallSite, etc.)
2455   __ bind(L_no_push);
2456 
2457   // compute return type
2458   __ load_unsigned_byte(index, Address(cache, in_bytes(ResolvedIndyEntry::result_type_offset())));
2459   // load return address
2460   // Return address is loaded into ra and not pushed to the stack like x86
2461   {
2462     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
2463     __ mv(t0, table_addr);
2464     __ shadd(t0, index, t0, index, 3);
2465     __ ld(ra, Address(t0, 0));
2466   }
2467 }
2468 
2469 // The registers cache and index expected to be set before call.
2470 // Correct values of the cache and index registers are preserved.
2471 void TemplateTable::jvmti_post_field_access(Register cache, Register index,
2472                                             bool is_static, bool has_tos) {
2473   // do the JVMTI work here to avoid disturbing the register state below
2474   // We use c_rarg registers here because we want to use the register used in
2475   // the call to the VM
2476   if (JvmtiExport::can_post_field_access()) {
2477     // Check to see if a field access watch has been set before we
2478     // take the time to call into the VM.
2479     Label L1;
2480     assert_different_registers(cache, index, x10);
2481     __ lwu(x10, ExternalAddress(JvmtiExport::get_field_access_count_addr()));
2482     __ beqz(x10, L1);
2483 
2484     __ load_field_entry(c_rarg2, index);
2485 
2486     if (is_static) {
2487       __ mv(c_rarg1, zr); // null object reference
2488     } else {
2489       __ ld(c_rarg1, at_tos()); // get object pointer without popping it
2490       __ verify_oop(c_rarg1);
2491     }
2492     // c_rarg1: object pointer or null
2493     // c_rarg2: cache entry pointer
2494     __ call_VM(noreg, CAST_FROM_FN_PTR(address,
2495                                        InterpreterRuntime::post_field_access),
2496                                        c_rarg1, c_rarg2);
2497     __ load_field_entry(cache, index);
2498     __ bind(L1);
2499   }
2500 }
2501 
2502 void TemplateTable::pop_and_check_object(Register r) {
2503   __ pop_ptr(r);
2504   __ null_check(r);  // for field access must check obj.
2505   __ verify_oop(r);
2506 }
2507 
2508 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2509   const Register cache     = x14;
2510   const Register obj       = x14;
2511   const Register index     = x13;
2512   const Register tos_state = x13;
2513   const Register off       = x9;
2514   const Register flags     = x16;
2515   const Register bc        = x14; // uses same reg as obj, so don't mix them
2516 
2517   resolve_cache_and_index_for_field(byte_no, cache, index);
2518   jvmti_post_field_access(cache, index, is_static, false);
2519   load_resolved_field_entry(obj, cache, tos_state, off, flags, is_static);
2520 
2521   if (!is_static) {
2522     // obj is on the stack
2523     pop_and_check_object(obj);
2524   }
2525 
2526   __ add(off, obj, off);
2527   const Address field(off);
2528 
2529   Label Done, notByte, notBool, notInt, notShort, notChar,
2530               notLong, notFloat, notObj, notDouble;
2531 
2532   assert(btos == 0, "change code, btos != 0");
2533   __ bnez(tos_state, notByte);
2534 
2535   // Don't rewrite getstatic, only getfield
2536   if (is_static) {
2537     rc = may_not_rewrite;
2538   }
2539 
2540   // btos
2541   __ access_load_at(T_BYTE, IN_HEAP, x10, field, noreg, noreg);
2542   __ push(btos);
2543   // Rewrite bytecode to be faster
2544   if (rc == may_rewrite) {
2545     patch_bytecode(Bytecodes::_fast_bgetfield, bc, x11);
2546   }
2547   __ j(Done);
2548 
2549   __ bind(notByte);
2550   __ subi(t0, tos_state, (u1)ztos);
2551   __ bnez(t0, notBool);
2552 
2553   // ztos (same code as btos)
2554   __ access_load_at(T_BOOLEAN, IN_HEAP, x10, field, noreg, noreg);
2555   __ push(ztos);
2556   // Rewrite bytecode to be faster
2557   if (rc == may_rewrite) {
2558     // uses btos rewriting, no truncating to t/f bit is needed for getfield
2559     patch_bytecode(Bytecodes::_fast_bgetfield, bc, x11);
2560   }
2561   __ j(Done);
2562 
2563   __ bind(notBool);
2564   __ subi(t0, tos_state, (u1)atos);
2565   __ bnez(t0, notObj);
2566   // atos
2567   __ load_heap_oop(x10, field, x28, x29, IN_HEAP);
2568   __ push(atos);
2569   if (rc == may_rewrite) {
2570     patch_bytecode(Bytecodes::_fast_agetfield, bc, x11);
2571   }
2572   __ j(Done);
2573 
2574   __ bind(notObj);
2575   __ subi(t0, tos_state, (u1)itos);
2576   __ bnez(t0, notInt);
2577   // itos
2578   __ access_load_at(T_INT, IN_HEAP, x10, field, noreg, noreg);
2579   __ sext(x10, x10, 32);
2580   __ push(itos);
2581   // Rewrite bytecode to be faster
2582   if (rc == may_rewrite) {
2583     patch_bytecode(Bytecodes::_fast_igetfield, bc, x11);
2584   }
2585   __ j(Done);
2586 
2587   __ bind(notInt);
2588   __ subi(t0, tos_state, (u1)ctos);
2589   __ bnez(t0, notChar);
2590   // ctos
2591   __ access_load_at(T_CHAR, IN_HEAP, x10, field, noreg, noreg);
2592   __ push(ctos);
2593   // Rewrite bytecode to be faster
2594   if (rc == may_rewrite) {
2595     patch_bytecode(Bytecodes::_fast_cgetfield, bc, x11);
2596   }
2597   __ j(Done);
2598 
2599   __ bind(notChar);
2600   __ subi(t0, tos_state, (u1)stos);
2601   __ bnez(t0, notShort);
2602   // stos
2603   __ access_load_at(T_SHORT, IN_HEAP, x10, field, noreg, noreg);
2604   __ push(stos);
2605   // Rewrite bytecode to be faster
2606   if (rc == may_rewrite) {
2607     patch_bytecode(Bytecodes::_fast_sgetfield, bc, x11);
2608   }
2609   __ j(Done);
2610 
2611   __ bind(notShort);
2612   __ subi(t0, tos_state, (u1)ltos);
2613   __ bnez(t0, notLong);
2614   // ltos
2615   __ access_load_at(T_LONG, IN_HEAP, x10, field, noreg, noreg);
2616   __ push(ltos);
2617   // Rewrite bytecode to be faster
2618   if (rc == may_rewrite) {
2619     patch_bytecode(Bytecodes::_fast_lgetfield, bc, x11);
2620   }
2621   __ j(Done);
2622 
2623   __ bind(notLong);
2624   __ subi(t0, tos_state, (u1)ftos);
2625   __ bnez(t0, notFloat);
2626   // ftos
2627   __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
2628   __ push(ftos);
2629   // Rewrite bytecode to be faster
2630   if (rc == may_rewrite) {
2631     patch_bytecode(Bytecodes::_fast_fgetfield, bc, x11);
2632   }
2633   __ j(Done);
2634 
2635   __ bind(notFloat);
2636 #ifdef ASSERT
2637   __ subi(t0, tos_state, (u1)dtos);
2638   __ bnez(t0, notDouble);
2639 #endif
2640   // dtos
2641   __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
2642   __ push(dtos);
2643   // Rewrite bytecode to be faster
2644   if (rc == may_rewrite) {
2645     patch_bytecode(Bytecodes::_fast_dgetfield, bc, x11);
2646   }
2647 #ifdef ASSERT
2648   __ j(Done);
2649 
2650   __ bind(notDouble);
2651   __ stop("Bad state");
2652 #endif
2653 
2654   __ bind(Done);
2655 
2656   Label notVolatile;
2657   __ test_bit(t0, flags, ResolvedFieldEntry::is_volatile_shift);
2658   __ beqz(t0, notVolatile);
2659   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
2660   __ bind(notVolatile);
2661 }
2662 
2663 void TemplateTable::getfield(int byte_no) {
2664   getfield_or_static(byte_no, false);
2665 }
2666 
2667 void TemplateTable::nofast_getfield(int byte_no) {
2668   getfield_or_static(byte_no, false, may_not_rewrite);
2669 }
2670 
2671 void TemplateTable::getstatic(int byte_no)
2672 {
2673   getfield_or_static(byte_no, true);
2674 }
2675 
2676 // The registers cache and index expected to be set before call.
2677 // The function may destroy various registers, just not the cache and index registers.
2678 void TemplateTable::jvmti_post_field_mod(Register cache, Register index, bool is_static) {
2679   transition(vtos, vtos);
2680 
2681   if (JvmtiExport::can_post_field_modification()) {
2682     // Check to see if a field modification watch has been set before
2683     // we take the time to call into the VM.
2684     Label L1;
2685     assert_different_registers(cache, index, x10);
2686     __ lwu(x10, ExternalAddress(JvmtiExport::get_field_modification_count_addr()));
2687     __ beqz(x10, L1);
2688 
2689     __ mv(c_rarg2, cache);
2690 
2691     if (is_static) {
2692       // Life is simple. Null out the object pointer.
2693       __ mv(c_rarg1, zr);
2694     } else {
2695       // Life is harder. The stack holds the value on top, followed by
2696       // the object. We don't know the size of the value, though; it
2697       // could be one or two words depending on its type. As a result,
2698       // we must find the type to determine where the object is.
2699       __ load_unsigned_byte(c_rarg3, Address(c_rarg2, in_bytes(ResolvedFieldEntry::type_offset())));
2700       Label nope2, done, ok;
2701       __ ld(c_rarg1, at_tos_p1());   // initially assume a one word jvalue
2702       __ subi(t0, c_rarg3, (u1)ltos);
2703       __ beqz(t0, ok);
2704       __ subi(t0, c_rarg3, (u1)dtos);
2705       __ bnez(t0, nope2);
2706       __ bind(ok);
2707       __ ld(c_rarg1, at_tos_p2());  // ltos (two word jvalue);
2708       __ bind(nope2);
2709     }
2710     // object (tos)
2711     __ mv(c_rarg3, esp);
2712     // c_rarg1: object pointer set up above (null if static)
2713     // c_rarg2: cache entry pointer
2714     // c_rarg3: jvalue object on  the stack
2715     __ call_VM(noreg,
2716                CAST_FROM_FN_PTR(address,
2717                                 InterpreterRuntime::post_field_modification),
2718                                 c_rarg1, c_rarg2, c_rarg3);
2719     __ load_field_entry(cache, index);
2720     __ bind(L1);
2721   }
2722 }
2723 
2724 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2725   transition(vtos, vtos);
2726 
2727   const Register cache     = x12;
2728   const Register index     = x13;
2729   const Register tos_state = x13;
2730   const Register obj       = x12;
2731   const Register off       = x9;
2732   const Register flags     = x10;
2733   const Register bc        = x14;
2734 
2735   resolve_cache_and_index_for_field(byte_no, cache, index);
2736   jvmti_post_field_mod(cache, index, is_static);
2737   load_resolved_field_entry(obj, cache, tos_state, off, flags, is_static);
2738 
2739   Label Done;
2740   __ mv(x15, flags);
2741 
2742   {
2743     Label notVolatile;
2744     __ test_bit(t0, x15, ResolvedFieldEntry::is_volatile_shift);
2745     __ beqz(t0, notVolatile);
2746     __ membar(MacroAssembler::StoreStore | MacroAssembler::LoadStore);
2747     __ bind(notVolatile);
2748   }
2749 
2750   Label notByte, notBool, notInt, notShort, notChar,
2751         notLong, notFloat, notObj, notDouble;
2752 
2753   assert(btos == 0, "change code, btos != 0");
2754   __ bnez(tos_state, notByte);
2755 
2756   // Don't rewrite putstatic, only putfield
2757   if (is_static) {
2758     rc = may_not_rewrite;
2759   }
2760 
2761   // btos
2762   {
2763     __ pop(btos);
2764     // field address
2765     if (!is_static) {
2766       pop_and_check_object(obj);
2767     }
2768     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2769     const Address field(off, 0); // off register as temparator register.
2770     __ access_store_at(T_BYTE, IN_HEAP, field, x10, noreg, noreg, noreg);
2771     if (rc == may_rewrite) {
2772       patch_bytecode(Bytecodes::_fast_bputfield, bc, x11, true, byte_no);
2773     }
2774     __ j(Done);
2775   }
2776 
2777   __ bind(notByte);
2778   __ subi(t0, tos_state, (u1)ztos);
2779   __ bnez(t0, notBool);
2780 
2781   // ztos
2782   {
2783     __ pop(ztos);
2784     // field address
2785     if (!is_static) {
2786       pop_and_check_object(obj);
2787     }
2788     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2789     const Address field(off, 0);
2790     __ access_store_at(T_BOOLEAN, IN_HEAP, field, x10, noreg, noreg, noreg);
2791     if (rc == may_rewrite) {
2792       patch_bytecode(Bytecodes::_fast_zputfield, bc, x11, true, byte_no);
2793     }
2794     __ j(Done);
2795   }
2796 
2797   __ bind(notBool);
2798   __ subi(t0, tos_state, (u1)atos);
2799   __ bnez(t0, notObj);
2800 
2801   // atos
2802   {
2803     __ pop(atos);
2804     // field address
2805     if (!is_static) {
2806       pop_and_check_object(obj);
2807     }
2808     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2809     const Address field(off, 0);
2810     // Store into the field
2811     __ store_heap_oop(field, x10, x28, x29, x13, IN_HEAP);
2812     if (rc == may_rewrite) {
2813       patch_bytecode(Bytecodes::_fast_aputfield, bc, x11, true, byte_no);
2814     }
2815     __ j(Done);
2816   }
2817 
2818   __ bind(notObj);
2819   __ subi(t0, tos_state, (u1)itos);
2820   __ bnez(t0, notInt);
2821 
2822   // itos
2823   {
2824     __ pop(itos);
2825     // field address
2826     if (!is_static) {
2827       pop_and_check_object(obj);
2828     }
2829     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2830     const Address field(off, 0);
2831     __ access_store_at(T_INT, IN_HEAP, field, x10, noreg, noreg, noreg);
2832     if (rc == may_rewrite) {
2833       patch_bytecode(Bytecodes::_fast_iputfield, bc, x11, true, byte_no);
2834     }
2835     __ j(Done);
2836   }
2837 
2838   __ bind(notInt);
2839   __ subi(t0, tos_state, (u1)ctos);
2840   __ bnez(t0, notChar);
2841 
2842   // ctos
2843   {
2844     __ pop(ctos);
2845     // field address
2846     if (!is_static) {
2847       pop_and_check_object(obj);
2848     }
2849     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2850     const Address field(off, 0);
2851     __ access_store_at(T_CHAR, IN_HEAP, field, x10, noreg, noreg, noreg);
2852     if (rc == may_rewrite) {
2853       patch_bytecode(Bytecodes::_fast_cputfield, bc, x11, true, byte_no);
2854     }
2855     __ j(Done);
2856   }
2857 
2858   __ bind(notChar);
2859   __ subi(t0, tos_state, (u1)stos);
2860   __ bnez(t0, notShort);
2861 
2862   // stos
2863   {
2864     __ pop(stos);
2865     // field address
2866     if (!is_static) {
2867       pop_and_check_object(obj);
2868     }
2869     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2870     const Address field(off, 0);
2871     __ access_store_at(T_SHORT, IN_HEAP, field, x10, noreg, noreg, noreg);
2872     if (rc == may_rewrite) {
2873       patch_bytecode(Bytecodes::_fast_sputfield, bc, x11, true, byte_no);
2874     }
2875     __ j(Done);
2876   }
2877 
2878   __ bind(notShort);
2879   __ subi(t0, tos_state, (u1)ltos);
2880   __ bnez(t0, notLong);
2881 
2882   // ltos
2883   {
2884     __ pop(ltos);
2885     // field address
2886     if (!is_static) {
2887       pop_and_check_object(obj);
2888     }
2889     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2890     const Address field(off, 0);
2891     __ access_store_at(T_LONG, IN_HEAP, field, x10, noreg, noreg, noreg);
2892     if (rc == may_rewrite) {
2893       patch_bytecode(Bytecodes::_fast_lputfield, bc, x11, true, byte_no);
2894     }
2895     __ j(Done);
2896   }
2897 
2898   __ bind(notLong);
2899   __ subi(t0, tos_state, (u1)ftos);
2900   __ bnez(t0, notFloat);
2901 
2902   // ftos
2903   {
2904     __ pop(ftos);
2905     // field address
2906     if (!is_static) {
2907       pop_and_check_object(obj);
2908     }
2909     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2910     const Address field(off, 0);
2911     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos */, noreg, noreg, noreg);
2912     if (rc == may_rewrite) {
2913       patch_bytecode(Bytecodes::_fast_fputfield, bc, x11, true, byte_no);
2914     }
2915     __ j(Done);
2916   }
2917 
2918   __ bind(notFloat);
2919 #ifdef ASSERT
2920   __ subi(t0, tos_state, (u1)dtos);
2921   __ bnez(t0, notDouble);
2922 #endif
2923 
2924   // dtos
2925   {
2926     __ pop(dtos);
2927     // field address
2928     if (!is_static) {
2929       pop_and_check_object(obj);
2930     }
2931     __ add(off, obj, off); // if static, obj from cache, else obj from stack.
2932     const Address field(off, 0);
2933     __ access_store_at(T_DOUBLE, IN_HEAP, field, noreg /* dtos */, noreg, noreg, noreg);
2934     if (rc == may_rewrite) {
2935       patch_bytecode(Bytecodes::_fast_dputfield, bc, x11, true, byte_no);
2936     }
2937   }
2938 
2939 #ifdef ASSERT
2940   __ j(Done);
2941 
2942   __ bind(notDouble);
2943   __ stop("Bad state");
2944 #endif
2945 
2946   __ bind(Done);
2947 
2948   {
2949     Label notVolatile;
2950     __ test_bit(t0, x15, ResolvedFieldEntry::is_volatile_shift);
2951     __ beqz(t0, notVolatile);
2952     __ membar(MacroAssembler::StoreLoad | MacroAssembler::StoreStore);
2953     __ bind(notVolatile);
2954   }
2955 }
2956 
2957 void TemplateTable::putfield(int byte_no) {
2958   putfield_or_static(byte_no, false);
2959 }
2960 
2961 void TemplateTable::nofast_putfield(int byte_no) {
2962   putfield_or_static(byte_no, false, may_not_rewrite);
2963 }
2964 
2965 void TemplateTable::putstatic(int byte_no) {
2966   putfield_or_static(byte_no, true);
2967 }
2968 
2969 void TemplateTable::jvmti_post_fast_field_mod() {
2970   if (JvmtiExport::can_post_field_modification()) {
2971     // Check to see if a field modification watch has been set before
2972     // we take the time to call into the VM.
2973     Label L2;
2974     __ lwu(c_rarg3, ExternalAddress(JvmtiExport::get_field_modification_count_addr()));
2975     __ beqz(c_rarg3, L2);
2976 
2977     __ pop_ptr(x9);                  // copy the object pointer from tos
2978     __ verify_oop(x9);
2979     __ push_ptr(x9);                 // put the object pointer back on tos
2980     // Save tos values before call_VM() clobbers them. Since we have
2981     // to do it for every data type, we use the saved values as the
2982     // jvalue object.
2983     switch (bytecode()) {          // load values into the jvalue object
2984       case Bytecodes::_fast_aputfield: __ push_ptr(x10); break;
2985       case Bytecodes::_fast_bputfield: // fall through
2986       case Bytecodes::_fast_zputfield: // fall through
2987       case Bytecodes::_fast_sputfield: // fall through
2988       case Bytecodes::_fast_cputfield: // fall through
2989       case Bytecodes::_fast_iputfield: __ push_i(x10); break;
2990       case Bytecodes::_fast_dputfield: __ push_d(); break;
2991       case Bytecodes::_fast_fputfield: __ push_f(); break;
2992       case Bytecodes::_fast_lputfield: __ push_l(x10); break;
2993 
2994       default:
2995         ShouldNotReachHere();
2996     }
2997     __ mv(c_rarg3, esp);             // points to jvalue on the stack
2998     // access constant pool cache entry
2999     __ load_field_entry(c_rarg2, x10);
3000     __ verify_oop(x9);
3001     // x9: object pointer copied above
3002     // c_rarg2: cache entry pointer
3003     // c_rarg3: jvalue object on the stack
3004     __ call_VM(noreg,
3005                CAST_FROM_FN_PTR(address,
3006                                 InterpreterRuntime::post_field_modification),
3007                x9, c_rarg2, c_rarg3);
3008 
3009     switch (bytecode()) {             // restore tos values
3010       case Bytecodes::_fast_aputfield: __ pop_ptr(x10); break;
3011       case Bytecodes::_fast_bputfield: // fall through
3012       case Bytecodes::_fast_zputfield: // fall through
3013       case Bytecodes::_fast_sputfield: // fall through
3014       case Bytecodes::_fast_cputfield: // fall through
3015       case Bytecodes::_fast_iputfield: __ pop_i(x10); break;
3016       case Bytecodes::_fast_dputfield: __ pop_d(); break;
3017       case Bytecodes::_fast_fputfield: __ pop_f(); break;
3018       case Bytecodes::_fast_lputfield: __ pop_l(x10); break;
3019       default: break;
3020     }
3021     __ bind(L2);
3022   }
3023 }
3024 
3025 void TemplateTable::fast_storefield(TosState state) {
3026   transition(state, vtos);
3027 
3028   ByteSize base = ConstantPoolCache::base_offset();
3029 
3030   jvmti_post_fast_field_mod();
3031 
3032   // access constant pool cache
3033   __ load_field_entry(x12, x11);
3034 
3035   // X11: field offset, X12: field holder, X13: flags
3036   load_resolved_field_entry(x12, x12, noreg, x11, x13);
3037   __ verify_field_offset(x11);
3038 
3039   {
3040     Label notVolatile;
3041     __ test_bit(t0, x13, ResolvedFieldEntry::is_volatile_shift);
3042     __ beqz(t0, notVolatile);
3043     __ membar(MacroAssembler::StoreStore | MacroAssembler::LoadStore);
3044     __ bind(notVolatile);
3045   }
3046 
3047   // Get object from stack
3048   pop_and_check_object(x12);
3049 
3050   // field address
3051   __ add(x11, x12, x11);
3052   const Address field(x11, 0);
3053 
3054   // access field, must not clobber x13 - flags
3055   switch (bytecode()) {
3056     case Bytecodes::_fast_aputfield:
3057       __ store_heap_oop(field, x10, x28, x29, x15, IN_HEAP);
3058       break;
3059     case Bytecodes::_fast_lputfield:
3060       __ access_store_at(T_LONG, IN_HEAP, field, x10, noreg, noreg, noreg);
3061       break;
3062     case Bytecodes::_fast_iputfield:
3063       __ access_store_at(T_INT, IN_HEAP, field, x10, noreg, noreg, noreg);
3064       break;
3065     case Bytecodes::_fast_zputfield:
3066       __ access_store_at(T_BOOLEAN, IN_HEAP, field, x10, noreg, noreg, noreg);
3067       break;
3068     case Bytecodes::_fast_bputfield:
3069       __ access_store_at(T_BYTE, IN_HEAP, field, x10, noreg, noreg, noreg);
3070       break;
3071     case Bytecodes::_fast_sputfield:
3072       __ access_store_at(T_SHORT, IN_HEAP, field, x10, noreg, noreg, noreg);
3073       break;
3074     case Bytecodes::_fast_cputfield:
3075       __ access_store_at(T_CHAR, IN_HEAP, field, x10, noreg, noreg, noreg);
3076       break;
3077     case Bytecodes::_fast_fputfield:
3078       __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos */, noreg, noreg, noreg);
3079       break;
3080     case Bytecodes::_fast_dputfield:
3081       __ access_store_at(T_DOUBLE, IN_HEAP, field, noreg /* dtos */, noreg, noreg, noreg);
3082       break;
3083     default:
3084       ShouldNotReachHere();
3085   }
3086 
3087   {
3088     Label notVolatile;
3089     __ test_bit(t0, x13, ResolvedFieldEntry::is_volatile_shift);
3090     __ beqz(t0, notVolatile);
3091     __ membar(MacroAssembler::StoreLoad | MacroAssembler::StoreStore);
3092     __ bind(notVolatile);
3093   }
3094 }
3095 
3096 void TemplateTable::fast_accessfield(TosState state) {
3097   transition(atos, state);
3098   // Do the JVMTI work here to avoid disturbing the register state below
3099   if (JvmtiExport::can_post_field_access()) {
3100     // Check to see if a field access watch has been set before we
3101     // take the time to call into the VM.
3102     Label L1;
3103     __ lwu(x12, ExternalAddress(JvmtiExport::get_field_access_count_addr()));
3104     __ beqz(x12, L1);
3105 
3106     // access constant pool cache entry
3107     __ load_field_entry(c_rarg2, t1);
3108     __ verify_oop(x10);
3109     __ push_ptr(x10);  // save object pointer before call_VM() clobbers it
3110     __ mv(c_rarg1, x10);
3111     // c_rarg1: object pointer copied above
3112     // c_rarg2: cache entry pointer
3113     __ call_VM(noreg,
3114                CAST_FROM_FN_PTR(address,
3115                                 InterpreterRuntime::post_field_access),
3116                c_rarg1, c_rarg2);
3117     __ pop_ptr(x10); // restore object pointer
3118     __ bind(L1);
3119   }
3120 
3121   // access constant pool cache
3122   __ load_field_entry(x12, x11);
3123 
3124   __ load_sized_value(x11, Address(x12, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
3125   __ verify_field_offset(x11);
3126 
3127   __ load_unsigned_byte(x13, Address(x12, in_bytes(ResolvedFieldEntry::flags_offset())));
3128 
3129   // x10: object
3130   __ verify_oop(x10);
3131   __ null_check(x10);
3132   __ add(x11, x10, x11);
3133   const Address field(x11, 0);
3134 
3135   // access field
3136   switch (bytecode()) {
3137     case Bytecodes::_fast_agetfield:
3138       __ load_heap_oop(x10, field, x28, x29, IN_HEAP);
3139       __ verify_oop(x10);
3140       break;
3141     case Bytecodes::_fast_lgetfield:
3142       __ access_load_at(T_LONG, IN_HEAP, x10, field, noreg, noreg);
3143       break;
3144     case Bytecodes::_fast_igetfield:
3145       __ access_load_at(T_INT, IN_HEAP, x10, field, noreg, noreg);
3146       __ sext(x10, x10, 32);
3147       break;
3148     case Bytecodes::_fast_bgetfield:
3149       __ access_load_at(T_BYTE, IN_HEAP, x10, field, noreg, noreg);
3150       break;
3151     case Bytecodes::_fast_sgetfield:
3152       __ access_load_at(T_SHORT, IN_HEAP, x10, field, noreg, noreg);
3153       break;
3154     case Bytecodes::_fast_cgetfield:
3155       __ access_load_at(T_CHAR, IN_HEAP, x10, field, noreg, noreg);
3156       break;
3157     case Bytecodes::_fast_fgetfield:
3158       __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3159       break;
3160     case Bytecodes::_fast_dgetfield:
3161       __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* dtos */, field, noreg, noreg);
3162       break;
3163     default:
3164       ShouldNotReachHere();
3165   }
3166   {
3167     Label notVolatile;
3168     __ test_bit(t0, x13, ResolvedFieldEntry::is_volatile_shift);
3169     __ beqz(t0, notVolatile);
3170     __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
3171     __ bind(notVolatile);
3172   }
3173 }
3174 
3175 void TemplateTable::fast_xaccess(TosState state) {
3176   transition(vtos, state);
3177 
3178   // get receiver
3179   __ ld(x10, aaddress(0));
3180   // access constant pool cache
3181   __ load_field_entry(x12, x13, 2);
3182 
3183   __ load_sized_value(x11, Address(x12, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
3184   __ verify_field_offset(x11);
3185 
3186   // make sure exception is reported in correct bcp range (getfield is
3187   // next instruction)
3188   __ addi(xbcp, xbcp, 1);
3189   __ null_check(x10);
3190   switch (state) {
3191     case itos:
3192       __ add(x10, x10, x11);
3193       __ access_load_at(T_INT, IN_HEAP, x10, Address(x10, 0), noreg, noreg);
3194       __ sext(x10, x10, 32);
3195       break;
3196     case atos:
3197       __ add(x10, x10, x11);
3198       __ load_heap_oop(x10, Address(x10, 0), x28, x29, IN_HEAP);
3199       __ verify_oop(x10);
3200       break;
3201     case ftos:
3202       __ add(x10, x10, x11);
3203       __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, Address(x10), noreg, noreg);
3204       break;
3205     default:
3206       ShouldNotReachHere();
3207   }
3208 
3209   {
3210     Label notVolatile;
3211     __ load_unsigned_byte(x13, Address(x12, in_bytes(ResolvedFieldEntry::flags_offset())));
3212     __ test_bit(t0, x13, ResolvedFieldEntry::is_volatile_shift);
3213     __ beqz(t0, notVolatile);
3214     __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
3215     __ bind(notVolatile);
3216   }
3217 
3218   __ subi(xbcp, xbcp, 1);
3219 }
3220 
3221 //-----------------------------------------------------------------------------
3222 // Calls
3223 
3224 void TemplateTable::prepare_invoke(Register cache, Register recv) {
3225 
3226   Bytecodes::Code code = bytecode();
3227   const bool load_receiver       = (code != Bytecodes::_invokestatic) && (code != Bytecodes::_invokedynamic);
3228 
3229   // save 'interpreter return address'
3230   __ save_bcp();
3231 
3232   // Load TOS state for later
3233   __ load_unsigned_byte(t1, Address(cache, in_bytes(ResolvedMethodEntry::type_offset())));
3234 
3235   // load receiver if needed (note: no return address pushed yet)
3236   if (load_receiver) {
3237     __ load_unsigned_short(recv, Address(cache, in_bytes(ResolvedMethodEntry::num_parameters_offset())));
3238     __ shadd(t0, recv, esp, t0, 3);
3239     __ ld(recv, Address(t0, -Interpreter::expr_offset_in_bytes(1)));
3240     __ verify_oop(recv);
3241   }
3242 
3243   // load return address
3244   {
3245     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
3246     __ mv(t0, table_addr);
3247     __ shadd(t0, t1, t0, t1, 3);
3248     __ ld(ra, Address(t0, 0));
3249   }
3250 }
3251 
3252 void TemplateTable::invokevirtual_helper(Register index,
3253                                          Register recv,
3254                                          Register flags) {
3255   // Uses temporary registers x10, x13
3256   assert_different_registers(index, recv, x10, x13);
3257   // Test for an invoke of a final method
3258   Label notFinal;
3259   __ test_bit(t0, flags, ResolvedMethodEntry::is_vfinal_shift);
3260   __ beqz(t0, notFinal);
3261 
3262   const Register method = index;  // method must be xmethod
3263   assert(method == xmethod, "Method must be xmethod for interpreter calling convention");
3264 
3265   // do the call - the index is actually the method to call
3266   // that is, f2 is a vtable index if !is_vfinal, else f2 is a Method*
3267 
3268   // It's final, need a null check here!
3269   __ null_check(recv);
3270 
3271   // profile this call
3272   __ profile_final_call(x10);
3273   __ profile_arguments_type(x10, method, x14, true);
3274 
3275   __ jump_from_interpreted(method);
3276 
3277   __ bind(notFinal);
3278 
3279   // get receiver klass
3280   __ load_klass(x10, recv);
3281 
3282   // profile this call
3283   __ profile_virtual_call(x10, xlocals, x13);
3284 
3285   // get target Method & entry point
3286   __ lookup_virtual_method(x10, index, method);
3287   __ profile_arguments_type(x13, method, x14, true);
3288   __ jump_from_interpreted(method);
3289 }
3290 
3291 void TemplateTable::invokevirtual(int byte_no) {
3292   transition(vtos, vtos);
3293   assert(byte_no == f2_byte, "use this argument");
3294 
3295   load_resolved_method_entry_virtual(x12,      // ResolvedMethodEntry*
3296                                      xmethod, // Method* or itable index
3297                                      x13);     // flags
3298   prepare_invoke(x12, x12); // recv
3299 
3300   // xmethod: index (actually a Method*)
3301   // x12: receiver
3302   // x13: flags
3303 
3304   invokevirtual_helper(xmethod, x12, x13);
3305 }
3306 
3307 void TemplateTable::invokespecial(int byte_no) {
3308   transition(vtos, vtos);
3309   assert(byte_no == f1_byte, "use this argument");
3310 
3311   load_resolved_method_entry_special_or_static(x12,      // ResolvedMethodEntry*
3312                                                xmethod, // Method*
3313                                                x13);     // flags
3314   prepare_invoke(x12, x12);  // get receiver also for null check
3315 
3316   __ verify_oop(x12);
3317   __ null_check(x12);
3318   // do the call
3319   __ profile_call(x10);
3320   __ profile_arguments_type(x10, xmethod, xbcp, false);
3321   __ jump_from_interpreted(xmethod);
3322 }
3323 
3324 void TemplateTable::invokestatic(int byte_no) {
3325   transition(vtos, vtos);
3326   assert(byte_no == f1_byte, "use this argument");
3327 
3328   load_resolved_method_entry_special_or_static(x12,      // ResolvedMethodEntry*
3329                                                xmethod, // Method*
3330                                                x13);     // flags
3331   prepare_invoke(x12, x12);  // get receiver also for null check
3332 
3333   // do the call
3334   __ profile_call(x10);
3335   __ profile_arguments_type(x10, xmethod, x14, false);
3336   __ jump_from_interpreted(xmethod);
3337 }
3338 
3339 void TemplateTable::fast_invokevfinal(int byte_no) {
3340   __ call_Unimplemented();
3341 }
3342 
3343 void TemplateTable::invokeinterface(int byte_no) {
3344   transition(vtos, vtos);
3345   assert(byte_no == f1_byte, "use this argument");
3346 
3347   load_resolved_method_entry_interface(x12,      // ResolvedMethodEntry*
3348                                        x10,      // Klass*
3349                                        xmethod, // Method* or itable/vtable index
3350                                        x13);     // flags
3351   prepare_invoke(x12, x12); // receiver
3352 
3353   // x10: interface klass (from f1)
3354   // xmethod: method (from f2)
3355   // x12: receiver
3356   // x13: flags
3357 
3358   // First check for Object case, then private interface method,
3359   // then regular interface method.
3360 
3361   // Special case of invokeinterface called for virtual method of
3362   // java.lang.Object. See cpCache.cpp for details
3363   Label notObjectMethod;
3364   __ test_bit(t0, x13, ResolvedMethodEntry::is_forced_virtual_shift);
3365   __ beqz(t0, notObjectMethod);
3366 
3367   invokevirtual_helper(xmethod, x12, x13);
3368   __ bind(notObjectMethod);
3369 
3370   Label no_such_interface;
3371 
3372   // Check for private method invocation - indicated by vfinal
3373   Label notVFinal;
3374   __ test_bit(t0, x13, ResolvedMethodEntry::is_vfinal_shift);
3375   __ beqz(t0, notVFinal);
3376 
3377   // Check receiver klass into x13
3378   __ load_klass(x13, x12);
3379 
3380   Label subtype;
3381   __ check_klass_subtype(x13, x10, x14, subtype);
3382   // If we get here the typecheck failed
3383   __ j(no_such_interface);
3384   __ bind(subtype);
3385 
3386   __ profile_final_call(x10);
3387   __ profile_arguments_type(x10, xmethod, x14, true);
3388   __ jump_from_interpreted(xmethod);
3389 
3390   __ bind(notVFinal);
3391 
3392   // Get receiver klass into x13
3393   __ restore_locals();
3394   __ load_klass(x13, x12);
3395 
3396   Label no_such_method;
3397 
3398   // Preserve method for the throw_AbstractMethodErrorVerbose.
3399   __ mv(x28, xmethod);
3400   // Receiver subtype check against REFC.
3401   // Superklass in x10. Subklass in x13. Blows t1, x30
3402   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3403                              x13, x10, noreg,
3404                              // outputs: scan temp. reg, scan temp. reg
3405                              t1, x30,
3406                              no_such_interface,
3407                              /*return_method=*/false);
3408 
3409   // profile this call
3410   __ profile_virtual_call(x13, x30, x9);
3411 
3412   // Get declaring interface class from method, and itable index
3413   __ load_method_holder(x10, xmethod);
3414   __ lwu(xmethod, Address(xmethod, Method::itable_index_offset()));
3415   __ subw(xmethod, xmethod, Method::itable_index_max);
3416   __ negw(xmethod, xmethod);
3417 
3418   // Preserve recvKlass for throw_AbstractMethodErrorVerbose
3419   __ mv(xlocals, x13);
3420   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3421                              xlocals, x10, xmethod,
3422                              // outputs: method, scan temp. reg
3423                              xmethod, x30,
3424                              no_such_interface);
3425 
3426   // xmethod: Method to call
3427   // x12: receiver
3428   // Check for abstract method error
3429   // Note: This should be done more efficiently via a throw_abstract_method_error
3430   //       interpreter entry point and a conditional jump to it in case of a null
3431   //       method.
3432   __ beqz(xmethod, no_such_method);
3433 
3434   __ profile_arguments_type(x13, xmethod, x30, true);
3435 
3436   // do the call
3437   // x12: receiver
3438   // xmethod: Method
3439   __ jump_from_interpreted(xmethod);
3440   __ should_not_reach_here();
3441 
3442   // exception handling code follows ...
3443   // note: must restore interpreter registers to canonical
3444   //       state for exception handling to work correctly!
3445 
3446   __ bind(no_such_method);
3447   // throw exception
3448   __ restore_bcp();    // bcp must be correct for exception handler   (was destroyed)
3449   __ restore_locals(); // make sure locals pointer is correct as well (was destroyed)
3450   // Pass arguments for generating a verbose error message.
3451   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorVerbose), x13, x28);
3452   // the call_VM checks for exception, so we should never return here.
3453   __ should_not_reach_here();
3454 
3455   __ bind(no_such_interface);
3456   // throw exceptiong
3457   __ restore_bcp();    // bcp must be correct for exception handler   (was destroyed)
3458   __ restore_locals(); // make sure locals pointer is correct as well (was destroyed)
3459   // Pass arguments for generating a verbose error message.
3460   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
3461                                      InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose), x13, x10);
3462   // the call_VM checks for exception, so we should never return here.
3463   __ should_not_reach_here();
3464   return;
3465 }
3466 
3467 void TemplateTable::invokehandle(int byte_no) {
3468   transition(vtos, vtos);
3469   assert(byte_no == f1_byte, "use this argument");
3470 
3471   load_resolved_method_entry_handle(x12,      // ResolvedMethodEntry*
3472                                     xmethod, // Method*
3473                                     x10,      // Resolved reference
3474                                     x13);     // flags
3475   prepare_invoke(x12, x12);
3476 
3477   __ verify_method_ptr(x12);
3478   __ verify_oop(x12);
3479   __ null_check(x12);
3480 
3481   // FIXME: profile the LambdaForm also
3482 
3483   // x30 is safe to use here as a temp reg because it is about to
3484   // be clobbered by jump_from_interpreted().
3485   __ profile_final_call(x30);
3486   __ profile_arguments_type(x30, xmethod, x14, true);
3487 
3488   __ jump_from_interpreted(xmethod);
3489 }
3490 
3491 void TemplateTable::invokedynamic(int byte_no) {
3492   transition(vtos, vtos);
3493   assert(byte_no == f1_byte, "use this argument");
3494 
3495   load_invokedynamic_entry(xmethod);
3496 
3497   // x10: CallSite object (from cpool->resolved_references[])
3498   // xmethod: MH.linkToCallSite method
3499 
3500   // Note: x10_callsite is already pushed
3501 
3502   // %%% should make a type profile for any invokedynamic that takes a ref argument
3503   // profile this call
3504   __ profile_call(xbcp);
3505   __ profile_arguments_type(x13, xmethod, x30, false);
3506 
3507   __ verify_oop(x10);
3508 
3509   __ jump_from_interpreted(xmethod);
3510 }
3511 
3512 //-----------------------------------------------------------------------------
3513 // Allocation
3514 
3515 void TemplateTable::_new() {
3516   transition(vtos, atos);
3517 
3518   __ get_unsigned_2_byte_index_at_bcp(x13, 1);
3519   Label slow_case;
3520   Label done;
3521   Label initialize_header;
3522 
3523   __ get_cpool_and_tags(x14, x10);
3524   // Make sure the class we're about to instantiate has been resolved.
3525   // This is done before loading InstanceKlass to be consistent with the order
3526   // how Constant Pool is update (see ConstantPool::klass_at_put)
3527   const int tags_offset = Array<u1>::base_offset_in_bytes();
3528   __ add(t0, x10, x13);
3529   __ la(t0, Address(t0, tags_offset));
3530   __ lbu(t0, t0);
3531   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
3532   __ subi(t1, t0, (u1)JVM_CONSTANT_Class);
3533   __ bnez(t1, slow_case);
3534 
3535   // get InstanceKlass
3536   __ load_resolved_klass_at_offset(x14, x13, x14, t0);
3537 
3538   // make sure klass is initialized
3539   assert(VM_Version::supports_fast_class_init_checks(),
3540          "Optimization requires support for fast class initialization checks");
3541   __ clinit_barrier(x14, t0, nullptr /*L_fast_path*/, &slow_case);
3542 
3543   // get instance_size in InstanceKlass (scaled to a count of bytes)
3544   __ lwu(x13, Address(x14, Klass::layout_helper_offset()));
3545   // test to see if is malformed in some way
3546   __ test_bit(t0, x13, exact_log2(Klass::_lh_instance_slow_path_bit));
3547   __ bnez(t0, slow_case);
3548 
3549   // Allocate the instance:
3550   //  If TLAB is enabled:
3551   //    Try to allocate in the TLAB.
3552   //    If fails, go to the slow path.
3553   //    Initialize the allocation.
3554   //    Exit.
3555   //  Go to slow path.
3556 
3557   if (UseTLAB) {
3558     __ tlab_allocate(x10, x13, 0, noreg, x11, slow_case);
3559 
3560     if (ZeroTLAB) {
3561       // the fields have been already cleared
3562       __ j(initialize_header);
3563     }
3564 
3565     // The object is initialized before the header. If the object size is
3566     // zero, go directly to the header initialization.
3567     if (UseCompactObjectHeaders) {
3568       assert(is_aligned(oopDesc::base_offset_in_bytes(), BytesPerLong), "oop base offset must be 8-byte-aligned");
3569       __ subi(x13, x13, oopDesc::base_offset_in_bytes());
3570     } else {
3571       __ subi(x13, x13, sizeof(oopDesc));
3572     }
3573     __ beqz(x13, initialize_header);
3574 
3575     // Initialize object fields
3576     {
3577       if (UseCompactObjectHeaders) {
3578         assert(is_aligned(oopDesc::base_offset_in_bytes(), BytesPerLong), "oop base offset must be 8-byte-aligned");
3579         __ addi(x12, x10, oopDesc::base_offset_in_bytes());
3580       } else {
3581         __ addi(x12, x10, sizeof(oopDesc));
3582       }
3583       Label loop;
3584       __ bind(loop);
3585       __ sd(zr, Address(x12));
3586       __ addi(x12, x12, BytesPerLong);
3587       __ subi(x13, x13, BytesPerLong);
3588       __ bnez(x13, loop);
3589     }
3590 
3591     // initialize object hader only.
3592     __ bind(initialize_header);
3593     if (UseCompactObjectHeaders) {
3594       __ ld(t0, Address(x14, Klass::prototype_header_offset()));
3595       __ sd(t0, Address(x10, oopDesc::mark_offset_in_bytes()));
3596     } else {
3597       __ mv(t0, (intptr_t)markWord::prototype().value());
3598       __ sd(t0, Address(x10, oopDesc::mark_offset_in_bytes()));
3599       __ store_klass_gap(x10, zr);   // zero klass gap for compressed oops
3600       __ store_klass(x10, x14);      // store klass last
3601     }
3602 
3603     if (DTraceAllocProbes) {
3604       // Trigger dtrace event for fastpath
3605       __ push(atos); // save the return value
3606       __ call_VM_leaf(CAST_FROM_FN_PTR(address, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc)), x10);
3607       __ pop(atos); // restore the return value
3608     }
3609     __ j(done);
3610   }
3611 
3612   // slow case
3613   __ bind(slow_case);
3614   __ get_constant_pool(c_rarg1);
3615   __ get_unsigned_2_byte_index_at_bcp(c_rarg2, 1);
3616   call_VM(x10, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), c_rarg1, c_rarg2);
3617   __ verify_oop(x10);
3618 
3619   // continue
3620   __ bind(done);
3621   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3622   __ membar(MacroAssembler::StoreStore);
3623 }
3624 
3625 void TemplateTable::newarray() {
3626   transition(itos, atos);
3627   __ load_unsigned_byte(c_rarg1, at_bcp(1));
3628   __ mv(c_rarg2, x10);
3629   call_VM(x10, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
3630           c_rarg1, c_rarg2);
3631   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3632   __ membar(MacroAssembler::StoreStore);
3633 }
3634 
3635 void TemplateTable::anewarray() {
3636   transition(itos, atos);
3637   __ get_unsigned_2_byte_index_at_bcp(c_rarg2, 1);
3638   __ get_constant_pool(c_rarg1);
3639   __ mv(c_rarg3, x10);
3640   call_VM(x10, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
3641           c_rarg1, c_rarg2, c_rarg3);
3642   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3643   __ membar(MacroAssembler::StoreStore);
3644 }
3645 
3646 void TemplateTable::arraylength() {
3647   transition(atos, itos);
3648   __ lwu(x10, Address(x10, arrayOopDesc::length_offset_in_bytes()));
3649 }
3650 
3651 void TemplateTable::checkcast() {
3652   transition(atos, atos);
3653   Label done, is_null, ok_is_subtype, quicked, resolved;
3654   __ beqz(x10, is_null);
3655 
3656   // Get cpool & tags index
3657   __ get_cpool_and_tags(x12, x13); // x12=cpool, x13=tags array
3658   __ get_unsigned_2_byte_index_at_bcp(x9, 1); // x9=index
3659   // See if bytecode has already been quicked
3660   __ addi(t0, x13, Array<u1>::base_offset_in_bytes());
3661   __ add(x11, t0, x9);
3662   __ lbu(x11, x11);
3663   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
3664   __ subi(t0, x11, (u1)JVM_CONSTANT_Class);
3665   __ beqz(t0, quicked);
3666 
3667   __ push(atos); // save receiver for result, and for GC
3668   call_VM(x10, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3669   __ get_vm_result_metadata(x10, xthread);
3670   __ pop_reg(x13); // restore receiver
3671   __ j(resolved);
3672 
3673   // Get superklass in x10 and subklass in x13
3674   __ bind(quicked);
3675   __ mv(x13, x10); // Save object in x13; x10 needed for subtype check
3676   __ load_resolved_klass_at_offset(x12, x9, x10, t0); // x10 = klass
3677 
3678   __ bind(resolved);
3679   __ load_klass(x9, x13);
3680 
3681   // Generate subtype check.  Blows x12, x15.  Object in x13.
3682   // Superklass in x10.  Subklass in x9.
3683   __ gen_subtype_check(x9, ok_is_subtype);
3684 
3685   // Come here on failure
3686   __ push_reg(x13);
3687   // object is at TOS
3688   __ j(RuntimeAddress(Interpreter::_throw_ClassCastException_entry));
3689 
3690   // Come here on success
3691   __ bind(ok_is_subtype);
3692   __ mv(x10, x13); // Restore object in x13
3693 
3694   // Collect counts on whether this test sees nulls a lot or not.
3695   if (ProfileInterpreter) {
3696     __ j(done);
3697     __ bind(is_null);
3698     __ profile_null_seen(x12);
3699   } else {
3700     __ bind(is_null);   // same as 'done'
3701   }
3702   __ bind(done);
3703 }
3704 
3705 void TemplateTable::instanceof() {
3706   transition(atos, itos);
3707   Label done, is_null, ok_is_subtype, quicked, resolved;
3708   __ beqz(x10, is_null);
3709 
3710   // Get cpool & tags index
3711   __ get_cpool_and_tags(x12, x13); // x12=cpool, x13=tags array
3712   __ get_unsigned_2_byte_index_at_bcp(x9, 1); // x9=index
3713   // See if bytecode has already been quicked
3714   __ addi(t0, x13, Array<u1>::base_offset_in_bytes());
3715   __ add(x11, t0, x9);
3716   __ lbu(x11, x11);
3717   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
3718   __ subi(t0, x11, (u1)JVM_CONSTANT_Class);
3719   __ beqz(t0, quicked);
3720 
3721   __ push(atos); // save receiver for result, and for GC
3722   call_VM(x10, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3723   __ get_vm_result_metadata(x10, xthread);
3724   __ pop_reg(x13); // restore receiver
3725   __ verify_oop(x13);
3726   __ load_klass(x13, x13);
3727   __ j(resolved);
3728 
3729   // Get superklass in x10 and subklass in x13
3730   __ bind(quicked);
3731   __ load_klass(x13, x10);
3732   __ load_resolved_klass_at_offset(x12, x9, x10, t0);
3733 
3734   __ bind(resolved);
3735 
3736   // Generate subtype check.  Blows x12, x15
3737   // Superklass in x10.  Subklass in x13.
3738   __ gen_subtype_check(x13, ok_is_subtype);
3739 
3740   // Come here on failure
3741   __ mv(x10, zr);
3742   __ j(done);
3743   // Come here on success
3744   __ bind(ok_is_subtype);
3745   __ mv(x10, 1);
3746 
3747   // Collect counts on whether this test sees nulls a lot or not.
3748   if (ProfileInterpreter) {
3749     __ j(done);
3750     __ bind(is_null);
3751     __ profile_null_seen(x12);
3752   } else {
3753     __ bind(is_null);   // same as 'done'
3754   }
3755   __ bind(done);
3756   // x10 = 0: obj is    null or  obj is not an instanceof the specified klass
3757   // x10 = 1: obj isn't null and obj is     an instanceof the specified klass
3758 }
3759 
3760 //-----------------------------------------------------------------------------
3761 // Breakpoints
3762 
3763 void TemplateTable::_breakpoint() {
3764   // Note: We get here even if we are single stepping..
3765   // jbug inists on setting breakpoints at every bytecode
3766   // even if we are in single step mode.
3767 
3768   transition(vtos, vtos);
3769 
3770   // get the unpatched byte code
3771   __ get_method(c_rarg1);
3772   __ call_VM(noreg,
3773              CAST_FROM_FN_PTR(address,
3774                               InterpreterRuntime::get_original_bytecode_at),
3775              c_rarg1, xbcp);
3776   __ mv(x9, x10);
3777 
3778   // post the breakpoint event
3779   __ call_VM(noreg,
3780              CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint),
3781              xmethod, xbcp);
3782 
3783   // complete the execution of original bytecode
3784   __ mv(t0, x9);
3785   __ dispatch_only_normal(vtos);
3786 }
3787 
3788 //-----------------------------------------------------------------------------
3789 // Exceptions
3790 
3791 void TemplateTable::athrow() {
3792   transition(atos, vtos);
3793   __ null_check(x10);
3794   __ j(RuntimeAddress(Interpreter::throw_exception_entry()));
3795 }
3796 
3797 //-----------------------------------------------------------------------------
3798 // Synchronization
3799 //
3800 // Note: monitorenter & exit are symmetric routines; which is reflected
3801 //       in the assembly code structure as well
3802 //
3803 // Stack layout:
3804 //
3805 // [expressions  ] <--- esp               = expression stack top
3806 // ..
3807 // [expressions  ]
3808 // [monitor entry] <--- monitor block top = expression stack bot
3809 // ..
3810 // [monitor entry]
3811 // [frame data   ] <--- monitor block bot
3812 // ...
3813 // [saved fp     ] <--- fp
3814 
3815 void TemplateTable::monitorenter() {
3816   transition(atos, vtos);
3817 
3818    // check for null object
3819    __ null_check(x10);
3820 
3821    const Address monitor_block_top(
3822          fp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
3823    const Address monitor_block_bot(
3824          fp, frame::interpreter_frame_initial_sp_offset * wordSize);
3825    const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
3826 
3827    Label allocated;
3828 
3829    // initialize entry pointer
3830    __ mv(c_rarg1, zr); // points to free slot or null
3831 
3832    // find a free slot in the monitor block (result in c_rarg1)
3833    {
3834      Label entry, loop, exit, notUsed;
3835      __ ld(c_rarg3, monitor_block_top); // derelativize pointer
3836      __ shadd(c_rarg3, c_rarg3, fp, c_rarg3, LogBytesPerWord);
3837      // Now c_rarg3 points to current entry, starting with top-most entry
3838 
3839      __ la(c_rarg2, monitor_block_bot); // points to word before bottom
3840 
3841      __ j(entry);
3842 
3843      __ bind(loop);
3844      // check if current entry is used
3845      // if not used then remember entry in c_rarg1
3846      __ ld(t0, Address(c_rarg3, BasicObjectLock::obj_offset()));
3847      __ bnez(t0, notUsed);
3848      __ mv(c_rarg1, c_rarg3);
3849      __ bind(notUsed);
3850      // check if current entry is for same object
3851      // if same object then stop searching
3852      __ beq(x10, t0, exit);
3853      // otherwise advance to next entry
3854      __ add(c_rarg3, c_rarg3, entry_size);
3855      __ bind(entry);
3856      // check if bottom reached
3857      // if not at bottom then check this entry
3858      __ bne(c_rarg3, c_rarg2, loop);
3859      __ bind(exit);
3860    }
3861 
3862    __ bnez(c_rarg1, allocated); // check if a slot has been found and
3863                              // if found, continue with that on
3864 
3865    // allocate one if there's no free slot
3866    {
3867      Label entry, loop;
3868      // 1. compute new pointers            // esp: old expression stack top
3869 
3870      __ check_extended_sp();
3871      __ sub(sp, sp, entry_size);           // make room for the monitor
3872      __ sub(t0, sp, fp);
3873      __ srai(t0, t0, Interpreter::logStackElementSize);
3874      __ sd(t0, Address(fp, frame::interpreter_frame_extended_sp_offset * wordSize));
3875 
3876      __ ld(c_rarg1, monitor_block_bot);    // derelativize pointer
3877      __ shadd(c_rarg1, c_rarg1, fp, c_rarg1, LogBytesPerWord);
3878      // Now c_rarg1 points to the old expression stack bottom
3879 
3880      __ sub(esp, esp, entry_size);         // move expression stack top
3881      __ sub(c_rarg1, c_rarg1, entry_size); // move expression stack bottom
3882      __ mv(c_rarg3, esp);                  // set start value for copy loop
3883      __ sub(t0, c_rarg1, fp);              // relativize pointer
3884      __ srai(t0, t0, Interpreter::logStackElementSize);
3885      __ sd(t0, monitor_block_bot);         // set new monitor block bottom
3886 
3887      __ j(entry);
3888      // 2. move expression stack contents
3889      __ bind(loop);
3890      __ ld(c_rarg2, Address(c_rarg3, entry_size)); // load expression stack
3891                                                    // word from old location
3892      __ sd(c_rarg2, Address(c_rarg3, 0));          // and store it at new location
3893      __ addi(c_rarg3, c_rarg3, wordSize);          // advance to next word
3894      __ bind(entry);
3895      __ bne(c_rarg3, c_rarg1, loop);    // check if bottom reached.if not at bottom
3896                                         // then copy next word
3897    }
3898 
3899    // call run-time routine
3900    // c_rarg1: points to monitor entry
3901    __ bind(allocated);
3902 
3903    // Increment bcp to point to the next bytecode, so exception
3904    // handling for async. exceptions work correctly.
3905    // The object has already been popped from the stack, so the
3906    // expression stack looks correct.
3907    __ addi(xbcp, xbcp, 1);
3908 
3909    // store object
3910    __ sd(x10, Address(c_rarg1, BasicObjectLock::obj_offset()));
3911    __ lock_object(c_rarg1);
3912 
3913    // check to make sure this monitor doesn't cause stack overflow after locking
3914    __ save_bcp();  // in case of exception
3915    __ generate_stack_overflow_check(0);
3916 
3917    // The bcp has already been incremented. Just need to dispatch to
3918    // next instruction.
3919    __ dispatch_next(vtos);
3920 }
3921 
3922 void TemplateTable::monitorexit() {
3923   transition(atos, vtos);
3924 
3925   // check for null object
3926   __ null_check(x10);
3927 
3928   const Address monitor_block_top(
3929         fp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
3930   const Address monitor_block_bot(
3931         fp, frame::interpreter_frame_initial_sp_offset * wordSize);
3932   const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
3933 
3934   Label found;
3935 
3936   // find matching slot
3937   {
3938     Label entry, loop;
3939     __ ld(c_rarg1, monitor_block_top); // derelativize pointer
3940     __ shadd(c_rarg1, c_rarg1, fp, c_rarg1, LogBytesPerWord);
3941     // Now c_rarg1 points to current entry, starting with top-most entry
3942 
3943     __ la(c_rarg2, monitor_block_bot); // points to word before bottom
3944                                         // of monitor block
3945     __ j(entry);
3946 
3947     __ bind(loop);
3948     // check if current entry is for same object
3949     __ ld(t0, Address(c_rarg1, BasicObjectLock::obj_offset()));
3950     // if same object then stop searching
3951     __ beq(x10, t0, found);
3952     // otherwise advance to next entry
3953     __ add(c_rarg1, c_rarg1, entry_size);
3954     __ bind(entry);
3955     // check if bottom reached
3956     // if not at bottom then check this entry
3957     __ bne(c_rarg1, c_rarg2, loop);
3958   }
3959 
3960   // error handling. Unlocking was not block-structured
3961   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
3962              InterpreterRuntime::throw_illegal_monitor_state_exception));
3963   __ should_not_reach_here();
3964 
3965   // call run-time routine
3966   __ bind(found);
3967   __ push_ptr(x10); // make sure object is on stack (contract with oopMaps)
3968   __ unlock_object(c_rarg1);
3969   __ pop_ptr(x10); // discard object
3970 }
3971 
3972 // Wide instructions
3973 void TemplateTable::wide() {
3974   __ load_unsigned_byte(x9, at_bcp(1));
3975   __ mv(t0, (address)Interpreter::_wentry_point);
3976   __ shadd(t0, x9, t0, t1, 3);
3977   __ ld(t1, Address(t0));
3978   __ jr(t1);
3979 }
3980 
3981 // Multi arrays
3982 void TemplateTable::multianewarray() {
3983   transition(vtos, atos);
3984   __ load_unsigned_byte(x10, at_bcp(3)); // get number of dimensions
3985   // last dim is on top of stack; we want address of first one:
3986   // first_addr = last_addr + (ndims - 1) * wordSize
3987   __ shadd(c_rarg1, x10, esp, c_rarg1, 3);
3988   __ subi(c_rarg1, c_rarg1, wordSize);
3989   call_VM(x10,
3990           CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray),
3991           c_rarg1);
3992   __ load_unsigned_byte(x11, at_bcp(3));
3993   __ shadd(esp, x11, esp, t0, 3);
3994 }