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

LibJS+LibJIT: Make Assembler::native_call accept preserved_registers

This makes JS::JIT::Compiler less architecture-specific
and unifies aligning the stack into a single operation,
where previously we were doing it separately for preserved registers
and for stack arguments.
This commit is contained in:
Nikodem Rabuliński 2023-10-29 13:39:19 +01:00 committed by Andreas Kling
parent ecbb1df01b
commit 9f5450527f
2 changed files with 15 additions and 18 deletions

View file

@ -732,11 +732,17 @@ struct Assembler {
} }
} }
// NOTE: It's up to the caller of this function to preserve registers as needed. void native_call(
void native_call(void* callee, Vector<Operand> const& stack_arguments = {}) void* callee,
Vector<Operand> const& preserved_registers = {},
Vector<Operand> const& stack_arguments = {})
{ {
for (auto const& reg : preserved_registers.in_reverse())
push(reg);
// Preserve 16-byte stack alignment for non-even amount of stack-passed arguments // Preserve 16-byte stack alignment for non-even amount of stack-passed arguments
if ((stack_arguments.size() % 2) == 1) auto needs_aligning = ((stack_arguments.size() + preserved_registers.size()) % 2) == 1;
if (needs_aligning)
push(Operand::Imm(0)); push(Operand::Imm(0));
for (auto const& stack_argument : stack_arguments.in_reverse()) for (auto const& stack_argument : stack_arguments.in_reverse())
push(stack_argument); push(stack_argument);
@ -748,8 +754,11 @@ struct Assembler {
emit8(0xff); emit8(0xff);
emit_modrm_slash(2, Operand::Register(Reg::RAX)); emit_modrm_slash(2, Operand::Register(Reg::RAX));
if (!stack_arguments.is_empty()) if (!stack_arguments.is_empty() || needs_aligning)
add(Operand::Register(Reg::RSP), Operand::Imm(align_up_to(stack_arguments.size(), 2) * sizeof(void*))); add(Operand::Register(Reg::RSP), Operand::Imm((stack_arguments.size() + (needs_aligning ? 1 : 0)) * sizeof(void*)));
for (auto const& reg : preserved_registers)
pop(reg);
} }
void trap() void trap()

View file

@ -1917,21 +1917,9 @@ void Compiler::jump_to_exit()
void Compiler::native_call(void* function_address, Vector<Assembler::Operand> const& stack_arguments) void Compiler::native_call(void* function_address, Vector<Assembler::Operand> const& stack_arguments)
{ {
// Make sure we don't clobber the VM&.
m_assembler.push(Assembler::Operand::Register(ARG0));
// Align the stack pointer.
m_assembler.sub(Assembler::Operand::Register(STACK_POINTER), Assembler::Operand::Imm(8));
// NOTE: We don't preserve caller-saved registers when making a native call. // NOTE: We don't preserve caller-saved registers when making a native call.
// This means that they may have changed after we return from the call. // This means that they may have changed after we return from the call.
m_assembler.native_call(function_address, stack_arguments); m_assembler.native_call(function_address, { Assembler::Operand::Register(ARG0) }, stack_arguments);
// Restore the stack pointer.
m_assembler.add(Assembler::Operand::Register(STACK_POINTER), Assembler::Operand::Imm(8));
// Restore our VM&.
m_assembler.pop(Assembler::Operand::Register(ARG0));
} }
OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_executable) OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_executable)