bytecode2 #2

Merged
andre merged 20 commits from bytecode2 into master 2026-04-28 19:50:48 -05:00
5 changed files with 39 additions and 17 deletions
Showing only changes of commit 84fca2a615 - Show all commits

View File

@@ -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})

View File

@@ -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

View File

@@ -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<int32_t, size_t> get_int(uint32_t addr) const;
[[nodiscard]] std::pair<float, size_t> get_float(uint32_t addr) const;

View File

@@ -2,14 +2,14 @@
#define TYCHE_BYTECODE_HH
#include "bytearray.hh"
#include "bytecodeprototype.hh"
namespace tyche {
class Bytecode {
public:
// reading
Bytecode(std::vector<uint8_t> 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:

View File

@@ -0,0 +1,29 @@
#ifndef TYCHE_BYTECODEPROTOTYPE_HH
#define TYCHE_BYTECODEPROTOTYPE_HH
#include <cstdint>
#include <string>
#include <variant>
#include <vector>
namespace tyche {
struct BytecodePrototype {
using ConstantValue = std::variant<int32_t, float, std::string>;
using Value = std::variant<int32_t, float>;
struct Function {
uint16_t n_pars = 0;
uint16_t n_locals = 0;
ByteArray code {};
};
std::vector<ConstantValue> constants {};
std::vector<Function> functions {};
// TODO - debugging info
};
}
#endif //TYCHE_BYTECODEPROTOTYPE_HH