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