interp_masm_ppc_64.cpp revision 11374:3fb9a97eb099
1/*
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2012, 2016 SAP SE. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26
27#include "precompiled.hpp"
28#include "asm/macroAssembler.inline.hpp"
29#include "interp_masm_ppc_64.hpp"
30#include "interpreter/interpreterRuntime.hpp"
31#include "prims/jvmtiThreadState.hpp"
32#include "runtime/sharedRuntime.hpp"
33
34#ifdef PRODUCT
35#define BLOCK_COMMENT(str) // nothing
36#else
37#define BLOCK_COMMENT(str) block_comment(str)
38#endif
39
40void InterpreterMacroAssembler::null_check_throw(Register a, int offset, Register temp_reg) {
41  address exception_entry = Interpreter::throw_NullPointerException_entry();
42  MacroAssembler::null_check_throw(a, offset, temp_reg, exception_entry);
43}
44
45void InterpreterMacroAssembler::jump_to_entry(address entry, Register Rscratch) {
46  assert(entry, "Entry must have been generated by now");
47  if (is_within_range_of_b(entry, pc())) {
48    b(entry);
49  } else {
50    load_const_optimized(Rscratch, entry, R0);
51    mtctr(Rscratch);
52    bctr();
53  }
54}
55
56void InterpreterMacroAssembler::dispatch_next(TosState state, int bcp_incr) {
57  Register bytecode = R12_scratch2;
58  if (bcp_incr != 0) {
59    lbzu(bytecode, bcp_incr, R14_bcp);
60  } else {
61    lbz(bytecode, 0, R14_bcp);
62  }
63
64  dispatch_Lbyte_code(state, bytecode, Interpreter::dispatch_table(state));
65}
66
67void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) {
68  // Load current bytecode.
69  Register bytecode = R12_scratch2;
70  lbz(bytecode, 0, R14_bcp);
71  dispatch_Lbyte_code(state, bytecode, table);
72}
73
74// Dispatch code executed in the prolog of a bytecode which does not do it's
75// own dispatch. The dispatch address is computed and placed in R24_dispatch_addr.
76void InterpreterMacroAssembler::dispatch_prolog(TosState state, int bcp_incr) {
77  Register bytecode = R12_scratch2;
78  lbz(bytecode, bcp_incr, R14_bcp);
79
80  load_dispatch_table(R24_dispatch_addr, Interpreter::dispatch_table(state));
81
82  sldi(bytecode, bytecode, LogBytesPerWord);
83  ldx(R24_dispatch_addr, R24_dispatch_addr, bytecode);
84}
85
86// Dispatch code executed in the epilog of a bytecode which does not do it's
87// own dispatch. The dispatch address in R24_dispatch_addr is used for the
88// dispatch.
89void InterpreterMacroAssembler::dispatch_epilog(TosState state, int bcp_incr) {
90  if (bcp_incr) { addi(R14_bcp, R14_bcp, bcp_incr); }
91  mtctr(R24_dispatch_addr);
92  bcctr(bcondAlways, 0, bhintbhBCCTRisNotPredictable);
93}
94
95void InterpreterMacroAssembler::check_and_handle_popframe(Register scratch_reg) {
96  assert(scratch_reg != R0, "can't use R0 as scratch_reg here");
97  if (JvmtiExport::can_pop_frame()) {
98    Label L;
99
100    // Check the "pending popframe condition" flag in the current thread.
101    lwz(scratch_reg, in_bytes(JavaThread::popframe_condition_offset()), R16_thread);
102
103    // Initiate popframe handling only if it is not already being
104    // processed. If the flag has the popframe_processing bit set, it
105    // means that this code is called *during* popframe handling - we
106    // don't want to reenter.
107    andi_(R0, scratch_reg, JavaThread::popframe_pending_bit);
108    beq(CCR0, L);
109
110    andi_(R0, scratch_reg, JavaThread::popframe_processing_bit);
111    bne(CCR0, L);
112
113    // Call the Interpreter::remove_activation_preserving_args_entry()
114    // func to get the address of the same-named entrypoint in the
115    // generated interpreter code.
116#if defined(ABI_ELFv2)
117    call_c(CAST_FROM_FN_PTR(address,
118                            Interpreter::remove_activation_preserving_args_entry),
119           relocInfo::none);
120#else
121    call_c(CAST_FROM_FN_PTR(FunctionDescriptor*,
122                            Interpreter::remove_activation_preserving_args_entry),
123           relocInfo::none);
124#endif
125
126    // Jump to Interpreter::_remove_activation_preserving_args_entry.
127    mtctr(R3_RET);
128    bctr();
129
130    align(32, 12);
131    bind(L);
132  }
133}
134
135void InterpreterMacroAssembler::check_and_handle_earlyret(Register scratch_reg) {
136  const Register Rthr_state_addr = scratch_reg;
137  if (JvmtiExport::can_force_early_return()) {
138    Label Lno_early_ret;
139    ld(Rthr_state_addr, in_bytes(JavaThread::jvmti_thread_state_offset()), R16_thread);
140    cmpdi(CCR0, Rthr_state_addr, 0);
141    beq(CCR0, Lno_early_ret);
142
143    lwz(R0, in_bytes(JvmtiThreadState::earlyret_state_offset()), Rthr_state_addr);
144    cmpwi(CCR0, R0, JvmtiThreadState::earlyret_pending);
145    bne(CCR0, Lno_early_ret);
146
147    // Jump to Interpreter::_earlyret_entry.
148    lwz(R3_ARG1, in_bytes(JvmtiThreadState::earlyret_tos_offset()), Rthr_state_addr);
149    call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_early_entry));
150    mtlr(R3_RET);
151    blr();
152
153    align(32, 12);
154    bind(Lno_early_ret);
155  }
156}
157
158void InterpreterMacroAssembler::load_earlyret_value(TosState state, Register Rscratch1) {
159  const Register RjvmtiState = Rscratch1;
160  const Register Rscratch2   = R0;
161
162  ld(RjvmtiState, in_bytes(JavaThread::jvmti_thread_state_offset()), R16_thread);
163  li(Rscratch2, 0);
164
165  switch (state) {
166    case atos: ld(R17_tos, in_bytes(JvmtiThreadState::earlyret_oop_offset()), RjvmtiState);
167               std(Rscratch2, in_bytes(JvmtiThreadState::earlyret_oop_offset()), RjvmtiState);
168               break;
169    case ltos: ld(R17_tos, in_bytes(JvmtiThreadState::earlyret_value_offset()), RjvmtiState);
170               break;
171    case btos: // fall through
172    case ztos: // fall through
173    case ctos: // fall through
174    case stos: // fall through
175    case itos: lwz(R17_tos, in_bytes(JvmtiThreadState::earlyret_value_offset()), RjvmtiState);
176               break;
177    case ftos: lfs(F15_ftos, in_bytes(JvmtiThreadState::earlyret_value_offset()), RjvmtiState);
178               break;
179    case dtos: lfd(F15_ftos, in_bytes(JvmtiThreadState::earlyret_value_offset()), RjvmtiState);
180               break;
181    case vtos: break;
182    default  : ShouldNotReachHere();
183  }
184
185  // Clean up tos value in the jvmti thread state.
186  std(Rscratch2, in_bytes(JvmtiThreadState::earlyret_value_offset()), RjvmtiState);
187  // Set tos state field to illegal value.
188  li(Rscratch2, ilgl);
189  stw(Rscratch2, in_bytes(JvmtiThreadState::earlyret_tos_offset()), RjvmtiState);
190}
191
192// Common code to dispatch and dispatch_only.
193// Dispatch value in Lbyte_code and increment Lbcp.
194
195void InterpreterMacroAssembler::load_dispatch_table(Register dst, address* table) {
196  address table_base = (address)Interpreter::dispatch_table((TosState)0);
197  intptr_t table_offs = (intptr_t)table - (intptr_t)table_base;
198  if (is_simm16(table_offs)) {
199    addi(dst, R25_templateTableBase, (int)table_offs);
200  } else {
201    load_const_optimized(dst, table, R0);
202  }
203}
204
205void InterpreterMacroAssembler::dispatch_Lbyte_code(TosState state, Register bytecode,
206                                                    address* table, bool verify) {
207  if (verify) {
208    unimplemented("dispatch_Lbyte_code: verify"); // See Sparc Implementation to implement this
209  }
210
211  assert_different_registers(bytecode, R11_scratch1);
212
213  // Calc dispatch table address.
214  load_dispatch_table(R11_scratch1, table);
215
216  sldi(R12_scratch2, bytecode, LogBytesPerWord);
217  ldx(R11_scratch1, R11_scratch1, R12_scratch2);
218
219  // Jump off!
220  mtctr(R11_scratch1);
221  bcctr(bcondAlways, 0, bhintbhBCCTRisNotPredictable);
222}
223
224void InterpreterMacroAssembler::load_receiver(Register Rparam_count, Register Rrecv_dst) {
225  sldi(Rrecv_dst, Rparam_count, Interpreter::logStackElementSize);
226  ldx(Rrecv_dst, Rrecv_dst, R15_esp);
227}
228
229// helpers for expression stack
230
231void InterpreterMacroAssembler::pop_i(Register r) {
232  lwzu(r, Interpreter::stackElementSize, R15_esp);
233}
234
235void InterpreterMacroAssembler::pop_ptr(Register r) {
236  ldu(r, Interpreter::stackElementSize, R15_esp);
237}
238
239void InterpreterMacroAssembler::pop_l(Register r) {
240  ld(r, Interpreter::stackElementSize, R15_esp);
241  addi(R15_esp, R15_esp, 2 * Interpreter::stackElementSize);
242}
243
244void InterpreterMacroAssembler::pop_f(FloatRegister f) {
245  lfsu(f, Interpreter::stackElementSize, R15_esp);
246}
247
248void InterpreterMacroAssembler::pop_d(FloatRegister f) {
249  lfd(f, Interpreter::stackElementSize, R15_esp);
250  addi(R15_esp, R15_esp, 2 * Interpreter::stackElementSize);
251}
252
253void InterpreterMacroAssembler::push_i(Register r) {
254  stw(r, 0, R15_esp);
255  addi(R15_esp, R15_esp, - Interpreter::stackElementSize );
256}
257
258void InterpreterMacroAssembler::push_ptr(Register r) {
259  std(r, 0, R15_esp);
260  addi(R15_esp, R15_esp, - Interpreter::stackElementSize );
261}
262
263void InterpreterMacroAssembler::push_l(Register r) {
264  std(r, - Interpreter::stackElementSize, R15_esp);
265  addi(R15_esp, R15_esp, - 2 * Interpreter::stackElementSize );
266}
267
268void InterpreterMacroAssembler::push_f(FloatRegister f) {
269  stfs(f, 0, R15_esp);
270  addi(R15_esp, R15_esp, - Interpreter::stackElementSize );
271}
272
273void InterpreterMacroAssembler::push_d(FloatRegister f)   {
274  stfd(f, - Interpreter::stackElementSize, R15_esp);
275  addi(R15_esp, R15_esp, - 2 * Interpreter::stackElementSize );
276}
277
278void InterpreterMacroAssembler::push_2ptrs(Register first, Register second) {
279  std(first, 0, R15_esp);
280  std(second, -Interpreter::stackElementSize, R15_esp);
281  addi(R15_esp, R15_esp, - 2 * Interpreter::stackElementSize );
282}
283
284void InterpreterMacroAssembler::push_l_pop_d(Register l, FloatRegister d) {
285  std(l, 0, R15_esp);
286  lfd(d, 0, R15_esp);
287}
288
289void InterpreterMacroAssembler::push_d_pop_l(FloatRegister d, Register l) {
290  stfd(d, 0, R15_esp);
291  ld(l, 0, R15_esp);
292}
293
294void InterpreterMacroAssembler::push(TosState state) {
295  switch (state) {
296    case atos: push_ptr();                break;
297    case btos:
298    case ztos:
299    case ctos:
300    case stos:
301    case itos: push_i();                  break;
302    case ltos: push_l();                  break;
303    case ftos: push_f();                  break;
304    case dtos: push_d();                  break;
305    case vtos: /* nothing to do */        break;
306    default  : ShouldNotReachHere();
307  }
308}
309
310void InterpreterMacroAssembler::pop(TosState state) {
311  switch (state) {
312    case atos: pop_ptr();            break;
313    case btos:
314    case ztos:
315    case ctos:
316    case stos:
317    case itos: pop_i();              break;
318    case ltos: pop_l();              break;
319    case ftos: pop_f();              break;
320    case dtos: pop_d();              break;
321    case vtos: /* nothing to do */   break;
322    default  : ShouldNotReachHere();
323  }
324  verify_oop(R17_tos, state);
325}
326
327void InterpreterMacroAssembler::empty_expression_stack() {
328  addi(R15_esp, R26_monitor, - Interpreter::stackElementSize);
329}
330
331void InterpreterMacroAssembler::get_2_byte_integer_at_bcp(int         bcp_offset,
332                                                          Register    Rdst,
333                                                          signedOrNot is_signed) {
334#if defined(VM_LITTLE_ENDIAN)
335  if (bcp_offset) {
336    load_const_optimized(Rdst, bcp_offset);
337    lhbrx(Rdst, R14_bcp, Rdst);
338  } else {
339    lhbrx(Rdst, R14_bcp);
340  }
341  if (is_signed == Signed) {
342    extsh(Rdst, Rdst);
343  }
344#else
345  // Read Java big endian format.
346  if (is_signed == Signed) {
347    lha(Rdst, bcp_offset, R14_bcp);
348  } else {
349    lhz(Rdst, bcp_offset, R14_bcp);
350  }
351#endif
352}
353
354void InterpreterMacroAssembler::get_4_byte_integer_at_bcp(int         bcp_offset,
355                                                          Register    Rdst,
356                                                          signedOrNot is_signed) {
357#if defined(VM_LITTLE_ENDIAN)
358  if (bcp_offset) {
359    load_const_optimized(Rdst, bcp_offset);
360    lwbrx(Rdst, R14_bcp, Rdst);
361  } else {
362    lwbrx(Rdst, R14_bcp);
363  }
364  if (is_signed == Signed) {
365    extsw(Rdst, Rdst);
366  }
367#else
368  // Read Java big endian format.
369  if (bcp_offset & 3) { // Offset unaligned?
370    load_const_optimized(Rdst, bcp_offset);
371    if (is_signed == Signed) {
372      lwax(Rdst, R14_bcp, Rdst);
373    } else {
374      lwzx(Rdst, R14_bcp, Rdst);
375    }
376  } else {
377    if (is_signed == Signed) {
378      lwa(Rdst, bcp_offset, R14_bcp);
379    } else {
380      lwz(Rdst, bcp_offset, R14_bcp);
381    }
382  }
383#endif
384}
385
386
387// Load the constant pool cache index from the bytecode stream.
388//
389// Kills / writes:
390//   - Rdst, Rscratch
391void InterpreterMacroAssembler::get_cache_index_at_bcp(Register Rdst, int bcp_offset,
392                                                       size_t index_size) {
393  assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
394  // Cache index is always in the native format, courtesy of Rewriter.
395  if (index_size == sizeof(u2)) {
396    lhz(Rdst, bcp_offset, R14_bcp);
397  } else if (index_size == sizeof(u4)) {
398    if (bcp_offset & 3) {
399      load_const_optimized(Rdst, bcp_offset);
400      lwax(Rdst, R14_bcp, Rdst);
401    } else {
402      lwa(Rdst, bcp_offset, R14_bcp);
403    }
404    assert(ConstantPool::decode_invokedynamic_index(~123) == 123, "else change next line");
405    nand(Rdst, Rdst, Rdst); // convert to plain index
406  } else if (index_size == sizeof(u1)) {
407    lbz(Rdst, bcp_offset, R14_bcp);
408  } else {
409    ShouldNotReachHere();
410  }
411  // Rdst now contains cp cache index.
412}
413
414void InterpreterMacroAssembler::get_cache_and_index_at_bcp(Register cache, int bcp_offset,
415                                                           size_t index_size) {
416  get_cache_index_at_bcp(cache, bcp_offset, index_size);
417  sldi(cache, cache, exact_log2(in_words(ConstantPoolCacheEntry::size()) * BytesPerWord));
418  add(cache, R27_constPoolCache, cache);
419}
420
421// Load 4-byte signed or unsigned integer in Java format (that is, big-endian format)
422// from (Rsrc)+offset.
423void InterpreterMacroAssembler::get_u4(Register Rdst, Register Rsrc, int offset,
424                                       signedOrNot is_signed) {
425#if defined(VM_LITTLE_ENDIAN)
426  if (offset) {
427    load_const_optimized(Rdst, offset);
428    lwbrx(Rdst, Rdst, Rsrc);
429  } else {
430    lwbrx(Rdst, Rsrc);
431  }
432  if (is_signed == Signed) {
433    extsw(Rdst, Rdst);
434  }
435#else
436  if (is_signed == Signed) {
437    lwa(Rdst, offset, Rsrc);
438  } else {
439    lwz(Rdst, offset, Rsrc);
440  }
441#endif
442}
443
444// Load object from cpool->resolved_references(index).
445void InterpreterMacroAssembler::load_resolved_reference_at_index(Register result, Register index, Label *is_null) {
446  assert_different_registers(result, index);
447  get_constant_pool(result);
448
449  // Convert from field index to resolved_references() index and from
450  // word index to byte offset. Since this is a java object, it can be compressed.
451  Register tmp = index;  // reuse
452  sldi(tmp, index, LogBytesPerHeapOop);
453  // Load pointer for resolved_references[] objArray.
454  ld(result, ConstantPool::resolved_references_offset_in_bytes(), result);
455  // JNIHandles::resolve(result)
456  ld(result, 0, result);
457#ifdef ASSERT
458  Label index_ok;
459  lwa(R0, arrayOopDesc::length_offset_in_bytes(), result);
460  sldi(R0, R0, LogBytesPerHeapOop);
461  cmpd(CCR0, tmp, R0);
462  blt(CCR0, index_ok);
463  stop("resolved reference index out of bounds", 0x09256);
464  bind(index_ok);
465#endif
466  // Add in the index.
467  add(result, tmp, result);
468  load_heap_oop(result, arrayOopDesc::base_offset_in_bytes(T_OBJECT), result, is_null);
469}
470
471// Generate a subtype check: branch to ok_is_subtype if sub_klass is
472// a subtype of super_klass. Blows registers Rsub_klass, tmp1, tmp2.
473void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass, Register Rsuper_klass, Register Rtmp1,
474                                                  Register Rtmp2, Register Rtmp3, Label &ok_is_subtype) {
475  // Profile the not-null value's klass.
476  profile_typecheck(Rsub_klass, Rtmp1, Rtmp2);
477  check_klass_subtype(Rsub_klass, Rsuper_klass, Rtmp1, Rtmp2, ok_is_subtype);
478  profile_typecheck_failed(Rtmp1, Rtmp2);
479}
480
481void InterpreterMacroAssembler::generate_stack_overflow_check_with_compare_and_throw(Register Rmem_frame_size, Register Rscratch1) {
482  Label done;
483  BLOCK_COMMENT("stack_overflow_check_with_compare_and_throw {");
484  sub(Rmem_frame_size, R1_SP, Rmem_frame_size);
485  ld(Rscratch1, thread_(stack_overflow_limit));
486  cmpld(CCR0/*is_stack_overflow*/, Rmem_frame_size, Rscratch1);
487  bgt(CCR0/*is_stack_overflow*/, done);
488
489  // Load target address of the runtime stub.
490  assert(StubRoutines::throw_StackOverflowError_entry() != NULL, "generated in wrong order");
491  load_const_optimized(Rscratch1, (StubRoutines::throw_StackOverflowError_entry()), R0);
492  mtctr(Rscratch1);
493  // Restore caller_sp.
494#ifdef ASSERT
495  ld(Rscratch1, 0, R1_SP);
496  ld(R0, 0, R21_sender_SP);
497  cmpd(CCR0, R0, Rscratch1);
498  asm_assert_eq("backlink", 0x547);
499#endif // ASSERT
500  mr(R1_SP, R21_sender_SP);
501  bctr();
502
503  align(32, 12);
504  bind(done);
505  BLOCK_COMMENT("} stack_overflow_check_with_compare_and_throw");
506}
507
508// Separate these two to allow for delay slot in middle.
509// These are used to do a test and full jump to exception-throwing code.
510
511// Check that index is in range for array, then shift index by index_shift,
512// and put arrayOop + shifted_index into res.
513// Note: res is still shy of address by array offset into object.
514
515void InterpreterMacroAssembler::index_check_without_pop(Register Rarray, Register Rindex,
516                                                        int index_shift, Register Rtmp, Register Rres) {
517  // Check that index is in range for array, then shift index by index_shift,
518  // and put arrayOop + shifted_index into res.
519  // Note: res is still shy of address by array offset into object.
520  // Kills:
521  //   - Rindex
522  // Writes:
523  //   - Rres: Address that corresponds to the array index if check was successful.
524  verify_oop(Rarray);
525  const Register Rlength   = R0;
526  const Register RsxtIndex = Rtmp;
527  Label LisNull, LnotOOR;
528
529  // Array nullcheck
530  if (!ImplicitNullChecks) {
531    cmpdi(CCR0, Rarray, 0);
532    beq(CCR0, LisNull);
533  } else {
534    null_check_throw(Rarray, arrayOopDesc::length_offset_in_bytes(), /*temp*/RsxtIndex);
535  }
536
537  // Rindex might contain garbage in upper bits (remember that we don't sign extend
538  // during integer arithmetic operations). So kill them and put value into same register
539  // where ArrayIndexOutOfBounds would expect the index in.
540  rldicl(RsxtIndex, Rindex, 0, 32); // zero extend 32 bit -> 64 bit
541
542  // Index check
543  lwz(Rlength, arrayOopDesc::length_offset_in_bytes(), Rarray);
544  cmplw(CCR0, Rindex, Rlength);
545  sldi(RsxtIndex, RsxtIndex, index_shift);
546  blt(CCR0, LnotOOR);
547  // Index should be in R17_tos, array should be in R4_ARG2.
548  mr_if_needed(R17_tos, Rindex);
549  mr_if_needed(R4_ARG2, Rarray);
550  load_dispatch_table(Rtmp, (address*)Interpreter::_throw_ArrayIndexOutOfBoundsException_entry);
551  mtctr(Rtmp);
552  bctr();
553
554  if (!ImplicitNullChecks) {
555    bind(LisNull);
556    load_dispatch_table(Rtmp, (address*)Interpreter::_throw_NullPointerException_entry);
557    mtctr(Rtmp);
558    bctr();
559  }
560
561  align(32, 16);
562  bind(LnotOOR);
563
564  // Calc address
565  add(Rres, RsxtIndex, Rarray);
566}
567
568void InterpreterMacroAssembler::index_check(Register array, Register index,
569                                            int index_shift, Register tmp, Register res) {
570  // pop array
571  pop_ptr(array);
572
573  // check array
574  index_check_without_pop(array, index, index_shift, tmp, res);
575}
576
577void InterpreterMacroAssembler::get_const(Register Rdst) {
578  ld(Rdst, in_bytes(Method::const_offset()), R19_method);
579}
580
581void InterpreterMacroAssembler::get_constant_pool(Register Rdst) {
582  get_const(Rdst);
583  ld(Rdst, in_bytes(ConstMethod::constants_offset()), Rdst);
584}
585
586void InterpreterMacroAssembler::get_constant_pool_cache(Register Rdst) {
587  get_constant_pool(Rdst);
588  ld(Rdst, ConstantPool::cache_offset_in_bytes(), Rdst);
589}
590
591void InterpreterMacroAssembler::get_cpool_and_tags(Register Rcpool, Register Rtags) {
592  get_constant_pool(Rcpool);
593  ld(Rtags, ConstantPool::tags_offset_in_bytes(), Rcpool);
594}
595
596// Unlock if synchronized method.
597//
598// Unlock the receiver if this is a synchronized method.
599// Unlock any Java monitors from synchronized blocks.
600//
601// If there are locked Java monitors
602//   If throw_monitor_exception
603//     throws IllegalMonitorStateException
604//   Else if install_monitor_exception
605//     installs IllegalMonitorStateException
606//   Else
607//     no error processing
608void InterpreterMacroAssembler::unlock_if_synchronized_method(TosState state,
609                                                              bool throw_monitor_exception,
610                                                              bool install_monitor_exception) {
611  Label Lunlocked, Lno_unlock;
612  {
613    Register Rdo_not_unlock_flag = R11_scratch1;
614    Register Raccess_flags       = R12_scratch2;
615
616    // Check if synchronized method or unlocking prevented by
617    // JavaThread::do_not_unlock_if_synchronized flag.
618    lbz(Rdo_not_unlock_flag, in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()), R16_thread);
619    lwz(Raccess_flags, in_bytes(Method::access_flags_offset()), R19_method);
620    li(R0, 0);
621    stb(R0, in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()), R16_thread); // reset flag
622
623    push(state);
624
625    // Skip if we don't have to unlock.
626    rldicl_(R0, Raccess_flags, 64-JVM_ACC_SYNCHRONIZED_BIT, 63); // Extract bit and compare to 0.
627    beq(CCR0, Lunlocked);
628
629    cmpwi(CCR0, Rdo_not_unlock_flag, 0);
630    bne(CCR0, Lno_unlock);
631  }
632
633  // Unlock
634  {
635    Register Rmonitor_base = R11_scratch1;
636
637    Label Lunlock;
638    // If it's still locked, everything is ok, unlock it.
639    ld(Rmonitor_base, 0, R1_SP);
640    addi(Rmonitor_base, Rmonitor_base,
641         -(frame::ijava_state_size + frame::interpreter_frame_monitor_size_in_bytes())); // Monitor base
642
643    ld(R0, BasicObjectLock::obj_offset_in_bytes(), Rmonitor_base);
644    cmpdi(CCR0, R0, 0);
645    bne(CCR0, Lunlock);
646
647    // If it's already unlocked, throw exception.
648    if (throw_monitor_exception) {
649      call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
650      should_not_reach_here();
651    } else {
652      if (install_monitor_exception) {
653        call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::new_illegal_monitor_state_exception));
654        b(Lunlocked);
655      }
656    }
657
658    bind(Lunlock);
659    unlock_object(Rmonitor_base);
660  }
661
662  // Check that all other monitors are unlocked. Throw IllegelMonitorState exception if not.
663  bind(Lunlocked);
664  {
665    Label Lexception, Lrestart;
666    Register Rcurrent_obj_addr = R11_scratch1;
667    const int delta = frame::interpreter_frame_monitor_size_in_bytes();
668    assert((delta & LongAlignmentMask) == 0, "sizeof BasicObjectLock must be even number of doublewords");
669
670    bind(Lrestart);
671    // Set up search loop: Calc num of iterations.
672    {
673      Register Riterations = R12_scratch2;
674      Register Rmonitor_base = Rcurrent_obj_addr;
675      ld(Rmonitor_base, 0, R1_SP);
676      addi(Rmonitor_base, Rmonitor_base, - frame::ijava_state_size);  // Monitor base
677
678      subf_(Riterations, R26_monitor, Rmonitor_base);
679      ble(CCR0, Lno_unlock);
680
681      addi(Rcurrent_obj_addr, Rmonitor_base,
682           BasicObjectLock::obj_offset_in_bytes() - frame::interpreter_frame_monitor_size_in_bytes());
683      // Check if any monitor is on stack, bail out if not
684      srdi(Riterations, Riterations, exact_log2(delta));
685      mtctr(Riterations);
686    }
687
688    // The search loop: Look for locked monitors.
689    {
690      const Register Rcurrent_obj = R0;
691      Label Lloop;
692
693      ld(Rcurrent_obj, 0, Rcurrent_obj_addr);
694      addi(Rcurrent_obj_addr, Rcurrent_obj_addr, -delta);
695      bind(Lloop);
696
697      // Check if current entry is used.
698      cmpdi(CCR0, Rcurrent_obj, 0);
699      bne(CCR0, Lexception);
700      // Preload next iteration's compare value.
701      ld(Rcurrent_obj, 0, Rcurrent_obj_addr);
702      addi(Rcurrent_obj_addr, Rcurrent_obj_addr, -delta);
703      bdnz(Lloop);
704    }
705    // Fell through: Everything's unlocked => finish.
706    b(Lno_unlock);
707
708    // An object is still locked => need to throw exception.
709    bind(Lexception);
710    if (throw_monitor_exception) {
711      call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
712      should_not_reach_here();
713    } else {
714      // Stack unrolling. Unlock object and if requested, install illegal_monitor_exception.
715      // Unlock does not block, so don't have to worry about the frame.
716      Register Rmonitor_addr = R11_scratch1;
717      addi(Rmonitor_addr, Rcurrent_obj_addr, -BasicObjectLock::obj_offset_in_bytes() + delta);
718      unlock_object(Rmonitor_addr);
719      if (install_monitor_exception) {
720        call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::new_illegal_monitor_state_exception));
721      }
722      b(Lrestart);
723    }
724  }
725
726  align(32, 12);
727  bind(Lno_unlock);
728  pop(state);
729}
730
731// Support function for remove_activation & Co.
732void InterpreterMacroAssembler::merge_frames(Register Rsender_sp, Register return_pc,
733                                             Register Rscratch1, Register Rscratch2) {
734  // Pop interpreter frame.
735  ld(Rscratch1, 0, R1_SP); // *SP
736  ld(Rsender_sp, _ijava_state_neg(sender_sp), Rscratch1); // top_frame_sp
737  ld(Rscratch2, 0, Rscratch1); // **SP
738#ifdef ASSERT
739  {
740    Label Lok;
741    ld(R0, _ijava_state_neg(ijava_reserved), Rscratch1);
742    cmpdi(CCR0, R0, 0x5afe);
743    beq(CCR0, Lok);
744    stop("frame corrupted (remove activation)", 0x5afe);
745    bind(Lok);
746  }
747#endif
748  if (return_pc!=noreg) {
749    ld(return_pc, _abi(lr), Rscratch1); // LR
750  }
751
752  // Merge top frames.
753  subf(Rscratch1, R1_SP, Rsender_sp); // top_frame_sp - SP
754  stdux(Rscratch2, R1_SP, Rscratch1); // atomically set *(SP = top_frame_sp) = **SP
755}
756
757void InterpreterMacroAssembler::narrow(Register result) {
758  Register ret_type = R11_scratch1;
759  ld(R11_scratch1, in_bytes(Method::const_offset()), R19_method);
760  lbz(ret_type, in_bytes(ConstMethod::result_type_offset()), R11_scratch1);
761
762  Label notBool, notByte, notChar, done;
763
764  // common case first
765  cmpwi(CCR0, ret_type, T_INT);
766  beq(CCR0, done);
767
768  cmpwi(CCR0, ret_type, T_BOOLEAN);
769  bne(CCR0, notBool);
770  andi(result, result, 0x1);
771  b(done);
772
773  bind(notBool);
774  cmpwi(CCR0, ret_type, T_BYTE);
775  bne(CCR0, notByte);
776  extsb(result, result);
777  b(done);
778
779  bind(notByte);
780  cmpwi(CCR0, ret_type, T_CHAR);
781  bne(CCR0, notChar);
782  andi(result, result, 0xffff);
783  b(done);
784
785  bind(notChar);
786  // cmpwi(CCR0, ret_type, T_SHORT);  // all that's left
787  // bne(CCR0, done);
788  extsh(result, result);
789
790  // Nothing to do for T_INT
791  bind(done);
792}
793
794// Remove activation.
795//
796// Unlock the receiver if this is a synchronized method.
797// Unlock any Java monitors from synchronized blocks.
798// Remove the activation from the stack.
799//
800// If there are locked Java monitors
801//    If throw_monitor_exception
802//       throws IllegalMonitorStateException
803//    Else if install_monitor_exception
804//       installs IllegalMonitorStateException
805//    Else
806//       no error processing
807void InterpreterMacroAssembler::remove_activation(TosState state,
808                                                  bool throw_monitor_exception,
809                                                  bool install_monitor_exception) {
810  BLOCK_COMMENT("remove_activation {");
811  unlock_if_synchronized_method(state, throw_monitor_exception, install_monitor_exception);
812
813  // Save result (push state before jvmti call and pop it afterwards) and notify jvmti.
814  notify_method_exit(false, state, NotifyJVMTI, true);
815
816  BLOCK_COMMENT("reserved_stack_check:");
817  if (StackReservedPages > 0) {
818    // Test if reserved zone needs to be enabled.
819    Label no_reserved_zone_enabling;
820
821    // Compare frame pointers. There is no good stack pointer, as with stack
822    // frame compression we can get different SPs when we do calls. A subsequent
823    // call could have a smaller SP, so that this compare succeeds for an
824    // inner call of the method annotated with ReservedStack.
825    ld_ptr(R0, JavaThread::reserved_stack_activation_offset(), R16_thread);
826    ld_ptr(R11_scratch1, _abi(callers_sp), R1_SP); // Load frame pointer.
827    cmpld(CCR0, R11_scratch1, R0);
828    blt_predict_taken(CCR0, no_reserved_zone_enabling);
829
830    // Enable reserved zone again, throw stack overflow exception.
831    call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), R16_thread);
832    call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_delayed_StackOverflowError));
833
834    should_not_reach_here();
835
836    bind(no_reserved_zone_enabling);
837  }
838
839  verify_oop(R17_tos, state);
840  verify_thread();
841
842  merge_frames(/*top_frame_sp*/ R21_sender_SP, /*return_pc*/ R0, R11_scratch1, R12_scratch2);
843  mtlr(R0);
844  BLOCK_COMMENT("} remove_activation");
845}
846
847// Lock object
848//
849// Registers alive
850//   monitor - Address of the BasicObjectLock to be used for locking,
851//             which must be initialized with the object to lock.
852//   object  - Address of the object to be locked.
853//
854void InterpreterMacroAssembler::lock_object(Register monitor, Register object) {
855  if (UseHeavyMonitors) {
856    call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
857            monitor, /*check_for_exceptions=*/true);
858  } else {
859    // template code:
860    //
861    // markOop displaced_header = obj->mark().set_unlocked();
862    // monitor->lock()->set_displaced_header(displaced_header);
863    // if (Atomic::cmpxchg_ptr(/*ex=*/monitor, /*addr*/obj->mark_addr(), /*cmp*/displaced_header) == displaced_header) {
864    //   // We stored the monitor address into the object's mark word.
865    // } else if (THREAD->is_lock_owned((address)displaced_header))
866    //   // Simple recursive case.
867    //   monitor->lock()->set_displaced_header(NULL);
868    // } else {
869    //   // Slow path.
870    //   InterpreterRuntime::monitorenter(THREAD, monitor);
871    // }
872
873    const Register displaced_header = R7_ARG5;
874    const Register object_mark_addr = R8_ARG6;
875    const Register current_header   = R9_ARG7;
876    const Register tmp              = R10_ARG8;
877
878    Label done;
879    Label cas_failed, slow_case;
880
881    assert_different_registers(displaced_header, object_mark_addr, current_header, tmp);
882
883    // markOop displaced_header = obj->mark().set_unlocked();
884
885    // Load markOop from object into displaced_header.
886    ld(displaced_header, oopDesc::mark_offset_in_bytes(), object);
887
888    if (UseBiasedLocking) {
889      biased_locking_enter(CCR0, object, displaced_header, tmp, current_header, done, &slow_case);
890    }
891
892    // Set displaced_header to be (markOop of object | UNLOCK_VALUE).
893    ori(displaced_header, displaced_header, markOopDesc::unlocked_value);
894
895    // monitor->lock()->set_displaced_header(displaced_header);
896
897    // Initialize the box (Must happen before we update the object mark!).
898    std(displaced_header, BasicObjectLock::lock_offset_in_bytes() +
899        BasicLock::displaced_header_offset_in_bytes(), monitor);
900
901    // if (Atomic::cmpxchg_ptr(/*ex=*/monitor, /*addr*/obj->mark_addr(), /*cmp*/displaced_header) == displaced_header) {
902
903    // Store stack address of the BasicObjectLock (this is monitor) into object.
904    addi(object_mark_addr, object, oopDesc::mark_offset_in_bytes());
905
906    // Must fence, otherwise, preceding store(s) may float below cmpxchg.
907    // CmpxchgX sets CCR0 to cmpX(current, displaced).
908    cmpxchgd(/*flag=*/CCR0,
909             /*current_value=*/current_header,
910             /*compare_value=*/displaced_header, /*exchange_value=*/monitor,
911             /*where=*/object_mark_addr,
912             MacroAssembler::MemBarRel | MacroAssembler::MemBarAcq,
913             MacroAssembler::cmpxchgx_hint_acquire_lock(),
914             noreg,
915             &cas_failed,
916             /*check without membar and ldarx first*/true);
917
918    // If the compare-and-exchange succeeded, then we found an unlocked
919    // object and we have now locked it.
920    b(done);
921    bind(cas_failed);
922
923    // } else if (THREAD->is_lock_owned((address)displaced_header))
924    //   // Simple recursive case.
925    //   monitor->lock()->set_displaced_header(NULL);
926
927    // We did not see an unlocked object so try the fast recursive case.
928
929    // Check if owner is self by comparing the value in the markOop of object
930    // (current_header) with the stack pointer.
931    sub(current_header, current_header, R1_SP);
932
933    assert(os::vm_page_size() > 0xfff, "page size too small - change the constant");
934    load_const_optimized(tmp, ~(os::vm_page_size()-1) | markOopDesc::lock_mask_in_place);
935
936    and_(R0/*==0?*/, current_header, tmp);
937    // If condition is true we are done and hence we can store 0 in the displaced
938    // header indicating it is a recursive lock.
939    bne(CCR0, slow_case);
940    std(R0/*==0!*/, BasicObjectLock::lock_offset_in_bytes() +
941        BasicLock::displaced_header_offset_in_bytes(), monitor);
942    b(done);
943
944    // } else {
945    //   // Slow path.
946    //   InterpreterRuntime::monitorenter(THREAD, monitor);
947
948    // None of the above fast optimizations worked so we have to get into the
949    // slow case of monitor enter.
950    bind(slow_case);
951    call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
952            monitor, /*check_for_exceptions=*/true);
953    // }
954    align(32, 12);
955    bind(done);
956  }
957}
958
959// Unlocks an object. Used in monitorexit bytecode and remove_activation.
960//
961// Registers alive
962//   monitor - Address of the BasicObjectLock to be used for locking,
963//             which must be initialized with the object to lock.
964//
965// Throw IllegalMonitorException if object is not locked by current thread.
966void InterpreterMacroAssembler::unlock_object(Register monitor, bool check_for_exceptions) {
967  if (UseHeavyMonitors) {
968    call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit),
969            monitor, check_for_exceptions);
970  } else {
971
972    // template code:
973    //
974    // if ((displaced_header = monitor->displaced_header()) == NULL) {
975    //   // Recursive unlock. Mark the monitor unlocked by setting the object field to NULL.
976    //   monitor->set_obj(NULL);
977    // } else if (Atomic::cmpxchg_ptr(displaced_header, obj->mark_addr(), monitor) == monitor) {
978    //   // We swapped the unlocked mark in displaced_header into the object's mark word.
979    //   monitor->set_obj(NULL);
980    // } else {
981    //   // Slow path.
982    //   InterpreterRuntime::monitorexit(THREAD, monitor);
983    // }
984
985    const Register object           = R7_ARG5;
986    const Register displaced_header = R8_ARG6;
987    const Register object_mark_addr = R9_ARG7;
988    const Register current_header   = R10_ARG8;
989
990    Label free_slot;
991    Label slow_case;
992
993    assert_different_registers(object, displaced_header, object_mark_addr, current_header);
994
995    if (UseBiasedLocking) {
996      // The object address from the monitor is in object.
997      ld(object, BasicObjectLock::obj_offset_in_bytes(), monitor);
998      assert(oopDesc::mark_offset_in_bytes() == 0, "offset of _mark is not 0");
999      biased_locking_exit(CCR0, object, displaced_header, free_slot);
1000    }
1001
1002    // Test first if we are in the fast recursive case.
1003    ld(displaced_header, BasicObjectLock::lock_offset_in_bytes() +
1004           BasicLock::displaced_header_offset_in_bytes(), monitor);
1005
1006    // If the displaced header is zero, we have a recursive unlock.
1007    cmpdi(CCR0, displaced_header, 0);
1008    beq(CCR0, free_slot); // recursive unlock
1009
1010    // } else if (Atomic::cmpxchg_ptr(displaced_header, obj->mark_addr(), monitor) == monitor) {
1011    //   // We swapped the unlocked mark in displaced_header into the object's mark word.
1012    //   monitor->set_obj(NULL);
1013
1014    // If we still have a lightweight lock, unlock the object and be done.
1015
1016    // The object address from the monitor is in object.
1017    if (!UseBiasedLocking) { ld(object, BasicObjectLock::obj_offset_in_bytes(), monitor); }
1018    addi(object_mark_addr, object, oopDesc::mark_offset_in_bytes());
1019
1020    // We have the displaced header in displaced_header. If the lock is still
1021    // lightweight, it will contain the monitor address and we'll store the
1022    // displaced header back into the object's mark word.
1023    // CmpxchgX sets CCR0 to cmpX(current, monitor).
1024    cmpxchgd(/*flag=*/CCR0,
1025             /*current_value=*/current_header,
1026             /*compare_value=*/monitor, /*exchange_value=*/displaced_header,
1027             /*where=*/object_mark_addr,
1028             MacroAssembler::MemBarRel,
1029             MacroAssembler::cmpxchgx_hint_release_lock(),
1030             noreg,
1031             &slow_case);
1032    b(free_slot);
1033
1034    // } else {
1035    //   // Slow path.
1036    //   InterpreterRuntime::monitorexit(THREAD, monitor);
1037
1038    // The lock has been converted into a heavy lock and hence
1039    // we need to get into the slow case.
1040    bind(slow_case);
1041    call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit),
1042            monitor, check_for_exceptions);
1043    // }
1044
1045    Label done;
1046    b(done); // Monitor register may be overwritten! Runtime has already freed the slot.
1047
1048    // Exchange worked, do monitor->set_obj(NULL);
1049    align(32, 12);
1050    bind(free_slot);
1051    li(R0, 0);
1052    std(R0, BasicObjectLock::obj_offset_in_bytes(), monitor);
1053    bind(done);
1054  }
1055}
1056
1057// Load compiled (i2c) or interpreter entry when calling from interpreted and
1058// do the call. Centralized so that all interpreter calls will do the same actions.
1059// If jvmti single stepping is on for a thread we must not call compiled code.
1060//
1061// Input:
1062//   - Rtarget_method: method to call
1063//   - Rret_addr:      return address
1064//   - 2 scratch regs
1065//
1066void InterpreterMacroAssembler::call_from_interpreter(Register Rtarget_method, Register Rret_addr,
1067                                                      Register Rscratch1, Register Rscratch2) {
1068  assert_different_registers(Rscratch1, Rscratch2, Rtarget_method, Rret_addr);
1069  // Assume we want to go compiled if available.
1070  const Register Rtarget_addr = Rscratch1;
1071  const Register Rinterp_only = Rscratch2;
1072
1073  ld(Rtarget_addr, in_bytes(Method::from_interpreted_offset()), Rtarget_method);
1074
1075  if (JvmtiExport::can_post_interpreter_events()) {
1076    lwz(Rinterp_only, in_bytes(JavaThread::interp_only_mode_offset()), R16_thread);
1077
1078    // JVMTI events, such as single-stepping, are implemented partly by avoiding running
1079    // compiled code in threads for which the event is enabled. Check here for
1080    // interp_only_mode if these events CAN be enabled.
1081    Label done;
1082    verify_thread();
1083    cmpwi(CCR0, Rinterp_only, 0);
1084    beq(CCR0, done);
1085    ld(Rtarget_addr, in_bytes(Method::interpreter_entry_offset()), Rtarget_method);
1086    align(32, 12);
1087    bind(done);
1088  }
1089
1090#ifdef ASSERT
1091  {
1092    Label Lok;
1093    cmpdi(CCR0, Rtarget_addr, 0);
1094    bne(CCR0, Lok);
1095    stop("null entry point");
1096    bind(Lok);
1097  }
1098#endif // ASSERT
1099
1100  mr(R21_sender_SP, R1_SP);
1101
1102  // Calc a precise SP for the call. The SP value we calculated in
1103  // generate_fixed_frame() is based on the max_stack() value, so we would waste stack space
1104  // if esp is not max. Also, the i2c adapter extends the stack space without restoring
1105  // our pre-calced value, so repeating calls via i2c would result in stack overflow.
1106  // Since esp already points to an empty slot, we just have to sub 1 additional slot
1107  // to meet the abi scratch requirements.
1108  // The max_stack pointer will get restored by means of the GR_Lmax_stack local in
1109  // the return entry of the interpreter.
1110  addi(Rscratch2, R15_esp, Interpreter::stackElementSize - frame::abi_reg_args_size);
1111  clrrdi(Rscratch2, Rscratch2, exact_log2(frame::alignment_in_bytes)); // round towards smaller address
1112  resize_frame_absolute(Rscratch2, Rscratch2, R0);
1113
1114  mr_if_needed(R19_method, Rtarget_method);
1115  mtctr(Rtarget_addr);
1116  mtlr(Rret_addr);
1117
1118  save_interpreter_state(Rscratch2);
1119#ifdef ASSERT
1120  ld(Rscratch1, _ijava_state_neg(top_frame_sp), Rscratch2); // Rscratch2 contains fp
1121  cmpd(CCR0, R21_sender_SP, Rscratch1);
1122  asm_assert_eq("top_frame_sp incorrect", 0x951);
1123#endif
1124
1125  bctr();
1126}
1127
1128// Set the method data pointer for the current bcp.
1129void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
1130  assert(ProfileInterpreter, "must be profiling interpreter");
1131  Label get_continue;
1132  ld(R28_mdx, in_bytes(Method::method_data_offset()), R19_method);
1133  test_method_data_pointer(get_continue);
1134  call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), R19_method, R14_bcp);
1135
1136  addi(R28_mdx, R28_mdx, in_bytes(MethodData::data_offset()));
1137  add(R28_mdx, R28_mdx, R3_RET);
1138  bind(get_continue);
1139}
1140
1141// Test ImethodDataPtr. If it is null, continue at the specified label.
1142void InterpreterMacroAssembler::test_method_data_pointer(Label& zero_continue) {
1143  assert(ProfileInterpreter, "must be profiling interpreter");
1144  cmpdi(CCR0, R28_mdx, 0);
1145  beq(CCR0, zero_continue);
1146}
1147
1148void InterpreterMacroAssembler::verify_method_data_pointer() {
1149  assert(ProfileInterpreter, "must be profiling interpreter");
1150#ifdef ASSERT
1151  Label verify_continue;
1152  test_method_data_pointer(verify_continue);
1153
1154  // If the mdp is valid, it will point to a DataLayout header which is
1155  // consistent with the bcp. The converse is highly probable also.
1156  lhz(R11_scratch1, in_bytes(DataLayout::bci_offset()), R28_mdx);
1157  ld(R12_scratch2, in_bytes(Method::const_offset()), R19_method);
1158  addi(R11_scratch1, R11_scratch1, in_bytes(ConstMethod::codes_offset()));
1159  add(R11_scratch1, R12_scratch2, R12_scratch2);
1160  cmpd(CCR0, R11_scratch1, R14_bcp);
1161  beq(CCR0, verify_continue);
1162
1163  call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp ), R19_method, R14_bcp, R28_mdx);
1164
1165  bind(verify_continue);
1166#endif
1167}
1168
1169void InterpreterMacroAssembler::test_invocation_counter_for_mdp(Register invocation_count,
1170                                                                Register method_counters,
1171                                                                Register Rscratch,
1172                                                                Label &profile_continue) {
1173  assert(ProfileInterpreter, "must be profiling interpreter");
1174  // Control will flow to "profile_continue" if the counter is less than the
1175  // limit or if we call profile_method().
1176  Label done;
1177
1178  // If no method data exists, and the counter is high enough, make one.
1179  lwz(Rscratch, in_bytes(MethodCounters::interpreter_profile_limit_offset()), method_counters);
1180
1181  cmpdi(CCR0, R28_mdx, 0);
1182  // Test to see if we should create a method data oop.
1183  cmpd(CCR1, Rscratch, invocation_count);
1184  bne(CCR0, done);
1185  bge(CCR1, profile_continue);
1186
1187  // Build it now.
1188  call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::profile_method));
1189  set_method_data_pointer_for_bcp();
1190  b(profile_continue);
1191
1192  align(32, 12);
1193  bind(done);
1194}
1195
1196void InterpreterMacroAssembler::test_backedge_count_for_osr(Register backedge_count, Register method_counters,
1197                                                            Register target_bcp, Register disp, Register Rtmp) {
1198  assert_different_registers(backedge_count, target_bcp, disp, Rtmp, R4_ARG2);
1199  assert(UseOnStackReplacement,"Must UseOnStackReplacement to test_backedge_count_for_osr");
1200
1201  Label did_not_overflow;
1202  Label overflow_with_error;
1203
1204  lwz(Rtmp, in_bytes(MethodCounters::interpreter_backward_branch_limit_offset()), method_counters);
1205  cmpw(CCR0, backedge_count, Rtmp);
1206
1207  blt(CCR0, did_not_overflow);
1208
1209  // When ProfileInterpreter is on, the backedge_count comes from the
1210  // methodDataOop, which value does not get reset on the call to
1211  // frequency_counter_overflow(). To avoid excessive calls to the overflow
1212  // routine while the method is being compiled, add a second test to make sure
1213  // the overflow function is called only once every overflow_frequency.
1214  if (ProfileInterpreter) {
1215    const int overflow_frequency = 1024;
1216    andi_(Rtmp, backedge_count, overflow_frequency-1);
1217    bne(CCR0, did_not_overflow);
1218  }
1219
1220  // Overflow in loop, pass branch bytecode.
1221  subf(R4_ARG2, disp, target_bcp); // Compute branch bytecode (previous bcp).
1222  call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), R4_ARG2, true);
1223
1224  // Was an OSR adapter generated?
1225  cmpdi(CCR0, R3_RET, 0);
1226  beq(CCR0, overflow_with_error);
1227
1228  // Has the nmethod been invalidated already?
1229  lbz(Rtmp, nmethod::state_offset(), R3_RET);
1230  cmpwi(CCR0, Rtmp, nmethod::in_use);
1231  bne(CCR0, overflow_with_error);
1232
1233  // Migrate the interpreter frame off of the stack.
1234  // We can use all registers because we will not return to interpreter from this point.
1235
1236  // Save nmethod.
1237  const Register osr_nmethod = R31;
1238  mr(osr_nmethod, R3_RET);
1239  set_top_ijava_frame_at_SP_as_last_Java_frame(R1_SP, R11_scratch1);
1240  call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin), R16_thread);
1241  reset_last_Java_frame();
1242  // OSR buffer is in ARG1
1243
1244  // Remove the interpreter frame.
1245  merge_frames(/*top_frame_sp*/ R21_sender_SP, /*return_pc*/ R0, R11_scratch1, R12_scratch2);
1246
1247  // Jump to the osr code.
1248  ld(R11_scratch1, nmethod::osr_entry_point_offset(), osr_nmethod);
1249  mtlr(R0);
1250  mtctr(R11_scratch1);
1251  bctr();
1252
1253  align(32, 12);
1254  bind(overflow_with_error);
1255  bind(did_not_overflow);
1256}
1257
1258// Store a value at some constant offset from the method data pointer.
1259void InterpreterMacroAssembler::set_mdp_data_at(int constant, Register value) {
1260  assert(ProfileInterpreter, "must be profiling interpreter");
1261
1262  std(value, constant, R28_mdx);
1263}
1264
1265// Increment the value at some constant offset from the method data pointer.
1266void InterpreterMacroAssembler::increment_mdp_data_at(int constant,
1267                                                      Register counter_addr,
1268                                                      Register Rbumped_count,
1269                                                      bool decrement) {
1270  // Locate the counter at a fixed offset from the mdp:
1271  addi(counter_addr, R28_mdx, constant);
1272  increment_mdp_data_at(counter_addr, Rbumped_count, decrement);
1273}
1274
1275// Increment the value at some non-fixed (reg + constant) offset from
1276// the method data pointer.
1277void InterpreterMacroAssembler::increment_mdp_data_at(Register reg,
1278                                                      int constant,
1279                                                      Register scratch,
1280                                                      Register Rbumped_count,
1281                                                      bool decrement) {
1282  // Add the constant to reg to get the offset.
1283  add(scratch, R28_mdx, reg);
1284  // Then calculate the counter address.
1285  addi(scratch, scratch, constant);
1286  increment_mdp_data_at(scratch, Rbumped_count, decrement);
1287}
1288
1289void InterpreterMacroAssembler::increment_mdp_data_at(Register counter_addr,
1290                                                      Register Rbumped_count,
1291                                                      bool decrement) {
1292  assert(ProfileInterpreter, "must be profiling interpreter");
1293
1294  // Load the counter.
1295  ld(Rbumped_count, 0, counter_addr);
1296
1297  if (decrement) {
1298    // Decrement the register. Set condition codes.
1299    addi(Rbumped_count, Rbumped_count, - DataLayout::counter_increment);
1300    // Store the decremented counter, if it is still negative.
1301    std(Rbumped_count, 0, counter_addr);
1302    // Note: add/sub overflow check are not ported, since 64 bit
1303    // calculation should never overflow.
1304  } else {
1305    // Increment the register. Set carry flag.
1306    addi(Rbumped_count, Rbumped_count, DataLayout::counter_increment);
1307    // Store the incremented counter.
1308    std(Rbumped_count, 0, counter_addr);
1309  }
1310}
1311
1312// Set a flag value at the current method data pointer position.
1313void InterpreterMacroAssembler::set_mdp_flag_at(int flag_constant,
1314                                                Register scratch) {
1315  assert(ProfileInterpreter, "must be profiling interpreter");
1316  // Load the data header.
1317  lbz(scratch, in_bytes(DataLayout::flags_offset()), R28_mdx);
1318  // Set the flag.
1319  ori(scratch, scratch, flag_constant);
1320  // Store the modified header.
1321  stb(scratch, in_bytes(DataLayout::flags_offset()), R28_mdx);
1322}
1323
1324// Test the location at some offset from the method data pointer.
1325// If it is not equal to value, branch to the not_equal_continue Label.
1326void InterpreterMacroAssembler::test_mdp_data_at(int offset,
1327                                                 Register value,
1328                                                 Label& not_equal_continue,
1329                                                 Register test_out) {
1330  assert(ProfileInterpreter, "must be profiling interpreter");
1331
1332  ld(test_out, offset, R28_mdx);
1333  cmpd(CCR0,  value, test_out);
1334  bne(CCR0, not_equal_continue);
1335}
1336
1337// Update the method data pointer by the displacement located at some fixed
1338// offset from the method data pointer.
1339void InterpreterMacroAssembler::update_mdp_by_offset(int offset_of_disp,
1340                                                     Register scratch) {
1341  assert(ProfileInterpreter, "must be profiling interpreter");
1342
1343  ld(scratch, offset_of_disp, R28_mdx);
1344  add(R28_mdx, scratch, R28_mdx);
1345}
1346
1347// Update the method data pointer by the displacement located at the
1348// offset (reg + offset_of_disp).
1349void InterpreterMacroAssembler::update_mdp_by_offset(Register reg,
1350                                                     int offset_of_disp,
1351                                                     Register scratch) {
1352  assert(ProfileInterpreter, "must be profiling interpreter");
1353
1354  add(scratch, reg, R28_mdx);
1355  ld(scratch, offset_of_disp, scratch);
1356  add(R28_mdx, scratch, R28_mdx);
1357}
1358
1359// Update the method data pointer by a simple constant displacement.
1360void InterpreterMacroAssembler::update_mdp_by_constant(int constant) {
1361  assert(ProfileInterpreter, "must be profiling interpreter");
1362  addi(R28_mdx, R28_mdx, constant);
1363}
1364
1365// Update the method data pointer for a _ret bytecode whose target
1366// was not among our cached targets.
1367void InterpreterMacroAssembler::update_mdp_for_ret(TosState state,
1368                                                   Register return_bci) {
1369  assert(ProfileInterpreter, "must be profiling interpreter");
1370
1371  push(state);
1372  assert(return_bci->is_nonvolatile(), "need to protect return_bci");
1373  call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret), return_bci);
1374  pop(state);
1375}
1376
1377// Increments the backedge counter.
1378// Returns backedge counter + invocation counter in Rdst.
1379void InterpreterMacroAssembler::increment_backedge_counter(const Register Rcounters, const Register Rdst,
1380                                                           const Register Rtmp1, Register Rscratch) {
1381  assert(UseCompiler, "incrementing must be useful");
1382  assert_different_registers(Rdst, Rtmp1);
1383  const Register invocation_counter = Rtmp1;
1384  const Register counter = Rdst;
1385  // TODO: PPC port: assert(4 == InvocationCounter::sz_counter(), "unexpected field size.");
1386
1387  // Load backedge counter.
1388  lwz(counter, in_bytes(MethodCounters::backedge_counter_offset()) +
1389               in_bytes(InvocationCounter::counter_offset()), Rcounters);
1390  // Load invocation counter.
1391  lwz(invocation_counter, in_bytes(MethodCounters::invocation_counter_offset()) +
1392                          in_bytes(InvocationCounter::counter_offset()), Rcounters);
1393
1394  // Add the delta to the backedge counter.
1395  addi(counter, counter, InvocationCounter::count_increment);
1396
1397  // Mask the invocation counter.
1398  andi(invocation_counter, invocation_counter, InvocationCounter::count_mask_value);
1399
1400  // Store new counter value.
1401  stw(counter, in_bytes(MethodCounters::backedge_counter_offset()) +
1402               in_bytes(InvocationCounter::counter_offset()), Rcounters);
1403  // Return invocation counter + backedge counter.
1404  add(counter, counter, invocation_counter);
1405}
1406
1407// Count a taken branch in the bytecodes.
1408void InterpreterMacroAssembler::profile_taken_branch(Register scratch, Register bumped_count) {
1409  if (ProfileInterpreter) {
1410    Label profile_continue;
1411
1412    // If no method data exists, go to profile_continue.
1413    test_method_data_pointer(profile_continue);
1414
1415    // We are taking a branch. Increment the taken count.
1416    increment_mdp_data_at(in_bytes(JumpData::taken_offset()), scratch, bumped_count);
1417
1418    // The method data pointer needs to be updated to reflect the new target.
1419    update_mdp_by_offset(in_bytes(JumpData::displacement_offset()), scratch);
1420    bind (profile_continue);
1421  }
1422}
1423
1424// Count a not-taken branch in the bytecodes.
1425void InterpreterMacroAssembler::profile_not_taken_branch(Register scratch1, Register scratch2) {
1426  if (ProfileInterpreter) {
1427    Label profile_continue;
1428
1429    // If no method data exists, go to profile_continue.
1430    test_method_data_pointer(profile_continue);
1431
1432    // We are taking a branch. Increment the not taken count.
1433    increment_mdp_data_at(in_bytes(BranchData::not_taken_offset()), scratch1, scratch2);
1434
1435    // The method data pointer needs to be updated to correspond to the
1436    // next bytecode.
1437    update_mdp_by_constant(in_bytes(BranchData::branch_data_size()));
1438    bind (profile_continue);
1439  }
1440}
1441
1442// Count a non-virtual call in the bytecodes.
1443void InterpreterMacroAssembler::profile_call(Register scratch1, Register scratch2) {
1444  if (ProfileInterpreter) {
1445    Label profile_continue;
1446
1447    // If no method data exists, go to profile_continue.
1448    test_method_data_pointer(profile_continue);
1449
1450    // We are making a call. Increment the count.
1451    increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch1, scratch2);
1452
1453    // The method data pointer needs to be updated to reflect the new target.
1454    update_mdp_by_constant(in_bytes(CounterData::counter_data_size()));
1455    bind (profile_continue);
1456  }
1457}
1458
1459// Count a final call in the bytecodes.
1460void InterpreterMacroAssembler::profile_final_call(Register scratch1, Register scratch2) {
1461  if (ProfileInterpreter) {
1462    Label profile_continue;
1463
1464    // If no method data exists, go to profile_continue.
1465    test_method_data_pointer(profile_continue);
1466
1467    // We are making a call. Increment the count.
1468    increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch1, scratch2);
1469
1470    // The method data pointer needs to be updated to reflect the new target.
1471    update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1472    bind (profile_continue);
1473  }
1474}
1475
1476// Count a virtual call in the bytecodes.
1477void InterpreterMacroAssembler::profile_virtual_call(Register Rreceiver,
1478                                                     Register Rscratch1,
1479                                                     Register Rscratch2,
1480                                                     bool receiver_can_be_null) {
1481  if (!ProfileInterpreter) { return; }
1482  Label profile_continue;
1483
1484  // If no method data exists, go to profile_continue.
1485  test_method_data_pointer(profile_continue);
1486
1487  Label skip_receiver_profile;
1488  if (receiver_can_be_null) {
1489    Label not_null;
1490    cmpdi(CCR0, Rreceiver, 0);
1491    bne(CCR0, not_null);
1492    // We are making a call. Increment the count for null receiver.
1493    increment_mdp_data_at(in_bytes(CounterData::count_offset()), Rscratch1, Rscratch2);
1494    b(skip_receiver_profile);
1495    bind(not_null);
1496  }
1497
1498  // Record the receiver type.
1499  record_klass_in_profile(Rreceiver, Rscratch1, Rscratch2, true);
1500  bind(skip_receiver_profile);
1501
1502  // The method data pointer needs to be updated to reflect the new target.
1503  update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1504  bind (profile_continue);
1505}
1506
1507void InterpreterMacroAssembler::profile_typecheck(Register Rklass, Register Rscratch1, Register Rscratch2) {
1508  if (ProfileInterpreter) {
1509    Label profile_continue;
1510
1511    // If no method data exists, go to profile_continue.
1512    test_method_data_pointer(profile_continue);
1513
1514    int mdp_delta = in_bytes(BitData::bit_data_size());
1515    if (TypeProfileCasts) {
1516      mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1517
1518      // Record the object type.
1519      record_klass_in_profile(Rklass, Rscratch1, Rscratch2, false);
1520    }
1521
1522    // The method data pointer needs to be updated.
1523    update_mdp_by_constant(mdp_delta);
1524
1525    bind (profile_continue);
1526  }
1527}
1528
1529void InterpreterMacroAssembler::profile_typecheck_failed(Register Rscratch1, Register Rscratch2) {
1530  if (ProfileInterpreter && TypeProfileCasts) {
1531    Label profile_continue;
1532
1533    // If no method data exists, go to profile_continue.
1534    test_method_data_pointer(profile_continue);
1535
1536    int count_offset = in_bytes(CounterData::count_offset());
1537    // Back up the address, since we have already bumped the mdp.
1538    count_offset -= in_bytes(VirtualCallData::virtual_call_data_size());
1539
1540    // *Decrement* the counter. We expect to see zero or small negatives.
1541    increment_mdp_data_at(count_offset, Rscratch1, Rscratch2, true);
1542
1543    bind (profile_continue);
1544  }
1545}
1546
1547// Count a ret in the bytecodes.
1548void InterpreterMacroAssembler::profile_ret(TosState state, Register return_bci,
1549                                            Register scratch1, Register scratch2) {
1550  if (ProfileInterpreter) {
1551    Label profile_continue;
1552    uint row;
1553
1554    // If no method data exists, go to profile_continue.
1555    test_method_data_pointer(profile_continue);
1556
1557    // Update the total ret count.
1558    increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch1, scratch2 );
1559
1560    for (row = 0; row < RetData::row_limit(); row++) {
1561      Label next_test;
1562
1563      // See if return_bci is equal to bci[n]:
1564      test_mdp_data_at(in_bytes(RetData::bci_offset(row)), return_bci, next_test, scratch1);
1565
1566      // return_bci is equal to bci[n]. Increment the count.
1567      increment_mdp_data_at(in_bytes(RetData::bci_count_offset(row)), scratch1, scratch2);
1568
1569      // The method data pointer needs to be updated to reflect the new target.
1570      update_mdp_by_offset(in_bytes(RetData::bci_displacement_offset(row)), scratch1);
1571      b(profile_continue);
1572      bind(next_test);
1573    }
1574
1575    update_mdp_for_ret(state, return_bci);
1576
1577    bind (profile_continue);
1578  }
1579}
1580
1581// Count the default case of a switch construct.
1582void InterpreterMacroAssembler::profile_switch_default(Register scratch1,  Register scratch2) {
1583  if (ProfileInterpreter) {
1584    Label profile_continue;
1585
1586    // If no method data exists, go to profile_continue.
1587    test_method_data_pointer(profile_continue);
1588
1589    // Update the default case count
1590    increment_mdp_data_at(in_bytes(MultiBranchData::default_count_offset()),
1591                          scratch1, scratch2);
1592
1593    // The method data pointer needs to be updated.
1594    update_mdp_by_offset(in_bytes(MultiBranchData::default_displacement_offset()),
1595                         scratch1);
1596
1597    bind (profile_continue);
1598  }
1599}
1600
1601// Count the index'th case of a switch construct.
1602void InterpreterMacroAssembler::profile_switch_case(Register index,
1603                                                    Register scratch1,
1604                                                    Register scratch2,
1605                                                    Register scratch3) {
1606  if (ProfileInterpreter) {
1607    assert_different_registers(index, scratch1, scratch2, scratch3);
1608    Label profile_continue;
1609
1610    // If no method data exists, go to profile_continue.
1611    test_method_data_pointer(profile_continue);
1612
1613    // Build the base (index * per_case_size_in_bytes()) + case_array_offset_in_bytes().
1614    li(scratch3, in_bytes(MultiBranchData::case_array_offset()));
1615
1616    assert (in_bytes(MultiBranchData::per_case_size()) == 16, "so that shladd works");
1617    sldi(scratch1, index, exact_log2(in_bytes(MultiBranchData::per_case_size())));
1618    add(scratch1, scratch1, scratch3);
1619
1620    // Update the case count.
1621    increment_mdp_data_at(scratch1, in_bytes(MultiBranchData::relative_count_offset()), scratch2, scratch3);
1622
1623    // The method data pointer needs to be updated.
1624    update_mdp_by_offset(scratch1, in_bytes(MultiBranchData::relative_displacement_offset()), scratch2);
1625
1626    bind (profile_continue);
1627  }
1628}
1629
1630void InterpreterMacroAssembler::profile_null_seen(Register Rscratch1, Register Rscratch2) {
1631  if (ProfileInterpreter) {
1632    assert_different_registers(Rscratch1, Rscratch2);
1633    Label profile_continue;
1634
1635    // If no method data exists, go to profile_continue.
1636    test_method_data_pointer(profile_continue);
1637
1638    set_mdp_flag_at(BitData::null_seen_byte_constant(), Rscratch1);
1639
1640    // The method data pointer needs to be updated.
1641    int mdp_delta = in_bytes(BitData::bit_data_size());
1642    if (TypeProfileCasts) {
1643      mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1644    }
1645    update_mdp_by_constant(mdp_delta);
1646
1647    bind (profile_continue);
1648  }
1649}
1650
1651void InterpreterMacroAssembler::record_klass_in_profile(Register Rreceiver,
1652                                                        Register Rscratch1, Register Rscratch2,
1653                                                        bool is_virtual_call) {
1654  assert(ProfileInterpreter, "must be profiling");
1655  assert_different_registers(Rreceiver, Rscratch1, Rscratch2);
1656
1657  Label done;
1658  record_klass_in_profile_helper(Rreceiver, Rscratch1, Rscratch2, 0, done, is_virtual_call);
1659  bind (done);
1660}
1661
1662void InterpreterMacroAssembler::record_klass_in_profile_helper(
1663                                        Register receiver, Register scratch1, Register scratch2,
1664                                        int start_row, Label& done, bool is_virtual_call) {
1665  if (TypeProfileWidth == 0) {
1666    if (is_virtual_call) {
1667      increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch1, scratch2);
1668    }
1669    return;
1670  }
1671
1672  int last_row = VirtualCallData::row_limit() - 1;
1673  assert(start_row <= last_row, "must be work left to do");
1674  // Test this row for both the receiver and for null.
1675  // Take any of three different outcomes:
1676  //   1. found receiver => increment count and goto done
1677  //   2. found null => keep looking for case 1, maybe allocate this cell
1678  //   3. found something else => keep looking for cases 1 and 2
1679  // Case 3 is handled by a recursive call.
1680  for (int row = start_row; row <= last_row; row++) {
1681    Label next_test;
1682    bool test_for_null_also = (row == start_row);
1683
1684    // See if the receiver is receiver[n].
1685    int recvr_offset = in_bytes(VirtualCallData::receiver_offset(row));
1686    test_mdp_data_at(recvr_offset, receiver, next_test, scratch1);
1687    // delayed()->tst(scratch);
1688
1689    // The receiver is receiver[n]. Increment count[n].
1690    int count_offset = in_bytes(VirtualCallData::receiver_count_offset(row));
1691    increment_mdp_data_at(count_offset, scratch1, scratch2);
1692    b(done);
1693    bind(next_test);
1694
1695    if (test_for_null_also) {
1696      Label found_null;
1697      // Failed the equality check on receiver[n]... Test for null.
1698      if (start_row == last_row) {
1699        // The only thing left to do is handle the null case.
1700        if (is_virtual_call) {
1701          // Scratch1 contains test_out from test_mdp_data_at.
1702          cmpdi(CCR0, scratch1, 0);
1703          beq(CCR0, found_null);
1704          // Receiver did not match any saved receiver and there is no empty row for it.
1705          // Increment total counter to indicate polymorphic case.
1706          increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch1, scratch2);
1707          b(done);
1708          bind(found_null);
1709        } else {
1710          cmpdi(CCR0, scratch1, 0);
1711          bne(CCR0, done);
1712        }
1713        break;
1714      }
1715      // Since null is rare, make it be the branch-taken case.
1716      cmpdi(CCR0, scratch1, 0);
1717      beq(CCR0, found_null);
1718
1719      // Put all the "Case 3" tests here.
1720      record_klass_in_profile_helper(receiver, scratch1, scratch2, start_row + 1, done, is_virtual_call);
1721
1722      // Found a null. Keep searching for a matching receiver,
1723      // but remember that this is an empty (unused) slot.
1724      bind(found_null);
1725    }
1726  }
1727
1728  // In the fall-through case, we found no matching receiver, but we
1729  // observed the receiver[start_row] is NULL.
1730
1731  // Fill in the receiver field and increment the count.
1732  int recvr_offset = in_bytes(VirtualCallData::receiver_offset(start_row));
1733  set_mdp_data_at(recvr_offset, receiver);
1734  int count_offset = in_bytes(VirtualCallData::receiver_count_offset(start_row));
1735  li(scratch1, DataLayout::counter_increment);
1736  set_mdp_data_at(count_offset, scratch1);
1737  if (start_row > 0) {
1738    b(done);
1739  }
1740}
1741
1742// Argument and return type profilig.
1743// kills: tmp, tmp2, R0, CR0, CR1
1744void InterpreterMacroAssembler::profile_obj_type(Register obj, Register mdo_addr_base,
1745                                                 RegisterOrConstant mdo_addr_offs,
1746                                                 Register tmp, Register tmp2) {
1747  Label do_nothing, do_update;
1748
1749  // tmp2 = obj is allowed
1750  assert_different_registers(obj, mdo_addr_base, tmp, R0);
1751  assert_different_registers(tmp2, mdo_addr_base, tmp, R0);
1752  const Register klass = tmp2;
1753
1754  verify_oop(obj);
1755
1756  ld(tmp, mdo_addr_offs, mdo_addr_base);
1757
1758  // Set null_seen if obj is 0.
1759  cmpdi(CCR0, obj, 0);
1760  ori(R0, tmp, TypeEntries::null_seen);
1761  beq(CCR0, do_update);
1762
1763  load_klass(klass, obj);
1764
1765  clrrdi(R0, tmp, exact_log2(-TypeEntries::type_klass_mask));
1766  // Basically same as andi(R0, tmp, TypeEntries::type_klass_mask);
1767  cmpd(CCR1, R0, klass);
1768  // Klass seen before, nothing to do (regardless of unknown bit).
1769  //beq(CCR1, do_nothing);
1770
1771  andi_(R0, klass, TypeEntries::type_unknown);
1772  // Already unknown. Nothing to do anymore.
1773  //bne(CCR0, do_nothing);
1774  crorc(CCR0, Assembler::equal, CCR1, Assembler::equal); // cr0 eq = cr1 eq or cr0 ne
1775  beq(CCR0, do_nothing);
1776
1777  clrrdi_(R0, tmp, exact_log2(-TypeEntries::type_mask));
1778  orr(R0, klass, tmp); // Combine klass and null_seen bit (only used if (tmp & type_mask)==0).
1779  beq(CCR0, do_update); // First time here. Set profile type.
1780
1781  // Different than before. Cannot keep accurate profile.
1782  ori(R0, tmp, TypeEntries::type_unknown);
1783
1784  bind(do_update);
1785  // update profile
1786  std(R0, mdo_addr_offs, mdo_addr_base);
1787
1788  align(32, 12);
1789  bind(do_nothing);
1790}
1791
1792void InterpreterMacroAssembler::profile_arguments_type(Register callee,
1793                                                       Register tmp1, Register tmp2,
1794                                                       bool is_virtual) {
1795  if (!ProfileInterpreter) {
1796    return;
1797  }
1798
1799  assert_different_registers(callee, tmp1, tmp2, R28_mdx);
1800
1801  if (MethodData::profile_arguments() || MethodData::profile_return()) {
1802    Label profile_continue;
1803
1804    test_method_data_pointer(profile_continue);
1805
1806    int off_to_start = is_virtual ?
1807      in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
1808
1809    lbz(tmp1, in_bytes(DataLayout::tag_offset()) - off_to_start, R28_mdx);
1810    cmpwi(CCR0, tmp1, is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag);
1811    bne(CCR0, profile_continue);
1812
1813    if (MethodData::profile_arguments()) {
1814      Label done;
1815      int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset());
1816      add(R28_mdx, off_to_args, R28_mdx);
1817
1818      for (int i = 0; i < TypeProfileArgsLimit; i++) {
1819        if (i > 0 || MethodData::profile_return()) {
1820          // If return value type is profiled we may have no argument to profile.
1821          ld(tmp1, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args, R28_mdx);
1822          cmpdi(CCR0, tmp1, (i+1)*TypeStackSlotEntries::per_arg_count());
1823          addi(tmp1, tmp1, -i*TypeStackSlotEntries::per_arg_count());
1824          blt(CCR0, done);
1825        }
1826        ld(tmp1, in_bytes(Method::const_offset()), callee);
1827        lhz(tmp1, in_bytes(ConstMethod::size_of_parameters_offset()), tmp1);
1828        // Stack offset o (zero based) from the start of the argument
1829        // list, for n arguments translates into offset n - o - 1 from
1830        // the end of the argument list. But there's an extra slot at
1831        // the top of the stack. So the offset is n - o from Lesp.
1832        ld(tmp2, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))-off_to_args, R28_mdx);
1833        subf(tmp1, tmp2, tmp1);
1834
1835        sldi(tmp1, tmp1, Interpreter::logStackElementSize);
1836        ldx(tmp1, tmp1, R15_esp);
1837
1838        profile_obj_type(tmp1, R28_mdx, in_bytes(TypeEntriesAtCall::argument_type_offset(i))-off_to_args, tmp2, tmp1);
1839
1840        int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
1841        addi(R28_mdx, R28_mdx, to_add);
1842        off_to_args += to_add;
1843      }
1844
1845      if (MethodData::profile_return()) {
1846        ld(tmp1, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args, R28_mdx);
1847        addi(tmp1, tmp1, -TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count());
1848      }
1849
1850      bind(done);
1851
1852      if (MethodData::profile_return()) {
1853        // We're right after the type profile for the last
1854        // argument. tmp1 is the number of cells left in the
1855        // CallTypeData/VirtualCallTypeData to reach its end. Non null
1856        // if there's a return to profile.
1857        assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(),
1858               "can't move past ret type");
1859        sldi(tmp1, tmp1, exact_log2(DataLayout::cell_size));
1860        add(R28_mdx, tmp1, R28_mdx);
1861      }
1862    } else {
1863      assert(MethodData::profile_return(), "either profile call args or call ret");
1864      update_mdp_by_constant(in_bytes(TypeEntriesAtCall::return_only_size()));
1865    }
1866
1867    // Mdp points right after the end of the
1868    // CallTypeData/VirtualCallTypeData, right after the cells for the
1869    // return value type if there's one.
1870    align(32, 12);
1871    bind(profile_continue);
1872  }
1873}
1874
1875void InterpreterMacroAssembler::profile_return_type(Register ret, Register tmp1, Register tmp2) {
1876  assert_different_registers(ret, tmp1, tmp2);
1877  if (ProfileInterpreter && MethodData::profile_return()) {
1878    Label profile_continue;
1879
1880    test_method_data_pointer(profile_continue);
1881
1882    if (MethodData::profile_return_jsr292_only()) {
1883      // If we don't profile all invoke bytecodes we must make sure
1884      // it's a bytecode we indeed profile. We can't go back to the
1885      // begining of the ProfileData we intend to update to check its
1886      // type because we're right after it and we don't known its
1887      // length.
1888      lbz(tmp1, 0, R14_bcp);
1889      lbz(tmp2, Method::intrinsic_id_offset_in_bytes(), R19_method);
1890      cmpwi(CCR0, tmp1, Bytecodes::_invokedynamic);
1891      cmpwi(CCR1, tmp1, Bytecodes::_invokehandle);
1892      cror(CCR0, Assembler::equal, CCR1, Assembler::equal);
1893      cmpwi(CCR1, tmp2, vmIntrinsics::_compiledLambdaForm);
1894      cror(CCR0, Assembler::equal, CCR1, Assembler::equal);
1895      bne(CCR0, profile_continue);
1896    }
1897
1898    profile_obj_type(ret, R28_mdx, -in_bytes(ReturnTypeEntry::size()), tmp1, tmp2);
1899
1900    align(32, 12);
1901    bind(profile_continue);
1902  }
1903}
1904
1905void InterpreterMacroAssembler::profile_parameters_type(Register tmp1, Register tmp2,
1906                                                        Register tmp3, Register tmp4) {
1907  if (ProfileInterpreter && MethodData::profile_parameters()) {
1908    Label profile_continue, done;
1909
1910    test_method_data_pointer(profile_continue);
1911
1912    // Load the offset of the area within the MDO used for
1913    // parameters. If it's negative we're not profiling any parameters.
1914    lwz(tmp1, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset()), R28_mdx);
1915    cmpwi(CCR0, tmp1, 0);
1916    blt(CCR0, profile_continue);
1917
1918    // Compute a pointer to the area for parameters from the offset
1919    // and move the pointer to the slot for the last
1920    // parameters. Collect profiling from last parameter down.
1921    // mdo start + parameters offset + array length - 1
1922
1923    // Pointer to the parameter area in the MDO.
1924    const Register mdp = tmp1;
1925    add(mdp, tmp1, R28_mdx);
1926
1927    // Offset of the current profile entry to update.
1928    const Register entry_offset = tmp2;
1929    // entry_offset = array len in number of cells
1930    ld(entry_offset, in_bytes(ArrayData::array_len_offset()), mdp);
1931
1932    int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
1933    assert(off_base % DataLayout::cell_size == 0, "should be a number of cells");
1934
1935    // entry_offset (number of cells)  = array len - size of 1 entry + offset of the stack slot field
1936    addi(entry_offset, entry_offset, -TypeStackSlotEntries::per_arg_count() + (off_base / DataLayout::cell_size));
1937    // entry_offset in bytes
1938    sldi(entry_offset, entry_offset, exact_log2(DataLayout::cell_size));
1939
1940    Label loop;
1941    align(32, 12);
1942    bind(loop);
1943
1944    // Load offset on the stack from the slot for this parameter.
1945    ld(tmp3, entry_offset, mdp);
1946    sldi(tmp3, tmp3, Interpreter::logStackElementSize);
1947    neg(tmp3, tmp3);
1948    // Read the parameter from the local area.
1949    ldx(tmp3, tmp3, R18_locals);
1950
1951    // Make entry_offset now point to the type field for this parameter.
1952    int type_base = in_bytes(ParametersTypeData::type_offset(0));
1953    assert(type_base > off_base, "unexpected");
1954    addi(entry_offset, entry_offset, type_base - off_base);
1955
1956    // Profile the parameter.
1957    profile_obj_type(tmp3, mdp, entry_offset, tmp4, tmp3);
1958
1959    // Go to next parameter.
1960    int delta = TypeStackSlotEntries::per_arg_count() * DataLayout::cell_size + (type_base - off_base);
1961    cmpdi(CCR0, entry_offset, off_base + delta);
1962    addi(entry_offset, entry_offset, -delta);
1963    bge(CCR0, loop);
1964
1965    align(32, 12);
1966    bind(profile_continue);
1967  }
1968}
1969
1970// Add a InterpMonitorElem to stack (see frame_sparc.hpp).
1971void InterpreterMacroAssembler::add_monitor_to_stack(bool stack_is_empty, Register Rtemp1, Register Rtemp2) {
1972
1973  // Very-local scratch registers.
1974  const Register esp  = Rtemp1;
1975  const Register slot = Rtemp2;
1976
1977  // Extracted monitor_size.
1978  int monitor_size = frame::interpreter_frame_monitor_size_in_bytes();
1979  assert(Assembler::is_aligned((unsigned int)monitor_size,
1980                               (unsigned int)frame::alignment_in_bytes),
1981         "size of a monitor must respect alignment of SP");
1982
1983  resize_frame(-monitor_size, /*temp*/esp); // Allocate space for new monitor
1984  std(R1_SP, _ijava_state_neg(top_frame_sp), esp); // esp contains fp
1985
1986  // Shuffle expression stack down. Recall that stack_base points
1987  // just above the new expression stack bottom. Old_tos and new_tos
1988  // are used to scan thru the old and new expression stacks.
1989  if (!stack_is_empty) {
1990    Label copy_slot, copy_slot_finished;
1991    const Register n_slots = slot;
1992
1993    addi(esp, R15_esp, Interpreter::stackElementSize); // Point to first element (pre-pushed stack).
1994    subf(n_slots, esp, R26_monitor);
1995    srdi_(n_slots, n_slots, LogBytesPerWord);          // Compute number of slots to copy.
1996    assert(LogBytesPerWord == 3, "conflicts assembler instructions");
1997    beq(CCR0, copy_slot_finished);                     // Nothing to copy.
1998
1999    mtctr(n_slots);
2000
2001    // loop
2002    bind(copy_slot);
2003    ld(slot, 0, esp);              // Move expression stack down.
2004    std(slot, -monitor_size, esp); // distance = monitor_size
2005    addi(esp, esp, BytesPerWord);
2006    bdnz(copy_slot);
2007
2008    bind(copy_slot_finished);
2009  }
2010
2011  addi(R15_esp, R15_esp, -monitor_size);
2012  addi(R26_monitor, R26_monitor, -monitor_size);
2013
2014  // Restart interpreter
2015}
2016
2017// ============================================================================
2018// Java locals access
2019
2020// Load a local variable at index in Rindex into register Rdst_value.
2021// Also puts address of local into Rdst_address as a service.
2022// Kills:
2023//   - Rdst_value
2024//   - Rdst_address
2025void InterpreterMacroAssembler::load_local_int(Register Rdst_value, Register Rdst_address, Register Rindex) {
2026  sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
2027  subf(Rdst_address, Rdst_address, R18_locals);
2028  lwz(Rdst_value, 0, Rdst_address);
2029}
2030
2031// Load a local variable at index in Rindex into register Rdst_value.
2032// Also puts address of local into Rdst_address as a service.
2033// Kills:
2034//   - Rdst_value
2035//   - Rdst_address
2036void InterpreterMacroAssembler::load_local_long(Register Rdst_value, Register Rdst_address, Register Rindex) {
2037  sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
2038  subf(Rdst_address, Rdst_address, R18_locals);
2039  ld(Rdst_value, -8, Rdst_address);
2040}
2041
2042// Load a local variable at index in Rindex into register Rdst_value.
2043// Also puts address of local into Rdst_address as a service.
2044// Input:
2045//   - Rindex:      slot nr of local variable
2046// Kills:
2047//   - Rdst_value
2048//   - Rdst_address
2049void InterpreterMacroAssembler::load_local_ptr(Register Rdst_value,
2050                                               Register Rdst_address,
2051                                               Register Rindex) {
2052  sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
2053  subf(Rdst_address, Rdst_address, R18_locals);
2054  ld(Rdst_value, 0, Rdst_address);
2055}
2056
2057// Load a local variable at index in Rindex into register Rdst_value.
2058// Also puts address of local into Rdst_address as a service.
2059// Kills:
2060//   - Rdst_value
2061//   - Rdst_address
2062void InterpreterMacroAssembler::load_local_float(FloatRegister Rdst_value,
2063                                                 Register Rdst_address,
2064                                                 Register Rindex) {
2065  sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
2066  subf(Rdst_address, Rdst_address, R18_locals);
2067  lfs(Rdst_value, 0, Rdst_address);
2068}
2069
2070// Load a local variable at index in Rindex into register Rdst_value.
2071// Also puts address of local into Rdst_address as a service.
2072// Kills:
2073//   - Rdst_value
2074//   - Rdst_address
2075void InterpreterMacroAssembler::load_local_double(FloatRegister Rdst_value,
2076                                                  Register Rdst_address,
2077                                                  Register Rindex) {
2078  sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
2079  subf(Rdst_address, Rdst_address, R18_locals);
2080  lfd(Rdst_value, -8, Rdst_address);
2081}
2082
2083// Store an int value at local variable slot Rindex.
2084// Kills:
2085//   - Rindex
2086void InterpreterMacroAssembler::store_local_int(Register Rvalue, Register Rindex) {
2087  sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2088  subf(Rindex, Rindex, R18_locals);
2089  stw(Rvalue, 0, Rindex);
2090}
2091
2092// Store a long value at local variable slot Rindex.
2093// Kills:
2094//   - Rindex
2095void InterpreterMacroAssembler::store_local_long(Register Rvalue, Register Rindex) {
2096  sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2097  subf(Rindex, Rindex, R18_locals);
2098  std(Rvalue, -8, Rindex);
2099}
2100
2101// Store an oop value at local variable slot Rindex.
2102// Kills:
2103//   - Rindex
2104void InterpreterMacroAssembler::store_local_ptr(Register Rvalue, Register Rindex) {
2105  sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2106  subf(Rindex, Rindex, R18_locals);
2107  std(Rvalue, 0, Rindex);
2108}
2109
2110// Store an int value at local variable slot Rindex.
2111// Kills:
2112//   - Rindex
2113void InterpreterMacroAssembler::store_local_float(FloatRegister Rvalue, Register Rindex) {
2114  sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2115  subf(Rindex, Rindex, R18_locals);
2116  stfs(Rvalue, 0, Rindex);
2117}
2118
2119// Store an int value at local variable slot Rindex.
2120// Kills:
2121//   - Rindex
2122void InterpreterMacroAssembler::store_local_double(FloatRegister Rvalue, Register Rindex) {
2123  sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2124  subf(Rindex, Rindex, R18_locals);
2125  stfd(Rvalue, -8, Rindex);
2126}
2127
2128// Read pending exception from thread and jump to interpreter.
2129// Throw exception entry if one if pending. Fall through otherwise.
2130void InterpreterMacroAssembler::check_and_forward_exception(Register Rscratch1, Register Rscratch2) {
2131  assert_different_registers(Rscratch1, Rscratch2, R3);
2132  Register Rexception = Rscratch1;
2133  Register Rtmp       = Rscratch2;
2134  Label Ldone;
2135  // Get pending exception oop.
2136  ld(Rexception, thread_(pending_exception));
2137  cmpdi(CCR0, Rexception, 0);
2138  beq(CCR0, Ldone);
2139  li(Rtmp, 0);
2140  mr_if_needed(R3, Rexception);
2141  std(Rtmp, thread_(pending_exception)); // Clear exception in thread
2142  if (Interpreter::rethrow_exception_entry() != NULL) {
2143    // Already got entry address.
2144    load_dispatch_table(Rtmp, (address*)Interpreter::rethrow_exception_entry());
2145  } else {
2146    // Dynamically load entry address.
2147    int simm16_rest = load_const_optimized(Rtmp, &Interpreter::_rethrow_exception_entry, R0, true);
2148    ld(Rtmp, simm16_rest, Rtmp);
2149  }
2150  mtctr(Rtmp);
2151  save_interpreter_state(Rtmp);
2152  bctr();
2153
2154  align(32, 12);
2155  bind(Ldone);
2156}
2157
2158void InterpreterMacroAssembler::call_VM(Register oop_result, address entry_point, bool check_exceptions) {
2159  save_interpreter_state(R11_scratch1);
2160
2161  MacroAssembler::call_VM(oop_result, entry_point, false);
2162
2163  restore_interpreter_state(R11_scratch1, /*bcp_and_mdx_only*/ true);
2164
2165  check_and_handle_popframe(R11_scratch1);
2166  check_and_handle_earlyret(R11_scratch1);
2167  // Now check exceptions manually.
2168  if (check_exceptions) {
2169    check_and_forward_exception(R11_scratch1, R12_scratch2);
2170  }
2171}
2172
2173void InterpreterMacroAssembler::call_VM(Register oop_result, address entry_point,
2174                                        Register arg_1, bool check_exceptions) {
2175  // ARG1 is reserved for the thread.
2176  mr_if_needed(R4_ARG2, arg_1);
2177  call_VM(oop_result, entry_point, check_exceptions);
2178}
2179
2180void InterpreterMacroAssembler::call_VM(Register oop_result, address entry_point,
2181                                        Register arg_1, Register arg_2,
2182                                        bool check_exceptions) {
2183  // ARG1 is reserved for the thread.
2184  mr_if_needed(R4_ARG2, arg_1);
2185  assert(arg_2 != R4_ARG2, "smashed argument");
2186  mr_if_needed(R5_ARG3, arg_2);
2187  call_VM(oop_result, entry_point, check_exceptions);
2188}
2189
2190void InterpreterMacroAssembler::call_VM(Register oop_result, address entry_point,
2191                                        Register arg_1, Register arg_2, Register arg_3,
2192                                        bool check_exceptions) {
2193  // ARG1 is reserved for the thread.
2194  mr_if_needed(R4_ARG2, arg_1);
2195  assert(arg_2 != R4_ARG2, "smashed argument");
2196  mr_if_needed(R5_ARG3, arg_2);
2197  assert(arg_3 != R4_ARG2 && arg_3 != R5_ARG3, "smashed argument");
2198  mr_if_needed(R6_ARG4, arg_3);
2199  call_VM(oop_result, entry_point, check_exceptions);
2200}
2201
2202void InterpreterMacroAssembler::save_interpreter_state(Register scratch) {
2203  ld(scratch, 0, R1_SP);
2204  std(R15_esp, _ijava_state_neg(esp), scratch);
2205  std(R14_bcp, _ijava_state_neg(bcp), scratch);
2206  std(R26_monitor, _ijava_state_neg(monitors), scratch);
2207  if (ProfileInterpreter) { std(R28_mdx, _ijava_state_neg(mdx), scratch); }
2208  // Other entries should be unchanged.
2209}
2210
2211void InterpreterMacroAssembler::restore_interpreter_state(Register scratch, bool bcp_and_mdx_only) {
2212  ld(scratch, 0, R1_SP);
2213  ld(R14_bcp, _ijava_state_neg(bcp), scratch); // Changed by VM code (exception).
2214  if (ProfileInterpreter) { ld(R28_mdx, _ijava_state_neg(mdx), scratch); } // Changed by VM code.
2215  if (!bcp_and_mdx_only) {
2216    // Following ones are Metadata.
2217    ld(R19_method, _ijava_state_neg(method), scratch);
2218    ld(R27_constPoolCache, _ijava_state_neg(cpoolCache), scratch);
2219    // Following ones are stack addresses and don't require reload.
2220    ld(R15_esp, _ijava_state_neg(esp), scratch);
2221    ld(R18_locals, _ijava_state_neg(locals), scratch);
2222    ld(R26_monitor, _ijava_state_neg(monitors), scratch);
2223  }
2224#ifdef ASSERT
2225  {
2226    Label Lok;
2227    subf(R0, R1_SP, scratch);
2228    cmpdi(CCR0, R0, frame::abi_reg_args_size + frame::ijava_state_size);
2229    bge(CCR0, Lok);
2230    stop("frame too small (restore istate)", 0x5432);
2231    bind(Lok);
2232  }
2233  {
2234    Label Lok;
2235    ld(R0, _ijava_state_neg(ijava_reserved), scratch);
2236    cmpdi(CCR0, R0, 0x5afe);
2237    beq(CCR0, Lok);
2238    stop("frame corrupted (restore istate)", 0x5afe);
2239    bind(Lok);
2240  }
2241#endif
2242}
2243
2244void InterpreterMacroAssembler::get_method_counters(Register method,
2245                                                    Register Rcounters,
2246                                                    Label& skip) {
2247  BLOCK_COMMENT("Load and ev. allocate counter object {");
2248  Label has_counters;
2249  ld(Rcounters, in_bytes(Method::method_counters_offset()), method);
2250  cmpdi(CCR0, Rcounters, 0);
2251  bne(CCR0, has_counters);
2252  call_VM(noreg, CAST_FROM_FN_PTR(address,
2253                                  InterpreterRuntime::build_method_counters), method, false);
2254  ld(Rcounters, in_bytes(Method::method_counters_offset()), method);
2255  cmpdi(CCR0, Rcounters, 0);
2256  beq(CCR0, skip); // No MethodCounters, OutOfMemory.
2257  BLOCK_COMMENT("} Load and ev. allocate counter object");
2258
2259  bind(has_counters);
2260}
2261
2262void InterpreterMacroAssembler::increment_invocation_counter(Register Rcounters,
2263                                                             Register iv_be_count,
2264                                                             Register Rtmp_r0) {
2265  assert(UseCompiler || LogTouchedMethods, "incrementing must be useful");
2266  Register invocation_count = iv_be_count;
2267  Register backedge_count   = Rtmp_r0;
2268  int delta = InvocationCounter::count_increment;
2269
2270  // Load each counter in a register.
2271  //  ld(inv_counter, Rtmp);
2272  //  ld(be_counter, Rtmp2);
2273  int inv_counter_offset = in_bytes(MethodCounters::invocation_counter_offset() +
2274                                    InvocationCounter::counter_offset());
2275  int be_counter_offset  = in_bytes(MethodCounters::backedge_counter_offset() +
2276                                    InvocationCounter::counter_offset());
2277
2278  BLOCK_COMMENT("Increment profiling counters {");
2279
2280  // Load the backedge counter.
2281  lwz(backedge_count, be_counter_offset, Rcounters); // is unsigned int
2282  // Mask the backedge counter.
2283  andi(backedge_count, backedge_count, InvocationCounter::count_mask_value);
2284
2285  // Load the invocation counter.
2286  lwz(invocation_count, inv_counter_offset, Rcounters); // is unsigned int
2287  // Add the delta to the invocation counter and store the result.
2288  addi(invocation_count, invocation_count, delta);
2289  // Store value.
2290  stw(invocation_count, inv_counter_offset, Rcounters);
2291
2292  // Add invocation counter + backedge counter.
2293  add(iv_be_count, backedge_count, invocation_count);
2294
2295  // Note that this macro must leave the backedge_count + invocation_count in
2296  // register iv_be_count!
2297  BLOCK_COMMENT("} Increment profiling counters");
2298}
2299
2300void InterpreterMacroAssembler::verify_oop(Register reg, TosState state) {
2301  if (state == atos) { MacroAssembler::verify_oop(reg); }
2302}
2303
2304// Local helper function for the verify_oop_or_return_address macro.
2305static bool verify_return_address(Method* m, int bci) {
2306#ifndef PRODUCT
2307  address pc = (address)(m->constMethod()) + in_bytes(ConstMethod::codes_offset()) + bci;
2308  // Assume it is a valid return address if it is inside m and is preceded by a jsr.
2309  if (!m->contains(pc))                                            return false;
2310  address jsr_pc;
2311  jsr_pc = pc - Bytecodes::length_for(Bytecodes::_jsr);
2312  if (*jsr_pc == Bytecodes::_jsr   && jsr_pc >= m->code_base())    return true;
2313  jsr_pc = pc - Bytecodes::length_for(Bytecodes::_jsr_w);
2314  if (*jsr_pc == Bytecodes::_jsr_w && jsr_pc >= m->code_base())    return true;
2315#endif // PRODUCT
2316  return false;
2317}
2318
2319void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) {
2320  if (VerifyFPU) {
2321    unimplemented("verfiyFPU");
2322  }
2323}
2324
2325void InterpreterMacroAssembler::verify_oop_or_return_address(Register reg, Register Rtmp) {
2326  if (!VerifyOops) return;
2327
2328  // The VM documentation for the astore[_wide] bytecode allows
2329  // the TOS to be not only an oop but also a return address.
2330  Label test;
2331  Label skip;
2332  // See if it is an address (in the current method):
2333
2334  const int log2_bytecode_size_limit = 16;
2335  srdi_(Rtmp, reg, log2_bytecode_size_limit);
2336  bne(CCR0, test);
2337
2338  address fd = CAST_FROM_FN_PTR(address, verify_return_address);
2339  const int nbytes_save = MacroAssembler::num_volatile_regs * 8;
2340  save_volatile_gprs(R1_SP, -nbytes_save); // except R0
2341  save_LR_CR(Rtmp); // Save in old frame.
2342  push_frame_reg_args(nbytes_save, Rtmp);
2343
2344  load_const_optimized(Rtmp, fd, R0);
2345  mr_if_needed(R4_ARG2, reg);
2346  mr(R3_ARG1, R19_method);
2347  call_c(Rtmp); // call C
2348
2349  pop_frame();
2350  restore_LR_CR(Rtmp);
2351  restore_volatile_gprs(R1_SP, -nbytes_save); // except R0
2352  b(skip);
2353
2354  // Perform a more elaborate out-of-line call.
2355  // Not an address; verify it:
2356  bind(test);
2357  verify_oop(reg);
2358  bind(skip);
2359}
2360
2361// Inline assembly for:
2362//
2363// if (thread is in interp_only_mode) {
2364//   InterpreterRuntime::post_method_entry();
2365// }
2366// if (*jvmpi::event_flags_array_at_addr(JVMPI_EVENT_METHOD_ENTRY ) ||
2367//     *jvmpi::event_flags_array_at_addr(JVMPI_EVENT_METHOD_ENTRY2)   ) {
2368//   SharedRuntime::jvmpi_method_entry(method, receiver);
2369// }
2370void InterpreterMacroAssembler::notify_method_entry() {
2371  // JVMTI
2372  // Whenever JVMTI puts a thread in interp_only_mode, method
2373  // entry/exit events are sent for that thread to track stack
2374  // depth. If it is possible to enter interp_only_mode we add
2375  // the code to check if the event should be sent.
2376  if (JvmtiExport::can_post_interpreter_events()) {
2377    Label jvmti_post_done;
2378
2379    lwz(R0, in_bytes(JavaThread::interp_only_mode_offset()), R16_thread);
2380    cmpwi(CCR0, R0, 0);
2381    beq(CCR0, jvmti_post_done);
2382    call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_entry),
2383            /*check_exceptions=*/true);
2384
2385    bind(jvmti_post_done);
2386  }
2387}
2388
2389// Inline assembly for:
2390//
2391// if (thread is in interp_only_mode) {
2392//   // save result
2393//   InterpreterRuntime::post_method_exit();
2394//   // restore result
2395// }
2396// if (*jvmpi::event_flags_array_at_addr(JVMPI_EVENT_METHOD_EXIT)) {
2397//   // save result
2398//   SharedRuntime::jvmpi_method_exit();
2399//   // restore result
2400// }
2401//
2402// Native methods have their result stored in d_tmp and l_tmp.
2403// Java methods have their result stored in the expression stack.
2404void InterpreterMacroAssembler::notify_method_exit(bool is_native_method, TosState state,
2405                                                   NotifyMethodExitMode mode, bool check_exceptions) {
2406  // JVMTI
2407  // Whenever JVMTI puts a thread in interp_only_mode, method
2408  // entry/exit events are sent for that thread to track stack
2409  // depth. If it is possible to enter interp_only_mode we add
2410  // the code to check if the event should be sent.
2411  if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
2412    Label jvmti_post_done;
2413
2414    lwz(R0, in_bytes(JavaThread::interp_only_mode_offset()), R16_thread);
2415    cmpwi(CCR0, R0, 0);
2416    beq(CCR0, jvmti_post_done);
2417    if (!is_native_method) { push(state); } // Expose tos to GC.
2418    call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit),
2419            /*check_exceptions=*/check_exceptions);
2420    if (!is_native_method) { pop(state); }
2421
2422    align(32, 12);
2423    bind(jvmti_post_done);
2424  }
2425
2426  // Dtrace support not implemented.
2427}
2428
2429