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