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   // Clobbers: r10, r11, r3
1160   do_oop_store(_masm, element_address, r0, IS_ARRAY);
1161   __ b(done);
1162 
1163   // Have a null in r0, r3=array, r2=index.  Store null at ary[idx]
1164   __ bind(is_null);
1165   __ profile_null_seen(r2);
1166 
1167   // Store a null
1168   // Clobbers: r10, r11, r3
1169   do_oop_store(_masm, element_address, noreg, IS_ARRAY);
1170 
1171   // Pop stack arguments
1172   __ bind(done);
1173   __ add(esp, esp, 3 * Interpreter::stackElementSize);
1174 }
1175 
1176 void TemplateTable::bastore()
1177 {
1178   transition(itos, vtos);
1179   __ pop_i(r1);
1180   __ pop_ptr(r3);
1181   // r0: value
1182   // r1: index
1183   // r3: array
1184   index_check(r3, r1); // prefer index in r1
1185 
1186   // Need to check whether array is boolean or byte
1187   // since both types share the bastore bytecode.
1188   __ load_klass(r2, r3);
1189   __ ldrw(r2, Address(r2, Klass::layout_helper_offset()));
1190   int diffbit_index = exact_log2(Klass::layout_helper_boolean_diffbit());
1191   Label L_skip;
1192   __ tbz(r2, diffbit_index, L_skip);
1193   __ andw(r0, r0, 1);  // if it is a T_BOOLEAN array, mask the stored value to 0/1
1194   __ bind(L_skip);
1195 
1196   __ add(r1, r1, arrayOopDesc::base_offset_in_bytes(T_BYTE) >> 0);
1197   __ access_store_at(T_BYTE, IN_HEAP | IS_ARRAY, Address(r3, r1, Address::uxtw(0)), r0, noreg, noreg, noreg);
1198 }
1199 
1200 void TemplateTable::castore()
1201 {
1202   transition(itos, vtos);
1203   __ pop_i(r1);
1204   __ pop_ptr(r3);
1205   // r0: value
1206   // r1: index
1207   // r3: array
1208   index_check(r3, r1); // prefer index in r1
1209   __ add(r1, r1, arrayOopDesc::base_offset_in_bytes(T_CHAR) >> 1);
1210   __ access_store_at(T_CHAR, IN_HEAP | IS_ARRAY, Address(r3, r1, Address::uxtw(1)), r0, noreg, noreg, noreg);
1211 }
1212 
1213 void TemplateTable::sastore()
1214 {
1215   castore();
1216 }
1217 
1218 void TemplateTable::istore(int n)
1219 {
1220   transition(itos, vtos);
1221   __ str(r0, iaddress(n));
1222 }
1223 
1224 void TemplateTable::lstore(int n)
1225 {
1226   transition(ltos, vtos);
1227   __ str(r0, laddress(n));
1228 }
1229 
1230 void TemplateTable::fstore(int n)
1231 {
1232   transition(ftos, vtos);
1233   __ strs(v0, faddress(n));
1234 }
1235 
1236 void TemplateTable::dstore(int n)
1237 {
1238   transition(dtos, vtos);
1239   __ strd(v0, daddress(n));
1240 }
1241 
1242 void TemplateTable::astore(int n)
1243 {
1244   transition(vtos, vtos);
1245   __ pop_ptr(r0);
1246   __ str(r0, iaddress(n));
1247 }
1248 
1249 void TemplateTable::pop()
1250 {
1251   transition(vtos, vtos);
1252   __ add(esp, esp, Interpreter::stackElementSize);
1253 }
1254 
1255 void TemplateTable::pop2()
1256 {
1257   transition(vtos, vtos);
1258   __ add(esp, esp, 2 * Interpreter::stackElementSize);
1259 }
1260 
1261 void TemplateTable::dup()
1262 {
1263   transition(vtos, vtos);
1264   __ ldr(r0, Address(esp, 0));
1265   __ push(r0);
1266   // stack: ..., a, a
1267 }
1268 
1269 void TemplateTable::dup_x1()
1270 {
1271   transition(vtos, vtos);
1272   // stack: ..., a, b
1273   __ ldr(r0, at_tos());  // load b
1274   __ ldr(r2, at_tos_p1());  // load a
1275   __ str(r0, at_tos_p1());  // store b
1276   __ str(r2, at_tos());  // store a
1277   __ push(r0);                  // push b
1278   // stack: ..., b, a, b
1279 }
1280 
1281 void TemplateTable::dup_x2()
1282 {
1283   transition(vtos, vtos);
1284   // stack: ..., a, b, c
1285   __ ldr(r0, at_tos());  // load c
1286   __ ldr(r2, at_tos_p2());  // load a
1287   __ str(r0, at_tos_p2());  // store c in a
1288   __ push(r0);      // push c
1289   // stack: ..., c, b, c, c
1290   __ ldr(r0, at_tos_p2());  // load b
1291   __ str(r2, at_tos_p2());  // store a in b
1292   // stack: ..., c, a, c, c
1293   __ str(r0, at_tos_p1());  // store b in c
1294   // stack: ..., c, a, b, c
1295 }
1296 
1297 void TemplateTable::dup2()
1298 {
1299   transition(vtos, vtos);
1300   // stack: ..., a, b
1301   __ ldr(r0, at_tos_p1());  // load a
1302   __ push(r0);                  // push a
1303   __ ldr(r0, at_tos_p1());  // load b
1304   __ push(r0);                  // push b
1305   // stack: ..., a, b, a, b
1306 }
1307 
1308 void TemplateTable::dup2_x1()
1309 {
1310   transition(vtos, vtos);
1311   // stack: ..., a, b, c
1312   __ ldr(r2, at_tos());  // load c
1313   __ ldr(r0, at_tos_p1());  // load b
1314   __ push(r0);                  // push b
1315   __ push(r2);                  // push c
1316   // stack: ..., a, b, c, b, c
1317   __ str(r2, at_tos_p3());  // store c in b
1318   // stack: ..., a, c, c, b, c
1319   __ ldr(r2, at_tos_p4());  // load a
1320   __ str(r2, at_tos_p2());  // store a in 2nd c
1321   // stack: ..., a, c, a, b, c
1322   __ str(r0, at_tos_p4());  // store b in a
1323   // stack: ..., b, c, a, b, c
1324 }
1325 
1326 void TemplateTable::dup2_x2()
1327 {
1328   transition(vtos, vtos);
1329   // stack: ..., a, b, c, d
1330   __ ldr(r2, at_tos());  // load d
1331   __ ldr(r0, at_tos_p1());  // load c
1332   __ push(r0)            ;      // push c
1333   __ push(r2);                  // push d
1334   // stack: ..., a, b, c, d, c, d
1335   __ ldr(r0, at_tos_p4());  // load b
1336   __ str(r0, at_tos_p2());  // store b in d
1337   __ str(r2, at_tos_p4());  // store d in b
1338   // stack: ..., a, d, c, b, c, d
1339   __ ldr(r2, at_tos_p5());  // load a
1340   __ ldr(r0, at_tos_p3());  // load c
1341   __ str(r2, at_tos_p3());  // store a in c
1342   __ str(r0, at_tos_p5());  // store c in a
1343   // stack: ..., c, d, a, b, c, d
1344 }
1345 
1346 void TemplateTable::swap()
1347 {
1348   transition(vtos, vtos);
1349   // stack: ..., a, b
1350   __ ldr(r2, at_tos_p1());  // load a
1351   __ ldr(r0, at_tos());  // load b
1352   __ str(r2, at_tos());  // store a in b
1353   __ str(r0, at_tos_p1());  // store b in a
1354   // stack: ..., b, a
1355 }
1356 
1357 void TemplateTable::iop2(Operation op)
1358 {
1359   transition(itos, itos);
1360   // r0 <== r1 op r0
1361   __ pop_i(r1);
1362   switch (op) {
1363   case add  : __ addw(r0, r1, r0); break;
1364   case sub  : __ subw(r0, r1, r0); break;
1365   case mul  : __ mulw(r0, r1, r0); break;
1366   case _and : __ andw(r0, r1, r0); break;
1367   case _or  : __ orrw(r0, r1, r0); break;
1368   case _xor : __ eorw(r0, r1, r0); break;
1369   case shl  : __ lslvw(r0, r1, r0); break;
1370   case shr  : __ asrvw(r0, r1, r0); break;
1371   case ushr : __ lsrvw(r0, r1, r0);break;
1372   default   : ShouldNotReachHere();
1373   }
1374 }
1375 
1376 void TemplateTable::lop2(Operation op)
1377 {
1378   transition(ltos, ltos);
1379   // r0 <== r1 op r0
1380   __ pop_l(r1);
1381   switch (op) {
1382   case add  : __ add(r0, r1, r0); break;
1383   case sub  : __ sub(r0, r1, r0); break;
1384   case mul  : __ mul(r0, r1, r0); break;
1385   case _and : __ andr(r0, r1, r0); break;
1386   case _or  : __ orr(r0, r1, r0); break;
1387   case _xor : __ eor(r0, r1, r0); break;
1388   default   : ShouldNotReachHere();
1389   }
1390 }
1391 
1392 void TemplateTable::idiv()
1393 {
1394   transition(itos, itos);
1395   // explicitly check for div0
1396   Label no_div0;
1397   __ cbnzw(r0, no_div0);
1398   __ mov(rscratch1, Interpreter::_throw_ArithmeticException_entry);
1399   __ br(rscratch1);
1400   __ bind(no_div0);
1401   __ pop_i(r1);
1402   // r0 <== r1 idiv r0
1403   __ corrected_idivl(r0, r1, r0, /* want_remainder */ false);
1404 }
1405 
1406 void TemplateTable::irem()
1407 {
1408   transition(itos, itos);
1409   // explicitly check for div0
1410   Label no_div0;
1411   __ cbnzw(r0, no_div0);
1412   __ mov(rscratch1, Interpreter::_throw_ArithmeticException_entry);
1413   __ br(rscratch1);
1414   __ bind(no_div0);
1415   __ pop_i(r1);
1416   // r0 <== r1 irem r0
1417   __ corrected_idivl(r0, r1, r0, /* want_remainder */ true);
1418 }
1419 
1420 void TemplateTable::lmul()
1421 {
1422   transition(ltos, ltos);
1423   __ pop_l(r1);
1424   __ mul(r0, r0, r1);
1425 }
1426 
1427 void TemplateTable::ldiv()
1428 {
1429   transition(ltos, ltos);
1430   // explicitly check for div0
1431   Label no_div0;
1432   __ cbnz(r0, no_div0);
1433   __ mov(rscratch1, Interpreter::_throw_ArithmeticException_entry);
1434   __ br(rscratch1);
1435   __ bind(no_div0);
1436   __ pop_l(r1);
1437   // r0 <== r1 ldiv r0
1438   __ corrected_idivq(r0, r1, r0, /* want_remainder */ false);
1439 }
1440 
1441 void TemplateTable::lrem()
1442 {
1443   transition(ltos, ltos);
1444   // explicitly check for div0
1445   Label no_div0;
1446   __ cbnz(r0, no_div0);
1447   __ mov(rscratch1, Interpreter::_throw_ArithmeticException_entry);
1448   __ br(rscratch1);
1449   __ bind(no_div0);
1450   __ pop_l(r1);
1451   // r0 <== r1 lrem r0
1452   __ corrected_idivq(r0, r1, r0, /* want_remainder */ true);
1453 }
1454 
1455 void TemplateTable::lshl()
1456 {
1457   transition(itos, ltos);
1458   // shift count is in r0
1459   __ pop_l(r1);
1460   __ lslv(r0, r1, r0);
1461 }
1462 
1463 void TemplateTable::lshr()
1464 {
1465   transition(itos, ltos);
1466   // shift count is in r0
1467   __ pop_l(r1);
1468   __ asrv(r0, r1, r0);
1469 }
1470 
1471 void TemplateTable::lushr()
1472 {
1473   transition(itos, ltos);
1474   // shift count is in r0
1475   __ pop_l(r1);
1476   __ lsrv(r0, r1, r0);
1477 }
1478 
1479 void TemplateTable::fop2(Operation op)
1480 {
1481   transition(ftos, ftos);
1482   switch (op) {
1483   case add:
1484     // n.b. use ldrd because this is a 64 bit slot
1485     __ pop_f(v1);
1486     __ fadds(v0, v1, v0);
1487     break;
1488   case sub:
1489     __ pop_f(v1);
1490     __ fsubs(v0, v1, v0);
1491     break;
1492   case mul:
1493     __ pop_f(v1);
1494     __ fmuls(v0, v1, v0);
1495     break;
1496   case div:
1497     __ pop_f(v1);
1498     __ fdivs(v0, v1, v0);
1499     break;
1500   case rem:
1501     __ fmovs(v1, v0);
1502     __ pop_f(v0);
1503     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::frem));
1504     break;
1505   default:
1506     ShouldNotReachHere();
1507     break;
1508   }
1509 }
1510 
1511 void TemplateTable::dop2(Operation op)
1512 {
1513   transition(dtos, dtos);
1514   switch (op) {
1515   case add:
1516     // n.b. use ldrd because this is a 64 bit slot
1517     __ pop_d(v1);
1518     __ faddd(v0, v1, v0);
1519     break;
1520   case sub:
1521     __ pop_d(v1);
1522     __ fsubd(v0, v1, v0);
1523     break;
1524   case mul:
1525     __ pop_d(v1);
1526     __ fmuld(v0, v1, v0);
1527     break;
1528   case div:
1529     __ pop_d(v1);
1530     __ fdivd(v0, v1, v0);
1531     break;
1532   case rem:
1533     __ fmovd(v1, v0);
1534     __ pop_d(v0);
1535     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::drem));
1536     break;
1537   default:
1538     ShouldNotReachHere();
1539     break;
1540   }
1541 }
1542 
1543 void TemplateTable::ineg()
1544 {
1545   transition(itos, itos);
1546   __ negw(r0, r0);
1547 
1548 }
1549 
1550 void TemplateTable::lneg()
1551 {
1552   transition(ltos, ltos);
1553   __ neg(r0, r0);
1554 }
1555 
1556 void TemplateTable::fneg()
1557 {
1558   transition(ftos, ftos);
1559   __ fnegs(v0, v0);
1560 }
1561 
1562 void TemplateTable::dneg()
1563 {
1564   transition(dtos, dtos);
1565   __ fnegd(v0, v0);
1566 }
1567 
1568 void TemplateTable::iinc()
1569 {
1570   transition(vtos, vtos);
1571   __ load_signed_byte(r1, at_bcp(2)); // get constant
1572   locals_index(r2);
1573   __ ldr(r0, iaddress(r2));
1574   __ addw(r0, r0, r1);
1575   __ str(r0, iaddress(r2));
1576 }
1577 
1578 void TemplateTable::wide_iinc()
1579 {
1580   transition(vtos, vtos);
1581   // __ mov(r1, zr);
1582   __ ldrw(r1, at_bcp(2)); // get constant and index
1583   __ rev16(r1, r1);
1584   __ ubfx(r2, r1, 0, 16);
1585   __ neg(r2, r2);
1586   __ sbfx(r1, r1, 16, 16);
1587   __ ldr(r0, iaddress(r2));
1588   __ addw(r0, r0, r1);
1589   __ str(r0, iaddress(r2));
1590 }
1591 
1592 void TemplateTable::convert()
1593 {
1594   // Checking
1595 #ifdef ASSERT
1596   {
1597     TosState tos_in  = ilgl;
1598     TosState tos_out = ilgl;
1599     switch (bytecode()) {
1600     case Bytecodes::_i2l: // fall through
1601     case Bytecodes::_i2f: // fall through
1602     case Bytecodes::_i2d: // fall through
1603     case Bytecodes::_i2b: // fall through
1604     case Bytecodes::_i2c: // fall through
1605     case Bytecodes::_i2s: tos_in = itos; break;
1606     case Bytecodes::_l2i: // fall through
1607     case Bytecodes::_l2f: // fall through
1608     case Bytecodes::_l2d: tos_in = ltos; break;
1609     case Bytecodes::_f2i: // fall through
1610     case Bytecodes::_f2l: // fall through
1611     case Bytecodes::_f2d: tos_in = ftos; break;
1612     case Bytecodes::_d2i: // fall through
1613     case Bytecodes::_d2l: // fall through
1614     case Bytecodes::_d2f: tos_in = dtos; break;
1615     default             : ShouldNotReachHere();
1616     }
1617     switch (bytecode()) {
1618     case Bytecodes::_l2i: // fall through
1619     case Bytecodes::_f2i: // fall through
1620     case Bytecodes::_d2i: // fall through
1621     case Bytecodes::_i2b: // fall through
1622     case Bytecodes::_i2c: // fall through
1623     case Bytecodes::_i2s: tos_out = itos; break;
1624     case Bytecodes::_i2l: // fall through
1625     case Bytecodes::_f2l: // fall through
1626     case Bytecodes::_d2l: tos_out = ltos; break;
1627     case Bytecodes::_i2f: // fall through
1628     case Bytecodes::_l2f: // fall through
1629     case Bytecodes::_d2f: tos_out = ftos; break;
1630     case Bytecodes::_i2d: // fall through
1631     case Bytecodes::_l2d: // fall through
1632     case Bytecodes::_f2d: tos_out = dtos; break;
1633     default             : ShouldNotReachHere();
1634     }
1635     transition(tos_in, tos_out);
1636   }
1637 #endif // ASSERT
1638   // static const int64_t is_nan = 0x8000000000000000L;
1639 
1640   // Conversion
1641   switch (bytecode()) {
1642   case Bytecodes::_i2l:
1643     __ sxtw(r0, r0);
1644     break;
1645   case Bytecodes::_i2f:
1646     __ scvtfws(v0, r0);
1647     break;
1648   case Bytecodes::_i2d:
1649     __ scvtfwd(v0, r0);
1650     break;
1651   case Bytecodes::_i2b:
1652     __ sxtbw(r0, r0);
1653     break;
1654   case Bytecodes::_i2c:
1655     __ uxthw(r0, r0);
1656     break;
1657   case Bytecodes::_i2s:
1658     __ sxthw(r0, r0);
1659     break;
1660   case Bytecodes::_l2i:
1661     __ uxtw(r0, r0);
1662     break;
1663   case Bytecodes::_l2f:
1664     __ scvtfs(v0, r0);
1665     break;
1666   case Bytecodes::_l2d:
1667     __ scvtfd(v0, r0);
1668     break;
1669   case Bytecodes::_f2i:
1670   {
1671     Label L_Okay;
1672     __ clear_fpsr();
1673     __ fcvtzsw(r0, v0);
1674     __ get_fpsr(r1);
1675     __ cbzw(r1, L_Okay);
1676     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2i));
1677     __ bind(L_Okay);
1678   }
1679     break;
1680   case Bytecodes::_f2l:
1681   {
1682     Label L_Okay;
1683     __ clear_fpsr();
1684     __ fcvtzs(r0, v0);
1685     __ get_fpsr(r1);
1686     __ cbzw(r1, L_Okay);
1687     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2l));
1688     __ bind(L_Okay);
1689   }
1690     break;
1691   case Bytecodes::_f2d:
1692     __ fcvts(v0, v0);
1693     break;
1694   case Bytecodes::_d2i:
1695   {
1696     Label L_Okay;
1697     __ clear_fpsr();
1698     __ fcvtzdw(r0, v0);
1699     __ get_fpsr(r1);
1700     __ cbzw(r1, L_Okay);
1701     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2i));
1702     __ bind(L_Okay);
1703   }
1704     break;
1705   case Bytecodes::_d2l:
1706   {
1707     Label L_Okay;
1708     __ clear_fpsr();
1709     __ fcvtzd(r0, v0);
1710     __ get_fpsr(r1);
1711     __ cbzw(r1, L_Okay);
1712     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2l));
1713     __ bind(L_Okay);
1714   }
1715     break;
1716   case Bytecodes::_d2f:
1717     __ fcvtd(v0, v0);
1718     break;
1719   default:
1720     ShouldNotReachHere();
1721   }
1722 }
1723 
1724 void TemplateTable::lcmp()
1725 {
1726   transition(ltos, itos);
1727   Label done;
1728   __ pop_l(r1);
1729   __ cmp(r1, r0);
1730   __ mov(r0, (uint64_t)-1L);
1731   __ br(Assembler::LT, done);
1732   // __ mov(r0, 1UL);
1733   // __ csel(r0, r0, zr, Assembler::NE);
1734   // and here is a faster way
1735   __ csinc(r0, zr, zr, Assembler::EQ);
1736   __ bind(done);
1737 }
1738 
1739 void TemplateTable::float_cmp(bool is_float, int unordered_result)
1740 {
1741   Label done;
1742   if (is_float) {
1743     // XXX get rid of pop here, use ... reg, mem32
1744     __ pop_f(v1);
1745     __ fcmps(v1, v0);
1746   } else {
1747     // XXX get rid of pop here, use ... reg, mem64
1748     __ pop_d(v1);
1749     __ fcmpd(v1, v0);
1750   }
1751   if (unordered_result < 0) {
1752     // we want -1 for unordered or less than, 0 for equal and 1 for
1753     // greater than.
1754     __ mov(r0, (uint64_t)-1L);
1755     // for FP LT tests less than or unordered
1756     __ br(Assembler::LT, done);
1757     // install 0 for EQ otherwise 1
1758     __ csinc(r0, zr, zr, Assembler::EQ);
1759   } else {
1760     // we want -1 for less than, 0 for equal and 1 for unordered or
1761     // greater than.
1762     __ mov(r0, 1L);
1763     // for FP HI tests greater than or unordered
1764     __ br(Assembler::HI, done);
1765     // install 0 for EQ otherwise ~0
1766     __ csinv(r0, zr, zr, Assembler::EQ);
1767 
1768   }
1769   __ bind(done);
1770 }
1771 
1772 void TemplateTable::branch(bool is_jsr, bool is_wide)
1773 {
1774   __ profile_taken_branch(r0);
1775   const ByteSize be_offset = MethodCounters::backedge_counter_offset() +
1776                              InvocationCounter::counter_offset();
1777   const ByteSize inv_offset = MethodCounters::invocation_counter_offset() +
1778                               InvocationCounter::counter_offset();
1779 
1780   // load branch displacement
1781   if (!is_wide) {
1782     __ ldrh(r2, at_bcp(1));
1783     __ rev16(r2, r2);
1784     // sign extend the 16 bit value in r2
1785     __ sbfm(r2, r2, 0, 15);
1786   } else {
1787     __ ldrw(r2, at_bcp(1));
1788     __ revw(r2, r2);
1789     // sign extend the 32 bit value in r2
1790     __ sbfm(r2, r2, 0, 31);
1791   }
1792 
1793   // Handle all the JSR stuff here, then exit.
1794   // It's much shorter and cleaner than intermingling with the non-JSR
1795   // normal-branch stuff occurring below.
1796 
1797   if (is_jsr) {
1798     // Pre-load the next target bytecode into rscratch1
1799     __ load_unsigned_byte(rscratch1, Address(rbcp, r2));
1800     // compute return address as bci
1801     __ ldr(rscratch2, Address(rmethod, Method::const_offset()));
1802     __ add(rscratch2, rscratch2,
1803            in_bytes(ConstMethod::codes_offset()) - (is_wide ? 5 : 3));
1804     __ sub(r1, rbcp, rscratch2);
1805     __ push_i(r1);
1806     // Adjust the bcp by the 16-bit displacement in r2
1807     __ add(rbcp, rbcp, r2);
1808     __ dispatch_only(vtos, /*generate_poll*/true);
1809     return;
1810   }
1811 
1812   // Normal (non-jsr) branch handling
1813 
1814   // Adjust the bcp by the displacement in r2
1815   __ add(rbcp, rbcp, r2);
1816 
1817   assert(UseLoopCounter || !UseOnStackReplacement,
1818          "on-stack-replacement requires loop counters");
1819   Label backedge_counter_overflow;
1820   Label dispatch;
1821   if (UseLoopCounter) {
1822     // increment backedge counter for backward branches
1823     // r0: MDO
1824     // r2: target offset
1825     __ cmp(r2, zr);
1826     __ br(Assembler::GT, dispatch); // count only if backward branch
1827 
1828     // ECN: FIXME: This code smells
1829     // check if MethodCounters exists
1830     Label has_counters;
1831     __ ldr(rscratch1, Address(rmethod, Method::method_counters_offset()));
1832     __ cbnz(rscratch1, has_counters);
1833     __ push(r0);
1834     __ push(r2);
1835     __ call_VM(noreg, CAST_FROM_FN_PTR(address,
1836             InterpreterRuntime::build_method_counters), rmethod);
1837     __ pop(r2);
1838     __ pop(r0);
1839     __ ldr(rscratch1, Address(rmethod, Method::method_counters_offset()));
1840     __ cbz(rscratch1, dispatch); // No MethodCounters allocated, OutOfMemory
1841     __ bind(has_counters);
1842 
1843     Label no_mdo;
1844     int increment = InvocationCounter::count_increment;
1845     if (ProfileInterpreter) {
1846       // Are we profiling?
1847       __ ldr(r1, Address(rmethod, in_bytes(Method::method_data_offset())));
1848       __ cbz(r1, no_mdo);
1849       // Increment the MDO backedge counter
1850       const Address mdo_backedge_counter(r1, in_bytes(MethodData::backedge_counter_offset()) +
1851                                          in_bytes(InvocationCounter::counter_offset()));
1852       const Address mask(r1, in_bytes(MethodData::backedge_mask_offset()));
1853       __ increment_mask_and_jump(mdo_backedge_counter, increment, mask,
1854                                  r0, rscratch1, false, Assembler::EQ,
1855                                  UseOnStackReplacement ? &backedge_counter_overflow : &dispatch);
1856       __ b(dispatch);
1857     }
1858     __ bind(no_mdo);
1859     // Increment backedge counter in MethodCounters*
1860     __ ldr(rscratch1, Address(rmethod, Method::method_counters_offset()));
1861     const Address mask(rscratch1, in_bytes(MethodCounters::backedge_mask_offset()));
1862     __ increment_mask_and_jump(Address(rscratch1, be_offset), increment, mask,
1863                                r0, rscratch2, false, Assembler::EQ,
1864                                UseOnStackReplacement ? &backedge_counter_overflow : &dispatch);
1865     __ bind(dispatch);
1866   }
1867 
1868   // Pre-load the next target bytecode into rscratch1
1869   __ load_unsigned_byte(rscratch1, Address(rbcp, 0));
1870 
1871   // continue with the bytecode @ target
1872   // rscratch1: target bytecode
1873   // rbcp: target bcp
1874   __ dispatch_only(vtos, /*generate_poll*/true);
1875 
1876   if (UseLoopCounter && UseOnStackReplacement) {
1877     // invocation counter overflow
1878     __ bind(backedge_counter_overflow);
1879     __ neg(r2, r2);
1880     __ add(r2, r2, rbcp);     // branch bcp
1881     // IcoResult frequency_counter_overflow([JavaThread*], address branch_bcp)
1882     __ call_VM(noreg,
1883                CAST_FROM_FN_PTR(address,
1884                                 InterpreterRuntime::frequency_counter_overflow),
1885                r2);
1886     __ load_unsigned_byte(r1, Address(rbcp, 0));  // restore target bytecode
1887 
1888     // r0: osr nmethod (osr ok) or null (osr not possible)
1889     // w1: target bytecode
1890     // r2: scratch
1891     __ cbz(r0, dispatch);     // test result -- no osr if null
1892     // nmethod may have been invalidated (VM may block upon call_VM return)
1893     __ ldrb(r2, Address(r0, nmethod::state_offset()));
1894     if (nmethod::in_use != 0)
1895       __ sub(r2, r2, nmethod::in_use);
1896     __ cbnz(r2, dispatch);
1897 
1898     // We have the address of an on stack replacement routine in r0
1899     // We need to prepare to execute the OSR method. First we must
1900     // migrate the locals and monitors off of the stack.
1901 
1902     __ mov(r19, r0);                             // save the nmethod
1903 
1904     JFR_ONLY(__ enter_jfr_critical_section();)
1905 
1906     call_VM(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin));
1907 
1908     // r0 is OSR buffer, move it to expected parameter location
1909     __ mov(j_rarg0, r0);
1910 
1911     // remove activation
1912     // get sender esp
1913     __ ldr(esp,
1914         Address(rfp, frame::interpreter_frame_sender_sp_offset * wordSize));
1915     // remove frame anchor
1916     __ leave();
1917 
1918     JFR_ONLY(__ leave_jfr_critical_section();)
1919 
1920     // Ensure compiled code always sees stack at proper alignment
1921     __ andr(sp, esp, -16);
1922 
1923     // and begin the OSR nmethod
1924     __ ldr(rscratch1, Address(r19, nmethod::osr_entry_point_offset()));
1925     __ br(rscratch1);
1926   }
1927 }
1928 
1929 
1930 void TemplateTable::if_0cmp(Condition cc)
1931 {
1932   transition(itos, vtos);
1933   // assume branch is more often taken than not (loops use backward branches)
1934   Label not_taken;
1935   if (cc == equal)
1936     __ cbnzw(r0, not_taken);
1937   else if (cc == not_equal)
1938     __ cbzw(r0, not_taken);
1939   else {
1940     __ andsw(zr, r0, r0);
1941     __ br(j_not(cc), not_taken);
1942   }
1943 
1944   branch(false, false);
1945   __ bind(not_taken);
1946   __ profile_not_taken_branch(r0);
1947 }
1948 
1949 void TemplateTable::if_icmp(Condition cc)
1950 {
1951   transition(itos, vtos);
1952   // assume branch is more often taken than not (loops use backward branches)
1953   Label not_taken;
1954   __ pop_i(r1);
1955   __ cmpw(r1, r0, Assembler::LSL);
1956   __ br(j_not(cc), not_taken);
1957   branch(false, false);
1958   __ bind(not_taken);
1959   __ profile_not_taken_branch(r0);
1960 }
1961 
1962 void TemplateTable::if_nullcmp(Condition cc)
1963 {
1964   transition(atos, vtos);
1965   // assume branch is more often taken than not (loops use backward branches)
1966   Label not_taken;
1967   if (cc == equal)
1968     __ cbnz(r0, not_taken);
1969   else
1970     __ cbz(r0, not_taken);
1971   branch(false, false);
1972   __ bind(not_taken);
1973   __ profile_not_taken_branch(r0);
1974 }
1975 
1976 void TemplateTable::if_acmp(Condition cc)
1977 {
1978   transition(atos, vtos);
1979   // assume branch is more often taken than not (loops use backward branches)
1980   Label not_taken;
1981   __ pop_ptr(r1);
1982   __ cmpoop(r1, r0);
1983   __ br(j_not(cc), not_taken);
1984   branch(false, false);
1985   __ bind(not_taken);
1986   __ profile_not_taken_branch(r0);
1987 }
1988 
1989 void TemplateTable::ret() {
1990   transition(vtos, vtos);
1991   locals_index(r1);
1992   __ ldr(r1, aaddress(r1)); // get return bci, compute return bcp
1993   __ profile_ret(r1, r2);
1994   __ ldr(rbcp, Address(rmethod, Method::const_offset()));
1995   __ lea(rbcp, Address(rbcp, r1));
1996   __ add(rbcp, rbcp, in_bytes(ConstMethod::codes_offset()));
1997   __ dispatch_next(vtos, 0, /*generate_poll*/true);
1998 }
1999 
2000 void TemplateTable::wide_ret() {
2001   transition(vtos, vtos);
2002   locals_index_wide(r1);
2003   __ ldr(r1, aaddress(r1)); // get return bci, compute return bcp
2004   __ profile_ret(r1, r2);
2005   __ ldr(rbcp, Address(rmethod, Method::const_offset()));
2006   __ lea(rbcp, Address(rbcp, r1));
2007   __ add(rbcp, rbcp, in_bytes(ConstMethod::codes_offset()));
2008   __ dispatch_next(vtos, 0, /*generate_poll*/true);
2009 }
2010 
2011 
2012 void TemplateTable::tableswitch() {
2013   Label default_case, continue_execution;
2014   transition(itos, vtos);
2015   // align rbcp
2016   __ lea(r1, at_bcp(BytesPerInt));
2017   __ andr(r1, r1, -BytesPerInt);
2018   // load lo & hi
2019   __ ldrw(r2, Address(r1, BytesPerInt));
2020   __ ldrw(r3, Address(r1, 2 * BytesPerInt));
2021   __ rev32(r2, r2);
2022   __ rev32(r3, r3);
2023   // check against lo & hi
2024   __ cmpw(r0, r2);
2025   __ br(Assembler::LT, default_case);
2026   __ cmpw(r0, r3);
2027   __ br(Assembler::GT, default_case);
2028   // lookup dispatch offset
2029   __ subw(r0, r0, r2);
2030   __ lea(r3, Address(r1, r0, Address::uxtw(2)));
2031   __ ldrw(r3, Address(r3, 3 * BytesPerInt));
2032   __ profile_switch_case(r0, r1, r2);
2033   // continue execution
2034   __ bind(continue_execution);
2035   __ rev32(r3, r3);
2036   __ load_unsigned_byte(rscratch1, Address(rbcp, r3, Address::sxtw(0)));
2037   __ add(rbcp, rbcp, r3, ext::sxtw);
2038   __ dispatch_only(vtos, /*generate_poll*/true);
2039   // handle default
2040   __ bind(default_case);
2041   __ profile_switch_default(r0);
2042   __ ldrw(r3, Address(r1, 0));
2043   __ b(continue_execution);
2044 }
2045 
2046 void TemplateTable::lookupswitch() {
2047   transition(itos, itos);
2048   __ stop("lookupswitch bytecode should have been rewritten");
2049 }
2050 
2051 void TemplateTable::fast_linearswitch() {
2052   transition(itos, vtos);
2053   Label loop_entry, loop, found, continue_execution;
2054   // bswap r0 so we can avoid bswapping the table entries
2055   __ rev32(r0, r0);
2056   // align rbcp
2057   __ lea(r19, at_bcp(BytesPerInt)); // btw: should be able to get rid of
2058                                     // this instruction (change offsets
2059                                     // below)
2060   __ andr(r19, r19, -BytesPerInt);
2061   // set counter
2062   __ ldrw(r1, Address(r19, BytesPerInt));
2063   __ rev32(r1, r1);
2064   __ b(loop_entry);
2065   // table search
2066   __ bind(loop);
2067   __ lea(rscratch1, Address(r19, r1, Address::lsl(3)));
2068   __ ldrw(rscratch1, Address(rscratch1, 2 * BytesPerInt));
2069   __ cmpw(r0, rscratch1);
2070   __ br(Assembler::EQ, found);
2071   __ bind(loop_entry);
2072   __ subs(r1, r1, 1);
2073   __ br(Assembler::PL, loop);
2074   // default case
2075   __ profile_switch_default(r0);
2076   __ ldrw(r3, Address(r19, 0));
2077   __ b(continue_execution);
2078   // entry found -> get offset
2079   __ bind(found);
2080   __ lea(rscratch1, Address(r19, r1, Address::lsl(3)));
2081   __ ldrw(r3, Address(rscratch1, 3 * BytesPerInt));
2082   __ profile_switch_case(r1, r0, r19);
2083   // continue execution
2084   __ bind(continue_execution);
2085   __ rev32(r3, r3);
2086   __ add(rbcp, rbcp, r3, ext::sxtw);
2087   __ ldrb(rscratch1, Address(rbcp, 0));
2088   __ dispatch_only(vtos, /*generate_poll*/true);
2089 }
2090 
2091 void TemplateTable::fast_binaryswitch() {
2092   transition(itos, vtos);
2093   // Implementation using the following core algorithm:
2094   //
2095   // int binary_search(int key, LookupswitchPair* array, int n) {
2096   //   // Binary search according to "Methodik des Programmierens" by
2097   //   // Edsger W. Dijkstra and W.H.J. Feijen, Addison Wesley Germany 1985.
2098   //   int i = 0;
2099   //   int j = n;
2100   //   while (i+1 < j) {
2101   //     // invariant P: 0 <= i < j <= n and (a[i] <= key < a[j] or Q)
2102   //     // with      Q: for all i: 0 <= i < n: key < a[i]
2103   //     // where a stands for the array and assuming that the (inexisting)
2104   //     // element a[n] is infinitely big.
2105   //     int h = (i + j) >> 1;
2106   //     // i < h < j
2107   //     if (key < array[h].fast_match()) {
2108   //       j = h;
2109   //     } else {
2110   //       i = h;
2111   //     }
2112   //   }
2113   //   // R: a[i] <= key < a[i+1] or Q
2114   //   // (i.e., if key is within array, i is the correct index)
2115   //   return i;
2116   // }
2117 
2118   // Register allocation
2119   const Register key   = r0; // already set (tosca)
2120   const Register array = r1;
2121   const Register i     = r2;
2122   const Register j     = r3;
2123   const Register h     = rscratch1;
2124   const Register temp  = rscratch2;
2125 
2126   // Find array start
2127   __ lea(array, at_bcp(3 * BytesPerInt)); // btw: should be able to
2128                                           // get rid of this
2129                                           // instruction (change
2130                                           // offsets below)
2131   __ andr(array, array, -BytesPerInt);
2132 
2133   // Initialize i & j
2134   __ mov(i, 0);                            // i = 0;
2135   __ ldrw(j, Address(array, -BytesPerInt)); // j = length(array);
2136 
2137   // Convert j into native byteordering
2138   __ rev32(j, j);
2139 
2140   // And start
2141   Label entry;
2142   __ b(entry);
2143 
2144   // binary search loop
2145   {
2146     Label loop;
2147     __ bind(loop);
2148     // int h = (i + j) >> 1;
2149     __ addw(h, i, j);                           // h = i + j;
2150     __ lsrw(h, h, 1);                                   // h = (i + j) >> 1;
2151     // if (key < array[h].fast_match()) {
2152     //   j = h;
2153     // } else {
2154     //   i = h;
2155     // }
2156     // Convert array[h].match to native byte-ordering before compare
2157     __ ldr(temp, Address(array, h, Address::lsl(3)));
2158     __ rev32(temp, temp);
2159     __ cmpw(key, temp);
2160     // j = h if (key <  array[h].fast_match())
2161     __ csel(j, h, j, Assembler::LT);
2162     // i = h if (key >= array[h].fast_match())
2163     __ csel(i, h, i, Assembler::GE);
2164     // while (i+1 < j)
2165     __ bind(entry);
2166     __ addw(h, i, 1);          // i+1
2167     __ cmpw(h, j);             // i+1 < j
2168     __ br(Assembler::LT, loop);
2169   }
2170 
2171   // end of binary search, result index is i (must check again!)
2172   Label default_case;
2173   // Convert array[i].match to native byte-ordering before compare
2174   __ ldr(temp, Address(array, i, Address::lsl(3)));
2175   __ rev32(temp, temp);
2176   __ cmpw(key, temp);
2177   __ br(Assembler::NE, default_case);
2178 
2179   // entry found -> j = offset
2180   __ add(j, array, i, ext::uxtx, 3);
2181   __ ldrw(j, Address(j, BytesPerInt));
2182   __ profile_switch_case(i, key, array);
2183   __ rev32(j, j);
2184   __ load_unsigned_byte(rscratch1, Address(rbcp, j, Address::sxtw(0)));
2185   __ lea(rbcp, Address(rbcp, j, Address::sxtw(0)));
2186   __ dispatch_only(vtos, /*generate_poll*/true);
2187 
2188   // default case -> j = default offset
2189   __ bind(default_case);
2190   __ profile_switch_default(i);
2191   __ ldrw(j, Address(array, -2 * BytesPerInt));
2192   __ rev32(j, j);
2193   __ load_unsigned_byte(rscratch1, Address(rbcp, j, Address::sxtw(0)));
2194   __ lea(rbcp, Address(rbcp, j, Address::sxtw(0)));
2195   __ dispatch_only(vtos, /*generate_poll*/true);
2196 }
2197 
2198 
2199 void TemplateTable::_return(TosState state)
2200 {
2201   transition(state, state);
2202   assert(_desc->calls_vm(),
2203          "inconsistent calls_vm information"); // call in remove_activation
2204 
2205   if (_desc->bytecode() == Bytecodes::_return_register_finalizer) {
2206     assert(state == vtos, "only valid state");
2207 
2208     __ ldr(c_rarg1, aaddress(0));
2209     __ load_klass(r3, c_rarg1);
2210     __ ldrb(r3, Address(r3, Klass::misc_flags_offset()));
2211     Label skip_register_finalizer;
2212     __ tbz(r3, exact_log2(KlassFlags::_misc_has_finalizer), skip_register_finalizer);
2213 
2214     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::register_finalizer), c_rarg1);
2215 
2216     __ bind(skip_register_finalizer);
2217   }
2218 
2219   // Issue a StoreStore barrier after all stores but before return
2220   // from any constructor for any class with a final field.  We don't
2221   // know if this is a finalizer, so we always do so.
2222   if (_desc->bytecode() == Bytecodes::_return)
2223     __ membar(MacroAssembler::StoreStore);
2224 
2225   if (_desc->bytecode() != Bytecodes::_return_register_finalizer) {
2226     Label no_safepoint;
2227     __ ldr(rscratch1, Address(rthread, JavaThread::polling_word_offset()));
2228     __ tbz(rscratch1, log2i_exact(SafepointMechanism::poll_bit()), no_safepoint);
2229     __ push(state);
2230     __ push_cont_fastpath(rthread);
2231     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint));
2232     __ pop_cont_fastpath(rthread);
2233     __ pop(state);
2234     __ bind(no_safepoint);
2235   }
2236 
2237   // Narrow result if state is itos but result type is smaller.
2238   // Need to narrow in the return bytecode rather than in generate_return_entry
2239   // since compiled code callers expect the result to already be narrowed.
2240   if (state == itos) {
2241     __ narrow(r0);
2242   }
2243 
2244   __ remove_activation(state);
2245   __ ret(lr);
2246 }
2247 
2248 // ----------------------------------------------------------------------------
2249 // Volatile variables demand their effects be made known to all CPU's
2250 // in order.  Store buffers on most chips allow reads & writes to
2251 // reorder; the JMM's ReadAfterWrite.java test fails in -Xint mode
2252 // without some kind of memory barrier (i.e., it's not sufficient that
2253 // the interpreter does not reorder volatile references, the hardware
2254 // also must not reorder them).
2255 //
2256 // According to the new Java Memory Model (JMM):
2257 // (1) All volatiles are serialized wrt to each other.  ALSO reads &
2258 //     writes act as acquire & release, so:
2259 // (2) A read cannot let unrelated NON-volatile memory refs that
2260 //     happen after the read float up to before the read.  It's OK for
2261 //     non-volatile memory refs that happen before the volatile read to
2262 //     float down below it.
2263 // (3) Similar a volatile write cannot let unrelated NON-volatile
2264 //     memory refs that happen BEFORE the write float down to after the
2265 //     write.  It's OK for non-volatile memory refs that happen after the
2266 //     volatile write to float up before it.
2267 //
2268 // We only put in barriers around volatile refs (they are expensive),
2269 // not _between_ memory refs (that would require us to track the
2270 // flavor of the previous memory refs).  Requirements (2) and (3)
2271 // require some barriers before volatile stores and after volatile
2272 // loads.  These nearly cover requirement (1) but miss the
2273 // volatile-store-volatile-load case.  This final case is placed after
2274 // volatile-stores although it could just as well go before
2275 // volatile-loads.
2276 
2277 void TemplateTable::resolve_cache_and_index_for_method(int byte_no,
2278                                             Register Rcache,
2279                                             Register index) {
2280   const Register temp = r19;
2281   assert_different_registers(Rcache, index, temp);
2282   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2283 
2284   Label resolved, clinit_barrier_slow;
2285 
2286   Bytecodes::Code code = bytecode();
2287   __ load_method_entry(Rcache, index);
2288   switch(byte_no) {
2289     case f1_byte:
2290       __ lea(temp, Address(Rcache, in_bytes(ResolvedMethodEntry::bytecode1_offset())));
2291       break;
2292     case f2_byte:
2293       __ lea(temp, Address(Rcache, in_bytes(ResolvedMethodEntry::bytecode2_offset())));
2294       break;
2295   }
2296   // Load-acquire the bytecode to match store-release in InterpreterRuntime
2297   __ ldarb(temp, temp);
2298   __ subs(zr, temp, (int) code);  // have we resolved this bytecode?
2299   __ br(Assembler::EQ, resolved);
2300 
2301   // resolve first time through
2302   // Class initialization barrier slow path lands here as well.
2303   __ bind(clinit_barrier_slow);
2304   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2305   __ mov(temp, (int) code);
2306   __ call_VM(noreg, entry, temp);
2307 
2308   // Update registers with resolved info
2309   __ load_method_entry(Rcache, index);
2310   // n.b. unlike x86 Rcache is now rcpool plus the indexed offset
2311   // so all clients ofthis method must be modified accordingly
2312   __ bind(resolved);
2313 
2314   // Class initialization barrier for static methods
2315   if (VM_Version::supports_fast_class_init_checks() && bytecode() == Bytecodes::_invokestatic) {
2316     __ ldr(temp, Address(Rcache, in_bytes(ResolvedMethodEntry::method_offset())));
2317     __ load_method_holder(temp, temp);
2318     __ clinit_barrier(temp, rscratch1, nullptr, &clinit_barrier_slow);
2319   }
2320 }
2321 
2322 void TemplateTable::resolve_cache_and_index_for_field(int byte_no,
2323                                             Register Rcache,
2324                                             Register index) {
2325   const Register temp = r19;
2326   assert_different_registers(Rcache, index, temp);
2327 
2328   Label resolved, clinit_barrier_slow;
2329 
2330   Bytecodes::Code code = bytecode();
2331   switch (code) {
2332   case Bytecodes::_nofast_getfield: code = Bytecodes::_getfield; break;
2333   case Bytecodes::_nofast_putfield: code = Bytecodes::_putfield; break;
2334   default: break;
2335   }
2336 
2337   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2338   __ load_field_entry(Rcache, index);
2339   if (byte_no == f1_byte) {
2340     __ lea(temp, Address(Rcache, in_bytes(ResolvedFieldEntry::get_code_offset())));
2341   } else {
2342     __ lea(temp, Address(Rcache, in_bytes(ResolvedFieldEntry::put_code_offset())));
2343   }
2344   // Load-acquire the bytecode to match store-release in ResolvedFieldEntry::fill_in()
2345   __ ldarb(temp, temp);
2346   __ subs(zr, temp, (int) code);  // have we resolved this bytecode?
2347   __ br(Assembler::EQ, resolved);
2348 
2349   // resolve first time through
2350   __ bind(clinit_barrier_slow);
2351   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2352   __ mov(temp, (int) code);
2353   __ call_VM(noreg, entry, temp);
2354 
2355   // Update registers with resolved info
2356   __ load_field_entry(Rcache, index);
2357   __ bind(resolved);
2358 
2359   // Class initialization barrier for static fields
2360   if (VM_Version::supports_fast_class_init_checks() &&
2361       (bytecode() == Bytecodes::_getstatic || bytecode() == Bytecodes::_putstatic)) {
2362     __ ldr(temp, Address(Rcache, ResolvedFieldEntry::field_holder_offset()));
2363     __ clinit_barrier(temp, rscratch1, nullptr, &clinit_barrier_slow);
2364   }
2365 }
2366 
2367 void TemplateTable::load_resolved_field_entry(Register obj,
2368                                               Register cache,
2369                                               Register tos_state,
2370                                               Register offset,
2371                                               Register flags,
2372                                               bool is_static = false) {
2373   assert_different_registers(cache, tos_state, flags, offset);
2374 
2375   // Field offset
2376   __ load_sized_value(offset, Address(cache, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
2377 
2378   // Flags
2379   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedFieldEntry::flags_offset())));
2380 
2381   // TOS state
2382   if (tos_state != noreg) {
2383     __ load_unsigned_byte(tos_state, Address(cache, in_bytes(ResolvedFieldEntry::type_offset())));
2384   }
2385 
2386   // Klass overwrite register
2387   if (is_static) {
2388     __ ldr(obj, Address(cache, ResolvedFieldEntry::field_holder_offset()));
2389     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
2390     __ ldr(obj, Address(obj, mirror_offset));
2391     __ resolve_oop_handle(obj, r5, rscratch2);
2392   }
2393 }
2394 
2395 void TemplateTable::load_resolved_method_entry_special_or_static(Register cache,
2396                                                                  Register method,
2397                                                                  Register flags) {
2398 
2399   // setup registers
2400   const Register index = flags;
2401   assert_different_registers(method, cache, flags);
2402 
2403   // determine constant pool cache field offsets
2404   resolve_cache_and_index_for_method(f1_byte, cache, index);
2405   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2406   __ ldr(method, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2407 }
2408 
2409 void TemplateTable::load_resolved_method_entry_handle(Register cache,
2410                                                       Register method,
2411                                                       Register ref_index,
2412                                                       Register flags) {
2413   // setup registers
2414   const Register index = ref_index;
2415   assert_different_registers(method, flags);
2416   assert_different_registers(method, cache, index);
2417 
2418   // determine constant pool cache field offsets
2419   resolve_cache_and_index_for_method(f1_byte, cache, index);
2420   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2421 
2422   // maybe push appendix to arguments (just before return address)
2423   Label L_no_push;
2424   __ tbz(flags, ResolvedMethodEntry::has_appendix_shift, L_no_push);
2425   // invokehandle uses an index into the resolved references array
2426   __ load_unsigned_short(ref_index, Address(cache, in_bytes(ResolvedMethodEntry::resolved_references_index_offset())));
2427   // Push the appendix as a trailing parameter.
2428   // This must be done before we get the receiver,
2429   // since the parameter_size includes it.
2430   Register appendix = method;
2431   __ load_resolved_reference_at_index(appendix, ref_index);
2432   __ push(appendix);  // push appendix (MethodType, CallSite, etc.)
2433   __ bind(L_no_push);
2434 
2435   __ ldr(method, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2436 }
2437 
2438 void TemplateTable::load_resolved_method_entry_interface(Register cache,
2439                                                          Register klass,
2440                                                          Register method_or_table_index,
2441                                                          Register flags) {
2442   // setup registers
2443   const Register index = method_or_table_index;
2444   assert_different_registers(method_or_table_index, cache, flags);
2445 
2446   // determine constant pool cache field offsets
2447   resolve_cache_and_index_for_method(f1_byte, cache, index);
2448   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2449 
2450   // Invokeinterface can behave in different ways:
2451   // If calling a method from java.lang.Object, the forced virtual flag is true so the invocation will
2452   // behave like an invokevirtual call. The state of the virtual final flag will determine whether a method or
2453   // vtable index is placed in the register.
2454   // Otherwise, the registers will be populated with the klass and method.
2455 
2456   Label NotVirtual; Label NotVFinal; Label Done;
2457   __ tbz(flags, ResolvedMethodEntry::is_forced_virtual_shift, NotVirtual);
2458   __ tbz(flags, ResolvedMethodEntry::is_vfinal_shift, NotVFinal);
2459   __ ldr(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2460   __ b(Done);
2461 
2462   __ bind(NotVFinal);
2463   __ load_unsigned_short(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::table_index_offset())));
2464   __ b(Done);
2465 
2466   __ bind(NotVirtual);
2467   __ ldr(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2468   __ ldr(klass, Address(cache, in_bytes(ResolvedMethodEntry::klass_offset())));
2469   __ bind(Done);
2470 }
2471 
2472 void TemplateTable::load_resolved_method_entry_virtual(Register cache,
2473                                                        Register method_or_table_index,
2474                                                        Register flags) {
2475   // setup registers
2476   const Register index = flags;
2477   assert_different_registers(method_or_table_index, cache, flags);
2478 
2479   // determine constant pool cache field offsets
2480   resolve_cache_and_index_for_method(f2_byte, cache, index);
2481   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2482 
2483   // method_or_table_index can either be an itable index or a method depending on the virtual final flag
2484   Label NotVFinal; Label Done;
2485   __ tbz(flags, ResolvedMethodEntry::is_vfinal_shift, NotVFinal);
2486   __ ldr(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2487   __ b(Done);
2488 
2489   __ bind(NotVFinal);
2490   __ load_unsigned_short(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::table_index_offset())));
2491   __ bind(Done);
2492 }
2493 
2494 // The rmethod register is input and overwritten to be the adapter method for the
2495 // indy call. Link Register (lr) is set to the return address for the adapter and
2496 // an appendix may be pushed to the stack. Registers r0-r3 are clobbered
2497 void TemplateTable::load_invokedynamic_entry(Register method) {
2498   // setup registers
2499   const Register appendix = r0;
2500   const Register cache = r2;
2501   const Register index = r3;
2502   assert_different_registers(method, appendix, cache, index, rcpool);
2503 
2504   __ save_bcp();
2505 
2506   Label resolved;
2507 
2508   __ load_resolved_indy_entry(cache, index);
2509   // Load-acquire the adapter method to match store-release in ResolvedIndyEntry::fill_in()
2510   __ lea(method, Address(cache, in_bytes(ResolvedIndyEntry::method_offset())));
2511   __ ldar(method, method);
2512 
2513   // Compare the method to zero
2514   __ cbnz(method, resolved);
2515 
2516   Bytecodes::Code code = bytecode();
2517 
2518   // Call to the interpreter runtime to resolve invokedynamic
2519   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2520   __ mov(method, code); // this is essentially Bytecodes::_invokedynamic
2521   __ call_VM(noreg, entry, method);
2522   // Update registers with resolved info
2523   __ load_resolved_indy_entry(cache, index);
2524   // Load-acquire the adapter method to match store-release in ResolvedIndyEntry::fill_in()
2525   __ lea(method, Address(cache, in_bytes(ResolvedIndyEntry::method_offset())));
2526   __ ldar(method, method);
2527 
2528 #ifdef ASSERT
2529   __ cbnz(method, resolved);
2530   __ stop("Should be resolved by now");
2531 #endif // ASSERT
2532   __ bind(resolved);
2533 
2534   Label L_no_push;
2535   // Check if there is an appendix
2536   __ load_unsigned_byte(index, Address(cache, in_bytes(ResolvedIndyEntry::flags_offset())));
2537   __ tbz(index, ResolvedIndyEntry::has_appendix_shift, L_no_push);
2538 
2539   // Get appendix
2540   __ load_unsigned_short(index, Address(cache, in_bytes(ResolvedIndyEntry::resolved_references_index_offset())));
2541   // Push the appendix as a trailing parameter
2542   // since the parameter_size includes it.
2543   __ push(method);
2544   __ mov(method, index);
2545   __ load_resolved_reference_at_index(appendix, method);
2546   __ verify_oop(appendix);
2547   __ pop(method);
2548   __ push(appendix);  // push appendix (MethodType, CallSite, etc.)
2549   __ bind(L_no_push);
2550 
2551   // compute return type
2552   __ load_unsigned_byte(index, Address(cache, in_bytes(ResolvedIndyEntry::result_type_offset())));
2553   // load return address
2554   // Return address is loaded into link register(lr) and not pushed to the stack
2555   // like x86
2556   {
2557     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
2558     __ mov(rscratch1, table_addr);
2559     __ ldr(lr, Address(rscratch1, index, Address::lsl(3)));
2560   }
2561 }
2562 
2563 // The registers cache and index expected to be set before call.
2564 // Correct values of the cache and index registers are preserved.
2565 void TemplateTable::jvmti_post_field_access(Register cache, Register index,
2566                                             bool is_static, bool has_tos) {
2567   // do the JVMTI work here to avoid disturbing the register state below
2568   // We use c_rarg registers here because we want to use the register used in
2569   // the call to the VM
2570   if (JvmtiExport::can_post_field_access()) {
2571     // Check to see if a field access watch has been set before we
2572     // take the time to call into the VM.
2573     Label L1;
2574     assert_different_registers(cache, index, r0);
2575     __ lea(rscratch1, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
2576     __ ldrw(r0, Address(rscratch1));
2577     __ cbzw(r0, L1);
2578 
2579     __ load_field_entry(c_rarg2, index);
2580 
2581     if (is_static) {
2582       __ mov(c_rarg1, zr); // null object reference
2583     } else {
2584       __ ldr(c_rarg1, at_tos()); // get object pointer without popping it
2585       __ verify_oop(c_rarg1);
2586     }
2587     // c_rarg1: object pointer or null
2588     // c_rarg2: cache entry pointer
2589     __ call_VM(noreg, CAST_FROM_FN_PTR(address,
2590                                        InterpreterRuntime::post_field_access),
2591                c_rarg1, c_rarg2);
2592     __ load_field_entry(cache, index);
2593     __ bind(L1);
2594   }
2595 }
2596 
2597 void TemplateTable::pop_and_check_object(Register r)
2598 {
2599   __ pop_ptr(r);
2600   __ null_check(r);  // for field access must check obj.
2601   __ verify_oop(r);
2602 }
2603 
2604 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc)
2605 {
2606   const Register cache     = r4;
2607   const Register obj       = r4;
2608   const Register index     = r3;
2609   const Register tos_state = r3;
2610   const Register off       = r19;
2611   const Register flags     = r6;
2612   const Register bc        = r4; // uses same reg as obj, so don't mix them
2613 
2614   resolve_cache_and_index_for_field(byte_no, cache, index);
2615   jvmti_post_field_access(cache, index, is_static, false);
2616   load_resolved_field_entry(obj, cache, tos_state, off, flags, is_static);
2617 
2618   if (!is_static) {
2619     // obj is on the stack
2620     pop_and_check_object(obj);
2621   }
2622 
2623   // 8179954: We need to make sure that the code generated for
2624   // volatile accesses forms a sequentially-consistent set of
2625   // operations when combined with STLR and LDAR.  Without a leading
2626   // membar it's possible for a simple Dekker test to fail if loads
2627   // use LDR;DMB but stores use STLR.  This can happen if C2 compiles
2628   // the stores in one method and we interpret the loads in another.
2629   if (!CompilerConfig::is_c1_or_interpreter_only_no_jvmci()){
2630     Label notVolatile;
2631     __ tbz(flags, ResolvedFieldEntry::is_volatile_shift, notVolatile);
2632     __ membar(MacroAssembler::AnyAny);
2633     __ bind(notVolatile);
2634   }
2635 
2636   const Address field(obj, off);
2637 
2638   Label Done, notByte, notBool, notInt, notShort, notChar,
2639               notLong, notFloat, notObj, notDouble;
2640 
2641   assert(btos == 0, "change code, btos != 0");
2642   __ cbnz(tos_state, notByte);
2643 
2644   // Don't rewrite getstatic, only getfield
2645   if (is_static) rc = may_not_rewrite;
2646 
2647   // btos
2648   __ access_load_at(T_BYTE, IN_HEAP, r0, field, noreg, noreg);
2649   __ push(btos);
2650   // Rewrite bytecode to be faster
2651   if (rc == may_rewrite) {
2652     patch_bytecode(Bytecodes::_fast_bgetfield, bc, r1);
2653   }
2654   __ b(Done);
2655 
2656   __ bind(notByte);
2657   __ cmp(tos_state, (u1)ztos);
2658   __ br(Assembler::NE, notBool);
2659 
2660   // ztos (same code as btos)
2661   __ access_load_at(T_BOOLEAN, IN_HEAP, r0, field, noreg, noreg);
2662   __ push(ztos);
2663   // Rewrite bytecode to be faster
2664   if (rc == may_rewrite) {
2665     // use btos rewriting, no truncating to t/f bit is needed for getfield.
2666     patch_bytecode(Bytecodes::_fast_bgetfield, bc, r1);
2667   }
2668   __ b(Done);
2669 
2670   __ bind(notBool);
2671   __ cmp(tos_state, (u1)atos);
2672   __ br(Assembler::NE, notObj);
2673   // atos
2674   do_oop_load(_masm, field, r0, IN_HEAP);
2675   __ push(atos);
2676   if (rc == may_rewrite) {
2677     patch_bytecode(Bytecodes::_fast_agetfield, bc, r1);
2678   }
2679   __ b(Done);
2680 
2681   __ bind(notObj);
2682   __ cmp(tos_state, (u1)itos);
2683   __ br(Assembler::NE, notInt);
2684   // itos
2685   __ access_load_at(T_INT, IN_HEAP, r0, field, noreg, noreg);
2686   __ push(itos);
2687   // Rewrite bytecode to be faster
2688   if (rc == may_rewrite) {
2689     patch_bytecode(Bytecodes::_fast_igetfield, bc, r1);
2690   }
2691   __ b(Done);
2692 
2693   __ bind(notInt);
2694   __ cmp(tos_state, (u1)ctos);
2695   __ br(Assembler::NE, notChar);
2696   // ctos
2697   __ access_load_at(T_CHAR, IN_HEAP, r0, field, noreg, noreg);
2698   __ push(ctos);
2699   // Rewrite bytecode to be faster
2700   if (rc == may_rewrite) {
2701     patch_bytecode(Bytecodes::_fast_cgetfield, bc, r1);
2702   }
2703   __ b(Done);
2704 
2705   __ bind(notChar);
2706   __ cmp(tos_state, (u1)stos);
2707   __ br(Assembler::NE, notShort);
2708   // stos
2709   __ access_load_at(T_SHORT, IN_HEAP, r0, field, noreg, noreg);
2710   __ push(stos);
2711   // Rewrite bytecode to be faster
2712   if (rc == may_rewrite) {
2713     patch_bytecode(Bytecodes::_fast_sgetfield, bc, r1);
2714   }
2715   __ b(Done);
2716 
2717   __ bind(notShort);
2718   __ cmp(tos_state, (u1)ltos);
2719   __ br(Assembler::NE, notLong);
2720   // ltos
2721   __ access_load_at(T_LONG, IN_HEAP, r0, field, noreg, noreg);
2722   __ push(ltos);
2723   // Rewrite bytecode to be faster
2724   if (rc == may_rewrite) {
2725     patch_bytecode(Bytecodes::_fast_lgetfield, bc, r1);
2726   }
2727   __ b(Done);
2728 
2729   __ bind(notLong);
2730   __ cmp(tos_state, (u1)ftos);
2731   __ br(Assembler::NE, notFloat);
2732   // ftos
2733   __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
2734   __ push(ftos);
2735   // Rewrite bytecode to be faster
2736   if (rc == may_rewrite) {
2737     patch_bytecode(Bytecodes::_fast_fgetfield, bc, r1);
2738   }
2739   __ b(Done);
2740 
2741   __ bind(notFloat);
2742 #ifdef ASSERT
2743   __ cmp(tos_state, (u1)dtos);
2744   __ br(Assembler::NE, notDouble);
2745 #endif
2746   // dtos
2747   __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
2748   __ push(dtos);
2749   // Rewrite bytecode to be faster
2750   if (rc == may_rewrite) {
2751     patch_bytecode(Bytecodes::_fast_dgetfield, bc, r1);
2752   }
2753 #ifdef ASSERT
2754   __ b(Done);
2755 
2756   __ bind(notDouble);
2757   __ stop("Bad state");
2758 #endif
2759 
2760   __ bind(Done);
2761 
2762   Label notVolatile;
2763   __ tbz(flags, ResolvedFieldEntry::is_volatile_shift, notVolatile);
2764   __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
2765   __ bind(notVolatile);
2766 }
2767 
2768 
2769 void TemplateTable::getfield(int byte_no)
2770 {
2771   getfield_or_static(byte_no, false);
2772 }
2773 
2774 void TemplateTable::nofast_getfield(int byte_no) {
2775   getfield_or_static(byte_no, false, may_not_rewrite);
2776 }
2777 
2778 void TemplateTable::getstatic(int byte_no)
2779 {
2780   getfield_or_static(byte_no, true);
2781 }
2782 
2783 // The registers cache and index expected to be set before call.
2784 // The function may destroy various registers, just not the cache and index registers.
2785 void TemplateTable::jvmti_post_field_mod(Register cache, Register index, bool is_static) {
2786   transition(vtos, vtos);
2787 
2788   if (JvmtiExport::can_post_field_modification()) {
2789     // Check to see if a field modification watch has been set before
2790     // we take the time to call into the VM.
2791     Label L1;
2792     assert_different_registers(cache, index, r0);
2793     __ lea(rscratch1, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
2794     __ ldrw(r0, Address(rscratch1));
2795     __ cbz(r0, L1);
2796 
2797     __ mov(c_rarg2, cache);
2798 
2799     if (is_static) {
2800       // Life is simple.  Null out the object pointer.
2801       __ mov(c_rarg1, zr);
2802     } else {
2803       // Life is harder. The stack holds the value on top, followed by
2804       // the object.  We don't know the size of the value, though; it
2805       // could be one or two words depending on its type. As a result,
2806       // we must find the type to determine where the object is.
2807       __ load_unsigned_byte(c_rarg3, Address(c_rarg2, in_bytes(ResolvedFieldEntry::type_offset())));
2808       Label nope2, done, ok;
2809       __ ldr(c_rarg1, at_tos_p1());  // initially assume a one word jvalue
2810       __ cmpw(c_rarg3, ltos);
2811       __ br(Assembler::EQ, ok);
2812       __ cmpw(c_rarg3, dtos);
2813       __ br(Assembler::NE, nope2);
2814       __ bind(ok);
2815       __ ldr(c_rarg1, at_tos_p2()); // ltos (two word jvalue)
2816       __ bind(nope2);
2817     }
2818     // object (tos)
2819     __ mov(c_rarg3, esp);
2820     // c_rarg1: object pointer set up above (null if static)
2821     // c_rarg2: cache entry pointer
2822     // c_rarg3: jvalue object on the stack
2823     __ call_VM(noreg,
2824                CAST_FROM_FN_PTR(address,
2825                                 InterpreterRuntime::post_field_modification),
2826                c_rarg1, c_rarg2, c_rarg3);
2827     __ load_field_entry(cache, index);
2828     __ bind(L1);
2829   }
2830 }
2831 
2832 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2833   transition(vtos, vtos);
2834 
2835   const Register cache     = r2;
2836   const Register index     = r3;
2837   const Register tos_state = r3;
2838   const Register obj       = r2;
2839   const Register off       = r19;
2840   const Register flags     = r0;
2841   const Register bc        = r4;
2842 
2843   resolve_cache_and_index_for_field(byte_no, cache, index);
2844   jvmti_post_field_mod(cache, index, is_static);
2845   load_resolved_field_entry(obj, cache, tos_state, off, flags, is_static);
2846 
2847   Label Done;
2848   __ mov(r5, flags);
2849 
2850   {
2851     Label notVolatile;
2852     __ tbz(r5, ResolvedFieldEntry::is_volatile_shift, notVolatile);
2853     __ membar(MacroAssembler::StoreStore | MacroAssembler::LoadStore);
2854     __ bind(notVolatile);
2855   }
2856 
2857   // field address
2858   const Address field(obj, off);
2859 
2860   Label notByte, notBool, notInt, notShort, notChar,
2861         notLong, notFloat, notObj, notDouble;
2862 
2863   assert(btos == 0, "change code, btos != 0");
2864   __ cbnz(tos_state, notByte);
2865 
2866   // Don't rewrite putstatic, only putfield
2867   if (is_static) rc = may_not_rewrite;
2868 
2869   // btos
2870   {
2871     __ pop(btos);
2872     if (!is_static) pop_and_check_object(obj);
2873     __ access_store_at(T_BYTE, IN_HEAP, field, r0, noreg, noreg, noreg);
2874     if (rc == may_rewrite) {
2875       patch_bytecode(Bytecodes::_fast_bputfield, bc, r1, true, byte_no);
2876     }
2877     __ b(Done);
2878   }
2879 
2880   __ bind(notByte);
2881   __ cmp(tos_state, (u1)ztos);
2882   __ br(Assembler::NE, notBool);
2883 
2884   // ztos
2885   {
2886     __ pop(ztos);
2887     if (!is_static) pop_and_check_object(obj);
2888     __ access_store_at(T_BOOLEAN, IN_HEAP, field, r0, noreg, noreg, noreg);
2889     if (rc == may_rewrite) {
2890       patch_bytecode(Bytecodes::_fast_zputfield, bc, r1, true, byte_no);
2891     }
2892     __ b(Done);
2893   }
2894 
2895   __ bind(notBool);
2896   __ cmp(tos_state, (u1)atos);
2897   __ br(Assembler::NE, notObj);
2898 
2899   // atos
2900   {
2901     __ pop(atos);
2902     if (!is_static) pop_and_check_object(obj);
2903     // Store into the field
2904     // Clobbers: r10, r11, r3
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, R5: flags
3101   load_resolved_field_entry(r2, r2, noreg, r1, r5);
3102 
3103   {
3104     Label notVolatile;
3105     __ tbz(r5, 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     // Clobbers: r10, r11, r3
3122     do_oop_store(_masm, field, r0, IN_HEAP);
3123     break;
3124   case Bytecodes::_fast_lputfield:
3125     __ access_store_at(T_LONG, IN_HEAP, field, r0, noreg, noreg, noreg);
3126     break;
3127   case Bytecodes::_fast_iputfield:
3128     __ access_store_at(T_INT, IN_HEAP, field, r0, noreg, noreg, noreg);
3129     break;
3130   case Bytecodes::_fast_zputfield:
3131     __ access_store_at(T_BOOLEAN, IN_HEAP, field, r0, noreg, noreg, noreg);
3132     break;
3133   case Bytecodes::_fast_bputfield:
3134     __ access_store_at(T_BYTE, IN_HEAP, field, r0, noreg, noreg, noreg);
3135     break;
3136   case Bytecodes::_fast_sputfield:
3137     __ access_store_at(T_SHORT, IN_HEAP, field, r0, noreg, noreg, noreg);
3138     break;
3139   case Bytecodes::_fast_cputfield:
3140     __ access_store_at(T_CHAR, IN_HEAP, field, r0, noreg, noreg, noreg);
3141     break;
3142   case Bytecodes::_fast_fputfield:
3143     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos */, noreg, noreg, noreg);
3144     break;
3145   case Bytecodes::_fast_dputfield:
3146     __ access_store_at(T_DOUBLE, IN_HEAP, field, noreg /* dtos */, noreg, noreg, noreg);
3147     break;
3148   default:
3149     ShouldNotReachHere();
3150   }
3151 
3152   {
3153     Label notVolatile;
3154     __ tbz(r5, ResolvedFieldEntry::is_volatile_shift, notVolatile);
3155     __ membar(MacroAssembler::StoreLoad | MacroAssembler::StoreStore);
3156     __ bind(notVolatile);
3157   }
3158 }
3159 
3160 
3161 void TemplateTable::fast_accessfield(TosState state)
3162 {
3163   transition(atos, state);
3164   // Do the JVMTI work here to avoid disturbing the register state below
3165   if (JvmtiExport::can_post_field_access()) {
3166     // Check to see if a field access watch has been set before we
3167     // take the time to call into the VM.
3168     Label L1;
3169     __ lea(rscratch1, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
3170     __ ldrw(r2, Address(rscratch1));
3171     __ cbzw(r2, L1);
3172     // access constant pool cache entry
3173     __ load_field_entry(c_rarg2, rscratch2);
3174     __ verify_oop(r0);
3175     __ push_ptr(r0);  // save object pointer before call_VM() clobbers it
3176     __ mov(c_rarg1, r0);
3177     // c_rarg1: object pointer copied above
3178     // c_rarg2: cache entry pointer
3179     __ call_VM(noreg,
3180                CAST_FROM_FN_PTR(address,
3181                                 InterpreterRuntime::post_field_access),
3182                c_rarg1, c_rarg2);
3183     __ pop_ptr(r0); // restore object pointer
3184     __ bind(L1);
3185   }
3186 
3187   // access constant pool cache
3188   __ load_field_entry(r2, r1);
3189 
3190   __ load_sized_value(r1, Address(r2, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
3191   __ load_unsigned_byte(r3, Address(r2, in_bytes(ResolvedFieldEntry::flags_offset())));
3192 
3193   // r0: object
3194   __ verify_oop(r0);
3195   __ null_check(r0);
3196   const Address field(r0, r1);
3197 
3198   // 8179954: We need to make sure that the code generated for
3199   // volatile accesses forms a sequentially-consistent set of
3200   // operations when combined with STLR and LDAR.  Without a leading
3201   // membar it's possible for a simple Dekker test to fail if loads
3202   // use LDR;DMB but stores use STLR.  This can happen if C2 compiles
3203   // the stores in one method and we interpret the loads in another.
3204   if (!CompilerConfig::is_c1_or_interpreter_only_no_jvmci()) {
3205     Label notVolatile;
3206     __ tbz(r3, ResolvedFieldEntry::is_volatile_shift, notVolatile);
3207     __ membar(MacroAssembler::AnyAny);
3208     __ bind(notVolatile);
3209   }
3210 
3211   // access field
3212   switch (bytecode()) {
3213   case Bytecodes::_fast_agetfield:
3214     do_oop_load(_masm, field, r0, IN_HEAP);
3215     __ verify_oop(r0);
3216     break;
3217   case Bytecodes::_fast_lgetfield:
3218     __ access_load_at(T_LONG, IN_HEAP, r0, field, noreg, noreg);
3219     break;
3220   case Bytecodes::_fast_igetfield:
3221     __ access_load_at(T_INT, IN_HEAP, r0, field, noreg, noreg);
3222     break;
3223   case Bytecodes::_fast_bgetfield:
3224     __ access_load_at(T_BYTE, IN_HEAP, r0, field, noreg, noreg);
3225     break;
3226   case Bytecodes::_fast_sgetfield:
3227     __ access_load_at(T_SHORT, IN_HEAP, r0, field, noreg, noreg);
3228     break;
3229   case Bytecodes::_fast_cgetfield:
3230     __ access_load_at(T_CHAR, IN_HEAP, r0, field, noreg, noreg);
3231     break;
3232   case Bytecodes::_fast_fgetfield:
3233     __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3234     break;
3235   case Bytecodes::_fast_dgetfield:
3236     __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* dtos */, field, noreg, noreg);
3237     break;
3238   default:
3239     ShouldNotReachHere();
3240   }
3241   {
3242     Label notVolatile;
3243     __ tbz(r3, ResolvedFieldEntry::is_volatile_shift, notVolatile);
3244     __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
3245     __ bind(notVolatile);
3246   }
3247 }
3248 
3249 void TemplateTable::fast_xaccess(TosState state)
3250 {
3251   transition(vtos, state);
3252 
3253   // get receiver
3254   __ ldr(r0, aaddress(0));
3255   // access constant pool cache
3256   __ load_field_entry(r2, r3, 2);
3257   __ load_sized_value(r1, Address(r2, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
3258 
3259   // 8179954: We need to make sure that the code generated for
3260   // volatile accesses forms a sequentially-consistent set of
3261   // operations when combined with STLR and LDAR.  Without a leading
3262   // membar it's possible for a simple Dekker test to fail if loads
3263   // use LDR;DMB but stores use STLR.  This can happen if C2 compiles
3264   // the stores in one method and we interpret the loads in another.
3265   if (!CompilerConfig::is_c1_or_interpreter_only_no_jvmci()) {
3266     Label notVolatile;
3267     __ load_unsigned_byte(r3, Address(r2, in_bytes(ResolvedFieldEntry::flags_offset())));
3268     __ tbz(r3, ResolvedFieldEntry::is_volatile_shift, notVolatile);
3269     __ membar(MacroAssembler::AnyAny);
3270     __ bind(notVolatile);
3271   }
3272 
3273   // make sure exception is reported in correct bcp range (getfield is
3274   // next instruction)
3275   __ increment(rbcp);
3276   __ null_check(r0);
3277   switch (state) {
3278   case itos:
3279     __ access_load_at(T_INT, IN_HEAP, r0, Address(r0, r1, Address::lsl(0)), noreg, noreg);
3280     break;
3281   case atos:
3282     do_oop_load(_masm, Address(r0, r1, Address::lsl(0)), r0, IN_HEAP);
3283     __ verify_oop(r0);
3284     break;
3285   case ftos:
3286     __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, Address(r0, r1, Address::lsl(0)), noreg, noreg);
3287     break;
3288   default:
3289     ShouldNotReachHere();
3290   }
3291 
3292   {
3293     Label notVolatile;
3294     __ load_unsigned_byte(r3, Address(r2, in_bytes(ResolvedFieldEntry::flags_offset())));
3295     __ tbz(r3, ResolvedFieldEntry::is_volatile_shift, notVolatile);
3296     __ membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
3297     __ bind(notVolatile);
3298   }
3299 
3300   __ decrement(rbcp);
3301 }
3302 
3303 
3304 
3305 //-----------------------------------------------------------------------------
3306 // Calls
3307 
3308 void TemplateTable::prepare_invoke(Register cache, Register recv) {
3309 
3310   Bytecodes::Code code = bytecode();
3311   const bool load_receiver       = (code != Bytecodes::_invokestatic) && (code != Bytecodes::_invokedynamic);
3312 
3313   // save 'interpreter return address'
3314   __ save_bcp();
3315 
3316   // Load TOS state for later
3317   __ load_unsigned_byte(rscratch2, Address(cache, in_bytes(ResolvedMethodEntry::type_offset())));
3318 
3319   // load receiver if needed (note: no return address pushed yet)
3320   if (load_receiver) {
3321     __ load_unsigned_short(recv, Address(cache, in_bytes(ResolvedMethodEntry::num_parameters_offset())));
3322     __ add(rscratch1, esp, recv, ext::uxtx, 3);
3323     __ ldr(recv, Address(rscratch1, -Interpreter::expr_offset_in_bytes(1)));
3324     __ verify_oop(recv);
3325   }
3326 
3327   // load return address
3328   {
3329     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
3330     __ mov(rscratch1, table_addr);
3331     __ ldr(lr, Address(rscratch1, rscratch2, Address::lsl(3)));
3332   }
3333 }
3334 
3335 
3336 void TemplateTable::invokevirtual_helper(Register index,
3337                                          Register recv,
3338                                          Register flags)
3339 {
3340   // Uses temporary registers r0, r3
3341   assert_different_registers(index, recv, r0, r3);
3342   // Test for an invoke of a final method
3343   Label notFinal;
3344   __ tbz(flags, ResolvedMethodEntry::is_vfinal_shift, notFinal);
3345 
3346   const Register method = index;  // method must be rmethod
3347   assert(method == rmethod,
3348          "Method must be rmethod for interpreter calling convention");
3349 
3350   // do the call - the index is actually the method to call
3351   // that is, f2 is a vtable index if !is_vfinal, else f2 is a Method*
3352 
3353   // It's final, need a null check here!
3354   __ null_check(recv);
3355 
3356   // profile this call
3357   __ profile_final_call(r0);
3358   __ profile_arguments_type(r0, method, r4, true);
3359 
3360   __ jump_from_interpreted(method, r0);
3361 
3362   __ bind(notFinal);
3363 
3364   // get receiver klass
3365   __ load_klass(r0, recv);
3366 
3367   // profile this call
3368   __ profile_virtual_call(r0, rlocals, r3);
3369 
3370   // get target Method & entry point
3371   __ lookup_virtual_method(r0, index, method);
3372   __ profile_arguments_type(r3, method, r4, true);
3373   // FIXME -- this looks completely redundant. is it?
3374   // __ ldr(r3, Address(method, Method::interpreter_entry_offset()));
3375   __ jump_from_interpreted(method, r3);
3376 }
3377 
3378 void TemplateTable::invokevirtual(int byte_no)
3379 {
3380   transition(vtos, vtos);
3381   assert(byte_no == f2_byte, "use this argument");
3382 
3383   load_resolved_method_entry_virtual(r2,      // ResolvedMethodEntry*
3384                                      rmethod, // Method* or itable index
3385                                      r3);     // flags
3386   prepare_invoke(r2, r2); // recv
3387 
3388   // rmethod: index (actually a Method*)
3389   // r2: receiver
3390   // r3: flags
3391 
3392   invokevirtual_helper(rmethod, r2, r3);
3393 }
3394 
3395 void TemplateTable::invokespecial(int byte_no)
3396 {
3397   transition(vtos, vtos);
3398   assert(byte_no == f1_byte, "use this argument");
3399 
3400   load_resolved_method_entry_special_or_static(r2,      // ResolvedMethodEntry*
3401                                                rmethod, // Method*
3402                                                r3);     // flags
3403   prepare_invoke(r2, r2);  // get receiver also for null check
3404   __ verify_oop(r2);
3405   __ null_check(r2);
3406   // do the call
3407   __ profile_call(r0);
3408   __ profile_arguments_type(r0, rmethod, rbcp, false);
3409   __ jump_from_interpreted(rmethod, r0);
3410 }
3411 
3412 void TemplateTable::invokestatic(int byte_no)
3413 {
3414   transition(vtos, vtos);
3415   assert(byte_no == f1_byte, "use this argument");
3416 
3417   load_resolved_method_entry_special_or_static(r2,      // ResolvedMethodEntry*
3418                                                rmethod, // Method*
3419                                                r3);     // flags
3420   prepare_invoke(r2, r2);  // get receiver also for null check
3421 
3422   // do the call
3423   __ profile_call(r0);
3424   __ profile_arguments_type(r0, rmethod, r4, false);
3425   __ jump_from_interpreted(rmethod, r0);
3426 }
3427 
3428 void TemplateTable::fast_invokevfinal(int byte_no)
3429 {
3430   __ call_Unimplemented();
3431 }
3432 
3433 void TemplateTable::invokeinterface(int byte_no) {
3434   transition(vtos, vtos);
3435   assert(byte_no == f1_byte, "use this argument");
3436 
3437   load_resolved_method_entry_interface(r2,      // ResolvedMethodEntry*
3438                                        r0,      // Klass*
3439                                        rmethod, // Method* or itable/vtable index
3440                                        r3);     // flags
3441   prepare_invoke(r2, r2); // receiver
3442 
3443   // r0: interface klass (from f1)
3444   // rmethod: method (from f2)
3445   // r2: receiver
3446   // r3: flags
3447 
3448   // First check for Object case, then private interface method,
3449   // then regular interface method.
3450 
3451   // Special case of invokeinterface called for virtual method of
3452   // java.lang.Object.  See cpCache.cpp for details.
3453   Label notObjectMethod;
3454   __ tbz(r3, ResolvedMethodEntry::is_forced_virtual_shift, notObjectMethod);
3455 
3456   invokevirtual_helper(rmethod, r2, r3);
3457   __ bind(notObjectMethod);
3458 
3459   Label no_such_interface;
3460 
3461   // Check for private method invocation - indicated by vfinal
3462   Label notVFinal;
3463   __ tbz(r3, ResolvedMethodEntry::is_vfinal_shift, notVFinal);
3464 
3465   // Get receiver klass into r3
3466   __ load_klass(r3, r2);
3467 
3468   Label subtype;
3469   __ check_klass_subtype(r3, r0, r4, subtype);
3470   // If we get here the typecheck failed
3471   __ b(no_such_interface);
3472   __ bind(subtype);
3473 
3474   __ profile_final_call(r0);
3475   __ profile_arguments_type(r0, rmethod, r4, true);
3476   __ jump_from_interpreted(rmethod, r0);
3477 
3478   __ bind(notVFinal);
3479 
3480   // Get receiver klass into r3
3481   __ restore_locals();
3482   __ load_klass(r3, r2);
3483 
3484   Label no_such_method;
3485 
3486   // Preserve method for throw_AbstractMethodErrorVerbose.
3487   __ mov(r16, rmethod);
3488   // Receiver subtype check against REFC.
3489   // Superklass in r0. Subklass in r3. Blows rscratch2, r13
3490   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3491                              r3, r0, noreg,
3492                              // outputs: scan temp. reg, scan temp. reg
3493                              rscratch2, r13,
3494                              no_such_interface,
3495                              /*return_method=*/false);
3496 
3497   // profile this call
3498   __ profile_virtual_call(r3, r13, r19);
3499 
3500   // Get declaring interface class from method, and itable index
3501 
3502   __ load_method_holder(r0, rmethod);
3503   __ ldrw(rmethod, Address(rmethod, Method::itable_index_offset()));
3504   __ subw(rmethod, rmethod, Method::itable_index_max);
3505   __ negw(rmethod, rmethod);
3506 
3507   // Preserve recvKlass for throw_AbstractMethodErrorVerbose.
3508   __ mov(rlocals, r3);
3509   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3510                              rlocals, r0, rmethod,
3511                              // outputs: method, scan temp. reg
3512                              rmethod, r13,
3513                              no_such_interface);
3514 
3515   // rmethod,: Method to call
3516   // r2: receiver
3517   // Check for abstract method error
3518   // Note: This should be done more efficiently via a throw_abstract_method_error
3519   //       interpreter entry point and a conditional jump to it in case of a null
3520   //       method.
3521   __ cbz(rmethod, no_such_method);
3522 
3523   __ profile_arguments_type(r3, rmethod, r13, true);
3524 
3525   // do the call
3526   // r2: receiver
3527   // rmethod,: Method
3528   __ jump_from_interpreted(rmethod, r3);
3529   __ should_not_reach_here();
3530 
3531   // exception handling code follows...
3532   // note: must restore interpreter registers to canonical
3533   //       state for exception handling to work correctly!
3534 
3535   __ bind(no_such_method);
3536   // throw exception
3537   __ restore_bcp();      // bcp must be correct for exception handler   (was destroyed)
3538   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3539   // Pass arguments for generating a verbose error message.
3540   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorVerbose), r3, r16);
3541   // the call_VM checks for exception, so we should never return here.
3542   __ should_not_reach_here();
3543 
3544   __ bind(no_such_interface);
3545   // throw exception
3546   __ restore_bcp();      // bcp must be correct for exception handler   (was destroyed)
3547   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3548   // Pass arguments for generating a verbose error message.
3549   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
3550                    InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose), r3, r0);
3551   // the call_VM checks for exception, so we should never return here.
3552   __ should_not_reach_here();
3553   return;
3554 }
3555 
3556 void TemplateTable::invokehandle(int byte_no) {
3557   transition(vtos, vtos);
3558   assert(byte_no == f1_byte, "use this argument");
3559 
3560   load_resolved_method_entry_handle(r2,      // ResolvedMethodEntry*
3561                                     rmethod, // Method*
3562                                     r0,      // Resolved reference
3563                                     r3);     // flags
3564   prepare_invoke(r2, r2);
3565 
3566   __ verify_method_ptr(r2);
3567   __ verify_oop(r2);
3568   __ null_check(r2);
3569 
3570   // FIXME: profile the LambdaForm also
3571 
3572   // r13 is safe to use here as a scratch reg because it is about to
3573   // be clobbered by jump_from_interpreted().
3574   __ profile_final_call(r13);
3575   __ profile_arguments_type(r13, rmethod, r4, true);
3576 
3577   __ jump_from_interpreted(rmethod, r0);
3578 }
3579 
3580 void TemplateTable::invokedynamic(int byte_no) {
3581   transition(vtos, vtos);
3582   assert(byte_no == f1_byte, "use this argument");
3583 
3584   load_invokedynamic_entry(rmethod);
3585 
3586   // r0: CallSite object (from cpool->resolved_references[])
3587   // rmethod: MH.linkToCallSite method
3588 
3589   // Note:  r0_callsite is already pushed
3590 
3591   // %%% should make a type profile for any invokedynamic that takes a ref argument
3592   // profile this call
3593   __ profile_call(rbcp);
3594   __ profile_arguments_type(r3, rmethod, r13, false);
3595 
3596   __ verify_oop(r0);
3597 
3598   __ jump_from_interpreted(rmethod, r0);
3599 }
3600 
3601 
3602 //-----------------------------------------------------------------------------
3603 // Allocation
3604 
3605 void TemplateTable::_new() {
3606   transition(vtos, atos);
3607 
3608   __ get_unsigned_2_byte_index_at_bcp(r3, 1);
3609   Label slow_case;
3610   Label done;
3611   Label initialize_header;
3612 
3613   __ get_cpool_and_tags(r4, r0);
3614   // Make sure the class we're about to instantiate has been resolved.
3615   // This is done before loading InstanceKlass to be consistent with the order
3616   // how Constant Pool is updated (see ConstantPool::klass_at_put)
3617   const int tags_offset = Array<u1>::base_offset_in_bytes();
3618   __ lea(rscratch1, Address(r0, r3, Address::lsl(0)));
3619   __ lea(rscratch1, Address(rscratch1, tags_offset));
3620   __ ldarb(rscratch1, rscratch1);
3621   __ cmp(rscratch1, (u1)JVM_CONSTANT_Class);
3622   __ br(Assembler::NE, slow_case);
3623 
3624   // get InstanceKlass
3625   __ load_resolved_klass_at_offset(r4, r3, r4, rscratch1);
3626 
3627   // make sure klass is initialized
3628   assert(VM_Version::supports_fast_class_init_checks(), "Optimization requires support for fast class initialization checks");
3629   __ clinit_barrier(r4, rscratch1, nullptr /*L_fast_path*/, &slow_case);
3630 
3631   // get instance_size in InstanceKlass (scaled to a count of bytes)
3632   __ ldrw(r3,
3633           Address(r4,
3634                   Klass::layout_helper_offset()));
3635   // test to see if it is malformed in some way
3636   __ tbnz(r3, exact_log2(Klass::_lh_instance_slow_path_bit), slow_case);
3637 
3638   // Allocate the instance:
3639   //  If TLAB is enabled:
3640   //    Try to allocate in the TLAB.
3641   //    If fails, go to the slow path.
3642   //    Initialize the allocation.
3643   //    Exit.
3644   //
3645   //  Go to slow path.
3646 
3647   if (UseTLAB) {
3648     __ tlab_allocate(r0, r3, 0, noreg, r1, slow_case);
3649 
3650     if (ZeroTLAB) {
3651       // the fields have been already cleared
3652       __ b(initialize_header);
3653     }
3654 
3655     // The object is initialized before the header.  If the object size is
3656     // zero, go directly to the header initialization.
3657     int header_size = oopDesc::header_size() * HeapWordSize;
3658     assert(is_aligned(header_size, BytesPerLong), "oop header size must be 8-byte-aligned");
3659     __ sub(r3, r3, header_size);
3660     __ cbz(r3, initialize_header);
3661 
3662     // Initialize object fields
3663     {
3664       __ add(r2, r0, header_size);
3665       Label loop;
3666       __ bind(loop);
3667       __ str(zr, Address(__ post(r2, BytesPerLong)));
3668       __ sub(r3, r3, BytesPerLong);
3669       __ cbnz(r3, loop);
3670     }
3671 
3672     // initialize object header only.
3673     __ bind(initialize_header);
3674     if (UseCompactObjectHeaders) {
3675       __ ldr(rscratch1, Address(r4, Klass::prototype_header_offset()));
3676       __ str(rscratch1, Address(r0, oopDesc::mark_offset_in_bytes()));
3677     } else {
3678       __ mov(rscratch1, (intptr_t)markWord::prototype().value());
3679       __ str(rscratch1, Address(r0, oopDesc::mark_offset_in_bytes()));
3680       __ store_klass_gap(r0, zr);  // zero klass gap for compressed oops
3681       __ store_klass(r0, r4);      // store klass last
3682     }
3683 
3684     if (DTraceAllocProbes) {
3685       // Trigger dtrace event for fastpath
3686       __ push(atos); // save the return value
3687       __ call_VM_leaf(
3688            CAST_FROM_FN_PTR(address, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc)), r0);
3689       __ pop(atos); // restore the return value
3690 
3691     }
3692     __ b(done);
3693   }
3694 
3695   // slow case
3696   __ bind(slow_case);
3697   __ get_constant_pool(c_rarg1);
3698   __ get_unsigned_2_byte_index_at_bcp(c_rarg2, 1);
3699   call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), c_rarg1, c_rarg2);
3700   __ verify_oop(r0);
3701 
3702   // continue
3703   __ bind(done);
3704   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3705   __ membar(Assembler::StoreStore);
3706 }
3707 
3708 void TemplateTable::newarray() {
3709   transition(itos, atos);
3710   __ load_unsigned_byte(c_rarg1, at_bcp(1));
3711   __ mov(c_rarg2, r0);
3712   call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
3713           c_rarg1, c_rarg2);
3714   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3715   __ membar(Assembler::StoreStore);
3716 }
3717 
3718 void TemplateTable::anewarray() {
3719   transition(itos, atos);
3720   __ get_unsigned_2_byte_index_at_bcp(c_rarg2, 1);
3721   __ get_constant_pool(c_rarg1);
3722   __ mov(c_rarg3, r0);
3723   call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
3724           c_rarg1, c_rarg2, c_rarg3);
3725   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3726   __ membar(Assembler::StoreStore);
3727 }
3728 
3729 void TemplateTable::arraylength() {
3730   transition(atos, itos);
3731   __ ldrw(r0, Address(r0, arrayOopDesc::length_offset_in_bytes()));
3732 }
3733 
3734 void TemplateTable::checkcast()
3735 {
3736   transition(atos, atos);
3737   Label done, is_null, ok_is_subtype, quicked, resolved;
3738   __ cbz(r0, is_null);
3739 
3740   // Get cpool & tags index
3741   __ get_cpool_and_tags(r2, r3); // r2=cpool, r3=tags array
3742   __ get_unsigned_2_byte_index_at_bcp(r19, 1); // r19=index
3743   // See if bytecode has already been quicked
3744   __ add(rscratch1, r3, Array<u1>::base_offset_in_bytes());
3745   __ lea(r1, Address(rscratch1, r19));
3746   __ ldarb(r1, r1);
3747   __ cmp(r1, (u1)JVM_CONSTANT_Class);
3748   __ br(Assembler::EQ, quicked);
3749 
3750   __ push(atos); // save receiver for result, and for GC
3751   call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3752   __ get_vm_result_metadata(r0, rthread);
3753   __ pop(r3); // restore receiver
3754   __ b(resolved);
3755 
3756   // Get superklass in r0 and subklass in r3
3757   __ bind(quicked);
3758   __ mov(r3, r0); // Save object in r3; r0 needed for subtype check
3759   __ load_resolved_klass_at_offset(r2, r19, r0, rscratch1); // r0 = klass
3760 
3761   __ bind(resolved);
3762   __ load_klass(r19, r3);
3763 
3764   // Generate subtype check.  Blows r2, r5.  Object in r3.
3765   // Superklass in r0.  Subklass in r19.
3766   __ gen_subtype_check(r19, ok_is_subtype);
3767 
3768   // Come here on failure
3769   __ push(r3);
3770   // object is at TOS
3771   __ b(Interpreter::_throw_ClassCastException_entry);
3772 
3773   // Come here on success
3774   __ bind(ok_is_subtype);
3775   __ mov(r0, r3); // Restore object in r3
3776 
3777   // Collect counts on whether this test sees nulls a lot or not.
3778   if (ProfileInterpreter) {
3779     __ b(done);
3780     __ bind(is_null);
3781     __ profile_null_seen(r2);
3782   } else {
3783     __ bind(is_null);   // same as 'done'
3784   }
3785   __ bind(done);
3786 }
3787 
3788 void TemplateTable::instanceof() {
3789   transition(atos, itos);
3790   Label done, is_null, ok_is_subtype, quicked, resolved;
3791   __ cbz(r0, is_null);
3792 
3793   // Get cpool & tags index
3794   __ get_cpool_and_tags(r2, r3); // r2=cpool, r3=tags array
3795   __ get_unsigned_2_byte_index_at_bcp(r19, 1); // r19=index
3796   // See if bytecode has already been quicked
3797   __ add(rscratch1, r3, Array<u1>::base_offset_in_bytes());
3798   __ lea(r1, Address(rscratch1, r19));
3799   __ ldarb(r1, r1);
3800   __ cmp(r1, (u1)JVM_CONSTANT_Class);
3801   __ br(Assembler::EQ, quicked);
3802 
3803   __ push(atos); // save receiver for result, and for GC
3804   call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3805   __ get_vm_result_metadata(r0, rthread);
3806   __ pop(r3); // restore receiver
3807   __ verify_oop(r3);
3808   __ load_klass(r3, r3);
3809   __ b(resolved);
3810 
3811   // Get superklass in r0 and subklass in r3
3812   __ bind(quicked);
3813   __ load_klass(r3, r0);
3814   __ load_resolved_klass_at_offset(r2, r19, r0, rscratch1);
3815 
3816   __ bind(resolved);
3817 
3818   // Generate subtype check.  Blows r2, r5
3819   // Superklass in r0.  Subklass in r3.
3820   __ gen_subtype_check(r3, ok_is_subtype);
3821 
3822   // Come here on failure
3823   __ mov(r0, 0);
3824   __ b(done);
3825   // Come here on success
3826   __ bind(ok_is_subtype);
3827   __ mov(r0, 1);
3828 
3829   // Collect counts on whether this test sees nulls a lot or not.
3830   if (ProfileInterpreter) {
3831     __ b(done);
3832     __ bind(is_null);
3833     __ profile_null_seen(r2);
3834   } else {
3835     __ bind(is_null);   // same as 'done'
3836   }
3837   __ bind(done);
3838   // r0 = 0: obj == nullptr or  obj is not an instanceof the specified klass
3839   // r0 = 1: obj != nullptr and obj is     an instanceof the specified klass
3840 }
3841 
3842 //-----------------------------------------------------------------------------
3843 // Breakpoints
3844 void TemplateTable::_breakpoint() {
3845   // Note: We get here even if we are single stepping..
3846   // jbug inists on setting breakpoints at every bytecode
3847   // even if we are in single step mode.
3848 
3849   transition(vtos, vtos);
3850 
3851   // get the unpatched byte code
3852   __ get_method(c_rarg1);
3853   __ call_VM(noreg,
3854              CAST_FROM_FN_PTR(address,
3855                               InterpreterRuntime::get_original_bytecode_at),
3856              c_rarg1, rbcp);
3857   __ mov(r19, r0);
3858 
3859   // post the breakpoint event
3860   __ call_VM(noreg,
3861              CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint),
3862              rmethod, rbcp);
3863 
3864   // complete the execution of original bytecode
3865   __ mov(rscratch1, r19);
3866   __ dispatch_only_normal(vtos);
3867 }
3868 
3869 //-----------------------------------------------------------------------------
3870 // Exceptions
3871 
3872 void TemplateTable::athrow() {
3873   transition(atos, vtos);
3874   __ null_check(r0);
3875   __ b(Interpreter::throw_exception_entry());
3876 }
3877 
3878 //-----------------------------------------------------------------------------
3879 // Synchronization
3880 //
3881 // Note: monitorenter & exit are symmetric routines; which is reflected
3882 //       in the assembly code structure as well
3883 //
3884 // Stack layout:
3885 //
3886 // [expressions  ] <--- esp               = expression stack top
3887 // ..
3888 // [expressions  ]
3889 // [monitor entry] <--- monitor block top = expression stack bot
3890 // ..
3891 // [monitor entry]
3892 // [frame data   ] <--- monitor block bot
3893 // ...
3894 // [saved rfp    ] <--- rfp
3895 void TemplateTable::monitorenter()
3896 {
3897   transition(atos, vtos);
3898 
3899   // check for null object
3900   __ null_check(r0);
3901 
3902   const Address monitor_block_top(
3903         rfp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
3904   const Address monitor_block_bot(
3905         rfp, frame::interpreter_frame_initial_sp_offset * wordSize);
3906   const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
3907 
3908   Label allocated;
3909 
3910   // initialize entry pointer
3911   __ mov(c_rarg1, zr); // points to free slot or null
3912 
3913   // find a free slot in the monitor block (result in c_rarg1)
3914   {
3915     Label entry, loop, exit;
3916     __ ldr(c_rarg3, monitor_block_top); // derelativize pointer
3917     __ lea(c_rarg3, Address(rfp, c_rarg3, Address::lsl(Interpreter::logStackElementSize)));
3918     // c_rarg3 points to current entry, starting with top-most entry
3919 
3920     __ lea(c_rarg2, monitor_block_bot); // points to word before bottom
3921 
3922     __ b(entry);
3923 
3924     __ bind(loop);
3925     // check if current entry is used
3926     // if not used then remember entry in c_rarg1
3927     __ ldr(rscratch1, Address(c_rarg3, BasicObjectLock::obj_offset()));
3928     __ cmp(zr, rscratch1);
3929     __ csel(c_rarg1, c_rarg3, c_rarg1, Assembler::EQ);
3930     // check if current entry is for same object
3931     __ cmp(r0, rscratch1);
3932     // if same object then stop searching
3933     __ br(Assembler::EQ, exit);
3934     // otherwise advance to next entry
3935     __ add(c_rarg3, c_rarg3, entry_size);
3936     __ bind(entry);
3937     // check if bottom reached
3938     __ cmp(c_rarg3, c_rarg2);
3939     // if not at bottom then check this entry
3940     __ br(Assembler::NE, loop);
3941     __ bind(exit);
3942   }
3943 
3944   __ cbnz(c_rarg1, allocated); // check if a slot has been found and
3945                             // if found, continue with that on
3946 
3947   // allocate one if there's no free slot
3948   {
3949     Label entry, loop;
3950     // 1. compute new pointers            // rsp: old expression stack top
3951 
3952     __ check_extended_sp();
3953     __ sub(sp, sp, entry_size);           // make room for the monitor
3954     __ sub(rscratch1, sp, rfp);
3955     __ asr(rscratch1, rscratch1, Interpreter::logStackElementSize);
3956     __ str(rscratch1, Address(rfp, frame::interpreter_frame_extended_sp_offset * wordSize));
3957 
3958     __ ldr(c_rarg1, monitor_block_bot);   // derelativize pointer
3959     __ lea(c_rarg1, Address(rfp, c_rarg1, Address::lsl(Interpreter::logStackElementSize)));
3960     // c_rarg1 points to the old expression stack bottom
3961 
3962     __ sub(esp, esp, entry_size);         // move expression stack top
3963     __ sub(c_rarg1, c_rarg1, entry_size); // move expression stack bottom
3964     __ mov(c_rarg3, esp);                 // set start value for copy loop
3965     __ sub(rscratch1, c_rarg1, rfp);      // relativize pointer
3966     __ asr(rscratch1, rscratch1, Interpreter::logStackElementSize);
3967     __ str(rscratch1, monitor_block_bot);  // set new monitor block bottom
3968 
3969     __ b(entry);
3970     // 2. move expression stack contents
3971     __ bind(loop);
3972     __ ldr(c_rarg2, Address(c_rarg3, entry_size)); // load expression stack
3973                                                    // word from old location
3974     __ str(c_rarg2, Address(c_rarg3, 0));          // and store it at new location
3975     __ add(c_rarg3, c_rarg3, wordSize);            // advance to next word
3976     __ bind(entry);
3977     __ cmp(c_rarg3, c_rarg1);        // check if bottom reached
3978     __ br(Assembler::NE, loop);      // if not at bottom then
3979                                      // copy next word
3980   }
3981 
3982   // call run-time routine
3983   // c_rarg1: points to monitor entry
3984   __ bind(allocated);
3985 
3986   // Increment bcp to point to the next bytecode, so exception
3987   // handling for async. exceptions work correctly.
3988   // The object has already been popped from the stack, so the
3989   // expression stack looks correct.
3990   __ increment(rbcp);
3991 
3992   // store object
3993   __ str(r0, Address(c_rarg1, BasicObjectLock::obj_offset()));
3994   __ lock_object(c_rarg1);
3995 
3996   // check to make sure this monitor doesn't cause stack overflow after locking
3997   __ save_bcp();  // in case of exception
3998   __ generate_stack_overflow_check(0);
3999 
4000   // The bcp has already been incremented. Just need to dispatch to
4001   // next instruction.
4002   __ dispatch_next(vtos);
4003 }
4004 
4005 
4006 void TemplateTable::monitorexit()
4007 {
4008   transition(atos, vtos);
4009 
4010   // check for null object
4011   __ null_check(r0);
4012 
4013   const Address monitor_block_top(
4014         rfp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4015   const Address monitor_block_bot(
4016         rfp, frame::interpreter_frame_initial_sp_offset * wordSize);
4017   const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
4018 
4019   Label found;
4020 
4021   // find matching slot
4022   {
4023     Label entry, loop;
4024     __ ldr(c_rarg1, monitor_block_top); // derelativize pointer
4025     __ lea(c_rarg1, Address(rfp, c_rarg1, Address::lsl(Interpreter::logStackElementSize)));
4026     // c_rarg1 points to current entry, starting with top-most entry
4027 
4028     __ lea(c_rarg2, monitor_block_bot); // points to word before bottom
4029                                         // of monitor block
4030     __ b(entry);
4031 
4032     __ bind(loop);
4033     // check if current entry is for same object
4034     __ ldr(rscratch1, Address(c_rarg1, BasicObjectLock::obj_offset()));
4035     __ cmp(r0, rscratch1);
4036     // if same object then stop searching
4037     __ br(Assembler::EQ, found);
4038     // otherwise advance to next entry
4039     __ add(c_rarg1, c_rarg1, entry_size);
4040     __ bind(entry);
4041     // check if bottom reached
4042     __ cmp(c_rarg1, c_rarg2);
4043     // if not at bottom then check this entry
4044     __ br(Assembler::NE, loop);
4045   }
4046 
4047   // error handling. Unlocking was not block-structured
4048   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4049                    InterpreterRuntime::throw_illegal_monitor_state_exception));
4050   __ should_not_reach_here();
4051 
4052   // call run-time routine
4053   __ bind(found);
4054   __ push_ptr(r0); // make sure object is on stack (contract with oopMaps)
4055   __ unlock_object(c_rarg1);
4056   __ pop_ptr(r0); // discard object
4057 }
4058 
4059 
4060 // Wide instructions
4061 void TemplateTable::wide()
4062 {
4063   __ load_unsigned_byte(r19, at_bcp(1));
4064   __ mov(rscratch1, (address)Interpreter::_wentry_point);
4065   __ ldr(rscratch1, Address(rscratch1, r19, Address::uxtw(3)));
4066   __ br(rscratch1);
4067 }
4068 
4069 
4070 // Multi arrays
4071 void TemplateTable::multianewarray() {
4072   transition(vtos, atos);
4073   __ load_unsigned_byte(r0, at_bcp(3)); // get number of dimensions
4074   // last dim is on top of stack; we want address of first one:
4075   // first_addr = last_addr + (ndims - 1) * wordSize
4076   __ lea(c_rarg1, Address(esp, r0, Address::uxtw(3)));
4077   __ sub(c_rarg1, c_rarg1, wordSize);
4078   call_VM(r0,
4079           CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray),
4080           c_rarg1);
4081   __ load_unsigned_byte(r1, at_bcp(3));
4082   __ lea(esp, Address(esp, r1, Address::uxtw(3)));
4083 }