diff options
author | Gunnar Beutner <gbeutner@serenityos.org> | 2021-06-07 19:17:20 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-07 19:21:36 +0200 |
commit | 3c5ce9b5b7fb7846d96eb4247835804403269b2e (patch) | |
tree | c9bef5c0a5672f3455d19c858d354aa2d22d8cfd /Userland/Libraries | |
parent | 5183952d1d556ec43a0ced2290029122e91a85bd (diff) | |
download | serenity-3c5ce9b5b7fb7846d96eb4247835804403269b2e.zip |
LibJS: Add bytecode instructions for multiplication and division
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Bytecode/Instruction.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Bytecode/Op.cpp | 20 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Bytecode/Op.h | 38 |
4 files changed, 66 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index d8d06feabf..b3e7f2da49 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -49,6 +49,12 @@ Optional<Bytecode::Register> BinaryExpression::generate_bytecode(Bytecode::Gener case BinaryOp::Subtraction: generator.emit<Bytecode::Op::Sub>(dst_reg, *lhs_reg, *rhs_reg); return dst_reg; + case BinaryOp::Multiplication: + generator.emit<Bytecode::Op::Mul>(dst_reg, *lhs_reg, *rhs_reg); + return dst_reg; + case BinaryOp::Division: + generator.emit<Bytecode::Op::Div>(dst_reg, *lhs_reg, *rhs_reg); + return dst_reg; case BinaryOp::LessThan: generator.emit<Bytecode::Op::LessThan>(dst_reg, *lhs_reg, *rhs_reg); return dst_reg; diff --git a/Userland/Libraries/LibJS/Bytecode/Instruction.h b/Userland/Libraries/LibJS/Bytecode/Instruction.h index 62d3d6ca30..3c2c04aa88 100644 --- a/Userland/Libraries/LibJS/Bytecode/Instruction.h +++ b/Userland/Libraries/LibJS/Bytecode/Instruction.h @@ -13,6 +13,8 @@ O(Load) \ O(Add) \ O(Sub) \ + O(Mul) \ + O(Div) \ O(LessThan) \ O(AbstractInequals) \ O(AbstractEquals) \ diff --git a/Userland/Libraries/LibJS/Bytecode/Op.cpp b/Userland/Libraries/LibJS/Bytecode/Op.cpp index 51d0d63ece..e98aeac56e 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Op.cpp @@ -62,6 +62,16 @@ void Sub::execute(Bytecode::Interpreter& interpreter) const interpreter.reg(m_dst) = sub(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2)); } +void Mul::execute(Bytecode::Interpreter& interpreter) const +{ + interpreter.reg(m_dst) = mul(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2)); +} + +void Div::execute(Bytecode::Interpreter& interpreter) const +{ + interpreter.reg(m_dst) = div(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2)); +} + void LessThan::execute(Bytecode::Interpreter& interpreter) const { interpreter.reg(m_dst) = less_than(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2)); @@ -193,6 +203,16 @@ String Sub::to_string() const return String::formatted("Sub dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2); } +String Mul::to_string() const +{ + return String::formatted("Mul dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2); +} + +String Div::to_string() const +{ + return String::formatted("Div dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2); +} + String LessThan::to_string() const { return String::formatted("LessThan dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2); diff --git a/Userland/Libraries/LibJS/Bytecode/Op.h b/Userland/Libraries/LibJS/Bytecode/Op.h index 289f0bef73..d45779b783 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.h +++ b/Userland/Libraries/LibJS/Bytecode/Op.h @@ -70,6 +70,44 @@ private: Register m_src2; }; +class Mul final : public Instruction { +public: + Mul(Register dst, Register src1, Register src2) + : Instruction(Type::Mul) + , m_dst(dst) + , m_src1(src1) + , m_src2(src2) + { + } + + void execute(Bytecode::Interpreter&) const; + String to_string() const; + +private: + Register m_dst; + Register m_src1; + Register m_src2; +}; + +class Div final : public Instruction { +public: + Div(Register dst, Register src1, Register src2) + : Instruction(Type::Div) + , m_dst(dst) + , m_src1(src1) + , m_src2(src2) + { + } + + void execute(Bytecode::Interpreter&) const; + String to_string() const; + +private: + Register m_dst; + Register m_src1; + Register m_src2; +}; + class LessThan final : public Instruction { public: LessThan(Register dst, Register src1, Register src2) |