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