From 84fca2a615f87cd4ff6a06e8b7d2802dd5b56790 Mon Sep 17 00:00:00 2001 From: Andre Wagner Date: Mon, 27 Apr 2026 13:28:03 -0500 Subject: [PATCH] . --- CMakeLists.txt | 1 + doc/OPCODES | 4 ++-- src/bytecode/bytearray.hh | 5 +++++ src/bytecode/bytecode.hh | 17 ++--------------- src/bytecode/bytecodeprototype.hh | 29 +++++++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 src/bytecode/bytecodeprototype.hh diff --git a/CMakeLists.txt b/CMakeLists.txt index 935c03d..ac29828 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,7 @@ add_library(lib${PROJECT_NAME} STATIC src/bytecode/bytearray.cc src/bytecode/bytecode.cc src/bytecode/bytecode.hh + src/bytecode/bytecodeprototype.hh ) target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings}) diff --git a/doc/OPCODES b/doc/OPCODES index 6476112..a84051c 100644 --- a/doc/OPCODES +++ b/doc/OPCODES @@ -80,8 +80,8 @@ Bytecode format The bytecode file is composed of the following sections: * [0x0] 16-byte header - [00]: VM format - [??]: reserved + [0:3]: Magic + [4]: VM format * [0x1] Index: pointers to each one of the sections, up to 8 Each pointer: 4 bits * [0x2] Constants: all constants (such as strings) used in the code diff --git a/src/bytecode/bytearray.hh b/src/bytecode/bytearray.hh index a7d5b85..4b25e2f 100644 --- a/src/bytecode/bytearray.hh +++ b/src/bytecode/bytearray.hh @@ -15,6 +15,11 @@ public: void add_float(uint32_t addr, float value); void add_string(uint32_t addr, std::string const& str); + void append_byte(uint32_t addr, uint8_t byte) { add_byte(data_.size(), byte); } + void append_int(uint32_t addr, int32_t value) { add_int(data_.size(), value); } + void append_float(uint32_t addr, float value) { add_float(data_.size(), value); } + void append_string(uint32_t addr, std::string const& str) { add_string(data_.size(), str); } + [[nodiscard]] uint8_t get_byte(uint32_t addr) const; [[nodiscard]] std::pair get_int(uint32_t addr) const; [[nodiscard]] std::pair get_float(uint32_t addr) const; diff --git a/src/bytecode/bytecode.hh b/src/bytecode/bytecode.hh index 1a0fa14..4b2e69a 100644 --- a/src/bytecode/bytecode.hh +++ b/src/bytecode/bytecode.hh @@ -2,14 +2,14 @@ #define TYCHE_BYTECODE_HH #include "bytearray.hh" +#include "bytecodeprototype.hh" namespace tyche { class Bytecode { public: - // reading - Bytecode(std::vector data); + Bytecode(BytecodePrototype const& bp); [[nodiscard]] uint32_t n_constants() const; [[nodiscard]] uint32_t n_functions() const; @@ -25,19 +25,6 @@ public: [[nodiscard]] int32_t get_code_int(uint32_t function_id, uint32_t idx) const; [[nodiscard]] float get_code_float(uint32_t function_id, uint32_t idx) const; - // writing - - Bytecode(); - - uint32_t add_constant(int32_t value); - uint32_t add_constant(float value); - uint32_t add_constant(std::string const& str); - - uint32_t add_function(uint16_t n_params, uint16_t locals); - - uint32_t add_code(uint8_t operation); - uint32_t add_code(uint8_t operation, int32_t operand_); - // TODO - debugging info private: diff --git a/src/bytecode/bytecodeprototype.hh b/src/bytecode/bytecodeprototype.hh new file mode 100644 index 0000000..e292589 --- /dev/null +++ b/src/bytecode/bytecodeprototype.hh @@ -0,0 +1,29 @@ +#ifndef TYCHE_BYTECODEPROTOTYPE_HH +#define TYCHE_BYTECODEPROTOTYPE_HH + +#include +#include +#include +#include + +namespace tyche { + +struct BytecodePrototype { + using ConstantValue = std::variant; + using Value = std::variant; + + struct Function { + uint16_t n_pars = 0; + uint16_t n_locals = 0; + ByteArray code {}; + }; + + std::vector constants {}; + std::vector functions {}; + + // TODO - debugging info +}; + +} + +#endif //TYCHE_BYTECODEPROTOTYPE_HH