1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 16:27:35 +00:00

LibJIT: Support passing stack arguments to native_call()s

The x86-64 SystemV ABI specifies that additional arguments after the
first 6 register-passed ones should be passed on the stack.
This commit is contained in:
Idan Horowitz 2023-10-28 15:17:47 +03:00 committed by Andreas Kling
parent e63423554f
commit 863314ff10

View file

@ -564,7 +564,7 @@ struct Assembler {
} }
} }
void native_call(void* callee) void native_call(void* callee, Vector<Operand> const& stack_arguments = {})
{ {
// push caller-saved registers on the stack // push caller-saved registers on the stack
// (callee-saved registers: RBX, RSP, RBP, and R12R15) // (callee-saved registers: RBX, RSP, RBP, and R12R15)
@ -577,6 +577,12 @@ struct Assembler {
push(Operand::Register(Reg::R10)); push(Operand::Register(Reg::R10));
push(Operand::Register(Reg::R11)); push(Operand::Register(Reg::R11));
// Preserve 16-byte stack alignment for non-even amount of stack-passed arguments
if ((stack_arguments.size() % 2) == 1)
push(Operand::Imm(0));
for (auto const& stack_argument : stack_arguments.in_reverse())
push(stack_argument);
// load callee into RAX // load callee into RAX
mov(Operand::Register(Reg::RAX), Operand::Imm(bit_cast<u64>(callee))); mov(Operand::Register(Reg::RAX), Operand::Imm(bit_cast<u64>(callee)));
@ -584,6 +590,9 @@ struct Assembler {
emit8(0xff); emit8(0xff);
emit8(0xd0); emit8(0xd0);
if (!stack_arguments.is_empty())
add(Operand::Register(Reg::RSP), Operand::Imm(align_up_to(stack_arguments.size(), 2) * sizeof(void*)));
// restore caller-saved registers from the stack // restore caller-saved registers from the stack
pop(Operand::Register(Reg::R11)); pop(Operand::Register(Reg::R11));
pop(Operand::Register(Reg::R10)); pop(Operand::Register(Reg::R10));