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, 2022, 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 Register InterpreterRuntime::SignatureHandlerGenerator::next_gpr() {
 49   if (_num_reg_int_args < Argument::n_int_register_parameters_c - 1) {
 50     return g_INTArgReg[++_num_reg_int_args];
 51   }
 52   return noreg;
 53 }
 54 
 55 FloatRegister InterpreterRuntime::SignatureHandlerGenerator::next_fpr() {
 56   if (_num_reg_fp_args < Argument::n_float_register_parameters_c) {
 57     return g_FPArgReg[_num_reg_fp_args++];
 58   } else {
 59     return fnoreg;
 60   }
 61 }
 62 
 63 int InterpreterRuntime::SignatureHandlerGenerator::next_stack_offset() {
 64   int ret = _stack_offset;
 65   _stack_offset += wordSize;
 66   return ret;
 67 }
 68 
 69 InterpreterRuntime::SignatureHandlerGenerator::SignatureHandlerGenerator(
 70   const methodHandle& method, CodeBuffer* buffer) : NativeSignatureIterator(method) {
 71   _masm = new MacroAssembler(buffer); // allocate on resourse area by default
 72   _num_reg_int_args = (method->is_static() ? 1 : 0);
 73   _num_reg_fp_args = 0;
 74   _stack_offset = 0;
 75 }
 76 
 77 void InterpreterRuntime::SignatureHandlerGenerator::pass_int() {
 78   const Address src(from(), Interpreter::local_offset_in_bytes(offset()));
 79 
 80   Register reg = next_gpr();
 81   if (reg != noreg) {
 82     __ lw(reg, src);
 83   } else {
 84     __ lw(x10, src);
 85     __ sw(x10, Address(to(), next_stack_offset()));
 86   }
 87 }
 88 
 89 void InterpreterRuntime::SignatureHandlerGenerator::pass_long() {
 90   const Address src(from(), Interpreter::local_offset_in_bytes(offset() + 1));
 91 
 92   Register reg = next_gpr();
 93   if (reg != noreg) {
 94     __ ld(reg, src);
 95   } else  {
 96     __ ld(x10, src);
 97     __ sd(x10, Address(to(), next_stack_offset()));
 98   }
 99 }
