1 /*
   2  * Copyright 1997-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 #include "incls/_precompiled.incl"
  26 #include "incls/_templateInterpreter_sparc.cpp.incl"
  27 
  28 #ifndef CC_INTERP
  29 #ifndef FAST_DISPATCH
  30 #define FAST_DISPATCH 1
  31 #endif
  32 #undef FAST_DISPATCH
  33 
  34 
  35 // Generation of Interpreter
  36 //
  37 // The InterpreterGenerator generates the interpreter into Interpreter::_code.
  38 
  39 
  40 #define __ _masm->
  41 
  42 
  43 //----------------------------------------------------------------------------------------------------
  44 
  45 
  46 void InterpreterGenerator::save_native_result(void) {
  47   // result potentially in O0/O1: save it across calls
  48   const Address& l_tmp = InterpreterMacroAssembler::l_tmp;
  49 
  50   // result potentially in F0/F1: save it across calls
  51   const Address& d_tmp = InterpreterMacroAssembler::d_tmp;
  52 
  53   // save and restore any potential method result value around the unlocking operation
  54   __ stf(FloatRegisterImpl::D, F0, d_tmp);
  55 #ifdef _LP64
  56   __ stx(O0, l_tmp);
  57 #else
  58   __ std(O0, l_tmp);
  59 #endif
  60 }
  61 
  62 void InterpreterGenerator::restore_native_result(void) {
  63   const Address& l_tmp = InterpreterMacroAssembler::l_tmp;
  64   const Address& d_tmp = InterpreterMacroAssembler::d_tmp;
  65 
  66   // Restore any method result value
  67   __ ldf(FloatRegisterImpl::D, d_tmp, F0);
  68 #ifdef _LP64
  69   __ ldx(l_tmp, O0);
  70 #else
  71   __ ldd(l_tmp, O0);
  72 #endif
  73 }
  74 
  75 address TemplateInterpreterGenerator::generate_exception_handler_common(const char* name, const char* message, bool pass_oop) {
  76   assert(!pass_oop || message == NULL, "either oop or message but not both");
  77   address entry = __ pc();
  78   // expression stack must be empty before entering the VM if an exception happened
  79   __ empty_expression_stack();
  80   // load exception object
  81   __ set((intptr_t)name, G3_scratch);
  82   if (pass_oop) {
  83     __ call_VM(Oexception, CAST_FROM_FN_PTR(address, InterpreterRuntime::create_klass_exception), G3_scratch, Otos_i);
  84   } else {
  85     __ set((intptr_t)message, G4_scratch);
  86     __ call_VM(Oexception, CAST_FROM_FN_PTR(address, InterpreterRuntime::create_exception), G3_scratch, G4_scratch);
  87   }
  88   // throw exception
  89   assert(Interpreter::throw_exception_entry() != NULL, "generate it first");
  90   Address thrower(G3_scratch, Interpreter::throw_exception_entry());
  91   __ jump_to (thrower);
  92   __ delayed()->nop();
  93   return entry;
  94 }
  95 
  96 address TemplateInterpreterGenerator::generate_ClassCastException_handler() {
  97   address entry = __ pc();
  98   // expression stack must be empty before entering the VM if an exception
  99   // happened
 100   __ empty_expression_stack();
 101   // load exception object
 102   __ call_VM(Oexception,
 103              CAST_FROM_FN_PTR(address,
 104                               InterpreterRuntime::throw_ClassCastException),
 105              Otos_i);
 106   __ should_not_reach_here();
 107   return entry;
 108 }
 109 
 110 
 111 #ifdef ASSERT
 112 address last_WrongMethodType_caller;
 113 #endif //ASSERT
 114 
 115 // Arguments are: required type in G5_method_type, and
 116 // failing object (or NULL) in G3_method_handle.
 117 // In the debug build, the caller should put his own PC in G1.
 118 address TemplateInterpreterGenerator::generate_WrongMethodType_handler() {
 119   address entry = __ pc();
 120 #ifdef ASSERT
 121   Address last_caller_addr(O3, (address)&last_WrongMethodType_caller);
 122   __ sethi(last_caller_addr);
 123   __ st_ptr(G1, last_caller_addr);
 124 #endif //ASSERT
 125   // expression stack must be empty before entering the VM if an exception
 126   // happened
 127   __ empty_expression_stack();
 128   // load exception object
 129   __ call_VM(Oexception,
 130              CAST_FROM_FN_PTR(address,
 131                               InterpreterRuntime::throw_WrongMethodTypeException),
 132              G5_method_type,    // required
 133              G3_method_handle); // actual
 134   __ should_not_reach_here();
 135   return entry;
 136 }
 137 
 138 
 139 address TemplateInterpreterGenerator::generate_ArrayIndexOutOfBounds_handler(const char* name) {
 140   address entry = __ pc();
 141   // expression stack must be empty before entering the VM if an exception happened
 142   __ empty_expression_stack();
 143   // convention: expect aberrant index in register G3_scratch, then shuffle the
 144   // index to G4_scratch for the VM call
 145   __ mov(G3_scratch, G4_scratch);
 146   __ set((intptr_t)name, G3_scratch);
 147   __ call_VM(Oexception, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException), G3_scratch, G4_scratch);
 148   __ should_not_reach_here();
 149   return entry;
 150 }
 151 
 152 
 153 address TemplateInterpreterGenerator::generate_StackOverflowError_handler() {
 154   address entry = __ pc();
 155   // expression stack must be empty before entering the VM if an exception happened
 156   __ empty_expression_stack();
 157   __ call_VM(Oexception, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_StackOverflowError));
 158   __ should_not_reach_here();
 159   return entry;
 160 }
 161 
 162 
 163 address TemplateInterpreterGenerator::generate_return_entry_for(TosState state, int step, bool unbox) {
 164   TosState incoming_state = state;
 165   if (InvokeDynamic) {
 166     if (unbox) {
 167       incoming_state = atos;
 168     }
 169   } else {
 170     assert(!unbox, "old behavior");
 171   }
 172 
 173   address compiled_entry = __ pc();
 174   Label cont;
 175 
 176   address entry = __ pc();
 177 #if !defined(_LP64) && defined(COMPILER2)
 178   // All return values are where we want them, except for Longs.  C2 returns
 179   // longs in G1 in the 32-bit build whereas the interpreter wants them in O0/O1.
 180   // Since the interpreter will return longs in G1 and O0/O1 in the 32bit
 181   // build even if we are returning from interpreted we just do a little
 182   // stupid shuffing.
 183   // Note: I tried to make c2 return longs in O0/O1 and G1 so we wouldn't have to
 184   // do this here. Unfortunately if we did a rethrow we'd see an machepilog node
 185   // first which would move g1 -> O0/O1 and destroy the exception we were throwing.
 186 
 187   if( incoming_state == ltos ) {
 188     __ srl (G1, 0,O1);
 189     __ srlx(G1,32,O0);
 190   }
 191 #endif /* !_LP64 && COMPILER2 */
 192 
 193 
 194   __ bind(cont);
 195 
 196   // The callee returns with the stack possibly adjusted by adapter transition
 197   // We remove that possible adjustment here.
 198   // All interpreter local registers are untouched. Any result is passed back
 199   // in the O0/O1 or float registers. Before continuing, the arguments must be
 200   // popped from the java expression stack; i.e., Lesp must be adjusted.
 201 
 202   __ mov(Llast_SP, SP);   // Remove any adapter added stack space.
 203 
 204   if (unbox && state != atos) {
 205     // cast and unbox
 206     __ unimplemented();
 207   }
 208 
 209   const Register cache = G3_scratch;
 210   const Register size  = G1_scratch;
 211   Label L_got_cache, L_giant_index;
 212   if (InvokeDynamic) {
 213     __ ldub(Lbcp, 0, size);
 214     __ cmp(size, Bytecodes::_invokedynamic);
 215     __ br(Assembler::equal, false, Assembler::pn, L_giant_index);
 216     __ delayed()->nop();
 217   }
 218   __ get_cache_and_index_at_bcp(cache, G1_scratch, 1);
 219   ////__ get_cache_and_index_at_bcp(cache, G1_scratch, 1, false);
 220   __ bind(L_got_cache);
 221   if (unbox && state == atos) {
 222     // insert a casting conversion, to keep verifier sane
 223     __ unimplemented();
 224   }
 225   __ ld_ptr(Address(cache, 0, in_bytes(constantPoolCacheOopDesc::base_offset()) +
 226                     in_bytes(ConstantPoolCacheEntry::flags_offset())), size);
 227   __ and3(size, 0xFF, size);                   // argument size in words
 228   __ sll(size, Interpreter::logStackElementSize(), size); // each argument size in bytes
 229   __ add(Lesp, size, Lesp);                    // pop arguments
 230   __ dispatch_next(state, step);
 231 
 232   // out of the main line of code...
 233   if (InvokeDynamic) {
 234     __ bind(L_giant_index);
 235     __ unimplemented();
 236     ////__ get_cache_and_index_at_bcp(cache, G1_scratch, 1, true);
 237     __ ba(false, L_got_cache);
 238     __ delayed()->nop();
 239   }
 240 
 241   return entry;
 242 }
 243 
 244 
 245 address TemplateInterpreterGenerator::generate_deopt_entry_for(TosState state, int step) {
 246   address entry = __ pc();
 247   __ get_constant_pool_cache(LcpoolCache); // load LcpoolCache
 248   { Label L;
 249     Address exception_addr (G2_thread, 0, in_bytes(Thread::pending_exception_offset()));
 250 
 251     __ ld_ptr(exception_addr, Gtemp);
 252     __ tst(Gtemp);
 253     __ brx(Assembler::equal, false, Assembler::pt, L);
 254     __ delayed()->nop();
 255     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_pending_exception));
 256     __ should_not_reach_here();
 257     __ bind(L);
 258   }
 259   __ dispatch_next(state, step);
 260   return entry;
 261 }
 262 
 263 // A result handler converts/unboxes a native call result into
 264 // a java interpreter/compiler result. The current frame is an
 265 // interpreter frame. The activation frame unwind code must be
 266 // consistent with that of TemplateTable::_return(...). In the
 267 // case of native methods, the caller's SP was not modified.
 268 address TemplateInterpreterGenerator::generate_result_handler_for(BasicType type) {
 269   address entry = __ pc();
 270   Register Itos_i  = Otos_i ->after_save();
 271   Register Itos_l  = Otos_l ->after_save();
 272   Register Itos_l1 = Otos_l1->after_save();
 273   Register Itos_l2 = Otos_l2->after_save();
 274   switch (type) {
 275     case T_BOOLEAN: __ subcc(G0, O0, G0); __ addc(G0, 0, Itos_i); break; // !0 => true; 0 => false
 276     case T_CHAR   : __ sll(O0, 16, O0); __ srl(O0, 16, Itos_i);   break; // cannot use and3, 0xFFFF too big as immediate value!
 277     case T_BYTE   : __ sll(O0, 24, O0); __ sra(O0, 24, Itos_i);   break;
 278     case T_SHORT  : __ sll(O0, 16, O0); __ sra(O0, 16, Itos_i);   break;
 279     case T_LONG   :
 280 #ifndef _LP64
 281                     __ mov(O1, Itos_l2);  // move other half of long
 282 #endif              // ifdef or no ifdef, fall through to the T_INT case
 283     case T_INT    : __ mov(O0, Itos_i);                         break;
 284     case T_VOID   : /* nothing to do */                         break;
 285     case T_FLOAT  : assert(F0 == Ftos_f, "fix this code" );     break;
 286     case T_DOUBLE : assert(F0 == Ftos_d, "fix this code" );     break;
 287     case T_OBJECT :
 288       __ ld_ptr(FP, (frame::interpreter_frame_oop_temp_offset*wordSize) + STACK_BIAS, Itos_i);
 289       __ verify_oop(Itos_i);
 290       break;
 291     default       : ShouldNotReachHere();
 292   }
 293   __ ret();                           // return from interpreter activation
 294   __ delayed()->restore(I5_savedSP, G0, SP);  // remove interpreter frame
 295   NOT_PRODUCT(__ emit_long(0);)       // marker for disassembly
 296   return entry;
 297 }
 298 
 299 address TemplateInterpreterGenerator::generate_safept_entry_for(TosState state, address runtime_entry) {
 300   address entry = __ pc();
 301   __ push(state);
 302   __ call_VM(noreg, runtime_entry);
 303   __ dispatch_via(vtos, Interpreter::normal_table(vtos));
 304   return entry;
 305 }
 306 
 307 
 308 address TemplateInterpreterGenerator::generate_continuation_for(TosState state) {
 309   address entry = __ pc();
 310   __ dispatch_next(state);
 311   return entry;
 312 }
 313 
 314 //
 315 // Helpers for commoning out cases in the various type of method entries.
 316 //
 317 
 318 // increment invocation count & check for overflow
 319 //
 320 // Note: checking for negative value instead of overflow
 321 //       so we have a 'sticky' overflow test
 322 //
 323 // Lmethod: method
 324 // ??: invocation counter
 325 //
 326 void InterpreterGenerator::generate_counter_incr(Label* overflow, Label* profile_method, Label* profile_method_continue) {
 327   // Update standard invocation counters
 328   __ increment_invocation_counter(O0, G3_scratch);
 329   if (ProfileInterpreter) {  // %%% Merge this into methodDataOop
 330     Address interpreter_invocation_counter(Lmethod, 0, in_bytes(methodOopDesc::interpreter_invocation_counter_offset()));
 331     __ ld(interpreter_invocation_counter, G3_scratch);
 332     __ inc(G3_scratch);
 333     __ st(G3_scratch, interpreter_invocation_counter);
 334   }
 335 
 336   if (ProfileInterpreter && profile_method != NULL) {
 337     // Test to see if we should create a method data oop
 338     Address profile_limit(G3_scratch, (address)&InvocationCounter::InterpreterProfileLimit);
 339     __ sethi(profile_limit);
 340     __ ld(profile_limit, G3_scratch);
 341     __ cmp(O0, G3_scratch);
 342     __ br(Assembler::lessUnsigned, false, Assembler::pn, *profile_method_continue);
 343     __ delayed()->nop();
 344 
 345     // if no method data exists, go to profile_method
 346     __ test_method_data_pointer(*profile_method);
 347   }
 348 
 349   Address invocation_limit(G3_scratch, (address)&InvocationCounter::InterpreterInvocationLimit);
 350   __ sethi(invocation_limit);
 351   __ ld(invocation_limit, G3_scratch);
 352   __ cmp(O0, G3_scratch);
 353   __ br(Assembler::greaterEqualUnsigned, false, Assembler::pn, *overflow);
 354   __ delayed()->nop();
 355 
 356 }
 357 
 358 // Allocate monitor and lock method (asm interpreter)
 359 // ebx - methodOop
 360 //
 361 void InterpreterGenerator::lock_method(void) {
 362   const Address access_flags      (Lmethod, 0, in_bytes(methodOopDesc::access_flags_offset()));
 363   __ ld(access_flags, O0);
 364 
 365 #ifdef ASSERT
 366  { Label ok;
 367    __ btst(JVM_ACC_SYNCHRONIZED, O0);
 368    __ br( Assembler::notZero, false, Assembler::pt, ok);
 369    __ delayed()->nop();
 370    __ stop("method doesn't need synchronization");
 371    __ bind(ok);
 372   }
 373 #endif // ASSERT
 374 
 375   // get synchronization object to O0
 376   { Label done;
 377     const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
 378     __ btst(JVM_ACC_STATIC, O0);
 379     __ br( Assembler::zero, true, Assembler::pt, done);
 380     __ delayed()->ld_ptr(Llocals, Interpreter::local_offset_in_bytes(0), O0); // get receiver for not-static case
 381 
 382     __ ld_ptr( Lmethod, in_bytes(methodOopDesc::constants_offset()), O0);
 383     __ ld_ptr( O0, constantPoolOopDesc::pool_holder_offset_in_bytes(), O0);
 384 
 385     // lock the mirror, not the klassOop
 386     __ ld_ptr( O0, mirror_offset, O0);
 387 
 388 #ifdef ASSERT
 389     __ tst(O0);
 390     __ breakpoint_trap(Assembler::zero);
 391 #endif // ASSERT
 392 
 393     __ bind(done);
 394   }
 395 
 396   __ add_monitor_to_stack(true, noreg, noreg);  // allocate monitor elem
 397   __ st_ptr( O0, Lmonitors, BasicObjectLock::obj_offset_in_bytes());   // store object
 398   // __ untested("lock_object from method entry");
 399   __ lock_object(Lmonitors, O0);
 400 }
 401 
 402 
 403 void TemplateInterpreterGenerator::generate_stack_overflow_check(Register Rframe_size,
 404                                                          Register Rscratch,
 405                                                          Register Rscratch2) {
 406   const int page_size = os::vm_page_size();
 407   Address saved_exception_pc(G2_thread, 0,
 408                              in_bytes(JavaThread::saved_exception_pc_offset()));
 409   Label after_frame_check;
 410 
 411   assert_different_registers(Rframe_size, Rscratch, Rscratch2);
 412 
 413   __ set( page_size,   Rscratch );
 414   __ cmp( Rframe_size, Rscratch );
 415 
 416   __ br( Assembler::lessEqual, false, Assembler::pt, after_frame_check );
 417   __ delayed()->nop();
 418 
 419   // get the stack base, and in debug, verify it is non-zero
 420   __ ld_ptr( G2_thread, in_bytes(Thread::stack_base_offset()), Rscratch );
 421 #ifdef ASSERT
 422   Label base_not_zero;
 423   __ cmp( Rscratch, G0 );
 424   __ brx( Assembler::notEqual, false, Assembler::pn, base_not_zero );
 425   __ delayed()->nop();
 426   __ stop("stack base is zero in generate_stack_overflow_check");
 427   __ bind(base_not_zero);
 428 #endif
 429 
 430   // get the stack size, and in debug, verify it is non-zero
 431   assert( sizeof(size_t) == sizeof(intptr_t), "wrong load size" );
 432   __ ld_ptr( G2_thread, in_bytes(Thread::stack_size_offset()), Rscratch2 );
 433 #ifdef ASSERT
 434   Label size_not_zero;
 435   __ cmp( Rscratch2, G0 );
 436   __ brx( Assembler::notEqual, false, Assembler::pn, size_not_zero );
 437   __ delayed()->nop();
 438   __ stop("stack size is zero in generate_stack_overflow_check");
 439   __ bind(size_not_zero);
 440 #endif
 441 
 442   // compute the beginning of the protected zone minus the requested frame size
 443   __ sub( Rscratch, Rscratch2,   Rscratch );
 444   __ set( (StackRedPages+StackYellowPages) * page_size, Rscratch2 );
 445   __ add( Rscratch, Rscratch2,   Rscratch );
 446 
 447   // Add in the size of the frame (which is the same as subtracting it from the
 448   // SP, which would take another register
 449   __ add( Rscratch, Rframe_size, Rscratch );
 450 
 451   // the frame is greater than one page in size, so check against
 452   // the bottom of the stack
 453   __ cmp( SP, Rscratch );
 454   __ brx( Assembler::greater, false, Assembler::pt, after_frame_check );
 455   __ delayed()->nop();
 456 
 457   // Save the return address as the exception pc
 458   __ st_ptr(O7, saved_exception_pc);
 459 
 460   // the stack will overflow, throw an exception
 461   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_StackOverflowError));
 462 
 463   // if you get to here, then there is enough stack space
 464   __ bind( after_frame_check );
 465 }
 466 
 467 
 468 //
 469 // Generate a fixed interpreter frame. This is identical setup for interpreted
 470 // methods and for native methods hence the shared code.
 471 
 472 void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call) {
 473   //
 474   //
 475   // The entry code sets up a new interpreter frame in 4 steps:
 476   //
 477   // 1) Increase caller's SP by for the extra local space needed:
 478   //    (check for overflow)
 479   //    Efficient implementation of xload/xstore bytecodes requires
 480   //    that arguments and non-argument locals are in a contigously
 481   //    addressable memory block => non-argument locals must be
 482   //    allocated in the caller's frame.
 483   //
 484   // 2) Create a new stack frame and register window:
 485   //    The new stack frame must provide space for the standard
 486   //    register save area, the maximum java expression stack size,
 487   //    the monitor slots (0 slots initially), and some frame local
 488   //    scratch locations.
 489   //
 490   // 3) The following interpreter activation registers must be setup:
 491   //    Lesp       : expression stack pointer
 492   //    Lbcp       : bytecode pointer
 493   //    Lmethod    : method
 494   //    Llocals    : locals pointer
 495   //    Lmonitors  : monitor pointer
 496   //    LcpoolCache: constant pool cache
 497   //
 498   // 4) Initialize the non-argument locals if necessary:
 499   //    Non-argument locals may need to be initialized to NULL
 500   //    for GC to work. If the oop-map information is accurate
 501   //    (in the absence of the JSR problem), no initialization
 502   //    is necessary.
 503   //
 504   // (gri - 2/25/2000)
 505 
 506 
 507   const Address size_of_parameters(G5_method, 0, in_bytes(methodOopDesc::size_of_parameters_offset()));
 508   const Address size_of_locals    (G5_method, 0, in_bytes(methodOopDesc::size_of_locals_offset()));
 509   const Address max_stack         (G5_method, 0, in_bytes(methodOopDesc::max_stack_offset()));
 510   int rounded_vm_local_words = round_to( frame::interpreter_frame_vm_local_words, WordsPerLong );
 511 
 512   const int extra_space =
 513     rounded_vm_local_words +                   // frame local scratch space
 514     methodOopDesc::extra_stack() +             // extra push slot for MH insertion
 515     frame::memory_parameter_word_sp_offset +   // register save area
 516     (native_call ? frame::interpreter_frame_extra_outgoing_argument_words : 0);
 517 
 518   const Register Glocals_size = G3;
 519   const Register Otmp1 = O3;
 520   const Register Otmp2 = O4;
 521   // Lscratch can't be used as a temporary because the call_stub uses
 522   // it to assert that the stack frame was setup correctly.
 523 
 524   __ lduh( size_of_parameters, Glocals_size);
 525 
 526   // Gargs points to first local + BytesPerWord
 527   // Set the saved SP after the register window save
 528   //
 529   assert_different_registers(Gargs, Glocals_size, Gframe_size, O5_savedSP);
 530   __ sll(Glocals_size, Interpreter::logStackElementSize(), Otmp1);
 531   __ add(Gargs, Otmp1, Gargs);
 532 
 533   if (native_call) {
 534     __ calc_mem_param_words( Glocals_size, Gframe_size );
 535     __ add( Gframe_size,  extra_space, Gframe_size);
 536     __ round_to( Gframe_size, WordsPerLong );
 537     __ sll( Gframe_size, LogBytesPerWord, Gframe_size );
 538   } else {
 539 
 540     //
 541     // Compute number of locals in method apart from incoming parameters
 542     //
 543     __ lduh( size_of_locals, Otmp1 );
 544     __ sub( Otmp1, Glocals_size, Glocals_size );
 545     __ round_to( Glocals_size, WordsPerLong );
 546     __ sll( Glocals_size, Interpreter::logStackElementSize(), Glocals_size );
 547 
 548     // see if the frame is greater than one page in size. If so,
 549     // then we need to verify there is enough stack space remaining
 550     // Frame_size = (max_stack + extra_space) * BytesPerWord;
 551     __ lduh( max_stack, Gframe_size );
 552     __ add( Gframe_size, extra_space, Gframe_size );
 553     __ round_to( Gframe_size, WordsPerLong );
 554     __ sll( Gframe_size, Interpreter::logStackElementSize(), Gframe_size);
 555 
 556     // Add in java locals size for stack overflow check only
 557     __ add( Gframe_size, Glocals_size, Gframe_size );
 558 
 559     const Register Otmp2 = O4;
 560     assert_different_registers(Otmp1, Otmp2, O5_savedSP);
 561     generate_stack_overflow_check(Gframe_size, Otmp1, Otmp2);
 562 
 563     __ sub( Gframe_size, Glocals_size, Gframe_size);
 564 
 565     //
 566     // bump SP to accomodate the extra locals
 567     //
 568     __ sub( SP, Glocals_size, SP );
 569   }
 570 
 571   //
 572   // now set up a stack frame with the size computed above
 573   //
 574   __ neg( Gframe_size );
 575   __ save( SP, Gframe_size, SP );
 576 
 577   //
 578   // now set up all the local cache registers
 579   //
 580   // NOTE: At this point, Lbyte_code/Lscratch has been modified. Note
 581   // that all present references to Lbyte_code initialize the register
 582   // immediately before use
 583   if (native_call) {
 584     __ mov(G0, Lbcp);
 585   } else {
 586     __ ld_ptr(Address(G5_method, 0, in_bytes(methodOopDesc::const_offset())), Lbcp );
 587     __ add(Address(Lbcp, 0, in_bytes(constMethodOopDesc::codes_offset())), Lbcp );
 588   }
 589   __ mov( G5_method, Lmethod);                 // set Lmethod
 590   __ get_constant_pool_cache( LcpoolCache );   // set LcpoolCache
 591   __ sub(FP, rounded_vm_local_words * BytesPerWord, Lmonitors ); // set Lmonitors
 592 #ifdef _LP64
 593   __ add( Lmonitors, STACK_BIAS, Lmonitors );   // Account for 64 bit stack bias
 594 #endif
 595   __ sub(Lmonitors, BytesPerWord, Lesp);       // set Lesp
 596 
 597   // setup interpreter activation registers
 598   __ sub(Gargs, BytesPerWord, Llocals);        // set Llocals
 599 
 600   if (ProfileInterpreter) {
 601 #ifdef FAST_DISPATCH
 602     // FAST_DISPATCH and ProfileInterpreter are mutually exclusive since
 603     // they both use I2.
 604     assert(0, "FAST_DISPATCH and +ProfileInterpreter are mutually exclusive");
 605 #endif // FAST_DISPATCH
 606     __ set_method_data_pointer();
 607   }
 608 
 609 }
 610 
 611 // Empty method, generate a very fast return.
 612 
 613 address InterpreterGenerator::generate_empty_entry(void) {
 614 
 615   // A method that does nother but return...
 616 
 617   address entry = __ pc();
 618   Label slow_path;
 619 
 620   __ verify_oop(G5_method);
 621 
 622   // do nothing for empty methods (do not even increment invocation counter)
 623   if ( UseFastEmptyMethods) {
 624     // If we need a safepoint check, generate full interpreter entry.
 625     Address sync_state(G3_scratch, SafepointSynchronize::address_of_state());
 626     __ load_contents(sync_state, G3_scratch);
 627     __ cmp(G3_scratch, SafepointSynchronize::_not_synchronized);
 628     __ br(Assembler::notEqual, false, Assembler::pn, slow_path);
 629     __ delayed()->nop();
 630 
 631     // Code: _return
 632     __ retl();
 633     __ delayed()->mov(O5_savedSP, SP);
 634 
 635     __ bind(slow_path);
 636     (void) generate_normal_entry(false);
 637 
 638     return entry;
 639   }
 640   return NULL;
 641 }
 642 
 643 // Call an accessor method (assuming it is resolved, otherwise drop into
 644 // vanilla (slow path) entry
 645 
 646 // Generates code to elide accessor methods
 647 // Uses G3_scratch and G1_scratch as scratch
 648 address InterpreterGenerator::generate_accessor_entry(void) {
 649 
 650   // Code: _aload_0, _(i|a)getfield, _(i|a)return or any rewrites thereof;
 651   // parameter size = 1
 652   // Note: We can only use this code if the getfield has been resolved
 653   //       and if we don't have a null-pointer exception => check for
 654   //       these conditions first and use slow path if necessary.
 655   address entry = __ pc();
 656   Label slow_path;
 657 
 658 
 659   // XXX: for compressed oops pointer loading and decoding doesn't fit in
 660   // delay slot and damages G1
 661   if ( UseFastAccessorMethods && !UseCompressedOops ) {
 662     // Check if we need to reach a safepoint and generate full interpreter
 663     // frame if so.
 664     Address sync_state(G3_scratch, SafepointSynchronize::address_of_state());
 665     __ load_contents(sync_state, G3_scratch);
 666     __ cmp(G3_scratch, SafepointSynchronize::_not_synchronized);
 667     __ br(Assembler::notEqual, false, Assembler::pn, slow_path);
 668     __ delayed()->nop();
 669 
 670     // Check if local 0 != NULL
 671     __ ld_ptr(Gargs, G0, Otos_i ); // get local 0
 672     __ tst(Otos_i);  // check if local 0 == NULL and go the slow path
 673     __ brx(Assembler::zero, false, Assembler::pn, slow_path);
 674     __ delayed()->nop();
 675 
 676 
 677     // read first instruction word and extract bytecode @ 1 and index @ 2
 678     // get first 4 bytes of the bytecodes (big endian!)
 679     __ ld_ptr(Address(G5_method, 0, in_bytes(methodOopDesc::const_offset())), G1_scratch);
 680     __ ld(Address(G1_scratch, 0, in_bytes(constMethodOopDesc::codes_offset())), G1_scratch);
 681 
 682     // move index @ 2 far left then to the right most two bytes.
 683     __ sll(G1_scratch, 2*BitsPerByte, G1_scratch);
 684     __ srl(G1_scratch, 2*BitsPerByte - exact_log2(in_words(
 685                       ConstantPoolCacheEntry::size()) * BytesPerWord), G1_scratch);
 686 
 687     // get constant pool cache
 688     __ ld_ptr(G5_method, in_bytes(methodOopDesc::constants_offset()), G3_scratch);
 689     __ ld_ptr(G3_scratch, constantPoolOopDesc::cache_offset_in_bytes(), G3_scratch);
 690 
 691     // get specific constant pool cache entry
 692     __ add(G3_scratch, G1_scratch, G3_scratch);
 693 
 694     // Check the constant Pool cache entry to see if it has been resolved.
 695     // If not, need the slow path.
 696     ByteSize cp_base_offset = constantPoolCacheOopDesc::base_offset();
 697     __ ld_ptr(G3_scratch, in_bytes(cp_base_offset + ConstantPoolCacheEntry::indices_offset()), G1_scratch);
 698     __ srl(G1_scratch, 2*BitsPerByte, G1_scratch);
 699     __ and3(G1_scratch, 0xFF, G1_scratch);
 700     __ cmp(G1_scratch, Bytecodes::_getfield);
 701     __ br(Assembler::notEqual, false, Assembler::pn, slow_path);
 702     __ delayed()->nop();
 703 
 704     // Get the type and return field offset from the constant pool cache
 705     __ ld_ptr(G3_scratch, in_bytes(cp_base_offset + ConstantPoolCacheEntry::flags_offset()), G1_scratch);
 706     __ ld_ptr(G3_scratch, in_bytes(cp_base_offset + ConstantPoolCacheEntry::f2_offset()), G3_scratch);
 707 
 708     Label xreturn_path;
 709     // Need to differentiate between igetfield, agetfield, bgetfield etc.
 710     // because they are different sizes.
 711     // Get the type from the constant pool cache
 712     __ srl(G1_scratch, ConstantPoolCacheEntry::tosBits, G1_scratch);
 713     // Make sure we don't need to mask G1_scratch for tosBits after the above shift
 714     ConstantPoolCacheEntry::verify_tosBits();
 715     __ cmp(G1_scratch, atos );
 716     __ br(Assembler::equal, true, Assembler::pt, xreturn_path);
 717     __ delayed()->ld_ptr(Otos_i, G3_scratch, Otos_i);
 718     __ cmp(G1_scratch, itos);
 719     __ br(Assembler::equal, true, Assembler::pt, xreturn_path);
 720     __ delayed()->ld(Otos_i, G3_scratch, Otos_i);
 721     __ cmp(G1_scratch, stos);
 722     __ br(Assembler::equal, true, Assembler::pt, xreturn_path);
 723     __ delayed()->ldsh(Otos_i, G3_scratch, Otos_i);
 724     __ cmp(G1_scratch, ctos);
 725     __ br(Assembler::equal, true, Assembler::pt, xreturn_path);
 726     __ delayed()->lduh(Otos_i, G3_scratch, Otos_i);
 727 #ifdef ASSERT
 728     __ cmp(G1_scratch, btos);
 729     __ br(Assembler::equal, true, Assembler::pt, xreturn_path);
 730     __ delayed()->ldsb(Otos_i, G3_scratch, Otos_i);
 731     __ should_not_reach_here();
 732 #endif
 733     __ ldsb(Otos_i, G3_scratch, Otos_i);
 734     __ bind(xreturn_path);
 735 
 736     // _ireturn/_areturn
 737     __ retl();                      // return from leaf routine
 738     __ delayed()->mov(O5_savedSP, SP);
 739 
 740     // Generate regular method entry
 741     __ bind(slow_path);
 742     (void) generate_normal_entry(false);
 743     return entry;
 744   }
 745   return NULL;
 746 }
 747 
 748 //
 749 // Interpreter stub for calling a native method. (asm interpreter)
 750 // This sets up a somewhat different looking stack for calling the native method
 751 // than the typical interpreter frame setup.
 752 //
 753 
 754 address InterpreterGenerator::generate_native_entry(bool synchronized) {
 755   address entry = __ pc();
 756 
 757   // the following temporary registers are used during frame creation
 758   const Register Gtmp1 = G3_scratch ;
 759   const Register Gtmp2 = G1_scratch;
 760   bool inc_counter  = UseCompiler || CountCompiledCalls;
 761 
 762   // make sure registers are different!
 763   assert_different_registers(G2_thread, G5_method, Gargs, Gtmp1, Gtmp2);
 764 
 765   const Address Laccess_flags     (Lmethod, 0, in_bytes(methodOopDesc::access_flags_offset()));
 766 
 767   __ verify_oop(G5_method);
 768 
 769   const Register Glocals_size = G3;
 770   assert_different_registers(Glocals_size, G4_scratch, Gframe_size);
 771 
 772   // make sure method is native & not abstract
 773   // rethink these assertions - they can be simplified and shared (gri 2/25/2000)
 774 #ifdef ASSERT
 775   __ ld(G5_method, in_bytes(methodOopDesc::access_flags_offset()), Gtmp1);
 776   {
 777     Label L;
 778     __ btst(JVM_ACC_NATIVE, Gtmp1);
 779     __ br(Assembler::notZero, false, Assembler::pt, L);
 780     __ delayed()->nop();
 781     __ stop("tried to execute non-native method as native");
 782     __ bind(L);
 783   }
 784   { Label L;
 785     __ btst(JVM_ACC_ABSTRACT, Gtmp1);
 786     __ br(Assembler::zero, false, Assembler::pt, L);
 787     __ delayed()->nop();
 788     __ stop("tried to execute abstract method as non-abstract");
 789     __ bind(L);
 790   }
 791 #endif // ASSERT
 792 
 793  // generate the code to allocate the interpreter stack frame
 794   generate_fixed_frame(true);
 795 
 796   //
 797   // No locals to initialize for native method
 798   //
 799 
 800   // this slot will be set later, we initialize it to null here just in
 801   // case we get a GC before the actual value is stored later
 802   __ st_ptr(G0, Address(FP, 0, (frame::interpreter_frame_oop_temp_offset*wordSize) + STACK_BIAS));
 803 
 804   const Address do_not_unlock_if_synchronized(G2_thread, 0,
 805       in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 806   // Since at this point in the method invocation the exception handler
 807   // would try to exit the monitor of synchronized methods which hasn't
 808   // been entered yet, we set the thread local variable
 809   // _do_not_unlock_if_synchronized to true. If any exception was thrown by
 810   // runtime, exception handling i.e. unlock_if_synchronized_method will
 811   // check this thread local flag.
 812   // This flag has two effects, one is to force an unwind in the topmost
 813   // interpreter frame and not perform an unlock while doing so.
 814 
 815   __ movbool(true, G3_scratch);
 816   __ stbool(G3_scratch, do_not_unlock_if_synchronized);
 817 
 818   // increment invocation counter and check for overflow
 819   //
 820   // Note: checking for negative value instead of overflow
 821   //       so we have a 'sticky' overflow test (may be of
 822   //       importance as soon as we have true MT/MP)
 823   Label invocation_counter_overflow;
 824   Label Lcontinue;
 825   if (inc_counter) {
 826     generate_counter_incr(&invocation_counter_overflow, NULL, NULL);
 827 
 828   }
 829   __ bind(Lcontinue);
 830 
 831   bang_stack_shadow_pages(true);
 832 
 833   // reset the _do_not_unlock_if_synchronized flag
 834   __ stbool(G0, do_not_unlock_if_synchronized);
 835 
 836   // check for synchronized methods
 837   // Must happen AFTER invocation_counter check and stack overflow check,
 838   // so method is not locked if overflows.
 839 
 840   if (synchronized) {
 841     lock_method();
 842   } else {
 843 #ifdef ASSERT
 844     { Label ok;
 845       __ ld(Laccess_flags, O0);
 846       __ btst(JVM_ACC_SYNCHRONIZED, O0);
 847       __ br( Assembler::zero, false, Assembler::pt, ok);
 848       __ delayed()->nop();
 849       __ stop("method needs synchronization");
 850       __ bind(ok);
 851     }
 852 #endif // ASSERT
 853   }
 854 
 855 
 856   // start execution
 857   __ verify_thread();
 858 
 859   // JVMTI support
 860   __ notify_method_entry();
 861 
 862   // native call
 863 
 864   // (note that O0 is never an oop--at most it is a handle)
 865   // It is important not to smash any handles created by this call,
 866   // until any oop handle in O0 is dereferenced.
 867 
 868   // (note that the space for outgoing params is preallocated)
 869 
 870   // get signature handler
 871   { Label L;
 872     __ ld_ptr(Address(Lmethod, 0, in_bytes(methodOopDesc::signature_handler_offset())), G3_scratch);
 873     __ tst(G3_scratch);
 874     __ brx(Assembler::notZero, false, Assembler::pt, L);
 875     __ delayed()->nop();
 876     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), Lmethod);
 877     __ ld_ptr(Address(Lmethod, 0, in_bytes(methodOopDesc::signature_handler_offset())), G3_scratch);
 878     __ bind(L);
 879   }
 880 
 881   // Push a new frame so that the args will really be stored in
 882   // Copy a few locals across so the new frame has the variables
 883   // we need but these values will be dead at the jni call and
 884   // therefore not gc volatile like the values in the current
 885   // frame (Lmethod in particular)
 886 
 887   // Flush the method pointer to the register save area
 888   __ st_ptr(Lmethod, SP, (Lmethod->sp_offset_in_saved_window() * wordSize) + STACK_BIAS);
 889   __ mov(Llocals, O1);
 890   // calculate where the mirror handle body is allocated in the interpreter frame:
 891 
 892   Address mirror(FP, 0, (frame::interpreter_frame_oop_temp_offset*wordSize) + STACK_BIAS);
 893   __ add(mirror, O2);
 894 
 895   // Calculate current frame size
 896   __ sub(SP, FP, O3);         // Calculate negative of current frame size
 897   __ save(SP, O3, SP);        // Allocate an identical sized frame
 898 
 899   // Note I7 has leftover trash. Slow signature handler will fill it in
 900   // should we get there. Normal jni call will set reasonable last_Java_pc
 901   // below (and fix I7 so the stack trace doesn't have a meaningless frame
 902   // in it).
 903 
 904   // Load interpreter frame's Lmethod into same register here
 905 
 906   __ ld_ptr(FP, (Lmethod->sp_offset_in_saved_window() * wordSize) + STACK_BIAS, Lmethod);
 907 
 908   __ mov(I1, Llocals);
 909   __ mov(I2, Lscratch2);     // save the address of the mirror
 910 
 911 
 912   // ONLY Lmethod and Llocals are valid here!
 913 
 914   // call signature handler, It will move the arg properly since Llocals in current frame
 915   // matches that in outer frame
 916 
 917   __ callr(G3_scratch, 0);
 918   __ delayed()->nop();
 919 
 920   // Result handler is in Lscratch
 921 
 922   // Reload interpreter frame's Lmethod since slow signature handler may block
 923   __ ld_ptr(FP, (Lmethod->sp_offset_in_saved_window() * wordSize) + STACK_BIAS, Lmethod);
 924 
 925   { Label not_static;
 926 
 927     __ ld(Laccess_flags, O0);
 928     __ btst(JVM_ACC_STATIC, O0);
 929     __ br( Assembler::zero, false, Assembler::pt, not_static);
 930     __ delayed()->
 931       // get native function entry point(O0 is a good temp until the very end)
 932        ld_ptr(Address(Lmethod, 0, in_bytes(methodOopDesc::native_function_offset())), O0);
 933     // for static methods insert the mirror argument
 934     const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
 935 
 936     __ ld_ptr(Address(Lmethod, 0, in_bytes(methodOopDesc:: constants_offset())), O1);
 937     __ ld_ptr(Address(O1, 0, constantPoolOopDesc::pool_holder_offset_in_bytes()), O1);
 938     __ ld_ptr(O1, mirror_offset, O1);
 939 #ifdef ASSERT
 940     if (!PrintSignatureHandlers)  // do not dirty the output with this
 941     { Label L;
 942       __ tst(O1);
 943       __ brx(Assembler::notZero, false, Assembler::pt, L);
 944       __ delayed()->nop();
 945       __ stop("mirror is missing");
 946       __ bind(L);
 947     }
 948 #endif // ASSERT
 949     __ st_ptr(O1, Lscratch2, 0);
 950     __ mov(Lscratch2, O1);
 951     __ bind(not_static);
 952   }
 953 
 954   // At this point, arguments have been copied off of stack into
 955   // their JNI positions, which are O1..O5 and SP[68..].
 956   // Oops are boxed in-place on the stack, with handles copied to arguments.
 957   // The result handler is in Lscratch.  O0 will shortly hold the JNIEnv*.
 958 
 959 #ifdef ASSERT
 960   { Label L;
 961     __ tst(O0);
 962     __ brx(Assembler::notZero, false, Assembler::pt, L);
 963     __ delayed()->nop();
 964     __ stop("native entry point is missing");
 965     __ bind(L);
 966   }
 967 #endif // ASSERT
 968 
 969   //
 970   // setup the frame anchor
 971   //
 972   // The scavenge function only needs to know that the PC of this frame is
 973   // in the interpreter method entry code, it doesn't need to know the exact
 974   // PC and hence we can use O7 which points to the return address from the
 975   // previous call in the code stream (signature handler function)
 976   //
 977   // The other trick is we set last_Java_sp to FP instead of the usual SP because
 978   // we have pushed the extra frame in order to protect the volatile register(s)
 979   // in that frame when we return from the jni call
 980   //
 981 
 982   __ set_last_Java_frame(FP, O7);
 983   __ mov(O7, I7);  // make dummy interpreter frame look like one above,
 984                    // not meaningless information that'll confuse me.
 985 
 986   // flush the windows now. We don't care about the current (protection) frame
 987   // only the outer frames
 988 
 989   __ flush_windows();
 990 
 991   // mark windows as flushed
 992   Address flags(G2_thread,
 993                 0,
 994                 in_bytes(JavaThread::frame_anchor_offset()) + in_bytes(JavaFrameAnchor::flags_offset()));
 995   __ set(JavaFrameAnchor::flushed, G3_scratch);
 996   __ st(G3_scratch, flags);
 997 
 998   // Transition from _thread_in_Java to _thread_in_native. We are already safepoint ready.
 999 
