From 55f0791b13b3bab901f3b687e502beb751e4d54f Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Mon, 7 Jun 2021 19:37:23 +0200 Subject: [PATCH] LibJS: Add bytecode instructions for modulo and exponentiation --- .../Libraries/LibJS/Bytecode/ASTCodegen.cpp | 6 +++ .../Libraries/LibJS/Bytecode/Instruction.h | 2 + Userland/Libraries/LibJS/Bytecode/Op.cpp | 20 ++++++++++ Userland/Libraries/LibJS/Bytecode/Op.h | 38 +++++++++++++++++++ 4 files changed, 66 insertions(+) diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index b3e7f2da49..090dcb9114 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -55,6 +55,12 @@ Optional BinaryExpression::generate_bytecode(Bytecode::Gener case BinaryOp::Division: generator.emit(dst_reg, *lhs_reg, *rhs_reg); return dst_reg; + case BinaryOp::Modulo: + generator.emit(dst_reg, *lhs_reg, *rhs_reg); + return dst_reg; + case BinaryOp::Exponentiation: + generator.emit(dst_reg, *lhs_reg, *rhs_reg); + return dst_reg; case BinaryOp::LessThan: generator.emit(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 3c2c04aa88..dc523ebf93 100644 --- a/Userland/Libraries/LibJS/Bytecode/Instruction.h +++ b/Userland/Libraries/LibJS/Bytecode/Instruction.h @@ -15,6 +15,8 @@ O(Sub) \ O(Mul) \ O(Div) \ + O(Mod) \ + O(Exp) \ O(LessThan) \ O(AbstractInequals) \ O(AbstractEquals) \ diff --git a/Userland/Libraries/LibJS/Bytecode/Op.cpp b/Userland/Libraries/LibJS/Bytecode/Op.cpp index e98aeac56e..512ab3f081 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Op.cpp @@ -72,6 +72,16 @@ void Div::execute(Bytecode::Interpreter& interpreter) const interpreter.reg(m_dst) = div(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2)); } +void Mod::execute(Bytecode::Interpreter& interpreter) const +{ + interpreter.reg(m_dst) = mod(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2)); +} + +void Exp::execute(Bytecode::Interpreter& interpreter) const +{ + interpreter.reg(m_dst) = exp(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)); @@ -213,6 +223,16 @@ String Div::to_string() const return String::formatted("Div dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2); } +String Mod::to_string() const +{ + return String::formatted("Mod dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2); +} + +String Exp::to_string() const +{ + return String::formatted("Exp 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 d45779b783..b718c8b85f 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.h +++ b/Userland/Libraries/LibJS/Bytecode/Op.h @@ -108,6 +108,44 @@ private: Register m_src2; }; +class Mod final : public Instruction { +public: + Mod(Register dst, Register src1, Register src2) + : Instruction(Type::Mod) + , 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 Exp final : public Instruction { +public: + Exp(Register dst, Register src1, Register src2) + : Instruction(Type::Exp) + , 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)