From 046d5cb6092dcb0c9b485ab5a3512a69b70c0f94 Mon Sep 17 00:00:00 2001 From: Andre Wagner Date: Wed, 29 Apr 2026 14:14:33 -0500 Subject: [PATCH] . --- CMakeLists.txt | 10 ++++++--- src/bytecode/bytearray.cc | 7 ++----- src/bytecode/bytecode.hh | 1 + src/bytecode/bytecodeprototype.hh | 1 + src/bytecode/tests.cc | 1 - src/vm/code.cc | 6 ++++-- src/vm/code.hh | 2 +- src/vm/tests.cc | 35 +++++++++++++++++++++++++++++++ 8 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 src/vm/tests.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 64f8546..81b6b6c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,16 +82,20 @@ add_executable(${PROJECT_NAME}-bytecode-test src/bytecode/tests.cc) target_link_libraries(${PROJECT_NAME}-bytecode-test lib${PROJECT_NAME} gtest_main) add_test(NAME tyche_bytecode_test COMMAND ${PROJECT_NAME}-bytecode-test) +add_executable(${PROJECT_NAME}-vm-test src/vm/tests.cc) +target_link_libraries(${PROJECT_NAME}-vm-test lib${PROJECT_NAME} gtest_main) +add_test(NAME tyche_vm_test COMMAND ${PROJECT_NAME}-vm-test) + # # check for leaks # -add_custom_target(leaks) -add_custom_command(TARGET leaks +add_custom_target(leaks-vm-test) +add_custom_command(TARGET leaks-vm-test POST_BUILD COMMENT "Check for leaks using valgrind." WORKING_DIRECTORY ${CMAKE_BINARY_DIR} - COMMAND valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --suppressions=${CMAKE_SOURCE_DIR}/valgrind.supp ./${PROJECT_NAME} + COMMAND valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --suppressions=${CMAKE_SOURCE_DIR}/valgrind.supp ./${PROJECT_NAME}-vm-test ) # diff --git a/src/bytecode/bytearray.cc b/src/bytecode/bytearray.cc index 317218a..2af83b2 100644 --- a/src/bytecode/bytearray.cc +++ b/src/bytecode/bytearray.cc @@ -7,12 +7,9 @@ namespace tyche { void ByteArray::set_byte(uint32_t addr, uint8_t byte) { - try { - data_.at(addr) = byte; - } catch (std::out_of_range&) { + if (data_.size() < (addr + 1)) data_.resize(addr + 1, 0); - data_.at(addr) = byte; - } + data_.at(addr) = byte; } void ByteArray::set_int8(uint32_t addr, int8_t value) diff --git a/src/bytecode/bytecode.hh b/src/bytecode/bytecode.hh index a6001c6..9542f89 100644 --- a/src/bytecode/bytecode.hh +++ b/src/bytecode/bytecode.hh @@ -8,6 +8,7 @@ namespace tyche { class Bytecode { public: + Bytecode() = default; explicit Bytecode(ByteArray ba); [[nodiscard]] uint32_t n_constants() const; diff --git a/src/bytecode/bytecodeprototype.hh b/src/bytecode/bytecodeprototype.hh index d7159fb..3f32ea9 100644 --- a/src/bytecode/bytecodeprototype.hh +++ b/src/bytecode/bytecodeprototype.hh @@ -6,6 +6,7 @@ #include #include #include "constant.hh" +#include "bytearray.hh" namespace tyche { diff --git a/src/bytecode/tests.cc b/src/bytecode/tests.cc index 83f764c..a0dc262 100644 --- a/src/bytecode/tests.cc +++ b/src/bytecode/tests.cc @@ -1,5 +1,4 @@ #include "gtest/gtest.h" -#include "gmock/gmock.h" #include #include diff --git a/src/vm/code.cc b/src/vm/code.cc index 365e320..83fe958 100644 --- a/src/vm/code.cc +++ b/src/vm/code.cc @@ -4,10 +4,12 @@ namespace tyche { -void Code::import_bytecode(Bytecode const& incoming) +void Code::import_bytecode(ByteArray incoming) { + Bytecode bc(std::move(incoming)); // TODO - adjust function calls, constants - bytecode_ = incoming; + + bytecode_ = std::move(bc); } std::string Code::disassemble() const diff --git a/src/vm/code.hh b/src/vm/code.hh index ef558a9..1a86df2 100644 --- a/src/vm/code.hh +++ b/src/vm/code.hh @@ -7,7 +7,7 @@ namespace tyche { class Code { public: - void import_bytecode(Bytecode const& incoming); + void import_bytecode(ByteArray incoming); [[nodiscard]] std::string disassemble() const; diff --git a/src/vm/tests.cc b/src/vm/tests.cc new file mode 100644 index 0000000..40b0bb0 --- /dev/null +++ b/src/vm/tests.cc @@ -0,0 +1,35 @@ +#include "gtest/gtest.h" + +#include "../bytecode/bytecodeprototype.hh" +#include "../bytecode/bytearray.hh" +#include "../bytecode/bytecode.hh" +#include "code.hh" + +using namespace tyche; + +TEST(Code, ImportSingleAndDebug) +{ + BytecodePrototype bp; + + bp.constants.emplace_back(3.14f); + bp.constants.emplace_back("HELLO"); + + bp.functions.emplace_back(0, 0); + bp.functions.at(0).code.append_byte(0xa0); // pushi + bp.functions.at(0).code.append_int8(42); + + bp.functions.emplace_back(2, 1); + bp.functions.at(1).code.append_byte(0x1a); // appnd + + ByteArray ba = Bytecode::generate(bp); + + Code code; + code.import_bytecode(std::move(ba)); + printf("%s\n", code.disassemble().c_str()); +} + +int main(int argc, char** argv) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}