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