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