1 /*
  2  * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, 2020, 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 #include "precompiled.hpp"
 28 #include "asm/macroAssembler.inline.hpp"
 29 #include "interpreter/interp_masm.hpp"
 30 #include "interpreter/interpreter.hpp"
 31 #include "interpreter/interpreterRuntime.hpp"
 32 #include "memory/allocation.inline.hpp"
 33 #include "memory/universe.hpp"
 34 #include "oops/method.hpp"
 35 #include "oops/oop.inline.hpp"
 36 #include "runtime/handles.inline.hpp"
 37 #include "runtime/icache.hpp"
 38 #include "runtime/interfaceSupport.inline.hpp"
 39 #include "runtime/signature.hpp"
 40 
 41 #define __ _masm->
 42 
 43 // Implementation of SignatureHandlerGenerator
 44 Register InterpreterRuntime::SignatureHandlerGenerator::from() { return xlocals; }
 45 Register InterpreterRuntime::SignatureHandlerGenerator::to()   { return sp; }
 46 Register InterpreterRuntime::SignatureHandlerGenerator::temp() { return t0; }
 47 
 48 InterpreterRuntime::SignatureHandlerGenerator::SignatureHandlerGenerator(
 49   const methodHandle& method, CodeBuffer* buffer) : NativeSignatureIterator(method) {
 50   _masm = new MacroAssembler(buffer); // allocate on resourse area by default
 51   _num_int_args = (method->is_static() ? 1 : 0);
 52   _num_fp_args = 0;
 53   _stack_offset = 0;
 54 }
 55 
 56 void InterpreterRuntime::SignatureHandlerGenerator::pass_int() {
 57   const Address src(from(), Interpreter::local_offset_in_bytes(offset()));
 58 
 59   if (_num_int_args < Argument::n_int_register_parameters_c - 1) {
 60     __ lw(g_INTArgReg[++_num_int_args], src);
 61   } else {
 62     __ lw(x10, src);
 63     __ sw(x10, Address(to(), _stack_offset));
 64     _stack_offset += wordSize;
 65     _num_int_args++;
 66   }
 67 }
 68 
 69 void InterpreterRuntime::SignatureHandlerGenerator::pass_long() {
 70   const Address src(from(), Interpreter::local_offset_in_bytes(offset() + 1));
 71 
 72   if (_num_int_args < Argument::n_int_register_parameters_c - 1) {
 73     __ ld(g_INTArgReg[++_num_int_args], src);
 74   } else {
 75     __ ld(x10, src);
 76     __ sd(x10, Address(to(), _stack_offset));
 77     _stack_offset += wordSize;
 78     _num_int_args++;
 79   }
 80 }
 81 
 82 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
 83   const Address src(from(), Interpreter::local_offset_in_bytes(offset()));
 84 
 85   if (_num_fp_args < Argument::n_float_register_parameters_c) {
 86     // to c_farg
 87     __ flw(g_FPArgReg[_num_fp_args++], src);
 88   } else if (_num_int_args < Argument::n_int_register_parameters_c - 1) {
 89     // to c_rarg
 90     __ lwu(g_INTArgReg[++_num_int_args], src);
 91   } else {
 92     // to stack
 93     __ lwu(x10, src);
 94     __ sw(x10, Address(to(), _stack_offset));
 95     _stack_offset += wordSize;
 96     _num_fp_args++;
 97   }
 98 }
 99 
100 void InterpreterRuntime::SignatureHandlerGenerator::pass_double() {
101   const Address src(from(), Interpreter::local_offset_in_bytes(offset() + 1));
102 
103   if (_num_fp_args < Argument::n_float_register_parameters_c) {
104     // to c_farg
105     __ fld(g_FPArgReg[_num_fp_args++], src);
106   } else if (_num_int_args < Argument::n_int_register_parameters_c - 1) {
107     // to c_rarg
108     __ ld(g_INTArgReg[++_num_int_args], src);
109   } else {
110     // to stack
111     __ ld(x10, src);
112     __ sd(x10, Address(to(), _stack_offset));
113     _stack_offset += wordSize;
114     _num_fp_args++;
115   }
116 }
117 
118 void InterpreterRuntime::SignatureHandlerGenerator::pass_object() {
119 
120   if (_num_int_args < Argument::n_int_register_parameters_c - 1) {
121     // to reg
122     if (_num_int_args == 0) {
123       assert(offset() == 0, "argument register 1 can only be (non-null) receiver");
124       __ addi(c_rarg1, from(), Interpreter::local_offset_in_bytes(offset()));
125       _num_int_args++;
126     } else {
127       // c_rarg2-c_rarg7
128       __ addi(x10, from(), Interpreter::local_offset_in_bytes(offset()));
129       __ mv(g_INTArgReg[++_num_int_args], 0); //_num_int_args:c_rarg -> 1:c_rarg2,  2:c_rarg3...
130       __ ld(temp(), x10);
131       Label L;
132       __ beqz(temp(), L);
133       __ mv(g_INTArgReg[_num_int_args], x10);
134       __ bind(L);
135     }
136   } else {
137     //to stack
138     __ addi(x10, from(), Interpreter::local_offset_in_bytes(offset()));
139     __ ld(temp(), x10);
140     Label L;
141     __ bnez(temp(), L);
142     __ mv(x10, zr);
143     __ bind(L);
144     __ sd(x10, Address(to(), _stack_offset));
145     _stack_offset += wordSize;
146     _num_int_args++;
147   }
148 }
149 
150 void InterpreterRuntime::SignatureHandlerGenerator::generate(uint64_t fingerprint) {
151   // generate code to handle arguments
152   iterate(fingerprint);
153 
154   // return result handler
155   __ la(x10, ExternalAddress(Interpreter::result_handler(method()->result_type())));
156   __ ret();
157 
158   __ flush();
159 }
160 
161 
162 // Implementation of SignatureHandlerLibrary
163 
164 void SignatureHandlerLibrary::pd_set_handler(address handler) {}
165 
166 
167 class SlowSignatureHandler
168   : public NativeSignatureIterator {
169  private:
170   address   _from;
171   intptr_t* _to;
172   intptr_t* _int_args;
173   intptr_t* _fp_args;
174   intptr_t* _fp_identifiers;
175   unsigned int _num_int_args;
176   unsigned int _num_fp_args;
177 
178   virtual void pass_int()
179   {
180     jint from_obj = *(jint *)(_from + Interpreter::local_offset_in_bytes(0));
181     _from -= Interpreter::stackElementSize;
182 
183     if (_num_int_args < Argument::n_int_register_parameters_c - 1) {
184       *_int_args++ = from_obj;
185       _num_int_args++;
186     } else {
187       *_to++ = from_obj;
188       _num_int_args++;
189     }
190   }
191 
192   virtual void pass_long()
193   {
194     intptr_t from_obj = *(intptr_t*)(_from + Interpreter::local_offset_in_bytes(1));
195     _from -= 2*Interpreter::stackElementSize;
196 
197     if (_num_int_args < Argument::n_int_register_parameters_c - 1) {
198       *_int_args++ = from_obj;
199       _num_int_args++;
200     } else {
201       *_to++ = from_obj;
202       _num_int_args++;
203     }
204   }
205 
206   virtual void pass_object()
207   {
208     intptr_t *from_addr = (intptr_t*)(_from + Interpreter::local_offset_in_bytes(0));
209     _from -= Interpreter::stackElementSize;
210 
211     if (_num_int_args < Argument::n_int_register_parameters_c - 1) {
212       *_int_args++ = (*from_addr == 0) ? NULL : (intptr_t)from_addr;
213       _num_int_args++;
214     } else {
215       *_to++ = (*from_addr == 0) ? NULL : (intptr_t) from_addr;
216       _num_int_args++;
217     }
218   }
219 
220   virtual void pass_float()
221   {
222     jint from_obj = *(jint*)(_from + Interpreter::local_offset_in_bytes(0));
223     _from -= Interpreter::stackElementSize;
224 
225     if (_num_fp_args < Argument::n_float_register_parameters_c) {
226       *_fp_args++ = from_obj;
227       _num_fp_args++;
228     } else if (_num_int_args < Argument::n_int_register_parameters_c - 1) {
229       *_int_args++ = from_obj;
230       _num_int_args++;
231     } else {
232       *_to++ = from_obj;
233       _num_fp_args++;
234     }
235   }
236 
237   virtual void pass_double()
238   {
239     intptr_t from_obj = *(intptr_t*)(_from + Interpreter::local_offset_in_bytes(1));
240     _from -= 2*Interpreter::stackElementSize;
241 
242     if (_num_fp_args < Argument::n_float_register_parameters_c) {
243       *_fp_args++ = from_obj;
244       *_fp_identifiers |= (1ull << _num_fp_args); // mark as double
245       _num_fp_args++;
246     } else if (_num_int_args < Argument::n_int_register_parameters_c - 1) {
247       // ld/st from_obj as integer, no need to mark _fp_identifiers
248       *_int_args++ = from_obj;
249       _num_int_args++;
250     } else {
251       *_to++ = from_obj;
252       _num_fp_args++;
253     }
254   }
255 
256  public:
257   SlowSignatureHandler(const methodHandle& method, address from, intptr_t* to)
258     : NativeSignatureIterator(method)
259   {
260     _from = from;
261     _to   = to;
262 
263     _int_args = to - (method->is_static() ? 16 : 17);
264     _fp_args =  to - 8;
265     _fp_identifiers = to - 9;
266     *(int*) _fp_identifiers = 0;
267     _num_int_args = (method->is_static() ? 1 : 0);
268     _num_fp_args = 0;
269   }
270   ~SlowSignatureHandler()
271   {
272     _from           = NULL;
273     _to             = NULL;
274     _int_args       = NULL;
275     _fp_args        = NULL;
276     _fp_identifiers = NULL;
277   }
278 };
279 
280 
281 JRT_ENTRY(address,
282           InterpreterRuntime::slow_signature_handler(JavaThread* current,
283                                                      Method* method,
284                                                      intptr_t* from,
285                                                      intptr_t* to))
286   methodHandle m(current, (Method*)method);
287   assert(m->is_native(), "sanity check");
288 
289   // handle arguments
290   SlowSignatureHandler ssh(m, (address)from, to);
291   ssh.iterate(UCONST64(-1));
292 
293   // return result handler
294   return Interpreter::result_handler(m->result_type());
295 JRT_END