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