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