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 "gc/shared/barrierSetAssembler.hpp"
29 #include "interpreter/bytecodeHistogram.hpp"
30 #include "interpreter/interpreter.hpp"
31 #include "interpreter/interpreterRuntime.hpp"
32 #include "interpreter/interp_masm.hpp"
33 #include "interpreter/templateInterpreterGenerator.hpp"
34 #include "interpreter/templateTable.hpp"
35 #include "interpreter/bytecodeTracer.hpp"
36 #include "memory/resourceArea.hpp"
37 #include "oops/arrayOop.hpp"
38 #include "oops/methodData.hpp"
39 #include "oops/method.hpp"
40 #include "oops/oop.inline.hpp"
41 #include "prims/jvmtiExport.hpp"
42 #include "prims/jvmtiThreadState.hpp"
43 #include "runtime/arguments.hpp"
44 #include "runtime/deoptimization.hpp"
45 #include "runtime/frame.inline.hpp"
46 #include "runtime/sharedRuntime.hpp"
47 #include "runtime/stubRoutines.hpp"
48 #include "runtime/synchronizer.hpp"
49 #include "runtime/timer.hpp"
50 #include "runtime/vframeArray.hpp"
51 #include "utilities/debug.hpp"
52 #include "utilities/macros.hpp"
53 #include <sys/types.h>
54 
55 #ifndef PRODUCT
56 #include "oops/method.hpp"
57 #endif // !PRODUCT
58 
59 // Size of interpreter code.  Increase if too small.  Interpreter will
60 // fail with a guarantee ("not enough space for interpreter generation");
61 // if too small.
62 // Run with +PrintInterpreter to get the VM to print out the size.
63 // Max size with JVMTI
64 int TemplateInterpreter::InterpreterCodeSize = 200 * 1024;
65 
66 #define __ _masm->
67 
68 //-----------------------------------------------------------------------------
69 
70 extern "C" void entry(CodeBuffer*);
71 
72 //-----------------------------------------------------------------------------
73 
generate_slow_signature_handler()74 address TemplateInterpreterGenerator::generate_slow_signature_handler() {
75   address entry = __ pc();
76 
77   __ andr(esp, esp, -16);
78   __ mov(c_rarg3, esp);
79   // rmethod
80   // rlocals
81   // c_rarg3: first stack arg - wordSize
82 
83   // adjust sp
84   __ sub(sp, c_rarg3, 18 * wordSize);
85   __ str(lr, Address(__ pre(sp, -2 * wordSize)));
86   __ call_VM(noreg,
87              CAST_FROM_FN_PTR(address,
88                               InterpreterRuntime::slow_signature_handler),
89              rmethod, rlocals, c_rarg3);
90 
91   // r0: result handler
92 
93   // Stack layout:
94   // rsp: 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 LR
106   __ ldr(lr, Address(__ post(sp, 2 * wordSize)));
107 
108   // Do FP first so we can use c_rarg3 as temp
109   __ ldrw(c_rarg3, Address(sp, 9 * wordSize)); // float/double identifiers
110 
111   for (int i = 0; i < Argument::n_float_register_parameters_c; i++) {
112     const FloatRegister r = as_FloatRegister(i);
113 
114     Label d, done;
115 
116     __ tbnz(c_rarg3, i, d);
117     __ ldrs(r, Address(sp, (10 + i) * wordSize));
118     __ b(done);
119     __ bind(d);
120     __ ldrd(r, Address(sp, (10 + i) * wordSize));
121     __ bind(done);
122   }
123 
124   // c_rarg0 contains the result from the call of
125   // InterpreterRuntime::slow_signature_handler so we don't touch it
126   // here.  It will be loaded with the JNIEnv* later.
127   __ ldr(c_rarg1, Address(sp, 1 * wordSize));
128   for (int i = c_rarg2->encoding(); i <= c_rarg7->encoding(); i += 2) {
129     Register rm = as_Register(i), rn = as_Register(i+1);
130     __ ldp(rm, rn, Address(sp, i * wordSize));
131   }
132 
133   __ add(sp, sp, 18 * wordSize);
134   __ ret(lr);
135 
136   return entry;
137 }
138 
139 
140 //
141 // Various method entries
142 //
143 
generate_math_entry(AbstractInterpreter::MethodKind kind)144 address TemplateInterpreterGenerator::generate_math_entry(AbstractInterpreter::MethodKind kind) {
145   // rmethod: Method*
146   // r13: sender sp
147   // esp: args
148 
149   if (!InlineIntrinsics) return NULL; // Generate a vanilla entry
150 
151   // These don't need a safepoint check because they aren't virtually
152   // callable. We won't enter these intrinsics from compiled code.
153   // If in the future we added an intrinsic which was virtually callable
154   // we'd have to worry about how to safepoint so that this code is used.
155 
156   // mathematical functions inlined by compiler
157   // (interpreter must provide identical implementation
158   // in order to avoid monotonicity bugs when switching
159   // from interpreter to compiler in the middle of some
160   // computation)
161   //
162   // stack:
163   //        [ arg ] <-- esp
164   //        [ arg ]
165   // retaddr in lr
166 
167   address entry_point = NULL;
168   Register continuation = lr;
169   switch (kind) {
170   case Interpreter::java_lang_math_abs:
171     entry_point = __ pc();
172     __ ldrd(v0, Address(esp));
173     __ fabsd(v0, v0);
174     __ mov(sp, r13); // Restore caller's SP
175     break;
176   case Interpreter::java_lang_math_sqrt:
177     entry_point = __ pc();
178     __ ldrd(v0, Address(esp));
179     __ fsqrtd(v0, v0);
180     __ mov(sp, r13);
181     break;
182   case Interpreter::java_lang_math_sin :
183   case Interpreter::java_lang_math_cos :
184   case Interpreter::java_lang_math_tan :
185   case Interpreter::java_lang_math_log :
186   case Interpreter::java_lang_math_log10 :
187   case Interpreter::java_lang_math_exp :
188     entry_point = __ pc();
189     __ ldrd(v0, Address(esp));
190     __ mov(sp, r13);
191     __ mov(r19, lr);
192     continuation = r19;  // The first callee-saved register
193     generate_transcendental_entry(kind, 1);
194     break;
195   case Interpreter::java_lang_math_pow :
196     entry_point = __ pc();
197     __ mov(r19, lr);
198     continuation = r19;
199     __ ldrd(v0, Address(esp, 2 * Interpreter::stackElementSize));
200     __ ldrd(v1, Address(esp));
201     __ mov(sp, r13);
202     generate_transcendental_entry(kind, 2);
203     break;
204   case Interpreter::java_lang_math_fmaD :
205     if (UseFMA) {
206       entry_point = __ pc();
207       __ ldrd(v0, Address(esp, 4 * Interpreter::stackElementSize));
208       __ ldrd(v1, Address(esp, 2 * Interpreter::stackElementSize));
209       __ ldrd(v2, Address(esp));
210       __ fmaddd(v0, v0, v1, v2);
211       __ mov(sp, r13); // Restore caller's SP
212     }
213     break;
214   case Interpreter::java_lang_math_fmaF :
215     if (UseFMA) {
216       entry_point = __ pc();
217       __ ldrs(v0, Address(esp, 2 * Interpreter::stackElementSize));
218       __ ldrs(v1, Address(esp, Interpreter::stackElementSize));
219       __ ldrs(v2, Address(esp));
220       __ fmadds(v0, v0, v1, v2);
221       __ mov(sp, r13); // Restore caller's SP
222     }
223     break;
224   default:
225     ;
226   }
227   if (entry_point) {
228     __ br(continuation);
229   }
230 
231   return entry_point;
232 }
233 
234   // double trigonometrics and transcendentals
235   // static jdouble dsin(jdouble x);
236   // static jdouble dcos(jdouble x);
237   // static jdouble dtan(jdouble x);
238   // static jdouble dlog(jdouble x);
239   // static jdouble dlog10(jdouble x);
240   // static jdouble dexp(jdouble x);
241   // static jdouble dpow(jdouble x, jdouble y);
242 
generate_transcendental_entry(AbstractInterpreter::MethodKind kind,int fpargs)243 void TemplateInterpreterGenerator::generate_transcendental_entry(AbstractInterpreter::MethodKind kind, int fpargs) {
244   address fn;
245   switch (kind) {
246   case Interpreter::java_lang_math_sin :
247     if (StubRoutines::dsin() == NULL) {
248       fn = CAST_FROM_FN_PTR(address, SharedRuntime::dsin);
249     } else {
250       fn = CAST_FROM_FN_PTR(address, StubRoutines::dsin());
251     }
252     break;
253   case Interpreter::java_lang_math_cos :
254     if (StubRoutines::dcos() == NULL) {
255       fn = CAST_FROM_FN_PTR(address, SharedRuntime::dcos);
256     } else {
257       fn = CAST_FROM_FN_PTR(address, StubRoutines::dcos());
258     }
259     break;
260   case Interpreter::java_lang_math_tan :
261     if (StubRoutines::dtan() == NULL) {
262       fn = CAST_FROM_FN_PTR(address, SharedRuntime::dtan);
263     } else {
264       fn = CAST_FROM_FN_PTR(address, StubRoutines::dtan());
265     }
266     break;
267   case Interpreter::java_lang_math_log :
268     if (StubRoutines::dlog() == NULL) {
269       fn = CAST_FROM_FN_PTR(address, SharedRuntime::dlog);
270     } else {
271       fn = CAST_FROM_FN_PTR(address, StubRoutines::dlog());
272     }
273     break;
274   case Interpreter::java_lang_math_log10 :
275     if (StubRoutines::dlog10() == NULL) {
276       fn = CAST_FROM_FN_PTR(address, SharedRuntime::dlog10);
277     } else {
278       fn = CAST_FROM_FN_PTR(address, StubRoutines::dlog10());
279     }
280     break;
281   case Interpreter::java_lang_math_exp :
282     if (StubRoutines::dexp() == NULL) {
283       fn = CAST_FROM_FN_PTR(address, SharedRuntime::dexp);
284     } else {
285       fn = CAST_FROM_FN_PTR(address, StubRoutines::dexp());
286     }
287     break;
288   case Interpreter::java_lang_math_pow :
289     fpargs = 2;
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   }
828 
829   // add space for monitor & lock
830   __ sub(sp, sp, entry_size); // add space for a monitor entry
831   __ sub(esp, esp, entry_size);
832   __ mov(rscratch1, esp);
833   __ str(rscratch1, monitor_block_top);  // set new monitor block top
834   // store object
835   __ str(r0, Address(esp, BasicObjectLock::obj_offset_in_bytes()));
836   __ mov(c_rarg1, esp); // object address
837   __ lock_object(c_rarg1);
838 }
839 
840 // Generate a fixed interpreter frame. This is identical setup for
841 // interpreted methods and for native methods hence the shared code.
842 //
843 // Args:
844 //      lr: return address
845 //      rmethod: Method*
846 //      rlocals: pointer to locals
847 //      rcpool: cp cache
848 //      stack_pointer: previous sp
generate_fixed_frame(bool native_call)849 void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call) {
850   // initialize fixed part of activation frame
851   if (native_call) {
852     __ sub(esp, sp, 14 *  wordSize);
853     __ mov(rbcp, zr);
854     __ stp(esp, zr, Address(__ pre(sp, -14 * wordSize)));
855     // add 2 zero-initialized slots for native calls
856     __ stp(zr, zr, Address(sp, 12 * wordSize));
857   } else {
858     __ sub(esp, sp, 12 *  wordSize);
859     __ ldr(rscratch1, Address(rmethod, Method::const_offset()));      // get ConstMethod
860     __ add(rbcp, rscratch1, in_bytes(ConstMethod::codes_offset())); // get codebase
861     __ stp(esp, rbcp, Address(__ pre(sp, -12 * wordSize)));
862   }
863 
864   if (ProfileInterpreter) {
865     Label method_data_continue;
866     __ ldr(rscratch1, Address(rmethod, Method::method_data_offset()));
867     __ cbz(rscratch1, method_data_continue);
868     __ lea(rscratch1, Address(rscratch1, in_bytes(MethodData::data_offset())));
869     __ bind(method_data_continue);
870     __ stp(rscratch1, rmethod, Address(sp, 6 * wordSize));  // save Method* and mdp (method data pointer)
871   } else {
872     __ stp(zr, rmethod, Address(sp, 6 * wordSize));        // save Method* (no mdp)
873   }
874 
875   // Get mirror and store it in the frame as GC root for this Method*
876 #if INCLUDE_SHENANDOAHGC
877   if (UseShenandoahGC) {
878     __ load_mirror(r10, rmethod);
879     __ stp(r10, zr, Address(sp, 4 * wordSize));
880   } else
881 #endif
882   {
883     __ load_mirror(rscratch1, rmethod);
884     __ stp(rscratch1, zr, Address(sp, 4 * wordSize));
885   }
886 
887   __ ldr(rcpool, Address(rmethod, Method::const_offset()));
888   __ ldr(rcpool, Address(rcpool, ConstMethod::constants_offset()));
889   __ ldr(rcpool, Address(rcpool, ConstantPool::cache_offset_in_bytes()));
890   __ stp(rlocals, rcpool, Address(sp, 2 * wordSize));
891 
892   __ stp(rfp, lr, Address(sp, 10 * wordSize));
893   __ lea(rfp, Address(sp, 10 * wordSize));
894 
895   // set sender sp
896   // leave last_sp as null
897   __ stp(zr, r13, Address(sp, 8 * wordSize));
898 
899   // Move SP out of the way
900   if (! native_call) {
901     __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
902     __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
903     __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size() + 2);
904     __ sub(rscratch1, sp, rscratch1, ext::uxtw, 3);
905     __ andr(sp, rscratch1, -16);
906   }
907 }
908 
909 // End of helpers
910 
911 // Various method entries
912 //------------------------------------------------------------------------------------------------------------------------
913 //
914 //
915 
916 // Method entry for java.lang.ref.Reference.get.
generate_Reference_get_entry(void)917 address TemplateInterpreterGenerator::generate_Reference_get_entry(void) {
918   // Code: _aload_0, _getfield, _areturn
919   // parameter size = 1
920   //
921   // The code that gets generated by this routine is split into 2 parts:
922   //    1. The "intrinsified" code for G1 (or any SATB based GC),
923   //    2. The slow path - which is an expansion of the regular method entry.
924   //
925   // Notes:-
926   // * In the G1 code we do not check whether we need to block for
927   //   a safepoint. If G1 is enabled then we must execute the specialized
928   //   code for Reference.get (except when the Reference object is null)
929   //   so that we can log the value in the referent field with an SATB
930   //   update buffer.
931   //   If the code for the getfield template is modified so that the
932   //   G1 pre-barrier code is executed when the current method is
933   //   Reference.get() then going through the normal method entry
934   //   will be fine.
935   // * The G1 code can, however, check the receiver object (the instance
936   //   of java.lang.Reference) and jump to the slow path if null. If the
937   //   Reference object is null then we obviously cannot fetch the referent
938   //   and so we don't need to call the G1 pre-barrier. Thus we can use the
939   //   regular method entry code to generate the NPE.
940   //
941   // This code is based on generate_accessor_entry.
942   //
943   // rmethod: Method*
944   // r13: senderSP must preserve for slow path, set SP to it on fast path
945 
946   // LR is live.  It must be saved around calls.
947 
948   address entry = __ pc();
949 
950   const int referent_offset = java_lang_ref_Reference::referent_offset;
951   guarantee(referent_offset > 0, "referent offset not initialized");
952 
953   Label slow_path;
954   const Register local_0 = c_rarg0;
955   // Check if local 0 != NULL
956   // If the receiver is null then it is OK to jump to the slow path.
957   __ ldr(local_0, Address(esp, 0));
958   __ cbz(local_0, slow_path);
959 
960   __ mov(r19, r13);   // Move senderSP to a callee-saved register
961 
962   // Load the value of the referent field.
963   const Address field_address(local_0, referent_offset);
964   BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
965   bs->load_at(_masm, IN_HEAP | ON_WEAK_OOP_REF, T_OBJECT, local_0, field_address, /*tmp1*/ rscratch2, /*tmp2*/ rscratch1);
966 
967   // areturn
968   __ andr(sp, r19, -16);  // done with stack
969   __ ret(lr);
970 
971   // generate a vanilla interpreter entry as the slow path
972   __ bind(slow_path);
973   __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::zerolocals));
974   return entry;
975 
976 }
977 
978 /**
979  * Method entry for static native methods:
980  *   int java.util.zip.CRC32.update(int crc, int b)
981  */
generate_CRC32_update_entry()982 address TemplateInterpreterGenerator::generate_CRC32_update_entry() {
983   if (UseCRC32Intrinsics) {
984     address entry = __ pc();
985 
986     // rmethod: Method*
987     // r13: senderSP must preserved for slow path
988     // esp: args
989 
990     Label slow_path;
991     // If we need a safepoint check, generate full interpreter entry.
992     __ safepoint_poll(slow_path);
993 
994     // We don't generate local frame and don't align stack because
995     // we call stub code and there is no safepoint on this path.
996 
997     // Load parameters
998     const Register crc = c_rarg0;  // crc
999     const Register val = c_rarg1;  // source java byte value
1000     const Register tbl = c_rarg2;  // scratch
1001 
1002     // Arguments are reversed on java expression stack
1003     __ ldrw(val, Address(esp, 0));              // byte value
1004     __ ldrw(crc, Address(esp, wordSize));       // Initial CRC
1005 
1006     uint64_t offset;
1007     __ adrp(tbl, ExternalAddress(StubRoutines::crc_table_addr()), offset);
1008     __ add(tbl, tbl, offset);
1009 
1010     __ mvnw(crc, crc); // ~crc
1011     __ update_byte_crc32(crc, val, tbl);
1012     __ mvnw(crc, crc); // ~crc
1013 
1014     // result in c_rarg0
1015 
1016     __ andr(sp, r13, -16);
1017     __ ret(lr);
1018 
1019     // generate a vanilla native entry as the slow path
1020     __ bind(slow_path);
1021     __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::native));
1022     return entry;
1023   }
1024   return NULL;
1025 }
1026 
1027 /**
1028  * Method entry for static native methods:
1029  *   int java.util.zip.CRC32.updateBytes(int crc, byte[] b, int off, int len)
1030  *   int java.util.zip.CRC32.updateByteBuffer(int crc, long buf, int off, int len)
1031  */
generate_CRC32_updateBytes_entry(AbstractInterpreter::MethodKind kind)1032 address TemplateInterpreterGenerator::generate_CRC32_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
1033   if (UseCRC32Intrinsics) {
1034     address entry = __ pc();
1035 
1036     // rmethod,: Method*
1037     // r13: senderSP must preserved for slow path
1038 
1039     Label slow_path;
1040     // If we need a safepoint check, generate full interpreter entry.
1041     __ safepoint_poll(slow_path);
1042 
1043     // We don't generate local frame and don't align stack because
1044     // we call stub code and there is no safepoint on this path.
1045 
1046     // Load parameters
1047     const Register crc = c_rarg0;  // crc
1048     const Register buf = c_rarg1;  // source java byte array address
1049     const Register len = c_rarg2;  // length
1050     const Register off = len;      // offset (never overlaps with 'len')
1051 
1052     // Arguments are reversed on java expression stack
1053     // Calculate address of start element
1054     if (kind == Interpreter::java_util_zip_CRC32_updateByteBuffer) {
1055       __ ldr(buf, Address(esp, 2*wordSize)); // long buf
1056       __ ldrw(off, Address(esp, wordSize)); // offset
1057       __ add(buf, buf, off); // + offset
1058       __ ldrw(crc,   Address(esp, 4*wordSize)); // Initial CRC
1059     } else {
1060       __ ldr(buf, Address(esp, 2*wordSize)); // byte[] array
1061       __ add(buf, buf, arrayOopDesc::base_offset_in_bytes(T_BYTE)); // + header size
1062       __ ldrw(off, Address(esp, wordSize)); // offset
1063       __ add(buf, buf, off); // + offset
1064       __ ldrw(crc,   Address(esp, 3*wordSize)); // Initial CRC
1065     }
1066     // Can now load 'len' since we're finished with 'off'
1067     __ ldrw(len, Address(esp, 0x0)); // Length
1068 
1069     __ andr(sp, r13, -16); // Restore the caller's SP
1070 
1071     // We are frameless so we can just jump to the stub.
1072     __ b(CAST_FROM_FN_PTR(address, StubRoutines::updateBytesCRC32()));
1073 
1074     // generate a vanilla native entry as the slow path
1075     __ bind(slow_path);
1076     __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::native));
1077     return entry;
1078   }
1079   return NULL;
1080 }
1081 
1082 /**
1083  * Method entry for intrinsic-candidate (non-native) methods:
1084  *   int java.util.zip.CRC32C.updateBytes(int crc, byte[] b, int off, int end)
1085  *   int java.util.zip.CRC32C.updateDirectByteBuffer(int crc, long buf, int off, int end)
1086  * Unlike CRC32, CRC32C does not have any methods marked as native
1087  * CRC32C also uses an "end" variable instead of the length variable CRC32 uses
1088  */
generate_CRC32C_updateBytes_entry(AbstractInterpreter::MethodKind kind)1089 address TemplateInterpreterGenerator::generate_CRC32C_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
1090   if (UseCRC32CIntrinsics) {
1091     address entry = __ pc();
1092 
1093     // Prepare jump to stub using parameters from the stack
1094     const Register crc = c_rarg0; // initial crc
1095     const Register buf = c_rarg1; // source java byte array address
1096     const Register len = c_rarg2; // len argument to the kernel
1097 
1098     const Register end = len; // index of last element to process
1099     const Register off = crc; // offset
1100 
1101     __ ldrw(end, Address(esp)); // int end
1102     __ ldrw(off, Address(esp, wordSize)); // int offset
1103     __ sub(len, end, off);
1104     __ ldr(buf, Address(esp, 2*wordSize)); // byte[] buf | long buf
1105     __ add(buf, buf, off); // + offset
1106     if (kind == Interpreter::java_util_zip_CRC32C_updateDirectByteBuffer) {
1107       __ ldrw(crc, Address(esp, 4*wordSize)); // long crc
1108     } else {
1109       __ add(buf, buf, arrayOopDesc::base_offset_in_bytes(T_BYTE)); // + header size
1110       __ ldrw(crc, Address(esp, 3*wordSize)); // long crc
1111     }
1112 
1113     __ andr(sp, r13, -16); // Restore the caller's SP
1114 
1115     // Jump to the stub.
1116     __ b(CAST_FROM_FN_PTR(address, StubRoutines::updateBytesCRC32C()));
1117 
1118     return entry;
1119   }
1120   return NULL;
1121 }
1122 
bang_stack_shadow_pages(bool native_call)1123 void TemplateInterpreterGenerator::bang_stack_shadow_pages(bool native_call) {
1124   // Bang each page in the shadow zone. We can't assume it's been done for
1125   // an interpreter frame with greater than a page of locals, so each page
1126   // needs to be checked.  Only true for non-native.
1127   if (UseStackBanging) {
1128     const int n_shadow_pages = JavaThread::stack_shadow_zone_size() / os::vm_page_size();
1129     const int start_page = native_call ? n_shadow_pages : 1;
1130     const int page_size = os::vm_page_size();
1131     for (int pages = start_page; pages <= n_shadow_pages ; pages++) {
1132       __ sub(rscratch2, sp, pages*page_size);
1133       __ str(zr, Address(rscratch2));
1134     }
1135   }
1136 }
1137 
1138 
1139 // Interpreter stub for calling a native method. (asm interpreter)
1140 // This sets up a somewhat different looking stack for calling the
1141 // native method than the typical interpreter frame setup.
generate_native_entry(bool synchronized)1142 address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
1143   // determine code generation flags
1144   bool inc_counter  = UseCompiler || CountCompiledCalls || LogTouchedMethods;
1145 
1146   // r1: Method*
1147   // rscratch1: sender sp
1148 
1149   address entry_point = __ pc();
1150 
1151   const Address constMethod       (rmethod, Method::const_offset());
1152   const Address access_flags      (rmethod, Method::access_flags_offset());
1153   const Address size_of_parameters(r2, ConstMethod::
1154                                        size_of_parameters_offset());
1155 
1156   // get parameter size (always needed)
1157   __ ldr(r2, constMethod);
1158   __ load_unsigned_short(r2, size_of_parameters);
1159 
1160   // Native calls don't need the stack size check since they have no
1161   // expression stack and the arguments are already on the stack and
1162   // we only add a handful of words to the stack.
1163 
1164   // rmethod: Method*
1165   // r2: size of parameters
1166   // rscratch1: sender sp
1167 
1168   // for natives the size of locals is zero
1169 
1170   // compute beginning of parameters (rlocals)
1171   __ add(rlocals, esp, r2, ext::uxtx, 3);
1172   __ add(rlocals, rlocals, -wordSize);
1173 
1174   // Pull SP back to minimum size: this avoids holes in the stack
1175   __ andr(sp, esp, -16);
1176 
1177   // initialize fixed part of activation frame
1178   generate_fixed_frame(true);
1179 
1180   // make sure method is native & not abstract
1181 #ifdef ASSERT
1182   __ ldrw(r0, access_flags);
1183   {
1184     Label L;
1185     __ tst(r0, JVM_ACC_NATIVE);
1186     __ br(Assembler::NE, L);
1187     __ stop("tried to execute non-native method as native");
1188     __ bind(L);
1189   }
1190   {
1191     Label L;
1192     __ tst(r0, JVM_ACC_ABSTRACT);
1193     __ br(Assembler::EQ, L);
1194     __ stop("tried to execute abstract method in interpreter");
1195     __ bind(L);
1196   }
1197 #endif
1198 
1199   // Since at this point in the method invocation the exception
1200   // handler would try to exit the monitor of synchronized methods
1201   // which hasn't been entered yet, we set the thread local variable
1202   // _do_not_unlock_if_synchronized to true. The remove_activation
1203   // will check this flag.
1204 
1205    const Address do_not_unlock_if_synchronized(rthread,
1206         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1207   __ mov(rscratch2, true);
1208   __ strb(rscratch2, do_not_unlock_if_synchronized);
1209 
1210   // increment invocation count & check for overflow
1211   Label invocation_counter_overflow;
1212   if (inc_counter) {
1213     generate_counter_incr(&invocation_counter_overflow, NULL, NULL);
1214   }
1215 
1216   Label continue_after_compile;
1217   __ bind(continue_after_compile);
1218 
1219   bang_stack_shadow_pages(true);
1220 
1221   // reset the _do_not_unlock_if_synchronized flag
1222   __ strb(zr, do_not_unlock_if_synchronized);
1223 
1224   // check for synchronized methods
1225   // Must happen AFTER invocation_counter check and stack overflow check,
1226   // so method is not locked if overflows.
1227   if (synchronized) {
1228     lock_method();
1229   } else {
1230     // no synchronization necessary
1231 #ifdef ASSERT
1232     {
1233       Label L;
1234       __ ldrw(r0, access_flags);
1235       __ tst(r0, JVM_ACC_SYNCHRONIZED);
1236       __ br(Assembler::EQ, L);
1237       __ stop("method needs synchronization");
1238       __ bind(L);
1239     }
1240 #endif
1241   }
1242 
1243   // start execution
1244 #ifdef ASSERT
1245   {
1246     Label L;
1247     const Address monitor_block_top(rfp,
1248                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
1249     __ ldr(rscratch1, monitor_block_top);
1250     __ cmp(esp, rscratch1);
1251     __ br(Assembler::EQ, L);
1252     __ stop("broken stack frame setup in interpreter");
1253     __ bind(L);
1254   }
1255 #endif
1256 
1257   // jvmti support
1258   __ notify_method_entry();
1259 
1260   // work registers
1261   const Register t = r17;
1262   const Register result_handler = r19;
1263 
1264   // allocate space for parameters
1265   __ ldr(t, Address(rmethod, Method::const_offset()));
1266   __ load_unsigned_short(t, Address(t, ConstMethod::size_of_parameters_offset()));
1267 
1268   __ sub(rscratch1, esp, t, ext::uxtx, Interpreter::logStackElementSize);
1269   __ andr(sp, rscratch1, -16);
1270   __ mov(esp, rscratch1);
1271 
1272   // get signature handler
1273   {
1274     Label L;
1275     __ ldr(t, Address(rmethod, Method::signature_handler_offset()));
1276     __ cbnz(t, L);
1277     __ call_VM(noreg,
1278                CAST_FROM_FN_PTR(address,
1279                                 InterpreterRuntime::prepare_native_call),
1280                rmethod);
1281     __ ldr(t, Address(rmethod, Method::signature_handler_offset()));
1282     __ bind(L);
1283   }
1284 
1285   // call signature handler
1286   assert(InterpreterRuntime::SignatureHandlerGenerator::from() == rlocals,
1287          "adjust this code");
1288   assert(InterpreterRuntime::SignatureHandlerGenerator::to() == sp,
1289          "adjust this code");
1290   assert(InterpreterRuntime::SignatureHandlerGenerator::temp() == rscratch1,
1291           "adjust this code");
1292 
1293   // The generated handlers do not touch rmethod (the method).
1294   // However, large signatures cannot be cached and are generated
1295   // each time here.  The slow-path generator can do a GC on return,
1296   // so we must reload it after the call.
1297   __ blr(t);
1298   __ get_method(rmethod);        // slow path can do a GC, reload rmethod
1299 
1300 
1301   // result handler is in r0
1302   // set result handler
1303   __ mov(result_handler, r0);
1304   // pass mirror handle if static call
1305   {
1306     Label L;
1307     __ ldrw(t, Address(rmethod, Method::access_flags_offset()));
1308     __ tbz(t, exact_log2(JVM_ACC_STATIC), L);
1309     // get mirror
1310     __ load_mirror(t, rmethod);
1311     // copy mirror into activation frame
1312     __ str(t, Address(rfp, frame::interpreter_frame_oop_temp_offset * wordSize));
1313     // pass handle to mirror
1314     __ add(c_rarg1, rfp, frame::interpreter_frame_oop_temp_offset * wordSize);
1315     __ bind(L);
1316   }
1317 
1318   // get native function entry point in r10
1319   {
1320     Label L;
1321     __ ldr(r10, Address(rmethod, Method::native_function_offset()));
1322     address unsatisfied = (SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
1323     __ mov(rscratch2, unsatisfied);
1324     __ ldr(rscratch2, rscratch2);
1325     __ cmp(r10, rscratch2);
1326     __ br(Assembler::NE, L);
1327     __ call_VM(noreg,
1328                CAST_FROM_FN_PTR(address,
1329                                 InterpreterRuntime::prepare_native_call),
1330                rmethod);
1331     __ get_method(rmethod);
1332     __ ldr(r10, Address(rmethod, Method::native_function_offset()));
1333     __ bind(L);
1334   }
1335 
1336   // pass JNIEnv
1337   __ add(c_rarg0, rthread, in_bytes(JavaThread::jni_environment_offset()));
1338 
1339   // Set the last Java PC in the frame anchor to be the return address from
1340   // the call to the native method: this will allow the debugger to
1341   // generate an accurate stack trace.
1342   Label native_return;
1343   __ set_last_Java_frame(esp, rfp, native_return, rscratch1);
1344 
1345   // change thread state
1346 #ifdef ASSERT
1347   {
1348     Label L;
1349     __ ldrw(t, Address(rthread, JavaThread::thread_state_offset()));
1350     __ cmp(t, _thread_in_Java);
1351     __ br(Assembler::EQ, L);
1352     __ stop("Wrong thread state in native stub");
1353     __ bind(L);
1354   }
1355 #endif
1356 
1357   // Change state to native
1358   __ mov(rscratch1, _thread_in_native);
1359   __ lea(rscratch2, Address(rthread, JavaThread::thread_state_offset()));
1360   __ stlrw(rscratch1, rscratch2);
1361 
1362   // Call the native method.
1363   __ blr(r10);
1364   __ bind(native_return);
1365   __ maybe_isb();
1366   __ get_method(rmethod);
1367   // result potentially in r0 or v0
1368 
1369   // make room for the pushes we're about to do
1370   __ sub(rscratch1, esp, 4 * wordSize);
1371   __ andr(sp, rscratch1, -16);
1372 
1373   // NOTE: The order of these pushes is known to frame::interpreter_frame_result
1374   // in order to extract the result of a method call. If the order of these
1375   // pushes change or anything else is added to the stack then the code in
1376   // interpreter_frame_result must also change.
1377   __ push(dtos);
1378   __ push(ltos);
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   if (os::is_MP()) {
1386     if (UseMembar) {
1387       // Force this write out before the read below
1388       __ dmb(Assembler::ISH);
1389     } else {
1390       // Write serialization page so VM thread can do a pseudo remote membar.
1391       // We use the current thread pointer to calculate a thread specific
1392       // offset to write to within the page. This minimizes bus traffic
1393       // due to cache line collision.
1394       __ serialize_memory(rthread, rscratch2);
1395     }
1396   }
1397 
1398   // check for safepoint operation in progress and/or pending suspend requests
1399   {
1400     Label L, Continue;
1401     __ safepoint_poll_acquire(L);
1402     __ ldrw(rscratch2, Address(rthread, JavaThread::suspend_flags_offset()));
1403     __ cbz(rscratch2, Continue);
1404     __ bind(L);
1405 
1406     // Don't use call_VM as it will see a possible pending exception
1407     // and forward it and never return here preventing us from
1408     // clearing _last_native_pc down below. So we do a runtime call by
1409     // hand.
1410     //
1411     __ mov(c_rarg0, rthread);
1412     __ mov(rscratch2, CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans));
1413     __ blr(rscratch2);
1414     __ maybe_isb();
1415     __ get_method(rmethod);
1416     __ reinit_heapbase();
1417     __ bind(Continue);
1418   }
1419 
1420   // change thread state
1421   __ mov(rscratch1, _thread_in_Java);
1422   __ lea(rscratch2, Address(rthread, JavaThread::thread_state_offset()));
1423   __ stlrw(rscratch1, rscratch2);
1424 
1425   // reset_last_Java_frame
1426   __ reset_last_Java_frame(true);
1427 
1428   if (CheckJNICalls) {
1429     // clear_pending_jni_exception_check
1430     __ str(zr, Address(rthread, JavaThread::pending_jni_exception_check_fn_offset()));
1431   }
1432 
1433   // reset handle block
1434   __ ldr(t, Address(rthread, JavaThread::active_handles_offset()));
1435   __ str(zr, Address(t, JNIHandleBlock::top_offset_in_bytes()));
1436 
1437   // If result is an oop unbox and store it in frame where gc will see it
1438   // and result handler will pick it up
1439 
1440   {
1441     Label no_oop, not_weak, store_result;
1442     __ adr(t, ExternalAddress(AbstractInterpreter::result_handler(T_OBJECT)));
1443     __ cmp(t, result_handler);
1444     __ br(Assembler::NE, no_oop);
1445     // Unbox oop result, e.g. JNIHandles::resolve result.
1446     __ pop(ltos);
1447     __ resolve_jobject(r0, rthread, t);
1448     __ str(r0, Address(rfp, frame::interpreter_frame_oop_temp_offset*wordSize));
1449     // keep stack depth as expected by pushing oop which will eventually be discarded
1450     __ push(ltos);
1451     __ bind(no_oop);
1452   }
1453 
1454   {
1455     Label no_reguard;
1456     __ lea(rscratch1, Address(rthread, in_bytes(JavaThread::stack_guard_state_offset())));
1457     __ ldrw(rscratch1, Address(rscratch1));
1458     __ cmp(rscratch1, JavaThread::stack_guard_yellow_reserved_disabled);
1459     __ br(Assembler::NE, no_reguard);
1460 
1461     __ pusha(); // XXX only save smashed registers
1462     __ mov(c_rarg0, rthread);
1463     __ mov(rscratch2, CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages));
1464     __ blr(rscratch2);
1465     __ popa(); // XXX only restore smashed registers
1466     __ bind(no_reguard);
1467   }
1468 
1469   // The method register is junk from after the thread_in_native transition
1470   // until here.  Also can't call_VM until the bcp has been
1471   // restored.  Need bcp for throwing exception below so get it now.
1472   __ get_method(rmethod);
1473 
1474   // restore bcp to have legal interpreter frame, i.e., bci == 0 <=>
1475   // rbcp == code_base()
1476   __ ldr(rbcp, Address(rmethod, Method::const_offset()));   // get ConstMethod*
1477   __ add(rbcp, rbcp, in_bytes(ConstMethod::codes_offset()));          // get codebase
1478   // handle exceptions (exception handling will handle unlocking!)
1479   {
1480     Label L;
1481     __ ldr(rscratch1, Address(rthread, Thread::pending_exception_offset()));
1482     __ cbz(rscratch1, L);
1483     // Note: At some point we may want to unify this with the code
1484     // used in call_VM_base(); i.e., we should use the
1485     // StubRoutines::forward_exception code. For now this doesn't work
1486     // here because the rsp is not correctly set at this point.
1487     __ MacroAssembler::call_VM(noreg,
1488                                CAST_FROM_FN_PTR(address,
1489                                InterpreterRuntime::throw_pending_exception));
1490     __ should_not_reach_here();
1491     __ bind(L);
1492   }
1493 
1494   // do unlocking if necessary
1495   {
1496     Label L;
1497     __ ldrw(t, Address(rmethod, Method::access_flags_offset()));
1498     __ tbz(t, exact_log2(JVM_ACC_SYNCHRONIZED), L);
1499     // the code below should be shared with interpreter macro
1500     // assembler implementation
1501     {
1502       Label unlock;
1503       // BasicObjectLock will be first in list, since this is a
1504       // synchronized method. However, need to check that the object
1505       // has not been unlocked by an explicit monitorexit bytecode.
1506 
1507       // monitor expect in c_rarg1 for slow unlock path
1508       __ lea (c_rarg1, Address(rfp,   // address of first monitor
1509                                (intptr_t)(frame::interpreter_frame_initial_sp_offset *
1510                                           wordSize - sizeof(BasicObjectLock))));
1511 
1512       __ ldr(t, Address(c_rarg1, BasicObjectLock::obj_offset_in_bytes()));
1513       __ cbnz(t, unlock);
1514 
1515       // Entry already unlocked, need to throw exception
1516       __ MacroAssembler::call_VM(noreg,
1517                                  CAST_FROM_FN_PTR(address,
1518                    InterpreterRuntime::throw_illegal_monitor_state_exception));
1519       __ should_not_reach_here();
1520 
1521       __ bind(unlock);
1522       __ unlock_object(c_rarg1);
1523     }
1524     __ bind(L);
1525   }
1526 
1527   // jvmti support
1528   // Note: This must happen _after_ handling/throwing any exceptions since
1529   //       the exception handler code notifies the runtime of method exits
1530   //       too. If this happens before, method entry/exit notifications are
1531   //       not properly paired (was bug - gri 11/22/99).
1532   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1533 
1534   // restore potential result in r0:d0, call result handler to
1535   // restore potential result in ST0 & handle result
1536 
1537   __ pop(ltos);
1538   __ pop(dtos);
1539 
1540   __ blr(result_handler);
1541 
1542   // remove activation
1543   __ ldr(esp, Address(rfp,
1544                     frame::interpreter_frame_sender_sp_offset *
1545                     wordSize)); // get sender sp
1546   // remove frame anchor
1547   __ leave();
1548 
1549   // resture sender sp
1550   __ mov(sp, esp);
1551 
1552   __ ret(lr);
1553 
1554   if (inc_counter) {
1555     // Handle overflow of counter and compile method
1556     __ bind(invocation_counter_overflow);
1557     generate_counter_overflow(continue_after_compile);
1558   }
1559 
1560   return entry_point;
1561 }
1562 
1563 //
1564 // Generic interpreted method entry to (asm) interpreter
1565 //
generate_normal_entry(bool synchronized)1566 address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {
1567   // determine code generation flags
1568   bool inc_counter  = UseCompiler || CountCompiledCalls || LogTouchedMethods;
1569 
1570   // rscratch1: sender sp
1571   address entry_point = __ pc();
1572 
1573   const Address constMethod(rmethod, Method::const_offset());
1574   const Address access_flags(rmethod, Method::access_flags_offset());
1575   const Address size_of_parameters(r3,
1576                                    ConstMethod::size_of_parameters_offset());
1577   const Address size_of_locals(r3, ConstMethod::size_of_locals_offset());
1578 
1579   // get parameter size (always needed)
1580   // need to load the const method first
1581   __ ldr(r3, constMethod);
1582   __ load_unsigned_short(r2, size_of_parameters);
1583 
1584   // r2: size of parameters
1585 
1586   __ load_unsigned_short(r3, size_of_locals); // get size of locals in words
1587   __ sub(r3, r3, r2); // r3 = no. of additional locals
1588 
1589   // see if we've got enough room on the stack for locals plus overhead.
1590   generate_stack_overflow_check();
1591 
1592   // compute beginning of parameters (rlocals)
1593   __ add(rlocals, esp, r2, ext::uxtx, 3);
1594   __ sub(rlocals, rlocals, wordSize);
1595 
1596   __ mov(rscratch1, esp);
1597 
1598   // r3 - # of additional locals
1599   // allocate space for locals
1600   // explicitly initialize locals
1601   // Initializing memory allocated for locals in the same direction as
1602   // the stack grows to ensure page initialization order according
1603   // to windows-aarch64 stack page growth requirement (see
1604   // https://docs.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-160#stack)
1605   {
1606     Label exit, loop;
1607     __ ands(zr, r3, r3);
1608     __ br(Assembler::LE, exit); // do nothing if r3 <= 0
1609     __ bind(loop);
1610     __ str(zr, Address(__ pre(rscratch1, -wordSize)));
1611     __ sub(r3, r3, 1); // until everything initialized
1612     __ cbnz(r3, loop);
1613     __ bind(exit);
1614   }
1615 
1616   // Padding between locals and fixed part of activation frame to ensure
1617   // SP is always 16-byte aligned.
1618   __ andr(sp, rscratch1, -16);
1619 
1620   // And the base dispatch table
1621   __ get_dispatch();
1622 
1623   // initialize fixed part of activation frame
1624   generate_fixed_frame(false);
1625 
1626   // make sure method is not native & not abstract
1627 #ifdef ASSERT
1628   __ ldrw(r0, access_flags);
1629   {
1630     Label L;
1631     __ tst(r0, JVM_ACC_NATIVE);
1632     __ br(Assembler::EQ, L);
1633     __ stop("tried to execute native method as non-native");
1634     __ bind(L);
1635   }
1636  {
1637     Label L;
1638     __ tst(r0, JVM_ACC_ABSTRACT);
1639     __ br(Assembler::EQ, L);
1640     __ stop("tried to execute abstract method in interpreter");
1641     __ bind(L);
1642   }
1643 #endif
1644 
1645   // Since at this point in the method invocation the exception
1646   // handler would try to exit the monitor of synchronized methods
1647   // which hasn't been entered yet, we set the thread local variable
1648   // _do_not_unlock_if_synchronized to true. The remove_activation
1649   // will check this flag.
1650 
1651    const Address do_not_unlock_if_synchronized(rthread,
1652         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1653   __ mov(rscratch2, true);
1654   __ strb(rscratch2, do_not_unlock_if_synchronized);
1655 
1656   Register mdp = r3;
1657   __ profile_parameters_type(mdp, r1, r2);
1658 
1659   // increment invocation count & check for overflow
1660   Label invocation_counter_overflow;
1661   Label profile_method;
1662   Label profile_method_continue;
1663   if (inc_counter) {
1664     generate_counter_incr(&invocation_counter_overflow,
1665                           &profile_method,
1666                           &profile_method_continue);
1667     if (ProfileInterpreter) {
1668       __ bind(profile_method_continue);
1669     }
1670   }
1671 
1672   Label continue_after_compile;
1673   __ bind(continue_after_compile);
1674 
1675   bang_stack_shadow_pages(false);
1676 
1677   // reset the _do_not_unlock_if_synchronized flag
1678   __ strb(zr, do_not_unlock_if_synchronized);
1679 
1680   // check for synchronized methods
1681   // Must happen AFTER invocation_counter check and stack overflow check,
1682   // so method is not locked if overflows.
1683   if (synchronized) {
1684     // Allocate monitor and lock method
1685     lock_method();
1686   } else {
1687     // no synchronization necessary
1688 #ifdef ASSERT
1689     {
1690       Label L;
1691       __ ldrw(r0, access_flags);
1692       __ tst(r0, JVM_ACC_SYNCHRONIZED);
1693       __ br(Assembler::EQ, L);
1694       __ stop("method needs synchronization");
1695       __ bind(L);
1696     }
1697 #endif
1698   }
1699 
1700   // start execution
1701 #ifdef ASSERT
1702   {
1703     Label L;
1704      const Address monitor_block_top (rfp,
1705                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
1706     __ ldr(rscratch1, monitor_block_top);
1707     __ cmp(esp, rscratch1);
1708     __ br(Assembler::EQ, L);
1709     __ stop("broken stack frame setup in interpreter");
1710     __ bind(L);
1711   }
1712 #endif
1713 
1714   // jvmti support
1715   __ notify_method_entry();
1716 
1717   __ dispatch_next(vtos);
1718 
1719   // invocation counter overflow
1720   if (inc_counter) {
1721     if (ProfileInterpreter) {
1722       // We have decided to profile this method in the interpreter
1723       __ bind(profile_method);
1724       __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::profile_method));
1725       __ set_method_data_pointer_for_bcp();
1726       // don't think we need this
1727       __ get_method(r1);
1728       __ b(profile_method_continue);
1729     }
1730     // Handle overflow of counter and compile method
1731     __ bind(invocation_counter_overflow);
1732     generate_counter_overflow(continue_after_compile);
1733   }
1734 
1735   return entry_point;
1736 }
1737 
1738 //-----------------------------------------------------------------------------
1739 // Exceptions
1740 
generate_throw_exception()1741 void TemplateInterpreterGenerator::generate_throw_exception() {
1742   // Entry point in previous activation (i.e., if the caller was
1743   // interpreted)
1744   Interpreter::_rethrow_exception_entry = __ pc();
1745   // Restore sp to interpreter_frame_last_sp even though we are going
1746   // to empty the expression stack for the exception processing.
1747   __ str(zr, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1748   // r0: exception
1749   // r3: return address/pc that threw exception
1750   __ restore_bcp();    // rbcp points to call/send
1751   __ restore_locals();
1752   __ restore_constant_pool_cache();
1753   __ reinit_heapbase();  // restore rheapbase as heapbase.
1754   __ get_dispatch();
1755 
1756   // Entry point for exceptions thrown within interpreter code
1757   Interpreter::_throw_exception_entry = __ pc();
1758   // If we came here via a NullPointerException on the receiver of a
1759   // method, rmethod may be corrupt.
1760   __ get_method(rmethod);
1761   // expression stack is undefined here
1762   // r0: exception
1763   // rbcp: exception bcp
1764   __ verify_oop(r0);
1765   __ mov(c_rarg1, r0);
1766 
1767   // expression stack must be empty before entering the VM in case of
1768   // an exception
1769   __ empty_expression_stack();
1770   // find exception handler address and preserve exception oop
1771   __ call_VM(r3,
1772              CAST_FROM_FN_PTR(address,
1773                           InterpreterRuntime::exception_handler_for_exception),
1774              c_rarg1);
1775 
1776   // Calculate stack limit
1777   __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
1778   __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
1779   __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size() + 4);
1780   __ ldr(rscratch2,
1781          Address(rfp, frame::interpreter_frame_initial_sp_offset * wordSize));
1782   __ sub(rscratch1, rscratch2, rscratch1, ext::uxtx, 3);
1783   __ andr(sp, rscratch1, -16);
1784 
1785   // r0: exception handler entry point
1786   // r3: preserved exception oop
1787   // rbcp: bcp for exception handler
1788   __ push_ptr(r3); // push exception which is now the only value on the stack
1789   __ br(r0); // jump to exception handler (may be _remove_activation_entry!)
1790 
1791   // If the exception is not handled in the current frame the frame is
1792   // removed and the exception is rethrown (i.e. exception
1793   // continuation is _rethrow_exception).
1794   //
1795   // Note: At this point the bci is still the bxi for the instruction
1796   // which caused the exception and the expression stack is
1797   // empty. Thus, for any VM calls at this point, GC will find a legal
1798   // oop map (with empty expression stack).
1799 
1800   //
1801   // JVMTI PopFrame support
1802   //
1803 
1804   Interpreter::_remove_activation_preserving_args_entry = __ pc();
1805   __ empty_expression_stack();
1806   // Set the popframe_processing bit in pending_popframe_condition
1807   // indicating that we are currently handling popframe, so that
1808   // call_VMs that may happen later do not trigger new popframe
1809   // handling cycles.
1810   __ ldrw(r3, Address(rthread, JavaThread::popframe_condition_offset()));
1811   __ orr(r3, r3, JavaThread::popframe_processing_bit);
1812   __ strw(r3, Address(rthread, JavaThread::popframe_condition_offset()));
1813 
1814   {
1815     // Check to see whether we are returning to a deoptimized frame.
1816     // (The PopFrame call ensures that the caller of the popped frame is
1817     // either interpreted or compiled and deoptimizes it if compiled.)
1818     // In this case, we can't call dispatch_next() after the frame is
1819     // popped, but instead must save the incoming arguments and restore
1820     // them after deoptimization has occurred.
1821     //
1822     // Note that we don't compare the return PC against the
1823     // deoptimization blob's unpack entry because of the presence of
1824     // adapter frames in C2.
1825     Label caller_not_deoptimized;
1826     __ ldr(c_rarg1, Address(rfp, frame::return_addr_offset * wordSize));
1827     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1828                                InterpreterRuntime::interpreter_contains), c_rarg1);
1829     __ cbnz(r0, caller_not_deoptimized);
1830 
1831     // Compute size of arguments for saving when returning to
1832     // deoptimized caller
1833     __ get_method(r0);
1834     __ ldr(r0, Address(r0, Method::const_offset()));
1835     __ load_unsigned_short(r0, Address(r0, in_bytes(ConstMethod::
1836                                                     size_of_parameters_offset())));
1837     __ lsl(r0, r0, Interpreter::logStackElementSize);
1838     __ restore_locals(); // XXX do we need this?
1839     __ sub(rlocals, rlocals, r0);
1840     __ add(rlocals, rlocals, wordSize);
1841     // Save these arguments
1842     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1843                                            Deoptimization::
1844                                            popframe_preserve_args),
1845                           rthread, r0, rlocals);
1846 
1847     __ remove_activation(vtos,
1848                          /* throw_monitor_exception */ false,
1849                          /* install_monitor_exception */ false,
1850                          /* notify_jvmdi */ false);
1851 
1852     // Inform deoptimization that it is responsible for restoring
1853     // these arguments
1854     __ mov(rscratch1, JavaThread::popframe_force_deopt_reexecution_bit);
1855     __ strw(rscratch1, Address(rthread, JavaThread::popframe_condition_offset()));
1856 
1857     // Continue in deoptimization handler
1858     __ ret(lr);
1859 
1860     __ bind(caller_not_deoptimized);
1861   }
1862 
1863   __ remove_activation(vtos,
1864                        /* throw_monitor_exception */ false,
1865                        /* install_monitor_exception */ false,
1866                        /* notify_jvmdi */ false);
1867 
1868   // Restore the last_sp and null it out
1869   __ ldr(esp, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1870   __ str(zr, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1871 
1872   __ restore_bcp();
1873   __ restore_locals();
1874   __ restore_constant_pool_cache();
1875   __ get_method(rmethod);
1876   __ get_dispatch();
1877 
1878   // The method data pointer was incremented already during
1879   // call profiling. We have to restore the mdp for the current bcp.
1880   if (ProfileInterpreter) {
1881     __ set_method_data_pointer_for_bcp();
1882   }
1883 
1884   // Clear the popframe condition flag
1885   __ strw(zr, Address(rthread, JavaThread::popframe_condition_offset()));
1886   assert(JavaThread::popframe_inactive == 0, "fix popframe_inactive");
1887 
1888 #if INCLUDE_JVMTI
1889   {
1890     Label L_done;
1891 
1892     __ ldrb(rscratch1, Address(rbcp, 0));
1893     __ cmpw(rscratch1, Bytecodes::_invokestatic);
1894     __ br(Assembler::NE, L_done);
1895 
1896     // The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.
1897     // Detect such a case in the InterpreterRuntime function and return the member name argument, or NULL.
1898 
1899     __ ldr(c_rarg0, Address(rlocals, 0));
1900     __ call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null), c_rarg0, rmethod, rbcp);
1901 
1902     __ cbz(r0, L_done);
1903 
1904     __ str(r0, Address(esp, 0));
1905     __ bind(L_done);
1906   }
1907 #endif // INCLUDE_JVMTI
1908 
1909   // Restore machine SP
1910   __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
1911   __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
1912   __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size() + 4);
1913   __ ldr(rscratch2,
1914          Address(rfp, frame::interpreter_frame_initial_sp_offset * wordSize));
1915   __ sub(rscratch1, rscratch2, rscratch1, ext::uxtw, 3);
1916   __ andr(sp, rscratch1, -16);
1917 
1918   __ dispatch_next(vtos);
1919   // end of PopFrame support
1920 
1921   Interpreter::_remove_activation_entry = __ pc();
1922 
1923   // preserve exception over this code sequence
1924   __ pop_ptr(r0);
1925   __ str(r0, Address(rthread, JavaThread::vm_result_offset()));
1926   // remove the activation (without doing throws on illegalMonitorExceptions)
1927   __ remove_activation(vtos, false, true, false);
1928   // restore exception
1929   __ get_vm_result(r0, rthread);
1930 
1931   // In between activations - previous activation type unknown yet
1932   // compute continuation point - the continuation point expects the
1933   // following registers set up:
1934   //
1935   // r0: exception
1936   // lr: return address/pc that threw exception
1937   // esp: expression stack of caller
1938   // rfp: fp of caller
1939   __ stp(r0, lr, Address(__ pre(sp, -2 * wordSize)));  // save exception & return address
1940   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1941                           SharedRuntime::exception_handler_for_return_address),
1942                         rthread, lr);
1943   __ mov(r1, r0);                               // save exception handler
1944   __ ldp(r0, lr, Address(__ post(sp, 2 * wordSize)));  // restore exception & return address
1945   // We might be returning to a deopt handler that expects r3 to
1946   // contain the exception pc
1947   __ mov(r3, lr);
1948   // Note that an "issuing PC" is actually the next PC after the call
1949   __ br(r1);                                    // jump to exception
1950                                                 // handler of caller
1951 }
1952 
1953 
1954 //
1955 // JVMTI ForceEarlyReturn support
1956 //
generate_earlyret_entry_for(TosState state)1957 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1958   address entry = __ pc();
1959 
1960   __ restore_bcp();
1961   __ restore_locals();
1962   __ empty_expression_stack();
1963   __ load_earlyret_value(state);
1964 
1965   __ ldr(rscratch1, Address(rthread, JavaThread::jvmti_thread_state_offset()));
1966   Address cond_addr(rscratch1, JvmtiThreadState::earlyret_state_offset());
1967 
1968   // Clear the earlyret state
1969   assert(JvmtiThreadState::earlyret_inactive == 0, "should be");
1970   __ str(zr, cond_addr);
1971 
1972   __ remove_activation(state,
1973                        false, /* throw_monitor_exception */
1974                        false, /* install_monitor_exception */
1975                        true); /* notify_jvmdi */
1976   __ ret(lr);
1977 
1978   return entry;
1979 } // end of ForceEarlyReturn support
1980 
1981 
1982 
1983 //-----------------------------------------------------------------------------
1984 // Helper for vtos entry point generation
1985 
set_vtos_entry_points(Template * t,address & bep,address & cep,address & sep,address & aep,address & iep,address & lep,address & fep,address & dep,address & vep)1986 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,
1987                                                          address& bep,
1988                                                          address& cep,
1989                                                          address& sep,
1990                                                          address& aep,
1991                                                          address& iep,
1992                                                          address& lep,
1993                                                          address& fep,
1994                                                          address& dep,
1995                                                          address& vep) {
1996   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1997   Label L;
1998   aep = __ pc();  __ push_ptr();  __ b(L);
1999   fep = __ pc();  __ push_f();    __ b(L);
2000   dep = __ pc();  __ push_d();    __ b(L);
2001   lep = __ pc();  __ push_l();    __ b(L);
2002   bep = cep = sep =
2003   iep = __ pc();  __ push_i();
2004   vep = __ pc();
2005   __ bind(L);
2006   generate_and_dispatch(t);
2007 }
2008 
2009 //-----------------------------------------------------------------------------
2010 
2011 // Non-product code
2012 #ifndef PRODUCT
generate_trace_code(TosState state)2013 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
2014   address entry = __ pc();
2015 
2016   __ push(lr);
2017   __ push(state);
2018   __ push(RegSet::range(r0, r15), sp);
2019   __ mov(c_rarg2, r0);  // Pass itos
2020   __ call_VM(noreg,
2021              CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode),
2022              c_rarg1, c_rarg2, c_rarg3);
2023   __ pop(RegSet::range(r0, r15), sp);
2024   __ pop(state);
2025   __ pop(lr);
2026   __ ret(lr);                                   // return from result handler
2027 
2028   return entry;
2029 }
2030 
count_bytecode()2031 void TemplateInterpreterGenerator::count_bytecode() {
2032   Register rscratch3 = r0;
2033   __ push(rscratch1);
2034   __ push(rscratch2);
2035   __ push(rscratch3);
2036   __ mov(rscratch3, (address) &BytecodeCounter::_counter_value);
2037   __ atomic_add(noreg, 1, rscratch3);
2038   __ pop(rscratch3);
2039   __ pop(rscratch2);
2040   __ pop(rscratch1);
2041 }
2042 
histogram_bytecode(Template * t)2043 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) { ; }
2044 
histogram_bytecode_pair(Template * t)2045 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) { ; }
2046 
2047 
trace_bytecode(Template * t)2048 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
2049   // Call a little run-time stub to avoid blow-up for each bytecode.
2050   // The run-time runtime saves the right registers, depending on
2051   // the tosca in-state for the given template.
2052 
2053   assert(Interpreter::trace_code(t->tos_in()) != NULL,
2054          "entry must have been generated");
2055   __ bl(Interpreter::trace_code(t->tos_in()));
2056   __ reinit_heapbase();
2057 }
2058 
2059 
stop_interpreter_at()2060 void TemplateInterpreterGenerator::stop_interpreter_at() {
2061   Label L;
2062   __ push(rscratch1);
2063   __ mov(rscratch1, (address) &BytecodeCounter::_counter_value);
2064   __ ldr(rscratch1, Address(rscratch1));
2065   __ mov(rscratch2, StopInterpreterAt);
2066   __ cmpw(rscratch1, rscratch2);
2067   __ br(Assembler::NE, L);
2068   __ brk(0);
2069   __ bind(L);
2070   __ pop(rscratch1);
2071 }
2072 
2073 #endif // !PRODUCT
2074