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