1 /*
   2  * Copyright (c) 1999, 2011, 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 "utilities/array.hpp"
  34 
  35 class CodeEmitInfo;
  36 class LIR_Assembler;
  37 class LIR_OpVisitState;
  38 
  39 // CodeStubs are little 'out-of-line' pieces of code that
  40 // usually handle slow cases of operations. All code stubs
  41 // are collected and code is emitted at the end of the
  42 // nmethod.
  43 
  44 class CodeStub: public CompilationResourceObj {
  45  protected:
  46   Label _entry;                                  // label at the stub entry point
  47   Label _continuation;                           // label where stub continues, if any
  48 
  49  public:
  50   CodeStub() {}
  51 
  52   // code generation
  53   void assert_no_unbound_labels()                { assert(!_entry.is_unbound() && !_continuation.is_unbound(), "unbound label"); }
  54   virtual void emit_code(LIR_Assembler* e) = 0;
  55   virtual CodeEmitInfo* info() const             { return NULL; }
  56   virtual bool is_exception_throw_stub() const   { return false; }
  57   virtual bool is_range_check_stub() const       { return false; }
  58   virtual bool is_divbyzero_stub() const         { return false; }
  59 #ifndef PRODUCT
  60   virtual void print_name(outputStream* out) const = 0;
  61 #endif
  62 
  63   // label access
  64   Label* entry()                                 { return &_entry; }
  65   Label* continuation()                          { return &_continuation; }
  66   // for LIR
  67   virtual void visit(LIR_OpVisitState* visit) {
  68 #ifndef PRODUCT
  69     if (LIRTracePeephole && Verbose) {
  70       tty->print("no visitor for ");
  71       print_name(tty);
  72       tty->cr();
  73     }
  74 #endif
  75   }
  76 };
  77 
  78 
  79 define_array(CodeStubArray, CodeStub*)
  80 define_stack(_CodeStubList, CodeStubArray)
  81 
  82 class CodeStubList: public _CodeStubList {
  83  public:
  84   CodeStubList(): _CodeStubList() {}
  85 
  86   void append(CodeStub* stub) {
  87     if (!contains(stub)) {
  88       _CodeStubList::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   bool          _throw_index_out_of_bounds_exception;
 152 
 153  public:
 154   RangeCheckStub(CodeEmitInfo* info, LIR_Opr index, bool throw_index_out_of_bounds_exception = false);
 155   virtual void emit_code(LIR_Assembler* e);
 156   virtual CodeEmitInfo* info() const             { return _info; }
 157   virtual bool is_exception_throw_stub() const   { return true; }
 158   virtual bool is_range_check_stub() const       { return true; }
 159   virtual void visit(LIR_OpVisitState* visitor) {
 160     visitor->do_slow_case(_info);
 161     visitor->do_input(_index);
 162   }
 163 #ifndef PRODUCT
 164   virtual void print_name(outputStream* out) const { out->print("RangeCheckStub"); }
 165 #endif // PRODUCT
 166 };
 167 
 168 
 169 class DivByZeroStub: public CodeStub {
 170  private:
 171   CodeEmitInfo* _info;
 172   int           _offset;
 173 
 174  public:
 175   DivByZeroStub(CodeEmitInfo* info)
 176     : _info(info), _offset(-1) {
 177   }
 178   DivByZeroStub(int offset, CodeEmitInfo* info)
 179     : _info(info), _offset(offset) {
 180   }
 181   virtual void emit_code(LIR_Assembler* e);
 182   virtual CodeEmitInfo* info() const             { return _info; }
 183   virtual bool is_exception_throw_stub() const   { return true; }
 184   virtual bool is_divbyzero_stub() const         { return true; }
 185   virtual void visit(LIR_OpVisitState* visitor) {
 186     visitor->do_slow_case(_info);
 187   }
 188 #ifndef PRODUCT
 189   virtual void print_name(outputStream* out) const { out->print("DivByZeroStub"); }
 190 #endif // PRODUCT
 191 };
 192 
 193 
 194 class ImplicitNullCheckStub: public CodeStub {
 195  private:
 196   CodeEmitInfo* _info;
 197   int           _offset;
 198 
 199  public:
 200   ImplicitNullCheckStub(int offset, CodeEmitInfo* info)
 201     : _offset(offset), _info(info) {
 202   }
 203   virtual void emit_code(LIR_Assembler* e);
 204   virtual CodeEmitInfo* info() const             { return _info; }
 205   virtual bool is_exception_throw_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("ImplicitNullCheckStub"); }
 211 #endif // PRODUCT
 212 };
 213 
 214 
 215 class NewInstanceStub: public CodeStub {
 216  private:
 217   ciInstanceKlass* _klass;
 218   LIR_Opr          _klass_reg;
 219   LIR_Opr          _result;
 220   CodeEmitInfo*    _info;
 221   Runtime1::StubID _stub_id;
 222 
 223  public:
 224   NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id);
 225   virtual void emit_code(LIR_Assembler* e);
 226   virtual CodeEmitInfo* info() const             { return _info; }
 227   virtual void visit(LIR_OpVisitState* visitor) {
 228     visitor->do_slow_case(_info);
 229     visitor->do_input(_klass_reg);
 230     visitor->do_output(_result);
 231   }
 232 #ifndef PRODUCT
 233   virtual void print_name(outputStream* out) const { out->print("NewInstanceStub"); }
 234 #endif // PRODUCT
 235 };
 236 
 237 
 238 class NewTypeArrayStub: public CodeStub {
 239  private:
 240   LIR_Opr       _klass_reg;
 241   LIR_Opr       _length;
 242   LIR_Opr       _result;
 243   CodeEmitInfo* _info;
 244 
 245  public:
 246   NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info);
 247   virtual void emit_code(LIR_Assembler* e);
 248   virtual CodeEmitInfo* info() const             { return _info; }
 249   virtual void visit(LIR_OpVisitState* visitor) {
 250     visitor->do_slow_case(_info);
 251     visitor->do_input(_klass_reg);
 252     visitor->do_input(_length);
 253     assert(_result->is_valid(), "must be valid"); visitor->do_output(_result);
 254   }
 255 #ifndef PRODUCT
 256   virtual void print_name(outputStream* out) const { out->print("NewTypeArrayStub"); }
 257 #endif // PRODUCT
 258 };
 259 
 260 
 261 class NewObjectArrayStub: public CodeStub {
 262  private:
 263   LIR_Opr        _klass_reg;
 264   LIR_Opr        _length;
 265   LIR_Opr        _result;
 266   CodeEmitInfo*  _info;
 267 
 268  public:
 269   NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info);
 270   virtual void emit_code(LIR_Assembler* e);
 271   virtual CodeEmitInfo* info() const             { return _info; }
 272   virtual void visit(LIR_OpVisitState* visitor) {
 273     visitor->do_slow_case(_info);
 274     visitor->do_input(_klass_reg);
 275     visitor->do_input(_length);
 276     assert(_result->is_valid(), "must be valid"); visitor->do_output(_result);
 277   }
 278 #ifndef PRODUCT
 279   virtual void print_name(outputStream* out) const { out->print("NewObjectArrayStub"); }
 280 #endif // PRODUCT
 281 };
 282 
 283 
 284 class MonitorAccessStub: public CodeStub {
 285  protected:
 286   LIR_Opr _obj_reg;
 287   LIR_Opr _lock_reg;
 288 
 289  public:
 290   MonitorAccessStub(LIR_Opr obj_reg, LIR_Opr lock_reg) {
 291     _obj_reg  = obj_reg;
 292     _lock_reg  = lock_reg;
 293   }
 294 
 295 #ifndef PRODUCT
 296   virtual void print_name(outputStream* out) const { out->print("MonitorAccessStub"); }
 297 #endif // PRODUCT
 298 };
 299 
 300 
 301 class MonitorEnterStub: public MonitorAccessStub {
 302  private:
 303   CodeEmitInfo* _info;
 304 
 305  public:
 306   MonitorEnterStub(LIR_Opr obj_reg, LIR_Opr lock_reg, CodeEmitInfo* info);
 307 
 308   virtual void emit_code(LIR_Assembler* e);
 309   virtual CodeEmitInfo* info() const             { return _info; }
 310   virtual void visit(LIR_OpVisitState* visitor) {
 311     visitor->do_input(_obj_reg);
 312     visitor->do_input(_lock_reg);
 313     visitor->do_slow_case(_info);
 314   }
 315 #ifndef PRODUCT
 316   virtual void print_name(outputStream* out) const { out->print("MonitorEnterStub"); }
 317 #endif // PRODUCT
 318 };
 319 
 320 
 321 class MonitorExitStub: public MonitorAccessStub {
 322  private:
 323   bool _compute_lock;
 324   int  _monitor_ix;
 325 
 326  public:
 327   MonitorExitStub(LIR_Opr lock_reg, bool compute_lock, int monitor_ix)
 328     : MonitorAccessStub(LIR_OprFact::illegalOpr, lock_reg),
 329       _compute_lock(compute_lock), _monitor_ix(monitor_ix) { }
 330   virtual void emit_code(LIR_Assembler* e);
 331   virtual void visit(LIR_OpVisitState* visitor) {
 332     assert(_obj_reg->is_illegal(), "unused");
 333     if (_compute_lock) {
 334       visitor->do_temp(_lock_reg);
 335     } else {
 336       visitor->do_input(_lock_reg);
 337     }
 338   }
 339 #ifndef PRODUCT
 340   virtual void print_name(outputStream* out) const { out->print("MonitorExitStub"); }
 341 #endif // PRODUCT
 342 };
 343 
 344 
 345 class PatchingStub: public CodeStub {
 346  public:
 347   enum PatchID {
 348     access_field_id,
 349     load_klass_id
 350   };
 351   enum constants {
 352     patch_info_size = 3
 353   };
 354  private:
 355   PatchID       _id;
 356   address       _pc_start;
 357   int           _bytes_to_copy;
 358   Label         _patched_code_entry;
 359   Label         _patch_site_entry;
 360   Label         _patch_site_continuation;
 361   Register      _obj;
 362   CodeEmitInfo* _info;
 363   int           _oop_index;  // index of the patchable oop in nmethod oop table if needed
 364   static int    _patch_info_offset;
 365 
 366   void align_patch_site(MacroAssembler* masm);
 367 
 368  public:
 369   static int patch_info_offset() { return _patch_info_offset; }
 370 
 371   PatchingStub(MacroAssembler* masm, PatchID id, int oop_index = -1):
 372       _id(id)
 373     , _info(NULL)
 374     , _oop_index(oop_index) {
 375     if (os::is_MP()) {
 376       // force alignment of patch sites on MP hardware so we
 377       // can guarantee atomic writes to the patch site.
 378       align_patch_site(masm);
 379     }
 380     _pc_start = masm->pc();
 381     masm->bind(_patch_site_entry);
 382   }
 383 
 384   void install(MacroAssembler* masm, LIR_PatchCode patch_code, Register obj, CodeEmitInfo* info) {
 385     _info = info;
 386     _obj = obj;
 387     masm->bind(_patch_site_continuation);
 388     _bytes_to_copy = masm->pc() - pc_start();
 389     if (_id == PatchingStub::access_field_id) {
 390       // embed a fixed offset to handle long patches which need to be offset by a word.
 391       // the patching code will just add the field offset field to this offset so
 392       // that we can refernce either the high or low word of a double word field.
 393       int field_offset = 0;
 394       switch (patch_code) {
 395       case lir_patch_low:         field_offset = lo_word_offset_in_bytes; break;
 396       case lir_patch_high:        field_offset = hi_word_offset_in_bytes; break;
 397       case lir_patch_normal:      field_offset = 0;                       break;
 398       default: ShouldNotReachHere();
 399       }
 400       NativeMovRegMem* n_move = nativeMovRegMem_at(pc_start());
 401       n_move->set_offset(field_offset);
 402     } else if (_id == load_klass_id) {
 403       assert(_obj != noreg, "must have register object for load_klass");
 404 #ifdef ASSERT
 405       // verify that we're pointing at a NativeMovConstReg
 406       nativeMovConstReg_at(pc_start());
 407 #endif
 408     } else {
 409       ShouldNotReachHere();
 410     }
 411     assert(_bytes_to_copy <= (masm->pc() - pc_start()), "not enough bytes");
 412   }
 413 
 414   address pc_start() const                       { return _pc_start; }
 415   PatchID id() const                             { return _id; }
 416 
 417   virtual void emit_code(LIR_Assembler* e);
 418   virtual CodeEmitInfo* info() const             { return _info; }
 419   virtual void visit(LIR_OpVisitState* visitor) {
 420     visitor->do_slow_case(_info);
 421   }
 422 #ifndef PRODUCT
 423   virtual void print_name(outputStream* out) const { out->print("PatchingStub"); }
 424 #endif // PRODUCT
 425 };
 426 
 427 
 428 //------------------------------------------------------------------------------
 429 // DeoptimizeStub
 430 //
 431 class DeoptimizeStub : public CodeStub {
 432 private:
 433   CodeEmitInfo* _info;
 434 
 435 public:
 436   DeoptimizeStub(CodeEmitInfo* info) : _info(new CodeEmitInfo(info)) {}
 437 
 438   virtual void emit_code(LIR_Assembler* e);
 439   virtual CodeEmitInfo* info() const           { return _info; }
 440   virtual bool is_exception_throw_stub() const { return true; }
 441   virtual void visit(LIR_OpVisitState* visitor) {
 442     visitor->do_slow_case(_info);
 443   }
 444 #ifndef PRODUCT
 445   virtual void print_name(outputStream* out) const { out->print("DeoptimizeStub"); }
 446 #endif // PRODUCT
 447 };
 448 
 449 
 450 class SimpleExceptionStub: public CodeStub {
 451  private:
 452   LIR_Opr          _obj;
 453   Runtime1::StubID _stub;
 454   CodeEmitInfo*    _info;
 455 
 456  public:
 457   SimpleExceptionStub(Runtime1::StubID stub, LIR_Opr obj, CodeEmitInfo* info):
 458     _obj(obj), _info(info), _stub(stub) {
 459   }
 460 
 461   void set_obj(LIR_Opr obj) {
 462     _obj = obj;
 463   }
 464 
 465   virtual void emit_code(LIR_Assembler* e);
 466   virtual CodeEmitInfo* info() const             { return _info; }
 467   virtual bool is_exception_throw_stub() const   { return true; }
 468   virtual void visit(LIR_OpVisitState* visitor) {
 469     if (_obj->is_valid()) visitor->do_input(_obj);
 470     visitor->do_slow_case(_info);
 471   }
 472 #ifndef PRODUCT
 473   virtual void print_name(outputStream* out) const { out->print("SimpleExceptionStub"); }
 474 #endif // PRODUCT
 475 };
 476 
 477 
 478 
 479 class ArrayStoreExceptionStub: public SimpleExceptionStub {
 480  private:
 481   CodeEmitInfo* _info;
 482 
 483  public:
 484   ArrayStoreExceptionStub(LIR_Opr obj, CodeEmitInfo* info): SimpleExceptionStub(Runtime1::throw_array_store_exception_id, obj, info) {}
 485 #ifndef PRODUCT
 486   virtual void print_name(outputStream* out) const { out->print("ArrayStoreExceptionStub"); }
 487 #endif // PRODUCT
 488 };
 489 
 490 
 491 class ArrayCopyStub: public CodeStub {
 492  private:
 493   LIR_OpArrayCopy* _op;
 494 
 495  public:
 496   ArrayCopyStub(LIR_OpArrayCopy* op): _op(op) { }
 497 
 498   LIR_Opr src() const                         { return _op->src(); }
 499   LIR_Opr src_pos() const                     { return _op->src_pos(); }
 500   LIR_Opr dst() const                         { return _op->dst(); }
 501   LIR_Opr dst_pos() const                     { return _op->dst_pos(); }
 502   LIR_Opr length() const                      { return _op->length(); }
 503   LIR_Opr tmp() const                         { return _op->tmp(); }
 504 
 505   virtual void emit_code(LIR_Assembler* e);
 506   virtual CodeEmitInfo* info() const          { return _op->info(); }
 507   virtual void visit(LIR_OpVisitState* visitor) {
 508     // don't pass in the code emit info since it's processed in the fast path
 509     visitor->do_slow_case();
 510   }
 511 #ifndef PRODUCT
 512   virtual void print_name(outputStream* out) const { out->print("ArrayCopyStub"); }
 513 #endif // PRODUCT
 514 };
 515 
 516 //////////////////////////////////////////////////////////////////////////////////////////
 517 #ifndef SERIALGC
 518 
 519 // Code stubs for Garbage-First barriers.
 520 class G1PreBarrierStub: public CodeStub {
 521  private:
 522   bool _do_load;
 523   LIR_Opr _addr;
 524   LIR_Opr _pre_val;
 525   LIR_PatchCode _patch_code;
 526   CodeEmitInfo* _info;
 527 
 528  public:
 529  
 530   // Version that _does_ generate a load of the previous value from addr.
 531   // addr (the address of the field to be read) must be a LIR_Address
 532   // pre_val (a temporary register) must be a register;
 533   G1PreBarrierStub(LIR_Opr addr, LIR_Opr pre_val, LIR_PatchCode patch_code, CodeEmitInfo* info) :
 534     _addr(addr), _pre_val(pre_val), _do_load(true),
 535     _patch_code(patch_code), _info(info)
 536   {
 537     assert(_pre_val->is_register(), "should be temporary register");
 538     assert(_addr->is_address(), "should be the address of the field");
 539   }
 540 
 541   // Version that _does not_ generate load of the previous value; the
 542   // previous value is assumed to have already been loaded into pre_val.
 543   G1PreBarrierStub(LIR_Opr pre_val) :
 544     _addr(LIR_OprFact::illegalOpr), _pre_val(pre_val), _do_load(false),
 545     _patch_code(lir_patch_none), _info(NULL)
 546   {
 547     assert(_pre_val->is_register(), "should be a register");
 548   }
 549 
 550   LIR_Opr addr() const { return _addr; }
 551   LIR_Opr pre_val() const { return _pre_val; }
 552   LIR_PatchCode patch_code() const { return _patch_code; }
 553   CodeEmitInfo* info() const { return _info; }
 554   bool do_load() const { return _do_load; }
 555 
 556   virtual void emit_code(LIR_Assembler* e);
 557   virtual void visit(LIR_OpVisitState* visitor) {
 558     if (_do_load) {
 559       // don't pass in the code emit info since it's processed in the fast
 560       // path
 561       if (_info != NULL)
 562         visitor->do_slow_case(_info);
 563       else
 564         visitor->do_slow_case();
 565 
 566       visitor->do_input(_addr);
 567       visitor->do_temp(_pre_val);
 568     } else {
 569       visitor->do_slow_case();
 570       visitor->do_input(_pre_val);
 571     }
 572   }
 573 #ifndef PRODUCT
 574   virtual void print_name(outputStream* out) const { out->print("G1PreBarrierStub"); }
 575 #endif // PRODUCT
 576 };
 577 
 578 class G1PostBarrierStub: public CodeStub {
 579  private:
 580   LIR_Opr _addr;
 581   LIR_Opr _new_val;
 582 
 583   static jbyte* _byte_map_base;
 584   static jbyte* byte_map_base_slow();
 585   static jbyte* byte_map_base() {
 586     if (_byte_map_base == NULL) {
 587       _byte_map_base = byte_map_base_slow();
 588     }
 589     return _byte_map_base;
 590   }
 591 
 592  public:
 593   // addr (the address of the object head) and new_val must be registers.
 594   G1PostBarrierStub(LIR_Opr addr, LIR_Opr new_val): _addr(addr), _new_val(new_val) { }
 595 
 596   LIR_Opr addr() const { return _addr; }
 597   LIR_Opr new_val() const { return _new_val; }
 598 
 599   virtual void emit_code(LIR_Assembler* e);
 600   virtual void visit(LIR_OpVisitState* visitor) {
 601     // don't pass in the code emit info since it's processed in the fast path
 602     visitor->do_slow_case();
 603     visitor->do_input(_addr);
 604     visitor->do_input(_new_val);
 605   }
 606 #ifndef PRODUCT
 607   virtual void print_name(outputStream* out) const { out->print("G1PostBarrierStub"); }
 608 #endif // PRODUCT
 609 };
 610 
 611 #endif // SERIALGC
 612 //////////////////////////////////////////////////////////////////////////////////////////
 613 
 614 #endif // SHARE_VM_C1_C1_CODESTUBS_HPP