.
This commit is contained in:
@@ -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)
|
target_link_libraries(${PROJECT_NAME}-bytecode-test lib${PROJECT_NAME} gtest_main)
|
||||||
add_test(NAME tyche_bytecode_test COMMAND ${PROJECT_NAME}-bytecode-test)
|
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
|
# check for leaks
|
||||||
#
|
#
|
||||||
|
|
||||||
add_custom_target(leaks)
|
add_custom_target(leaks-vm-test)
|
||||||
add_custom_command(TARGET leaks
|
add_custom_command(TARGET leaks-vm-test
|
||||||
POST_BUILD
|
POST_BUILD
|
||||||
COMMENT "Check for leaks using valgrind."
|
COMMENT "Check for leaks using valgrind."
|
||||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
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
|
||||||
)
|
)
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|||||||
@@ -7,13 +7,10 @@ namespace tyche {
|
|||||||
|
|
||||||
void ByteArray::set_byte(uint32_t addr, uint8_t byte)
|
void ByteArray::set_byte(uint32_t addr, uint8_t byte)
|
||||||
{
|
{
|
||||||
try {
|
if (data_.size() < (addr + 1))
|
||||||
data_.at(addr) = byte;
|
|
||||||
} catch (std::out_of_range&) {
|
|
||||||
data_.resize(addr + 1, 0);
|
data_.resize(addr + 1, 0);
|
||||||
data_.at(addr) = byte;
|
data_.at(addr) = byte;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void ByteArray::set_int8(uint32_t addr, int8_t value)
|
void ByteArray::set_int8(uint32_t addr, int8_t value)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ namespace tyche {
|
|||||||
|
|
||||||
class Bytecode {
|
class Bytecode {
|
||||||
public:
|
public:
|
||||||
|
Bytecode() = default;
|
||||||
explicit Bytecode(ByteArray ba);
|
explicit Bytecode(ByteArray ba);
|
||||||
|
|
||||||
[[nodiscard]] uint32_t n_constants() const;
|
[[nodiscard]] uint32_t n_constants() const;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <variant>
|
#include <variant>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "constant.hh"
|
#include "constant.hh"
|
||||||
|
#include "bytearray.hh"
|
||||||
|
|
||||||
namespace tyche {
|
namespace tyche {
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include "gmock/gmock.h"
|
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|||||||
@@ -4,10 +4,12 @@
|
|||||||
|
|
||||||
namespace tyche {
|
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
|
// TODO - adjust function calls, constants
|
||||||
bytecode_ = incoming;
|
|
||||||
|
bytecode_ = std::move(bc);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Code::disassemble() const
|
std::string Code::disassemble() const
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ namespace tyche {
|
|||||||
|
|
||||||
class Code {
|
class Code {
|
||||||
public:
|
public:
|
||||||
void import_bytecode(Bytecode const& incoming);
|
void import_bytecode(ByteArray incoming);
|
||||||
|
|
||||||
[[nodiscard]] std::string disassemble() const;
|
[[nodiscard]] std::string disassemble() const;
|
||||||
|
|
||||||
|
|||||||
35
src/vm/tests.cc
Normal file
35
src/vm/tests.cc
Normal file
@@ -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();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user