1 /*
2 * Copyright (c) 2003, 2025, 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 "asm/macroAssembler.inline.hpp"
27 #include "compiler/compiler_globals.hpp"
28 #include "gc/shared/barrierSet.hpp"
29 #include "gc/shared/barrierSetAssembler.hpp"
30 #include "interp_masm_aarch64.hpp"
31 #include "interpreter/interpreter.hpp"
32 #include "interpreter/interpreterRuntime.hpp"
33 #include "logging/log.hpp"
34 #include "oops/arrayOop.hpp"
35 #include "oops/markWord.hpp"
36 #include "oops/method.hpp"
37 #include "oops/methodData.hpp"
38 #include "oops/resolvedFieldEntry.hpp"
39 #include "oops/resolvedIndyEntry.hpp"
40 #include "oops/resolvedMethodEntry.hpp"
41 #include "prims/jvmtiExport.hpp"
42 #include "prims/jvmtiThreadState.hpp"
43 #include "runtime/basicLock.hpp"
44 #include "runtime/frame.inline.hpp"
45 #include "runtime/javaThread.hpp"
46 #include "runtime/runtimeUpcalls.hpp"
47 #include "runtime/safepointMechanism.hpp"
48 #include "runtime/sharedRuntime.hpp"
49 #include "utilities/powerOfTwo.hpp"
50
51 void InterpreterMacroAssembler::narrow(Register result) {
52
53 // Get method->_constMethod->_result_type
54 ldr(rscratch1, Address(rfp, frame::interpreter_frame_method_offset * wordSize));
55 ldr(rscratch1, Address(rscratch1, Method::const_offset()));
56 ldrb(rscratch1, Address(rscratch1, ConstMethod::result_type_offset()));
57
58 Label done, notBool, notByte, notChar;
59
60 // common case first
61 cmpw(rscratch1, T_INT);
62 br(Assembler::EQ, done);
63
64 // mask integer result to narrower return type.
65 cmpw(rscratch1, T_BOOLEAN);
66 br(Assembler::NE, notBool);
67 andw(result, result, 0x1);
68 b(done);
69
70 bind(notBool);
71 cmpw(rscratch1, T_BYTE);
72 br(Assembler::NE, notByte);
73 sbfx(result, result, 0, 8);
74 b(done);
75
76 bind(notByte);
77 cmpw(rscratch1, T_CHAR);
78 br(Assembler::NE, notChar);
79 ubfx(result, result, 0, 16); // truncate upper 16 bits
80 b(done);
81
82 bind(notChar);
83 sbfx(result, result, 0, 16); // sign-extend short
84
85 // Nothing to do for T_INT
86 bind(done);
87 }
88
89 void InterpreterMacroAssembler::jump_to_entry(address entry) {
90 assert(entry, "Entry must have been generated by now");
91 b(entry);
92 }
93
94 void InterpreterMacroAssembler::check_and_handle_popframe(Register java_thread) {
95 if (JvmtiExport::can_pop_frame()) {
96 Label L;
97 // Initiate popframe handling only if it is not already being
98 // processed. If the flag has the popframe_processing bit set, it
99 // means that this code is called *during* popframe handling - we
100 // don't want to reenter.
101 // This method is only called just after the call into the vm in
102 // call_VM_base, so the arg registers are available.
103 ldrw(rscratch1, Address(rthread, JavaThread::popframe_condition_offset()));
104 tbz(rscratch1, exact_log2(JavaThread::popframe_pending_bit), L);
105 tbnz(rscratch1, exact_log2(JavaThread::popframe_processing_bit), L);
106 // Call Interpreter::remove_activation_preserving_args_entry() to get the
107 // address of the same-named entrypoint in the generated interpreter code.
108 call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_preserving_args_entry));
109 br(r0);
110 bind(L);
111 }
112 }
113
114
115 void InterpreterMacroAssembler::load_earlyret_value(TosState state) {
116 ldr(r2, Address(rthread, JavaThread::jvmti_thread_state_offset()));
117 const Address tos_addr(r2, JvmtiThreadState::earlyret_tos_offset());
118 const Address oop_addr(r2, JvmtiThreadState::earlyret_oop_offset());
119 const Address val_addr(r2, JvmtiThreadState::earlyret_value_offset());
120 switch (state) {
121 case atos: ldr(r0, oop_addr);
122 str(zr, oop_addr);
123 interp_verify_oop(r0, state); break;
124 case ltos: ldr(r0, val_addr); break;
125 case btos: // fall through
126 case ztos: // fall through
127 case ctos: // fall through
128 case stos: // fall through
129 case itos: ldrw(r0, val_addr); break;
130 case ftos: ldrs(v0, val_addr); break;
131 case dtos: ldrd(v0, val_addr); break;
132 case vtos: /* nothing to do */ break;
133 default : ShouldNotReachHere();
134 }
135 // Clean up tos value in the thread object
136 movw(rscratch1, (int) ilgl);
137 strw(rscratch1, tos_addr);
138 strw(zr, val_addr);
139 }
140
141
142 void InterpreterMacroAssembler::check_and_handle_earlyret(Register java_thread) {
143 if (JvmtiExport::can_force_early_return()) {
144 Label L;
145 ldr(rscratch1, Address(rthread, JavaThread::jvmti_thread_state_offset()));
146 cbz(rscratch1, L); // if (thread->jvmti_thread_state() == nullptr) exit;
147
148 // Initiate earlyret handling only if it is not already being processed.
149 // If the flag has the earlyret_processing bit set, it means that this code
150 // is called *during* earlyret handling - we don't want to reenter.
151 ldrw(rscratch1, Address(rscratch1, JvmtiThreadState::earlyret_state_offset()));
152 cmpw(rscratch1, JvmtiThreadState::earlyret_pending);
153 br(Assembler::NE, L);
154
155 // Call Interpreter::remove_activation_early_entry() to get the address of the
156 // same-named entrypoint in the generated interpreter code.
157 ldr(rscratch1, Address(rthread, JavaThread::jvmti_thread_state_offset()));
158 ldrw(rscratch1, Address(rscratch1, JvmtiThreadState::earlyret_tos_offset()));
159 call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_early_entry), rscratch1);
160 br(r0);
161 bind(L);
162 }
163 }
164
165 void InterpreterMacroAssembler::get_unsigned_2_byte_index_at_bcp(
166 Register reg,
167 int bcp_offset) {
168 assert(bcp_offset >= 0, "bcp is still pointing to start of bytecode");
169 ldrh(reg, Address(rbcp, bcp_offset));
170 rev16(reg, reg);
171 }
172
173 void InterpreterMacroAssembler::get_dispatch() {
174 uint64_t offset;
175 adrp(rdispatch, ExternalAddress((address)Interpreter::dispatch_table()), offset);
176 // Use add() here after ARDP, rather than lea().
177 // lea() does not generate anything if its offset is zero.
178 // However, relocs expect to find either an ADD or a load/store
179 // insn after an ADRP. add() always generates an ADD insn, even
180 // for add(Rn, Rn, 0).
181 add(rdispatch, rdispatch, offset);
182 }
183
184 void InterpreterMacroAssembler::get_cache_index_at_bcp(Register index,
185 int bcp_offset,
186 size_t index_size) {
187 assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
188 if (index_size == sizeof(u2)) {
189 load_unsigned_short(index, Address(rbcp, bcp_offset));
190 } else if (index_size == sizeof(u4)) {
191 // assert(EnableInvokeDynamic, "giant index used only for JSR 292");
192 ldrw(index, Address(rbcp, bcp_offset));
193 } else if (index_size == sizeof(u1)) {
194 load_unsigned_byte(index, Address(rbcp, bcp_offset));
195 } else {
196 ShouldNotReachHere();
197 }
198 }
199
200 void InterpreterMacroAssembler::get_method_counters(Register method,
201 Register mcs, Label& skip) {
202 Label has_counters;
203 ldr(mcs, Address(method, Method::method_counters_offset()));
204 cbnz(mcs, has_counters);
205 call_VM(noreg, CAST_FROM_FN_PTR(address,
206 InterpreterRuntime::build_method_counters), method);
207 ldr(mcs, Address(method, Method::method_counters_offset()));
208 cbz(mcs, skip); // No MethodCounters allocated, OutOfMemory
209 bind(has_counters);
210 }
211
212 // Load object from cpool->resolved_references(index)
213 void InterpreterMacroAssembler::load_resolved_reference_at_index(
214 Register result, Register index, Register tmp) {
215 assert_different_registers(result, index);
216
217 get_constant_pool(result);
218 // load pointer for resolved_references[] objArray
219 ldr(result, Address(result, ConstantPool::cache_offset()));
220 ldr(result, Address(result, ConstantPoolCache::resolved_references_offset()));
221 resolve_oop_handle(result, tmp, rscratch2);
222 // Add in the index
223 add(index, index, arrayOopDesc::base_offset_in_bytes(T_OBJECT) >> LogBytesPerHeapOop);
224 load_heap_oop(result, Address(result, index, Address::uxtw(LogBytesPerHeapOop)), tmp, rscratch2);
225 }
226
227 void InterpreterMacroAssembler::load_resolved_klass_at_offset(
228 Register cpool, Register index, Register klass, Register temp) {
229 add(temp, cpool, index, LSL, LogBytesPerWord);
230 ldrh(temp, Address(temp, sizeof(ConstantPool))); // temp = resolved_klass_index
231 ldr(klass, Address(cpool, ConstantPool::resolved_klasses_offset())); // klass = cpool->_resolved_klasses
232 add(klass, klass, temp, LSL, LogBytesPerWord);
233 ldr(klass, Address(klass, Array<Klass*>::base_offset_in_bytes()));
234 }
235
236 // Generate a subtype check: branch to ok_is_subtype if sub_klass is a
237 // subtype of super_klass.
238 //
239 // Args:
240 // r0: superklass
241 // Rsub_klass: subklass
242 //
243 // Kills:
244 // r2, r5
245 void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass,
246 Label& ok_is_subtype) {
247 assert(Rsub_klass != r0, "r0 holds superklass");
248 assert(Rsub_klass != r2, "r2 holds 2ndary super array length");
249 assert(Rsub_klass != r5, "r5 holds 2ndary super array scan ptr");
250
251 // Profile the not-null value's klass.
252 profile_typecheck(r2, Rsub_klass, r5); // blows r2, reloads r5
253
254 // Do the check.
255 check_klass_subtype(Rsub_klass, r0, r2, ok_is_subtype); // blows r2
256 }
257
258 // Java Expression Stack
259
260 void InterpreterMacroAssembler::pop_ptr(Register r) {
261 ldr(r, post(esp, wordSize));
262 }
263
264 void InterpreterMacroAssembler::pop_i(Register r) {
265 ldrw(r, post(esp, wordSize));
266 }
267
268 void InterpreterMacroAssembler::pop_l(Register r) {
269 ldr(r, post(esp, 2 * Interpreter::stackElementSize));
270 }
271
272 void InterpreterMacroAssembler::push_ptr(Register r) {
273 str(r, pre(esp, -wordSize));
274 }
275
276 void InterpreterMacroAssembler::push_i(Register r) {
277 str(r, pre(esp, -wordSize));
278 }
279
280 void InterpreterMacroAssembler::push_l(Register r) {
281 str(zr, pre(esp, -wordSize));
282 str(r, pre(esp, - wordSize));
283 }
284
285 void InterpreterMacroAssembler::pop_f(FloatRegister r) {
286 ldrs(r, post(esp, wordSize));
287 }
288
289 void InterpreterMacroAssembler::pop_d(FloatRegister r) {
290 ldrd(r, post(esp, 2 * Interpreter::stackElementSize));
291 }
292
293 void InterpreterMacroAssembler::push_f(FloatRegister r) {
294 strs(r, pre(esp, -wordSize));
295 }
296
297 void InterpreterMacroAssembler::push_d(FloatRegister r) {
298 strd(r, pre(esp, 2* -wordSize));
299 }
300
301 void InterpreterMacroAssembler::pop(TosState state) {
302 switch (state) {
303 case atos: pop_ptr(); break;
304 case btos:
305 case ztos:
306 case ctos:
307 case stos:
308 case itos: pop_i(); break;
309 case ltos: pop_l(); break;
310 case ftos: pop_f(); break;
311 case dtos: pop_d(); break;
312 case vtos: /* nothing to do */ break;
313 default: ShouldNotReachHere();
314 }
315 interp_verify_oop(r0, state);
316 }
317
318 void InterpreterMacroAssembler::push(TosState state) {
319 interp_verify_oop(r0, state);
320 switch (state) {
321 case atos: push_ptr(); break;
322 case btos:
323 case ztos:
324 case ctos:
325 case stos:
326 case itos: push_i(); break;
327 case ltos: push_l(); break;
328 case ftos: push_f(); break;
329 case dtos: push_d(); break;
330 case vtos: /* nothing to do */ break;
331 default : ShouldNotReachHere();
332 }
333 }
334
335 // Helpers for swap and dup
336 void InterpreterMacroAssembler::load_ptr(int n, Register val) {
337 ldr(val, Address(esp, Interpreter::expr_offset_in_bytes(n)));
338 }
339
340 void InterpreterMacroAssembler::store_ptr(int n, Register val) {
341 str(val, Address(esp, Interpreter::expr_offset_in_bytes(n)));
342 }
343
344 void InterpreterMacroAssembler::load_float(Address src) {
345 ldrs(v0, src);
346 }
347
348 void InterpreterMacroAssembler::load_double(Address src) {
349 ldrd(v0, src);
350 }
351
352 void InterpreterMacroAssembler::prepare_to_jump_from_interpreted() {
353 // set sender sp
354 mov(r19_sender_sp, sp);
355 // record last_sp
356 sub(rscratch1, esp, rfp);
357 asr(rscratch1, rscratch1, Interpreter::logStackElementSize);
358 str(rscratch1, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
359 }
360
361 // Jump to from_interpreted entry of a call unless single stepping is possible
362 // in this thread in which case we must call the i2i entry
363 void InterpreterMacroAssembler::jump_from_interpreted(Register method, Register temp) {
364 prepare_to_jump_from_interpreted();
365
366 if (JvmtiExport::can_post_interpreter_events()) {
367 Label run_compiled_code;
368 // JVMTI events, such as single-stepping, are implemented partly by avoiding running
369 // compiled code in threads for which the event is enabled. Check here for
370 // interp_only_mode if these events CAN be enabled.
371 ldrw(rscratch1, Address(rthread, JavaThread::interp_only_mode_offset()));
372 cbzw(rscratch1, run_compiled_code);
373 ldr(rscratch1, Address(method, Method::interpreter_entry_offset()));
374 br(rscratch1);
375 bind(run_compiled_code);
376 }
377
378 ldr(rscratch1, Address(method, Method::from_interpreted_offset()));
379 br(rscratch1);
380 }
381
382 // The following two routines provide a hook so that an implementation
383 // can schedule the dispatch in two parts. amd64 does not do this.
384 void InterpreterMacroAssembler::dispatch_prolog(TosState state, int step) {
385 }
386
387 void InterpreterMacroAssembler::dispatch_epilog(TosState state, int step) {
388 dispatch_next(state, step);
389 }
390
391 void InterpreterMacroAssembler::dispatch_base(TosState state,
392 address* table,
393 bool verifyoop,
394 bool generate_poll) {
395 if (VerifyActivationFrameSize) {
396 Label L;
397 sub(rscratch2, rfp, esp);
398 int min_frame_size = (frame::link_offset - frame::interpreter_frame_initial_sp_offset) * wordSize;
399 subs(rscratch2, rscratch2, min_frame_size);
400 br(Assembler::GE, L);
401 stop("broken stack frame");
402 bind(L);
403 }
404 if (verifyoop) {
405 interp_verify_oop(r0, state);
406 }
407
408 Label safepoint;
409 address* const safepoint_table = Interpreter::safept_table(state);
410 bool needs_thread_local_poll = generate_poll && table != safepoint_table;
411
412 if (needs_thread_local_poll) {
413 NOT_PRODUCT(block_comment("Thread-local Safepoint poll"));
414 ldr(rscratch2, Address(rthread, JavaThread::polling_word_offset()));
415 tbnz(rscratch2, exact_log2(SafepointMechanism::poll_bit()), safepoint);
416 }
417
418 if (table == Interpreter::dispatch_table(state)) {
419 addw(rscratch2, rscratch1, Interpreter::distance_from_dispatch_table(state));
420 ldr(rscratch2, Address(rdispatch, rscratch2, Address::uxtw(3)));
421 } else {
422 mov(rscratch2, (address)table);
423 ldr(rscratch2, Address(rscratch2, rscratch1, Address::uxtw(3)));
424 }
425 br(rscratch2);
426
427 if (needs_thread_local_poll) {
428 bind(safepoint);
429 lea(rscratch2, ExternalAddress((address)safepoint_table));
430 ldr(rscratch2, Address(rscratch2, rscratch1, Address::uxtw(3)));
431 br(rscratch2);
432 }
433 }
434
435 void InterpreterMacroAssembler::dispatch_only(TosState state, bool generate_poll) {
436 dispatch_base(state, Interpreter::dispatch_table(state), true, generate_poll);
437 }
438
439 void InterpreterMacroAssembler::dispatch_only_normal(TosState state) {
440 dispatch_base(state, Interpreter::normal_table(state));
441 }
442
443 void InterpreterMacroAssembler::dispatch_only_noverify(TosState state) {
444 dispatch_base(state, Interpreter::normal_table(state), false);
445 }
446
447
448 void InterpreterMacroAssembler::dispatch_next(TosState state, int step, bool generate_poll) {
449 // load next bytecode
450 ldrb(rscratch1, Address(pre(rbcp, step)));
451 dispatch_base(state, Interpreter::dispatch_table(state), /*verifyoop*/true, generate_poll);
452 }
453
454 void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) {
455 // load current bytecode
456 ldrb(rscratch1, Address(rbcp, 0));
457 dispatch_base(state, table);
458 }
459
460 // remove activation
461 //
462 // Unlock the receiver if this is a synchronized method.
463 // Unlock any Java monitors from synchronized blocks.
464 // Apply stack watermark barrier.
465 // Notify JVMTI.
466 // Remove the activation from the stack.
467 //
468 // If there are locked Java monitors
469 // If throw_monitor_exception
470 // throws IllegalMonitorStateException
471 // Else if install_monitor_exception
472 // installs IllegalMonitorStateException
473 // Else
474 // no error processing
475 void InterpreterMacroAssembler::remove_activation(TosState state,
476 bool throw_monitor_exception,
477 bool install_monitor_exception,
478 bool notify_jvmdi) {
479 // Note: Registers r3 xmm0 may be in use for the
480 // result check if synchronized method
481 Label unlocked, unlock, no_unlock;
482
483 // get the value of _do_not_unlock_if_synchronized into r3
484 const Address do_not_unlock_if_synchronized(rthread,
485 in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
486 ldrb(r3, do_not_unlock_if_synchronized);
487 strb(zr, do_not_unlock_if_synchronized); // reset the flag
488
489 // get method access flags
490 ldr(r1, Address(rfp, frame::interpreter_frame_method_offset * wordSize));
491 ldrh(r2, Address(r1, Method::access_flags_offset()));
492 tbz(r2, exact_log2(JVM_ACC_SYNCHRONIZED), unlocked);
493
494 // Don't unlock anything if the _do_not_unlock_if_synchronized flag
495 // is set.
496 cbnz(r3, no_unlock);
497
498 // unlock monitor
499 push(state); // save result
500
501 // BasicObjectLock will be first in list, since this is a
502 // synchronized method. However, need to check that the object has
503 // not been unlocked by an explicit monitorexit bytecode.
504 const Address monitor(rfp, frame::interpreter_frame_initial_sp_offset *
505 wordSize - (int) sizeof(BasicObjectLock));
506 // We use c_rarg1 so that if we go slow path it will be the correct
507 // register for unlock_object to pass to VM directly
508 lea(c_rarg1, monitor); // address of first monitor
509
510 ldr(r0, Address(c_rarg1, BasicObjectLock::obj_offset()));
511 cbnz(r0, unlock);
512
513 pop(state);
514 if (throw_monitor_exception) {
515 // Entry already unlocked, need to throw exception
516 call_VM(noreg, CAST_FROM_FN_PTR(address,
517 InterpreterRuntime::throw_illegal_monitor_state_exception));
518 should_not_reach_here();
519 } else {
520 // Monitor already unlocked during a stack unroll. If requested,
521 // install an illegal_monitor_state_exception. Continue with
522 // stack unrolling.
523 if (install_monitor_exception) {
524 call_VM(noreg, CAST_FROM_FN_PTR(address,
525 InterpreterRuntime::new_illegal_monitor_state_exception));
526 }
527 b(unlocked);
528 }
529
530 bind(unlock);
531 unlock_object(c_rarg1);
532 pop(state);
533
534 // Check that for block-structured locking (i.e., that all locked
535 // objects has been unlocked)
536 bind(unlocked);
537
538 // r0: Might contain return value
539
540 // Check that all monitors are unlocked
541 {
542 Label loop, exception, entry, restart;
543 const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
544 const Address monitor_block_top(
545 rfp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
546 const Address monitor_block_bot(
547 rfp, frame::interpreter_frame_initial_sp_offset * wordSize);
548
549 bind(restart);
550 // We use c_rarg1 so that if we go slow path it will be the correct
551 // register for unlock_object to pass to VM directly
552 ldr(c_rarg1, monitor_block_top); // derelativize pointer
553 lea(c_rarg1, Address(rfp, c_rarg1, Address::lsl(Interpreter::logStackElementSize)));
554 // c_rarg1 points to current entry, starting with top-most entry
555
556 lea(r19, monitor_block_bot); // points to word before bottom of
557 // monitor block
558 b(entry);
559
560 // Entry already locked, need to throw exception
561 bind(exception);
562
563 if (throw_monitor_exception) {
564 // Throw exception
565 MacroAssembler::call_VM(noreg,
566 CAST_FROM_FN_PTR(address, InterpreterRuntime::
567 throw_illegal_monitor_state_exception));
568 should_not_reach_here();
569 } else {
570 // Stack unrolling. Unlock object and install illegal_monitor_exception.
571 // Unlock does not block, so don't have to worry about the frame.
572 // We don't have to preserve c_rarg1 since we are going to throw an exception.
573
574 push(state);
575 unlock_object(c_rarg1);
576 pop(state);
577
578 if (install_monitor_exception) {
579 call_VM(noreg, CAST_FROM_FN_PTR(address,
580 InterpreterRuntime::
581 new_illegal_monitor_state_exception));
582 }
583
584 b(restart);
585 }
586
587 bind(loop);
588 // check if current entry is used
589 ldr(rscratch1, Address(c_rarg1, BasicObjectLock::obj_offset()));
590 cbnz(rscratch1, exception);
591
592 add(c_rarg1, c_rarg1, entry_size); // otherwise advance to next entry
593 bind(entry);
594 cmp(c_rarg1, r19); // check if bottom reached
595 br(Assembler::NE, loop); // if not at bottom then check this entry
596 }
597
598 bind(no_unlock);
599
600 JFR_ONLY(enter_jfr_critical_section();)
601
602 // The below poll is for the stack watermark barrier. It allows fixing up frames lazily,
603 // that would normally not be safe to use. Such bad returns into unsafe territory of
604 // the stack, will call InterpreterRuntime::at_unwind.
605 Label slow_path;
606 Label fast_path;
607 safepoint_poll(slow_path, true /* at_return */, false /* in_nmethod */);
608 br(Assembler::AL, fast_path);
609 bind(slow_path);
610 push(state);
611 set_last_Java_frame(esp, rfp, pc(), rscratch1);
612 super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::at_unwind), rthread);
613 reset_last_Java_frame(true);
614 pop(state);
615 bind(fast_path);
616
617 // JVMTI support. Make sure the safepoint poll test is issued prior.
618 if (notify_jvmdi) {
619 notify_method_exit(state, NotifyJVMTI); // preserve TOSCA
620 } else {
621 notify_method_exit(state, SkipNotifyJVMTI); // preserve TOSCA
622 }
623
624 // remove activation
625 // get sender esp
626 ldr(rscratch2,
627 Address(rfp, frame::interpreter_frame_sender_sp_offset * wordSize));
628 if (StackReservedPages > 0) {
629 // testing if reserved zone needs to be re-enabled
630 Label no_reserved_zone_enabling;
631
632 // check if already enabled - if so no re-enabling needed
633 assert(sizeof(StackOverflow::StackGuardState) == 4, "unexpected size");
634 ldrw(rscratch1, Address(rthread, JavaThread::stack_guard_state_offset()));
635 cmpw(rscratch1, (u1)StackOverflow::stack_guard_enabled);
636 br(Assembler::EQ, no_reserved_zone_enabling);
637
638 // look for an overflow into the stack reserved zone, i.e.
639 // interpreter_frame_sender_sp <= JavaThread::reserved_stack_activation
640 ldr(rscratch1, Address(rthread, JavaThread::reserved_stack_activation_offset()));
641 cmp(rscratch2, rscratch1);
642 br(Assembler::LS, no_reserved_zone_enabling);
643
644 JFR_ONLY(leave_jfr_critical_section();)
645
646 call_VM_leaf(
647 CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), rthread);
648 call_VM(noreg, CAST_FROM_FN_PTR(address,
649 InterpreterRuntime::throw_delayed_StackOverflowError));
650 should_not_reach_here();
651
652 bind(no_reserved_zone_enabling);
653 }
654
655 // remove frame anchor
656 leave();
657
658 JFR_ONLY(leave_jfr_critical_section();)
659
660 // restore sender esp
661 mov(esp, rscratch2);
662
663 // If we're returning to interpreted code we will shortly be
664 // adjusting SP to allow some space for ESP. If we're returning to
665 // compiled code the saved sender SP was saved in sender_sp, so this
666 // restores it.
667 andr(sp, esp, -16);
668 }
669
670 #if INCLUDE_JFR
671 void InterpreterMacroAssembler::enter_jfr_critical_section() {
672 const Address sampling_critical_section(rthread, in_bytes(SAMPLING_CRITICAL_SECTION_OFFSET_JFR));
673 mov(rscratch1, true);
674 strb(rscratch1, sampling_critical_section);
675 }
676
677 void InterpreterMacroAssembler::leave_jfr_critical_section() {
678 const Address sampling_critical_section(rthread, in_bytes(SAMPLING_CRITICAL_SECTION_OFFSET_JFR));
679 strb(zr, sampling_critical_section);
680 }
681 #endif // INCLUDE_JFR
682
683 // Lock object
684 //
685 // Args:
686 // c_rarg1: BasicObjectLock to be used for locking
687 //
688 // Kills:
689 // r0
690 // c_rarg0, c_rarg1, c_rarg2, c_rarg3, c_rarg4, .. (param regs)
691 // rscratch1, rscratch2 (scratch regs)
692 void InterpreterMacroAssembler::lock_object(Register lock_reg)
693 {
694 assert(lock_reg == c_rarg1, "The argument is only for looks. It must be c_rarg1");
695
696 const Register tmp = c_rarg2;
697 const Register obj_reg = c_rarg3; // Will contain the oop
698 const Register tmp2 = c_rarg4;
699 const Register tmp3 = c_rarg5;
700
701 // Load object pointer into obj_reg %c_rarg3
702 ldr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset()));
703
704 Label slow_case, done;
705 lightweight_lock(lock_reg, obj_reg, tmp, tmp2, tmp3, slow_case);
706 b(done);
707
708 bind(slow_case);
709
710 // Call the runtime routine for slow case
711 call_VM_preemptable(noreg,
712 CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
713 lock_reg);
714
715 bind(done);
716 }
717
718
719 // Unlocks an object. Used in monitorexit bytecode and
720 // remove_activation. Throws an IllegalMonitorException if object is
721 // not locked by current thread.
722 //
723 // Args:
724 // c_rarg1: BasicObjectLock for lock
725 //
726 // Kills:
727 // r0
728 // c_rarg0, c_rarg1, c_rarg2, c_rarg3, ... (param regs)
729 // rscratch1, rscratch2 (scratch regs)
730 void InterpreterMacroAssembler::unlock_object(Register lock_reg)
731 {
732 assert(lock_reg == c_rarg1, "The argument is only for looks. It must be rarg1");
733
734 const Register swap_reg = r0;
735 const Register header_reg = c_rarg2; // Will contain the old oopMark
736 const Register obj_reg = c_rarg3; // Will contain the oop
737 const Register tmp_reg = c_rarg4; // Temporary used by lightweight_unlock
738
739 save_bcp(); // Save in case of exception
740
741 // Load oop into obj_reg(%c_rarg3)
742 ldr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset()));
743
744 // Free entry
745 str(zr, Address(lock_reg, BasicObjectLock::obj_offset()));
746
747 Label slow_case, done;
748 lightweight_unlock(obj_reg, header_reg, swap_reg, tmp_reg, slow_case);
749 b(done);
750
751 bind(slow_case);
752 // Call the runtime routine for slow case.
753 str(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset())); // restore obj
754 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
755 bind(done);
756 restore_bcp();
757 }
758
759 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp,
760 Label& zero_continue) {
761 assert(ProfileInterpreter, "must be profiling interpreter");
762 ldr(mdp, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
763 cbz(mdp, zero_continue);
764 }
765
766 // Set the method data pointer for the current bcp.
767 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
768 assert(ProfileInterpreter, "must be profiling interpreter");
769 Label set_mdp;
770 stp(r0, r1, Address(pre(sp, -2 * wordSize)));
771
772 // Test MDO to avoid the call if it is null.
773 ldr(r0, Address(rmethod, in_bytes(Method::method_data_offset())));
774 cbz(r0, set_mdp);
775 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), rmethod, rbcp);
776 // r0: mdi
777 // mdo is guaranteed to be non-zero here, we checked for it before the call.
778 ldr(r1, Address(rmethod, in_bytes(Method::method_data_offset())));
779 lea(r1, Address(r1, in_bytes(MethodData::data_offset())));
780 add(r0, r1, r0);
781 str(r0, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
782 bind(set_mdp);
783 ldp(r0, r1, Address(post(sp, 2 * wordSize)));
784 }
785
786 void InterpreterMacroAssembler::verify_method_data_pointer() {
787 assert(ProfileInterpreter, "must be profiling interpreter");
788 #ifdef ASSERT
789 Label verify_continue;
790 stp(r0, r1, Address(pre(sp, -2 * wordSize)));
791 stp(r2, r3, Address(pre(sp, -2 * wordSize)));
792 test_method_data_pointer(r3, verify_continue); // If mdp is zero, continue
793 get_method(r1);
794
795 // If the mdp is valid, it will point to a DataLayout header which is
796 // consistent with the bcp. The converse is highly probable also.
797 ldrsh(r2, Address(r3, in_bytes(DataLayout::bci_offset())));
798 ldr(rscratch1, Address(r1, Method::const_offset()));
799 add(r2, r2, rscratch1, Assembler::LSL);
800 lea(r2, Address(r2, ConstMethod::codes_offset()));
801 cmp(r2, rbcp);
802 br(Assembler::EQ, verify_continue);
803 // r1: method
804 // rbcp: bcp // rbcp == 22
805 // r3: mdp
806 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp),
807 r1, rbcp, r3);
808 bind(verify_continue);
809 ldp(r2, r3, Address(post(sp, 2 * wordSize)));
810 ldp(r0, r1, Address(post(sp, 2 * wordSize)));
811 #endif // ASSERT
812 }
813
814
815 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in,
816 int constant,
817 Register value) {
818 assert(ProfileInterpreter, "must be profiling interpreter");
819 Address data(mdp_in, constant);
820 str(value, data);
821 }
822
823
824 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
825 int constant) {
826 increment_mdp_data_at(mdp_in, noreg, constant);
827 }
828
829 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
830 Register index,
831 int constant) {
832 assert(ProfileInterpreter, "must be profiling interpreter");
833
834 assert_different_registers(rscratch2, rscratch1, mdp_in, index);
835
836 Address addr1(mdp_in, constant);
837 Address addr2(rscratch2, index, Address::lsl(0));
838 Address &addr = addr1;
839 if (index != noreg) {
840 lea(rscratch2, addr1);
841 addr = addr2;
842 }
843
844 increment(addr, DataLayout::counter_increment);
845 }
846
847 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in,
848 int flag_byte_constant) {
849 assert(ProfileInterpreter, "must be profiling interpreter");
850 int flags_offset = in_bytes(DataLayout::flags_offset());
851 // Set the flag
852 ldrb(rscratch1, Address(mdp_in, flags_offset));
853 orr(rscratch1, rscratch1, flag_byte_constant);
854 strb(rscratch1, Address(mdp_in, flags_offset));
855 }
856
857
858 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in,
859 int offset,
860 Register value,
861 Register test_value_out,
862 Label& not_equal_continue) {
863 assert(ProfileInterpreter, "must be profiling interpreter");
864 if (test_value_out == noreg) {
865 ldr(rscratch1, Address(mdp_in, offset));
866 cmp(value, rscratch1);
867 } else {
868 // Put the test value into a register, so caller can use it:
869 ldr(test_value_out, Address(mdp_in, offset));
870 cmp(value, test_value_out);
871 }
872 br(Assembler::NE, not_equal_continue);
873 }
874
875
876 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
877 int offset_of_disp) {
878 assert(ProfileInterpreter, "must be profiling interpreter");
879 ldr(rscratch1, Address(mdp_in, offset_of_disp));
880 add(mdp_in, mdp_in, rscratch1, LSL);
881 str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
882 }
883
884
885 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
886 Register reg,
887 int offset_of_disp) {
888 assert(ProfileInterpreter, "must be profiling interpreter");
889 lea(rscratch1, Address(mdp_in, offset_of_disp));
890 ldr(rscratch1, Address(rscratch1, reg, Address::lsl(0)));
891 add(mdp_in, mdp_in, rscratch1, LSL);
892 str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
893 }
894
895
896 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in,
897 int constant) {
898 assert(ProfileInterpreter, "must be profiling interpreter");
899 add(mdp_in, mdp_in, (unsigned)constant);
900 str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
901 }
902
903
904 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) {
905 assert(ProfileInterpreter, "must be profiling interpreter");
906 // save/restore across call_VM
907 stp(zr, return_bci, Address(pre(sp, -2 * wordSize)));
908 call_VM(noreg,
909 CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret),
910 return_bci);
911 ldp(zr, return_bci, Address(post(sp, 2 * wordSize)));
912 }
913
914
915 void InterpreterMacroAssembler::profile_taken_branch(Register mdp) {
916 if (ProfileInterpreter) {
917 Label profile_continue;
918
919 // If no method data exists, go to profile_continue.
920 test_method_data_pointer(mdp, profile_continue);
921
922 // We are taking a branch. Increment the taken count.
923 increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset()));
924
925 // The method data pointer needs to be updated to reflect the new target.
926 update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset()));
927 bind(profile_continue);
928 }
929 }
930
931
932 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp) {
933 if (ProfileInterpreter) {
934 Label profile_continue;
935
936 // If no method data exists, go to profile_continue.
937 test_method_data_pointer(mdp, profile_continue);
938
939 // We are not taking a branch. Increment the not taken count.
940 increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset()));
941
942 // The method data pointer needs to be updated to correspond to
943 // the next bytecode
944 update_mdp_by_constant(mdp, in_bytes(BranchData::branch_data_size()));
945 bind(profile_continue);
946 }
947 }
948
949
950 void InterpreterMacroAssembler::profile_call(Register mdp) {
951 if (ProfileInterpreter) {
952 Label profile_continue;
953
954 // If no method data exists, go to profile_continue.
955 test_method_data_pointer(mdp, profile_continue);
956
957 // We are making a call. Increment the count.
958 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
959
960 // The method data pointer needs to be updated to reflect the new target.
961 update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size()));
962 bind(profile_continue);
963 }
964 }
965
966 void InterpreterMacroAssembler::profile_final_call(Register mdp) {
967 if (ProfileInterpreter) {
968 Label profile_continue;
969
970 // If no method data exists, go to profile_continue.
971 test_method_data_pointer(mdp, profile_continue);
972
973 // We are making a call. Increment the count.
974 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
975
976 // The method data pointer needs to be updated to reflect the new target.
977 update_mdp_by_constant(mdp,
978 in_bytes(VirtualCallData::
979 virtual_call_data_size()));
980 bind(profile_continue);
981 }
982 }
983
984
985 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
986 Register mdp,
987 Register reg2,
988 bool receiver_can_be_null) {
989 if (ProfileInterpreter) {
990 Label profile_continue;
991
992 // If no method data exists, go to profile_continue.
993 test_method_data_pointer(mdp, profile_continue);
994
995 Label skip_receiver_profile;
996 if (receiver_can_be_null) {
997 Label not_null;
998 // We are making a call. Increment the count for null receiver.
999 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1000 b(skip_receiver_profile);
1001 bind(not_null);
1002 }
1003
1004 // Record the receiver type.
1005 record_klass_in_profile(receiver, mdp, reg2);
1006 bind(skip_receiver_profile);
1007
1008 // The method data pointer needs to be updated to reflect the new target.
1009 update_mdp_by_constant(mdp, in_bytes(VirtualCallData::virtual_call_data_size()));
1010 bind(profile_continue);
1011 }
1012 }
1013
1014 // This routine creates a state machine for updating the multi-row
1015 // type profile at a virtual call site (or other type-sensitive bytecode).
1016 // The machine visits each row (of receiver/count) until the receiver type
1017 // is found, or until it runs out of rows. At the same time, it remembers
1018 // the location of the first empty row. (An empty row records null for its
1019 // receiver, and can be allocated for a newly-observed receiver type.)
1020 // Because there are two degrees of freedom in the state, a simple linear
1021 // search will not work; it must be a decision tree. Hence this helper
1022 // function is recursive, to generate the required tree structured code.
1023 // It's the interpreter, so we are trading off code space for speed.
1024 // See below for example code.
1025 void InterpreterMacroAssembler::record_klass_in_profile_helper(
1026 Register receiver, Register mdp,
1027 Register reg2, int start_row,
1028 Label& done) {
1029 if (TypeProfileWidth == 0) {
1030 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1031 } else {
1032 record_item_in_profile_helper(receiver, mdp, reg2, 0, done, TypeProfileWidth,
1033 &VirtualCallData::receiver_offset, &VirtualCallData::receiver_count_offset);
1034 }
1035 }
1036
1037 void InterpreterMacroAssembler::record_item_in_profile_helper(Register item, Register mdp,
1038 Register reg2, int start_row, Label& done, int total_rows,
1039 OffsetFunction item_offset_fn, OffsetFunction item_count_offset_fn) {
1040 int last_row = total_rows - 1;
1041 assert(start_row <= last_row, "must be work left to do");
1042 // Test this row for both the item and for null.
1043 // Take any of three different outcomes:
1044 // 1. found item => increment count and goto done
1045 // 2. found null => keep looking for case 1, maybe allocate this cell
1046 // 3. found something else => keep looking for cases 1 and 2
1047 // Case 3 is handled by a recursive call.
1048 for (int row = start_row; row <= last_row; row++) {
1049 Label next_test;
1050 bool test_for_null_also = (row == start_row);
1051
1052 // See if the item is item[n].
1053 int item_offset = in_bytes(item_offset_fn(row));
1054 test_mdp_data_at(mdp, item_offset, item,
1055 (test_for_null_also ? reg2 : noreg),
1056 next_test);
1057 // (Reg2 now contains the item from the CallData.)
1058
1059 // The item is item[n]. Increment count[n].
1060 int count_offset = in_bytes(item_count_offset_fn(row));
1061 increment_mdp_data_at(mdp, count_offset);
1062 b(done);
1063 bind(next_test);
1064
1065 if (test_for_null_also) {
1066 Label found_null;
1067 // Failed the equality check on item[n]... Test for null.
1068 if (start_row == last_row) {
1069 // The only thing left to do is handle the null case.
1070 cbz(reg2, found_null);
1071 // Item did not match any saved item and there is no empty row for it.
1072 // Increment total counter to indicate polymorphic case.
1073 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1074 b(done);
1075 bind(found_null);
1076 break;
1077 }
1078 // Since null is rare, make it be the branch-taken case.
1079 cbz(reg2, found_null);
1080
1081 // Put all the "Case 3" tests here.
1082 record_item_in_profile_helper(item, mdp, reg2, start_row + 1, done, total_rows,
1083 item_offset_fn, item_count_offset_fn);
1084
1085 // Found a null. Keep searching for a matching item,
1086 // but remember that this is an empty (unused) slot.
1087 bind(found_null);
1088 }
1089 }
1090
1091 // In the fall-through case, we found no matching item, but we
1092 // observed the item[start_row] is null.
1093
1094 // Fill in the item field and increment the count.
1095 int item_offset = in_bytes(item_offset_fn(start_row));
1096 set_mdp_data_at(mdp, item_offset, item);
1097 int count_offset = in_bytes(item_count_offset_fn(start_row));
1098 mov(reg2, DataLayout::counter_increment);
1099 set_mdp_data_at(mdp, count_offset, reg2);
1100 if (start_row > 0) {
1101 b(done);
1102 }
1103 }
1104
1105 // Example state machine code for three profile rows:
1106 // // main copy of decision tree, rooted at row[1]
1107 // if (row[0].rec == rec) { row[0].incr(); goto done; }
1108 // if (row[0].rec != nullptr) {
1109 // // inner copy of decision tree, rooted at row[1]
1110 // if (row[1].rec == rec) { row[1].incr(); goto done; }
1111 // if (row[1].rec != nullptr) {
1112 // // degenerate decision tree, rooted at row[2]
1113 // if (row[2].rec == rec) { row[2].incr(); goto done; }
1114 // if (row[2].rec != nullptr) { count.incr(); goto done; } // overflow
1115 // row[2].init(rec); goto done;
1116 // } else {
1117 // // remember row[1] is empty
1118 // if (row[2].rec == rec) { row[2].incr(); goto done; }
1119 // row[1].init(rec); goto done;
1120 // }
1121 // } else {
1122 // // remember row[0] is empty
1123 // if (row[1].rec == rec) { row[1].incr(); goto done; }
1124 // if (row[2].rec == rec) { row[2].incr(); goto done; }
1125 // row[0].init(rec); goto done;
1126 // }
1127 // done:
1128
1129 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1130 Register mdp, Register reg2) {
1131 assert(ProfileInterpreter, "must be profiling");
1132 Label done;
1133
1134 record_klass_in_profile_helper(receiver, mdp, reg2, 0, done);
1135
1136 bind (done);
1137 }
1138
1139 void InterpreterMacroAssembler::profile_ret(Register return_bci,
1140 Register mdp) {
1141 if (ProfileInterpreter) {
1142 Label profile_continue;
1143 uint row;
1144
1145 // If no method data exists, go to profile_continue.
1146 test_method_data_pointer(mdp, profile_continue);
1147
1148 // Update the total ret count.
1149 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1150
1151 for (row = 0; row < RetData::row_limit(); row++) {
1152 Label next_test;
1153
1154 // See if return_bci is equal to bci[n]:
1155 test_mdp_data_at(mdp,
1156 in_bytes(RetData::bci_offset(row)),
1157 return_bci, noreg,
1158 next_test);
1159
1160 // return_bci is equal to bci[n]. Increment the count.
1161 increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row)));
1162
1163 // The method data pointer needs to be updated to reflect the new target.
1164 update_mdp_by_offset(mdp,
1165 in_bytes(RetData::bci_displacement_offset(row)));
1166 b(profile_continue);
1167 bind(next_test);
1168 }
1169
1170 update_mdp_for_ret(return_bci);
1171
1172 bind(profile_continue);
1173 }
1174 }
1175
1176 void InterpreterMacroAssembler::profile_null_seen(Register mdp) {
1177 if (ProfileInterpreter) {
1178 Label profile_continue;
1179
1180 // If no method data exists, go to profile_continue.
1181 test_method_data_pointer(mdp, profile_continue);
1182
1183 set_mdp_flag_at(mdp, BitData::null_seen_byte_constant());
1184
1185 // The method data pointer needs to be updated.
1186 int mdp_delta = in_bytes(BitData::bit_data_size());
1187 if (TypeProfileCasts) {
1188 mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1189 }
1190 update_mdp_by_constant(mdp, mdp_delta);
1191
1192 bind(profile_continue);
1193 }
1194 }
1195
1196 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass, Register reg2) {
1197 if (ProfileInterpreter) {
1198 Label profile_continue;
1199
1200 // If no method data exists, go to profile_continue.
1201 test_method_data_pointer(mdp, profile_continue);
1202
1203 // The method data pointer needs to be updated.
1204 int mdp_delta = in_bytes(BitData::bit_data_size());
1205 if (TypeProfileCasts) {
1206 mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1207
1208 // Record the object type.
1209 record_klass_in_profile(klass, mdp, reg2);
1210 }
1211 update_mdp_by_constant(mdp, mdp_delta);
1212
1213 bind(profile_continue);
1214 }
1215 }
1216
1217 void InterpreterMacroAssembler::profile_switch_default(Register mdp) {
1218 if (ProfileInterpreter) {
1219 Label profile_continue;
1220
1221 // If no method data exists, go to profile_continue.
1222 test_method_data_pointer(mdp, profile_continue);
1223
1224 // Update the default case count
1225 increment_mdp_data_at(mdp,
1226 in_bytes(MultiBranchData::default_count_offset()));
1227
1228 // The method data pointer needs to be updated.
1229 update_mdp_by_offset(mdp,
1230 in_bytes(MultiBranchData::
1231 default_displacement_offset()));
1232
1233 bind(profile_continue);
1234 }
1235 }
1236
1237 void InterpreterMacroAssembler::profile_switch_case(Register index,
1238 Register mdp,
1239 Register reg2) {
1240 if (ProfileInterpreter) {
1241 Label profile_continue;
1242
1243 // If no method data exists, go to profile_continue.
1244 test_method_data_pointer(mdp, profile_continue);
1245
1246 // Build the base (index * per_case_size_in_bytes()) +
1247 // case_array_offset_in_bytes()
1248 movw(reg2, in_bytes(MultiBranchData::per_case_size()));
1249 movw(rscratch1, in_bytes(MultiBranchData::case_array_offset()));
1250 Assembler::maddw(index, index, reg2, rscratch1);
1251
1252 // Update the case count
1253 increment_mdp_data_at(mdp,
1254 index,
1255 in_bytes(MultiBranchData::relative_count_offset()));
1256
1257 // The method data pointer needs to be updated.
1258 update_mdp_by_offset(mdp,
1259 index,
1260 in_bytes(MultiBranchData::
1261 relative_displacement_offset()));
1262
1263 bind(profile_continue);
1264 }
1265 }
1266
1267 void InterpreterMacroAssembler::_interp_verify_oop(Register reg, TosState state, const char* file, int line) {
1268 if (state == atos) {
1269 MacroAssembler::_verify_oop_checked(reg, "broken oop", file, line);
1270 }
1271 }
1272
1273 void InterpreterMacroAssembler::generate_runtime_upcalls_on_method_entry()
1274 {
1275 address upcall = RuntimeUpcalls::on_method_entry_upcall_address();
1276 if (RuntimeUpcalls::does_upcall_need_method_parameter(upcall)) {
1277 get_method(c_rarg1);
1278 call_VM(noreg,upcall, c_rarg1);
1279 } else {
1280 call_VM(noreg,upcall);
1281 }
1282 }
1283
1284 void InterpreterMacroAssembler::notify_method_entry() {
1285 // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1286 // track stack depth. If it is possible to enter interp_only_mode we add
1287 // the code to check if the event should be sent.
1288 if (JvmtiExport::can_post_interpreter_events()) {
1289 Label L;
1290 ldrw(r3, Address(rthread, JavaThread::interp_only_mode_offset()));
1291 cbzw(r3, L);
1292 call_VM(noreg, CAST_FROM_FN_PTR(address,
1293 InterpreterRuntime::post_method_entry));
1294 bind(L);
1295 }
1296
1297 if (DTraceMethodProbes) {
1298 get_method(c_rarg1);
1299 call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
1300 rthread, c_rarg1);
1301 }
1302
1303 // RedefineClasses() tracing support for obsolete method entry
1304 if (log_is_enabled(Trace, redefine, class, obsolete) ||
1305 log_is_enabled(Trace, interpreter, bytecode)) {
1306 get_method(c_rarg1);
1307 call_VM_leaf(
1308 CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
1309 rthread, c_rarg1);
1310 }
1311
1312 }
1313
1314
1315 void InterpreterMacroAssembler::notify_method_exit(
1316 TosState state, NotifyMethodExitMode mode) {
1317 // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1318 // track stack depth. If it is possible to enter interp_only_mode we add
1319 // the code to check if the event should be sent.
1320 if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
1321 Label L;
1322 // Note: frame::interpreter_frame_result has a dependency on how the
1323 // method result is saved across the call to post_method_exit. If this
1324 // is changed then the interpreter_frame_result implementation will
1325 // need to be updated too.
1326
1327 // template interpreter will leave the result on the top of the stack.
1328 push(state);
1329 ldrw(r3, Address(rthread, JavaThread::interp_only_mode_offset()));
1330 cbz(r3, L);
1331 call_VM(noreg,
1332 CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
1333 bind(L);
1334 pop(state);
1335 }
1336
1337 if (DTraceMethodProbes) {
1338 push(state);
1339 get_method(c_rarg1);
1340 call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
1341 rthread, c_rarg1);
1342 pop(state);
1343 }
1344 }
1345
1346
1347 // Jump if ((*counter_addr += increment) & mask) satisfies the condition.
1348 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr,
1349 int increment, Address mask,
1350 Register scratch, Register scratch2,
1351 bool preloaded, Condition cond,
1352 Label* where) {
1353 if (!preloaded) {
1354 ldrw(scratch, counter_addr);
1355 }
1356 add(scratch, scratch, increment);
1357 strw(scratch, counter_addr);
1358 ldrw(scratch2, mask);
1359 ands(scratch, scratch, scratch2);
1360 br(cond, *where);
1361 }
1362
1363 void InterpreterMacroAssembler::call_VM_leaf_base(address entry_point,
1364 int number_of_arguments) {
1365 // interpreter specific
1366 //
1367 // Note: No need to save/restore rbcp & rlocals pointer since these
1368 // are callee saved registers and no blocking/ GC can happen
1369 // in leaf calls.
1370 #ifdef ASSERT
1371 {
1372 Label L;
1373 ldr(rscratch1, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1374 cbz(rscratch1, L);
1375 stop("InterpreterMacroAssembler::call_VM_leaf_base:"
1376 " last_sp != nullptr");
1377 bind(L);
1378 }
1379 #endif /* ASSERT */
1380 // super call
1381 MacroAssembler::call_VM_leaf_base(entry_point, number_of_arguments);
1382 }
1383
1384 void InterpreterMacroAssembler::call_VM_base(Register oop_result,
1385 Register java_thread,
1386 Register last_java_sp,
1387 address entry_point,
1388 int number_of_arguments,
1389 bool check_exceptions) {
1390 // interpreter specific
1391 //
1392 // Note: Could avoid restoring locals ptr (callee saved) - however doesn't
1393 // really make a difference for these runtime calls, since they are
1394 // slow anyway. Btw., bcp must be saved/restored since it may change
1395 // due to GC.
1396 // assert(java_thread == noreg , "not expecting a precomputed java thread");
1397 save_bcp();
1398 #ifdef ASSERT
1399 {
1400 Label L;
1401 ldr(rscratch1, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1402 cbz(rscratch1, L);
1403 stop("InterpreterMacroAssembler::call_VM_base:"
1404 " last_sp != nullptr");
1405 bind(L);
1406 }
1407 #endif /* ASSERT */
1408 // super call
1409 MacroAssembler::call_VM_base(oop_result, noreg, last_java_sp,
1410 entry_point, number_of_arguments,
1411 check_exceptions);
1412 // interpreter specific
1413 restore_bcp();
1414 restore_locals();
1415 }
1416
1417 void InterpreterMacroAssembler::call_VM_preemptable(Register oop_result,
1418 address entry_point,
1419 Register arg_1) {
1420 assert(arg_1 == c_rarg1, "");
1421 Label resume_pc, not_preempted;
1422
1423 #ifdef ASSERT
1424 {
1425 Label L;
1426 ldr(rscratch1, Address(rthread, JavaThread::preempt_alternate_return_offset()));
1427 cbz(rscratch1, L);
1428 stop("Should not have alternate return address set");
1429 bind(L);
1430 }
1431 #endif /* ASSERT */
1432
1433 // Force freeze slow path.
1434 push_cont_fastpath();
1435
1436 // Make VM call. In case of preemption set last_pc to the one we want to resume to.
1437 adr(rscratch1, resume_pc);
1438 str(rscratch1, Address(rthread, JavaThread::last_Java_pc_offset()));
1439 call_VM_base(oop_result, noreg, noreg, entry_point, 1, false /*check_exceptions*/);
1440
1441 pop_cont_fastpath();
1442
1443 // Check if preempted.
1444 ldr(rscratch1, Address(rthread, JavaThread::preempt_alternate_return_offset()));
1445 cbz(rscratch1, not_preempted);
1446 str(zr, Address(rthread, JavaThread::preempt_alternate_return_offset()));
1447 br(rscratch1);
1448
1449 // In case of preemption, this is where we will resume once we finally acquire the monitor.
1450 bind(resume_pc);
1451 restore_after_resume(false /* is_native */);
1452
1453 bind(not_preempted);
1454 }
1455
1456 void InterpreterMacroAssembler::restore_after_resume(bool is_native) {
1457 lea(rscratch1, ExternalAddress(Interpreter::cont_resume_interpreter_adapter()));
1458 blr(rscratch1);
1459 if (is_native) {
1460 // On resume we need to set up stack as expected
1461 push(dtos);
1462 push(ltos);
1463 }
1464 }
1465
1466 void InterpreterMacroAssembler::profile_obj_type(Register obj, const Address& mdo_addr) {
1467 assert_different_registers(obj, rscratch1, mdo_addr.base(), mdo_addr.index());
1468 Label update, next, none;
1469
1470 verify_oop(obj);
1471
1472 cbnz(obj, update);
1473 orptr(mdo_addr, TypeEntries::null_seen);
1474 b(next);
1475
1476 bind(update);
1477 load_klass(obj, obj);
1478
1479 ldr(rscratch1, mdo_addr);
1480 eor(obj, obj, rscratch1);
1481 tst(obj, TypeEntries::type_klass_mask);
1482 br(Assembler::EQ, next); // klass seen before, nothing to
1483 // do. The unknown bit may have been
1484 // set already but no need to check.
1485
1486 tbnz(obj, exact_log2(TypeEntries::type_unknown), next);
1487 // already unknown. Nothing to do anymore.
1488
1489 cbz(rscratch1, none);
1490 cmp(rscratch1, (u1)TypeEntries::null_seen);
1491 br(Assembler::EQ, none);
1492 // There is a chance that the checks above
1493 // fail if another thread has just set the
1494 // profiling to this obj's klass
1495 eor(obj, obj, rscratch1); // get back original value before XOR
1496 ldr(rscratch1, mdo_addr);
1497 eor(obj, obj, rscratch1);
1498 tst(obj, TypeEntries::type_klass_mask);
1499 br(Assembler::EQ, next);
1500
1501 // different than before. Cannot keep accurate profile.
1502 orptr(mdo_addr, TypeEntries::type_unknown);
1503 b(next);
1504
1505 bind(none);
1506 // first time here. Set profile type.
1507 str(obj, mdo_addr);
1508 #ifdef ASSERT
1509 andr(obj, obj, TypeEntries::type_mask);
1510 verify_klass_ptr(obj);
1511 #endif
1512
1513 bind(next);
1514 }
1515
1516 void InterpreterMacroAssembler::profile_arguments_type(Register mdp, Register callee, Register tmp, bool is_virtual) {
1517 if (!ProfileInterpreter) {
1518 return;
1519 }
1520
1521 if (MethodData::profile_arguments() || MethodData::profile_return()) {
1522 Label profile_continue;
1523
1524 test_method_data_pointer(mdp, profile_continue);
1525
1526 int off_to_start = is_virtual ? in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
1527
1528 ldrb(rscratch1, Address(mdp, in_bytes(DataLayout::tag_offset()) - off_to_start));
1529 cmp(rscratch1, u1(is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag));
1530 br(Assembler::NE, profile_continue);
1531
1532 if (MethodData::profile_arguments()) {
1533 Label done;
1534 int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset());
1535
1536 for (int i = 0; i < TypeProfileArgsLimit; i++) {
1537 if (i > 0 || MethodData::profile_return()) {
1538 // If return value type is profiled we may have no argument to profile
1539 ldr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())));
1540 sub(tmp, tmp, i*TypeStackSlotEntries::per_arg_count());
1541 cmp(tmp, (u1)TypeStackSlotEntries::per_arg_count());
1542 add(rscratch1, mdp, off_to_args);
1543 br(Assembler::LT, done);
1544 }
1545 ldr(tmp, Address(callee, Method::const_offset()));
1546 load_unsigned_short(tmp, Address(tmp, ConstMethod::size_of_parameters_offset()));
1547 // stack offset o (zero based) from the start of the argument
1548 // list, for n arguments translates into offset n - o - 1 from
1549 // the end of the argument list
1550 ldr(rscratch1, Address(mdp, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))));
1551 sub(tmp, tmp, rscratch1);
1552 sub(tmp, tmp, 1);
1553 Address arg_addr = argument_address(tmp);
1554 ldr(tmp, arg_addr);
1555
1556 Address mdo_arg_addr(mdp, in_bytes(TypeEntriesAtCall::argument_type_offset(i)));
1557 profile_obj_type(tmp, mdo_arg_addr);
1558
1559 int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
1560 off_to_args += to_add;
1561 }
1562
1563 if (MethodData::profile_return()) {
1564 ldr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())));
1565 sub(tmp, tmp, TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count());
1566 }
1567
1568 add(rscratch1, mdp, off_to_args);
1569 bind(done);
1570 mov(mdp, rscratch1);
1571
1572 if (MethodData::profile_return()) {
1573 // We're right after the type profile for the last
1574 // argument. tmp is the number of cells left in the
1575 // CallTypeData/VirtualCallTypeData to reach its end. Non null
1576 // if there's a return to profile.
1577 assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(), "can't move past ret type");
1578 add(mdp, mdp, tmp, LSL, exact_log2(DataLayout::cell_size));
1579 }
1580 str(mdp, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
1581 } else {
1582 assert(MethodData::profile_return(), "either profile call args or call ret");
1583 update_mdp_by_constant(mdp, in_bytes(TypeEntriesAtCall::return_only_size()));
1584 }
1585
1586 // mdp points right after the end of the
1587 // CallTypeData/VirtualCallTypeData, right after the cells for the
1588 // return value type if there's one
1589
1590 bind(profile_continue);
1591 }
1592 }
1593
1594 void InterpreterMacroAssembler::profile_return_type(Register mdp, Register ret, Register tmp) {
1595 assert_different_registers(mdp, ret, tmp, rbcp);
1596 if (ProfileInterpreter && MethodData::profile_return()) {
1597 Label profile_continue, done;
1598
1599 test_method_data_pointer(mdp, profile_continue);
1600
1601 if (MethodData::profile_return_jsr292_only()) {
1602 assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
1603
1604 // If we don't profile all invoke bytecodes we must make sure
1605 // it's a bytecode we indeed profile. We can't go back to the
1606 // beginning of the ProfileData we intend to update to check its
1607 // type because we're right after it and we don't known its
1608 // length
1609 Label do_profile;
1610 ldrb(rscratch1, Address(rbcp, 0));
1611 cmp(rscratch1, (u1)Bytecodes::_invokedynamic);
1612 br(Assembler::EQ, do_profile);
1613 cmp(rscratch1, (u1)Bytecodes::_invokehandle);
1614 br(Assembler::EQ, do_profile);
1615 get_method(tmp);
1616 ldrh(rscratch1, Address(tmp, Method::intrinsic_id_offset()));
1617 subs(zr, rscratch1, static_cast<int>(vmIntrinsics::_compiledLambdaForm));
1618 br(Assembler::NE, profile_continue);
1619
1620 bind(do_profile);
1621 }
1622
1623 Address mdo_ret_addr(mdp, -in_bytes(ReturnTypeEntry::size()));
1624 mov(tmp, ret);
1625 profile_obj_type(tmp, mdo_ret_addr);
1626
1627 bind(profile_continue);
1628 }
1629 }
1630
1631 void InterpreterMacroAssembler::profile_parameters_type(Register mdp, Register tmp1, Register tmp2) {
1632 assert_different_registers(rscratch1, rscratch2, mdp, tmp1, tmp2);
1633 if (ProfileInterpreter && MethodData::profile_parameters()) {
1634 Label profile_continue, done;
1635
1636 test_method_data_pointer(mdp, profile_continue);
1637
1638 // Load the offset of the area within the MDO used for
1639 // parameters. If it's negative we're not profiling any parameters
1640 ldrw(tmp1, Address(mdp, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset())));
1641 tbnz(tmp1, 31, profile_continue); // i.e. sign bit set
1642
1643 // Compute a pointer to the area for parameters from the offset
1644 // and move the pointer to the slot for the last
1645 // parameters. Collect profiling from last parameter down.
1646 // mdo start + parameters offset + array length - 1
1647 add(mdp, mdp, tmp1);
1648 ldr(tmp1, Address(mdp, ArrayData::array_len_offset()));
1649 sub(tmp1, tmp1, TypeStackSlotEntries::per_arg_count());
1650
1651 Label loop;
1652 bind(loop);
1653
1654 int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
1655 int type_base = in_bytes(ParametersTypeData::type_offset(0));
1656 int per_arg_scale = exact_log2(DataLayout::cell_size);
1657 add(rscratch1, mdp, off_base);
1658 add(rscratch2, mdp, type_base);
1659
1660 Address arg_off(rscratch1, tmp1, Address::lsl(per_arg_scale));
1661 Address arg_type(rscratch2, tmp1, Address::lsl(per_arg_scale));
1662
1663 // load offset on the stack from the slot for this parameter
1664 ldr(tmp2, arg_off);
1665 neg(tmp2, tmp2);
1666 // read the parameter from the local area
1667 ldr(tmp2, Address(rlocals, tmp2, Address::lsl(Interpreter::logStackElementSize)));
1668
1669 // profile the parameter
1670 profile_obj_type(tmp2, arg_type);
1671
1672 // go to next parameter
1673 subs(tmp1, tmp1, TypeStackSlotEntries::per_arg_count());
1674 br(Assembler::GE, loop);
1675
1676 bind(profile_continue);
1677 }
1678 }
1679
1680 void InterpreterMacroAssembler::load_resolved_indy_entry(Register cache, Register index) {
1681 // Get index out of bytecode pointer, get_cache_entry_pointer_at_bcp
1682 get_cache_index_at_bcp(index, 1, sizeof(u4));
1683 // Get address of invokedynamic array
1684 ldr(cache, Address(rcpool, in_bytes(ConstantPoolCache::invokedynamic_entries_offset())));
1685 // Scale the index to be the entry index * sizeof(ResolvedIndyEntry)
1686 lsl(index, index, log2i_exact(sizeof(ResolvedIndyEntry)));
1687 add(cache, cache, Array<ResolvedIndyEntry>::base_offset_in_bytes());
1688 lea(cache, Address(cache, index));
1689 }
1690
1691 void InterpreterMacroAssembler::load_field_entry(Register cache, Register index, int bcp_offset) {
1692 // Get index out of bytecode pointer
1693 get_cache_index_at_bcp(index, bcp_offset, sizeof(u2));
1694 // Take shortcut if the size is a power of 2
1695 if (is_power_of_2(sizeof(ResolvedFieldEntry))) {
1696 lsl(index, index, log2i_exact(sizeof(ResolvedFieldEntry))); // Scale index by power of 2
1697 } else {
1698 mov(cache, sizeof(ResolvedFieldEntry));
1699 mul(index, index, cache); // Scale the index to be the entry index * sizeof(ResolvedFieldEntry)
1700 }
1701 // Get address of field entries array
1702 ldr(cache, Address(rcpool, ConstantPoolCache::field_entries_offset()));
1703 add(cache, cache, Array<ResolvedFieldEntry>::base_offset_in_bytes());
1704 lea(cache, Address(cache, index));
1705 // Prevents stale data from being read after the bytecode is patched to the fast bytecode
1706 membar(MacroAssembler::LoadLoad);
1707 }
1708
1709 void InterpreterMacroAssembler::load_method_entry(Register cache, Register index, int bcp_offset) {
1710 // Get index out of bytecode pointer
1711 get_cache_index_at_bcp(index, bcp_offset, sizeof(u2));
1712 mov(cache, sizeof(ResolvedMethodEntry));
1713 mul(index, index, cache); // Scale the index to be the entry index * sizeof(ResolvedMethodEntry)
1714
1715 // Get address of field entries array
1716 ldr(cache, Address(rcpool, ConstantPoolCache::method_entries_offset()));
1717 add(cache, cache, Array<ResolvedMethodEntry>::base_offset_in_bytes());
1718 lea(cache, Address(cache, index));
1719 }