interp_masm_sparc.cpp revision 13249:a2753984d2c1
1284345Ssjg/*
2284345Ssjg * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3284345Ssjg * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4284345Ssjg *
5284345Ssjg * This code is free software; you can redistribute it and/or modify it
6284345Ssjg * under the terms of the GNU General Public License version 2 only, as
7284345Ssjg * published by the Free Software Foundation.
8284345Ssjg *
9284345Ssjg * This code is distributed in the hope that it will be useful, but WITHOUT
10284345Ssjg * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11284345Ssjg * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#include "precompiled.hpp"
26#include "interp_masm_sparc.hpp"
27#include "interpreter/interpreter.hpp"
28#include "interpreter/interpreterRuntime.hpp"
29#include "logging/log.hpp"
30#include "oops/arrayOop.hpp"
31#include "oops/markOop.hpp"
32#include "oops/methodData.hpp"
33#include "oops/method.hpp"
34#include "oops/methodCounters.hpp"
35#include "prims/jvmtiExport.hpp"
36#include "prims/jvmtiThreadState.hpp"
37#include "runtime/basicLock.hpp"
38#include "runtime/biasedLocking.hpp"
39#include "runtime/sharedRuntime.hpp"
40#include "runtime/thread.inline.hpp"
41#include "utilities/align.hpp"
42
43// Implementation of InterpreterMacroAssembler
44
45// This file specializes the assember with interpreter-specific macros
46
47const Address InterpreterMacroAssembler::l_tmp(FP, (frame::interpreter_frame_l_scratch_fp_offset * wordSize) + STACK_BIAS);
48const Address InterpreterMacroAssembler::d_tmp(FP, (frame::interpreter_frame_d_scratch_fp_offset * wordSize) + STACK_BIAS);
49
50void InterpreterMacroAssembler::jump_to_entry(address entry) {
51  assert(entry, "Entry must have been generated by now");
52  AddressLiteral al(entry);
53  jump_to(al, G3_scratch);
54  delayed()->nop();
55}
56
57void InterpreterMacroAssembler::compute_extra_locals_size_in_bytes(Register args_size, Register locals_size, Register delta) {
58  // Note: this algorithm is also used by C1's OSR entry sequence.
59  // Any changes should also be applied to CodeEmitter::emit_osr_entry().
60  assert_different_registers(args_size, locals_size);
61  // max_locals*2 for TAGS.  Assumes that args_size has already been adjusted.
62  subcc(locals_size, args_size, delta);// extra space for non-arguments locals in words
63  // Use br/mov combination because it works on both V8 and V9 and is
64  // faster.
65  Label skip_move;
66  br(Assembler::negative, true, Assembler::pt, skip_move);
67  delayed()->mov(G0, delta);
68  bind(skip_move);
69  align_up(delta, WordsPerLong);       // make multiple of 2 (SP must be 2-word aligned)
70  sll(delta, LogBytesPerWord, delta);  // extra space for locals in bytes
71}
72
73// Dispatch code executed in the prolog of a bytecode which does not do it's
74// own dispatch. The dispatch address is computed and placed in IdispatchAddress
75void InterpreterMacroAssembler::dispatch_prolog(TosState state, int bcp_incr) {
76  assert_not_delayed();
77  ldub( Lbcp, bcp_incr, Lbyte_code);                    // load next bytecode
78  // dispatch table to use
79  AddressLiteral tbl(Interpreter::dispatch_table(state));
80  sll(Lbyte_code, LogBytesPerWord, Lbyte_code);         // multiply by wordSize
81  set(tbl, G3_scratch);                                 // compute addr of table
82  ld_ptr(G3_scratch, Lbyte_code, IdispatchAddress);     // get entry addr
83}
84
85
86// Dispatch code executed in the epilog of a bytecode which does not do it's
87// own dispatch. The dispatch address in IdispatchAddress is used for the
88// dispatch.
89void InterpreterMacroAssembler::dispatch_epilog(TosState state, int bcp_incr) {
90  assert_not_delayed();
91  verify_FPU(1, state);
92  interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
93  jmp( IdispatchAddress, 0 );
94  if (bcp_incr != 0)  delayed()->inc(Lbcp, bcp_incr);
95  else                delayed()->nop();
96}
97
98
99void InterpreterMacroAssembler::dispatch_next(TosState state, int bcp_incr) {
100  // %%%% consider branching to a single shared dispatch stub (for each bcp_incr)
101  assert_not_delayed();
102  ldub( Lbcp, bcp_incr, Lbyte_code);               // load next bytecode
103  dispatch_Lbyte_code(state, Interpreter::dispatch_table(state), bcp_incr);
104}
105
106
107void InterpreterMacroAssembler::dispatch_next_noverify_oop(TosState state, int bcp_incr) {
108  // %%%% consider branching to a single shared dispatch stub (for each bcp_incr)
109  assert_not_delayed();
110  ldub( Lbcp, bcp_incr, Lbyte_code);               // load next bytecode
111  dispatch_Lbyte_code(state, Interpreter::dispatch_table(state), bcp_incr, false);
112}
113
114
115void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) {
116  // load current bytecode
117  assert_not_delayed();
118  ldub( Lbcp, 0, Lbyte_code);               // load next bytecode
119  dispatch_base(state, table);
120}
121
122
123void InterpreterMacroAssembler::call_VM_leaf_base(
124  Register java_thread,
125  address  entry_point,
126  int      number_of_arguments
127) {
128  if (!java_thread->is_valid())
129    java_thread = L7_thread_cache;
130  // super call
131  MacroAssembler::call_VM_leaf_base(java_thread, entry_point, number_of_arguments);
132}
133
134
135void InterpreterMacroAssembler::call_VM_base(
136  Register        oop_result,
137  Register        java_thread,
138  Register        last_java_sp,
139  address         entry_point,
140  int             number_of_arguments,
141  bool            check_exception
142) {
143  if (!java_thread->is_valid())
144    java_thread = L7_thread_cache;
145  // See class ThreadInVMfromInterpreter, which assumes that the interpreter
146  // takes responsibility for setting its own thread-state on call-out.
147  // However, ThreadInVMfromInterpreter resets the state to "in_Java".
148
149  //save_bcp();                                  // save bcp
150  MacroAssembler::call_VM_base(oop_result, java_thread, last_java_sp, entry_point, number_of_arguments, check_exception);
151  //restore_bcp();                               // restore bcp
152  //restore_locals();                            // restore locals pointer
153}
154
155
156void InterpreterMacroAssembler::check_and_handle_popframe(Register scratch_reg) {
157  if (JvmtiExport::can_pop_frame()) {
158    Label L;
159
160    // Check the "pending popframe condition" flag in the current thread
161    ld(G2_thread, JavaThread::popframe_condition_offset(), scratch_reg);
162
163    // Initiate popframe handling only if it is not already being processed.  If the flag
164    // has the popframe_processing bit set, it means that this code is called *during* popframe
165    // handling - we don't want to reenter.
166    btst(JavaThread::popframe_pending_bit, scratch_reg);
167    br(zero, false, pt, L);
168    delayed()->nop();
169    btst(JavaThread::popframe_processing_bit, scratch_reg);
170    br(notZero, false, pt, L);
171    delayed()->nop();
172
173    // Call Interpreter::remove_activation_preserving_args_entry() to get the
174    // address of the same-named entrypoint in the generated interpreter code.
175    call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, Interpreter::remove_activation_preserving_args_entry));
176
177    // Jump to Interpreter::_remove_activation_preserving_args_entry
178    jmpl(O0, G0, G0);
179    delayed()->nop();
180    bind(L);
181  }
182}
183
184
185void InterpreterMacroAssembler::load_earlyret_value(TosState state) {
186  Register thr_state = G4_scratch;
187  ld_ptr(G2_thread, JavaThread::jvmti_thread_state_offset(), thr_state);
188  const Address tos_addr(thr_state, JvmtiThreadState::earlyret_tos_offset());
189  const Address oop_addr(thr_state, JvmtiThreadState::earlyret_oop_offset());
190  const Address val_addr(thr_state, JvmtiThreadState::earlyret_value_offset());
191  switch (state) {
192  case ltos: ld_long(val_addr, Otos_l);                   break;
193  case atos: ld_ptr(oop_addr, Otos_l);
194             st_ptr(G0, oop_addr);                        break;
195  case btos:                                           // fall through
196  case ztos:                                           // fall through
197  case ctos:                                           // fall through
198  case stos:                                           // fall through
199  case itos: ld(val_addr, Otos_l1);                       break;
200  case ftos: ldf(FloatRegisterImpl::S, val_addr, Ftos_f); break;
201  case dtos: ldf(FloatRegisterImpl::D, val_addr, Ftos_d); break;
202  case vtos: /* nothing to do */                          break;
203  default  : ShouldNotReachHere();
204  }
205  // Clean up tos value in the jvmti thread state
206  or3(G0, ilgl, G3_scratch);
207  stw(G3_scratch, tos_addr);
208  st_long(G0, val_addr);
209  interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
210}
211
212
213void InterpreterMacroAssembler::check_and_handle_earlyret(Register scratch_reg) {
214  if (JvmtiExport::can_force_early_return()) {
215    Label L;
216    Register thr_state = G3_scratch;
217    ld_ptr(G2_thread, JavaThread::jvmti_thread_state_offset(), thr_state);
218    br_null_short(thr_state, pt, L); // if (thread->jvmti_thread_state() == NULL) exit;
219
220    // Initiate earlyret handling only if it is not already being processed.
221    // If the flag has the earlyret_processing bit set, it means that this code
222    // is called *during* earlyret handling - we don't want to reenter.
223    ld(thr_state, JvmtiThreadState::earlyret_state_offset(), G4_scratch);
224    cmp_and_br_short(G4_scratch, JvmtiThreadState::earlyret_pending, Assembler::notEqual, pt, L);
225
226    // Call Interpreter::remove_activation_early_entry() to get the address of the
227    // same-named entrypoint in the generated interpreter code
228    ld(thr_state, JvmtiThreadState::earlyret_tos_offset(), Otos_l1);
229    call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, Interpreter::remove_activation_early_entry), Otos_l1);
230
231    // Jump to Interpreter::_remove_activation_early_entry
232    jmpl(O0, G0, G0);
233    delayed()->nop();
234    bind(L);
235  }
236}
237
238
239void InterpreterMacroAssembler::super_call_VM_leaf(Register thread_cache, address entry_point, Register arg_1, Register arg_2) {
240  mov(arg_1, O0);
241  mov(arg_2, O1);
242  MacroAssembler::call_VM_leaf_base(thread_cache, entry_point, 2);
243}
244
245void InterpreterMacroAssembler::dispatch_base(TosState state, address* table) {
246  assert_not_delayed();
247  dispatch_Lbyte_code(state, table);
248}
249
250
251void InterpreterMacroAssembler::dispatch_normal(TosState state) {
252  dispatch_base(state, Interpreter::normal_table(state));
253}
254
255
256void InterpreterMacroAssembler::dispatch_only(TosState state) {
257  dispatch_base(state, Interpreter::dispatch_table(state));
258}
259
260
261// common code to dispatch and dispatch_only
262// dispatch value in Lbyte_code and increment Lbcp
263
264void InterpreterMacroAssembler::dispatch_Lbyte_code(TosState state, address* table, int bcp_incr, bool verify) {
265  verify_FPU(1, state);
266  // %%%%% maybe implement +VerifyActivationFrameSize here
267  //verify_thread(); //too slow; we will just verify on method entry & exit
268  if (verify) interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
269  // dispatch table to use
270  AddressLiteral tbl(table);
271  sll(Lbyte_code, LogBytesPerWord, Lbyte_code);       // multiply by wordSize
272  set(tbl, G3_scratch);                               // compute addr of table
273  ld_ptr(G3_scratch, Lbyte_code, G3_scratch);         // get entry addr
274  jmp( G3_scratch, 0 );
275  if (bcp_incr != 0)  delayed()->inc(Lbcp, bcp_incr);
276  else                delayed()->nop();
277}
278
279
280// Helpers for expression stack
281
282// Longs and doubles are Category 2 computational types in the
283// JVM specification (section 3.11.1) and take 2 expression stack or
284// local slots.
285// Aligning them on 32 bit with tagged stacks is hard because the code generated
286// for the dup* bytecodes depends on what types are already on the stack.
287// If the types are split into the two stack/local slots, that is much easier
288// (and we can use 0 for non-reference tags).
289
290// Known good alignment in _LP64 but unknown otherwise
291void InterpreterMacroAssembler::load_unaligned_double(Register r1, int offset, FloatRegister d) {
292  assert_not_delayed();
293
294  ldf(FloatRegisterImpl::D, r1, offset, d);
295}
296
297// Known good alignment in _LP64 but unknown otherwise
298void InterpreterMacroAssembler::store_unaligned_double(FloatRegister d, Register r1, int offset) {
299  assert_not_delayed();
300
301  stf(FloatRegisterImpl::D, d, r1, offset);
302  // store something more useful here
303  debug_only(stx(G0, r1, offset+Interpreter::stackElementSize);)
304}
305
306
307// Known good alignment in _LP64 but unknown otherwise
308void InterpreterMacroAssembler::load_unaligned_long(Register r1, int offset, Register rd) {
309  assert_not_delayed();
310  ldx(r1, offset, rd);
311}
312
313// Known good alignment in _LP64 but unknown otherwise
314void InterpreterMacroAssembler::store_unaligned_long(Register l, Register r1, int offset) {
315  assert_not_delayed();
316
317  stx(l, r1, offset);
318  // store something more useful here
319  stx(G0, r1, offset+Interpreter::stackElementSize);
320}
321
322void InterpreterMacroAssembler::pop_i(Register r) {
323  assert_not_delayed();
324  ld(Lesp, Interpreter::expr_offset_in_bytes(0), r);
325  inc(Lesp, Interpreter::stackElementSize);
326  debug_only(verify_esp(Lesp));
327}
328
329void InterpreterMacroAssembler::pop_ptr(Register r, Register scratch) {
330  assert_not_delayed();
331  ld_ptr(Lesp, Interpreter::expr_offset_in_bytes(0), r);
332  inc(Lesp, Interpreter::stackElementSize);
333  debug_only(verify_esp(Lesp));
334}
335
336void InterpreterMacroAssembler::pop_l(Register r) {
337  assert_not_delayed();
338  load_unaligned_long(Lesp, Interpreter::expr_offset_in_bytes(0), r);
339  inc(Lesp, 2*Interpreter::stackElementSize);
340  debug_only(verify_esp(Lesp));
341}
342
343
344void InterpreterMacroAssembler::pop_f(FloatRegister f, Register scratch) {
345  assert_not_delayed();
346  ldf(FloatRegisterImpl::S, Lesp, Interpreter::expr_offset_in_bytes(0), f);
347  inc(Lesp, Interpreter::stackElementSize);
348  debug_only(verify_esp(Lesp));
349}
350
351
352void InterpreterMacroAssembler::pop_d(FloatRegister f, Register scratch) {
353  assert_not_delayed();
354  load_unaligned_double(Lesp, Interpreter::expr_offset_in_bytes(0), f);
355  inc(Lesp, 2*Interpreter::stackElementSize);
356  debug_only(verify_esp(Lesp));
357}
358
359
360void InterpreterMacroAssembler::push_i(Register r) {
361  assert_not_delayed();
362  debug_only(verify_esp(Lesp));
363  st(r, Lesp, 0);
364  dec(Lesp, Interpreter::stackElementSize);
365}
366
367void InterpreterMacroAssembler::push_ptr(Register r) {
368  assert_not_delayed();
369  st_ptr(r, Lesp, 0);
370  dec(Lesp, Interpreter::stackElementSize);
371}
372
373// remember: our convention for longs in SPARC is:
374// O0 (Otos_l1) has high-order part in first word,
375// O1 (Otos_l2) has low-order part in second word
376
377void InterpreterMacroAssembler::push_l(Register r) {
378  assert_not_delayed();
379  debug_only(verify_esp(Lesp));
380  // Longs are stored in memory-correct order, even if unaligned.
381  int offset = -Interpreter::stackElementSize;
382  store_unaligned_long(r, Lesp, offset);
383  dec(Lesp, 2 * Interpreter::stackElementSize);
384}
385
386
387void InterpreterMacroAssembler::push_f(FloatRegister f) {
388  assert_not_delayed();
389  debug_only(verify_esp(Lesp));
390  stf(FloatRegisterImpl::S, f, Lesp, 0);
391  dec(Lesp, Interpreter::stackElementSize);
392}
393
394
395void InterpreterMacroAssembler::push_d(FloatRegister d)   {
396  assert_not_delayed();
397  debug_only(verify_esp(Lesp));
398  // Longs are stored in memory-correct order, even if unaligned.
399  int offset = -Interpreter::stackElementSize;
400  store_unaligned_double(d, Lesp, offset);
401  dec(Lesp, 2 * Interpreter::stackElementSize);
402}
403
404
405void InterpreterMacroAssembler::push(TosState state) {
406  interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
407  switch (state) {
408    case atos: push_ptr();            break;
409    case btos:                        // fall through
410    case ztos:                        // fall through
411    case ctos:                        // fall through
412    case stos:                        // fall through
413    case itos: push_i();              break;
414    case ltos: push_l();              break;
415    case ftos: push_f();              break;
416    case dtos: push_d();              break;
417    case vtos: /* nothing to do */    break;
418    default  : ShouldNotReachHere();
419  }
420}
421
422
423void InterpreterMacroAssembler::pop(TosState state) {
424  switch (state) {
425    case atos: pop_ptr();            break;
426    case btos:                       // fall through
427    case ztos:                       // fall through
428    case ctos:                       // fall through
429    case stos:                       // fall through
430    case itos: pop_i();              break;
431    case ltos: pop_l();              break;
432    case ftos: pop_f();              break;
433    case dtos: pop_d();              break;
434    case vtos: /* nothing to do */   break;
435    default  : ShouldNotReachHere();
436  }
437  interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
438}
439
440
441// Helpers for swap and dup
442void InterpreterMacroAssembler::load_ptr(int n, Register val) {
443  ld_ptr(Lesp, Interpreter::expr_offset_in_bytes(n), val);
444}
445void InterpreterMacroAssembler::store_ptr(int n, Register val) {
446  st_ptr(val, Lesp, Interpreter::expr_offset_in_bytes(n));
447}
448
449
450void InterpreterMacroAssembler::load_receiver(Register param_count,
451                                              Register recv) {
452  sll(param_count, Interpreter::logStackElementSize, param_count);
453  ld_ptr(Lesp, param_count, recv);  // gets receiver oop
454}
455
456void InterpreterMacroAssembler::empty_expression_stack() {
457  // Reset Lesp.
458  sub( Lmonitors, wordSize, Lesp );
459
460  // Reset SP by subtracting more space from Lesp.
461  Label done;
462  assert(G4_scratch != Gframe_size, "Only you can prevent register aliasing!");
463
464  // A native does not need to do this, since its callee does not change SP.
465  ld(Lmethod, Method::access_flags_offset(), Gframe_size);  // Load access flags.
466  btst(JVM_ACC_NATIVE, Gframe_size);
467  br(Assembler::notZero, false, Assembler::pt, done);
468  delayed()->nop();
469
470  // Compute max expression stack+register save area
471  ld_ptr(Lmethod, in_bytes(Method::const_offset()), Gframe_size);
472  lduh(Gframe_size, in_bytes(ConstMethod::max_stack_offset()), Gframe_size);  // Load max stack.
473  add(Gframe_size, frame::memory_parameter_word_sp_offset+Method::extra_stack_entries(), Gframe_size );
474
475  //
476  // now set up a stack frame with the size computed above
477  //
478  //round_to( Gframe_size, WordsPerLong ); // -- moved down to the "and" below
479  sll( Gframe_size, LogBytesPerWord, Gframe_size );
480  sub( Lesp, Gframe_size, Gframe_size );
481  and3( Gframe_size, -(2 * wordSize), Gframe_size );          // align SP (downwards) to an 8/16-byte boundary
482  debug_only(verify_sp(Gframe_size, G4_scratch));
483  sub(Gframe_size, STACK_BIAS, Gframe_size );
484  mov(Gframe_size, SP);
485
486  bind(done);
487}
488
489
490#ifdef ASSERT
491void InterpreterMacroAssembler::verify_sp(Register Rsp, Register Rtemp) {
492  Label Bad, OK;
493
494  // Saved SP must be aligned.
495  btst(2*BytesPerWord-1, Rsp);
496  br(Assembler::notZero, false, Assembler::pn, Bad);
497  delayed()->nop();
498
499  // Saved SP, plus register window size, must not be above FP.
500  add(Rsp, frame::register_save_words * wordSize, Rtemp);
501  sub(Rtemp, STACK_BIAS, Rtemp);  // Bias Rtemp before cmp to FP
502  cmp_and_brx_short(Rtemp, FP, Assembler::greaterUnsigned, Assembler::pn, Bad);
503
504  // Saved SP must not be ridiculously below current SP.
505  size_t maxstack = MAX2(JavaThread::stack_size_at_create(), (size_t) 4*K*K);
506  set(maxstack, Rtemp);
507  sub(SP, Rtemp, Rtemp);
508  add(Rtemp, STACK_BIAS, Rtemp);  // Unbias Rtemp before cmp to Rsp
509  cmp_and_brx_short(Rsp, Rtemp, Assembler::lessUnsigned, Assembler::pn, Bad);
510
511  ba_short(OK);
512
513  bind(Bad);
514  stop("on return to interpreted call, restored SP is corrupted");
515
516  bind(OK);
517}
518
519
520void InterpreterMacroAssembler::verify_esp(Register Resp) {
521  // about to read or write Resp[0]
522  // make sure it is not in the monitors or the register save area
523  Label OK1, OK2;
524
525  cmp(Resp, Lmonitors);
526  brx(Assembler::lessUnsigned, true, Assembler::pt, OK1);
527  delayed()->sub(Resp, frame::memory_parameter_word_sp_offset * wordSize, Resp);
528  stop("too many pops:  Lesp points into monitor area");
529  bind(OK1);
530  sub(Resp, STACK_BIAS, Resp);
531  cmp(Resp, SP);
532  brx(Assembler::greaterEqualUnsigned, false, Assembler::pt, OK2);
533  delayed()->add(Resp, STACK_BIAS + frame::memory_parameter_word_sp_offset * wordSize, Resp);
534  stop("too many pushes:  Lesp points into register window");
535  bind(OK2);
536}
537#endif // ASSERT
538
539// Load compiled (i2c) or interpreter entry when calling from interpreted and
540// do the call. Centralized so that all interpreter calls will do the same actions.
541// If jvmti single stepping is on for a thread we must not call compiled code.
542void InterpreterMacroAssembler::call_from_interpreter(Register target, Register scratch, Register Rret) {
543
544  // Assume we want to go compiled if available
545
546  ld_ptr(G5_method, in_bytes(Method::from_interpreted_offset()), target);
547
548  if (JvmtiExport::can_post_interpreter_events()) {
549    // JVMTI events, such as single-stepping, are implemented partly by avoiding running
550    // compiled code in threads for which the event is enabled.  Check here for
551    // interp_only_mode if these events CAN be enabled.
552    verify_thread();
553    Label skip_compiled_code;
554
555    const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
556    ld(interp_only, scratch);
557    cmp_zero_and_br(Assembler::notZero, scratch, skip_compiled_code, true, Assembler::pn);
558    delayed()->ld_ptr(G5_method, in_bytes(Method::interpreter_entry_offset()), target);
559    bind(skip_compiled_code);
560  }
561
562  // the i2c_adapters need Method* in G5_method (right? %%%)
563  // do the call
564#ifdef ASSERT
565  {
566    Label ok;
567    br_notnull_short(target, Assembler::pt, ok);
568    stop("null entry point");
569    bind(ok);
570  }
571#endif // ASSERT
572
573  // Adjust Rret first so Llast_SP can be same as Rret
574  add(Rret, -frame::pc_return_offset, O7);
575  add(Lesp, BytesPerWord, Gargs); // setup parameter pointer
576  // Record SP so we can remove any stack space allocated by adapter transition
577  jmp(target, 0);
578  delayed()->mov(SP, Llast_SP);
579}
580
581void InterpreterMacroAssembler::if_cmp(Condition cc, bool ptr_compare) {
582  assert_not_delayed();
583
584  Label not_taken;
585  if (ptr_compare) brx(cc, false, Assembler::pn, not_taken);
586  else             br (cc, false, Assembler::pn, not_taken);
587  delayed()->nop();
588
589  TemplateTable::branch(false,false);
590
591  bind(not_taken);
592
593  profile_not_taken_branch(G3_scratch);
594}
595
596
597void InterpreterMacroAssembler::get_2_byte_integer_at_bcp(
598                                  int         bcp_offset,
599                                  Register    Rtmp,
600                                  Register    Rdst,
601                                  signedOrNot is_signed,
602                                  setCCOrNot  should_set_CC ) {
603  assert(Rtmp != Rdst, "need separate temp register");
604  assert_not_delayed();
605  switch (is_signed) {
606   default: ShouldNotReachHere();
607
608   case   Signed:  ldsb( Lbcp, bcp_offset, Rdst  );  break; // high byte
609   case Unsigned:  ldub( Lbcp, bcp_offset, Rdst  );  break; // high byte
610  }
611  ldub( Lbcp, bcp_offset + 1, Rtmp ); // low byte
612  sll( Rdst, BitsPerByte, Rdst);
613  switch (should_set_CC ) {
614   default: ShouldNotReachHere();
615
616   case      set_CC:  orcc( Rdst, Rtmp, Rdst ); break;
617   case dont_set_CC:  or3(  Rdst, Rtmp, Rdst ); break;
618  }
619}
620
621
622void InterpreterMacroAssembler::get_4_byte_integer_at_bcp(
623                                  int        bcp_offset,
624                                  Register   Rtmp,
625                                  Register   Rdst,
626                                  setCCOrNot should_set_CC ) {
627  assert(Rtmp != Rdst, "need separate temp register");
628  assert_not_delayed();
629  add( Lbcp, bcp_offset, Rtmp);
630  andcc( Rtmp, 3, G0);
631  Label aligned;
632  switch (should_set_CC ) {
633   default: ShouldNotReachHere();
634
635   case      set_CC: break;
636   case dont_set_CC: break;
637  }
638
639  br(Assembler::zero, true, Assembler::pn, aligned);
640  delayed()->ldsw(Rtmp, 0, Rdst);
641
642  ldub(Lbcp, bcp_offset + 3, Rdst);
643  ldub(Lbcp, bcp_offset + 2, Rtmp);  sll(Rtmp,  8, Rtmp);  or3(Rtmp, Rdst, Rdst);
644  ldub(Lbcp, bcp_offset + 1, Rtmp);  sll(Rtmp, 16, Rtmp);  or3(Rtmp, Rdst, Rdst);
645  ldsb(Lbcp, bcp_offset + 0, Rtmp);  sll(Rtmp, 24, Rtmp);
646  or3(Rtmp, Rdst, Rdst );
647
648  bind(aligned);
649  if (should_set_CC == set_CC) tst(Rdst);
650}
651
652void InterpreterMacroAssembler::get_cache_index_at_bcp(Register temp, Register index,
653                                                       int bcp_offset, size_t index_size) {
654  assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
655  if (index_size == sizeof(u2)) {
656    get_2_byte_integer_at_bcp(bcp_offset, temp, index, Unsigned);
657  } else if (index_size == sizeof(u4)) {
658    get_4_byte_integer_at_bcp(bcp_offset, temp, index);
659    assert(ConstantPool::decode_invokedynamic_index(~123) == 123, "else change next line");
660    xor3(index, -1, index);  // convert to plain index
661  } else if (index_size == sizeof(u1)) {
662    ldub(Lbcp, bcp_offset, index);
663  } else {
664    ShouldNotReachHere();
665  }
666}
667
668
669void InterpreterMacroAssembler::get_cache_and_index_at_bcp(Register cache, Register tmp,
670                                                           int bcp_offset, size_t index_size) {
671  assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
672  assert_different_registers(cache, tmp);
673  assert_not_delayed();
674  get_cache_index_at_bcp(cache, tmp, bcp_offset, index_size);
675  // convert from field index to ConstantPoolCacheEntry index and from
676  // word index to byte offset
677  sll(tmp, exact_log2(in_words(ConstantPoolCacheEntry::size()) * BytesPerWord), tmp);
678  add(LcpoolCache, tmp, cache);
679}
680
681
682void InterpreterMacroAssembler::get_cache_and_index_and_bytecode_at_bcp(Register cache,
683                                                                        Register temp,
684                                                                        Register bytecode,
685                                                                        int byte_no,
686                                                                        int bcp_offset,
687                                                                        size_t index_size) {
688  get_cache_and_index_at_bcp(cache, temp, bcp_offset, index_size);
689  ld_ptr(cache, ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::indices_offset(), bytecode);
690  const int shift_count = (1 + byte_no) * BitsPerByte;
691  assert((byte_no == TemplateTable::f1_byte && shift_count == ConstantPoolCacheEntry::bytecode_1_shift) ||
692         (byte_no == TemplateTable::f2_byte && shift_count == ConstantPoolCacheEntry::bytecode_2_shift),
693         "correct shift count");
694  srl(bytecode, shift_count, bytecode);
695  assert(ConstantPoolCacheEntry::bytecode_1_mask == ConstantPoolCacheEntry::bytecode_2_mask, "common mask");
696  and3(bytecode, ConstantPoolCacheEntry::bytecode_1_mask, bytecode);
697}
698
699
700void InterpreterMacroAssembler::get_cache_entry_pointer_at_bcp(Register cache, Register tmp,
701                                                               int bcp_offset, size_t index_size) {
702  assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
703  assert_different_registers(cache, tmp);
704  assert_not_delayed();
705  if (index_size == sizeof(u2)) {
706    get_2_byte_integer_at_bcp(bcp_offset, cache, tmp, Unsigned);
707  } else {
708    ShouldNotReachHere();  // other sizes not supported here
709  }
710              // convert from field index to ConstantPoolCacheEntry index
711              // and from word index to byte offset
712  sll(tmp, exact_log2(in_words(ConstantPoolCacheEntry::size()) * BytesPerWord), tmp);
713              // skip past the header
714  add(tmp, in_bytes(ConstantPoolCache::base_offset()), tmp);
715              // construct pointer to cache entry
716  add(LcpoolCache, tmp, cache);
717}
718
719
720// Load object from cpool->resolved_references(index)
721void InterpreterMacroAssembler::load_resolved_reference_at_index(
722                                           Register result, Register index) {
723  assert_different_registers(result, index);
724  assert_not_delayed();
725  // convert from field index to resolved_references() index and from
726  // word index to byte offset. Since this is a java object, it can be compressed
727  Register tmp = index;  // reuse
728  sll(index, LogBytesPerHeapOop, tmp);
729  get_constant_pool(result);
730  // load pointer for resolved_references[] objArray
731  ld_ptr(result, ConstantPool::cache_offset_in_bytes(), result);
732  ld_ptr(result, ConstantPoolCache::resolved_references_offset_in_bytes(), result);
733  // JNIHandles::resolve(result)
734  ld_ptr(result, 0, result);
735  // Add in the index
736  add(result, tmp, result);
737  load_heap_oop(result, arrayOopDesc::base_offset_in_bytes(T_OBJECT), result);
738}
739
740
741// load cpool->resolved_klass_at(index)
742void InterpreterMacroAssembler::load_resolved_klass_at_offset(Register Rcpool,
743                                           Register Roffset, Register Rklass) {
744  // int value = *this_cp->int_at_addr(which);
745  // int resolved_klass_index = extract_low_short_from_int(value);
746  //
747  // Because SPARC is big-endian, the low_short is at (cpool->int_at_addr(which) + 2 bytes)
748  add(Roffset, Rcpool, Roffset);
749  lduh(Roffset, sizeof(ConstantPool) + 2, Roffset);  // Roffset = resolved_klass_index
750
751  Register Rresolved_klasses = Rklass;
752  ld_ptr(Rcpool, ConstantPool::resolved_klasses_offset_in_bytes(), Rresolved_klasses);
753  sll(Roffset, LogBytesPerWord, Roffset);
754  add(Roffset, Array<Klass*>::base_offset_in_bytes(), Roffset);
755  ld_ptr(Rresolved_klasses, Roffset, Rklass);
756}
757
758
759// Generate a subtype check: branch to ok_is_subtype if sub_klass is
760// a subtype of super_klass.  Blows registers Rsuper_klass, Rsub_klass, tmp1, tmp2.
761void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass,
762                                                  Register Rsuper_klass,
763                                                  Register Rtmp1,
764                                                  Register Rtmp2,
765                                                  Register Rtmp3,
766                                                  Label &ok_is_subtype ) {
767  Label not_subtype;
768
769  // Profile the not-null value's klass.
770  profile_typecheck(Rsub_klass, Rtmp1);
771
772  check_klass_subtype_fast_path(Rsub_klass, Rsuper_klass,
773                                Rtmp1, Rtmp2,
774                                &ok_is_subtype, &not_subtype, NULL);
775
776  check_klass_subtype_slow_path(Rsub_klass, Rsuper_klass,
777                                Rtmp1, Rtmp2, Rtmp3, /*hack:*/ noreg,
778                                &ok_is_subtype, NULL);
779
780  bind(not_subtype);
781  profile_typecheck_failed(Rtmp1);
782}
783
784// Separate these two to allow for delay slot in middle
785// These are used to do a test and full jump to exception-throwing code.
786
787// %%%%% Could possibly reoptimize this by testing to see if could use
788// a single conditional branch (i.e. if span is small enough.
789// If you go that route, than get rid of the split and give up
790// on the delay-slot hack.
791
792void InterpreterMacroAssembler::throw_if_not_1_icc( Condition ok_condition,
793                                                    Label&    ok ) {
794  assert_not_delayed();
795  br(ok_condition, true, pt, ok);
796  // DELAY SLOT
797}
798
799void InterpreterMacroAssembler::throw_if_not_1_xcc( Condition ok_condition,
800                                                    Label&    ok ) {
801  assert_not_delayed();
802  bp( ok_condition, true, Assembler::xcc, pt, ok);
803  // DELAY SLOT
804}
805
806void InterpreterMacroAssembler::throw_if_not_1_x( Condition ok_condition,
807                                                  Label&    ok ) {
808  assert_not_delayed();
809  brx(ok_condition, true, pt, ok);
810  // DELAY SLOT
811}
812
813void InterpreterMacroAssembler::throw_if_not_2( address  throw_entry_point,
814                                                Register Rscratch,
815                                                Label&   ok ) {
816  assert(throw_entry_point != NULL, "entry point must be generated by now");
817  AddressLiteral dest(throw_entry_point);
818  jump_to(dest, Rscratch);
819  delayed()->nop();
820  bind(ok);
821}
822
823
824// And if you cannot use the delay slot, here is a shorthand:
825
826void InterpreterMacroAssembler::throw_if_not_icc( Condition ok_condition,
827                                                  address   throw_entry_point,
828                                                  Register  Rscratch ) {
829  Label ok;
830  if (ok_condition != never) {
831    throw_if_not_1_icc( ok_condition, ok);
832    delayed()->nop();
833  }
834  throw_if_not_2( throw_entry_point, Rscratch, ok);
835}
836void InterpreterMacroAssembler::throw_if_not_xcc( Condition ok_condition,
837                                                  address   throw_entry_point,
838                                                  Register  Rscratch ) {
839  Label ok;
840  if (ok_condition != never) {
841    throw_if_not_1_xcc( ok_condition, ok);
842    delayed()->nop();
843  }
844  throw_if_not_2( throw_entry_point, Rscratch, ok);
845}
846void InterpreterMacroAssembler::throw_if_not_x( Condition ok_condition,
847                                                address   throw_entry_point,
848                                                Register  Rscratch ) {
849  Label ok;
850  if (ok_condition != never) {
851    throw_if_not_1_x( ok_condition, ok);
852    delayed()->nop();
853  }
854  throw_if_not_2( throw_entry_point, Rscratch, ok);
855}
856
857// Check that index is in range for array, then shift index by index_shift, and put arrayOop + shifted_index into res
858// Note: res is still shy of address by array offset into object.
859
860void InterpreterMacroAssembler::index_check_without_pop(Register array, Register index, int index_shift, Register tmp, Register res) {
861  assert_not_delayed();
862
863  verify_oop(array);
864  // sign extend since tos (index) can be a 32bit value
865  sra(index, G0, index);
866
867  // check array
868  Label ptr_ok;
869  tst(array);
870  throw_if_not_1_x( notZero, ptr_ok );
871  delayed()->ld( array, arrayOopDesc::length_offset_in_bytes(), tmp ); // check index
872  throw_if_not_2( Interpreter::_throw_NullPointerException_entry, G3_scratch, ptr_ok);
873
874  Label index_ok;
875  cmp(index, tmp);
876  throw_if_not_1_icc( lessUnsigned, index_ok );
877  if (index_shift > 0)  delayed()->sll(index, index_shift, index);
878  else                  delayed()->add(array, index, res); // addr - const offset in index
879  // convention: move aberrant index into G3_scratch for exception message
880  mov(index, G3_scratch);
881  throw_if_not_2( Interpreter::_throw_ArrayIndexOutOfBoundsException_entry, G4_scratch, index_ok);
882
883  // add offset if didn't do it in delay slot
884  if (index_shift > 0)   add(array, index, res); // addr - const offset in index
885}
886
887
888void InterpreterMacroAssembler::index_check(Register array, Register index, int index_shift, Register tmp, Register res) {
889  assert_not_delayed();
890
891  // pop array
892  pop_ptr(array);
893
894  // check array
895  index_check_without_pop(array, index, index_shift, tmp, res);
896}
897
898
899void InterpreterMacroAssembler::get_const(Register Rdst) {
900  ld_ptr(Lmethod, in_bytes(Method::const_offset()), Rdst);
901}
902
903
904void InterpreterMacroAssembler::get_constant_pool(Register Rdst) {
905  get_const(Rdst);
906  ld_ptr(Rdst, in_bytes(ConstMethod::constants_offset()), Rdst);
907}
908
909
910void InterpreterMacroAssembler::get_constant_pool_cache(Register Rdst) {
911  get_constant_pool(Rdst);
912  ld_ptr(Rdst, ConstantPool::cache_offset_in_bytes(), Rdst);
913}
914
915
916void InterpreterMacroAssembler::get_cpool_and_tags(Register Rcpool, Register Rtags) {
917  get_constant_pool(Rcpool);
918  ld_ptr(Rcpool, ConstantPool::tags_offset_in_bytes(), Rtags);
919}
920
921
922// unlock if synchronized method
923//
924// Unlock the receiver if this is a synchronized method.
925// Unlock any Java monitors from syncronized blocks.
926//
927// If there are locked Java monitors
928//    If throw_monitor_exception
929//       throws IllegalMonitorStateException
930//    Else if install_monitor_exception
931//       installs IllegalMonitorStateException
932//    Else
933//       no error processing
934void InterpreterMacroAssembler::unlock_if_synchronized_method(TosState state,
935                                                              bool throw_monitor_exception,
936                                                              bool install_monitor_exception) {
937  Label unlocked, unlock, no_unlock;
938
939  // get the value of _do_not_unlock_if_synchronized into G1_scratch
940  const Address do_not_unlock_if_synchronized(G2_thread,
941    JavaThread::do_not_unlock_if_synchronized_offset());
942  ldbool(do_not_unlock_if_synchronized, G1_scratch);
943  stbool(G0, do_not_unlock_if_synchronized); // reset the flag
944
945  // check if synchronized method
946  const Address access_flags(Lmethod, Method::access_flags_offset());
947  interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
948  push(state); // save tos
949  ld(access_flags, G3_scratch); // Load access flags.
950  btst(JVM_ACC_SYNCHRONIZED, G3_scratch);
951  br(zero, false, pt, unlocked);
952  delayed()->nop();
953
954  // Don't unlock anything if the _do_not_unlock_if_synchronized flag
955  // is set.
956  cmp_zero_and_br(Assembler::notZero, G1_scratch, no_unlock);
957  delayed()->nop();
958
959  // BasicObjectLock will be first in list, since this is a synchronized method. However, need
960  // to check that the object has not been unlocked by an explicit monitorexit bytecode.
961
962  //Intel: if (throw_monitor_exception) ... else ...
963  // Entry already unlocked, need to throw exception
964  //...
965
966  // pass top-most monitor elem
967  add( top_most_monitor(), O1 );
968
969  ld_ptr(O1, BasicObjectLock::obj_offset_in_bytes(), G3_scratch);
970  br_notnull_short(G3_scratch, pt, unlock);
971
972  if (throw_monitor_exception) {
973    // Entry already unlocked need to throw an exception
974    MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
975    should_not_reach_here();
976  } else {
977    // Monitor already unlocked during a stack unroll.
978    // If requested, install an illegal_monitor_state_exception.
979    // Continue with stack unrolling.
980    if (install_monitor_exception) {
981      MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::new_illegal_monitor_state_exception));
982    }
983    ba_short(unlocked);
984  }
985
986  bind(unlock);
987
988  unlock_object(O1);
989
990  bind(unlocked);
991
992  // I0, I1: Might contain return value
993
994  // Check that all monitors are unlocked
995  { Label loop, exception, entry, restart;
996
997    Register Rmptr   = O0;
998    Register Rtemp   = O1;
999    Register Rlimit  = Lmonitors;
1000    const jint delta = frame::interpreter_frame_monitor_size() * wordSize;
1001    assert( (delta & LongAlignmentMask) == 0,
1002            "sizeof BasicObjectLock must be even number of doublewords");
1003
1004    #ifdef ASSERT
1005    add(top_most_monitor(), Rmptr, delta);
1006    { Label L;
1007      // ensure that Rmptr starts out above (or at) Rlimit
1008      cmp_and_brx_short(Rmptr, Rlimit, Assembler::greaterEqualUnsigned, pn, L);
1009      stop("monitor stack has negative size");
1010      bind(L);
1011    }
1012    #endif
1013    bind(restart);
1014    ba(entry);
1015    delayed()->
1016    add(top_most_monitor(), Rmptr, delta);      // points to current entry, starting with bottom-most entry
1017
1018    // Entry is still locked, need to throw exception
1019    bind(exception);
1020    if (throw_monitor_exception) {
1021      MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
1022      should_not_reach_here();
1023    } else {
1024      // Stack unrolling. Unlock object and if requested, install illegal_monitor_exception.
1025      // Unlock does not block, so don't have to worry about the frame
1026      unlock_object(Rmptr);
1027      if (install_monitor_exception) {
1028        MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::new_illegal_monitor_state_exception));
1029      }
1030      ba_short(restart);
1031    }
1032
1033    bind(loop);
1034    cmp(Rtemp, G0);                             // check if current entry is used
1035    brx(Assembler::notEqual, false, pn, exception);
1036    delayed()->
1037    dec(Rmptr, delta);                          // otherwise advance to next entry
1038    #ifdef ASSERT
1039    { Label L;
1040      // ensure that Rmptr has not somehow stepped below Rlimit
1041      cmp_and_brx_short(Rmptr, Rlimit, Assembler::greaterEqualUnsigned, pn, L);
1042      stop("ran off the end of the monitor stack");
1043      bind(L);
1044    }
1045    #endif
1046    bind(entry);
1047    cmp(Rmptr, Rlimit);                         // check if bottom reached
1048    brx(Assembler::notEqual, true, pn, loop);   // if not at bottom then check this entry
1049    delayed()->
1050    ld_ptr(Rmptr, BasicObjectLock::obj_offset_in_bytes() - delta, Rtemp);
1051  }
1052
1053  bind(no_unlock);
1054  pop(state);
1055  interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
1056}
1057
1058void InterpreterMacroAssembler::narrow(Register result) {
1059
1060  ld_ptr(Address(Lmethod, Method::const_offset()), G3_scratch);
1061  ldub(G3_scratch, in_bytes(ConstMethod::result_type_offset()), G3_scratch);
1062
1063  Label notBool, notByte, notChar, done;
1064
1065  // common case first
1066  cmp(G3_scratch, T_INT);
1067  br(Assembler::equal, true, pn, done);
1068  delayed()->nop();
1069
1070  cmp(G3_scratch, T_BOOLEAN);
1071  br(Assembler::notEqual, true, pn, notBool);
1072  delayed()->cmp(G3_scratch, T_BYTE);
1073  and3(result, 1, result);
1074  ba(done);
1075  delayed()->nop();
1076
1077  bind(notBool);
1078  // cmp(G3_scratch, T_BYTE);
1079  br(Assembler::notEqual, true, pn, notByte);
1080  delayed()->cmp(G3_scratch, T_CHAR);
1081  sll(result, 24, result);
1082  sra(result, 24, result);
1083  ba(done);
1084  delayed()->nop();
1085
1086  bind(notByte);
1087  // cmp(G3_scratch, T_CHAR);
1088  sll(result, 16, result);
1089  br(Assembler::notEqual, true, pn, done);
1090  delayed()->sra(result, 16, result);
1091  // sll(result, 16, result);
1092  srl(result, 16, result);
1093
1094  // bind(notChar);
1095  // must be short, instructions already executed in delay slot
1096  // sll(result, 16, result);
1097  // sra(result, 16, result);
1098
1099  bind(done);
1100}
1101
1102// remove activation
1103//
1104// Unlock the receiver if this is a synchronized method.
1105// Unlock any Java monitors from syncronized blocks.
1106// Remove the activation from the stack.
1107//
1108// If there are locked Java monitors
1109//    If throw_monitor_exception
1110//       throws IllegalMonitorStateException
1111//    Else if install_monitor_exception
1112//       installs IllegalMonitorStateException
1113//    Else
1114//       no error processing
1115void InterpreterMacroAssembler::remove_activation(TosState state,
1116                                                  bool throw_monitor_exception,
1117                                                  bool install_monitor_exception) {
1118
1119  unlock_if_synchronized_method(state, throw_monitor_exception, install_monitor_exception);
1120
1121  // save result (push state before jvmti call and pop it afterwards) and notify jvmti
1122  notify_method_exit(false, state, NotifyJVMTI);
1123
1124  if (StackReservedPages > 0) {
1125    // testing if Stack Reserved Area needs to be re-enabled
1126    Label no_reserved_zone_enabling;
1127    ld_ptr(G2_thread, JavaThread::reserved_stack_activation_offset(), G3_scratch);
1128    cmp_and_brx_short(SP, G3_scratch, Assembler::lessUnsigned, Assembler::pt, no_reserved_zone_enabling);
1129
1130    call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), G2_thread);
1131    call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_delayed_StackOverflowError), G2_thread);
1132    should_not_reach_here();
1133
1134    bind(no_reserved_zone_enabling);
1135  }
1136
1137  interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
1138  verify_thread();
1139
1140  // return tos
1141  assert(Otos_l1 == Otos_i, "adjust code below");
1142  switch (state) {
1143  case ltos: mov(Otos_l, Otos_l->after_save()); break; // O0 -> I0
1144  case btos:                                      // fall through
1145  case ztos:                                      // fall through
1146  case ctos:
1147  case stos:                                      // fall through
1148  case atos:                                      // fall through
1149  case itos: mov(Otos_l1, Otos_l1->after_save());    break;        // O0 -> I0
1150  case ftos:                                      // fall through
1151  case dtos:                                      // fall through
1152  case vtos: /* nothing to do */                     break;
1153  default  : ShouldNotReachHere();
1154  }
1155}
1156
1157// Lock object
1158//
1159// Argument - lock_reg points to the BasicObjectLock to be used for locking,
1160//            it must be initialized with the object to lock
1161void InterpreterMacroAssembler::lock_object(Register lock_reg, Register Object) {
1162  if (UseHeavyMonitors) {
1163    call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), lock_reg);
1164  }
1165  else {
1166    Register obj_reg = Object;
1167    Register mark_reg = G4_scratch;
1168    Register temp_reg = G1_scratch;
1169    Address  lock_addr(lock_reg, BasicObjectLock::lock_offset_in_bytes());
1170    Address  mark_addr(obj_reg, oopDesc::mark_offset_in_bytes());
1171    Label    done;
1172
1173    Label slow_case;
1174
1175    assert_different_registers(lock_reg, obj_reg, mark_reg, temp_reg);
1176
1177    // load markOop from object into mark_reg
1178    ld_ptr(mark_addr, mark_reg);
1179
1180    if (UseBiasedLocking) {
1181      biased_locking_enter(obj_reg, mark_reg, temp_reg, done, &slow_case);
1182    }
1183
1184    // get the address of basicLock on stack that will be stored in the object
1185    // we need a temporary register here as we do not want to clobber lock_reg
1186    // (cas clobbers the destination register)
1187    mov(lock_reg, temp_reg);
1188    // set mark reg to be (markOop of object | UNLOCK_VALUE)
1189    or3(mark_reg, markOopDesc::unlocked_value, mark_reg);
1190    // initialize the box  (Must happen before we update the object mark!)
1191    st_ptr(mark_reg, lock_addr, BasicLock::displaced_header_offset_in_bytes());
1192    // compare and exchange object_addr, markOop | 1, stack address of basicLock
1193    assert(mark_addr.disp() == 0, "cas must take a zero displacement");
1194    cas_ptr(mark_addr.base(), mark_reg, temp_reg);
1195
1196    // if the compare and exchange succeeded we are done (we saw an unlocked object)
1197    cmp_and_brx_short(mark_reg, temp_reg, Assembler::equal, Assembler::pt, done);
1198
1199    // We did not see an unlocked object so try the fast recursive case
1200
1201    // Check if owner is self by comparing the value in the markOop of object
1202    // with the stack pointer
1203    sub(temp_reg, SP, temp_reg);
1204    sub(temp_reg, STACK_BIAS, temp_reg);
1205    assert(os::vm_page_size() > 0xfff, "page size too small - change the constant");
1206
1207    // Composite "andcc" test:
1208    // (a) %sp -vs- markword proximity check, and,
1209    // (b) verify mark word LSBs == 0 (Stack-locked).
1210    //
1211    // FFFFF003/FFFFFFFFFFFF003 is (markOopDesc::lock_mask_in_place | -os::vm_page_size())
1212    // Note that the page size used for %sp proximity testing is arbitrary and is
1213    // unrelated to the actual MMU page size.  We use a 'logical' page size of
1214    // 4096 bytes.   F..FFF003 is designed to fit conveniently in the SIMM13 immediate
1215    // field of the andcc instruction.
1216    andcc (temp_reg, 0xFFFFF003, G0) ;
1217
1218    // if condition is true we are done and hence we can store 0 in the displaced
1219    // header indicating it is a recursive lock and be done
1220    brx(Assembler::zero, true, Assembler::pt, done);
1221    delayed()->st_ptr(G0, lock_addr, BasicLock::displaced_header_offset_in_bytes());
1222
1223    // none of the above fast optimizations worked so we have to get into the
1224    // slow case of monitor enter
1225    bind(slow_case);
1226    call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), lock_reg);
1227
1228    bind(done);
1229  }
1230}
1231
1232// Unlocks an object. Used in monitorexit bytecode and remove_activation.
1233//
1234// Argument - lock_reg points to the BasicObjectLock for lock
1235// Throw IllegalMonitorException if object is not locked by current thread
1236void InterpreterMacroAssembler::unlock_object(Register lock_reg) {
1237  if (UseHeavyMonitors) {
1238    call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
1239  } else {
1240    Register obj_reg = G3_scratch;
1241    Register mark_reg = G4_scratch;
1242    Register displaced_header_reg = G1_scratch;
1243    Address  lockobj_addr(lock_reg, BasicObjectLock::obj_offset_in_bytes());
1244    Address  mark_addr(obj_reg, oopDesc::mark_offset_in_bytes());
1245    Label    done;
1246
1247    if (UseBiasedLocking) {
1248      // load the object out of the BasicObjectLock
1249      ld_ptr(lockobj_addr, obj_reg);
1250      biased_locking_exit(mark_addr, mark_reg, done, true);
1251      st_ptr(G0, lockobj_addr);  // free entry
1252    }
1253
1254    // Test first if we are in the fast recursive case
1255    Address lock_addr(lock_reg, BasicObjectLock::lock_offset_in_bytes() + BasicLock::displaced_header_offset_in_bytes());
1256    ld_ptr(lock_addr, displaced_header_reg);
1257    br_null(displaced_header_reg, true, Assembler::pn, done);
1258    delayed()->st_ptr(G0, lockobj_addr);  // free entry
1259
1260    // See if it is still a light weight lock, if so we just unlock
1261    // the object and we are done
1262
1263    if (!UseBiasedLocking) {
1264      // load the object out of the BasicObjectLock
1265      ld_ptr(lockobj_addr, obj_reg);
1266    }
1267
1268    // we have the displaced header in displaced_header_reg
1269    // we expect to see the stack address of the basicLock in case the
1270    // lock is still a light weight lock (lock_reg)
1271    assert(mark_addr.disp() == 0, "cas must take a zero displacement");
1272    cas_ptr(mark_addr.base(), lock_reg, displaced_header_reg);
1273    cmp(lock_reg, displaced_header_reg);
1274    brx(Assembler::equal, true, Assembler::pn, done);
1275    delayed()->st_ptr(G0, lockobj_addr);  // free entry
1276
1277    // The lock has been converted into a heavy lock and hence
1278    // we need to get into the slow case
1279
1280    call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
1281
1282    bind(done);
1283  }
1284}
1285
1286// Get the method data pointer from the Method* and set the
1287// specified register to its value.
1288
1289void InterpreterMacroAssembler::set_method_data_pointer() {
1290  assert(ProfileInterpreter, "must be profiling interpreter");
1291  Label get_continue;
1292
1293  ld_ptr(Lmethod, in_bytes(Method::method_data_offset()), ImethodDataPtr);
1294  test_method_data_pointer(get_continue);
1295  add(ImethodDataPtr, in_bytes(MethodData::data_offset()), ImethodDataPtr);
1296  bind(get_continue);
1297}
1298
1299// Set the method data pointer for the current bcp.
1300
1301void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
1302  assert(ProfileInterpreter, "must be profiling interpreter");
1303  Label zero_continue;
1304
1305  // Test MDO to avoid the call if it is NULL.
1306  ld_ptr(Lmethod, in_bytes(Method::method_data_offset()), ImethodDataPtr);
1307  test_method_data_pointer(zero_continue);
1308  call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), Lmethod, Lbcp);
1309  add(ImethodDataPtr, in_bytes(MethodData::data_offset()), ImethodDataPtr);
1310  add(ImethodDataPtr, O0, ImethodDataPtr);
1311  bind(zero_continue);
1312}
1313
1314// Test ImethodDataPtr.  If it is null, continue at the specified label
1315
1316void InterpreterMacroAssembler::test_method_data_pointer(Label& zero_continue) {
1317  assert(ProfileInterpreter, "must be profiling interpreter");
1318  br_null_short(ImethodDataPtr, Assembler::pn, zero_continue);
1319}
1320
1321void InterpreterMacroAssembler::verify_method_data_pointer() {
1322  assert(ProfileInterpreter, "must be profiling interpreter");
1323#ifdef ASSERT
1324  Label verify_continue;
1325  test_method_data_pointer(verify_continue);
1326
1327  // If the mdp is valid, it will point to a DataLayout header which is
1328  // consistent with the bcp.  The converse is highly probable also.
1329  lduh(ImethodDataPtr, in_bytes(DataLayout::bci_offset()), G3_scratch);
1330  ld_ptr(Lmethod, Method::const_offset(), O5);
1331  add(G3_scratch, in_bytes(ConstMethod::codes_offset()), G3_scratch);
1332  add(G3_scratch, O5, G3_scratch);
1333  cmp(Lbcp, G3_scratch);
1334  brx(Assembler::equal, false, Assembler::pt, verify_continue);
1335
1336  Register temp_reg = O5;
1337  delayed()->mov(ImethodDataPtr, temp_reg);
1338  // %%% should use call_VM_leaf here?
1339  //call_VM_leaf(noreg, ..., Lmethod, Lbcp, ImethodDataPtr);
1340  save_frame_and_mov(sizeof(jdouble) / wordSize, Lmethod, O0, Lbcp, O1);
1341  Address d_save(FP, -sizeof(jdouble) + STACK_BIAS);
1342  stf(FloatRegisterImpl::D, Ftos_d, d_save);
1343  mov(temp_reg->after_save(), O2);
1344  save_thread(L7_thread_cache);
1345  call(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp), relocInfo::none);
1346  delayed()->nop();
1347  restore_thread(L7_thread_cache);
1348  ldf(FloatRegisterImpl::D, d_save, Ftos_d);
1349  restore();
1350  bind(verify_continue);
1351#endif // ASSERT
1352}
1353
1354void InterpreterMacroAssembler::test_invocation_counter_for_mdp(Register invocation_count,
1355                                                                Register method_counters,
1356                                                                Register Rtmp,
1357                                                                Label &profile_continue) {
1358  assert(ProfileInterpreter, "must be profiling interpreter");
1359  // Control will flow to "profile_continue" if the counter is less than the
1360  // limit or if we call profile_method()
1361
1362  Label done;
1363
1364  // if no method data exists, and the counter is high enough, make one
1365  br_notnull_short(ImethodDataPtr, Assembler::pn, done);
1366
1367  // Test to see if we should create a method data oop
1368  Address profile_limit(method_counters, MethodCounters::interpreter_profile_limit_offset());
1369  ld(profile_limit, Rtmp);
1370  cmp(invocation_count, Rtmp);
1371  // Use long branches because call_VM() code and following code generated by
1372  // test_backedge_count_for_osr() is large in debug VM.
1373  br(Assembler::lessUnsigned, false, Assembler::pn, profile_continue);
1374  delayed()->nop();
1375
1376  // Build it now.
1377  call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::profile_method));
1378  set_method_data_pointer_for_bcp();
1379  ba(profile_continue);
1380  delayed()->nop();
1381  bind(done);
1382}
1383
1384// Store a value at some constant offset from the method data pointer.
1385
1386void InterpreterMacroAssembler::set_mdp_data_at(int constant, Register value) {
1387  assert(ProfileInterpreter, "must be profiling interpreter");
1388  st_ptr(value, ImethodDataPtr, constant);
1389}
1390
1391void InterpreterMacroAssembler::increment_mdp_data_at(Address counter,
1392                                                      Register bumped_count,
1393                                                      bool decrement) {
1394  assert(ProfileInterpreter, "must be profiling interpreter");
1395
1396  // Load the counter.
1397  ld_ptr(counter, bumped_count);
1398
1399  if (decrement) {
1400    // Decrement the register.  Set condition codes.
1401    subcc(bumped_count, DataLayout::counter_increment, bumped_count);
1402
1403    // If the decrement causes the counter to overflow, stay negative
1404    Label L;
1405    brx(Assembler::negative, true, Assembler::pn, L);
1406
1407    // Store the decremented counter, if it is still negative.
1408    delayed()->st_ptr(bumped_count, counter);
1409    bind(L);
1410  } else {
1411    // Increment the register.  Set carry flag.
1412    addcc(bumped_count, DataLayout::counter_increment, bumped_count);
1413
1414    // If the increment causes the counter to overflow, pull back by 1.
1415    assert(DataLayout::counter_increment == 1, "subc works");
1416    subc(bumped_count, G0, bumped_count);
1417
1418    // Store the incremented counter.
1419    st_ptr(bumped_count, counter);
1420  }
1421}
1422
1423// Increment the value at some constant offset from the method data pointer.
1424
1425void InterpreterMacroAssembler::increment_mdp_data_at(int constant,
1426                                                      Register bumped_count,
1427                                                      bool decrement) {
1428  // Locate the counter at a fixed offset from the mdp:
1429  Address counter(ImethodDataPtr, constant);
1430  increment_mdp_data_at(counter, bumped_count, decrement);
1431}
1432
1433// Increment the value at some non-fixed (reg + constant) offset from
1434// the method data pointer.
1435
1436void InterpreterMacroAssembler::increment_mdp_data_at(Register reg,
1437                                                      int constant,
1438                                                      Register bumped_count,
1439                                                      Register scratch2,
1440                                                      bool decrement) {
1441  // Add the constant to reg to get the offset.
1442  add(ImethodDataPtr, reg, scratch2);
1443  Address counter(scratch2, constant);
1444  increment_mdp_data_at(counter, bumped_count, decrement);
1445}
1446
1447// Set a flag value at the current method data pointer position.
1448// Updates a single byte of the header, to avoid races with other header bits.
1449
1450void InterpreterMacroAssembler::set_mdp_flag_at(int flag_constant,
1451                                                Register scratch) {
1452  assert(ProfileInterpreter, "must be profiling interpreter");
1453  // Load the data header
1454  ldub(ImethodDataPtr, in_bytes(DataLayout::flags_offset()), scratch);
1455
1456  // Set the flag
1457  or3(scratch, flag_constant, scratch);
1458
1459  // Store the modified header.
1460  stb(scratch, ImethodDataPtr, in_bytes(DataLayout::flags_offset()));
1461}
1462
1463// Test the location at some offset from the method data pointer.
1464// If it is not equal to value, branch to the not_equal_continue Label.
1465// Set condition codes to match the nullness of the loaded value.
1466
1467void InterpreterMacroAssembler::test_mdp_data_at(int offset,
1468                                                 Register value,
1469                                                 Label& not_equal_continue,
1470                                                 Register scratch) {
1471  assert(ProfileInterpreter, "must be profiling interpreter");
1472  ld_ptr(ImethodDataPtr, offset, scratch);
1473  cmp(value, scratch);
1474  brx(Assembler::notEqual, false, Assembler::pn, not_equal_continue);
1475  delayed()->tst(scratch);
1476}
1477
1478// Update the method data pointer by the displacement located at some fixed
1479// offset from the method data pointer.
1480
1481void InterpreterMacroAssembler::update_mdp_by_offset(int offset_of_disp,
1482                                                     Register scratch) {
1483  assert(ProfileInterpreter, "must be profiling interpreter");
1484  ld_ptr(ImethodDataPtr, offset_of_disp, scratch);
1485  add(ImethodDataPtr, scratch, ImethodDataPtr);
1486}
1487
1488// Update the method data pointer by the displacement located at the
1489// offset (reg + offset_of_disp).
1490
1491void InterpreterMacroAssembler::update_mdp_by_offset(Register reg,
1492                                                     int offset_of_disp,
1493                                                     Register scratch) {
1494  assert(ProfileInterpreter, "must be profiling interpreter");
1495  add(reg, offset_of_disp, scratch);
1496  ld_ptr(ImethodDataPtr, scratch, scratch);
1497  add(ImethodDataPtr, scratch, ImethodDataPtr);
1498}
1499
1500// Update the method data pointer by a simple constant displacement.
1501
1502void InterpreterMacroAssembler::update_mdp_by_constant(int constant) {
1503  assert(ProfileInterpreter, "must be profiling interpreter");
1504  add(ImethodDataPtr, constant, ImethodDataPtr);
1505}
1506
1507// Update the method data pointer for a _ret bytecode whose target
1508// was not among our cached targets.
1509
1510void InterpreterMacroAssembler::update_mdp_for_ret(TosState state,
1511                                                   Register return_bci) {
1512  assert(ProfileInterpreter, "must be profiling interpreter");
1513  push(state);
1514  st_ptr(return_bci, l_tmp);  // protect return_bci, in case it is volatile
1515  call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret), return_bci);
1516  ld_ptr(l_tmp, return_bci);
1517  pop(state);
1518}
1519
1520// Count a taken branch in the bytecodes.
1521
1522void InterpreterMacroAssembler::profile_taken_branch(Register scratch, Register bumped_count) {
1523  if (ProfileInterpreter) {
1524    Label profile_continue;
1525
1526    // If no method data exists, go to profile_continue.
1527    test_method_data_pointer(profile_continue);
1528
1529    // We are taking a branch.  Increment the taken count.
1530    increment_mdp_data_at(in_bytes(JumpData::taken_offset()), bumped_count);
1531
1532    // The method data pointer needs to be updated to reflect the new target.
1533    update_mdp_by_offset(in_bytes(JumpData::displacement_offset()), scratch);
1534    bind (profile_continue);
1535  }
1536}
1537
1538
1539// Count a not-taken branch in the bytecodes.
1540
1541void InterpreterMacroAssembler::profile_not_taken_branch(Register scratch) {
1542  if (ProfileInterpreter) {
1543    Label profile_continue;
1544
1545    // If no method data exists, go to profile_continue.
1546    test_method_data_pointer(profile_continue);
1547
1548    // We are taking a branch.  Increment the not taken count.
1549    increment_mdp_data_at(in_bytes(BranchData::not_taken_offset()), scratch);
1550
1551    // The method data pointer needs to be updated to correspond to the
1552    // next bytecode.
1553    update_mdp_by_constant(in_bytes(BranchData::branch_data_size()));
1554    bind (profile_continue);
1555  }
1556}
1557
1558
1559// Count a non-virtual call in the bytecodes.
1560
1561void InterpreterMacroAssembler::profile_call(Register scratch) {
1562  if (ProfileInterpreter) {
1563    Label profile_continue;
1564
1565    // If no method data exists, go to profile_continue.
1566    test_method_data_pointer(profile_continue);
1567
1568    // We are making a call.  Increment the count.
1569    increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch);
1570
1571    // The method data pointer needs to be updated to reflect the new target.
1572    update_mdp_by_constant(in_bytes(CounterData::counter_data_size()));
1573    bind (profile_continue);
1574  }
1575}
1576
1577
1578// Count a final call in the bytecodes.
1579
1580void InterpreterMacroAssembler::profile_final_call(Register scratch) {
1581  if (ProfileInterpreter) {
1582    Label profile_continue;
1583
1584    // If no method data exists, go to profile_continue.
1585    test_method_data_pointer(profile_continue);
1586
1587    // We are making a call.  Increment the count.
1588    increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch);
1589
1590    // The method data pointer needs to be updated to reflect the new target.
1591    update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1592    bind (profile_continue);
1593  }
1594}
1595
1596
1597// Count a virtual call in the bytecodes.
1598
1599void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
1600                                                     Register scratch,
1601                                                     bool receiver_can_be_null) {
1602  if (ProfileInterpreter) {
1603    Label profile_continue;
1604
1605    // If no method data exists, go to profile_continue.
1606    test_method_data_pointer(profile_continue);
1607
1608
1609    Label skip_receiver_profile;
1610    if (receiver_can_be_null) {
1611      Label not_null;
1612      br_notnull_short(receiver, Assembler::pt, not_null);
1613      // We are making a call.  Increment the count for null receiver.
1614      increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch);
1615      ba_short(skip_receiver_profile);
1616      bind(not_null);
1617    }
1618
1619    // Record the receiver type.
1620    record_klass_in_profile(receiver, scratch, true);
1621    bind(skip_receiver_profile);
1622
1623    // The method data pointer needs to be updated to reflect the new target.
1624#if INCLUDE_JVMCI
1625    if (MethodProfileWidth == 0) {
1626      update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1627    }
1628#else
1629    update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1630#endif
1631    bind(profile_continue);
1632  }
1633}
1634
1635#if INCLUDE_JVMCI
1636void InterpreterMacroAssembler::profile_called_method(Register method, Register scratch) {
1637  assert_different_registers(method, scratch);
1638  if (ProfileInterpreter && MethodProfileWidth > 0) {
1639    Label profile_continue;
1640
1641    // If no method data exists, go to profile_continue.
1642    test_method_data_pointer(profile_continue);
1643
1644    Label done;
1645    record_item_in_profile_helper(method, scratch, 0, done, MethodProfileWidth,
1646      &VirtualCallData::method_offset, &VirtualCallData::method_count_offset, in_bytes(VirtualCallData::nonprofiled_receiver_count_offset()));
1647    bind(done);
1648
1649    update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1650    bind(profile_continue);
1651  }
1652}
1653#endif // INCLUDE_JVMCI
1654
1655void InterpreterMacroAssembler::record_klass_in_profile_helper(Register receiver, Register scratch,
1656                                                               Label& done, bool is_virtual_call) {
1657  if (TypeProfileWidth == 0) {
1658    if (is_virtual_call) {
1659      increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch);
1660    }
1661#if INCLUDE_JVMCI
1662    else if (EnableJVMCI) {
1663      increment_mdp_data_at(in_bytes(ReceiverTypeData::nonprofiled_receiver_count_offset()), scratch);
1664    }
1665#endif
1666  } else {
1667    int non_profiled_offset = -1;
1668    if (is_virtual_call) {
1669      non_profiled_offset = in_bytes(CounterData::count_offset());
1670    }
1671#if INCLUDE_JVMCI
1672    else if (EnableJVMCI) {
1673      non_profiled_offset = in_bytes(ReceiverTypeData::nonprofiled_receiver_count_offset());
1674    }
1675#endif
1676
1677    record_item_in_profile_helper(receiver, scratch, 0, done, TypeProfileWidth,
1678      &VirtualCallData::receiver_offset, &VirtualCallData::receiver_count_offset, non_profiled_offset);
1679  }
1680}
1681
1682void InterpreterMacroAssembler::record_item_in_profile_helper(Register item,
1683                                          Register scratch, int start_row, Label& done, int total_rows,
1684                                          OffsetFunction item_offset_fn, OffsetFunction item_count_offset_fn,
1685                                          int non_profiled_offset) {
1686  int last_row = total_rows - 1;
1687  assert(start_row <= last_row, "must be work left to do");
1688  // Test this row for both the item and for null.
1689  // Take any of three different outcomes:
1690  //   1. found item => increment count and goto done
1691  //   2. found null => keep looking for case 1, maybe allocate this cell
1692  //   3. found something else => keep looking for cases 1 and 2
1693  // Case 3 is handled by a recursive call.
1694  for (int row = start_row; row <= last_row; row++) {
1695    Label next_test;
1696    bool test_for_null_also = (row == start_row);
1697
1698    // See if the item is item[n].
1699    int item_offset = in_bytes(item_offset_fn(row));
1700    test_mdp_data_at(item_offset, item, next_test, scratch);
1701    // delayed()->tst(scratch);
1702
1703    // The receiver is item[n].  Increment count[n].
1704    int count_offset = in_bytes(item_count_offset_fn(row));
1705    increment_mdp_data_at(count_offset, scratch);
1706    ba_short(done);
1707    bind(next_test);
1708
1709    if (test_for_null_also) {
1710      Label found_null;
1711      // Failed the equality check on item[n]...  Test for null.
1712      if (start_row == last_row) {
1713        // The only thing left to do is handle the null case.
1714        if (non_profiled_offset >= 0) {
1715          brx(Assembler::zero, false, Assembler::pn, found_null);
1716          delayed()->nop();
1717          // Item did not match any saved item and there is no empty row for it.
1718          // Increment total counter to indicate polymorphic case.
1719          increment_mdp_data_at(non_profiled_offset, scratch);
1720          ba_short(done);
1721          bind(found_null);
1722        } else {
1723          brx(Assembler::notZero, false, Assembler::pt, done);
1724          delayed()->nop();
1725        }
1726        break;
1727      }
1728      // Since null is rare, make it be the branch-taken case.
1729      brx(Assembler::zero, false, Assembler::pn, found_null);
1730      delayed()->nop();
1731
1732      // Put all the "Case 3" tests here.
1733      record_item_in_profile_helper(item, scratch, start_row + 1, done, total_rows,
1734        item_offset_fn, item_count_offset_fn, non_profiled_offset);
1735
1736      // Found a null.  Keep searching for a matching item,
1737      // but remember that this is an empty (unused) slot.
1738      bind(found_null);
1739    }
1740  }
1741
1742  // In the fall-through case, we found no matching item, but we
1743  // observed the item[start_row] is NULL.
1744
1745  // Fill in the item field and increment the count.
1746  int item_offset = in_bytes(item_offset_fn(start_row));
1747  set_mdp_data_at(item_offset, item);
1748  int count_offset = in_bytes(item_count_offset_fn(start_row));
1749  mov(DataLayout::counter_increment, scratch);
1750  set_mdp_data_at(count_offset, scratch);
1751  if (start_row > 0) {
1752    ba_short(done);
1753  }
1754}
1755
1756void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1757                                                        Register scratch, bool is_virtual_call) {
1758  assert(ProfileInterpreter, "must be profiling");
1759  Label done;
1760
1761  record_klass_in_profile_helper(receiver, scratch, done, is_virtual_call);
1762
1763  bind (done);
1764}
1765
1766
1767// Count a ret in the bytecodes.
1768
1769void InterpreterMacroAssembler::profile_ret(TosState state,
1770                                            Register return_bci,
1771                                            Register scratch) {
1772  if (ProfileInterpreter) {
1773    Label profile_continue;
1774    uint row;
1775
1776    // If no method data exists, go to profile_continue.
1777    test_method_data_pointer(profile_continue);
1778
1779    // Update the total ret count.
1780    increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch);
1781
1782    for (row = 0; row < RetData::row_limit(); row++) {
1783      Label next_test;
1784
1785      // See if return_bci is equal to bci[n]:
1786      test_mdp_data_at(in_bytes(RetData::bci_offset(row)),
1787                       return_bci, next_test, scratch);
1788
1789      // return_bci is equal to bci[n].  Increment the count.
1790      increment_mdp_data_at(in_bytes(RetData::bci_count_offset(row)), scratch);
1791
1792      // The method data pointer needs to be updated to reflect the new target.
1793      update_mdp_by_offset(in_bytes(RetData::bci_displacement_offset(row)), scratch);
1794      ba_short(profile_continue);
1795      bind(next_test);
1796    }
1797
1798    update_mdp_for_ret(state, return_bci);
1799
1800    bind (profile_continue);
1801  }
1802}
1803
1804// Profile an unexpected null in the bytecodes.
1805void InterpreterMacroAssembler::profile_null_seen(Register scratch) {
1806  if (ProfileInterpreter) {
1807    Label profile_continue;
1808
1809    // If no method data exists, go to profile_continue.
1810    test_method_data_pointer(profile_continue);
1811
1812    set_mdp_flag_at(BitData::null_seen_byte_constant(), scratch);
1813
1814    // The method data pointer needs to be updated.
1815    int mdp_delta = in_bytes(BitData::bit_data_size());
1816    if (TypeProfileCasts) {
1817      mdp_delta = in_bytes(ReceiverTypeData::receiver_type_data_size());
1818    }
1819    update_mdp_by_constant(mdp_delta);
1820
1821    bind (profile_continue);
1822  }
1823}
1824
1825void InterpreterMacroAssembler::profile_typecheck(Register klass,
1826                                                  Register scratch) {
1827  if (ProfileInterpreter) {
1828    Label profile_continue;
1829
1830    // If no method data exists, go to profile_continue.
1831    test_method_data_pointer(profile_continue);
1832
1833    int mdp_delta = in_bytes(BitData::bit_data_size());
1834    if (TypeProfileCasts) {
1835      mdp_delta = in_bytes(ReceiverTypeData::receiver_type_data_size());
1836
1837      // Record the object type.
1838      record_klass_in_profile(klass, scratch, false);
1839    }
1840
1841    // The method data pointer needs to be updated.
1842    update_mdp_by_constant(mdp_delta);
1843
1844    bind (profile_continue);
1845  }
1846}
1847
1848void InterpreterMacroAssembler::profile_typecheck_failed(Register scratch) {
1849  if (ProfileInterpreter && TypeProfileCasts) {
1850    Label profile_continue;
1851
1852    // If no method data exists, go to profile_continue.
1853    test_method_data_pointer(profile_continue);
1854
1855    int count_offset = in_bytes(CounterData::count_offset());
1856    // Back up the address, since we have already bumped the mdp.
1857    count_offset -= in_bytes(ReceiverTypeData::receiver_type_data_size());
1858
1859    // *Decrement* the counter.  We expect to see zero or small negatives.
1860    increment_mdp_data_at(count_offset, scratch, true);
1861
1862    bind (profile_continue);
1863  }
1864}
1865
1866// Count the default case of a switch construct.
1867
1868void InterpreterMacroAssembler::profile_switch_default(Register scratch) {
1869  if (ProfileInterpreter) {
1870    Label profile_continue;
1871
1872    // If no method data exists, go to profile_continue.
1873    test_method_data_pointer(profile_continue);
1874
1875    // Update the default case count
1876    increment_mdp_data_at(in_bytes(MultiBranchData::default_count_offset()),
1877                          scratch);
1878
1879    // The method data pointer needs to be updated.
1880    update_mdp_by_offset(
1881                    in_bytes(MultiBranchData::default_displacement_offset()),
1882                    scratch);
1883
1884    bind (profile_continue);
1885  }
1886}
1887
1888// Count the index'th case of a switch construct.
1889
1890void InterpreterMacroAssembler::profile_switch_case(Register index,
1891                                                    Register scratch,
1892                                                    Register scratch2,
1893                                                    Register scratch3) {
1894  if (ProfileInterpreter) {
1895    Label profile_continue;
1896
1897    // If no method data exists, go to profile_continue.
1898    test_method_data_pointer(profile_continue);
1899
1900    // Build the base (index * per_case_size_in_bytes()) + case_array_offset_in_bytes()
1901    set(in_bytes(MultiBranchData::per_case_size()), scratch);
1902    smul(index, scratch, scratch);
1903    add(scratch, in_bytes(MultiBranchData::case_array_offset()), scratch);
1904
1905    // Update the case count
1906    increment_mdp_data_at(scratch,
1907                          in_bytes(MultiBranchData::relative_count_offset()),
1908                          scratch2,
1909                          scratch3);
1910
1911    // The method data pointer needs to be updated.
1912    update_mdp_by_offset(scratch,
1913                     in_bytes(MultiBranchData::relative_displacement_offset()),
1914                     scratch2);
1915
1916    bind (profile_continue);
1917  }
1918}
1919
1920void InterpreterMacroAssembler::profile_obj_type(Register obj, const Address& mdo_addr, Register tmp) {
1921  Label not_null, do_nothing, do_update;
1922
1923  assert_different_registers(obj, mdo_addr.base(), tmp);
1924
1925  verify_oop(obj);
1926
1927  ld_ptr(mdo_addr, tmp);
1928
1929  br_notnull_short(obj, pt, not_null);
1930  or3(tmp, TypeEntries::null_seen, tmp);
1931  ba_short(do_update);
1932
1933  bind(not_null);
1934  load_klass(obj, obj);
1935
1936  xor3(obj, tmp, obj);
1937  btst(TypeEntries::type_klass_mask, obj);
1938  // klass seen before, nothing to do. The unknown bit may have been
1939  // set already but no need to check.
1940  brx(zero, false, pt, do_nothing);
1941  delayed()->
1942
1943  btst(TypeEntries::type_unknown, obj);
1944  // already unknown. Nothing to do anymore.
1945  brx(notZero, false, pt, do_nothing);
1946  delayed()->
1947
1948  btst(TypeEntries::type_mask, tmp);
1949  brx(zero, true, pt, do_update);
1950  // first time here. Set profile type.
1951  delayed()->or3(tmp, obj, tmp);
1952
1953  // different than before. Cannot keep accurate profile.
1954  or3(tmp, TypeEntries::type_unknown, tmp);
1955
1956  bind(do_update);
1957  // update profile
1958  st_ptr(tmp, mdo_addr);
1959
1960  bind(do_nothing);
1961}
1962
1963void InterpreterMacroAssembler::profile_arguments_type(Register callee, Register tmp1, Register tmp2, bool is_virtual) {
1964  if (!ProfileInterpreter) {
1965    return;
1966  }
1967
1968  assert_different_registers(callee, tmp1, tmp2, ImethodDataPtr);
1969
1970  if (MethodData::profile_arguments() || MethodData::profile_return()) {
1971    Label profile_continue;
1972
1973    test_method_data_pointer(profile_continue);
1974
1975    int off_to_start = is_virtual ? in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
1976
1977    ldub(ImethodDataPtr, in_bytes(DataLayout::tag_offset()) - off_to_start, tmp1);
1978    cmp_and_br_short(tmp1, is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag, notEqual, pn, profile_continue);
1979
1980    if (MethodData::profile_arguments()) {
1981      Label done;
1982      int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset());
1983      add(ImethodDataPtr, off_to_args, ImethodDataPtr);
1984
1985      for (int i = 0; i < TypeProfileArgsLimit; i++) {
1986        if (i > 0 || MethodData::profile_return()) {
1987          // If return value type is profiled we may have no argument to profile
1988          ld_ptr(ImethodDataPtr, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args, tmp1);
1989          sub(tmp1, i*TypeStackSlotEntries::per_arg_count(), tmp1);
1990          cmp_and_br_short(tmp1, TypeStackSlotEntries::per_arg_count(), less, pn, done);
1991        }
1992        ld_ptr(Address(callee, Method::const_offset()), tmp1);
1993        lduh(Address(tmp1, ConstMethod::size_of_parameters_offset()), tmp1);
1994        // stack offset o (zero based) from the start of the argument
1995        // list, for n arguments translates into offset n - o - 1 from
1996        // the end of the argument list. But there's an extra slot at
1997        // the stop of the stack. So the offset is n - o from Lesp.
1998        ld_ptr(ImethodDataPtr, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))-off_to_args, tmp2);
1999        sub(tmp1, tmp2, tmp1);
2000
2001        // Can't use MacroAssembler::argument_address() which needs Gargs to be set up
2002        sll(tmp1, Interpreter::logStackElementSize, tmp1);
2003        ld_ptr(Lesp, tmp1, tmp1);
2004
2005        Address mdo_arg_addr(ImethodDataPtr, in_bytes(TypeEntriesAtCall::argument_type_offset(i))-off_to_args);
2006        profile_obj_type(tmp1, mdo_arg_addr, tmp2);
2007
2008        int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
2009        add(ImethodDataPtr, to_add, ImethodDataPtr);
2010        off_to_args += to_add;
2011      }
2012
2013      if (MethodData::profile_return()) {
2014        ld_ptr(ImethodDataPtr, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args, tmp1);
2015        sub(tmp1, TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count(), tmp1);
2016      }
2017
2018      bind(done);
2019
2020      if (MethodData::profile_return()) {
2021        // We're right after the type profile for the last
2022        // argument. tmp1 is the number of cells left in the
2023        // CallTypeData/VirtualCallTypeData to reach its end. Non null
2024        // if there's a return to profile.
2025        assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(), "can't move past ret type");
2026        sll(tmp1, exact_log2(DataLayout::cell_size), tmp1);
2027        add(ImethodDataPtr, tmp1, ImethodDataPtr);
2028      }
2029    } else {
2030      assert(MethodData::profile_return(), "either profile call args or call ret");
2031      update_mdp_by_constant(in_bytes(TypeEntriesAtCall::return_only_size()));
2032    }
2033
2034    // mdp points right after the end of the
2035    // CallTypeData/VirtualCallTypeData, right after the cells for the
2036    // return value type if there's one.
2037
2038    bind(profile_continue);
2039  }
2040}
2041
2042void InterpreterMacroAssembler::profile_return_type(Register ret, Register tmp1, Register tmp2) {
2043  assert_different_registers(ret, tmp1, tmp2);
2044  if (ProfileInterpreter && MethodData::profile_return()) {
2045    Label profile_continue, done;
2046
2047    test_method_data_pointer(profile_continue);
2048
2049    if (MethodData::profile_return_jsr292_only()) {
2050      assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
2051
2052      // If we don't profile all invoke bytecodes we must make sure
2053      // it's a bytecode we indeed profile. We can't go back to the
2054      // begining of the ProfileData we intend to update to check its
2055      // type because we're right after it and we don't known its
2056      // length.
2057      Label do_profile;
2058      ldub(Lbcp, 0, tmp1);
2059      cmp_and_br_short(tmp1, Bytecodes::_invokedynamic, equal, pn, do_profile);
2060      cmp(tmp1, Bytecodes::_invokehandle);
2061      br(equal, false, pn, do_profile);
2062      delayed()->lduh(Lmethod, Method::intrinsic_id_offset_in_bytes(), tmp1);
2063      cmp_and_br_short(tmp1, vmIntrinsics::_compiledLambdaForm, notEqual, pt, profile_continue);
2064
2065      bind(do_profile);
2066    }
2067
2068    Address mdo_ret_addr(ImethodDataPtr, -in_bytes(ReturnTypeEntry::size()));
2069    mov(ret, tmp1);
2070    profile_obj_type(tmp1, mdo_ret_addr, tmp2);
2071
2072    bind(profile_continue);
2073  }
2074}
2075
2076void InterpreterMacroAssembler::profile_parameters_type(Register tmp1, Register tmp2, Register tmp3, Register tmp4) {
2077  if (ProfileInterpreter && MethodData::profile_parameters()) {
2078    Label profile_continue, done;
2079
2080    test_method_data_pointer(profile_continue);
2081
2082    // Load the offset of the area within the MDO used for
2083    // parameters. If it's negative we're not profiling any parameters.
2084    lduw(ImethodDataPtr, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset()), tmp1);
2085    cmp_and_br_short(tmp1, 0, less, pn, profile_continue);
2086
2087    // Compute a pointer to the area for parameters from the offset
2088    // and move the pointer to the slot for the last
2089    // parameters. Collect profiling from last parameter down.
2090    // mdo start + parameters offset + array length - 1
2091
2092    // Pointer to the parameter area in the MDO
2093    Register mdp = tmp1;
2094    add(ImethodDataPtr, tmp1, mdp);
2095
2096    // offset of the current profile entry to update
2097    Register entry_offset = tmp2;
2098    // entry_offset = array len in number of cells
2099    ld_ptr(mdp, ArrayData::array_len_offset(), entry_offset);
2100
2101    int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
2102    assert(off_base % DataLayout::cell_size == 0, "should be a number of cells");
2103
2104    // entry_offset (number of cells)  = array len - size of 1 entry + offset of the stack slot field
2105    sub(entry_offset, TypeStackSlotEntries::per_arg_count() - (off_base / DataLayout::cell_size), entry_offset);
2106    // entry_offset in bytes
2107    sll(entry_offset, exact_log2(DataLayout::cell_size), entry_offset);
2108
2109    Label loop;
2110    bind(loop);
2111
2112    // load offset on the stack from the slot for this parameter
2113    ld_ptr(mdp, entry_offset, tmp3);
2114    sll(tmp3,Interpreter::logStackElementSize, tmp3);
2115    neg(tmp3);
2116    // read the parameter from the local area
2117    ld_ptr(Llocals, tmp3, tmp3);
2118
2119    // make entry_offset now point to the type field for this parameter
2120    int type_base = in_bytes(ParametersTypeData::type_offset(0));
2121    assert(type_base > off_base, "unexpected");
2122    add(entry_offset, type_base - off_base, entry_offset);
2123
2124    // profile the parameter
2125    Address arg_type(mdp, entry_offset);
2126    profile_obj_type(tmp3, arg_type, tmp4);
2127
2128    // go to next parameter
2129    sub(entry_offset, TypeStackSlotEntries::per_arg_count() * DataLayout::cell_size + (type_base - off_base), entry_offset);
2130    cmp_and_br_short(entry_offset, off_base, greaterEqual, pt, loop);
2131
2132    bind(profile_continue);
2133  }
2134}
2135
2136// add a InterpMonitorElem to stack (see frame_sparc.hpp)
2137
2138void InterpreterMacroAssembler::add_monitor_to_stack( bool stack_is_empty,
2139                                                      Register Rtemp,
2140                                                      Register Rtemp2 ) {
2141
2142  Register Rlimit = Lmonitors;
2143  const jint delta = frame::interpreter_frame_monitor_size() * wordSize;
2144  assert( (delta & LongAlignmentMask) == 0,
2145          "sizeof BasicObjectLock must be even number of doublewords");
2146
2147  sub( SP,        delta, SP);
2148  sub( Lesp,      delta, Lesp);
2149  sub( Lmonitors, delta, Lmonitors);
2150
2151  if (!stack_is_empty) {
2152
2153    // must copy stack contents down
2154
2155    Label start_copying, next;
2156
2157    // untested("monitor stack expansion");
2158    compute_stack_base(Rtemp);
2159    ba(start_copying);
2160    delayed()->cmp(Rtemp, Rlimit); // done? duplicated below
2161
2162    // note: must copy from low memory upwards
2163    // On entry to loop,
2164    // Rtemp points to new base of stack, Lesp points to new end of stack (1 past TOS)
2165    // Loop mutates Rtemp
2166
2167    bind( next);
2168
2169    st_ptr(Rtemp2, Rtemp, 0);
2170    inc(Rtemp, wordSize);
2171    cmp(Rtemp, Rlimit); // are we done? (duplicated above)
2172
2173    bind( start_copying );
2174
2175    brx( notEqual, true, pn, next );
2176    delayed()->ld_ptr( Rtemp, delta, Rtemp2 );
2177
2178    // done copying stack
2179  }
2180}
2181
2182// Locals
2183void InterpreterMacroAssembler::access_local_ptr( Register index, Register dst ) {
2184  assert_not_delayed();
2185  sll(index, Interpreter::logStackElementSize, index);
2186  sub(Llocals, index, index);
2187  ld_ptr(index, 0, dst);
2188  // Note:  index must hold the effective address--the iinc template uses it
2189}
2190
2191// Just like access_local_ptr but the tag is a returnAddress
2192void InterpreterMacroAssembler::access_local_returnAddress(Register index,
2193                                                           Register dst ) {
2194  assert_not_delayed();
2195  sll(index, Interpreter::logStackElementSize, index);
2196  sub(Llocals, index, index);
2197  ld_ptr(index, 0, dst);
2198}
2199
2200void InterpreterMacroAssembler::access_local_int( Register index, Register dst ) {
2201  assert_not_delayed();
2202  sll(index, Interpreter::logStackElementSize, index);
2203  sub(Llocals, index, index);
2204  ld(index, 0, dst);
2205  // Note:  index must hold the effective address--the iinc template uses it
2206}
2207
2208
2209void InterpreterMacroAssembler::access_local_long( Register index, Register dst ) {
2210  assert_not_delayed();
2211  sll(index, Interpreter::logStackElementSize, index);
2212  sub(Llocals, index, index);
2213  // First half stored at index n+1 (which grows down from Llocals[n])
2214  load_unaligned_long(index, Interpreter::local_offset_in_bytes(1), dst);
2215}
2216
2217
2218void InterpreterMacroAssembler::access_local_float( Register index, FloatRegister dst ) {
2219  assert_not_delayed();
2220  sll(index, Interpreter::logStackElementSize, index);
2221  sub(Llocals, index, index);
2222  ldf(FloatRegisterImpl::S, index, 0, dst);
2223}
2224
2225
2226void InterpreterMacroAssembler::access_local_double( Register index, FloatRegister dst ) {
2227  assert_not_delayed();
2228  sll(index, Interpreter::logStackElementSize, index);
2229  sub(Llocals, index, index);
2230  load_unaligned_double(index, Interpreter::local_offset_in_bytes(1), dst);
2231}
2232
2233
2234#ifdef ASSERT
2235void InterpreterMacroAssembler::check_for_regarea_stomp(Register Rindex, int offset, Register Rlimit, Register Rscratch, Register Rscratch1) {
2236  Label L;
2237
2238  assert(Rindex != Rscratch, "Registers cannot be same");
2239  assert(Rindex != Rscratch1, "Registers cannot be same");
2240  assert(Rlimit != Rscratch, "Registers cannot be same");
2241  assert(Rlimit != Rscratch1, "Registers cannot be same");
2242  assert(Rscratch1 != Rscratch, "Registers cannot be same");
2243
2244  // untested("reg area corruption");
2245  add(Rindex, offset, Rscratch);
2246  add(Rlimit, 64 + STACK_BIAS, Rscratch1);
2247  cmp_and_brx_short(Rscratch, Rscratch1, Assembler::greaterEqualUnsigned, pn, L);
2248  stop("regsave area is being clobbered");
2249  bind(L);
2250}
2251#endif // ASSERT
2252
2253
2254void InterpreterMacroAssembler::store_local_int( Register index, Register src ) {
2255  assert_not_delayed();
2256  sll(index, Interpreter::logStackElementSize, index);
2257  sub(Llocals, index, index);
2258  debug_only(check_for_regarea_stomp(index, 0, FP, G1_scratch, G4_scratch);)
2259  st(src, index, 0);
2260}
2261
2262void InterpreterMacroAssembler::store_local_ptr( Register index, Register src ) {
2263  assert_not_delayed();
2264  sll(index, Interpreter::logStackElementSize, index);
2265  sub(Llocals, index, index);
2266#ifdef ASSERT
2267  check_for_regarea_stomp(index, 0, FP, G1_scratch, G4_scratch);
2268#endif
2269  st_ptr(src, index, 0);
2270}
2271
2272
2273
2274void InterpreterMacroAssembler::store_local_ptr( int n, Register src ) {
2275  st_ptr(src, Llocals, Interpreter::local_offset_in_bytes(n));
2276}
2277
2278void InterpreterMacroAssembler::store_local_long( Register index, Register src ) {
2279  assert_not_delayed();
2280  sll(index, Interpreter::logStackElementSize, index);
2281  sub(Llocals, index, index);
2282#ifdef ASSERT
2283  check_for_regarea_stomp(index, Interpreter::local_offset_in_bytes(1), FP, G1_scratch, G4_scratch);
2284#endif
2285  store_unaligned_long(src, index, Interpreter::local_offset_in_bytes(1)); // which is n+1
2286}
2287
2288
2289void InterpreterMacroAssembler::store_local_float( Register index, FloatRegister src ) {
2290  assert_not_delayed();
2291  sll(index, Interpreter::logStackElementSize, index);
2292  sub(Llocals, index, index);
2293#ifdef ASSERT
2294  check_for_regarea_stomp(index, 0, FP, G1_scratch, G4_scratch);
2295#endif
2296  stf(FloatRegisterImpl::S, src, index, 0);
2297}
2298
2299
2300void InterpreterMacroAssembler::store_local_double( Register index, FloatRegister src ) {
2301  assert_not_delayed();
2302  sll(index, Interpreter::logStackElementSize, index);
2303  sub(Llocals, index, index);
2304#ifdef ASSERT
2305  check_for_regarea_stomp(index, Interpreter::local_offset_in_bytes(1), FP, G1_scratch, G4_scratch);
2306#endif
2307  store_unaligned_double(src, index, Interpreter::local_offset_in_bytes(1));
2308}
2309
2310
2311int InterpreterMacroAssembler::top_most_monitor_byte_offset() {
2312  const jint delta = frame::interpreter_frame_monitor_size() * wordSize;
2313  int rounded_vm_local_words = align_up((int)frame::interpreter_frame_vm_local_words, WordsPerLong);
2314  return ((-rounded_vm_local_words * wordSize) - delta ) + STACK_BIAS;
2315}
2316
2317
2318Address InterpreterMacroAssembler::top_most_monitor() {
2319  return Address(FP, top_most_monitor_byte_offset());
2320}
2321
2322
2323void InterpreterMacroAssembler::compute_stack_base( Register Rdest ) {
2324  add( Lesp,      wordSize,                                    Rdest );
2325}
2326
2327void InterpreterMacroAssembler::get_method_counters(Register method,
2328                                                    Register Rcounters,
2329                                                    Label& skip) {
2330  Label has_counters;
2331  Address method_counters(method, in_bytes(Method::method_counters_offset()));
2332  ld_ptr(method_counters, Rcounters);
2333  br_notnull_short(Rcounters, Assembler::pt, has_counters);
2334  call_VM(noreg, CAST_FROM_FN_PTR(address,
2335          InterpreterRuntime::build_method_counters), method);
2336  ld_ptr(method_counters, Rcounters);
2337  br_null(Rcounters, false, Assembler::pn, skip); // No MethodCounters, OutOfMemory
2338  delayed()->nop();
2339  bind(has_counters);
2340}
2341
2342void InterpreterMacroAssembler::increment_invocation_counter( Register Rcounters, Register Rtmp, Register Rtmp2 ) {
2343  assert(UseCompiler || LogTouchedMethods, "incrementing must be useful");
2344  assert_different_registers(Rcounters, Rtmp, Rtmp2);
2345
2346  Address inv_counter(Rcounters, MethodCounters::invocation_counter_offset() +
2347                                 InvocationCounter::counter_offset());
2348  Address be_counter (Rcounters, MethodCounters::backedge_counter_offset() +
2349                                 InvocationCounter::counter_offset());
2350  int delta = InvocationCounter::count_increment;
2351
2352  // Load each counter in a register
2353  ld( inv_counter, Rtmp );
2354  ld( be_counter, Rtmp2 );
2355
2356  assert( is_simm13( delta ), " delta too large.");
2357
2358  // Add the delta to the invocation counter and store the result
2359  add( Rtmp, delta, Rtmp );
2360
2361  // Mask the backedge counter
2362  and3( Rtmp2, InvocationCounter::count_mask_value, Rtmp2 );
2363
2364  // Store value
2365  st( Rtmp, inv_counter);
2366
2367  // Add invocation counter + backedge counter
2368  add( Rtmp, Rtmp2, Rtmp);
2369
2370  // Note that this macro must leave the backedge_count + invocation_count in Rtmp!
2371}
2372
2373void InterpreterMacroAssembler::increment_backedge_counter( Register Rcounters, Register Rtmp, Register Rtmp2 ) {
2374  assert(UseCompiler, "incrementing must be useful");
2375  assert_different_registers(Rcounters, Rtmp, Rtmp2);
2376
2377  Address be_counter (Rcounters, MethodCounters::backedge_counter_offset() +
2378                                 InvocationCounter::counter_offset());
2379  Address inv_counter(Rcounters, MethodCounters::invocation_counter_offset() +
2380                                 InvocationCounter::counter_offset());
2381
2382  int delta = InvocationCounter::count_increment;
2383  // Load each counter in a register
2384  ld( be_counter, Rtmp );
2385  ld( inv_counter, Rtmp2 );
2386
2387  // Add the delta to the backedge counter
2388  add( Rtmp, delta, Rtmp );
2389
2390  // Mask the invocation counter, add to backedge counter
2391  and3( Rtmp2, InvocationCounter::count_mask_value, Rtmp2 );
2392
2393  // and store the result to memory
2394  st( Rtmp, be_counter );
2395
2396  // Add backedge + invocation counter
2397  add( Rtmp, Rtmp2, Rtmp );
2398
2399  // Note that this macro must leave backedge_count + invocation_count in Rtmp!
2400}
2401
2402void InterpreterMacroAssembler::test_backedge_count_for_osr( Register backedge_count,
2403                                                             Register method_counters,
2404                                                             Register branch_bcp,
2405                                                             Register Rtmp ) {
2406  Label did_not_overflow;
2407  Label overflow_with_error;
2408  assert_different_registers(backedge_count, Rtmp, branch_bcp);
2409  assert(UseOnStackReplacement,"Must UseOnStackReplacement to test_backedge_count_for_osr");
2410
2411  Address limit(method_counters, in_bytes(MethodCounters::interpreter_backward_branch_limit_offset()));
2412  ld(limit, Rtmp);
2413  cmp_and_br_short(backedge_count, Rtmp, Assembler::lessUnsigned, Assembler::pt, did_not_overflow);
2414
2415  // When ProfileInterpreter is on, the backedge_count comes from the
2416  // MethodData*, which value does not get reset on the call to
2417  // frequency_counter_overflow().  To avoid excessive calls to the overflow
2418  // routine while the method is being compiled, add a second test to make sure
2419  // the overflow function is called only once every overflow_frequency.
2420  if (ProfileInterpreter) {
2421    const int overflow_frequency = 1024;
2422    andcc(backedge_count, overflow_frequency-1, Rtmp);
2423    brx(Assembler::notZero, false, Assembler::pt, did_not_overflow);
2424    delayed()->nop();
2425  }
2426
2427  // overflow in loop, pass branch bytecode
2428  set(6,Rtmp);
2429  call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), branch_bcp, Rtmp);
2430
2431  // Was an OSR adapter generated?
2432  // O0 = osr nmethod
2433  br_null_short(O0, Assembler::pn, overflow_with_error);
2434
2435  // Has the nmethod been invalidated already?
2436  ldub(O0, nmethod::state_offset(), O2);
2437  cmp_and_br_short(O2, nmethod::in_use, Assembler::notEqual, Assembler::pn, overflow_with_error);
2438
2439  // migrate the interpreter frame off of the stack
2440
2441  mov(G2_thread, L7);
2442  // save nmethod
2443  mov(O0, L6);
2444  set_last_Java_frame(SP, noreg);
2445  call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin), L7);
2446  reset_last_Java_frame();
2447  mov(L7, G2_thread);
2448
2449  // move OSR nmethod to I1
2450  mov(L6, I1);
2451
2452  // OSR buffer to I0
2453  mov(O0, I0);
2454
2455  // remove the interpreter frame
2456  restore(I5_savedSP, 0, SP);
2457
2458  // Jump to the osr code.
2459  ld_ptr(O1, nmethod::osr_entry_point_offset(), O2);
2460  jmp(O2, G0);
2461  delayed()->nop();
2462
2463  bind(overflow_with_error);
2464
2465  bind(did_not_overflow);
2466}
2467
2468
2469
2470void InterpreterMacroAssembler::interp_verify_oop(Register reg, TosState state, const char * file, int line) {
2471  if (state == atos) { MacroAssembler::_verify_oop(reg, "broken oop ", file, line); }
2472}
2473
2474
2475// local helper function for the verify_oop_or_return_address macro
2476static bool verify_return_address(Method* m, int bci) {
2477#ifndef PRODUCT
2478  address pc = (address)(m->constMethod())
2479             + in_bytes(ConstMethod::codes_offset()) + bci;
2480  // assume it is a valid return address if it is inside m and is preceded by a jsr
2481  if (!m->contains(pc))                                          return false;
2482  address jsr_pc;
2483  jsr_pc = pc - Bytecodes::length_for(Bytecodes::_jsr);
2484  if (*jsr_pc == Bytecodes::_jsr   && jsr_pc >= m->code_base())    return true;
2485  jsr_pc = pc - Bytecodes::length_for(Bytecodes::_jsr_w);
2486  if (*jsr_pc == Bytecodes::_jsr_w && jsr_pc >= m->code_base())    return true;
2487#endif // PRODUCT
2488  return false;
2489}
2490
2491
2492void InterpreterMacroAssembler::verify_oop_or_return_address(Register reg, Register Rtmp) {
2493  if (!VerifyOops)  return;
2494  // the VM documentation for the astore[_wide] bytecode allows
2495  // the TOS to be not only an oop but also a return address
2496  Label test;
2497  Label skip;
2498  // See if it is an address (in the current method):
2499
2500  mov(reg, Rtmp);
2501  const int log2_bytecode_size_limit = 16;
2502  srl(Rtmp, log2_bytecode_size_limit, Rtmp);
2503  br_notnull_short( Rtmp, pt, test );
2504
2505  // %%% should use call_VM_leaf here?
2506  save_frame_and_mov(0, Lmethod, O0, reg, O1);
2507  save_thread(L7_thread_cache);
2508  call(CAST_FROM_FN_PTR(address,verify_return_address), relocInfo::none);
2509  delayed()->nop();
2510  restore_thread(L7_thread_cache);
2511  br_notnull( O0, false, pt, skip );
2512  delayed()->restore();
2513
2514  // Perform a more elaborate out-of-line call
2515  // Not an address; verify it:
2516  bind(test);
2517  verify_oop(reg);
2518  bind(skip);
2519}
2520
2521
2522void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) {
2523  if (state == ftos || state == dtos) MacroAssembler::verify_FPU(stack_depth);
2524}
2525
2526
2527// Jump if ((*counter_addr += increment) & mask) satisfies the condition.
2528void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr,
2529                                                        int increment, Address mask_addr,
2530                                                        Register scratch1, Register scratch2,
2531                                                        Condition cond, Label *where) {
2532  ld(counter_addr, scratch1);
2533  add(scratch1, increment, scratch1);
2534  ld(mask_addr, scratch2);
2535  andcc(scratch1, scratch2,  G0);
2536  br(cond, false, Assembler::pn, *where);
2537  delayed()->st(scratch1, counter_addr);
2538}
2539
2540// Inline assembly for:
2541//
2542// if (thread is in interp_only_mode) {
2543//   InterpreterRuntime::post_method_entry();
2544// }
2545// if (DTraceMethodProbes) {
2546//   SharedRuntime::dtrace_method_entry(method, receiver);
2547// }
2548// if (RC_TRACE_IN_RANGE(0x00001000, 0x00002000)) {
2549//   SharedRuntime::rc_trace_method_entry(method, receiver);
2550// }
2551
2552void InterpreterMacroAssembler::notify_method_entry() {
2553
2554  // Whenever JVMTI puts a thread in interp_only_mode, method
2555  // entry/exit events are sent for that thread to track stack
2556  // depth.  If it is possible to enter interp_only_mode we add
2557  // the code to check if the event should be sent.
2558  if (JvmtiExport::can_post_interpreter_events()) {
2559    Label L;
2560    Register temp_reg = O5;
2561    const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
2562    ld(interp_only, temp_reg);
2563    cmp_and_br_short(temp_reg, 0, equal, pt, L);
2564    call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_entry));
2565    bind(L);
2566  }
2567
2568  {
2569    Register temp_reg = O5;
2570    SkipIfEqual skip_if(this, temp_reg, &DTraceMethodProbes, zero);
2571    call_VM_leaf(noreg,
2572      CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
2573      G2_thread, Lmethod);
2574  }
2575
2576  // RedefineClasses() tracing support for obsolete method entry
2577  if (log_is_enabled(Trace, redefine, class, obsolete)) {
2578    call_VM_leaf(noreg,
2579      CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
2580      G2_thread, Lmethod);
2581  }
2582}
2583
2584
2585// Inline assembly for:
2586//
2587// if (thread is in interp_only_mode) {
2588//   // save result
2589//   InterpreterRuntime::post_method_exit();
2590//   // restore result
2591// }
2592// if (DTraceMethodProbes) {
2593//   SharedRuntime::dtrace_method_exit(thread, method);
2594// }
2595//
2596// Native methods have their result stored in d_tmp and l_tmp
2597// Java methods have their result stored in the expression stack
2598
2599void InterpreterMacroAssembler::notify_method_exit(bool is_native_method,
2600                                                   TosState state,
2601                                                   NotifyMethodExitMode mode) {
2602
2603  // Whenever JVMTI puts a thread in interp_only_mode, method
2604  // entry/exit events are sent for that thread to track stack
2605  // depth.  If it is possible to enter interp_only_mode we add
2606  // the code to check if the event should be sent.
2607  if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
2608    Label L;
2609    Register temp_reg = O5;
2610    const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
2611    ld(interp_only, temp_reg);
2612    cmp_and_br_short(temp_reg, 0, equal, pt, L);
2613
2614    // Note: frame::interpreter_frame_result has a dependency on how the
2615    // method result is saved across the call to post_method_exit. For
2616    // native methods it assumes the result registers are saved to
2617    // l_scratch and d_scratch. If this changes then the interpreter_frame_result
2618    // implementation will need to be updated too.
2619
2620    save_return_value(state, is_native_method);
2621    call_VM(noreg,
2622            CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
2623    restore_return_value(state, is_native_method);
2624    bind(L);
2625  }
2626
2627  {
2628    Register temp_reg = O5;
2629    // Dtrace notification
2630    SkipIfEqual skip_if(this, temp_reg, &DTraceMethodProbes, zero);
2631    save_return_value(state, is_native_method);
2632    call_VM_leaf(
2633      noreg,
2634      CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
2635      G2_thread, Lmethod);
2636    restore_return_value(state, is_native_method);
2637  }
2638}
2639
2640void InterpreterMacroAssembler::save_return_value(TosState state, bool is_native_call) {
2641  if (is_native_call) {
2642    stf(FloatRegisterImpl::D, F0, d_tmp);
2643    stx(O0, l_tmp);
2644  } else {
2645    push(state);
2646  }
2647}
2648
2649void InterpreterMacroAssembler::restore_return_value( TosState state, bool is_native_call) {
2650  if (is_native_call) {
2651    ldf(FloatRegisterImpl::D, d_tmp, F0);
2652    ldx(l_tmp, O0);
2653  } else {
2654    pop(state);
2655  }
2656}
2657