.
This commit is contained in:
22
lib/code.c
22
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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user