From fb013fba8e396144f245be94b4d64d205f0b866d Mon Sep 17 00:00:00 2001 From: Lev Bagryansky <32939651+levBagryansky@users.noreply.github.com> Date: Mon, 19 Feb 2024 20:14:59 +0300 Subject: [PATCH] #54: refactoring of load methods (#81) * #54: fitignore * #54: startrefactoring of load methods * #54: continuing to clean load methods * #54 add todos --- .gitignore | 4 +- bench/sin_plus_cos_bench.cpp | 1 - src/ChaiVM/utils/instr2Raw.cpp | 1 + .../interpreter/executor-test-fixture.cpp | 39 ++- .../interpreter/executor-test-fixture.hpp | 71 +++- test/ChaiVM/interpreter/executor_test.cpp | 330 +++++++++--------- .../simple-programs-executor-test.cpp | 100 +++--- tools/opcode2operation-generator.py | 2 +- tools/resources/instructions.yml | 2 +- 9 files changed, 297 insertions(+), 253 deletions(-) diff --git a/.gitignore b/.gitignore index feb3847..53e7552 100644 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,10 @@ # CMake build files build/ +third_party/benchmark +third_party/googletest # ide stuff .idea cmake-build* -include/ChaiVM/interpreter/autogen \ No newline at end of file +include/ChaiVM/interpreter/autogen diff --git a/bench/sin_plus_cos_bench.cpp b/bench/sin_plus_cos_bench.cpp index c31015b..3f8579b 100644 --- a/bench/sin_plus_cos_bench.cpp +++ b/bench/sin_plus_cos_bench.cpp @@ -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(); } diff --git a/src/ChaiVM/utils/instr2Raw.cpp b/src/ChaiVM/utils/instr2Raw.cpp index f635224..2a6edff 100644 --- a/src/ChaiVM/utils/instr2Raw.cpp +++ b/src/ChaiVM/utils/instr2Raw.cpp @@ -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); } diff --git a/test/ChaiVM/interpreter/executor-test-fixture.cpp b/test/ChaiVM/interpreter/executor-test-fixture.cpp index 173e380..8fd4ff8 100644 --- a/test/ChaiVM/interpreter/executor-test-fixture.cpp +++ b/test/ChaiVM/interpreter/executor-test-fixture.cpp @@ -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); @@ -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()); } diff --git a/test/ChaiVM/interpreter/executor-test-fixture.hpp b/test/ChaiVM/interpreter/executor-test-fixture.hpp index 6ef5ea1..fcb06e4 100644 --- a/test/ChaiVM/interpreter/executor-test-fixture.hpp +++ b/test/ChaiVM/interpreter/executor-test-fixture.hpp @@ -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; @@ -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 + typename std::enable_if::type + load(chai::interpreter::RegisterId reg1) { + return loadRR(op, reg1, 0); + } + template + typename std::enable_if::type + load(chai::interpreter::RegisterId reg1, + chai::interpreter::RegisterId reg2) { + return loadRR(op, reg1, reg2); + } + template + typename std::enable_if::type + load(chai::interpreter::RegisterId reg1, chai::interpreter::Immidiate imm) { + return loadRI(op, reg1, imm); + } + template + typename std::enable_if::type + load(chai::interpreter::Immidiate imm) { + return loadI(op, imm); + } + template + typename std::enable_if::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); diff --git a/test/ChaiVM/interpreter/executor_test.cpp b/test/ChaiVM/interpreter/executor_test.cpp index 3194986..4599aab 100644 --- a/test/ChaiVM/interpreter/executor_test.cpp +++ b/test/ChaiVM/interpreter/executor_test.cpp @@ -20,31 +20,31 @@ class MathTest : public ExecutorTest {}; */ TEST_F(ExecutorTest, run) { loadWithConst(Ldia, static_cast(6)); - loadRR(Star, 2, 0); + load(2); loadWithConst(Ldia, static_cast(8)); - loadRR(Star, 3, 0); - loadRR(Ldra, 3, 0); - loadRR(Mul, 2, 0); - loadRR(Ret, 0, 0); + load(3); + load(3); + load(2); + load(); update(); exec_.run(); EXPECT_EQ(exec_.acc(), 48); EXPECT_EQ(exec_.getCurrentFrame(), nullptr); } TEST_F(ExecutorTest, inv) { - load(Inv); + load(); update(); EXPECT_THROW(exec_.run(), InvalidInstruction); } TEST_F(ExecutorTest, nop) { - load(Nop); - load(Ret); + load(); + load(); update(); exec_.run(); EXPECT_EQ(exec_.getCurrentFrame(), nullptr); } TEST_F(ExecutorTest, ret) { - load(Ret); + load(); update(); exec_.run(); EXPECT_EQ(exec_.getCurrentFrame(), nullptr); @@ -55,11 +55,11 @@ TEST_F(ExecutorTest, mov) { RegisterId r1 = 0; RegisterId r2 = 1; loadWithConst(Ldia, val); - loadRR(Star, r1, 0); - loadRR(Mov, r1, r2); + load(r1); + load(r1, r2); loadWithConst(Mul, static_cast(-1)); - loadRR(Ldra, r2, 0); - load(Ret); + load(r2); + load(); update(); exec_.run(); EXPECT_EQ(exec_.acc(), val); @@ -68,7 +68,7 @@ TEST_F(ExecutorTest, mov) { TEST_F(ExecutorTest, ldia) { int64_t val = -1L; loadWithConst(Ldia, val); - load(Ret); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), static_cast(val)); @@ -78,9 +78,9 @@ TEST_F(ExecutorTest, ldra) { int64_t val = -3L; RegisterId r1 = 0; loadWithConst(Ldia, val); - loadRR(Star, r1, 0); - loadRR(Ldra, r1, 0); - load(Ret); + load(r1); + load(r1); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), static_cast(val)); @@ -90,8 +90,8 @@ TEST_F(ExecutorTest, star) { int64_t val = 3L; RegisterId r1 = 0; loadWithConst(Ldia, val); - loadRR(Star, r1, 0); - load(Ret); + load(r1); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), val); @@ -100,10 +100,10 @@ TEST_F(ExecutorTest, add) { int64_t val1 = 10; int64_t val2 = 33; loadWithConst(Ldia, val1); - loadRR(Star, R1, 0); + load(R1); loadWithConst(Ldia, val2); - loadRR(Add, R1, 0); - load(Ret); + load(R1); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), val1 + val2); @@ -113,10 +113,10 @@ TEST_F(ExecutorTest, addNeg) { int64_t val2 = -33; RegisterId r1 = 0; loadWithConst(Ldia, val1); - loadRR(Star, r1, 0); + load(r1); loadWithConst(Ldia, val2); - loadRR(Add, r1, 0); - load(Ret); + load(r1); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), val1 + val2); @@ -126,7 +126,7 @@ TEST_F(ExecutorTest, addi) { int64_t val2 = std::numeric_limits::min(); loadWithConst(Ldia, val2); loadWithConst(Addi, val1); - load(Ret); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), val1 + val2); @@ -136,10 +136,10 @@ TEST_F(ExecutorTest, sub) { int64_t val2 = 33; RegisterId r1 = 0; loadWithConst(Ldia, val1); - loadRR(Star, r1, 0); + load(r1); loadWithConst(Ldia, val2); - loadRR(Sub, r1, 0); - load(Ret); + load(r1); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), val2 - val1); @@ -149,7 +149,7 @@ TEST_F(ExecutorTest, subi) { int64_t val2 = -1028; loadWithConst(Ldia, val2); loadWithConst(Subi, val1); - load(Ret); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), val2 - val1); @@ -159,10 +159,10 @@ TEST_F(ExecutorTest, mul) { int64_t val2 = -33; RegisterId r1 = 0; loadWithConst(Ldia, val1); - loadRR(Star, r1, 0); + load(r1); loadWithConst(Ldia, val2); - loadRR(Mul, r1, 0); - load(Ret); + load(r1); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), val1 * val2); @@ -172,7 +172,7 @@ TEST_F(ExecutorTest, muli) { int64_t val2 = 345; loadWithConst(Ldia, val2); loadWithConst(Muli, val1); - load(Ret); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), val1 * val2); @@ -182,10 +182,10 @@ TEST_F(ExecutorTest, div) { int64_t val2 = -33; RegisterId r1 = 1; loadWithConst(Ldia, val1); - loadRR(Star, r1, 0); + load(r1); loadWithConst(Ldia, val2); - loadRR(Div, r1, 0); - load(Ret); + load
(r1); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), val2 / val1); @@ -195,7 +195,7 @@ TEST_F(ExecutorTest, divi) { int64_t val2 = 4; loadWithConst(Ldia, val2); loadWithConst(Divi, val1); - load(Ret); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), val2 / val1); @@ -203,7 +203,7 @@ TEST_F(ExecutorTest, divi) { TEST_F(ExecutorTest, ldiaf) { double val = 3.14; loadWithConst(Ldiaf, val); - load(Ret); + load(); update(); exec_.run(); EXPECT_FLOAT_EQ(std::bit_cast(exec_.acc()), val); @@ -213,10 +213,10 @@ TEST_F(ExecutorTest, addf) { auto val2 = 2.71; RegisterId r1 = 0; loadWithConst(Ldiaf, val1); - loadRR(Star, r1, 0); + load(r1); loadWithConst(Ldiaf, val2); - loadRR(Addf, r1, 0); - load(Ret); + load(r1); + load(); update(); exec_.run(); EXPECT_FLOAT_EQ(std::bit_cast(exec_.acc()), val1 + val2); @@ -226,7 +226,7 @@ TEST_F(ExecutorTest, addif) { auto val2 = -2.71; loadWithConst(Ldiaf, val2); loadWithConst(Addif, val1); - load(Ret); + load(); update(); exec_.run(); EXPECT_FLOAT_EQ(std::bit_cast(exec_.acc()), val1 + val2); @@ -235,10 +235,10 @@ TEST_F(ExecutorTest, subf) { auto val1 = 3.14; auto val2 = 2.71; loadWithConst(Ldiaf, val1); - loadRR(Star, R0, 0); + load(R0); loadWithConst(Ldiaf, val2); - loadRR(Subf, R0, 0); - load(Ret); + load(R0); + load(); update(); exec_.run(); EXPECT_FLOAT_EQ(std::bit_cast(exec_.acc()), val2 - val1); @@ -248,7 +248,7 @@ TEST_F(ExecutorTest, subif) { auto val2 = 2.71; loadWithConst(Ldiaf, val2); loadWithConst(Subif, val1); - load(Ret); + load(); update(); exec_.run(); EXPECT_FLOAT_EQ(std::bit_cast(exec_.acc()), val2 - val1); @@ -258,10 +258,10 @@ TEST_F(ExecutorTest, mulf) { auto val2 = 2.71; RegisterId r1 = 0; loadWithConst(Ldiaf, val1); - loadRR(Star, r1, 0); + load(r1); loadWithConst(Ldiaf, val2); - loadRR(Mulf, r1, 0); - load(Ret); + load(r1); + load(); update(); exec_.run(); EXPECT_FLOAT_EQ(std::bit_cast(exec_.acc()), val1 * val2); @@ -271,7 +271,7 @@ TEST_F(ExecutorTest, mulif) { auto val2 = 2.71; loadWithConst(Ldiaf, val2); loadWithConst(Mulif, val1); - load(Ret); + load(); update(); exec_.run(); EXPECT_FLOAT_EQ(std::bit_cast(exec_.acc()), val1 * val2); @@ -280,10 +280,10 @@ TEST_F(ExecutorTest, divf) { auto val1 = 3.14; auto val2 = 2.71; loadWithConst(Ldiaf, val2); - loadRR(Star, R0, 0); + load(R0); loadWithConst(Ldiaf, val1); - loadRR(Divf, R0, 0); - load(Ret); + load(R0); + load(); update(); exec_.run(); EXPECT_FLOAT_EQ(std::bit_cast(exec_.acc()), val1 / val2); @@ -293,7 +293,7 @@ TEST_F(ExecutorTest, divif) { auto val2 = 2.71; loadWithConst(Ldiaf, val1); loadWithConst(Divif, val2); - load(Ret); + load(); update(); exec_.run(); EXPECT_FLOAT_EQ(std::bit_cast(exec_.acc()), val1 / val2); @@ -301,8 +301,8 @@ TEST_F(ExecutorTest, divif) { TEST_F(MathTest, icsqrt) { loadWithConst(Ldiaf, 4.0); - load(IcSqrt); - load(Ret); + load(); + load(); update(); exec_.run(); EXPECT_EQ(std::bit_cast(exec_.acc()), 2.0); @@ -310,8 +310,8 @@ TEST_F(MathTest, icsqrt) { TEST_F(MathTest, icsin) { loadWithConst(Ldiaf, 30.0 * static_cast(M_PI) / 180); - load(IcSin); - load(Ret); + load(); + load(); update(); exec_.run(); EXPECT_FLOAT_EQ(std::bit_cast(exec_.acc()), 0.5); @@ -319,8 +319,8 @@ TEST_F(MathTest, icsin) { TEST_F(MathTest, iccos) { loadWithConst(Ldiaf, 60.0 * (M_PI) / 180); - load(IcCos); - load(Ret); + load(); + load(); update(); exec_.run(); EXPECT_FLOAT_EQ(std::bit_cast(exec_.acc()), 0.5); @@ -328,11 +328,11 @@ TEST_F(MathTest, iccos) { TEST_F(ExecutorTest, If_icmpeq_simple) { Immidiate val = chaiFile_.addConst(std::make_unique(42)); - loadI(Ldia, val); - loadRR(Star, R1); - loadRI(If_icmpeq, R1, 2 * sizeof(bytecode_t)); - load(Inv); - load(Ret); + load(val); + load(R1); + load(R1, 2 * sizeof(bytecode_t)); + load(); + load(); update(); exec_.run(); } @@ -340,13 +340,13 @@ TEST_F(ExecutorTest, If_icmpeq_simple) { TEST_F(ExecutorTest, If_icmpne_cycle) { constexpr int64_t threshold = 2000; loadWithConst(Ldia, static_cast(1)); - loadRR(Star, R1); + load(R1); loadWithConst(Ldia, threshold); - loadRR(Star, R10); + load(R10); loadWithConst(Ldia, static_cast(0)); - loadRR(Add, R1); - loadRI(If_icmpne, R10, static_cast(-1 * sizeof(bytecode_t))); - load(Ret); + load(R1); + load(R10, static_cast(-1 * sizeof(bytecode_t))); + load(); update(); exec_.run(); EXPECT_EQ(exec_.acc(), threshold); @@ -355,13 +355,13 @@ TEST_F(ExecutorTest, If_icmpne_cycle) { TEST_F(ExecutorTest, If_icmpgt_cycle) { constexpr int64_t threshold = -200; loadWithConst(Ldia, static_cast(1)); - loadRR(Star, R1); + load(R1); loadWithConst(Ldia, threshold); - loadRR(Star, R10); + load(R10); loadWithConst(Ldia, static_cast(0)); - loadRR(Sub, R1); - loadRI(If_icmpgt, R10, static_cast(-1 * sizeof(bytecode_t))); - load(Ret); + load(R1); + load(R10, static_cast(-1 * sizeof(bytecode_t))); + load(); update(); exec_.run(); EXPECT_EQ(exec_.acc(), threshold); @@ -370,13 +370,13 @@ TEST_F(ExecutorTest, If_icmpgt_cycle) { TEST_F(ExecutorTest, If_icmpge_cycle) { constexpr int64_t threshold = -200; loadWithConst(Ldia, static_cast(1)); - loadRR(Star, R1); + load(R1); loadWithConst(Ldia, threshold); - loadRR(Star, R10); + load(R10); loadWithConst(Ldia, static_cast(0)); - loadRR(Sub, R1); - loadRI(If_icmpge, R10, static_cast(-1 * sizeof(bytecode_t))); - load(Ret); + load(R1); + load(R10, static_cast(-1 * sizeof(bytecode_t))); + load(); update(); exec_.run(); EXPECT_EQ(exec_.acc(), threshold - 1); @@ -385,13 +385,13 @@ TEST_F(ExecutorTest, If_icmpge_cycle) { TEST_F(ExecutorTest, If_icmplt_cycle) { constexpr int64_t threshold = 200; loadWithConst(Ldia, static_cast(1)); - loadRR(Star, R1); + load(R1); loadWithConst(Ldia, threshold); - loadRR(Star, R10); + load(R10); loadWithConst(Ldia, static_cast(0)); - loadRR(Add, R1); - loadRI(If_icmplt, R10, static_cast(-1 * sizeof(bytecode_t))); - load(Ret); + load(R1); + load(R10, static_cast(-1 * sizeof(bytecode_t))); + load(); update(); exec_.run(); EXPECT_EQ(exec_.acc(), threshold); @@ -400,13 +400,13 @@ TEST_F(ExecutorTest, If_icmplt_cycle) { TEST_F(ExecutorTest, If_icmple_cycle) { constexpr int64_t threshold = 200; loadWithConst(Ldia, static_cast(1)); - loadRR(Star, R1); + load(R1); loadWithConst(Ldia, threshold); - loadRR(Star, R10); + load(R10); loadWithConst(Ldia, static_cast(0)); - loadRR(Add, R1); - loadRI(If_icmple, R10, static_cast(-1 * sizeof(bytecode_t))); - load(Ret); + load(R1); + load(R10, static_cast(-1 * sizeof(bytecode_t))); + load(); update(); exec_.run(); EXPECT_EQ(exec_.acc(), threshold + 1); @@ -414,11 +414,11 @@ TEST_F(ExecutorTest, If_icmple_cycle) { TEST_F(ExecutorTest, Cmpgf_greater) { loadWithConst(Ldiaf, 0.1234567); - loadRR(Star, R1); + load(R1); loadWithConst(Ldiaf, 123.9); // acc > r1 - loadRR(Сmpgf, R1); - load(Ret); + load<Сmpgf>(R1); + load(); update(); exec_.run(); EXPECT_EQ(exec_.acc(), 1); @@ -426,11 +426,11 @@ TEST_F(ExecutorTest, Cmpgf_greater) { TEST_F(ExecutorTest, Cmpgf_less) { loadWithConst(Ldiaf, 0.1234567); - loadRR(Star, R1); + load(R1); loadWithConst(Ldiaf, -123.9); // acc < r1 - loadRR(Сmpgf, R1); - load(Ret); + load<Сmpgf>(R1); + load(); update(); exec_.run(); EXPECT_EQ(exec_.acc(), -1); @@ -438,11 +438,11 @@ TEST_F(ExecutorTest, Cmpgf_less) { TEST_F(ExecutorTest, Cmpgf_equal) { loadWithConst(Ldiaf, 0.123456789); - loadRR(Star, R1); + load(R1); loadWithConst(Ldiaf, 0.123456789); // acc < r1 - loadRR(Сmpgf, R1); - load(Ret); + load<Сmpgf>(R1); + load(); update(); exec_.run(); EXPECT_EQ(exec_.acc(), 0); @@ -450,11 +450,11 @@ TEST_F(ExecutorTest, Cmpgf_equal) { TEST_F(ExecutorTest, Cmpgf_nan) { loadWithConst(Ldiaf, NAN); - loadRR(Star, R1); + load(R1); loadWithConst(Ldiaf, 0.123456789); // r1 is NaN - loadRR(Сmpgf, R1); - load(Ret); + load<Сmpgf>(R1); + load(); update(); exec_.run(); EXPECT_EQ(exec_.acc(), -1); @@ -462,11 +462,11 @@ TEST_F(ExecutorTest, Cmpgf_nan) { TEST_F(ExecutorTest, Cmplf_less) { loadWithConst(Ldiaf, 0.15); - loadRR(Star, R1); + load(R1); loadWithConst(Ldiaf, -1234.5); // acc < r1 - loadRR(Cmplf, R1); - load(Ret); + load(R1); + load(); update(); exec_.run(); EXPECT_EQ(exec_.acc(), 1); @@ -474,11 +474,11 @@ TEST_F(ExecutorTest, Cmplf_less) { TEST_F(ExecutorTest, Cmplf_nan) { loadWithConst(Ldiaf, 0.15); - loadRR(Star, R1); + load(R1); loadWithConst(Ldiaf, NAN); // acc is NaN - loadRR(Cmplf, R1); - load(Ret); + load(R1); + load(); update(); exec_.run(); EXPECT_EQ(exec_.acc(), -1); @@ -486,13 +486,13 @@ TEST_F(ExecutorTest, Cmplf_nan) { TEST_F(ExecutorTest, Goto_forward) { constexpr int ret = 345; - loadI(Goto, (ret) * sizeof(bytecode_t)); + load((ret) * sizeof(bytecode_t)); for (int i = 0; i < ret - 1; ++i) { - load(Inv); + load(); } - ASSERT_EQ(load(Ret), ret); + ASSERT_EQ(load(), ret); for (int i = 0; i < 500; ++i) { - load(Inv); + load(); } update(); exec_.run(); @@ -502,19 +502,19 @@ TEST_F(ExecutorTest, Goto_forward_and_back) { // jump to constexpr int ret1 = 345; constexpr int ret2 = ret1 + 5; - loadI(Goto, (ret2) * sizeof(bytecode_t)); + load((ret2) * sizeof(bytecode_t)); for (int i = 0; i < ret1 - 1; ++i) { - load(Inv); + load(); } - ASSERT_EQ(load(Ret), ret1); - load(Inv); - load(Inv); - load(Inv); - load(Inv); - ASSERT_EQ(loadI(Goto, static_cast(-5 * sizeof(bytecode_t))), + ASSERT_EQ(load(), ret1); + load(); + load(); + load(); + load(); + ASSERT_EQ(load(static_cast(-5 * sizeof(bytecode_t))), ret2); for (int i = 0; i < 500; ++i) { - load(Inv); + load(); } update(); exec_.run(); @@ -530,9 +530,9 @@ TEST_F(ExecutorTest, Call) { int64_t val1 = static_cast(314); int64_t val2 = static_cast(271); loadWithConst(Ldia, val2); - loadRR(Star, 99); + load(99); loadWithConst(Ldia, val1); - loadRR(Star, 98); + load(98); Immidiate func_ref = chaiFile_.addFunction(UINT16_MAX, "aboba_func", "(II)I", std::vector{ @@ -541,8 +541,8 @@ TEST_F(ExecutorTest, Call) { instr2Raw(Ret), }, 2, 50); - loadI(Call, func_ref); - load(Ret); + load(func_ref); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), val2 - val1); @@ -559,11 +559,11 @@ TEST_F(ExecutorTest, GetI64FromArr) { constexpr auto i = static_cast(23); constexpr auto size = static_cast(50); loadWithConst(Ldia, i); - loadRR(Star, R3); + load(R3); loadWithConst(Ldia, size); - load(NewI64Array); - loadRR(GetI64FromArr, R3); - load(Ret); + load(); + load(R3); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), 0); @@ -582,14 +582,14 @@ TEST_F(ExecutorTest, SetI64SetArr) { constexpr auto size = static_cast(50); constexpr auto value = static_cast(12345); loadWithConst(Ldia, i); - loadRR(Star, R3); + load(R3); loadWithConst(Ldia, value); - loadRR(Star, R4); + load(R4); loadWithConst(Ldia, size); - load(NewI64Array); - loadRR(SetI64InArr, R3, R4); - loadRR(GetI64FromArr, R3); - load(Ret); + load(); + load(R3, R4); + load(R3); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), value); @@ -607,11 +607,11 @@ TEST_F(ExecutorTest, GetF64FromArr) { constexpr auto i = static_cast(23); constexpr auto size = static_cast(50); loadWithConst(Ldia, i); - loadRR(Star, R3); + load(R3); loadWithConst(Ldia, size); - load(NewF64Array); - loadRR(GetF64FromArr, R3); - load(Ret); + load(); + load(R3); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), 0.0); @@ -630,14 +630,14 @@ TEST_F(ExecutorTest, SetF64SetArr) { constexpr auto size = static_cast(50); constexpr auto value = static_cast(12.345); loadWithConst(Ldia, i); - loadRR(Star, R3); + load(R3); loadWithConst(Ldiaf, value); - loadRR(Star, R4); + load(R4); loadWithConst(Ldia, size); - load(NewF64Array); - loadRR(SetF64InArr, R3, R4); - loadRR(GetF64FromArr, R3); - load(Ret); + load(); + load(R3, R4); + load(R3); + load(); update(); exec_.run(); EXPECT_EQ(std::bit_cast(exec_.acc()), value); @@ -646,9 +646,9 @@ TEST_F(ExecutorTest, SetF64SetArr) { TEST_F(ExecutorTest, StringPrint) { Immidiate raw = chaiFile_.addConst(std::make_unique("ABOBA")); - loadI(Ldia, raw); - load(StringPrint); - load(Ret); + load(raw); + load(); + load(); update(); exec_.run(); EXPECT_EQ(codeManager_.getCnstString(exec_.acc()), "ABOBA"); @@ -659,13 +659,13 @@ TEST_F(ExecutorTest, StringConcat) { Immidiate raw1 = chaiFile_.addConst(std::make_unique("ABOBA")); Immidiate raw2 = chaiFile_.addConst(std::make_unique(" Yeash")); - loadI(Ldia, raw2); - loadRR(Star, R2); - load(StringPrint); - loadI(Ldia, raw1); - load(StringPrint); - loadRR(StringConcat, R2); - load(Ret); + load(raw2); + load(R2); + load(); + load(raw1); + load(); + load(R2); + load(); update(); exec_.run(); EXPECT_EQ(codeManager_.getCnstString(exec_.acc()), "ABOBA Yeash"); @@ -674,9 +674,9 @@ TEST_F(ExecutorTest, StringConcat) { TEST_F(ExecutorTest, StringSize) { Immidiate raw = chaiFile_.addConst(std::make_unique("ABOBA")); - loadI(Ldia, raw); - load(StringLen); - load(Ret); + load(raw); + load(); + load(); update(); exec_.run(); EXPECT_EQ(exec_.acc(), 5); @@ -687,13 +687,13 @@ TEST_F(ExecutorTest, StringSlice) { constexpr auto start = static_cast(1); constexpr auto end = static_cast(4); loadWithConst(Ldia, start); - loadRR(Star, R2); + load(R2); loadWithConst(Ldia, end); - loadRR(Star, R3); - loadI(Ldia, raw); - load(chai::interpreter::StringPrint); - loadRR(StringSlice, R2, R3); - load(Ret); + load(R3); + load(raw); + load(); + load(R2, R3); + load(); update(); exec_.run(); EXPECT_EQ(codeManager_.getCnstString(exec_.acc()), "BOB"); diff --git a/test/ChaiVM/interpreter/simple-programs-executor-test.cpp b/test/ChaiVM/interpreter/simple-programs-executor-test.cpp index 868deb4..4c7a790 100644 --- a/test/ChaiVM/interpreter/simple-programs-executor-test.cpp +++ b/test/ChaiVM/interpreter/simple-programs-executor-test.cpp @@ -9,56 +9,56 @@ using namespace chai::utils::fileformat; TEST_F(ExecutorTest, SquareEquation) { // r1 = 1.0, r2 = -5.0, r3 = 6.0 loadWithConst(Ldiaf, 1.0); - loadRR(Star, R1, 0); + load(R1); loadWithConst(Ldiaf, -5.0); - loadRR(Star, R2, 0); + load(R2); loadWithConst(Ldiaf, +6.0); - loadRR(Star, R3, 0); + load(R3); // r4 = -4*r1*r3 loadWithConst(Ldiaf, -4.0); - loadRR(Mulf, R1, 0); - loadRR(Mulf, R3, 0); - loadRR(Star, R4, 0); + load(R1); + load(R3); + load(R4); // r5 = b * b - loadRR(Ldra, R2, 0); - loadRR(Mulf, R2, 0); - loadRR(Star, R5, 0); + load(R2); + load(R2); + load(R5); // r6 = r5 + r4 - loadRR(Ldra, R5, 0); - loadRR(Addf, R4, 0); - loadRR(Star, R6, 0); + load(R5); + load(R4); + load(R6); // r6 = sqrt(r6) - loadRR(Ldra, R6, 0); - load(IcSqrt); - loadRR(Star, R6, 0); + load(R6); + load(); + load(R6); // r7 = 2a - loadRR(Ldra, R1, 0); + load(R1); loadWithConst(Mulif, 2.0); - loadRR(Star, R7, 0); + load(R7); // r8 = r6 - r2 - loadRR(Ldra, R6, 0); - loadRR(Subf, R2, 0); - loadRR(Star, R8, 0); + load(R6); + load(R2); + load(R8); // X1 = r9 = r8 / r7 - loadRR(Ldra, R8, 0); - loadRR(Divf, R7, 0); - loadRR(Star, R9, 0); + load(R8); + load(R7); + load(R9); // acc = -r2 - r6 // r11 = acc / r7 loadWithConst(Ldiaf, 0.0); - loadRR(Subf, R2, 0); - loadRR(Subf, R6, 0); - loadRR(Divf, R7, 0); - loadRR(Star, R11, 0); - load(Ret); + load(R2); + load(R6); + load(R7); + load(R11); + load(); update(); exec_.run(); EXPECT_FLOAT_EQ(std::bit_cast(exec_.acc()), 2.0); @@ -75,7 +75,7 @@ TEST_F(ExecutorTest, Factorial) { int64_t n = static_cast(5); Immidiate one = chaiFile_.addConst(std::make_unique(1)); loadWithConst(Ldia, n); - loadRR(Star, 99); + load(99); Immidiate func_ref = chaiFile_.nextFunc(); EXPECT_EQ( chaiFile_.addFunction( @@ -90,8 +90,8 @@ TEST_F(ExecutorTest, Factorial) { instr2Raw(Ret)}, 1, 8), func_ref); - loadI(Call, func_ref); - load(Ret); + load(func_ref); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), 120); @@ -102,14 +102,14 @@ TEST_F(ExecutorTest, ArraysDemo) { constexpr auto size = static_cast(50); constexpr auto value = static_cast(12345); loadWithConst(Ldia, i); - loadRR(Star, R3); + load(R3); loadWithConst(Ldia, value); - loadRR(Star, R4); + load(R4); loadWithConst(Ldia, size); - load(NewI64Array); - loadRR(SetI64InArr, R3, R4); - loadRR(GetI64FromArr, R3); - load(Ret); + load(); + load(R3, R4); + load(R3); + load(); update(); exec_.run(); EXPECT_EQ(static_cast(exec_.acc()), value); @@ -129,22 +129,22 @@ TEST_F(ExecutorTest, StringDemo) { Immidiate str2 = chaiFile_.addConst( std::make_unique("dungeons and dragons")); loadWithConst(Ldia, static_cast(0)); - loadRR(Star, R1); // R1 = 0 + load(R1); // R1 = 0 loadWithConst(Ldia, static_cast(5)); - loadRR(Star, R2); // R2 = 5 + load(R2); // R2 = 5 loadWithConst(Ldia, static_cast(13)); - loadRR(Star, R3); // R3 = 13 + load(R3); // R3 = 13 loadWithConst(Ldia, static_cast(20)); - loadRR(Star, R4); // R4 = 20 - loadI(Ldia, str1); - loadRR(StringSlice, R1, R2); - loadRR(Star, R10); // R10 = substr1 = "Hello" - loadI(Ldia, str2); - loadRR(StringSlice, R3, R4); - loadRR(Star, R11); // R11 = substr2 = "dragons" - loadRR(Ldra, R10); - loadRR(StringConcat, R11); - load(Ret); + load(R4); // R4 = 20 + load(str1); + load(R1, R2); + load(R10); // R10 = substr1 = "Hello" + load(str2); + load(R3, R4); + load(R11); // R11 = substr2 = "dragons" + load(R10); + load(R11); + load(); update(); exec_.run(); EXPECT_EQ(codeManager_.getCnstString(exec_.acc()), "Hellodragons"); diff --git a/tools/opcode2operation-generator.py b/tools/opcode2operation-generator.py index ff77bea..472eb57 100644 --- a/tools/opcode2operation-generator.py +++ b/tools/opcode2operation-generator.py @@ -48,7 +48,7 @@ def main() -> int: {% for op in operations %}"{{ op }}", {% endfor %} }; constexpr OperationFormat OP_TO_FORMAT[] = { -Unknown, {% for n, item in enumerate(items, 1) %}{{ item.format }}, {% endfor %} +N, {% for n, item in enumerate(items, 1) %}{{ item.format }}, {% endfor %} }; } // namespace chai::interpreter diff --git a/tools/resources/instructions.yml b/tools/resources/instructions.yml index 321f281..5945c41 100644 --- a/tools/resources/instructions.yml +++ b/tools/resources/instructions.yml @@ -159,7 +159,7 @@ instructions: - fixedvalue: 30 mnemonic: If_icmpeq - format: N + format: RI description: "i64, if acc == r1 then branch to instruction at offset [imm] otherwise just next instr" - fixedvalue: 31