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   _scratch_reg = FrameMap::r0_oop_opr;
 128   _info = new CodeEmitInfo(info);
 129 }
 130 
 131 void LoadFlattenedArrayStub::emit_code(LIR_Assembler* ce) {
 132   assert(__ rsp_offset() == 0, "frame size should be fixed");
 133   __ bind(_entry);
 134   ce->store_parameter(_array->as_register(), 1);
 135   ce->store_parameter(_index->as_register(), 0);
 136   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::load_flattened_array_id)));
 137   ce->add_call_info_here(_info);
 138   ce->verify_oop_map(_info);
 139   if (_result->as_register() != r0) { 
 140     __ mov(_result->as_register(), r0);
 141   }
 142   __ b(_continuation);
 143 }
 144 
 145 
 146 // Implementation of StoreFlattenedArrayStub
 147 
 148 StoreFlattenedArrayStub::StoreFlattenedArrayStub(LIR_Opr array, LIR_Opr index, LIR_Opr value, CodeEmitInfo* info) {
 149   _array = array;
 150   _index = index;
 151   _value = value;
 152   _scratch_reg = FrameMap::r0_oop_opr;
 153   _info = new CodeEmitInfo(info);
 154 }
 155 
 156 
 157 void StoreFlattenedArrayStub::emit_code(LIR_Assembler* ce) {
 158   assert(__ rsp_offset() == 0, "frame size should be fixed");
 159   __ bind(_entry);
 160   ce->store_parameter(_array->as_register(), 2);
 161   ce->store_parameter(_index->as_register(), 1);
 162   ce->store_parameter(_value->as_register(), 0);
 163   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::store_flattened_array_id)));
 164   ce->add_call_info_here(_info);
 165   ce->verify_oop_map(_info);
 166   __ b(_continuation);
 167 }
 168 
 169 // Implementation of SubstitutabilityCheckStub
 170 SubstitutabilityCheckStub::SubstitutabilityCheckStub(LIR_Opr left, LIR_Opr right, LIR_Opr result, CodeEmitInfo* info) {
 171   _left = left;
 172   _right = right;
 173   _result = result;
 174   _scratch_reg = FrameMap::r0_oop_opr;
 175   _info = new CodeEmitInfo(info);
 176 }
 177 
 178 void SubstitutabilityCheckStub::emit_code(LIR_Assembler* ce) {
 179   assert(__ rsp_offset() == 0, "frame size should be fixed");
 180   __ bind(_entry);
 181   ce->store_parameter(_left->as_register(), 1);
 182   ce->store_parameter(_right->as_register(), 0);
 183   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::substitutability_check_id)));
 184   ce->add_call_info_here(_info);
 185   ce->verify_oop_map(_info);
 186   if (_result->as_register() != r0) {
 187     __ mov(_result->as_register(), r0);
 188   }
 189   __ b(_continuation);
 190 }
 191 
 192 
 193 // Implementation of NewInstanceStub
 194 
 195 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id) {
 196   _result = result;
 197   _klass = klass;
 198   _klass_reg = klass_reg;
 199   _info = new CodeEmitInfo(info);
 200   assert(stub_id == Runtime1::new_instance_id                 ||
 201          stub_id == Runtime1::fast_new_instance_id            ||
 202          stub_id == Runtime1::fast_new_instance_init_check_id,
 203          "need new_instance id");
 204   _stub_id   = stub_id;
 205 }
 206 
 207 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
 208   assert(__ rsp_offset() == 0, "frame size should be fixed");
 209   __ bind(_entry);
 210   __ mov(r3, _klass_reg->as_register());
 211   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub_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 NewTypeArrayStub
 220 
 221 // Implementation of NewTypeArrayStub
 222 
 223 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
 224   _klass_reg = klass_reg;
 225   _length = length;
 226   _result = result;
 227   _info = new CodeEmitInfo(info);
 228 }
 229 
 230 
 231 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
 232   assert(__ rsp_offset() == 0, "frame size should be fixed");
 233   __ bind(_entry);
 234   assert(_length->as_register() == r19, "length must in r19,");
 235   assert(_klass_reg->as_register() == r3, "klass_reg must in r3");
 236   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_type_array_id)));
 237   ce->add_call_info_here(_info);
 238   ce->verify_oop_map(_info);
 239   assert(_result->as_register() == r0, "result must in r0");
 240   __ b(_continuation);
 241 }
 242 
 243 
 244 // Implementation of NewObjectArrayStub
 245 
 246 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info, bool is_value_type) {
 247   _klass_reg = klass_reg;
 248   _result = result;
 249   _length = length;
 250   _info = new CodeEmitInfo(info);
 251   _is_value_type = is_value_type; 
 252 }
 253 
 254 
 255 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
 256   assert(__ rsp_offset() == 0, "frame size should be fixed");
 257   __ bind(_entry);
 258   assert(_length->as_register() == r19, "length must in r19,");
 259   assert(_klass_reg->as_register() == r3, "klass_reg must in r3");
 260 
 261   if (_is_value_type) {
 262     __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_value_array_id)));
 263   } else {
 264     __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_object_array_id)));
 265   }
 266 
 267   ce->add_call_info_here(_info);
 268   ce->verify_oop_map(_info);
 269   assert(_result->as_register() == r0, "result must in r0");
 270   __ b(_continuation);
 271 }
 272 // Implementation of MonitorAccessStubs
 273 
 274 MonitorEnterStub::MonitorEnterStub(LIR_Opr obj_reg, LIR_Opr lock_reg, CodeEmitInfo* info,  CodeStub* throw_imse_stub, LIR_Opr scratch_reg)
 275 : MonitorAccessStub(obj_reg, lock_reg)
 276 {
 277   _info = new CodeEmitInfo(info);
 278   _scratch_reg = scratch_reg;
 279   _throw_imse_stub = throw_imse_stub;
 280   if (_throw_imse_stub != NULL) {
 281     assert(_scratch_reg != LIR_OprFact::illegalOpr, "must be");
 282   }
 283 }
 284 
 285 
 286 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
 287   assert(__ rsp_offset() == 0, "frame size should be fixed");
 288   __ bind(_entry);
 289   if (_throw_imse_stub != NULL) {
 290     // When we come here, _obj_reg has already been checked to be non-null.
 291     __ ldr(rscratch1, Address(_obj_reg->as_register(), oopDesc::mark_offset_in_bytes()));
 292     __ mov(rscratch2, markOopDesc::always_locked_pattern);
 293     __ andr(rscratch1, rscratch1, rscratch2);
 294 
 295     __ cmp(rscratch1, rscratch2); 
 296     __ br(Assembler::NE, *_throw_imse_stub->entry());
 297   }
 298 
 299   ce->store_parameter(_obj_reg->as_register(),  1);
 300   ce->store_parameter(_lock_reg->as_register(), 0);
 301   Runtime1::StubID enter_id;
 302   if (ce->compilation()->has_fpu_code()) {
 303     enter_id = Runtime1::monitorenter_id;
 304   } else {
 305     enter_id = Runtime1::monitorenter_nofpu_id;
 306   }
 307   __ far_call(RuntimeAddress(Runtime1::entry_for(enter_id)));
 308   ce->add_call_info_here(_info);
 309   ce->verify_oop_map(_info);
 310   __ b(_continuation);
 311 }
 312 
 313 
 314 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
 315   __ bind(_entry);
 316   if (_compute_lock) {
 317     // lock_reg was destroyed by fast unlocking attempt => recompute it
 318     ce->monitor_address(_monitor_ix, _lock_reg);
 319   }
 320   ce->store_parameter(_lock_reg->as_register(), 0);
 321   // note: non-blocking leaf routine => no call info needed
 322   Runtime1::StubID exit_id;
 323   if (ce->compilation()->has_fpu_code()) {
 324     exit_id = Runtime1::monitorexit_id;
 325   } else {
 326     exit_id = Runtime1::monitorexit_nofpu_id;
 327   }
 328   __ adr(lr, _continuation);
 329   __ far_jump(RuntimeAddress(Runtime1::entry_for(exit_id)));
 330 }
 331 
 332 
 333 // Implementation of patching:
 334 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes)
 335 // - Replace original code with a call to the stub
 336 // At Runtime:
 337 // - call to stub, jump to runtime
 338 // - in runtime: preserve all registers (rspecially objects, i.e., source and destination object)
 339 // - in runtime: after initializing class, restore original code, reexecute instruction
 340 
 341 int PatchingStub::_patch_info_offset = -NativeGeneralJump::instruction_size;
 342 
 343 void PatchingStub::align_patch_site(MacroAssembler* masm) {
 344 }
 345 
 346 void PatchingStub::emit_code(LIR_Assembler* ce) {
 347   assert(false, "AArch64 should not use C1 runtime patching");
 348 }
 349 
 350 
 351 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
 352   __ bind(_entry);
 353   ce->store_parameter(_trap_request, 0);
 354   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::deoptimize_id)));
 355   ce->add_call_info_here(_info);
 356   DEBUG_ONLY(__ should_not_reach_here());
 357 }
 358 
 359 
 360 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
 361   address a;
 362   if (_info->deoptimize_on_exception()) {
 363     // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
 364     a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
 365   } else {
 366     a = Runtime1::entry_for(Runtime1::throw_null_pointer_exception_id);
 367   }
 368 
 369   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
 370   __ bind(_entry);
 371   __ far_call(RuntimeAddress(a));
 372   ce->add_call_info_here(_info);
 373   ce->verify_oop_map(_info);
 374   debug_only(__ should_not_reach_here());
 375 }
 376 
 377 
 378 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
 379   assert(__ rsp_offset() == 0, "frame size should be fixed");
 380 
 381   __ bind(_entry);
 382   // pass the object in a scratch register because all other registers
 383   // must be preserved
 384   if (_obj->is_cpu_register()) {
 385     __ mov(rscratch1, _obj->as_register());
 386   }
 387   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub)), NULL, rscratch2);
 388   ce->add_call_info_here(_info);
 389   debug_only(__ should_not_reach_here());
 390 }
 391 
 392 
 393 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
 394   //---------------slow case: call to native-----------------
 395   __ bind(_entry);
 396   // Figure out where the args should go
 397   // This should really convert the IntrinsicID to the Method* and signature
 398   // but I don't know how to do that.
 399   //
 400   VMRegPair args[5];
 401   BasicType signature[5] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT};
 402   SharedRuntime::java_calling_convention(signature, args, 5, true);
 403 
 404   // push parameters
 405   // (src, src_pos, dest, destPos, length)
 406   Register r[5];
 407   r[0] = src()->as_register();
 408   r[1] = src_pos()->as_register();
 409   r[2] = dst()->as_register();
 410   r[3] = dst_pos()->as_register();
 411   r[4] = length()->as_register();
 412 
 413   // next registers will get stored on the stack
 414   for (int i = 0; i < 5 ; i++ ) {
 415     VMReg r_1 = args[i].first();
 416     if (r_1->is_stack()) {
 417       int st_off = r_1->reg2stack() * wordSize;
 418       __ str (r[i], Address(sp, st_off));
 419     } else {
 420       assert(r[i] == args[i].first()->as_Register(), "Wrong register for arg ");
 421     }
 422   }
 423 
 424   ce->align_call(lir_static_call);
 425 
 426   ce->emit_static_call_stub();
 427   if (ce->compilation()->bailed_out()) {
 428     return; // CodeCache is full
 429   }
 430   Address resolve(SharedRuntime::get_resolve_static_call_stub(),
 431                   relocInfo::static_call_type);
 432   address call = __ trampoline_call(resolve);
 433   if (call == NULL) {
 434     ce->bailout("trampoline stub overflow");
 435     return;
 436   }
 437   ce->add_call_info_here(info());
 438 
 439 #ifndef PRODUCT
 440   __ lea(rscratch2, ExternalAddress((address)&Runtime1::_arraycopy_slowcase_cnt));
 441   __ incrementw(Address(rscratch2));
 442 #endif
 443 
 444   __ b(_continuation);
 445 }
 446 
 447 #undef __