VM basics #5

Merged
andre merged 9 commits from vm into master 2026-04-30 13:34:49 -05:00
23 changed files with 33 additions and 30 deletions
Showing only changes of commit bf0159c551 - Show all commits

View File

@@ -59,15 +59,15 @@ FetchContent_MakeAvailable(googletest)
# #
add_library(lib${PROJECT_NAME} SHARED add_library(lib${PROJECT_NAME} SHARED
src/bytecode/bytearray.hh src/common/overloaded.hh
src/bytecode/bytearray.cc src/common/bytearray.hh
src/common/bytearray.cc
src/bytecode/bytecode.cc src/bytecode/bytecode.cc
src/bytecode/bytecode.hh src/bytecode/bytecode.hh
src/bytecode/bytecodeprototype.hh src/bytecode/bytecodeprototype.hh
src/common/overloaded.hh src/bytecode/constant.hh
src/vm/code.cc src/vm/code.cc
src/vm/code.hh src/vm/code.hh
src/bytecode/constant.hh
src/vm/instruction.hh src/vm/instruction.hh
src/vm/instruction.cc src/vm/instruction.cc
src/vm/value.cc src/vm/value.cc

View File

@@ -1,7 +1,7 @@
#include "bytecode.hh" #include "bytecode.hh"
#include "../common/overloaded.hh" #include "../common/overloaded.hh"
namespace tyche { namespace tyche::bc {
Bytecode::Bytecode(ByteArray ba) Bytecode::Bytecode(ByteArray ba)
: byte_array_(std::move(ba)) : byte_array_(std::move(ba))

View File

@@ -1,10 +1,10 @@
#ifndef TYCHE_BYTECODE_HH #ifndef TYCHE_BYTECODE_HH
#define TYCHE_BYTECODE_HH #define TYCHE_BYTECODE_HH
#include "bytearray.hh" #include "../common/bytearray.hh"
#include "bytecodeprototype.hh" #include "bytecodeprototype.hh"
namespace tyche { namespace tyche::bc {
class Bytecode { class Bytecode {
public: public:

View File

@@ -6,9 +6,9 @@
#include <variant> #include <variant>
#include <vector> #include <vector>
#include "constant.hh" #include "constant.hh"
#include "bytearray.hh" #include "../common/bytearray.hh"
namespace tyche { namespace tyche::bc {
struct BytecodePrototype { struct BytecodePrototype {
struct Function { struct Function {

View File

@@ -4,7 +4,7 @@
#include <string> #include <string>
#include <variant> #include <variant>
namespace tyche { namespace tyche::bc {
using ConstantValue = std::variant<float, std::string>; using ConstantValue = std::variant<float, std::string>;

View File

@@ -3,11 +3,12 @@
#include <cstring> #include <cstring>
#include <functional> #include <functional>
#include "bytearray.hh" #include "../common/bytearray.hh"
#include "bytecodeprototype.hh" #include "bytecodeprototype.hh"
#include "bytecode.hh" #include "bytecode.hh"
using namespace tyche; using namespace tyche;
using namespace tyche::bc;
TEST(ByteArray, ByteArray) TEST(ByteArray, ByteArray)
{ {

View File

@@ -2,11 +2,11 @@
#include "../common/overloaded.hh" #include "../common/overloaded.hh"
#include "instruction.hh" #include "instruction.hh"
namespace tyche { namespace tyche::vm {
FunctionId Code::import_bytecode(ByteArray incoming) FunctionId Code::import_bytecode(ByteArray incoming)
{ {
Bytecode bc(std::move(incoming)); bc::Bytecode bc(std::move(incoming));
// TODO - adjust function calls, constants // TODO - adjust function calls, constants
bytecode_ = std::move(bc); bytecode_ = std::move(bc);

View File

@@ -6,7 +6,7 @@
#include "value.hh" #include "value.hh"
#include "../bytecode/bytecode.hh" #include "../bytecode/bytecode.hh"
namespace tyche { namespace tyche::vm {
struct Operation struct Operation
{ {
@@ -24,7 +24,7 @@ public:
[[nodiscard]] Operation operation(Location const& location) const; [[nodiscard]] Operation operation(Location const& location) const;
private: private:
Bytecode bytecode_; bc::Bytecode bytecode_;
}; };
} // tyche } // tyche

View File

@@ -2,7 +2,7 @@
#include "vm_exceptions.hh" #include "vm_exceptions.hh"
namespace tyche { namespace tyche::vm {
Value binary_operation(Value const& a, Value const& b, BinaryOperationType op) Value binary_operation(Value const& a, Value const& b, BinaryOperationType op)
{ {

View File

@@ -2,7 +2,7 @@
#define TYCHE_EXPR_HH #define TYCHE_EXPR_HH
#include "value.hh" #include "value.hh"
namespace tyche { namespace tyche::vm {
enum class BinaryOperationType { Sum }; enum class BinaryOperationType { Sum };

View File

@@ -1,6 +1,6 @@
#include "instruction.hh" #include "instruction.hh"
namespace tyche { namespace tyche::vm {
std::pair<std::string, size_t> debug_instruction(Instruction inst, int oper) std::pair<std::string, size_t> debug_instruction(Instruction inst, int oper)
{ {
@@ -112,7 +112,7 @@ std::pair<std::string, size_t> debug_instruction(Instruction inst, int oper)
return { out, 2 }; return { out, 2 };
} }
std::pair<std::string, size_t> debug_instruction(Bytecode const& bt, uint32_t function_id, uint32_t addr) std::pair<std::string, size_t> debug_instruction(bc::Bytecode const& bt, uint32_t function_id, uint32_t addr)
{ {
auto inst = (Instruction) bt.get_code_byte(function_id, addr); auto inst = (Instruction) bt.get_code_byte(function_id, addr);

View File

@@ -7,7 +7,7 @@
#include "../bytecode/bytecode.hh" #include "../bytecode/bytecode.hh"
namespace tyche { namespace tyche::vm {
enum class Instruction : uint8_t { enum class Instruction : uint8_t {
@@ -96,7 +96,7 @@ enum class Instruction : uint8_t {
}; };
std::pair<std::string, size_t> debug_instruction(Instruction inst, int oper=0); std::pair<std::string, size_t> debug_instruction(Instruction inst, int oper=0);
std::pair<std::string, size_t> debug_instruction(Bytecode const& bt, uint32_t function_id, uint32_t addr); std::pair<std::string, size_t> debug_instruction(bc::Bytecode const& bt, uint32_t function_id, uint32_t addr);
enum class OperandType { NoOperand, Int8, Int16, Int32 }; enum class OperandType { NoOperand, Int8, Int16, Int32 };
OperandType instruction_operand_type(Instruction instruction); OperandType instruction_operand_type(Instruction instruction);

View File

@@ -3,7 +3,7 @@
#include <cstdint> #include <cstdint>
namespace tyche { namespace tyche::vm {
struct Location struct Location
{ {

View File

@@ -2,7 +2,7 @@
#include "vm_exceptions.hh" #include "vm_exceptions.hh"
namespace tyche { namespace tyche::vm {
Stack::Stack() Stack::Stack()
{ {

View File

@@ -6,7 +6,7 @@
#include "value.hh" #include "value.hh"
namespace tyche { namespace tyche::vm {
class Stack { class Stack {
public: public:

View File

@@ -1,13 +1,15 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "../bytecode/bytecodeprototype.hh" #include "../bytecode/bytecodeprototype.hh"
#include "../bytecode/bytearray.hh" #include "../common/bytearray.hh"
#include "../bytecode/bytecode.hh" #include "../bytecode/bytecode.hh"
#include "code.hh" #include "code.hh"
#include "stack.hh" #include "stack.hh"
#include "vm.hh" #include "vm.hh"
using namespace tyche; using namespace tyche;
using namespace tyche::bc;
using namespace tyche::vm;
TEST(Code, ImportSingleAndDebug) TEST(Code, ImportSingleAndDebug)
{ {

View File

@@ -2,7 +2,7 @@
#include "../common/overloaded.hh" #include "../common/overloaded.hh"
namespace tyche { namespace tyche::vm {
Type Value::type() const Type Value::type() const
{ {

View File

@@ -4,7 +4,7 @@
#include <string> #include <string>
#include <variant> #include <variant>
namespace tyche { namespace tyche::vm {
using FunctionId = uint32_t; using FunctionId = uint32_t;

View File

@@ -3,7 +3,7 @@
#include "vm_exceptions.hh" #include "vm_exceptions.hh"
#include "expr.hh" #include "expr.hh"
namespace tyche { namespace tyche::vm {
void VM::load_bytecode(ByteArray const& ba) void VM::load_bytecode(ByteArray const& ba)
{ {

View File

@@ -5,7 +5,7 @@
#include "location.hh" #include "location.hh"
#include "stack.hh" #include "stack.hh"
namespace tyche { namespace tyche::vm {
class VM { class VM {
public: public:

View File

@@ -6,7 +6,7 @@
#include "expr.hh" #include "expr.hh"
namespace tyche { namespace tyche::vm {
class VMRuntimeError : public std::runtime_error class VMRuntimeError : public std::runtime_error
{ {