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