1 /*
  2  * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, 2015, Red Hat Inc. All rights reserved.
  4  * Copyright (c) 2020, 2021, Huawei Technologies Co., Ltd. All rights reserved.
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 
 27 #ifndef CPU_RISCV_INTERP_MASM_RISCV_HPP
 28 #define CPU_RISCV_INTERP_MASM_RISCV_HPP
 29 
 30 #include "asm/macroAssembler.hpp"
 31 #include "interpreter/invocationCounter.hpp"
 32 #include "runtime/frame.hpp"
 33 
 34 // This file specializes the assember with interpreter-specific macros
 35 
 36 typedef ByteSize (*OffsetFunction)(uint);
 37 
 38 class InterpreterMacroAssembler: public MacroAssembler {
 39  protected:
 40   // Interpreter specific version of call_VM_base
 41   using MacroAssembler::call_VM_leaf_base;
 42 
 43   virtual void call_VM_leaf_base(address entry_point,
 44                                  int number_of_arguments);
 45 
 46   virtual void call_VM_base(Register oop_result,
 47                             Register java_thread,
 48                             Register last_java_sp,
 49                             address  entry_point,
 50                             int number_of_arguments,
 51                             bool check_exceptions);
 52 
 53   // base routine for all dispatches
 54   void dispatch_base(TosState state, address* table, bool verifyoop = true,
 55                      bool generate_poll = false, Register Rs = t0);
 56 
 57  public:
 58   InterpreterMacroAssembler(CodeBuffer* code) : MacroAssembler(code) {}
 59   virtual ~InterpreterMacroAssembler() {}
 60 
 61   void load_earlyret_value(TosState state);
 62 
 63   void jump_to_entry(address entry);
 64 
 65   virtual void check_and_handle_popframe(Register java_thread);
 66   virtual void check_and_handle_earlyret(Register java_thread);
 67 
 68   // Interpreter-specific registers
 69   void save_bcp() {
 70     sd(xbcp, Address(fp, frame::interpreter_frame_bcp_offset * wordSize));
 71   }
 72 
 73   void restore_bcp() {
 74     ld(xbcp, Address(fp, frame::interpreter_frame_bcp_offset * wordSize));
 75   }
 76 
 77   void restore_locals() {
 78     ld(xlocals, Address(fp, frame::interpreter_frame_locals_offset * wordSize));
 79   }
 80 
 81   void restore_constant_pool_cache() {
 82     ld(xcpool, Address(fp, frame::interpreter_frame_cache_offset * wordSize));
 83   }
 84 
 85   void get_dispatch();
 86 
 87   // Helpers for runtime call arguments/results
 88   void get_method(Register reg) {
 89     ld(reg, Address(fp, frame::interpreter_frame_method_offset * wordSize));
 90   }
 91 
 92   void get_const(Register reg) {
 93     get_method(reg);
 94     ld(reg, Address(reg, in_bytes(Method::const_offset())));
 95   }
 96 
 97   void get_constant_pool(Register reg) {
 98     get_const(reg);
 99     ld(reg, Address(reg, in_bytes(ConstMethod::constants_offset())));
100   }
101 
102   void get_constant_pool_cache(Register reg) {
103     get_constant_pool(reg);
104     ld(reg, Address(reg, ConstantPool::cache_offset_in_bytes()));
105   }
106 
107   void get_cpool_and_tags(Register cpool, Register tags) {
108     get_constant_pool(cpool);
109     ld(tags, Address(cpool, ConstantPool::tags_offset_in_bytes()));
110   }
111 
112   void get_unsigned_2_byte_index_at_bcp(Register reg, int bcp_offset);
113   void get_cache_and_index_at_bcp(Register cache, Register index, int bcp_offset, size_t index_size = sizeof(u2));
114   void get_cache_and_index_and_bytecode_at_bcp(Register cache, Register index, Register bytecode, int byte_no, int bcp_offset, size_t index_size = sizeof(u2));
115   void get_cache_entry_pointer_at_bcp(Register cache, Register tmp, int bcp_offset, size_t index_size = sizeof(u2));
116   void get_cache_index_at_bcp(Register index, int bcp_offset, size_t index_size = sizeof(u2));
117   void get_method_counters(Register method, Register mcs, Label& skip);
118 
119   // Load cpool->resolved_references(index).
120   void load_resolved_reference_at_index(Register result, Register index, Register tmp = x15);
121 
122   // Load cpool->resolved_klass_at(index).
123   void load_resolved_klass_at_offset(Register cpool, Register index, Register klass, Register temp);
124 
125   void load_resolved_method_at_index(int byte_no, Register method, Register cache);
126 
127   void pop_ptr(Register r = x10);
128   void pop_i(Register r = x10);
129   void pop_l(Register r = x10);
130   void pop_f(FloatRegister r = f10);
131   void pop_d(FloatRegister r = f10);
132   void push_ptr(Register r = x10);
133   void push_i(Register r = x10);
134   void push_l(Register r = x10);
135   void push_f(FloatRegister r = f10);
136   void push_d(FloatRegister r = f10);
137 
138   void pop(TosState state); // transition vtos -> state
139   void push(TosState state); // transition state -> vtos
140 
141   void empty_expression_stack() {
142     ld(esp, Address(fp, frame::interpreter_frame_monitor_block_top_offset * wordSize));
143     // NULL last_sp until next java call
144     sd(zr, Address(fp, frame::interpreter_frame_last_sp_offset * wordSize));
145   }
146 
147   // Helpers for swap and dup
148   void load_ptr(int n, Register val);
149   void store_ptr(int n, Register val);
150 
151 // Load float value from 'address'. The value is loaded onto the FPU register v0.
152   void load_float(Address src);
153   void load_double(Address src);
154 
155   // Generate a subtype check: branch to ok_is_subtype if sub_klass is
156   // a subtype of super_klass.
157   void gen_subtype_check( Register sub_klass, Label &ok_is_subtype );
158 
159   // Dispatching
160   void dispatch_prolog(TosState state, int step = 0);
161   void dispatch_epilog(TosState state, int step = 0);
162   // dispatch via t0
163   void dispatch_only(TosState state, bool generate_poll = false, Register Rs = t0);
164   // dispatch normal table via t0 (assume t0 is loaded already)
165   void dispatch_only_normal(TosState state, Register Rs = t0);
166   void dispatch_only_noverify(TosState state, Register Rs = t0);
167   // load t0 from [xbcp + step] and dispatch via t0
168   void dispatch_next(TosState state, int step = 0, bool generate_poll = false);
169   // load t0 from [xbcp] and dispatch via t0 and table
170   void dispatch_via (TosState state, address* table);
171 
172   // jump to an invoked target
173   void prepare_to_jump_from_interpreted();
174   void jump_from_interpreted(Register method);
175 
176 
177   // Returning from interpreted functions
178   //
179   // Removes the current activation (incl. unlocking of monitors)
180   // and sets up the return address.  This code is also used for
181   // exception unwindwing. In that case, we do not want to throw
182   // IllegalMonitorStateExceptions, since that might get us into an
183   // infinite rethrow exception loop.
184   // Additionally this code is used for popFrame and earlyReturn.
185   // In popFrame case we want to skip throwing an exception,
186   // installing an exception, and notifying jvmdi.
187   // In earlyReturn case we only want to skip throwing an exception
188   // and installing an exception.
189   void remove_activation(TosState state,
190                          bool throw_monitor_exception = true,
191                          bool install_monitor_exception = true,
192                          bool notify_jvmdi = true);
193 
194   // FIXME: Give us a valid frame at a null check.
195   virtual void null_check(Register reg, int offset = -1) {
196         MacroAssembler::null_check(reg, offset);
197   }
198 
199   // Object locking
200   void lock_object  (Register lock_reg);
201   void unlock_object(Register lock_reg);
202 
203   // Interpreter profiling operations
204   void set_method_data_pointer_for_bcp();
205   void test_method_data_pointer(Register mdp, Label& zero_continue);
206   void verify_method_data_pointer();
207 
208   void set_mdp_data_at(Register mdp_in, int constant, Register value);
209   void increment_mdp_data_at(Address data, bool decrement = false);
210   void increment_mdp_data_at(Register mdp_in, int constant,
211                              bool decrement = false);
212   void increment_mdp_data_at(Register mdp_in, Register reg, int constant,
213                              bool decrement = false);
214   void increment_mask_and_jump(Address counter_addr,
215                                int increment, Address mask,
216                                Register tmp1, Register tmp2,
217                                bool preloaded, Label* where);
218 
219   void set_mdp_flag_at(Register mdp_in, int flag_constant);
220   void test_mdp_data_at(Register mdp_in, int offset, Register value,
221                         Register test_value_out,
222                         Label& not_equal_continue);
223 
224   void record_klass_in_profile(Register receiver, Register mdp,
225                                Register reg2, bool is_virtual_call);
226   void record_klass_in_profile_helper(Register receiver, Register mdp,
227                                       Register reg2,
228                                       Label& done, bool is_virtual_call);
229   void record_item_in_profile_helper(Register item, Register mdp,
230                                      Register reg2, int start_row, Label& done, int total_rows,
231                                      OffsetFunction item_offset_fn, OffsetFunction item_count_offset_fn,
232                                      int non_profiled_offset);
233 
234   void update_mdp_by_offset(Register mdp_in, int offset_of_offset);
235   void update_mdp_by_offset(Register mdp_in, Register reg, int offset_of_disp);
236   void update_mdp_by_constant(Register mdp_in, int constant);
237   void update_mdp_for_ret(Register return_bci);
238 
239   // narrow int return value
240   void narrow(Register result);
241 
242   void profile_taken_branch(Register mdp, Register bumped_count);
243   void profile_not_taken_branch(Register mdp);
244   void profile_call(Register mdp);
245   void profile_final_call(Register mdp);
246   void profile_virtual_call(Register receiver, Register mdp,
247                             Register t1,
248                             bool receiver_can_be_null = false);
249   void profile_ret(Register return_bci, Register mdp);
250   void profile_null_seen(Register mdp);
251   void profile_typecheck(Register mdp, Register klass, Register temp);
252   void profile_typecheck_failed(Register mdp);
253   void profile_switch_default(Register mdp);
254   void profile_switch_case(Register index_in_scratch, Register mdp,
255                            Register temp);
256 
257   void profile_obj_type(Register obj, const Address& mdo_addr, Register tmp);
258   void profile_arguments_type(Register mdp, Register callee, Register tmp, bool is_virtual);
259   void profile_return_type(Register mdp, Register ret, Register tmp);
260   void profile_parameters_type(Register mdp, Register tmp1, Register tmp2, Register tmp3);
261 
262   // Debugging
263   // only if +VerifyFPU  && (state == ftos || state == dtos)
264   void verify_FPU(int stack_depth, TosState state = ftos);
265 
266   typedef enum { NotifyJVMTI, SkipNotifyJVMTI } NotifyMethodExitMode;
267 
268   // support for jvmti/dtrace
269   void notify_method_entry();
270   void notify_method_exit(TosState state, NotifyMethodExitMode mode);
271 
272   virtual void _call_Unimplemented(address call_site) {
273     save_bcp();
274     set_last_Java_frame(esp, fp, (address) pc(), t0);
275     MacroAssembler::_call_Unimplemented(call_site);
276   }
277 
278 #ifdef ASSERT
279   void verify_access_flags(Register access_flags, uint32_t flag_bits,
280                            const char* msg, bool stop_by_hit = true);
281   void verify_frame_setup();
282 #endif
283 };
284 
285 #endif // CPU_RISCV_INTERP_MASM_RISCV_HPP