1 /*
   2  * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "c1/c1_Defs.hpp"
  27 #include "c1/c1_MacroAssembler.hpp"
  28 #include "c1/c1_Runtime1.hpp"
  29 #include "interpreter/interpreter.hpp"
  30 #include "nativeInst_sparc.hpp"
  31 #include "oops/compiledICHolderOop.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "prims/jvmtiExport.hpp"
  34 #include "register_sparc.hpp"
  35 #include "runtime/sharedRuntime.hpp"
  36 #include "runtime/signature.hpp"
  37 #include "runtime/vframeArray.hpp"
  38 #include "vmreg_sparc.inline.hpp"
  39 
  40 // Implementation of StubAssembler
  41 
  42 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry_point, int number_of_arguments) {
  43   // for sparc changing the number of arguments doesn't change
  44   // anything about the frame size so we'll always lie and claim that
  45   // we are only passing 1 argument.
  46   set_num_rt_args(1);
  47 
  48   assert_not_delayed();
  49   // bang stack before going to runtime
  50   set(-os::vm_page_size() + STACK_BIAS, G3_scratch);
  51   st(G0, SP, G3_scratch);
  52 
  53   // debugging support
  54   assert(number_of_arguments >= 0   , "cannot have negative number of arguments");
  55 
  56   set_last_Java_frame(SP, noreg);
  57   if (VerifyThread)  mov(G2_thread, O0); // about to be smashed; pass early
  58   save_thread(L7_thread_cache);
  59   // do the call
  60   if (is_in_wdisp30_range(entry_point)) {
  61     call(entry_point, relocInfo::runtime_call_type);
  62   } else {
  63     relocate(relocInfo::runtime_call_type);
  64     AddressLiteral dest(entry_point);
  65     jumpl_to(dest, O7, O7);
  66   }
  67 
  68   if (!VerifyThread) {
  69     delayed()->mov(G2_thread, O0);  // pass thread as first argument
  70   } else {
  71     delayed()->nop();             // (thread already passed)
  72   }
  73   int call_offset = offset();  // offset of return address
  74   restore_thread(L7_thread_cache);
  75   reset_last_Java_frame();
  76 
  77   // check for pending exceptions
  78   { Label L;
  79     Address exception_addr(G2_thread, Thread::pending_exception_offset());
  80     ld_ptr(exception_addr, Gtemp);
  81     br_null(Gtemp, false, pt, L);
  82     delayed()->nop();
  83     Address vm_result_addr(G2_thread, JavaThread::vm_result_offset());
  84     st_ptr(G0, vm_result_addr);
  85     Address vm_result_addr_2(G2_thread, JavaThread::vm_result_2_offset());
  86     st_ptr(G0, vm_result_addr_2);
  87 
  88     if (frame_size() == no_frame_size) {
  89       // we use O7 linkage so that forward_exception_entry has the issuing PC
  90       call(StubRoutines::forward_exception_entry(), relocInfo::runtime_call_type);
  91       delayed()->restore();
  92     } else if (_stub_id == Runtime1::forward_exception_id) {
  93       should_not_reach_here();
  94     } else {
  95       AddressLiteral exc(Runtime1::entry_for(Runtime1::forward_exception_id));
  96       jump_to(exc, G4);
  97       delayed()->nop();
  98     }
  99     bind(L);
 100   }
 101 
 102   // get oop result if there is one and reset the value in the thread
 103   if (oop_result1->is_valid()) {                    // get oop result if there is one and reset it in the thread
 104     get_vm_result  (oop_result1);
 105   } else {
 106     // be a little paranoid and clear the result
 107     Address vm_result_addr(G2_thread, JavaThread::vm_result_offset());
 108     st_ptr(G0, vm_result_addr);
 109   }
 110 
 111   if (oop_result2->is_valid()) {
 112     get_vm_result_2(oop_result2);
 113   } else {
 114     // be a little paranoid and clear the result
 115     Address vm_result_addr_2(G2_thread, JavaThread::vm_result_2_offset());
 116     st_ptr(G0, vm_result_addr_2);
 117   }
 118 
 119   return call_offset;
 120 }
 121 
 122 
 123 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry, Register arg1) {
 124   // O0 is reserved for the thread
 125   mov(arg1, O1);
 126   return call_RT(oop_result1, oop_result2, entry, 1);
 127 }
 128 
 129 
 130 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry, Register arg1, Register arg2) {
 131   // O0 is reserved for the thread
 132   mov(arg1, O1);
 133   mov(arg2, O2); assert(arg2 != O1, "smashed argument");
 134   return call_RT(oop_result1, oop_result2, entry, 2);
 135 }
 136 
 137 
 138 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry, Register arg1, Register arg2, Register arg3) {
 139   // O0 is reserved for the thread
 140   mov(arg1, O1);
 141   mov(arg2, O2); assert(arg2 != O1,               "smashed argument");
 142   mov(arg3, O3); assert(arg3 != O1 && arg3 != O2, "smashed argument");
 143   return call_RT(oop_result1, oop_result2, entry, 3);
 144 }
 145 
 146 
 147 // Implementation of Runtime1
 148 
 149 #define __ sasm->
 150 
 151 static int cpu_reg_save_offsets[FrameMap::nof_cpu_regs];
 152 static int fpu_reg_save_offsets[FrameMap::nof_fpu_regs];
 153 static int reg_save_size_in_words;
 154 static int frame_size_in_bytes = -1;
 155 
 156 static OopMap* generate_oop_map(StubAssembler* sasm, bool save_fpu_registers) {
 157   assert(frame_size_in_bytes == __ total_frame_size_in_bytes(reg_save_size_in_words),
 158          " mismatch in calculation");
 159   sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
 160   int frame_size_in_slots = frame_size_in_bytes / sizeof(jint);
 161   OopMap* oop_map = new OopMap(frame_size_in_slots, 0);
 162 
 163   int i;
 164   for (i = 0; i < FrameMap::nof_cpu_regs; i++) {
 165     Register r = as_Register(i);
 166     if (r == G1 || r == G3 || r == G4 || r == G5) {
 167       int sp_offset = cpu_reg_save_offsets[i];
 168       oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset),
 169                                 r->as_VMReg());
 170     }
 171   }
 172 
 173   if (save_fpu_registers) {
 174     for (i = 0; i < FrameMap::nof_fpu_regs; i++) {
 175       FloatRegister r = as_FloatRegister(i);
 176       int sp_offset = fpu_reg_save_offsets[i];
 177       oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset),
 178                                 r->as_VMReg());
 179     }
 180   }
 181   return oop_map;
 182 }
 183 
 184 static OopMap* save_live_registers(StubAssembler* sasm, bool save_fpu_registers = true) {
 185   assert(frame_size_in_bytes == __ total_frame_size_in_bytes(reg_save_size_in_words),
 186          " mismatch in calculation");
 187   __ save_frame_c1(frame_size_in_bytes);
 188   sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
 189 
 190   // Record volatile registers as callee-save values in an OopMap so their save locations will be
 191   // propagated to the caller frame's RegisterMap during StackFrameStream construction (needed for
 192   // deoptimization; see compiledVFrame::create_stack_value).  The caller's I, L and O registers
 193   // are saved in register windows - I's and L's in the caller's frame and O's in the stub frame
 194   // (as the stub's I's) when the runtime routine called by the stub creates its frame.
 195   // OopMap frame sizes are in c2 stack slot sizes (sizeof(jint))
 196 
 197   int i;
 198   for (i = 0; i < FrameMap::nof_cpu_regs; i++) {
 199     Register r = as_Register(i);
 200     if (r == G1 || r == G3 || r == G4 || r == G5) {
 201       int sp_offset = cpu_reg_save_offsets[i];
 202       __ st_ptr(r, SP, (sp_offset * BytesPerWord) + STACK_BIAS);
 203     }
 204   }
 205 
 206   if (save_fpu_registers) {
 207     for (i = 0; i < FrameMap::nof_fpu_regs; i++) {
 208       FloatRegister r = as_FloatRegister(i);
 209       int sp_offset = fpu_reg_save_offsets[i];
 210       __ stf(FloatRegisterImpl::S, r, SP, (sp_offset * BytesPerWord) + STACK_BIAS);
 211     }
 212   }
 213 
 214   return generate_oop_map(sasm, save_fpu_registers);
 215 }
 216 
 217 static void restore_live_registers(StubAssembler* sasm, bool restore_fpu_registers = true) {
 218   for (int i = 0; i < FrameMap::nof_cpu_regs; i++) {
 219     Register r = as_Register(i);
 220     if (r == G1 || r == G3 || r == G4 || r == G5) {
 221       __ ld_ptr(SP, (cpu_reg_save_offsets[i] * BytesPerWord) + STACK_BIAS, r);
 222     }
 223   }
 224 
 225   if (restore_fpu_registers) {
 226     for (int i = 0; i < FrameMap::nof_fpu_regs; i++) {
 227       FloatRegister r = as_FloatRegister(i);
 228       __ ldf(FloatRegisterImpl::S, SP, (fpu_reg_save_offsets[i] * BytesPerWord) + STACK_BIAS, r);
 229     }
 230   }
 231 }
 232 
 233 
 234 void Runtime1::initialize_pd() {
 235   // compute word offsets from SP at which live (non-windowed) registers are captured by stub routines
 236   //
 237   // A stub routine will have a frame that is at least large enough to hold
 238   // a register window save area (obviously) and the volatile g registers
 239   // and floating registers. A user of save_live_registers can have a frame
 240   // that has more scratch area in it (although typically they will use L-regs).
 241   // in that case the frame will look like this (stack growing down)
 242   //
 243   // FP -> |             |
 244   //       | scratch mem |
 245   //       |   "      "  |
 246   //       --------------
 247   //       | float regs  |
 248   //       |   "    "    |
 249   //       ---------------
 250   //       | G regs      |
 251   //       | "  "        |
 252   //       ---------------
 253   //       | abi reg.    |
 254   //       | window save |
 255   //       | area        |
 256   // SP -> ---------------
 257   //
 258   int i;
 259   int sp_offset = round_to(frame::register_save_words, 2); //  start doubleword aligned
 260 
 261   // only G int registers are saved explicitly; others are found in register windows
 262   for (i = 0; i < FrameMap::nof_cpu_regs; i++) {
 263     Register r = as_Register(i);
 264     if (r == G1 || r == G3 || r == G4 || r == G5) {
 265       cpu_reg_save_offsets[i] = sp_offset;
 266       sp_offset++;
 267     }
 268   }
 269 
 270   // all float registers are saved explicitly
 271   assert(FrameMap::nof_fpu_regs == 32, "double registers not handled here");
 272   for (i = 0; i < FrameMap::nof_fpu_regs; i++) {
 273     fpu_reg_save_offsets[i] = sp_offset;
 274     sp_offset++;
 275   }
 276   reg_save_size_in_words = sp_offset - frame::memory_parameter_word_sp_offset;
 277   // this should match assembler::total_frame_size_in_bytes, which
 278   // isn't callable from this context.  It's checked by an assert when
 279   // it's used though.
 280   frame_size_in_bytes = align_size_up(sp_offset * wordSize, 8);
 281 }
 282 
 283 
 284 OopMapSet* Runtime1::generate_exception_throw(StubAssembler* sasm, address target, bool has_argument) {
 285   // make a frame and preserve the caller's caller-save registers
 286   OopMap* oop_map = save_live_registers(sasm);
 287   int call_offset;
 288   if (!has_argument) {
 289     call_offset = __ call_RT(noreg, noreg, target);
 290   } else {
 291     call_offset = __ call_RT(noreg, noreg, target, G4);
 292   }
 293   OopMapSet* oop_maps = new OopMapSet();
 294   oop_maps->add_gc_map(call_offset, oop_map);
 295 
 296   __ should_not_reach_here();
 297   return oop_maps;
 298 }
 299 
 300 
 301 OopMapSet* Runtime1::generate_stub_call(StubAssembler* sasm, Register result, address target,
 302                                         Register arg1, Register arg2, Register arg3) {
 303   // make a frame and preserve the caller's caller-save registers
 304   OopMap* oop_map = save_live_registers(sasm);
 305 
 306   int call_offset;
 307   if (arg1 == noreg) {
 308     call_offset = __ call_RT(result, noreg, target);
 309   } else if (arg2 == noreg) {
 310     call_offset = __ call_RT(result, noreg, target, arg1);
 311   } else if (arg3 == noreg) {
 312     call_offset = __ call_RT(result, noreg, target, arg1, arg2);
 313   } else {
 314     call_offset = __ call_RT(result, noreg, target, arg1, arg2, arg3);
 315   }
 316   OopMapSet* oop_maps = NULL;
 317 
 318   oop_maps = new OopMapSet();
 319   oop_maps->add_gc_map(call_offset, oop_map);
 320   restore_live_registers(sasm);
 321 
 322   __ ret();
 323   __ delayed()->restore();
 324 
 325   return oop_maps;
 326 }
 327 
 328 
 329 OopMapSet* Runtime1::generate_patching(StubAssembler* sasm, address target) {
 330   // make a frame and preserve the caller's caller-save registers
 331   OopMap* oop_map = save_live_registers(sasm);
 332 
 333   // call the runtime patching routine, returns non-zero if nmethod got deopted.
 334   int call_offset = __ call_RT(noreg, noreg, target);
 335   OopMapSet* oop_maps = new OopMapSet();
 336   oop_maps->add_gc_map(call_offset, oop_map);
 337 
 338   // re-execute the patched instruction or, if the nmethod was deoptmized, return to the
 339   // deoptimization handler entry that will cause re-execution of the current bytecode
 340   DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
 341   assert(deopt_blob != NULL, "deoptimization blob must have been created");
 342 
 343   Label no_deopt;
 344   __ tst(O0);
 345   __ brx(Assembler::equal, false, Assembler::pt, no_deopt);
 346   __ delayed()->nop();
 347 
 348   // return to the deoptimization handler entry for unpacking and rexecute
 349   // if we simply returned the we'd deopt as if any call we patched had just
 350   // returned.
 351 
 352   restore_live_registers(sasm);
 353   __ restore();
 354   __ br(Assembler::always, false, Assembler::pt, deopt_blob->unpack_with_reexecution(), relocInfo::runtime_call_type);
 355   __ delayed()->nop();
 356 
 357   __ bind(no_deopt);
 358   restore_live_registers(sasm);
 359   __ ret();
 360   __ delayed()->restore();
 361 
 362   return oop_maps;
 363 }
 364 
 365 OopMapSet* Runtime1::generate_code_for(StubID id, StubAssembler* sasm) {
 366 
 367   OopMapSet* oop_maps = NULL;
 368   // for better readability
 369   const bool must_gc_arguments = true;
 370   const bool dont_gc_arguments = false;
 371 
 372   // stub code & info for the different stubs
 373   switch (id) {
 374     case forward_exception_id:
 375       {
 376         // we're handling an exception in the context of a compiled
 377         // frame.  The registers have been saved in the standard
 378         // places.  Perform an exception lookup in the caller and
 379         // dispatch to the handler if found.  Otherwise unwind and
 380         // dispatch to the callers exception handler.
 381 
 382         oop_maps = new OopMapSet();
 383         OopMap* oop_map = generate_oop_map(sasm, true);
 384 
 385         // transfer the pending exception to the exception_oop
 386         __ ld_ptr(G2_thread, in_bytes(JavaThread::pending_exception_offset()), Oexception);
 387         __ ld_ptr(Oexception, 0, G0);
 388         __ st_ptr(G0, G2_thread, in_bytes(JavaThread::pending_exception_offset()));
 389         __ add(I7, frame::pc_return_offset, Oissuing_pc);
 390 
 391         generate_handle_exception(sasm, oop_maps, oop_map);
 392         __ should_not_reach_here();
 393       }
 394       break;
 395 
 396     case new_instance_id:
 397     case fast_new_instance_id:
 398     case fast_new_instance_init_check_id:
 399       {
 400         Register G5_klass = G5; // Incoming
 401         Register O0_obj   = O0; // Outgoing
 402 
 403         if (id == new_instance_id) {
 404           __ set_info("new_instance", dont_gc_arguments);
 405         } else if (id == fast_new_instance_id) {
 406           __ set_info("fast new_instance", dont_gc_arguments);
 407         } else {
 408           assert(id == fast_new_instance_init_check_id, "bad StubID");
 409           __ set_info("fast new_instance init check", dont_gc_arguments);
 410         }
 411 
 412         if ((id == fast_new_instance_id || id == fast_new_instance_init_check_id) &&
 413             UseTLAB && FastTLABRefill) {
 414           Label slow_path;
 415           Register G1_obj_size = G1;
 416           Register G3_t1 = G3;
 417           Register G4_t2 = G4;
 418           assert_different_registers(G5_klass, G1_obj_size, G3_t1, G4_t2);
 419 
 420           // Push a frame since we may do dtrace notification for the
 421           // allocation which requires calling out and we don't want
 422           // to stomp the real return address.
 423           __ save_frame(0);
 424 
 425           if (id == fast_new_instance_init_check_id) {
 426             // make sure the klass is initialized
 427             __ ld(G5_klass, instanceKlass::init_state_offset_in_bytes() + sizeof(oopDesc), G3_t1);
 428             __ cmp(G3_t1, instanceKlass::fully_initialized);
 429             __ br(Assembler::notEqual, false, Assembler::pn, slow_path);
 430             __ delayed()->nop();
 431           }
 432 #ifdef ASSERT
 433           // assert object can be fast path allocated
 434           {
 435             Label ok, not_ok;
 436           __ ld(G5_klass, Klass::layout_helper_offset_in_bytes() + sizeof(oopDesc), G1_obj_size);
 437           __ cmp(G1_obj_size, 0);  // make sure it's an instance (LH > 0)
 438           __ br(Assembler::lessEqual, false, Assembler::pn, not_ok);
 439           __ delayed()->nop();
 440           __ btst(Klass::_lh_instance_slow_path_bit, G1_obj_size);
 441           __ br(Assembler::zero, false, Assembler::pn, ok);
 442           __ delayed()->nop();
 443           __ bind(not_ok);
 444           __ stop("assert(can be fast path allocated)");
 445           __ should_not_reach_here();
 446           __ bind(ok);
 447           }
 448 #endif // ASSERT
 449           // if we got here then the TLAB allocation failed, so try
 450           // refilling the TLAB or allocating directly from eden.
 451           Label retry_tlab, try_eden;
 452           __ tlab_refill(retry_tlab, try_eden, slow_path); // preserves G5_klass
 453 
 454           __ bind(retry_tlab);
 455 
 456           // get the instance size
 457           __ ld(G5_klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes(), G1_obj_size);
 458           __ tlab_allocate(O0_obj, G1_obj_size, 0, G3_t1, slow_path);
 459           __ initialize_object(O0_obj, G5_klass, G1_obj_size, 0, G3_t1, G4_t2);
 460           __ verify_oop(O0_obj);
 461           __ mov(O0, I0);
 462           __ ret();
 463           __ delayed()->restore();
 464 
 465           __ bind(try_eden);
 466           // get the instance size
 467           __ ld(G5_klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes(), G1_obj_size);
 468           __ eden_allocate(O0_obj, G1_obj_size, 0, G3_t1, G4_t2, slow_path);
 469           __ initialize_object(O0_obj, G5_klass, G1_obj_size, 0, G3_t1, G4_t2);
 470           __ verify_oop(O0_obj);
 471           __ mov(O0, I0);
 472           __ ret();
 473           __ delayed()->restore();
 474 
 475           __ bind(slow_path);
 476 
 477           // pop this frame so generate_stub_call can push it's own
 478           __ restore();
 479         }
 480 
 481         oop_maps = generate_stub_call(sasm, I0, CAST_FROM_FN_PTR(address, new_instance), G5_klass);
 482         // I0->O0: new instance
 483       }
 484 
 485       break;
 486 
 487     case counter_overflow_id:
 488         // G4 contains bci, G5 contains method
 489       oop_maps = generate_stub_call(sasm, noreg, CAST_FROM_FN_PTR(address, counter_overflow), G4, G5);
 490       break;
 491 
 492     case new_type_array_id:
 493     case new_object_array_id:
 494       {
 495         Register G5_klass = G5; // Incoming
 496         Register G4_length = G4; // Incoming
 497         Register O0_obj   = O0; // Outgoing
 498 
 499         Address klass_lh(G5_klass, ((klassOopDesc::header_size() * HeapWordSize)
 500                                     + Klass::layout_helper_offset_in_bytes()));
 501         assert(Klass::_lh_header_size_shift % BitsPerByte == 0, "bytewise");
 502         assert(Klass::_lh_header_size_mask == 0xFF, "bytewise");
 503         // Use this offset to pick out an individual byte of the layout_helper:
 504         const int klass_lh_header_size_offset = ((BytesPerInt - 1)  // 3 - 2 selects byte {0,1,0,0}
 505                                                  - Klass::_lh_header_size_shift / BitsPerByte);
 506 
 507         if (id == new_type_array_id) {
 508           __ set_info("new_type_array", dont_gc_arguments);
 509         } else {
 510           __ set_info("new_object_array", dont_gc_arguments);
 511         }
 512 
 513 #ifdef ASSERT
 514         // assert object type is really an array of the proper kind
 515         {
 516           Label ok;
 517           Register G3_t1 = G3;
 518           __ ld(klass_lh, G3_t1);
 519           __ sra(G3_t1, Klass::_lh_array_tag_shift, G3_t1);
 520           int tag = ((id == new_type_array_id)
 521                      ? Klass::_lh_array_tag_type_value
 522                      : Klass::_lh_array_tag_obj_value);
 523           __ cmp(G3_t1, tag);
 524           __ brx(Assembler::equal, false, Assembler::pt, ok);
 525           __ delayed()->nop();
 526           __ stop("assert(is an array klass)");
 527           __ should_not_reach_here();
 528           __ bind(ok);
 529         }
 530 #endif // ASSERT
 531 
 532         if (UseTLAB && FastTLABRefill) {
 533           Label slow_path;
 534           Register G1_arr_size = G1;
 535           Register G3_t1 = G3;
 536           Register O1_t2 = O1;
 537           assert_different_registers(G5_klass, G4_length, G1_arr_size, G3_t1, O1_t2);
 538 
 539           // check that array length is small enough for fast path
 540           __ set(C1_MacroAssembler::max_array_allocation_length, G3_t1);
 541           __ cmp(G4_length, G3_t1);
 542           __ br(Assembler::greaterUnsigned, false, Assembler::pn, slow_path);
 543           __ delayed()->nop();
 544 
 545           // if we got here then the TLAB allocation failed, so try
 546           // refilling the TLAB or allocating directly from eden.
 547           Label retry_tlab, try_eden;
 548           __ tlab_refill(retry_tlab, try_eden, slow_path); // preserves G4_length and G5_klass
 549 
 550           __ bind(retry_tlab);
 551 
 552           // get the allocation size: (length << (layout_helper & 0x1F)) + header_size
 553           __ ld(klass_lh, G3_t1);
 554           __ sll(G4_length, G3_t1, G1_arr_size);
 555           __ srl(G3_t1, Klass::_lh_header_size_shift, G3_t1);
 556           __ and3(G3_t1, Klass::_lh_header_size_mask, G3_t1);
 557           __ add(G1_arr_size, G3_t1, G1_arr_size);
 558           __ add(G1_arr_size, MinObjAlignmentInBytesMask, G1_arr_size);  // align up
 559           __ and3(G1_arr_size, ~MinObjAlignmentInBytesMask, G1_arr_size);
 560 
 561           __ tlab_allocate(O0_obj, G1_arr_size, 0, G3_t1, slow_path);  // preserves G1_arr_size
 562 
 563           __ initialize_header(O0_obj, G5_klass, G4_length, G3_t1, O1_t2);
 564           __ ldub(klass_lh, G3_t1, klass_lh_header_size_offset);
 565           __ sub(G1_arr_size, G3_t1, O1_t2);  // body length
 566           __ add(O0_obj, G3_t1, G3_t1);       // body start
 567           __ initialize_body(G3_t1, O1_t2);
 568           __ verify_oop(O0_obj);
 569           __ retl();
 570           __ delayed()->nop();
 571 
 572           __ bind(try_eden);
 573           // get the allocation size: (length << (layout_helper & 0x1F)) + header_size
 574           __ ld(klass_lh, G3_t1);
 575           __ sll(G4_length, G3_t1, G1_arr_size);
 576           __ srl(G3_t1, Klass::_lh_header_size_shift, G3_t1);
 577           __ and3(G3_t1, Klass::_lh_header_size_mask, G3_t1);
 578           __ add(G1_arr_size, G3_t1, G1_arr_size);
 579           __ add(G1_arr_size, MinObjAlignmentInBytesMask, G1_arr_size);
 580           __ and3(G1_arr_size, ~MinObjAlignmentInBytesMask, G1_arr_size);
 581 
 582           __ eden_allocate(O0_obj, G1_arr_size, 0, G3_t1, O1_t2, slow_path);  // preserves G1_arr_size
 583 
 584           __ initialize_header(O0_obj, G5_klass, G4_length, G3_t1, O1_t2);
 585           __ ldub(klass_lh, G3_t1, klass_lh_header_size_offset);
 586           __ sub(G1_arr_size, G3_t1, O1_t2);  // body length
 587           __ add(O0_obj, G3_t1, G3_t1);       // body start
 588           __ initialize_body(G3_t1, O1_t2);
 589           __ verify_oop(O0_obj);
 590           __ retl();
 591           __ delayed()->nop();
 592 
 593           __ bind(slow_path);
 594         }
 595 
 596         if (id == new_type_array_id) {
 597           oop_maps = generate_stub_call(sasm, I0, CAST_FROM_FN_PTR(address, new_type_array), G5_klass, G4_length);
 598         } else {
 599           oop_maps = generate_stub_call(sasm, I0, CAST_FROM_FN_PTR(address, new_object_array), G5_klass, G4_length);
 600         }
 601         // I0 -> O0: new array
 602       }
 603       break;
 604 
 605     case new_multi_array_id:
 606       { // O0: klass
 607         // O1: rank
 608         // O2: address of 1st dimension
 609         __ set_info("new_multi_array", dont_gc_arguments);
 610         oop_maps = generate_stub_call(sasm, I0, CAST_FROM_FN_PTR(address, new_multi_array), I0, I1, I2);
 611         // I0 -> O0: new multi array
 612       }
 613       break;
 614 
 615     case register_finalizer_id:
 616       {
 617         __ set_info("register_finalizer", dont_gc_arguments);
 618 
 619         // load the klass and check the has finalizer flag
 620         Label register_finalizer;
 621         Register t = O1;
 622         __ load_klass(O0, t);
 623         __ ld(t, Klass::access_flags_offset_in_bytes() + sizeof(oopDesc), t);
 624         __ set(JVM_ACC_HAS_FINALIZER, G3);
 625         __ andcc(G3, t, G0);
 626         __ br(Assembler::notZero, false, Assembler::pt, register_finalizer);
 627         __ delayed()->nop();
 628 
 629         // do a leaf return
 630         __ retl();
 631         __ delayed()->nop();
 632 
 633         __ bind(register_finalizer);
 634         OopMap* oop_map = save_live_registers(sasm);
 635         int call_offset = __ call_RT(noreg, noreg,
 636                                      CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), I0);
 637         oop_maps = new OopMapSet();
 638         oop_maps->add_gc_map(call_offset, oop_map);
 639 
 640         // Now restore all the live registers
 641         restore_live_registers(sasm);
 642 
 643         __ ret();
 644         __ delayed()->restore();
 645       }
 646       break;
 647 
 648     case throw_range_check_failed_id:
 649       { __ set_info("range_check_failed", dont_gc_arguments); // arguments will be discarded
 650         // G4: index
 651         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_range_check_exception), true);
 652       }
 653       break;
 654 
 655     case throw_index_exception_id:
 656       { __ set_info("index_range_check_failed", dont_gc_arguments); // arguments will be discarded
 657         // G4: index
 658         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_index_exception), true);
 659       }
 660       break;
 661 
 662     case throw_div0_exception_id:
 663       { __ set_info("throw_div0_exception", dont_gc_arguments);
 664         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_div0_exception), false);
 665       }
 666       break;
 667 
 668     case throw_null_pointer_exception_id:
 669       { __ set_info("throw_null_pointer_exception", dont_gc_arguments);
 670         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_null_pointer_exception), false);
 671       }
 672       break;
 673 
 674     case handle_exception_id:
 675       {
 676         __ set_info("handle_exception", dont_gc_arguments);
 677         // make a frame and preserve the caller's caller-save registers
 678 
 679         oop_maps = new OopMapSet();
 680         OopMap* oop_map = save_live_registers(sasm);
 681         __ mov(Oexception->after_save(),  Oexception);
 682         __ mov(Oissuing_pc->after_save(), Oissuing_pc);
 683         generate_handle_exception(sasm, oop_maps, oop_map);
 684       }
 685       break;
 686 
 687     case unwind_exception_id:
 688       {
 689         // O0: exception
 690         // I7: address of call to this method
 691 
 692         __ set_info("unwind_exception", dont_gc_arguments);
 693         __ mov(Oexception, Oexception->after_save());
 694         __ add(I7, frame::pc_return_offset, Oissuing_pc->after_save());
 695 
 696         __ call_VM_leaf(L7_thread_cache, CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address),
 697                         G2_thread, Oissuing_pc->after_save());
 698         __ verify_not_null_oop(Oexception->after_save());
 699 
 700         // Restore SP from L7 if the exception PC is a MethodHandle call site.
 701         __ mov(O0, G5);  // Save the target address.
 702         __ lduw(Address(G2_thread, JavaThread::is_method_handle_return_offset()), L0);
 703         __ tst(L0);  // Condition codes are preserved over the restore.
 704         __ restore();
 705 
 706         __ jmp(G5, 0);
 707         __ delayed()->movcc(Assembler::notZero, false, Assembler::icc, L7_mh_SP_save, SP);  // Restore SP if required.
 708       }
 709       break;
 710 
 711     case throw_array_store_exception_id:
 712       {
 713         __ set_info("throw_array_store_exception", dont_gc_arguments);
 714         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), false);
 715       }
 716       break;
 717 
 718     case throw_class_cast_exception_id:
 719       {
 720         // G4: object
 721         __ set_info("throw_class_cast_exception", dont_gc_arguments);
 722         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
 723       }
 724       break;
 725 
 726     case throw_incompatible_class_change_error_id:
 727       {
 728         __ set_info("throw_incompatible_class_cast_exception", dont_gc_arguments);
 729         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
 730       }
 731       break;
 732 
 733     case slow_subtype_check_id:
 734       { // Support for uint StubRoutine::partial_subtype_check( Klass sub, Klass super );
 735         // Arguments :
 736         //
 737         //      ret  : G3
 738         //      sub  : G3, argument, destroyed
 739         //      super: G1, argument, not changed
 740         //      raddr: O7, blown by call
 741         Label miss;
 742 
 743         __ save_frame(0);               // Blow no registers!
 744 
 745         __ check_klass_subtype_slow_path(G3, G1, L0, L1, L2, L4, NULL, &miss);
 746 
 747         __ mov(1, G3);
 748         __ ret();                       // Result in G5 is 'true'
 749         __ delayed()->restore();        // free copy or add can go here
 750 
 751         __ bind(miss);
 752         __ mov(0, G3);
 753         __ ret();                       // Result in G5 is 'false'
 754         __ delayed()->restore();        // free copy or add can go here
 755       }
 756 
 757     case monitorenter_nofpu_id:
 758     case monitorenter_id:
 759       { // G4: object
 760         // G5: lock address
 761         __ set_info("monitorenter", dont_gc_arguments);
 762 
 763         int save_fpu_registers = (id == monitorenter_id);
 764         // make a frame and preserve the caller's caller-save registers
 765         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
 766 
 767         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), G4, G5);
 768 
 769         oop_maps = new OopMapSet();
 770         oop_maps->add_gc_map(call_offset, oop_map);
 771         restore_live_registers(sasm, save_fpu_registers);
 772 
 773         __ ret();
 774         __ delayed()->restore();
 775       }
 776       break;
 777 
 778     case monitorexit_nofpu_id:
 779     case monitorexit_id:
 780       { // G4: lock address
 781         // note: really a leaf routine but must setup last java sp
 782         //       => use call_RT for now (speed can be improved by
 783         //       doing last java sp setup manually)
 784         __ set_info("monitorexit", dont_gc_arguments);
 785 
 786         int save_fpu_registers = (id == monitorexit_id);
 787         // make a frame and preserve the caller's caller-save registers
 788         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
 789 
 790         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), G4);
 791 
 792         oop_maps = new OopMapSet();
 793         oop_maps->add_gc_map(call_offset, oop_map);
 794         restore_live_registers(sasm, save_fpu_registers);
 795 
 796         __ ret();
 797         __ delayed()->restore();
 798 
 799       }
 800       break;
 801 
 802     case access_field_patching_id:
 803       { __ set_info("access_field_patching", dont_gc_arguments);
 804         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
 805       }
 806       break;
 807 
 808     case load_klass_patching_id:
 809       { __ set_info("load_klass_patching", dont_gc_arguments);
 810         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_klass_patching));
 811       }
 812       break;
 813 
 814     case jvmti_exception_throw_id:
 815       { // Oexception : exception
 816         __ set_info("jvmti_exception_throw", dont_gc_arguments);
 817         oop_maps = generate_stub_call(sasm, noreg, CAST_FROM_FN_PTR(address, Runtime1::post_jvmti_exception_throw), I0);
 818       }
 819       break;
 820 
 821     case dtrace_object_alloc_id:
 822       { // O0: object
 823         __ set_info("dtrace_object_alloc", dont_gc_arguments);
 824         // we can't gc here so skip the oopmap but make sure that all
 825         // the live registers get saved.
 826         save_live_registers(sasm);
 827 
 828         __ save_thread(L7_thread_cache);
 829         __ call(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc),
 830                 relocInfo::runtime_call_type);
 831         __ delayed()->mov(I0, O0);
 832         __ restore_thread(L7_thread_cache);
 833 
 834         restore_live_registers(sasm);
 835         __ ret();
 836         __ delayed()->restore();
 837       }
 838       break;
 839 
 840 #ifndef SERIALGC
 841     case g1_pre_barrier_slow_id:
 842       { // G4: previous value of memory
 843         BarrierSet* bs = Universe::heap()->barrier_set();
 844         if (bs->kind() != BarrierSet::G1SATBCTLogging) {
 845           __ save_frame(0);
 846           __ set((int)id, O1);
 847           __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), I0);
 848           __ should_not_reach_here();
 849           break;
 850         }
 851 
 852         __ set_info("g1_pre_barrier_slow_id", dont_gc_arguments);
 853 
 854         Register pre_val = G4;
 855         Register tmp  = G1_scratch;
 856         Register tmp2 = G3_scratch;
 857 
 858         Label refill, restart;
 859         bool with_frame = false; // I don't know if we can do with-frame.
 860         int satb_q_index_byte_offset =
 861           in_bytes(JavaThread::satb_mark_queue_offset() +
 862                    PtrQueue::byte_offset_of_index());
 863         int satb_q_buf_byte_offset =
 864           in_bytes(JavaThread::satb_mark_queue_offset() +
 865                    PtrQueue::byte_offset_of_buf());
 866         __ bind(restart);
 867         __ ld_ptr(G2_thread, satb_q_index_byte_offset, tmp);
 868 
 869         __ br_on_reg_cond(Assembler::rc_z, /*annul*/false,
 870                           Assembler::pn, tmp, refill);
 871 
 872         // If the branch is taken, no harm in executing this in the delay slot.
 873         __ delayed()->ld_ptr(G2_thread, satb_q_buf_byte_offset, tmp2);
 874         __ sub(tmp, oopSize, tmp);
 875 
 876         __ st_ptr(pre_val, tmp2, tmp);  // [_buf + index] := <address_of_card>
 877         // Use return-from-leaf
 878         __ retl();
 879         __ delayed()->st_ptr(tmp, G2_thread, satb_q_index_byte_offset);
 880 
 881         __ bind(refill);
 882         __ save_frame(0);
 883 
 884         __ mov(pre_val, L0);
 885         __ mov(tmp,     L1);
 886         __ mov(tmp2,    L2);
 887 
 888         __ call_VM_leaf(L7_thread_cache,
 889                         CAST_FROM_FN_PTR(address,
 890                                          SATBMarkQueueSet::handle_zero_index_for_thread),
 891                                          G2_thread);
 892 
 893         __ mov(L0, pre_val);
 894         __ mov(L1, tmp);
 895         __ mov(L2, tmp2);
 896 
 897         __ br(Assembler::always, /*annul*/false, Assembler::pt, restart);
 898         __ delayed()->restore();
 899       }
 900       break;
 901 
 902     case g1_post_barrier_slow_id:
 903       {
 904         BarrierSet* bs = Universe::heap()->barrier_set();
 905         if (bs->kind() != BarrierSet::G1SATBCTLogging) {
 906           __ save_frame(0);
 907           __ set((int)id, O1);
 908           __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), I0);
 909           __ should_not_reach_here();
 910           break;
 911         }
 912 
 913         __ set_info("g1_post_barrier_slow_id", dont_gc_arguments);
 914 
 915         Register addr = G4;
 916         Register cardtable = G5;
 917         Register tmp  = G1_scratch;
 918         Register tmp2 = G3_scratch;
 919         jbyte* byte_map_base = ((CardTableModRefBS*)bs)->byte_map_base;
 920 
 921         Label not_already_dirty, restart, refill;
 922 
 923 #ifdef _LP64
 924         __ srlx(addr, CardTableModRefBS::card_shift, addr);
 925 #else
 926         __ srl(addr, CardTableModRefBS::card_shift, addr);
 927 #endif
 928 
 929         AddressLiteral rs(byte_map_base);
 930         __ set(rs, cardtable);         // cardtable := <card table base>
 931         __ ldub(addr, cardtable, tmp); // tmp := [addr + cardtable]
 932 
 933         __ br_on_reg_cond(Assembler::rc_nz, /*annul*/false, Assembler::pt,
 934                           tmp, not_already_dirty);
 935         // Get cardtable + tmp into a reg by itself -- useful in the take-the-branch
 936         // case, harmless if not.
 937         __ delayed()->add(addr, cardtable, tmp2);
 938 
 939         // We didn't take the branch, so we're already dirty: return.
 940         // Use return-from-leaf
 941         __ retl();
 942         __ delayed()->nop();
 943 
 944         // Not dirty.
 945         __ bind(not_already_dirty);
 946         // First, dirty it.
 947         __ stb(G0, tmp2, 0);  // [cardPtr] := 0  (i.e., dirty).
 948 
 949         Register tmp3 = cardtable;
 950         Register tmp4 = tmp;
 951 
 952         // these registers are now dead
 953         addr = cardtable = tmp = noreg;
 954 
 955         int dirty_card_q_index_byte_offset =
 956           in_bytes(JavaThread::dirty_card_queue_offset() +
 957                    PtrQueue::byte_offset_of_index());
 958         int dirty_card_q_buf_byte_offset =
 959           in_bytes(JavaThread::dirty_card_queue_offset() +
 960                    PtrQueue::byte_offset_of_buf());
 961         __ bind(restart);
 962         __ ld_ptr(G2_thread, dirty_card_q_index_byte_offset, tmp3);
 963 
 964         __ br_on_reg_cond(Assembler::rc_z, /*annul*/false, Assembler::pn,
 965                           tmp3, refill);
 966         // If the branch is taken, no harm in executing this in the delay slot.
 967         __ delayed()->ld_ptr(G2_thread, dirty_card_q_buf_byte_offset, tmp4);
 968         __ sub(tmp3, oopSize, tmp3);
 969 
 970         __ st_ptr(tmp2, tmp4, tmp3);  // [_buf + index] := <address_of_card>
 971         // Use return-from-leaf
 972         __ retl();
 973         __ delayed()->st_ptr(tmp3, G2_thread, dirty_card_q_index_byte_offset);
 974 
 975         __ bind(refill);
 976         __ save_frame(0);
 977 
 978         __ mov(tmp2, L0);
 979         __ mov(tmp3, L1);
 980         __ mov(tmp4, L2);
 981 
 982         __ call_VM_leaf(L7_thread_cache,
 983                         CAST_FROM_FN_PTR(address,
 984                                          DirtyCardQueueSet::handle_zero_index_for_thread),
 985                                          G2_thread);
 986 
 987         __ mov(L0, tmp2);
 988         __ mov(L1, tmp3);
 989         __ mov(L2, tmp4);
 990 
 991         __ br(Assembler::always, /*annul*/false, Assembler::pt, restart);
 992         __ delayed()->restore();
 993       }
 994       break;
 995 #endif // !SERIALGC
 996 
 997     default:
 998       { __ set_info("unimplemented entry", dont_gc_arguments);
 999         __ save_frame(0);
1000         __ set((int)id, O1);
1001         __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), O1);
1002         __ should_not_reach_here();
1003       }
1004       break;
1005   }
1006   return oop_maps;
1007 }
1008 
1009 
1010 void Runtime1::generate_handle_exception(StubAssembler* sasm, OopMapSet* oop_maps, OopMap* oop_map, bool) {
1011   Label no_deopt;
1012 
1013   __ verify_not_null_oop(Oexception);
1014 
1015   // save the exception and issuing pc in the thread
1016   __ st_ptr(Oexception, G2_thread, in_bytes(JavaThread::exception_oop_offset()));
1017   __ st_ptr(Oissuing_pc, G2_thread, in_bytes(JavaThread::exception_pc_offset()));
1018 
1019   // save the real return address and use the throwing pc as the return address to lookup (has bci & oop map)
1020   __ mov(I7, L0);
1021   __ mov(Oissuing_pc, I7);
1022   __ sub(I7, frame::pc_return_offset, I7);
1023   int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
1024 
1025   // Note: if nmethod has been deoptimized then regardless of
1026   // whether it had a handler or not we will deoptimize
1027   // by entering the deopt blob with a pending exception.
1028 
1029 #ifdef ASSERT
1030   Label done;
1031   __ tst(O0);
1032   __ br(Assembler::notZero, false, Assembler::pn, done);
1033   __ delayed()->nop();
1034   __ stop("should have found address");
1035   __ bind(done);
1036 #endif
1037 
1038   // restore the registers that were saved at the beginning and jump to the exception handler.
1039   restore_live_registers(sasm);
1040 
1041   __ jmp(O0, 0);
1042   __ delayed()->restore();
1043 
1044   oop_maps->add_gc_map(call_offset, oop_map);
1045 }
1046 
1047 
1048 #undef __
1049 
1050 #define __ masm->
1051 
1052 const char *Runtime1::pd_name_for_address(address entry) {
1053   return "<unknown function>";
1054 }