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