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