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