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