1 /*
   2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2016, 2017 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 // Major contributions by AHa, AS, JL, ML.
  27 
  28 #include "precompiled.hpp"
  29 #include "asm/macroAssembler.inline.hpp"
  30 #include "interp_masm_s390.hpp"
  31 #include "interpreter/interpreter.hpp"
  32 #include "interpreter/interpreterRuntime.hpp"
  33 #include "oops/arrayOop.hpp"
  34 #include "oops/markOop.hpp"
  35 #include "prims/jvmtiExport.hpp"
  36 #include "prims/jvmtiThreadState.hpp"
  37 #include "runtime/basicLock.hpp"
  38 #include "runtime/biasedLocking.hpp"
  39 #include "runtime/frame.inline.hpp"
  40 #include "runtime/safepointMechanism.hpp"
  41 #include "runtime/sharedRuntime.hpp"
  42 #include "runtime/thread.inline.hpp"
  43 
  44 // Implementation of InterpreterMacroAssembler.
  45 // This file specializes the assember with interpreter-specific macros.
  46 
  47 #ifdef PRODUCT
  48 #define BLOCK_COMMENT(str)
  49 #define BIND(label)        bind(label);
  50 #else
  51 #define BLOCK_COMMENT(str) block_comment(str)
  52 #define BIND(label)        bind(label); BLOCK_COMMENT(#label ":")
  53 #endif
  54 
  55 void InterpreterMacroAssembler::jump_to_entry(address entry, Register Rscratch) {
  56   assert(entry != NULL, "Entry must have been generated by now");
  57   assert(Rscratch != Z_R0, "Can't use R0 for addressing");
  58   branch_optimized(Assembler::bcondAlways, entry);
  59 }
  60 
  61 void InterpreterMacroAssembler::empty_expression_stack(void) {
  62   get_monitors(Z_R1_scratch);
  63   add2reg(Z_esp, -Interpreter::stackElementSize, Z_R1_scratch);
  64 }
  65 
  66 // Dispatch code executed in the prolog of a bytecode which does not do it's
  67 // own dispatch.
  68 void InterpreterMacroAssembler::dispatch_prolog(TosState state, int bcp_incr) {
  69   // On z/Architecture we are short on registers, therefore we do not preload the
  70   // dispatch address of the next bytecode.
  71 }
  72 
  73 // Dispatch code executed in the epilog of a bytecode which does not do it's
  74 // own dispatch.
  75 void InterpreterMacroAssembler::dispatch_epilog(TosState state, int step) {
  76   dispatch_next(state, step);
  77 }
  78 
  79 void InterpreterMacroAssembler::dispatch_next(TosState state, int bcp_incr, bool generate_poll) {
  80   z_llgc(Z_bytecode, bcp_incr, Z_R0, Z_bcp);  // Load next bytecode.
  81   add2reg(Z_bcp, bcp_incr);                   // Advance bcp. Add2reg produces optimal code.
  82   dispatch_base(state, Interpreter::dispatch_table(state), generate_poll);
  83 }
  84 
  85 // Common code to dispatch and dispatch_only.
  86 // Dispatch value in Lbyte_code and increment Lbcp.
  87 
  88 void InterpreterMacroAssembler::dispatch_base(TosState state, address* table, bool generate_poll) {
  89   verify_FPU(1, state);
  90 
  91 #ifdef ASSERT
  92   address reentry = NULL;
  93   { Label OK;
  94     // Check if the frame pointer in Z_fp is correct.
  95     z_cg(Z_fp, 0, Z_SP);
  96     z_bre(OK);
  97     reentry = stop_chain_static(reentry, "invalid frame pointer Z_fp: " FILE_AND_LINE);
  98     bind(OK);
  99   }
 100   { Label OK;
 101     // check if the locals pointer in Z_locals is correct
 102     z_cg(Z_locals, _z_ijava_state_neg(locals), Z_fp);
 103     z_bre(OK);
 104     reentry = stop_chain_static(reentry, "invalid locals pointer Z_locals: " FILE_AND_LINE);
 105     bind(OK);
 106   }
 107 #endif
 108 
 109   // TODO: Maybe implement +VerifyActivationFrameSize here.
 110   // verify_thread(); // Too slow. We will just verify on method entry & exit.
 111   verify_oop(Z_tos, state);
 112 
 113   // Dispatch table to use.
 114   load_absolute_address(Z_tmp_1, (address)table);  // Z_tmp_1 = table;
 115 
 116   if (SafepointMechanism::uses_thread_local_poll() && generate_poll) {
 117     address *sfpt_tbl = Interpreter::safept_table(state);
 118     if (table != sfpt_tbl) {
 119       Label dispatch;
 120       const Address poll_byte_addr(Z_thread, in_bytes(Thread::polling_page_offset()) + 7 /* Big Endian */);
 121       // Armed page has poll_bit set, if poll bit is cleared just continue.
 122       z_tm(poll_byte_addr, SafepointMechanism::poll_bit());
 123       z_braz(dispatch);
 124       load_absolute_address(Z_tmp_1, (address)sfpt_tbl);  // Z_tmp_1 = table;
 125       bind(dispatch);
 126     }
 127   }
 128 
 129   // 0 <= Z_bytecode < 256 => Use a 32 bit shift, because it is shorter than sllg.
 130   // Z_bytecode must have been loaded zero-extended for this approach to be correct.
 131   z_sll(Z_bytecode, LogBytesPerWord, Z_R0);   // Multiply by wordSize.
 132   z_lg(Z_tmp_1, 0, Z_bytecode, Z_tmp_1);      // Get entry addr.
 133 
 134   z_br(Z_tmp_1);
 135 }
 136 
 137 void InterpreterMacroAssembler::dispatch_only(TosState state, bool generate_poll) {
 138   dispatch_base(state, Interpreter::dispatch_table(state), generate_poll);
 139 }
 140 
 141 void InterpreterMacroAssembler::dispatch_only_normal(TosState state) {
 142   dispatch_base(state, Interpreter::normal_table(state));
 143 }
 144 
 145 void InterpreterMacroAssembler::dispatch_via(TosState state, address *table) {
 146   // Load current bytecode.
 147   z_llgc(Z_bytecode, Address(Z_bcp, (intptr_t)0));
 148   dispatch_base(state, table);
 149 }
 150 
 151 // The following call_VM*_base() methods overload and mask the respective
 152 // declarations/definitions in class MacroAssembler. They are meant as a "detour"
 153 // to perform additional, template interpreter specific tasks before actually
 154 // calling their MacroAssembler counterparts.
 155 
 156 void InterpreterMacroAssembler::call_VM_leaf_base(address entry_point) {
 157   bool allow_relocation = true; // Fenerally valid variant. Assume code is relocated.
 158   // interpreter specific
 159   // Note: No need to save/restore bcp (Z_R13) pointer since these are callee
 160   // saved registers and no blocking/ GC can happen in leaf calls.
 161 
 162   // super call
 163   MacroAssembler::call_VM_leaf_base(entry_point, allow_relocation);
 164 }
 165 
 166 void InterpreterMacroAssembler::call_VM_leaf_base(address entry_point, bool allow_relocation) {
 167   // interpreter specific
 168   // Note: No need to save/restore bcp (Z_R13) pointer since these are callee
 169   // saved registers and no blocking/ GC can happen in leaf calls.
 170 
 171   // super call
 172   MacroAssembler::call_VM_leaf_base(entry_point, allow_relocation);
 173 }
 174 
 175 void InterpreterMacroAssembler::call_VM_base(Register oop_result, Register last_java_sp,
 176                                              address entry_point, bool check_exceptions) {
 177   bool allow_relocation = true; // Fenerally valid variant. Assume code is relocated.
 178   // interpreter specific
 179 
 180   save_bcp();
 181   save_esp();
 182   // super call
 183   MacroAssembler::call_VM_base(oop_result, last_java_sp,
 184                                entry_point, allow_relocation, check_exceptions);
 185   restore_bcp();
 186 }
 187 
 188 void InterpreterMacroAssembler::call_VM_base(Register oop_result, Register last_java_sp,
 189                                              address entry_point, bool allow_relocation,
 190                                              bool check_exceptions) {
 191   // interpreter specific
 192 
 193   save_bcp();
 194   save_esp();
 195   // super call
 196   MacroAssembler::call_VM_base(oop_result, last_java_sp,
 197                                entry_point, allow_relocation, check_exceptions);
 198   restore_bcp();
 199 }
 200 
 201 void InterpreterMacroAssembler::check_and_handle_popframe(Register scratch_reg) {
 202   if (JvmtiExport::can_pop_frame()) {
 203     BLOCK_COMMENT("check_and_handle_popframe {");
 204     Label L;
 205     // Initiate popframe handling only if it is not already being
 206     // processed. If the flag has the popframe_processing bit set, it
 207     // means that this code is called *during* popframe handling - we
 208     // don't want to reenter.
 209     // TODO: Check if all four state combinations could be visible.
 210     // If (processing and !pending) is an invisible/impossible state,
 211     // there is optimization potential by testing both bits at once.
 212     // Then, All_Zeroes and All_Ones means skip, Mixed means doit.
 213     testbit(Address(Z_thread, JavaThread::popframe_condition_offset()),
 214             exact_log2(JavaThread::popframe_pending_bit));
 215     z_bfalse(L);
 216     testbit(Address(Z_thread, JavaThread::popframe_condition_offset()),
 217             exact_log2(JavaThread::popframe_processing_bit));
 218     z_btrue(L);
 219 
 220     // Call Interpreter::remove_activation_preserving_args_entry() to get the
 221     // address of the same-named entrypoint in the generated interpreter code.
 222     call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_preserving_args_entry));
 223     // The above call should (as its only effect) return the contents of the field
 224     // _remove_activation_preserving_args_entry in Z_RET.
 225     // We just jump there to have the work done.
 226     z_br(Z_RET);
 227     // There is no way for control to fall thru here.
 228 
 229     bind(L);
 230     BLOCK_COMMENT("} check_and_handle_popframe");
 231   }
 232 }
 233 
 234 
 235 void InterpreterMacroAssembler::load_earlyret_value(TosState state) {
 236   Register RjvmtiState = Z_R1_scratch;
 237   int      tos_off     = in_bytes(JvmtiThreadState::earlyret_tos_offset());
 238   int      oop_off     = in_bytes(JvmtiThreadState::earlyret_oop_offset());
 239   int      val_off     = in_bytes(JvmtiThreadState::earlyret_value_offset());
 240   int      state_off   = in_bytes(JavaThread::jvmti_thread_state_offset());
 241 
 242   z_lg(RjvmtiState, state_off, Z_thread);
 243 
 244   switch (state) {
 245     case atos: z_lg(Z_tos, oop_off, RjvmtiState);
 246       store_const(Address(RjvmtiState, oop_off), 0L, 8, 8, Z_R0_scratch);
 247                                                     break;
 248     case ltos: z_lg(Z_tos, val_off, RjvmtiState);   break;
 249     case btos: // fall through
 250     case ztos: // fall through
 251     case ctos: // fall through
 252     case stos: // fall through
 253     case itos: z_llgf(Z_tos, val_off, RjvmtiState); break;
 254     case ftos: z_le(Z_ftos, val_off, RjvmtiState);  break;
 255     case dtos: z_ld(Z_ftos, val_off, RjvmtiState);  break;
 256     case vtos:   /* nothing to do */                break;
 257     default  : ShouldNotReachHere();
 258   }
 259 
 260   // Clean up tos value in the jvmti thread state.
 261   store_const(Address(RjvmtiState, val_off),   0L, 8, 8, Z_R0_scratch);
 262   // Set tos state field to illegal value.
 263   store_const(Address(RjvmtiState, tos_off), ilgl, 4, 1, Z_R0_scratch);
 264 }
 265 
 266 void InterpreterMacroAssembler::check_and_handle_earlyret(Register scratch_reg) {
 267   if (JvmtiExport::can_force_early_return()) {
 268     BLOCK_COMMENT("check_and_handle_earlyret {");
 269     Label L;
 270     // arg regs are save, because we are just behind the call in call_VM_base
 271     Register jvmti_thread_state = Z_ARG2;
 272     Register tmp                = Z_ARG3;
 273     load_and_test_long(jvmti_thread_state, Address(Z_thread, JavaThread::jvmti_thread_state_offset()));
 274     z_bre(L); // if (thread->jvmti_thread_state() == NULL) exit;
 275 
 276     // Initiate earlyret handling only if it is not already being processed.
 277     // If the flag has the earlyret_processing bit set, it means that this code
 278     // is called *during* earlyret handling - we don't want to reenter.
 279 
 280     assert((JvmtiThreadState::earlyret_pending != 0) && (JvmtiThreadState::earlyret_inactive == 0),
 281           "must fix this check, when changing the values of the earlyret enum");
 282     assert(JvmtiThreadState::earlyret_pending == 1, "must fix this check, when changing the values of the earlyret enum");
 283 
 284     load_and_test_int(tmp, Address(jvmti_thread_state, JvmtiThreadState::earlyret_state_offset()));
 285     z_brz(L); // if (thread->jvmti_thread_state()->_earlyret_state != JvmtiThreadState::earlyret_pending) exit;
 286 
 287     // Call Interpreter::remove_activation_early_entry() to get the address of the
 288     // same-named entrypoint in the generated interpreter code.
 289     assert(sizeof(TosState) == 4, "unexpected size");
 290     z_l(Z_ARG1, Address(jvmti_thread_state, JvmtiThreadState::earlyret_tos_offset()));
 291     call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_early_entry), Z_ARG1);
 292     // The above call should (as its only effect) return the contents of the field
 293     // _remove_activation_preserving_args_entry in Z_RET.
 294     // We just jump there to have the work done.
 295     z_br(Z_RET);
 296     // There is no way for control to fall thru here.
 297 
 298     bind(L);
 299     BLOCK_COMMENT("} check_and_handle_earlyret");
 300   }
 301 }
 302 
 303 void InterpreterMacroAssembler::super_call_VM_leaf(address entry_point, Register arg_1, Register arg_2) {
 304   lgr_if_needed(Z_ARG1, arg_1);
 305   assert(arg_2 != Z_ARG1, "smashed argument");
 306   lgr_if_needed(Z_ARG2, arg_2);
 307   MacroAssembler::call_VM_leaf_base(entry_point, true);
 308 }
 309 
 310 void InterpreterMacroAssembler::get_cache_index_at_bcp(Register index, int bcp_offset, size_t index_size) {
 311   Address param(Z_bcp, bcp_offset);
 312 
 313   BLOCK_COMMENT("get_cache_index_at_bcp {");
 314   assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
 315   if (index_size == sizeof(u2)) {
 316     load_sized_value(index, param, 2, false /*signed*/);
 317   } else if (index_size == sizeof(u4)) {
 318 
 319     load_sized_value(index, param, 4, false);
 320 
 321     // Check if the secondary index definition is still ~x, otherwise
 322     // we have to change the following assembler code to calculate the
 323     // plain index.
 324     assert(ConstantPool::decode_invokedynamic_index(~123) == 123, "else change next line");
 325     not_(index);  // Convert to plain index.
 326   } else if (index_size == sizeof(u1)) {
 327     z_llgc(index, param);
 328   } else {
 329     ShouldNotReachHere();
 330   }
 331   BLOCK_COMMENT("}");
 332 }
 333 
 334 
 335 void InterpreterMacroAssembler::get_cache_and_index_at_bcp(Register cache, Register cpe_offset,
 336                                                            int bcp_offset, size_t index_size) {
 337   BLOCK_COMMENT("get_cache_and_index_at_bcp {");
 338   assert_different_registers(cache, cpe_offset);
 339   get_cache_index_at_bcp(cpe_offset, bcp_offset, index_size);
 340   z_lg(cache, Address(Z_fp, _z_ijava_state_neg(cpoolCache)));
 341   // Convert from field index to ConstantPoolCache offset in bytes.
 342   z_sllg(cpe_offset, cpe_offset, exact_log2(in_words(ConstantPoolCacheEntry::size()) * BytesPerWord));
 343   BLOCK_COMMENT("}");
 344 }
 345 
 346 // Kills Z_R0_scratch.
 347 void InterpreterMacroAssembler::get_cache_and_index_and_bytecode_at_bcp(Register cache,
 348                                                                         Register cpe_offset,
 349                                                                         Register bytecode,
 350                                                                         int byte_no,
 351                                                                         int bcp_offset,
 352                                                                         size_t index_size) {
 353   BLOCK_COMMENT("get_cache_and_index_and_bytecode_at_bcp {");
 354   get_cache_and_index_at_bcp(cache, cpe_offset, bcp_offset, index_size);
 355 
 356   // We want to load (from CP cache) the bytecode that corresponds to the passed-in byte_no.
 357   // It is located at (cache + cpe_offset + base_offset + indices_offset + (8-1) (last byte in DW) - (byte_no+1).
 358   // Instead of loading, shifting and masking a DW, we just load that one byte of interest with z_llgc (unsigned).
 359   const int base_ix_off = in_bytes(ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::indices_offset());
 360   const int off_in_DW   = (8-1) - (1+byte_no);
 361   assert(ConstantPoolCacheEntry::bytecode_1_mask == ConstantPoolCacheEntry::bytecode_2_mask, "common mask");
 362   assert(ConstantPoolCacheEntry::bytecode_1_mask == 0xff, "");
 363   load_sized_value(bytecode, Address(cache, cpe_offset, base_ix_off+off_in_DW), 1, false /*signed*/);
 364 
 365   BLOCK_COMMENT("}");
 366 }
 367 
 368 // Load object from cpool->resolved_references(index).
 369 void InterpreterMacroAssembler::load_resolved_reference_at_index(Register result, Register index) {
 370   assert_different_registers(result, index);
 371   get_constant_pool(result);
 372 
 373   // Convert
 374   //  - from field index to resolved_references() index and
 375   //  - from word index to byte offset.
 376   // Since this is a java object, it is potentially compressed.
 377   Register tmp = index;  // reuse
 378   z_sllg(index, index, LogBytesPerHeapOop); // Offset into resolved references array.
 379   // Load pointer for resolved_references[] objArray.
 380   z_lg(result, ConstantPool::cache_offset_in_bytes(), result);
 381   z_lg(result, ConstantPoolCache::resolved_references_offset_in_bytes(), result);
 382   resolve_oop_handle(result); // Load resolved references array itself.
 383 #ifdef ASSERT
 384   NearLabel index_ok;
 385   z_lgf(Z_R0, Address(result, arrayOopDesc::length_offset_in_bytes()));
 386   z_sllg(Z_R0, Z_R0, LogBytesPerHeapOop);
 387   compare64_and_branch(tmp, Z_R0, Assembler::bcondLow, index_ok);
 388   stop("resolved reference index out of bounds", 0x09256);
 389   bind(index_ok);
 390 #endif
 391   z_agr(result, index);    // Address of indexed array element.
 392   load_heap_oop(result, arrayOopDesc::base_offset_in_bytes(T_OBJECT), result);
 393   // The resulting oop is null if the reference is not yet resolved.
 394   // It is Universe::the_null_sentinel() if the reference resolved to NULL via condy.
 395 }
 396 
 397 // load cpool->resolved_klass_at(index)
 398 void InterpreterMacroAssembler::load_resolved_klass_at_offset(Register cpool, Register offset, Register iklass) {
 399   // int value = *(Rcpool->int_at_addr(which));
 400   // int resolved_klass_index = extract_low_short_from_int(value);
 401   z_llgh(offset, Address(cpool, offset, sizeof(ConstantPool) + 2)); // offset = resolved_klass_index (s390 is big-endian)
 402   z_sllg(offset, offset, LogBytesPerWord);                          // Convert 'index' to 'offset'
 403   z_lg(iklass, Address(cpool, ConstantPool::resolved_klasses_offset_in_bytes())); // iklass = cpool->_resolved_klasses
 404   z_lg(iklass, Address(iklass, offset, Array<Klass*>::base_offset_in_bytes()));
 405 }
 406 
 407 void InterpreterMacroAssembler::get_cache_entry_pointer_at_bcp(Register cache,
 408                                                                Register tmp,
 409                                                                int bcp_offset,
 410                                                                size_t index_size) {
 411   BLOCK_COMMENT("get_cache_entry_pointer_at_bcp {");
 412     get_cache_and_index_at_bcp(cache, tmp, bcp_offset, index_size);
 413     add2reg_with_index(cache, in_bytes(ConstantPoolCache::base_offset()), tmp, cache);
 414   BLOCK_COMMENT("}");
 415 }
 416 
 417 // Generate a subtype check: branch to ok_is_subtype if sub_klass is
 418 // a subtype of super_klass. Blows registers Rsuper_klass, Rsub_klass, tmp1, tmp2.
 419 void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass,
 420                                                   Register Rsuper_klass,
 421                                                   Register Rtmp1,
 422                                                   Register Rtmp2,
 423                                                   Label &ok_is_subtype) {
 424   // Profile the not-null value's klass.
 425   profile_typecheck(Rtmp1, Rsub_klass, Rtmp2);
 426 
 427   // Do the check.
 428   check_klass_subtype(Rsub_klass, Rsuper_klass, Rtmp1, Rtmp2, ok_is_subtype);
 429 
 430   // Profile the failure of the check.
 431   profile_typecheck_failed(Rtmp1, Rtmp2);
 432 }
 433 
 434 // Pop topmost element from stack. It just disappears.
 435 // Useful if consumed previously by access via stackTop().
 436 void InterpreterMacroAssembler::popx(int len) {
 437   add2reg(Z_esp, len*Interpreter::stackElementSize);
 438   debug_only(verify_esp(Z_esp, Z_R1_scratch));
 439 }
 440 
 441 // Get Address object of stack top. No checks. No pop.
 442 // Purpose: - Provide address of stack operand to exploit reg-mem operations.
 443 //          - Avoid RISC-like mem2reg - reg-reg-op sequence.
 444 Address InterpreterMacroAssembler::stackTop() {
 445   return Address(Z_esp, Interpreter::expr_offset_in_bytes(0));
 446 }
 447 
 448 void InterpreterMacroAssembler::pop_i(Register r) {
 449   z_l(r, Interpreter::expr_offset_in_bytes(0), Z_esp);
 450   add2reg(Z_esp, Interpreter::stackElementSize);
 451   assert_different_registers(r, Z_R1_scratch);
 452   debug_only(verify_esp(Z_esp, Z_R1_scratch));
 453 }
 454 
 455 void InterpreterMacroAssembler::pop_ptr(Register r) {
 456   z_lg(r, Interpreter::expr_offset_in_bytes(0), Z_esp);
 457   add2reg(Z_esp, Interpreter::stackElementSize);
 458   assert_different_registers(r, Z_R1_scratch);
 459   debug_only(verify_esp(Z_esp, Z_R1_scratch));
 460 }
 461 
 462 void InterpreterMacroAssembler::pop_l(Register r) {
 463   z_lg(r, Interpreter::expr_offset_in_bytes(0), Z_esp);
 464   add2reg(Z_esp, 2*Interpreter::stackElementSize);
 465   assert_different_registers(r, Z_R1_scratch);
 466   debug_only(verify_esp(Z_esp, Z_R1_scratch));
 467 }
 468 
 469 void InterpreterMacroAssembler::pop_f(FloatRegister f) {
 470   mem2freg_opt(f, Address(Z_esp, Interpreter::expr_offset_in_bytes(0)), false);
 471   add2reg(Z_esp, Interpreter::stackElementSize);
 472   debug_only(verify_esp(Z_esp, Z_R1_scratch));
 473 }
 474 
 475 void InterpreterMacroAssembler::pop_d(FloatRegister f) {
 476   mem2freg_opt(f, Address(Z_esp, Interpreter::expr_offset_in_bytes(0)), true);
 477   add2reg(Z_esp, 2*Interpreter::stackElementSize);
 478   debug_only(verify_esp(Z_esp, Z_R1_scratch));
 479 }
 480 
 481 void InterpreterMacroAssembler::push_i(Register r) {
 482   assert_different_registers(r, Z_R1_scratch);
 483   debug_only(verify_esp(Z_esp, Z_R1_scratch));
 484   z_st(r, Address(Z_esp));
 485   add2reg(Z_esp, -Interpreter::stackElementSize);
 486 }
 487 
 488 void InterpreterMacroAssembler::push_ptr(Register r) {
 489   z_stg(r, Address(Z_esp));
 490   add2reg(Z_esp, -Interpreter::stackElementSize);
 491 }
 492 
 493 void InterpreterMacroAssembler::push_l(Register r) {
 494   assert_different_registers(r, Z_R1_scratch);
 495   debug_only(verify_esp(Z_esp, Z_R1_scratch));
 496   int offset = -Interpreter::stackElementSize;
 497   z_stg(r, Address(Z_esp, offset));
 498   clear_mem(Address(Z_esp), Interpreter::stackElementSize);
 499   add2reg(Z_esp, 2 * offset);
 500 }
 501 
 502 void InterpreterMacroAssembler::push_f(FloatRegister f) {
 503   debug_only(verify_esp(Z_esp, Z_R1_scratch));
 504   freg2mem_opt(f, Address(Z_esp), false);
 505   add2reg(Z_esp, -Interpreter::stackElementSize);
 506 }
 507 
 508 void InterpreterMacroAssembler::push_d(FloatRegister d) {
 509   debug_only(verify_esp(Z_esp, Z_R1_scratch));
 510   int offset = -Interpreter::stackElementSize;
 511   freg2mem_opt(d, Address(Z_esp, offset));
 512   add2reg(Z_esp, 2 * offset);
 513 }
 514 
 515 void InterpreterMacroAssembler::push(TosState state) {
 516   verify_oop(Z_tos, state);
 517   switch (state) {
 518     case atos: push_ptr();           break;
 519     case btos: push_i();             break;
 520     case ztos:
 521     case ctos:
 522     case stos: push_i();             break;
 523     case itos: push_i();             break;
 524     case ltos: push_l();             break;
 525     case ftos: push_f();             break;
 526     case dtos: push_d();             break;
 527     case vtos: /* nothing to do */   break;
 528     default  : ShouldNotReachHere();
 529   }
 530 }
 531 
 532 void InterpreterMacroAssembler::pop(TosState state) {
 533   switch (state) {
 534     case atos: pop_ptr(Z_tos);       break;
 535     case btos: pop_i(Z_tos);         break;
 536     case ztos:
 537     case ctos:
 538     case stos: pop_i(Z_tos);         break;
 539     case itos: pop_i(Z_tos);         break;
 540     case ltos: pop_l(Z_tos);         break;
 541     case ftos: pop_f(Z_ftos);        break;
 542     case dtos: pop_d(Z_ftos);        break;
 543     case vtos: /* nothing to do */   break;
 544     default  : ShouldNotReachHere();
 545   }
 546   verify_oop(Z_tos, state);
 547 }
 548 
 549 // Helpers for swap and dup.
 550 void InterpreterMacroAssembler::load_ptr(int n, Register val) {
 551   z_lg(val, Address(Z_esp, Interpreter::expr_offset_in_bytes(n)));
 552 }
 553 
 554 void InterpreterMacroAssembler::store_ptr(int n, Register val) {
 555   z_stg(val, Address(Z_esp, Interpreter::expr_offset_in_bytes(n)));
 556 }
 557 
 558 void InterpreterMacroAssembler::prepare_to_jump_from_interpreted(Register method) {
 559   // Satisfy interpreter calling convention (see generate_normal_entry()).
 560   z_lgr(Z_R10, Z_SP); // Set sender sp (aka initial caller sp, aka unextended sp).
 561   // Record top_frame_sp, because the callee might modify it, if it's compiled.
 562   z_stg(Z_SP, _z_ijava_state_neg(top_frame_sp), Z_fp);
 563   save_bcp();
 564   save_esp();
 565   z_lgr(Z_method, method); // Set Z_method (kills Z_fp!).
 566 }
 567 
 568 // Jump to from_interpreted entry of a call unless single stepping is possible
 569 // in this thread in which case we must call the i2i entry.
 570 void InterpreterMacroAssembler::jump_from_interpreted(Register method, Register temp) {
 571   assert_different_registers(method, Z_R10 /*used for initial_caller_sp*/, temp);
 572   prepare_to_jump_from_interpreted(method);
 573 
 574   if (JvmtiExport::can_post_interpreter_events()) {
 575     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
 576     // compiled code in threads for which the event is enabled. Check here for
 577     // interp_only_mode if these events CAN be enabled.
 578     z_lg(Z_R1_scratch, Address(method, Method::from_interpreted_offset()));
 579     MacroAssembler::load_and_test_int(Z_R0_scratch, Address(Z_thread, JavaThread::interp_only_mode_offset()));
 580     z_bcr(bcondEqual, Z_R1_scratch); // Run compiled code if zero.
 581     // Run interpreted.
 582     z_lg(Z_R1_scratch, Address(method, Method::interpreter_entry_offset()));
 583     z_br(Z_R1_scratch);
 584   } else {
 585     // Run compiled code.
 586     z_lg(Z_R1_scratch, Address(method, Method::from_interpreted_offset()));
 587     z_br(Z_R1_scratch);
 588   }
 589 }
 590 
 591 #ifdef ASSERT
 592 void InterpreterMacroAssembler::verify_esp(Register Resp, Register Rtemp) {
 593   // About to read or write Resp[0].
 594   // Make sure it is not in the monitors or the TOP_IJAVA_FRAME_ABI.
 595   address reentry = NULL;
 596 
 597   {
 598     // Check if the frame pointer in Z_fp is correct.
 599     NearLabel OK;
 600     z_cg(Z_fp, 0, Z_SP);
 601     z_bre(OK);
 602     reentry = stop_chain_static(reentry, "invalid frame pointer Z_fp");
 603     bind(OK);
 604   }
 605   {
 606     // Resp must not point into or below the operand stack,
 607     // i.e. IJAVA_STATE.monitors > Resp.
 608     NearLabel OK;
 609     Register Rmonitors = Rtemp;
 610     z_lg(Rmonitors, _z_ijava_state_neg(monitors), Z_fp);
 611     compareU64_and_branch(Rmonitors, Resp, bcondHigh, OK);
 612     reentry = stop_chain_static(reentry, "too many pops: Z_esp points into monitor area");
 613     bind(OK);
 614   }
 615   {
 616     // Resp may point to the last word of TOP_IJAVA_FRAME_ABI, but not below
 617     // i.e. !(Z_SP + frame::z_top_ijava_frame_abi_size - Interpreter::stackElementSize > Resp).
 618     NearLabel OK;
 619     Register Rabi_bottom = Rtemp;
 620     add2reg(Rabi_bottom, frame::z_top_ijava_frame_abi_size - Interpreter::stackElementSize, Z_SP);
 621     compareU64_and_branch(Rabi_bottom, Resp, bcondNotHigh, OK);
 622     reentry = stop_chain_static(reentry, "too many pushes: Z_esp points into TOP_IJAVA_FRAME_ABI");
 623     bind(OK);
 624   }
 625 }
 626 
 627 void InterpreterMacroAssembler::asm_assert_ijava_state_magic(Register tmp) {
 628   Label magic_ok;
 629   load_const_optimized(tmp, frame::z_istate_magic_number);
 630   z_cg(tmp, Address(Z_fp, _z_ijava_state_neg(magic)));
 631   z_bre(magic_ok);
 632   stop_static("error: wrong magic number in ijava_state access");
 633   bind(magic_ok);
 634 }
 635 #endif // ASSERT
 636 
 637 void InterpreterMacroAssembler::save_bcp() {
 638   z_stg(Z_bcp, Address(Z_fp, _z_ijava_state_neg(bcp)));
 639   asm_assert_ijava_state_magic(Z_bcp);
 640   NOT_PRODUCT(z_lg(Z_bcp, Address(Z_fp, _z_ijava_state_neg(bcp))));
 641 }
 642 
 643 void InterpreterMacroAssembler::restore_bcp() {
 644   asm_assert_ijava_state_magic(Z_bcp);
 645   z_lg(Z_bcp, Address(Z_fp, _z_ijava_state_neg(bcp)));
 646 }
 647 
 648 void InterpreterMacroAssembler::save_esp() {
 649   z_stg(Z_esp, Address(Z_fp, _z_ijava_state_neg(esp)));
 650 }
 651 
 652 void InterpreterMacroAssembler::restore_esp() {
 653   asm_assert_ijava_state_magic(Z_esp);
 654   z_lg(Z_esp, Address(Z_fp, _z_ijava_state_neg(esp)));
 655 }
 656 
 657 void InterpreterMacroAssembler::get_monitors(Register reg) {
 658   asm_assert_ijava_state_magic(reg);
 659   mem2reg_opt(reg, Address(Z_fp, _z_ijava_state_neg(monitors)));
 660 }
 661 
 662 void InterpreterMacroAssembler::save_monitors(Register reg) {
 663   reg2mem_opt(reg, Address(Z_fp, _z_ijava_state_neg(monitors)));
 664 }
 665 
 666 void InterpreterMacroAssembler::get_mdp(Register mdp) {
 667   z_lg(mdp, _z_ijava_state_neg(mdx), Z_fp);
 668 }
 669 
 670 void InterpreterMacroAssembler::save_mdp(Register mdp) {
 671   z_stg(mdp, _z_ijava_state_neg(mdx), Z_fp);
 672 }
 673 
 674 // Values that are only read (besides initialization).
 675 void InterpreterMacroAssembler::restore_locals() {
 676   asm_assert_ijava_state_magic(Z_locals);
 677   z_lg(Z_locals, Address(Z_fp, _z_ijava_state_neg(locals)));
 678 }
 679 
 680 void InterpreterMacroAssembler::get_method(Register reg) {
 681   asm_assert_ijava_state_magic(reg);
 682   z_lg(reg, Address(Z_fp, _z_ijava_state_neg(method)));
 683 }
 684 
 685 void InterpreterMacroAssembler::get_2_byte_integer_at_bcp(Register Rdst, int bcp_offset,
 686                                                           signedOrNot is_signed) {
 687   // Rdst is an 8-byte return value!!!
 688 
 689   // Unaligned loads incur only a small penalty on z/Architecture. The penalty
 690   // is a few (2..3) ticks, even when the load crosses a cache line
 691   // boundary. In case of a cache miss, the stall could, of course, be
 692   // much longer.
 693 
 694   switch (is_signed) {
 695     case Signed:
 696       z_lgh(Rdst, bcp_offset, Z_R0, Z_bcp);
 697      break;
 698    case Unsigned:
 699      z_llgh(Rdst, bcp_offset, Z_R0, Z_bcp);
 700      break;
 701    default:
 702      ShouldNotReachHere();
 703   }
 704 }
 705 
 706 
 707 void InterpreterMacroAssembler::get_4_byte_integer_at_bcp(Register Rdst, int bcp_offset,
 708                                                           setCCOrNot set_cc) {
 709   // Rdst is an 8-byte return value!!!
 710 
 711   // Unaligned loads incur only a small penalty on z/Architecture. The penalty
 712   // is a few (2..3) ticks, even when the load crosses a cache line
 713   // boundary. In case of a cache miss, the stall could, of course, be
 714   // much longer.
 715 
 716   // Both variants implement a sign-extending int2long load.
 717   if (set_cc == set_CC) {
 718     load_and_test_int2long(Rdst, Address(Z_bcp, (intptr_t)bcp_offset));
 719   } else {
 720     mem2reg_signed_opt(    Rdst, Address(Z_bcp, (intptr_t)bcp_offset));
 721   }
 722 }
 723 
 724 void InterpreterMacroAssembler::get_constant_pool(Register Rdst) {
 725   get_method(Rdst);
 726   mem2reg_opt(Rdst, Address(Rdst, Method::const_offset()));
 727   mem2reg_opt(Rdst, Address(Rdst, ConstMethod::constants_offset()));
 728 }
 729 
 730 void InterpreterMacroAssembler::get_cpool_and_tags(Register Rcpool, Register Rtags) {
 731   get_constant_pool(Rcpool);
 732   mem2reg_opt(Rtags, Address(Rcpool, ConstantPool::tags_offset_in_bytes()));
 733 }
 734 
 735 // Unlock if synchronized method.
 736 //
 737 // Unlock the receiver if this is a synchronized method.
 738 // Unlock any Java monitors from syncronized blocks.
 739 //
 740 // If there are locked Java monitors
 741 //   If throw_monitor_exception
 742 //     throws IllegalMonitorStateException
 743 //   Else if install_monitor_exception
 744 //     installs IllegalMonitorStateException
 745 //   Else
 746 //     no error processing
 747 void InterpreterMacroAssembler::unlock_if_synchronized_method(TosState state,
 748                                                               bool throw_monitor_exception,
 749                                                               bool install_monitor_exception) {
 750   NearLabel unlocked, unlock, no_unlock;
 751 
 752   {
 753     Register R_method = Z_ARG2;
 754     Register R_do_not_unlock_if_synchronized = Z_ARG3;
 755 
 756     // Get the value of _do_not_unlock_if_synchronized into G1_scratch.
 757     const Address do_not_unlock_if_synchronized(Z_thread,
 758                                                 JavaThread::do_not_unlock_if_synchronized_offset());
 759     load_sized_value(R_do_not_unlock_if_synchronized, do_not_unlock_if_synchronized, 1, false /*unsigned*/);
 760     z_mvi(do_not_unlock_if_synchronized, false); // Reset the flag.
 761 
 762     // Check if synchronized method.
 763     get_method(R_method);
 764     verify_oop(Z_tos, state);
 765     push(state); // Save tos/result.
 766     testbit(method2_(R_method, access_flags), JVM_ACC_SYNCHRONIZED_BIT);
 767     z_bfalse(unlocked);
 768 
 769     // Don't unlock anything if the _do_not_unlock_if_synchronized flag
 770     // is set.
 771     compareU64_and_branch(R_do_not_unlock_if_synchronized, (intptr_t)0L, bcondNotEqual, no_unlock);
 772   }
 773 
 774   // unlock monitor
 775 
 776   // BasicObjectLock will be first in list, since this is a
 777   // synchronized method. However, need to check that the object has
 778   // not been unlocked by an explicit monitorexit bytecode.
 779   const Address monitor(Z_fp, -(frame::z_ijava_state_size + (int) sizeof(BasicObjectLock)));
 780   // We use Z_ARG2 so that if we go slow path it will be the correct
 781   // register for unlock_object to pass to VM directly.
 782   load_address(Z_ARG2, monitor); // Address of first monitor.
 783   z_lg(Z_ARG3, Address(Z_ARG2, BasicObjectLock::obj_offset_in_bytes()));
 784   compareU64_and_branch(Z_ARG3, (intptr_t)0L, bcondNotEqual, unlock);
 785 
 786   if (throw_monitor_exception) {
 787     // Entry already unlocked need to throw an exception.
 788     MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
 789     should_not_reach_here();
 790   } else {
 791     // Monitor already unlocked during a stack unroll.
 792     // If requested, install an illegal_monitor_state_exception.
 793     // Continue with stack unrolling.
 794     if (install_monitor_exception) {
 795       MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::new_illegal_monitor_state_exception));
 796     }
 797    z_bru(unlocked);
 798   }
 799 
 800   bind(unlock);
 801 
 802   unlock_object(Z_ARG2);
 803 
 804   bind(unlocked);
 805 
 806   // I0, I1: Might contain return value
 807 
 808   // Check that all monitors are unlocked.
 809   {
 810     NearLabel loop, exception, entry, restart;
 811     const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
 812     // We use Z_ARG2 so that if we go slow path it will be the correct
 813     // register for unlock_object to pass to VM directly.
 814     Register R_current_monitor = Z_ARG2;
 815     Register R_monitor_block_bot = Z_ARG1;
 816     const Address monitor_block_top(Z_fp, _z_ijava_state_neg(monitors));
 817     const Address monitor_block_bot(Z_fp, -frame::z_ijava_state_size);
 818 
 819     bind(restart);
 820     // Starting with top-most entry.
 821     z_lg(R_current_monitor, monitor_block_top);
 822     // Points to word before bottom of monitor block.
 823     load_address(R_monitor_block_bot, monitor_block_bot);
 824     z_bru(entry);
 825 
 826     // Entry already locked, need to throw exception.
 827     bind(exception);
 828 
 829     if (throw_monitor_exception) {
 830       // Throw exception.
 831       MacroAssembler::call_VM(noreg,
 832                               CAST_FROM_FN_PTR(address, InterpreterRuntime::
 833                                                throw_illegal_monitor_state_exception));
 834       should_not_reach_here();
 835     } else {
 836       // Stack unrolling. Unlock object and install illegal_monitor_exception.
 837       // Unlock does not block, so don't have to worry about the frame.
 838       // We don't have to preserve c_rarg1 since we are going to throw an exception.
 839       unlock_object(R_current_monitor);
 840       if (install_monitor_exception) {
 841         call_VM(noreg, CAST_FROM_FN_PTR(address,
 842                                         InterpreterRuntime::
 843                                         new_illegal_monitor_state_exception));
 844       }
 845       z_bru(restart);
 846     }
 847 
 848     bind(loop);
 849     // Check if current entry is used.
 850     load_and_test_long(Z_R0_scratch, Address(R_current_monitor, BasicObjectLock::obj_offset_in_bytes()));
 851     z_brne(exception);
 852 
 853     add2reg(R_current_monitor, entry_size); // Otherwise advance to next entry.
 854     bind(entry);
 855     compareU64_and_branch(R_current_monitor, R_monitor_block_bot, bcondNotEqual, loop);
 856   }
 857 
 858   bind(no_unlock);
 859   pop(state);
 860   verify_oop(Z_tos, state);
 861 }
 862 
 863 void InterpreterMacroAssembler::narrow(Register result, Register ret_type) {
 864   get_method(ret_type);
 865   z_lg(ret_type, Address(ret_type, in_bytes(Method::const_offset())));
 866   z_lb(ret_type, Address(ret_type, in_bytes(ConstMethod::result_type_offset())));
 867 
 868   Label notBool, notByte, notChar, done;
 869 
 870   // common case first
 871   compareU32_and_branch(ret_type, T_INT, bcondEqual, done);
 872 
 873   compareU32_and_branch(ret_type, T_BOOLEAN, bcondNotEqual, notBool);
 874   z_nilf(result, 0x1);
 875   z_bru(done);
 876 
 877   bind(notBool);
 878   compareU32_and_branch(ret_type, T_BYTE, bcondNotEqual, notByte);
 879   z_lbr(result, result);
 880   z_bru(done);
 881 
 882   bind(notByte);
 883   compareU32_and_branch(ret_type, T_CHAR, bcondNotEqual, notChar);
 884   z_nilf(result, 0xffff);
 885   z_bru(done);
 886 
 887   bind(notChar);
 888   // compareU32_and_branch(ret_type, T_SHORT, bcondNotEqual, notShort);
 889   z_lhr(result, result);
 890 
 891   // Nothing to do for T_INT
 892   bind(done);
 893 }
 894 
 895 // remove activation
 896 //
 897 // Unlock the receiver if this is a synchronized method.
 898 // Unlock any Java monitors from syncronized blocks.
 899 // Remove the activation from the stack.
 900 //
 901 // If there are locked Java monitors
 902 //   If throw_monitor_exception
 903 //     throws IllegalMonitorStateException
 904 //   Else if install_monitor_exception
 905 //     installs IllegalMonitorStateException
 906 //   Else
 907 //     no error processing
 908 void InterpreterMacroAssembler::remove_activation(TosState state,
 909                                                   Register return_pc,
 910                                                   bool throw_monitor_exception,
 911                                                   bool install_monitor_exception,
 912                                                   bool notify_jvmti) {
 913   BLOCK_COMMENT("remove_activation {");
 914   unlock_if_synchronized_method(state, throw_monitor_exception, install_monitor_exception);
 915 
 916   // Save result (push state before jvmti call and pop it afterwards) and notify jvmti.
 917   notify_method_exit(false, state, notify_jvmti ? NotifyJVMTI : SkipNotifyJVMTI);
 918 
 919   if (StackReservedPages > 0) {
 920     BLOCK_COMMENT("reserved_stack_check:");
 921     // Test if reserved zone needs to be enabled.
 922     Label no_reserved_zone_enabling;
 923 
 924     // Compare frame pointers. There is no good stack pointer, as with stack
 925     // frame compression we can get different SPs when we do calls. A subsequent
 926     // call could have a smaller SP, so that this compare succeeds for an
 927     // inner call of the method annotated with ReservedStack.
 928     z_lg(Z_R0, Address(Z_SP, (intptr_t)_z_abi(callers_sp)));
 929     z_clg(Z_R0, Address(Z_thread, JavaThread::reserved_stack_activation_offset())); // Compare with frame pointer in memory.
 930     z_brl(no_reserved_zone_enabling);
 931 
 932     // Enable reserved zone again, throw stack overflow exception.
 933     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), Z_thread);
 934     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_delayed_StackOverflowError));
 935 
 936     should_not_reach_here();
 937 
 938     bind(no_reserved_zone_enabling);
 939   }
 940 
 941   verify_oop(Z_tos, state);
 942   verify_thread();
 943 
 944   pop_interpreter_frame(return_pc, Z_ARG2, Z_ARG3);
 945   BLOCK_COMMENT("} remove_activation");
 946 }
 947 
 948 // lock object
 949 //
 950 // Registers alive
 951 //   monitor - Address of the BasicObjectLock to be used for locking,
 952 //             which must be initialized with the object to lock.
 953 //   object  - Address of the object to be locked.
 954 void InterpreterMacroAssembler::lock_object(Register monitor, Register object) {
 955 
 956   if (UseHeavyMonitors) {
 957     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
 958             monitor, /*check_for_exceptions=*/false);
 959     return;
 960   }
 961 
 962   // template code:
 963   //
 964   // markOop displaced_header = obj->mark().set_unlocked();
 965   // monitor->lock()->set_displaced_header(displaced_header);
 966   // if (Atomic::cmpxchg(/*ex=*/monitor, /*addr*/obj->mark_addr(), /*cmp*/displaced_header) == displaced_header) {
 967   //   // We stored the monitor address into the object's mark word.
 968   // } else if (THREAD->is_lock_owned((address)displaced_header))
 969   //   // Simple recursive case.
 970   //   monitor->lock()->set_displaced_header(NULL);
 971   // } else {
 972   //   // Slow path.
 973   //   InterpreterRuntime::monitorenter(THREAD, monitor);
 974   // }
 975 
 976   const Register displaced_header = Z_ARG5;
 977   const Register object_mark_addr = Z_ARG4;
 978   const Register current_header   = Z_ARG5;
 979 
 980   NearLabel done;
 981   NearLabel slow_case;
 982 
 983   // markOop displaced_header = obj->mark().set_unlocked();
 984 
 985   // Load markOop from object into displaced_header.
 986   z_lg(displaced_header, oopDesc::mark_offset_in_bytes(), object);
 987 
 988   if (UseBiasedLocking) {
 989     biased_locking_enter(object, displaced_header, Z_R1, Z_R0, done, &slow_case);
 990   }
 991 
 992   // Set displaced_header to be (markOop of object | UNLOCK_VALUE).
 993   z_oill(displaced_header, markOopDesc::unlocked_value);
 994 
 995   // monitor->lock()->set_displaced_header(displaced_header);
 996 
 997   // Initialize the box (Must happen before we update the object mark!).
 998   z_stg(displaced_header, BasicObjectLock::lock_offset_in_bytes() +
 999                           BasicLock::displaced_header_offset_in_bytes(), monitor);
