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