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