bytecode2 #2

Merged
andre merged 20 commits from bytecode2 into master 2026-04-28 19:50:48 -05:00
4 changed files with 88 additions and 2 deletions
Showing only changes of commit 566f210f3f - Show all commits

View File

@@ -24,6 +24,14 @@ void ByteArray::add_int(uint32_t addr, int32_t value)
add_byte(addr, zz & 0x7F);
}
void ByteArray::add_uint32(uint32_t addr, uint32_t value)
{
add_byte(addr, (uint8_t)(value));
add_byte(addr+1, (uint8_t)(value >> 8));
add_byte(addr+2, (uint8_t)(value >> 16));
add_byte(addr+3, (uint8_t)(value >> 24));
}
void ByteArray::add_float(uint32_t addr, float value)
{
uint32_t bits;
@@ -46,6 +54,14 @@ uint8_t ByteArray::get_byte(uint32_t addr) const
return data_.at(addr);
}
uint32_t ByteArray::get_uint32(uint32_t addr) const
{
return (uint32_t) get_byte(addr)
| (uint32_t) get_byte(addr+1) << 8
| (uint32_t) get_byte(addr+2) << 16
| (uint32_t) get_byte(addr+3) << 24;
}
std::pair<int32_t, size_t> ByteArray::get_int(uint32_t addr) const
{
uint32_t zz = 0;

View File

@@ -10,17 +10,23 @@ namespace tyche {
class ByteArray {
public:
ByteArray() = default;
explicit ByteArray(std::vector<uint8_t> data) : data_(std::move(data)) {}
void add_byte(uint32_t addr, uint8_t byte);
void add_uint32(uint32_t addr, uint32_t value);
void add_int(uint32_t addr, int32_t value);
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_uint32(uint32_t addr, uint32_t value) { add_uint32(data_.size(), value); }
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]] uint32_t get_uint32(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;
[[nodiscard]] std::pair<std::string, size_t> get_string(uint32_t addr) const;

View File

@@ -2,5 +2,67 @@
namespace tyche {
Bytecode::Bytecode(BytecodePrototype const& bp)
{
// header
byte_array_.add_uint32(0, MAGIC);
byte_array_.add_byte(4, VERSION);
// constants
std::vector<uint32_t> constant_indexes;
std::vector<uint8_t> constant_array;
for (auto const& constant: bp.constants) {
}
// constants table
// function table
}
uint32_t Bytecode::n_constants() const
{
return 0;
}
uint32_t Bytecode::n_functions() const
{
return 0;
}
int32_t Bytecode::get_constant_int(uint32_t addr) const
{
return 0;
}
float Bytecode::get_constant_float(uint32_t addr) const
{
return 0;
}
std::string Bytecode::get_constant_string(uint32_t addr) const
{
return std::string();
}
Bytecode::FunctionDef Bytecode::get_function_def(uint32_t function_id) const
{
return Bytecode::FunctionDef();
}
uint8_t Bytecode::get_code_byte(uint32_t function_id, uint32_t idx) const
{
return 0;
}
int32_t Bytecode::get_code_int(uint32_t function_id, uint32_t idx) const
{
return 0;
}
float Bytecode::get_code_float(uint32_t function_id, uint32_t idx) const
{
return 0;
}
}

View File

@@ -8,8 +8,8 @@ namespace tyche {
class Bytecode {
public:
Bytecode(std::vector<uint8_t> data);
Bytecode(BytecodePrototype const& bp);
explicit Bytecode(std::vector<uint8_t> data) : byte_array_(std::move(data)) {}
explicit Bytecode(BytecodePrototype const& bp);
[[nodiscard]] uint32_t n_constants() const;
[[nodiscard]] uint32_t n_functions() const;
@@ -29,6 +29,8 @@ public:
private:
ByteArray byte_array_;
static constexpr uint8_t VERSION = 1;
static constexpr uint32_t MAGIC = 0x74b3c138;
};
}