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