diff --git a/src/bytecode/bytecode.cc b/src/bytecode/bytecode.cc index 59c2336..563b536 100644 --- a/src/bytecode/bytecode.cc +++ b/src/bytecode/bytecode.cc @@ -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); diff --git a/src/bytecode/bytecode.hh b/src/bytecode/bytecode.hh index f9ba207..8ba8fe2 100644 --- a/src/bytecode/bytecode.hh +++ b/src/bytecode/bytecode.hh @@ -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 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 function_addr; + std::vector function_sz; }; Cache cache_ {}; }; diff --git a/src/bytecode/tests.cc b/src/bytecode/tests.cc index cdedf1c..29c5ce9 100644 --- a/src/bytecode/tests.cc +++ b/src/bytecode/tests.cc @@ -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