1 /*
   2  * Copyright (c) 2003, 2018, 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 = rscratch2;
 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, /*generate_poll*/true);
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, /*generate_poll*/true);
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, 0, /*generate_poll*/true);
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, 0, /*generate_poll*/true);
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, /*generate_poll*/true);
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, /*generate_poll*/true);
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, /*generate_poll*/true);
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, /*generate_poll*/true);
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     __ resolve_oop_handle(obj);
2301   }
2302 }
2303 
2304 void TemplateTable::load_invoke_cp_cache_entry(int byte_no,
2305                                                Register method,
2306                                                Register itable_index,
2307                                                Register flags,
2308                                                bool is_invokevirtual,
2309                                                bool is_invokevfinal, /*unused*/
2310                                                bool is_invokedynamic) {
2311   // setup registers
2312   const Register cache = rscratch2;
2313   const Register index = r4;
2314   assert_different_registers(method, flags);
2315   assert_different_registers(method, cache, index);
2316   assert_different_registers(itable_index, flags);
2317   assert_different_registers(itable_index, cache, index);
2318   // determine constant pool cache field offsets
2319   assert(is_invokevirtual == (byte_no == f2_byte), "is_invokevirtual flag redundant");
2320   const int method_offset = in_bytes(
2321     ConstantPoolCache::base_offset() +
2322       (is_invokevirtual
2323        ? ConstantPoolCacheEntry::f2_offset()
2324        : ConstantPoolCacheEntry::f1_offset()));
2325   const int flags_offset = in_bytes(ConstantPoolCache::base_offset() +
2326                                     ConstantPoolCacheEntry::flags_offset());
2327   // access constant pool cache fields
2328   const int index_offset = in_bytes(ConstantPoolCache::base_offset() +
2329                                     ConstantPoolCacheEntry::f2_offset());
2330 
2331   size_t index_size = (is_invokedynamic ? sizeof(u4) : sizeof(u2));
2332   resolve_cache_and_index(byte_no, cache, index, index_size);
2333   __ ldr(method, Address(cache, method_offset));
2334 
2335   if (itable_index != noreg) {
2336     __ ldr(itable_index, Address(cache, index_offset));
2337   }
2338   __ ldrw(flags, Address(cache, flags_offset));
2339 }
2340 
2341 
2342 // The registers cache and index expected to be set before call.
2343 // Correct values of the cache and index registers are preserved.
2344 void TemplateTable::jvmti_post_field_access(Register cache, Register index,
2345                                             bool is_static, bool has_tos) {
2346   // do the JVMTI work here to avoid disturbing the register state below
2347   // We use c_rarg registers here because we want to use the register used in
2348   // the call to the VM
2349   if (JvmtiExport::can_post_field_access()) {
2350     // Check to see if a field access watch has been set before we
2351     // take the time to call into the VM.
2352     Label L1;
2353     assert_different_registers(cache, index, r0);
2354     __ lea(rscratch1, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
2355     __ ldrw(r0, Address(rscratch1));
2356     __ cbzw(r0, L1);
2357 
2358     __ get_cache_and_index_at_bcp(c_rarg2, c_rarg3, 1);
2359     __ lea(c_rarg2, Address(c_rarg2, in_bytes(ConstantPoolCache::base_offset())));
2360 
2361     if (is_static) {
2362       __ mov(c_rarg1, zr); // NULL object reference
2363     } else {
2364       __ ldr(c_rarg1, at_tos()); // get object pointer without popping it
2365       __ verify_oop(c_rarg1);
2366     }
2367     // c_rarg1: object pointer or NULL
2368     // c_rarg2: cache entry pointer
2369     // c_rarg3: jvalue object on the stack
2370     __ call_VM(noreg, CAST_FROM_FN_PTR(address,
2371                                        InterpreterRuntime::post_field_access),
2372                c_rarg1, c_rarg2, c_rarg3);
2373     __ get_cache_and_index_at_bcp(cache, index, 1);
2374     __ bind(L1);
2375   }
2376 }
2377 
2378 void TemplateTable::pop_and_check_object(Register r)
2379 {
2380   __ pop_ptr(r);
2381   __ null_check(r);  // for field access must check obj.
2382   __ verify_oop(r);
2383 }
2384 
2385 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc)
2386 {
2387   const Register cache = r2;
2388   const Register index = r3;
2389   const Register obj   = r4;
2390   const Register off   = r19;
2391   const Register flags = r0;
2392   const Register raw_flags = r6;
2393   const Register bc    = r4; // uses same reg as obj, so don't mix them
2394 
2395   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
2396   jvmti_post_field_access(cache, index, is_static, false);
2397   load_field_cp_cache_entry(obj, cache, index, off, raw_flags, is_static);
2398 
2399   if (!is_static) {
2400     // obj is on the stack
2401     pop_and_check_object(obj);
2402   }
2403 
2404   // 8179954: We need to make sure that the code generated for
2405   // volatile accesses forms a sequentially-consistent set of
2406   // operations when combined with STLR and LDAR.  Without a leading
2407   // membar it's possible for a simple Dekker test to fail if loads
2408   // use LDR;DMB but stores use STLR.  This can happen if C2 compiles
2409   // the stores in one method and we interpret the loads in another.
2410   if (! UseBarriersForVolatile) {
2411     Label notVolatile;
2412     __ tbz(raw_flags, ConstantPoolCacheEntry::is_volatile_shift, notVolatile);
2413     __ membar(MacroAssembler::AnyAny);
2414     __ bind(notVolatile);
2415   }
2416 
2417   const Address field(obj, off);
2418 
2419   Label Done, notByte, notBool, notInt, notShort, notChar,
2420               notLong, notFloat, notObj, notDouble;
2421 
2422   // x86 uses a shift and mask or wings it with a shift plus assert
2423   // the mask is not needed. aarch64 just uses bitfield extract
2424   __ ubfxw(flags, raw_flags, ConstantPoolCacheEntry::tos_state_shift,
2425            ConstantPoolCacheEntry::tos_state_bits);
2426 
2427   assert(btos == 0, "change code, btos != 0");
2428   __ cbnz(flags, notByte);
2429 
2430   // Don't rewrite getstatic, only getfield
2431   if (is_static) rc = may_not_rewrite;
2432 
2433   // btos
2434   __ load_signed_byte(r0, field);
2435   __ push(btos);
2436   // Rewrite bytecode to be faster
2437   if (rc == may_rewrite) {
2438     patch_bytecode(Bytecodes::_fast_bgetfield, bc, r1);
2439   }
2440   __ b(Done);
2441 
2442   __ bind(notByte);
2443   __ cmp(flags, ztos);
2444   __ br(Assembler::NE, notBool);
2445 
2446   // ztos (same code as btos)
2447   __ ldrsb(r0, field);
2448   __ push(ztos);
2449   // Rewrite bytecode to be faster
2450   if (rc == may_rewrite) {
2451     // use btos rewriting, no truncating to t/f bit is needed for getfield.
2452     patch_bytecode(Bytecodes::_fast_bgetfield, bc, r1);
2453   }
2454   __ b(Done);
2455 
2456   __ bind(notBool);
2457   __ cmp(flags, atos);
2458   __ br(Assembler::NE, notObj);
2459   // atos
2460   __ load_heap_oop(r0, field);
2461   __ push(atos);
2462   if (rc == may_rewrite) {
2463     patch_bytecode(Bytecodes::_fast_agetfield, bc, r1);
2464   }
2465   __ b(Done);
2466 
2467   __ bind(notObj);
2468   __ cmp(flags, itos);
2469   __ br(Assembler::NE, notInt);
2470   // itos
2471   __ ldrw(r0, field);
2472   __ push(itos);
2473   // Rewrite bytecode to be faster
2474   if (rc == may_rewrite) {
2475     patch_bytecode(Bytecodes::_fast_igetfield, bc, r1);
2476   }
2477   __ b(Done);
2478 
2479   __ bind(notInt);
2480   __ cmp(flags, ctos);
2481   __ br(Assembler::NE, notChar);
2482   // ctos
2483   __ load_unsigned_short(r0, field);
2484   __ push(ctos);
2485   // Rewrite bytecode to be faster
2486   if (rc == may_rewrite) {
2487     patch_bytecode(Bytecodes::_fast_cgetfield, bc, r1);
2488   }
2489   __ b(Done);
2490 
2491   __ bind(notChar);
2492   __ cmp(flags, stos);
2493   __ br(Assembler::NE, notShort);
2494   // stos
2495   __ load_signed_short(r0, field);
2496   __ push(stos);
2497   // Rewrite bytecode to be faster
2498   if (rc == may_rewrite) {
2499     patch_bytecode(Bytecodes::_fast_sgetfield, bc, r1);
2500   }
2501   __ b(Done);
2502 
2503   __ bind(notShort);
2504   __ cmp(flags, ltos);
2505   __ br(Assembler::NE, notLong);
2506   // ltos
2507   __ ldr(r0, field);
2508   __ push(ltos);
2509   // Rewrite bytecode to be faster
2510   if (rc == may_rewrite) {
2511     patch_bytecode(Bytecodes::_fast_lgetfield, bc, r1);
2512   }
2513   __ b(Done);
2514 
2515   __ bind(notLong);
2516   __ cmp(flags, ftos);
2517   __ br(Assembler::NE, notFloat);
2518   // ftos
2519   __ ldrs(v0, field);
2520   __ push(ftos);
2521   // Rewrite bytecode to be faster
2522   if (rc == may_rewrite) {
2523     patch_bytecode(Bytecodes::_fast_fgetfield, bc, r1);
2524   }
2525   __ b(Done);
2526 
2527   __ bind(notFloat);
2528 #ifdef ASSERT
2529   __ cmp(flags, dtos);
2530   __ br(Assembler::NE, notDouble);
2531 #endif
2532   // dtos
2533   __ ldrd(v0, field);
2534   __ push(dtos);
2535   // Rewrite bytecode to be faster
2536   if (rc == may_rewrite) {
2537     patch_bytecode(Bytecodes::_fast_dgetfield, bc, r1);
2538   }
2539 #ifdef ASSERT
2540   __ b(Done);
2541 
2542   __ bind(notDouble);
2543   __ stop("Bad state");
2544 #endif
2545 
2546   __ bind(Done);
2547 
2548   Label notVolatile;
2549   __ tbz(raw_flags, ConstantPoolCacheEntry::is_volatile_shift, notVolatile);
2550   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
2551   __ bind(notVolatile);
2552 }
2553 
2554 
2555 void TemplateTable::getfield(int byte_no)
2556 {
2557   getfield_or_static(byte_no, false);
2558 }
2559 
2560 void TemplateTable::nofast_getfield(int byte_no) {
2561   getfield_or_static(byte_no, false, may_not_rewrite);
2562 }
2563 
2564 void TemplateTable::getstatic(int byte_no)
2565 {
2566   getfield_or_static(byte_no, true);
2567 }
2568 
2569 // The registers cache and index expected to be set before call.
2570 // The function may destroy various registers, just not the cache and index registers.
2571 void TemplateTable::jvmti_post_field_mod(Register cache, Register index, bool is_static) {
2572   transition(vtos, vtos);
2573 
2574   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2575 
2576   if (JvmtiExport::can_post_field_modification()) {
2577     // Check to see if a field modification watch has been set before
2578     // we take the time to call into the VM.
2579     Label L1;
2580     assert_different_registers(cache, index, r0);
2581     __ lea(rscratch1, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
2582     __ ldrw(r0, Address(rscratch1));
2583     __ cbz(r0, L1);
2584 
2585     __ get_cache_and_index_at_bcp(c_rarg2, rscratch1, 1);
2586 
2587     if (is_static) {
2588       // Life is simple.  Null out the object pointer.
2589       __ mov(c_rarg1, zr);
2590     } else {
2591       // Life is harder. The stack holds the value on top, followed by
2592       // the object.  We don't know the size of the value, though; it
2593       // could be one or two words depending on its type. As a result,
2594       // we must find the type to determine where the object is.
2595       __ ldrw(c_rarg3, Address(c_rarg2,
2596                                in_bytes(cp_base_offset +
2597                                         ConstantPoolCacheEntry::flags_offset())));
2598       __ lsr(c_rarg3, c_rarg3,
2599              ConstantPoolCacheEntry::tos_state_shift);
2600       ConstantPoolCacheEntry::verify_tos_state_shift();
2601       Label nope2, done, ok;
2602       __ ldr(c_rarg1, at_tos_p1());  // initially assume a one word jvalue
2603       __ cmpw(c_rarg3, ltos);
2604       __ br(Assembler::EQ, ok);
2605       __ cmpw(c_rarg3, dtos);
2606       __ br(Assembler::NE, nope2);
2607       __ bind(ok);
2608       __ ldr(c_rarg1, at_tos_p2()); // ltos (two word jvalue)
2609       __ bind(nope2);
2610     }
2611     // cache entry pointer
2612     __ add(c_rarg2, c_rarg2, in_bytes(cp_base_offset));
2613     // object (tos)
2614     __ mov(c_rarg3, esp);
2615     // c_rarg1: object pointer set up above (NULL if static)
2616     // c_rarg2: cache entry pointer
2617     // c_rarg3: jvalue object on the stack
2618     __ call_VM(noreg,
2619                CAST_FROM_FN_PTR(address,
2620                                 InterpreterRuntime::post_field_modification),
2621                c_rarg1, c_rarg2, c_rarg3);
2622     __ get_cache_and_index_at_bcp(cache, index, 1);
2623     __ bind(L1);
2624   }
2625 }
2626 
2627 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2628   transition(vtos, vtos);
2629 
2630   const Register cache = r2;
2631   const Register index = r3;
2632   const Register obj   = r2;
2633   const Register off   = r19;
2634   const Register flags = r0;
2635   const Register bc    = r4;
2636 
2637   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
2638   jvmti_post_field_mod(cache, index, is_static);
2639   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
2640 
2641   Label Done;
2642   __ mov(r5, flags);
2643 
2644   {
2645     Label notVolatile;
2646     __ tbz(r5, ConstantPoolCacheEntry::is_volatile_shift, notVolatile);
2647     __ membar(MacroAssembler::StoreStore);
2648     __ bind(notVolatile);
2649   }
2650 
2651   // field address
2652   const Address field(obj, off);
2653 
2654   Label notByte, notBool, notInt, notShort, notChar,
2655         notLong, notFloat, notObj, notDouble;
2656 
2657   // x86 uses a shift and mask or wings it with a shift plus assert
2658   // the mask is not needed. aarch64 just uses bitfield extract
2659   __ ubfxw(flags, flags, ConstantPoolCacheEntry::tos_state_shift,  ConstantPoolCacheEntry::tos_state_bits);
2660 
2661   assert(btos == 0, "change code, btos != 0");
2662   __ cbnz(flags, notByte);
2663 
2664   // Don't rewrite putstatic, only putfield
2665   if (is_static) rc = may_not_rewrite;
2666 
2667   // btos
2668   {
2669     __ pop(btos);
2670     if (!is_static) pop_and_check_object(obj);
2671     __ strb(r0, field);
2672     if (rc == may_rewrite) {
2673       patch_bytecode(Bytecodes::_fast_bputfield, bc, r1, true, byte_no);
2674     }
2675     __ b(Done);
2676   }
2677 
2678   __ bind(notByte);
2679   __ cmp(flags, ztos);
2680   __ br(Assembler::NE, notBool);
2681 
2682   // ztos
2683   {
2684     __ pop(ztos);
2685     if (!is_static) pop_and_check_object(obj);
2686     __ andw(r0, r0, 0x1);
2687     __ strb(r0, field);
2688     if (rc == may_rewrite) {
2689       patch_bytecode(Bytecodes::_fast_zputfield, bc, r1, true, byte_no);
2690     }
2691     __ b(Done);
2692   }
2693 
2694   __ bind(notBool);
2695   __ cmp(flags, atos);
2696   __ br(Assembler::NE, notObj);
2697 
2698   // atos
2699   {
2700     __ pop(atos);
2701     if (!is_static) pop_and_check_object(obj);
2702     // Store into the field
2703     do_oop_store(_masm, field, r0, _bs->kind(), false);
2704     if (rc == may_rewrite) {
2705       patch_bytecode(Bytecodes::_fast_aputfield, bc, r1, true, byte_no);
2706     }
2707     __ b(Done);
2708   }
2709 
2710   __ bind(notObj);
2711   __ cmp(flags, itos);
2712   __ br(Assembler::NE, notInt);
2713 
2714   // itos
2715   {
2716     __ pop(itos);
2717     if (!is_static) pop_and_check_object(obj);
2718     __ strw(r0, field);
2719     if (rc == may_rewrite) {
2720       patch_bytecode(Bytecodes::_fast_iputfield, bc, r1, true, byte_no);
2721     }
2722     __ b(Done);
2723   }
2724 
2725   __ bind(notInt);
2726   __ cmp(flags, ctos);
2727   __ br(Assembler::NE, notChar);
2728 
2729   // ctos
2730   {
2731     __ pop(ctos);
2732     if (!is_static) pop_and_check_object(obj);
2733     __ strh(r0, field);
2734     if (rc == may_rewrite) {
2735       patch_bytecode(Bytecodes::_fast_cputfield, bc, r1, true, byte_no);
2736     }
2737     __ b(Done);
2738   }
2739 
2740   __ bind(notChar);
2741   __ cmp(flags, stos);
2742   __ br(Assembler::NE, notShort);
2743 
2744   // stos
2745   {
2746     __ pop(stos);
2747     if (!is_static) pop_and_check_object(obj);
2748     __ strh(r0, field);
2749     if (rc == may_rewrite) {
2750       patch_bytecode(Bytecodes::_fast_sputfield, bc, r1, true, byte_no);
2751     }
2752     __ b(Done);
2753   }
2754 
2755   __ bind(notShort);
2756   __ cmp(flags, ltos);
2757   __ br(Assembler::NE, notLong);
2758 
2759   // ltos
2760   {
2761     __ pop(ltos);
2762     if (!is_static) pop_and_check_object(obj);
2763     __ str(r0, field);
2764     if (rc == may_rewrite) {
2765       patch_bytecode(Bytecodes::_fast_lputfield, bc, r1, true, byte_no);
2766     }
2767     __ b(Done);
2768   }
2769 
2770   __ bind(notLong);
2771   __ cmp(flags, ftos);
2772   __ br(Assembler::NE, notFloat);
2773 
2774   // ftos
2775   {
2776     __ pop(ftos);
2777     if (!is_static) pop_and_check_object(obj);
2778     __ strs(v0, field);
2779     if (rc == may_rewrite) {
2780       patch_bytecode(Bytecodes::_fast_fputfield, bc, r1, true, byte_no);
2781     }
2782     __ b(Done);
2783   }
2784 
2785   __ bind(notFloat);
2786 #ifdef ASSERT
2787   __ cmp(flags, dtos);
2788   __ br(Assembler::NE, notDouble);
2789 #endif
2790 
2791   // dtos
2792   {
2793     __ pop(dtos);
2794     if (!is_static) pop_and_check_object(obj);
2795     __ strd(v0, field);
2796     if (rc == may_rewrite) {
2797       patch_bytecode(Bytecodes::_fast_dputfield, bc, r1, true, byte_no);
2798     }
2799   }
2800 
2801 #ifdef ASSERT
2802   __ b(Done);
2803 
2804   __ bind(notDouble);
2805   __ stop("Bad state");
2806 #endif
2807 
2808   __ bind(Done);
2809 
2810   {
2811     Label notVolatile;
2812     __ tbz(r5, ConstantPoolCacheEntry::is_volatile_shift, notVolatile);
2813     __ membar(MacroAssembler::StoreLoad);
2814     __ bind(notVolatile);
2815   }
2816 }
2817 
2818 void TemplateTable::putfield(int byte_no)
2819 {
2820   putfield_or_static(byte_no, false);
2821 }
2822 
2823 void TemplateTable::nofast_putfield(int byte_no) {
2824   putfield_or_static(byte_no, false, may_not_rewrite);
2825 }
2826 
2827 void TemplateTable::putstatic(int byte_no) {
2828   putfield_or_static(byte_no, true);
2829 }
2830 
2831 void TemplateTable::jvmti_post_fast_field_mod()
2832 {
2833   if (JvmtiExport::can_post_field_modification()) {
2834     // Check to see if a field modification watch has been set before
2835     // we take the time to call into the VM.
2836     Label L2;
2837     __ lea(rscratch1, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
2838     __ ldrw(c_rarg3, Address(rscratch1));
2839     __ cbzw(c_rarg3, L2);
2840     __ pop_ptr(r19);                  // copy the object pointer from tos
2841     __ verify_oop(r19);
2842     __ push_ptr(r19);                 // put the object pointer back on tos
2843     // Save tos values before call_VM() clobbers them. Since we have
2844     // to do it for every data type, we use the saved values as the
2845     // jvalue object.
2846     switch (bytecode()) {          // load values into the jvalue object
2847     case Bytecodes::_fast_aputfield: __ push_ptr(r0); break;
2848     case Bytecodes::_fast_bputfield: // fall through
2849     case Bytecodes::_fast_zputfield: // fall through
2850     case Bytecodes::_fast_sputfield: // fall through
2851     case Bytecodes::_fast_cputfield: // fall through
2852     case Bytecodes::_fast_iputfield: __ push_i(r0); break;
2853     case Bytecodes::_fast_dputfield: __ push_d(); break;
2854     case Bytecodes::_fast_fputfield: __ push_f(); break;
2855     case Bytecodes::_fast_lputfield: __ push_l(r0); break;
2856 
2857     default:
2858       ShouldNotReachHere();
2859     }
2860     __ mov(c_rarg3, esp);             // points to jvalue on the stack
2861     // access constant pool cache entry
2862     __ get_cache_entry_pointer_at_bcp(c_rarg2, r0, 1);
2863     __ verify_oop(r19);
2864     // r19: object pointer copied above
2865     // c_rarg2: cache entry pointer
2866     // c_rarg3: jvalue object on the stack
2867     __ call_VM(noreg,
2868                CAST_FROM_FN_PTR(address,
2869                                 InterpreterRuntime::post_field_modification),
2870                r19, c_rarg2, c_rarg3);
2871 
2872     switch (bytecode()) {             // restore tos values
2873     case Bytecodes::_fast_aputfield: __ pop_ptr(r0); break;
2874     case Bytecodes::_fast_bputfield: // fall through
2875     case Bytecodes::_fast_zputfield: // fall through
2876     case Bytecodes::_fast_sputfield: // fall through
2877     case Bytecodes::_fast_cputfield: // fall through
2878     case Bytecodes::_fast_iputfield: __ pop_i(r0); break;
2879     case Bytecodes::_fast_dputfield: __ pop_d(); break;
2880     case Bytecodes::_fast_fputfield: __ pop_f(); break;
2881     case Bytecodes::_fast_lputfield: __ pop_l(r0); break;
2882     }
2883     __ bind(L2);
2884   }
2885 }
2886 
2887 void TemplateTable::fast_storefield(TosState state)
2888 {
2889   transition(state, vtos);
2890 
2891   ByteSize base = ConstantPoolCache::base_offset();
2892 
2893   jvmti_post_fast_field_mod();
2894 
2895   // access constant pool cache
2896   __ get_cache_and_index_at_bcp(r2, r1, 1);
2897 
2898   // test for volatile with r3
2899   __ ldrw(r3, Address(r2, in_bytes(base +
2900                                    ConstantPoolCacheEntry::flags_offset())));
2901 
2902   // replace index with field offset from cache entry
2903   __ ldr(r1, Address(r2, in_bytes(base + ConstantPoolCacheEntry::f2_offset())));
2904 
2905   {
2906     Label notVolatile;
2907     __ tbz(r3, ConstantPoolCacheEntry::is_volatile_shift, notVolatile);
2908     __ membar(MacroAssembler::StoreStore);
2909     __ bind(notVolatile);
2910   }
2911 
2912   Label notVolatile;
2913 
2914   // Get object from stack
2915   pop_and_check_object(r2);
2916 
2917   // field address
2918   const Address field(r2, r1);
2919 
2920   // access field
2921   switch (bytecode()) {
2922   case Bytecodes::_fast_aputfield:
2923     do_oop_store(_masm, field, r0, _bs->kind(), false);
2924     break;
2925   case Bytecodes::_fast_lputfield:
2926     __ str(r0, field);
2927     break;
2928   case Bytecodes::_fast_iputfield:
2929     __ strw(r0, field);
2930     break;
2931   case Bytecodes::_fast_zputfield:
2932     __ andw(r0, r0, 0x1);  // boolean is true if LSB is 1
2933     // fall through to bputfield
2934   case Bytecodes::_fast_bputfield:
2935     __ strb(r0, field);
2936     break;
2937   case Bytecodes::_fast_sputfield:
2938     // fall through
2939   case Bytecodes::_fast_cputfield:
2940     __ strh(r0, field);
2941     break;
2942   case Bytecodes::_fast_fputfield:
2943     __ strs(v0, field);
2944     break;
2945   case Bytecodes::_fast_dputfield:
2946     __ strd(v0, field);
2947     break;
2948   default:
2949     ShouldNotReachHere();
2950   }
2951 
2952   {
2953     Label notVolatile;
2954     __ tbz(r3, ConstantPoolCacheEntry::is_volatile_shift, notVolatile);
2955     __ membar(MacroAssembler::StoreLoad);
2956     __ bind(notVolatile);
2957   }
2958 }
2959 
2960 
2961 void TemplateTable::fast_accessfield(TosState state)
2962 {
2963   transition(atos, state);
2964   // Do the JVMTI work here to avoid disturbing the register state below
2965   if (JvmtiExport::can_post_field_access()) {
2966     // Check to see if a field access watch has been set before we
2967     // take the time to call into the VM.
2968     Label L1;
2969     __ lea(rscratch1, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
2970     __ ldrw(r2, Address(rscratch1));
2971     __ cbzw(r2, L1);
2972     // access constant pool cache entry
2973     __ get_cache_entry_pointer_at_bcp(c_rarg2, rscratch2, 1);
2974     __ verify_oop(r0);
2975     __ push_ptr(r0);  // save object pointer before call_VM() clobbers it
2976     __ mov(c_rarg1, r0);
2977     // c_rarg1: object pointer copied above
2978     // c_rarg2: cache entry pointer
2979     __ call_VM(noreg,
2980                CAST_FROM_FN_PTR(address,
2981                                 InterpreterRuntime::post_field_access),
2982                c_rarg1, c_rarg2);
2983     __ pop_ptr(r0); // restore object pointer
2984     __ bind(L1);
2985   }
2986 
2987   // access constant pool cache
2988   __ get_cache_and_index_at_bcp(r2, r1, 1);
2989   __ ldr(r1, Address(r2, in_bytes(ConstantPoolCache::base_offset() +
2990                                   ConstantPoolCacheEntry::f2_offset())));
2991   __ ldrw(r3, Address(r2, in_bytes(ConstantPoolCache::base_offset() +
2992                                    ConstantPoolCacheEntry::flags_offset())));
2993 
2994   // r0: object
2995   __ verify_oop(r0);
2996   __ null_check(r0);
2997   const Address field(r0, r1);
2998 
2999   // 8179954: We need to make sure that the code generated for
3000   // volatile accesses forms a sequentially-consistent set of
3001   // operations when combined with STLR and LDAR.  Without a leading
3002   // membar it's possible for a simple Dekker test to fail if loads
3003   // use LDR;DMB but stores use STLR.  This can happen if C2 compiles
3004   // the stores in one method and we interpret the loads in another.
3005   if (! UseBarriersForVolatile) {
3006     Label notVolatile;
3007     __ tbz(r3, ConstantPoolCacheEntry::is_volatile_shift, notVolatile);
3008     __ membar(MacroAssembler::AnyAny);
3009     __ bind(notVolatile);
3010   }
3011 
3012   // access field
3013   switch (bytecode()) {
3014   case Bytecodes::_fast_agetfield:
3015     __ load_heap_oop(r0, field);
3016     __ verify_oop(r0);
3017     break;
3018   case Bytecodes::_fast_lgetfield:
3019     __ ldr(r0, field);
3020     break;
3021   case Bytecodes::_fast_igetfield:
3022     __ ldrw(r0, field);
3023     break;
3024   case Bytecodes::_fast_bgetfield:
3025     __ load_signed_byte(r0, field);
3026     break;
3027   case Bytecodes::_fast_sgetfield:
3028     __ load_signed_short(r0, field);
3029     break;
3030   case Bytecodes::_fast_cgetfield:
3031     __ load_unsigned_short(r0, field);
3032     break;
3033   case Bytecodes::_fast_fgetfield:
3034     __ ldrs(v0, field);
3035     break;
3036   case Bytecodes::_fast_dgetfield:
3037     __ ldrd(v0, field);
3038     break;
3039   default:
3040     ShouldNotReachHere();
3041   }
3042   {
3043     Label notVolatile;
3044     __ tbz(r3, ConstantPoolCacheEntry::is_volatile_shift, notVolatile);
3045     __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
3046     __ bind(notVolatile);
3047   }
3048 }
3049 
3050 void TemplateTable::fast_xaccess(TosState state)
3051 {
3052   transition(vtos, state);
3053 
3054   // get receiver
3055   __ ldr(r0, aaddress(0));
3056   // access constant pool cache
3057   __ get_cache_and_index_at_bcp(r2, r3, 2);
3058   __ ldr(r1, Address(r2, in_bytes(ConstantPoolCache::base_offset() +
3059                                   ConstantPoolCacheEntry::f2_offset())));
3060 
3061   // 8179954: We need to make sure that the code generated for
3062   // volatile accesses forms a sequentially-consistent set of
3063   // operations when combined with STLR and LDAR.  Without a leading
3064   // membar it's possible for a simple Dekker test to fail if loads
3065   // use LDR;DMB but stores use STLR.  This can happen if C2 compiles
3066   // the stores in one method and we interpret the loads in another.
3067   if (! UseBarriersForVolatile) {
3068     Label notVolatile;
3069     __ ldrw(r3, Address(r2, in_bytes(ConstantPoolCache::base_offset() +
3070                                      ConstantPoolCacheEntry::flags_offset())));
3071     __ tbz(r3, ConstantPoolCacheEntry::is_volatile_shift, notVolatile);
3072     __ membar(MacroAssembler::AnyAny);
3073     __ bind(notVolatile);
3074   }
3075 
3076   // make sure exception is reported in correct bcp range (getfield is
3077   // next instruction)
3078   __ increment(rbcp);
3079   __ null_check(r0);
3080   switch (state) {
3081   case itos:
3082     __ ldrw(r0, Address(r0, r1, Address::lsl(0)));
3083     break;
3084   case atos:
3085     __ load_heap_oop(r0, Address(r0, r1, Address::lsl(0)));
3086     __ verify_oop(r0);
3087     break;
3088   case ftos:
3089     __ ldrs(v0, Address(r0, r1, Address::lsl(0)));
3090     break;
3091   default:
3092     ShouldNotReachHere();
3093   }
3094 
3095   {
3096     Label notVolatile;
3097     __ ldrw(r3, Address(r2, in_bytes(ConstantPoolCache::base_offset() +
3098                                      ConstantPoolCacheEntry::flags_offset())));
3099     __ tbz(r3, ConstantPoolCacheEntry::is_volatile_shift, notVolatile);
3100     __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
3101     __ bind(notVolatile);
3102   }
3103 
3104   __ decrement(rbcp);
3105 }
3106 
3107 
3108 
3109 //-----------------------------------------------------------------------------
3110 // Calls
3111 
3112 void TemplateTable::count_calls(Register method, Register temp)
3113 {
3114   __ call_Unimplemented();
3115 }
3116 
3117 void TemplateTable::prepare_invoke(int byte_no,
3118                                    Register method, // linked method (or i-klass)
3119                                    Register index,  // itable index, MethodType, etc.
3120                                    Register recv,   // if caller wants to see it
3121                                    Register flags   // if caller wants to test it
3122                                    ) {
3123   // determine flags
3124   Bytecodes::Code code = bytecode();
3125   const bool is_invokeinterface  = code == Bytecodes::_invokeinterface;
3126   const bool is_invokedynamic    = code == Bytecodes::_invokedynamic;
3127   const bool is_invokehandle     = code == Bytecodes::_invokehandle;
3128   const bool is_invokevirtual    = code == Bytecodes::_invokevirtual;
3129   const bool is_invokespecial    = code == Bytecodes::_invokespecial;
3130   const bool load_receiver       = (recv  != noreg);
3131   const bool save_flags          = (flags != noreg);
3132   assert(load_receiver == (code != Bytecodes::_invokestatic && code != Bytecodes::_invokedynamic), "");
3133   assert(save_flags    == (is_invokeinterface || is_invokevirtual), "need flags for vfinal");
3134   assert(flags == noreg || flags == r3, "");
3135   assert(recv  == noreg || recv  == r2, "");
3136 
3137   // setup registers & access constant pool cache
3138   if (recv  == noreg)  recv  = r2;
3139   if (flags == noreg)  flags = r3;
3140   assert_different_registers(method, index, recv, flags);
3141 
3142   // save 'interpreter return address'
3143   __ save_bcp();
3144 
3145   load_invoke_cp_cache_entry(byte_no, method, index, flags, is_invokevirtual, false, is_invokedynamic);
3146 
3147   // maybe push appendix to arguments (just before return address)
3148   if (is_invokedynamic || is_invokehandle) {
3149     Label L_no_push;
3150     __ tbz(flags, ConstantPoolCacheEntry::has_appendix_shift, L_no_push);
3151     // Push the appendix as a trailing parameter.
3152     // This must be done before we get the receiver,
3153     // since the parameter_size includes it.
3154     __ push(r19);
3155     __ mov(r19, index);
3156     assert(ConstantPoolCacheEntry::_indy_resolved_references_appendix_offset == 0, "appendix expected at index+0");
3157     __ load_resolved_reference_at_index(index, r19);
3158     __ pop(r19);
3159     __ push(index);  // push appendix (MethodType, CallSite, etc.)
3160     __ bind(L_no_push);
3161   }
3162 
3163   // load receiver if needed (note: no return address pushed yet)
3164   if (load_receiver) {
3165     __ andw(recv, flags, ConstantPoolCacheEntry::parameter_size_mask);
3166     // FIXME -- is this actually correct? looks like it should be 2
3167     // const int no_return_pc_pushed_yet = -1;  // argument slot correction before we push return address
3168     // const int receiver_is_at_end      = -1;  // back off one slot to get receiver
3169     // Address recv_addr = __ argument_address(recv, no_return_pc_pushed_yet + receiver_is_at_end);
3170     // __ movptr(recv, recv_addr);
3171     __ add(rscratch1, esp, recv, ext::uxtx, 3); // FIXME: uxtb here?
3172     __ ldr(recv, Address(rscratch1, -Interpreter::expr_offset_in_bytes(1)));
3173     __ verify_oop(recv);
3174   }
3175 
3176   // compute return type
3177   // x86 uses a shift and mask or wings it with a shift plus assert
3178   // the mask is not needed. aarch64 just uses bitfield extract
3179   __ ubfxw(rscratch2, flags, ConstantPoolCacheEntry::tos_state_shift,  ConstantPoolCacheEntry::tos_state_bits);
3180   // load return address
3181   {
3182     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
3183     __ mov(rscratch1, table_addr);
3184     __ ldr(lr, Address(rscratch1, rscratch2, Address::lsl(3)));
3185   }
3186 }
3187 
3188 
3189 void TemplateTable::invokevirtual_helper(Register index,
3190                                          Register recv,
3191                                          Register flags)
3192 {
3193   // Uses temporary registers r0, r3
3194   assert_different_registers(index, recv, r0, r3);
3195   // Test for an invoke of a final method
3196   Label notFinal;
3197   __ tbz(flags, ConstantPoolCacheEntry::is_vfinal_shift, notFinal);
3198 
3199   const Register method = index;  // method must be rmethod
3200   assert(method == rmethod,
3201          "methodOop must be rmethod for interpreter calling convention");
3202 
3203   // do the call - the index is actually the method to call
3204   // that is, f2 is a vtable index if !is_vfinal, else f2 is a Method*
3205 
3206   // It's final, need a null check here!
3207   __ null_check(recv);
3208 
3209   // profile this call
3210   __ profile_final_call(r0);
3211   __ profile_arguments_type(r0, method, r4, true);
3212 
3213   __ jump_from_interpreted(method, r0);
3214 
3215   __ bind(notFinal);
3216 
3217   // get receiver klass
3218   __ null_check(recv, oopDesc::klass_offset_in_bytes());
3219   __ load_klass(r0, recv);
3220 
3221   // profile this call
3222   __ profile_virtual_call(r0, rlocals, r3);
3223 
3224   // get target methodOop & entry point
3225   __ lookup_virtual_method(r0, index, method);
3226   __ profile_arguments_type(r3, method, r4, true);
3227   // FIXME -- this looks completely redundant. is it?
3228   // __ ldr(r3, Address(method, Method::interpreter_entry_offset()));
3229   __ jump_from_interpreted(method, r3);
3230 }
3231 
3232 void TemplateTable::invokevirtual(int byte_no)
3233 {
3234   transition(vtos, vtos);
3235   assert(byte_no == f2_byte, "use this argument");
3236 
3237   prepare_invoke(byte_no, rmethod, noreg, r2, r3);
3238 
3239   // rmethod: index (actually a Method*)
3240   // r2: receiver
3241   // r3: flags
3242 
3243   invokevirtual_helper(rmethod, r2, r3);
3244 }
3245 
3246 void TemplateTable::invokespecial(int byte_no)
3247 {
3248   transition(vtos, vtos);
3249   assert(byte_no == f1_byte, "use this argument");
3250 
3251   prepare_invoke(byte_no, rmethod, noreg,  // get f1 Method*
3252                  r2);  // get receiver also for null check
3253   __ verify_oop(r2);
3254   __ null_check(r2);
3255   // do the call
3256   __ profile_call(r0);
3257   __ profile_arguments_type(r0, rmethod, rbcp, false);
3258   __ jump_from_interpreted(rmethod, r0);
3259 }
3260 
3261 void TemplateTable::invokestatic(int byte_no)
3262 {
3263   transition(vtos, vtos);
3264   assert(byte_no == f1_byte, "use this argument");
3265 
3266   prepare_invoke(byte_no, rmethod);  // get f1 Method*
3267   // do the call
3268   __ profile_call(r0);
3269   __ profile_arguments_type(r0, rmethod, r4, false);
3270   __ jump_from_interpreted(rmethod, r0);
3271 }
3272 
3273 void TemplateTable::fast_invokevfinal(int byte_no)
3274 {
3275   __ call_Unimplemented();
3276 }
3277 
3278 void TemplateTable::invokeinterface(int byte_no) {
3279   transition(vtos, vtos);
3280   assert(byte_no == f1_byte, "use this argument");
3281 
3282   prepare_invoke(byte_no, r0, rmethod,  // get f1 Klass*, f2 itable index
3283                  r2, r3); // recv, flags
3284 
3285   // r0: interface klass (from f1)
3286   // rmethod: itable index (from f2) or Method*
3287   // r2: receiver
3288   // r3: flags
3289 
3290   // Check for private method invocation - indicated by vfinal
3291   Label notVFinal;
3292   __ tbz(flags, ConstantPoolCacheEntry::is_vfinal_shift, notVFinal);
3293 
3294   // do the call - the index is actually the method to call
3295 
3296   __ null_check(r2);
3297 
3298   __ profile_final_call(r0);
3299   __ profile_arguments_type(r0, rmethod, r4, true);
3300   __ jump_from_interpreted(rmethod, r0);
3301 
3302   __ bind(notVFinal);
3303 
3304   // Special case of invokeinterface called for virtual method of
3305   // java.lang.Object.  See cpCacheOop.cpp for details.
3306   // This code isn't produced by javac, but could be produced by
3307   // another compliant java compiler.
3308   Label notMethod;
3309   __ tbz(r3, ConstantPoolCacheEntry::is_forced_virtual_shift, notMethod);
3310 
3311   invokevirtual_helper(rmethod, r2, r3);
3312   __ bind(notMethod);
3313 
3314   // Get receiver klass into r3 - also a null check
3315   __ restore_locals();
3316   __ null_check(r2, oopDesc::klass_offset_in_bytes());
3317   __ load_klass(r3, r2);
3318 
3319   // profile this call
3320   __ profile_virtual_call(r3, r13, r19);
3321 
3322   Label no_such_interface, no_such_method;
3323 
3324   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3325                              r3, r0, rmethod,
3326                              // outputs: method, scan temp. reg
3327                              rmethod, r13,
3328                              no_such_interface);
3329 
3330   // rmethod,: methodOop to call
3331   // r2: receiver
3332   // Check for abstract method error
3333   // Note: This should be done more efficiently via a throw_abstract_method_error
3334   //       interpreter entry point and a conditional jump to it in case of a null
3335   //       method.
3336   __ cbz(rmethod, no_such_method);
3337 
3338   __ profile_arguments_type(r3, rmethod, r13, true);
3339 
3340   // do the call
3341   // r2: receiver
3342   // rmethod,: methodOop
3343   __ jump_from_interpreted(rmethod, r3);
3344   __ should_not_reach_here();
3345 
3346   // exception handling code follows...
3347   // note: must restore interpreter registers to canonical
3348   //       state for exception handling to work correctly!
3349 
3350   __ bind(no_such_method);
3351   // throw exception
3352   __ restore_bcp();      // bcp must be correct for exception handler   (was destroyed)
3353   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3354   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodError));
3355   // the call_VM checks for exception, so we should never return here.
3356   __ should_not_reach_here();
3357 
3358   __ bind(no_such_interface);
3359   // throw exception
3360   __ restore_bcp();      // bcp must be correct for exception handler   (was destroyed)
3361   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3362   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
3363                    InterpreterRuntime::throw_IncompatibleClassChangeError));
3364   // the call_VM checks for exception, so we should never return here.
3365   __ should_not_reach_here();
3366   return;
3367 }
3368 
3369 void TemplateTable::invokehandle(int byte_no) {
3370   transition(vtos, vtos);
3371   assert(byte_no == f1_byte, "use this argument");
3372 
3373   prepare_invoke(byte_no, rmethod, r0, r2);
3374   __ verify_method_ptr(r2);
3375   __ verify_oop(r2);
3376   __ null_check(r2);
3377 
3378   // FIXME: profile the LambdaForm also
3379 
3380   // r13 is safe to use here as a scratch reg because it is about to
3381   // be clobbered by jump_from_interpreted().
3382   __ profile_final_call(r13);
3383   __ profile_arguments_type(r13, rmethod, r4, true);
3384 
3385   __ jump_from_interpreted(rmethod, r0);
3386 }
3387 
3388 void TemplateTable::invokedynamic(int byte_no) {
3389   transition(vtos, vtos);
3390   assert(byte_no == f1_byte, "use this argument");
3391 
3392   prepare_invoke(byte_no, rmethod, r0);
3393 
3394   // r0: CallSite object (from cpool->resolved_references[])
3395   // rmethod: MH.linkToCallSite method (from f2)
3396 
3397   // Note:  r0_callsite is already pushed by prepare_invoke
3398 
3399   // %%% should make a type profile for any invokedynamic that takes a ref argument
3400   // profile this call
3401   __ profile_call(rbcp);
3402   __ profile_arguments_type(r3, rmethod, r13, false);
3403 
3404   __ verify_oop(r0);
3405 
3406   __ jump_from_interpreted(rmethod, r0);
3407 }
3408 
3409 
3410 //-----------------------------------------------------------------------------
3411 // Allocation
3412 
3413 void TemplateTable::_new() {
3414   transition(vtos, atos);
3415 
3416   __ get_unsigned_2_byte_index_at_bcp(r3, 1);
3417   Label slow_case;
3418   Label done;
3419   Label initialize_header;
3420   Label initialize_object; // including clearing the fields
3421   Label allocate_shared;
3422 
3423   __ get_cpool_and_tags(r4, r0);
3424   // Make sure the class we're about to instantiate has been resolved.
3425   // This is done before loading InstanceKlass to be consistent with the order
3426   // how Constant Pool is updated (see ConstantPool::klass_at_put)
3427   const int tags_offset = Array<u1>::base_offset_in_bytes();
3428   __ lea(rscratch1, Address(r0, r3, Address::lsl(0)));
3429   __ lea(rscratch1, Address(rscratch1, tags_offset));
3430   __ ldarb(rscratch1, rscratch1);
3431   __ cmp(rscratch1, JVM_CONSTANT_Class);
3432   __ br(Assembler::NE, slow_case);
3433 
3434   // get InstanceKlass
3435   __ load_resolved_klass_at_offset(r4, r3, r4, rscratch1);
3436 
3437   // make sure klass is initialized & doesn't have finalizer
3438   // make sure klass is fully initialized
3439   __ ldrb(rscratch1, Address(r4, InstanceKlass::init_state_offset()));
3440   __ cmp(rscratch1, InstanceKlass::fully_initialized);
3441   __ br(Assembler::NE, slow_case);
3442 
3443   // get instance_size in InstanceKlass (scaled to a count of bytes)
3444   __ ldrw(r3,
3445           Address(r4,
3446                   Klass::layout_helper_offset()));
3447   // test to see if it has a finalizer or is malformed in some way
3448   __ tbnz(r3, exact_log2(Klass::_lh_instance_slow_path_bit), slow_case);
3449 
3450   // Allocate the instance
3451   // 1) Try to allocate in the TLAB
3452   // 2) if fail and the object is large allocate in the shared Eden
3453   // 3) if the above fails (or is not applicable), go to a slow case
3454   // (creates a new TLAB, etc.)
3455 
3456   const bool allow_shared_alloc =
3457     Universe::heap()->supports_inline_contig_alloc();
3458 
3459   if (UseTLAB) {
3460     __ tlab_allocate(r0, r3, 0, noreg, r1,
3461                      allow_shared_alloc ? allocate_shared : slow_case);
3462 
3463     if (ZeroTLAB) {
3464       // the fields have been already cleared
3465       __ b(initialize_header);
3466     } else {
3467       // initialize both the header and fields
3468       __ b(initialize_object);
3469     }
3470   }
3471 
3472   // Allocation in the shared Eden, if allowed.
3473   //
3474   // r3: instance size in bytes
3475   if (allow_shared_alloc) {
3476     __ bind(allocate_shared);
3477 
3478     __ eden_allocate(r0, r3, 0, r10, slow_case);
3479     __ incr_allocated_bytes(rthread, r3, 0, rscratch1);
3480   }
3481 
3482   if (UseTLAB || Universe::heap()->supports_inline_contig_alloc()) {
3483     // The object is initialized before the header.  If the object size is
3484     // zero, go directly to the header initialization.
3485     __ bind(initialize_object);
3486     __ sub(r3, r3, sizeof(oopDesc));
3487     __ cbz(r3, initialize_header);
3488 
3489     // Initialize object fields
3490     {
3491       __ add(r2, r0, sizeof(oopDesc));
3492       Label loop;
3493       __ bind(loop);
3494       __ str(zr, Address(__ post(r2, BytesPerLong)));
3495       __ sub(r3, r3, BytesPerLong);
3496       __ cbnz(r3, loop);
3497     }
3498 
3499     // initialize object header only.
3500     __ bind(initialize_header);
3501     if (UseBiasedLocking) {
3502       __ ldr(rscratch1, Address(r4, Klass::prototype_header_offset()));
3503     } else {
3504       __ mov(rscratch1, (intptr_t)markOopDesc::prototype());
3505     }
3506     __ str(rscratch1, Address(r0, oopDesc::mark_offset_in_bytes()));
3507     __ store_klass_gap(r0, zr);  // zero klass gap for compressed oops
3508     __ store_klass(r0, r4);      // store klass last
3509 
3510     {
3511       SkipIfEqual skip(_masm, &DTraceAllocProbes, false);
3512       // Trigger dtrace event for fastpath
3513       __ push(atos); // save the return value
3514       __ call_VM_leaf(
3515            CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc), r0);
3516       __ pop(atos); // restore the return value
3517 
3518     }
3519     __ b(done);
3520   }
3521 
3522   // slow case
3523   __ bind(slow_case);
3524   __ get_constant_pool(c_rarg1);
3525   __ get_unsigned_2_byte_index_at_bcp(c_rarg2, 1);
3526   call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), c_rarg1, c_rarg2);
3527   __ verify_oop(r0);
3528 
3529   // continue
3530   __ bind(done);
3531   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3532   __ membar(Assembler::StoreStore);
3533 }
3534 
3535 void TemplateTable::newarray() {
3536   transition(itos, atos);
3537   __ load_unsigned_byte(c_rarg1, at_bcp(1));
3538   __ mov(c_rarg2, r0);
3539   call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
3540           c_rarg1, c_rarg2);
3541   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3542   __ membar(Assembler::StoreStore);
3543 }
3544 
3545 void TemplateTable::anewarray() {
3546   transition(itos, atos);
3547   __ get_unsigned_2_byte_index_at_bcp(c_rarg2, 1);
3548   __ get_constant_pool(c_rarg1);
3549   __ mov(c_rarg3, r0);
3550   call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
3551           c_rarg1, c_rarg2, c_rarg3);
3552   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3553   __ membar(Assembler::StoreStore);
3554 }
3555 
3556 void TemplateTable::arraylength() {
3557   transition(atos, itos);
3558   __ null_check(r0, arrayOopDesc::length_offset_in_bytes());
3559   __ ldrw(r0, Address(r0, arrayOopDesc::length_offset_in_bytes()));
3560 }
3561 
3562 void TemplateTable::checkcast()
3563 {
3564   transition(atos, atos);
3565   Label done, is_null, ok_is_subtype, quicked, resolved;
3566   __ cbz(r0, is_null);
3567 
3568   // Get cpool & tags index
3569   __ get_cpool_and_tags(r2, r3); // r2=cpool, r3=tags array
3570   __ get_unsigned_2_byte_index_at_bcp(r19, 1); // r19=index
3571   // See if bytecode has already been quicked
3572   __ add(rscratch1, r3, Array<u1>::base_offset_in_bytes());
3573   __ lea(r1, Address(rscratch1, r19));
3574   __ ldarb(r1, r1);
3575   __ cmp(r1, JVM_CONSTANT_Class);
3576   __ br(Assembler::EQ, quicked);
3577 
3578   __ push(atos); // save receiver for result, and for GC
3579   call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3580   // vm_result_2 has metadata result
3581   __ get_vm_result_2(r0, rthread);
3582   __ pop(r3); // restore receiver
3583   __ b(resolved);
3584 
3585   // Get superklass in r0 and subklass in r3
3586   __ bind(quicked);
3587   __ mov(r3, r0); // Save object in r3; r0 needed for subtype check
3588   __ load_resolved_klass_at_offset(r2, r19, r0, rscratch1); // r0 = klass
3589 
3590   __ bind(resolved);
3591   __ load_klass(r19, r3);
3592 
3593   // Generate subtype check.  Blows r2, r5.  Object in r3.
3594   // Superklass in r0.  Subklass in r19.
3595   __ gen_subtype_check(r19, ok_is_subtype);
3596 
3597   // Come here on failure
3598   __ push(r3);
3599   // object is at TOS
3600   __ b(Interpreter::_throw_ClassCastException_entry);
3601 
3602   // Come here on success
3603   __ bind(ok_is_subtype);
3604   __ mov(r0, r3); // Restore object in r3
3605 
3606   // Collect counts on whether this test sees NULLs a lot or not.
3607   if (ProfileInterpreter) {
3608     __ b(done);
3609     __ bind(is_null);
3610     __ profile_null_seen(r2);
3611   } else {
3612     __ bind(is_null);   // same as 'done'
3613   }
3614   __ bind(done);
3615 }
3616 
3617 void TemplateTable::instanceof() {
3618   transition(atos, itos);
3619   Label done, is_null, ok_is_subtype, quicked, resolved;
3620   __ cbz(r0, is_null);
3621 
3622   // Get cpool & tags index
3623   __ get_cpool_and_tags(r2, r3); // r2=cpool, r3=tags array
3624   __ get_unsigned_2_byte_index_at_bcp(r19, 1); // r19=index
3625   // See if bytecode has already been quicked
3626   __ add(rscratch1, r3, Array<u1>::base_offset_in_bytes());
3627   __ lea(r1, Address(rscratch1, r19));
3628   __ ldarb(r1, r1);
3629   __ cmp(r1, JVM_CONSTANT_Class);
3630   __ br(Assembler::EQ, quicked);
3631 
3632   __ push(atos); // save receiver for result, and for GC
3633   call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3634   // vm_result_2 has metadata result
3635   __ get_vm_result_2(r0, rthread);
3636   __ pop(r3); // restore receiver
3637   __ verify_oop(r3);
3638   __ load_klass(r3, r3);
3639   __ b(resolved);
3640 
3641   // Get superklass in r0 and subklass in r3
3642   __ bind(quicked);
3643   __ load_klass(r3, r0);
3644   __ load_resolved_klass_at_offset(r2, r19, r0, rscratch1);
3645 
3646   __ bind(resolved);
3647 
3648   // Generate subtype check.  Blows r2, r5
3649   // Superklass in r0.  Subklass in r3.
3650   __ gen_subtype_check(r3, ok_is_subtype);
3651 
3652   // Come here on failure
3653   __ mov(r0, 0);
3654   __ b(done);
3655   // Come here on success
3656   __ bind(ok_is_subtype);
3657   __ mov(r0, 1);
3658 
3659   // Collect counts on whether this test sees NULLs a lot or not.
3660   if (ProfileInterpreter) {
3661     __ b(done);
3662     __ bind(is_null);
3663     __ profile_null_seen(r2);
3664   } else {
3665     __ bind(is_null);   // same as 'done'
3666   }
3667   __ bind(done);
3668   // r0 = 0: obj == NULL or  obj is not an instanceof the specified klass
3669   // r0 = 1: obj != NULL and obj is     an instanceof the specified klass
3670 }
3671 
3672 //-----------------------------------------------------------------------------
3673 // Breakpoints
3674 void TemplateTable::_breakpoint() {
3675   // Note: We get here even if we are single stepping..
3676   // jbug inists on setting breakpoints at every bytecode
3677   // even if we are in single step mode.
3678 
3679   transition(vtos, vtos);
3680 
3681   // get the unpatched byte code
3682   __ get_method(c_rarg1);
3683   __ call_VM(noreg,
3684              CAST_FROM_FN_PTR(address,
3685                               InterpreterRuntime::get_original_bytecode_at),
3686              c_rarg1, rbcp);
3687   __ mov(r19, r0);
3688 
3689   // post the breakpoint event
3690   __ call_VM(noreg,
3691              CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint),
3692              rmethod, rbcp);
3693 
3694   // complete the execution of original bytecode
3695   __ mov(rscratch1, r19);
3696   __ dispatch_only_normal(vtos);
3697 }
3698 
3699 //-----------------------------------------------------------------------------
3700 // Exceptions
3701 
3702 void TemplateTable::athrow() {
3703   transition(atos, vtos);
3704   __ null_check(r0);
3705   __ b(Interpreter::throw_exception_entry());
3706 }
3707 
3708 //-----------------------------------------------------------------------------
3709 // Synchronization
3710 //
3711 // Note: monitorenter & exit are symmetric routines; which is reflected
3712 //       in the assembly code structure as well
3713 //
3714 // Stack layout:
3715 //
3716 // [expressions  ] <--- esp               = expression stack top
3717 // ..
3718 // [expressions  ]
3719 // [monitor entry] <--- monitor block top = expression stack bot
3720 // ..
3721 // [monitor entry]
3722 // [frame data   ] <--- monitor block bot
3723 // ...
3724 // [saved rbp    ] <--- rbp
3725 void TemplateTable::monitorenter()
3726 {
3727   transition(atos, vtos);
3728 
3729   // check for NULL object
3730   __ null_check(r0);
3731 
3732   const Address monitor_block_top(
3733         rfp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
3734   const Address monitor_block_bot(
3735         rfp, frame::interpreter_frame_initial_sp_offset * wordSize);
3736   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
3737 
3738   Label allocated;
3739 
3740   // initialize entry pointer
3741   __ mov(c_rarg1, zr); // points to free slot or NULL
3742 
3743   // find a free slot in the monitor block (result in c_rarg1)
3744   {
3745     Label entry, loop, exit;
3746     __ ldr(c_rarg3, monitor_block_top); // points to current entry,
3747                                         // starting with top-most entry
3748     __ lea(c_rarg2, monitor_block_bot); // points to word before bottom
3749 
3750     __ b(entry);
3751 
3752     __ bind(loop);
3753     // check if current entry is used
3754     // if not used then remember entry in c_rarg1
3755     __ ldr(rscratch1, Address(c_rarg3, BasicObjectLock::obj_offset_in_bytes()));
3756     __ cmp(zr, rscratch1);
3757     __ csel(c_rarg1, c_rarg3, c_rarg1, Assembler::EQ);
3758     // check if current entry is for same object
3759     __ cmp(r0, rscratch1);
3760     // if same object then stop searching
3761     __ br(Assembler::EQ, exit);
3762     // otherwise advance to next entry
3763     __ add(c_rarg3, c_rarg3, entry_size);
3764     __ bind(entry);
3765     // check if bottom reached
3766     __ cmp(c_rarg3, c_rarg2);
3767     // if not at bottom then check this entry
3768     __ br(Assembler::NE, loop);
3769     __ bind(exit);
3770   }
3771 
3772   __ cbnz(c_rarg1, allocated); // check if a slot has been found and
3773                             // if found, continue with that on
3774 
3775   // allocate one if there's no free slot
3776   {
3777     Label entry, loop;
3778     // 1. compute new pointers            // rsp: old expression stack top
3779     __ ldr(c_rarg1, monitor_block_bot);   // c_rarg1: old expression stack bottom
3780     __ sub(esp, esp, entry_size);         // move expression stack top
3781     __ sub(c_rarg1, c_rarg1, entry_size); // move expression stack bottom
3782     __ mov(c_rarg3, esp);                 // set start value for copy loop
3783     __ str(c_rarg1, monitor_block_bot);   // set new monitor block bottom
3784 
3785     __ sub(sp, sp, entry_size);           // make room for the monitor
3786 
3787     __ b(entry);
3788     // 2. move expression stack contents
3789     __ bind(loop);
3790     __ ldr(c_rarg2, Address(c_rarg3, entry_size)); // load expression stack
3791                                                    // word from old location
3792     __ str(c_rarg2, Address(c_rarg3, 0));          // and store it at new location
3793     __ add(c_rarg3, c_rarg3, wordSize);            // advance to next word
3794     __ bind(entry);
3795     __ cmp(c_rarg3, c_rarg1);        // check if bottom reached
3796     __ br(Assembler::NE, loop);      // if not at bottom then
3797                                      // copy next word
3798   }
3799 
3800   // call run-time routine
3801   // c_rarg1: points to monitor entry
3802   __ bind(allocated);
3803 
3804   // Increment bcp to point to the next bytecode, so exception
3805   // handling for async. exceptions work correctly.
3806   // The object has already been poped from the stack, so the
3807   // expression stack looks correct.
3808   __ increment(rbcp);
3809 
3810   // store object
3811   __ str(r0, Address(c_rarg1, BasicObjectLock::obj_offset_in_bytes()));
3812   __ lock_object(c_rarg1);
3813 
3814   // check to make sure this monitor doesn't cause stack overflow after locking
3815   __ save_bcp();  // in case of exception
3816   __ generate_stack_overflow_check(0);
3817 
3818   // The bcp has already been incremented. Just need to dispatch to
3819   // next instruction.
3820   __ dispatch_next(vtos);
3821 }
3822 
3823 
3824 void TemplateTable::monitorexit()
3825 {
3826   transition(atos, vtos);
3827 
3828   // check for NULL object
3829   __ null_check(r0);
3830 
3831   const Address monitor_block_top(
3832         rfp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
3833   const Address monitor_block_bot(
3834         rfp, frame::interpreter_frame_initial_sp_offset * wordSize);
3835   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
3836 
3837   Label found;
3838 
3839   // find matching slot
3840   {
3841     Label entry, loop;
3842     __ ldr(c_rarg1, monitor_block_top); // points to current entry,
3843                                         // starting with top-most entry
3844     __ lea(c_rarg2, monitor_block_bot); // points to word before bottom
3845                                         // of monitor block
3846     __ b(entry);
3847 
3848     __ bind(loop);
3849     // check if current entry is for same object
3850     __ ldr(rscratch1, Address(c_rarg1, BasicObjectLock::obj_offset_in_bytes()));
3851     __ cmp(r0, rscratch1);
3852     // if same object then stop searching
3853     __ br(Assembler::EQ, found);
3854     // otherwise advance to next entry
3855     __ add(c_rarg1, c_rarg1, entry_size);
3856     __ bind(entry);
3857     // check if bottom reached
3858     __ cmp(c_rarg1, c_rarg2);
3859     // if not at bottom then check this entry
3860     __ br(Assembler::NE, loop);
3861   }
3862 
3863   // error handling. Unlocking was not block-structured
3864   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
3865                    InterpreterRuntime::throw_illegal_monitor_state_exception));
3866   __ should_not_reach_here();
3867 
3868   // call run-time routine
3869   __ bind(found);
3870   __ push_ptr(r0); // make sure object is on stack (contract with oopMaps)
3871   __ unlock_object(c_rarg1);
3872   __ pop_ptr(r0); // discard object
3873 }
3874 
3875 
3876 // Wide instructions
3877 void TemplateTable::wide()
3878 {
3879   __ load_unsigned_byte(r19, at_bcp(1));
3880   __ mov(rscratch1, (address)Interpreter::_wentry_point);
3881   __ ldr(rscratch1, Address(rscratch1, r19, Address::uxtw(3)));
3882   __ br(rscratch1);
3883 }
3884 
3885 
3886 // Multi arrays
3887 void TemplateTable::multianewarray() {
3888   transition(vtos, atos);
3889   __ load_unsigned_byte(r0, at_bcp(3)); // get number of dimensions
3890   // last dim is on top of stack; we want address of first one:
3891   // first_addr = last_addr + (ndims - 1) * wordSize
3892   __ lea(c_rarg1, Address(esp, r0, Address::uxtw(3)));
3893   __ sub(c_rarg1, c_rarg1, wordSize);
3894   call_VM(r0,
3895           CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray),
3896           c_rarg1);
3897   __ load_unsigned_byte(r1, at_bcp(3));
3898   __ lea(esp, Address(esp, r1, Address::uxtw(3)));
3899 }