1 /*
   2  * Copyright (c) 1999, 2018, 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 "c1/c1_CodeStubs.hpp"
  28 #include "c1/c1_FrameMap.hpp"
  29 #include "c1/c1_LIRAssembler.hpp"
  30 #include "c1/c1_MacroAssembler.hpp"
  31 #include "c1/c1_Runtime1.hpp"
  32 #include "nativeInst_aarch64.hpp"
  33 #include "runtime/sharedRuntime.hpp"
  34 #include "vmreg_aarch64.inline.hpp"
  35 #if INCLUDE_ALL_GCS
  36 #include "gc/g1/g1BarrierSet.hpp"
  37 #endif
  38 
  39 
  40 #define __ ce->masm()->
  41 
  42 void CounterOverflowStub::emit_code(LIR_Assembler* ce) {
  43   __ bind(_entry);
  44   Metadata *m = _method->as_constant_ptr()->as_metadata();
  45   __ mov_metadata(rscratch1, m);
  46   ce->store_parameter(rscratch1, 1);
  47   ce->store_parameter(_bci, 0);
  48   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::counter_overflow_id)));
  49   ce->add_call_info_here(_info);
  50   ce->verify_oop_map(_info);
  51   __ b(_continuation);
  52 }
  53 
  54 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index, LIR_Opr array)
  55   : _throw_index_out_of_bounds_exception(array == NULL), _index(index), _array(array) {
  56   assert(info != NULL, "must have info");
  57   _info = new CodeEmitInfo(info);
  58 }
  59 
  60 void RangeCheckStub::emit_code(LIR_Assembler* ce) {
  61   __ bind(_entry);
  62   if (_info->deoptimize_on_exception()) {
  63     address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
  64     __ far_call(RuntimeAddress(a));
  65     ce->add_call_info_here(_info);
  66     ce->verify_oop_map(_info);
  67     debug_only(__ should_not_reach_here());
  68     return;
  69   }
  70 
  71   if (_index->is_cpu_register()) {
  72     __ mov(rscratch1, _index->as_register());
  73   } else {
  74     __ mov(rscratch1, _index->as_jint());
  75   }
  76   Runtime1::StubID stub_id;
  77   if (_throw_index_out_of_bounds_exception) {
  78     stub_id = Runtime1::throw_index_exception_id;
  79   } else {
  80     assert(_array != NULL, "sanity");
  81     __ mov(rscratch2, _array->as_pointer_register());
  82     stub_id = Runtime1::throw_range_check_failed_id;
  83   }
  84   __ far_call(RuntimeAddress(Runtime1::entry_for(stub_id)), NULL, rscratch2);
  85   ce->add_call_info_here(_info);
  86   ce->verify_oop_map(_info);
  87   debug_only(__ should_not_reach_here());
  88 }
  89 
  90 PredicateFailedStub::PredicateFailedStub(CodeEmitInfo* info) {
  91   _info = new CodeEmitInfo(info);
  92 }
  93 
  94 void PredicateFailedStub::emit_code(LIR_Assembler* ce) {
  95   __ bind(_entry);
  96   address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
  97   __ far_call(RuntimeAddress(a));
  98   ce->add_call_info_here(_info);
  99   ce->verify_oop_map(_info);
 100   debug_only(__ should_not_reach_here());
 101 }
 102 
 103 void DivByZeroStub::emit_code(LIR_Assembler* ce) {
 104   if (_offset != -1) {
 105     ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
 106   }
 107   __ bind(_entry);
 108   __ far_call(Address(Runtime1::entry_for(Runtime1::throw_div0_exception_id), relocInfo::runtime_call_type));
 109   ce->add_call_info_here(_info);
 110   ce->verify_oop_map(_info);
 111 #ifdef ASSERT
 112   __ should_not_reach_here();
 113 #endif
 114 }
 115 
 116 
 117 
 118 // Implementation of NewInstanceStub
 119 
 120 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id) {
 121   _result = result;
 122   _klass = klass;
 123   _klass_reg = klass_reg;
 124   _info = new CodeEmitInfo(info);
 125   assert(stub_id == Runtime1::new_instance_id                 ||
 126          stub_id == Runtime1::fast_new_instance_id            ||
 127          stub_id == Runtime1::fast_new_instance_init_check_id,
 128          "need new_instance id");
 129   _stub_id   = stub_id;
 130 }
 131 
 132 
 133 
 134 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
 135   assert(__ rsp_offset() == 0, "frame size should be fixed");
 136   __ bind(_entry);
 137   __ mov(r3, _klass_reg->as_register());
 138   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub_id)));
 139   ce->add_call_info_here(_info);
 140   ce->verify_oop_map(_info);
 141   assert(_result->as_register() == r0, "result must in r0,");
 142   __ b(_continuation);
 143 }
 144 
 145 
 146 // Implementation of NewTypeArrayStub
 147 
 148 // Implementation of NewTypeArrayStub
 149 
 150 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
 151   _klass_reg = klass_reg;
 152   _length = length;
 153   _result = result;
 154   _info = new CodeEmitInfo(info);
 155 }
 156 
 157 
 158 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
 159   assert(__ rsp_offset() == 0, "frame size should be fixed");
 160   __ bind(_entry);
 161   assert(_length->as_register() == r19, "length must in r19,");
 162   assert(_klass_reg->as_register() == r3, "klass_reg must in r3");
 163   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_type_array_id)));
 164   ce->add_call_info_here(_info);
 165   ce->verify_oop_map(_info);
 166   assert(_result->as_register() == r0, "result must in r0");
 167   __ b(_continuation);
 168 }
 169 
 170 
 171 // Implementation of NewObjectArrayStub
 172 
 173 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
 174   _klass_reg = klass_reg;
 175   _result = result;
 176   _length = length;
 177   _info = new CodeEmitInfo(info);
 178 }
 179 
 180 
 181 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
 182   assert(__ rsp_offset() == 0, "frame size should be fixed");
 183   __ bind(_entry);
 184   assert(_length->as_register() == r19, "length must in r19,");
 185   assert(_klass_reg->as_register() == r3, "klass_reg must in r3");
 186   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_object_array_id)));
 187   ce->add_call_info_here(_info);
 188   ce->verify_oop_map(_info);
 189   assert(_result->as_register() == r0, "result must in r0");
 190   __ b(_continuation);
 191 }
 192 // Implementation of MonitorAccessStubs
 193 
 194 MonitorEnterStub::MonitorEnterStub(LIR_Opr obj_reg, LIR_Opr lock_reg, CodeEmitInfo* info)
 195 : MonitorAccessStub(obj_reg, lock_reg)
 196 {
 197   _info = new CodeEmitInfo(info);
 198 }
 199 
 200 
 201 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
 202   assert(__ rsp_offset() == 0, "frame size should be fixed");
 203   __ bind(_entry);
 204   ce->store_parameter(_obj_reg->as_register(),  1);
 205   ce->store_parameter(_lock_reg->as_register(), 0);
 206   Runtime1::StubID enter_id;
 207   if (ce->compilation()->has_fpu_code()) {
 208     enter_id = Runtime1::monitorenter_id;
 209   } else {
 210     enter_id = Runtime1::monitorenter_nofpu_id;
 211   }
 212   __ far_call(RuntimeAddress(Runtime1::entry_for(enter_id)));
 213   ce->add_call_info_here(_info);
 214   ce->verify_oop_map(_info);
 215   __ b(_continuation);
 216 }
 217 
 218 
 219 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
 220   __ bind(_entry);
 221   if (_compute_lock) {
 222     // lock_reg was destroyed by fast unlocking attempt => recompute it
 223     ce->monitor_address(_monitor_ix, _lock_reg);
 224   }
 225   ce->store_parameter(_lock_reg->as_register(), 0);
 226   // note: non-blocking leaf routine => no call info needed
 227   Runtime1::StubID exit_id;
 228   if (ce->compilation()->has_fpu_code()) {
 229     exit_id = Runtime1::monitorexit_id;
 230   } else {
 231     exit_id = Runtime1::monitorexit_nofpu_id;
 232   }
 233   __ adr(lr, _continuation);
 234   __ far_jump(RuntimeAddress(Runtime1::entry_for(exit_id)));
 235 }
 236 
 237 
 238 // Implementation of patching:
 239 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes)
 240 // - Replace original code with a call to the stub
 241 // At Runtime:
 242 // - call to stub, jump to runtime
 243 // - in runtime: preserve all registers (rspecially objects, i.e., source and destination object)
 244 // - in runtime: after initializing class, restore original code, reexecute instruction
 245 
 246 int PatchingStub::_patch_info_offset = -NativeGeneralJump::instruction_size;
 247 
 248 void PatchingStub::align_patch_site(MacroAssembler* masm) {
 249 }
 250 
 251 void PatchingStub::emit_code(LIR_Assembler* ce) {
 252   assert(false, "AArch64 should not use C1 runtime patching");
 253 }
 254 
 255 
 256 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
 257   __ bind(_entry);
 258   ce->store_parameter(_trap_request, 0);
 259   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::deoptimize_id)));
 260   ce->add_call_info_here(_info);
 261   DEBUG_ONLY(__ should_not_reach_here());
 262 }
 263 
 264 
 265 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
 266   address a;
 267   if (_info->deoptimize_on_exception()) {
 268     // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
 269     a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
 270   } else {
 271     a = Runtime1::entry_for(Runtime1::throw_null_pointer_exception_id);
 272   }
 273 
 274   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
 275   __ bind(_entry);
 276   __ far_call(RuntimeAddress(a));
 277   ce->add_call_info_here(_info);
 278   ce->verify_oop_map(_info);
 279   debug_only(__ should_not_reach_here());
 280 }
 281 
 282 
 283 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
 284   assert(__ rsp_offset() == 0, "frame size should be fixed");
 285 
 286   __ bind(_entry);
 287   // pass the object in a scratch register because all other registers
 288   // must be preserved
 289   if (_obj->is_cpu_register()) {
 290     __ mov(rscratch1, _obj->as_register());
 291   }
 292   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub)), NULL, rscratch2);
 293   ce->add_call_info_here(_info);
 294   debug_only(__ should_not_reach_here());
 295 }
 296 
 297 
 298 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
 299   //---------------slow case: call to native-----------------
 300   __ bind(_entry);
 301   // Figure out where the args should go
 302   // This should really convert the IntrinsicID to the Method* and signature
 303   // but I don't know how to do that.
 304   //
 305   VMRegPair args[5];
 306   BasicType signature[5] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT};
 307   SharedRuntime::java_calling_convention(signature, args, 5, true);
 308 
 309   // push parameters
 310   // (src, src_pos, dest, destPos, length)
 311   Register r[5];
 312   r[0] = src()->as_register();
 313   r[1] = src_pos()->as_register();
 314   r[2] = dst()->as_register();
 315   r[3] = dst_pos()->as_register();
 316   r[4] = length()->as_register();
 317 
 318   // next registers will get stored on the stack
 319   for (int i = 0; i < 5 ; i++ ) {
 320     VMReg r_1 = args[i].first();
 321     if (r_1->is_stack()) {
 322       int st_off = r_1->reg2stack() * wordSize;
 323       __ str (r[i], Address(sp, st_off));
 324     } else {
 325       assert(r[i] == args[i].first()->as_Register(), "Wrong register for arg ");
 326     }
 327   }
 328 
 329   ce->align_call(lir_static_call);
 330 
 331   ce->emit_static_call_stub();
 332   if (ce->compilation()->bailed_out()) {
 333     return; // CodeCache is full
 334   }
 335   Address resolve(SharedRuntime::get_resolve_static_call_stub(),
 336                   relocInfo::static_call_type);
 337   address call = __ trampoline_call(resolve);
 338   if (call == NULL) {
 339     ce->bailout("trampoline stub overflow");
 340     return;
 341   }
 342   ce->add_call_info_here(info());
 343 
 344 #ifndef PRODUCT
 345   __ lea(rscratch2, ExternalAddress((address)&Runtime1::_arraycopy_slowcase_cnt));
 346   __ incrementw(Address(rscratch2));
 347 #endif
 348 
 349   __ b(_continuation);
 350 }
 351 
 352 
 353 /////////////////////////////////////////////////////////////////////////////
 354 #if INCLUDE_ALL_GCS
 355 
 356 void G1PreBarrierStub::emit_code(LIR_Assembler* ce) {
 357   // At this point we know that marking is in progress.
 358   // If do_load() is true then we have to emit the
 359   // load of the previous value; otherwise it has already
 360   // been loaded into _pre_val.
 361 
 362   __ bind(_entry);
 363   assert(pre_val()->is_register(), "Precondition.");
 364 
 365   Register pre_val_reg = pre_val()->as_register();
 366 
 367   if (do_load()) {
 368     ce->mem2reg(addr(), pre_val(), T_OBJECT, patch_code(), info(), false /*wide*/, false /*unaligned*/);
 369   }
 370   __ cbz(pre_val_reg, _continuation);
 371   ce->store_parameter(pre_val()->as_register(), 0);
 372   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::g1_pre_barrier_slow_id)));
 373   __ b(_continuation);
 374 }
 375 
 376 void G1PostBarrierStub::emit_code(LIR_Assembler* ce) {
 377   __ bind(_entry);
 378   assert(addr()->is_register(), "Precondition.");
 379   assert(new_val()->is_register(), "Precondition.");
 380   Register new_val_reg = new_val()->as_register();
 381   __ cbz(new_val_reg, _continuation);
 382   ce->store_parameter(addr()->as_pointer_register(), 0);
 383   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::g1_post_barrier_slow_id)));
 384   __ b(_continuation);
 385 }
 386 
 387 #endif // INCLUDE_ALL_GCS
 388 /////////////////////////////////////////////////////////////////////////////
 389 
 390 #undef __