1 /*
2 * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
4 * Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 *
7 * This code is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 only, as
9 * published by the Free Software Foundation.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 */
26
27 #include "precompiled.hpp"
28 #include "asm/macroAssembler.inline.hpp"
29 #include "gc/shared/barrierSet.hpp"
30 #include "gc/shared/barrierSetAssembler.hpp"
31 #include "interp_masm_riscv.hpp"
32 #include "interpreter/interpreter.hpp"
33 #include "interpreter/interpreterRuntime.hpp"
34 #include "logging/log.hpp"
35 #include "oops/arrayOop.hpp"
36 #include "oops/markWord.hpp"
37 #include "oops/method.hpp"
38 #include "oops/methodData.hpp"
39 #include "prims/jvmtiExport.hpp"
40 #include "prims/jvmtiThreadState.hpp"
41 #include "runtime/basicLock.hpp"
42 #include "runtime/frame.inline.hpp"
43 #include "runtime/javaThread.hpp"
44 #include "runtime/safepointMechanism.hpp"
45 #include "runtime/sharedRuntime.hpp"
46 #include "utilities/powerOfTwo.hpp"
47
48 void InterpreterMacroAssembler::narrow(Register result) {
49 // Get method->_constMethod->_result_type
50 ld(t0, Address(fp, frame::interpreter_frame_method_offset * wordSize));
51 ld(t0, Address(t0, Method::const_offset()));
52 lbu(t0, Address(t0, ConstMethod::result_type_offset()));
53
54 Label done, notBool, notByte, notChar;
55
56 // common case first
57 mv(t1, T_INT);
58 beq(t0, t1, done);
59
60 // mask integer result to narrower return type.
61 mv(t1, T_BOOLEAN);
62 bne(t0, t1, notBool);
63
64 andi(result, result, 0x1);
65 j(done);
66
67 bind(notBool);
68 mv(t1, T_BYTE);
69 bne(t0, t1, notByte);
70 sign_extend(result, result, 8);
71 j(done);
72
73 bind(notByte);
74 mv(t1, T_CHAR);
75 bne(t0, t1, notChar);
76 zero_extend(result, result, 16);
77 j(done);
78
79 bind(notChar);
80 sign_extend(result, result, 16);
81
82 bind(done);
83 sign_extend(result, result, 32);
84 }
85
86 void InterpreterMacroAssembler::jump_to_entry(address entry) {
87 assert(entry != nullptr, "Entry must have been generated by now");
88 j(entry);
89 }
90
91 void InterpreterMacroAssembler::check_and_handle_popframe(Register java_thread) {
92 if (JvmtiExport::can_pop_frame()) {
93 Label L;
94 // Initiate popframe handling only if it is not already being
95 // processed. If the flag has the popframe_processing bit set,
96 // it means that this code is called *during* popframe handling - we
97 // don't want to reenter.
98 // This method is only called just after the call into the vm in
99 // call_VM_base, so the arg registers are available.
100 lwu(t1, Address(xthread, JavaThread::popframe_condition_offset()));
101 test_bit(t0, t1, exact_log2(JavaThread::popframe_pending_bit));
102 beqz(t0, L);
103 test_bit(t0, t1, exact_log2(JavaThread::popframe_processing_bit));
104 bnez(t0, L);
105 // Call Interpreter::remove_activation_preserving_args_entry() to get the
106 // address of the same-named entrypoint in the generated interpreter code.
107 call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_preserving_args_entry));
108 jr(x10);
109 bind(L);
110 }
111 }
112
113
114 void InterpreterMacroAssembler::load_earlyret_value(TosState state) {
115 ld(x12, Address(xthread, JavaThread::jvmti_thread_state_offset()));
116 const Address tos_addr(x12, JvmtiThreadState::earlyret_tos_offset());
117 const Address oop_addr(x12, JvmtiThreadState::earlyret_oop_offset());
118 const Address val_addr(x12, JvmtiThreadState::earlyret_value_offset());
119 switch (state) {
120 case atos:
121 ld(x10, oop_addr);
122 sd(zr, oop_addr);
123 verify_oop(x10);
124 break;
125 case ltos:
126 ld(x10, val_addr);
127 break;
128 case btos: // fall through
129 case ztos: // fall through
130 case ctos: // fall through
131 case stos: // fall through
132 case itos:
133 lwu(x10, val_addr);
134 break;
135 case ftos:
136 flw(f10, val_addr);
137 break;
138 case dtos:
139 fld(f10, val_addr);
140 break;
141 case vtos:
142 /* nothing to do */
143 break;
144 default:
145 ShouldNotReachHere();
146 }
147 // Clean up tos value in the thread object
148 mv(t0, (int)ilgl);
149 sw(t0, tos_addr);
150 sw(zr, val_addr);
151 }
152
153
154 void InterpreterMacroAssembler::check_and_handle_earlyret(Register java_thread) {
155 if (JvmtiExport::can_force_early_return()) {
156 Label L;
157 ld(t0, Address(xthread, JavaThread::jvmti_thread_state_offset()));
158 beqz(t0, L); // if thread->jvmti_thread_state() is null then exit
159
160 // Initiate earlyret handling only if it is not already being processed.
161 // If the flag has the earlyret_processing bit set, it means that this code
162 // is called *during* earlyret handling - we don't want to reenter.
163 lwu(t0, Address(t0, JvmtiThreadState::earlyret_state_offset()));
164 mv(t1, JvmtiThreadState::earlyret_pending);
165 bne(t0, t1, L);
166
167 // Call Interpreter::remove_activation_early_entry() to get the address of the
168 // same-named entrypoint in the generated interpreter code.
169 ld(t0, Address(xthread, JavaThread::jvmti_thread_state_offset()));
170 lwu(t0, Address(t0, JvmtiThreadState::earlyret_tos_offset()));
171 call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_early_entry), t0);
172 jr(x10);
173 bind(L);
174 }
175 }
176
177 void InterpreterMacroAssembler::get_unsigned_2_byte_index_at_bcp(Register reg, int bcp_offset) {
178 assert(bcp_offset >= 0, "bcp is still pointing to start of bytecode");
179 if (AvoidUnalignedAccesses && (bcp_offset % 2)) {
180 lbu(t1, Address(xbcp, bcp_offset));
181 lbu(reg, Address(xbcp, bcp_offset + 1));
182 slli(t1, t1, 8);
183 add(reg, reg, t1);
184 } else {
185 lhu(reg, Address(xbcp, bcp_offset));
186 revb_h_h_u(reg, reg);
187 }
188 }
189
190 void InterpreterMacroAssembler::get_dispatch() {
191 ExternalAddress target((address)Interpreter::dispatch_table());
192 relocate(target.rspec(), [&] {
193 int32_t offset;
194 la_patchable(xdispatch, target, offset);
195 addi(xdispatch, xdispatch, offset);
196 });
197 }
198
199 void InterpreterMacroAssembler::get_cache_index_at_bcp(Register index,
200 Register tmp,
201 int bcp_offset,
202 size_t index_size) {
203 assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
204 if (index_size == sizeof(u2)) {
205 if (AvoidUnalignedAccesses) {
206 assert_different_registers(index, tmp);
207 load_unsigned_byte(index, Address(xbcp, bcp_offset));
208 load_unsigned_byte(tmp, Address(xbcp, bcp_offset + 1));
209 slli(tmp, tmp, 8);
210 add(index, index, tmp);
211 } else {
212 load_unsigned_short(index, Address(xbcp, bcp_offset));
213 }
214 } else if (index_size == sizeof(u4)) {
215 load_int_misaligned(index, Address(xbcp, bcp_offset), tmp, false);
216
217 // Check if the secondary index definition is still ~x, otherwise
218 // we have to change the following assembler code to calculate the
219 // plain index.
220 assert(ConstantPool::decode_invokedynamic_index(~123) == 123, "else change next line");
221 xori(index, index, -1);
222 sign_extend(index, index, 32);
223 } else if (index_size == sizeof(u1)) {
224 load_unsigned_byte(index, Address(xbcp, bcp_offset));
225 } else {
226 ShouldNotReachHere();
227 }
228 }
229
230 // Return
231 // Rindex: index into constant pool
232 // Rcache: address of cache entry - ConstantPoolCache::base_offset()
233 //
234 // A caller must add ConstantPoolCache::base_offset() to Rcache to get
235 // the true address of the cache entry.
236 //
237 void InterpreterMacroAssembler::get_cache_and_index_at_bcp(Register cache,
238 Register index,
239 int bcp_offset,
240 size_t index_size) {
241 assert_different_registers(cache, index);
242 assert_different_registers(cache, xcpool);
243 // register "cache" is trashed in next shadd, so lets use it as a temporary register
244 get_cache_index_at_bcp(index, cache, bcp_offset, index_size);
245 assert(sizeof(ConstantPoolCacheEntry) == 4 * wordSize, "adjust code below");
246 // Convert from field index to ConstantPoolCacheEntry
247 // riscv already has the cache in xcpool so there is no need to
248 // install it in cache. Instead we pre-add the indexed offset to
249 // xcpool and return it in cache. All clients of this method need to
250 // be modified accordingly.
251 shadd(cache, index, xcpool, cache, 5);
252 }
253
254
255 void InterpreterMacroAssembler::get_cache_and_index_and_bytecode_at_bcp(Register cache,
256 Register index,
257 Register bytecode,
258 int byte_no,
259 int bcp_offset,
260 size_t index_size) {
261 get_cache_and_index_at_bcp(cache, index, bcp_offset, index_size);
262 // We use a 32-bit load here since the layout of 64-bit words on
263 // little-endian machines allow us that.
264 // n.b. unlike x86 cache already includes the index offset
265 la(bytecode, Address(cache,
266 ConstantPoolCache::base_offset() +
267 ConstantPoolCacheEntry::indices_offset()));
268 membar(MacroAssembler::AnyAny);
269 lwu(bytecode, bytecode);
270 membar(MacroAssembler::LoadLoad | MacroAssembler::LoadStore);
271 const int shift_count = (1 + byte_no) * BitsPerByte;
272 slli(bytecode, bytecode, XLEN - (shift_count + BitsPerByte));
273 srli(bytecode, bytecode, XLEN - BitsPerByte);
274 }
275
276 void InterpreterMacroAssembler::get_cache_entry_pointer_at_bcp(Register cache,
277 Register tmp,
278 int bcp_offset,
279 size_t index_size) {
280 assert_different_registers(cache, tmp);
281 // register "cache" is trashed in next ld, so lets use it as a temporary register
282 get_cache_index_at_bcp(tmp, cache, bcp_offset, index_size);
283 assert(sizeof(ConstantPoolCacheEntry) == 4 * wordSize, "adjust code below");
284 // Convert from field index to ConstantPoolCacheEntry index
285 // and from word offset to byte offset
286 assert(exact_log2(in_bytes(ConstantPoolCacheEntry::size_in_bytes())) == 2 + LogBytesPerWord,
287 "else change next line");
288 ld(cache, Address(fp, frame::interpreter_frame_cache_offset * wordSize));
289 // skip past the header
290 add(cache, cache, in_bytes(ConstantPoolCache::base_offset()));
291 // construct pointer to cache entry
292 shadd(cache, tmp, cache, tmp, 2 + LogBytesPerWord);
293 }
294
295 // Load object from cpool->resolved_references(index)
296 void InterpreterMacroAssembler::load_resolved_reference_at_index(
297 Register result, Register index, Register tmp) {
298 assert_different_registers(result, index);
299
300 get_constant_pool(result);
301 // Load pointer for resolved_references[] objArray
302 ld(result, Address(result, ConstantPool::cache_offset()));
303 ld(result, Address(result, ConstantPoolCache::resolved_references_offset()));
304 resolve_oop_handle(result, tmp, t1);
305 // Add in the index
306 addi(index, index, arrayOopDesc::base_offset_in_bytes(T_OBJECT) >> LogBytesPerHeapOop);
307 shadd(result, index, result, index, LogBytesPerHeapOop);
308 load_heap_oop(result, Address(result, 0), tmp, t1);
309 }
310
311 void InterpreterMacroAssembler::load_resolved_klass_at_offset(
312 Register cpool, Register index, Register klass, Register temp) {
313 shadd(temp, index, cpool, temp, LogBytesPerWord);
314 lhu(temp, Address(temp, sizeof(ConstantPool))); // temp = resolved_klass_index
315 ld(klass, Address(cpool, ConstantPool::resolved_klasses_offset())); // klass = cpool->_resolved_klasses
316 shadd(klass, temp, klass, temp, LogBytesPerWord);
317 ld(klass, Address(klass, Array<Klass*>::base_offset_in_bytes()));
318 }
319
320 void InterpreterMacroAssembler::load_resolved_method_at_index(int byte_no,
321 Register method,
322 Register cache) {
323 const int method_offset = in_bytes(
324 ConstantPoolCache::base_offset() +
325 ((byte_no == TemplateTable::f2_byte)
326 ? ConstantPoolCacheEntry::f2_offset()
327 : ConstantPoolCacheEntry::f1_offset()));
328
329 ld(method, Address(cache, method_offset)); // get f1 Method*
330 }
331
332 // Generate a subtype check: branch to ok_is_subtype if sub_klass is a
333 // subtype of super_klass.
334 //
335 // Args:
336 // x10: superklass
337 // Rsub_klass: subklass
338 //
339 // Kills:
340 // x12, x15
341 void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass,
342 Label& ok_is_subtype) {
343 assert(Rsub_klass != x10, "x10 holds superklass");
344 assert(Rsub_klass != x12, "x12 holds 2ndary super array length");
345 assert(Rsub_klass != x15, "x15 holds 2ndary super array scan ptr");
346
347 // Profile the not-null value's klass.
348 profile_typecheck(x12, Rsub_klass, x15); // blows x12, reloads x15
349
350 // Do the check.
351 check_klass_subtype(Rsub_klass, x10, x12, ok_is_subtype); // blows x12
352
353 // Profile the failure of the check.
354 profile_typecheck_failed(x12); // blows x12
355 }
356
357 // Java Expression Stack
358
359 void InterpreterMacroAssembler::pop_ptr(Register r) {
360 ld(r, Address(esp, 0));
361 addi(esp, esp, wordSize);
362 }
363
364 void InterpreterMacroAssembler::pop_i(Register r) {
365 lw(r, Address(esp, 0)); // lw do signed extended
366 addi(esp, esp, wordSize);
367 }
368
369 void InterpreterMacroAssembler::pop_l(Register r) {
370 ld(r, Address(esp, 0));
371 addi(esp, esp, 2 * Interpreter::stackElementSize);
372 }
373
374 void InterpreterMacroAssembler::push_ptr(Register r) {
375 addi(esp, esp, -wordSize);
376 sd(r, Address(esp, 0));
377 }
378
379 void InterpreterMacroAssembler::push_i(Register r) {
380 addi(esp, esp, -wordSize);
381 sign_extend(r, r, 32);
382 sd(r, Address(esp, 0));
383 }
384
385 void InterpreterMacroAssembler::push_l(Register r) {
386 addi(esp, esp, -2 * wordSize);
387 sd(zr, Address(esp, wordSize));
388 sd(r, Address(esp));
389 }
390
391 void InterpreterMacroAssembler::pop_f(FloatRegister r) {
392 flw(r, Address(esp, 0));
393 addi(esp, esp, wordSize);
394 }
395
396 void InterpreterMacroAssembler::pop_d(FloatRegister r) {
397 fld(r, Address(esp, 0));
398 addi(esp, esp, 2 * Interpreter::stackElementSize);
399 }
400
401 void InterpreterMacroAssembler::push_f(FloatRegister r) {
402 addi(esp, esp, -wordSize);
403 fsw(r, Address(esp, 0));
404 }
405
406 void InterpreterMacroAssembler::push_d(FloatRegister r) {
407 addi(esp, esp, -2 * wordSize);
408 fsd(r, Address(esp, 0));
409 }
410
411 void InterpreterMacroAssembler::pop(TosState state) {
412 switch (state) {
413 case atos:
414 pop_ptr();
415 verify_oop(x10);
416 break;
417 case btos: // fall through
418 case ztos: // fall through
419 case ctos: // fall through
420 case stos: // fall through
421 case itos:
422 pop_i();
423 break;
424 case ltos:
425 pop_l();
426 break;
427 case ftos:
428 pop_f();
429 break;
430 case dtos:
431 pop_d();
432 break;
433 case vtos:
434 /* nothing to do */
435 break;
436 default:
437 ShouldNotReachHere();
438 }
439 }
440
441 void InterpreterMacroAssembler::push(TosState state) {
442 switch (state) {
443 case atos:
444 verify_oop(x10);
445 push_ptr();
446 break;
447 case btos: // fall through
448 case ztos: // fall through
449 case ctos: // fall through
450 case stos: // fall through
451 case itos:
452 push_i();
453 break;
454 case ltos:
455 push_l();
456 break;
457 case ftos:
458 push_f();
459 break;
460 case dtos:
461 push_d();
462 break;
463 case vtos:
464 /* nothing to do */
465 break;
466 default:
467 ShouldNotReachHere();
468 }
469 }
470
471 // Helpers for swap and dup
472 void InterpreterMacroAssembler::load_ptr(int n, Register val) {
473 ld(val, Address(esp, Interpreter::expr_offset_in_bytes(n)));
474 }
475
476 void InterpreterMacroAssembler::store_ptr(int n, Register val) {
477 sd(val, Address(esp, Interpreter::expr_offset_in_bytes(n)));
478 }
479
480 void InterpreterMacroAssembler::load_float(Address src) {
481 flw(f10, src);
482 }
483
484 void InterpreterMacroAssembler::load_double(Address src) {
485 fld(f10, src);
486 }
487
488 void InterpreterMacroAssembler::prepare_to_jump_from_interpreted() {
489 // set sender sp
490 mv(x19_sender_sp, sp);
491 // record last_sp
492 sd(esp, Address(fp, frame::interpreter_frame_last_sp_offset * wordSize));
493 }
494
495 // Jump to from_interpreted entry of a call unless single stepping is possible
496 // in this thread in which case we must call the i2i entry
497 void InterpreterMacroAssembler::jump_from_interpreted(Register method) {
498 prepare_to_jump_from_interpreted();
499 if (JvmtiExport::can_post_interpreter_events()) {
500 Label run_compiled_code;
501 // JVMTI events, such as single-stepping, are implemented partly by avoiding running
502 // compiled code in threads for which the event is enabled. Check here for
503 // interp_only_mode if these events CAN be enabled.
504 lwu(t0, Address(xthread, JavaThread::interp_only_mode_offset()));
505 beqz(t0, run_compiled_code);
506 ld(t0, Address(method, Method::interpreter_entry_offset()));
507 jr(t0);
508 bind(run_compiled_code);
509 }
510
511 ld(t0, Address(method, Method::from_interpreted_offset()));
512 jr(t0);
513 }
514
515 // The following two routines provide a hook so that an implementation
516 // can schedule the dispatch in two parts. amd64 does not do this.
517 void InterpreterMacroAssembler::dispatch_prolog(TosState state, int step) {
518 }
519
520 void InterpreterMacroAssembler::dispatch_epilog(TosState state, int step) {
521 dispatch_next(state, step);
522 }
523
524 void InterpreterMacroAssembler::dispatch_base(TosState state,
525 address* table,
526 bool verifyoop,
527 bool generate_poll,
528 Register Rs) {
529 // Pay attention to the argument Rs, which is acquiesce in t0.
530 if (VerifyActivationFrameSize) {
531 Unimplemented();
532 }
533 if (verifyoop && state == atos) {
534 verify_oop(x10);
535 }
536
537 Label safepoint;
538 address* const safepoint_table = Interpreter::safept_table(state);
539 bool needs_thread_local_poll = generate_poll && table != safepoint_table;
540
541 if (needs_thread_local_poll) {
542 NOT_PRODUCT(block_comment("Thread-local Safepoint poll"));
543 ld(t1, Address(xthread, JavaThread::polling_word_offset()));
544 test_bit(t1, t1, exact_log2(SafepointMechanism::poll_bit()));
545 bnez(t1, safepoint);
546 }
547 if (table == Interpreter::dispatch_table(state)) {
548 mv(t1, Interpreter::distance_from_dispatch_table(state));
549 add(t1, Rs, t1);
550 shadd(t1, t1, xdispatch, t1, 3);
551 } else {
552 mv(t1, (address)table);
553 shadd(t1, Rs, t1, Rs, 3);
554 }
555 ld(t1, Address(t1));
556 jr(t1);
557
558 if (needs_thread_local_poll) {
559 bind(safepoint);
560 la(t1, ExternalAddress((address)safepoint_table));
561 shadd(t1, Rs, t1, Rs, 3);
562 ld(t1, Address(t1));
563 jr(t1);
564 }
565 }
566
567 void InterpreterMacroAssembler::dispatch_only(TosState state, bool generate_poll, Register Rs) {
568 dispatch_base(state, Interpreter::dispatch_table(state), true, generate_poll, Rs);
569 }
570
571 void InterpreterMacroAssembler::dispatch_only_normal(TosState state, Register Rs) {
572 dispatch_base(state, Interpreter::normal_table(state), true, false, Rs);
573 }
574
575 void InterpreterMacroAssembler::dispatch_only_noverify(TosState state, Register Rs) {
576 dispatch_base(state, Interpreter::normal_table(state), false, false, Rs);
577 }
578
579 void InterpreterMacroAssembler::dispatch_next(TosState state, int step, bool generate_poll) {
580 // load next bytecode
581 load_unsigned_byte(t0, Address(xbcp, step));
582 add(xbcp, xbcp, step);
583 dispatch_base(state, Interpreter::dispatch_table(state), true, generate_poll);
584 }
585
586 void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) {
587 // load current bytecode
588 lbu(t0, Address(xbcp, 0));
589 dispatch_base(state, table);
590 }
591
592 // remove activation
593 //
594 // Apply stack watermark barrier.
595 // Unlock the receiver if this is a synchronized method.
596 // Unlock any Java monitors from synchronized blocks.
597 // Remove the activation from the stack.
598 //
599 // If there are locked Java monitors
600 // If throw_monitor_exception
601 // throws IllegalMonitorStateException
602 // Else if install_monitor_exception
603 // installs IllegalMonitorStateException
604 // Else
605 // no error processing
606 void InterpreterMacroAssembler::remove_activation(
607 TosState state,
608 bool throw_monitor_exception,
609 bool install_monitor_exception,
610 bool notify_jvmdi) {
611 // Note: Registers x13 may be in use for the
612 // result check if synchronized method
613 Label unlocked, unlock, no_unlock;
614
615 // The below poll is for the stack watermark barrier. It allows fixing up frames lazily,
616 // that would normally not be safe to use. Such bad returns into unsafe territory of
617 // the stack, will call InterpreterRuntime::at_unwind.
618 Label slow_path;
619 Label fast_path;
620 safepoint_poll(slow_path, true /* at_return */, false /* acquire */, false /* in_nmethod */);
621 j(fast_path);
622
623 bind(slow_path);
624 push(state);
625 set_last_Java_frame(esp, fp, (address)pc(), t0);
626 super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::at_unwind), xthread);
627 reset_last_Java_frame(true);
628 pop(state);
629
630 bind(fast_path);
631
632 // get the value of _do_not_unlock_if_synchronized into x13
633 const Address do_not_unlock_if_synchronized(xthread,
634 in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
635 lbu(x13, do_not_unlock_if_synchronized);
636 sb(zr, do_not_unlock_if_synchronized); // reset the flag
637
638 // get method access flags
639 ld(x11, Address(fp, frame::interpreter_frame_method_offset * wordSize));
640 ld(x12, Address(x11, Method::access_flags_offset()));
641 test_bit(t0, x12, exact_log2(JVM_ACC_SYNCHRONIZED));
642 beqz(t0, unlocked);
643
644 // Don't unlock anything if the _do_not_unlock_if_synchronized flag
645 // is set.
646 bnez(x13, no_unlock);
647
648 // unlock monitor
649 push(state); // save result
650
651 // BasicObjectLock will be first in list, since this is a
652 // synchronized method. However, need to check that the object has
653 // not been unlocked by an explicit monitorexit bytecode.
654 const Address monitor(fp, frame::interpreter_frame_initial_sp_offset *
655 wordSize - (int) sizeof(BasicObjectLock));
656 // We use c_rarg1 so that if we go slow path it will be the correct
657 // register for unlock_object to pass to VM directly
658 la(c_rarg1, monitor); // address of first monitor
659
660 ld(x10, Address(c_rarg1, BasicObjectLock::obj_offset()));
661 bnez(x10, unlock);
662
663 pop(state);
664 if (throw_monitor_exception) {
665 // Entry already unlocked, need to throw exception
666 call_VM(noreg, CAST_FROM_FN_PTR(address,
667 InterpreterRuntime::throw_illegal_monitor_state_exception));
668 should_not_reach_here();
669 } else {
670 // Monitor already unlocked during a stack unroll. If requested,
671 // install an illegal_monitor_state_exception. Continue with
672 // stack unrolling.
673 if (install_monitor_exception) {
674 call_VM(noreg, CAST_FROM_FN_PTR(address,
675 InterpreterRuntime::new_illegal_monitor_state_exception));
676 }
677 j(unlocked);
678 }
679
680 bind(unlock);
681 unlock_object(c_rarg1);
682 pop(state);
683
684 // Check that for block-structured locking (i.e., that all locked
685 // objects has been unlocked)
686 bind(unlocked);
687
688 // x10: Might contain return value
689
690 // Check that all monitors are unlocked
691 {
692 Label loop, exception, entry, restart;
693 const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
694 const Address monitor_block_top(
695 fp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
696 const Address monitor_block_bot(
697 fp, frame::interpreter_frame_initial_sp_offset * wordSize);
698
699 bind(restart);
700 // We use c_rarg1 so that if we go slow path it will be the correct
701 // register for unlock_object to pass to VM directly
702 ld(c_rarg1, monitor_block_top); // points to current entry, starting
703 // with top-most entry
704 la(x9, monitor_block_bot); // points to word before bottom of
705 // monitor block
706
707 j(entry);
708
709 // Entry already locked, need to throw exception
710 bind(exception);
711
712 if (throw_monitor_exception) {
713 // Throw exception
714 MacroAssembler::call_VM(noreg,
715 CAST_FROM_FN_PTR(address, InterpreterRuntime::
716 throw_illegal_monitor_state_exception));
717
718 should_not_reach_here();
719 } else {
720 // Stack unrolling. Unlock object and install illegal_monitor_exception.
721 // Unlock does not block, so don't have to worry about the frame.
722 // We don't have to preserve c_rarg1 since we are going to throw an exception.
723
724 push(state);
725 unlock_object(c_rarg1);
726 pop(state);
727
728 if (install_monitor_exception) {
729 call_VM(noreg, CAST_FROM_FN_PTR(address,
730 InterpreterRuntime::
731 new_illegal_monitor_state_exception));
732 }
733
734 j(restart);
735 }
736
737 bind(loop);
738 // check if current entry is used
739 add(t0, c_rarg1, in_bytes(BasicObjectLock::obj_offset()));
740 ld(t0, Address(t0, 0));
741 bnez(t0, exception);
742
743 add(c_rarg1, c_rarg1, entry_size); // otherwise advance to next entry
744 bind(entry);
745 bne(c_rarg1, x9, loop); // check if bottom reached if not at bottom then check this entry
746 }
747
748 bind(no_unlock);
749
750 // jvmti support
751 if (notify_jvmdi) {
752 notify_method_exit(state, NotifyJVMTI); // preserve TOSCA
753
754 } else {
755 notify_method_exit(state, SkipNotifyJVMTI); // preserve TOSCA
756 }
757
758 // remove activation
759 // get sender esp
760 ld(t1,
761 Address(fp, frame::interpreter_frame_sender_sp_offset * wordSize));
762 if (StackReservedPages > 0) {
763 // testing if reserved zone needs to be re-enabled
764 Label no_reserved_zone_enabling;
765
766 // check if already enabled - if so no re-enabling needed
767 assert(sizeof(StackOverflow::StackGuardState) == 4, "unexpected size");
768 lw(t0, Address(xthread, JavaThread::stack_guard_state_offset()));
769 subw(t0, t0, StackOverflow::stack_guard_enabled);
770 beqz(t0, no_reserved_zone_enabling);
771
772 ld(t0, Address(xthread, JavaThread::reserved_stack_activation_offset()));
773 ble(t1, t0, no_reserved_zone_enabling);
774
775 call_VM_leaf(
776 CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), xthread);
777 call_VM(noreg, CAST_FROM_FN_PTR(address,
778 InterpreterRuntime::throw_delayed_StackOverflowError));
779 should_not_reach_here();
780
781 bind(no_reserved_zone_enabling);
782 }
783
784 // restore sender esp
785 mv(esp, t1);
786
787 // remove frame anchor
788 leave();
789 // If we're returning to interpreted code we will shortly be
790 // adjusting SP to allow some space for ESP. If we're returning to
791 // compiled code the saved sender SP was saved in sender_sp, so this
792 // restores it.
793 andi(sp, esp, -16);
794 }
795
796 // Lock object
797 //
798 // Args:
799 // c_rarg1: BasicObjectLock to be used for locking
800 //
801 // Kills:
802 // x10
803 // c_rarg0, c_rarg1, c_rarg2, c_rarg3, c_rarg4, c_rarg5, .. (param regs)
804 // t0, t1 (temp regs)
805 void InterpreterMacroAssembler::lock_object(Register lock_reg)
806 {
807 assert(lock_reg == c_rarg1, "The argument is only for looks. It must be c_rarg1");
808 if (LockingMode == LM_MONITOR) {
809 call_VM(noreg,
810 CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
811 lock_reg);
812 } else {
813 Label count, done;
814
815 const Register swap_reg = x10;
816 const Register tmp = c_rarg2;
817 const Register obj_reg = c_rarg3; // Will contain the oop
818 const Register tmp2 = c_rarg4;
819 const Register tmp3 = c_rarg5;
820
821 const int obj_offset = in_bytes(BasicObjectLock::obj_offset());
822 const int lock_offset = in_bytes(BasicObjectLock::lock_offset());
823 const int mark_offset = lock_offset +
824 BasicLock::displaced_header_offset_in_bytes();
825
826 Label slow_case;
827
828 // Load object pointer into obj_reg c_rarg3
829 ld(obj_reg, Address(lock_reg, obj_offset));
830
831 if (DiagnoseSyncOnValueBasedClasses != 0) {
832 load_klass(tmp, obj_reg);
833 lwu(tmp, Address(tmp, Klass::access_flags_offset()));
834 test_bit(tmp, tmp, exact_log2(JVM_ACC_IS_VALUE_BASED_CLASS));
835 bnez(tmp, slow_case);
836 }
837
838 if (LockingMode == LM_LIGHTWEIGHT) {
839 ld(tmp, Address(obj_reg, oopDesc::mark_offset_in_bytes()));
840 lightweight_lock(obj_reg, tmp, tmp2, tmp3, slow_case);
841 j(count);
842 } else if (LockingMode == LM_LEGACY) {
843 // Load (object->mark() | 1) into swap_reg
844 ld(t0, Address(obj_reg, oopDesc::mark_offset_in_bytes()));
845 ori(swap_reg, t0, 1);
846
847 // Save (object->mark() | 1) into BasicLock's displaced header
848 sd(swap_reg, Address(lock_reg, mark_offset));
849
850 assert(lock_offset == 0,
851 "displached header must be first word in BasicObjectLock");
852
853 cmpxchg_obj_header(swap_reg, lock_reg, obj_reg, t0, count, /*fallthrough*/nullptr);
854
855 // Test if the oopMark is an obvious stack pointer, i.e.,
856 // 1) (mark & 7) == 0, and
857 // 2) sp <= mark < mark + os::pagesize()
858 //
859 // These 3 tests can be done by evaluating the following
860 // expression: ((mark - sp) & (7 - os::vm_page_size())),
861 // assuming both stack pointer and pagesize have their
862 // least significant 3 bits clear.
863 // NOTE: the oopMark is in swap_reg x10 as the result of cmpxchg
864 sub(swap_reg, swap_reg, sp);
865 mv(t0, (int64_t)(7 - (int)os::vm_page_size()));
866 andr(swap_reg, swap_reg, t0);
867
868 // Save the test result, for recursive case, the result is zero
869 sd(swap_reg, Address(lock_reg, mark_offset));
870 beqz(swap_reg, count);
871 }
872
873 bind(slow_case);
874
875 // Call the runtime routine for slow case
876 if (LockingMode == LM_LIGHTWEIGHT) {
877 call_VM(noreg,
878 CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter_obj),
879 obj_reg);
880 } else {
881 call_VM(noreg,
882 CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
883 lock_reg);
884 }
885 j(done);
886
887 bind(count);
888 increment(Address(xthread, JavaThread::held_monitor_count_offset()));
889
890 bind(done);
891 }
892 }
893
894
895 // Unlocks an object. Used in monitorexit bytecode and
896 // remove_activation. Throws an IllegalMonitorException if object is
897 // not locked by current thread.
898 //
899 // Args:
900 // c_rarg1: BasicObjectLock for lock
901 //
902 // Kills:
903 // x10
904 // c_rarg0, c_rarg1, c_rarg2, c_rarg3, c_rarg4, ... (param regs)
905 // t0, t1 (temp regs)
906 void InterpreterMacroAssembler::unlock_object(Register lock_reg)
907 {
908 assert(lock_reg == c_rarg1, "The argument is only for looks. It must be rarg1");
909
910 if (LockingMode == LM_MONITOR) {
911 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
912 } else {
913 Label count, done;
914
915 const Register swap_reg = x10;
916 const Register header_reg = c_rarg2; // Will contain the old oopMark
917 const Register obj_reg = c_rarg3; // Will contain the oop
918 const Register tmp_reg = c_rarg4; // Temporary used by lightweight_unlock
919
920 save_bcp(); // Save in case of exception
921
922 if (LockingMode != LM_LIGHTWEIGHT) {
923 // Convert from BasicObjectLock structure to object and BasicLock
924 // structure Store the BasicLock address into x10
925 la(swap_reg, Address(lock_reg, BasicObjectLock::lock_offset()));
926 }
927
928 // Load oop into obj_reg(c_rarg3)
929 ld(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset()));
930
931 // Free entry
932 sd(zr, Address(lock_reg, BasicObjectLock::obj_offset()));
933
934 if (LockingMode == LM_LIGHTWEIGHT) {
935 Label slow_case;
936
937 // Check for non-symmetric locking. This is allowed by the spec and the interpreter
938 // must handle it.
939 Register tmp1 = t0;
940 Register tmp2 = header_reg;
941 // First check for lock-stack underflow.
942 lwu(tmp1, Address(xthread, JavaThread::lock_stack_top_offset()));
943 mv(tmp2, (unsigned)LockStack::start_offset());
944 ble(tmp1, tmp2, slow_case);
945 // Then check if the top of the lock-stack matches the unlocked object.
946 subw(tmp1, tmp1, oopSize);
947 add(tmp1, xthread, tmp1);
948 ld(tmp1, Address(tmp1, 0));
949 bne(tmp1, obj_reg, slow_case);
950
951 ld(header_reg, Address(obj_reg, oopDesc::mark_offset_in_bytes()));
952 test_bit(t0, header_reg, exact_log2(markWord::monitor_value));
953 bnez(t0, slow_case);
954 lightweight_unlock(obj_reg, header_reg, swap_reg, tmp_reg, slow_case);
955 j(count);
956
957 bind(slow_case);
958 } else if (LockingMode == LM_LEGACY) {
959 // Load the old header from BasicLock structure
960 ld(header_reg, Address(swap_reg,
961 BasicLock::displaced_header_offset_in_bytes()));
962
963 // Test for recursion
964 beqz(header_reg, count);
965
966 // Atomic swap back the old header
967 cmpxchg_obj_header(swap_reg, header_reg, obj_reg, t0, count, /*fallthrough*/nullptr);
968 }
969
970 // Call the runtime routine for slow case.
971 sd(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset())); // restore obj
972 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
973
974 j(done);
975
976 bind(count);
977 decrement(Address(xthread, JavaThread::held_monitor_count_offset()));
978
979 bind(done);
980
981 restore_bcp();
982 }
983 }
984
985
986 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp,
987 Label& zero_continue) {
988 assert(ProfileInterpreter, "must be profiling interpreter");
989 ld(mdp, Address(fp, frame::interpreter_frame_mdp_offset * wordSize));
990 beqz(mdp, zero_continue);
991 }
992
993 // Set the method data pointer for the current bcp.
994 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
995 assert(ProfileInterpreter, "must be profiling interpreter");
996 Label set_mdp;
997 push_reg(RegSet::of(x10, x11), sp); // save x10, x11
998
999 // Test MDO to avoid the call if it is null.
1000 ld(x10, Address(xmethod, in_bytes(Method::method_data_offset())));
1001 beqz(x10, set_mdp);
1002 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), xmethod, xbcp);
1003 // x10: mdi
1004 // mdo is guaranteed to be non-zero here, we checked for it before the call.
1005 ld(x11, Address(xmethod, in_bytes(Method::method_data_offset())));
1006 la(x11, Address(x11, in_bytes(MethodData::data_offset())));
1007 add(x10, x11, x10);
1008 sd(x10, Address(fp, frame::interpreter_frame_mdp_offset * wordSize));
1009 bind(set_mdp);
1010 pop_reg(RegSet::of(x10, x11), sp);
1011 }
1012
1013 void InterpreterMacroAssembler::verify_method_data_pointer() {
1014 assert(ProfileInterpreter, "must be profiling interpreter");
1015 #ifdef ASSERT
1016 Label verify_continue;
1017 add(sp, sp, -4 * wordSize);
1018 sd(x10, Address(sp, 0));
1019 sd(x11, Address(sp, wordSize));
1020 sd(x12, Address(sp, 2 * wordSize));
1021 sd(x13, Address(sp, 3 * wordSize));
1022 test_method_data_pointer(x13, verify_continue); // If mdp is zero, continue
1023 get_method(x11);
1024
1025 // If the mdp is valid, it will point to a DataLayout header which is
1026 // consistent with the bcp. The converse is highly probable also.
1027 lh(x12, Address(x13, in_bytes(DataLayout::bci_offset())));
1028 ld(t0, Address(x11, Method::const_offset()));
1029 add(x12, x12, t0);
1030 la(x12, Address(x12, ConstMethod::codes_offset()));
1031 beq(x12, xbcp, verify_continue);
1032 // x10: method
1033 // xbcp: bcp // xbcp == 22
1034 // x13: mdp
1035 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp),
1036 x11, xbcp, x13);
1037 bind(verify_continue);
1038 ld(x10, Address(sp, 0));
1039 ld(x11, Address(sp, wordSize));
1040 ld(x12, Address(sp, 2 * wordSize));
1041 ld(x13, Address(sp, 3 * wordSize));
1042 add(sp, sp, 4 * wordSize);
1043 #endif // ASSERT
1044 }
1045
1046
1047 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in,
1048 int constant,
1049 Register value) {
1050 assert(ProfileInterpreter, "must be profiling interpreter");
1051 Address data(mdp_in, constant);
1052 sd(value, data);
1053 }
1054
1055
1056 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
1057 int constant,
1058 bool decrement) {
1059 increment_mdp_data_at(mdp_in, noreg, constant, decrement);
1060 }
1061
1062 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
1063 Register reg,
1064 int constant,
1065 bool decrement) {
1066 assert(ProfileInterpreter, "must be profiling interpreter");
1067 // %%% this does 64bit counters at best it is wasting space
1068 // at worst it is a rare bug when counters overflow
1069
1070 assert_different_registers(t1, t0, mdp_in, reg);
1071
1072 Address addr1(mdp_in, constant);
1073 Address addr2(t1, 0);
1074 Address &addr = addr1;
1075 if (reg != noreg) {
1076 la(t1, addr1);
1077 add(t1, t1, reg);
1078 addr = addr2;
1079 }
1080
1081 if (decrement) {
1082 ld(t0, addr);
1083 addi(t0, t0, -DataLayout::counter_increment);
1084 Label L;
1085 bltz(t0, L); // skip store if counter underflow
1086 sd(t0, addr);
1087 bind(L);
1088 } else {
1089 assert(DataLayout::counter_increment == 1,
1090 "flow-free idiom only works with 1");
1091 ld(t0, addr);
1092 addi(t0, t0, DataLayout::counter_increment);
1093 Label L;
1094 blez(t0, L); // skip store if counter overflow
1095 sd(t0, addr);
1096 bind(L);
1097 }
1098 }
1099
1100 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in,
1101 int flag_byte_constant) {
1102 assert(ProfileInterpreter, "must be profiling interpreter");
1103 int flags_offset = in_bytes(DataLayout::flags_offset());
1104 // Set the flag
1105 lbu(t1, Address(mdp_in, flags_offset));
1106 ori(t1, t1, flag_byte_constant);
1107 sb(t1, Address(mdp_in, flags_offset));
1108 }
1109
1110
1111 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in,
1112 int offset,
1113 Register value,
1114 Register test_value_out,
1115 Label& not_equal_continue) {
1116 assert(ProfileInterpreter, "must be profiling interpreter");
1117 if (test_value_out == noreg) {
1118 ld(t1, Address(mdp_in, offset));
1119 bne(value, t1, not_equal_continue);
1120 } else {
1121 // Put the test value into a register, so caller can use it:
1122 ld(test_value_out, Address(mdp_in, offset));
1123 bne(value, test_value_out, not_equal_continue);
1124 }
1125 }
1126
1127
1128 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
1129 int offset_of_disp) {
1130 assert(ProfileInterpreter, "must be profiling interpreter");
1131 ld(t1, Address(mdp_in, offset_of_disp));
1132 add(mdp_in, mdp_in, t1);
1133 sd(mdp_in, Address(fp, frame::interpreter_frame_mdp_offset * wordSize));
1134 }
1135
1136 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
1137 Register reg,
1138 int offset_of_disp) {
1139 assert(ProfileInterpreter, "must be profiling interpreter");
1140 add(t1, mdp_in, reg);
1141 ld(t1, Address(t1, offset_of_disp));
1142 add(mdp_in, mdp_in, t1);
1143 sd(mdp_in, Address(fp, frame::interpreter_frame_mdp_offset * wordSize));
1144 }
1145
1146
1147 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in,
1148 int constant) {
1149 assert(ProfileInterpreter, "must be profiling interpreter");
1150 addi(mdp_in, mdp_in, (unsigned)constant);
1151 sd(mdp_in, Address(fp, frame::interpreter_frame_mdp_offset * wordSize));
1152 }
1153
1154
1155 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) {
1156 assert(ProfileInterpreter, "must be profiling interpreter");
1157
1158 // save/restore across call_VM
1159 addi(sp, sp, -2 * wordSize);
1160 sd(zr, Address(sp, 0));
1161 sd(return_bci, Address(sp, wordSize));
1162 call_VM(noreg,
1163 CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret),
1164 return_bci);
1165 ld(zr, Address(sp, 0));
1166 ld(return_bci, Address(sp, wordSize));
1167 addi(sp, sp, 2 * wordSize);
1168 }
1169
1170 void InterpreterMacroAssembler::profile_taken_branch(Register mdp,
1171 Register bumped_count) {
1172 if (ProfileInterpreter) {
1173 Label profile_continue;
1174
1175 // If no method data exists, go to profile_continue.
1176 // Otherwise, assign to mdp
1177 test_method_data_pointer(mdp, profile_continue);
1178
1179 // We are taking a branch. Increment the taken count.
1180 Address data(mdp, in_bytes(JumpData::taken_offset()));
1181 ld(bumped_count, data);
1182 assert(DataLayout::counter_increment == 1,
1183 "flow-free idiom only works with 1");
1184 addi(bumped_count, bumped_count, DataLayout::counter_increment);
1185 Label L;
1186 // eg: bumped_count=0x7fff ffff ffff ffff + 1 < 0. so we use <= 0;
1187 blez(bumped_count, L); // skip store if counter overflow,
1188 sd(bumped_count, data);
1189 bind(L);
1190 // The method data pointer needs to be updated to reflect the new target.
1191 update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset()));
1192 bind(profile_continue);
1193 }
1194 }
1195
1196 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp) {
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 // We are taking a branch. Increment the not taken count.
1204 increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset()));
1205
1206 // The method data pointer needs to be updated to correspond to
1207 // the next bytecode
1208 update_mdp_by_constant(mdp, in_bytes(BranchData::branch_data_size()));
1209 bind(profile_continue);
1210 }
1211 }
1212
1213 void InterpreterMacroAssembler::profile_call(Register mdp) {
1214 if (ProfileInterpreter) {
1215 Label profile_continue;
1216
1217 // If no method data exists, go to profile_continue.
1218 test_method_data_pointer(mdp, profile_continue);
1219
1220 // We are making a call. Increment the count.
1221 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1222
1223 // The method data pointer needs to be updated to reflect the new target.
1224 update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size()));
1225 bind(profile_continue);
1226 }
1227 }
1228
1229 void InterpreterMacroAssembler::profile_final_call(Register mdp) {
1230 if (ProfileInterpreter) {
1231 Label profile_continue;
1232
1233 // If no method data exists, go to profile_continue.
1234 test_method_data_pointer(mdp, profile_continue);
1235
1236 // We are making a call. Increment the count.
1237 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1238
1239 // The method data pointer needs to be updated to reflect the new target.
1240 update_mdp_by_constant(mdp,
1241 in_bytes(VirtualCallData::
1242 virtual_call_data_size()));
1243 bind(profile_continue);
1244 }
1245 }
1246
1247
1248 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
1249 Register mdp,
1250 Register reg2,
1251 bool receiver_can_be_null) {
1252 if (ProfileInterpreter) {
1253 Label profile_continue;
1254
1255 // If no method data exists, go to profile_continue.
1256 test_method_data_pointer(mdp, profile_continue);
1257
1258 Label skip_receiver_profile;
1259 if (receiver_can_be_null) {
1260 Label not_null;
1261 // We are making a call. Increment the count for null receiver.
1262 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1263 j(skip_receiver_profile);
1264 bind(not_null);
1265 }
1266
1267 // Record the receiver type.
1268 record_klass_in_profile(receiver, mdp, reg2, true);
1269 bind(skip_receiver_profile);
1270
1271 // The method data pointer needs to be updated to reflect the new target.
1272
1273 update_mdp_by_constant(mdp,
1274 in_bytes(VirtualCallData::
1275 virtual_call_data_size()));
1276 bind(profile_continue);
1277 }
1278 }
1279
1280 // This routine creates a state machine for updating the multi-row
1281 // type profile at a virtual call site (or other type-sensitive bytecode).
1282 // The machine visits each row (of receiver/count) until the receiver type
1283 // is found, or until it runs out of rows. At the same time, it remembers
1284 // the location of the first empty row. (An empty row records null for its
1285 // receiver, and can be allocated for a newly-observed receiver type.)
1286 // Because there are two degrees of freedom in the state, a simple linear
1287 // search will not work; it must be a decision tree. Hence this helper
1288 // function is recursive, to generate the required tree structured code.
1289 // It's the interpreter, so we are trading off code space for speed.
1290 // See below for example code.
1291 void InterpreterMacroAssembler::record_klass_in_profile_helper(
1292 Register receiver, Register mdp,
1293 Register reg2,
1294 Label& done, bool is_virtual_call) {
1295 if (TypeProfileWidth == 0) {
1296 if (is_virtual_call) {
1297 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1298 }
1299
1300 } else {
1301 int non_profiled_offset = -1;
1302 if (is_virtual_call) {
1303 non_profiled_offset = in_bytes(CounterData::count_offset());
1304 }
1305
1306 record_item_in_profile_helper(receiver, mdp, reg2, 0, done, TypeProfileWidth,
1307 &VirtualCallData::receiver_offset, &VirtualCallData::receiver_count_offset, non_profiled_offset);
1308 }
1309 }
1310
1311 void InterpreterMacroAssembler::record_item_in_profile_helper(
1312 Register item, Register mdp, Register reg2, int start_row, Label& done, int total_rows,
1313 OffsetFunction item_offset_fn, OffsetFunction item_count_offset_fn, int non_profiled_offset) {
1314 int last_row = total_rows - 1;
1315 assert(start_row <= last_row, "must be work left to do");
1316 // Test this row for both the item and for null.
1317 // Take any of three different outcomes:
1318 // 1. found item => increment count and goto done
1319 // 2. found null => keep looking for case 1, maybe allocate this cell
1320 // 3. found something else => keep looking for cases 1 and 2
1321 // Case 3 is handled by a recursive call.
1322 for (int row = start_row; row <= last_row; row++) {
1323 Label next_test;
1324 bool test_for_null_also = (row == start_row);
1325
1326 // See if the item is item[n].
1327 int item_offset = in_bytes(item_offset_fn(row));
1328 test_mdp_data_at(mdp, item_offset, item,
1329 (test_for_null_also ? reg2 : noreg),
1330 next_test);
1331 // (Reg2 now contains the item from the CallData.)
1332
1333 // The item is item[n]. Increment count[n].
1334 int count_offset = in_bytes(item_count_offset_fn(row));
1335 increment_mdp_data_at(mdp, count_offset);
1336 j(done);
1337 bind(next_test);
1338
1339 if (test_for_null_also) {
1340 Label found_null;
1341 // Failed the equality check on item[n]... Test for null.
1342 if (start_row == last_row) {
1343 // The only thing left to do is handle the null case.
1344 if (non_profiled_offset >= 0) {
1345 beqz(reg2, found_null);
1346 // Item did not match any saved item and there is no empty row for it.
1347 // Increment total counter to indicate polymorphic case.
1348 increment_mdp_data_at(mdp, non_profiled_offset);
1349 j(done);
1350 bind(found_null);
1351 } else {
1352 bnez(reg2, done);
1353 }
1354 break;
1355 }
1356 // Since null is rare, make it be the branch-taken case.
1357 beqz(reg2, found_null);
1358
1359 // Put all the "Case 3" tests here.
1360 record_item_in_profile_helper(item, mdp, reg2, start_row + 1, done, total_rows,
1361 item_offset_fn, item_count_offset_fn, non_profiled_offset);
1362
1363 // Found a null. Keep searching for a matching item,
1364 // but remember that this is an empty (unused) slot.
1365 bind(found_null);
1366 }
1367 }
1368
1369 // In the fall-through case, we found no matching item, but we
1370 // observed the item[start_row] is null.
1371 // Fill in the item field and increment the count.
1372 int item_offset = in_bytes(item_offset_fn(start_row));
1373 set_mdp_data_at(mdp, item_offset, item);
1374 int count_offset = in_bytes(item_count_offset_fn(start_row));
1375 mv(reg2, DataLayout::counter_increment);
1376 set_mdp_data_at(mdp, count_offset, reg2);
1377 if (start_row > 0) {
1378 j(done);
1379 }
1380 }
1381
1382 // Example state machine code for three profile rows:
1383 // # main copy of decision tree, rooted at row[1]
1384 // if (row[0].rec == rec) then [
1385 // row[0].incr()
1386 // goto done
1387 // ]
1388 // if (row[0].rec != nullptr) then [
1389 // # inner copy of decision tree, rooted at row[1]
1390 // if (row[1].rec == rec) then [
1391 // row[1].incr()
1392 // goto done
1393 // ]
1394 // if (row[1].rec != nullptr) then [
1395 // # degenerate decision tree, rooted at row[2]
1396 // if (row[2].rec == rec) then [
1397 // row[2].incr()
1398 // goto done
1399 // ]
1400 // if (row[2].rec != nullptr) then [
1401 // count.incr()
1402 // goto done
1403 // ] # overflow
1404 // row[2].init(rec)
1405 // goto done
1406 // ] else [
1407 // # remember row[1] is empty
1408 // if (row[2].rec == rec) then [
1409 // row[2].incr()
1410 // goto done
1411 // ]
1412 // row[1].init(rec)
1413 // goto done
1414 // ]
1415 // else [
1416 // # remember row[0] is empty
1417 // if (row[1].rec == rec) then [
1418 // row[1].incr()
1419 // goto done
1420 // ]
1421 // if (row[2].rec == rec) then [
1422 // row[2].incr()
1423 // goto done
1424 // ]
1425 // row[0].init(rec)
1426 // goto done
1427 // ]
1428 // done:
1429
1430 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1431 Register mdp, Register reg2,
1432 bool is_virtual_call) {
1433 assert(ProfileInterpreter, "must be profiling");
1434 Label done;
1435
1436 record_klass_in_profile_helper(receiver, mdp, reg2, done, is_virtual_call);
1437
1438 bind(done);
1439 }
1440
1441 void InterpreterMacroAssembler::profile_ret(Register return_bci, Register mdp) {
1442 if (ProfileInterpreter) {
1443 Label profile_continue;
1444
1445 // If no method data exists, go to profile_continue.
1446 test_method_data_pointer(mdp, profile_continue);
1447
1448 // Update the total ret count.
1449 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1450
1451 for (uint row = 0; row < RetData::row_limit(); row++) {
1452 Label next_test;
1453
1454 // See if return_bci is equal to bci[n]:
1455 test_mdp_data_at(mdp,
1456 in_bytes(RetData::bci_offset(row)),
1457 return_bci, noreg,
1458 next_test);
1459
1460 // return_bci is equal to bci[n]. Increment the count.
1461 increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row)));
1462
1463 // The method data pointer needs to be updated to reflect the new target.
1464 update_mdp_by_offset(mdp,
1465 in_bytes(RetData::bci_displacement_offset(row)));
1466 j(profile_continue);
1467 bind(next_test);
1468 }
1469
1470 update_mdp_for_ret(return_bci);
1471
1472 bind(profile_continue);
1473 }
1474 }
1475
1476 void InterpreterMacroAssembler::profile_null_seen(Register mdp) {
1477 if (ProfileInterpreter) {
1478 Label profile_continue;
1479
1480 // If no method data exists, go to profile_continue.
1481 test_method_data_pointer(mdp, profile_continue);
1482
1483 set_mdp_flag_at(mdp, BitData::null_seen_byte_constant());
1484
1485 // The method data pointer needs to be updated.
1486 int mdp_delta = in_bytes(BitData::bit_data_size());
1487 if (TypeProfileCasts) {
1488 mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1489 }
1490 update_mdp_by_constant(mdp, mdp_delta);
1491
1492 bind(profile_continue);
1493 }
1494 }
1495
1496 void InterpreterMacroAssembler::profile_typecheck_failed(Register mdp) {
1497 if (ProfileInterpreter && TypeProfileCasts) {
1498 Label profile_continue;
1499
1500 // If no method data exists, go to profile_continue.
1501 test_method_data_pointer(mdp, profile_continue);
1502
1503 int count_offset = in_bytes(CounterData::count_offset());
1504 // Back up the address, since we have already bumped the mdp.
1505 count_offset -= in_bytes(VirtualCallData::virtual_call_data_size());
1506
1507 // *Decrement* the counter. We expect to see zero or small negatives.
1508 increment_mdp_data_at(mdp, count_offset, true);
1509
1510 bind (profile_continue);
1511 }
1512 }
1513
1514 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass, Register reg2) {
1515 if (ProfileInterpreter) {
1516 Label profile_continue;
1517
1518 // If no method data exists, go to profile_continue.
1519 test_method_data_pointer(mdp, profile_continue);
1520
1521 // The method data pointer needs to be updated.
1522 int mdp_delta = in_bytes(BitData::bit_data_size());
1523 if (TypeProfileCasts) {
1524 mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1525
1526 // Record the object type.
1527 record_klass_in_profile(klass, mdp, reg2, false);
1528 }
1529 update_mdp_by_constant(mdp, mdp_delta);
1530
1531 bind(profile_continue);
1532 }
1533 }
1534
1535 void InterpreterMacroAssembler::profile_switch_default(Register mdp) {
1536 if (ProfileInterpreter) {
1537 Label profile_continue;
1538
1539 // If no method data exists, go to profile_continue.
1540 test_method_data_pointer(mdp, profile_continue);
1541
1542 // Update the default case count
1543 increment_mdp_data_at(mdp,
1544 in_bytes(MultiBranchData::default_count_offset()));
1545
1546 // The method data pointer needs to be updated.
1547 update_mdp_by_offset(mdp,
1548 in_bytes(MultiBranchData::
1549 default_displacement_offset()));
1550
1551 bind(profile_continue);
1552 }
1553 }
1554
1555 void InterpreterMacroAssembler::profile_switch_case(Register index,
1556 Register mdp,
1557 Register reg2) {
1558 if (ProfileInterpreter) {
1559 Label profile_continue;
1560
1561 // If no method data exists, go to profile_continue.
1562 test_method_data_pointer(mdp, profile_continue);
1563
1564 // Build the base (index * per_case_size_in_bytes()) +
1565 // case_array_offset_in_bytes()
1566 mv(reg2, in_bytes(MultiBranchData::per_case_size()));
1567 mv(t0, in_bytes(MultiBranchData::case_array_offset()));
1568 Assembler::mul(index, index, reg2);
1569 Assembler::add(index, index, t0);
1570
1571 // Update the case count
1572 increment_mdp_data_at(mdp,
1573 index,
1574 in_bytes(MultiBranchData::relative_count_offset()));
1575
1576 // The method data pointer need to be updated.
1577 update_mdp_by_offset(mdp,
1578 index,
1579 in_bytes(MultiBranchData::
1580 relative_displacement_offset()));
1581
1582 bind(profile_continue);
1583 }
1584 }
1585
1586 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) { ; }
1587
1588 void InterpreterMacroAssembler::notify_method_entry() {
1589 // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1590 // track stack depth. If it is possible to enter interp_only_mode we add
1591 // the code to check if the event should be sent.
1592 if (JvmtiExport::can_post_interpreter_events()) {
1593 Label L;
1594 lwu(x13, Address(xthread, JavaThread::interp_only_mode_offset()));
1595 beqz(x13, L);
1596 call_VM(noreg, CAST_FROM_FN_PTR(address,
1597 InterpreterRuntime::post_method_entry));
1598 bind(L);
1599 }
1600
1601 {
1602 SkipIfEqual skip(this, &DTraceMethodProbes, false);
1603 get_method(c_rarg1);
1604 call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
1605 xthread, c_rarg1);
1606 }
1607
1608 // RedefineClasses() tracing support for obsolete method entry
1609 if (log_is_enabled(Trace, redefine, class, obsolete)) {
1610 get_method(c_rarg1);
1611 call_VM_leaf(
1612 CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
1613 xthread, c_rarg1);
1614 }
1615 }
1616
1617
1618 void InterpreterMacroAssembler::notify_method_exit(
1619 TosState state, NotifyMethodExitMode mode) {
1620 // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1621 // track stack depth. If it is possible to enter interp_only_mode we add
1622 // the code to check if the event should be sent.
1623 if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
1624 Label L;
1625 // Note: frame::interpreter_frame_result has a dependency on how the
1626 // method result is saved across the call to post_method_exit. If this
1627 // is changed then the interpreter_frame_result implementation will
1628 // need to be updated too.
1629
1630 // template interpreter will leave the result on the top of the stack.
1631 push(state);
1632 lwu(x13, Address(xthread, JavaThread::interp_only_mode_offset()));
1633 beqz(x13, L);
1634 call_VM(noreg,
1635 CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
1636 bind(L);
1637 pop(state);
1638 }
1639
1640 {
1641 SkipIfEqual skip(this, &DTraceMethodProbes, false);
1642 push(state);
1643 get_method(c_rarg1);
1644 call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
1645 xthread, c_rarg1);
1646 pop(state);
1647 }
1648 }
1649
1650
1651 // Jump if ((*counter_addr += increment) & mask) satisfies the condition.
1652 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr,
1653 int increment, Address mask,
1654 Register tmp1, Register tmp2,
1655 bool preloaded, Label* where) {
1656 Label done;
1657 if (!preloaded) {
1658 lwu(tmp1, counter_addr);
1659 }
1660 add(tmp1, tmp1, increment);
1661 sw(tmp1, counter_addr);
1662 lwu(tmp2, mask);
1663 andr(tmp1, tmp1, tmp2);
1664 bnez(tmp1, done);
1665 j(*where); // offset is too large so we have to use j instead of beqz here
1666 bind(done);
1667 }
1668
1669 void InterpreterMacroAssembler::call_VM_leaf_base(address entry_point,
1670 int number_of_arguments) {
1671 // interpreter specific
1672 //
1673 // Note: No need to save/restore rbcp & rlocals pointer since these
1674 // are callee saved registers and no blocking/ GC can happen
1675 // in leaf calls.
1676 #ifdef ASSERT
1677 {
1678 Label L;
1679 ld(t0, Address(fp, frame::interpreter_frame_last_sp_offset * wordSize));
1680 beqz(t0, L);
1681 stop("InterpreterMacroAssembler::call_VM_leaf_base:"
1682 " last_sp isn't null");
1683 bind(L);
1684 }
1685 #endif /* ASSERT */
1686 // super call
1687 MacroAssembler::call_VM_leaf_base(entry_point, number_of_arguments);
1688 }
1689
1690 void InterpreterMacroAssembler::call_VM_base(Register oop_result,
1691 Register java_thread,
1692 Register last_java_sp,
1693 address entry_point,
1694 int number_of_arguments,
1695 bool check_exceptions) {
1696 // interpreter specific
1697 //
1698 // Note: Could avoid restoring locals ptr (callee saved) - however doesn't
1699 // really make a difference for these runtime calls, since they are
1700 // slow anyway. Btw., bcp must be saved/restored since it may change
1701 // due to GC.
1702 save_bcp();
1703 #ifdef ASSERT
1704 {
1705 Label L;
1706 ld(t0, Address(fp, frame::interpreter_frame_last_sp_offset * wordSize));
1707 beqz(t0, L);
1708 stop("InterpreterMacroAssembler::call_VM_base:"
1709 " last_sp isn't null");
1710 bind(L);
1711 }
1712 #endif /* ASSERT */
1713 // super call
1714 MacroAssembler::call_VM_base(oop_result, noreg, last_java_sp,
1715 entry_point, number_of_arguments,
1716 check_exceptions);
1717 // interpreter specific
1718 restore_bcp();
1719 restore_locals();
1720 }
1721
1722 void InterpreterMacroAssembler::profile_obj_type(Register obj, const Address& mdo_addr, Register tmp) {
1723 assert_different_registers(obj, tmp, t0, mdo_addr.base());
1724 Label update, next, none;
1725
1726 verify_oop(obj);
1727
1728 bnez(obj, update);
1729 orptr(mdo_addr, TypeEntries::null_seen, t0, tmp);
1730 j(next);
1731
1732 bind(update);
1733 load_klass(obj, obj);
1734
1735 ld(tmp, mdo_addr);
1736 xorr(obj, obj, tmp);
1737 andi(t0, obj, TypeEntries::type_klass_mask);
1738 beqz(t0, next); // klass seen before, nothing to
1739 // do. The unknown bit may have been
1740 // set already but no need to check.
1741
1742 test_bit(t0, obj, exact_log2(TypeEntries::type_unknown));
1743 bnez(t0, next);
1744 // already unknown. Nothing to do anymore.
1745
1746 beqz(tmp, none);
1747 mv(t0, (u1)TypeEntries::null_seen);
1748 beq(tmp, t0, none);
1749 // There is a chance that the checks above
1750 // fail if another thread has just set the
1751 // profiling to this obj's klass
1752 xorr(obj, obj, tmp); // get back original value before XOR
1753 ld(tmp, mdo_addr);
1754 xorr(obj, obj, tmp);
1755 andi(t0, obj, TypeEntries::type_klass_mask);
1756 beqz(t0, next);
1757
1758 // different than before. Cannot keep accurate profile.
1759 orptr(mdo_addr, TypeEntries::type_unknown, t0, tmp);
1760 j(next);
1761
1762 bind(none);
1763 // first time here. Set profile type.
1764 sd(obj, mdo_addr);
1765 #ifdef ASSERT
1766 andi(obj, obj, TypeEntries::type_mask);
1767 verify_klass_ptr(obj);
1768 #endif
1769
1770 bind(next);
1771 }
1772
1773 void InterpreterMacroAssembler::profile_arguments_type(Register mdp, Register callee, Register tmp, bool is_virtual) {
1774 if (!ProfileInterpreter) {
1775 return;
1776 }
1777
1778 if (MethodData::profile_arguments() || MethodData::profile_return()) {
1779 Label profile_continue;
1780
1781 test_method_data_pointer(mdp, profile_continue);
1782
1783 int off_to_start = is_virtual ? in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
1784
1785 lbu(t0, Address(mdp, in_bytes(DataLayout::tag_offset()) - off_to_start));
1786 if (is_virtual) {
1787 mv(tmp, (u1)DataLayout::virtual_call_type_data_tag);
1788 bne(t0, tmp, profile_continue);
1789 } else {
1790 mv(tmp, (u1)DataLayout::call_type_data_tag);
1791 bne(t0, tmp, profile_continue);
1792 }
1793
1794 // calculate slot step
1795 static int stack_slot_offset0 = in_bytes(TypeEntriesAtCall::stack_slot_offset(0));
1796 static int slot_step = in_bytes(TypeEntriesAtCall::stack_slot_offset(1)) - stack_slot_offset0;
1797
1798 // calculate type step
1799 static int argument_type_offset0 = in_bytes(TypeEntriesAtCall::argument_type_offset(0));
1800 static int type_step = in_bytes(TypeEntriesAtCall::argument_type_offset(1)) - argument_type_offset0;
1801
1802 if (MethodData::profile_arguments()) {
1803 Label done, loop, loopEnd, profileArgument, profileReturnType;
1804 RegSet pushed_registers;
1805 pushed_registers += x15;
1806 pushed_registers += x16;
1807 pushed_registers += x17;
1808 Register mdo_addr = x15;
1809 Register index = x16;
1810 Register off_to_args = x17;
1811 push_reg(pushed_registers, sp);
1812
1813 mv(off_to_args, in_bytes(TypeEntriesAtCall::args_data_offset()));
1814 mv(t0, TypeProfileArgsLimit);
1815 beqz(t0, loopEnd);
1816
1817 mv(index, zr); // index < TypeProfileArgsLimit
1818 bind(loop);
1819 bgtz(index, profileReturnType);
1820 mv(t0, (int)MethodData::profile_return());
1821 beqz(t0, profileArgument); // (index > 0 || MethodData::profile_return()) == false
1822 bind(profileReturnType);
1823 // If return value type is profiled we may have no argument to profile
1824 ld(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())));
1825 mv(t1, - TypeStackSlotEntries::per_arg_count());
1826 mul(t1, index, t1);
1827 add(tmp, tmp, t1);
1828 mv(t1, TypeStackSlotEntries::per_arg_count());
1829 add(t0, mdp, off_to_args);
1830 blt(tmp, t1, done);
1831
1832 bind(profileArgument);
1833
1834 ld(tmp, Address(callee, Method::const_offset()));
1835 load_unsigned_short(tmp, Address(tmp, ConstMethod::size_of_parameters_offset()));
1836 // stack offset o (zero based) from the start of the argument
1837 // list, for n arguments translates into offset n - o - 1 from
1838 // the end of the argument list
1839 mv(t0, stack_slot_offset0);
1840 mv(t1, slot_step);
1841 mul(t1, index, t1);
1842 add(t0, t0, t1);
1843 add(t0, mdp, t0);
1844 ld(t0, Address(t0));
1845 sub(tmp, tmp, t0);
1846 addi(tmp, tmp, -1);
1847 Address arg_addr = argument_address(tmp);
1848 ld(tmp, arg_addr);
1849
1850 mv(t0, argument_type_offset0);
1851 mv(t1, type_step);
1852 mul(t1, index, t1);
1853 add(t0, t0, t1);
1854 add(mdo_addr, mdp, t0);
1855 Address mdo_arg_addr(mdo_addr, 0);
1856 profile_obj_type(tmp, mdo_arg_addr, t1);
1857
1858 int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
1859 addi(off_to_args, off_to_args, to_add);
1860
1861 // increment index by 1
1862 addi(index, index, 1);
1863 mv(t1, TypeProfileArgsLimit);
1864 blt(index, t1, loop);
1865 bind(loopEnd);
1866
1867 if (MethodData::profile_return()) {
1868 ld(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())));
1869 addi(tmp, tmp, -TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count());
1870 }
1871
1872 add(t0, mdp, off_to_args);
1873 bind(done);
1874 mv(mdp, t0);
1875
1876 // unspill the clobbered registers
1877 pop_reg(pushed_registers, sp);
1878
1879 if (MethodData::profile_return()) {
1880 // We're right after the type profile for the last
1881 // argument. tmp is the number of cells left in the
1882 // CallTypeData/VirtualCallTypeData to reach its end. Non null
1883 // if there's a return to profile.
1884 assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(), "can't move past ret type");
1885 shadd(mdp, tmp, mdp, tmp, exact_log2(DataLayout::cell_size));
1886 }
1887 sd(mdp, Address(fp, frame::interpreter_frame_mdp_offset * wordSize));
1888 } else {
1889 assert(MethodData::profile_return(), "either profile call args or call ret");
1890 update_mdp_by_constant(mdp, in_bytes(TypeEntriesAtCall::return_only_size()));
1891 }
1892
1893 // mdp points right after the end of the
1894 // CallTypeData/VirtualCallTypeData, right after the cells for the
1895 // return value type if there's one
1896
1897 bind(profile_continue);
1898 }
1899 }
1900
1901 void InterpreterMacroAssembler::profile_return_type(Register mdp, Register ret, Register tmp) {
1902 assert_different_registers(mdp, ret, tmp, xbcp, t0, t1);
1903 if (ProfileInterpreter && MethodData::profile_return()) {
1904 Label profile_continue, done;
1905
1906 test_method_data_pointer(mdp, profile_continue);
1907
1908 if (MethodData::profile_return_jsr292_only()) {
1909 assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
1910
1911 // If we don't profile all invoke bytecodes we must make sure
1912 // it's a bytecode we indeed profile. We can't go back to the
1913 // beginning of the ProfileData we intend to update to check its
1914 // type because we're right after it and we don't known its
1915 // length
1916 Label do_profile;
1917 lbu(t0, Address(xbcp, 0));
1918 mv(tmp, (u1)Bytecodes::_invokedynamic);
1919 beq(t0, tmp, do_profile);
1920 mv(tmp, (u1)Bytecodes::_invokehandle);
1921 beq(t0, tmp, do_profile);
1922 get_method(tmp);
1923 lhu(t0, Address(tmp, Method::intrinsic_id_offset()));
1924 mv(t1, static_cast<int>(vmIntrinsics::_compiledLambdaForm));
1925 bne(t0, t1, profile_continue);
1926 bind(do_profile);
1927 }
1928
1929 Address mdo_ret_addr(mdp, -in_bytes(ReturnTypeEntry::size()));
1930 mv(tmp, ret);
1931 profile_obj_type(tmp, mdo_ret_addr, t1);
1932
1933 bind(profile_continue);
1934 }
1935 }
1936
1937 void InterpreterMacroAssembler::profile_parameters_type(Register mdp, Register tmp1, Register tmp2, Register tmp3) {
1938 assert_different_registers(t0, t1, mdp, tmp1, tmp2, tmp3);
1939 if (ProfileInterpreter && MethodData::profile_parameters()) {
1940 Label profile_continue, done;
1941
1942 test_method_data_pointer(mdp, profile_continue);
1943
1944 // Load the offset of the area within the MDO used for
1945 // parameters. If it's negative we're not profiling any parameters
1946 lwu(tmp1, Address(mdp, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset())));
1947 srli(tmp2, tmp1, 31);
1948 bnez(tmp2, profile_continue); // i.e. sign bit set
1949
1950 // Compute a pointer to the area for parameters from the offset
1951 // and move the pointer to the slot for the last
1952 // parameters. Collect profiling from last parameter down.
1953 // mdo start + parameters offset + array length - 1
1954 add(mdp, mdp, tmp1);
1955 ld(tmp1, Address(mdp, ArrayData::array_len_offset()));
1956 add(tmp1, tmp1, - TypeStackSlotEntries::per_arg_count());
1957
1958 Label loop;
1959 bind(loop);
1960
1961 int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
1962 int type_base = in_bytes(ParametersTypeData::type_offset(0));
1963 int per_arg_scale = exact_log2(DataLayout::cell_size);
1964 add(t0, mdp, off_base);
1965 add(t1, mdp, type_base);
1966
1967 shadd(tmp2, tmp1, t0, tmp2, per_arg_scale);
1968 // load offset on the stack from the slot for this parameter
1969 ld(tmp2, Address(tmp2, 0));
1970 neg(tmp2, tmp2);
1971
1972 // read the parameter from the local area
1973 shadd(tmp2, tmp2, xlocals, tmp2, Interpreter::logStackElementSize);
1974 ld(tmp2, Address(tmp2, 0));
1975
1976 // profile the parameter
1977 shadd(t1, tmp1, t1, t0, per_arg_scale);
1978 Address arg_type(t1, 0);
1979 profile_obj_type(tmp2, arg_type, tmp3);
1980
1981 // go to next parameter
1982 add(tmp1, tmp1, - TypeStackSlotEntries::per_arg_count());
1983 bgez(tmp1, loop);
1984
1985 bind(profile_continue);
1986 }
1987 }
1988
1989 void InterpreterMacroAssembler::load_resolved_indy_entry(Register cache, Register index) {
1990 // Get index out of bytecode pointer, get_cache_entry_pointer_at_bcp
1991 // register "cache" is trashed in next ld, so lets use it as a temporary register
1992 get_cache_index_at_bcp(index, cache, 1, sizeof(u4));
1993 // Get address of invokedynamic array
1994 ld(cache, Address(xcpool, in_bytes(ConstantPoolCache::invokedynamic_entries_offset())));
1995 // Scale the index to be the entry index * sizeof(ResolvedInvokeDynamicInfo)
1996 slli(index, index, log2i_exact(sizeof(ResolvedIndyEntry)));
1997 add(cache, cache, Array<ResolvedIndyEntry>::base_offset_in_bytes());
1998 add(cache, cache, index);
1999 la(cache, Address(cache, 0));
2000 }
2001
2002 void InterpreterMacroAssembler::get_method_counters(Register method,
2003 Register mcs, Label& skip) {
2004 Label has_counters;
2005 ld(mcs, Address(method, Method::method_counters_offset()));
2006 bnez(mcs, has_counters);
2007 call_VM(noreg, CAST_FROM_FN_PTR(address,
2008 InterpreterRuntime::build_method_counters), method);
2009 ld(mcs, Address(method, Method::method_counters_offset()));
2010 beqz(mcs, skip); // No MethodCounters allocated, OutOfMemory
2011 bind(has_counters);
2012 }
2013
2014 #ifdef ASSERT
2015 void InterpreterMacroAssembler::verify_access_flags(Register access_flags, uint32_t flag,
2016 const char* msg, bool stop_by_hit) {
2017 Label L;
2018 test_bit(t0, access_flags, exact_log2(flag));
2019 if (stop_by_hit) {
2020 beqz(t0, L);
2021 } else {
2022 bnez(t0, L);
2023 }
2024 stop(msg);
2025 bind(L);
2026 }
2027
2028 void InterpreterMacroAssembler::verify_frame_setup() {
2029 Label L;
2030 const Address monitor_block_top(fp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
2031 ld(t0, monitor_block_top);
2032 beq(esp, t0, L);
2033 stop("broken stack frame setup in interpreter");
2034 bind(L);
2035 }
2036 #endif