Skip to content

Commit

Permalink
#54: refactoring of load methods (#81)
Browse files Browse the repository at this point in the history
* #54: fitignore

* #54: startrefactoring of load methods

* #54: continuing to clean load methods

* #54 add todos
  • Loading branch information
levBagryansky authored Feb 19, 2024
1 parent df859e2 commit fb013fb
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 253 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

# CMake build files
build/
third_party/benchmark
third_party/googletest

# ide stuff
.idea
cmake-build*
include/ChaiVM/interpreter/autogen
include/ChaiVM/interpreter/autogen
1 change: 0 additions & 1 deletion bench/sin_plus_cos_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ static void BM_SinCos(benchmark::State &state) {
for (auto _ : state) {
chai::memory::LinearBuffer buffer_ =
chai::memory::LinearBuffer(1024 * 256);
;
Executor executor{&wrapper.manager_, buffer_};
executor.run();
}
Expand Down
1 change: 1 addition & 0 deletions src/ChaiVM/utils/instr2Raw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ static uint8_t operation2opcode(Operation operation) {
return (uint8_t)operation;
}

// @todo #54:60min add template parameter by format and refactor naming
chai::bytecode_t instr2Raw(Operation op, RegisterId r1, RegisterId r2) {
return (operation2opcode(op)) | (r1 << 8) | (r2 << 16);
}
Expand Down
39 changes: 20 additions & 19 deletions test/ChaiVM/interpreter/executor-test-fixture.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
#include "executor-test-fixture.hpp"

using chai::interpreter::Immidiate;
using namespace chai::utils::fileformat;

void ExecutorTest::loadRR(chai::interpreter::Operation op,
chai::interpreter::RegisterId reg1,
chai::interpreter::RegisterId reg2) {
chaiFile_.addInstr(chai::utils::instr2Raw(op, reg1, reg2));
Immidiate ExecutorTest::loadRR(chai::interpreter::Operation op,
chai::interpreter::RegisterId reg1,
chai::interpreter::RegisterId reg2) {
return chaiFile_.addInstr(chai::utils::instr2Raw(op, reg1, reg2));
}

void ExecutorTest::loadRI(chai::interpreter::Operation op,
chai::interpreter::RegisterId reg1,
chai::interpreter::Immidiate imm) {
chaiFile_.addInstr(chai::utils::inst2RawRI(op, reg1, imm));
Immidiate ExecutorTest::loadRI(chai::interpreter::Operation op,
chai::interpreter::RegisterId reg1,
chai::interpreter::Immidiate imm) {
return chaiFile_.addInstr(chai::utils::inst2RawRI(op, reg1, imm));
}

int ExecutorTest::loadI(chai::interpreter::Operation op,
chai::interpreter::Immidiate imm) {
Immidiate ExecutorTest::loadI(chai::interpreter::Operation op,
chai::interpreter::Immidiate imm) {
return chaiFile_.addInstr(chai::utils::instr2Raw(op, imm));
}

Immidiate ExecutorTest::loadN(chai::interpreter::Operation op) {
return chaiFile_.addInstr(chai::utils::instr2Raw(op));
}

void ExecutorTest::loadWithConst(chai::interpreter::Operation op,
int64_t data) {
chaiFile_.addWithConst(op, data);
Expand All @@ -28,19 +33,15 @@ void ExecutorTest::loadWithConst(chai::interpreter::Operation op, double data) {
chaiFile_.addWithConst(op, data);
}

int ExecutorTest::load(chai::interpreter::Operation op) {
return chaiFile_.addInstr(chai::utils::instr2Raw(op));
}

void ExecutorTest::update() {
chaiFile_.toFile(PATH);
codeManager_.load(PATH);
chaiFile_.toFile(path_);
codeManager_.load(path_);
}

void ExecutorTest::SetUp() {
PATH = std::string{"test_"}.append(std::string{
path_ = std::string{"test_"}.append(std::string{
testing::UnitTest::GetInstance()->current_test_info()->name()});
std::remove(PATH.c_str());
std::remove(path_.c_str());
}

void ExecutorTest::TearDown() { std::remove(PATH.c_str()); }
void ExecutorTest::TearDown() { std::remove(path_.c_str()); }
71 changes: 56 additions & 15 deletions test/ChaiVM/interpreter/executor-test-fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class ExecutorTest : public ::testing::Test {
protected:
static constexpr chai::interpreter::RegisterId R0 = 1;
static constexpr chai::interpreter::RegisterId R0 = 0;
static constexpr chai::interpreter::RegisterId R1 = 1;
static constexpr chai::interpreter::RegisterId R2 = 2;
static constexpr chai::interpreter::RegisterId R3 = 3;
Expand All @@ -22,33 +22,74 @@ class ExecutorTest : public ::testing::Test {
static constexpr chai::interpreter::RegisterId R9 = 9;
static constexpr chai::interpreter::RegisterId R10 = 10;
static constexpr chai::interpreter::RegisterId R11 = 11;
std::filesystem::path PATH;
std::filesystem::path path_;

/*
* @todo #42:60min Rename all load methods to more appropriate names.
/**
* These methods loads the operation in template parameter with the
* corresponding parameters.
* @param op Operation.
* @return Number of added instruction among all instructions.
*/
void loadRR(chai::interpreter::Operation op,
chai::interpreter::RegisterId reg1,
chai::interpreter::RegisterId reg2 = 0);
void loadRI(chai::interpreter::Operation op,
chai::interpreter::RegisterId reg1,
chai::interpreter::Immidiate imm);

int loadI(chai::interpreter::Operation op,
chai::interpreter::Immidiate imm);
template <chai::interpreter::Operation op>
typename std::enable_if<chai::interpreter::OP_TO_FORMAT[op] ==
chai::interpreter::R,
chai::interpreter::Immidiate>::type
load(chai::interpreter::RegisterId reg1) {
return loadRR(op, reg1, 0);
}
template <chai::interpreter::Operation op>
typename std::enable_if<chai::interpreter::OP_TO_FORMAT[op] ==
chai::interpreter::RR,
chai::interpreter::Immidiate>::type
load(chai::interpreter::RegisterId reg1,
chai::interpreter::RegisterId reg2) {
return loadRR(op, reg1, reg2);
}
template <chai::interpreter::Operation op>
typename std::enable_if<chai::interpreter::OP_TO_FORMAT[op] ==
chai::interpreter::RI,
chai::interpreter::Immidiate>::type
load(chai::interpreter::RegisterId reg1, chai::interpreter::Immidiate imm) {
return loadRI(op, reg1, imm);
}
template <chai::interpreter::Operation op>
typename std::enable_if<chai::interpreter::OP_TO_FORMAT[op] ==
chai::interpreter::I,
chai::interpreter::Immidiate>::type
load(chai::interpreter::Immidiate imm) {
return loadI(op, imm);
}
template <chai::interpreter::Operation op>
typename std::enable_if<chai::interpreter::OP_TO_FORMAT[op] ==
chai::interpreter::N,
chai::interpreter::Immidiate>::type
load() {
return loadN(op);
}

// @todo #54:60min do something to add this functionality to load Immediate
void loadWithConst(chai::interpreter::Operation op, int64_t data);

void loadWithConst(chai::interpreter::Operation op, double data);

int load(chai::interpreter::Operation op);

void update();

void SetUp() override;

void TearDown() override;

private:
chai::interpreter::Immidiate loadRR(chai::interpreter::Operation op,
chai::interpreter::RegisterId reg1,
chai::interpreter::RegisterId reg2 = 0);
chai::interpreter::Immidiate loadRI(chai::interpreter::Operation op,
chai::interpreter::RegisterId reg1,
chai::interpreter::Immidiate imm);
chai::interpreter::Immidiate loadI(chai::interpreter::Operation op,
chai::interpreter::Immidiate imm);
chai::interpreter::Immidiate loadN(chai::interpreter::Operation op);

protected:
chai::utils::fileformat::ChaiFile chaiFile_;
chai::interpreter::CodeManager codeManager_;
chai::memory::LinearBuffer buffer_ = chai::memory::LinearBuffer(1024 * 256);
Expand Down
Loading

3 comments on commit fb013fb

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on fb013fb Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 42-1789e504 disappeared from test/ChaiVM/interpreter/executor_test.cpp), that's why I closed #54. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on fb013fb Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 54-89a87874 discovered in src/ChaiVM/utils/instr2Raw.cpp) and submitted as #82. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on fb013fb Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 54-d786432f discovered in test/ChaiVM/interpreter/executor-test-fixture.hpp) and submitted as #83. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.