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