Prechádzať zdrojové kódy

LibJS: Some more opcodes for the bytecode VM

- NewString (allocates a new PrimitiveString from the GC heap)
- GetVariable (retrieves a variable in the current scope)
- SetVariable (assigns a variable in the current scope)
Andreas Kling 4 rokov pred
rodič
commit
37cb70836b

+ 27 - 0
Userland/Libraries/LibJS/AST.cpp

@@ -2282,4 +2282,31 @@ Optional<Bytecode::Register> NumericLiteral::generate_bytecode(Bytecode::Generat
     return dst;
 }
 
+Optional<Bytecode::Register> StringLiteral::generate_bytecode(Bytecode::Generator& generator) const
+{
+    auto dst = generator.allocate_register();
+    generator.emit<Bytecode::Op::NewString>(dst, m_value);
+    return dst;
+}
+
+Optional<Bytecode::Register> Identifier::generate_bytecode(Bytecode::Generator& generator) const
+{
+    auto reg = generator.allocate_register();
+    generator.emit<Bytecode::Op::GetVariable>(reg, m_string);
+    return reg;
+}
+
+Optional<Bytecode::Register> AssignmentExpression::generate_bytecode(Bytecode::Generator& generator) const
+{
+    if (is<Identifier>(*m_lhs)) {
+        auto& identifier = static_cast<Identifier const&>(*m_lhs);
+        auto rhs_reg = m_rhs->generate_bytecode(generator);
+        VERIFY(rhs_reg.has_value());
+        generator.emit<Bytecode::Op::SetVariable>(identifier.string(), *rhs_reg);
+        return rhs_reg;
+    }
+
+    TODO();
+}
+
 }

+ 3 - 0
Userland/Libraries/LibJS/AST.h

@@ -666,6 +666,7 @@ public:
 
     virtual Value execute(Interpreter&, GlobalObject&) const override;
     virtual void dump(int indent) const override;
+    virtual Optional<Bytecode::Register> generate_bytecode(Bytecode::Generator&) const override;
 
     StringView value() const { return m_value; }
     bool is_use_strict_directive() const { return m_is_use_strict_directive; };
@@ -719,6 +720,7 @@ public:
     virtual Value execute(Interpreter&, GlobalObject&) const override;
     virtual void dump(int indent) const override;
     virtual Reference to_reference(Interpreter&, GlobalObject&) const override;
+    virtual Optional<Bytecode::Register> generate_bytecode(Bytecode::Generator&) const override;
 
 private:
     FlyString m_string;
@@ -896,6 +898,7 @@ public:
 
     virtual Value execute(Interpreter&, GlobalObject&) const override;
     virtual void dump(int indent) const override;
+    virtual Optional<Bytecode::Register> generate_bytecode(Bytecode::Generator&) const override;
 
 private:
     AssignmentOp m_op;

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

@@ -6,6 +6,7 @@
 
 #include <LibJS/Bytecode/Interpreter.h>
 #include <LibJS/Bytecode/Op.h>
+#include <LibJS/Runtime/GlobalObject.h>
 #include <LibJS/Runtime/Value.h>
 
 namespace JS::Bytecode::Op {
@@ -20,6 +21,21 @@ void Add::execute(Bytecode::Interpreter& interpreter) const
     interpreter.reg(m_dst) = add(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
 }
 
+void NewString::execute(Bytecode::Interpreter& interpreter) const
+{
+    interpreter.reg(m_dst) = js_string(interpreter.vm(), m_string);
+}
+
+void GetVariable::execute(Bytecode::Interpreter& interpreter) const
+{
+    interpreter.reg(m_dst) = interpreter.vm().get_variable(m_identifier, interpreter.global_object());
+}
+
+void SetVariable::execute(Bytecode::Interpreter& interpreter) const
+{
+    interpreter.vm().set_variable(m_identifier, interpreter.reg(m_src), interpreter.global_object());
+}
+
 String Load::to_string() const
 {
     return String::formatted("Load dst:r{}, value:{}", m_dst.index(), m_value.to_string_without_side_effects());
@@ -30,4 +46,19 @@ String Add::to_string() const
     return String::formatted("Add dst:r{}, src1:r{}, src2:r{}", m_dst.index(), m_src1.index(), m_src2.index());
 }
 
+String NewString::to_string() const
+{
+    return String::formatted("NewString dst:r{}, string:\"{}\"", m_dst.index(), m_string);
+}
+
+String GetVariable::to_string() const
+{
+    return String::formatted("GetVariable dst:r{}, identifier:{}", m_dst.index(), m_identifier);
+}
+
+String SetVariable::to_string() const
+{
+    return String::formatted("SetVariable identifier:{}, src:r{}", m_identifier, m_src.index());
+}
+
 }

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

@@ -6,6 +6,7 @@
 
 #pragma once
 
+#include <AK/FlyString.h>
 #include <LibJS/Bytecode/Instruction.h>
 #include <LibJS/Bytecode/Register.h>
 #include <LibJS/Heap/Cell.h>
@@ -49,4 +50,55 @@ private:
     Register m_src2;
 };
 
+class NewString final : public Instruction {
+public:
+    NewString(Register dst, String string)
+        : m_dst(dst)
+        , m_string(move(string))
+    {
+    }
+
+    virtual ~NewString() override { }
+    virtual void execute(Bytecode::Interpreter&) const override;
+    virtual String to_string() const override;
+
+private:
+    Register m_dst;
+    String m_string;
+};
+
+class SetVariable final : public Instruction {
+public:
+    SetVariable(FlyString identifier, Register src)
+        : m_identifier(move(identifier))
+        , m_src(src)
+    {
+    }
+
+    virtual ~SetVariable() override { }
+    virtual void execute(Bytecode::Interpreter&) const override;
+    virtual String to_string() const override;
+
+private:
+    FlyString m_identifier;
+    Register m_src;
+};
+
+class GetVariable final : public Instruction {
+public:
+    GetVariable(Register dst, FlyString identifier)
+        : m_dst(dst)
+        , m_identifier(move(identifier))
+    {
+    }
+
+    virtual ~GetVariable() override { }
+    virtual void execute(Bytecode::Interpreter&) const override;
+    virtual String to_string() const override;
+
+private:
+    Register m_dst;
+    FlyString m_identifier;
+};
+
 }