This commit is contained in:
2026-04-27 20:38:49 -05:00
parent 7b39a40a32
commit 960cc76005
5 changed files with 90 additions and 18 deletions

View File

@@ -20,6 +20,12 @@ Bytecode::Bytecode(BytecodePrototype const& bp)
}
// function table
std::vector<std::pair<FunctionDef, uint32_t>> functions;
ByteArray code;
for (auto const& f: bp.functions) {
functions.emplace_back(std::make_pair(FunctionDef { f.n_pars, f.n_locals }, code.size()));
code.append_bytearray(f.code);
}
//
// build binary
@@ -29,9 +35,6 @@ Bytecode::Bytecode(BytecodePrototype const& bp)
byte_array_.set_uint32(0, MAGIC);
byte_array_.set_byte(4, VERSION);
// index - other entries created later
byte_array_.set_uint32(HEADER_SZ, HEADER_SZ + INDEX_SZ);
// constants
idx = HEADER_SZ + INDEX_SZ;
for (auto const& const_idx: constant_table) {
@@ -39,6 +42,28 @@ Bytecode::Bytecode(BytecodePrototype const& bp)
idx += 4;
}
byte_array_.append_bytearray(constant_array);
// constant index
if (!constant_table.empty())
byte_array_.set_uint32(HEADER_SZ, HEADER_SZ + INDEX_SZ);
// functions
size_t functions_start = idx + (constant_table.size() * 4) + byte_array_.size();
idx += functions_start;
uint32_t code_idx = 0;
for (auto const& f: functions) {
byte_array_.set_uint32(idx, code_idx);
byte_array_.set_uint16(idx + 4, f.first.n_params);
byte_array_.set_uint16(idx + 6, f.first.n_params);
idx += 8;
code_idx += f.second;
}
for (auto const& f: bp.functions)
byte_array_.append_bytearray(f.code);
// function index
if (!functions.empty())
byte_array_.set_uint32(HEADER_SZ + 4, functions_start);
}
uint32_t Bytecode::n_constants() const