1 /*
   2  * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/macroAssembler.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_arm.hpp"
  33 #include "runtime/sharedRuntime.hpp"
  34 #include "utilities/macros.hpp"
  35 #include "vmreg_arm.inline.hpp"
  36 
  37 #define __ ce->masm()->
  38 
  39 void CounterOverflowStub::emit_code(LIR_Assembler* ce) {
  40   __ bind(_entry);
  41   ce->store_parameter(_bci, 0);
  42   ce->store_parameter(_method->as_constant_ptr()->as_metadata(), 1);
  43   __ call(Runtime1::entry_for(Runtime1::counter_overflow_id), relocInfo::runtime_call_type);
  44   ce->add_call_info_here(_info);
  45   ce->verify_oop_map(_info);
  46 
  47   __ b(_continuation);
  48 }
  49 
  50 
  51 // TODO: ARM - is it possible to inline these stubs into the main code stream?
  52 
  53 
  54 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index, LIR_Opr array)
  55   : _throw_index_out_of_bounds_exception(false), _index(index), _array(array) {
  56   assert(info != NULL, "must have info");
  57   _info = new CodeEmitInfo(info);
  58 }
  59 
  60 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index)
  61   : _throw_index_out_of_bounds_exception(true), _index(index), _array(NULL) {
  62   assert(info != NULL, "must have info");
  63   _info = new CodeEmitInfo(info);
  64 }
  65 
  66 void RangeCheckStub::emit_code(LIR_Assembler* ce) {
  67   __ bind(_entry);
  68 
  69   if (_info->deoptimize_on_exception()) {
  70 #ifdef AARCH64
  71     __ NOT_TESTED();
  72 #endif
  73     __ call(Runtime1::entry_for(Runtime1::predicate_failed_trap_id), relocInfo::runtime_call_type);
  74     ce->add_call_info_here(_info);
  75     ce->verify_oop_map(_info);
  76     debug_only(__ should_not_reach_here());
  77     return;
  78   }
  79   // Pass the array index on stack because all registers must be preserved
  80   ce->verify_reserved_argument_area_size(2);
  81   if (_index->is_cpu_register()) {
  82     __ str_32(_index->as_register(), Address(SP));
  83   } else {
  84     __ mov_slow(Rtemp, _index->as_jint()); // Rtemp should be OK in C1
  85     __ str_32(Rtemp, Address(SP));
  86   }
  87   __ mov_slow(Rtemp, _array->as_pointer_register());
  88   __ str(Rtemp, Address(SP, BytesPerWord)); // ??? Correct offset? Correct instruction?
  89 
  90   if (_throw_index_out_of_bounds_exception) {
  91 #ifdef AARCH64
  92     __ NOT_TESTED();
  93 #endif
  94     __ call(Runtime1::entry_for(Runtime1::throw_index_exception_id), relocInfo::runtime_call_type);
  95   } else {
  96     __ call(Runtime1::entry_for(Runtime1::throw_range_check_failed_id), relocInfo::runtime_call_type);
  97   }
  98   ce->add_call_info_here(_info);
  99   ce->verify_oop_map(_info);
 100   DEBUG_ONLY(STOP("RangeCheck");)
 101 }
 102 
 103 PredicateFailedStub::PredicateFailedStub(CodeEmitInfo* info) {
 104   _info = new CodeEmitInfo(info);
 105 }
 106 
 107 void PredicateFailedStub::emit_code(LIR_Assembler* ce) {
 108   __ bind(_entry);
 109   __ call(Runtime1::entry_for(Runtime1::predicate_failed_trap_id), relocInfo::runtime_call_type);
 110   ce->add_call_info_here(_info);
 111   ce->verify_oop_map(_info);
 112   debug_only(__ should_not_reach_here());
 113 }
 114 
 115 void DivByZeroStub::emit_code(LIR_Assembler* ce) {
 116   if (_offset != -1) {
 117     ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
 118   }
 119   __ bind(_entry);
 120   __ call(Runtime1::entry_for(Runtime1::throw_div0_exception_id),
 121           relocInfo::runtime_call_type);
 122   ce->add_call_info_here(_info);
 123   DEBUG_ONLY(STOP("DivByZero");)
 124 }
 125 
 126 
 127 // Implementation of NewInstanceStub
 128 
 129 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id) {
 130   _result = result;
 131   _klass = klass;
 132   _klass_reg = klass_reg;
 133   _info = new CodeEmitInfo(info);
 134   assert(stub_id == Runtime1::new_instance_id                 ||
 135          stub_id == Runtime1::fast_new_instance_id            ||
 136          stub_id == Runtime1::fast_new_instance_init_check_id,
 137          "need new_instance id");
 138   _stub_id   = stub_id;
 139 }
 140 
 141 
 142 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
 143   assert(_result->as_register() == R0, "runtime call setup");
 144   assert(_klass_reg->as_register() == R1, "runtime call setup");
 145   __ bind(_entry);
 146   __ call(Runtime1::entry_for(_stub_id), relocInfo::runtime_call_type);
 147   ce->add_call_info_here(_info);
 148   ce->verify_oop_map(_info);
 149   __ b(_continuation);
 150 }
 151 
 152 
 153 // Implementation of NewTypeArrayStub
 154 
 155 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
 156   _klass_reg = klass_reg;
 157   _length = length;
 158   _result = result;
 159   _info = new CodeEmitInfo(info);
 160 }
 161 
 162 
 163 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
 164   assert(_result->as_register() == R0, "runtime call setup");
 165   assert(_klass_reg->as_register() == R1, "runtime call setup");
 166   assert(_length->as_register() == R2, "runtime call setup");
 167   __ bind(_entry);
 168   __ call(Runtime1::entry_for(Runtime1::new_type_array_id), relocInfo::runtime_call_type);
 169   ce->add_call_info_here(_info);
 170   ce->verify_oop_map(_info);
 171   __ b(_continuation);
 172 }
 173 
 174 
 175 // Implementation of NewObjectArrayStub
 176 
 177 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
 178   _klass_reg = klass_reg;
 179   _result = result;
 180   _length = length;
 181   _info = new CodeEmitInfo(info);
 182 }
 183 
 184 
 185 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
 186   assert(_result->as_register() == R0, "runtime call setup");
 187   assert(_klass_reg->as_register() == R1, "runtime call setup");
 188   assert(_length->as_register() == R2, "runtime call setup");
 189   __ bind(_entry);
 190   __ call(Runtime1::entry_for(Runtime1::new_object_array_id), relocInfo::runtime_call_type);
 191   ce->add_call_info_here(_info);
 192   ce->verify_oop_map(_info);
 193   __ b(_continuation);
 194 }
 195 
 196 
 197 // Implementation of MonitorAccessStubs
 198 
 199 MonitorEnterStub::MonitorEnterStub(LIR_Opr obj_reg, LIR_Opr lock_reg, CodeEmitInfo* info)
 200 : MonitorAccessStub(obj_reg, lock_reg)
 201 {
 202   _info = new CodeEmitInfo(info);
 203 }
 204 
 205 
 206 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
 207   __ bind(_entry);
 208   const Register obj_reg = _obj_reg->as_pointer_register();
 209   const Register lock_reg = _lock_reg->as_pointer_register();
 210 
 211   ce->verify_reserved_argument_area_size(2);
 212 #ifdef AARCH64
 213   __ stp(obj_reg, lock_reg, Address(SP));
 214 #else
 215   if (obj_reg < lock_reg) {
 216     __ stmia(SP, RegisterSet(obj_reg) | RegisterSet(lock_reg));
 217   } else {
 218     __ str(obj_reg, Address(SP));
 219     __ str(lock_reg, Address(SP, BytesPerWord));
 220   }
 221 #endif // AARCH64
 222 
 223   Runtime1::StubID enter_id = ce->compilation()->has_fpu_code() ?
 224                               Runtime1::monitorenter_id :
 225                               Runtime1::monitorenter_nofpu_id;
 226   __ call(Runtime1::entry_for(enter_id), relocInfo::runtime_call_type);
 227   ce->add_call_info_here(_info);
 228   ce->verify_oop_map(_info);
 229   __ b(_continuation);
 230 }
 231 
 232 
 233 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
 234   __ bind(_entry);
 235   if (_compute_lock) {
 236     ce->monitor_address(_monitor_ix, _lock_reg);
 237   }
 238   const Register lock_reg = _lock_reg->as_pointer_register();
 239 
 240   ce->verify_reserved_argument_area_size(1);
 241   __ str(lock_reg, Address(SP));
 242 
 243   // Non-blocking leaf routine - no call info needed
 244   Runtime1::StubID exit_id = ce->compilation()->has_fpu_code() ?
 245                              Runtime1::monitorexit_id :
 246                              Runtime1::monitorexit_nofpu_id;
 247   __ call(Runtime1::entry_for(exit_id), relocInfo::runtime_call_type);
 248   __ b(_continuation);
 249 }
 250 
 251 
 252 // Call return is directly after patch word
 253 int PatchingStub::_patch_info_offset = 0;
 254 
 255 void PatchingStub::align_patch_site(MacroAssembler* masm) {
 256 #if 0
 257   // TODO: investigate if we required to implement this
 258     ShouldNotReachHere();
 259 #endif
 260 }
 261 
 262 void PatchingStub::emit_code(LIR_Assembler* ce) {
 263   const int patchable_instruction_offset = AARCH64_ONLY(NativeInstruction::instruction_size) NOT_AARCH64(0);
 264 
 265   assert(NativeCall::instruction_size <= _bytes_to_copy && _bytes_to_copy <= 0xFF,
 266          "not enough room for call");
 267   assert((_bytes_to_copy & 3) == 0, "must copy a multiple of four bytes");
 268   Label call_patch;
 269   bool is_load = (_id == load_klass_id) || (_id == load_mirror_id) || (_id == load_appendix_id);
 270 
 271 #ifdef AARCH64
 272   assert(nativeInstruction_at(_pc_start)->is_nop(), "required for MT safe patching");
 273 
 274   // Same alignment of reg2mem code and PatchingStub code. Required to make copied bind_literal() code properly aligned.
 275   __ align(wordSize);
 276 #endif // AARCH64
 277 
 278   if (is_load NOT_AARCH64(&& !VM_Version::supports_movw())) {
 279     address start = __ pc();
 280 
 281     // The following sequence duplicates code provided in MacroAssembler::patchable_mov_oop()
 282     // without creating relocation info entry.
 283 #ifdef AARCH64
 284     // Extra nop for MT safe patching
 285     __ nop();
 286 #endif // AARCH64
 287 
 288     assert((__ pc() - start) == patchable_instruction_offset, "should be");
 289 #ifdef AARCH64
 290     __ ldr(_obj, __ pc());
 291 #else
 292     __ ldr(_obj, Address(PC));
 293     // Extra nop to handle case of large offset of oop placeholder (see NativeMovConstReg::set_data).
 294     __ nop();
 295 #endif // AARCH64
 296 
 297 #ifdef ASSERT
 298     for (int i = 0; i < _bytes_to_copy; i++) {
 299       assert(((address)_pc_start)[i] == start[i], "should be the same code");
 300     }
 301 #endif // ASSERT
 302   }
 303 
 304   address being_initialized_entry = __ pc();
 305   if (CommentedAssembly) {
 306     __ block_comment(" patch template");
 307   }
 308   if (is_load) {
 309     address start = __ pc();
 310     if (_id == load_mirror_id || _id == load_appendix_id) {
 311       __ patchable_mov_oop(_obj, (jobject)Universe::non_oop_word(), _index);
 312     } else {
 313       __ patchable_mov_metadata(_obj, (Metadata*)Universe::non_oop_word(), _index);
 314     }
 315 #ifdef ASSERT
 316     for (int i = 0; i < _bytes_to_copy; i++) {
 317       assert(((address)_pc_start)[i] == start[i], "should be the same code");
 318     }
 319 #endif // ASSERT
 320   } else {
 321     int* start = (int*)_pc_start;
 322     int* end = start + (_bytes_to_copy / BytesPerInt);
 323     while (start < end) {
 324       __ emit_int32(*start++);
 325     }
 326   }
 327   address end_of_patch = __ pc();
 328 
 329   int bytes_to_skip = 0;
 330   if (_id == load_mirror_id) {
 331     int offset = __ offset();
 332     if (CommentedAssembly) {
 333       __ block_comment(" being_initialized check");
 334     }
 335 
 336     assert(_obj != noreg, "must be a valid register");
 337     // Rtemp should be OK in C1
 338     __ ldr(Rtemp, Address(_obj, java_lang_Class::klass_offset_in_bytes()));
 339     __ ldr(Rtemp, Address(Rtemp, InstanceKlass::init_thread_offset()));
 340     __ cmp(Rtemp, Rthread);
 341     __ b(call_patch, ne);
 342     __ b(_patch_site_continuation);
 343 
 344     bytes_to_skip += __ offset() - offset;
 345   }
 346 
 347   if (CommentedAssembly) {
 348     __ block_comment("patch data - 3 high bytes of the word");
 349   }
 350   const int sizeof_patch_record = 4;
 351   bytes_to_skip += sizeof_patch_record;
 352   int being_initialized_entry_offset = __ pc() - being_initialized_entry + sizeof_patch_record;
 353   __ emit_int32(0xff | being_initialized_entry_offset << 8 | bytes_to_skip << 16 | _bytes_to_copy << 24);
 354 
 355   address patch_info_pc = __ pc();
 356   assert(patch_info_pc - end_of_patch == bytes_to_skip, "incorrect patch info");
 357 
 358   // runtime call will return here
 359   Label call_return;
 360   __ bind(call_return);
 361   ce->add_call_info_here(_info);
 362   assert(_patch_info_offset == (patch_info_pc - __ pc()), "must not change");
 363   __ b(_patch_site_entry);
 364 
 365   address entry = __ pc();
 366   NativeGeneralJump::insert_unconditional((address)_pc_start, entry);
 367   address target = NULL;
 368   relocInfo::relocType reloc_type = relocInfo::none;
 369   switch (_id) {
 370     case access_field_id:  target = Runtime1::entry_for(Runtime1::access_field_patching_id); break;
 371     case load_klass_id:    target = Runtime1::entry_for(Runtime1::load_klass_patching_id); reloc_type = relocInfo::metadata_type; break;
 372     case load_mirror_id:   target = Runtime1::entry_for(Runtime1::load_mirror_patching_id); reloc_type = relocInfo::oop_type; break;
 373     case load_appendix_id: target = Runtime1::entry_for(Runtime1::load_appendix_patching_id); reloc_type = relocInfo::oop_type; break;
 374     default: ShouldNotReachHere();
 375   }
 376   __ bind(call_patch);
 377 
 378   if (CommentedAssembly) {
 379     __ block_comment("patch entry point");
 380   }
 381 
 382   // arrange for call to return just after patch word
 383   __ adr(LR, call_return);
 384   __ jump(target, relocInfo::runtime_call_type, Rtemp);
 385 
 386   if (is_load) {
 387     CodeSection* cs = __ code_section();
 388     address pc = (address)_pc_start;
 389     RelocIterator iter(cs, pc, pc + 1);
 390     relocInfo::change_reloc_info_for_address(&iter, pc, reloc_type, relocInfo::none);
 391   }
 392 }
 393 
 394 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
 395   __ bind(_entry);
 396   __ mov_slow(Rtemp, _trap_request);
 397   ce->verify_reserved_argument_area_size(1);
 398   __ str(Rtemp, Address(SP));
 399   __ call(Runtime1::entry_for(Runtime1::deoptimize_id), relocInfo::runtime_call_type);
 400   ce->add_call_info_here(_info);
 401   DEBUG_ONLY(__ should_not_reach_here());
 402 }
 403 
 404 
 405 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
 406   address a;
 407   if (_info->deoptimize_on_exception()) {
 408     // Deoptimize, do not throw the exception, because it is
 409     // probably wrong to do it here.
 410     a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
 411   } else {
 412     a = Runtime1::entry_for(Runtime1::throw_null_pointer_exception_id);
 413   }
 414   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
 415   __ bind(_entry);
 416   __ call(a, relocInfo::runtime_call_type);
 417   ce->add_call_info_here(_info);
 418   ce->verify_oop_map(_info);
 419   DEBUG_ONLY(STOP("ImplicitNullCheck");)
 420 }
 421 
 422 
 423 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
 424   __ bind(_entry);
 425   // Pass the object on stack because all registers must be preserved
 426   if (_obj->is_cpu_register()) {
 427     ce->verify_reserved_argument_area_size(1);
 428     __ str(_obj->as_pointer_register(), Address(SP));
 429   } else {
 430     assert(_obj->is_illegal(), "should be");
 431   }
 432   __ call(Runtime1::entry_for(_stub), relocInfo::runtime_call_type);
 433   ce->add_call_info_here(_info);
 434   DEBUG_ONLY(STOP("SimpleException");)
 435 }
 436 
 437 
 438 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
 439   __ bind(_entry);
 440 
 441   VMRegPair args[5];
 442   BasicType signature[5] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT };
 443   SharedRuntime::java_calling_convention(signature, args, 5, true);
 444 
 445   Register r[5];
 446   r[0] = src()->as_pointer_register();
 447   r[1] = src_pos()->as_register();
 448   r[2] = dst()->as_pointer_register();
 449   r[3] = dst_pos()->as_register();
 450   r[4] = length()->as_register();
 451 
 452   for (int i = 0; i < 5; i++) {
 453     VMReg arg = args[i].first();
 454     if (arg->is_stack()) {
 455       __ str(r[i], Address(SP, arg->reg2stack() * VMRegImpl::stack_slot_size));
 456     } else {
 457       assert(r[i] == arg->as_Register(), "Calling conventions must match");
 458     }
 459   }
 460 
 461   ce->emit_static_call_stub();
 462   if (ce->compilation()->bailed_out()) {
 463     return; // CodeCache is full
 464   }
 465   int ret_addr_offset = __ patchable_call(SharedRuntime::get_resolve_static_call_stub(), relocInfo::static_call_type);
 466   assert(ret_addr_offset == __ offset(), "embedded return address not allowed");
 467   ce->add_call_info_here(info());
 468   ce->verify_oop_map(info());
 469   __ b(_continuation);
 470 }
 471 
 472 #undef __