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