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