1 /* 2 * Copyright (c) 1999, 2016, 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 #ifndef SHARE_VM_C1_C1_CODESTUBS_HPP 26 #define SHARE_VM_C1_C1_CODESTUBS_HPP 27 28 #include "c1/c1_FrameMap.hpp" 29 #include "c1/c1_IR.hpp" 30 #include "c1/c1_Instruction.hpp" 31 #include "c1/c1_LIR.hpp" 32 #include "c1/c1_Runtime1.hpp" 33 #include "code/nativeInst.hpp" 34 #include "utilities/array.hpp" 35 #include "utilities/macros.hpp" 36 37 class CodeEmitInfo; 38 class LIR_Assembler; 39 class LIR_OpVisitState; 40 41 // CodeStubs are little 'out-of-line' pieces of code that 42 // usually handle slow cases of operations. All code stubs 43 // are collected and code is emitted at the end of the 44 // nmethod. 45 46 class CodeStub: public CompilationResourceObj { 47 protected: 48 Label _entry; // label at the stub entry point 49 Label _continuation; // label where stub continues, if any 50 51 public: 52 CodeStub() {} 53 54 // code generation 55 void assert_no_unbound_labels() { assert(!_entry.is_unbound() && !_continuation.is_unbound(), "unbound label"); } 56 virtual void emit_code(LIR_Assembler* e) = 0; 57 virtual CodeEmitInfo* info() const { return NULL; } 58 virtual bool is_exception_throw_stub() const { return false; } 59 virtual bool is_range_check_stub() const { return false; } 60 virtual bool is_divbyzero_stub() const { return false; } 61 #ifndef PRODUCT 62 virtual void print_name(outputStream* out) const = 0; 63 #endif 64 65 // label access 66 Label* entry() { return &_entry; } 67 Label* continuation() { return &_continuation; } 68 // for LIR 69 virtual void visit(LIR_OpVisitState* visit) { 70 #ifndef PRODUCT 71 if (LIRTracePeephole && Verbose) { 72 tty->print("no visitor for "); 73 print_name(tty); 74 tty->cr(); 75 } 76 #endif 77 } 78 }; 79 80 class CodeStubList: public GrowableArray<CodeStub*> { 81 public: 82 CodeStubList(): GrowableArray<CodeStub*>() {} 83 84 void append(CodeStub* stub) { 85 if (!contains(stub)) { 86 GrowableArray<CodeStub*>::append(stub); 87 } 88 } 89 }; 90 91 class CounterOverflowStub: public CodeStub { 92 private: 93 CodeEmitInfo* _info; 94 int _bci; 95 LIR_Opr _method; 96 97 public: 98 CounterOverflowStub(CodeEmitInfo* info, int bci, LIR_Opr method) : _info(info), _bci(bci), _method(method) { 99 } 100 101 virtual void emit_code(LIR_Assembler* e); 102 103 virtual void visit(LIR_OpVisitState* visitor) { 104 visitor->do_slow_case(_info); 105 visitor->do_input(_method); 106 } 107 108 #ifndef PRODUCT 109 virtual void print_name(outputStream* out) const { out->print("CounterOverflowStub"); } 110 #endif // PRODUCT 111 112 }; 113 114 class ConversionStub: public CodeStub { 115 private: 116 Bytecodes::Code _bytecode; 117 LIR_Opr _input; 118 LIR_Opr _result; 119 120 static float float_zero; 121 static double double_zero; 122 public: 123 ConversionStub(Bytecodes::Code bytecode, LIR_Opr input, LIR_Opr result) 124 : _bytecode(bytecode), _input(input), _result(result) { 125 } 126 127 Bytecodes::Code bytecode() { return _bytecode; } 128 LIR_Opr input() { return _input; } 129 LIR_Opr result() { return _result; } 130 131 virtual void emit_code(LIR_Assembler* e); 132 virtual void visit(LIR_OpVisitState* visitor) { 133 visitor->do_slow_case(); 134 visitor->do_input(_input); 135 visitor->do_output(_result); 136 } 137 #ifndef PRODUCT 138 virtual void print_name(outputStream* out) const { out->print("ConversionStub"); } 139 #endif // PRODUCT 140 }; 141 142 143 // Throws ArrayIndexOutOfBoundsException by default but can be 144 // configured to throw IndexOutOfBoundsException in constructor 145 class RangeCheckStub: public CodeStub { 146 private: 147 CodeEmitInfo* _info; 148 LIR_Opr _index; 149 bool _throw_index_out_of_bounds_exception; 150 151 public: 152 RangeCheckStub(CodeEmitInfo* info, LIR_Opr index, bool throw_index_out_of_bounds_exception = false); 153 virtual void emit_code(LIR_Assembler* e); 154 virtual CodeEmitInfo* info() const { return _info; } 155 virtual bool is_exception_throw_stub() const { return true; } 156 virtual bool is_range_check_stub() const { return true; } 157 virtual void visit(LIR_OpVisitState* visitor) { 158 visitor->do_slow_case(_info); 159 visitor->do_input(_index); 160 } 161 #ifndef PRODUCT 162 virtual void print_name(outputStream* out) const { out->print("RangeCheckStub"); } 163 #endif // PRODUCT 164 }; 165 166 // stub used when predicate fails and deoptimization is needed 167 class PredicateFailedStub: public CodeStub { 168 private: 169 CodeEmitInfo* _info; 170 171 public: 172 PredicateFailedStub(CodeEmitInfo* info); 173 virtual void emit_code(LIR_Assembler* e); 174 virtual CodeEmitInfo* info() const { return _info; } 175 virtual void visit(LIR_OpVisitState* visitor) { 176 visitor->do_slow_case(_info); 177 } 178 #ifndef PRODUCT 179 virtual void print_name(outputStream* out) const { out->print("PredicateFailedStub"); } 180 #endif // PRODUCT 181 }; 182 183 class DivByZeroStub: public CodeStub { 184 private: 185 CodeEmitInfo* _info; 186 int _offset; 187 188 public: 189 DivByZeroStub(CodeEmitInfo* info) 190 : _info(info), _offset(-1) { 191 } 192 DivByZeroStub(int offset, CodeEmitInfo* info) 193 : _info(info), _offset(offset) { 194 } 195 virtual void emit_code(LIR_Assembler* e); 196 virtual CodeEmitInfo* info() const { return _info; } 197 virtual bool is_exception_throw_stub() const { return true; } 198 virtual bool is_divbyzero_stub() const { return true; } 199 virtual void visit(LIR_OpVisitState* visitor) { 200 visitor->do_slow_case(_info); 201 } 202 #ifndef PRODUCT 203 virtual void print_name(outputStream* out) const { out->print("DivByZeroStub"); } 204 #endif // PRODUCT 205 }; 206 207 208 class ImplicitNullCheckStub: public CodeStub { 209 private: 210 CodeEmitInfo* _info; 211 int _offset; 212 213 public: 214 ImplicitNullCheckStub(int offset, CodeEmitInfo* info) 215 : _offset(offset), _info(info) { 216 } 217 virtual void emit_code(LIR_Assembler* e); 218 virtual CodeEmitInfo* info() const { return _info; } 219 virtual bool is_exception_throw_stub() const { return true; } 220 virtual void visit(LIR_OpVisitState* visitor) { 221 visitor->do_slow_case(_info); 222 } 223 #ifndef PRODUCT 224 virtual void print_name(outputStream* out) const { out->print("ImplicitNullCheckStub"); } 225 #endif // PRODUCT 226 }; 227 228 229 class NewInstanceStub: public CodeStub { 230 private: 231 ciInstanceKlass* _klass; 232 LIR_Opr _klass_reg; 233 LIR_Opr _result; 234 CodeEmitInfo* _info; 235 Runtime1::StubID _stub_id; 236 237 public: 238 NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id); 239 virtual void emit_code(LIR_Assembler* e); 240 virtual CodeEmitInfo* info() const { return _info; } 241 virtual void visit(LIR_OpVisitState* visitor) { 242 visitor->do_slow_case(_info); 243 visitor->do_input(_klass_reg); 244 visitor->do_output(_result); 245 } 246 #ifndef PRODUCT 247 virtual void print_name(outputStream* out) const { out->print("NewInstanceStub"); } 248 #endif // PRODUCT 249 }; 250 251 252 class NewTypeArrayStub: public CodeStub { 253 private: 254 LIR_Opr _klass_reg; 255 LIR_Opr _length; 256 LIR_Opr _result; 257 CodeEmitInfo* _info; 258 259 public: 260 NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info); 261 virtual void emit_code(LIR_Assembler* e); 262 virtual CodeEmitInfo* info() const { return _info; } 263 virtual void visit(LIR_OpVisitState* visitor) { 264 visitor->do_slow_case(_info); 265 visitor->do_input(_klass_reg); 266 visitor->do_input(_length); 267 assert(_result->is_valid(), "must be valid"); visitor->do_output(_result); 268 } 269 #ifndef PRODUCT 270 virtual void print_name(outputStream* out) const { out->print("NewTypeArrayStub"); } 271 #endif // PRODUCT 272 }; 273 274 275 class NewObjectArrayStub: public CodeStub { 276 private: 277 LIR_Opr _klass_reg; 278 LIR_Opr _length; 279 LIR_Opr _result; 280 CodeEmitInfo* _info; 281 282 public: 283 NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info); 284 virtual void emit_code(LIR_Assembler* e); 285 virtual CodeEmitInfo* info() const { return _info; } 286 virtual void visit(LIR_OpVisitState* visitor) { 287 visitor->do_slow_case(_info); 288 visitor->do_input(_klass_reg); 289 visitor->do_input(_length); 290 assert(_result->is_valid(), "must be valid"); visitor->do_output(_result); 291 } 292 #ifndef PRODUCT 293 virtual void print_name(outputStream* out) const { out->print("NewObjectArrayStub"); } 294 #endif // PRODUCT 295 }; 296 297 298 class MonitorAccessStub: public CodeStub { 299 protected: 300 LIR_Opr _obj_reg; 301 LIR_Opr _lock_reg; 302 303 public: 304 MonitorAccessStub(LIR_Opr obj_reg, LIR_Opr lock_reg) { 305 _obj_reg = obj_reg; 306 _lock_reg = lock_reg; 307 } 308 309 #ifndef PRODUCT 310 virtual void print_name(outputStream* out) const { out->print("MonitorAccessStub"); } 311 #endif // PRODUCT 312 }; 313 314 315 class MonitorEnterStub: public MonitorAccessStub { 316 private: 317 CodeEmitInfo* _info; 318 319 public: 320 MonitorEnterStub(LIR_Opr obj_reg, LIR_Opr lock_reg, CodeEmitInfo* info); 321 322 virtual void emit_code(LIR_Assembler* e); 323 virtual CodeEmitInfo* info() const { return _info; } 324 virtual void visit(LIR_OpVisitState* visitor) { 325 visitor->do_input(_obj_reg); 326 visitor->do_input(_lock_reg); 327 visitor->do_slow_case(_info); 328 } 329 #ifndef PRODUCT 330 virtual void print_name(outputStream* out) const { out->print("MonitorEnterStub"); } 331 #endif // PRODUCT 332 }; 333 334 335 class MonitorExitStub: public MonitorAccessStub { 336 private: 337 bool _compute_lock; 338 int _monitor_ix; 339 340 public: 341 MonitorExitStub(LIR_Opr lock_reg, bool compute_lock, int monitor_ix) 342 : MonitorAccessStub(LIR_OprFact::illegalOpr, lock_reg), 343 _compute_lock(compute_lock), _monitor_ix(monitor_ix) { } 344 virtual void emit_code(LIR_Assembler* e); 345 virtual void visit(LIR_OpVisitState* visitor) { 346 assert(_obj_reg->is_illegal(), "unused"); 347 if (_compute_lock) { 348 visitor->do_temp(_lock_reg); 349 } else { 350 visitor->do_input(_lock_reg); 351 } 352 } 353 #ifndef PRODUCT 354 virtual void print_name(outputStream* out) const { out->print("MonitorExitStub"); } 355 #endif // PRODUCT 356 }; 357 358 359 class PatchingStub: public CodeStub { 360 public: 361 enum PatchID { 362 access_field_id, 363 load_klass_id, 364 load_mirror_id, 365 load_appendix_id 366 }; 367 enum constants { 368 patch_info_size = 3 369 }; 370 private: 371 PatchID _id; 372 address _pc_start; 373 int _bytes_to_copy; 374 Label _patched_code_entry; 375 Label _patch_site_entry; 376 Label _patch_site_continuation; 377 Register _obj; 378 CodeEmitInfo* _info; 379 int _index; // index of the patchable oop or Klass* in nmethod oop or metadata table if needed 380 static int _patch_info_offset; 381 382 void align_patch_site(MacroAssembler* masm); 383 384 public: 385 static int patch_info_offset() { return _patch_info_offset; } 386 387 PatchingStub(MacroAssembler* masm, PatchID id, int index = -1): 388 _id(id) 389 , _info(NULL) 390 , _index(index) { 391 if (os::is_MP()) { 392 // force alignment of patch sites on MP hardware so we 393 // can guarantee atomic writes to the patch site. 394 align_patch_site(masm); 395 } 396 _pc_start = masm->pc(); 397 masm->bind(_patch_site_entry); 398 } 399 400 void install(MacroAssembler* masm, LIR_PatchCode patch_code, Register obj, CodeEmitInfo* info) { 401 _info = info; 402 _obj = obj; 403 masm->bind(_patch_site_continuation); 404 _bytes_to_copy = masm->pc() - pc_start(); 405 if (_id == PatchingStub::access_field_id) { 406 // embed a fixed offset to handle long patches which need to be offset by a word. 407 // the patching code will just add the field offset field to this offset so 408 // that we can refernce either the high or low word of a double word field. 409 int field_offset = 0; 410 switch (patch_code) { 411 case lir_patch_low: field_offset = lo_word_offset_in_bytes; break; 412 case lir_patch_high: field_offset = hi_word_offset_in_bytes; break; 413 case lir_patch_normal: field_offset = 0; break; 414 default: ShouldNotReachHere(); 415 } 416 NativeMovRegMem* n_move = nativeMovRegMem_at(pc_start()); 417 n_move->set_offset(field_offset); 418 } else if (_id == load_klass_id || _id == load_mirror_id || _id == load_appendix_id) { 419 assert(_obj != noreg, "must have register object for load_klass/load_mirror"); 420 #ifdef ASSERT 421 // verify that we're pointing at a NativeMovConstReg 422 nativeMovConstReg_at(pc_start()); 423 #endif 424 } else { 425 ShouldNotReachHere(); 426 } 427 assert(_bytes_to_copy <= (masm->pc() - pc_start()), "not enough bytes"); 428 } 429 430 address pc_start() const { return _pc_start; } 431 PatchID id() const { return _id; } 432 433 virtual void emit_code(LIR_Assembler* e); 434 virtual CodeEmitInfo* info() const { return _info; } 435 virtual void visit(LIR_OpVisitState* visitor) { 436 visitor->do_slow_case(_info); 437 } 438 #ifndef PRODUCT 439 virtual void print_name(outputStream* out) const { out->print("PatchingStub"); } 440 #endif // PRODUCT 441 }; 442 443 444 //------------------------------------------------------------------------------ 445 // DeoptimizeStub 446 // 447 class DeoptimizeStub : public CodeStub { 448 private: 449 CodeEmitInfo* _info; 450 jint _trap_request; 451 452 public: 453 DeoptimizeStub(CodeEmitInfo* info, Deoptimization::DeoptReason reason, Deoptimization::DeoptAction action) : 454 _info(new CodeEmitInfo(info)), _trap_request(Deoptimization::make_trap_request(reason, action)) {} 455 456 virtual void emit_code(LIR_Assembler* e); 457 virtual CodeEmitInfo* info() const { return _info; } 458 virtual bool is_exception_throw_stub() const { return true; } 459 virtual void visit(LIR_OpVisitState* visitor) { 460 visitor->do_slow_case(_info); 461 } 462 #ifndef PRODUCT 463 virtual void print_name(outputStream* out) const { out->print("DeoptimizeStub"); } 464 #endif // PRODUCT 465 }; 466 467 468 class SimpleExceptionStub: public CodeStub { 469 private: 470 LIR_Opr _obj; 471 Runtime1::StubID _stub; 472 CodeEmitInfo* _info; 473 474 public: 475 SimpleExceptionStub(Runtime1::StubID stub, LIR_Opr obj, CodeEmitInfo* info): 476 _obj(obj), _info(info), _stub(stub) { 477 } 478 479 void set_obj(LIR_Opr obj) { 480 _obj = obj; 481 } 482 483 virtual void emit_code(LIR_Assembler* e); 484 virtual CodeEmitInfo* info() const { return _info; } 485 virtual bool is_exception_throw_stub() const { return true; } 486 virtual void visit(LIR_OpVisitState* visitor) { 487 if (_obj->is_valid()) visitor->do_input(_obj); 488 visitor->do_slow_case(_info); 489 } 490 #ifndef PRODUCT 491 virtual void print_name(outputStream* out) const { out->print("SimpleExceptionStub"); } 492 #endif // PRODUCT 493 }; 494 495 496 497 class ArrayStoreExceptionStub: public SimpleExceptionStub { 498 private: 499 CodeEmitInfo* _info; 500 501 public: 502 ArrayStoreExceptionStub(LIR_Opr obj, CodeEmitInfo* info): SimpleExceptionStub(Runtime1::throw_array_store_exception_id, obj, info) {} 503 #ifndef PRODUCT 504 virtual void print_name(outputStream* out) const { out->print("ArrayStoreExceptionStub"); } 505 #endif // PRODUCT 506 }; 507 508 509 class ArrayCopyStub: public CodeStub { 510 private: 511 LIR_OpArrayCopy* _op; 512 513 public: 514 ArrayCopyStub(LIR_OpArrayCopy* op): _op(op) { } 515 516 LIR_Opr src() const { return _op->src(); } 517 LIR_Opr src_pos() const { return _op->src_pos(); } 518 LIR_Opr dst() const { return _op->dst(); } 519 LIR_Opr dst_pos() const { return _op->dst_pos(); } 520 LIR_Opr length() const { return _op->length(); } 521 LIR_Opr tmp() const { return _op->tmp(); } 522 523 virtual void emit_code(LIR_Assembler* e); 524 virtual CodeEmitInfo* info() const { return _op->info(); } 525 virtual void visit(LIR_OpVisitState* visitor) { 526 // don't pass in the code emit info since it's processed in the fast path 527 visitor->do_slow_case(); 528 } 529 #ifndef PRODUCT 530 virtual void print_name(outputStream* out) const { out->print("ArrayCopyStub"); } 531 #endif // PRODUCT 532 }; 533 534 #endif // SHARE_VM_C1_C1_CODESTUBS_HPP