1 /*
   2  * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
   4  * Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #include "precompiled.hpp"
  28 #include "asm/macroAssembler.inline.hpp"
  29 #include "classfile/javaClasses.hpp"
  30 #include "gc/shared/barrierSetAssembler.hpp"
  31 #include "interpreter/bytecodeHistogram.hpp"
  32 #include "interpreter/bytecodeTracer.hpp"
  33 #include "interpreter/interp_masm.hpp"
  34 #include "interpreter/interpreter.hpp"
  35 #include "interpreter/interpreterRuntime.hpp"
  36 #include "interpreter/templateInterpreterGenerator.hpp"
  37 #include "interpreter/templateTable.hpp"
  38 #include "memory/resourceArea.hpp"
  39 #include "oops/arrayOop.hpp"
  40 #include "oops/method.hpp"
  41 #include "oops/methodData.hpp"
  42 #include "oops/oop.inline.hpp"
  43 #include "prims/jvmtiExport.hpp"
  44 #include "prims/jvmtiThreadState.hpp"
  45 #include "runtime/arguments.hpp"
  46 #include "runtime/deoptimization.hpp"
  47 #include "runtime/frame.inline.hpp"
  48 #include "runtime/jniHandles.hpp"
  49 #include "runtime/sharedRuntime.hpp"
  50 #include "runtime/stubRoutines.hpp"
  51 #include "runtime/synchronizer.hpp"
  52 #include "runtime/timer.hpp"
  53 #include "runtime/vframeArray.hpp"
  54 #include "utilities/debug.hpp"
  55 #include "utilities/powerOfTwo.hpp"
  56 #include <sys/types.h>
  57 
  58 #ifndef PRODUCT
  59 #include "oops/method.hpp"
  60 #endif // !PRODUCT
  61 
  62 // Size of interpreter code.  Increase if too small.  Interpreter will
  63 // fail with a guarantee ("not enough space for interpreter generation");
  64 // if too small.
  65 // Run with +PrintInterpreter to get the VM to print out the size.
  66 // Max size with JVMTI
  67 int TemplateInterpreter::InterpreterCodeSize = 256 * 1024;
  68 
  69 #define __ _masm->
  70 
  71 //-----------------------------------------------------------------------------
  72 
  73 address TemplateInterpreterGenerator::generate_slow_signature_handler() {
  74   address entry = __ pc();
  75 
  76   __ andi(esp, esp, -16);
  77   __ mv(c_rarg3, esp);
  78   // xmethod
  79   // xlocals
  80   // c_rarg3: first stack arg - wordSize
  81   // adjust sp
  82 
  83   __ addi(sp, c_rarg3, -18 * wordSize);
  84   __ addi(sp, sp, -2 * wordSize);
  85   __ sd(ra, Address(sp, 0));
  86 
  87   __ call_VM(noreg,
  88              CAST_FROM_FN_PTR(address,
  89                               InterpreterRuntime::slow_signature_handler),
  90              xmethod, xlocals, c_rarg3);
  91 
  92   // x10: result handler
  93 
  94   // Stack layout:
  95   // sp: return address           <- sp
  96   //      1 garbage
  97   //      8 integer args (if static first is unused)
  98   //      1 float/double identifiers
  99   //      8 double args
 100   //        stack args              <- esp
 101   //        garbage
 102   //        expression stack bottom
 103   //        bcp (NULL)
 104   //        ...
 105 
 106   // Restore ra
 107   __ ld(ra, Address(sp, 0));
 108   __ addi(sp, sp , 2 * wordSize);
 109 
 110   // Do FP first so we can use c_rarg3 as temp
 111   __ lwu(c_rarg3, Address(sp, 9 * wordSize)); // float/double identifiers
 112 
 113   for (int i = 0; i < Argument::n_float_register_parameters_c; i++) {
 114     const FloatRegister r = g_FPArgReg[i];
 115     Label d, done;
 116 
 117     __ andi(t0, c_rarg3, 1UL << i);
 118     __ bnez(t0, d);
 119     __ flw(r, Address(sp, (10 + i) * wordSize));
 120     __ j(done);
 121     __ bind(d);
 122     __ fld(r, Address(sp, (10 + i) * wordSize));
 123     __ bind(done);
 124   }
 125 
 126   // c_rarg0 contains the result from the call of
 127   // InterpreterRuntime::slow_signature_handler so we don't touch it
 128   // here.  It will be loaded with the JNIEnv* later.
 129   for (int i = 1; i < Argument::n_int_register_parameters_c; i++) {
 130     const Register rm = g_INTArgReg[i];
 131     __ ld(rm, Address(sp, i * wordSize));
 132   }
 133 
 134   __ addi(sp, sp, 18 * wordSize);
 135   __ ret();
 136 
 137   return entry;
 138 }
 139 
 140 // Various method entries
 141 address TemplateInterpreterGenerator::generate_math_entry(AbstractInterpreter::MethodKind kind) {
 142   // xmethod: Method*
 143   // x30: sender sp
 144   // esp: args
 145 
 146   if (!InlineIntrinsics) {
 147     return NULL; // Generate a vanilla entry
 148   }
 149 
 150   // These don't need a safepoint check because they aren't virtually
 151   // callable. We won't enter these intrinsics from compiled code.
 152   // If in the future we added an intrinsic which was virtually callable
 153   // we'd have to worry about how to safepoint so that this code is used.
 154 
 155   // mathematical functions inlined by compiler
 156   // (interpreter must provide identical implementation
 157   // in order to avoid monotonicity bugs when switching
 158   // from interpreter to compiler in the middle of some
 159   // computation)
 160   //
 161   // stack:
 162   //        [ arg ] <-- esp
 163   //        [ arg ]
 164   // retaddr in ra
 165 
 166   address fn = NULL;
 167   address entry_point = NULL;
 168   Register continuation = ra;
 169   switch (kind) {
 170     case Interpreter::java_lang_math_abs:
 171       entry_point = __ pc();
 172       __ fld(f10, Address(esp));
 173       __ fabs_d(f10, f10);
 174       __ mv(sp, x30); // Restore caller's SP
 175       break;
 176     case Interpreter::java_lang_math_sqrt:
 177       entry_point = __ pc();
 178       __ fld(f10, Address(esp));
 179       __ fsqrt_d(f10, f10);
 180       __ mv(sp, x30);
 181       break;
 182     case Interpreter::java_lang_math_sin :
 183       entry_point = __ pc();
 184       __ fld(f10, Address(esp));
 185       __ mv(sp, x30);
 186       __ mv(x9, ra);
 187       continuation = x9;  // The first callee-saved register
 188       if (StubRoutines::dsin() == NULL) {
 189         fn = CAST_FROM_FN_PTR(address, SharedRuntime::dsin);
 190       } else {
 191         fn = CAST_FROM_FN_PTR(address, StubRoutines::dsin());
 192       }
 193       __ mv(t0, fn);
 194       __ jalr(t0);
 195       break;
 196     case Interpreter::java_lang_math_cos :
 197       entry_point = __ pc();
 198       __ fld(f10, Address(esp));
 199       __ mv(sp, x30);
 200       __ mv(x9, ra);
 201       continuation = x9;  // The first callee-saved register
 202       if (StubRoutines::dcos() == NULL) {
 203         fn = CAST_FROM_FN_PTR(address, SharedRuntime::dcos);
 204       } else {
 205         fn = CAST_FROM_FN_PTR(address, StubRoutines::dcos());
 206       }
 207       __ mv(t0, fn);
 208       __ jalr(t0);
 209       break;
 210     case Interpreter::java_lang_math_tan :
 211       entry_point = __ pc();
 212       __ fld(f10, Address(esp));
 213       __ mv(sp, x30);
 214       __ mv(x9, ra);
 215       continuation = x9;  // The first callee-saved register
 216       if (StubRoutines::dtan() == NULL) {
 217         fn = CAST_FROM_FN_PTR(address, SharedRuntime::dtan);
 218       } else {
 219         fn = CAST_FROM_FN_PTR(address, StubRoutines::dtan());
 220       }
 221       __ mv(t0, fn);
 222       __ jalr(t0);
 223       break;
 224     case Interpreter::java_lang_math_log :
 225       entry_point = __ pc();
 226       __ fld(f10, Address(esp));
 227       __ mv(sp, x30);
 228       __ mv(x9, ra);
 229       continuation = x9;  // The first callee-saved register
 230       if (StubRoutines::dlog() == NULL) {
 231         fn = CAST_FROM_FN_PTR(address, SharedRuntime::dlog);
 232       } else {
 233         fn = CAST_FROM_FN_PTR(address, StubRoutines::dlog());
 234       }
 235       __ mv(t0, fn);
 236       __ jalr(t0);
 237       break;
 238     case Interpreter::java_lang_math_log10 :
 239       entry_point = __ pc();
 240       __ fld(f10, Address(esp));
 241       __ mv(sp, x30);
 242       __ mv(x9, ra);
 243       continuation = x9;  // The first callee-saved register
 244       if (StubRoutines::dlog10() == NULL) {
 245         fn = CAST_FROM_FN_PTR(address, SharedRuntime::dlog10);
 246       } else {
 247         fn = CAST_FROM_FN_PTR(address, StubRoutines::dlog10());
 248       }
 249       __ mv(t0, fn);
 250       __ jalr(t0);
 251       break;
 252     case Interpreter::java_lang_math_exp :
 253       entry_point = __ pc();
 254       __ fld(f10, Address(esp));
 255       __ mv(sp, x30);
 256       __ mv(x9, ra);
 257       continuation = x9;  // The first callee-saved register
 258       if (StubRoutines::dexp() == NULL) {
 259         fn = CAST_FROM_FN_PTR(address, SharedRuntime::dexp);
 260       } else {
 261         fn = CAST_FROM_FN_PTR(address, StubRoutines::dexp());
 262       }
 263       __ mv(t0, fn);
 264       __ jalr(t0);
 265       break;
 266     case Interpreter::java_lang_math_pow :
 267       entry_point = __ pc();
 268       __ mv(x9, ra);
 269       continuation = x9;
 270       __ fld(f10, Address(esp, 2 * Interpreter::stackElementSize));
 271       __ fld(f11, Address(esp));
 272       __ mv(sp, x30);
 273       if (StubRoutines::dpow() == NULL) {
 274         fn = CAST_FROM_FN_PTR(address, SharedRuntime::dpow);
 275       } else {
 276         fn = CAST_FROM_FN_PTR(address, StubRoutines::dpow());
 277       }
 278       __ mv(t0, fn);
 279       __ jalr(t0);
 280       break;
 281     case Interpreter::java_lang_math_fmaD :
 282       if (UseFMA) {
 283         entry_point = __ pc();
 284         __ fld(f10, Address(esp, 4 * Interpreter::stackElementSize));
 285         __ fld(f11, Address(esp, 2 * Interpreter::stackElementSize));
 286         __ fld(f12, Address(esp));
 287         __ fmadd_d(f10, f10, f11, f12);
 288         __ mv(sp, x30); // Restore caller's SP
 289       }
 290       break;
 291     case Interpreter::java_lang_math_fmaF :
 292       if (UseFMA) {
 293         entry_point = __ pc();
 294         __ flw(f10, Address(esp, 2 * Interpreter::stackElementSize));
 295         __ flw(f11, Address(esp, Interpreter::stackElementSize));
 296         __ flw(f12, Address(esp));
 297         __ fmadd_s(f10, f10, f11, f12);
 298         __ mv(sp, x30); // Restore caller's SP
 299       }
 300       break;
 301     default:
 302       ;
 303   }
 304   if (entry_point != NULL) {
 305     __ jr(continuation);
 306   }
 307 
 308   return entry_point;
 309 }
 310 
 311 // Abstract method entry
 312 // Attempt to execute abstract method. Throw exception
 313 address TemplateInterpreterGenerator::generate_abstract_entry(void) {
 314   // xmethod: Method*
 315   // x30: sender SP
 316 
 317   address entry_point = __ pc();
 318 
 319   // abstract method entry
 320 
 321   //  pop return address, reset last_sp to NULL
 322   __ empty_expression_stack();
 323   __ restore_bcp();      // bcp must be correct for exception handler   (was destroyed)
 324   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
 325 
 326   // throw exception
 327   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
 328                                      InterpreterRuntime::throw_AbstractMethodErrorWithMethod),
 329                                      xmethod);
 330   // the call_VM checks for exception, so we should never return here.
 331   __ should_not_reach_here();
 332 
 333   return entry_point;
 334 }
 335 
 336 address TemplateInterpreterGenerator::generate_StackOverflowError_handler() {
 337   address entry = __ pc();
 338 
 339 #ifdef ASSERT
 340   {
 341     Label L;
 342     __ ld(t0, Address(fp, frame::interpreter_frame_monitor_block_top_offset * wordSize));
 343     __ mv(t1, sp);
 344     // maximal sp for current fp (stack grows negative)
 345     // check if frame is complete
 346     __ bge(t0, t1, L);
 347     __ stop ("interpreter frame not set up");
 348     __ bind(L);
 349   }
 350 #endif // ASSERT
 351   // Restore bcp under the assumption that the current frame is still
 352   // interpreted
 353   __ restore_bcp();
 354 
 355   // expression stack must be empty before entering the VM if an
 356   // exception happened
 357   __ empty_expression_stack();
 358   // throw exception
 359   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_StackOverflowError));
 360   return entry;
 361 }
 362 
 363 address TemplateInterpreterGenerator::generate_ArrayIndexOutOfBounds_handler() {
 364   address entry = __ pc();
 365   // expression stack must be empty before entering the VM if an
 366   // exception happened
 367   __ empty_expression_stack();
 368   // setup parameters
 369 
 370   // convention: expect aberrant index in register x11
 371   __ zero_extend(c_rarg2, x11, 32);
 372   // convention: expect array in register x13
 373   __ mv(c_rarg1, x13);
 374   __ call_VM(noreg,
 375              CAST_FROM_FN_PTR(address,
 376                               InterpreterRuntime::
 377                               throw_ArrayIndexOutOfBoundsException),
 378              c_rarg1, c_rarg2);
 379   return entry;
 380 }
 381 
 382 address TemplateInterpreterGenerator::generate_ClassCastException_handler() {
 383   address entry = __ pc();
 384 
 385   // object is at TOS
 386   __ pop_reg(c_rarg1);
 387 
 388   // expression stack must be empty before entering the VM if an
 389   // exception happened
 390   __ empty_expression_stack();
 391 
 392   __ call_VM(noreg,
 393              CAST_FROM_FN_PTR(address,
 394                               InterpreterRuntime::
 395                               throw_ClassCastException),
 396              c_rarg1);
 397   return entry;
 398 }
 399 
 400 address TemplateInterpreterGenerator::generate_exception_handler_common(
 401   const char* name, const char* message, bool pass_oop) {
 402   assert(!pass_oop || message == NULL, "either oop or message but not both");
 403   address entry = __ pc();
 404   if (pass_oop) {
 405     // object is at TOS
 406     __ pop_reg(c_rarg2);
 407   }
 408   // expression stack must be empty before entering the VM if an
 409   // exception happened
 410   __ empty_expression_stack();
 411   // setup parameters
 412   __ la(c_rarg1, Address((address)name));
 413   if (pass_oop) {
 414     __ call_VM(x10, CAST_FROM_FN_PTR(address,
 415                                      InterpreterRuntime::
 416                                      create_klass_exception),
 417                c_rarg1, c_rarg2);
 418   } else {
 419     // kind of lame ExternalAddress can't take NULL because
 420     // external_word_Relocation will assert.
 421     if (message != NULL) {
 422       __ la(c_rarg2, Address((address)message));
 423     } else {
 424       __ mv(c_rarg2, NULL_WORD);
 425     }
 426     __ call_VM(x10,
 427                CAST_FROM_FN_PTR(address, InterpreterRuntime::create_exception),
 428                c_rarg1, c_rarg2);
 429   }
 430   // throw exception
 431   __ j(address(Interpreter::throw_exception_entry()));
 432   return entry;
 433 }
 434 
 435 address TemplateInterpreterGenerator::generate_return_entry_for(TosState state, int step, size_t index_size) {
 436   address entry = __ pc();
 437 
 438   // Restore stack bottom in case i2c adjusted stack
 439   __ ld(esp, Address(fp, frame::interpreter_frame_last_sp_offset * wordSize));
 440   // and NULL it as marker that esp is now tos until next java call
 441   __ sd(zr, Address(fp, frame::interpreter_frame_last_sp_offset * wordSize));
 442   __ restore_bcp();
 443   __ restore_locals();
 444   __ restore_constant_pool_cache();
 445   __ get_method(xmethod);
 446 
 447   if (state == atos) {
 448     Register obj = x10;
 449     Register mdp = x11;
 450     Register tmp = x12;
 451     __ ld(mdp, Address(xmethod, Method::method_data_offset()));
 452     __ profile_return_type(mdp, obj, tmp);
 453   }
 454 
 455   // Pop N words from the stack
 456   __ get_cache_and_index_at_bcp(x11, x12, 1, index_size);
 457   __ ld(x11, Address(x11, ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::flags_offset()));
 458   __ andi(x11, x11, ConstantPoolCacheEntry::parameter_size_mask);
 459 
 460   __ shadd(esp, x11, esp, t0, 3);
 461 
 462   // Restore machine SP
 463   __ ld(t0, Address(xmethod, Method::const_offset()));
 464   __ lhu(t0, Address(t0, ConstMethod::max_stack_offset()));
 465   __ addi(t0, t0, frame::interpreter_frame_monitor_size() + 2);
 466   __ ld(t1,
 467         Address(fp, frame::interpreter_frame_initial_sp_offset * wordSize));
 468   __ slli(t0, t0, 3);
 469   __ sub(t0, t1, t0);
 470   __ andi(sp, t0, -16);
 471 
 472  __ check_and_handle_popframe(xthread);
 473  __ check_and_handle_earlyret(xthread);
 474 
 475   __ get_dispatch();
 476   __ dispatch_next(state, step);
 477 
 478   return entry;
 479 }
 480 
 481 address TemplateInterpreterGenerator::generate_deopt_entry_for(TosState state,
 482                                                                int step,
 483                                                                address continuation) {
 484   address entry = __ pc();
 485   __ restore_bcp();
 486   __ restore_locals();
 487   __ restore_constant_pool_cache();
 488   __ get_method(xmethod);
 489   __ get_dispatch();
 490 
 491   // Calculate stack limit
 492   __ ld(t0, Address(xmethod, Method::const_offset()));
 493   __ lhu(t0, Address(t0, ConstMethod::max_stack_offset()));
 494   __ addi(t0, t0, frame::interpreter_frame_monitor_size() + 2);
 495   __ ld(t1, Address(fp, frame::interpreter_frame_initial_sp_offset * wordSize));
 496   __ slli(t0, t0, 3);
 497   __ sub(t0, t1, t0);
 498   __ andi(sp, t0, -16);
 499 
 500   // Restore expression stack pointer
 501   __ ld(esp, Address(fp, frame::interpreter_frame_last_sp_offset * wordSize));
 502   // NULL last_sp until next java call
 503   __ sd(zr, Address(fp, frame::interpreter_frame_last_sp_offset * wordSize));
 504 
 505   // handle exceptions
 506   {
 507     Label L;
 508     __ ld(t0, Address(xthread, Thread::pending_exception_offset()));
 509     __ beqz(t0, L);
 510     __ call_VM(noreg,
 511                CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_pending_exception));
 512     __ should_not_reach_here();
 513     __ bind(L);
 514   }
 515 
 516   if (continuation == NULL) {
 517     __ dispatch_next(state, step);
 518   } else {
 519     __ jump_to_entry(continuation);
 520   }
 521   return entry;
 522 }
 523 
 524 address TemplateInterpreterGenerator::generate_result_handler_for(BasicType type) {
 525   address entry = __ pc();
 526   if (type == T_OBJECT) {
 527     // retrieve result from frame
 528     __ ld(x10, Address(fp, frame::interpreter_frame_oop_temp_offset * wordSize));
 529     // and verify it
 530     __ verify_oop(x10);
 531   } else {
 532    __ cast_primitive_type(type, x10);
 533   }
 534 
 535   __ ret();                                  // return from result handler
 536   return entry;
 537 }
 538 
 539 address TemplateInterpreterGenerator::generate_safept_entry_for(TosState state,
 540                                                                 address runtime_entry) {
 541   assert_cond(runtime_entry != NULL);
 542   address entry = __ pc();
 543   __ push(state);
 544   __ call_VM(noreg, runtime_entry);
 545   __ fence(0xf, 0xf);
 546   __ dispatch_via(vtos, Interpreter::_normal_table.table_for(vtos));
 547   return entry;
 548 }
 549 
 550 // Helpers for commoning out cases in the various type of method entries.
 551 //
 552 
 553 
 554 // increment invocation count & check for overflow
 555 //
 556 // Note: checking for negative value instead of overflow
 557 //       so we have a 'sticky' overflow test
 558 //
 559 // xmethod: method
 560 //
 561 void TemplateInterpreterGenerator::generate_counter_incr(Label* overflow) {
 562   Label done;
 563   // Note: In tiered we increment either counters in Method* or in MDO depending if we're profiling or not.
 564   int increment = InvocationCounter::count_increment;
 565   Label no_mdo;
 566   if (ProfileInterpreter) {
 567     // Are we profiling?
 568     __ ld(x10, Address(xmethod, Method::method_data_offset()));
 569     __ beqz(x10, no_mdo);
 570     // Increment counter in the MDO
 571     const Address mdo_invocation_counter(x10, in_bytes(MethodData::invocation_counter_offset()) +
 572                                          in_bytes(InvocationCounter::counter_offset()));
 573     const Address mask(x10, in_bytes(MethodData::invoke_mask_offset()));
 574     __ increment_mask_and_jump(mdo_invocation_counter, increment, mask, t0, t1, false, overflow);
 575     __ j(done);
 576   }
 577   __ bind(no_mdo);
 578   // Increment counter in MethodCounters
 579   const Address invocation_counter(t1,
 580                                    MethodCounters::invocation_counter_offset() +
 581                                    InvocationCounter::counter_offset());
 582   __ get_method_counters(xmethod, t1, done);
 583   const Address mask(t1, in_bytes(MethodCounters::invoke_mask_offset()));
 584   __ increment_mask_and_jump(invocation_counter, increment, mask, t0, x11, false, overflow);
 585   __ bind(done);
 586 }
 587 
 588 void TemplateInterpreterGenerator::generate_counter_overflow(Label& do_continue) {
 589   __ mv(c_rarg1, zr);
 590   __ call_VM(noreg,
 591              CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), c_rarg1);
 592   __ j(do_continue);
 593 }
 594 
 595 // See if we've got enough room on the stack for locals plus overhead
 596 // below JavaThread::stack_overflow_limit(). If not, throw a StackOverflowError
 597 // without going through the signal handler, i.e., reserved and yellow zones
 598 // will not be made usable. The shadow zone must suffice to handle the
 599 // overflow.
 600 // The expression stack grows down incrementally, so the normal guard
 601 // page mechanism will work for that.
 602 //
 603 // NOTE: Since the additional locals are also always pushed (wasn't
 604 // obvious in generate_method_entry) so the guard should work for them
 605 // too.
 606 //
 607 // Args:
 608 //      x13: number of additional locals this frame needs (what we must check)
 609 //      xmethod: Method*
 610 //
 611 // Kills:
 612 //      x10
 613 void TemplateInterpreterGenerator::generate_stack_overflow_check(void) {
 614 
 615   // monitor entry size: see picture of stack set
 616   // (generate_method_entry) and frame_amd64.hpp
 617   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
 618 
 619   // total overhead size: entry_size + (saved fp through expr stack
 620   // bottom).  be sure to change this if you add/subtract anything
 621   // to/from the overhead area
 622   const int overhead_size =
 623     -(frame::interpreter_frame_initial_sp_offset * wordSize) + entry_size;
 624 
 625   const int page_size = os::vm_page_size();
 626 
 627   Label after_frame_check;
 628 
 629   // see if the frame is greater than one page in size. If so,
 630   // then we need to verify there is enough stack space remaining
 631   // for the additional locals.
 632   __ mv(t0, (page_size - overhead_size) / Interpreter::stackElementSize);
 633   __ bleu(x13, t0, after_frame_check);
 634 
 635   // compute sp as if this were going to be the last frame on
 636   // the stack before the red zone
 637 
 638   // locals + overhead, in bytes
 639   __ mv(x10, overhead_size);
 640   __ shadd(x10, x13, x10, t0, Interpreter::logStackElementSize);  // 2 slots per parameter.
 641 
 642   const Address stack_limit(xthread, JavaThread::stack_overflow_limit_offset());
 643   __ ld(t0, stack_limit);
 644 
 645 #ifdef ASSERT
 646   Label limit_okay;
 647   // Verify that thread stack limit is non-zero.
 648   __ bnez(t0, limit_okay);
 649   __ stop("stack overflow limit is zero");
 650   __ bind(limit_okay);
 651 #endif
 652 
 653   // Add stack limit to locals.
 654   __ add(x10, x10, t0);
 655 
 656   // Check against the current stack bottom.
 657   __ bgtu(sp, x10, after_frame_check);
 658 
 659   // Remove the incoming args, peeling the machine SP back to where it
 660   // was in the caller.  This is not strictly necessary, but unless we
 661   // do so the stack frame may have a garbage FP; this ensures a
 662   // correct call stack that we can always unwind.  The ANDI should be
 663   // unnecessary because the sender SP in x30 is always aligned, but
 664   // it doesn't hurt.
 665   __ andi(sp, x30, -16);
 666 
 667   // Note: the restored frame is not necessarily interpreted.
 668   // Use the shared runtime version of the StackOverflowError.
 669   assert(StubRoutines::throw_StackOverflowError_entry() != NULL, "stub not yet generated");
 670   __ far_jump(RuntimeAddress(StubRoutines::throw_StackOverflowError_entry()));
 671 
 672   // all done with frame size check
 673   __ bind(after_frame_check);
 674 }
 675 
 676 // Allocate monitor and lock method (asm interpreter)
 677 //
 678 // Args:
 679 //      xmethod: Method*
 680 //      xlocals: locals
 681 //
 682 // Kills:
 683 //      x10
 684 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, ...(param regs)
 685 //      t0, t1 (temporary regs)
 686 void TemplateInterpreterGenerator::lock_method() {
 687   // synchronize method
 688   const Address access_flags(xmethod, Method::access_flags_offset());
 689   const Address monitor_block_top(fp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
 690   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
 691 
 692 #ifdef ASSERT
 693   __ lwu(x10, access_flags);
 694   __ verify_access_flags(x10, JVM_ACC_SYNCHRONIZED, "method doesn't need synchronization", false);
 695 #endif // ASSERT
 696 
 697   // get synchronization object
 698   {
 699     Label done;
 700     __ lwu(x10, access_flags);
 701     __ andi(t0, x10, JVM_ACC_STATIC);
 702     // get receiver (assume this is frequent case)
 703     __ ld(x10, Address(xlocals, Interpreter::local_offset_in_bytes(0)));
 704     __ beqz(t0, done);
 705     __ load_mirror(x10, xmethod);
 706 
 707 #ifdef ASSERT
 708     {
 709       Label L;
 710       __ bnez(x10, L);
 711       __ stop("synchronization object is NULL");
 712       __ bind(L);
 713     }
 714 #endif // ASSERT
 715 
 716     __ bind(done);
 717   }
 718 
 719   // add space for monitor & lock
 720   __ add(sp, sp, - entry_size); // add space for a monitor entry
 721   __ add(esp, esp, - entry_size);
 722   __ mv(t0, esp);
 723   __ sd(t0, monitor_block_top);  // set new monitor block top
 724   // store object
 725   __ sd(x10, Address(esp, BasicObjectLock::obj_offset_in_bytes()));
 726   __ mv(c_rarg1, esp); // object address
 727   __ lock_object(c_rarg1);
 728 }
 729 
 730 // Generate a fixed interpreter frame. This is identical setup for
 731 // interpreted methods and for native methods hence the shared code.
 732 //
 733 // Args:
 734 //      ra: return address
 735 //      xmethod: Method*
 736 //      xlocals: pointer to locals
 737 //      xcpool: cp cache
 738 //      stack_pointer: previous sp
 739 void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call) {
 740   // initialize fixed part of activation frame
 741   if (native_call) {
 742     __ add(esp, sp, - 14 * wordSize);
 743     __ mv(xbcp, zr);
 744     __ add(sp, sp, - 14 * wordSize);
 745     // add 2 zero-initialized slots for native calls
 746     __ sd(zr, Address(sp, 13 * wordSize));
 747     __ sd(zr, Address(sp, 12 * wordSize));
 748   } else {
 749     __ add(esp, sp, - 12 * wordSize);
 750     __ ld(t0, Address(xmethod, Method::const_offset()));     // get ConstMethod
 751     __ add(xbcp, t0, in_bytes(ConstMethod::codes_offset())); // get codebase
 752     __ add(sp, sp, - 12 * wordSize);
 753   }
 754   __ sd(xbcp, Address(sp, wordSize));
 755   __ sd(esp, Address(sp, 0));
 756 
 757   if (ProfileInterpreter) {
 758     Label method_data_continue;
 759     __ ld(t0, Address(xmethod, Method::method_data_offset()));
 760     __ beqz(t0, method_data_continue);
 761     __ la(t0, Address(t0, in_bytes(MethodData::data_offset())));
 762     __ bind(method_data_continue);
 763   }
 764 
 765   __ sd(xmethod, Address(sp, 7 * wordSize));
 766   __ sd(ProfileInterpreter ? t0 : zr, Address(sp, 6 * wordSize));
 767 
 768   // Get mirror and store it in the frame as GC root for this Method*
 769   __ load_mirror(t2, xmethod);
 770   __ sd(zr, Address(sp, 5 * wordSize));
 771   __ sd(t2, Address(sp, 4 * wordSize));
 772 
 773   __ ld(xcpool, Address(xmethod, Method::const_offset()));
 774   __ ld(xcpool, Address(xcpool, ConstMethod::constants_offset()));
 775   __ ld(xcpool, Address(xcpool, ConstantPool::cache_offset_in_bytes()));
 776   __ sd(xcpool, Address(sp, 3 * wordSize));
 777   __ sd(xlocals, Address(sp, 2 * wordSize));
 778 
 779   __ sd(ra, Address(sp, 11 * wordSize));
 780   __ sd(fp, Address(sp, 10 * wordSize));
 781   __ la(fp, Address(sp, 12 * wordSize)); // include ra & fp
 782 
 783   // set sender sp
 784   // leave last_sp as null
 785   __ sd(x30, Address(sp, 9 * wordSize));
 786   __ sd(zr, Address(sp, 8 * wordSize));
 787 
 788   // Move SP out of the way
 789   if (!native_call) {
 790     __ ld(t0, Address(xmethod, Method::const_offset()));
 791     __ lhu(t0, Address(t0, ConstMethod::max_stack_offset()));
 792     __ add(t0, t0, frame::interpreter_frame_monitor_size() + 2);
 793     __ slli(t0, t0, 3);
 794     __ sub(t0, sp, t0);
 795     __ andi(sp, t0, -16);
 796   }
 797 }
 798 
 799 // End of helpers
 800 
 801 // Various method entries
 802 //------------------------------------------------------------------------------------------------------------------------
 803 //
 804 //
 805 
 806 // Method entry for java.lang.ref.Reference.get.
 807 address TemplateInterpreterGenerator::generate_Reference_get_entry(void) {
 808   // Code: _aload_0, _getfield, _areturn
 809   // parameter size = 1
 810   //
 811   // The code that gets generated by this routine is split into 2 parts:
 812   //    1. The "intrinsified" code for G1 (or any SATB based GC),
 813   //    2. The slow path - which is an expansion of the regular method entry.
 814   //
 815   // Notes:-
 816   // * In the G1 code we do not check whether we need to block for
 817   //   a safepoint. If G1 is enabled then we must execute the specialized
 818   //   code for Reference.get (except when the Reference object is null)
 819   //   so that we can log the value in the referent field with an SATB
 820   //   update buffer.
 821   //   If the code for the getfield template is modified so that the
 822   //   G1 pre-barrier code is executed when the current method is
 823   //   Reference.get() then going through the normal method entry
 824   //   will be fine.
 825   // * The G1 code can, however, check the receiver object (the instance
 826   //   of java.lang.Reference) and jump to the slow path if null. If the
 827   //   Reference object is null then we obviously cannot fetch the referent
 828   //   and so we don't need to call the G1 pre-barrier. Thus we can use the
 829   //   regular method entry code to generate the NPE.
 830   //
 831   // This code is based on generate_accessor_entry.
 832   //
 833   // xmethod: Method*
 834   // x30: senderSP must preserve for slow path, set SP to it on fast path
 835 
 836   // ra is live.  It must be saved around calls.
 837 
 838   address entry = __ pc();
 839 
 840   const int referent_offset = java_lang_ref_Reference::referent_offset();
 841   guarantee(referent_offset > 0, "referent offset not initialized");
 842 
 843   Label slow_path;
 844   const Register local_0 = c_rarg0;
 845   // Check if local 0 != NULL
 846   // If the receiver is null then it is OK to jump to the slow path.
 847   __ ld(local_0, Address(esp, 0));
 848   __ beqz(local_0, slow_path);
 849 
 850   __ mv(x9, x30);   // Move senderSP to a callee-saved register
 851 
 852   // Load the value of the referent field.
 853   const Address field_address(local_0, referent_offset);
 854   BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
 855   bs->load_at(_masm, IN_HEAP | ON_WEAK_OOP_REF, T_OBJECT, local_0, field_address, /*tmp1*/ t1, /*tmp2*/ t0);
 856 
 857   // areturn
 858   __ andi(sp, x9, -16);  // done with stack
 859   __ ret();
 860 
 861   // generate a vanilla interpreter entry as the slow path
 862   __ bind(slow_path);
 863   __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::zerolocals));
 864   return entry;
 865 }
 866 
 867 /**
 868  * Method entry for static native methods:
 869  *   int java.util.zip.CRC32.update(int crc, int b)
 870  */
 871 address TemplateInterpreterGenerator::generate_CRC32_update_entry() {
 872   // TODO: Unimplemented generate_CRC32_update_entry
 873   return 0;
 874 }
 875 
 876 /**
 877  * Method entry for static native methods:
 878  *   int java.util.zip.CRC32.updateBytes(int crc, byte[] b, int off, int len)
 879  *   int java.util.zip.CRC32.updateByteBuffer(int crc, long buf, int off, int len)
 880  */
 881 address TemplateInterpreterGenerator::generate_CRC32_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
 882   // TODO: Unimplemented generate_CRC32_updateBytes_entry
 883   return 0;
 884 }
 885 
 886 /**
 887  * Method entry for intrinsic-candidate (non-native) methods:
 888  *   int java.util.zip.CRC32C.updateBytes(int crc, byte[] b, int off, int end)
 889  *   int java.util.zip.CRC32C.updateDirectByteBuffer(int crc, long buf, int off, int end)
 890  * Unlike CRC32, CRC32C does not have any methods marked as native
 891  * CRC32C also uses an "end" variable instead of the length variable CRC32 uses
 892  */
 893 address TemplateInterpreterGenerator::generate_CRC32C_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
 894   // TODO: Unimplemented generate_CRC32C_updateBytes_entry
 895   return 0;
 896 }
 897 
 898 void TemplateInterpreterGenerator::bang_stack_shadow_pages(bool native_call) {
 899   // Bang each page in the shadow zone. We can't assume it's been done for
 900   // an interpreter frame with greater than a page of locals, so each page
 901   // needs to be checked.  Only true for non-native.
 902   const int n_shadow_pages = (int)(StackOverflow::stack_shadow_zone_size() / os::vm_page_size());
 903   const int start_page = native_call ? n_shadow_pages : 1;
 904   const int page_size = os::vm_page_size();
 905   for (int pages = start_page; pages <= n_shadow_pages ; pages++) {
 906     __ sub(t1, sp, pages * page_size);
 907     __ sd(zr, Address(t1));
 908   }
 909 }
 910 
 911 // Interpreter stub for calling a native method. (asm interpreter)
 912 // This sets up a somewhat different looking stack for calling the
 913 // native method than the typical interpreter frame setup.
 914 address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
 915   // determine code generation flags
 916   bool inc_counter = UseCompiler || CountCompiledCalls || LogTouchedMethods;
 917 
 918   // x11: Method*
 919   // x30: sender sp
 920 
 921   address entry_point = __ pc();
 922 
 923   const Address constMethod       (xmethod, Method::const_offset());
 924   const Address access_flags      (xmethod, Method::access_flags_offset());
 925   const Address size_of_parameters(x12, ConstMethod::
 926                                    size_of_parameters_offset());
 927 
 928   // get parameter size (always needed)
 929   __ ld(x12, constMethod);
 930   __ load_unsigned_short(x12, size_of_parameters);
 931 
 932   // Native calls don't need the stack size check since they have no
 933   // expression stack and the arguments are already on the stack and
 934   // we only add a handful of words to the stack.
 935 
 936   // xmethod: Method*
 937   // x12: size of parameters
 938   // x30: sender sp
 939 
 940   // for natives the size of locals is zero
 941 
 942   // compute beginning of parameters (xlocals)
 943   __ shadd(xlocals, x12, esp, xlocals, 3);
 944   __ addi(xlocals, xlocals, -wordSize);
 945 
 946   // Pull SP back to minimum size: this avoids holes in the stack
 947   __ andi(sp, esp, -16);
 948 
 949   // initialize fixed part of activation frame
 950   generate_fixed_frame(true);
 951 
 952   // make sure method is native & not abstract
 953 #ifdef ASSERT
 954   __ lwu(x10, access_flags);
 955   __ verify_access_flags(x10, JVM_ACC_NATIVE, "tried to execute non-native method as native", false);
 956   __ verify_access_flags(x10, JVM_ACC_ABSTRACT, "tried to execute abstract method in interpreter");
 957 #endif
 958 
 959   // Since at this point in the method invocation the exception
 960   // handler would try to exit the monitor of synchronized methods
 961   // which hasn't been entered yet, we set the thread local variable
 962   // _do_not_unlock_if_synchronized to true. The remove_activation
 963   // will check this flag.
 964 
 965   const Address do_not_unlock_if_synchronized(xthread,
 966                                               in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 967   __ mv(t1, true);
 968   __ sb(t1, do_not_unlock_if_synchronized);
 969 
 970   // increment invocation count & check for overflow
 971   Label invocation_counter_overflow;
 972   if (inc_counter) {
 973     generate_counter_incr(&invocation_counter_overflow);
 974   }
 975 
 976   Label continue_after_compile;
 977   __ bind(continue_after_compile);
 978 
 979   bang_stack_shadow_pages(true);
 980 
 981   // reset the _do_not_unlock_if_synchronized flag
 982   __ sb(zr, do_not_unlock_if_synchronized);
 983 
 984   // check for synchronized methods
 985   // Must happen AFTER invocation_counter check and stack overflow check,
 986   // so method is not locked if overflows.
 987   if (synchronized) {
 988     lock_method();
 989   } else {
 990     // no synchronization necessary
 991 #ifdef ASSERT
 992     __ lwu(x10, access_flags);
 993     __ verify_access_flags(x10, JVM_ACC_SYNCHRONIZED, "method needs synchronization");
 994 #endif
 995   }
 996 
 997   // start execution
 998 #ifdef ASSERT
 999   __ verify_frame_setup();
1000 #endif
1001 
1002   // jvmti support
1003   __ notify_method_entry();
1004 
1005   // work registers
1006   const Register t = x18;
1007   const Register result_handler = x19;
1008 
1009   // allocate space for parameters
1010   __ ld(t, Address(xmethod, Method::const_offset()));
1011   __ load_unsigned_short(t, Address(t, ConstMethod::size_of_parameters_offset()));
1012 
1013   __ slli(t, t, Interpreter::logStackElementSize);
1014   __ sub(x30, esp, t);
1015   __ andi(sp, x30, -16);
1016   __ mv(esp, x30);
1017 
1018   // get signature handler
1019   {
1020     Label L;
1021     __ ld(t, Address(xmethod, Method::signature_handler_offset()));
1022     __ bnez(t, L);
1023     __ call_VM(noreg,
1024                CAST_FROM_FN_PTR(address,
1025                                 InterpreterRuntime::prepare_native_call),
1026                xmethod);
1027     __ ld(t, Address(xmethod, Method::signature_handler_offset()));
1028     __ bind(L);
1029   }
1030 
1031   // call signature handler
1032   assert(InterpreterRuntime::SignatureHandlerGenerator::from() == xlocals,
1033          "adjust this code");
1034   assert(InterpreterRuntime::SignatureHandlerGenerator::to() == sp,
1035          "adjust this code");
1036   assert(InterpreterRuntime::SignatureHandlerGenerator::temp() == t0,
1037           "adjust this code");
1038 
1039   // The generated handlers do not touch xmethod (the method).
1040   // However, large signatures cannot be cached and are generated
1041   // each time here.  The slow-path generator can do a GC on return,
1042   // so we must reload it after the call.
1043   __ jalr(t);
1044   __ get_method(xmethod);        // slow path can do a GC, reload xmethod
1045 
1046 
1047   // result handler is in x10
1048   // set result handler
1049   __ mv(result_handler, x10);
1050   // pass mirror handle if static call
1051   {
1052     Label L;
1053     __ lwu(t, Address(xmethod, Method::access_flags_offset()));
1054     __ andi(t0, t, JVM_ACC_STATIC);
1055     __ beqz(t0, L);
1056     // get mirror
1057     __ load_mirror(t, xmethod);
1058     // copy mirror into activation frame
1059     __ sd(t, Address(fp, frame::interpreter_frame_oop_temp_offset * wordSize));
1060     // pass handle to mirror
1061     __ addi(c_rarg1, fp, frame::interpreter_frame_oop_temp_offset * wordSize);
1062     __ bind(L);
1063   }
1064 
1065   // get native function entry point in x28
1066   {
1067     Label L;
1068     __ ld(x28, Address(xmethod, Method::native_function_offset()));
1069     address unsatisfied = (SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
1070     __ mv(t1, unsatisfied);
1071     __ ld(t1, t1);
1072     __ bne(x28, t1, L);
1073     __ call_VM(noreg,
1074                CAST_FROM_FN_PTR(address,
1075                                 InterpreterRuntime::prepare_native_call),
1076                xmethod);
1077     __ get_method(xmethod);
1078     __ ld(x28, Address(xmethod, Method::native_function_offset()));
1079     __ bind(L);
1080   }
1081 
1082   // pass JNIEnv
1083   __ add(c_rarg0, xthread, in_bytes(JavaThread::jni_environment_offset()));
1084 
1085   // It is enough that the pc() points into the right code
1086   // segment. It does not have to be the correct return pc.
1087   Label native_return;
1088   __ set_last_Java_frame(esp, fp, native_return, x30);
1089 
1090   // change thread state
1091 #ifdef ASSERT
1092   {
1093     Label L;
1094     __ lwu(t, Address(xthread, JavaThread::thread_state_offset()));
1095     __ addi(t0, zr, (u1)_thread_in_Java);
1096     __ beq(t, t0, L);
1097     __ stop("Wrong thread state in native stub");
1098     __ bind(L);
1099   }
1100 #endif
1101 
1102   // Change state to native
1103   __ la(t1, Address(xthread, JavaThread::thread_state_offset()));
1104   __ mv(t0, _thread_in_native);
1105   __ membar(MacroAssembler::LoadStore | MacroAssembler::StoreStore);
1106   __ sw(t0, Address(t1));
1107 
1108   // Call the native method.
1109   __ jalr(x28);
1110   __ bind(native_return);
1111   __ get_method(xmethod);
1112   // result potentially in x10 or f10
1113 
1114   // make room for the pushes we're about to do
1115   __ sub(t0, esp, 4 * wordSize);
1116   __ andi(sp, t0, -16);
1117 
1118   // NOTE: The order of these pushes is known to frame::interpreter_frame_result
1119   // in order to extract the result of a method call. If the order of these
1120   // pushes change or anything else is added to the stack then the code in
1121   // interpreter_frame_result must also change.
1122   __ push(dtos);
1123   __ push(ltos);
1124 
1125   // change thread state
1126   // Force all preceding writes to be observed prior to thread state change
1127   __ membar(MacroAssembler::LoadStore | MacroAssembler::StoreStore);
1128 
1129   __ mv(t0, _thread_in_native_trans);
1130   __ sw(t0, Address(xthread, JavaThread::thread_state_offset()));
1131 
1132   // Force this write out before the read below
1133   __ membar(MacroAssembler::AnyAny);
1134 
1135   // check for safepoint operation in progress and/or pending suspend requests
1136   {
1137     Label L, Continue;
1138 
1139     // We need an acquire here to ensure that any subsequent load of the
1140     // global SafepointSynchronize::_state flag is ordered after this load
1141     // of the thread-local polling word. We don't want this poll to
1142     // return false (i.e. not safepointing) and a later poll of the global
1143     // SafepointSynchronize::_state spuriously to return true.
1144     //
1145     // This is to avoid a race when we're in a native->Java transition
1146     // racing the code which wakes up from a safepoint.
1147     __ safepoint_poll(L, true /* at_return */, true /* acquire */, false /* in_nmethod */);
1148     __ lwu(t1, Address(xthread, JavaThread::suspend_flags_offset()));
1149     __ beqz(t1, Continue);
1150     __ bind(L);
1151 
1152     // Don't use call_VM as it will see a possible pending exception
1153     // and forward it and never return here preventing us from
1154     // clearing _last_native_pc down below. So we do a runtime call by
1155     // hand.
1156     //
1157     __ mv(c_rarg0, xthread);
1158     __ mv(t1, CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans));
1159     __ jalr(t1);
1160     __ get_method(xmethod);
1161     __ reinit_heapbase();
1162     __ bind(Continue);
1163   }
1164 
1165   // change thread state
1166   // Force all preceding writes to be observed prior to thread state change
1167   __ membar(MacroAssembler::LoadStore | MacroAssembler::StoreStore);
1168 
1169   __ mv(t0, _thread_in_Java);
1170   __ sw(t0, Address(xthread, JavaThread::thread_state_offset()));
1171 
1172   // reset_last_Java_frame
1173   __ reset_last_Java_frame(true);
1174 
1175   if (CheckJNICalls) {
1176     // clear_pending_jni_exception_check
1177     __ sd(zr, Address(xthread, JavaThread::pending_jni_exception_check_fn_offset()));
1178   }
1179 
1180   // reset handle block
1181   __ ld(t, Address(xthread, JavaThread::active_handles_offset()));
1182   __ sd(zr, Address(t, JNIHandleBlock::top_offset_in_bytes()));
1183 
1184   // If result is an oop unbox and store it in frame where gc will see it
1185   // and result handler will pick it up
1186 
1187   {
1188     Label no_oop;
1189     __ la(t, ExternalAddress(AbstractInterpreter::result_handler(T_OBJECT)));
1190     __ bne(t, result_handler, no_oop);
1191     // Unbox oop result, e.g. JNIHandles::resolve result.
1192     __ pop(ltos);
1193     __ resolve_jobject(x10, xthread, t);
1194     __ sd(x10, Address(fp, frame::interpreter_frame_oop_temp_offset * wordSize));
1195     // keep stack depth as expected by pushing oop which will eventually be discarded
1196     __ push(ltos);
1197     __ bind(no_oop);
1198   }
1199 
1200   {
1201     Label no_reguard;
1202     __ lwu(t0, Address(xthread, in_bytes(JavaThread::stack_guard_state_offset())));
1203     __ addi(t1, zr, (u1)StackOverflow::stack_guard_yellow_reserved_disabled);
1204     __ bne(t0, t1, no_reguard);
1205 
1206     __ push_call_clobbered_registers();
1207     __ mv(c_rarg0, xthread);
1208     __ mv(t1, CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages));
1209     __ jalr(t1);
1210     __ pop_call_clobbered_registers();
1211     __ bind(no_reguard);
1212   }
1213 
1214   // The method register is junk from after the thread_in_native transition
1215   // until here.  Also can't call_VM until the bcp has been
1216   // restored.  Need bcp for throwing exception below so get it now.
1217   __ get_method(xmethod);
1218 
1219   // restore bcp to have legal interpreter frame, i.e., bci == 0 <=>
1220   // xbcp == code_base()
1221   __ ld(xbcp, Address(xmethod, Method::const_offset()));   // get ConstMethod*
1222   __ add(xbcp, xbcp, in_bytes(ConstMethod::codes_offset()));          // get codebase
1223   // handle exceptions (exception handling will handle unlocking!)
1224   {
1225     Label L;
1226     __ ld(t0, Address(xthread, Thread::pending_exception_offset()));
1227     __ beqz(t0, L);
1228     // Note: At some point we may want to unify this with the code
1229     // used in call_VM_base(); i.e., we should use the
1230     // StubRoutines::forward_exception code. For now this doesn't work
1231     // here because the sp is not correctly set at this point.
1232     __ MacroAssembler::call_VM(noreg,
1233                                CAST_FROM_FN_PTR(address,
1234                                InterpreterRuntime::throw_pending_exception));
1235     __ should_not_reach_here();
1236     __ bind(L);
1237   }
1238 
1239   // do unlocking if necessary
1240   {
1241     Label L;
1242     __ lwu(t, Address(xmethod, Method::access_flags_offset()));
1243     __ andi(t0, t, JVM_ACC_SYNCHRONIZED);
1244     __ beqz(t0, L);
1245     // the code below should be shared with interpreter macro
1246     // assembler implementation
1247     {
1248       Label unlock;
1249       // BasicObjectLock will be first in list, since this is a
1250       // synchronized method. However, need to check that the object
1251       // has not been unlocked by an explicit monitorexit bytecode.
1252 
1253       // monitor expect in c_rarg1 for slow unlock path
1254       __ la(c_rarg1, Address(fp,   // address of first monitor
1255                              (intptr_t)(frame::interpreter_frame_initial_sp_offset *
1256                                         wordSize - sizeof(BasicObjectLock))));
1257 
1258       __ ld(t, Address(c_rarg1, BasicObjectLock::obj_offset_in_bytes()));
1259       __ bnez(t, unlock);
1260 
1261       // Entry already unlocked, need to throw exception
1262       __ MacroAssembler::call_VM(noreg,
1263                                  CAST_FROM_FN_PTR(address,
1264                                                   InterpreterRuntime::throw_illegal_monitor_state_exception));
1265       __ should_not_reach_here();
1266 
1267       __ bind(unlock);
1268       __ unlock_object(c_rarg1);
1269     }
1270     __ bind(L);
1271   }
1272 
1273   // jvmti support
1274   // Note: This must happen _after_ handling/throwing any exceptions since
1275   //       the exception handler code notifies the runtime of method exits
1276   //       too. If this happens before, method entry/exit notifications are
1277   //       not properly paired (was bug - gri 11/22/99).
1278   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1279 
1280   __ pop(ltos);
1281   __ pop(dtos);
1282 
1283   __ jalr(result_handler);
1284 
1285   // remove activation
1286   __ ld(esp, Address(fp, frame::interpreter_frame_sender_sp_offset * wordSize)); // get sender sp
1287   // remove frame anchor
1288   __ leave();
1289 
1290   // restore sender sp
1291   __ mv(sp, esp);
1292 
1293   __ ret();
1294 
1295   if (inc_counter) {
1296     // Handle overflow of counter and compile method
1297     __ bind(invocation_counter_overflow);
1298     generate_counter_overflow(continue_after_compile);
1299   }
1300 
1301   return entry_point;
1302 }
1303 
1304 //
1305 // Generic interpreted method entry to (asm) interpreter
1306 //
1307 address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {
1308 
1309   // determine code generation flags
1310   const bool inc_counter  = UseCompiler || CountCompiledCalls || LogTouchedMethods;
1311 
1312   // t0: sender sp
1313   address entry_point = __ pc();
1314 
1315   const Address constMethod(xmethod, Method::const_offset());
1316   const Address access_flags(xmethod, Method::access_flags_offset());
1317   const Address size_of_parameters(x13,
1318                                    ConstMethod::size_of_parameters_offset());
1319   const Address size_of_locals(x13, ConstMethod::size_of_locals_offset());
1320 
1321   // get parameter size (always needed)
1322   // need to load the const method first
1323   __ ld(x13, constMethod);
1324   __ load_unsigned_short(x12, size_of_parameters);
1325 
1326   // x12: size of parameters
1327 
1328   __ load_unsigned_short(x13, size_of_locals); // get size of locals in words
1329   __ sub(x13, x13, x12); // x13 = no. of additional locals
1330 
1331   // see if we've got enough room on the stack for locals plus overhead.
1332   generate_stack_overflow_check();
1333 
1334   // compute beginning of parameters (xlocals)
1335   __ shadd(xlocals, x12, esp, t1, 3);
1336   __ add(xlocals, xlocals, -wordSize);
1337 
1338   // Make room for additional locals
1339   __ slli(t1, x13, 3);
1340   __ sub(t0, esp, t1);
1341 
1342   // Padding between locals and fixed part of activation frame to ensure
1343   // SP is always 16-byte aligned.
1344   __ andi(sp, t0, -16);
1345 
1346   // x13 - # of additional locals
1347   // allocate space for locals
1348   // explicitly initialize locals
1349   {
1350     Label exit, loop;
1351     __ blez(x13, exit); // do nothing if x13 <= 0
1352     __ bind(loop);
1353     __ sd(zr, Address(t0));
1354     __ add(t0, t0, wordSize);
1355     __ add(x13, x13, -1); // until everything initialized
1356     __ bnez(x13, loop);
1357     __ bind(exit);
1358   }
1359 
1360   // And the base dispatch table
1361   __ get_dispatch();
1362 
1363   // initialize fixed part of activation frame
1364   generate_fixed_frame(false);
1365 
1366   // make sure method is not native & not abstract
1367 #ifdef ASSERT
1368   __ lwu(x10, access_flags);
1369   __ verify_access_flags(x10, JVM_ACC_NATIVE, "tried to execute native method as non-native");
1370   __ verify_access_flags(x10, JVM_ACC_ABSTRACT, "tried to execute abstract method in interpreter");
1371 #endif
1372 
1373   // Since at this point in the method invocation the exception
1374   // handler would try to exit the monitor of synchronized methods
1375   // which hasn't been entered yet, we set the thread local variable
1376   // _do_not_unlock_if_synchronized to true. The remove_activation
1377   // will check this flag.
1378 
1379   const Address do_not_unlock_if_synchronized(xthread,
1380                                               in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1381   __ mv(t1, true);
1382   __ sb(t1, do_not_unlock_if_synchronized);
1383 
1384   Label no_mdp;
1385   const Register mdp = x13;
1386   __ ld(mdp, Address(xmethod, Method::method_data_offset()));
1387   __ beqz(mdp, no_mdp);
1388   __ add(mdp, mdp, in_bytes(MethodData::data_offset()));
1389   __ profile_parameters_type(mdp, x11, x12, x14); // use x11, x12, x14 as tmp registers
1390   __ bind(no_mdp);
1391 
1392   // increment invocation count & check for overflow
1393   Label invocation_counter_overflow;
1394   if (inc_counter) {
1395     generate_counter_incr(&invocation_counter_overflow);
1396   }
1397 
1398   Label continue_after_compile;
1399   __ bind(continue_after_compile);
1400 
1401   bang_stack_shadow_pages(false);
1402 
1403   // reset the _do_not_unlock_if_synchronized flag
1404   __ sb(zr, do_not_unlock_if_synchronized);
1405 
1406   // check for synchronized methods
1407   // Must happen AFTER invocation_counter check and stack overflow check,
1408   // so method is not locked if overflows.
1409   if (synchronized) {
1410     // Allocate monitor and lock method
1411     lock_method();
1412   } else {
1413     // no synchronization necessary
1414 #ifdef ASSERT
1415     __ lwu(x10, access_flags);
1416     __ verify_access_flags(x10, JVM_ACC_SYNCHRONIZED, "method needs synchronization");
1417 #endif
1418   }
1419 
1420   // start execution
1421 #ifdef ASSERT
1422   __ verify_frame_setup();
1423 #endif
1424 
1425   // jvmti support
1426   __ notify_method_entry();
1427 
1428   __ dispatch_next(vtos);
1429 
1430   // invocation counter overflow
1431   if (inc_counter) {
1432     // Handle overflow of counter and compile method
1433     __ bind(invocation_counter_overflow);
1434     generate_counter_overflow(continue_after_compile);
1435   }
1436 
1437   return entry_point;
1438 }
1439 
1440 //-----------------------------------------------------------------------------
1441 // Exceptions
1442 
1443 void TemplateInterpreterGenerator::generate_throw_exception() {
1444   // Entry point in previous activation (i.e., if the caller was
1445   // interpreted)
1446   Interpreter::_rethrow_exception_entry = __ pc();
1447   // Restore sp to interpreter_frame_last_sp even though we are going
1448   // to empty the expression stack for the exception processing.
1449   __ sd(zr, Address(fp, frame::interpreter_frame_last_sp_offset * wordSize));
1450   // x10: exception
1451   // x13: return address/pc that threw exception
1452   __ restore_bcp();    // xbcp points to call/send
1453   __ restore_locals();
1454   __ restore_constant_pool_cache();
1455   __ reinit_heapbase();  // restore xheapbase as heapbase.
1456   __ get_dispatch();
1457 
1458   // Entry point for exceptions thrown within interpreter code
1459   Interpreter::_throw_exception_entry = __ pc();
1460   // If we came here via a NullPointerException on the receiver of a
1461   // method, xthread may be corrupt.
1462   __ get_method(xmethod);
1463   // expression stack is undefined here
1464   // x10: exception
1465   // xbcp: exception bcp
1466   __ verify_oop(x10);
1467   __ mv(c_rarg1, x10);
1468 
1469   // expression stack must be empty before entering the VM in case of
1470   // an exception
1471   __ empty_expression_stack();
1472   // find exception handler address and preserve exception oop
1473   __ call_VM(x13,
1474              CAST_FROM_FN_PTR(address,
1475                           InterpreterRuntime::exception_handler_for_exception),
1476              c_rarg1);
1477 
1478   // Calculate stack limit
1479   __ ld(t0, Address(xmethod, Method::const_offset()));
1480   __ lhu(t0, Address(t0, ConstMethod::max_stack_offset()));
1481   __ add(t0, t0, frame::interpreter_frame_monitor_size() + 4);
1482   __ ld(t1, Address(fp, frame::interpreter_frame_initial_sp_offset * wordSize));
1483   __ slli(t0, t0, 3);
1484   __ sub(t0, t1, t0);
1485   __ andi(sp, t0, -16);
1486 
1487   // x10: exception handler entry point
1488   // x13: preserved exception oop
1489   // xbcp: bcp for exception handler
1490   __ push_ptr(x13); // push exception which is now the only value on the stack
1491   __ jr(x10); // jump to exception handler (may be _remove_activation_entry!)
1492 
1493   // If the exception is not handled in the current frame the frame is
1494   // removed and the exception is rethrown (i.e. exception
1495   // continuation is _rethrow_exception).
1496   //
1497   // Note: At this point the bci is still the bxi for the instruction
1498   // which caused the exception and the expression stack is
1499   // empty. Thus, for any VM calls at this point, GC will find a legal
1500   // oop map (with empty expression stack).
1501 
1502   //
1503   // JVMTI PopFrame support
1504   //
1505 
1506   Interpreter::_remove_activation_preserving_args_entry = __ pc();
1507   __ empty_expression_stack();
1508   // Set the popframe_processing bit in pending_popframe_condition
1509   // indicating that we are currently handling popframe, so that
1510   // call_VMs that may happen later do not trigger new popframe
1511   // handling cycles.
1512   __ lwu(x13, Address(xthread, JavaThread::popframe_condition_offset()));
1513   __ ori(x13, x13, JavaThread::popframe_processing_bit);
1514   __ sw(x13, Address(xthread, JavaThread::popframe_condition_offset()));
1515 
1516   {
1517     // Check to see whether we are returning to a deoptimized frame.
1518     // (The PopFrame call ensures that the caller of the popped frame is
1519     // either interpreted or compiled and deoptimizes it if compiled.)
1520     // In this case, we can't call dispatch_next() after the frame is
1521     // popped, but instead must save the incoming arguments and restore
1522     // them after deoptimization has occurred.
1523     //
1524     // Note that we don't compare the return PC against the
1525     // deoptimization blob's unpack entry because of the presence of
1526     // adapter frames in C2.
1527     Label caller_not_deoptimized;
1528     __ ld(c_rarg1, Address(fp, frame::return_addr_offset * wordSize));
1529     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::interpreter_contains), c_rarg1);
1530     __ bnez(x10, caller_not_deoptimized);
1531 
1532     // Compute size of arguments for saving when returning to
1533     // deoptimized caller
1534     __ get_method(x10);
1535     __ ld(x10, Address(x10, Method::const_offset()));
1536     __ load_unsigned_short(x10, Address(x10, in_bytes(ConstMethod::
1537                                                       size_of_parameters_offset())));
1538     __ slli(x10, x10, Interpreter::logStackElementSize);
1539     __ restore_locals();
1540     __ sub(xlocals, xlocals, x10);
1541     __ add(xlocals, xlocals, wordSize);
1542     // Save these arguments
1543     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1544                                            Deoptimization::
1545                                            popframe_preserve_args),
1546                           xthread, x10, xlocals);
1547 
1548     __ remove_activation(vtos,
1549                          /* throw_monitor_exception */ false,
1550                          /* install_monitor_exception */ false,
1551                          /* notify_jvmdi */ false);
1552 
1553     // Inform deoptimization that it is responsible for restoring
1554     // these arguments
1555     __ mv(t0, JavaThread::popframe_force_deopt_reexecution_bit);
1556     __ sw(t0, Address(xthread, JavaThread::popframe_condition_offset()));
1557 
1558     // Continue in deoptimization handler
1559     __ ret();
1560 
1561     __ bind(caller_not_deoptimized);
1562   }
1563 
1564   __ remove_activation(vtos,
1565                        /* throw_monitor_exception */ false,
1566                        /* install_monitor_exception */ false,
1567                        /* notify_jvmdi */ false);
1568 
1569   // Restore the last_sp and null it out
1570   __ ld(esp, Address(fp, frame::interpreter_frame_last_sp_offset * wordSize));
1571   __ sd(zr, Address(fp, frame::interpreter_frame_last_sp_offset * wordSize));
1572 
1573   __ restore_bcp();
1574   __ restore_locals();
1575   __ restore_constant_pool_cache();
1576   __ get_method(xmethod);
1577   __ get_dispatch();
1578 
1579   // The method data pointer was incremented already during
1580   // call profiling. We have to restore the mdp for the current bcp.
1581   if (ProfileInterpreter) {
1582     __ set_method_data_pointer_for_bcp();
1583   }
1584 
1585   // Clear the popframe condition flag
1586   __ sw(zr, Address(xthread, JavaThread::popframe_condition_offset()));
1587   assert(JavaThread::popframe_inactive == 0, "fix popframe_inactive");
1588 
1589 #if INCLUDE_JVMTI
1590   {
1591     Label L_done;
1592 
1593     __ lbu(t0, Address(xbcp, 0));
1594     __ li(t1, Bytecodes::_invokestatic);
1595     __ bne(t1, t0, L_done);
1596 
1597     // The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.
1598     // Detect such a case in the InterpreterRuntime function and return the member name argument,or NULL.
1599 
1600     __ ld(c_rarg0, Address(xlocals, 0));
1601     __ call_VM(x10, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null),c_rarg0, xmethod, xbcp);
1602 
1603     __ beqz(x10, L_done);
1604 
1605     __ sd(x10, Address(esp, 0));
1606     __ bind(L_done);
1607   }
1608 #endif // INCLUDE_JVMTI
1609 
1610   // Restore machine SP
1611   __ ld(t0, Address(xmethod, Method::const_offset()));
1612   __ lhu(t0, Address(t0, ConstMethod::max_stack_offset()));
1613   __ add(t0, t0, frame::interpreter_frame_monitor_size() + 4);
1614   __ ld(t1, Address(fp, frame::interpreter_frame_initial_sp_offset * wordSize));
1615   __ slliw(t0, t0, 3);
1616   __ sub(t0, t1, t0);
1617   __ andi(sp, t0, -16);
1618 
1619   __ dispatch_next(vtos);
1620   // end of PopFrame support
1621 
1622   Interpreter::_remove_activation_entry = __ pc();
1623 
1624   // preserve exception over this code sequence
1625   __ pop_ptr(x10);
1626   __ sd(x10, Address(xthread, JavaThread::vm_result_offset()));
1627   // remove the activation (without doing throws on illegalMonitorExceptions)
1628   __ remove_activation(vtos, false, true, false);
1629   // restore exception
1630   __ get_vm_result(x10, xthread);
1631 
1632   // In between activations - previous activation type unknown yet
1633   // compute continuation point - the continuation point expects the
1634   // following registers set up:
1635   //
1636   // x10: exception
1637   // ra: return address/pc that threw exception
1638   // sp: expression stack of caller
1639   // fp: fp of caller
1640   // FIXME: There's no point saving ra here because VM calls don't trash it
1641   __ sub(sp, sp, 2 * wordSize);
1642   __ sd(x10, Address(sp, 0));                   // save exception
1643   __ sd(ra, Address(sp, wordSize));             // save return address
1644   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1645                                          SharedRuntime::exception_handler_for_return_address),
1646                         xthread, ra);
1647   __ mv(x11, x10);                              // save exception handler
1648   __ ld(x10, Address(sp, 0));                   // restore exception
1649   __ ld(ra, Address(sp, wordSize));             // restore return address
1650   __ add(sp, sp, 2 * wordSize);
1651   // We might be returning to a deopt handler that expects x13 to
1652   // contain the exception pc
1653   __ mv(x13, ra);
1654   // Note that an "issuing PC" is actually the next PC after the call
1655   __ jr(x11);                                   // jump to exception
1656                                                 // handler of caller
1657 }
1658 
1659 //
1660 // JVMTI ForceEarlyReturn support
1661 //
1662 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state)  {
1663   address entry = __ pc();
1664 
1665   __ restore_bcp();
1666   __ restore_locals();
1667   __ empty_expression_stack();
1668   __ load_earlyret_value(state);
1669 
1670   __ ld(t0, Address(xthread, JavaThread::jvmti_thread_state_offset()));
1671   Address cond_addr(t0, JvmtiThreadState::earlyret_state_offset());
1672 
1673   // Clear the earlyret state
1674   assert(JvmtiThreadState::earlyret_inactive == 0, "should be");
1675   __ sd(zr, cond_addr);
1676 
1677   __ remove_activation(state,
1678                        false, /* throw_monitor_exception */
1679                        false, /* install_monitor_exception */
1680                        true); /* notify_jvmdi */
1681   __ ret();
1682 
1683   return entry;
1684 }
1685 // end of ForceEarlyReturn support
1686 
1687 //-----------------------------------------------------------------------------
1688 // Helper for vtos entry point generation
1689 
1690 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,
1691                                                          address& bep,
1692                                                          address& cep,
1693                                                          address& sep,
1694                                                          address& aep,
1695                                                          address& iep,
1696                                                          address& lep,
1697                                                          address& fep,
1698                                                          address& dep,
1699                                                          address& vep) {
1700   assert(t != NULL && t->is_valid() && t->tos_in() == vtos, "illegal template");
1701   Label L;
1702   aep = __ pc();  __ push_ptr();  __ j(L);
1703   fep = __ pc();  __ push_f();    __ j(L);
1704   dep = __ pc();  __ push_d();    __ j(L);
1705   lep = __ pc();  __ push_l();    __ j(L);
1706   bep = cep = sep =
1707   iep = __ pc();  __ push_i();
1708   vep = __ pc();
1709   __ bind(L);
1710   generate_and_dispatch(t);
1711 }
1712 
1713 //-----------------------------------------------------------------------------
1714 
1715 // Non-product code
1716 #ifndef PRODUCT
1717 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1718   address entry = __ pc();
1719 
1720   __ push_reg(ra);
1721   __ push(state);
1722   __ push_reg(RegSet::range(x10, x17) + RegSet::range(x5, x7) + RegSet::range(x28, x31), sp);
1723   __ mv(c_rarg2, x10);  // Pass itos
1724   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode), c_rarg1, c_rarg2, c_rarg3);
1725   __ pop_reg(RegSet::range(x10, x17) + RegSet::range(x5, x7) + RegSet::range(x28, x31), sp);
1726   __ pop(state);
1727   __ pop_reg(ra);
1728   __ ret();                                   // return from result handler
1729 
1730   return entry;
1731 }
1732 
1733 void TemplateInterpreterGenerator::count_bytecode() {
1734   __ push_reg(t0);
1735   __ push_reg(x10);
1736   __ mv(x10, (address) &BytecodeCounter::_counter_value);
1737   __ li(t0, 1);
1738   __ amoadd_d(zr, x10, t0, Assembler::aqrl);
1739   __ pop_reg(x10);
1740   __ pop_reg(t0);
1741 }
1742 
1743 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) { ; }
1744 
1745 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) { ; }
1746 
1747 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
1748   // Call a little run-time stub to avoid blow-up for each bytecode.
1749   // The run-time runtime saves the right registers, depending on
1750   // the tosca in-state for the given template.
1751 
1752   assert(Interpreter::trace_code(t->tos_in()) != NULL, "entry must have been generated");
1753   __ jal(Interpreter::trace_code(t->tos_in()));
1754   __ reinit_heapbase();
1755 }
1756 
1757 void TemplateInterpreterGenerator::stop_interpreter_at() {
1758   Label L;
1759   __ push_reg(t0);
1760   __ mv(t0, (address) &BytecodeCounter::_counter_value);
1761   __ ld(t0, Address(t0));
1762   __ mv(t1, StopInterpreterAt);
1763   __ bne(t0, t1, L);
1764   __ ebreak();
1765   __ bind(L);
1766   __ pop_reg(t0);
1767 }
1768 
1769 #endif // !PRODUCT