bytecode2 #2

Merged
andre merged 20 commits from bytecode2 into master 2026-04-28 19:50:48 -05:00
3 changed files with 21 additions and 12 deletions
Showing only changes of commit a91e65dc84 - Show all commits

View File

@@ -19,12 +19,14 @@ Bytecode::Bytecode(ByteArray ba)
// load cache
cache_.constants_idx_addr = byte_array_.get_uint32(TOC_START);
cache_.n_constants = byte_array_.get_uint16(TOC_START + 4);
cache_.constants_start_addr = byte_array_.get_uint32(TOC_START + (6 * 2));
cache_.functions_idx_addr = byte_array_.get_uint32(TOC_START + 6);
cache_.n_functions = byte_array_.get_uint16(TOC_START + 10);
uint32_t code_start = byte_array_.get_uint32(TOC_START + (6 * 3));
for (uint32_t i = 0; i < cache_.n_functions; ++i)
cache_.functions_idx_addr = byte_array_.get_uint32(TOC_START + (1 * TOC_RECORD_SZ));
cache_.n_functions = byte_array_.get_uint16(TOC_START + (1 * TOC_RECORD_SZ) + 4);
cache_.constants_start_addr = byte_array_.get_uint32(TOC_START + (2 * TOC_RECORD_SZ));
uint32_t code_start = byte_array_.get_uint32(TOC_START + (3 * TOC_RECORD_SZ));
for (uint32_t i = 0; i < cache_.n_functions; ++i) {
cache_.function_addr.emplace_back(code_start + byte_array_.get_uint32(cache_.functions_idx_addr + (i * 8)));
cache_.function_sz.emplace_back(0); // TODO
}
}
uint32_t Bytecode::n_constants() const
@@ -39,31 +41,36 @@ uint32_t Bytecode::n_functions() const
int32_t Bytecode::get_constant_int(uint32_t idx) const
{
uint32_t constant_idx = byte_array_.get_uint32(cache_.constants_idx_addr + (idx * 4));
uint32_t constant_idx = byte_array_.get_uint32(cache_.constants_idx_addr + (idx * CONST_RECORD_SZ));
return byte_array_.get_int(cache_.constants_start_addr + constant_idx).first;
}
float Bytecode::get_constant_float(uint32_t idx) const
{
uint32_t constant_idx = byte_array_.get_uint32(cache_.constants_idx_addr + (idx * 4));
uint32_t constant_idx = byte_array_.get_uint32(cache_.constants_idx_addr + (idx * CONST_RECORD_SZ));
return byte_array_.get_float(cache_.constants_start_addr + constant_idx);
}
std::string Bytecode::get_constant_string(uint32_t idx) const
{
uint32_t constant_idx = byte_array_.get_uint32(cache_.constants_idx_addr + (idx * 4));
uint32_t constant_idx = byte_array_.get_uint32(cache_.constants_idx_addr + (idx * CONST_RECORD_SZ));
return byte_array_.get_string(cache_.constants_start_addr + constant_idx).first;
}
Bytecode::FunctionDef Bytecode::get_function_def(uint32_t function_id) const
{
uint32_t idx = cache_.functions_idx_addr + (function_id * 8);
uint32_t idx = cache_.functions_idx_addr + (function_id * FUNCTION_RECORD_SZ);
return {
.n_params = byte_array_.get_uint16(idx + 4),
.locals = byte_array_.get_uint16(idx + 6),
};
}
uint32_t Bytecode::get_function_sz(uint32_t function_id) const
{
return cache_.function_sz.at(function_id);
}
uint8_t Bytecode::get_code_byte(uint32_t function_id, uint32_t idx) const
{
return byte_array_.get_byte(cache_.function_addr.at(function_id) + idx);

View File

@@ -19,6 +19,7 @@ public:
struct FunctionDef { uint16_t n_params, locals; };
[[nodiscard]] FunctionDef get_function_def(uint32_t function_id) const;
[[nodiscard]] uint32_t get_function_sz(uint32_t function_id) const;
[[nodiscard]] uint8_t get_code_byte(uint32_t function_id, uint32_t idx) const;
[[nodiscard]] std::pair<int32_t, size_t> get_code_int(uint32_t function_id, uint32_t idx) const;
@@ -51,6 +52,7 @@ private:
uint32_t functions_idx_addr;
uint32_t n_functions;
std::vector<uint32_t> function_addr;
std::vector<uint32_t> function_sz;
};
Cache cache_ {};
};

View File

@@ -85,7 +85,7 @@ TEST(Bytecode, Constants)
};
ByteArray ba = Bytecode::generate(bp);
print(ba.data()); print(expected);
// print(ba.data()); print(expected);
ASSERT_EQ(ba.data(), expected);
}
@@ -125,7 +125,7 @@ TEST(Bytecode, Code)
};
ByteArray ba = Bytecode::generate(bp);
print(ba.data()); print(expected);
// print(ba.data()); print(expected);
ASSERT_EQ(ba.data(), expected);
}
@@ -147,7 +147,7 @@ TEST(Bytecode, Parsing)
ff.code.append_byte(0x42);
ByteArray ba = Bytecode::generate(bp);
print(ba.data());
// print(ba.data());
// read bytecode