Browse Source

LibJS: Add bytecode ops for >, >= and <=

Gunnar Beutner 4 years ago
parent
commit
4be3374b24

+ 9 - 0
Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp

@@ -61,9 +61,18 @@ Optional<Bytecode::Register> BinaryExpression::generate_bytecode(Bytecode::Gener
     case BinaryOp::Exponentiation:
         generator.emit<Bytecode::Op::Exp>(dst_reg, *lhs_reg, *rhs_reg);
         return dst_reg;
+    case BinaryOp::GreaterThan:
+        generator.emit<Bytecode::Op::GreaterThan>(dst_reg, *lhs_reg, *rhs_reg);
+        return dst_reg;
+    case BinaryOp::GreaterThanEquals:
+        generator.emit<Bytecode::Op::GreaterThanEquals>(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;
+    case BinaryOp::LessThanEquals:
+        generator.emit<Bytecode::Op::LessThanEquals>(dst_reg, *lhs_reg, *rhs_reg);
+        return dst_reg;
     case BinaryOp::AbstractInequals:
         generator.emit<Bytecode::Op::AbstractInequals>(dst_reg, *lhs_reg, *rhs_reg);
         return dst_reg;

+ 3 - 0
Userland/Libraries/LibJS/Bytecode/Instruction.h

@@ -17,7 +17,10 @@
     O(Div)                        \
     O(Mod)                        \
     O(Exp)                        \
+    O(GreaterThan)                \
+    O(GreaterThanEquals)          \
     O(LessThan)                   \
+    O(LessThanEquals)             \
     O(AbstractInequals)           \
     O(AbstractEquals)             \
     O(NewString)                  \

+ 30 - 0
Userland/Libraries/LibJS/Bytecode/Op.cpp

@@ -82,11 +82,26 @@ void Exp::execute(Bytecode::Interpreter& interpreter) const
     interpreter.reg(m_dst) = exp(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
 }
 
+void GreaterThan::execute(Bytecode::Interpreter& interpreter) const
+{
+    interpreter.reg(m_dst) = greater_than(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
+}
+
+void GreaterThanEquals::execute(Bytecode::Interpreter& interpreter) const
+{
+    interpreter.reg(m_dst) = greater_than_equals(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));
 }
 
+void LessThanEquals::execute(Bytecode::Interpreter& interpreter) const
+{
+    interpreter.reg(m_dst) = less_than_equals(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
+}
+
 void AbstractInequals::execute(Bytecode::Interpreter& interpreter) const
 {
     interpreter.reg(m_dst) = Value(!abstract_eq(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2)));
@@ -233,11 +248,26 @@ String Exp::to_string() const
     return String::formatted("Exp dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
 }
 
+String GreaterThan::to_string() const
+{
+    return String::formatted("GreaterThan dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
+}
+
+String GreaterThanEquals::to_string() const
+{
+    return String::formatted("GreaterThanEquals 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);
 }
 
+String LessThanEquals::to_string() const
+{
+    return String::formatted("LessThanEquals dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
+}
+
 String AbstractInequals::to_string() const
 {
     return String::formatted("AbstractInequals dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);

+ 57 - 0
Userland/Libraries/LibJS/Bytecode/Op.h

@@ -146,6 +146,44 @@ private:
     Register m_src2;
 };
 
+class GreaterThan final : public Instruction {
+public:
+    GreaterThan(Register dst, Register src1, Register src2)
+        : Instruction(Type::GreaterThan)
+        , 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 GreaterThanEquals final : public Instruction {
+public:
+    GreaterThanEquals(Register dst, Register src1, Register src2)
+        : Instruction(Type::GreaterThanEquals)
+        , 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)
@@ -165,6 +203,25 @@ private:
     Register m_src2;
 };
 
+class LessThanEquals final : public Instruction {
+public:
+    LessThanEquals(Register dst, Register src1, Register src2)
+        : Instruction(Type::LessThanEquals)
+        , 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 AbstractInequals final : public Instruction {
 public:
     AbstractInequals(Register dst, Register src1, Register src2)