1000   Address thread_state(G2_thread, 0, in_bytes(JavaThread::thread_state_offset()));
1001 #ifdef ASSERT
1002   { Label L;
1003     __ ld(thread_state, G3_scratch);
1004     __ cmp(G3_scratch, _thread_in_Java);
1005     __ br(Assembler::equal, false, Assembler::pt, L);
1006     __ delayed()->nop();
1007     __ stop("Wrong thread state in native stub");
1008     __ bind(L);
1009   }
1010 #endif // ASSERT
1011   __ set(_thread_in_native, G3_scratch);
1012   __ st(G3_scratch, thread_state);
1013 
1014   // Call the jni method, using the delay slot to set the JNIEnv* argument.
1015   __ save_thread(L7_thread_cache); // save Gthread
1016   __ callr(O0, 0);
1017   __ delayed()->
1018      add(L7_thread_cache, in_bytes(JavaThread::jni_environment_offset()), O0);
1019 
1020   // Back from jni method Lmethod in this frame is DEAD, DEAD, DEAD
1021 
1022   __ restore_thread(L7_thread_cache); // restore G2_thread
1023   __ reinit_heapbase();
1024 
1025   // must we block?
1026 
1027   // Block, if necessary, before resuming in _thread_in_Java state.
1028   // In order for GC to work, don't clear the last_Java_sp until after blocking.
1029   { Label no_block;
1030     Address sync_state(G3_scratch, SafepointSynchronize::address_of_state());
1031 
1032     // Switch thread to "native transition" state before reading the synchronization state.
1033     // This additional state is necessary because reading and testing the synchronization
1034     // state is not atomic w.r.t. GC, as this scenario demonstrates:
1035     //     Java thread A, in _thread_in_native state, loads _not_synchronized and is preempted.
1036     //     VM thread changes sync state to synchronizing and suspends threads for GC.
1037     //     Thread A is resumed to finish this native method, but doesn't block here since it
1038     //     didn't see any synchronization is progress, and escapes.
1039     __ set(_thread_in_native_trans, G3_scratch);
1040     __ st(G3_scratch, thread_state);
1041     if(os::is_MP()) {
1042       if (UseMembar) {
1043         // Force this write out before the read below
1044         __ membar(Assembler::StoreLoad);
1045       } else {
1046         // Write serialization page so VM thread can do a pseudo remote membar.
1047         // We use the current thread pointer to calculate a thread specific
1048         // offset to write to within the page. This minimizes bus traffic
1049         // due to cache line collision.
1050         __ serialize_memory(G2_thread, G1_scratch, G3_scratch);
1051       }
1052     }
1053     __ load_contents(sync_state, G3_scratch);
1054     __ cmp(G3_scratch, SafepointSynchronize::_not_synchronized);
1055 
1056     Label L;
1057     Address suspend_state(G2_thread, 0, in_bytes(JavaThread::suspend_flags_offset()));
1058     __ br(Assembler::notEqual, false, Assembler::pn, L);
1059     __ delayed()->
1060       ld(suspend_state, G3_scratch);
1061     __ cmp(G3_scratch, 0);
1062     __ br(Assembler::equal, false, Assembler::pt, no_block);
1063     __ delayed()->nop();
1064     __ bind(L);
1065 
1066     // Block.  Save any potential method result value before the operation and
1067     // use a leaf call to leave the last_Java_frame setup undisturbed.
1068     save_native_result();
1069     __ call_VM_leaf(L7_thread_cache,
1070                     CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans),
1071                     G2_thread);
1072 
1073     // Restore any method result value
1074     restore_native_result();
1075     __ bind(no_block);
1076   }
1077 
1078   // Clear the frame anchor now
1079 
1080   __ reset_last_Java_frame();
1081 
1082   // Move the result handler address
1083   __ mov(Lscratch, G3_scratch);
1084   // return possible result to the outer frame
1085 #ifndef __LP64
1086   __ mov(O0, I0);
1087   __ restore(O1, G0, O1);
1088 #else
1089   __ restore(O0, G0, O0);
1090 #endif /* __LP64 */
1091 
1092   // Move result handler to expected register
1093   __ mov(G3_scratch, Lscratch);
1094 
1095   // Back in normal (native) interpreter frame. State is thread_in_native_trans
1096   // switch to thread_in_Java.
1097 
1098   __ set(_thread_in_Java, G3_scratch);
1099   __ st(G3_scratch, thread_state);
1100 
1101   // reset handle block
1102   __ ld_ptr(G2_thread, in_bytes(JavaThread::active_handles_offset()), G3_scratch);
1103   __ st_ptr(G0, G3_scratch, JNIHandleBlock::top_offset_in_bytes());
1104 
1105   // If we have an oop result store it where it will be safe for any further gc
1106   // until we return now that we've released the handle it might be protected by
1107 
1108   {
1109     Label no_oop, store_result;
1110 
1111     __ set((intptr_t)AbstractInterpreter::result_handler(T_OBJECT), G3_scratch);
1112     __ cmp(G3_scratch, Lscratch);
1113     __ brx(Assembler::notEqual, false, Assembler::pt, no_oop);
1114     __ delayed()->nop();
1115     __ addcc(G0, O0, O0);
1116     __ brx(Assembler::notZero, true, Assembler::pt, store_result);     // if result is not NULL:
1117     __ delayed()->ld_ptr(O0, 0, O0);                                   // unbox it
1118     __ mov(G0, O0);
1119 
1120     __ bind(store_result);
1121     // Store it where gc will look for it and result handler expects it.
1122     __ st_ptr(O0, FP, (frame::interpreter_frame_oop_temp_offset*wordSize) + STACK_BIAS);
1123 
1124     __ bind(no_oop);
1125 
1126   }
1127 
1128 
1129   // handle exceptions (exception handling will handle unlocking!)
1130   { Label L;
1131     Address exception_addr (G2_thread, 0, in_bytes(Thread::pending_exception_offset()));
1132 
1133     __ ld_ptr(exception_addr, Gtemp);
1134     __ tst(Gtemp);
1135     __ brx(Assembler::equal, false, Assembler::pt, L);
1136     __ delayed()->nop();
1137     // Note: This could be handled more efficiently since we know that the native
1138     //       method doesn't have an exception handler. We could directly return
1139     //       to the exception handler for the caller.
1140     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_pending_exception));
1141     __ should_not_reach_here();
1142     __ bind(L);
1143   }
1144 
1145   // JVMTI support (preserves thread register)
1146   __ notify_method_exit(true, ilgl, InterpreterMacroAssembler::NotifyJVMTI);
1147 
1148   if (synchronized) {
1149     // save and restore any potential method result value around the unlocking operation
1150     save_native_result();
1151 
1152     __ add( __ top_most_monitor(), O1);
1153     __ unlock_object(O1);
1154 
1155     restore_native_result();
1156   }
1157 
1158 #if defined(COMPILER2) && !defined(_LP64)
1159 
1160   // C2 expects long results in G1 we can't tell if we're returning to interpreted
1161   // or compiled so just be safe.
1162 
1163   __ sllx(O0, 32, G1);          // Shift bits into high G1
1164   __ srl (O1, 0, O1);           // Zero extend O1
1165   __ or3 (O1, G1, G1);          // OR 64 bits into G1
1166 
1167 #endif /* COMPILER2 && !_LP64 */
1168 
1169   // dispose of return address and remove activation
1170 #ifdef ASSERT
1171   {
1172     Label ok;
1173     __ cmp(I5_savedSP, FP);
1174     __ brx(Assembler::greaterEqualUnsigned, false, Assembler::pt, ok);
1175     __ delayed()->nop();
1176     __ stop("bad I5_savedSP value");
1177     __ should_not_reach_here();
1178     __ bind(ok);
1179   }
1180 #endif
1181   if (TraceJumps) {
1182     // Move target to register that is recordable
1183     __ mov(Lscratch, G3_scratch);
1184     __ JMP(G3_scratch, 0);
1185   } else {
1186     __ jmp(Lscratch, 0);
1187   }
1188   __ delayed()->nop();
1189 
1190 
1191   if (inc_counter) {
1192     // handle invocation counter overflow
1193     __ bind(invocation_counter_overflow);
1194     generate_counter_overflow(Lcontinue);
1195   }
1196 
1197 
1198 
1199   return entry;
1200 }
1201 
1202 
1203 // Generic method entry to (asm) interpreter
1204 //------------------------------------------------------------------------------------------------------------------------
1205 //
1206 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
1207   address entry = __ pc();
1208 
1209   bool inc_counter  = UseCompiler || CountCompiledCalls;
1210 
1211   // the following temporary registers are used during frame creation
1212   const Register Gtmp1 = G3_scratch ;
1213   const Register Gtmp2 = G1_scratch;
1214 
1215   // make sure registers are different!
1216   assert_different_registers(G2_thread, G5_method, Gargs, Gtmp1, Gtmp2);
1217 
1218   const Address size_of_parameters(G5_method, 0, in_bytes(methodOopDesc::size_of_parameters_offset()));
1219   const Address size_of_locals    (G5_method, 0, in_bytes(methodOopDesc::size_of_locals_offset()));
1220   // Seems like G5_method is live at the point this is used. So we could make this look consistent
1221   // and use in the asserts.
1222   const Address access_flags      (Lmethod, 0, in_bytes(methodOopDesc::access_flags_offset()));
1223 
1224   __ verify_oop(G5_method);
1225 
1226   const Register Glocals_size = G3;
1227   assert_different_registers(Glocals_size, G4_scratch, Gframe_size);
1228 
1229   // make sure method is not native & not abstract
1230   // rethink these assertions - they can be simplified and shared (gri 2/25/2000)
1231 #ifdef ASSERT
1232   __ ld(G5_method, in_bytes(methodOopDesc::access_flags_offset()), Gtmp1);
1233   {
1234     Label L;
1235     __ btst(JVM_ACC_NATIVE, Gtmp1);
1236     __ br(Assembler::zero, false, Assembler::pt, L);
1237     __ delayed()->nop();
1238     __ stop("tried to execute native method as non-native");
1239     __ bind(L);
1240   }
1241   { Label L;
1242     __ btst(JVM_ACC_ABSTRACT, Gtmp1);
1243     __ br(Assembler::zero, false, Assembler::pt, L);
1244     __ delayed()->nop();
1245     __ stop("tried to execute abstract method as non-abstract");
1246     __ bind(L);
1247   }
1248 #endif // ASSERT
1249 
1250   // generate the code to allocate the interpreter stack frame
1251 
1252   generate_fixed_frame(false);
1253 
1254 #ifdef FAST_DISPATCH
1255   __ set((intptr_t)Interpreter::dispatch_table(), IdispatchTables);
1256                                           // set bytecode dispatch table base
1257 #endif
1258 
1259   //
1260   // Code to initialize the extra (i.e. non-parm) locals
1261   //
1262   Register init_value = noreg;    // will be G0 if we must clear locals
1263   // The way the code was setup before zerolocals was always true for vanilla java entries.
1264   // It could only be false for the specialized entries like accessor or empty which have
1265   // no extra locals so the testing was a waste of time and the extra locals were always
1266   // initialized. We removed this extra complication to already over complicated code.
1267 
1268   init_value = G0;
1269   Label clear_loop;
1270 
1271   // NOTE: If you change the frame layout, this code will need to
1272   // be updated!
1273   __ lduh( size_of_locals, O2 );
1274   __ lduh( size_of_parameters, O1 );
1275   __ sll( O2, Interpreter::logStackElementSize(), O2);
1276   __ sll( O1, Interpreter::logStackElementSize(), O1 );
1277   __ sub( Llocals, O2, O2 );
1278   __ sub( Llocals, O1, O1 );
1279 
1280   __ bind( clear_loop );
1281   __ inc( O2, wordSize );
1282 
1283   __ cmp( O2, O1 );
1284   __ brx( Assembler::lessEqualUnsigned, true, Assembler::pt, clear_loop );
1285   __ delayed()->st_ptr( init_value, O2, 0 );
1286 
1287   const Address do_not_unlock_if_synchronized(G2_thread, 0,
1288         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1289   // Since at this point in the method invocation the exception handler
1290   // would try to exit the monitor of synchronized methods which hasn't
1291   // been entered yet, we set the thread local variable
1292   // _do_not_unlock_if_synchronized to true. If any exception was thrown by
1293   // runtime, exception handling i.e. unlock_if_synchronized_method will
1294   // check this thread local flag.
1295   __ movbool(true, G3_scratch);
1296   __ stbool(G3_scratch, do_not_unlock_if_synchronized);
1297 
1298   // increment invocation counter and check for overflow
1299   //
1300   // Note: checking for negative value instead of overflow
1301   //       so we have a 'sticky' overflow test (may be of
1302   //       importance as soon as we have true MT/MP)
1303   Label invocation_counter_overflow;
1304   Label profile_method;
1305   Label profile_method_continue;
1306   Label Lcontinue;
1307   if (inc_counter) {
1308     generate_counter_incr(&invocation_counter_overflow, &profile_method, &profile_method_continue);
1309     if (ProfileInterpreter) {
1310       __ bind(profile_method_continue);
1311     }
1312   }
1313   __ bind(Lcontinue);
1314 
1315   bang_stack_shadow_pages(false);
1316 
1317   // reset the _do_not_unlock_if_synchronized flag
1318   __ stbool(G0, do_not_unlock_if_synchronized);
1319 
1320   // check for synchronized methods
1321   // Must happen AFTER invocation_counter check and stack overflow check,
1322   // so method is not locked if overflows.
1323 
1324   if (synchronized) {
1325     lock_method();
1326   } else {
1327 #ifdef ASSERT
1328     { Label ok;
1329       __ ld(access_flags, O0);
1330       __ btst(JVM_ACC_SYNCHRONIZED, O0);
1331       __ br( Assembler::zero, false, Assembler::pt, ok);
1332       __ delayed()->nop();
1333       __ stop("method needs synchronization");
1334       __ bind(ok);
1335     }
1336 #endif // ASSERT
1337   }
1338 
1339   // start execution
1340 
1341   __ verify_thread();
1342 
1343   // jvmti support
1344   __ notify_method_entry();
1345 
1346   // start executing instructions
1347   __ dispatch_next(vtos);
1348 
1349 
1350   if (inc_counter) {
1351     if (ProfileInterpreter) {
1352       // We have decided to profile this method in the interpreter
1353       __ bind(profile_method);
1354 
1355       __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::profile_method), Lbcp, true);
1356 
1357 #ifdef ASSERT
1358       __ tst(O0);
1359       __ breakpoint_trap(Assembler::notEqual);
1360 #endif
1361 
1362       __ set_method_data_pointer();
1363 
1364       __ ba(false, profile_method_continue);
1365       __ delayed()->nop();
1366     }
1367 
1368     // handle invocation counter overflow
1369     __ bind(invocation_counter_overflow);
1370     generate_counter_overflow(Lcontinue);
1371   }
1372 
1373 
1374   return entry;
1375 }
1376 
1377 
1378 //----------------------------------------------------------------------------------------------------
1379 // Entry points & stack frame layout
1380 //
1381 // Here we generate the various kind of entries into the interpreter.
1382 // The two main entry type are generic bytecode methods and native call method.
1383 // These both come in synchronized and non-synchronized versions but the
1384 // frame layout they create is very similar. The other method entry
1385 // types are really just special purpose entries that are really entry
1386 // and interpretation all in one. These are for trivial methods like
1387 // accessor, empty, or special math methods.
1388 //
1389 // When control flow reaches any of the entry types for the interpreter
1390 // the following holds ->
1391 //
1392 // C2 Calling Conventions:
1393 //
1394 // The entry code below assumes that the following registers are set
1395 // when coming in:
1396 //    G5_method: holds the methodOop of the method to call
1397 //    Lesp:    points to the TOS of the callers expression stack
1398 //             after having pushed all the parameters
1399 //
1400 // The entry code does the following to setup an interpreter frame
1401 //   pop parameters from the callers stack by adjusting Lesp
1402 //   set O0 to Lesp
1403 //   compute X = (max_locals - num_parameters)
1404 //   bump SP up by X to accomadate the extra locals
1405 //   compute X = max_expression_stack
1406 //               + vm_local_words
1407 //               + 16 words of register save area
1408 //   save frame doing a save sp, -X, sp growing towards lower addresses
1409 //   set Lbcp, Lmethod, LcpoolCache
1410 //   set Llocals to i0
1411 //   set Lmonitors to FP - rounded_vm_local_words
1412 //   set Lesp to Lmonitors - 4
1413 //
1414 //  The frame has now been setup to do the rest of the entry code
1415 
1416 // Try this optimization:  Most method entries could live in a
1417 // "one size fits all" stack frame without all the dynamic size
1418 // calculations.  It might be profitable to do all this calculation
1419 // statically and approximately for "small enough" methods.
1420 
1421 //-----------------------------------------------------------------------------------------------
1422 
1423 // C1 Calling conventions
1424 //
1425 // Upon method entry, the following registers are setup:
1426 //
1427 // g2 G2_thread: current thread
1428 // g5 G5_method: method to activate
1429 // g4 Gargs  : pointer to last argument
1430 //
1431 //
1432 // Stack:
1433 //
1434 // +---------------+ <--- sp
1435 // |               |
1436 // : reg save area :
1437 // |               |
1438 // +---------------+ <--- sp + 0x40
1439 // |               |
1440 // : extra 7 slots :      note: these slots are not really needed for the interpreter (fix later)
1441 // |               |
1442 // +---------------+ <--- sp + 0x5c
1443 // |               |
1444 // :     free      :
1445 // |               |
1446 // +---------------+ <--- Gargs
1447 // |               |
1448 // :   arguments   :
1449 // |               |
1450 // +---------------+
1451 // |               |
1452 //
1453 //
1454 //
1455 // AFTER FRAME HAS BEEN SETUP for method interpretation the stack looks like:
1456 //
1457 // +---------------+ <--- sp
1458 // |               |
1459 // : reg save area :
1460 // |               |
1461 // +---------------+ <--- sp + 0x40
1462 // |               |
1463 // : extra 7 slots :      note: these slots are not really needed for the interpreter (fix later)
1464 // |               |
1465 // +---------------+ <--- sp + 0x5c
1466 // |               |
1467 // :               :
1468 // |               | <--- Lesp
1469 // +---------------+ <--- Lmonitors (fp - 0x18)
1470 // |   VM locals   |
1471 // +---------------+ <--- fp
1472 // |               |
1473 // : reg save area :
1474 // |               |
1475 // +---------------+ <--- fp + 0x40
1476 // |               |
1477 // : extra 7 slots :      note: these slots are not really needed for the interpreter (fix later)
1478 // |               |
1479 // +---------------+ <--- fp + 0x5c
1480 // |               |
1481 // :     free      :
1482 // |               |
1483 // +---------------+
1484 // |               |
1485 // : nonarg locals :
1486 // |               |
1487 // +---------------+
1488 // |               |
1489 // :   arguments   :
1490 // |               | <--- Llocals
1491 // +---------------+ <--- Gargs
1492 // |               |
1493 
1494 static int size_activation_helper(int callee_extra_locals, int max_stack, int monitor_size) {
1495 
1496   // Figure out the size of an interpreter frame (in words) given that we have a fully allocated
1497   // expression stack, the callee will have callee_extra_locals (so we can account for
1498   // frame extension) and monitor_size for monitors. Basically we need to calculate
1499   // this exactly like generate_fixed_frame/generate_compute_interpreter_state.
1500   //
1501   //
1502   // The big complicating thing here is that we must ensure that the stack stays properly
1503   // aligned. This would be even uglier if monitor size wasn't modulo what the stack
1504   // needs to be aligned for). We are given that the sp (fp) is already aligned by
1505   // the caller so we must ensure that it is properly aligned for our callee.
1506   //
1507   const int rounded_vm_local_words =
1508        round_to(frame::interpreter_frame_vm_local_words,WordsPerLong);
1509   // callee_locals and max_stack are counts, not the size in frame.
1510   const int locals_size =
1511        round_to(callee_extra_locals * Interpreter::stackElementWords(), WordsPerLong);
1512   const int extra_stack = methodOopDesc::extra_stack();
1513   const int max_stack_words = (max_stack + extra_stack) * Interpreter::stackElementWords();
1514   return (round_to((max_stack_words
1515                    + rounded_vm_local_words
1516                    + frame::memory_parameter_word_sp_offset), WordsPerLong)
1517                    // already rounded
1518                    + locals_size + monitor_size);
1519 }
1520 
1521 // How much stack a method top interpreter activation needs in words.
1522 int AbstractInterpreter::size_top_interpreter_activation(methodOop method) {
1523 
1524   // See call_stub code
1525   int call_stub_size  = round_to(7 + frame::memory_parameter_word_sp_offset,
1526                                  WordsPerLong);    // 7 + register save area
1527 
1528   // Save space for one monitor to get into the interpreted method in case
1529   // the method is synchronized
1530   int monitor_size    = method->is_synchronized() ?
1531                                 1*frame::interpreter_frame_monitor_size() : 0;
1532   return size_activation_helper(method->max_locals(), method->max_stack(),
1533                                  monitor_size) + call_stub_size;
1534 }
1535 
1536 int AbstractInterpreter::layout_activation(methodOop method,
1537                                            int tempcount,
1538                                            int popframe_extra_args,
1539                                            int moncount,
1540                                            int callee_param_count,
1541                                            int callee_local_count,
1542                                            frame* caller,
1543                                            frame* interpreter_frame,
1544                                            bool is_top_frame) {
1545   // Note: This calculation must exactly parallel the frame setup
1546   // in InterpreterGenerator::generate_fixed_frame.
1547   // If f!=NULL, set up the following variables:
1548   //   - Lmethod
1549   //   - Llocals
1550   //   - Lmonitors (to the indicated number of monitors)
1551   //   - Lesp (to the indicated number of temps)
1552   // The frame f (if not NULL) on entry is a description of the caller of the frame
1553   // we are about to layout. We are guaranteed that we will be able to fill in a
1554   // new interpreter frame as its callee (i.e. the stack space is allocated and
1555   // the amount was determined by an earlier call to this method with f == NULL).
1556   // On return f (if not NULL) while describe the interpreter frame we just layed out.
1557 
1558   int monitor_size           = moncount * frame::interpreter_frame_monitor_size();
1559   int rounded_vm_local_words = round_to(frame::interpreter_frame_vm_local_words,WordsPerLong);
1560 
1561   assert(monitor_size == round_to(monitor_size, WordsPerLong), "must align");
1562   //
1563   // Note: if you look closely this appears to be doing something much different
1564   // than generate_fixed_frame. What is happening is this. On sparc we have to do
1565   // this dance with interpreter_sp_adjustment because the window save area would
1566   // appear just below the bottom (tos) of the caller's java expression stack. Because
1567   // the interpreter want to have the locals completely contiguous generate_fixed_frame
1568   // will adjust the caller's sp for the "extra locals" (max_locals - parameter_size).
1569   // Now in generate_fixed_frame the extension of the caller's sp happens in the callee.
1570   // In this code the opposite occurs the caller adjusts it's own stack base on the callee.
1571   // This is mostly ok but it does cause a problem when we get to the initial frame (the oldest)
1572   // because the oldest frame would have adjust its callers frame and yet that frame
1573   // already exists and isn't part of this array of frames we are unpacking. So at first
1574   // glance this would seem to mess up that frame. However Deoptimization::fetch_unroll_info_helper()
1575   // will after it calculates all of the frame's on_stack_size()'s will then figure out the
1576   // amount to adjust the caller of the initial (oldest) frame and the calculation will all
1577   // add up. It does seem like it simpler to account for the adjustment here (and remove the
1578   // callee... parameters here). However this would mean that this routine would have to take
1579   // the caller frame as input so we could adjust its sp (and set it's interpreter_sp_adjustment)
1580   // and run the calling loop in the reverse order. This would also would appear to mean making
1581   // this code aware of what the interactions are when that initial caller fram was an osr or
1582   // other adapter frame. deoptimization is complicated enough and  hard enough to debug that
1583   // there is no sense in messing working code.
1584   //
1585 
1586   int rounded_cls = round_to((callee_local_count - callee_param_count), WordsPerLong);
1587   assert(rounded_cls == round_to(rounded_cls, WordsPerLong), "must align");
1588 
1589   int raw_frame_size = size_activation_helper(rounded_cls, method->max_stack(),
1590                                               monitor_size);
1591 
1592   if (interpreter_frame != NULL) {
1593     // The skeleton frame must already look like an interpreter frame
1594     // even if not fully filled out.
1595     assert(interpreter_frame->is_interpreted_frame(), "Must be interpreted frame");
1596 
1597     intptr_t* fp = interpreter_frame->fp();
1598 
1599     JavaThread* thread = JavaThread::current();
1600     RegisterMap map(thread, false);
1601     // More verification that skeleton frame is properly walkable
1602     assert(fp == caller->sp(), "fp must match");
1603 
1604     intptr_t* montop     = fp - rounded_vm_local_words;
1605 
1606     // preallocate monitors (cf. __ add_monitor_to_stack)
1607     intptr_t* monitors = montop - monitor_size;
1608 
1609     // preallocate stack space
1610     intptr_t*  esp = monitors - 1 -
1611                      (tempcount * Interpreter::stackElementWords()) -
1612                      popframe_extra_args;
1613 
1614     int local_words = method->max_locals() * Interpreter::stackElementWords();
1615     int parm_words  = method->size_of_parameters() * Interpreter::stackElementWords();
1616     NEEDS_CLEANUP;
1617     intptr_t* locals;
1618     if (caller->is_interpreted_frame()) {
1619       // Can force the locals area to end up properly overlapping the top of the expression stack.
1620       intptr_t* Lesp_ptr = caller->interpreter_frame_tos_address() - 1;
1621       // Note that this computation means we replace size_of_parameters() values from the caller
1622       // interpreter frame's expression stack with our argument locals
1623       locals = Lesp_ptr + parm_words;
1624       int delta = local_words - parm_words;
1625       int computed_sp_adjustment = (delta > 0) ? round_to(delta, WordsPerLong) : 0;
1626       *interpreter_frame->register_addr(I5_savedSP)    = (intptr_t) (fp + computed_sp_adjustment) - STACK_BIAS;
1627     } else {
1628       assert(caller->is_compiled_frame() || caller->is_entry_frame(), "only possible cases");
1629       // Don't have Lesp available; lay out locals block in the caller
1630       // adjacent to the register window save area.
1631       //
1632       // Compiled frames do not allocate a varargs area which is why this if
1633       // statement is needed.
1634       //
1635       if (caller->is_compiled_frame()) {
1636         locals = fp + frame::register_save_words + local_words - 1;
1637       } else {
1638         locals = fp + frame::memory_parameter_word_sp_offset + local_words - 1;
1639       }
1640       if (!caller->is_entry_frame()) {
1641         // Caller wants his own SP back
1642         int caller_frame_size = caller->cb()->frame_size();
1643         *interpreter_frame->register_addr(I5_savedSP) = (intptr_t)(caller->fp() - caller_frame_size) - STACK_BIAS;
1644       }
1645     }
1646     if (TraceDeoptimization) {
1647       if (caller->is_entry_frame()) {
1648         // make sure I5_savedSP and the entry frames notion of saved SP
1649         // agree.  This assertion duplicate a check in entry frame code
1650         // but catches the failure earlier.
1651         assert(*caller->register_addr(Lscratch) == *interpreter_frame->register_addr(I5_savedSP),
1652                "would change callers SP");
1653       }
1654       if (caller->is_entry_frame()) {
1655         tty->print("entry ");
1656       }
1657       if (caller->is_compiled_frame()) {
1658         tty->print("compiled ");
1659         if (caller->is_deoptimized_frame()) {
1660           tty->print("(deopt) ");
1661         }
1662       }
1663       if (caller->is_interpreted_frame()) {
1664         tty->print("interpreted ");
1665       }
1666       tty->print_cr("caller fp=0x%x sp=0x%x", caller->fp(), caller->sp());
1667       tty->print_cr("save area = 0x%x, 0x%x", caller->sp(), caller->sp() + 16);
1668       tty->print_cr("save area = 0x%x, 0x%x", caller->fp(), caller->fp() + 16);
1669       tty->print_cr("interpreter fp=0x%x sp=0x%x", interpreter_frame->fp(), interpreter_frame->sp());
1670       tty->print_cr("save area = 0x%x, 0x%x", interpreter_frame->sp(), interpreter_frame->sp() + 16);
1671       tty->print_cr("save area = 0x%x, 0x%x", interpreter_frame->fp(), interpreter_frame->fp() + 16);
1672       tty->print_cr("Llocals = 0x%x", locals);
1673       tty->print_cr("Lesp = 0x%x", esp);
1674       tty->print_cr("Lmonitors = 0x%x", monitors);
1675     }
1676 
1677     if (method->max_locals() > 0) {
1678       assert(locals < caller->sp() || locals >= (caller->sp() + 16), "locals in save area");
1679       assert(locals < caller->fp() || locals > (caller->fp() + 16), "locals in save area");
1680       assert(locals < interpreter_frame->sp() || locals > (interpreter_frame->sp() + 16), "locals in save area");
1681       assert(locals < interpreter_frame->fp() || locals >= (interpreter_frame->fp() + 16), "locals in save area");
1682     }
1683 #ifdef _LP64
1684     assert(*interpreter_frame->register_addr(I5_savedSP) & 1, "must be odd");
1685 #endif
1686 
1687     *interpreter_frame->register_addr(Lmethod)     = (intptr_t) method;
1688     *interpreter_frame->register_addr(Llocals)     = (intptr_t) locals;
1689     *interpreter_frame->register_addr(Lmonitors)   = (intptr_t) monitors;
1690     *interpreter_frame->register_addr(Lesp)        = (intptr_t) esp;
1691     // Llast_SP will be same as SP as there is no adapter space
1692     *interpreter_frame->register_addr(Llast_SP)    = (intptr_t) interpreter_frame->sp() - STACK_BIAS;
1693     *interpreter_frame->register_addr(LcpoolCache) = (intptr_t) method->constants()->cache();
1694 #ifdef FAST_DISPATCH
1695     *interpreter_frame->register_addr(IdispatchTables) = (intptr_t) Interpreter::dispatch_table();
1696 #endif
1697 
1698 
1699 #ifdef ASSERT
1700     BasicObjectLock* mp = (BasicObjectLock*)monitors;
1701 
1702     assert(interpreter_frame->interpreter_frame_method() == method, "method matches");
1703     assert(interpreter_frame->interpreter_frame_local_at(9) == (intptr_t *)((intptr_t)locals - (9 * Interpreter::stackElementSize())+Interpreter::value_offset_in_bytes()), "locals match");
1704     assert(interpreter_frame->interpreter_frame_monitor_end()   == mp, "monitor_end matches");
1705     assert(((intptr_t *)interpreter_frame->interpreter_frame_monitor_begin()) == ((intptr_t *)mp)+monitor_size, "monitor_begin matches");
1706     assert(interpreter_frame->interpreter_frame_tos_address()-1 == esp, "esp matches");
1707 
1708     // check bounds
1709     intptr_t* lo = interpreter_frame->sp() + (frame::memory_parameter_word_sp_offset - 1);
1710     intptr_t* hi = interpreter_frame->fp() - rounded_vm_local_words;
1711     assert(lo < monitors && montop <= hi, "monitors in bounds");
1712     assert(lo <= esp && esp < monitors, "esp in bounds");
1713 #endif // ASSERT
1714   }
1715 
1716   return raw_frame_size;
1717 }
1718 
1719 //----------------------------------------------------------------------------------------------------
1720 // Exceptions
1721 void TemplateInterpreterGenerator::generate_throw_exception() {
1722 
1723   // Entry point in previous activation (i.e., if the caller was interpreted)
1724   Interpreter::_rethrow_exception_entry = __ pc();
1725   // O0: exception
1726 
1727   // entry point for exceptions thrown within interpreter code
1728   Interpreter::_throw_exception_entry = __ pc();
1729   __ verify_thread();
1730   // expression stack is undefined here
1731   // O0: exception, i.e. Oexception
1732   // Lbcp: exception bcx
1733   __ verify_oop(Oexception);
1734 
1735 
1736   // expression stack must be empty before entering the VM in case of an exception
1737   __ empty_expression_stack();
1738   // find exception handler address and preserve exception oop
1739   // call C routine to find handler and jump to it
1740   __ call_VM(O1, CAST_FROM_FN_PTR(address, InterpreterRuntime::exception_handler_for_exception), Oexception);
1741   __ push_ptr(O1); // push exception for exception handler bytecodes
1742 
1743   __ JMP(O0, 0); // jump to exception handler (may be remove activation entry!)
1744   __ delayed()->nop();
1745 
1746 
1747   // if the exception is not handled in the current frame
1748   // the frame is removed and the exception is rethrown
1749   // (i.e. exception continuation is _rethrow_exception)
1750   //
1751   // Note: At this point the bci is still the bxi for the instruction which caused
1752   //       the exception and the expression stack is empty. Thus, for any VM calls
1753   //       at this point, GC will find a legal oop map (with empty expression stack).
1754 
1755   // in current activation
1756   // tos: exception
1757   // Lbcp: exception bcp
1758 
1759   //
1760   // JVMTI PopFrame support
1761   //
1762 
1763   Interpreter::_remove_activation_preserving_args_entry = __ pc();
1764   Address popframe_condition_addr (G2_thread, 0, in_bytes(JavaThread::popframe_condition_offset()));
1765   // Set the popframe_processing bit in popframe_condition indicating that we are
1766   // currently handling popframe, so that call_VMs that may happen later do not trigger new
1767   // popframe handling cycles.
1768 
1769   __ ld(popframe_condition_addr, G3_scratch);
1770   __ or3(G3_scratch, JavaThread::popframe_processing_bit, G3_scratch);
1771   __ stw(G3_scratch, popframe_condition_addr);
1772 
1773   // Empty the expression stack, as in normal exception handling
1774   __ empty_expression_stack();
1775   __ unlock_if_synchronized_method(vtos, /* throw_monitor_exception */ false, /* install_monitor_exception */ false);
1776 
1777   {
1778     // Check to see whether we are returning to a deoptimized frame.
1779     // (The PopFrame call ensures that the caller of the popped frame is
1780     // either interpreted or compiled and deoptimizes it if compiled.)
1781     // In this case, we can't call dispatch_next() after the frame is
1782     // popped, but instead must save the incoming arguments and restore
1783     // them after deoptimization has occurred.
1784     //
1785     // Note that we don't compare the return PC against the
1786     // deoptimization blob's unpack entry because of the presence of
1787     // adapter frames in C2.
1788     Label caller_not_deoptimized;
1789     __ call_VM_leaf(L7_thread_cache, CAST_FROM_FN_PTR(address, InterpreterRuntime::interpreter_contains), I7);
1790     __ tst(O0);
1791     __ brx(Assembler::notEqual, false, Assembler::pt, caller_not_deoptimized);
1792     __ delayed()->nop();
1793 
1794     const Register Gtmp1 = G3_scratch;
1795     const Register Gtmp2 = G1_scratch;
1796 
1797     // Compute size of arguments for saving when returning to deoptimized caller
1798     __ lduh(Lmethod, in_bytes(methodOopDesc::size_of_parameters_offset()), Gtmp1);
1799     __ sll(Gtmp1, Interpreter::logStackElementSize(), Gtmp1);
1800     __ sub(Llocals, Gtmp1, Gtmp2);
1801     __ add(Gtmp2, wordSize, Gtmp2);
1802     // Save these arguments
1803     __ call_VM_leaf(L7_thread_cache, CAST_FROM_FN_PTR(address, Deoptimization::popframe_preserve_args), G2_thread, Gtmp1, Gtmp2);
1804     // Inform deoptimization that it is responsible for restoring these arguments
1805     __ set(JavaThread::popframe_force_deopt_reexecution_bit, Gtmp1);
1806     Address popframe_condition_addr(G2_thread, 0, in_bytes(JavaThread::popframe_condition_offset()));
1807     __ st(Gtmp1, popframe_condition_addr);
1808 
1809     // Return from the current method
1810     // The caller's SP was adjusted upon method entry to accomodate
1811     // the callee's non-argument locals. Undo that adjustment.
1812     __ ret();
1813     __ delayed()->restore(I5_savedSP, G0, SP);
1814 
1815     __ bind(caller_not_deoptimized);
1816   }
1817 
1818   // Clear the popframe condition flag
1819   __ stw(G0 /* popframe_inactive */, popframe_condition_addr);
1820 
1821   // Get out of the current method (how this is done depends on the particular compiler calling
1822   // convention that the interpreter currently follows)
1823   // The caller's SP was adjusted upon method entry to accomodate
1824   // the callee's non-argument locals. Undo that adjustment.
1825   __ restore(I5_savedSP, G0, SP);
1826   // The method data pointer was incremented already during
1827   // call profiling. We have to restore the mdp for the current bcp.
1828   if (ProfileInterpreter) {
1829     __ set_method_data_pointer_for_bcp();
1830   }
1831   // Resume bytecode interpretation at the current bcp
1832   __ dispatch_next(vtos);
1833   // end of JVMTI PopFrame support
1834 
1835   Interpreter::_remove_activation_entry = __ pc();
1836 
1837   // preserve exception over this code sequence (remove activation calls the vm, but oopmaps are not correct here)
1838   __ pop_ptr(Oexception);                                  // get exception
1839 
1840   // Intel has the following comment:
1841   //// remove the activation (without doing throws on illegalMonitorExceptions)
1842   // They remove the activation without checking for bad monitor state.
1843   // %%% We should make sure this is the right semantics before implementing.
1844 
1845   // %%% changed set_vm_result_2 to set_vm_result and get_vm_result_2 to get_vm_result. Is there a bug here?
1846   __ set_vm_result(Oexception);
1847   __ unlock_if_synchronized_method(vtos, /* throw_monitor_exception */ false);
1848 
1849   __ notify_method_exit(false, vtos, InterpreterMacroAssembler::SkipNotifyJVMTI);
1850 
1851   __ get_vm_result(Oexception);
1852   __ verify_oop(Oexception);
1853 
1854     const int return_reg_adjustment = frame::pc_return_offset;
1855   Address issuing_pc_addr(I7, 0, return_reg_adjustment);
1856 
1857   // We are done with this activation frame; find out where to go next.
1858   // The continuation point will be an exception handler, which expects
1859   // the following registers set up:
1860   //
1861   // Oexception: exception
1862   // Oissuing_pc: the local call that threw exception
1863   // Other On: garbage
1864   // In/Ln:  the contents of the caller's register window
1865   //
1866   // We do the required restore at the last possible moment, because we
1867   // need to preserve some state across a runtime call.
1868   // (Remember that the caller activation is unknown--it might not be
1869   // interpreted, so things like Lscratch are useless in the caller.)
1870 
1871   // Although the Intel version uses call_C, we can use the more
1872   // compact call_VM.  (The only real difference on SPARC is a
1873   // harmlessly ignored [re]set_last_Java_frame, compared with
1874   // the Intel code which lacks this.)
1875   __ mov(Oexception,      Oexception ->after_save());  // get exception in I0 so it will be on O0 after restore
1876   __ add(issuing_pc_addr, Oissuing_pc->after_save());  // likewise set I1 to a value local to the caller
1877   __ super_call_VM_leaf(L7_thread_cache,
1878                         CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address),
1879                         Oissuing_pc->after_save());
1880 
1881   // The caller's SP was adjusted upon method entry to accomodate
1882   // the callee's non-argument locals. Undo that adjustment.
1883   __ JMP(O0, 0);                         // return exception handler in caller
1884   __ delayed()->restore(I5_savedSP, G0, SP);
1885 
1886   // (same old exception object is already in Oexception; see above)
1887   // Note that an "issuing PC" is actually the next PC after the call
1888 }
1889 
1890 
1891 //
1892 // JVMTI ForceEarlyReturn support
1893 //
1894 
1895 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1896   address entry = __ pc();
1897 
1898   __ empty_expression_stack();
1899   __ load_earlyret_value(state);
1900 
1901   __ ld_ptr(Address(G2_thread, 0, in_bytes(JavaThread::jvmti_thread_state_offset())), G3_scratch);
1902   Address cond_addr(G3_scratch, 0, in_bytes(JvmtiThreadState::earlyret_state_offset()));
1903 
1904   // Clear the earlyret state
1905   __ stw(G0 /* JvmtiThreadState::earlyret_inactive */, cond_addr);
1906 
1907   __ remove_activation(state,
1908                        /* throw_monitor_exception */ false,
1909                        /* install_monitor_exception */ false);
1910 
1911   // The caller's SP was adjusted upon method entry to accomodate
1912   // the callee's non-argument locals. Undo that adjustment.
1913   __ ret();                             // return to caller
1914   __ delayed()->restore(I5_savedSP, G0, SP);
1915 
1916   return entry;
1917 } // end of JVMTI ForceEarlyReturn support
1918 
1919 
1920 //------------------------------------------------------------------------------------------------------------------------
1921 // Helper for vtos entry point generation
1922 
1923 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t, address& bep, address& cep, address& sep, address& aep, address& iep, address& lep, address& fep, address& dep, address& vep) {
1924   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1925   Label L;
1926   aep = __ pc(); __ push_ptr(); __ ba(false, L); __ delayed()->nop();
1927   fep = __ pc(); __ push_f();   __ ba(false, L); __ delayed()->nop();
1928   dep = __ pc(); __ push_d();   __ ba(false, L); __ delayed()->nop();
1929   lep = __ pc(); __ push_l();   __ ba(false, L); __ delayed()->nop();
1930   iep = __ pc(); __ push_i();
1931   bep = cep = sep = iep;                        // there aren't any
1932   vep = __ pc(); __ bind(L);                    // fall through
1933   generate_and_dispatch(t);
1934 }
1935 
1936 // --------------------------------------------------------------------------------
1937 
1938 
1939 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
1940  : TemplateInterpreterGenerator(code) {
1941    generate_all(); // down here so it can be "virtual"
1942 }
1943 
1944 // --------------------------------------------------------------------------------
1945 
1946 // Non-product code
1947 #ifndef PRODUCT
1948 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1949   address entry = __ pc();
1950 
1951   __ push(state);
1952   __ mov(O7, Lscratch); // protect return address within interpreter
1953 
1954   // Pass a 0 (not used in sparc) and the top of stack to the bytecode tracer
1955   __ mov( Otos_l2, G3_scratch );
1956   __ call_VM(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::trace_bytecode), G0, Otos_l1, G3_scratch);
1957   __ mov(Lscratch, O7); // restore return address
1958   __ pop(state);
1959   __ retl();
1960   __ delayed()->nop();
1961 
1962   return entry;
1963 }
1964 
1965 
1966 // helpers for generate_and_dispatch
1967 
1968 void TemplateInterpreterGenerator::count_bytecode() {
1969   Address c(G3_scratch, (address)&BytecodeCounter::_counter_value);
1970   __ load_contents(c, G4_scratch);
1971   __ inc(G4_scratch);
1972   __ st(G4_scratch, c);
1973 }
1974 
1975 
1976 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) {
1977   Address bucket( G3_scratch, (address) &BytecodeHistogram::_counters[t->bytecode()] );
1978   __ load_contents(bucket, G4_scratch);
1979   __ inc(G4_scratch);
1980   __ st(G4_scratch, bucket);
1981 }
1982 
1983 
1984 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) {
1985   address index_addr      = (address)&BytecodePairHistogram::_index;
1986   Address index(G3_scratch, index_addr);
1987 
1988   address counters_addr   = (address)&BytecodePairHistogram::_counters;
1989   Address counters(G3_scratch, counters_addr);
1990 
1991   // get index, shift out old bytecode, bring in new bytecode, and store it
1992   // _index = (_index >> log2_number_of_codes) |
1993   //          (bytecode << log2_number_of_codes);
1994 
1995 
1996   __ load_contents( index,      G4_scratch );
1997   __ srl( G4_scratch, BytecodePairHistogram::log2_number_of_codes, G4_scratch );
1998   __ set( ((int)t->bytecode()) << BytecodePairHistogram::log2_number_of_codes,  G3_scratch );
1999   __ or3( G3_scratch,  G4_scratch, G4_scratch );
2000   __ store_contents( G4_scratch, index );
2001 
2002   // bump bucket contents
2003   // _counters[_index] ++;
2004 
2005   __ load_address( counters );  // loads into G3_scratch
2006   __ sll( G4_scratch, LogBytesPerWord, G4_scratch );  // Index is word address
2007   __ add (G3_scratch, G4_scratch, G3_scratch);        // Add in index
2008   __ ld (G3_scratch, 0, G4_scratch);
2009   __ inc (G4_scratch);
2010   __ st (G4_scratch, 0, G3_scratch);
2011 }
2012 
2013 
2014 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
2015   // Call a little run-time stub to avoid blow-up for each bytecode.
2016   // The run-time runtime saves the right registers, depending on
2017   // the tosca in-state for the given template.
2018   address entry = Interpreter::trace_code(t->tos_in());
2019   guarantee(entry != NULL, "entry must have been generated");
2020   __ call(entry, relocInfo::none);
2021   __ delayed()->nop();
2022 }
2023 
2024 
2025 void TemplateInterpreterGenerator::stop_interpreter_at() {
2026   Address counter(G3_scratch , (address)&BytecodeCounter::_counter_value);
2027   __ load_contents    (counter, G3_scratch );
2028   Address stop_at(G4_scratch, (address)&StopInterpreterAt);
2029   __ load_ptr_contents(stop_at, G4_scratch);
2030   __ cmp(G3_scratch, G4_scratch);
2031   __ breakpoint_trap(Assembler::equal);
2032 }
2033 #endif // not PRODUCT
2034 #endif // !CC_INTERP