1 /*
  2  * Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2020, 2023, Huawei Technologies Co., Ltd. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef CPU_RISCV_GC_SHARED_BARRIERSETASSEMBLER_RISCV_HPP
 27 #define CPU_RISCV_GC_SHARED_BARRIERSETASSEMBLER_RISCV_HPP
 28 
 29 #include "asm/macroAssembler.hpp"
 30 #include "gc/shared/barrierSet.hpp"
 31 #include "gc/shared/barrierSetNMethod.hpp"
 32 #include "memory/allocation.hpp"
 33 #include "oops/access.hpp"
 34 #ifdef COMPILER2
 35 #include "opto/optoreg.hpp"
 36 
 37 class BarrierStubC2;
 38 class Node;
 39 #endif // COMPILER2
 40 
 41 enum class NMethodPatchingType {
 42   stw_instruction_and_data_patch,
 43   conc_instruction_and_data_patch
 44 };
 45 
 46 class BarrierSetAssembler: public CHeapObj<mtGC> {
 47 public:
 48   virtual void arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
 49                                   Register src, Register dst, Register count, RegSet saved_regs) {}
 50   virtual void arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
 51                                   Register start, Register count, Register tmp) {}
 52 
 53   virtual void copy_load_at(MacroAssembler* masm,
 54                             DecoratorSet decorators,
 55                             BasicType type,
 56                             size_t bytes,
 57                             Register dst,
 58                             Address src,
 59                             Register tmp);
 60 
 61   virtual void copy_store_at(MacroAssembler* masm,
 62                              DecoratorSet decorators,
 63                              BasicType type,
 64                              size_t bytes,
 65                              Address dst,
 66                              Register src,
 67                              Register tmp1,
 68                              Register tmp2,
 69                              Register tmp3);
 70 
 71   virtual bool supports_rvv_arraycopy() { return true; }
 72 
 73   virtual void load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 74                        Register dst, Address src, Register tmp1, Register tmp2);
 75   virtual void store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 76                         Address dst, Register val, Register tmp1, Register tmp2, Register tmp3);
 77 
 78   virtual void try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
 79                                              Register obj, Register tmp, Label& slowpath);
 80 
 81   virtual void tlab_allocate(MacroAssembler* masm,
 82     Register obj,                      // result: pointer to object after successful allocation
 83     Register var_size_in_bytes,        // object size in bytes if unknown at compile time; invalid otherwise
 84     int      con_size_in_bytes,        // object size in bytes if   known at compile time
 85     Register tmp1,                     // temp register
 86     Register tmp2,                     // temp register
 87     Label&   slow_case,                // continuation point if fast allocation fails
 88     bool is_far = false
 89   );
 90 
 91   virtual void barrier_stubs_init() {}
 92 
 93   virtual NMethodPatchingType nmethod_patching_type() { return NMethodPatchingType::stw_instruction_and_data_patch; }
 94 
 95   virtual void nmethod_entry_barrier(MacroAssembler* masm, Label* slow_path, Label* continuation, Label* guard);
 96   virtual void c2i_entry_barrier(MacroAssembler* masm);
 97 
 98   virtual void check_oop(MacroAssembler* masm, Register obj, Register tmp1, Register tmp2, Label& error);
 99 
100   virtual bool supports_instruction_patching() {
101     NMethodPatchingType patching_type = nmethod_patching_type();
102     return patching_type == NMethodPatchingType::conc_instruction_and_data_patch ||
103             patching_type == NMethodPatchingType::stw_instruction_and_data_patch;
104   }
105 
106   static address patching_epoch_addr();
107   static void clear_patching_epoch();
108   static void increment_patching_epoch();
109 
110   // See AS_NO_KEEPALIVE for peek semantics
111   // weak_handle and obj may alias
112   virtual void try_peek_weak_handle_in_nmethod(MacroAssembler* masm, Register weak_handle, Register obj,
113                                                Register tmp, Label& slow_path);
114 
115 #ifdef COMPILER2
116   OptoReg::Name refine_register(const Node* node,
117                                 OptoReg::Name opto_reg);
118 #endif // COMPILER2
119 };
120 
121 #ifdef COMPILER2
122 
123 // This class saves and restores the registers that need to be preserved across
124 // the runtime call represented by a given C2 barrier stub. Use as follows:
125 // {
126 //   SaveLiveRegisters save(masm, stub);
127 //   ..
128 //   __ jalr(...);
129 //   ..
130 // }
131 class SaveLiveRegisters {
132 private:
133   MacroAssembler* const _masm;
134   RegSet                _gp_regs;
135   FloatRegSet           _fp_regs;
136   VectorRegSet          _vp_regs;
137 
138 public:
139   void initialize(BarrierStubC2* stub);
140   SaveLiveRegisters(MacroAssembler* masm, BarrierStubC2* stub);
141   ~SaveLiveRegisters();
142 };
143 
144 #endif // COMPILER2
145 
146 #endif // CPU_RISCV_GC_SHARED_BARRIERSETASSEMBLER_RISCV_HPP