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