1 /*
   2  * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2016, 2023 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "compiler/disassembler.hpp"
  30 #include "gc/shared/barrierSetAssembler.hpp"
  31 #include "interpreter/abstractInterpreter.hpp"
  32 #include "interpreter/bytecodeHistogram.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "interpreter/interpreterRuntime.hpp"
  35 #include "interpreter/interp_masm.hpp"
  36 #include "interpreter/templateInterpreterGenerator.hpp"
  37 #include "interpreter/templateTable.hpp"
  38 #include "oops/arrayOop.hpp"
  39 #include "oops/methodCounters.hpp"
  40 #include "oops/methodData.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "oops/resolvedIndyEntry.hpp"
  43 #include "oops/resolvedMethodEntry.hpp"
  44 #include "prims/jvmtiExport.hpp"
  45 #include "prims/jvmtiThreadState.hpp"
  46 #include "runtime/arguments.hpp"
  47 #include "runtime/deoptimization.hpp"
  48 #include "runtime/frame.inline.hpp"
  49 #include "runtime/jniHandles.hpp"
  50 #include "runtime/sharedRuntime.hpp"
  51 #include "runtime/stubRoutines.hpp"
  52 #include "runtime/synchronizer.hpp"
  53 #include "runtime/timer.hpp"
  54 #include "runtime/vframeArray.hpp"
  55 #include "utilities/debug.hpp"
  56 #include "utilities/macros.hpp"
  57 
  58 // Size of interpreter code.  Increase if too small.  Interpreter will
  59 // fail with a guarantee ("not enough space for interpreter generation");
  60 // if too small.
  61 // Run with +PrintInterpreter to get the VM to print out the size.
  62 // Max size with JVMTI
  63 int TemplateInterpreter::InterpreterCodeSize = 320*K;
  64 
  65 #undef  __
  66 #ifdef PRODUCT
  67   #define __ Disassembler::hook<InterpreterMacroAssembler>(__FILE__, __LINE__, _masm)->
  68 #else
  69   #define __ Disassembler::hook<InterpreterMacroAssembler>(__FILE__, __LINE__, _masm)->
  70 //  #define __ (Verbose ? (_masm->block_comment(FILE_AND_LINE),_masm):_masm)->
  71 #endif
  72 
  73 #define BLOCK_COMMENT(str) __ block_comment(str)
  74 #define BIND(label)        __ bind(label); BLOCK_COMMENT(#label ":")
  75 
  76 #define oop_tmp_offset     _z_ijava_state_neg(oop_tmp)
  77 
  78 //-----------------------------------------------------------------------------
  79 
  80 address TemplateInterpreterGenerator::generate_slow_signature_handler() {
  81   //
  82   // New slow_signature handler that respects the z/Architecture
  83   // C calling conventions.
  84   //
  85   // We get called by the native entry code with our output register
  86   // area == 8. First we call InterpreterRuntime::get_result_handler
  87   // to copy the pointer to the signature string temporarily to the
  88   // first C-argument and to return the result_handler in
  89   // Z_RET. Since native_entry will copy the jni-pointer to the
  90   // first C-argument slot later on, it's OK to occupy this slot
  91   // temporarily. Then we copy the argument list on the java
  92   // expression stack into native varargs format on the native stack
  93   // and load arguments into argument registers. Integer arguments in
  94   // the varargs vector will be sign-extended to 8 bytes.
  95   //
  96   // On entry:
  97   //   Z_ARG1  - intptr_t*       Address of java argument list in memory.
  98   //   Z_state - zeroInterpreter* Address of interpreter state for
  99   //                              this method
 100   //   Z_method
 101   //
 102   // On exit (just before return instruction):
 103   //   Z_RET contains the address of the result_handler.
 104   //   Z_ARG2 is not updated for static methods and contains "this" otherwise.
 105   //   Z_ARG3-Z_ARG5 contain the first 3 arguments of types other than float and double.
 106   //   Z_FARG1-Z_FARG4 contain the first 4 arguments of type float or double.
 107 
 108   const int LogSizeOfCase = 3;
 109 
 110   const int max_fp_register_arguments   = Argument::n_float_register_parameters;
 111   const int max_int_register_arguments  = Argument::n_register_parameters - 2;  // First 2 are reserved.
 112 
 113   const Register arg_java       = Z_tmp_2;
 114   const Register arg_c          = Z_tmp_3;
 115   const Register signature      = Z_R1_scratch; // Is a string.
 116   const Register fpcnt          = Z_R0_scratch;
 117   const Register argcnt         = Z_tmp_4;
 118   const Register intSlot        = Z_tmp_1;
 119   const Register sig_end        = Z_tmp_1; // Assumed end of signature (only used in do_object).
 120   const Register target_sp      = Z_tmp_1;
 121   const FloatRegister floatSlot = Z_F1;
 122 
 123   const int d_signature         = _z_abi(gpr6); // Only spill space, register contents not affected.
 124   const int d_fpcnt             = _z_abi(gpr7); // Only spill space, register contents not affected.
 125 
 126   unsigned int entry_offset = __ offset();
 127 
 128   BLOCK_COMMENT("slow_signature_handler {");
 129 
 130   // We use target_sp for storing arguments in the C frame.
 131   __ save_return_pc();
 132   __ push_frame_abi160(4*BytesPerWord);                 // Reserve space to save the tmp_[1..4] registers.
 133   __ z_stmg(Z_R10, Z_R13, frame::z_abi_160_size, Z_SP); // Save registers only after frame is pushed.
 134 
 135   __ z_lgr(arg_java, Z_ARG1);
 136 
 137   Register   method = Z_ARG2; // Directly load into correct argument register.
 138 
 139   __ get_method(method);
 140   __ call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::get_signature), Z_thread, method);
 141 
 142   // Move signature to callee saved register.
 143   // Don't directly write to stack. Frame is used by VM call.
 144   __ z_lgr(Z_tmp_1, Z_RET);
 145 
 146   // Reload method. Register may have been altered by VM call.
 147   __ get_method(method);
 148 
 149   // Get address of result handler.
 150   __ call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::get_result_handler), Z_thread, method);
 151 
 152   // Save signature address to stack.
 153   __ z_stg(Z_tmp_1, d_signature, Z_SP);
 154 
 155   // Don't overwrite return value (Z_RET, Z_ARG1) in rest of the method !
 156 
 157   {
 158     Label   isStatic;
 159 
 160     // Test if static.
 161     // We can test the bit directly.
 162     // Path is Z_method->_access_flags._flags.
 163     // We only support flag bits in the least significant byte (assert !).
 164     // Therefore add 3 to address that byte within "_flags".
 165     // Reload method. VM call above may have destroyed register contents
 166     __ get_method(method);
 167     __ testbit(method2_(method, access_flags), JVM_ACC_STATIC_BIT);
 168     method = noreg;  // end of life
 169     __ z_btrue(isStatic);
 170 
 171     // For non-static functions, pass "this" in Z_ARG2 and copy it to 2nd C-arg slot.
 172     // Need to box the Java object here, so we use arg_java
 173     // (address of current Java stack slot) as argument and
 174     // don't dereference it as in case of ints, floats, etc..
 175     __ z_lgr(Z_ARG2, arg_java);
 176     __ add2reg(arg_java, -BytesPerWord);
 177     __ bind(isStatic);
 178   }
 179 
 180   // argcnt == 0 corresponds to 3rd C argument.
 181   //   arg #1 (result handler) and
 182   //   arg #2 (this, for non-statics), unused else
 183   // are reserved and pre-filled above.
 184   // arg_java points to the corresponding Java argument here. It
 185   // has been decremented by one argument (this) in case of non-static.
 186   __ clear_reg(argcnt, true, false);  // Don't set CC.
 187   __ z_lg(target_sp, 0, Z_SP);
 188   __ add2reg(arg_c, _z_abi(remaining_cargs), target_sp);
 189   // No floating-point args parsed so far.
 190   __ clear_mem(Address(Z_SP, d_fpcnt), 8);
 191 
 192   NearLabel   move_intSlot_to_ARG, move_floatSlot_to_FARG;
 193   NearLabel   loop_start, loop_start_restore, loop_end;
 194   NearLabel   do_int, do_long, do_float, do_double;
 195   NearLabel   do_dontreachhere, do_object, do_array, do_boxed;
 196 
 197 #ifdef ASSERT
 198   // Signature needs to point to '(' (== 0x28) at entry.
 199   __ z_lg(signature, d_signature, Z_SP);
 200   __ z_cli(0, signature, (int) '(');
 201   __ z_brne(do_dontreachhere);
 202 #endif
 203 
 204   __ bind(loop_start_restore);
 205   __ z_lg(signature, d_signature, Z_SP);  // Restore signature ptr, destroyed by move_XX_to_ARG.
 206 
 207   BIND(loop_start);
 208   // Advance to next argument type token from the signature.
 209   __ add2reg(signature, 1);
 210 
 211   // Use CLI, works well on all CPU versions.
 212     __ z_cli(0, signature, (int) ')');
 213     __ z_bre(loop_end);                // end of signature
 214     __ z_cli(0, signature, (int) 'L');
 215     __ z_bre(do_object);               // object     #9
 216     __ z_cli(0, signature, (int) 'F');
 217     __ z_bre(do_float);                // float      #7
 218     __ z_cli(0, signature, (int) 'J');
 219     __ z_bre(do_long);                 // long       #6
 220     __ z_cli(0, signature, (int) 'B');
 221     __ z_bre(do_int);                  // byte       #1
 222     __ z_cli(0, signature, (int) 'Z');
 223     __ z_bre(do_int);                  // boolean    #2
 224     __ z_cli(0, signature, (int) 'C');
 225     __ z_bre(do_int);                  // char       #3
 226     __ z_cli(0, signature, (int) 'S');
 227     __ z_bre(do_int);                  // short      #4
 228     __ z_cli(0, signature, (int) 'I');
 229     __ z_bre(do_int);                  // int        #5
 230     __ z_cli(0, signature, (int) 'D');
 231     __ z_bre(do_double);               // double     #8
 232     __ z_cli(0, signature, (int) '[');
 233     __ z_bre(do_array);                // array      #10
 234 
 235   __ bind(do_dontreachhere);
 236 
 237   __ unimplemented("ShouldNotReachHere in slow_signature_handler", 120);
 238 
 239   // Array argument
 240   BIND(do_array);
 241 
 242   {
 243     Label   start_skip, end_skip;
 244 
 245     __ bind(start_skip);
 246 
 247     // Advance to next type tag from signature.
 248     __ add2reg(signature, 1);
 249 
 250     // Use CLI, works well on all CPU versions.
 251     __ z_cli(0, signature, (int) '[');
 252     __ z_bre(start_skip);               // Skip further brackets.
 253 
 254     __ z_cli(0, signature, (int) '9');
 255     __ z_brh(end_skip);                 // no optional size
 256 
 257     __ z_cli(0, signature, (int) '0');
 258     __ z_brnl(start_skip);              // Skip optional size.
 259 
 260     __ bind(end_skip);
 261 
 262     __ z_cli(0, signature, (int) 'L');
 263     __ z_brne(do_boxed);                // If not array of objects: go directly to do_boxed.
 264   }
 265 
 266   //  OOP argument
 267   BIND(do_object);
 268   // Pass by an object's type name.
 269   {
 270     Label   L;
 271 
 272     __ add2reg(sig_end, 4095, signature);     // Assume object type name is shorter than 4k.
 273     __ load_const_optimized(Z_R0, (int) ';'); // Type name terminator (must be in Z_R0!).
 274     __ MacroAssembler::search_string(sig_end, signature);
 275     __ z_brl(L);
 276     __ z_illtrap();  // No semicolon found: internal error or object name too long.
 277     __ bind(L);
 278     __ z_lgr(signature, sig_end);
 279     // fallthru to do_boxed
 280   }
 281 
 282   // Need to box the Java object here, so we use arg_java
 283   // (address of current Java stack slot) as argument and
 284   // don't dereference it as in case of ints, floats, etc..
 285 
 286   // UNBOX argument
 287   // Load reference and check for null.
 288   Label  do_int_Entry4Boxed;
 289   __ bind(do_boxed);
 290   {
 291     __ load_and_test_long(intSlot, Address(arg_java));
 292     __ z_bre(do_int_Entry4Boxed);
 293     __ z_lgr(intSlot, arg_java);
 294     __ z_bru(do_int_Entry4Boxed);
 295   }
 296 
 297   // INT argument
 298 
 299   // (also for byte, boolean, char, short)
 300   // Use lgf for load (sign-extend) and stg for store.
 301   BIND(do_int);
 302   __ z_lgf(intSlot, 0, arg_java);
 303 
 304   __ bind(do_int_Entry4Boxed);
 305   __ add2reg(arg_java, -BytesPerWord);
 306   // If argument fits into argument register, go and handle it, otherwise continue.
 307   __ compare32_and_branch(argcnt, max_int_register_arguments,
 308                           Assembler::bcondLow, move_intSlot_to_ARG);
 309   __ z_stg(intSlot, 0, arg_c);
 310   __ add2reg(arg_c, BytesPerWord);
 311   __ z_bru(loop_start);
 312 
 313   // LONG argument
 314 
 315   BIND(do_long);
 316   __ add2reg(arg_java, -2*BytesPerWord);  // Decrement first to have positive displacement for lg.
 317   __ z_lg(intSlot, BytesPerWord, arg_java);
 318   // If argument fits into argument register, go and handle it, otherwise continue.
 319   __ compare32_and_branch(argcnt, max_int_register_arguments,
 320                           Assembler::bcondLow, move_intSlot_to_ARG);
 321   __ z_stg(intSlot, 0, arg_c);
 322   __ add2reg(arg_c, BytesPerWord);
 323   __ z_bru(loop_start);
 324 
 325   // FLOAT argumen
 326 
 327   BIND(do_float);
 328   __ z_le(floatSlot, 0, arg_java);
 329   __ add2reg(arg_java, -BytesPerWord);
 330   assert(max_fp_register_arguments <= 255, "always true");  // safety net
 331   __ z_cli(d_fpcnt+7, Z_SP, max_fp_register_arguments);
 332   __ z_brl(move_floatSlot_to_FARG);
 333   __ z_ste(floatSlot, 4, arg_c);
 334   __ add2reg(arg_c, BytesPerWord);
 335   __ z_bru(loop_start);
 336 
 337   // DOUBLE argument
 338 
 339   BIND(do_double);
 340   __ add2reg(arg_java, -2*BytesPerWord);  // Decrement first to have positive displacement for lg.
 341   __ z_ld(floatSlot, BytesPerWord, arg_java);
 342   assert(max_fp_register_arguments <= 255, "always true");  // safety net
 343   __ z_cli(d_fpcnt+7, Z_SP, max_fp_register_arguments);
 344   __ z_brl(move_floatSlot_to_FARG);
 345   __ z_std(floatSlot, 0, arg_c);
 346   __ add2reg(arg_c, BytesPerWord);
 347   __ z_bru(loop_start);
 348 
 349   // Method exit, all arguments processed.
 350   __ bind(loop_end);
 351   __ z_lmg(Z_R10, Z_R13, frame::z_abi_160_size, Z_SP); // restore registers before frame is popped.
 352   __ pop_frame();
 353   __ restore_return_pc();
 354   __ z_br(Z_R14);
 355 
 356   // Copy int arguments.
 357 
 358   Label  iarg_caselist;   // Distance between each case has to be a power of 2
 359                           // (= 1 << LogSizeOfCase).
 360   __ align(16);
 361   BIND(iarg_caselist);
 362   __ z_lgr(Z_ARG3, intSlot);    // 4 bytes
 363   __ z_bru(loop_start_restore); // 4 bytes
 364 
 365   __ z_lgr(Z_ARG4, intSlot);
 366   __ z_bru(loop_start_restore);
 367 
 368   __ z_lgr(Z_ARG5, intSlot);
 369   __ z_bru(loop_start_restore);
 370 
 371   __ align(16);
 372   __ bind(move_intSlot_to_ARG);
 373   __ z_stg(signature, d_signature, Z_SP);       // Spill since signature == Z_R1_scratch.
 374   __ z_larl(Z_R1_scratch, iarg_caselist);
 375   __ z_sllg(Z_R0_scratch, argcnt, LogSizeOfCase);
 376   __ add2reg(argcnt, 1);
 377   __ z_agr(Z_R1_scratch, Z_R0_scratch);
 378   __ z_bcr(Assembler::bcondAlways, Z_R1_scratch);
 379 
 380   // Copy float arguments.
 381 
 382   Label  farg_caselist;   // Distance between each case has to be a power of 2
 383                           // (= 1 << logSizeOfCase, padded with nop.
 384   __ align(16);
 385   BIND(farg_caselist);
 386   __ z_ldr(Z_FARG1, floatSlot); // 2 bytes
 387   __ z_bru(loop_start_restore); // 4 bytes
 388   __ z_nop();                   // 2 bytes
 389 
 390   __ z_ldr(Z_FARG2, floatSlot);
 391   __ z_bru(loop_start_restore);
 392   __ z_nop();
 393 
 394   __ z_ldr(Z_FARG3, floatSlot);
 395   __ z_bru(loop_start_restore);
 396   __ z_nop();
 397 
 398   __ z_ldr(Z_FARG4, floatSlot);
 399   __ z_bru(loop_start_restore);
 400   __ z_nop();
 401 
 402   __ align(16);
 403   __ bind(move_floatSlot_to_FARG);
 404   __ z_stg(signature, d_signature, Z_SP);        // Spill since signature == Z_R1_scratch.
 405   __ z_lg(Z_R0_scratch, d_fpcnt, Z_SP);          // Need old value for indexing.
 406   __ add2mem_64(Address(Z_SP, d_fpcnt), 1, Z_R1_scratch); // Increment index.
 407   __ z_larl(Z_R1_scratch, farg_caselist);
 408   __ z_sllg(Z_R0_scratch, Z_R0_scratch, LogSizeOfCase);
 409   __ z_agr(Z_R1_scratch, Z_R0_scratch);
 410   __ z_bcr(Assembler::bcondAlways, Z_R1_scratch);
 411 
 412   BLOCK_COMMENT("} slow_signature_handler");
 413 
 414   return __ addr_at(entry_offset);
 415 }
 416 
 417 address TemplateInterpreterGenerator::generate_result_handler_for (BasicType type) {
 418   address entry = __ pc();
 419 
 420   assert(Z_tos == Z_RET, "Result handler: must move result!");
 421   assert(Z_ftos == Z_FRET, "Result handler: must move float result!");
 422 
 423   switch (type) {
 424     case T_BOOLEAN:
 425       __ c2bool(Z_tos);
 426       break;
 427     case T_CHAR:
 428       __ and_imm(Z_tos, 0xffff);
 429       break;
 430     case T_BYTE:
 431       __ z_lbr(Z_tos, Z_tos);
 432       break;
 433     case T_SHORT:
 434       __ z_lhr(Z_tos, Z_tos);
 435       break;
 436     case T_INT:
 437     case T_LONG:
 438     case T_VOID:
 439     case T_FLOAT:
 440     case T_DOUBLE:
 441       break;
 442     case T_OBJECT:
 443       // Retrieve result from frame...
 444       __ mem2reg_opt(Z_tos, Address(Z_fp, oop_tmp_offset));
 445       // and verify it.
 446       __ verify_oop(Z_tos);
 447       break;
 448     default:
 449       ShouldNotReachHere();
 450   }
 451   __ z_br(Z_R14);      // Return from result handler.
 452   return entry;
 453 }
 454 
 455 // Abstract method entry.
 456 // Attempt to execute abstract method. Throw exception.
 457 address TemplateInterpreterGenerator::generate_abstract_entry(void) {
 458   unsigned int entry_offset = __ offset();
 459 
 460   // Caller could be the call_stub or a compiled method (x86 version is wrong!).
 461 
 462   BLOCK_COMMENT("abstract_entry {");
 463 
 464   // Implement call of InterpreterRuntime::throw_AbstractMethodError.
 465   __ set_top_ijava_frame_at_SP_as_last_Java_frame(Z_SP, Z_R1);
 466   __ save_return_pc();       // Save Z_R14.
 467   __ push_frame_abi160(0);   // Without new frame the RT call could overwrite the saved Z_R14.
 468 
 469   __ call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorWithMethod),
 470                   Z_thread, Z_method);
 471 
 472   __ pop_frame();
 473   __ restore_return_pc();    // Restore Z_R14.
 474   __ reset_last_Java_frame();
 475 
 476   // Restore caller sp for c2i case.
 477   __ resize_frame_absolute(Z_R10, Z_R0, true); // Cut the stack back to where the caller started.
 478 
 479   // branch to SharedRuntime::generate_forward_exception() which handles all possible callers,
 480   // i.e. call stub, compiled method, interpreted method.
 481   __ load_absolute_address(Z_tmp_1, StubRoutines::forward_exception_entry());
 482   __ z_br(Z_tmp_1);
 483 
 484   BLOCK_COMMENT("} abstract_entry");
 485 
 486   return __ addr_at(entry_offset);
 487 }
 488 
 489 address TemplateInterpreterGenerator::generate_Reference_get_entry(void) {
 490   // Inputs:
 491   //  Z_ARG1 - receiver
 492   //
 493   // What we do:
 494   //  - Load the referent field address.
 495   //  - Load the value in the referent field.
 496   //  - Pass that value to the pre-barrier.
 497   //
 498   // In the case of G1 this will record the value of the
 499   // referent in an SATB buffer if marking is active.
 500   // This will cause concurrent marking to mark the referent
 501   // field as live.
 502 
 503   Register  scratch1 = Z_tmp_2;
 504   Register  scratch2 = Z_tmp_3;
 505   Register  pre_val  = Z_RET;   // return value
 506   // Z_esp is callers operand stack pointer, i.e. it points to the parameters.
 507   Register  Rargp    = Z_esp;
 508 
 509   Label     slow_path;
 510   address   entry = __ pc();
 511 
 512   const int referent_offset = java_lang_ref_Reference::referent_offset();
 513 
 514   BLOCK_COMMENT("Reference_get {");
 515 
 516   //  If the receiver is null then it is OK to jump to the slow path.
 517   __ load_and_test_long(pre_val, Address(Rargp, Interpreter::stackElementSize)); // Get receiver.
 518   __ z_bre(slow_path);
 519 
 520   //  Load the value of the referent field.
 521   __ load_heap_oop(pre_val, Address(pre_val, referent_offset), scratch1, scratch2, ON_WEAK_OOP_REF);
 522 
 523   // Restore caller sp for c2i case.
 524   __ resize_frame_absolute(Z_R10, Z_R0, true); // Cut the stack back to where the caller started.
 525   __ z_br(Z_R14);
 526 
 527   // Branch to previously generated regular method entry.
 528   __ bind(slow_path);
 529 
 530   address meth_entry = Interpreter::entry_for_kind(Interpreter::zerolocals);
 531   __ jump_to_entry(meth_entry, Z_R1);
 532 
 533   BLOCK_COMMENT("} Reference_get");
 534 
 535   return entry;
 536 }
 537 
 538 address TemplateInterpreterGenerator::generate_StackOverflowError_handler() {
 539   address entry = __ pc();
 540 
 541   DEBUG_ONLY(__ verify_esp(Z_esp, Z_ARG5));
 542 
 543   // Restore bcp under the assumption that the current frame is still
 544   // interpreted.
 545   __ restore_bcp();
 546 
 547   // Expression stack must be empty before entering the VM if an
 548   // exception happened.
 549   __ empty_expression_stack();
 550   // Throw exception.
 551   __ call_VM(noreg,
 552              CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_StackOverflowError));
 553   return entry;
 554 }
 555 
 556 //
 557 // Args:
 558 //   Z_ARG2: oop of array
 559 //   Z_ARG3: aberrant index
 560 //
 561 address TemplateInterpreterGenerator::generate_ArrayIndexOutOfBounds_handler() {
 562   address entry = __ pc();
 563   address excp = CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException);
 564 
 565   // Expression stack must be empty before entering the VM if an
 566   // exception happened.
 567   __ empty_expression_stack();
 568 
 569   // Setup parameters.
 570   // Pass register with array to create more detailed exceptions.
 571   __ call_VM(noreg, excp, Z_ARG2, Z_ARG3);
 572   return entry;
 573 }
 574 
 575 address TemplateInterpreterGenerator::generate_ClassCastException_handler() {
 576   address entry = __ pc();
 577 
 578   // Object is at TOS.
 579   __ pop_ptr(Z_ARG2);
 580 
 581   // Expression stack must be empty before entering the VM if an
 582   // exception happened.
 583   __ empty_expression_stack();
 584 
 585   __ call_VM(Z_ARG1,
 586              CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_ClassCastException),
 587              Z_ARG2);
 588 
 589   DEBUG_ONLY(__ should_not_reach_here();)
 590 
 591   return entry;
 592 }
 593 
 594 address TemplateInterpreterGenerator::generate_exception_handler_common(const char* name, const char* message, bool pass_oop) {
 595   assert(!pass_oop || message == nullptr, "either oop or message but not both");
 596   address entry = __ pc();
 597 
 598   BLOCK_COMMENT("exception_handler_common {");
 599 
 600   // Expression stack must be empty before entering the VM if an
 601   // exception happened.
 602   __ empty_expression_stack();
 603   if (name != nullptr) {
 604     __ load_absolute_address(Z_ARG2, (address)name);
 605   } else {
 606     __ clear_reg(Z_ARG2, true, false);
 607   }
 608 
 609   if (pass_oop) {
 610     __ call_VM(Z_tos,
 611                CAST_FROM_FN_PTR(address, InterpreterRuntime::create_klass_exception),
 612                Z_ARG2, Z_tos /*object (see TT::aastore())*/);
 613   } else {
 614     if (message != nullptr) {
 615       __ load_absolute_address(Z_ARG3, (address)message);
 616     } else {
 617       __ clear_reg(Z_ARG3, true, false);
 618     }
 619     __ call_VM(Z_tos,
 620                CAST_FROM_FN_PTR(address, InterpreterRuntime::create_exception),
 621                Z_ARG2, Z_ARG3);
 622   }
 623   // Throw exception.
 624   __ load_absolute_address(Z_R1_scratch, Interpreter::throw_exception_entry());
 625   __ z_br(Z_R1_scratch);
 626 
 627   BLOCK_COMMENT("} exception_handler_common");
 628 
 629   return entry;
 630 }
 631 
 632 address TemplateInterpreterGenerator::generate_return_entry_for (TosState state, int step, size_t index_size) {
 633   address entry = __ pc();
 634 
 635   BLOCK_COMMENT("return_entry {");
 636 
 637   // Pop i2c extension or revert top-2-parent-resize done by interpreted callees.
 638   Register sp_before_i2c_extension = Z_bcp;
 639   __ z_lg(Z_fp, _z_abi(callers_sp), Z_SP); // Restore frame pointer.
 640   __ z_lg(sp_before_i2c_extension, Address(Z_fp, _z_ijava_state_neg(top_frame_sp)));
 641   __ resize_frame_absolute(sp_before_i2c_extension, Z_locals/*tmp*/, true/*load_fp*/);
 642 
 643   // TODO(ZASM): necessary??
 644   //  // and null it as marker that esp is now tos until next java call
 645   //  __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), (int32_t)NULL_WORD);
 646 
 647   __ restore_bcp();
 648   __ restore_locals();
 649   __ restore_esp();
 650 
 651   if (state == atos) {
 652     __ profile_return_type(Z_tmp_1, Z_tos, Z_tmp_2);
 653   }
 654 
 655   Register cache  = Z_tmp_1;
 656   Register size   = Z_tmp_2;
 657   Register index  = Z_tmp_2;
 658   if (index_size == sizeof(u4)) {
 659     __ load_resolved_indy_entry(cache, index);
 660     __ z_llgh(size, in_bytes(ResolvedIndyEntry::num_parameters_offset()), cache);
 661   } else {
 662     assert(index_size == sizeof(u2), "Can only be u2");
 663     __ load_method_entry(cache, index);
 664     __ load_sized_value(size, Address(cache, in_bytes(ResolvedMethodEntry::num_parameters_offset())), sizeof(u2), false /*is_signed*/);
 665   }
 666   __ z_sllg(size, size, Interpreter::logStackElementSize); // Each argument size in bytes.
 667   __ z_agr(Z_esp, size);                                   // Pop arguments.
 668 
 669   __ check_and_handle_popframe(Z_thread);
 670   __ check_and_handle_earlyret(Z_thread);
 671 
 672   __ dispatch_next(state, step);
 673 
 674   BLOCK_COMMENT("} return_entry");
 675 
 676   return entry;
 677 }
 678 
 679 address TemplateInterpreterGenerator::generate_deopt_entry_for(TosState state,
 680                                                                int step,
 681                                                                address continuation) {
 682   address entry = __ pc();
 683 
 684   BLOCK_COMMENT("deopt_entry {");
 685 
 686   // TODO(ZASM): necessary? null last_sp until next java call
 687   // __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), (int32_t)NULL_WORD);
 688   __ z_lg(Z_fp, _z_abi(callers_sp), Z_SP); // Restore frame pointer.
 689   __ restore_bcp();
 690   __ restore_locals();
 691   __ restore_esp();
 692 
 693   // Handle exceptions.
 694   {
 695     Label L;
 696     __ load_and_test_long(Z_R0/*pending_exception*/, thread_(pending_exception));
 697     __ z_bre(L);
 698     __ call_VM(noreg,
 699                CAST_FROM_FN_PTR(address,
 700                                 InterpreterRuntime::throw_pending_exception));
 701     __ should_not_reach_here();
 702     __ bind(L);
 703   }
 704   if (continuation == nullptr) {
 705     __ dispatch_next(state, step);
 706   } else {
 707     __ jump_to_entry(continuation, Z_R1_scratch);
 708   }
 709 
 710   BLOCK_COMMENT("} deopt_entry");
 711 
 712   return entry;
 713 }
 714 
 715 address TemplateInterpreterGenerator::generate_safept_entry_for (TosState state,
 716                                                                 address runtime_entry) {
 717   address entry = __ pc();
 718   __ push(state);
 719   __ call_VM(noreg, runtime_entry);
 720   __ dispatch_via(vtos, Interpreter::_normal_table.table_for (vtos));
 721   return entry;
 722 }
 723 
 724 address TemplateInterpreterGenerator::generate_cont_resume_interpreter_adapter() {
 725   return nullptr;
 726 }
 727 
 728 
 729 //
 730 // Helpers for commoning out cases in the various type of method entries.
 731 //
 732 
 733 // Increment invocation count & check for overflow.
 734 //
 735 // Note: checking for negative value instead of overflow
 736 // so we have a 'sticky' overflow test.
 737 //
 738 // Z_ARG2: method (see generate_fixed_frame())
 739 //
 740 void TemplateInterpreterGenerator::generate_counter_incr(Label* overflow) {
 741   Label done;
 742   Register method = Z_ARG2; // Generate_fixed_frame() copies Z_method into Z_ARG2.
 743   Register m_counters = Z_ARG4;
 744 
 745   BLOCK_COMMENT("counter_incr {");
 746 
 747   // Note: In tiered we increment either counters in method or in MDO depending
 748   // if we are profiling or not.
 749   int increment = InvocationCounter::count_increment;
 750   if (ProfileInterpreter) {
 751     NearLabel no_mdo;
 752     Register mdo = m_counters;
 753     // Are we profiling?
 754     __ load_and_test_long(mdo, method2_(method, method_data));
 755     __ branch_optimized(Assembler::bcondZero, no_mdo);
 756     // Increment counter in the MDO.
 757     const Address mdo_invocation_counter(mdo, MethodData::invocation_counter_offset() +
 758                                          InvocationCounter::counter_offset());
 759     const Address mask(mdo, MethodData::invoke_mask_offset());
 760     __ increment_mask_and_jump(mdo_invocation_counter, increment, mask,
 761                                Z_R1_scratch, false, Assembler::bcondZero,
 762                                overflow);
 763     __ z_bru(done);
 764     __ bind(no_mdo);
 765   }
 766 
 767   // Increment counter in MethodCounters.
 768   const Address invocation_counter(m_counters,
 769                                    MethodCounters::invocation_counter_offset() +
 770                                    InvocationCounter::counter_offset());
 771   // Get address of MethodCounters object.
 772   __ get_method_counters(method, m_counters, done);
 773   const Address mask(m_counters, MethodCounters::invoke_mask_offset());
 774   __ increment_mask_and_jump(invocation_counter,
 775                              increment, mask,
 776                              Z_R1_scratch, false, Assembler::bcondZero,
 777                              overflow);
 778 
 779   __ bind(done);
 780 
 781   BLOCK_COMMENT("} counter_incr");
 782 }
 783 
 784 void TemplateInterpreterGenerator::generate_counter_overflow(Label& do_continue) {
 785   // InterpreterRuntime::frequency_counter_overflow takes two
 786   // arguments, the first (thread) is passed by call_VM, the second
 787   // indicates if the counter overflow occurs at a backwards branch
 788   // (null bcp). We pass zero for it. The call returns the address
 789   // of the verified entry point for the method or null if the
 790   // compilation did not complete (either went background or bailed
 791   // out).
 792   __ clear_reg(Z_ARG2);
 793   __ call_VM(noreg,
 794              CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow),
 795              Z_ARG2);
 796   __ z_bru(do_continue);
 797 }
 798 
 799 void TemplateInterpreterGenerator::generate_stack_overflow_check(Register frame_size, Register tmp1) {
 800   Register tmp2 = Z_R1_scratch;
 801   const int page_size = (int)os::vm_page_size();
 802   NearLabel after_frame_check;
 803 
 804   BLOCK_COMMENT("stack_overflow_check {");
 805 
 806   assert_different_registers(frame_size, tmp1);
 807 
 808   // Stack banging is sufficient overflow check if frame_size < page_size.
 809   if (Immediate::is_uimm(page_size, 15)) {
 810     __ z_chi(frame_size, page_size);
 811     __ z_brl(after_frame_check);
 812   } else {
 813     __ load_const_optimized(tmp1, page_size);
 814     __ compareU32_and_branch(frame_size, tmp1, Assembler::bcondLow, after_frame_check);
 815   }
 816 
 817   // Get the stack base, and in debug, verify it is non-zero.
 818   __ z_lg(tmp1, thread_(stack_base));
 819 #ifdef ASSERT
 820   address reentry = nullptr;
 821   NearLabel base_not_zero;
 822   __ compareU64_and_branch(tmp1, (intptr_t)0L, Assembler::bcondNotEqual, base_not_zero);
 823   reentry = __ stop_chain_static(reentry, "stack base is zero in generate_stack_overflow_check");
 824   __ bind(base_not_zero);
 825 #endif
 826 
 827   // Get the stack size, and in debug, verify it is non-zero.
 828   assert(sizeof(size_t) == sizeof(intptr_t), "wrong load size");
 829   __ z_lg(tmp2, thread_(stack_size));
 830 #ifdef ASSERT
 831   NearLabel size_not_zero;
 832   __ compareU64_and_branch(tmp2, (intptr_t)0L, Assembler::bcondNotEqual, size_not_zero);
 833   reentry = __ stop_chain_static(reentry, "stack size is zero in generate_stack_overflow_check");
 834   __ bind(size_not_zero);
 835 #endif
 836 
 837   // Compute the beginning of the protected zone minus the requested frame size.
 838   __ z_sgr(tmp1, tmp2);
 839   __ add2reg(tmp1, StackOverflow::stack_guard_zone_size());
 840 
 841   // Add in the size of the frame (which is the same as subtracting it from the
 842   // SP, which would take another register.
 843   __ z_agr(tmp1, frame_size);
 844 
 845   // The frame is greater than one page in size, so check against
 846   // the bottom of the stack.
 847   __ compareU64_and_branch(Z_SP, tmp1, Assembler::bcondHigh, after_frame_check);
 848 
 849   // The stack will overflow, throw an exception.
 850 
 851   // Restore SP to sender's sp. This is necessary if the sender's frame is an
 852   // extended compiled frame (see gen_c2i_adapter()) and safer anyway in case of
 853   // JSR292 adaptations.
 854   __ resize_frame_absolute(Z_R10, tmp1, true/*load_fp*/);
 855 
 856   // Note also that the restored frame is not necessarily interpreted.
 857   // Use the shared runtime version of the StackOverflowError.
 858   assert(SharedRuntime::throw_StackOverflowError_entry() != nullptr, "stub not yet generated");
 859   AddressLiteral stub(SharedRuntime::throw_StackOverflowError_entry());
 860   __ load_absolute_address(tmp1, SharedRuntime::throw_StackOverflowError_entry());
 861   __ z_br(tmp1);
 862 
 863   // If you get to here, then there is enough stack space.
 864   __ bind(after_frame_check);
 865 
 866   BLOCK_COMMENT("} stack_overflow_check");
 867 }
 868 
 869 // Allocate monitor and lock method (asm interpreter).
 870 //
 871 // Args:
 872 //   Z_locals: locals
 873 
 874 void TemplateInterpreterGenerator::lock_method(void) {
 875 
 876   BLOCK_COMMENT("lock_method {");
 877 
 878   // Synchronize method.
 879   const Register method = Z_tmp_2;
 880   __ get_method(method);
 881 
 882 #ifdef ASSERT
 883   address reentry = nullptr;
 884   {
 885     Label L;
 886     __ testbit(method2_(method, access_flags), JVM_ACC_SYNCHRONIZED_BIT);
 887     __ z_btrue(L);
 888     reentry = __ stop_chain_static(reentry, "method doesn't need synchronization");
 889     __ bind(L);
 890   }
 891 #endif // ASSERT
 892 
 893   // Get synchronization object.
 894   const Register object = Z_tmp_2;
 895 
 896   {
 897     Label     done;
 898     Label     static_method;
 899 
 900     __ testbit(method2_(method, access_flags), JVM_ACC_STATIC_BIT);
 901     __ z_btrue(static_method);
 902 
 903     // non-static method: Load receiver obj from stack.
 904     __ mem2reg_opt(object, Address(Z_locals, Interpreter::local_offset_in_bytes(0)));
 905     __ z_bru(done);
 906 
 907     __ bind(static_method);
 908 
 909     // Lock the java mirror.
 910     // Load mirror from interpreter frame.
 911     __ z_lg(object, _z_ijava_state_neg(mirror), Z_fp);
 912 
 913 #ifdef ASSERT
 914     {
 915       NearLabel L;
 916       __ compare64_and_branch(object, (intptr_t) 0, Assembler::bcondNotEqual, L);
 917       reentry = __ stop_chain_static(reentry, "synchronization object is null");
 918       __ bind(L);
 919     }
 920 #endif // ASSERT
 921 
 922     __ bind(done);
 923   }
 924 
 925   __ add_monitor_to_stack(true, Z_ARG3, Z_ARG4, Z_ARG5); // Allocate monitor elem.
 926   // Store object and lock it.
 927   __ get_monitors(Z_tmp_1);
 928   __ reg2mem_opt(object, Address(Z_tmp_1, BasicObjectLock::obj_offset()));
 929   __ lock_object(Z_tmp_1, object);
 930 
 931   BLOCK_COMMENT("} lock_method");
 932 }
 933 
 934 // Generate a fixed interpreter frame. This is identical setup for
 935 // interpreted methods and for native methods hence the shared code.
 936 //
 937 // Registers alive
 938 //   Z_thread   - JavaThread*
 939 //   Z_SP       - old stack pointer
 940 //   Z_method   - callee's method
 941 //   Z_esp      - parameter list (slot 'above' last param)
 942 //   Z_R14      - return pc, to be stored in caller's frame
 943 //   Z_R10      - sender sp, note: Z_tmp_1 is Z_R10!
 944 //
 945 // Registers updated
 946 //   Z_SP       - new stack pointer
 947 //   Z_esp      - callee's operand stack pointer
 948 //                points to the slot above the value on top
 949 //   Z_locals   - used to access locals: locals[i] := *(Z_locals - i*BytesPerWord)
 950 //   Z_bcp      - the bytecode pointer
 951 //   Z_fp       - the frame pointer, thereby killing Z_method
 952 //   Z_ARG2     - copy of Z_method
 953 //
 954 void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call) {
 955 
 956   //  stack layout
 957   //
 958   //   F1 [TOP_IJAVA_FRAME_ABI]              <-- Z_SP, Z_R10 (see note below)
 959   //      [F1's operand stack (unused)]
 960   //      [F1's outgoing Java arguments]     <-- Z_esp
 961   //      [F1's operand stack (non args)]
 962   //      [monitors]      (optional)
 963   //      [IJAVA_STATE]
 964   //
 965   //   F2 [PARENT_IJAVA_FRAME_ABI]
 966   //      ...
 967   //
 968   //  0x000
 969   //
 970   // Note: Z_R10, the sender sp, will be below Z_SP if F1 was extended by a c2i adapter.
 971 
 972   //=============================================================================
 973   // Allocate space for locals other than the parameters, the
 974   // interpreter state, monitors, and the expression stack.
 975 
 976   const Register local_count  = Z_ARG5;
 977   const Register fp           = Z_tmp_2;
 978   const Register const_method = Z_ARG1;
 979 
 980   BLOCK_COMMENT("generate_fixed_frame {");
 981   {
 982   // local registers
 983   const Register top_frame_size  = Z_ARG2;
 984   const Register sp_after_resize = Z_ARG3;
 985   const Register max_stack       = Z_ARG4;
 986 
 987   __ z_lg(const_method, Address(Z_method, Method::const_offset()));
 988   __ z_llgh(max_stack, Address(const_method, ConstMethod::size_of_parameters_offset()));
 989   __ z_sllg(Z_locals /*parameter_count bytes*/, max_stack /*parameter_count*/, LogBytesPerWord);
 990 
 991   if (native_call) {
 992     // If we're calling a native method, we replace max_stack (which is
 993     // zero) with space for the worst-case signature handler varargs
 994     // vector, which is:
 995     //   max_stack = max(Argument::n_register_parameters, parameter_count+2);
 996     //
 997     // We add two slots to the parameter_count, one for the jni
 998     // environment and one for a possible native mirror. We allocate
 999     // space for at least the number of ABI registers, even though
1000     // InterpreterRuntime::slow_signature_handler won't write more than
1001     // parameter_count+2 words when it creates the varargs vector at the
1002     // top of the stack. The generated slow signature handler will just
1003     // load trash into registers beyond the necessary number. We're
1004     // still going to cut the stack back by the ABI register parameter
1005     // count so as to get SP+16 pointing at the ABI outgoing parameter
1006     // area, so we need to allocate at least that much even though we're
1007     // going to throw it away.
1008     //
1009     __ add2reg(max_stack, 2);
1010 
1011     NearLabel passing_args_on_stack;
1012 
1013     // max_stack in bytes
1014     __ z_sllg(max_stack, max_stack, LogBytesPerWord);
1015 
1016     int argument_registers_in_bytes = Argument::n_register_parameters << LogBytesPerWord;
1017     __ compare64_and_branch(max_stack, argument_registers_in_bytes, Assembler::bcondNotLow, passing_args_on_stack);
1018 
1019     __ load_const_optimized(max_stack, argument_registers_in_bytes);
1020 
1021     __ bind(passing_args_on_stack);
1022   } else {
1023     // !native_call
1024     // local_count = method->constMethod->max_locals();
1025     __ z_llgh(local_count, Address(const_method, ConstMethod::size_of_locals_offset()));
1026 
1027     // Calculate number of non-parameter locals (in slots):
1028     __ z_sgr(local_count, max_stack);
1029 
1030     // max_stack = method->max_stack();
1031     __ z_llgh(max_stack, Address(const_method, ConstMethod::max_stack_offset()));
1032     // max_stack in bytes
1033     __ z_sllg(max_stack, max_stack, LogBytesPerWord);
1034   }
1035 
1036   // Resize (i.e. normally shrink) the top frame F1 ...
1037   //   F1      [TOP_IJAVA_FRAME_ABI]          <-- Z_SP, Z_R10
1038   //           F1's operand stack (free)
1039   //           ...
1040   //           F1's operand stack (free)      <-- Z_esp
1041   //           F1's outgoing Java arg m
1042   //           ...
1043   //           F1's outgoing Java arg 0
1044   //           ...
1045   //
1046   //  ... into a parent frame (Z_R10 holds F1's SP before any modification, see also above)
1047   //
1048   //           +......................+
1049   //           :                      :        <-- Z_R10, saved below as F0's z_ijava_state.sender_sp
1050   //           :                      :
1051   //   F1      [PARENT_IJAVA_FRAME_ABI]        <-- Z_SP       \
1052   //           F0's non arg local                             | = delta
1053   //           ...                                            |
1054   //           F0's non arg local              <-- Z_esp      /
1055   //           F1's outgoing Java arg m
1056   //           ...
1057   //           F1's outgoing Java arg 0
1058   //           ...
1059   //
1060   // then push the new top frame F0.
1061   //
1062   //   F0      [TOP_IJAVA_FRAME_ABI]    = frame::z_top_ijava_frame_abi_size \
1063   //           [operand stack]          = max_stack                          | = top_frame_size
1064   //           [IJAVA_STATE]            = frame::z_ijava_state_size         /
1065 
1066   // sp_after_resize = Z_esp - delta
1067   //
1068   // delta = PARENT_IJAVA_FRAME_ABI + (locals_count - params_count)
1069 
1070   __ add2reg(sp_after_resize, (Interpreter::stackElementSize) - (frame::z_parent_ijava_frame_abi_size), Z_esp);
1071   if (!native_call) {
1072     __ z_sllg(Z_R0_scratch, local_count, LogBytesPerWord); // Params have already been subtracted from local_count.
1073     __ z_slgr(sp_after_resize, Z_R0_scratch);
1074   }
1075 
1076   // top_frame_size = TOP_IJAVA_FRAME_ABI + max_stack + size of interpreter state
1077   __ add2reg(top_frame_size,
1078              frame::z_top_ijava_frame_abi_size +
1079              frame::z_ijava_state_size,
1080              max_stack);
1081 
1082   if (!native_call) {
1083     // Stack overflow check.
1084     // Native calls don't need the stack size check since they have no
1085     // expression stack and the arguments are already on the stack and
1086     // we only add a handful of words to the stack.
1087     Register frame_size = max_stack; // Reuse the register for max_stack.
1088     __ z_lgr(frame_size, Z_SP);
1089     __ z_sgr(frame_size, sp_after_resize);
1090     __ z_agr(frame_size, top_frame_size);
1091     generate_stack_overflow_check(frame_size, fp/*tmp1*/);
1092   }
1093 
1094   // asm_assert* is a nop in product builds
1095   NOT_PRODUCT(__ z_cg(Z_R14, _z_common_abi(return_pc), Z_SP));
1096   NOT_PRODUCT(__ asm_assert(Assembler::bcondEqual, "killed Z_R14", 0));
1097   __ resize_frame_absolute(sp_after_resize, fp, true);
1098   __ save_return_pc(Z_R14);
1099 
1100   // ... and push the new frame F0.
1101   __ push_frame(top_frame_size, fp, true /*copy_sp*/, false);
1102   }
1103 
1104   //=============================================================================
1105   // Initialize the new frame F0: initialize interpreter state.
1106 
1107   {
1108   // locals
1109   const Register local_addr = Z_ARG4;
1110 
1111   BLOCK_COMMENT("generate_fixed_frame: initialize interpreter state {");
1112 
1113 #ifdef ASSERT
1114   // Set the magic number (using local_addr as tmp register).
1115   __ load_const_optimized(local_addr, frame::z_istate_magic_number);
1116   __ z_stg(local_addr, _z_ijava_state_neg(magic), fp);
1117 #endif
1118 
1119   // Save sender SP from F1 (i.e. before it was potentially modified by an
1120   // adapter) into F0's interpreter state. We use it as well to revert
1121   // resizing the frame above.
1122   __ z_stg(Z_R10, _z_ijava_state_neg(sender_sp), fp);
1123 
1124   // Load cp cache and save it at the end of this block.
1125   __ z_lg(Z_R1_scratch, Address(const_method, ConstMethod::constants_offset()));
1126   __ z_lg(Z_R1_scratch, Address(Z_R1_scratch, ConstantPool::cache_offset()));
1127 
1128   // z_ijava_state->method = method;
1129   __ z_stg(Z_method, _z_ijava_state_neg(method), fp);
1130 
1131   // Point locals at the first argument. Method's locals are the
1132   // parameters on top of caller's expression stack.
1133   // Tos points past last Java argument.
1134 
1135   __ z_agr(Z_locals, Z_esp);
1136   // z_ijava_state->locals - i*BytesPerWord points to i-th Java local (i starts at 0)
1137   // z_ijava_state->locals = Z_esp + parameter_count bytes
1138   __ z_stg(Z_locals, _z_ijava_state_neg(locals), fp);
1139 
1140   // z_ijava_state->oop_temp = nullptr;
1141   __ store_const(Address(fp, oop_tmp_offset), 0);
1142 
1143   // Initialize z_ijava_state->mdx.
1144   Register Rmdp = Z_bcp;
1145   // native_call: assert that mdo is null
1146   const bool check_for_mdo = !native_call DEBUG_ONLY(|| native_call);
1147   if (ProfileInterpreter && check_for_mdo) {
1148     Label get_continue;
1149 
1150     __ load_and_test_long(Rmdp, method_(method_data));
1151     __ z_brz(get_continue);
1152     DEBUG_ONLY(if (native_call) __ stop("native methods don't have a mdo"));
1153     __ add2reg(Rmdp, in_bytes(MethodData::data_offset()));
1154     __ bind(get_continue);
1155   }
1156   __ z_stg(Rmdp, _z_ijava_state_neg(mdx), fp);
1157 
1158   // Initialize z_ijava_state->bcp and Z_bcp.
1159   if (native_call) {
1160     __ clear_reg(Z_bcp); // Must initialize. Will get written into frame where GC reads it.
1161   } else {
1162     __ add2reg(Z_bcp, in_bytes(ConstMethod::codes_offset()), const_method);
1163   }
1164   __ z_stg(Z_bcp, _z_ijava_state_neg(bcp), fp);
1165 
1166   // no monitors and empty operand stack
1167   // => z_ijava_state->monitors points to the top slot in IJAVA_STATE.
1168   // => Z_ijava_state->esp points one slot above into the operand stack.
1169   // z_ijava_state->monitors = fp - frame::z_ijava_state_size - Interpreter::stackElementSize;
1170   // z_ijava_state->esp = Z_esp = z_ijava_state->monitors;
1171   __ add2reg(Z_esp, -frame::z_ijava_state_size, fp);
1172   __ z_stg(Z_esp, _z_ijava_state_neg(monitors), fp);
1173   __ add2reg(Z_esp, -Interpreter::stackElementSize);
1174   __ z_stg(Z_esp, _z_ijava_state_neg(esp), fp);
1175 
1176   // z_ijava_state->cpoolCache = Z_R1_scratch (see load above);
1177   __ z_stg(Z_R1_scratch, _z_ijava_state_neg(cpoolCache), fp);
1178 
1179   // Get mirror and store it in the frame as GC root for this Method*.
1180   __ load_mirror_from_const_method(Z_R1_scratch, const_method);
1181   __ z_stg(Z_R1_scratch, _z_ijava_state_neg(mirror), fp);
1182 
1183   BLOCK_COMMENT("} generate_fixed_frame: initialize interpreter state");
1184 
1185   //=============================================================================
1186   if (!native_call) {
1187     // Local_count is already num_locals_slots - num_param_slots.
1188     // Start of locals: local_addr = Z_locals - locals size + 1 slot
1189     __ z_llgh(Z_R0_scratch, Address(const_method, ConstMethod::size_of_locals_offset()));
1190     __ add2reg(local_addr, BytesPerWord, Z_locals);
1191     __ z_sllg(Z_R0_scratch, Z_R0_scratch, LogBytesPerWord);
1192     __ z_sgr(local_addr, Z_R0_scratch);
1193 
1194     __ Clear_Array(local_count, local_addr, Z_ARG2);
1195   }
1196 
1197   }
1198   // Finally set the frame pointer, destroying Z_method.
1199   assert(Z_fp == Z_method, "maybe set Z_fp earlier if other register than Z_method");
1200   // Oprofile analysis suggests to keep a copy in a register to be used by
1201   // generate_counter_incr().
1202   __ z_lgr(Z_ARG2, Z_method);
1203   __ z_lgr(Z_fp, fp);
1204 
1205   BLOCK_COMMENT("} generate_fixed_frame");
1206 }
1207 
1208 // Various method entries
1209 
1210 // Math function, frame manager must set up an interpreter state, etc.
1211 address TemplateInterpreterGenerator::generate_math_entry(AbstractInterpreter::MethodKind kind) {
1212 
1213   // Decide what to do: Use same platform specific instructions and runtime calls as compilers.
1214   bool use_instruction = false;
1215   address runtime_entry = nullptr;
1216   int num_args = 1;
1217   bool double_precision = true;
1218 
1219   // s390 specific:
1220   switch (kind) {
1221     case Interpreter::java_lang_math_sqrt:
1222     case Interpreter::java_lang_math_abs:  use_instruction = true; break;
1223     case Interpreter::java_lang_math_fmaF:
1224     case Interpreter::java_lang_math_fmaD: use_instruction = UseFMA; break;
1225     default: break; // Fall back to runtime call.
1226   }
1227 
1228   switch (kind) {
1229     case Interpreter::java_lang_math_sin  : runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dsin);   break;
1230     case Interpreter::java_lang_math_cos  : runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dcos);   break;
1231     case Interpreter::java_lang_math_tan  : runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dtan);   break;
1232     case Interpreter::java_lang_math_tanh : /* run interpreted */ break;
1233     case Interpreter::java_lang_math_abs  : /* run interpreted */ break;
1234     case Interpreter::java_lang_math_sqrt : /* runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dsqrt); not available */ break;
1235     case Interpreter::java_lang_math_log  : runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dlog);   break;
1236     case Interpreter::java_lang_math_log10: runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dlog10); break;
1237     case Interpreter::java_lang_math_pow  : runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dpow); num_args = 2; break;
1238     case Interpreter::java_lang_math_exp  : runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dexp);   break;
1239     case Interpreter::java_lang_math_fmaF : /* run interpreted */ num_args = 3; double_precision = false; break;
1240     case Interpreter::java_lang_math_fmaD : /* run interpreted */ num_args = 3; break;
1241     default: ShouldNotReachHere();
1242   }
1243 
1244   // Use normal entry if neither instruction nor runtime call is used.
1245   if (!use_instruction && runtime_entry == nullptr) return nullptr;
1246 
1247   address entry = __ pc();
1248 
1249   if (use_instruction) {
1250     switch (kind) {
1251       case Interpreter::java_lang_math_sqrt:
1252         // Can use memory operand directly.
1253         __ z_sqdb(Z_FRET, Interpreter::stackElementSize, Z_esp);
1254         break;
1255       case Interpreter::java_lang_math_abs:
1256         // Load operand from stack.
1257         __ mem2freg_opt(Z_FRET, Address(Z_esp, Interpreter::stackElementSize));
1258         __ z_lpdbr(Z_FRET);
1259         break;
1260       case Interpreter::java_lang_math_fmaF:
1261         __ mem2freg_opt(Z_FRET,  Address(Z_esp,     Interpreter::stackElementSize)); // result reg = arg3
1262         __ mem2freg_opt(Z_FARG2, Address(Z_esp, 3 * Interpreter::stackElementSize)); // arg1
1263         __ z_maeb(Z_FRET, Z_FARG2, Address(Z_esp, 2 * Interpreter::stackElementSize));
1264         break;
1265       case Interpreter::java_lang_math_fmaD:
1266         __ mem2freg_opt(Z_FRET,  Address(Z_esp,     Interpreter::stackElementSize)); // result reg = arg3
1267         __ mem2freg_opt(Z_FARG2, Address(Z_esp, 5 * Interpreter::stackElementSize)); // arg1
1268         __ z_madb(Z_FRET, Z_FARG2, Address(Z_esp, 3 * Interpreter::stackElementSize));
1269         break;
1270       default: ShouldNotReachHere();
1271     }
1272   } else {
1273     // Load arguments
1274     assert(num_args <= 4, "passed in registers");
1275     if (double_precision) {
1276       int offset = (2 * num_args - 1) * Interpreter::stackElementSize;
1277       for (int i = 0; i < num_args; ++i) {
1278         __ mem2freg_opt(as_FloatRegister(Z_FARG1->encoding() + 2 * i), Address(Z_esp, offset));
1279         offset -= 2 * Interpreter::stackElementSize;
1280       }
1281     } else {
1282       int offset = num_args * Interpreter::stackElementSize;
1283       for (int i = 0; i < num_args; ++i) {
1284         __ mem2freg_opt(as_FloatRegister(Z_FARG1->encoding() + 2 * i), Address(Z_esp, offset));
1285         offset -= Interpreter::stackElementSize;
1286       }
1287     }
1288     // Call runtime
1289     __ save_return_pc();       // Save Z_R14.
1290     __ push_frame_abi160(0);   // Without new frame the RT call could overwrite the saved Z_R14.
1291 
1292     __ call_VM_leaf(runtime_entry);
1293 
1294     __ pop_frame();
1295     __ restore_return_pc();    // Restore Z_R14.
1296   }
1297 
1298   // Pop c2i arguments (if any) off when we return.
1299   __ resize_frame_absolute(Z_R10, Z_R0, true); // Cut the stack back to where the caller started.
1300 
1301   __ z_br(Z_R14);
1302 
1303   return entry;
1304 }
1305 
1306 // Interpreter stub for calling a native method. (asm interpreter).
1307 // This sets up a somewhat different looking stack for calling the
1308 // native method than the typical interpreter frame setup.
1309 address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
1310   // Determine code generation flags.
1311   bool inc_counter = UseCompiler || CountCompiledCalls;
1312 
1313   // Interpreter entry for ordinary Java methods.
1314   //
1315   // Registers alive
1316   //   Z_SP          - stack pointer
1317   //   Z_thread      - JavaThread*
1318   //   Z_method      - callee's method (method to be invoked)
1319   //   Z_esp         - operand (or expression) stack pointer of caller. one slot above last arg.
1320   //   Z_R10         - sender sp (before modifications, e.g. by c2i adapter
1321   //                   and as well by generate_fixed_frame below)
1322   //   Z_R14         - return address to caller (call_stub or c2i_adapter)
1323   //
1324   // Registers updated
1325   //   Z_SP          - stack pointer
1326   //   Z_fp          - callee's framepointer
1327   //   Z_esp         - callee's operand stack pointer
1328   //                   points to the slot above the value on top
1329   //   Z_locals      - used to access locals: locals[i] := *(Z_locals - i*BytesPerWord)
1330   //   Z_tos         - integer result, if any
1331   //   z_ftos        - floating point result, if any
1332   //
1333   // Stack layout at this point:
1334   //
1335   //   F1      [TOP_IJAVA_FRAME_ABI]         <-- Z_SP, Z_R10 (Z_R10 will be below Z_SP if
1336   //                                                          frame was extended by c2i adapter)
1337   //           [outgoing Java arguments]     <-- Z_esp
1338   //           ...
1339   //   PARENT  [PARENT_IJAVA_FRAME_ABI]
1340   //           ...
1341   //
1342 
1343   address entry_point = __ pc();
1344 
1345   // Make sure registers are different!
1346   assert_different_registers(Z_thread, Z_method, Z_esp);
1347 
1348   BLOCK_COMMENT("native_entry {");
1349 
1350   // Make sure method is native and not abstract.
1351 #ifdef ASSERT
1352   address reentry = nullptr;
1353   { Label L;
1354     __ testbit(method_(access_flags), JVM_ACC_NATIVE_BIT);
1355     __ z_btrue(L);
1356     reentry = __ stop_chain_static(reentry, "tried to execute non-native method as native");
1357     __ bind(L);
1358   }
1359   { Label L;
1360     __ testbit(method_(access_flags), JVM_ACC_ABSTRACT_BIT);
1361     __ z_bfalse(L);
1362     reentry = __ stop_chain_static(reentry, "tried to execute abstract method as non-abstract");
1363     __ bind(L);
1364   }
1365 #endif // ASSERT
1366 
1367   // Save the return PC into the callers frame for assertion in generate_fixed_frame.
1368   NOT_PRODUCT(__ save_return_pc(Z_R14));
1369 
1370   // Generate the code to allocate the interpreter stack frame.
1371   generate_fixed_frame(true);
1372 
1373   const Address do_not_unlock_if_synchronized(Z_thread, JavaThread::do_not_unlock_if_synchronized_offset());
1374   // Since at this point in the method invocation the exception handler
1375   // would try to exit the monitor of synchronized methods which hasn't
1376   // been entered yet, we set the thread local variable
1377   // _do_not_unlock_if_synchronized to true. If any exception was thrown by
1378   // runtime, exception handling i.e. unlock_if_synchronized_method will
1379   // check this thread local flag.
1380   __ z_mvi(do_not_unlock_if_synchronized, true);
1381 
1382   // Increment invocation count and check for overflow.
1383   NearLabel invocation_counter_overflow;
1384   if (inc_counter) {
1385     generate_counter_incr(&invocation_counter_overflow);
1386   }
1387 
1388   Label continue_after_compile;
1389   __ bind(continue_after_compile);
1390 
1391   bang_stack_shadow_pages(true);
1392 
1393   // Reset the _do_not_unlock_if_synchronized flag.
1394   __ z_mvi(do_not_unlock_if_synchronized, false);
1395 
1396   // Check for synchronized methods.
1397   // This mst happen AFTER invocation_counter check and stack overflow check,
1398   // so method is not locked if overflows.
1399   if (synchronized) {
1400     lock_method();
1401   } else {
1402     // No synchronization necessary.
1403 #ifdef ASSERT
1404     { Label L;
1405       __ get_method(Z_R1_scratch);
1406       __ testbit(method2_(Z_R1_scratch, access_flags), JVM_ACC_SYNCHRONIZED_BIT);
1407       __ z_bfalse(L);
1408       reentry = __ stop_chain_static(reentry, "method needs synchronization");
1409       __ bind(L);
1410     }
1411 #endif // ASSERT
1412   }
1413 
1414   // start execution
1415 
1416   // jvmti support
1417   __ notify_method_entry();
1418 
1419   //=============================================================================
1420   // Get and call the signature handler.
1421   const Register Rmethod                 = Z_tmp_2;
1422   const Register signature_handler_entry = Z_tmp_1;
1423   const Register Rresult_handler         = Z_tmp_3;
1424   Label call_signature_handler;
1425 
1426   assert_different_registers(Z_fp, Rmethod, signature_handler_entry, Rresult_handler);
1427   assert(Rresult_handler->is_nonvolatile(), "Rresult_handler must be in a non-volatile register");
1428 
1429   // Reload method.
1430   __ get_method(Rmethod);
1431 
1432   // Check for signature handler.
1433   __ load_and_test_long(signature_handler_entry, method2_(Rmethod, signature_handler));
1434   __ z_brne(call_signature_handler);
1435 
1436   // Method has never been called. Either generate a specialized
1437   // handler or point to the slow one.
1438   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call),
1439              Rmethod);
1440 
1441   // Reload method.
1442   __ get_method(Rmethod);
1443 
1444   // Reload signature handler, it must have been created/assigned in the meantime.
1445   __ z_lg(signature_handler_entry, method2_(Rmethod, signature_handler));
1446 
1447   __ bind(call_signature_handler);
1448 
1449   // We have a TOP_IJAVA_FRAME here, which belongs to us.
1450   __ set_top_ijava_frame_at_SP_as_last_Java_frame(Z_SP, Z_R1/*tmp*/);
1451 
1452   // Call signature handler and pass locals address in Z_ARG1.
1453   __ z_lgr(Z_ARG1, Z_locals);
1454   __ call_stub(signature_handler_entry);
1455   // Save result handler returned by signature handler.
1456   __ z_lgr(Rresult_handler, Z_RET);
1457 
1458   // Reload method (the slow signature handler may block for GC).
1459   __ get_method(Rmethod);
1460 
1461   // Pass mirror handle if static call.
1462   {
1463     Label method_is_not_static;
1464     __ testbit(method2_(Rmethod, access_flags), JVM_ACC_STATIC_BIT);
1465     __ z_bfalse(method_is_not_static);
1466     // Load mirror from interpreter frame.
1467     __ z_lg(Z_R1, _z_ijava_state_neg(mirror), Z_fp);
1468     // z_ijava_state.oop_temp = pool_holder->klass_part()->java_mirror();
1469     __ z_stg(Z_R1, oop_tmp_offset, Z_fp);
1470     // Pass handle to mirror as 2nd argument to JNI method.
1471     __ add2reg(Z_ARG2, oop_tmp_offset, Z_fp);
1472     __ bind(method_is_not_static);
1473   }
1474 
1475   // Pass JNIEnv address as first parameter.
1476   __ add2reg(Z_ARG1, in_bytes(JavaThread::jni_environment_offset()), Z_thread);
1477 
1478   // Note: last java frame has been set above already. The pc from there
1479   // is precise enough.
1480 
1481   // Get native function entry point before we change the thread state.
1482   __ z_lg(Z_R1/*native_method_entry*/, method2_(Rmethod, native_function));
1483 
1484   //=============================================================================
1485   // Transition from _thread_in_Java to _thread_in_native. As soon as
1486   // we make this change the safepoint code needs to be certain that
1487   // the last Java frame we established is good. The pc in that frame
1488   // just need to be near here not an actual return address.
1489 #ifdef ASSERT
1490   {
1491     NearLabel L;
1492     __ mem2reg_opt(Z_R14, Address(Z_thread, JavaThread::thread_state_offset()), false /*32 bits*/);
1493     __ compareU32_and_branch(Z_R14, _thread_in_Java, Assembler::bcondEqual, L);
1494     reentry = __ stop_chain_static(reentry, "Wrong thread state in native stub");
1495     __ bind(L);
1496   }
1497 #endif
1498 
1499   // Memory ordering: Z does not reorder store/load with subsequent load. That's strong enough.
1500   __ set_thread_state(_thread_in_native);
1501 
1502   //=============================================================================
1503   // Call the native method. Argument registers must not have been
1504   // overwritten since "__ call_stub(signature_handler);" (except for
1505   // ARG1 and ARG2 for static methods).
1506 
1507   __ call_c(Z_R1/*native_method_entry*/);
1508 
1509   // NOTE: frame::interpreter_frame_result() depends on these stores.
1510   __ z_stg(Z_RET, _z_ijava_state_neg(lresult), Z_fp);
1511   __ freg2mem_opt(Z_FRET, Address(Z_fp, _z_ijava_state_neg(fresult)));
1512   const Register Rlresult = signature_handler_entry;
1513   assert(Rlresult->is_nonvolatile(), "Rlresult must be in a non-volatile register");
1514   __ z_lgr(Rlresult, Z_RET);
1515 
1516   // Z_method may no longer be valid, because of GC.
1517 
1518   // Block, if necessary, before resuming in _thread_in_Java state.
1519   // In order for GC to work, don't clear the last_Java_sp until after
1520   // blocking.
1521 
1522   //=============================================================================
1523   // Switch thread to "native transition" state before reading the
1524   // synchronization state. This additional state is necessary
1525   // because reading and testing the synchronization state is not
1526   // atomic w.r.t. GC, as this scenario demonstrates: Java thread A,
1527   // in _thread_in_native state, loads _not_synchronized and is
1528   // preempted. VM thread changes sync state to synchronizing and
1529   // suspends threads for GC. Thread A is resumed to finish this
1530   // native method, but doesn't block here since it didn't see any
1531   // synchronization is progress, and escapes.
1532 
1533   __ set_thread_state(_thread_in_native_trans);
1534   if (!UseSystemMemoryBarrier) {
1535     __ z_fence();
1536   }
1537 
1538   // Now before we return to java we must look for a current safepoint
1539   // (a new safepoint can not start since we entered native_trans).
1540   // We must check here because a current safepoint could be modifying
1541   // the callers registers right this moment.
1542 
1543   // Check for safepoint operation in progress and/or pending suspend requests.
1544   {
1545     Label Continue, do_safepoint;
1546     __ safepoint_poll(do_safepoint, Z_R1);
1547     // Check for suspend.
1548     __ load_and_test_int(Z_R0/*suspend_flags*/, thread_(suspend_flags));
1549     __ z_bre(Continue); // 0 -> no flag set -> not suspended
1550     __ bind(do_safepoint);
1551     __ z_lgr(Z_ARG1, Z_thread);
1552     __ call_c(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans));
1553     __ bind(Continue);
1554   }
1555 
1556   //=============================================================================
1557   // Back in Interpreter Frame.
1558 
1559   // We are in thread_in_native_trans here and back in the normal
1560   // interpreter frame. We don't have to do anything special about
1561   // safepoints and we can switch to Java mode anytime we are ready.
1562 
1563   // Note: frame::interpreter_frame_result has a dependency on how the
1564   // method result is saved across the call to post_method_exit. For
1565   // native methods it assumes that the non-FPU/non-void result is
1566   // saved in z_ijava_state.lresult and a FPU result in z_ijava_state.fresult. If
1567   // this changes then the interpreter_frame_result implementation
1568   // will need to be updated too.
1569 
1570   //=============================================================================
1571   // Back in Java.
1572 
1573   // Memory ordering: Z does not reorder store/load with subsequent
1574   // load. That's strong enough.
1575   __ set_thread_state(_thread_in_Java);
1576 
1577   __ reset_last_Java_frame();
1578 
1579   // We reset the JNI handle block only after unboxing the result; see below.
1580 
1581   // The method register is junk from after the thread_in_native transition
1582   // until here. Also can't call_VM until the bcp has been
1583   // restored. Need bcp for throwing exception below so get it now.
1584   __ get_method(Rmethod);
1585 
1586   // Restore Z_bcp to have legal interpreter frame,
1587   // i.e., bci == 0 <=> Z_bcp == code_base().
1588   __ z_lg(Z_bcp, Address(Rmethod, Method::const_offset())); // get constMethod
1589   __ add2reg(Z_bcp, in_bytes(ConstMethod::codes_offset())); // get codebase
1590 
1591   if (CheckJNICalls) {
1592     // clear_pending_jni_exception_check
1593     __ clear_mem(Address(Z_thread, JavaThread::pending_jni_exception_check_fn_offset()), sizeof(oop));
1594   }
1595 
1596   // Check if the native method returns an oop, and if so, move it
1597   // from the jni handle to z_ijava_state.oop_temp. This is
1598   // necessary, because we reset the jni handle block below.
1599   // NOTE: frame::interpreter_frame_result() depends on this, too.
1600   { NearLabel no_oop_result;
1601   __ load_absolute_address(Z_R1, AbstractInterpreter::result_handler(T_OBJECT));
1602   __ compareU64_and_branch(Z_R1, Rresult_handler, Assembler::bcondNotEqual, no_oop_result);
1603   __ resolve_jobject(Rlresult, /* tmp1 */ Rmethod, /* tmp2 */ Z_R1);
1604   __ z_stg(Rlresult, oop_tmp_offset, Z_fp);
1605   __ bind(no_oop_result);
1606   }
1607 
1608   // Reset handle block.
1609   __ z_lg(Z_R1/*active_handles*/, thread_(active_handles));
1610   __ clear_mem(Address(Z_R1, JNIHandleBlock::top_offset()), 4);
1611 
1612   // Handle exceptions (exception handling will handle unlocking!).
1613   {
1614     Label L;
1615     __ load_and_test_long(Z_R0/*pending_exception*/, thread_(pending_exception));
1616     __ z_bre(L);
1617     __ MacroAssembler::call_VM(noreg,
1618                                CAST_FROM_FN_PTR(address,
1619                                InterpreterRuntime::throw_pending_exception));
1620     __ should_not_reach_here();
1621     __ bind(L);
1622   }
1623 
1624   if (synchronized) {
1625     Register Rfirst_monitor = Z_ARG2;
1626     __ add2reg(Rfirst_monitor, -(frame::z_ijava_state_size + (int)sizeof(BasicObjectLock)), Z_fp);
1627 #ifdef ASSERT
1628     NearLabel ok;
1629     __ z_lg(Z_R1, _z_ijava_state_neg(monitors), Z_fp);
1630     __ compareU64_and_branch(Rfirst_monitor, Z_R1, Assembler::bcondEqual, ok);
1631     reentry = __ stop_chain_static(reentry, "native_entry:unlock: inconsistent z_ijava_state.monitors");
1632     __ bind(ok);
1633 #endif
1634     __ unlock_object(Rfirst_monitor);
1635   }
1636 
1637   // JVMTI support. Result has already been saved above to the frame.
1638   __ notify_method_exit(true/*native_method*/, ilgl, InterpreterMacroAssembler::NotifyJVMTI);
1639 
1640   // Move native method result back into proper registers and return.
1641   __ mem2freg_opt(Z_FRET, Address(Z_fp, _z_ijava_state_neg(fresult)));
1642   __ mem2reg_opt(Z_RET, Address(Z_fp, _z_ijava_state_neg(lresult)));
1643   __ call_stub(Rresult_handler);
1644 
1645   // Pop the native method's interpreter frame.
1646   __ pop_interpreter_frame(Z_R14 /*return_pc*/, Z_ARG2/*tmp1*/, Z_ARG3/*tmp2*/);
1647 
1648   // Return to caller.
1649   __ z_br(Z_R14);
1650 
1651   if (inc_counter) {
1652     // Handle overflow of counter and compile method.
1653     __ bind(invocation_counter_overflow);
1654     generate_counter_overflow(continue_after_compile);
1655   }
1656 
1657   BLOCK_COMMENT("} native_entry");
1658 
1659   return entry_point;
1660 }
1661 
1662 //
1663 // Generic interpreted method entry to template interpreter.
1664 //
1665 address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {
1666   address entry_point = __ pc();
1667 
1668   bool inc_counter = UseCompiler || CountCompiledCalls;
1669 
1670   // Interpreter entry for ordinary Java methods.
1671   //
1672   // Registers alive
1673   //   Z_SP       - stack pointer
1674   //   Z_thread   - JavaThread*
1675   //   Z_method   - callee's method (method to be invoked)
1676   //   Z_esp      - operand (or expression) stack pointer of caller. one slot above last arg.
1677   //   Z_R10      - sender sp (before modifications, e.g. by c2i adapter
1678   //                           and as well by generate_fixed_frame below)
1679   //   Z_R14      - return address to caller (call_stub or c2i_adapter)
1680   //
1681   // Registers updated
1682   //   Z_SP       - stack pointer
1683   //   Z_fp       - callee's framepointer
1684   //   Z_esp      - callee's operand stack pointer
1685   //                points to the slot above the value on top
1686   //   Z_locals   - used to access locals: locals[i] := *(Z_locals - i*BytesPerWord)
1687   //   Z_tos      - integer result, if any
1688   //   z_ftos     - floating point result, if any
1689   //
1690   //
1691   // stack layout at this point:
1692   //
1693   //   F1      [TOP_IJAVA_FRAME_ABI]         <-- Z_SP, Z_R10 (Z_R10 will be below Z_SP if
1694   //                                                          frame was extended by c2i adapter)
1695   //           [outgoing Java arguments]     <-- Z_esp
1696   //           ...
1697   //   PARENT  [PARENT_IJAVA_FRAME_ABI]
1698   //           ...
1699   //
1700   // stack layout before dispatching the first bytecode:
1701   //
1702   //   F0      [TOP_IJAVA_FRAME_ABI]         <-- Z_SP
1703   //           [operand stack]               <-- Z_esp
1704   //           monitor (optional, can grow)
1705   //           [IJAVA_STATE]
1706   //   F1      [PARENT_IJAVA_FRAME_ABI]      <-- Z_fp (== *Z_SP)
1707   //           [F0's locals]                 <-- Z_locals
1708   //           [F1's operand stack]
1709   //           [F1's monitors] (optional)
1710   //           [IJAVA_STATE]
1711 
1712   // Make sure registers are different!
1713   assert_different_registers(Z_thread, Z_method, Z_esp);
1714 
1715   BLOCK_COMMENT("normal_entry {");
1716 
1717   // Make sure method is not native and not abstract.
1718   // Rethink these assertions - they can be simplified and shared.
1719 #ifdef ASSERT
1720   address reentry = nullptr;
1721   { Label L;
1722     __ testbit(method_(access_flags), JVM_ACC_NATIVE_BIT);
1723     __ z_bfalse(L);
1724     reentry = __ stop_chain_static(reentry, "tried to execute native method as non-native");
1725     __ bind(L);
1726   }
1727   { Label L;
1728     __ testbit(method_(access_flags), JVM_ACC_ABSTRACT_BIT);
1729     __ z_bfalse(L);
1730     reentry = __ stop_chain_static(reentry, "tried to execute abstract method as non-abstract");
1731     __ bind(L);
1732   }
1733 #endif // ASSERT
1734 
1735   // Save the return PC into the callers frame for assertion in generate_fixed_frame.
1736   NOT_PRODUCT(__ save_return_pc(Z_R14));
1737 
1738   // Generate the code to allocate the interpreter stack frame.
1739   generate_fixed_frame(false);
1740 
1741   const Address do_not_unlock_if_synchronized(Z_thread, JavaThread::do_not_unlock_if_synchronized_offset());
1742   // Since at this point in the method invocation the exception handler
1743   // would try to exit the monitor of synchronized methods which hasn't
1744   // been entered yet, we set the thread local variable
1745   // _do_not_unlock_if_synchronized to true. If any exception was thrown by
1746   // runtime, exception handling i.e. unlock_if_synchronized_method will
1747   // check this thread local flag.
1748   __ z_mvi(do_not_unlock_if_synchronized, true);
1749 
1750   __ profile_parameters_type(Z_tmp_2, Z_ARG3, Z_ARG4);
1751 
1752   // Increment invocation counter and check for overflow.
1753   //
1754   // Note: checking for negative value instead of overflow so we have a 'sticky'
1755   // overflow test (may be of importance as soon as we have true MT/MP).
1756   NearLabel invocation_counter_overflow;
1757   NearLabel Lcontinue;
1758   if (inc_counter) {
1759     generate_counter_incr(&invocation_counter_overflow);
1760   }
1761   __ bind(Lcontinue);
1762 
1763   bang_stack_shadow_pages(false);
1764 
1765   // Reset the _do_not_unlock_if_synchronized flag.
1766   __ z_mvi(do_not_unlock_if_synchronized, false);
1767 
1768   // Check for synchronized methods.
1769   // Must happen AFTER invocation_counter check and stack overflow check,
1770   // so method is not locked if overflows.
1771   if (synchronized) {
1772     // Allocate monitor and lock method.
1773     lock_method();
1774   } else {
1775 #ifdef ASSERT
1776     { Label L;
1777       __ get_method(Z_R1_scratch);
1778       __ testbit(method2_(Z_R1_scratch, access_flags), JVM_ACC_SYNCHRONIZED_BIT);
1779       __ z_bfalse(L);
1780       reentry = __ stop_chain_static(reentry, "method needs synchronization");
1781       __ bind(L);
1782     }
1783 #endif // ASSERT
1784   }
1785 
1786   // start execution
1787 
1788 #ifdef ASSERT
1789   __ verify_esp(Z_esp, Z_R1_scratch);
1790 #endif
1791 
1792   // jvmti support
1793   __ notify_method_entry();
1794 
1795   // Start executing instructions.
1796   __ dispatch_next(vtos);
1797   // Dispatch_next does not return.
1798   DEBUG_ONLY(__ should_not_reach_here());
1799 
1800   // Invocation counter overflow.
1801   if (inc_counter) {
1802     // Handle invocation counter overflow.
1803     __ bind(invocation_counter_overflow);
1804     generate_counter_overflow(Lcontinue);
1805   }
1806 
1807   BLOCK_COMMENT("} normal_entry");
1808 
1809   return entry_point;
1810 }
1811 
1812 
1813 /**
1814  * Method entry for static native methods:
1815  *   int java.util.zip.CRC32.update(int crc, int b)
1816  */
1817 address TemplateInterpreterGenerator::generate_CRC32_update_entry() {
1818   assert(UseCRC32Intrinsics, "this intrinsic is not supported");
1819   uint64_t entry_off = __ offset();
1820   Label    slow_path;
1821 
1822   // If we need a safepoint check, generate full interpreter entry.
1823   __ safepoint_poll(slow_path, Z_R1);
1824 
1825   BLOCK_COMMENT("CRC32_update {");
1826 
1827   // We don't generate local frame and don't align stack because
1828   // we not even call stub code (we generate the code inline)
1829   // and there is no safepoint on this path.
1830 
1831   // Load java parameters.
1832   // Z_esp is callers operand stack pointer, i.e. it points to the parameters.
1833   const Register argP    = Z_esp;
1834   const Register crc     = Z_ARG1;  // crc value
1835   const Register data    = Z_ARG2;  // address of java byte value (kernel_crc32 needs address)
1836   const Register dataLen = Z_ARG3;  // source data len (1 byte). Not used because calling the single-byte emitter.
1837   const Register table   = Z_ARG4;  // address of crc32 table
1838 
1839   // Arguments are reversed on java expression stack.
1840   __ z_la(data, 3+1*wordSize, argP);  // byte value (stack address).
1841                                         // Being passed as an int, the single byte is at offset +3.
1842   __ z_llgf(crc, 2 * wordSize, argP); // Current crc state, zero extend to 64 bit to have a clean register.
1843 
1844   StubRoutines::zarch::generate_load_crc_table_addr(_masm, table);
1845   __ kernel_crc32_singleByte(crc, data, dataLen, table, Z_R1, true);
1846 
1847   // Restore caller sp for c2i case.
1848   __ resize_frame_absolute(Z_R10, Z_R0, true); // Cut the stack back to where the caller started.
1849 
1850   __ z_br(Z_R14);
1851 
1852   BLOCK_COMMENT("} CRC32_update");
1853 
1854   // Use a previously generated vanilla native entry as the slow path.
1855   BIND(slow_path);
1856   __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::native), Z_R1);
1857   return __ addr_at(entry_off);
1858 }
1859 
1860 
1861 /**
1862  * Method entry for static native methods:
1863  *   int java.util.zip.CRC32.updateBytes(     int crc, byte[] b,  int off, int len)
1864  *   int java.util.zip.CRC32.updateByteBuffer(int crc, long* buf, int off, int len)
1865  */
1866 address TemplateInterpreterGenerator::generate_CRC32_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
1867   assert(UseCRC32Intrinsics, "this intrinsic is not supported");
1868   uint64_t entry_off = __ offset();
1869   Label    slow_path;
1870 
1871   // If we need a safepoint check, generate full interpreter entry.
1872   __ safepoint_poll(slow_path, Z_R1);
1873 
1874   // We don't generate local frame and don't align stack because
1875   // we call stub code and there is no safepoint on this path.
1876 
1877   // Load parameters.
1878   // Z_esp is callers operand stack pointer, i.e. it points to the parameters.
1879   const Register argP    = Z_esp;
1880   const Register crc     = Z_ARG1;  // crc value
1881   const Register data    = Z_ARG2;  // address of java byte array
1882   const Register dataLen = Z_ARG3;  // source data len
1883   const Register table   = Z_ARG4;  // address of crc32 table
1884   const Register t0      = Z_R10;   // work reg for kernel* emitters
1885   const Register t1      = Z_R11;   // work reg for kernel* emitters
1886   const Register t2      = Z_R12;   // work reg for kernel* emitters
1887   const Register t3      = Z_R13;   // work reg for kernel* emitters
1888 
1889   // Arguments are reversed on java expression stack.
1890   // Calculate address of start element.
1891   if (kind == Interpreter::java_util_zip_CRC32_updateByteBuffer) { // Used for "updateByteBuffer direct".
1892     // crc     @ (SP + 5W) (32bit)
1893     // buf     @ (SP + 3W) (64bit ptr to long array)
1894     // off     @ (SP + 2W) (32bit)
1895     // dataLen @ (SP + 1W) (32bit)
1896     // data = buf + off
1897     BLOCK_COMMENT("CRC32_updateByteBuffer {");
1898     __ z_llgf(crc,    5*wordSize, argP);  // current crc state
1899     __ z_lg(data,     3*wordSize, argP);  // start of byte buffer
1900     __ z_agf(data,    2*wordSize, argP);  // Add byte buffer offset.
1901     __ z_lgf(dataLen, 1*wordSize, argP);  // #bytes to process
1902   } else {                                                         // Used for "updateBytes update".
1903     // crc     @ (SP + 4W) (32bit)
1904     // buf     @ (SP + 3W) (64bit ptr to byte array)
1905     // off     @ (SP + 2W) (32bit)
1906     // dataLen @ (SP + 1W) (32bit)
1907     // data = buf + off + base_offset
1908     BLOCK_COMMENT("CRC32_updateBytes {");
1909     __ z_llgf(crc,    4*wordSize, argP);  // current crc state
1910     __ z_lg(data,     3*wordSize, argP);  // start of byte buffer
1911     __ z_agf(data,    2*wordSize, argP);  // Add byte buffer offset.
1912     __ z_lgf(dataLen, 1*wordSize, argP);  // #bytes to process
1913     __ z_aghi(data, arrayOopDesc::base_offset_in_bytes(T_BYTE));
1914   }
1915 
1916   StubRoutines::zarch::generate_load_crc_table_addr(_masm, table);
1917 
1918   __ resize_frame(-(6*8), Z_R0, true); // Resize frame to provide add'l space to spill 5 registers.
1919   __ z_stmg(t0, t3, 1*8, Z_SP);        // Spill regs 10..13 to make them available as work registers.
1920   __ kernel_crc32_1word(crc, data, dataLen, table, t0, t1, t2, t3, true);
1921   __ z_lmg(t0, t3, 1*8, Z_SP);         // Spill regs 10..13 back from stack.
1922 
1923   // Restore caller sp for c2i case.
1924   __ resize_frame_absolute(Z_R10, Z_R0, true); // Cut the stack back to where the caller started.
1925 
1926   __ z_br(Z_R14);
1927 
1928   BLOCK_COMMENT("} CRC32_update{Bytes|ByteBuffer}");
1929 
1930   // Use a previously generated vanilla native entry as the slow path.
1931   BIND(slow_path);
1932   __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::native), Z_R1);
1933   return __ addr_at(entry_off);
1934 }
1935 
1936 
1937 /**
1938  * Method entry for intrinsic-candidate (non-native) methods:
1939  *   int java.util.zip.CRC32C.updateBytes(           int crc, byte[] b,  int off, int end)
1940  *   int java.util.zip.CRC32C.updateDirectByteBuffer(int crc, long* buf, int off, int end)
1941  * Unlike CRC32, CRC32C does not have any methods marked as native
1942  * CRC32C also uses an "end" variable instead of the length variable CRC32 uses
1943  */
1944 address TemplateInterpreterGenerator::generate_CRC32C_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
1945   assert(UseCRC32CIntrinsics, "this intrinsic is not supported");
1946   uint64_t entry_off = __ offset();
1947 
1948   // We don't generate local frame and don't align stack because
1949   // we call stub code and there is no safepoint on this path.
1950 
1951   // Load parameters.
1952   // Z_esp is callers operand stack pointer, i.e. it points to the parameters.
1953   const Register argP    = Z_esp;
1954   const Register crc     = Z_ARG1;  // crc value
1955   const Register data    = Z_ARG2;  // address of java byte array
1956   const Register dataLen = Z_ARG3;  // source data len
1957   const Register table   = Z_ARG4;  // address of crc32 table
1958   const Register t0      = Z_R10;   // work reg for kernel* emitters
1959   const Register t1      = Z_R11;   // work reg for kernel* emitters
1960   const Register t2      = Z_R12;   // work reg for kernel* emitters
1961   const Register t3      = Z_R13;   // work reg for kernel* emitters
1962 
1963   // Arguments are reversed on java expression stack.
1964   // Calculate address of start element.
1965   if (kind == Interpreter::java_util_zip_CRC32C_updateDirectByteBuffer) { // Used for "updateByteBuffer direct".
1966     // crc     @ (SP + 5W) (32bit)
1967     // buf     @ (SP + 3W) (64bit ptr to long array)
1968     // off     @ (SP + 2W) (32bit)
1969     // dataLen @ (SP + 1W) (32bit)
1970     // data = buf + off
1971     BLOCK_COMMENT("CRC32C_updateDirectByteBuffer {");
1972     __ z_llgf(crc,    5*wordSize, argP);  // current crc state
1973     __ z_lg(data,     3*wordSize, argP);  // start of byte buffer
1974     __ z_agf(data,    2*wordSize, argP);  // Add byte buffer offset.
1975     __ z_lgf(dataLen, 1*wordSize, argP);  // #bytes to process, calculated as
1976     __ z_sgf(dataLen, Address(argP, 2*wordSize));  // (end_index - offset)
1977   } else {                                                                // Used for "updateBytes update".
1978     // crc     @ (SP + 4W) (32bit)
1979     // buf     @ (SP + 3W) (64bit ptr to byte array)
1980     // off     @ (SP + 2W) (32bit)
1981     // dataLen @ (SP + 1W) (32bit)
1982     // data = buf + off + base_offset
1983     BLOCK_COMMENT("CRC32C_updateBytes {");
1984     __ z_llgf(crc,    4*wordSize, argP);  // current crc state
1985     __ z_lg(data,     3*wordSize, argP);  // start of byte buffer
1986     __ z_agf(data,    2*wordSize, argP);  // Add byte buffer offset.
1987     __ z_lgf(dataLen, 1*wordSize, argP);  // #bytes to process, calculated as
1988     __ z_sgf(dataLen, Address(argP, 2*wordSize));  // (end_index - offset)
1989     __ z_aghi(data, arrayOopDesc::base_offset_in_bytes(T_BYTE));
1990   }
1991 
1992   StubRoutines::zarch::generate_load_crc32c_table_addr(_masm, table);
1993 
1994   __ resize_frame(-(6*8), Z_R0, true); // Resize frame to provide add'l space to spill 5 registers.
1995   __ z_stmg(t0, t3, 1*8, Z_SP);        // Spill regs 10..13 to make them available as work registers.
1996   __ kernel_crc32_1word(crc, data, dataLen, table, t0, t1, t2, t3, false);
1997   __ z_lmg(t0, t3, 1*8, Z_SP);         // Spill regs 10..13 back from stack.
1998 
1999   // Restore caller sp for c2i case.
2000   __ resize_frame_absolute(Z_R10, Z_R0, true); // Cut the stack back to where the caller started.
2001 
2002   __ z_br(Z_R14);
2003 
2004   BLOCK_COMMENT("} CRC32C_update{Bytes|DirectByteBuffer}");
2005   return __ addr_at(entry_off);
2006 }
2007 
2008 // Not supported
2009 address TemplateInterpreterGenerator::generate_currentThread() { return nullptr; }
2010 address TemplateInterpreterGenerator::generate_Float_intBitsToFloat_entry() { return nullptr; }
2011 address TemplateInterpreterGenerator::generate_Float_floatToRawIntBits_entry() { return nullptr; }
2012 address TemplateInterpreterGenerator::generate_Double_longBitsToDouble_entry() { return nullptr; }
2013 address TemplateInterpreterGenerator::generate_Double_doubleToRawLongBits_entry() { return nullptr; }
2014 address TemplateInterpreterGenerator::generate_Float_float16ToFloat_entry() { return nullptr; }
2015 address TemplateInterpreterGenerator::generate_Float_floatToFloat16_entry() { return nullptr; }
2016 
2017 void TemplateInterpreterGenerator::bang_stack_shadow_pages(bool native_call) {
2018   // Quick & dirty stack overflow checking: bang the stack & handle trap.
2019   // Note that we do the banging after the frame is setup, since the exception
2020   // handling code expects to find a valid interpreter frame on the stack.
2021   // Doing the banging earlier fails if the caller frame is not an interpreter
2022   // frame.
2023   // (Also, the exception throwing code expects to unlock any synchronized
2024   // method receiver, so do the banging after locking the receiver.)
2025 
2026   // Bang each page in the shadow zone. We can't assume it's been done for
2027   // an interpreter frame with greater than a page of locals, so each page
2028   // needs to be checked. Only true for non-native. For native, we only bang the last page.
2029   const size_t page_size      = os::vm_page_size();
2030   const int n_shadow_pages = (int)(StackOverflow::stack_shadow_zone_size()/page_size);
2031   const int start_page_num = native_call ? n_shadow_pages : 1;
2032   for (int pages = start_page_num; pages <= n_shadow_pages; pages++) {
2033     __ bang_stack_with_offset(pages*page_size);
2034   }
2035 }
2036 
2037 //-----------------------------------------------------------------------------
2038 // Exceptions
2039 
2040 void TemplateInterpreterGenerator::generate_throw_exception() {
2041 
2042   BLOCK_COMMENT("throw_exception {");
2043 
2044   // Entry point in previous activation (i.e., if the caller was interpreted).
2045   Interpreter::_rethrow_exception_entry = __ pc();
2046   __ z_lg(Z_fp, _z_abi(callers_sp), Z_SP); // Frame accessors use Z_fp.
2047   // Z_ARG1 (==Z_tos): exception
2048   // Z_ARG2          : Return address/pc that threw exception.
2049   __ restore_bcp();    // R13 points to call/send.
2050   __ restore_locals();
2051 
2052   // Fallthrough, no need to restore Z_esp.
2053 
2054   // Entry point for exceptions thrown within interpreter code.
2055   Interpreter::_throw_exception_entry = __ pc();
2056   // Expression stack is undefined here.
2057   // Z_ARG1 (==Z_tos): exception
2058   // Z_bcp: exception bcp
2059   __ verify_oop(Z_ARG1);
2060   __ z_lgr(Z_ARG2, Z_ARG1);
2061 
2062   // Expression stack must be empty before entering the VM in case of
2063   // an exception.
2064   __ empty_expression_stack();
2065   // Find exception handler address and preserve exception oop.
2066   const Register Rpreserved_exc_oop = Z_tmp_1;
2067   __ call_VM(Rpreserved_exc_oop,
2068              CAST_FROM_FN_PTR(address, InterpreterRuntime::exception_handler_for_exception),
2069              Z_ARG2);
2070   // Z_RET: exception handler entry point
2071   // Z_bcp: bcp for exception handler
2072   __ push_ptr(Rpreserved_exc_oop); // Push exception which is now the only value on the stack.
2073   __ z_br(Z_RET); // Jump to exception handler (may be _remove_activation_entry!).
2074 
2075   // If the exception is not handled in the current frame the frame is
2076   // removed and the exception is rethrown (i.e. exception
2077   // continuation is _rethrow_exception).
2078   //
2079   // Note: At this point the bci is still the bci for the instruction
2080   // which caused the exception and the expression stack is
2081   // empty. Thus, for any VM calls at this point, GC will find a legal
2082   // oop map (with empty expression stack).
2083 
2084   //
2085   // JVMTI PopFrame support
2086   //
2087 
2088   Interpreter::_remove_activation_preserving_args_entry = __ pc();
2089   __ z_lg(Z_fp, _z_parent_ijava_frame_abi(callers_sp), Z_SP);
2090   __ empty_expression_stack();
2091   // Set the popframe_processing bit in pending_popframe_condition
2092   // indicating that we are currently handling popframe, so that
2093   // call_VMs that may happen later do not trigger new popframe
2094   // handling cycles.
2095   __ load_sized_value(Z_tmp_1, Address(Z_thread, JavaThread::popframe_condition_offset()), 4, false /*signed*/);
2096   __ z_oill(Z_tmp_1, JavaThread::popframe_processing_bit);
2097   __ z_sty(Z_tmp_1, thread_(popframe_condition));
2098 
2099   {
2100     // Check to see whether we are returning to a deoptimized frame.
2101     // (The PopFrame call ensures that the caller of the popped frame is
2102     // either interpreted or compiled and deoptimizes it if compiled.)
2103     // In this case, we can't call dispatch_next() after the frame is
2104     // popped, but instead must save the incoming arguments and restore
2105     // them after deoptimization has occurred.
2106     //
2107     // Note that we don't compare the return PC against the
2108     // deoptimization blob's unpack entry because of the presence of
2109     // adapter frames in C2.
2110     NearLabel caller_not_deoptimized;
2111     __ z_lg(Z_ARG1, _z_parent_ijava_frame_abi(return_pc), Z_fp);
2112     __ call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::interpreter_contains), Z_ARG1);
2113     __ compareU64_and_branch(Z_RET, (intptr_t)0, Assembler::bcondNotEqual, caller_not_deoptimized);
2114 
2115     // Compute size of arguments for saving when returning to
2116     // deoptimized caller.
2117     __ get_method(Z_ARG2);
2118     __ z_lg(Z_ARG2, Address(Z_ARG2, Method::const_offset()));
2119     __ z_llgh(Z_ARG2, Address(Z_ARG2, ConstMethod::size_of_parameters_offset()));
2120     __ z_sllg(Z_ARG2, Z_ARG2, Interpreter::logStackElementSize); // slots 2 bytes
2121     __ restore_locals();
2122     // Compute address of args to be saved.
2123     __ z_lgr(Z_ARG3, Z_locals);
2124     __ z_slgr(Z_ARG3, Z_ARG2);
2125     __ add2reg(Z_ARG3, wordSize);
2126     // Save these arguments.
2127     __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::popframe_preserve_args),
2128                     Z_thread, Z_ARG2, Z_ARG3);
2129 
2130     __ remove_activation(vtos, Z_R14,
2131                          /* throw_monitor_exception */ false,
2132                          /* install_monitor_exception */ false,
2133                          /* notify_jvmdi */ false);
2134 
2135     // Inform deoptimization that it is responsible for restoring
2136     // these arguments.
2137     __ store_const(thread_(popframe_condition),
2138                    JavaThread::popframe_force_deopt_reexecution_bit,
2139                    Z_tmp_1, false);
2140 
2141     // Continue in deoptimization handler.
2142     __ z_br(Z_R14);
2143 
2144     __ bind(caller_not_deoptimized);
2145   }
2146 
2147   // Clear the popframe condition flag.
2148   __ clear_mem(thread_(popframe_condition), sizeof(int));
2149 
2150   __ remove_activation(vtos,
2151                        noreg,  // Retaddr is not used.
2152                        false,  // throw_monitor_exception
2153                        false,  // install_monitor_exception
2154                        false); // notify_jvmdi
2155   __ z_lg(Z_fp, _z_abi(callers_sp), Z_SP); // Restore frame pointer.
2156   __ restore_bcp();
2157   __ restore_locals();
2158   __ restore_esp();
2159   // The method data pointer was incremented already during
2160   // call profiling. We have to restore the mdp for the current bcp.
2161   if (ProfileInterpreter) {
2162     __ set_method_data_pointer_for_bcp();
2163   }
2164 #if INCLUDE_JVMTI
2165   {
2166     Label L_done;
2167 
2168     __ z_cli(0, Z_bcp, Bytecodes::_invokestatic);
2169     __ z_brc(Assembler::bcondNotEqual, L_done);
2170 
2171     // The member name argument must be restored if _invokestatic is
2172     // re-executed after a PopFrame call.  Detect such a case in the
2173     // InterpreterRuntime function and return the member name
2174     // argument, or null.
2175     __ z_lg(Z_ARG2, Address(Z_locals));
2176     __ get_method(Z_ARG3);
2177     __ call_VM(Z_tmp_1,
2178                CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null),
2179                Z_ARG2, Z_ARG3, Z_bcp);
2180 
2181     __ z_ltgr(Z_tmp_1, Z_tmp_1);
2182     __ z_brc(Assembler::bcondEqual, L_done);
2183 
2184     __ z_stg(Z_tmp_1, Address(Z_esp, wordSize));
2185     __ bind(L_done);
2186   }
2187 #endif // INCLUDE_JVMTI
2188   __ dispatch_next(vtos);
2189   // End of PopFrame support.
2190   Interpreter::_remove_activation_entry = __ pc();
2191 
2192   // In between activations - previous activation type unknown yet
2193   // compute continuation point - the continuation point expects the
2194   // following registers set up:
2195   //
2196   // Z_ARG1 (==Z_tos): exception
2197   // Z_ARG2          : return address/pc that threw exception
2198 
2199   Register return_pc = Z_tmp_1;
2200   Register handler   = Z_tmp_2;
2201    assert(return_pc->is_nonvolatile(), "use non-volatile reg. to preserve exception pc");
2202    assert(handler->is_nonvolatile(),   "use non-volatile reg. to handler pc");
2203   __ asm_assert_ijava_state_magic(return_pc/*tmp*/); // The top frame should be an interpreter frame.
2204   __ z_lg(return_pc, _z_parent_ijava_frame_abi(return_pc), Z_fp);
2205 
2206   // Moved removing the activation after VM call, because the new top
2207   // frame does not necessarily have the z_abi_160 required for a VM
2208   // call (e.g. if it is compiled).
2209 
2210   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
2211                                          SharedRuntime::exception_handler_for_return_address),
2212                         Z_thread, return_pc);
2213   __ z_lgr(handler, Z_RET); // Save exception handler.
2214 
2215   // Preserve exception over this code sequence.
2216   __ pop_ptr(Z_ARG1);
2217   __ set_vm_result(Z_ARG1);
2218   // Remove the activation (without doing throws on illegalMonitorExceptions).
2219   __ remove_activation(vtos, noreg/*ret.pc already loaded*/, false/*throw exc*/, true/*install exc*/, false/*notify jvmti*/);
2220   __ z_lg(Z_fp, _z_abi(callers_sp), Z_SP); // Restore frame pointer.
2221 
2222   __ get_vm_result(Z_ARG1);     // Restore exception.
2223   __ verify_oop(Z_ARG1);
2224   __ z_lgr(Z_ARG2, return_pc);  // Restore return address.
2225 
2226 #ifdef ASSERT
2227   // The return_pc in the new top frame is dead... at least that's my
2228   // current understanding. To assert this I overwrite it.
2229   // Note: for compiled frames the handler is the deopt blob
2230   // which writes Z_ARG2 into the return_pc slot.
2231   __ load_const_optimized(return_pc, 0xb00b1);
2232   __ z_stg(return_pc, _z_parent_ijava_frame_abi(return_pc), Z_SP);
2233 #endif
2234 
2235   // Z_ARG1 (==Z_tos): exception
2236   // Z_ARG2          : return address/pc that threw exception
2237 
2238   // Note that an "issuing PC" is actually the next PC after the call.
2239   __ z_br(handler);         // Jump to exception handler of caller.
2240 
2241   BLOCK_COMMENT("} throw_exception");
2242 }
2243 
2244 //
2245 // JVMTI ForceEarlyReturn support
2246 //
2247 address TemplateInterpreterGenerator::generate_earlyret_entry_for (TosState state) {
2248   address entry = __ pc();
2249 
2250   BLOCK_COMMENT("earlyret_entry {");
2251 
2252   __ z_lg(Z_fp, _z_parent_ijava_frame_abi(callers_sp), Z_SP);
2253   __ restore_bcp();
2254   __ restore_locals();
2255   __ restore_esp();
2256   __ empty_expression_stack();
2257   __ load_earlyret_value(state);
2258 
2259   Register RjvmtiState = Z_tmp_1;
2260   __ z_lg(RjvmtiState, thread_(jvmti_thread_state));
2261   __ store_const(Address(RjvmtiState, JvmtiThreadState::earlyret_state_offset()),
2262                  JvmtiThreadState::earlyret_inactive, 4, 4, Z_R0_scratch);
2263 
2264   if (state == itos) {
2265     // Narrow result if state is itos but result type is smaller.
2266     // Need to narrow in the return bytecode rather than in generate_return_entry
2267     // since compiled code callers expect the result to already be narrowed.
2268     __ narrow(Z_tos, Z_tmp_1); /* fall through */
2269   }
2270   __ remove_activation(state,
2271                        Z_tmp_1, // retaddr
2272                        false,   // throw_monitor_exception
2273                        false,   // install_monitor_exception
2274                        true);   // notify_jvmdi
2275   __ z_br(Z_tmp_1);
2276 
2277   BLOCK_COMMENT("} earlyret_entry");
2278 
2279   return entry;
2280 }
2281 
2282 //-----------------------------------------------------------------------------
2283 // Helper for vtos entry point generation.
2284 
2285 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,
2286                                                          address& bep,
2287                                                          address& cep,
2288                                                          address& sep,
2289                                                          address& aep,
2290                                                          address& iep,
2291                                                          address& lep,
2292                                                          address& fep,
2293                                                          address& dep,
2294                                                          address& vep) {
2295   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
2296   Label L;
2297   aep = __ pc(); __ push_ptr(); __ z_bru(L);
2298   fep = __ pc(); __ push_f();   __ z_bru(L);
2299   dep = __ pc(); __ push_d();   __ z_bru(L);
2300   lep = __ pc(); __ push_l();   __ z_bru(L);
2301   bep = cep = sep =
2302   iep = __ pc(); __ push_i();
2303   vep = __ pc();
2304   __ bind(L);
2305   generate_and_dispatch(t);
2306 }
2307 
2308 //-----------------------------------------------------------------------------
2309 
2310 #ifndef PRODUCT
2311 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
2312   address entry = __ pc();
2313   NearLabel counter_below_trace_threshold;
2314 
2315   if (TraceBytecodesAt > 0) {
2316     // Skip runtime call, if the trace threshold is not yet reached.
2317     __ load_absolute_address(Z_tmp_1, (address)&BytecodeCounter::_counter_value);
2318     __ load_absolute_address(Z_tmp_2, (address)&TraceBytecodesAt);
2319     __ load_sized_value(Z_tmp_1, Address(Z_tmp_1), 4, false /*signed*/);
2320     __ load_sized_value(Z_tmp_2, Address(Z_tmp_2), 8, false /*signed*/);
2321     __ compareU64_and_branch(Z_tmp_1, Z_tmp_2, Assembler::bcondLow, counter_below_trace_threshold);
2322   }
2323 
2324   int offset2 = state == ltos || state == dtos ? 2 : 1;
2325 
2326   __ push(state);
2327   // Preserved return pointer is in Z_R14.
2328   // InterpreterRuntime::trace_bytecode() preserved and returns the value passed as second argument.
2329   __ z_lgr(Z_ARG2, Z_R14);
2330   __ z_lg(Z_ARG3, Address(Z_esp, Interpreter::expr_offset_in_bytes(0)));
2331   if (WizardMode) {
2332     __ z_lgr(Z_ARG4, Z_esp); // Trace Z_esp in WizardMode.
2333   } else {
2334     __ z_lg(Z_ARG4, Address(Z_esp, Interpreter::expr_offset_in_bytes(offset2)));
2335   }
2336   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode), Z_ARG2, Z_ARG3, Z_ARG4);
2337   __ z_lgr(Z_R14, Z_RET); // Estore return address (see above).
2338   __ pop(state);
2339 
2340   __ bind(counter_below_trace_threshold);
2341   __ z_br(Z_R14); // return
2342 
2343   return entry;
2344 }
2345 
2346 // Make feasible for old CPUs.
2347 void TemplateInterpreterGenerator::count_bytecode() {
2348   __ load_absolute_address(Z_R1_scratch, (address) &BytecodeCounter::_counter_value);
2349   __ add2mem_32(Address(Z_R1_scratch), 1, Z_R0_scratch);
2350 }
2351 
2352 void TemplateInterpreterGenerator::histogram_bytecode(Template * t) {
2353   __ load_absolute_address(Z_R1_scratch, (address)&BytecodeHistogram::_counters[ t->bytecode() ]);
2354   __ add2mem_32(Address(Z_R1_scratch), 1, Z_tmp_1);
2355 }
2356 
2357 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template * t) {
2358   Address  index_addr(Z_tmp_1, (intptr_t) 0);
2359   Register index = Z_tmp_2;
2360 
2361   // Load previous index.
2362   __ load_absolute_address(Z_tmp_1, (address) &BytecodePairHistogram::_index);
2363   __ mem2reg_opt(index, index_addr, false);
2364 
2365   // Mask with current bytecode and store as new previous index.
2366   __ z_srl(index, BytecodePairHistogram::log2_number_of_codes);
2367   __ load_const_optimized(Z_R0_scratch,
2368                           (int)t->bytecode() << BytecodePairHistogram::log2_number_of_codes);
2369   __ z_or(index, Z_R0_scratch);
2370   __ reg2mem_opt(index, index_addr, false);
2371 
2372   // Load counter array's address.
2373   __ z_lgfr(index, index);   // Sign extend for addressing.
2374   __ z_sllg(index, index, LogBytesPerInt);  // index2bytes
2375   __ load_absolute_address(Z_R1_scratch,
2376                            (address) &BytecodePairHistogram::_counters);
2377   // Add index and increment counter.
2378   __ z_agr(Z_R1_scratch, index);
2379   __ add2mem_32(Address(Z_R1_scratch), 1, Z_tmp_1);
2380 }
2381 
2382 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
2383   // Call a little run-time stub to avoid blow-up for each bytecode.
2384   // The run-time runtime saves the right registers, depending on
2385   // the tosca in-state for the given template.
2386   address entry = Interpreter::trace_code(t->tos_in());
2387   guarantee(entry != nullptr, "entry must have been generated");
2388   __ call_stub(entry);
2389 }
2390 
2391 void TemplateInterpreterGenerator::stop_interpreter_at() {
2392   NearLabel L;
2393 
2394   __ load_absolute_address(Z_tmp_1, (address)&BytecodeCounter::_counter_value);
2395   __ load_absolute_address(Z_tmp_2, (address)&StopInterpreterAt);
2396   __ load_sized_value(Z_tmp_1, Address(Z_tmp_1), 4, false /*signed*/);
2397   __ load_sized_value(Z_tmp_2, Address(Z_tmp_2), 8, false /*signed*/);
2398   __ compareU64_and_branch(Z_tmp_1, Z_tmp_2, Assembler::bcondLow, L);
2399   assert(Z_tmp_1->is_nonvolatile(), "must be nonvolatile to preserve Z_tos");
2400   assert(Z_F8->is_nonvolatile(), "must be nonvolatile to preserve Z_ftos");
2401   __ z_lgr(Z_tmp_1, Z_tos);      // Save tos.
2402   __ z_lgr(Z_tmp_2, Z_bytecode); // Save Z_bytecode.
2403   __ z_ldr(Z_F8, Z_ftos);        // Save ftos.
2404   // Use -XX:StopInterpreterAt=<num> to set the limit
2405   // and break at breakpoint().
2406   __ call_VM(noreg, CAST_FROM_FN_PTR(address, breakpoint), false);
2407   __ z_lgr(Z_tos, Z_tmp_1);      // Restore tos.
2408   __ z_lgr(Z_bytecode, Z_tmp_2); // Save Z_bytecode.
2409   __ z_ldr(Z_ftos, Z_F8);        // Restore ftos.
2410   __ bind(L);
2411 }
2412 
2413 #endif // !PRODUCT