Skip to content

Commit

Permalink
refactor: split Store into StoreGlobal and StoreLocal
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Mar 8, 2024
1 parent 689e5aa commit 623d29c
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 82 deletions.
4 changes: 2 additions & 2 deletions src/bytecode/builtin_functions/DefineFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "Function.h"
#include "bytecode/instructions/Store.h"
#include "bytecode/objects/GlobalRegister.h"
#include "bytecode/instructions/StoreGlobal.h"

namespace Bytecode::BuiltinFunctions {
class Define final : public Function {
Expand All @@ -14,7 +14,7 @@ namespace Bytecode::BuiltinFunctions {
if (args[1]->children.empty()) {
compiler.compile(*args[1], result, instructions);
const auto reg = compiler.program.declare_global(args[0]->token.token);
instructions.push_back(new Store(new GlobalRegister(reg)));
instructions.push_back(new StoreGlobal(reg));
} else {
auto segment = new Segment({});
compiler.program.declare_function(args[0]->token.asString(), segment);
Expand Down
2 changes: 2 additions & 0 deletions src/bytecode/instructions/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace Bytecode {
CondJumpIfNot,
Jump,
LoadGlobal,
StoreLocal,
StoreGlobal,
};

class Instruction {
Expand Down
1 change: 0 additions & 1 deletion src/bytecode/instructions/LoadLocal.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "Instruction.h"
#include "bytecode/objects/LocalRegister.h"
#include "bytecode/objects/Register.h"

namespace Bytecode {
Expand Down
1 change: 0 additions & 1 deletion src/bytecode/instructions/Store.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "Instruction.h"
#include "bytecode/objects/LocalRegister.h"
#include "bytecode/objects/Register.h"
#include <string>

Expand Down
27 changes: 27 additions & 0 deletions src/bytecode/instructions/StoreGlobal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "Instruction.h"
#include "bytecode/objects/Register.h"
#include <string>

namespace Bytecode {
class StoreGlobal final : public Instruction {
size_t reg;

public:
explicit StoreGlobal(size_t reg) : reg(reg) {
type = InstructionType::StoreGlobal;
}

void execute(VM *vm) override {
vm->setGlobal(0, vm->program_stack.pop());
}
[[nodiscard]] std::string toString() const override {
return "Store $r" + std::to_string(reg);
}
bool operator==(const Instruction &instruction) const override {
return instruction.type == type &&
dynamic_cast<const StoreGlobal *>(&instruction)->reg == reg;
}
};
}// namespace Bytecode
27 changes: 27 additions & 0 deletions src/bytecode/instructions/StoreLocal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "Instruction.h"
#include "bytecode/objects/Register.h"
#include <string>

namespace Bytecode {
class StoreLocal final : public Instruction {
size_t reg;

public:
explicit StoreLocal(size_t reg) : reg(reg) {
type = InstructionType::StoreLocal;
}

void execute(VM *vm) override {
vm->call_stack.setLocal(0, vm->program_stack.pop());
}
[[nodiscard]] std::string toString() const override {
return "StoreLocal $r" + std::to_string(reg);
}
bool operator==(const Instruction &instruction) const override {
return instruction.type == type &&
dynamic_cast<const StoreLocal *>(&instruction)->reg == reg;
}
};
}// namespace Bytecode
32 changes: 0 additions & 32 deletions src/bytecode/objects/GlobalRegister.h

This file was deleted.

29 changes: 0 additions & 29 deletions src/bytecode/objects/LocalRegister.h

This file was deleted.

5 changes: 2 additions & 3 deletions tests/bytecode/compiler/add_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#include "bytecode/instructions/Add.h"
#include "bytecode/instructions/LoadGlobal.h"
#include "bytecode/instructions/LoadLiteral.h"
#include "bytecode/instructions/Store.h"
#include "bytecode/objects/GlobalRegister.h"
#include "bytecode/instructions/StoreGlobal.h"
#include "parser/SyntaxTreeNode.h"
#include <gtest/gtest.h>

Expand Down Expand Up @@ -71,7 +70,7 @@ TEST(compiler_add, DontOptimizeIfNotPossible) {
new Segment(
{
new LoadLiteral(12),
new Store(new GlobalRegister(0)),
new StoreGlobal(0),
new LoadGlobal(0),
new LoadLiteral(74),
new Add(),
Expand Down
5 changes: 2 additions & 3 deletions tests/bytecode/compiler/multiply_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#include "bytecode/instructions/LoadGlobal.h"
#include "bytecode/instructions/LoadLiteral.h"
#include "bytecode/instructions/Multiply.h"
#include "bytecode/instructions/Store.h"
#include "bytecode/objects/GlobalRegister.h"
#include "bytecode/instructions/StoreGlobal.h"
#include "parser/SyntaxTreeNode.h"
#include <gtest/gtest.h>

Expand Down Expand Up @@ -70,7 +69,7 @@ TEST(compiler_multiply, IgnoreOptimizationWhenNotPossible) {
auto expected_result = Program({
new Segment({
new LoadLiteral(12),
new Store(new GlobalRegister(0)),
new StoreGlobal(0),
new LoadGlobal(0),
new LoadLiteral(74),
new Multiply(),
Expand Down
6 changes: 3 additions & 3 deletions tests/bytecode/compiler/to_string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "bytecode/instructions/LoadLiteral.h"
#include "bytecode/instructions/LoadLocal.h"
#include "bytecode/instructions/Multiply.h"
#include "bytecode/instructions/Store.h"
#include "bytecode/instructions/StoreLocal.h"
#include "bytecode/instructions/Subtract.h"
#include <gtest/gtest.h>

Expand Down Expand Up @@ -55,8 +55,8 @@ TEST(instruction_to_string, load_literal) {
}

TEST(instruction_to_string, store) {
const auto instruction = Store(new LocalRegister(12));
const auto expected_result = "Store $r12";
const auto instruction = StoreLocal(12);
const auto expected_result = "StoreLocal $r12";
const auto actual_result = instruction.toString();
EXPECT_EQ(expected_result, actual_result);
}
Expand Down
11 changes: 5 additions & 6 deletions tests/bytecode/compiler/variables_test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "bytecode/compiler/Compiler.h"
#include "bytecode/instructions/LoadGlobal.h"
#include "bytecode/instructions/LoadLiteral.h"
#include "bytecode/instructions/Store.h"
#include "bytecode/objects/GlobalRegister.h"
#include "bytecode/instructions/StoreGlobal.h"
#include "parser/SyntaxTreeNode.h"
#include <gtest/gtest.h>

Expand All @@ -20,7 +19,7 @@ TEST(compiler_variables, GlobalVariableDefinition) {
const auto expected_result = Program({
new Segment({
new LoadLiteral(10),
new Store(new GlobalRegister(0)),
new StoreGlobal(0),
}),
});

Expand Down Expand Up @@ -48,9 +47,9 @@ TEST(compiler_variables, MultipleGlobalVariablesDefinitions) {
auto expected_result = Program({
new Segment({
new LoadLiteral(10),
new Store(new GlobalRegister(0)),
new StoreGlobal(0),
new LoadLiteral(15),
new Store(new GlobalRegister(1)),
new StoreGlobal(1),
}),
});

Expand All @@ -75,7 +74,7 @@ TEST(compiler_variables, UseGlobalVariableInAnExpression) {
auto expected_result = Program({
new Segment({
new LoadLiteral(10),
new Store(new GlobalRegister(0)),
new StoreGlobal(0),
new LoadGlobal(0),
}),
});
Expand Down
4 changes: 2 additions & 2 deletions tests/bytecode/vm/load_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "bytecode/instructions/LoadGlobal.h"
#include "bytecode/instructions/LoadLiteral.h"
#include "bytecode/instructions/Store.h"
#include "bytecode/objects/GlobalRegister.h"
#include "bytecode/instructions/StoreGlobal.h"
#include "bytecode/objects/StackObject.h"
#include "bytecode/vm/Interpreter.h"
#include <gtest/gtest.h>
Expand All @@ -26,7 +26,7 @@ TEST(vm_load_test, ShouldLoadGlobalVariableIntoStack) {
const auto program = Program({
new Segment({
new LoadLiteral(10),
new Store(new GlobalRegister(0)),
new StoreGlobal(0),
new LoadGlobal(0),
}),
});
Expand Down

0 comments on commit 623d29c

Please sign in to comment.