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