Skip to content

Commit

Permalink
JDK-8253817: Support macOS Aarch64 ABI in Interpreter
Browse files Browse the repository at this point in the history
ZULU-17389, ZULU-18130, ZULU-18625, ZULU-18639
  • Loading branch information
AntonKozlov committed Oct 1, 2020
1 parent 7092a06 commit 368f06e
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 13 deletions.
174 changes: 163 additions & 11 deletions src/hotspot/cpu/aarch64/interpreterRT_aarch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@

#define __ _masm->

//describe amount of space in bytes occupied by type on native stack
#ifdef __APPLE__
const int nativeByteSpace = sizeof(jbyte);
const int nativeShortSpace = sizeof(jshort);
const int nativeIntSpace = sizeof(jint);
const int nativeLongSpace = wordSize;
const int nativeFloatSpace = nativeIntSpace;
const int nativeDoubleSpace = nativeLongSpace;
#else
const int nativeByteSpace = wordSize;
const int nativeShortSpace = wordSize;
const int nativeIntSpace = wordSize;
const int nativeLongSpace = wordSize;
const int nativeFloatSpace = nativeIntSpace;
const int nativeDoubleSpace = nativeLongSpace;
#endif

template <typename T>
static inline void store_and_inc(char* &to, T value, int inc_size) {
to = align_up(to, inc_size);
*(T *)to = value;
to = to + inc_size;
}

// Implementation of SignatureHandlerGenerator
Register InterpreterRuntime::SignatureHandlerGenerator::from() { return rlocals; }
Register InterpreterRuntime::SignatureHandlerGenerator::to() { return sp; }
Expand All @@ -51,6 +75,95 @@ InterpreterRuntime::SignatureHandlerGenerator::SignatureHandlerGenerator(
_stack_offset = 0;
}

// On macos/aarch64 native stack is packed, int/float are using only 4 bytes
// on stack. Natural alignment for types are still in place,
// for example double/long should be 8 bytes alligned

void InterpreterRuntime::SignatureHandlerGenerator::pass_byte() {
const Address src(from(), Interpreter::local_offset_in_bytes(offset()));

switch (_num_int_args) {
case 0:
__ ldr(c_rarg1, src);
_num_int_args++;
break;
case 1:
__ ldr(c_rarg2, src);
_num_int_args++;
break;
case 2:
__ ldr(c_rarg3, src);
_num_int_args++;
break;
case 3:
__ ldr(c_rarg4, src);
_num_int_args++;
break;
case 4:
__ ldr(c_rarg5, src);
_num_int_args++;
break;
case 5:
__ ldr(c_rarg6, src);
_num_int_args++;
break;
case 6:
__ ldr(c_rarg7, src);
_num_int_args++;
break;
default:
__ ldrb(r0, src);
__ strb(r0, Address(to(), _stack_offset));
_stack_offset += nativeByteSpace;

_num_int_args++;
break;
}
}

void InterpreterRuntime::SignatureHandlerGenerator::pass_short() {
const Address src(from(), Interpreter::local_offset_in_bytes(offset()));

switch (_num_int_args) {
case 0:
__ ldr(c_rarg1, src);
_num_int_args++;
break;
case 1:
__ ldr(c_rarg2, src);
_num_int_args++;
break;
case 2:
__ ldr(c_rarg3, src);
_num_int_args++;
break;
case 3:
__ ldr(c_rarg4, src);
_num_int_args++;
break;
case 4:
__ ldr(c_rarg5, src);
_num_int_args++;
break;
case 5:
__ ldr(c_rarg6, src);
_num_int_args++;
break;
case 6:
__ ldr(c_rarg7, src);
_num_int_args++;
break;
default:
_stack_offset = align_up(_stack_offset, nativeShortSpace);
__ ldrh(r0, src);
__ strh(r0, Address(to(), _stack_offset));
_stack_offset += nativeShortSpace;

_num_int_args++;
break;
}
}

