2
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
4
* Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
5
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
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 "oops/method.hpp"
34
#include "oops/oop.inline.hpp"
35
#include "runtime/handles.inline.hpp"
36
#include "runtime/icache.hpp"
37
#include "runtime/interfaceSupport.inline.hpp"
38
#include "runtime/signature.hpp"
42
// Implementation of SignatureHandlerGenerator
43
Register InterpreterRuntime::SignatureHandlerGenerator::from() { return rlocals; }
44
Register InterpreterRuntime::SignatureHandlerGenerator::to() { return sp; }
45
Register InterpreterRuntime::SignatureHandlerGenerator::temp() { return rscratch1; }
47
Register InterpreterRuntime::SignatureHandlerGenerator::next_gpr() {
48
if (_num_reg_int_args < Argument::n_int_register_parameters_c-1) {
49
return as_Register(_num_reg_int_args++ + c_rarg1->encoding());
54
FloatRegister InterpreterRuntime::SignatureHandlerGenerator::next_fpr() {
55
if (_num_reg_fp_args < Argument::n_float_register_parameters_c) {
56
return as_FloatRegister(_num_reg_fp_args++);
61
// On macos/aarch64 native stack is packed, int/float are using only 4 bytes
62
// on stack. Natural alignment for types are still in place,
63
// for example double/long should be 8 bytes aligned.
65
int InterpreterRuntime::SignatureHandlerGenerator::next_stack_offset(unsigned elem_size) {
66
MACOS_ONLY(_stack_offset = align_up(_stack_offset, elem_size));
67
int ret = _stack_offset;
68
_stack_offset += NOT_MACOS(wordSize) MACOS_ONLY(elem_size);
72
InterpreterRuntime::SignatureHandlerGenerator::SignatureHandlerGenerator(
73
const methodHandle& method, CodeBuffer* buffer) : NativeSignatureIterator(method) {
74
_masm = new MacroAssembler(buffer);
75
_num_reg_int_args = (method->is_static() ? 1 : 0);
80
void InterpreterRuntime::SignatureHandlerGenerator::pass_byte() {
81
const Address src(from(), Interpreter::local_offset_in_bytes(offset()));
83
Register reg = next_gpr();
88
__ strb(r0, Address(to(), next_stack_offset(sizeof(jbyte))));
92
void InterpreterRuntime::SignatureHandlerGenerator::pass_short() {
93
const Address src(from(), Interpreter::local_offset_in_bytes(offset()));
95
Register reg = next_gpr();
100
__ strh(r0, Address(to(), next_stack_offset(sizeof(jshort))));
104
void InterpreterRuntime::SignatureHandlerGenerator::pass_int() {
105
const Address src(from(), Interpreter::local_offset_in_bytes(offset()));
107
Register reg = next_gpr();
112
__ strw(r0, Address(to(), next_stack_offset(sizeof(jint))));
116
void InterpreterRuntime::SignatureHandlerGenerator::pass_long() {
117
const Address src(from(), Interpreter::local_offset_in_bytes(offset() + 1));
119
Register reg = next_gpr();
124
__ str(r0, Address(to(), next_stack_offset(sizeof(jlong))));
128
void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
129
const Address src(from(), Interpreter::local_offset_in_bytes(offset()));
131
FloatRegister reg = next_fpr();
136
__ strw(r0, Address(to(), next_stack_offset(sizeof(jfloat))));
140
void InterpreterRuntime::SignatureHandlerGenerator::pass_double() {
141
const Address src(from(), Interpreter::local_offset_in_bytes(offset() + 1));
143
FloatRegister reg = next_fpr();
148
__ str(r0, Address(to(), next_stack_offset(sizeof(jdouble))));
152
void InterpreterRuntime::SignatureHandlerGenerator::pass_object() {
153
Register reg = next_gpr();
154
if (reg == c_rarg1) {
155
assert(offset() == 0, "argument register 1 can only be (non-null) receiver");
156
__ add(c_rarg1, from(), Interpreter::local_offset_in_bytes(offset()));
157
} else if (reg != noreg) {
158
__ add(r0, from(), Interpreter::local_offset_in_bytes(offset()));
166
__ add(r0, from(), Interpreter::local_offset_in_bytes(offset()));
172
static_assert(sizeof(jobject) == wordSize, "");
173
__ str(r0, Address(to(), next_stack_offset(sizeof(jobject))));
177
void InterpreterRuntime::SignatureHandlerGenerator::generate(uint64_t fingerprint) {
178
// generate code to handle arguments
179
iterate(fingerprint);
181
// return result handler
182
__ lea(r0, ExternalAddress(Interpreter::result_handler(method()->result_type())));
189
// Implementation of SignatureHandlerLibrary
191
void SignatureHandlerLibrary::pd_set_handler(address handler) {}
194
class SlowSignatureHandler
195
: public NativeSignatureIterator {
201
intptr_t* _fp_identifiers;
202
unsigned int _num_reg_int_args;
203
unsigned int _num_reg_fp_args;
205
intptr_t* single_slot_addr() {
206
intptr_t* from_addr = (intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
207
_from -= Interpreter::stackElementSize;
211
intptr_t* double_slot_addr() {
212
intptr_t* from_addr = (intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
213
_from -= 2*Interpreter::stackElementSize;
217
int pass_gpr(intptr_t value) {
218
if (_num_reg_int_args < Argument::n_int_register_parameters_c-1) {
219
*_int_args++ = value;
220
return _num_reg_int_args++;
225
int pass_fpr(intptr_t value) {
226
if (_num_reg_fp_args < Argument::n_float_register_parameters_c) {
228
return _num_reg_fp_args++;
234
void pass_stack(T value) {
235
MACOS_ONLY(_to = align_up(_to, sizeof(value)));
237
_to += NOT_MACOS(wordSize) MACOS_ONLY(sizeof(value));
240
virtual void pass_byte() {
241
jbyte value = *(jbyte*)single_slot_addr();
242
if (pass_gpr(value) < 0) {
247
virtual void pass_short() {
248
jshort value = *(jshort*)single_slot_addr();
249
if (pass_gpr(value) < 0) {
254
virtual void pass_int() {
255
jint value = *(jint*)single_slot_addr();
256
if (pass_gpr(value) < 0) {
261
virtual void pass_long() {
262
intptr_t value = *double_slot_addr();
263
if (pass_gpr(value) < 0) {
268
virtual void pass_object() {
269
intptr_t* addr = single_slot_addr();
270
intptr_t value = *addr == 0 ? (intptr_t)0 : (intptr_t)addr;
271
if (pass_gpr(value) < 0) {
276
virtual void pass_float() {
277
jint value = *(jint*)single_slot_addr();
278
if (pass_fpr(value) < 0) {
283
virtual void pass_double() {
284
intptr_t value = *double_slot_addr();
285
int arg = pass_fpr(value);
287
*_fp_identifiers |= (1ull << arg); // mark as double
294
SlowSignatureHandler(const methodHandle& method, address from, intptr_t* to)
295
: NativeSignatureIterator(method)
300
_int_args = to - (method->is_static() ? 16 : 17);
302
_fp_identifiers = to - 9;
303
*(int*) _fp_identifiers = 0;
304
_num_reg_int_args = (method->is_static() ? 1 : 0);
305
_num_reg_fp_args = 0;
312
InterpreterRuntime::slow_signature_handler(JavaThread* current,
316
methodHandle m(current, (Method*)method);
317
assert(m->is_native(), "sanity check");
320
SlowSignatureHandler ssh(m, (address)from, to);
321
ssh.iterate((uint64_t)CONST64(-1));
323
// return result handler
324
return Interpreter::result_handler(m->result_type());