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