1 /*
2  * Copyright (c) 2008, 2021, 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 #include "precompiled.hpp"
26 #include "asm/macroAssembler.inline.hpp"
27 #include "interpreter/interp_masm.hpp"
28 #include "interpreter/interpreter.hpp"
29 #include "interpreter/interpreterRuntime.hpp"
30 #include "memory/allocation.inline.hpp"
31 #include "oops/method.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "runtime/handles.inline.hpp"
34 #include "runtime/icache.hpp"
35 #include "runtime/interfaceSupport.inline.hpp"
36 #include "runtime/signature.hpp"
37 
38 #define __ _masm->
39 
SignatureHandlerGenerator(const methodHandle & method,CodeBuffer * buffer)40 InterpreterRuntime::SignatureHandlerGenerator::SignatureHandlerGenerator(
41     const methodHandle& method, CodeBuffer* buffer) : NativeSignatureIterator(method) {
42   _masm = new MacroAssembler(buffer);
43   _abi_offset = 0;
44   _ireg = is_static() ? 2 : 1;
45 #ifdef __ABI_HARD__
46   _fp_slot = 0;
47   _single_fpr_slot = 0;
48 #endif
49 }
50 
51 #ifdef SHARING_FAST_NATIVE_FINGERPRINTS
52 // mapping from SignatureIterator param to (common) type of parsing
53 static const BasicType shared_type[] = {
54   T_INT,    // bool
55   T_INT,    // char
56 #ifndef __ABI_HARD__
57   T_INT,    // float, passed as int
58   T_LONG,   // double, passed as long
59 #else
60   T_FLOAT,  // float
61   T_DOUBLE, // double
62 #endif
63   T_INT,    // byte
64   T_INT,    // short
65   T_INT,    // int
66   T_LONG,   // long
67   T_OBJECT, // obj
68   T_OBJECT, // array
69 };
70 
normalize_fast_native_fingerprint(uint64_t fingerprint)71 uint64_t InterpreterRuntime::normalize_fast_native_fingerprint(uint64_t fingerprint) {
72   if (fingerprint == UCONST64(-1)) {
73     // special signature used when the argument list cannot be encoded in a 64 bits value
74     return fingerprint;
75   }
76   int shift = SignatureIterator::fp_static_feature_size;
77   SignatureIterator::fingerprint_t result = fingerprint & ((1 << shift) - 1);
78 
79   BasicType ret_type = SignatureIterator::fp_return_type(fingerprint);
80   // For ARM, the fast signature handler only needs to know whether
81   // the return value must be unboxed. T_OBJECT and T_ARRAY need not
82   // be distinguished from each other and all other return values
83   // behave like integers with respect to the handler except T_BOOLEAN
84   // which must be mapped to the range 0..1.
85   if (is_reference_type(ret_type)) {
86     ret_type = T_OBJECT;
87   } else if (ret_type != T_BOOLEAN) {
88     ret_type = T_INT;
89   }
90   result |= ((SignatureIterator::fingerprint_t) ret_type) << shift;
91   shift += SignatureIterator::fp_result_feature_size;
92 
93   SignatureIterator::fingerprint_t unaccumulator = SignatureIterator::fp_start_parameters(fingerprint);
94   while (true) {
95     BasicType type = SignatureIterator::fp_next_parameter(unaccumulator);
96     if (type == (BasicType)SignatureIterator::fp_parameters_done) {
97       return result;
98     }
99     assert(SignatureIterator::fp_is_valid_type(type), "garbled fingerprint");
100     BasicType shared = shared_type[type - T_BOOLEAN];
101     result |= ((SignatureIterator::fingerprint_t) shared) << shift;
102     shift += SignatureIterator::fp_parameter_feature_size;
103   }
104 }
105 #endif // SHARING_FAST_NATIVE_FINGERPRINTS
106 
107 // Implementation of SignatureHandlerGenerator
pass_int()108 void InterpreterRuntime::SignatureHandlerGenerator::pass_int() {
109   if (_ireg < GPR_PARAMS) {
110     Register dst = as_Register(_ireg);
111     __ ldr_s32(dst, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
112     _ireg++;
113   } else {
114     __ ldr_s32(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
115     __ str_32(Rtemp, Address(SP, _abi_offset * wordSize));
116     _abi_offset++;
117   }
118 }
119 
pass_long()120 void InterpreterRuntime::SignatureHandlerGenerator::pass_long() {
121   if (_ireg <= 2) {
122 #if (ALIGN_WIDE_ARGUMENTS == 1)
123     if ((_ireg & 1) != 0) {
124       // 64-bit values should be 8-byte aligned
125       _ireg++;
126     }
127 #endif
128     Register dst1 = as_Register(_ireg);
129     Register dst2 = as_Register(_ireg+1);
130     __ ldr(dst1, Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
131     __ ldr(dst2, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
132     _ireg += 2;
133 #if (ALIGN_WIDE_ARGUMENTS == 0)
134   } else if (_ireg == 3) {
135     // uses R3 + one stack slot
136     Register dst1 = as_Register(_ireg);
137     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
138     __ ldr(dst1, Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
139     __ str(Rtemp, Address(SP, _abi_offset * wordSize));
140     _ireg += 1;
141     _abi_offset += 1;
142 #endif
143   } else {
144 #if (ALIGN_WIDE_ARGUMENTS == 1)
145     if(_abi_offset & 1) _abi_offset++;
146 #endif
147     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
148     __ str(Rtemp, Address(SP, (_abi_offset) * wordSize));
149     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
150     __ str(Rtemp, Address(SP, (_abi_offset+1) * wordSize));
151     _abi_offset += 2;
152     _ireg = 4;
153   }
154 }
155 
pass_object()156 void InterpreterRuntime::SignatureHandlerGenerator::pass_object() {
157   if (_ireg < 4) {
158     Register dst = as_Register(_ireg);
159     __ ldr(dst, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
160     __ cmp(dst, 0);
161     __ sub(dst, Rlocals, -Interpreter::local_offset_in_bytes(offset()), ne);
162     _ireg++;
163   } else {
164     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
165     __ cmp(Rtemp, 0);
166     __ sub(Rtemp, Rlocals, -Interpreter::local_offset_in_bytes(offset()), ne);
167     __ str(Rtemp, Address(SP, _abi_offset * wordSize));
168     _abi_offset++;
169   }
170 }
171 
172 #ifndef __ABI_HARD__
pass_float()173 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
174   if (_ireg < 4) {
175     Register dst = as_Register(_ireg);
176     __ ldr(dst, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
177     _ireg++;
178   } else {
179     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
180     __ str(Rtemp, Address(SP, _abi_offset * wordSize));
181     _abi_offset++;
182   }
183 }
184 
185 #else
186 #ifndef __SOFTFP__
pass_float()187 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
188     if((_fp_slot < 16) || (_single_fpr_slot & 1)) {
189       if ((_single_fpr_slot & 1) == 0) {
190         _single_fpr_slot = _fp_slot;
191         _fp_slot += 2;
192       }
193       __ flds(as_FloatRegister(_single_fpr_slot), Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
194       _single_fpr_slot++;
195     } else {
196       __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
197       __ str(Rtemp, Address(SP, _abi_offset * wordSize));
198       _abi_offset++;
199     }
200 }
201 
pass_double()202 void InterpreterRuntime::SignatureHandlerGenerator::pass_double() {
203     if(_fp_slot <= 14) {
204       __ fldd(as_FloatRegister(_fp_slot), Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
205       _fp_slot += 2;
206     } else {
207       __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
208       __ str(Rtemp, Address(SP, (_abi_offset) * wordSize));
209       __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
210       __ str(Rtemp, Address(SP, (_abi_offset+1) * wordSize));
211       _abi_offset += 2;
212       _single_fpr_slot = 16;
213     }
214 }
215 #endif // __SOFTFP__
216 #endif // __ABI_HARD__
217 
generate(uint64_t fingerprint)218 void InterpreterRuntime::SignatureHandlerGenerator::generate(uint64_t fingerprint) {
219   iterate(fingerprint);
220 
221   BasicType result_type = SignatureIterator::fp_return_type(fingerprint);
222 
223   address result_handler = Interpreter::result_handler(result_type);
224 
225   __ mov_slow(R0, (intptr_t)result_handler);
226 
227   __ ret();
228 }
229 
230 
231 // Implementation of SignatureHandlerLibrary
232 
pd_set_handler(address handler)233 void SignatureHandlerLibrary::pd_set_handler(address handler) {}
234 
235 class SlowSignatureHandler: public NativeSignatureIterator {
236  private:
237   address   _from;
238   intptr_t* _to;
239 
240 #ifndef __ABI_HARD__
pass_int()241   virtual void pass_int() {
242     *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
243     _from -= Interpreter::stackElementSize;
244   }
245 
pass_float()246   virtual void pass_float() {
247     *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
248     _from -= Interpreter::stackElementSize;
249   }
250 
pass_long()251   virtual void pass_long() {
252 #if (ALIGN_WIDE_ARGUMENTS == 1)
253     if (((intptr_t)_to & 7) != 0) {
254       // 64-bit values should be 8-byte aligned
255       _to++;
256     }
257 #endif
258     _to[0] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
259     _to[1] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
260     _to += 2;
261     _from -= 2*Interpreter::stackElementSize;
262   }
263 
pass_object()264   virtual void pass_object() {
265     intptr_t from_addr = (intptr_t)(_from + Interpreter::local_offset_in_bytes(0));
266     *_to++ = (*(intptr_t*)from_addr == 0) ? (intptr_t)NULL : from_addr;
267     _from -= Interpreter::stackElementSize;
268    }
269 
270 #else
271 
272   intptr_t* _toFP;
273   intptr_t* _toGP;
274   int       _last_gp;
275   int       _last_fp;
276   int       _last_single_fp;
277 
pass_int()278   virtual void pass_int() {
279     if(_last_gp < GPR_PARAMS) {
280       _toGP[_last_gp++] = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
281     } else {
282       *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
283     }
284     _from -= Interpreter::stackElementSize;
285   }
286 
pass_long()287   virtual void pass_long() {
288     assert(ALIGN_WIDE_ARGUMENTS == 1, "ABI_HARD not supported with unaligned wide arguments");
289     if (_last_gp <= 2) {
290       if(_last_gp & 1) _last_gp++;
291       _toGP[_last_gp++] = *(jint *)(_from+Interpreter::local_offset_in_bytes(1));
292       _toGP[_last_gp++] = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
293     } else {
294       if (((intptr_t)_to & 7) != 0) {
295         // 64-bit values should be 8-byte aligned
296         _to++;
297       }
298       _to[0] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
299       _to[1] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
300       _to += 2;
301       _last_gp = 4;
302     }
303     _from -= 2*Interpreter::stackElementSize;
304   }
305 
pass_object()306   virtual void pass_object() {
307     intptr_t from_addr = (intptr_t)(_from + Interpreter::local_offset_in_bytes(0));
308     if(_last_gp < GPR_PARAMS) {
309       _toGP[_last_gp++] = (*(intptr_t*)from_addr == 0) ? NULL : from_addr;
310     } else {
311       *_to++ = (*(intptr_t*)from_addr == 0) ? NULL : from_addr;
312     }
313     _from -= Interpreter::stackElementSize;
314   }
315 
pass_float()316   virtual void pass_float() {
317     if((_last_fp < 16) || (_last_single_fp & 1)) {
318       if ((_last_single_fp & 1) == 0) {
319         _last_single_fp = _last_fp;
320         _last_fp += 2;
321       }
322 
323       _toFP[_last_single_fp++] = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
324     } else {
325       *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
326     }
327     _from -= Interpreter::stackElementSize;
328   }
329 
pass_double()330   virtual void pass_double() {
331     assert(ALIGN_WIDE_ARGUMENTS == 1, "ABI_HARD not supported with unaligned wide arguments");
332     if(_last_fp <= 14) {
333       _toFP[_last_fp++] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
334       _toFP[_last_fp++] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
335     } else {
336       if (((intptr_t)_to & 7) != 0) {      // 64-bit values should be 8-byte aligned
337         _to++;
338       }
339       _to[0] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
340       _to[1] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
341       _to += 2;
342       _last_single_fp = 16;
343     }
344     _from -= 2*Interpreter::stackElementSize;
345   }
346 
347 #endif // !__ABI_HARD__
348 
349  public:
SlowSignatureHandler(const methodHandle & method,address from,intptr_t * to)350   SlowSignatureHandler(const methodHandle& method, address from, intptr_t* to) :
351     NativeSignatureIterator(method) {
352     _from = from;
353 
354 #ifdef __ABI_HARD__
355     _toGP  = to;
356     _toFP = _toGP + GPR_PARAMS;
357     _to   = _toFP + (8*2);
358     _last_gp = (is_static() ? 2 : 1);
359     _last_fp = 0;
360     _last_single_fp = 0;
361 #else
362     _to   = to + (is_static() ? 2 : 1);
363 #endif // __ABI_HARD__
364   }
365 };
366 
367 JRT_ENTRY(address, InterpreterRuntime::slow_signature_handler(JavaThread* current, Method* method, intptr_t* from, intptr_t* to))
368   methodHandle m(current, (Method*)method);
369   assert(m->is_native(), "sanity check");
370   SlowSignatureHandler(m, (address)from, to).iterate(UCONST64(-1));
371   return Interpreter::result_handler(m->result_type());
372 JRT_END
373