1 /*
   2  * Copyright 1999-2007 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/_c1_Runtime1_sparc.cpp.incl"
  27 
  28 // Implementation of StubAssembler
  29 
  30 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry_point, int number_of_arguments) {
  31   // for sparc changing the number of arguments doesn't change
  32   // anything about the frame size so we'll always lie and claim that
  33   // we are only passing 1 argument.
  34   set_num_rt_args(1);
  35 
  36   assert_not_delayed();
  37   // bang stack before going to runtime
  38   set(-os::vm_page_size() + STACK_BIAS, G3_scratch);
  39   st(G0, SP, G3_scratch);
  40 
  41   // debugging support
  42   assert(number_of_arguments >= 0   , "cannot have negative number of arguments");
  43 
  44   set_last_Java_frame(SP, noreg);
  45   if (VerifyThread)  mov(G2_thread, O0); // about to be smashed; pass early
  46   save_thread(L7_thread_cache);
  47   // do the call
  48   call(entry_point, relocInfo::runtime_call_type);
  49   if (!VerifyThread) {
  50     delayed()->mov(G2_thread, O0);  // pass thread as first argument
  51   } else {
  52     delayed()->nop();             // (thread already passed)
  53   }
  54   int call_offset = offset();  // offset of return address
  55   restore_thread(L7_thread_cache);
  56   reset_last_Java_frame();
  57 
  58   // check for pending exceptions
  59   { Label L;
  60     Address exception_addr(G2_thread, 0, in_bytes(Thread::pending_exception_offset()));
  61     ld_ptr(exception_addr, Gtemp);
  62     br_null(Gtemp, false, pt, L);
  63     delayed()->nop();
  64     Address vm_result_addr(G2_thread, 0, in_bytes(JavaThread::vm_result_offset()));
  65     st_ptr(G0, vm_result_addr);
  66     Address vm_result_addr_2(G2_thread, 0, in_bytes(JavaThread::vm_result_2_offset()));
  67     st_ptr(G0, vm_result_addr_2);
  68 
  69     if (frame_size() == no_frame_size) {
  70       // we use O7 linkage so that forward_exception_entry has the issuing PC
  71       call(StubRoutines::forward_exception_entry(), relocInfo::runtime_call_type);
  72       delayed()->restore();
  73     } else if (_stub_id == Runtime1::forward_exception_id) {
  74       should_not_reach_here();
  75     } else {
  76       Address exc(G4, Runtime1::entry_for(Runtime1::forward_exception_id));
  77       jump_to(exc, 0);
  78       delayed()->nop();
  79     }
  80     bind(L);
  81   }
  82 
  83   // get oop result if there is one and reset the value in the thread
  84   if (oop_result1->is_valid()) {                    // get oop result if there is one and reset it in the thread
  85     get_vm_result  (oop_result1);
  86   } else {
  87     // be a little paranoid and clear the result
  88     Address vm_result_addr(G2_thread, 0, in_bytes(JavaThread::vm_result_offset()));
  89     st_ptr(G0, vm_result_addr);
  90   }
  91 
  92   if (oop_result2->is_valid()) {
  93     get_vm_result_2(oop_result2);
  94   } else {
  95     // be a little paranoid and clear the result
  96     Address vm_result_addr_2(G2_thread, 0, in_bytes(JavaThread::vm_result_2_offset()));
  97     st_ptr(G0, vm_result_addr_2);
  98   }
  99 
 100   return call_offset;
 101 }
 102 
 103 
 104 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry, Register arg1) {
 105   // O0 is reserved for the thread
 106   mov(arg1, O1);
 107   return call_RT(oop_result1, oop_result2, entry, 1);
 108 }
 109 
 110 
 111 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry, Register arg1, Register arg2) {
 112   // O0 is reserved for the thread
 113   mov(arg1, O1);
 114   mov(arg2, O2); assert(arg2 != O1, "smashed argument");
 115   return call_RT(oop_result1, oop_result2, entry, 2);
 116 }
 117 
 118 
 119 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry, Register arg1, Register arg2, Register arg3) {
 120   // O0 is reserved for the thread
 121   mov(arg1, O1);
 122   mov(arg2, O2); assert(arg2 != O1,               "smashed argument");
 123   mov(arg3, O3); assert(arg3 != O1 && arg3 != O2, "smashed argument");
 124   return call_RT(oop_result1, oop_result2, entry, 3);
 125 }
 126 
 127 
 128 // Implementation of Runtime1
 129 
 130 #define __ sasm->
 131 
 132 static int cpu_reg_save_offsets[FrameMap::nof_cpu_regs];
 133 static int fpu_reg_save_offsets[FrameMap::nof_fpu_regs];
 134 static int reg_save_size_in_words;
 135 static int frame_size_in_bytes = -1;
 136 
 137 static OopMap* generate_oop_map(StubAssembler* sasm, bool save_fpu_registers) {
 138   assert(frame_size_in_bytes == __ total_frame_size_in_bytes(reg_save_size_in_words),
 139          " mismatch in calculation");
 140   sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
 141   int frame_size_in_slots = frame_size_in_bytes / sizeof(jint);
 142   OopMap* oop_map = new OopMap(frame_size_in_slots, 0);
 143 
 144   int i;
 145   for (i = 0; i < FrameMap::nof_cpu_regs; i++) {
 146     Register r = as_Register(i);
 147     if (r == G1 || r == G3 || r == G4 || r == G5) {
 148       int sp_offset = cpu_reg_save_offsets[i];
 149       oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset),
 150                                 r->as_VMReg());
 151     }
 152   }
 153 
 154   if (save_fpu_registers) {
 155     for (i = 0; i < FrameMap::nof_fpu_regs; i++) {
 156       FloatRegister r = as_FloatRegister(i);
 157       int sp_offset = fpu_reg_save_offsets[i];
 158       oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset),
 159                                 r->as_VMReg());
 160     }
 161   }
 162   return oop_map;
 163 }
 164 
 165 static OopMap* save_live_registers(StubAssembler* sasm, bool save_fpu_registers = true) {
 166   assert(frame_size_in_bytes == __ total_frame_size_in_bytes(reg_save_size_in_words),
 167          " mismatch in calculation");
 168   __ save_frame_c1(frame_size_in_bytes);
 169   sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
 170 
 171   // Record volatile registers as callee-save values in an OopMap so their save locations will be
 172   // propagated to the caller frame's RegisterMap during StackFrameStream construction (needed for
 173   // deoptimization; see compiledVFrame::create_stack_value).  The caller's I, L and O registers
 174   // are saved in register windows - I's and L's in the caller's frame and O's in the stub frame
 175   // (as the stub's I's) when the runtime routine called by the stub creates its frame.
 176   // OopMap frame sizes are in c2 stack slot sizes (sizeof(jint))
 177 
 178   int i;
 179   for (i = 0; i < FrameMap::nof_cpu_regs; i++) {
 180     Register r = as_Register(i);
 181     if (r == G1 || r == G3 || r == G4 || r == G5) {
 182       int sp_offset = cpu_reg_save_offsets[i];
 183       __ st_ptr(r, SP, (sp_offset * BytesPerWord) + STACK_BIAS);
 184     }
 185   }
 186 
 187   if (save_fpu_registers) {
 188     for (i = 0; i < FrameMap::nof_fpu_regs; i++) {
 189       FloatRegister r = as_FloatRegister(i);
 190       int sp_offset = fpu_reg_save_offsets[i];
 191       __ stf(FloatRegisterImpl::S, r, SP, (sp_offset * BytesPerWord) + STACK_BIAS);
 192     }
 193   }
 194 
 195   return generate_oop_map(sasm, save_fpu_registers);
 196 }
 197 
 198 static void restore_live_registers(StubAssembler* sasm, bool restore_fpu_registers = true) {
 199   for (int i = 0; i < FrameMap::nof_cpu_regs; i++) {
 200     Register r = as_Register(i);
 201     if (r == G1 || r == G3 || r == G4 || r == G5) {
 202       __ ld_ptr(SP, (cpu_reg_save_offsets[i] * BytesPerWord) + STACK_BIAS, r);
 203     }
 204   }
 205 
 206   if (restore_fpu_registers) {
 207     for (int i = 0; i < FrameMap::nof_fpu_regs; i++) {
 208       FloatRegister r = as_FloatRegister(i);
 209       __ ldf(FloatRegisterImpl::S, SP, (fpu_reg_save_offsets[i] * BytesPerWord) + STACK_BIAS, r);
 210     }
 211   }
 212 }
 213 
 214 
 215 void Runtime1::initialize_pd() {
 216   // compute word offsets from SP at which live (non-windowed) registers are captured by stub routines
 217   //
 218   // A stub routine will have a frame that is at least large enough to hold
 219   // a register window save area (obviously) and the volatile g registers
 220   // and floating registers. A user of save_live_registers can have a frame
 221   // that has more scratch area in it (although typically they will use L-regs).
 222   // in that case the frame will look like this (stack growing down)
 223   //
 224   // FP -> |             |
 225   //       | scratch mem |
 226   //       |   "      "  |
 227   //       --------------
 228   //       | float regs  |
 229   //       |   "    "    |
 230   //       ---------------
 231   //       | G regs      |
 232   //       | "  "        |
 233   //       ---------------
 234   //       | abi reg.    |
 235   //       | window save |
 236   //       | area        |
 237   // SP -> ---------------
 238   //
 239   int i;
 240   int sp_offset = round_to(frame::register_save_words, 2); //  start doubleword aligned
 241 
 242   // only G int registers are saved explicitly; others are found in register windows
 243   for (i = 0; i < FrameMap::nof_cpu_regs; i++) {
 244     Register r = as_Register(i);
 245     if (r == G1 || r == G3 || r == G4 || r == G5) {
 246       cpu_reg_save_offsets[i] = sp_offset;
 247       sp_offset++;
 248     }
 249   }
 250 
 251   // all float registers are saved explicitly
 252   assert(FrameMap::nof_fpu_regs == 32, "double registers not handled here");
 253   for (i = 0; i < FrameMap::nof_fpu_regs; i++) {
 254     fpu_reg_save_offsets[i] = sp_offset;
 255     sp_offset++;
 256   }
 257   reg_save_size_in_words = sp_offset - frame::memory_parameter_word_sp_offset;
 258   // this should match assembler::total_frame_size_in_bytes, which
 259   // isn't callable from this context.  It's checked by an assert when
 260   // it's used though.
 261   frame_size_in_bytes = align_size_up(sp_offset * wordSize, 8);
 262 }
 263 
 264 
 265 OopMapSet* Runtime1::generate_exception_throw(StubAssembler* sasm, address target, bool has_argument) {
 266   // make a frame and preserve the caller's caller-save registers
 267   OopMap* oop_map = save_live_registers(sasm);
 268   int call_offset;
 269   if (!has_argument) {
 270     call_offset = __ call_RT(noreg, noreg, target);
 271   } else {
 272     call_offset = __ call_RT(noreg, noreg, target, G4);
 273   }
 274   OopMapSet* oop_maps = new OopMapSet();
 275   oop_maps->add_gc_map(call_offset, oop_map);
 276 
 277   __ should_not_reach_here();
 278   return oop_maps;
 279 }
 280 
 281 
 282 OopMapSet* Runtime1::generate_stub_call(StubAssembler* sasm, Register result, address target,
 283                                         Register arg1, Register arg2, Register arg3) {
 284   // make a frame and preserve the caller's caller-save registers
 285   OopMap* oop_map = save_live_registers(sasm);
 286 
 287   int call_offset;
 288   if (arg1 == noreg) {
 289     call_offset = __ call_RT(result, noreg, target);
 290   } else if (arg2 == noreg) {
 291     call_offset = __ call_RT(result, noreg, target, arg1);
 292   } else if (arg3 == noreg) {
 293     call_offset = __ call_RT(result, noreg, target, arg1, arg2);
 294   } else {
 295     call_offset = __ call_RT(result, noreg, target, arg1, arg2, arg3);
 296   }
 297   OopMapSet* oop_maps = NULL;
 298 
 299   oop_maps = new OopMapSet();
 300   oop_maps->add_gc_map(call_offset, oop_map);
 301   restore_live_registers(sasm);
 302 
 303   __ ret();
 304   __ delayed()->restore();
 305 
 306   return oop_maps;
 307 }
 308 
 309 
 310 OopMapSet* Runtime1::generate_patching(StubAssembler* sasm, address target) {
 311   // make a frame and preserve the caller's caller-save registers
 312   OopMap* oop_map = save_live_registers(sasm);
 313 
 314   // call the runtime patching routine, returns non-zero if nmethod got deopted.
 315   int call_offset = __ call_RT(noreg, noreg, target);
 316   OopMapSet* oop_maps = new OopMapSet();
 317   oop_maps->add_gc_map(call_offset, oop_map);
 318 
 319   // re-execute the patched instruction or, if the nmethod was deoptmized, return to the
 320   // deoptimization handler entry that will cause re-execution of the current bytecode
 321   DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
 322   assert(deopt_blob != NULL, "deoptimization blob must have been created");
 323 
 324   Label no_deopt;
 325   __ tst(O0);
 326   __ brx(Assembler::equal, false, Assembler::pt, no_deopt);
 327   __ delayed()->nop();
 328 
 329   // return to the deoptimization handler entry for unpacking and rexecute
 330   // if we simply returned the we'd deopt as if any call we patched had just
 331   // returned.
 332 
 333   restore_live_registers(sasm);
 334   __ restore();
 335   __ br(Assembler::always, false, Assembler::pt, deopt_blob->unpack_with_reexecution(), relocInfo::runtime_call_type);
 336   __ delayed()->nop();
 337 
 338   __ bind(no_deopt);
 339   restore_live_registers(sasm);
 340   __ ret();
 341   __ delayed()->restore();
 342 
 343   return oop_maps;
 344 }
 345 
 346 inline Address layout_helper_addr(Register klass) {
 347   return Address(klass, 0, (klassOopDesc::header_size() * HeapWordSize
 348                             + Klass::layout_helper_offset_in_bytes()));
 349 }
 350 
 351 static void compute_instance_size(Register obj_size, Register klass,
 352                                   Label& have_obj_size, Label& size_is_variable,
 353                                   Register t1,
 354                                   StubAssembler* sasm, bool hot_part) {
 355   assert_different_registers(obj_size, klass, t1);
 356 
 357   if (hot_part) {
 358     __ lduw(layout_helper_addr(klass), obj_size);
 359     __ andcc(obj_size, ~LayoutHelper::_size_low_mask, obj_size);
 360     if (mixed_arrays) {
 361       __ jcc(Assembler::negative, size_is_variable);
 362       __ bind(have_obj_size);
 363     }
 364   } else {
 365     if (MixedArrays) {
 366       // Side path for fixing up the initial size of a variable object.
 367       // Must round from int to object alignment, and clear high bits.
 368       __ bind(size_is_variable);
 369       __ add(obj_size, LayoutHelper::_header_size_odd_mask, obj_size);
 370       __ set((LayoutHelper::_header_size_mask & ~MinObjAlignmentInBytesMask), t1);
 371       __ and3(obj_size, t1, obj_size);
 372       __ jmp(have_obj_size);
 373     }
 374   }
 375 }
 376 
 377 static void compute_array_size(Register arr_size, Register klass, Register length,
 378                                Label& have_scaled_length, Label& need_multiply,
 379                                Register t1, Register t2,
 380                                StubAssembler* sasm, bool hot_part) {
 381   assert_different_registers(arr_size, klass, length, t1, t2);
 382 
 383   if (hot_part) {
 384     // get the allocation size: round_up(hdr + length << (lh>>16 & 0x1F))
 385     __ lduw(layout_helper_addr(klass), t1);
 386     // int scale = (lh >> LayoutHelper::_element_size_shift);
 387     __ mov(t1, t2);               // spill layout helper
 388     __ srl(t1, LayoutHelper::_element_size_shift);  // lh>>16, no mask needed
 389     // size_t arr_size = (array_length << scale);
 390     __ sllx(length, t1, arr_size);
 391     if (MixedArrays) {
 392       // int sizem_ip = (scale & (LayoutHelper::_element_sizem_mask_ip));
 393       __ andcc(t1, LayoutHelper::_element_sizem_mask_ip, t1);
 394       __ jcc(Assembler::notZero, need_multiply);
 395       __ bind(have_scaled_length);
 396     }
 397     // arr_size += Klass::layout_helper_header_size_in_bytes(lh);
 398     assert(LayoutHelper::_header_size_shift == 0, "");
 399     // sll(t2, LayoutHelper::_header_size_shift, t2);
 400     __ set((LayoutHelper::_header_size_mask & ~MinObjAlignmentInBytesMask), t1);
 401     __ and(t2, t1, t2);           // t2 = lh & _header_size_mask
 402     __ add(arr_size, t2, arr_size);
 403     __ add(arr_size, MinObjAlignmentInBytesMask, arr_size); // align up
 404     __ and3(arr_size, ~MinObjAlignmentInBytesMask, arr_size);
 405   } else {
 406     if (MixedArrays) {
 407       // Side path for scaling by a non-power-of-two array element size.
 408       __ bind(need_multiply);
 409       // int sizem = (sizem_ip >> LayoutHelper::_element_scale_bits);
 410       __ srl(t1, LayoutHelper::_element_scale_bits, t1);
 411       // arr_size = (array_length * (1+sizem)) << scale;
 412       __ add(t1, 1, t1);
 413       // previous value of arr_size is junk; start from length again
 414       assert(wordSize == jintSize, "else use mulx");
 415       __ smul(length, t1, arr_size);
 416       __ srl(t2,  LayoutHelper::_element_size_shift, t1);  // lh>>16, mask needed
 417       __ and3(t1, LayoutHelper::_element_scale_mask, t1);
 418       __ sllx(arr_size, t1, arr_size);
 419       __ jmp(have_scaled_length);
 420     }
 421   }
 422 }
 423 
 424 static void initialize_array_body(Register obj, Register arr_size, Register t1, Register t2, StubAssembler* sasm) {
 425   assert_different_registers(obj, arr_size, t1, t2);
 426 
 427   int min_header_size = arrayOopDesc::header_size(T_BYTE);
 428   __ sub(arr_size, min_header_size, arr_size);  // body length
 429   __ add(obj,      min_header_size, t1); // body start
 430   __ initialize_body(t1, arr_size, 0, t2);
 431 }
 432 
 433 
 434 OopMapSet* Runtime1::generate_code_for(StubID id, StubAssembler* sasm) {
 435 
 436   OopMapSet* oop_maps = NULL;
 437   // for better readability
 438   const bool must_gc_arguments = true;
 439   const bool dont_gc_arguments = false;
 440 
 441   // stub code & info for the different stubs
 442   switch (id) {
 443     case forward_exception_id:
 444       {
 445         // we're handling an exception in the context of a compiled
 446         // frame.  The registers have been saved in the standard
 447         // places.  Perform an exception lookup in the caller and
 448         // dispatch to the handler if found.  Otherwise unwind and
 449         // dispatch to the callers exception handler.
 450 
 451         oop_maps = new OopMapSet();
 452         OopMap* oop_map = generate_oop_map(sasm, true);
 453 
 454         // transfer the pending exception to the exception_oop
 455         __ ld_ptr(G2_thread, in_bytes(JavaThread::pending_exception_offset()), Oexception);
 456         __ ld_ptr(Oexception, 0, G0);
 457         __ st_ptr(G0, G2_thread, in_bytes(JavaThread::pending_exception_offset()));
 458         __ add(I7, frame::pc_return_offset, Oissuing_pc);
 459 
 460         generate_handle_exception(sasm, oop_maps, oop_map);
 461         __ should_not_reach_here();
 462       }
 463       break;
 464 
 465     case new_instance_id:
 466     case fast_new_instance_id:
 467     case fast_new_instance_init_check_id:
 468       {
 469         Register G5_klass = G5; // Incoming
 470         Register O0_obj   = O0; // Outgoing
 471 
 472         if (id == new_instance_id) {
 473           __ set_info("new_instance", dont_gc_arguments);
 474         } else if (id == fast_new_instance_id) {
 475           __ set_info("fast new_instance", dont_gc_arguments);
 476         } else {
 477           assert(id == fast_new_instance_init_check_id, "bad StubID");
 478           __ set_info("fast new_instance init check", dont_gc_arguments);
 479         }
 480 
 481         if ((id == fast_new_instance_id || id == fast_new_instance_init_check_id) &&
 482             UseTLAB && FastTLABRefill) {
 483           Label slow_path;
 484           Register G1_obj_size = G1;
 485           Register G3_t1 = G3;
 486           Register G4_t2 = G4;
 487           assert_different_registers(G5_klass, G1_obj_size, G3_t1, G4_t2);
 488 
 489           // Push a frame since we may do dtrace notification for the
 490           // allocation which requires calling out and we don't want
 491           // to stomp the real return address.
 492           __ save_frame(0);
 493 
 494           if (id == fast_new_instance_init_check_id) {
 495             // make sure the klass is initialized
 496             __ ld(G5_klass, instanceKlass::init_state_offset_in_bytes() + sizeof(oopDesc), G3_t1);
 497             __ cmp(G3_t1, instanceKlass::fully_initialized);
 498             __ br(Assembler::notEqual, false, Assembler::pn, slow_path);
 499             __ delayed()->nop();
 500           }
 501 #ifdef ASSERT
 502           // assert object can be fast path allocated
 503           {
 504             Label ok;
 505           __ ld(G5_klass, Klass::layout_helper_offset_in_bytes() + sizeof(oopDesc), G1_obj_size);
 506           __ btst(LayoutHelper::_slow_path_low_bit, G1_obj_size);
 507           __ br(Assembler::zero, false, Assembler::pn, ok);
 508           __ delayed()->nop();
 509           __ stop("assert(can be fast path allocated)");
 510           __ should_not_reach_here();
 511           __ bind(ok);
 512           }
 513 #endif // ASSERT
 514           // if we got here then the TLAB allocation failed, so try
 515           // refilling the TLAB or allocating directly from eden.
 516           Label retry_tlab, try_eden;
 517           __ tlab_refill(retry_tlab, try_eden, slow_path); // preserves G5_klass
 518 
 519           __ bind(retry_tlab);
 520 
 521           // get the instance size
 522           Label have_obj_size, size_is_variable;
 523           compute_instance_size(G1_obj_size, G5_klass,
 524                                 have_obj_size, size_is_variable,
 525                                 G3_t1, sasm, true);
 526           __ tlab_allocate(O0_obj, G1_obj_size, 0, G3_t1, slow_path);
 527           __ initialize_object(O0_obj, G5_klass, G1_obj_size, 0, G3_t1, G4_t2);
 528           __ verify_oop(O0_obj);
 529           __ mov(O0, I0);
 530           __ ret();
 531           __ delayed()->restore();
 532 
 533           // generate side path, if any
 534           compute_instance_size(G1_obj_size, G5_klass,
 535                                 have_obj_size, size_is_variable,
 536                                 G3_t1, sasm, false);
 537 
 538           __ bind(try_eden);
 539           // get the instance size
 540           Label have_obj_size_2, size_is_variable_2;
 541           compute_instance_size(G1_obj_size, G5_klass,
 542                                 have_obj_size_2, size_is_variable_2,
 543                                 G3_t1, sasm, true);
 544           __ eden_allocate(O0_obj, G1_obj_size, 0, G3_t1, G4_t2, slow_path);
 545           __ initialize_object(O0_obj, G5_klass, G1_obj_size, 0, G3_t1, G4_t2);
 546           __ verify_oop(O0_obj);
 547           __ mov(O0, I0);
 548           __ ret();
 549           __ delayed()->restore();
 550 
 551           // generate side path, if any
 552           compute_instance_size(G1_obj_size, G5_klass,
 553                                 have_obj_size_2, size_is_variable_2,
 554                                 G3_t1, sasm, false);
 555 
 556           __ bind(slow_path);
 557 
 558           // pop this frame so generate_stub_call can push it's own
 559           __ restore();
 560         }
 561 
 562         oop_maps = generate_stub_call(sasm, I0, CAST_FROM_FN_PTR(address, new_instance), G5_klass);
 563         // I0->O0: new instance
 564       }
 565 
 566       break;
 567 
 568 #ifdef TIERED
 569     case counter_overflow_id:
 570         // G4 contains bci
 571       oop_maps = generate_stub_call(sasm, noreg, CAST_FROM_FN_PTR(address, counter_overflow), G4);
 572       break;
 573 #endif // TIERED
 574 
 575     case new_type_array_id:
 576     case new_object_array_id:
 577       {
 578         Register G5_klass = G5; // Incoming
 579         Register G4_length = G4; // Incoming
 580         Register O0_obj   = O0; // Outgoing
 581 
 582         if (id == new_type_array_id) {
 583           __ set_info("new_type_array", dont_gc_arguments);
 584         } else {
 585           __ set_info("new_object_array", dont_gc_arguments);
 586         }
 587 
 588 #ifdef ASSERT
 589         // assert object type is really an array of the proper kind
 590         {
 591           Label ok;
 592           Register G3_t1 = G3;
 593           __ lduw(layout_helper_addr(G5_klass), G3_t1);
 594           int flag_shift = LayoutHelper::_flags_shift;
 595           flag_shift += LayoutHelper::_flags_low_bits; // shift out BasicType also
 596           int tag = LayoutHelper::for_array((id == new_type_array_id) ? T_BYTE : T_OBJECT).as_int();
 597           tag     >>= flag_shift;
 598           __ sra(G3_t1, flag_shift, G3_t1);
 599           assert(id == new_object_array_id ||
 600                  tag == (LayoutHelper::for_array(T_DOUBLE).as_int() >> flag_shift),
 601                  "same tag for all type arrays");
 602           __ cmp(G3_t1, tag);
 603           __ brx(Assembler::equal, false, Assembler::pt, ok);
 604           __ delayed()->nop();
 605           __ stop("assert(is an array klass)");
 606           __ should_not_reach_here();
 607           __ bind(ok);
 608         }
 609 #endif // ASSERT
 610 
 611         if (UseTLAB && FastTLABRefill) {
 612           Label slow_path;
 613           Register G1_arr_size = G1;
 614           Register G3_t1 = G3;
 615           Register O1_t2 = O1;
 616           assert_different_registers(G5_klass, G4_length, G1_arr_size, G3_t1, O1_t2);
 617 
 618           // check that array length is small enough for fast path
 619           __ set(C1_MacroAssembler::max_array_allocation_length, G3_t1);
 620           __ cmp(G4_length, G3_t1);
 621           __ br(Assembler::greaterUnsigned, false, Assembler::pn, slow_path);
 622           __ delayed()->nop();
 623 
 624           // if we got here then the TLAB allocation failed, so try
 625           // refilling the TLAB or allocating directly from eden.
 626           Label retry_tlab, try_eden;
 627           __ tlab_refill(retry_tlab, try_eden, slow_path); // preserves G4_length and G5_klass
 628 
 629           __ bind(retry_tlab);
 630 
 631           // get the allocation size: (length << (layout_helper & 0x1F)) + header_size
 632           Label have_scaled_length, need_multiply;
 633           compute_array_size(G1_arr_size, G5_klass, G4_length,
 634                              have_scaled_length, need_multiply,
 635                              G3_t1, O1_t2, sasm, true);
 636           __ tlab_allocate(O0_obj, G1_arr_size, 0, G3_t1, slow_path);  // preserves G1_arr_size
 637 
 638           __ initialize_header(O0_obj, G5_klass, G4_length, G3_t1, O1_t2);
 639           initialize_array_body(O0_obj, G1_arr_size, G3_t1, O1_t2, sasm);
 640           __ verify_oop(O0_obj);
 641           __ retl();
 642           __ delayed()->nop();
 643 
 644           // generate side path, if any
 645           compute_array_size(G1_arr_size, G5_klass, G4_length,
 646                              have_scaled_length, need_multiply,
 647                              G3_t1, O1_t2, sasm, false);
 648 
 649           __ bind(try_eden);
 650           Label have_scaled_length_2, need_multiply_2;
 651           // get the allocation size: (length << (layout_helper & 0x1F)) + header_size
 652           compute_array_size(G1_arr_size, G5_klass, G4_length,
 653                              have_scaled_length_2, need_multiply_2,
 654                              G3_t1, O1_t2, sasm, true);
 655           __ eden_allocate(O0_obj, G1_arr_size, 0, G3_t1, O1_t2, slow_path);  // preserves G1_arr_size
 656 
 657           __ initialize_header(O0_obj, G5_klass, G4_length, G3_t1, O1_t2);
 658           initialize_array_body(O0_obj, G1_arr_size, G3_t1, O1_t2, sasm);
 659           __ verify_oop(O0_obj);
 660           __ retl();
 661           __ delayed()->nop();
 662 
 663           // generate side path, if any
 664           compute_array_size(G1_arr_size, G5_klass, G4_length,
 665                              have_scaled_length_2, need_multiply_2,
 666                              G3_t1, O1_t2, sasm, false);
 667 
 668           __ bind(slow_path);
 669         }
 670 
 671         if (id == new_type_array_id) {
 672           oop_maps = generate_stub_call(sasm, I0, CAST_FROM_FN_PTR(address, new_type_array), G5_klass, G4_length);
 673         } else {
 674           oop_maps = generate_stub_call(sasm, I0, CAST_FROM_FN_PTR(address, new_object_array), G5_klass, G4_length);
 675         }
 676         // I0 -> O0: new array
 677       }
 678       break;
 679 
 680     case new_multi_array_id:
 681       { // O0: klass
 682         // O1: rank
 683         // O2: address of 1st dimension
 684         __ set_info("new_multi_array", dont_gc_arguments);
 685         oop_maps = generate_stub_call(sasm, I0, CAST_FROM_FN_PTR(address, new_multi_array), I0, I1, I2);
 686         // I0 -> O0: new multi array
 687       }
 688       break;
 689 
 690     case register_finalizer_id:
 691       {
 692         __ set_info("register_finalizer", dont_gc_arguments);
 693 
 694         // load the klass and check the has finalizer flag
 695         Label register_finalizer;
 696         Register t = O1;
 697         __ ld_ptr(O0, oopDesc::klass_offset_in_bytes(), t);
 698         __ ld(t, Klass::access_flags_offset_in_bytes() + sizeof(oopDesc), t);
 699         __ set(JVM_ACC_HAS_FINALIZER, G3);
 700         __ andcc(G3, t, G0);
 701         __ br(Assembler::notZero, false, Assembler::pt, register_finalizer);
 702         __ delayed()->nop();
 703 
 704         // do a leaf return
 705         __ retl();
 706         __ delayed()->nop();
 707 
 708         __ bind(register_finalizer);
 709         OopMap* oop_map = save_live_registers(sasm);
 710         int call_offset = __ call_RT(noreg, noreg,
 711                                      CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), I0);
 712         oop_maps = new OopMapSet();
 713         oop_maps->add_gc_map(call_offset, oop_map);
 714 
 715         // Now restore all the live registers
 716         restore_live_registers(sasm);
 717 
 718         __ ret();
 719         __ delayed()->restore();
 720       }
 721       break;
 722 
 723     case throw_range_check_failed_id:
 724       { __ set_info("range_check_failed", dont_gc_arguments); // arguments will be discarded
 725         // G4: index
 726         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_range_check_exception), true);
 727       }
 728       break;
 729 
 730     case throw_index_exception_id:
 731       { __ set_info("index_range_check_failed", dont_gc_arguments); // arguments will be discarded
 732         // G4: index
 733         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_index_exception), true);
 734       }
 735       break;
 736 
 737     case throw_div0_exception_id:
 738       { __ set_info("throw_div0_exception", dont_gc_arguments);
 739         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_div0_exception), false);
 740       }
 741       break;
 742 
 743     case throw_null_pointer_exception_id:
 744       { __ set_info("throw_null_pointer_exception", dont_gc_arguments);
 745         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_null_pointer_exception), false);
 746       }
 747       break;
 748 
 749     case handle_exception_id:
 750       {
 751         __ set_info("handle_exception", dont_gc_arguments);
 752         // make a frame and preserve the caller's caller-save registers
 753 
 754         oop_maps = new OopMapSet();
 755         OopMap* oop_map = save_live_registers(sasm);
 756         __ mov(Oexception->after_save(),  Oexception);
 757         __ mov(Oissuing_pc->after_save(), Oissuing_pc);
 758         generate_handle_exception(sasm, oop_maps, oop_map);
 759       }
 760       break;
 761 
 762     case unwind_exception_id:
 763       {
 764         // O0: exception
 765         // I7: address of call to this method
 766 
 767         __ set_info("unwind_exception", dont_gc_arguments);
 768         __ mov(Oexception, Oexception->after_save());
 769         __ add(I7, frame::pc_return_offset, Oissuing_pc->after_save());
 770 
 771         __ call_VM_leaf(L7_thread_cache, CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address),
 772                         Oissuing_pc->after_save());
 773         __ verify_not_null_oop(Oexception->after_save());
 774         __ jmp(O0, 0);
 775         __ delayed()->restore();
 776       }
 777       break;
 778 
 779     case throw_array_store_exception_id:
 780       {
 781         __ set_info("throw_array_store_exception", dont_gc_arguments);
 782         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), false);
 783       }
 784       break;
 785 
 786     case throw_class_cast_exception_id:
 787       {
 788         // G4: object
 789         __ set_info("throw_class_cast_exception", dont_gc_arguments);
 790         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
 791       }
 792       break;
 793 
 794     case throw_incompatible_class_change_error_id:
 795       {
 796         __ set_info("throw_incompatible_class_cast_exception", dont_gc_arguments);
 797         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
 798       }
 799       break;
 800 
 801     case slow_subtype_check_id:
 802       { // Support for uint StubRoutine::partial_subtype_check( Klass sub, Klass super );
 803         // Arguments :
 804         //
 805         //      ret  : G3
 806         //      sub  : G3, argument, destroyed
 807         //      super: G1, argument, not changed
 808         //      raddr: O7, blown by call
 809         Label loop, miss;
 810 
 811         __ save_frame(0);               // Blow no registers!
 812 
 813         __ ld_ptr( G3, sizeof(oopDesc) + Klass::secondary_supers_offset_in_bytes(), L3 );
 814         __ lduw(L3,arrayOopDesc::length_offset_in_bytes(),L0); // length in l0
 815         __ add(L3,arrayOopDesc::base_offset_in_bytes(T_OBJECT),L1); // ptr into array
 816         __ clr(L4);                     // Index
 817         // Load a little early; will load 1 off the end of the array.
 818         // Ok for now; revisit if we have other uses of this routine.
 819         __ ld_ptr(L1,0,L2);             // Will load a little early
 820 
 821         // The scan loop
 822         __ bind(loop);
 823         __ add(L1,wordSize,L1); // Bump by OOP size
 824         __ cmp(L4,L0);
 825         __ br(Assembler::equal,false,Assembler::pn,miss);
 826         __ delayed()->inc(L4);  // Bump index
 827         __ subcc(L2,G1,L3);             // Check for match; zero in L3 for a hit
 828         __ brx( Assembler::notEqual, false, Assembler::pt, loop );
 829         __ delayed()->ld_ptr(L1,0,L2); // Will load a little early
 830 
 831         // Got a hit; report success; set cache
 832         __ st_ptr( G1, G3, sizeof(oopDesc) + Klass::secondary_super_cache_offset_in_bytes() );
 833 
 834         __ mov(1, G3);
 835         __ ret();                       // Result in G5 is ok; flags set
 836         __ delayed()->restore();        // free copy or add can go here
 837 
 838         __ bind(miss);
 839         __ mov(0, G3);
 840         __ ret();                       // Result in G5 is ok; flags set
 841         __ delayed()->restore();        // free copy or add can go here
 842       }
 843 
 844     case monitorenter_nofpu_id:
 845     case monitorenter_id:
 846       { // G4: object
 847         // G5: lock address
 848         __ set_info("monitorenter", dont_gc_arguments);
 849 
 850         int save_fpu_registers = (id == monitorenter_id);
 851         // make a frame and preserve the caller's caller-save registers
 852         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
 853 
 854         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), G4, G5);
 855 
 856         oop_maps = new OopMapSet();
 857         oop_maps->add_gc_map(call_offset, oop_map);
 858         restore_live_registers(sasm, save_fpu_registers);
 859 
 860         __ ret();
 861         __ delayed()->restore();
 862       }
 863       break;
 864 
 865     case monitorexit_nofpu_id:
 866     case monitorexit_id:
 867       { // G4: lock address
 868         // note: really a leaf routine but must setup last java sp
 869         //       => use call_RT for now (speed can be improved by
 870         //       doing last java sp setup manually)
 871         __ set_info("monitorexit", dont_gc_arguments);
 872 
 873         int save_fpu_registers = (id == monitorexit_id);
 874         // make a frame and preserve the caller's caller-save registers
 875         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
 876 
 877         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), G4);
 878 
 879         oop_maps = new OopMapSet();
 880         oop_maps->add_gc_map(call_offset, oop_map);
 881         restore_live_registers(sasm, save_fpu_registers);
 882 
 883         __ ret();
 884         __ delayed()->restore();
 885 
 886       }
 887       break;
 888 
 889     case access_field_patching_id:
 890       { __ set_info("access_field_patching", dont_gc_arguments);
 891         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
 892       }
 893       break;
 894 
 895     case load_klass_patching_id:
 896       { __ set_info("load_klass_patching", dont_gc_arguments);
 897         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_klass_patching));
 898       }
 899       break;
 900 
 901     case jvmti_exception_throw_id:
 902       { // Oexception : exception
 903         __ set_info("jvmti_exception_throw", dont_gc_arguments);
 904         oop_maps = generate_stub_call(sasm, noreg, CAST_FROM_FN_PTR(address, Runtime1::post_jvmti_exception_throw), I0);
 905       }
 906       break;
 907 
 908     case dtrace_object_alloc_id:
 909       { // O0: object
 910         __ set_info("dtrace_object_alloc", dont_gc_arguments);
 911         // we can't gc here so skip the oopmap but make sure that all
 912         // the live registers get saved.
 913         save_live_registers(sasm);
 914 
 915         __ save_thread(L7_thread_cache);
 916         __ call(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc),
 917                 relocInfo::runtime_call_type);
 918         __ delayed()->mov(I0, O0);
 919         __ restore_thread(L7_thread_cache);
 920 
 921         restore_live_registers(sasm);
 922         __ ret();
 923         __ delayed()->restore();
 924       }
 925       break;
 926 
 927     default:
 928       { __ set_info("unimplemented entry", dont_gc_arguments);
 929         __ save_frame(0);
 930         __ set((int)id, O1);
 931         __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), O1);
 932         __ should_not_reach_here();
 933       }
 934       break;
 935   }
 936   return oop_maps;
 937 }
 938 
 939 
 940 void Runtime1::generate_handle_exception(StubAssembler* sasm, OopMapSet* oop_maps, OopMap* oop_map, bool) {
 941   Label no_deopt;
 942   Label no_handler;
 943 
 944   __ verify_not_null_oop(Oexception);
 945 
 946   // save the exception and issuing pc in the thread
 947   __ st_ptr(Oexception, G2_thread, in_bytes(JavaThread::exception_oop_offset()));
 948   __ st_ptr(Oissuing_pc, G2_thread, in_bytes(JavaThread::exception_pc_offset()));
 949 
 950   // save the real return address and use the throwing pc as the return address to lookup (has bci & oop map)
 951   __ mov(I7, L0);
 952   __ mov(Oissuing_pc, I7);
 953   __ sub(I7, frame::pc_return_offset, I7);
 954   int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
 955 
 956   // Note: if nmethod has been deoptimized then regardless of
 957   // whether it had a handler or not we will deoptimize
 958   // by entering the deopt blob with a pending exception.
 959 
 960   __ tst(O0);
 961   __ br(Assembler::zero, false, Assembler::pn, no_handler);
 962   __ delayed()->nop();
 963 
 964   // restore the registers that were saved at the beginning and jump to the exception handler.
 965   restore_live_registers(sasm);
 966 
 967   __ jmp(O0, 0);
 968   __ delayed()->restore();
 969 
 970   __ bind(no_handler);
 971   __ mov(L0, I7); // restore return address
 972 
 973   // restore exception oop
 974   __ ld_ptr(G2_thread, in_bytes(JavaThread::exception_oop_offset()), Oexception->after_save());
 975   __ st_ptr(G0, G2_thread, in_bytes(JavaThread::exception_oop_offset()));
 976 
 977   __ restore();
 978 
 979   Address exc(G4, Runtime1::entry_for(Runtime1::unwind_exception_id));
 980   __ jump_to(exc, 0);
 981   __ delayed()->nop();
 982 
 983 
 984   oop_maps->add_gc_map(call_offset, oop_map);
 985 }
 986 
 987 
 988 #undef __
 989 
 990 #define __ masm->