mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:47:37 +00:00
LibJS/JIT: Add fast path for LessThan Int32 < Int32
This uses a new branch_if_both_int32() helper. It's interesting to note that we can compare encoded Int32 values without stripping the INT32_TAG, since it doesn't affect signedness of values.
This commit is contained in:
parent
895c613400
commit
1fb95c7df9
3 changed files with 120 additions and 3 deletions
|
@ -298,6 +298,17 @@ struct Assembler {
|
||||||
label.offset_in_instruction_stream = m_output.size();
|
label.offset_in_instruction_stream = m_output.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void jump_if_less_than(Operand lhs, Operand rhs, Label& label)
|
||||||
|
{
|
||||||
|
cmp(lhs, rhs);
|
||||||
|
|
||||||
|
// jl label (RIP-relative 32-bit offset)
|
||||||
|
emit8(0x0f);
|
||||||
|
emit8(0x8c);
|
||||||
|
emit32(0xdeadbeef);
|
||||||
|
label.offset_in_instruction_stream = m_output.size();
|
||||||
|
}
|
||||||
|
|
||||||
void bitwise_and(Operand dst, Operand src)
|
void bitwise_and(Operand dst, Operand src)
|
||||||
{
|
{
|
||||||
// and dst,src
|
// and dst,src
|
||||||
|
|
|
@ -187,6 +187,35 @@ void Compiler::branch_if_int32(Assembler::Reg reg, Codegen codegen)
|
||||||
not_int32_case.link(m_assembler);
|
not_int32_case.link(m_assembler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Codegen>
|
||||||
|
void Compiler::branch_if_both_int32(Assembler::Reg lhs, Assembler::Reg rhs, Codegen codegen)
|
||||||
|
{
|
||||||
|
// GPR0 = lhs >> 48;
|
||||||
|
m_assembler.mov(Assembler::Operand::Register(GPR0), Assembler::Operand::Register(lhs));
|
||||||
|
m_assembler.shift_right(Assembler::Operand::Register(GPR0), Assembler::Operand::Imm8(48));
|
||||||
|
|
||||||
|
// GPR1 = rhs >> 48;
|
||||||
|
m_assembler.mov(Assembler::Operand::Register(GPR1), Assembler::Operand::Register(rhs));
|
||||||
|
m_assembler.shift_right(Assembler::Operand::Register(GPR1), Assembler::Operand::Imm8(48));
|
||||||
|
|
||||||
|
// FIXME: Use one label once Assembler::Label supports multiple jumps to it.
|
||||||
|
auto not_int32_case1 = m_assembler.make_label();
|
||||||
|
auto not_int32_case2 = m_assembler.make_label();
|
||||||
|
m_assembler.jump_if_not_equal(
|
||||||
|
Assembler::Operand::Register(GPR0),
|
||||||
|
Assembler::Operand::Imm32(INT32_TAG),
|
||||||
|
not_int32_case1);
|
||||||
|
m_assembler.jump_if_not_equal(
|
||||||
|
Assembler::Operand::Register(GPR1),
|
||||||
|
Assembler::Operand::Imm32(INT32_TAG),
|
||||||
|
not_int32_case2);
|
||||||
|
|
||||||
|
codegen();
|
||||||
|
|
||||||
|
not_int32_case1.link(m_assembler);
|
||||||
|
not_int32_case2.link(m_assembler);
|
||||||
|
}
|
||||||
|
|
||||||
void Compiler::compile_increment(Bytecode::Op::Increment const&)
|
void Compiler::compile_increment(Bytecode::Op::Increment const&)
|
||||||
{
|
{
|
||||||
load_vm_register(ARG1, Bytecode::Register::accumulator());
|
load_vm_register(ARG1, Bytecode::Register::accumulator());
|
||||||
|
@ -422,9 +451,55 @@ static ThrowCompletionOr<Value> typed_equals(VM&, Value src1, Value src2)
|
||||||
check_exception(); \
|
check_exception(); \
|
||||||
}
|
}
|
||||||
|
|
||||||
JS_ENUMERATE_COMMON_BINARY_OPS(DO_COMPILE_COMMON_BINARY_OP)
|
JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(DO_COMPILE_COMMON_BINARY_OP)
|
||||||
#undef DO_COMPILE_COMMON_BINARY_OP
|
#undef DO_COMPILE_COMMON_BINARY_OP
|
||||||
|
|
||||||
|
static Value cxx_less_than(VM& vm, Value lhs, Value rhs)
|
||||||
|
{
|
||||||
|
return TRY_OR_SET_EXCEPTION(less_than(vm, lhs, rhs));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Compiler::compile_less_than(Bytecode::Op::LessThan const& op)
|
||||||
|
{
|
||||||
|
load_vm_register(ARG1, op.lhs());
|
||||||
|
load_vm_register(ARG2, Bytecode::Register::accumulator());
|
||||||
|
|
||||||
|
// FIXME: Unify when we have multi-jump labels.
|
||||||
|
auto end1 = m_assembler.make_label();
|
||||||
|
auto end2 = m_assembler.make_label();
|
||||||
|
|
||||||
|
branch_if_both_int32(ARG1, ARG2, [&] {
|
||||||
|
// if (ARG1 < ARG2) return true;
|
||||||
|
// else return false;
|
||||||
|
|
||||||
|
auto true_case = m_assembler.make_label();
|
||||||
|
m_assembler.jump_if_less_than(
|
||||||
|
Assembler::Operand::Register(ARG1),
|
||||||
|
Assembler::Operand::Register(ARG2),
|
||||||
|
true_case);
|
||||||
|
|
||||||
|
m_assembler.mov(
|
||||||
|
Assembler::Operand::Register(GPR0),
|
||||||
|
Assembler::Operand::Imm64(Value(false).encoded()));
|
||||||
|
store_vm_register(Bytecode::Register::accumulator(), GPR0);
|
||||||
|
m_assembler.jump(end1);
|
||||||
|
|
||||||
|
true_case.link(m_assembler);
|
||||||
|
m_assembler.mov(
|
||||||
|
Assembler::Operand::Register(GPR0),
|
||||||
|
Assembler::Operand::Imm64(Value(true).encoded()));
|
||||||
|
store_vm_register(Bytecode::Register::accumulator(), GPR0);
|
||||||
|
|
||||||
|
m_assembler.jump(end2);
|
||||||
|
});
|
||||||
|
|
||||||
|
m_assembler.native_call((void*)cxx_less_than);
|
||||||
|
store_vm_register(Bytecode::Register::accumulator(), RET);
|
||||||
|
check_exception();
|
||||||
|
end1.link(m_assembler);
|
||||||
|
end2.link(m_assembler);
|
||||||
|
}
|
||||||
|
|
||||||
static ThrowCompletionOr<Value> not_(VM&, Value value)
|
static ThrowCompletionOr<Value> not_(VM&, Value value)
|
||||||
{
|
{
|
||||||
return Value(!value.to_boolean());
|
return Value(!value.to_boolean());
|
||||||
|
@ -850,12 +925,15 @@ OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_execut
|
||||||
case Bytecode::Instruction::Type::SetVariable:
|
case Bytecode::Instruction::Type::SetVariable:
|
||||||
compiler.compile_set_variable(static_cast<Bytecode::Op::SetVariable const&>(op));
|
compiler.compile_set_variable(static_cast<Bytecode::Op::SetVariable const&>(op));
|
||||||
break;
|
break;
|
||||||
|
case Bytecode::Instruction::Type::LessThan:
|
||||||
|
compiler.compile_less_than(static_cast<Bytecode::Op::LessThan const&>(op));
|
||||||
|
break;
|
||||||
|
|
||||||
#define DO_COMPILE_COMMON_BINARY_OP(TitleCaseName, snake_case_name) \
|
#define DO_COMPILE_COMMON_BINARY_OP(TitleCaseName, snake_case_name) \
|
||||||
case Bytecode::Instruction::Type::TitleCaseName: \
|
case Bytecode::Instruction::Type::TitleCaseName: \
|
||||||
compiler.compile_##snake_case_name(static_cast<Bytecode::Op::TitleCaseName const&>(op)); \
|
compiler.compile_##snake_case_name(static_cast<Bytecode::Op::TitleCaseName const&>(op)); \
|
||||||
break;
|
break;
|
||||||
JS_ENUMERATE_COMMON_BINARY_OPS(DO_COMPILE_COMMON_BINARY_OP)
|
JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(DO_COMPILE_COMMON_BINARY_OP)
|
||||||
#undef DO_COMPILE_COMMON_BINARY_OP
|
#undef DO_COMPILE_COMMON_BINARY_OP
|
||||||
|
|
||||||
#define DO_COMPILE_COMMON_UNARY_OP(TitleCaseName, snake_case_name) \
|
#define DO_COMPILE_COMMON_UNARY_OP(TitleCaseName, snake_case_name) \
|
||||||
|
|
|
@ -47,10 +47,33 @@ private:
|
||||||
void compile_to_numeric(Bytecode::Op::ToNumeric const&);
|
void compile_to_numeric(Bytecode::Op::ToNumeric const&);
|
||||||
void compile_resolve_this_binding(Bytecode::Op::ResolveThisBinding const&);
|
void compile_resolve_this_binding(Bytecode::Op::ResolveThisBinding const&);
|
||||||
|
|
||||||
|
#define JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(O) \
|
||||||
|
O(Add, add) \
|
||||||
|
O(Sub, sub) \
|
||||||
|
O(Mul, mul) \
|
||||||
|
O(Div, div) \
|
||||||
|
O(Exp, exp) \
|
||||||
|
O(Mod, mod) \
|
||||||
|
O(In, in) \
|
||||||
|
O(InstanceOf, instance_of) \
|
||||||
|
O(GreaterThan, greater_than) \
|
||||||
|
O(GreaterThanEquals, greater_than_equals) \
|
||||||
|
O(LessThanEquals, less_than_equals) \
|
||||||
|
O(LooselyInequals, abstract_inequals) \
|
||||||
|
O(LooselyEquals, abstract_equals) \
|
||||||
|
O(StrictlyInequals, typed_inequals) \
|
||||||
|
O(StrictlyEquals, typed_equals) \
|
||||||
|
O(BitwiseAnd, bitwise_and) \
|
||||||
|
O(BitwiseOr, bitwise_or) \
|
||||||
|
O(BitwiseXor, bitwise_xor) \
|
||||||
|
O(LeftShift, left_shift) \
|
||||||
|
O(RightShift, right_shift) \
|
||||||
|
O(UnsignedRightShift, unsigned_right_shift)
|
||||||
|
|
||||||
#define DO_COMPILE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
|
#define DO_COMPILE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
|
||||||
void compile_##op_snake_case(Bytecode::Op::OpTitleCase const&);
|
void compile_##op_snake_case(Bytecode::Op::OpTitleCase const&);
|
||||||
|
|
||||||
JS_ENUMERATE_COMMON_BINARY_OPS(DO_COMPILE_COMMON_BINARY_OP)
|
JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(DO_COMPILE_COMMON_BINARY_OP)
|
||||||
#undef DO_COMPILE_COMMON_BINARY_OP
|
#undef DO_COMPILE_COMMON_BINARY_OP
|
||||||
|
|
||||||
#define DO_COMPILE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
|
#define DO_COMPILE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
|
||||||
|
@ -59,6 +82,8 @@ private:
|
||||||
JS_ENUMERATE_COMMON_UNARY_OPS(DO_COMPILE_COMMON_UNARY_OP)
|
JS_ENUMERATE_COMMON_UNARY_OPS(DO_COMPILE_COMMON_UNARY_OP)
|
||||||
#undef DO_COMPILE_COMMON_UNARY_OP
|
#undef DO_COMPILE_COMMON_UNARY_OP
|
||||||
|
|
||||||
|
void compile_less_than(Bytecode::Op::LessThan const&);
|
||||||
|
|
||||||
void compile_return(Bytecode::Op::Return const&);
|
void compile_return(Bytecode::Op::Return const&);
|
||||||
void compile_new_string(Bytecode::Op::NewString const&);
|
void compile_new_string(Bytecode::Op::NewString const&);
|
||||||
void compile_new_object(Bytecode::Op::NewObject const&);
|
void compile_new_object(Bytecode::Op::NewObject const&);
|
||||||
|
@ -92,6 +117,9 @@ private:
|
||||||
template<typename Codegen>
|
template<typename Codegen>
|
||||||
void branch_if_int32(Assembler::Reg, Codegen);
|
void branch_if_int32(Assembler::Reg, Codegen);
|
||||||
|
|
||||||
|
template<typename Codegen>
|
||||||
|
void branch_if_both_int32(Assembler::Reg, Assembler::Reg, Codegen);
|
||||||
|
|
||||||
explicit Compiler(Bytecode::Executable& bytecode_executable)
|
explicit Compiler(Bytecode::Executable& bytecode_executable)
|
||||||
: m_bytecode_executable(bytecode_executable)
|
: m_bytecode_executable(bytecode_executable)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue