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