This commit is contained in:
2026-04-28 17:14:35 -05:00
parent 8f5f470edd
commit a91e65dc84
3 changed files with 21 additions and 12 deletions

View File

@@ -19,12 +19,14 @@ Bytecode::Bytecode(ByteArray ba)
// load cache // load cache
cache_.constants_idx_addr = byte_array_.get_uint32(TOC_START); cache_.constants_idx_addr = byte_array_.get_uint32(TOC_START);
cache_.n_constants = byte_array_.get_uint16(TOC_START + 4); 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 + (1 * TOC_RECORD_SZ));
cache_.functions_idx_addr = byte_array_.get_uint32(TOC_START + 6); cache_.n_functions = byte_array_.get_uint16(TOC_START + (1 * TOC_RECORD_SZ) + 4);
cache_.n_functions = byte_array_.get_uint16(TOC_START + 10); cache_.constants_start_addr = byte_array_.get_uint32(TOC_START + (2 * TOC_RECORD_SZ));
uint32_t code_start = byte_array_.get_uint32(TOC_START + (6 * 3)); uint32_t code_start = byte_array_.get_uint32(TOC_START + (3 * TOC_RECORD_SZ));
for (uint32_t i = 0; i < cache_.n_functions; ++i) 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_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 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 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; return byte_array_.get_int(cache_.constants_start_addr + constant_idx).first;
} }
float Bytecode::get_constant_float(uint32_t idx) const 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); return byte_array_.get_float(cache_.constants_start_addr + constant_idx);
} }
std::string Bytecode::get_constant_string(uint32_t idx) const 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; return byte_array_.get_string(cache_.constants_start_addr + constant_idx).first;
} }
Bytecode::FunctionDef Bytecode::get_function_def(uint32_t function_id) const 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 { return {
.n_params = byte_array_.get_uint16(idx + 4), .n_params = byte_array_.get_uint16(idx + 4),
.locals = byte_array_.get_uint16(idx + 6), .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 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); 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; }; struct FunctionDef { uint16_t n_params, locals; };
[[nodiscard]] FunctionDef get_function_def(uint32_t function_id) const; [[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]] 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; [[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 functions_idx_addr;
uint32_t n_functions; uint32_t n_functions;
std::vector<uint32_t> function_addr; std::vector<uint32_t> function_addr;
std::vector<uint32_t> function_sz;
}; };
Cache cache_ {}; Cache cache_ {};
}; };

View File

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