1 /*
  2  * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, 2015, Red Hat Inc. 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_AARCH64_INTERP_MASM_AARCH64_HPP
 27 #define CPU_AARCH64_INTERP_MASM_AARCH64_HPP
 28 
 29 #include "asm/macroAssembler.hpp"
 30 #include "interpreter/invocationCounter.hpp"
 31 #include "runtime/frame.hpp"
 32 
 33 // This file specializes the assembler with interpreter-specific macros
 34 
 35 typedef ByteSize (*OffsetFunction)(uint);
 36 
 37 class InterpreterMacroAssembler: public MacroAssembler {
 38  protected:
 39   // Interpreter specific version of call_VM_base
 40   using MacroAssembler::call_VM_leaf_base;
 41 
 42   virtual void call_VM_leaf_base(address entry_point,
 43                                  int number_of_arguments);
 44 
 45   virtual void call_VM_base(Register oop_result,
 46                             Register java_thread,
 47                             Register last_java_sp,
 48                             Label*   return_pc,
 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,
 55                      bool verifyoop = true, bool generate_poll = false);
 56 
 57  public:
 58   InterpreterMacroAssembler(CodeBuffer* code) : MacroAssembler(code) {}
 59 
 60   void load_earlyret_value(TosState state);
 61 
 62   // Use for vthread preemption
 63   void call_VM_preemptable(Register oop_result,
 64                            address entry_point,
 65                            Register arg_1,
 66                            bool check_exceptions = true);
 67   void call_VM_preemptable(Register oop_result,
 68                            address entry_point,
 69                            Register arg_1,
 70                            Register arg_2,
 71                            bool check_exceptions = true);
 72   void restore_after_resume(bool is_native);
 73  private:
 74   void call_VM_preemptable_helper(Register oop_result,
 75                                   address entry_point,
 76                                   int number_of_arguments,
 77                                   bool check_exceptions);
 78 
 79  public:
 80   void jump_to_entry(address entry);
 81 
 82   virtual void check_and_handle_popframe(Register java_thread);
 83   virtual void check_and_handle_earlyret(Register java_thread);
 84 
 85   // Interpreter-specific registers
 86   void save_bcp() {
 87     str(rbcp, Address(rfp, frame::interpreter_frame_bcp_offset * wordSize));
 88   }
 89 
 90   void restore_bcp() {
 91     ldr(rbcp, Address(rfp, frame::interpreter_frame_bcp_offset * wordSize));
 92   }
 93 
 94   void restore_locals() {
 95     ldr(rlocals, Address(rfp, frame::interpreter_frame_locals_offset * wordSize));
 96     lea(rlocals, Address(rfp, rlocals, Address::lsl(3)));
 97   }
 98 
 99   void restore_constant_pool_cache() {
100     ldr(rcpool, Address(rfp, frame::interpreter_frame_cache_offset * wordSize));
101   }
102 
103   void restore_sp_after_call() {
104     Label L;
105     ldr(rscratch1, Address(rfp, frame::interpreter_frame_extended_sp_offset * wordSize));
106     lea(rscratch1, Address(rfp, rscratch1, Address::lsl(LogBytesPerWord)));
107 #ifdef ASSERT
108     cbnz(rscratch1, L);
109     stop("SP is null");
110 #endif
111     bind(L);
112     mov(sp, rscratch1);
113   }
114 
115   void check_extended_sp(const char* msg = "check extended SP") {
116 #ifdef ASSERT
117     Label L;
118     ldr(rscratch1, Address(rfp, frame::interpreter_frame_extended_sp_offset * wordSize));
119     lea(rscratch1, Address(rfp, rscratch1, Address::lsl(LogBytesPerWord)));
120     cmp(sp, rscratch1);
121     br(EQ, L);
122     stop(msg);
123     bind(L);
124 #endif
125   }
126 
127 #define check_extended_sp()                                             \
128   check_extended_sp("SP does not match extended SP in frame at " __FILE__ ":" XSTR(__LINE__))
129 
130   void get_dispatch();
131 
132   // Helpers for runtime call arguments/results
133   void get_method(Register reg) {
134     ldr(reg, Address(rfp, frame::interpreter_frame_method_offset * wordSize));
135   }
136 
137   void get_const(Register reg) {
138     get_method(reg);
139     ldr(reg, Address(reg, in_bytes(Method::const_offset())));
140   }
141 
142   void get_constant_pool(Register reg) {
143     get_const(reg);
144     ldr(reg, Address(reg, in_bytes(ConstMethod::constants_offset())));
145   }
146 
147   void get_constant_pool_cache(Register reg) {
148     get_constant_pool(reg);
149     ldr(reg, Address(reg, ConstantPool::cache_offset()));
150   }
151 
152   void get_cpool_and_tags(Register cpool, Register tags) {
153     get_constant_pool(cpool);
154     ldr(tags, Address(cpool, ConstantPool::tags_offset()));
155   }
156 
157   void get_unsigned_2_byte_index_at_bcp(Register reg, int bcp_offset);
158   void get_cache_index_at_bcp(Register index, int bcp_offset, size_t index_size = sizeof(u2));
159   void get_method_counters(Register method, Register mcs, Label& skip);
160 
161   // load cpool->resolved_references(index);
162   void load_resolved_reference_at_index(Register result, Register index, Register tmp = r5);
163 
164   // load cpool->resolved_klass_at(index);
165   void load_resolved_klass_at_offset(Register cpool, Register index, Register klass, Register temp);
166 
167   void pop_ptr(Register r = r0);
168   void pop_i(Register r = r0);
169   void pop_l(Register r = r0);
170   void pop_f(FloatRegister r = v0);
171   void pop_d(FloatRegister r = v0);
172   void push_ptr(Register r = r0);
173   void push_i(Register r = r0);
174   void push_l(Register r = r0);
175   void push_f(FloatRegister r = v0);
176   void push_d(FloatRegister r = v0);
177 
178   void pop(Register r ) { ((MacroAssembler*)this)->pop(r); }
179 
180   void push(Register r ) { ((MacroAssembler*)this)->push(r); }
181 
182   void pop(TosState state); // transition vtos -> state
183   void push(TosState state); // transition state -> vtos
184 
185   void pop(RegSet regs, Register stack) { ((MacroAssembler*)this)->pop(regs, stack); }
186   void push(RegSet regs, Register stack) { ((MacroAssembler*)this)->push(regs, stack); }
187 
188   void empty_expression_stack() {
189     ldr(rscratch1, Address(rfp, frame::interpreter_frame_monitor_block_top_offset * wordSize));
190     lea(esp, Address(rfp, rscratch1, Address::lsl(LogBytesPerWord)));
191     // null last_sp until next java call
192     str(zr, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
193   }
194 
195   // Helpers for swap and dup
196   void load_ptr(int n, Register val);
197   void store_ptr(int n, Register val);
198 
199   // Load float value from 'address'. The value is loaded onto the FPU register v0.
200   void load_float(Address src);
201   void load_double(Address src);
202 
203   // Generate a subtype check: branch to ok_is_subtype if sub_klass is
204   // a subtype of super_klass.
205   void gen_subtype_check( Register sub_klass, Label &ok_is_subtype );
206 
207   // Dispatching
208   void dispatch_prolog(TosState state, int step = 0);
209   void dispatch_epilog(TosState state, int step = 0);
210   // dispatch via rscratch1
211   void dispatch_only(TosState state, bool generate_poll = false);
212   // dispatch normal table via rscratch1 (assume rscratch1 is loaded already)
213   void dispatch_only_normal(TosState state);
214   void dispatch_only_noverify(TosState state);
215   // load rscratch1 from [rbcp + step] and dispatch via rscratch1
216   void dispatch_next(TosState state, int step = 0, bool generate_poll = false);
217   // load rscratch1 from [esi] and dispatch via rscratch1 and table
218   void dispatch_via (TosState state, address* table);
219 
220   // jump to an invoked target
221   void prepare_to_jump_from_interpreted();
222   void jump_from_interpreted(Register method, Register temp);
223 
224 
225   // Returning from interpreted functions
226   //
227   // Removes the current activation (incl. unlocking of monitors)
228   // and sets up the return address.  This code is also used for
229   // exception unwindwing. In that case, we do not want to throw
230   // IllegalMonitorStateExceptions, since that might get us into an
231   // infinite rethrow exception loop.
232   // Additionally this code is used for popFrame and earlyReturn.
233   // In popFrame case we want to skip throwing an exception,
234   // installing an exception, and notifying jvmdi.
235   // In earlyReturn case we only want to skip throwing an exception
236   // and installing an exception.
237   void remove_activation(TosState state,
238                          bool throw_monitor_exception = true,
239                          bool install_monitor_exception = true,
240                          bool notify_jvmdi = true);
241 
242   // FIXME: Give us a valid frame at a null check.
243   virtual void null_check(Register reg, int offset = -1) {
244 // #ifdef ASSERT
245 //     save_bcp();
246 //     set_last_Java_frame(esp, rfp, (address) pc());
247 // #endif
248     MacroAssembler::null_check(reg, offset);
249 // #ifdef ASSERT
250 //     reset_last_Java_frame(true);
251 // #endif
252   }
253 
254   // Object locking
255   void lock_object  (Register lock_reg);
256   void unlock_object(Register lock_reg);
257 
258   // Interpreter profiling operations
259   void set_method_data_pointer_for_bcp();
260   void test_method_data_pointer(Register mdp, Label& zero_continue);
261   void verify_method_data_pointer();
262 
263   void set_mdp_data_at(Register mdp_in, int constant, Register value);
264   void increment_mdp_data_at(Register mdp_in, int constant);
265   void increment_mdp_data_at(Register mdp_in, Register index, int constant);
266   void increment_mask_and_jump(Address counter_addr,
267                                int increment, Address mask,
268                                Register scratch, Register scratch2,
269                                bool preloaded, Condition cond,
270                                Label* where);
271   void set_mdp_flag_at(Register mdp_in, int flag_constant);
272   void test_mdp_data_at(Register mdp_in, int offset, Register value,
273                         Register test_value_out,
274                         Label& not_equal_continue);
275 
276   void record_klass_in_profile(Register receiver, Register mdp,
277                                Register reg2);
278   void record_klass_in_profile_helper(Register receiver, Register mdp,
279                                       Register reg2, int start_row,
280                                       Label& done);
281   void record_item_in_profile_helper(Register item, Register mdp,
282                                      Register reg2, int start_row, Label& done, int total_rows,
283                                      OffsetFunction item_offset_fn, OffsetFunction item_count_offset_fn);
284 
285   void update_mdp_by_offset(Register mdp_in, int offset_of_offset);
286   void update_mdp_by_offset(Register mdp_in, Register reg, int offset_of_disp);
287   void update_mdp_by_constant(Register mdp_in, int constant);
288   void update_mdp_for_ret(Register return_bci);
289 
290   // narrow int return value
291   void narrow(Register result);
292 
293   void profile_taken_branch(Register mdp);
294   void profile_not_taken_branch(Register mdp);
295   void profile_call(Register mdp);
296   void profile_final_call(Register mdp);
297   void profile_virtual_call(Register receiver, Register mdp,
298                             Register scratch2,
299                             bool receiver_can_be_null = false);
300   void profile_ret(Register return_bci, Register mdp);
301   void profile_null_seen(Register mdp);
302   void profile_typecheck(Register mdp, Register klass, Register scratch);
303   void profile_typecheck_failed(Register mdp);
304   void profile_switch_default(Register mdp);
305   void profile_switch_case(Register index_in_scratch, Register mdp,
306                            Register scratch2);
307 
308   void profile_obj_type(Register obj, const Address& mdo_addr);
309   void profile_arguments_type(Register mdp, Register callee, Register tmp, bool is_virtual);
310   void profile_return_type(Register mdp, Register ret, Register tmp);
311   void profile_parameters_type(Register mdp, Register tmp1, Register tmp2);
312 
313   // Debugging
314   // only if +VerifyOops && state == atos
315 #define interp_verify_oop(reg, state) _interp_verify_oop(reg, state, __FILE__, __LINE__);
316   void _interp_verify_oop(Register reg, TosState state, const char* file, int line);
317 
318   typedef enum { NotifyJVMTI, SkipNotifyJVMTI } NotifyMethodExitMode;
319 
320   // support for jvmti/dtrace
321   void notify_method_entry();
322   void notify_method_exit(TosState state, NotifyMethodExitMode mode);
323 
324   JFR_ONLY(void enter_jfr_critical_section();)
325   JFR_ONLY(void leave_jfr_critical_section();)
326 
327   virtual void _call_Unimplemented(address call_site) {
328     save_bcp();
329     set_last_Java_frame(esp, rfp, (address) pc(), rscratch1);
330     MacroAssembler::_call_Unimplemented(call_site);
331   }
332 
333   void load_resolved_indy_entry(Register cache, Register index);
334   void load_field_entry(Register cache, Register index, int bcp_offset = 1);
335   void load_method_entry(Register cache, Register index, int bcp_offset = 1);
336 };
337 
338 #endif // CPU_AARCH64_INTERP_MASM_AARCH64_HPP