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