1 /*
2  * Copyright (c) 1997, 2018, 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 CPU_SPARC_VM_INTERP_MASM_SPARC_HPP
26 #define CPU_SPARC_VM_INTERP_MASM_SPARC_HPP
27 
28 #include "asm/macroAssembler.hpp"
29 #include "interpreter/invocationCounter.hpp"
30 
31 // This file specializes the assember with interpreter-specific macros
32 
33 typedef ByteSize (*OffsetFunction)(uint);
34 
35 REGISTER_DECLARATION(     Register, Otos_i , O0); // tos for ints, etc
36 REGISTER_DECLARATION(     Register, Otos_l , O0); // for longs
37 REGISTER_DECLARATION(     Register, Otos_l1, O0); // for 1st part of longs
38 REGISTER_DECLARATION(     Register, Otos_l2, O1); // for 2nd part of longs
39 REGISTER_DECLARATION(FloatRegister, Ftos_f , F0); // for floats
40 REGISTER_DECLARATION(FloatRegister, Ftos_d , F0); // for doubles
41 REGISTER_DECLARATION(FloatRegister, Ftos_d1, F0); // for 1st part of double
42 REGISTER_DECLARATION(FloatRegister, Ftos_d2, F1); // for 2nd part of double
43 
44 class InterpreterMacroAssembler: public MacroAssembler {
45  protected:
46   // Interpreter specific version of call_VM_base
47     virtual void call_VM_leaf_base(
48     Register java_thread,
49     address  entry_point,
50     int      number_of_arguments
51   );
52 
53   virtual void call_VM_base(
54     Register        oop_result,
55     Register        java_thread,
56     Register        last_java_sp,
57     address         entry_point,
58     int             number_of_arguments,
59     bool            check_exception=true
60   );
61 
62   // base routine for all dispatches
63   void dispatch_base(TosState state, address* table);
64 
65  public:
InterpreterMacroAssembler(CodeBuffer * c)66   InterpreterMacroAssembler(CodeBuffer* c)
67     : MacroAssembler(c) {}
68 
69  virtual void check_and_handle_popframe(Register scratch_reg);
70  virtual void check_and_handle_earlyret(Register scratch_reg);
71 
72   void jump_to_entry(address entry);
73 
74   virtual void load_earlyret_value(TosState state);
75 
76   static const Address l_tmp ;
77   static const Address d_tmp ;
78 
79   // helper routine for frame allocation/deallocation
80   // compute the delta by which the caller's SP has to
81   // be adjusted to accomodate for the non-argument
82   // locals
83   void compute_extra_locals_size_in_bytes(Register args_size, Register locals_size, Register delta);
84 
85   // dispatch routines
86   void dispatch_prolog(TosState state, int step = 0);
87   void dispatch_epilog(TosState state, int step = 0);
88   void dispatch_only(TosState state);
89   void dispatch_normal(TosState state);
90   void dispatch_next(TosState state, int step = 0, bool generate_poll = false);
91   void dispatch_next_noverify_oop(TosState state, int step = 0);
92   void dispatch_via (TosState state, address* table);
93 
94 
95   void narrow(Register result);
96 
97   // Removes the current activation (incl. unlocking of monitors).
98   // Additionally this code is used for earlyReturn in which case we
99   // want to skip throwing an exception and installing an exception.
100   void remove_activation(TosState state,
101                          bool throw_monitor_exception = true,
102                          bool install_monitor_exception = true);
103 
104  protected:
105   void dispatch_Lbyte_code(TosState state, address* table, int bcp_incr = 0, bool verify = true, bool generate_poll = false);
106 
107  public:
108   // Super call_VM calls - correspond to MacroAssembler::call_VM(_leaf) calls
109   void super_call_VM(Register thread_cache,
110                      Register oop_result,
111                      Register last_java_sp,
112                      address entry_point,
113                      Register arg_1,
114                      Register arg_2,
115                      bool check_exception = true);
116 
117   void super_call_VM_leaf(Register thread_cache, address entry_point, Register arg_1, Register arg_2);
118 
119   // Generate a subtype check: branch to ok_is_subtype if sub_klass is
120   // a subtype of super_klass.  Blows registers tmp1, tmp2 and tmp3.
121   void gen_subtype_check( Register sub_klass, Register super_klass, Register tmp1, Register tmp2, Register tmp3, Label &ok_is_subtype );
122 
123   // helpers for tossing exceptions
124   void throw_if_not_1_icc( Condition ok_condition, Label& ok );
125   void throw_if_not_1_xcc( Condition ok_condition, Label& ok );
126   void throw_if_not_1_x  ( Condition ok_condition, Label& ok ); // chooses icc or xcc based on _LP64
127 
128   void throw_if_not_2( address throw_entry_point, Register Rscratch, Label& ok);
129 
130   void throw_if_not_icc( Condition ok_condition, address throw_entry_point, Register Rscratch );
131   void throw_if_not_xcc( Condition ok_condition, address throw_entry_point, Register Rscratch );
132   void throw_if_not_x  ( Condition ok_condition, address throw_entry_point, Register Rscratch );
133 
134   // helpers for expression stack
135 
136   void pop_i(     Register r = Otos_i);
137   void pop_ptr(   Register r = Otos_i, Register scratch = O4);
138   void pop_l(     Register r = Otos_l1);
139   // G4_scratch and Lscratch are used at call sites!!
140   void pop_f(FloatRegister f = Ftos_f,  Register scratch = G1_scratch);
141   void pop_d(FloatRegister f = Ftos_d1, Register scratch = G1_scratch);
142 
143   void push_i(     Register r = Otos_i);
144   void push_ptr(   Register r = Otos_i);
145   void push_l(     Register r = Otos_l1);
146   void push_f(FloatRegister f = Ftos_f);
147   void push_d(FloatRegister f = Ftos_d1);
148 
149 
150   void pop (TosState state);           // transition vtos -> state
151   void push(TosState state);           // transition state -> vtos
152   void empty_expression_stack();       // resets both Lesp and SP
153 
154 #ifdef ASSERT
155   void verify_sp(Register Rsp, Register Rtemp);
156   void verify_esp(Register Resp);      // verify that Lesp points to a word in the temp stack
157 #endif // ASSERT
158 
159  public:
160   void if_cmp(Condition cc, bool ptr_compare);
161 
162   // Load values from bytecode stream:
163 
164   enum signedOrNot { Signed, Unsigned };
165   enum setCCOrNot  { set_CC,  dont_set_CC };
166 
167   void get_2_byte_integer_at_bcp( int         bcp_offset,
168                                   Register    Rtmp,
169                                   Register    Rdst,
170                                   signedOrNot is_signed,
171                                   setCCOrNot  should_set_CC = dont_set_CC );
172 
173   void get_4_byte_integer_at_bcp( int        bcp_offset,
174                                   Register   Rtmp,
175                                   Register   Rdst,
176                                   setCCOrNot should_set_CC = dont_set_CC );
177 
178   // Note: "get_cache_and_index" really means "get the index, use it to get the cache entry, and throw away the index".
179   void get_cache_and_index_at_bcp(Register cache, Register tmp, int bcp_offset, size_t index_size = sizeof(u2));
180   void get_cache_and_index_and_bytecode_at_bcp(Register cache, Register temp, Register bytecode, int byte_no, int bcp_offset, size_t index_size = sizeof(u2));
181   void get_cache_entry_pointer_at_bcp(Register cache, Register tmp, int bcp_offset, size_t index_size = sizeof(u2));
182   // Note: This one does not fetch the cache.  The first argument is a temp which may be killed.
183   void get_cache_index_at_bcp(Register temp, Register index, int bcp_offset, size_t index_size = sizeof(u2));
184 
185   // load cpool->resolved_references(index);
186   void load_resolved_reference_at_index(Register result, Register index, Register tmp);
187 
188   // load cpool->resolved_klass_at(index)
189   void load_resolved_klass_at_offset(Register Rcpool, Register Roffset, Register Rklass);
190 
191   // common code
192 
193   void field_offset_at(int n, Register tmp, Register dest, Register base);
194   int  field_offset_at(Register object, address bcp, int offset);
195   void fast_iaaccess(int n, address bcp);
196   void fast_iaputfield(address bcp, bool do_store_check );
197 
198   void index_check(Register array, Register index, int index_shift, Register tmp, Register res);
199   void index_check_without_pop(Register array, Register index, int index_shift, Register tmp, Register res);
200 
201   void get_const(Register Rdst);
202   void get_constant_pool(Register Rdst);
203   void get_constant_pool_cache(Register Rdst);
204   void get_cpool_and_tags(Register Rcpool, Register Rtags);
205   void is_a(Label& L);
206 
207   // Load compiled (i2c) or interpreter entry and call from interpreted
208   void call_from_interpreter(Register target, Register scratch, Register Rret);
209 
210   // --------------------------------------------------
211 
212   void unlock_if_synchronized_method(TosState state, bool throw_monitor_exception = true, bool install_monitor_exception = true);
213 
214   void add_monitor_to_stack( bool stack_is_empty,
215                              Register Rtemp,
216                              Register Rtemp2 );
217 
218   // Load/store aligned in _LP64 but unaligned otherwise
219   // These only apply to the Interpreter expression stack and locals!
220   void  load_unaligned_double(Register r1, int offset,  FloatRegister d);
221   void store_unaligned_double(FloatRegister d, Register r1, int offset );
222 
223   // Load/store aligned in _LP64 but unaligned otherwise
224   void  load_unaligned_long(Register r1, int offset,  Register d);
225   void store_unaligned_long(Register d, Register r1, int offset );
226 
227   void access_local_int( Register index, Register dst );
228   void access_local_ptr( Register index, Register dst );
229   void access_local_returnAddress( Register index, Register dst );
230   void access_local_long( Register index, Register dst );
231   void access_local_float( Register index, FloatRegister dst );
232   void access_local_double( Register index, FloatRegister dst );
233 #ifdef ASSERT
234   void check_for_regarea_stomp( Register Rindex, int offset, Register Rlimit, Register Rscratch, Register Rscratch1);
235 #endif // ASSERT
236   void store_local_int( Register index, Register src );
237   void store_local_ptr( Register index, Register src );
238   void store_local_ptr( int n, Register src );
239   void store_local_long( Register index, Register src );
240   void store_local_float( Register index, FloatRegister src );
241   void store_local_double( Register index, FloatRegister src );
242 
243   // Helpers for swap and dup
244   void load_ptr(int n, Register val);
245   void store_ptr(int n, Register val);
246 
247   // Helper for getting receiver in register.
248   void load_receiver(Register param_count, Register recv);
249 
250   static int top_most_monitor_byte_offset(); // offset in bytes to top of monitor block
251   Address top_most_monitor();
252   void compute_stack_base( Register Rdest );
253 
254   void get_method_counters(Register method, Register Rcounters, Label& skip);
255   void increment_invocation_counter( Register Rcounters, Register Rtmp, Register Rtmp2 );
256   void increment_backedge_counter( Register Rcounters, Register Rtmp, Register Rtmp2 );
257   void test_backedge_count_for_osr(Register backedge_count, Register method_counters, Register branch_bcp, Register Rtmp );
258 
259   // Object locking
260   void lock_object  (Register lock_reg, Register obj_reg);
261   void unlock_object(Register lock_reg);
262 
263   // Interpreter profiling operations
264   void set_method_data_pointer();
265   void set_method_data_pointer_for_bcp();
266   void test_method_data_pointer(Label& zero_continue);
267   void verify_method_data_pointer();
268   void test_invocation_counter_for_mdp(Register invocation_count, Register method_counters, Register Rtmp, Label &profile_continue);
269 
270   void set_mdp_data_at(int constant, Register value);
271   void increment_mdp_data_at(Address counter, Register bumped_count,
272                              bool decrement = false);
273   void increment_mdp_data_at(int constant, Register bumped_count,
274                              bool decrement = false);
275   void increment_mdp_data_at(Register reg, int constant,
276                              Register bumped_count, Register scratch2,
277                              bool decrement = false);
278   void increment_mask_and_jump(Address counter_addr,
279                                int increment, Address mask_addr,
280                                Register scratch1, Register scratch2,
281                                Condition cond, Label *where);
282   void set_mdp_flag_at(int flag_constant, Register scratch);
283   void test_mdp_data_at(int offset, Register value, Label& not_equal_continue,
284                         Register scratch);
285 
286   void record_klass_in_profile(Register receiver, Register scratch, bool is_virtual_call);
287   void record_klass_in_profile_helper(Register receiver, Register scratch,
288                                       Label& done, bool is_virtual_call);
289   void record_item_in_profile_helper(Register item,
290                                      Register scratch, int start_row, Label& done, int total_rows,
291                                      OffsetFunction item_offset_fn, OffsetFunction item_count_offset_fn,
292                                      int non_profiled_offset);
293 
294   void update_mdp_by_offset(int offset_of_disp, Register scratch);
295   void update_mdp_by_offset(Register reg, int offset_of_disp,
296                             Register scratch);
297   void update_mdp_by_constant(int constant);
298   void update_mdp_for_ret(TosState state, Register return_bci);
299 
300   void profile_taken_branch(Register scratch, Register bumped_count);
301   void profile_not_taken_branch(Register scratch);
302   void profile_call(Register scratch);
303   void profile_final_call(Register scratch);
304   void profile_virtual_call(Register receiver, Register scratch, bool receiver_can_be_null = false);
305   void profile_called_method(Register method, Register scratch) NOT_JVMCI_RETURN;
306   void profile_ret(TosState state, Register return_bci, Register scratch);
307   void profile_null_seen(Register scratch);
308   void profile_typecheck(Register klass, Register scratch);
309   void profile_typecheck_failed(Register scratch);
310   void profile_switch_default(Register scratch);
311   void profile_switch_case(Register index,
312                            Register scratch1,
313                            Register scratch2,
314                            Register scratch3);
315 
316   void profile_obj_type(Register obj, const Address& mdo_addr, Register tmp);
317   void profile_arguments_type(Register callee, Register tmp1, Register tmp2, bool is_virtual);
318   void profile_return_type(Register ret, Register tmp1, Register tmp2);
319   void profile_parameters_type(Register tmp1, Register tmp2, Register tmp3, Register tmp4);
320 
321   // Debugging
322   void interp_verify_oop(Register reg, TosState state, const char * file, int line);    // only if +VerifyOops && state == atos
323   void verify_oop_or_return_address(Register reg, Register rtmp); // for astore
324   void verify_FPU(int stack_depth, TosState state = ftos); // only if +VerifyFPU  && (state == ftos || state == dtos)
325 
326   // support for JVMTI/Dtrace
327   typedef enum { NotifyJVMTI, SkipNotifyJVMTI } NotifyMethodExitMode;
328   void notify_method_entry();
329   void notify_method_exit(
330     bool save_result, TosState state, NotifyMethodExitMode mode);
331 
332   void save_return_value(TosState state, bool is_native_call);
333   void restore_return_value(TosState state, bool is_native_call);
334 
335 };
336 
337 #endif // CPU_SPARC_VM_INTERP_MASM_SPARC_HPP
338