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