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