.
This commit is contained in:
@@ -11,7 +11,6 @@ The bytecode file is composed of the following sections:
|
|||||||
* CONSTANTS
|
* CONSTANTS
|
||||||
[0:3]: Code start address
|
[0:3]: Code start address
|
||||||
[4:7]: Number of constants
|
[4:7]: Number of constants
|
||||||
[...]: Constant index
|
|
||||||
Each constant:
|
Each constant:
|
||||||
[0]: Type (0 = string, 1 = real)
|
[0]: Type (0 = string, 1 = real)
|
||||||
if string:
|
if string:
|
||||||
|
|||||||
18
lib/code.c
18
lib/code.c
@@ -9,9 +9,16 @@
|
|||||||
|
|
||||||
#define MAGIC 0xa7d6e9b1
|
#define MAGIC 0xa7d6e9b1
|
||||||
|
|
||||||
|
#define VERSION_ADDR 0x04
|
||||||
|
#define CODE_START_ADDR 0x08
|
||||||
|
#define N_CONST_ADDR 0x0c
|
||||||
|
#define CONST_START 0x10
|
||||||
|
|
||||||
struct Code {
|
struct Code {
|
||||||
uint8_t* bytecode;
|
uint8_t* bytecode;
|
||||||
size_t bytecode_sz;
|
size_t bytecode_sz;
|
||||||
|
uint32_t* const_addr;
|
||||||
|
uint32_t* fn_addr;
|
||||||
};
|
};
|
||||||
|
|
||||||
Code* code_new()
|
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 = bytecode;
|
||||||
code->bytecode_sz = bytecode_sz;
|
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;
|
return T_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t code_n_consts(Code const* code)
|
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)
|
TYC_CONST_TYPE code_const_type(Code const* code, size_t n)
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ local function assemble(proto)
|
|||||||
local push16 = function(data)
|
local push16 = function(data)
|
||||||
table.insert(bin, data & 0xff)
|
table.insert(bin, data & 0xff)
|
||||||
table.insert(bin, (data >> 8) & 0xff)
|
table.insert(bin, (data >> 8) & 0xff)
|
||||||
return #bin - 2
|
return #bin - 1
|
||||||
end
|
end
|
||||||
|
|
||||||
local push32 = function(data)
|
local push32 = function(data)
|
||||||
@@ -97,7 +97,7 @@ local function assemble(proto)
|
|||||||
table.insert(bin, (data >> 8) & 0xff)
|
table.insert(bin, (data >> 8) & 0xff)
|
||||||
table.insert(bin, (data >> 16) & 0xff)
|
table.insert(bin, (data >> 16) & 0xff)
|
||||||
table.insert(bin, (data >> 24) & 0xff)
|
table.insert(bin, (data >> 24) & 0xff)
|
||||||
return #bin - 4
|
return #bin - 3
|
||||||
end
|
end
|
||||||
|
|
||||||
local replace32 = function(pos, data)
|
local replace32 = function(pos, data)
|
||||||
@@ -123,13 +123,9 @@ local function assemble(proto)
|
|||||||
|
|
||||||
-- constants
|
-- constants
|
||||||
local code_addr_pos = push32(0) -- code address, to be replaced
|
local code_addr_pos = push32(0) -- code address, to be replaced
|
||||||
|
print(code_addr_pos)
|
||||||
push32(#proto.constants) -- number of constants
|
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
|
for i, const in ipairs(proto.constants) do
|
||||||
replace32(const_idx[i], #bin)
|
|
||||||
if type(const) == 'string' then
|
if type(const) == 'string' then
|
||||||
table.insert(bin, 0) -- string type
|
table.insert(bin, 0) -- string type
|
||||||
push32(#const)
|
push32(#const)
|
||||||
|
|||||||
Reference in New Issue
Block a user