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