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