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