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