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