void InterpreterRuntime::SignatureHandlerGenerator::pass_int() {
const Address src(from(), Interpreter::local_offset_in_bytes(offset()));

Expand Down Expand Up @@ -84,9 +197,10 @@ void InterpreterRuntime::SignatureHandlerGenerator::pass_int() {
_num_int_args++;
break;
default:
_stack_offset = align_up(_stack_offset, nativeIntSpace);
__ ldr(r0, src);
__ str(r0, Address(to(), _stack_offset));
_stack_offset += wordSize;
_stack_offset += nativeIntSpace;
_num_int_args++;
break;
}
Expand Down Expand Up @@ -125,9 +239,10 @@ void InterpreterRuntime::SignatureHandlerGenerator::pass_long() {
_num_int_args++;
break;
default:
_stack_offset = align_up(_stack_offset, nativeLongSpace);
__ ldr(r0, src);
__ str(r0, Address(to(), _stack_offset));
_stack_offset += wordSize;
_stack_offset += nativeLongSpace;
_num_int_args++;
break;
}
Expand All @@ -139,9 +254,10 @@ void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
if (_num_fp_args < Argument::n_float_register_parameters_c) {
__ ldrs(as_FloatRegister(_num_fp_args++), src);
} else {
_stack_offset = align_up(_stack_offset, nativeFloatSpace);
__ ldrw(r0, src);
__ strw(r0, Address(to(), _stack_offset));
_stack_offset += wordSize;
_stack_offset += nativeFloatSpace;
_num_fp_args++;
}
}
Expand All @@ -152,9 +268,10 @@ void InterpreterRuntime::SignatureHandlerGenerator::pass_double() {
if (_num_fp_args < Argument::n_float_register_parameters_c) {
__ ldrd(as_FloatRegister(_num_fp_args++), src);
} else {
_stack_offset = align_up(_stack_offset, nativeDoubleSpace);
__ ldr(r0, src);
__ str(r0, Address(to(), _stack_offset));
_stack_offset += wordSize;
_stack_offset += nativeDoubleSpace;
_num_fp_args++;
}
}
Expand Down Expand Up @@ -247,6 +364,7 @@ void InterpreterRuntime::SignatureHandlerGenerator::pass_object() {
__ cbnz(temp(), L);
__ mov(r0, zr);
__ bind(L);
_stack_offset = align_up(_stack_offset, wordSize);
__ str(r0, Address(to(), _stack_offset));
_stack_offset += wordSize;
_num_int_args++;
Expand Down Expand Up @@ -276,13 +394,45 @@ class SlowSignatureHandler
: public NativeSignatureIterator {
private:
address _from;
intptr_t* _to;
char* _to;
intptr_t* _int_args;
intptr_t* _fp_args;
intptr_t* _fp_identifiers;
unsigned int _num_int_args;
unsigned int _num_fp_args;


virtual void pass_byte()
{
NOT_MACOS(return pass_int();)
jbyte from_obj = *(jbyte *)(_from+Interpreter::local_offset_in_bytes(0));
_from -= Interpreter::stackElementSize;

if (_num_int_args < Argument::n_int_register_parameters_c-1) {
*_int_args++ = from_obj;
_num_int_args++;
} else {
store_and_inc(_to, from_obj, nativeByteSpace);

_num_int_args++;
}
}

virtual void pass_short()
{
NOT_MACOS(return pass_int();)
jshort from_obj = *(jshort *)(_from+Interpreter::local_offset_in_bytes(0));
_from -= Interpreter::stackElementSize;

if (_num_int_args < Argument::n_int_register_parameters_c-1) {
*_int_args++ = from_obj;
_num_int_args++;
} else {
store_and_inc(_to, from_obj, nativeShortSpace);

_num_int_args++;
}
}
virtual void pass_int()
{
jint from_obj = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
Expand All @@ -292,7 +442,8 @@ class SlowSignatureHandler
*_int_args++ = from_obj;
_num_int_args++;
} else {
*_to++ = from_obj;
store_and_inc(_to, from_obj, nativeIntSpace);

_num_int_args++;
}
}
Expand All @@ -306,7 +457,7 @@ class SlowSignatureHandler
*_int_args++ = from_obj;
_num_int_args++;
} else {
*_to++ = from_obj;
store_and_inc(_to, from_obj, nativeLongSpace);
_num_int_args++;
}
}
Expand All @@ -320,7 +471,7 @@ class SlowSignatureHandler
*_int_args++ = (*from_addr == 0) ? NULL : (intptr_t)from_addr;
_num_int_args++;
} else {
*_to++ = (*from_addr == 0) ? NULL : (intptr_t) from_addr;
store_and_inc(_to, (*from_addr == 0) ? (intptr_t)NULL : (intptr_t) from_addr, wordSize);
_num_int_args++;
}
}
Expand All @@ -334,7 +485,8 @@ class SlowSignatureHandler
*_fp_args++ = from_obj;
_num_fp_args++;
} else {
*_to++ = from_obj;
store_and_inc(_to, from_obj, nativeFloatSpace);

_num_fp_args++;
}
}
Expand All @@ -349,7 +501,7 @@ class SlowSignatureHandler
*_fp_identifiers |= (1ull << _num_fp_args); // mark as double
_num_fp_args++;
} else {
*_to++ = from_obj;
store_and_inc(_to, from_obj, nativeDoubleSpace);
_num_fp_args++;
}
}
Expand All @@ -359,7 +511,7 @@ class SlowSignatureHandler
: NativeSignatureIterator(method)
{
_from = from;
_to = to;
_to = (char *)to;

_int_args = to - (method->is_static() ? 16 : 17);
_fp_args = to - 8;
Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/cpu/aarch64/interpreterRT_aarch64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class SignatureHandlerGenerator: public NativeSignatureIterator {
unsigned int _num_int_args;
int _stack_offset;

void pass_byte();
void pass_short();
void pass_int();
void pass_long();
void pass_float();
Expand Down
14 changes: 14 additions & 0 deletions src/hotspot/cpu/aarch64/sharedRuntime_aarch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,11 @@ int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
if (int_args < Argument::n_int_register_parameters_c) {
regs[i].set1(INT_ArgReg[int_args++]->as_VMReg());
} else {
#ifdef __APPLE__
// Less-than word types are stored one after another.
// The code unable to handle this, bailout.
return -1;
#endif
regs[i].set1(VMRegImpl::stack2reg(stk_args));
stk_args += 2;
}
Expand All @@ -823,6 +828,11 @@ int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
if (fp_args < Argument::n_float_register_parameters_c) {
regs[i].set1(FP_ArgReg[fp_args++]->as_VMReg());
} else {
#ifdef __APPLE__
// Less-than word types are stored one after another.
// The code unable to handle this, bailout.
return -1;
#endif
regs[i].set1(VMRegImpl::stack2reg(stk_args));
stk_args += 2;
}
Expand Down Expand Up @@ -1384,6 +1394,10 @@ nmethod* SharedRuntime::generate_native_wrapper(MacroAssembler* masm,
int out_arg_slots;
out_arg_slots = c_calling_convention(out_sig_bt, out_regs, NULL, total_c_args);

if (out_arg_slots < 0) {
return NULL;
}

// Compute framesize for the wrapper. We need to handlize all oops in
// incoming registers

Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/interpreter/oopMapCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ class MaskFillerForNative: public NativeSignatureIterator {
}

public:
void pass_byte() { /* ignore */ }
void pass_short() { /* ignore */ }
void pass_int() { /* ignore */ }
void pass_long() { /* ignore */ }
void pass_float() { /* ignore */ }
Expand Down
10 changes: 8 additions & 2 deletions src/hotspot/share/runtime/signature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,14 @@ class NativeSignatureIterator: public SignatureIterator {
void do_type(BasicType type) {
switch (type) {
case T_BYTE:
case T_SHORT:
case T_INT:
case T_BOOLEAN:
pass_byte(); _jni_offset++; _offset++;
break;
case T_CHAR:
case T_SHORT:
pass_short(); _jni_offset++; _offset++;
break;
case T_INT:
pass_int(); _jni_offset++; _offset++;
break;
case T_FLOAT:
Expand Down Expand Up @@ -418,6 +422,8 @@ class NativeSignatureIterator: public SignatureIterator {
virtual void pass_long() = 0;
virtual void pass_object() = 0; // objects, arrays, inlines
virtual void pass_float() = 0;
virtual void pass_byte() { pass_int(); };
virtual void pass_short() { pass_int(); };
#ifdef _LP64
virtual void pass_double() = 0;
#else
Expand Down

0 comments on commit 368f06e

Please sign in to comment.