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