100 
101 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
102   const Address src(from(), Interpreter::local_offset_in_bytes(offset()));
103 
104   FloatRegister reg = next_fpr();
105   if (reg != fnoreg) {
106     __ flw(reg, src);
107   } else {
108     // a floating-point argument is passed according to the integer calling
109     // convention if no floating-point argument register available
110     pass_int();
111   }
112 }
113 
114 void InterpreterRuntime::SignatureHandlerGenerator::pass_double() {
115   const Address src(from(), Interpreter::local_offset_in_bytes(offset() + 1));
116 
117   FloatRegister reg = next_fpr();
118   if (reg != fnoreg) {
119     __ fld(reg, src);
120   } else {
121     // a floating-point argument is passed according to the integer calling
122     // convention if no floating-point argument register available
123     pass_long();
124   }
125 }
126 
127 void InterpreterRuntime::SignatureHandlerGenerator::pass_object() {
128   Register reg = next_gpr();
129   if (reg == c_rarg1) {
130     assert(offset() == 0, "argument register 1 can only be (non-null) receiver");
131     __ addi(c_rarg1, from(), Interpreter::local_offset_in_bytes(offset()));
132   } else if (reg != noreg) {
133       // c_rarg2-c_rarg7
134       __ addi(x10, from(), Interpreter::local_offset_in_bytes(offset()));
135       __ mv(reg, zr); //_num_reg_int_args:c_rarg -> 1:c_rarg2,  2:c_rarg3...
136       __ ld(temp(), x10);
137       Label L;
138       __ beqz(temp(), L);
139       __ mv(reg, x10);
140       __ bind(L);
141   } else {
142     //to stack
143     __ addi(x10, from(), Interpreter::local_offset_in_bytes(offset()));
144     __ ld(temp(), x10);
145     Label L;
146     __ bnez(temp(), L);
147     __ mv(x10, zr);
148     __ bind(L);
149     assert(sizeof(jobject) == wordSize, "");
150     __ sd(x10, Address(to(), next_stack_offset()));
151   }
152 }
153 
154 void InterpreterRuntime::SignatureHandlerGenerator::generate(uint64_t fingerprint) {
155   // generate code to handle arguments
156   iterate(fingerprint);
157 
158   // return result handler
159   __ la(x10, ExternalAddress(Interpreter::result_handler(method()->result_type())));
160   __ ret();
161 
162   __ flush();
163 }
164 
165 
166 // Implementation of SignatureHandlerLibrary
167 
168 void SignatureHandlerLibrary::pd_set_handler(address handler) {}
169 
170 
171 class SlowSignatureHandler
172   : public NativeSignatureIterator {
173  private:
174   address   _from;
175   intptr_t* _to;
176   intptr_t* _int_args;
177   intptr_t* _fp_args;
178   intptr_t* _fp_identifiers;
179   unsigned int _num_reg_int_args;
180   unsigned int _num_reg_fp_args;
181 
182   intptr_t* single_slot_addr() {
183     intptr_t* from_addr = (intptr_t*)(_from + Interpreter::local_offset_in_bytes(0));
184     _from -= Interpreter::stackElementSize;
185     return from_addr;
186   }
187 
188   intptr_t* double_slot_addr() {
189     intptr_t* from_addr = (intptr_t*)(_from + Interpreter::local_offset_in_bytes(1));
190     _from -= 2 * Interpreter::stackElementSize;
191     return from_addr;
192   }
193 
194   int pass_gpr(intptr_t value) {
195     if (_num_reg_int_args < Argument::n_int_register_parameters_c - 1) {
196       *_int_args++ = value;
197       return _num_reg_int_args++;
198     }
199     return -1;
200   }
201 
202   int pass_fpr(intptr_t value) {
203     if (_num_reg_fp_args < Argument::n_float_register_parameters_c) {
204       *_fp_args++ = value;
205       return _num_reg_fp_args++;
206     }
207     return -1;
208   }
209 
210   void pass_stack(intptr_t value) {
211     *_to++ = value;
212   }
213 
214   virtual void pass_int() {
215     jint value = *(jint*)single_slot_addr();
216     if (pass_gpr(value) < 0) {
217       pass_stack(value);
218     }
219   }
220 
221   virtual void pass_long() {
222     intptr_t value = *double_slot_addr();
223     if (pass_gpr(value) < 0) {
224       pass_stack(value);
225     }
226   }
227 
228   virtual void pass_object() {
229     intptr_t* addr = single_slot_addr();
230     intptr_t value = *addr == 0 ? NULL : (intptr_t)addr;
231     if (pass_gpr(value) < 0) {
232       pass_stack(value);
233     }
234   }
235 
236   virtual void pass_float() {
237     jint value = *(jint*) single_slot_addr();
238     // a floating-point argument is passed according to the integer calling
239     // convention if no floating-point argument register available
240     if (pass_fpr(value) < 0 && pass_gpr(value) < 0) {
241       pass_stack(value);
242     }
243   }
244 
245   virtual void pass_double() {
246     intptr_t value = *double_slot_addr();
247     int arg = pass_fpr(value);
248     if (0 <= arg) {
249       *_fp_identifiers |= (1ull << arg); // mark as double
250     } else if (pass_gpr(value) < 0) { // no need to mark if passing by integer registers or stack
251       pass_stack(value);
252     }
253   }
254 
255  public:
256   SlowSignatureHandler(const methodHandle& method, address from, intptr_t* to)
257     : NativeSignatureIterator(method)
258   {
259     _from = from;
260     _to   = to;
261 
262     _int_args = to - (method->is_static() ? 16 : 17);
263     _fp_args  = to - 8;
264     _fp_identifiers = to - 9;
265     *(int*) _fp_identifiers = 0;
266     _num_reg_int_args = (method->is_static() ? 1 : 0);
267     _num_reg_fp_args = 0;
268   }
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