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