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