1000 
1001   // if (Atomic::cmpxchg(/*ex=*/monitor, /*addr*/obj->mark_addr(), /*cmp*/displaced_header) == displaced_header) {
1002 
1003   // Store stack address of the BasicObjectLock (this is monitor) into object.
1004   add2reg(object_mark_addr, oopDesc::mark_offset_in_bytes(), object);
1005 
1006   z_csg(displaced_header, monitor, 0, object_mark_addr);
1007   assert(current_header==displaced_header, "must be same register"); // Identified two registers from z/Architecture.
1008 
1009   z_bre(done);
1010 
1011   // } else if (THREAD->is_lock_owned((address)displaced_header))
1012   //   // Simple recursive case.
1013   //   monitor->lock()->set_displaced_header(NULL);
1014 
1015   // We did not see an unlocked object so try the fast recursive case.
1016 
1017   // Check if owner is self by comparing the value in the markOop of object
1018   // (current_header) with the stack pointer.
1019   z_sgr(current_header, Z_SP);
1020 
1021   assert(os::vm_page_size() > 0xfff, "page size too small - change the constant");
1022 
1023   // The prior sequence "LGR, NGR, LTGR" can be done better
1024   // (Z_R1 is temp and not used after here).
1025   load_const_optimized(Z_R0, (~(os::vm_page_size()-1) | markOopDesc::lock_mask_in_place));
1026   z_ngr(Z_R0, current_header); // AND sets CC (result eq/ne 0)
1027 
1028   // If condition is true we are done and hence we can store 0 in the displaced
1029   // header indicating it is a recursive lock and be done.
1030   z_brne(slow_case);
1031   z_release();  // Membar unnecessary on zarch AND because the above csg does a sync before and after.
1032   z_stg(Z_R0/*==0!*/, BasicObjectLock::lock_offset_in_bytes() +
1033                       BasicLock::displaced_header_offset_in_bytes(), monitor);
1034   z_bru(done);
1035 
1036   // } else {
1037   //   // Slow path.
1038   //   InterpreterRuntime::monitorenter(THREAD, monitor);
1039 
1040   // None of the above fast optimizations worked so we have to get into the
1041   // slow case of monitor enter.
1042   bind(slow_case);
1043 
1044   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
1045           monitor, /*check_for_exceptions=*/false);
1046 
1047   // }
1048 
1049   bind(done);
1050 }
1051 
1052 // Unlocks an object. Used in monitorexit bytecode and remove_activation.
1053 //
1054 // Registers alive
1055 //   monitor - address of the BasicObjectLock to be used for locking,
1056 //             which must be initialized with the object to lock.
1057 //
1058 // Throw IllegalMonitorException if object is not locked by current thread.
1059 void InterpreterMacroAssembler::unlock_object(Register monitor, Register object) {
1060 
1061   if (UseHeavyMonitors) {
1062     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit),
1063             monitor, /*check_for_exceptions=*/ true);
1064     return;
1065   }
1066 
1067 // else {
1068   // template code:
1069   //
1070   // if ((displaced_header = monitor->displaced_header()) == NULL) {
1071   //   // Recursive unlock. Mark the monitor unlocked by setting the object field to NULL.
1072   //   monitor->set_obj(NULL);
1073   // } else if (Atomic::cmpxchg(displaced_header, obj->mark_addr(), monitor) == monitor) {
1074   //   // We swapped the unlocked mark in displaced_header into the object's mark word.
1075   //   monitor->set_obj(NULL);
1076   // } else {
1077   //   // Slow path.
1078   //   InterpreterRuntime::monitorexit(THREAD, monitor);
1079   // }
1080 
1081   const Register displaced_header = Z_ARG4;
1082   const Register current_header   = Z_R1;
1083   Address obj_entry(monitor, BasicObjectLock::obj_offset_in_bytes());
1084   Label done;
1085 
1086   if (object == noreg) {
1087     // In the template interpreter, we must assure that the object
1088     // entry in the monitor is cleared on all paths. Thus we move
1089     // loading up to here, and clear the entry afterwards.
1090     object = Z_ARG3; // Use Z_ARG3 if caller didn't pass object.
1091     z_lg(object, obj_entry);
1092   }
1093 
1094   assert_different_registers(monitor, object, displaced_header, current_header);
1095 
1096   // if ((displaced_header = monitor->displaced_header()) == NULL) {
1097   //   // Recursive unlock. Mark the monitor unlocked by setting the object field to NULL.
1098   //   monitor->set_obj(NULL);
1099 
1100   clear_mem(obj_entry, sizeof(oop));
1101 
1102   if (UseBiasedLocking) {
1103     // The object address from the monitor is in object.
1104     assert(oopDesc::mark_offset_in_bytes() == 0, "offset of _mark is not 0");
1105     biased_locking_exit(object, displaced_header, done);
1106   }
1107 
1108   // Test first if we are in the fast recursive case.
1109   MacroAssembler::load_and_test_long(displaced_header,
1110                                      Address(monitor, BasicObjectLock::lock_offset_in_bytes() +
1111                                                       BasicLock::displaced_header_offset_in_bytes()));
1112   z_bre(done); // displaced_header == 0 -> goto done
1113 
1114   // } else if (Atomic::cmpxchg(displaced_header, obj->mark_addr(), monitor) == monitor) {
1115   //   // We swapped the unlocked mark in displaced_header into the object's mark word.
1116   //   monitor->set_obj(NULL);
1117 
1118   // If we still have a lightweight lock, unlock the object and be done.
1119 
1120   // The markword is expected to be at offset 0.
1121   assert(oopDesc::mark_offset_in_bytes() == 0, "unlock_object: review code below");
1122 
1123   // We have the displaced header in displaced_header. If the lock is still
1124   // lightweight, it will contain the monitor address and we'll store the
1125   // displaced header back into the object's mark word.
1126   z_lgr(current_header, monitor);
1127   z_csg(current_header, displaced_header, 0, object);
1128   z_bre(done);
1129 
1130   // } else {
1131   //   // Slow path.
1132   //   InterpreterRuntime::monitorexit(THREAD, monitor);
1133 
1134   // The lock has been converted into a heavy lock and hence
1135   // we need to get into the slow case.
1136   z_stg(object, obj_entry);   // Restore object entry, has been cleared above.
1137   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit),
1138           monitor,  /*check_for_exceptions=*/false);
1139 
1140   // }
1141 
1142   bind(done);
1143 }
1144 
1145 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp, Label& zero_continue) {
1146   assert(ProfileInterpreter, "must be profiling interpreter");
1147   load_and_test_long(mdp, Address(Z_fp, _z_ijava_state_neg(mdx)));
1148   z_brz(zero_continue);
1149 }
1150 
1151 // Set the method data pointer for the current bcp.
1152 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
1153   assert(ProfileInterpreter, "must be profiling interpreter");
1154   Label    set_mdp;
1155   Register mdp    = Z_ARG4;
1156   Register method = Z_ARG5;
1157 
1158   get_method(method);
1159   // Test MDO to avoid the call if it is NULL.
1160   load_and_test_long(mdp, method2_(method, method_data));
1161   z_brz(set_mdp);
1162 
1163   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), method, Z_bcp);
1164   // Z_RET: mdi
1165   // Mdo is guaranteed to be non-zero here, we checked for it before the call.
1166   assert(method->is_nonvolatile(), "choose nonvolatile reg or reload from frame");
1167   z_lg(mdp, method2_(method, method_data)); // Must reload, mdp is volatile reg.
1168   add2reg_with_index(mdp, in_bytes(MethodData::data_offset()), Z_RET, mdp);
1169 
1170   bind(set_mdp);
1171   save_mdp(mdp);
1172 }
1173 
1174 void InterpreterMacroAssembler::verify_method_data_pointer() {
1175   assert(ProfileInterpreter, "must be profiling interpreter");
1176 #ifdef ASSERT
1177   NearLabel verify_continue;
1178   Register bcp_expected = Z_ARG3;
1179   Register mdp    = Z_ARG4;
1180   Register method = Z_ARG5;
1181 
1182   test_method_data_pointer(mdp, verify_continue); // If mdp is zero, continue
1183   get_method(method);
1184 
1185   // If the mdp is valid, it will point to a DataLayout header which is
1186   // consistent with the bcp. The converse is highly probable also.
1187   load_sized_value(bcp_expected, Address(mdp, DataLayout::bci_offset()), 2, false /*signed*/);
1188   z_ag(bcp_expected, Address(method, Method::const_offset()));
1189   load_address(bcp_expected, Address(bcp_expected, ConstMethod::codes_offset()));
1190   compareU64_and_branch(bcp_expected, Z_bcp, bcondEqual, verify_continue);
1191   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp), method, Z_bcp, mdp);
1192   bind(verify_continue);
1193 #endif // ASSERT
1194 }
1195 
1196 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in, int constant, Register value) {
1197   assert(ProfileInterpreter, "must be profiling interpreter");
1198   z_stg(value, constant, mdp_in);
1199 }
1200 
1201 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
1202                                                       int constant,
1203                                                       Register tmp,
1204                                                       bool decrement) {
1205   assert_different_registers(mdp_in, tmp);
1206   // counter address
1207   Address data(mdp_in, constant);
1208   const int delta = decrement ? -DataLayout::counter_increment : DataLayout::counter_increment;
1209   add2mem_64(Address(mdp_in, constant), delta, tmp);
1210 }
1211 
1212 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in,
1213                                                 int flag_byte_constant) {
1214   assert(ProfileInterpreter, "must be profiling interpreter");
1215   // Set the flag.
1216   z_oi(Address(mdp_in, DataLayout::flags_offset()), flag_byte_constant);
1217 }
1218 
1219 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in,
1220                                                  int offset,
1221                                                  Register value,
1222                                                  Register test_value_out,
1223                                                  Label& not_equal_continue) {
1224   assert(ProfileInterpreter, "must be profiling interpreter");
1225   if (test_value_out == noreg) {
1226     z_cg(value, Address(mdp_in, offset));
1227     z_brne(not_equal_continue);
1228   } else {
1229     // Put the test value into a register, so caller can use it:
1230     z_lg(test_value_out, Address(mdp_in, offset));
1231     compareU64_and_branch(test_value_out, value, bcondNotEqual, not_equal_continue);
1232   }
1233 }
1234 
1235 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in, int offset_of_disp) {
1236   update_mdp_by_offset(mdp_in, noreg, offset_of_disp);
1237 }
1238 
1239 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
1240                                                      Register dataidx,
1241                                                      int offset_of_disp) {
1242   assert(ProfileInterpreter, "must be profiling interpreter");
1243   Address disp_address(mdp_in, dataidx, offset_of_disp);
1244   Assembler::z_ag(mdp_in, disp_address);
1245   save_mdp(mdp_in);
1246 }
1247 
1248 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in, int constant) {
1249   assert(ProfileInterpreter, "must be profiling interpreter");
1250   add2reg(mdp_in, constant);
1251   save_mdp(mdp_in);
1252 }
1253 
1254 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) {
1255   assert(ProfileInterpreter, "must be profiling interpreter");
1256   assert(return_bci->is_nonvolatile(), "choose nonvolatile reg or save/restore");
1257   call_VM(noreg,
1258           CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret),
1259           return_bci);
1260 }
1261 
1262 void InterpreterMacroAssembler::profile_taken_branch(Register mdp, Register bumped_count) {
1263   if (ProfileInterpreter) {
1264     Label profile_continue;
1265 
1266     // If no method data exists, go to profile_continue.
1267     // Otherwise, assign to mdp.
1268     test_method_data_pointer(mdp, profile_continue);
1269 
1270     // We are taking a branch. Increment the taken count.
1271     // We inline increment_mdp_data_at to return bumped_count in a register
1272     //increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset()));
1273     Address data(mdp, JumpData::taken_offset());
1274     z_lg(bumped_count, data);
1275     // 64-bit overflow is very unlikely. Saturation to 32-bit values is
1276     // performed when reading the counts.
1277     add2reg(bumped_count, DataLayout::counter_increment);
1278     z_stg(bumped_count, data); // Store back out
1279 
1280     // The method data pointer needs to be updated to reflect the new target.
1281     update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset()));
1282     bind(profile_continue);
1283   }
1284 }
1285 
1286 // Kills Z_R1_scratch.
1287 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp) {
1288   if (ProfileInterpreter) {
1289     Label profile_continue;
1290 
1291     // If no method data exists, go to profile_continue.
1292     test_method_data_pointer(mdp, profile_continue);
1293 
1294     // We are taking a branch. Increment the not taken count.
1295     increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset()), Z_R1_scratch);
1296 
1297     // The method data pointer needs to be updated to correspond to
1298     // the next bytecode.
1299     update_mdp_by_constant(mdp, in_bytes(BranchData::branch_data_size()));
1300     bind(profile_continue);
1301   }
1302 }
1303 
1304 // Kills: Z_R1_scratch.
1305 void InterpreterMacroAssembler::profile_call(Register mdp) {
1306   if (ProfileInterpreter) {
1307     Label profile_continue;
1308 
1309     // If no method data exists, go to profile_continue.
1310     test_method_data_pointer(mdp, profile_continue);
1311 
1312     // We are making a call. Increment the count.
1313     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1314 
1315     // The method data pointer needs to be updated to reflect the new target.
1316     update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size()));
1317     bind(profile_continue);
1318   }
1319 }
1320 
1321 void InterpreterMacroAssembler::profile_final_call(Register mdp) {
1322   if (ProfileInterpreter) {
1323     Label profile_continue;
1324 
1325     // If no method data exists, go to profile_continue.
1326     test_method_data_pointer(mdp, profile_continue);
1327 
1328     // We are making a call. Increment the count.
1329     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1330 
1331     // The method data pointer needs to be updated to reflect the new target.
1332     update_mdp_by_constant(mdp, in_bytes(VirtualCallData::virtual_call_data_size()));
1333     bind(profile_continue);
1334   }
1335 }
1336 
1337 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
1338                                                      Register mdp,
1339                                                      Register reg2,
1340                                                      bool receiver_can_be_null) {
1341   if (ProfileInterpreter) {
1342     NearLabel profile_continue;
1343 
1344     // If no method data exists, go to profile_continue.
1345     test_method_data_pointer(mdp, profile_continue);
1346 
1347     NearLabel skip_receiver_profile;
1348     if (receiver_can_be_null) {
1349       NearLabel not_null;
1350       compareU64_and_branch(receiver, (intptr_t)0L, bcondNotEqual, not_null);
1351       // We are making a call. Increment the count for null receiver.
1352       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1353       z_bru(skip_receiver_profile);
1354       bind(not_null);
1355     }
1356 
1357     // Record the receiver type.
1358     record_klass_in_profile(receiver, mdp, reg2, true);
1359     bind(skip_receiver_profile);
1360 
1361     // The method data pointer needs to be updated to reflect the new target.
1362     update_mdp_by_constant(mdp, in_bytes(VirtualCallData::virtual_call_data_size()));
1363     bind(profile_continue);
1364   }
1365 }
1366 
1367 // This routine creates a state machine for updating the multi-row
1368 // type profile at a virtual call site (or other type-sensitive bytecode).
1369 // The machine visits each row (of receiver/count) until the receiver type
1370 // is found, or until it runs out of rows. At the same time, it remembers
1371 // the location of the first empty row. (An empty row records null for its
1372 // receiver, and can be allocated for a newly-observed receiver type.)
1373 // Because there are two degrees of freedom in the state, a simple linear
1374 // search will not work; it must be a decision tree. Hence this helper
1375 // function is recursive, to generate the required tree structured code.
1376 // It's the interpreter, so we are trading off code space for speed.
1377 // See below for example code.
1378 void InterpreterMacroAssembler::record_klass_in_profile_helper(
1379                                         Register receiver, Register mdp,
1380                                         Register reg2, int start_row,
1381                                         Label& done, bool is_virtual_call) {
1382   if (TypeProfileWidth == 0) {
1383     if (is_virtual_call) {
1384       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1385     }
1386     return;
1387   }
1388 
1389   int last_row = VirtualCallData::row_limit() - 1;
1390   assert(start_row <= last_row, "must be work left to do");
1391   // Test this row for both the receiver and for null.
1392   // Take any of three different outcomes:
1393   //   1. found receiver => increment count and goto done
1394   //   2. found null => keep looking for case 1, maybe allocate this cell
1395   //   3. found something else => keep looking for cases 1 and 2
1396   // Case 3 is handled by a recursive call.
1397   for (int row = start_row; row <= last_row; row++) {
1398     NearLabel next_test;
1399     bool test_for_null_also = (row == start_row);
1400 
1401     // See if the receiver is receiver[n].
1402     int recvr_offset = in_bytes(VirtualCallData::receiver_offset(row));
1403     test_mdp_data_at(mdp, recvr_offset, receiver,
1404                      (test_for_null_also ? reg2 : noreg),
1405                      next_test);
1406     // (Reg2 now contains the receiver from the CallData.)
1407 
1408     // The receiver is receiver[n]. Increment count[n].
1409     int count_offset = in_bytes(VirtualCallData::receiver_count_offset(row));
1410     increment_mdp_data_at(mdp, count_offset);
1411     z_bru(done);
1412     bind(next_test);
1413 
1414     if (test_for_null_also) {
1415       Label found_null;
1416       // Failed the equality check on receiver[n]... Test for null.
1417       z_ltgr(reg2, reg2);
1418       if (start_row == last_row) {
1419         // The only thing left to do is handle the null case.
1420         if (is_virtual_call) {
1421           z_brz(found_null);
1422           // Receiver did not match any saved receiver and there is no empty row for it.
1423           // Increment total counter to indicate polymorphic case.
1424           increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1425           z_bru(done);
1426           bind(found_null);
1427         } else {
1428           z_brnz(done);
1429         }
1430         break;
1431       }
1432       // Since null is rare, make it be the branch-taken case.
1433       z_brz(found_null);
1434 
1435       // Put all the "Case 3" tests here.
1436       record_klass_in_profile_helper(receiver, mdp, reg2, start_row + 1, done, is_virtual_call);
1437 
1438       // Found a null. Keep searching for a matching receiver,
1439       // but remember that this is an empty (unused) slot.
1440       bind(found_null);
1441     }
1442   }
1443 
1444   // In the fall-through case, we found no matching receiver, but we
1445   // observed the receiver[start_row] is NULL.
1446 
1447   // Fill in the receiver field and increment the count.
1448   int recvr_offset = in_bytes(VirtualCallData::receiver_offset(start_row));
1449   set_mdp_data_at(mdp, recvr_offset, receiver);
1450   int count_offset = in_bytes(VirtualCallData::receiver_count_offset(start_row));
1451   load_const_optimized(reg2, DataLayout::counter_increment);
1452   set_mdp_data_at(mdp, count_offset, reg2);
1453   if (start_row > 0) {
1454     z_bru(done);
1455   }
1456 }
1457 
1458 // Example state machine code for three profile rows:
1459 //   // main copy of decision tree, rooted at row[1]
1460 //   if (row[0].rec == rec) { row[0].incr(); goto done; }
1461 //   if (row[0].rec != NULL) {
1462 //     // inner copy of decision tree, rooted at row[1]
1463 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1464 //     if (row[1].rec != NULL) {
1465 //       // degenerate decision tree, rooted at row[2]
1466 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1467 //       if (row[2].rec != NULL) { count.incr(); goto done; } // overflow
1468 //       row[2].init(rec); goto done;
1469 //     } else {
1470 //       // remember row[1] is empty
1471 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1472 //       row[1].init(rec); goto done;
1473 //     }
1474 //   } else {
1475 //     // remember row[0] is empty
1476 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1477 //     if (row[2].rec == rec) { row[2].incr(); goto done; }
1478 //     row[0].init(rec); goto done;
1479 //   }
1480 //   done:
1481 
1482 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1483                                                         Register mdp, Register reg2,
1484                                                         bool is_virtual_call) {
1485   assert(ProfileInterpreter, "must be profiling");
1486   Label done;
1487 
1488   record_klass_in_profile_helper(receiver, mdp, reg2, 0, done, is_virtual_call);
1489 
1490   bind (done);
1491 }
1492 
1493 void InterpreterMacroAssembler::profile_ret(Register return_bci, Register mdp) {
1494   if (ProfileInterpreter) {
1495     NearLabel profile_continue;
1496     uint row;
1497 
1498     // If no method data exists, go to profile_continue.
1499     test_method_data_pointer(mdp, profile_continue);
1500 
1501     // Update the total ret count.
1502     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1503 
1504     for (row = 0; row < RetData::row_limit(); row++) {
1505       NearLabel next_test;
1506 
1507       // See if return_bci is equal to bci[n]:
1508       test_mdp_data_at(mdp,
1509                        in_bytes(RetData::bci_offset(row)),
1510                        return_bci, noreg,
1511                        next_test);
1512 
1513       // Return_bci is equal to bci[n]. Increment the count.
1514       increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row)));
1515 
1516       // The method data pointer needs to be updated to reflect the new target.
1517       update_mdp_by_offset(mdp, in_bytes(RetData::bci_displacement_offset(row)));
1518       z_bru(profile_continue);
1519       bind(next_test);
1520     }
1521 
1522     update_mdp_for_ret(return_bci);
1523 
1524     bind(profile_continue);
1525   }
1526 }
1527 
1528 void InterpreterMacroAssembler::profile_null_seen(Register mdp) {
1529   if (ProfileInterpreter) {
1530     Label profile_continue;
1531 
1532     // If no method data exists, go to profile_continue.
1533     test_method_data_pointer(mdp, profile_continue);
1534 
1535     set_mdp_flag_at(mdp, BitData::null_seen_byte_constant());
1536 
1537     // The method data pointer needs to be updated.
1538     int mdp_delta = in_bytes(BitData::bit_data_size());
1539     if (TypeProfileCasts) {
1540       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1541     }
1542     update_mdp_by_constant(mdp, mdp_delta);
1543 
1544     bind(profile_continue);
1545   }
1546 }
1547 
1548 void InterpreterMacroAssembler::profile_typecheck_failed(Register mdp, Register tmp) {
1549   if (ProfileInterpreter && TypeProfileCasts) {
1550     Label profile_continue;
1551 
1552     // If no method data exists, go to profile_continue.
1553     test_method_data_pointer(mdp, profile_continue);
1554 
1555     int count_offset = in_bytes(CounterData::count_offset());
1556     // Back up the address, since we have already bumped the mdp.
1557     count_offset -= in_bytes(VirtualCallData::virtual_call_data_size());
1558 
1559     // *Decrement* the counter. We expect to see zero or small negatives.
1560     increment_mdp_data_at(mdp, count_offset, tmp, true);
1561 
1562     bind (profile_continue);
1563   }
1564 }
1565 
1566 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass, Register reg2) {
1567   if (ProfileInterpreter) {
1568     Label profile_continue;
1569 
1570     // If no method data exists, go to profile_continue.
1571     test_method_data_pointer(mdp, profile_continue);
1572 
1573     // The method data pointer needs to be updated.
1574     int mdp_delta = in_bytes(BitData::bit_data_size());
1575     if (TypeProfileCasts) {
1576       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1577 
1578       // Record the object type.
1579       record_klass_in_profile(klass, mdp, reg2, false);
1580     }
1581     update_mdp_by_constant(mdp, mdp_delta);
1582 
1583     bind(profile_continue);
1584   }
1585 }
1586 
1587 void InterpreterMacroAssembler::profile_switch_default(Register mdp) {
1588   if (ProfileInterpreter) {
1589     Label profile_continue;
1590 
1591     // If no method data exists, go to profile_continue.
1592     test_method_data_pointer(mdp, profile_continue);
1593 
1594     // Update the default case count.
1595     increment_mdp_data_at(mdp, in_bytes(MultiBranchData::default_count_offset()));
1596 
1597     // The method data pointer needs to be updated.
1598     update_mdp_by_offset(mdp, in_bytes(MultiBranchData::default_displacement_offset()));
1599 
1600     bind(profile_continue);
1601   }
1602 }
1603 
1604 // Kills: index, scratch1, scratch2.
1605 void InterpreterMacroAssembler::profile_switch_case(Register index,
1606                                                     Register mdp,
1607                                                     Register scratch1,
1608                                                     Register scratch2) {
1609   if (ProfileInterpreter) {
1610     Label profile_continue;
1611     assert_different_registers(index, mdp, scratch1, scratch2);
1612 
1613     // If no method data exists, go to profile_continue.
1614     test_method_data_pointer(mdp, profile_continue);
1615 
1616     // Build the base (index * per_case_size_in_bytes()) +
1617     // case_array_offset_in_bytes().
1618     z_sllg(index, index, exact_log2(in_bytes(MultiBranchData::per_case_size())));
1619     add2reg(index, in_bytes(MultiBranchData::case_array_offset()));
1620 
1621     // Add the calculated base to the mdp -> address of the case' data.
1622     Address case_data_addr(mdp, index);
1623     Register case_data = scratch1;
1624     load_address(case_data, case_data_addr);
1625 
1626     // Update the case count.
1627     increment_mdp_data_at(case_data,
1628                           in_bytes(MultiBranchData::relative_count_offset()),
1629                           scratch2);
1630 
1631     // The method data pointer needs to be updated.
1632     update_mdp_by_offset(mdp,
1633                          index,
1634                          in_bytes(MultiBranchData::relative_displacement_offset()));
1635 
1636     bind(profile_continue);
1637   }
1638 }
1639 
1640 // kills: R0, R1, flags, loads klass from obj (if not null)
1641 void InterpreterMacroAssembler::profile_obj_type(Register obj, Address mdo_addr, Register klass, bool cmp_done) {
1642   NearLabel null_seen, init_klass, do_nothing, do_update;
1643 
1644   // Klass = obj is allowed.
1645   const Register tmp = Z_R1;
1646   assert_different_registers(obj, mdo_addr.base(), tmp, Z_R0);
1647   assert_different_registers(klass, mdo_addr.base(), tmp, Z_R0);
1648 
1649   z_lg(tmp, mdo_addr);
1650   if (cmp_done) {
1651     z_brz(null_seen);
1652   } else {
1653     compareU64_and_branch(obj, (intptr_t)0, Assembler::bcondEqual, null_seen);
1654   }
1655 
1656   verify_oop(obj);
1657   load_klass(klass, obj);
1658 
1659   // Klass seen before, nothing to do (regardless of unknown bit).
1660   z_lgr(Z_R0, tmp);
1661   assert(Immediate::is_uimm(~TypeEntries::type_klass_mask, 16), "or change following instruction");
1662   z_nill(Z_R0, TypeEntries::type_klass_mask & 0xFFFF);
1663   compareU64_and_branch(Z_R0, klass, Assembler::bcondEqual, do_nothing);
1664 
1665   // Already unknown. Nothing to do anymore.
1666   z_tmll(tmp, TypeEntries::type_unknown);
1667   z_brc(Assembler::bcondAllOne, do_nothing);
1668 
1669   z_lgr(Z_R0, tmp);
1670   assert(Immediate::is_uimm(~TypeEntries::type_mask, 16), "or change following instruction");
1671   z_nill(Z_R0, TypeEntries::type_mask & 0xFFFF);
1672   compareU64_and_branch(Z_R0, (intptr_t)0, Assembler::bcondEqual, init_klass);
1673 
1674   // Different than before. Cannot keep accurate profile.
1675   z_oill(tmp, TypeEntries::type_unknown);
1676   z_bru(do_update);
1677 
1678   bind(init_klass);
1679   // Combine klass and null_seen bit (only used if (tmp & type_mask)==0).
1680   z_ogr(tmp, klass);
1681   z_bru(do_update);
1682 
1683   bind(null_seen);
1684   // Set null_seen if obj is 0.
1685   z_oill(tmp, TypeEntries::null_seen);
1686   // fallthru: z_bru(do_update);
1687 
1688   bind(do_update);
1689   z_stg(tmp, mdo_addr);
1690 
1691   bind(do_nothing);
1692 }
1693 
1694 void InterpreterMacroAssembler::profile_arguments_type(Register mdp, Register callee, Register tmp, bool is_virtual) {
1695   if (!ProfileInterpreter) {
1696     return;
1697   }
1698 
1699   assert_different_registers(mdp, callee, tmp);
1700 
1701   if (MethodData::profile_arguments() || MethodData::profile_return()) {
1702     Label profile_continue;
1703 
1704     test_method_data_pointer(mdp, profile_continue);
1705 
1706     int off_to_start = is_virtual ? in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
1707 
1708     z_cliy(in_bytes(DataLayout::tag_offset()) - off_to_start, mdp,
1709            is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag);
1710     z_brne(profile_continue);
1711 
1712     if (MethodData::profile_arguments()) {
1713       NearLabel done;
1714       int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset());
1715       add2reg(mdp, off_to_args);
1716 
1717       for (int i = 0; i < TypeProfileArgsLimit; i++) {
1718         if (i > 0 || MethodData::profile_return()) {
1719           // If return value type is profiled we may have no argument to profile.
1720           z_lg(tmp, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args, mdp);
1721           add2reg(tmp, -i*TypeStackSlotEntries::per_arg_count());
1722           compare64_and_branch(tmp, TypeStackSlotEntries::per_arg_count(), Assembler::bcondLow, done);
1723         }
1724         z_lg(tmp, Address(callee, Method::const_offset()));
1725         z_lgh(tmp, Address(tmp, ConstMethod::size_of_parameters_offset()));
1726         // Stack offset o (zero based) from the start of the argument
1727         // list. For n arguments translates into offset n - o - 1 from
1728         // the end of the argument list. But there is an extra slot at
1729         // the top of the stack. So the offset is n - o from Lesp.
1730         z_sg(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))-off_to_args));
1731         z_sllg(tmp, tmp, Interpreter::logStackElementSize);
1732         Address stack_slot_addr(tmp, Z_esp);
1733         z_ltg(tmp, stack_slot_addr);
1734 
1735         Address mdo_arg_addr(mdp, in_bytes(TypeEntriesAtCall::argument_type_offset(i))-off_to_args);
1736         profile_obj_type(tmp, mdo_arg_addr, tmp, /*ltg did compare to 0*/ true);
1737 
1738         int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
1739         add2reg(mdp, to_add);
1740         off_to_args += to_add;
1741       }
1742 
1743       if (MethodData::profile_return()) {
1744         z_lg(tmp, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args, mdp);
1745         add2reg(tmp, -TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count());
1746       }
1747 
1748       bind(done);
1749 
1750       if (MethodData::profile_return()) {
1751         // We're right after the type profile for the last
1752         // argument. Tmp is the number of cells left in the
1753         // CallTypeData/VirtualCallTypeData to reach its end. Non null
1754         // if there's a return to profile.
1755         assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(), "can't move past ret type");
1756         z_sllg(tmp, tmp, exact_log2(DataLayout::cell_size));
1757         z_agr(mdp, tmp);
1758       }
1759       z_stg(mdp, _z_ijava_state_neg(mdx), Z_fp);
1760     } else {
1761       assert(MethodData::profile_return(), "either profile call args or call ret");
1762       update_mdp_by_constant(mdp, in_bytes(TypeEntriesAtCall::return_only_size()));
1763     }
1764 
1765     // Mdp points right after the end of the
1766     // CallTypeData/VirtualCallTypeData, right after the cells for the
1767     // return value type if there's one.
1768     bind(profile_continue);
1769   }
1770 }
1771 
1772 void InterpreterMacroAssembler::profile_return_type(Register mdp, Register ret, Register tmp) {
1773   assert_different_registers(mdp, ret, tmp);
1774   if (ProfileInterpreter && MethodData::profile_return()) {
1775     Label profile_continue;
1776 
1777     test_method_data_pointer(mdp, profile_continue);
1778 
1779     if (MethodData::profile_return_jsr292_only()) {
1780       // If we don't profile all invoke bytecodes we must make sure
1781       // it's a bytecode we indeed profile. We can't go back to the
1782       // beginning of the ProfileData we intend to update to check its
1783       // type because we're right after it and we don't known its
1784       // length.
1785       NearLabel do_profile;
1786       Address bc(Z_bcp);
1787       z_lb(tmp, bc);
1788       compare32_and_branch(tmp, Bytecodes::_invokedynamic, Assembler::bcondEqual, do_profile);
1789       compare32_and_branch(tmp, Bytecodes::_invokehandle, Assembler::bcondEqual, do_profile);
1790       get_method(tmp);
1791       // Supplement to 8139891: _intrinsic_id exceeded 1-byte size limit.
1792       if (Method::intrinsic_id_size_in_bytes() == 1) {
1793         z_cli(Method::intrinsic_id_offset_in_bytes(), tmp, vmIntrinsics::_compiledLambdaForm);
1794       } else {
1795         assert(Method::intrinsic_id_size_in_bytes() == 2, "size error: check Method::_intrinsic_id");
1796         z_lh(tmp, Method::intrinsic_id_offset_in_bytes(), Z_R0, tmp);
1797         z_chi(tmp, vmIntrinsics::_compiledLambdaForm);
1798       }
1799       z_brne(profile_continue);
1800 
1801       bind(do_profile);
1802     }
1803 
1804     Address mdo_ret_addr(mdp, -in_bytes(ReturnTypeEntry::size()));
1805     profile_obj_type(ret, mdo_ret_addr, tmp);
1806 
1807     bind(profile_continue);
1808   }
1809 }
1810 
1811 void InterpreterMacroAssembler::profile_parameters_type(Register mdp, Register tmp1, Register tmp2) {
1812   if (ProfileInterpreter && MethodData::profile_parameters()) {
1813     Label profile_continue, done;
1814 
1815     test_method_data_pointer(mdp, profile_continue);
1816 
1817     // Load the offset of the area within the MDO used for
1818     // parameters. If it's negative we're not profiling any parameters.
1819     Address parm_di_addr(mdp, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset()));
1820     load_and_test_int2long(tmp1, parm_di_addr);
1821     z_brl(profile_continue);
1822 
1823     // Compute a pointer to the area for parameters from the offset
1824     // and move the pointer to the slot for the last
1825     // parameters. Collect profiling from last parameter down.
1826     // mdo start + parameters offset + array length - 1
1827 
1828     // Pointer to the parameter area in the MDO.
1829     z_agr(mdp, tmp1);
1830 
1831     // Offset of the current profile entry to update.
1832     const Register entry_offset = tmp1;
1833     // entry_offset = array len in number of cells.
1834     z_lg(entry_offset, Address(mdp, ArrayData::array_len_offset()));
1835     // entry_offset (number of cells) = array len - size of 1 entry
1836     add2reg(entry_offset, -TypeStackSlotEntries::per_arg_count());
1837     // entry_offset in bytes
1838     z_sllg(entry_offset, entry_offset, exact_log2(DataLayout::cell_size));
1839 
1840     Label loop;
1841     bind(loop);
1842 
1843     Address arg_off(mdp, entry_offset, ParametersTypeData::stack_slot_offset(0));
1844     Address arg_type(mdp, entry_offset, ParametersTypeData::type_offset(0));
1845 
1846     // Load offset on the stack from the slot for this parameter.
1847     z_lg(tmp2, arg_off);
1848     z_sllg(tmp2, tmp2, Interpreter::logStackElementSize);
1849     z_lcgr(tmp2); // Negate.
1850 
1851     // Profile the parameter.
1852     z_ltg(tmp2, Address(Z_locals, tmp2));
1853     profile_obj_type(tmp2, arg_type, tmp2, /*ltg did compare to 0*/ true);
1854 
1855     // Go to next parameter.
1856     z_aghi(entry_offset, -TypeStackSlotEntries::per_arg_count() * DataLayout::cell_size);
1857     z_brnl(loop);
1858 
1859     bind(profile_continue);
1860   }
1861 }
1862 
1863 // Jump if ((*counter_addr += increment) & mask) satisfies the condition.
1864 void InterpreterMacroAssembler::increment_mask_and_jump(Address          counter_addr,
1865                                                         int              increment,
1866                                                         Address          mask,
1867                                                         Register         scratch,
1868                                                         bool             preloaded,
1869                                                         branch_condition cond,
1870                                                         Label           *where) {
1871   assert_different_registers(counter_addr.base(), scratch);
1872   if (preloaded) {
1873     add2reg(scratch, increment);
1874     reg2mem_opt(scratch, counter_addr, false);
1875   } else {
1876     if (VM_Version::has_MemWithImmALUOps() && Immediate::is_simm8(increment) && counter_addr.is_RSYform()) {
1877       z_alsi(counter_addr.disp20(), counter_addr.base(), increment);
1878       mem2reg_signed_opt(scratch, counter_addr);
1879     } else {
1880       mem2reg_signed_opt(scratch, counter_addr);
1881       add2reg(scratch, increment);
1882       reg2mem_opt(scratch, counter_addr, false);
1883     }
1884   }
1885   z_n(scratch, mask);
1886   if (where) { z_brc(cond, *where); }
1887 }
1888 
1889 // Get MethodCounters object for given method. Lazily allocated if necessary.
1890 //   method    - Ptr to Method object.
1891 //   Rcounters - Ptr to MethodCounters object associated with Method object.
1892 //   skip      - Exit point if MethodCounters object can't be created (OOM condition).
1893 void InterpreterMacroAssembler::get_method_counters(Register Rmethod,
1894                                                     Register Rcounters,
1895                                                     Label& skip) {
1896   assert_different_registers(Rmethod, Rcounters);
1897 
1898   BLOCK_COMMENT("get MethodCounters object {");
1899 
1900   Label has_counters;
1901   load_and_test_long(Rcounters, Address(Rmethod, Method::method_counters_offset()));
1902   z_brnz(has_counters);
1903 
1904   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::build_method_counters), Rmethod, false);
1905   z_ltgr(Rcounters, Z_RET); // Runtime call returns MethodCounters object.
1906   z_brz(skip); // No MethodCounters, out of memory.
1907 
1908   bind(has_counters);
1909 
1910   BLOCK_COMMENT("} get MethodCounters object");
1911 }
1912 
1913 // Increment invocation counter in MethodCounters object.
1914 // Return (invocation_counter+backedge_counter) as "result" in RctrSum.
1915 // Counter values are all unsigned.
1916 void InterpreterMacroAssembler::increment_invocation_counter(Register Rcounters, Register RctrSum) {
1917   assert(UseCompiler || LogTouchedMethods, "incrementing must be useful");
1918   assert_different_registers(Rcounters, RctrSum);
1919 
1920   int increment          = InvocationCounter::count_increment;
1921   int inv_counter_offset = in_bytes(MethodCounters::invocation_counter_offset() + InvocationCounter::counter_offset());
1922   int be_counter_offset  = in_bytes(MethodCounters::backedge_counter_offset()   + InvocationCounter::counter_offset());
1923 
1924   BLOCK_COMMENT("Increment invocation counter {");
1925 
1926   if (VM_Version::has_MemWithImmALUOps() && Immediate::is_simm8(increment)) {
1927     // Increment the invocation counter in place,
1928     // then add the incremented value to the backedge counter.
1929     z_l(RctrSum, be_counter_offset, Rcounters);
1930     z_alsi(inv_counter_offset, Rcounters, increment);     // Atomic increment @no extra cost!
1931     z_nilf(RctrSum, InvocationCounter::count_mask_value); // Mask off state bits.
1932     z_al(RctrSum, inv_counter_offset, Z_R0, Rcounters);
1933   } else {
1934     // This path is optimized for low register consumption
1935     // at the cost of somewhat higher operand delays.
1936     // It does not need an extra temp register.
1937 
1938     // Update the invocation counter.
1939     z_l(RctrSum, inv_counter_offset, Rcounters);
1940     if (RctrSum == Z_R0) {
1941       z_ahi(RctrSum, increment);
1942     } else {
1943       add2reg(RctrSum, increment);
1944     }
1945     z_st(RctrSum, inv_counter_offset, Rcounters);
1946 
1947     // Mask off the state bits.
1948     z_nilf(RctrSum, InvocationCounter::count_mask_value);
1949 
1950     // Add the backedge counter to the updated invocation counter to
1951     // form the result.
1952     z_al(RctrSum, be_counter_offset, Z_R0, Rcounters);
1953   }
1954 
1955   BLOCK_COMMENT("} Increment invocation counter");
1956 
1957   // Note that this macro must leave the backedge_count + invocation_count in Rtmp!
1958 }
1959 
1960 
1961 // increment backedge counter in MethodCounters object.
1962 // return (invocation_counter+backedge_counter) as "result" in RctrSum
1963 // counter values are all unsigned!
1964 void InterpreterMacroAssembler::increment_backedge_counter(Register Rcounters, Register RctrSum) {
1965   assert(UseCompiler, "incrementing must be useful");
1966   assert_different_registers(Rcounters, RctrSum);
1967 
1968   int increment          = InvocationCounter::count_increment;
1969   int inv_counter_offset = in_bytes(MethodCounters::invocation_counter_offset() + InvocationCounter::counter_offset());
1970   int be_counter_offset  = in_bytes(MethodCounters::backedge_counter_offset()   + InvocationCounter::counter_offset());
1971 
1972   BLOCK_COMMENT("Increment backedge counter {");
1973 
1974   if (VM_Version::has_MemWithImmALUOps() && Immediate::is_simm8(increment)) {
1975     // Increment the invocation counter in place,
1976     // then add the incremented value to the backedge counter.
1977     z_l(RctrSum, inv_counter_offset, Rcounters);
1978     z_alsi(be_counter_offset, Rcounters, increment);      // Atomic increment @no extra cost!
1979     z_nilf(RctrSum, InvocationCounter::count_mask_value); // Mask off state bits.
1980     z_al(RctrSum, be_counter_offset, Z_R0, Rcounters);
1981   } else {
1982     // This path is optimized for low register consumption
1983     // at the cost of somewhat higher operand delays.
1984     // It does not need an extra temp register.
1985 
1986     // Update the invocation counter.
1987     z_l(RctrSum, be_counter_offset, Rcounters);
1988     if (RctrSum == Z_R0) {
1989       z_ahi(RctrSum, increment);
1990     } else {
1991       add2reg(RctrSum, increment);
1992     }
1993     z_st(RctrSum, be_counter_offset, Rcounters);
1994 
1995     // Mask off the state bits.
1996     z_nilf(RctrSum, InvocationCounter::count_mask_value);
1997 
1998     // Add the backedge counter to the updated invocation counter to
1999     // form the result.
2000     z_al(RctrSum, inv_counter_offset, Z_R0, Rcounters);
2001   }
2002 
2003   BLOCK_COMMENT("} Increment backedge counter");
2004 
2005   // Note that this macro must leave the backedge_count + invocation_count in Rtmp!
2006 }
2007 
2008 // Add an InterpMonitorElem to stack (see frame_s390.hpp).
2009 void InterpreterMacroAssembler::add_monitor_to_stack(bool     stack_is_empty,
2010                                                      Register Rtemp1,
2011                                                      Register Rtemp2,
2012                                                      Register Rtemp3) {
2013 
2014   const Register Rcurr_slot = Rtemp1;
2015   const Register Rlimit     = Rtemp2;
2016   const jint delta = -frame::interpreter_frame_monitor_size() * wordSize;
2017 
2018   assert((delta & LongAlignmentMask) == 0,
2019          "sizeof BasicObjectLock must be even number of doublewords");
2020   assert(2 * wordSize == -delta, "this works only as long as delta == -2*wordSize");
2021   assert(Rcurr_slot != Z_R0, "Register must be usable as base register");
2022   assert_different_registers(Rlimit, Rcurr_slot, Rtemp3);
2023 
2024   get_monitors(Rlimit);
2025 
2026   // Adjust stack pointer for additional monitor entry.
2027   resize_frame(RegisterOrConstant((intptr_t) delta), Z_fp, false);
2028 
2029   if (!stack_is_empty) {
2030     // Must copy stack contents down.
2031     NearLabel next, done;
2032 
2033     // Rtemp := addr(Tos), Z_esp is pointing below it!
2034     add2reg(Rcurr_slot, wordSize, Z_esp);
2035 
2036     // Nothing to do, if already at monitor area.
2037     compareU64_and_branch(Rcurr_slot, Rlimit, bcondNotLow, done);
2038 
2039     bind(next);
2040 
2041     // Move one stack slot.
2042     mem2reg_opt(Rtemp3, Address(Rcurr_slot));
2043     reg2mem_opt(Rtemp3, Address(Rcurr_slot, delta));
2044     add2reg(Rcurr_slot, wordSize);
2045     compareU64_and_branch(Rcurr_slot, Rlimit, bcondLow, next); // Are we done?
2046 
2047     bind(done);
2048     // Done copying stack.
2049   }
2050 
2051   // Adjust expression stack and monitor pointers.
2052   add2reg(Z_esp, delta);
2053   add2reg(Rlimit, delta);
2054   save_monitors(Rlimit);
2055 }
2056 
2057 // Note: Index holds the offset in bytes afterwards.
2058 // You can use this to store a new value (with Llocals as the base).
2059 void InterpreterMacroAssembler::access_local_int(Register index, Register dst) {
2060   z_sllg(index, index, LogBytesPerWord);
2061   mem2reg_opt(dst, Address(Z_locals, index), false);
2062 }
2063 
2064 void InterpreterMacroAssembler::verify_oop(Register reg, TosState state) {
2065   if (state == atos) { MacroAssembler::verify_oop(reg); }
2066 }
2067 
2068 // Inline assembly for:
2069 //
2070 // if (thread is in interp_only_mode) {
2071 //   InterpreterRuntime::post_method_entry();
2072 // }
2073 
2074 void InterpreterMacroAssembler::notify_method_entry() {
2075 
2076   // JVMTI
2077   // Whenever JVMTI puts a thread in interp_only_mode, method
2078   // entry/exit events are sent for that thread to track stack
2079   // depth. If it is possible to enter interp_only_mode we add
2080   // the code to check if the event should be sent.
2081   if (JvmtiExport::can_post_interpreter_events()) {
2082     Label jvmti_post_done;
2083     MacroAssembler::load_and_test_int(Z_R0, Address(Z_thread, JavaThread::interp_only_mode_offset()));
2084     z_bre(jvmti_post_done);
2085     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_entry), /*check_exceptions=*/false);
2086     bind(jvmti_post_done);
2087   }
2088 }
2089 
2090 // Inline assembly for:
2091 //
2092 // if (thread is in interp_only_mode) {
2093 //   if (!native_method) save result
2094 //   InterpreterRuntime::post_method_exit();
2095 //   if (!native_method) restore result
2096 // }
2097 // if (DTraceMethodProbes) {
2098 //   SharedRuntime::dtrace_method_exit(thread, method);
2099 // }
2100 //
2101 // For native methods their result is stored in z_ijava_state.lresult
2102 // and z_ijava_state.fresult before coming here.
2103 // Java methods have their result stored in the expression stack.
2104 //
2105 // Notice the dependency to frame::interpreter_frame_result().
2106 void InterpreterMacroAssembler::notify_method_exit(bool native_method,
2107                                                    TosState state,
2108                                                    NotifyMethodExitMode mode) {
2109   // JVMTI
2110   // Whenever JVMTI puts a thread in interp_only_mode, method
2111   // entry/exit events are sent for that thread to track stack
2112   // depth. If it is possible to enter interp_only_mode we add
2113   // the code to check if the event should be sent.
2114   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
2115     Label jvmti_post_done;
2116     MacroAssembler::load_and_test_int(Z_R0, Address(Z_thread, JavaThread::interp_only_mode_offset()));
2117     z_bre(jvmti_post_done);
2118     if (!native_method) push(state); // see frame::interpreter_frame_result()
2119     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit), /*check_exceptions=*/false);
2120     if (!native_method) pop(state);
2121     bind(jvmti_post_done);
2122   }
2123 
2124 #if 0
2125   // Dtrace currently not supported on z/Architecture.
2126   {
2127     SkipIfEqual skip(this, &DTraceMethodProbes, false);
2128     push(state);
2129     get_method(c_rarg1);
2130     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
2131                  r15_thread, c_rarg1);
2132     pop(state);
2133   }
2134 #endif
2135 }
2136 
2137 void InterpreterMacroAssembler::skip_if_jvmti_mode(Label &Lskip, Register Rscratch) {
2138   if (!JvmtiExport::can_post_interpreter_events()) {
2139     return;
2140   }
2141 
2142   load_and_test_int(Rscratch, Address(Z_thread, JavaThread::interp_only_mode_offset()));
2143   z_brnz(Lskip);
2144 
2145 }
2146 
2147 // Pop the topmost TOP_IJAVA_FRAME and set it's sender_sp as new Z_SP.
2148 // The return pc is loaded into the register return_pc.
2149 //
2150 // Registers updated:
2151 //     return_pc  - The return pc of the calling frame.
2152 //     tmp1, tmp2 - scratch
2153 void InterpreterMacroAssembler::pop_interpreter_frame(Register return_pc, Register tmp1, Register tmp2) {
2154   // F0  Z_SP -> caller_sp (F1's)
2155   //             ...
2156   //             sender_sp (F1's)
2157   //             ...
2158   // F1  Z_fp -> caller_sp (F2's)
2159   //             return_pc (Continuation after return from F0.)
2160   //             ...
2161   // F2          caller_sp
2162 
2163   // Remove F0's activation. Restoring Z_SP to sender_sp reverts modifications
2164   // (a) by a c2i adapter and (b) by generate_fixed_frame().
2165   // In case (a) the new top frame F1 is an unextended compiled frame.
2166   // In case (b) F1 is converted from PARENT_IJAVA_FRAME to TOP_IJAVA_FRAME.
2167 
2168   // Case (b) seems to be redundant when returning to a interpreted caller,
2169   // because then the caller's top_frame_sp is installed as sp (see
2170   // TemplateInterpreterGenerator::generate_return_entry_for ()). But
2171   // pop_interpreter_frame() is also used in exception handling and there the
2172   // frame type of the caller is unknown, therefore top_frame_sp cannot be used,
2173   // so it is important that sender_sp is the caller's sp as TOP_IJAVA_FRAME.
2174 
2175   Register R_f1_sender_sp = tmp1;
2176   Register R_f2_sp = tmp2;
2177 
2178   // Tirst check the for the interpreter frame's magic.
2179   asm_assert_ijava_state_magic(R_f2_sp/*tmp*/);
2180   z_lg(R_f2_sp, _z_parent_ijava_frame_abi(callers_sp), Z_fp);
2181   z_lg(R_f1_sender_sp, _z_ijava_state_neg(sender_sp), Z_fp);
2182   if (return_pc->is_valid())
2183     z_lg(return_pc, _z_parent_ijava_frame_abi(return_pc), Z_fp);
2184   // Pop F0 by resizing to R_f1_sender_sp and using R_f2_sp as fp.
2185   resize_frame_absolute(R_f1_sender_sp, R_f2_sp, false/*load fp*/);
2186 
2187 #ifdef ASSERT
2188   // The return_pc in the new top frame is dead... at least that's my
2189   // current understanding; to assert this I overwrite it.
2190   load_const_optimized(Z_ARG3, 0xb00b1);
2191   z_stg(Z_ARG3, _z_parent_ijava_frame_abi(return_pc), Z_SP);
2192 #endif
2193 }
2194 
2195 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) {
2196   if (VerifyFPU) {
2197     unimplemented("verfiyFPU");
2198   }
2199 }
2200