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