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