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