1 /*
   2  * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/assembler.hpp"
  28 #include "c1/c1_CodeStubs.hpp"
  29 #include "c1/c1_Defs.hpp"
  30 #include "c1/c1_MacroAssembler.hpp"
  31 #include "c1/c1_Runtime1.hpp"
  32 #include "compiler/disassembler.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "nativeInst_aarch64.hpp"
  35 #include "oops/compiledICHolder.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "prims/jvmtiExport.hpp"
  38 #include "register_aarch64.hpp"
  39 #include "runtime/sharedRuntime.hpp"
  40 #include "runtime/signature.hpp"
  41 #include "runtime/vframe.hpp"
  42 #include "runtime/vframeArray.hpp"
  43 #include "vmreg_aarch64.inline.hpp"
  44 #if INCLUDE_ALL_GCS
  45 #include "gc/g1/g1SATBCardTableModRefBS.hpp"
  46 #endif
  47 
  48 
  49 // Implementation of StubAssembler
  50 
  51 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, int args_size) {
  52   // setup registers
  53   assert(!(oop_result1->is_valid() || metadata_result->is_valid()) || oop_result1 != metadata_result, "registers must be different");
  54   assert(oop_result1 != rthread && metadata_result != rthread, "registers must be different");
  55   assert(args_size >= 0, "illegal args_size");
  56   bool align_stack = false;
  57 
  58   mov(c_rarg0, rthread);
  59   set_num_rt_args(0); // Nothing on stack
  60 
  61   Label retaddr;
  62   set_last_Java_frame(sp, rfp, retaddr, rscratch1);
  63 
  64   // do the call
  65   lea(rscratch1, RuntimeAddress(entry));
  66   blrt(rscratch1, args_size + 1, 8, 1);
  67   bind(retaddr);
  68   int call_offset = offset();
  69   // verify callee-saved register
  70 #ifdef ASSERT
  71   push(r0, sp);
  72   { Label L;
  73     get_thread(r0);
  74     cmp(rthread, r0);
  75     br(Assembler::EQ, L);
  76     stop("StubAssembler::call_RT: rthread not callee saved?");
  77     bind(L);
  78   }
  79   pop(r0, sp);
  80 #endif
  81   reset_last_Java_frame(true, true);
  82   maybe_isb();
  83 
  84   // check for pending exceptions
  85   { Label L;
  86     // check for pending exceptions (java_thread is set upon return)
  87     ldr(rscratch1, Address(rthread, in_bytes(Thread::pending_exception_offset())));
  88     cbz(rscratch1, L);
  89     // exception pending => remove activation and forward to exception handler
  90     // make sure that the vm_results are cleared
  91     if (oop_result1->is_valid()) {
  92       str(zr, Address(rthread, JavaThread::vm_result_offset()));
  93     }
  94     if (metadata_result->is_valid()) {
  95       str(zr, Address(rthread, JavaThread::vm_result_2_offset()));
  96     }
  97     if (frame_size() == no_frame_size) {
  98       leave();
  99       far_jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
 100     } else if (_stub_id == Runtime1::forward_exception_id) {
 101       should_not_reach_here();
 102     } else {
 103       far_jump(RuntimeAddress(Runtime1::entry_for(Runtime1::forward_exception_id)));
 104     }
 105     bind(L);
 106   }
 107   // get oop results if there are any and reset the values in the thread
 108   if (oop_result1->is_valid()) {
 109     get_vm_result(oop_result1, rthread);
 110   }
 111   if (metadata_result->is_valid()) {
 112     get_vm_result_2(metadata_result, rthread);
 113   }
 114   return call_offset;
 115 }
 116 
 117 
 118 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1) {
 119   mov(c_rarg1, arg1);
 120   return call_RT(oop_result1, metadata_result, entry, 1);
 121 }
 122 
 123 
 124 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2) {
 125   if (c_rarg1 == arg2) {
 126     if (c_rarg2 == arg1) {
 127       mov(rscratch1, arg1);
 128       mov(arg1, arg2);
 129       mov(arg2, rscratch1);
 130     } else {
 131       mov(c_rarg2, arg2);
 132       mov(c_rarg1, arg1);
 133     }
 134   } else {
 135     mov(c_rarg1, arg1);
 136     mov(c_rarg2, arg2);
 137   }
 138   return call_RT(oop_result1, metadata_result, entry, 2);
 139 }
 140 
 141 
 142 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2, Register arg3) {
 143   // if there is any conflict use the stack
 144   if (arg1 == c_rarg2 || arg1 == c_rarg3 ||
 145       arg2 == c_rarg1 || arg1 == c_rarg3 ||
 146       arg3 == c_rarg1 || arg1 == c_rarg2) {
 147     stp(arg3, arg2, Address(pre(sp, 2 * wordSize)));
 148     stp(arg1, zr, Address(pre(sp, -2 * wordSize)));
 149     ldp(c_rarg1, zr, Address(post(sp, 2 * wordSize)));
 150     ldp(c_rarg3, c_rarg2, Address(post(sp, 2 * wordSize)));
 151   } else {
 152     mov(c_rarg1, arg1);
 153     mov(c_rarg2, arg2);
 154     mov(c_rarg3, arg3);
 155   }
 156   return call_RT(oop_result1, metadata_result, entry, 3);
 157 }
 158 
 159 // Implementation of StubFrame
 160 
 161 class StubFrame: public StackObj {
 162  private:
 163   StubAssembler* _sasm;
 164 
 165  public:
 166   StubFrame(StubAssembler* sasm, const char* name, bool must_gc_arguments);
 167   void load_argument(int offset_in_words, Register reg);
 168 
 169   ~StubFrame();
 170 };;
 171 
 172 
 173 #define __ _sasm->
 174 
 175 StubFrame::StubFrame(StubAssembler* sasm, const char* name, bool must_gc_arguments) {
 176   _sasm = sasm;
 177   __ set_info(name, must_gc_arguments);
 178   __ enter();
 179 }
 180 
 181 // load parameters that were stored with LIR_Assembler::store_parameter
 182 // Note: offsets for store_parameter and load_argument must match
 183 void StubFrame::load_argument(int offset_in_words, Register reg) {
 184   // rbp, + 0: link
 185   //     + 1: return address
 186   //     + 2: argument with offset 0
 187   //     + 3: argument with offset 1
 188   //     + 4: ...
 189 
 190   __ ldr(reg, Address(rfp, (offset_in_words + 2) * BytesPerWord));
 191 }
 192 
 193 
 194 StubFrame::~StubFrame() {
 195   __ leave();
 196   __ ret(lr);
 197 }
 198 
 199 #undef __
 200 
 201 
 202 // Implementation of Runtime1
 203 
 204 #define __ sasm->
 205 
 206 const int float_regs_as_doubles_size_in_slots = pd_nof_fpu_regs_frame_map * 2;
 207 
 208 // Stack layout for saving/restoring  all the registers needed during a runtime
 209 // call (this includes deoptimization)
 210 // Note: note that users of this frame may well have arguments to some runtime
 211 // while these values are on the stack. These positions neglect those arguments
 212 // but the code in save_live_registers will take the argument count into
 213 // account.
 214 //
 215 
 216 enum reg_save_layout {
 217   reg_save_frame_size = 32 /* float */ + 32 /* integer */
 218 };
 219 
 220 // Save off registers which might be killed by calls into the runtime.
 221 // Tries to smart of about FP registers.  In particular we separate
 222 // saving and describing the FPU registers for deoptimization since we
 223 // have to save the FPU registers twice if we describe them.  The
 224 // deopt blob is the only thing which needs to describe FPU registers.
 225 // In all other cases it should be sufficient to simply save their
 226 // current value.
 227 
 228 static int cpu_reg_save_offsets[FrameMap::nof_cpu_regs];
 229 static int fpu_reg_save_offsets[FrameMap::nof_fpu_regs];
 230 static int reg_save_size_in_words;
 231 static int frame_size_in_bytes = -1;
 232 
 233 static OopMap* generate_oop_map(StubAssembler* sasm, bool save_fpu_registers) {
 234   int frame_size_in_bytes = reg_save_frame_size * BytesPerWord;
 235   sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
 236   int frame_size_in_slots = frame_size_in_bytes / sizeof(jint);
 237   OopMap* oop_map = new OopMap(frame_size_in_slots, 0);
 238 
 239   for (int i = 0; i < FrameMap::nof_cpu_regs; i++) {
 240     Register r = as_Register(i);
 241     if (i <= 18 && i != rscratch1->encoding() && i != rscratch2->encoding()) {
 242       int sp_offset = cpu_reg_save_offsets[i];
 243       oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset),
 244                                 r->as_VMReg());
 245     }
 246   }
 247 
 248   if (save_fpu_registers) {
 249     for (int i = 0; i < FrameMap::nof_fpu_regs; i++) {
 250       FloatRegister r = as_FloatRegister(i);
 251       {
 252         int sp_offset = fpu_reg_save_offsets[i];
 253         oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset),
 254                                   r->as_VMReg());
 255       }
 256     }
 257   }
 258   return oop_map;
 259 }
 260 
 261 static OopMap* save_live_registers(StubAssembler* sasm,
 262                                    bool save_fpu_registers = true) {
 263   __ block_comment("save_live_registers");
 264 
 265   __ push(RegSet::range(r0, r29), sp);         // integer registers except lr & sp
 266 
 267   if (save_fpu_registers) {
 268     for (int i = 30; i >= 0; i -= 2)
 269       __ stpd(as_FloatRegister(i), as_FloatRegister(i+1),
 270               Address(__ pre(sp, -2 * wordSize)));
 271   } else {
 272     __ add(sp, sp, -32 * wordSize);
 273   }
 274 
 275   return generate_oop_map(sasm, save_fpu_registers);
 276 }
 277 
 278 static void restore_live_registers(StubAssembler* sasm, bool restore_fpu_registers = true) {
 279   if (restore_fpu_registers) {
 280     for (int i = 0; i < 32; i += 2)
 281       __ ldpd(as_FloatRegister(i), as_FloatRegister(i+1),
 282               Address(__ post(sp, 2 * wordSize)));
 283   } else {
 284     __ add(sp, sp, 32 * wordSize);
 285   }
 286 
 287   __ pop(RegSet::range(r0, r29), sp);
 288 }
 289 
 290 static void restore_live_registers_except_r0(StubAssembler* sasm, bool restore_fpu_registers = true)  {
 291 
 292   if (restore_fpu_registers) {
 293     for (int i = 0; i < 32; i += 2)
 294       __ ldpd(as_FloatRegister(i), as_FloatRegister(i+1),
 295               Address(__ post(sp, 2 * wordSize)));
 296   } else {
 297     __ add(sp, sp, 32 * wordSize);
 298   }
 299 
 300   __ ldp(zr, r1, Address(__ post(sp, 16)));
 301   __ pop(RegSet::range(r2, r29), sp);
 302 }
 303 
 304 
 305 
 306 void Runtime1::initialize_pd() {
 307   int i;
 308   int sp_offset = 0;
 309 
 310   // all float registers are saved explicitly
 311   assert(FrameMap::nof_fpu_regs == 32, "double registers not handled here");
 312   for (i = 0; i < FrameMap::nof_fpu_regs; i++) {
 313     fpu_reg_save_offsets[i] = sp_offset;
 314     sp_offset += 2;   // SP offsets are in halfwords
 315   }
 316 
 317   for (i = 0; i < FrameMap::nof_cpu_regs; i++) {
 318     Register r = as_Register(i);
 319     cpu_reg_save_offsets[i] = sp_offset;
 320     sp_offset += 2;   // SP offsets are in halfwords
 321   }
 322 }
 323 
 324 
 325 // target: the entry point of the method that creates and posts the exception oop
 326 // has_argument: true if the exception needs an argument (passed in rscratch1)
 327 
 328 OopMapSet* Runtime1::generate_exception_throw(StubAssembler* sasm, address target, bool has_argument) {
 329   // make a frame and preserve the caller's caller-save registers
 330   OopMap* oop_map = save_live_registers(sasm);
 331   int call_offset;
 332   if (!has_argument) {
 333     call_offset = __ call_RT(noreg, noreg, target);
 334   } else {
 335     call_offset = __ call_RT(noreg, noreg, target, rscratch1);
 336   }
 337   OopMapSet* oop_maps = new OopMapSet();
 338   oop_maps->add_gc_map(call_offset, oop_map);
 339 
 340   __ should_not_reach_here();
 341   return oop_maps;
 342 }
 343 
 344 
 345 OopMapSet* Runtime1::generate_handle_exception(StubID id, StubAssembler *sasm) {
 346   __ block_comment("generate_handle_exception");
 347 
 348   // incoming parameters
 349   const Register exception_oop = r0;
 350   const Register exception_pc  = r3;
 351   // other registers used in this stub
 352 
 353   // Save registers, if required.
 354   OopMapSet* oop_maps = new OopMapSet();
 355   OopMap* oop_map = NULL;
 356   switch (id) {
 357   case forward_exception_id:
 358     // We're handling an exception in the context of a compiled frame.
 359     // The registers have been saved in the standard places.  Perform
 360     // an exception lookup in the caller and dispatch to the handler
 361     // if found.  Otherwise unwind and dispatch to the callers
 362     // exception handler.
 363     oop_map = generate_oop_map(sasm, 1 /*thread*/);
 364 
 365     // load and clear pending exception oop into r0
 366     __ ldr(exception_oop, Address(rthread, Thread::pending_exception_offset()));
 367     __ str(zr, Address(rthread, Thread::pending_exception_offset()));
 368 
 369     // load issuing PC (the return address for this stub) into r3
 370     __ ldr(exception_pc, Address(rfp, 1*BytesPerWord));
 371 
 372     // make sure that the vm_results are cleared (may be unnecessary)
 373     __ str(zr, Address(rthread, JavaThread::vm_result_offset()));
 374     __ str(zr, Address(rthread, JavaThread::vm_result_2_offset()));
 375     break;
 376   case handle_exception_nofpu_id:
 377   case handle_exception_id:
 378     // At this point all registers MAY be live.
 379     oop_map = save_live_registers(sasm, id != handle_exception_nofpu_id);
 380     break;
 381   case handle_exception_from_callee_id: {
 382     // At this point all registers except exception oop (r0) and
 383     // exception pc (lr) are dead.
 384     const int frame_size = 2 /*fp, return address*/;
 385     oop_map = new OopMap(frame_size * VMRegImpl::slots_per_word, 0);
 386     sasm->set_frame_size(frame_size);
 387     break;
 388   }
 389   default:
 390     __ should_not_reach_here();
 391     break;
 392   }
 393 
 394   // verify that only r0 and r3 are valid at this time
 395   __ invalidate_registers(false, true, true, false, true, true);
 396   // verify that r0 contains a valid exception
 397   __ verify_not_null_oop(exception_oop);
 398 
 399 #ifdef ASSERT
 400   // check that fields in JavaThread for exception oop and issuing pc are
 401   // empty before writing to them
 402   Label oop_empty;
 403   __ ldr(rscratch1, Address(rthread, JavaThread::exception_oop_offset()));
 404   __ cbz(rscratch1, oop_empty);
 405   __ stop("exception oop already set");
 406   __ bind(oop_empty);
 407 
 408   Label pc_empty;
 409   __ ldr(rscratch1, Address(rthread, JavaThread::exception_pc_offset()));
 410   __ cbz(rscratch1, pc_empty);
 411   __ stop("exception pc already set");
 412   __ bind(pc_empty);
 413 #endif
 414 
 415   // save exception oop and issuing pc into JavaThread
 416   // (exception handler will load it from here)
 417   __ str(exception_oop, Address(rthread, JavaThread::exception_oop_offset()));
 418   __ str(exception_pc, Address(rthread, JavaThread::exception_pc_offset()));
 419 
 420   // patch throwing pc into return address (has bci & oop map)
 421   __ str(exception_pc, Address(rfp, 1*BytesPerWord));
 422 
 423   // compute the exception handler.
 424   // the exception oop and the throwing pc are read from the fields in JavaThread
 425   int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
 426   oop_maps->add_gc_map(call_offset, oop_map);
 427 
 428   // r0: handler address
 429   //      will be the deopt blob if nmethod was deoptimized while we looked up
 430   //      handler regardless of whether handler existed in the nmethod.
 431 
 432   // only r0 is valid at this time, all other registers have been destroyed by the runtime call
 433   __ invalidate_registers(false, true, true, true, true, true);
 434 
 435   // patch the return address, this stub will directly return to the exception handler
 436   __ str(r0, Address(rfp, 1*BytesPerWord));
 437 
 438   switch (id) {
 439   case forward_exception_id:
 440   case handle_exception_nofpu_id:
 441   case handle_exception_id:
 442     // Restore the registers that were saved at the beginning.
 443     restore_live_registers(sasm, id != handle_exception_nofpu_id);
 444     break;
 445   case handle_exception_from_callee_id:
 446     // Pop the return address since we are possibly changing SP (restoring from BP).
 447     __ leave();
 448 
 449     // Restore SP from FP if the exception PC is a method handle call site.
 450     {
 451       Label nope;
 452       __ ldrw(rscratch1, Address(rthread, JavaThread::is_method_handle_return_offset()));
 453       __ cbzw(rscratch1, nope);
 454       __ mov(sp, rfp);
 455       __ bind(nope);
 456     }
 457 
 458     __ ret(lr);  // jump to exception handler
 459     break;
 460   default:  ShouldNotReachHere();
 461   }
 462 
 463   return oop_maps;
 464 }
 465 
 466 
 467 void Runtime1::generate_unwind_exception(StubAssembler *sasm) {
 468   // incoming parameters
 469   const Register exception_oop = r0;
 470   // callee-saved copy of exception_oop during runtime call
 471   const Register exception_oop_callee_saved = r19;
 472   // other registers used in this stub
 473   const Register exception_pc = r3;
 474   const Register handler_addr = r1;
 475 
 476   // verify that only r0, is valid at this time
 477   __ invalidate_registers(false, true, true, true, true, true);
 478 
 479 #ifdef ASSERT
 480   // check that fields in JavaThread for exception oop and issuing pc are empty
 481   Label oop_empty;
 482   __ ldr(rscratch1, Address(rthread, JavaThread::exception_oop_offset()));
 483   __ cbz(rscratch1, oop_empty);
 484   __ stop("exception oop must be empty");
 485   __ bind(oop_empty);
 486 
 487   Label pc_empty;
 488   __ ldr(rscratch1, Address(rthread, JavaThread::exception_pc_offset()));
 489   __ cbz(rscratch1, pc_empty);
 490   __ stop("exception pc must be empty");
 491   __ bind(pc_empty);
 492 #endif
 493 
 494   // Save our return address because
 495   // exception_handler_for_return_address will destroy it.  We also
 496   // save exception_oop
 497   __ stp(lr, exception_oop, Address(__ pre(sp, -2 * wordSize)));
 498 
 499   // search the exception handler address of the caller (using the return address)
 500   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), rthread, lr);
 501   // r0: exception handler address of the caller
 502 
 503   // Only R0 is valid at this time; all other registers have been
 504   // destroyed by the call.
 505   __ invalidate_registers(false, true, true, true, false, true);
 506 
 507   // move result of call into correct register
 508   __ mov(handler_addr, r0);
 509 
 510   // get throwing pc (= return address).
 511   // lr has been destroyed by the call
 512   __ ldp(lr, exception_oop, Address(__ post(sp, 2 * wordSize)));
 513   __ mov(r3, lr);
 514 
 515   __ verify_not_null_oop(exception_oop);
 516 
 517   {
 518     Label foo;
 519     __ ldrw(rscratch1, Address(rthread, JavaThread::is_method_handle_return_offset()));
 520     __ cbzw(rscratch1, foo);
 521     __ mov(sp, rfp);
 522     __ bind(foo);
 523   }
 524 
 525   // continue at exception handler (return address removed)
 526   // note: do *not* remove arguments when unwinding the
 527   //       activation since the caller assumes having
 528   //       all arguments on the stack when entering the
 529   //       runtime to determine the exception handler
 530   //       (GC happens at call site with arguments!)
 531   // r0: exception oop
 532   // r3: throwing pc
 533   // r1: exception handler
 534   __ br(handler_addr);
 535 }
 536 
 537 
 538 
 539 OopMapSet* Runtime1::generate_patching(StubAssembler* sasm, address target) {
 540   // use the maximum number of runtime-arguments here because it is difficult to
 541   // distinguish each RT-Call.
 542   // Note: This number affects also the RT-Call in generate_handle_exception because
 543   //       the oop-map is shared for all calls.
 544   DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
 545   assert(deopt_blob != NULL, "deoptimization blob must have been created");
 546 
 547   OopMap* oop_map = save_live_registers(sasm);
 548 
 549   __ mov(c_rarg0, rthread);
 550   Label retaddr;
 551   __ set_last_Java_frame(sp, rfp, retaddr, rscratch1);
 552   // do the call
 553   __ lea(rscratch1, RuntimeAddress(target));
 554   __ blrt(rscratch1, 1, 0, 1);
 555   __ bind(retaddr);
 556   OopMapSet* oop_maps = new OopMapSet();
 557   oop_maps->add_gc_map(__ offset(), oop_map);
 558   // verify callee-saved register
 559 #ifdef ASSERT
 560   { Label L;
 561     __ get_thread(rscratch1);
 562     __ cmp(rthread, rscratch1);
 563     __ br(Assembler::EQ, L);
 564     __ stop("StubAssembler::call_RT: rthread not callee saved?");
 565     __ bind(L);
 566   }
 567 #endif
 568   __ reset_last_Java_frame(true, false);
 569   __ maybe_isb();
 570 
 571   // check for pending exceptions
 572   { Label L;
 573     __ ldr(rscratch1, Address(rthread, Thread::pending_exception_offset()));
 574     __ cbz(rscratch1, L);
 575     // exception pending => remove activation and forward to exception handler
 576 
 577     { Label L1;
 578       __ cbnz(r0, L1);                                  // have we deoptimized?
 579       __ far_jump(RuntimeAddress(Runtime1::entry_for(Runtime1::forward_exception_id)));
 580       __ bind(L1);
 581     }
 582 
 583     // the deopt blob expects exceptions in the special fields of
 584     // JavaThread, so copy and clear pending exception.
 585 
 586     // load and clear pending exception
 587     __ ldr(r0, Address(rthread, Thread::pending_exception_offset()));
 588     __ str(zr, Address(rthread, Thread::pending_exception_offset()));
 589 
 590     // check that there is really a valid exception
 591     __ verify_not_null_oop(r0);
 592 
 593     // load throwing pc: this is the return address of the stub
 594     __ mov(r3, lr);
 595 
 596 #ifdef ASSERT
 597     // check that fields in JavaThread for exception oop and issuing pc are empty
 598     Label oop_empty;
 599     __ ldr(rscratch1, Address(rthread, Thread::pending_exception_offset()));
 600     __ cbz(rscratch1, oop_empty);
 601     __ stop("exception oop must be empty");
 602     __ bind(oop_empty);
 603 
 604     Label pc_empty;
 605     __ ldr(rscratch1, Address(rthread, JavaThread::exception_pc_offset()));
 606     __ cbz(rscratch1, pc_empty);
 607     __ stop("exception pc must be empty");
 608     __ bind(pc_empty);
 609 #endif
 610 
 611     // store exception oop and throwing pc to JavaThread
 612     __ str(r0, Address(rthread, JavaThread::exception_oop_offset()));
 613     __ str(r3, Address(rthread, JavaThread::exception_pc_offset()));
 614 
 615     restore_live_registers(sasm);
 616 
 617     __ leave();
 618 
 619     // Forward the exception directly to deopt blob. We can blow no
 620     // registers and must leave throwing pc on the stack.  A patch may
 621     // have values live in registers so the entry point with the
 622     // exception in tls.
 623     __ far_jump(RuntimeAddress(deopt_blob->unpack_with_exception_in_tls()));
 624 
 625     __ bind(L);
 626   }
 627 
 628 
 629   // Runtime will return true if the nmethod has been deoptimized during
 630   // the patching process. In that case we must do a deopt reexecute instead.
 631 
 632   Label reexecuteEntry, cont;
 633 
 634   __ cbz(r0, cont);                                 // have we deoptimized?
 635 
 636   // Will reexecute. Proper return address is already on the stack we just restore
 637   // registers, pop all of our frame but the return address and jump to the deopt blob
 638   restore_live_registers(sasm);
 639   __ leave();
 640   __ far_jump(RuntimeAddress(deopt_blob->unpack_with_reexecution()));
 641 
 642   __ bind(cont);
 643   restore_live_registers(sasm);
 644   __ leave();
 645   __ ret(lr);
 646 
 647   return oop_maps;
 648 }
 649 
 650 
 651 OopMapSet* Runtime1::generate_code_for(StubID id, StubAssembler* sasm) {
 652 
 653   const Register exception_oop = r0;
 654   const Register exception_pc  = r3;
 655 
 656   // for better readability
 657   const bool must_gc_arguments = true;
 658   const bool dont_gc_arguments = false;
 659 
 660   // default value; overwritten for some optimized stubs that are called from methods that do not use the fpu
 661   bool save_fpu_registers = true;
 662 
 663   // stub code & info for the different stubs
 664   OopMapSet* oop_maps = NULL;
 665   OopMap* oop_map = NULL;
 666   switch (id) {
 667     {
 668     case forward_exception_id:
 669       {
 670         oop_maps = generate_handle_exception(id, sasm);
 671         __ leave();
 672         __ ret(lr);
 673       }
 674       break;
 675 
 676     case throw_div0_exception_id:
 677       { StubFrame f(sasm, "throw_div0_exception", dont_gc_arguments);
 678         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_div0_exception), false);
 679       }
 680       break;
 681 
 682     case throw_null_pointer_exception_id:
 683       { StubFrame f(sasm, "throw_null_pointer_exception", dont_gc_arguments);
 684         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_null_pointer_exception), false);
 685       }
 686       break;
 687 
 688     case new_instance_id:
 689     case fast_new_instance_id:
 690     case fast_new_instance_init_check_id:
 691       {
 692         Register klass = r3; // Incoming
 693         Register obj   = r0; // Result
 694 
 695         if (id == new_instance_id) {
 696           __ set_info("new_instance", dont_gc_arguments);
 697         } else if (id == fast_new_instance_id) {
 698           __ set_info("fast new_instance", dont_gc_arguments);
 699         } else {
 700           assert(id == fast_new_instance_init_check_id, "bad StubID");
 701           __ set_info("fast new_instance init check", dont_gc_arguments);
 702         }
 703 
 704         if ((id == fast_new_instance_id || id == fast_new_instance_init_check_id) &&
 705             UseTLAB && FastTLABRefill) {
 706           Label slow_path;
 707           Register obj_size = r2;
 708           Register t1       = r19;
 709           Register t2       = r4;
 710           assert_different_registers(klass, obj, obj_size, t1, t2);
 711 
 712           __ stp(r5, r19, Address(__ pre(sp, -2 * wordSize)));
 713 
 714           if (id == fast_new_instance_init_check_id) {
 715             // make sure the klass is initialized
 716             __ ldrb(rscratch1, Address(klass, InstanceKlass::init_state_offset()));
 717             __ cmpw(rscratch1, InstanceKlass::fully_initialized);
 718             __ br(Assembler::NE, slow_path);
 719           }
 720 
 721 #ifdef ASSERT
 722           // assert object can be fast path allocated
 723           {
 724             Label ok, not_ok;
 725             __ ldrw(obj_size, Address(klass, Klass::layout_helper_offset()));
 726             __ cmp(obj_size, 0u);
 727             __ br(Assembler::LE, not_ok);  // make sure it's an instance (LH > 0)
 728             __ tstw(obj_size, Klass::_lh_instance_slow_path_bit);
 729             __ br(Assembler::EQ, ok);
 730             __ bind(not_ok);
 731             __ stop("assert(can be fast path allocated)");
 732             __ should_not_reach_here();
 733             __ bind(ok);
 734           }
 735 #endif // ASSERT
 736 
 737           // if we got here then the TLAB allocation failed, so try
 738           // refilling the TLAB or allocating directly from eden.
 739           Label retry_tlab, try_eden;
 740           __ tlab_refill(retry_tlab, try_eden, slow_path); // does not destroy r3 (klass), returns r5
 741 
 742           __ bind(retry_tlab);
 743 
 744           // get the instance size (size is postive so movl is fine for 64bit)
 745           __ ldrw(obj_size, Address(klass, Klass::layout_helper_offset()));
 746 
 747           __ tlab_allocate(obj, obj_size, 0, t1, t2, slow_path);
 748 
 749           __ initialize_object(obj, klass, obj_size, 0, t1, t2);
 750           __ verify_oop(obj);
 751           __ ldp(r5, r19, Address(__ post(sp, 2 * wordSize)));
 752           __ ret(lr);
 753 
 754           __ bind(try_eden);
 755           // get the instance size (size is postive so movl is fine for 64bit)
 756           __ ldrw(obj_size, Address(klass, Klass::layout_helper_offset()));
 757 
 758           __ eden_allocate(obj, obj_size, 0, t1, slow_path);
 759           __ incr_allocated_bytes(rthread, obj_size, 0, rscratch1);
 760 
 761           __ initialize_object(obj, klass, obj_size, 0, t1, t2);
 762           __ verify_oop(obj);
 763           __ ldp(r5, r19, Address(__ post(sp, 2 * wordSize)));
 764           __ ret(lr);
 765 
 766           __ bind(slow_path);
 767           __ ldp(r5, r19, Address(__ post(sp, 2 * wordSize)));
 768         }
 769 
 770         __ enter();
 771         OopMap* map = save_live_registers(sasm);
 772         int call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_instance), klass);
 773         oop_maps = new OopMapSet();
 774         oop_maps->add_gc_map(call_offset, map);
 775         restore_live_registers_except_r0(sasm);
 776         __ verify_oop(obj);
 777         __ leave();
 778         __ ret(lr);
 779 
 780         // r0,: new instance
 781       }
 782 
 783       break;
 784 
 785     case counter_overflow_id:
 786       {
 787         Register bci = r0, method = r1;
 788         __ enter();
 789         OopMap* map = save_live_registers(sasm);
 790         // Retrieve bci
 791         __ ldrw(bci, Address(rfp, 2*BytesPerWord));
 792         // And a pointer to the Method*
 793         __ ldr(method, Address(rfp, 3*BytesPerWord));
 794         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, counter_overflow), bci, method);
 795         oop_maps = new OopMapSet();
 796         oop_maps->add_gc_map(call_offset, map);
 797         restore_live_registers(sasm);
 798         __ leave();
 799         __ ret(lr);
 800       }
 801       break;
 802 
 803     case new_type_array_id:
 804     case new_object_array_id:
 805       {
 806         Register length   = r19; // Incoming
 807         Register klass    = r3; // Incoming
 808         Register obj      = r0; // Result
 809 
 810         if (id == new_type_array_id) {
 811           __ set_info("new_type_array", dont_gc_arguments);
 812         } else {
 813           __ set_info("new_object_array", dont_gc_arguments);
 814         }
 815 
 816 #ifdef ASSERT
 817         // assert object type is really an array of the proper kind
 818         {
 819           Label ok;
 820           Register t0 = obj;
 821           __ ldrw(t0, Address(klass, Klass::layout_helper_offset()));
 822           __ asrw(t0, t0, Klass::_lh_array_tag_shift);
 823           int tag = ((id == new_type_array_id)
 824                      ? Klass::_lh_array_tag_type_value
 825                      : Klass::_lh_array_tag_obj_value);
 826           __ mov(rscratch1, tag);
 827           __ cmpw(t0, rscratch1);
 828           __ br(Assembler::EQ, ok);
 829           __ stop("assert(is an array klass)");
 830           __ should_not_reach_here();
 831           __ bind(ok);
 832         }
 833 #endif // ASSERT
 834 
 835         if (UseTLAB && FastTLABRefill) {
 836           Register arr_size = r4;
 837           Register t1       = r2;
 838           Register t2       = r5;
 839           Label slow_path;
 840           assert_different_registers(length, klass, obj, arr_size, t1, t2);
 841 
 842           // check that array length is small enough for fast path.
 843           __ mov(rscratch1, C1_MacroAssembler::max_array_allocation_length);
 844           __ cmpw(length, rscratch1);
 845           __ br(Assembler::HI, slow_path);
 846 
 847           // if we got here then the TLAB allocation failed, so try
 848           // refilling the TLAB or allocating directly from eden.
 849           Label retry_tlab, try_eden;
 850           const Register thread =
 851             __ tlab_refill(retry_tlab, try_eden, slow_path); // preserves r19 & r3, returns rthread
 852 
 853           __ bind(retry_tlab);
 854 
 855           // get the allocation size: round_up(hdr + length << (layout_helper & 0x1F))
 856           // since size is positive ldrw does right thing on 64bit
 857           __ ldrw(t1, Address(klass, Klass::layout_helper_offset()));
 858           __ lslvw(arr_size, length, t1);
 859           __ ubfx(t1, t1, Klass::_lh_header_size_shift,
 860                   exact_log2(Klass::_lh_header_size_mask + 1));
 861           __ add(arr_size, arr_size, t1);
 862           __ add(arr_size, arr_size, MinObjAlignmentInBytesMask); // align up
 863           __ andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask);
 864 
 865           __ tlab_allocate(obj, arr_size, 0, t1, t2, slow_path);  // preserves arr_size
 866 
 867           __ initialize_header(obj, klass, length, t1, t2);
 868           __ ldrb(t1, Address(klass, in_bytes(Klass::layout_helper_offset()) + (Klass::_lh_header_size_shift / BitsPerByte)));
 869           assert(Klass::_lh_header_size_shift % BitsPerByte == 0, "bytewise");
 870           assert(Klass::_lh_header_size_mask <= 0xFF, "bytewise");
 871           __ andr(t1, t1, Klass::_lh_header_size_mask);
 872           __ sub(arr_size, arr_size, t1);  // body length
 873           __ add(t1, t1, obj);       // body start
 874           __ initialize_body(t1, arr_size, 0, t2);
 875           __ verify_oop(obj);
 876 
 877           __ ret(lr);
 878 
 879           __ bind(try_eden);
 880           // get the allocation size: round_up(hdr + length << (layout_helper & 0x1F))
 881           // since size is positive ldrw does right thing on 64bit
 882           __ ldrw(t1, Address(klass, Klass::layout_helper_offset()));
 883           // since size is postive movw does right thing on 64bit
 884           __ movw(arr_size, length);
 885           __ lslvw(arr_size, length, t1);
 886           __ ubfx(t1, t1, Klass::_lh_header_size_shift,
 887                   exact_log2(Klass::_lh_header_size_mask + 1));
 888           __ add(arr_size, arr_size, t1);
 889           __ add(arr_size, arr_size, MinObjAlignmentInBytesMask); // align up
 890           __ andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask);
 891 
 892           __ eden_allocate(obj, arr_size, 0, t1, slow_path);  // preserves arr_size
 893           __ incr_allocated_bytes(thread, arr_size, 0, rscratch1);
 894 
 895           __ initialize_header(obj, klass, length, t1, t2);
 896           __ ldrb(t1, Address(klass, in_bytes(Klass::layout_helper_offset()) + (Klass::_lh_header_size_shift / BitsPerByte)));
 897           assert(Klass::_lh_header_size_shift % BitsPerByte == 0, "bytewise");
 898           assert(Klass::_lh_header_size_mask <= 0xFF, "bytewise");
 899           __ andr(t1, t1, Klass::_lh_header_size_mask);
 900           __ sub(arr_size, arr_size, t1);  // body length
 901           __ add(t1, t1, obj);       // body start
 902           __ initialize_body(t1, arr_size, 0, t2);
 903           __ verify_oop(obj);
 904 
 905           __ ret(lr);
 906 
 907           __ bind(slow_path);
 908         }
 909 
 910         __ enter();
 911         OopMap* map = save_live_registers(sasm);
 912         int call_offset;
 913         if (id == new_type_array_id) {
 914           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_type_array), klass, length);
 915         } else {
 916           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_object_array), klass, length);
 917         }
 918 
 919         oop_maps = new OopMapSet();
 920         oop_maps->add_gc_map(call_offset, map);
 921         restore_live_registers_except_r0(sasm);
 922 
 923         __ verify_oop(obj);
 924         __ leave();
 925         __ ret(lr);
 926 
 927         // r0: new array
 928       }
 929       break;
 930 
 931     case new_multi_array_id:
 932       { StubFrame f(sasm, "new_multi_array", dont_gc_arguments);
 933         // r0,: klass
 934         // r19,: rank
 935         // r2: address of 1st dimension
 936         OopMap* map = save_live_registers(sasm);
 937         __ mov(c_rarg1, r0);
 938         __ mov(c_rarg3, r2);
 939         __ mov(c_rarg2, r19);
 940         int call_offset = __ call_RT(r0, noreg, CAST_FROM_FN_PTR(address, new_multi_array), r1, r2, r3);
 941 
 942         oop_maps = new OopMapSet();
 943         oop_maps->add_gc_map(call_offset, map);
 944         restore_live_registers_except_r0(sasm);
 945 
 946         // r0,: new multi array
 947         __ verify_oop(r0);
 948       }
 949       break;
 950 
 951     case register_finalizer_id:
 952       {
 953         __ set_info("register_finalizer", dont_gc_arguments);
 954 
 955         // This is called via call_runtime so the arguments
 956         // will be place in C abi locations
 957 
 958         __ verify_oop(c_rarg0);
 959 
 960         // load the klass and check the has finalizer flag
 961         Label register_finalizer;
 962         Register t = r5;
 963         __ load_klass(t, r0);
 964         __ ldrw(t, Address(t, Klass::access_flags_offset()));
 965         __ tst(t, JVM_ACC_HAS_FINALIZER);
 966         __ br(Assembler::NE, register_finalizer);
 967         __ ret(lr);
 968 
 969         __ bind(register_finalizer);
 970         __ enter();
 971         OopMap* oop_map = save_live_registers(sasm);
 972         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), r0);
 973         oop_maps = new OopMapSet();
 974         oop_maps->add_gc_map(call_offset, oop_map);
 975 
 976         // Now restore all the live registers
 977         restore_live_registers(sasm);
 978 
 979         __ leave();
 980         __ ret(lr);
 981       }
 982       break;
 983 
 984     case throw_class_cast_exception_id:
 985       { StubFrame f(sasm, "throw_class_cast_exception", dont_gc_arguments);
 986         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
 987       }
 988       break;
 989 
 990     case throw_incompatible_class_change_error_id:
 991       { StubFrame f(sasm, "throw_incompatible_class_cast_exception", dont_gc_arguments);
 992         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
 993       }
 994       break;
 995 
 996     case slow_subtype_check_id:
 997       {
 998         // Typical calling sequence:
 999         // __ push(klass_RInfo);  // object klass or other subclass
1000         // __ push(sup_k_RInfo);  // array element klass or other superclass
1001         // __ bl(slow_subtype_check);
1002         // Note that the subclass is pushed first, and is therefore deepest.
1003         enum layout {
1004           r0_off, r0_off_hi,
1005           r2_off, r2_off_hi,
1006           r4_off, r4_off_hi,
1007           r5_off, r5_off_hi,
1008           sup_k_off, sup_k_off_hi,
1009           klass_off, klass_off_hi,
1010           framesize,
1011           result_off = sup_k_off
1012         };
1013 
1014         __ set_info("slow_subtype_check", dont_gc_arguments);
1015         __ push(RegSet::of(r0, r2, r4, r5), sp);
1016 
1017         // This is called by pushing args and not with C abi
1018         // __ ldr(r4, Address(sp, (klass_off) * VMRegImpl::stack_slot_size)); // subclass
1019         // __ ldr(r0, Address(sp, (sup_k_off) * VMRegImpl::stack_slot_size)); // superclass
1020 
1021         __ ldp(r4, r0, Address(sp, (sup_k_off) * VMRegImpl::stack_slot_size));
1022 
1023         Label miss;
1024         __ check_klass_subtype_slow_path(r4, r0, r2, r5, NULL, &miss);
1025 
1026         // fallthrough on success:
1027         __ mov(rscratch1, 1);
1028         __ str(rscratch1, Address(sp, (result_off) * VMRegImpl::stack_slot_size)); // result
1029         __ pop(RegSet::of(r0, r2, r4, r5), sp);
1030         __ ret(lr);
1031 
1032         __ bind(miss);
1033         __ str(zr, Address(sp, (result_off) * VMRegImpl::stack_slot_size)); // result
1034         __ pop(RegSet::of(r0, r2, r4, r5), sp);
1035         __ ret(lr);
1036       }
1037       break;
1038 
1039     case monitorenter_nofpu_id:
1040       save_fpu_registers = false;
1041       // fall through
1042     case monitorenter_id:
1043       {
1044         StubFrame f(sasm, "monitorenter", dont_gc_arguments);
1045         OopMap* map = save_live_registers(sasm, save_fpu_registers);
1046 
1047         // Called with store_parameter and not C abi
1048 
1049         f.load_argument(1, r0); // r0,: object
1050         f.load_argument(0, r1); // r1,: lock address
1051 
1052         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), r0, r1);
1053 
1054         oop_maps = new OopMapSet();
1055         oop_maps->add_gc_map(call_offset, map);
1056         restore_live_registers(sasm, save_fpu_registers);
1057       }
1058       break;
1059 
1060     case monitorexit_nofpu_id:
1061       save_fpu_registers = false;
1062       // fall through
1063     case monitorexit_id:
1064       {
1065         StubFrame f(sasm, "monitorexit", dont_gc_arguments);
1066         OopMap* map = save_live_registers(sasm, save_fpu_registers);
1067 
1068         // Called with store_parameter and not C abi
1069 
1070         f.load_argument(0, r0); // r0,: lock address
1071 
1072         // note: really a leaf routine but must setup last java sp
1073         //       => use call_RT for now (speed can be improved by
1074         //       doing last java sp setup manually)
1075         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), r0);
1076 
1077         oop_maps = new OopMapSet();
1078         oop_maps->add_gc_map(call_offset, map);
1079         restore_live_registers(sasm, save_fpu_registers);
1080       }
1081       break;
1082 
1083     case deoptimize_id:
1084       {
1085         StubFrame f(sasm, "deoptimize", dont_gc_arguments);
1086         OopMap* oop_map = save_live_registers(sasm);
1087         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, deoptimize));
1088         oop_maps = new OopMapSet();
1089         oop_maps->add_gc_map(call_offset, oop_map);
1090         restore_live_registers(sasm);
1091         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
1092         assert(deopt_blob != NULL, "deoptimization blob must have been created");
1093         __ leave();
1094         __ far_jump(RuntimeAddress(deopt_blob->unpack_with_reexecution()));
1095       }
1096       break;
1097 
1098     case throw_range_check_failed_id:
1099       { StubFrame f(sasm, "range_check_failed", dont_gc_arguments);
1100         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_range_check_exception), true);
1101       }
1102       break;
1103 
1104     case unwind_exception_id:
1105       { __ set_info("unwind_exception", dont_gc_arguments);
1106         // note: no stubframe since we are about to leave the current
1107         //       activation and we are calling a leaf VM function only.
1108         generate_unwind_exception(sasm);
1109       }
1110       break;
1111 
1112     case access_field_patching_id:
1113       { StubFrame f(sasm, "access_field_patching", dont_gc_arguments);
1114         // we should set up register map
1115         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
1116       }
1117       break;
1118 
1119     case load_klass_patching_id:
1120       { StubFrame f(sasm, "load_klass_patching", dont_gc_arguments);
1121         // we should set up register map
1122         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_klass_patching));
1123       }
1124       break;
1125 
1126     case load_mirror_patching_id:
1127       { StubFrame f(sasm, "load_mirror_patching", dont_gc_arguments);
1128         // we should set up register map
1129         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_mirror_patching));
1130       }
1131       break;
1132 
1133     case load_appendix_patching_id:
1134       { StubFrame f(sasm, "load_appendix_patching", dont_gc_arguments);
1135         // we should set up register map
1136         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_appendix_patching));
1137       }
1138       break;
1139 
1140     case handle_exception_nofpu_id:
1141     case handle_exception_id:
1142       { StubFrame f(sasm, "handle_exception", dont_gc_arguments);
1143         oop_maps = generate_handle_exception(id, sasm);
1144       }
1145       break;
1146 
1147     case handle_exception_from_callee_id:
1148       { StubFrame f(sasm, "handle_exception_from_callee", dont_gc_arguments);
1149         oop_maps = generate_handle_exception(id, sasm);
1150       }
1151       break;
1152 
1153     case throw_index_exception_id:
1154       { StubFrame f(sasm, "index_range_check_failed", dont_gc_arguments);
1155         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_index_exception), true);
1156       }
1157       break;
1158 
1159     case throw_array_store_exception_id:
1160       { StubFrame f(sasm, "throw_array_store_exception", dont_gc_arguments);
1161         // tos + 0: link
1162         //     + 1: return address
1163         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), true);
1164       }
1165       break;
1166 
1167 #if INCLUDE_ALL_GCS
1168 
1169 // Registers to be saved around calls to g1_wb_pre or g1_wb_post
1170 #define G1_SAVE_REGS (RegSet::range(r0, r18) - RegSet::of(rscratch1, rscratch2))
1171 
1172     case g1_pre_barrier_slow_id:
1173       {
1174         StubFrame f(sasm, "g1_pre_barrier", dont_gc_arguments);
1175         // arg0 : previous value of memory
1176 
1177         BarrierSet* bs = Universe::heap()->barrier_set();
1178         if (bs->kind() != BarrierSet::G1SATBCTLogging) {
1179           __ mov(r0, (int)id);
1180           __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), r0);
1181           __ should_not_reach_here();
1182           break;
1183         }
1184 
1185         const Register pre_val = r0;
1186         const Register thread = rthread;
1187         const Register tmp = rscratch1;
1188 
1189         Address in_progress(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
1190                                              PtrQueue::byte_offset_of_active()));
1191 
1192         Address queue_index(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
1193                                              PtrQueue::byte_offset_of_index()));
1194         Address buffer(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
1195                                         PtrQueue::byte_offset_of_buf()));
1196 
1197         Label done;
1198         Label runtime;
1199 
1200         // Can we store original value in the thread's buffer?
1201         __ ldr(tmp, queue_index);
1202         __ cbz(tmp, runtime);
1203 
1204         __ sub(tmp, tmp, wordSize);
1205         __ str(tmp, queue_index);
1206         __ ldr(rscratch2, buffer);
1207         __ add(tmp, tmp, rscratch2);
1208         f.load_argument(0, rscratch2);
1209         __ str(rscratch2, Address(tmp, 0));
1210         __ b(done);
1211 
1212         __ bind(runtime);
1213         __ push(G1_SAVE_REGS, sp);
1214         f.load_argument(0, pre_val);
1215         __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::g1_wb_pre), pre_val, thread);
1216         __ pop(G1_SAVE_REGS, sp);
1217         __ bind(done);
1218       }
1219       break;
1220     case g1_post_barrier_slow_id:
1221       {
1222         StubFrame f(sasm, "g1_post_barrier", dont_gc_arguments);
1223 
1224         // arg0: store_address
1225         Address store_addr(rfp, 2*BytesPerWord);
1226 
1227         BarrierSet* bs = Universe::heap()->barrier_set();
1228         CardTableModRefBS* ct = (CardTableModRefBS*)bs;
1229         assert(sizeof(*ct->byte_map_base) == sizeof(jbyte), "adjust this code");
1230 
1231         Label done;
1232         Label runtime;
1233 
1234         // At this point we know new_value is non-NULL and the new_value crosses regions.
1235         // Must check to see if card is already dirty
1236 
1237         const Register thread = rthread;
1238 
1239         Address queue_index(thread, in_bytes(JavaThread::dirty_card_queue_offset() +
1240                                              PtrQueue::byte_offset_of_index()));
1241         Address buffer(thread, in_bytes(JavaThread::dirty_card_queue_offset() +
1242                                         PtrQueue::byte_offset_of_buf()));
1243 
1244         const Register card_addr = rscratch2;
1245         ExternalAddress cardtable((address) ct->byte_map_base);
1246 
1247         f.load_argument(0, card_addr);
1248         __ lsr(card_addr, card_addr, CardTableModRefBS::card_shift);
1249         unsigned long offset;
1250         __ adrp(rscratch1, cardtable, offset);
1251         __ add(card_addr, card_addr, rscratch1);
1252         __ ldrb(rscratch1, Address(card_addr, offset));
1253         __ cmpw(rscratch1, (int)G1SATBCardTableModRefBS::g1_young_card_val());
1254         __ br(Assembler::EQ, done);
1255 
1256         assert((int)CardTableModRefBS::dirty_card_val() == 0, "must be 0");
1257 
1258         __ membar(Assembler::StoreLoad);
1259         __ ldrb(rscratch1, Address(card_addr, offset));
1260         __ cbzw(rscratch1, done);
1261 
1262         // storing region crossing non-NULL, card is clean.
1263         // dirty card and log.
1264         __ strb(zr, Address(card_addr, offset));
1265 
1266         __ ldr(rscratch1, queue_index);
1267         __ cbz(rscratch1, runtime);
1268         __ sub(rscratch1, rscratch1, wordSize);
1269         __ str(rscratch1, queue_index);
1270 
1271         const Register buffer_addr = r0;
1272 
1273         __ push(RegSet::of(r0, r1), sp);
1274         __ ldr(buffer_addr, buffer);
1275         __ str(card_addr, Address(buffer_addr, rscratch1));
1276         __ pop(RegSet::of(r0, r1), sp);
1277         __ b(done);
1278 
1279         __ bind(runtime);
1280         __ push(G1_SAVE_REGS, sp);
1281         __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::g1_wb_post), card_addr, thread);
1282         __ pop(G1_SAVE_REGS, sp);
1283         __ bind(done);
1284 
1285       }
1286       break;
1287 #endif
1288 
1289     case predicate_failed_trap_id:
1290       {
1291         StubFrame f(sasm, "predicate_failed_trap", dont_gc_arguments);
1292 
1293         OopMap* map = save_live_registers(sasm);
1294 
1295         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, predicate_failed_trap));
1296         oop_maps = new OopMapSet();
1297         oop_maps->add_gc_map(call_offset, map);
1298         restore_live_registers(sasm);
1299         __ leave();
1300         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
1301         assert(deopt_blob != NULL, "deoptimization blob must have been created");
1302 
1303         __ far_jump(RuntimeAddress(deopt_blob->unpack_with_reexecution()));
1304       }
1305       break;
1306 
1307 
1308     default:
1309       { StubFrame f(sasm, "unimplemented entry", dont_gc_arguments);
1310         __ mov(r0, (int)id);
1311         __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), r0);
1312         __ should_not_reach_here();
1313       }
1314       break;
1315     }
1316   }
1317   return oop_maps;
1318 }
1319 
1320 #undef __
1321 
1322 const char *Runtime1::pd_name_for_address(address entry) { Unimplemented(); return 0; }