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