diff --git a/doc/BYTECODE b/doc/BYTECODE index 3305954..d0d6571 100644 --- a/doc/BYTECODE +++ b/doc/BYTECODE @@ -18,6 +18,7 @@ The bytecode file is composed of the following sections: [0:3]: function pointer (counter start at the beginning of executable code) [4:5]: number of parameters [6:7]: number of local variables + [8:b]: function size * [0x2] Constants raw data * [0x3] Code: executable code * [0x4] Debugging info diff --git a/src/bytecode/bytecode.cc b/src/bytecode/bytecode.cc index 563b536..b5a3c5c 100644 --- a/src/bytecode/bytecode.cc +++ b/src/bytecode/bytecode.cc @@ -24,8 +24,8 @@ Bytecode::Bytecode(ByteArray ba) 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 + cache_.function_addr.emplace_back(code_start + byte_array_.get_uint32(cache_.functions_idx_addr + (i * FUNCTION_RECORD_SZ))); + cache_.function_sz.emplace_back(code_start + byte_array_.get_uint32(cache_.functions_idx_addr + (i * FUNCTION_RECORD_SZ) + 8)); } } @@ -117,6 +117,7 @@ ByteArray Bytecode::generate(BytecodePrototype const& bp) functions_indexes.set_uint32(idx_idx, code_idx); functions_indexes.set_uint16(idx_idx + 4, f.n_pars); functions_indexes.set_uint16(idx_idx + 6, f.n_locals); + functions_indexes.set_uint32(idx_idx + 8, f.code.size()); raw_code.append_bytearray(f.code); code_idx = raw_code.size(); idx_idx += FUNCTION_RECORD_SZ; diff --git a/src/bytecode/bytecode.hh b/src/bytecode/bytecode.hh index 8ba8fe2..fa3dc98 100644 --- a/src/bytecode/bytecode.hh +++ b/src/bytecode/bytecode.hh @@ -40,7 +40,7 @@ private: TOC_SZ = TOC_N_RECORDS * TOC_RECORD_SZ; static constexpr uint32_t CONST_IDX_START = TOC_START + TOC_SZ, CONST_RECORD_SZ = 4; - static constexpr uint32_t FUNCTION_RECORD_SZ = 8; + static constexpr uint32_t FUNCTION_RECORD_SZ = 12; enum Sections { SEC_CONST_IDX = 0, SEC_FUNC_IDX = 1, SEC_CONST_DATA = 2, SEC_CODE = 3 }; diff --git a/src/bytecode/tests.cc b/src/bytecode/tests.cc index 29c5ce9..435ce45 100644 --- a/src/bytecode/tests.cc +++ b/src/bytecode/tests.cc @@ -110,15 +110,15 @@ TEST(Bytecode, Code) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // constant index 0x50, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // variable index 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // raw constants - 0x60, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, // raw code + 0x68, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, // raw code 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // function definitions - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, // code 0x68, 0x54, 0x42,