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