wip: working on extern in VM

- Feat: add usage of vm reader instead of old way in vm.c
- Ref: update debug opcodes printing
This commit is contained in:
samsonium
2026-11-05 23:23:38 +03:00
parent 1db42c1e8e
commit d95ff88b66
19 changed files with 133 additions and 146 deletions
+11 -91
View File
@@ -12,6 +12,7 @@
#include <vm/fzl_vm.h>
#include <vm/fzl_vm_env.h>
#include <vm/fzl_vm_object.h>
#include <vm/fzl_vm_reader.h>
#include <vm/internals/opcode.h>
char last_err[256];
@@ -20,8 +21,14 @@ size_t total_jmp_on_same = 0;
static char *expect_hdr = "FZL\0\2";
fzl_vm_t *fzl_mkvm(const char *filepath)
fzl_vm_t *fzl_mkvm(fzl_vm_reader_t *reader)
{
if (!reader)
{
snprintf(last_err, sizeof(last_err), "reader is not provided");
return NULL;
}
fzl_vm_t *vm = malloc(sizeof(fzl_vm_t));
if (!vm)
{
@@ -30,86 +37,7 @@ fzl_vm_t *fzl_mkvm(const char *filepath)
}
memset(vm, 0, sizeof(fzl_vm_t));
FILE *fd = fopen(filepath, "rb");
if (!fd)
{
snprintf(last_err, sizeof(last_err), "failed to open file '%s'", filepath);
free(vm);
return NULL;
}
char hdr[5];
fread(hdr, sizeof(char), 5,
fd); // NOTE: add red bytes check (also for freads below)
if (memcmp(hdr, expect_hdr, 5) != 0)
{
snprintf(last_err, sizeof(last_err), "magic check error");
fclose(fd);
free(vm);
return NULL;
}
size_t consts_count, ops_count;
fread(&consts_count, sizeof(size_t), 1, fd);
fread(&ops_count, sizeof(size_t), 1, fd);
// TODO: read externs count
if (ops_count < 1)
{
snprintf(last_err, sizeof(last_err), "there is nothing to do (0 ops)");
fclose(fd);
free(vm);
return NULL;
}
if (consts_count > 0)
{
vm->consts_count = consts_count;
vm->consts = malloc(consts_count * sizeof(fzl_value_t));
for (size_t i = 0; i < consts_count; i++)
{
fzl_consttype_t type;
fread(&type, sizeof(fzl_consttype_t), 1, fd);
if (type == FZL_CONSTTYPE_STRING)
{
size_t length;
char *chars;
fread(&length, sizeof(size_t), 1, fd);
chars = malloc(length * sizeof(char) + 1);
fread(chars, sizeof(char), length, fd);
chars[length] = 0;
vm->consts[i] = (fzl_const_t){
.type = type, .as.string.length = length, .as.string.chars = chars};
}
else
{
fclose(fd);
free(vm->consts);
free(vm);
snprintf(last_err, sizeof(last_err), "unknown const type at index %ld",
i);
return NULL;
}
}
}
vm->ops_count = ops_count;
vm->ops = malloc(ops_count * sizeof(uint64_t));
for (size_t i = 0; i < ops_count; i++)
{
uint64_t op;
fread(&op, sizeof(uint64_t), 1, fd);
vm->ops[i] = op;
}
// TODO: read externs
fclose(fd);
vm->reader = reader;
return vm;
}
@@ -229,9 +157,9 @@ int fzl_vm_run(fzl_vm_t *vm)
vm->ip = 0;
int result = 0;
while (vm->ip < vm->ops_count)
while (vm->ip < vm->reader->ops_count)
{
fzl_opcode_t opcode = vm->ops[vm->ip];
fzl_opcode_t opcode = vm->reader->ops[vm->ip];
size_t op_group = (opcode >> 16) & 0xFF;
size_t op_fnnum = (opcode & 0xFF);
@@ -265,13 +193,5 @@ void fzl_freevm(fzl_vm_t *vm)
while (vm->etop > 0)
fzl_freeenv(vm->estack[--vm->etop]);
for (size_t i = 0; i < vm->consts_count; i++)
{
if (vm->consts[i].type == FZL_CONSTTYPE_STRING)
free((void *)vm->consts[i].as.string.chars);
}
free(vm->consts);
free(vm->ops);
free(vm);
}