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