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