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