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