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