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