From 8a4cce0da423bf15772ad8f761ff321688dc8161 Mon Sep 17 00:00:00 2001 From: Andre Wagner Date: Thu, 14 May 2026 20:45:58 -0500 Subject: [PATCH] . --- doc/BYTECODE | 1 - lib/code.c | 22 +++++++++++++++++++--- lib/compiler/compiler.lua | 10 +++------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/doc/BYTECODE b/doc/BYTECODE index 6456367..c4d7450 100644 --- a/doc/BYTECODE +++ b/doc/BYTECODE @@ -11,7 +11,6 @@ The bytecode file is composed of the following sections: * CONSTANTS [0:3]: Code start address [4:7]: Number of constants - [...]: Constant index Each constant: [0]: Type (0 = string, 1 = real) if string: diff --git a/lib/code.c b/lib/code.c index df963c1..8d055ae 100644 --- a/lib/code.c +++ b/lib/code.c @@ -9,9 +9,16 @@ #define MAGIC 0xa7d6e9b1 +#define VERSION_ADDR 0x04 +#define CODE_START_ADDR 0x08 +#define N_CONST_ADDR 0x0c +#define CONST_START 0x10 + struct Code { - uint8_t* bytecode; - size_t bytecode_sz; + uint8_t* bytecode; + size_t bytecode_sz; + uint32_t* const_addr; + uint32_t* fn_addr; }; Code* code_new() @@ -39,12 +46,21 @@ TYC_RESULT code_load_bytecode(Code* code, uint8_t* bytecode, size_t bytecode_sz) code->bytecode = bytecode; code->bytecode_sz = bytecode_sz; + for (size_t i = 0; i < bytecode_sz; ++i) { + if (i % 16 == 0) + printf("%04X: ", i); + printf("%02x ", bytecode[i]); + if (i % 16 == 15) + printf("\n"); + } + return T_OK; } size_t code_n_consts(Code const* code) { - return 0; // TODO + uint32_t n_consts = *(uint32_t*) &code->bytecode[N_CONST_ADDR]; + return n_consts; } TYC_CONST_TYPE code_const_type(Code const* code, size_t n) diff --git a/lib/compiler/compiler.lua b/lib/compiler/compiler.lua index a6dc9d3..7dacd15 100644 --- a/lib/compiler/compiler.lua +++ b/lib/compiler/compiler.lua @@ -89,7 +89,7 @@ local function assemble(proto) local push16 = function(data) table.insert(bin, data & 0xff) table.insert(bin, (data >> 8) & 0xff) - return #bin - 2 + return #bin - 1 end local push32 = function(data) @@ -97,7 +97,7 @@ local function assemble(proto) table.insert(bin, (data >> 8) & 0xff) table.insert(bin, (data >> 16) & 0xff) table.insert(bin, (data >> 24) & 0xff) - return #bin - 4 + return #bin - 3 end local replace32 = function(pos, data) @@ -123,13 +123,9 @@ local function assemble(proto) -- constants local code_addr_pos = push32(0) -- code address, to be replaced + print(code_addr_pos) push32(#proto.constants) -- number of constants - local const_idx = {} - for _, _ in ipairs(proto.constants) do - table.insert(const_idx, push32(0)) -- constant address, to be replaced - end for i, const in ipairs(proto.constants) do - replace32(const_idx[i], #bin) if type(const) == 'string' then table.insert(bin, 0) -- string type push32(#const)