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