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