1 /*
   2  * Copyright (c) 1999, 2012, 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 "c1/c1_CodeStubs.hpp"
  27 #include "c1/c1_FrameMap.hpp"
  28 #include "c1/c1_LIRAssembler.hpp"
  29 #include "c1/c1_MacroAssembler.hpp"
  30 #include "c1/c1_Runtime1.hpp"
  31 #include "nativeInst_sparc.hpp"
  32 #include "runtime/sharedRuntime.hpp"
  33 #include "utilities/macros.hpp"
  34 #include "vmreg_sparc.inline.hpp"
  35 #if INCLUDE_ALL_GCS
  36 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
  37 #endif // INCLUDE_ALL_GCS
  38 
  39 #define __ ce->masm()->
  40 
  41 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index,
  42                                bool throw_index_out_of_bounds_exception)
  43   : _throw_index_out_of_bounds_exception(throw_index_out_of_bounds_exception)
  44   , _index(index)
  45 {
  46   assert(info != NULL, "must have info");
  47   _info = new CodeEmitInfo(info);
  48 }
  49 
  50 
  51 void RangeCheckStub::emit_code(LIR_Assembler* ce) {
  52   __ bind(_entry);
  53 
  54   if (_info->deoptimize_on_exception()) {
  55     address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
  56     __ call(a, relocInfo::runtime_call_type);
  57     __ delayed()->nop();
  58     ce->add_call_info_here(_info);
  59     debug_only(__ should_not_reach_here());
  60     return;
  61   }
  62 
  63   if (_index->is_register()) {
  64     __ mov(_index->as_register(), G4);
  65   } else {
  66     __ set(_index->as_jint(), G4);
  67   }
  68   if (_throw_index_out_of_bounds_exception) {
  69     __ call(Runtime1::entry_for(Runtime1::throw_index_exception_id), relocInfo::runtime_call_type);
  70   } else {
  71     __ call(Runtime1::entry_for(Runtime1::throw_range_check_failed_id), relocInfo::runtime_call_type);
  72   }
  73   __ delayed()->nop();
  74   ce->add_call_info_here(_info);
  75   ce->verify_oop_map(_info);
  76 #ifdef ASSERT
  77   __ should_not_reach_here();
  78 #endif
  79 }
  80 
  81 PredicateFailedStub::PredicateFailedStub(CodeEmitInfo* info) { 
  82   _info = new CodeEmitInfo(info);
  83 }
  84 
  85 void PredicateFailedStub::emit_code(LIR_Assembler* ce) {
  86   __ bind(_entry); 
  87   address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
  88   __ call(a, relocInfo::runtime_call_type);
  89   __ delayed()->nop();
  90   ce->add_call_info_here(_info);
  91   ce->verify_oop_map(_info); 
  92   debug_only(__ should_not_reach_here()); 
  93 }
  94 
  95 void CounterOverflowStub::emit_code(LIR_Assembler* ce) {
  96   __ bind(_entry);
  97   __ set(_bci, G4);
  98   __ call(Runtime1::entry_for(Runtime1::counter_overflow_id), relocInfo::runtime_call_type);
  99   __ delayed()->mov_or_nop(_method->as_register(), G5);
 100   ce->add_call_info_here(_info);
 101   ce->verify_oop_map(_info);
 102 
 103   __ br(Assembler::always, true, Assembler::pt, _continuation);
 104   __ delayed()->nop();
 105 }
 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   __ call(Runtime1::entry_for(Runtime1::throw_div0_exception_id), relocInfo::runtime_call_type);
 114   __ delayed()->nop();
 115   ce->add_call_info_here(_info);
 116   ce->verify_oop_map(_info);
 117 #ifdef ASSERT
 118   __ should_not_reach_here();
 119 #endif
 120 }
 121 
 122 
 123 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
 124   address a;
 125   if (_info->deoptimize_on_exception()) {
 126     // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
 127     a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
 128   } else {
 129     a = Runtime1::entry_for(Runtime1::throw_null_pointer_exception_id);
 130   }
 131 
 132   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
 133   __ bind(_entry);
 134   __ call(a, relocInfo::runtime_call_type);
 135   __ delayed()->nop();
 136   ce->add_call_info_here(_info);
 137   ce->verify_oop_map(_info);
 138 #ifdef ASSERT
 139   __ should_not_reach_here();
 140 #endif
 141 }
 142 
 143 
 144 // Implementation of SimpleExceptionStub
 145 // Note: %g1 and %g3 are already in use
 146 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
 147   __ bind(_entry);
 148   __ call(Runtime1::entry_for(_stub), relocInfo::runtime_call_type);
 149 
 150   if (_obj->is_valid()) {
 151     __ delayed()->mov(_obj->as_register(), G4); // _obj contains the optional argument to the stub
 152   } else {
 153     __ delayed()->mov(G0, G4);
 154   }
 155   ce->add_call_info_here(_info);
 156 #ifdef ASSERT
 157   __ should_not_reach_here();
 158 #endif
 159 }
 160 
 161 
 162 // Implementation of NewInstanceStub
 163 
 164 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id) {
 165   _result = result;
 166   _klass = klass;
 167   _klass_reg = klass_reg;
 168   _info = new CodeEmitInfo(info);
 169   assert(stub_id == Runtime1::new_instance_id                 ||
 170          stub_id == Runtime1::fast_new_instance_id            ||
 171          stub_id == Runtime1::fast_new_instance_init_check_id,
 172          "need new_instance id");
 173   _stub_id   = stub_id;
 174 }
 175 
 176 
 177 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
 178   __ bind(_entry);
 179   __ call(Runtime1::entry_for(_stub_id), relocInfo::runtime_call_type);
 180   __ delayed()->mov_or_nop(_klass_reg->as_register(), G5);
 181   ce->add_call_info_here(_info);
 182   ce->verify_oop_map(_info);
 183   __ br(Assembler::always, false, Assembler::pt, _continuation);
 184   __ delayed()->mov_or_nop(O0, _result->as_register());
 185 }
 186 
 187 
 188 // Implementation of NewTypeArrayStub
 189 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
 190   _klass_reg = klass_reg;
 191   _length = length;
 192   _result = result;
 193   _info = new CodeEmitInfo(info);
 194 }
 195 
 196 
 197 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
 198   __ bind(_entry);
 199 
 200   __ mov(_length->as_register(), G4);
 201   __ call(Runtime1::entry_for(Runtime1::new_type_array_id), relocInfo::runtime_call_type);
 202   __ delayed()->mov_or_nop(_klass_reg->as_register(), G5);
 203   ce->add_call_info_here(_info);
 204   ce->verify_oop_map(_info);
 205   __ br(Assembler::always, false, Assembler::pt, _continuation);
 206   __ delayed()->mov_or_nop(O0, _result->as_register());
 207 }
 208 
 209 
 210 // Implementation of NewObjectArrayStub
 211 
 212 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
 213   _klass_reg = klass_reg;
 214   _length = length;
 215   _result = result;
 216   _info = new CodeEmitInfo(info);
 217 }
 218 
 219 
 220 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
 221   __ bind(_entry);
 222 
 223   __ mov(_length->as_register(), G4);
 224   __ call(Runtime1::entry_for(Runtime1::new_object_array_id), relocInfo::runtime_call_type);
 225   __ delayed()->mov_or_nop(_klass_reg->as_register(), G5);
 226   ce->add_call_info_here(_info);
 227   ce->verify_oop_map(_info);
 228   __ br(Assembler::always, false, Assembler::pt, _continuation);
 229   __ delayed()->mov_or_nop(O0, _result->as_register());
 230 }
 231 
 232 
 233 // Implementation of MonitorAccessStubs
 234 MonitorEnterStub::MonitorEnterStub(LIR_Opr obj_reg, LIR_Opr lock_reg, CodeEmitInfo* info)
 235   : MonitorAccessStub(obj_reg, lock_reg) {
 236   _info = new CodeEmitInfo(info);
 237 }
 238 
 239 
 240 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
 241   __ bind(_entry);
 242   __ mov(_obj_reg->as_register(), G4);
 243   if (ce->compilation()->has_fpu_code()) {
 244     __ call(Runtime1::entry_for(Runtime1::monitorenter_id), relocInfo::runtime_call_type);
 245   } else {
 246     __ call(Runtime1::entry_for(Runtime1::monitorenter_nofpu_id), relocInfo::runtime_call_type);
 247   }
 248   __ delayed()->mov_or_nop(_lock_reg->as_register(), G5);
 249   ce->add_call_info_here(_info);
 250   ce->verify_oop_map(_info);
 251   __ br(Assembler::always, true, Assembler::pt, _continuation);
 252   __ delayed()->nop();
 253 }
 254 
 255 
 256 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
 257   __ bind(_entry);
 258   if (_compute_lock) {
 259     ce->monitor_address(_monitor_ix, _lock_reg);
 260   }
 261   if (ce->compilation()->has_fpu_code()) {
 262     __ call(Runtime1::entry_for(Runtime1::monitorexit_id), relocInfo::runtime_call_type);
 263   } else {
 264     __ call(Runtime1::entry_for(Runtime1::monitorexit_nofpu_id), relocInfo::runtime_call_type);
 265   }
 266 
 267   __ delayed()->mov_or_nop(_lock_reg->as_register(), G4);
 268   __ br(Assembler::always, true, Assembler::pt, _continuation);
 269   __ delayed()->nop();
 270 }
 271 
 272 // Implementation of patching:
 273 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes)
 274 // - Replace original code with a call to the stub
 275 // At Runtime:
 276 // - call to stub, jump to runtime
 277 // - in runtime: preserve all registers (especially objects, i.e., source and destination object)
 278 // - in runtime: after initializing class, restore original code, reexecute instruction
 279 
 280 int PatchingStub::_patch_info_offset = -NativeGeneralJump::instruction_size;
 281 
 282 void PatchingStub::align_patch_site(MacroAssembler* ) {
 283   // patch sites on sparc are always properly aligned.
 284 }
 285 
 286 void PatchingStub::emit_code(LIR_Assembler* ce) {
 287   // copy original code here
 288   assert(NativeCall::instruction_size <= _bytes_to_copy && _bytes_to_copy <= 0xFF,
 289          "not enough room for call");
 290   assert((_bytes_to_copy & 0x3) == 0, "must copy a multiple of four bytes");
 291 
 292   Label call_patch;
 293 
 294   int being_initialized_entry = __ offset();
 295 
 296   if (_id == load_klass_id) {
 297     // produce a copy of the load klass instruction for use by the being initialized case
 298 #ifdef ASSERT
 299     address start = __ pc();
 300 #endif
 301     AddressLiteral addrlit(NULL, metadata_Relocation::spec(_index));
 302     __ patchable_set(addrlit, _obj);
 303 
 304 #ifdef ASSERT
 305     for (int i = 0; i < _bytes_to_copy; i++) {
 306       address ptr = (address)(_pc_start + i);
 307       int a_byte = (*ptr) & 0xFF;
 308       assert(a_byte == *start++, "should be the same code");
 309     }
 310 #endif
 311   } else if (_id == load_mirror_id) {
 312     // produce a copy of the load mirror instruction for use by the being initialized case
 313 #ifdef ASSERT
 314     address start = __ pc();
 315 #endif
 316     AddressLiteral addrlit(NULL, oop_Relocation::spec(_index));
 317     __ patchable_set(addrlit, _obj);
 318 
 319 #ifdef ASSERT
 320     for (int i = 0; i < _bytes_to_copy; i++) {
 321       address ptr = (address)(_pc_start + i);
 322       int a_byte = (*ptr) & 0xFF;
 323       assert(a_byte == *start++, "should be the same code");
 324     }
 325 #endif
 326   } else {
 327     // make a copy the code which is going to be patched.
 328     for (int i = 0; i < _bytes_to_copy; i++) {
 329       address ptr = (address)(_pc_start + i);
 330       int a_byte = (*ptr) & 0xFF;
 331       __ emit_int8 (a_byte);
 332     }
 333   }
 334 
 335   address end_of_patch = __ pc();
 336   int bytes_to_skip = 0;
 337   if (_id == load_mirror_id) {
 338     int offset = __ offset();
 339     if (CommentedAssembly) {
 340       __ block_comment(" being_initialized check");
 341     }
 342 
 343     // static field accesses have special semantics while the class
 344     // initializer is being run so we emit a test which can be used to
 345     // check that this code is being executed by the initializing
 346     // thread.
 347     assert(_obj != noreg, "must be a valid register");
 348     assert(_index >= 0, "must have oop index");
 349     __ ld_ptr(_obj, java_lang_Class::klass_offset_in_bytes(), G3);
 350     __ ld_ptr(G3, in_bytes(InstanceKlass::init_thread_offset()), G3);
 351     __ cmp_and_brx_short(G2_thread, G3, Assembler::notEqual, Assembler::pn, call_patch);
 352 
 353     // load_klass patches may execute the patched code before it's
 354     // copied back into place so we need to jump back into the main
 355     // code of the nmethod to continue execution.
 356     __ br(Assembler::always, false, Assembler::pt, _patch_site_continuation);
 357     __ delayed()->nop();
 358 
 359     // make sure this extra code gets skipped
 360     bytes_to_skip += __ offset() - offset;
 361   }
 362 
 363   // Now emit the patch record telling the runtime how to find the
 364   // pieces of the patch.  We only need 3 bytes but it has to be
 365   // aligned as an instruction so emit 4 bytes.
 366   int sizeof_patch_record = 4;
 367   bytes_to_skip += sizeof_patch_record;
 368 
 369   // emit the offsets needed to find the code to patch
 370   int being_initialized_entry_offset = __ offset() - being_initialized_entry + sizeof_patch_record;
 371 
 372   // Emit the patch record.  We need to emit a full word, so emit an extra empty byte
 373   __ emit_int8(0);
 374   __ emit_int8(being_initialized_entry_offset);
 375   __ emit_int8(bytes_to_skip);
 376   __ emit_int8(_bytes_to_copy);
 377   address patch_info_pc = __ pc();
 378   assert(patch_info_pc - end_of_patch == bytes_to_skip, "incorrect patch info");
 379 
 380   address entry = __ pc();
 381   NativeGeneralJump::insert_unconditional((address)_pc_start, entry);
 382   address target = NULL;
 383   relocInfo::relocType reloc_type = relocInfo::none;
 384   switch (_id) {
 385     case access_field_id:  target = Runtime1::entry_for(Runtime1::access_field_patching_id); break;
 386     case load_klass_id:    target = Runtime1::entry_for(Runtime1::load_klass_patching_id); reloc_type = relocInfo::metadata_type; break;
 387     case load_mirror_id:   target = Runtime1::entry_for(Runtime1::load_mirror_patching_id); reloc_type = relocInfo::oop_type; break;
 388     default: ShouldNotReachHere();
 389   }
 390   __ bind(call_patch);
 391 
 392   if (CommentedAssembly) {
 393     __ block_comment("patch entry point");
 394   }
 395   __ call(target, relocInfo::runtime_call_type);
 396   __ delayed()->nop();
 397   assert(_patch_info_offset == (patch_info_pc - __ pc()), "must not change");
 398   ce->add_call_info_here(_info);
 399   __ br(Assembler::always, false, Assembler::pt, _patch_site_entry);
 400   __ delayed()->nop();
 401   if (_id == load_klass_id || _id == load_mirror_id) {
 402     CodeSection* cs = __ code_section();
 403     address pc = (address)_pc_start;
 404     RelocIterator iter(cs, pc, pc + 1);
 405     relocInfo::change_reloc_info_for_address(&iter, (address) pc, reloc_type, relocInfo::none);
 406 
 407     pc = (address)(_pc_start + NativeMovConstReg::add_offset);
 408     RelocIterator iter2(cs, pc, pc+1);
 409     relocInfo::change_reloc_info_for_address(&iter2, (address) pc, reloc_type, relocInfo::none);
 410   }
 411 
 412 }
 413 
 414 
 415 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
 416   __ bind(_entry);
 417   __ call(Runtime1::entry_for(Runtime1::deoptimize_id), relocInfo::runtime_call_type);
 418   __ delayed()->nop();
 419   ce->add_call_info_here(_info);
 420   DEBUG_ONLY(__ should_not_reach_here());
 421 }
 422 
 423 
 424 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
 425   //---------------slow case: call to native-----------------
 426   __ bind(_entry);
 427   __ mov(src()->as_register(),     O0);
 428   __ mov(src_pos()->as_register(), O1);
 429   __ mov(dst()->as_register(),     O2);
 430   __ mov(dst_pos()->as_register(), O3);
 431   __ mov(length()->as_register(),  O4);
 432 
 433   ce->emit_static_call_stub();
 434 
 435   __ call(SharedRuntime::get_resolve_static_call_stub(), relocInfo::static_call_type);
 436   __ delayed()->nop();
 437   ce->add_call_info_here(info());
 438   ce->verify_oop_map(info());
 439 
 440 #ifndef PRODUCT
 441   __ set((intptr_t)&Runtime1::_arraycopy_slowcase_cnt, O0);
 442   __ ld(O0, 0, O1);
 443   __ inc(O1);
 444   __ st(O1, 0, O0);
 445 #endif
 446 
 447   __ br(Assembler::always, false, Assembler::pt, _continuation);
 448   __ delayed()->nop();
 449 }
 450 
 451 
 452 ///////////////////////////////////////////////////////////////////////////////////
 453 #if INCLUDE_ALL_GCS
 454 
 455 void G1PreBarrierStub::emit_code(LIR_Assembler* ce) {
 456   // At this point we know that marking is in progress.
 457   // If do_load() is true then we have to emit the
 458   // load of the previous value; otherwise it has already
 459   // been loaded into _pre_val.
 460 
 461   __ bind(_entry);
 462 
 463   assert(pre_val()->is_register(), "Precondition.");
 464   Register pre_val_reg = pre_val()->as_register();
 465 
 466   if (do_load()) {
 467     ce->mem2reg(addr(), pre_val(), T_OBJECT, patch_code(), info(), false /*wide*/, false /*unaligned*/);
 468   }
 469 
 470   if (__ is_in_wdisp16_range(_continuation)) {
 471     __ br_null(pre_val_reg, /*annul*/false, Assembler::pt, _continuation);
 472   } else {
 473     __ cmp(pre_val_reg, G0);
 474     __ brx(Assembler::equal, false, Assembler::pn, _continuation);
 475   }
 476   __ delayed()->nop();
 477 
 478   __ call(Runtime1::entry_for(Runtime1::Runtime1::g1_pre_barrier_slow_id));
 479   __ delayed()->mov(pre_val_reg, G4);
 480   __ br(Assembler::always, false, Assembler::pt, _continuation);
 481   __ delayed()->nop();
 482 
 483 }
 484 
 485 jbyte* G1PostBarrierStub::_byte_map_base = NULL;
 486 
 487 jbyte* G1PostBarrierStub::byte_map_base_slow() {
 488   BarrierSet* bs = Universe::heap()->barrier_set();
 489   assert(bs->is_a(BarrierSet::G1SATBCTLogging),
 490          "Must be if we're using this.");
 491   return ((G1SATBCardTableModRefBS*)bs)->byte_map_base;
 492 }
 493 
 494 void G1PostBarrierStub::emit_code(LIR_Assembler* ce) {
 495   __ bind(_entry);
 496 
 497   assert(addr()->is_register(), "Precondition.");
 498   assert(new_val()->is_register(), "Precondition.");
 499   Register addr_reg = addr()->as_pointer_register();
 500   Register new_val_reg = new_val()->as_register();
 501 
 502   if (__ is_in_wdisp16_range(_continuation)) {
 503     __ br_null(new_val_reg, /*annul*/false, Assembler::pt, _continuation);
 504   } else {
 505     __ cmp(new_val_reg, G0);
 506     __ brx(Assembler::equal, false, Assembler::pn, _continuation);
 507   }
 508   __ delayed()->nop();
 509 
 510   __ call(Runtime1::entry_for(Runtime1::Runtime1::g1_post_barrier_slow_id));
 511   __ delayed()->mov(addr_reg, G4);
 512   __ br(Assembler::always, false, Assembler::pt, _continuation);
 513   __ delayed()->nop();
 514 }
 515 
 516 #endif // INCLUDE_ALL_GCS
 517 ///////////////////////////////////////////////////////////////////////////////////
 518 
 519 #undef __