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