wip: working on extern in VM

- Feat: add writer externs writing fns
- Feat: add VM reader functions
This commit is contained in:
samsonium
2026-11-05 21:48:40 +03:00
parent 84572edb0d
commit 1db42c1e8e
17 changed files with 608 additions and 16 deletions
+32
View File
@@ -0,0 +1,32 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <vm/internals/reader.h>
// Read characters with red bytes check
bool vm_rd_char(FILE *fd, size_t count, char *o)
{
if (!fd || !o)
return false;
return fread(o, sizeof(char), count, fd) == (sizeof(char) * count);
}
// Read 16-byte uints with red bytes check
bool vm_rd_u16(FILE *fd, size_t count, uint16_t *o)
{
if (!fd || !o)
return false;
return fread(o, sizeof(uint16_t), count, fd) == (sizeof(uint16_t) * count);
}
// Read machine word-size uints with red bytes check
bool vm_rd_word(FILE *fd, size_t count, size_t *o)
{
if (!fd || !o)
return false;
return fread(o, sizeof(size_t), count, fd) == (sizeof(size_t) * count);
}
+52
View File
@@ -0,0 +1,52 @@
#include <common/fzl_types.h>
#include <common/fzl_value.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <vm/internals/reader.h>
// Read constants table
bool vm_rd_consts(fzl_vm_reader_t *reader, FILE *fd)
{
if (!reader || !fd)
return false;
size_t consts_count = reader->consts_count;
if (!consts_count)
return true;
reader->consts = malloc(sizeof(fzl_value_t) * consts_count);
if (!reader->consts)
return false;
memset(reader->consts, 0, sizeof(fzl_value_t) * consts_count);
bool ok = false;
uint16_t const_size;
char contents[512];
for (uint16_t i = 0; i < reader->consts_count; i++)
{
memset(contents, 0, sizeof(contents));
ok = vm_rd_u16(fd, 1, &const_size);
if (!ok || const_size < 1)
return false;
ok = vm_rd_char(fd, const_size, contents);
if (!ok)
return false;
char *val = malloc(sizeof(char) * const_size);
if (!val)
return false;
memset(val, 0, sizeof(char) * const_size);
memcpy(val, contents, sizeof(char) * const_size);
reader->consts[i] = (fzl_value_t){
.type = FZL_TYPE_STRING, .as.s.value = val, .as.s.length = const_size};
}
return true;
}
+20
View File
@@ -0,0 +1,20 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <vm/fzl_vm_reader.h>
#include <vm/internals/reader.h>
// Read external functions definitions
bool vm_rd_extern_fns(fzl_vm_reader_t *reader, FILE *fd)
{
if (!reader || !fd)
return false;
uint16_t fns_count = reader->extern_fns_count;
if (!fns_count)
return true;
// TODO: fuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuck
return true;
}
+49
View File
@@ -0,0 +1,49 @@
#include <common/fzl_extern.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <vm/internals/reader.h>
// Read external lib names
bool vm_rd_extern_libs(fzl_vm_reader_t *reader, FILE *fd)
{
if (!reader || !fd)
return false;
uint16_t libs_count = reader->extern_libs_count;
if (!libs_count)
return true;
reader->extern_libs = malloc(sizeof(char *) * libs_count);
if (!reader->extern_libs)
return false;
memset(reader->extern_libs, 0, sizeof(char *) * libs_count);
bool ok = false;
uint16_t name_sz = 0;
char name[256];
for (uint16_t i = 0; i < libs_count; i++)
{
memset(name, 0, sizeof(name));
ok = vm_rd_u16(fd, 1, &name_sz);
if (!ok || name_sz < 1)
return false;
ok = vm_rd_char(fd, name_sz, name);
if (!ok)
return false;
char *val = malloc(sizeof(char) * name_sz);
if (!val)
return false;
memset(val, 0, sizeof(char) * name_sz);
memcpy(val, name, sizeof(char) * name_sz);
reader->extern_libs[i] = val;
}
return true;
}
+156
View File
@@ -0,0 +1,156 @@
#include <common/fzl_extern.h>
#include <common/fzl_types.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vm/fzl_vm_reader.h>
#include <vm/internals/reader.h>
// Read external types
bool vm_rd_extern_types(fzl_vm_reader_t *reader, FILE *fd)
{
if (!reader || !fd)
return false;
uint16_t types_count = reader->extern_types_count;
if (!types_count)
return true;
reader->extern_types = malloc(sizeof(fzl_extern_type_t) * types_count);
if (!reader->extern_types)
return false;
memset(reader->extern_types, 0, sizeof(fzl_extern_type_t) * types_count);
char namebuf[256];
for (uint16_t i = 0; i < types_count; i++)
{
fzl_extern_type_t *type = &reader->extern_types[i];
uint16_t lib_slot;
if (!vm_rd_u16(fd, 1, &lib_slot))
return false;
type->lib_slot = lib_slot;
char kind;
if (!vm_rd_char(fd, 1, &kind))
return false;
type->kind = (fzl_extern_kind_t)kind;
uint16_t name_sz;
if (!vm_rd_u16(fd, 1, &name_sz) || name_sz < 1)
return false;
if (!vm_rd_char(fd, name_sz, namebuf))
return false;
char *val = malloc(sizeof(char) * name_sz);
if (!val)
return false;
type->name = val;
memset(val, 0, sizeof(char) * name_sz);
memcpy(val, namebuf, sizeof(char) * name_sz);
if (type->kind == FZL_EXTERN_KIND_HANDLE)
continue;
if (type->kind == FZL_EXTERN_KIND_ALIAS ||
type->kind == FZL_EXTERN_KIND_REF)
{
fzl_extern_type_ref_t *typeref = type->kind == FZL_EXTERN_KIND_ALIAS
? &type->alias_target
: &type->ref_target;
char is_primitive;
if (!vm_rd_char(fd, 1, &is_primitive))
return false;
typeref->is_primitive = (bool)is_primitive;
if ((bool)is_primitive)
{
char primitive;
if (!vm_rd_char(fd, 1, &primitive))
return false;
typeref->primitive = (fzl_type_t)primitive;
}
else
{
uint16_t slot;
if (!vm_rd_u16(fd, 1, &slot))
return false;
typeref->type_slot = slot;
}
continue;
}
if (type->kind != FZL_EXTERN_KIND_STRUCT)
return false;
uint16_t fields_count;
if (!vm_rd_u16(fd, 1, &fields_count))
return false;
fzl_extern_struct_field_t *fields =
malloc(sizeof(fzl_extern_struct_field_t) * fields_count);
if (!fields)
return false;
memset(fields, 0, sizeof(fzl_extern_struct_field_t) * fields_count);
type->structure.fields = fields;
type->structure.fields_count = fields_count;
for (uint16_t j = 0; j < fields_count; j++)
{
fzl_extern_struct_field_t *f = &fields[i];
char has_name;
if (!vm_rd_char(fd, 1, &has_name))
return false;
if (has_name)
{
memset(namebuf, 0, sizeof(namebuf));
if (!vm_rd_u16(fd, 1, &name_sz))
return false;
if (!vm_rd_char(fd, name_sz, namebuf))
return false;
char *fval = malloc(sizeof(char) * name_sz);
if (!fval)
return false;
memset(fval, 0, sizeof(char) * name_sz);
memcpy(fval, namebuf, sizeof(char) * name_sz);
f->name = fval;
}
char is_primitive;
if (!vm_rd_char(fd, 1, &is_primitive))
return false;
f->type.is_primitive = (bool)is_primitive;
if (f->type.is_primitive)
{
char ftype;
if (!vm_rd_char(fd, 1, &ftype))
return false;
f->type.primitive = (fzl_type_t)ftype;
}
else
{
uint16_t fslot;
if (!vm_rd_u16(fd, 1, &fslot))
return false;
f->type.type_slot = fslot;
}
}
}
return true;
}
+24
View File
@@ -0,0 +1,24 @@
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <vm/internals/reader.h>
size_t hdr_size = 5;
char hdr[5] = {'F', 'Z', 'L', 0, 2};
// Reader file header
bool vm_rd_header(fzl_vm_reader_t *reader, FILE *fd)
{
if (!reader || !fd)
return false;
char chkbuf[5];
bool ok = vm_rd_char(fd, 5, chkbuf);
if (!ok)
return false;
if (memcmp(chkbuf, hdr, hdr_size) != 0)
return false;
return true;
}
+29
View File
@@ -0,0 +1,29 @@
#include <stdbool.h>
#include <stdint.h>
#include <vm/internals/reader.h>
// Reader sizes section
bool vm_rd_sections_sz(fzl_vm_reader_t *reader, FILE *fd)
{
if (!reader || !fd)
return false;
bool ok = vm_rd_u16(fd, 1, (uint16_t *)&reader->consts_count);
if (!ok)
return false;
ok = vm_rd_u16(fd, 1, &reader->extern_libs_count);
if (!ok)
return false;
ok = vm_rd_u16(fd, 1, &reader->extern_types_count);
if (!ok)
return false;
ok = vm_rd_u16(fd, 1, &reader->extern_fns_count);
if (!ok)
return false;
ok = vm_rd_word(fd, 1, &reader->ops_count);
return ok;
}