feat: implement extern libs data writing
This commit is contained in:
+50
-23
@@ -1,6 +1,8 @@
|
||||
#include <compiler/fzl_gen.h>
|
||||
#include <compiler/fzl_writer.h>
|
||||
#include <compiler/internals/writer.h>
|
||||
#include <compiler/utils/fzl_const_pool.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
@@ -28,41 +30,66 @@ int fzl_write(fzl_writer_t *writer, fzl_gen_t *gen)
|
||||
if (!writer || !gen)
|
||||
return -1;
|
||||
|
||||
FILE *fp = fopen(writer->filepath, "wb");
|
||||
if (!fp)
|
||||
FILE *fd = fopen(writer->filepath, "wb");
|
||||
if (!fd)
|
||||
{
|
||||
snprintf(wrlast_err, sizeof(wrlast_err), "failed to open '%s' to write",
|
||||
writer->filepath);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char hdr[5] = {'F', 'Z', 'L', 0x0, 0x1};
|
||||
fwrite(hdr, sizeof(char), 5, fp);
|
||||
fwrite(&gen->consts->size, sizeof(size_t), 1, fp);
|
||||
fwrite(&gen->extern_libs_count, sizeof(size_t), 1, fp);
|
||||
fwrite(&gen->extern_types_count, sizeof(size_t), 1, fp);
|
||||
fwrite(&gen->extern_fns_count, sizeof(size_t), 1, fp);
|
||||
fwrite(&gen->ops_count, sizeof(size_t), 1, fp);
|
||||
const char hdr[5] = {'F', 'Z', 'L', 0x0, 0x2};
|
||||
fwrite(hdr, sizeof(char), 5, fd);
|
||||
fwrite(&gen->consts->size, sizeof(size_t), 1, fd);
|
||||
fwrite(&gen->extern_libs_count, sizeof(size_t), 1, fd);
|
||||
fwrite(&gen->extern_types_count, sizeof(size_t), 1, fd);
|
||||
fwrite(&gen->extern_fns_count, sizeof(size_t), 1, fd);
|
||||
fwrite(&gen->ops_count, sizeof(size_t), 1, fd);
|
||||
|
||||
for (size_t i = 0; i < gen->consts->size; i++)
|
||||
bool result = wr_consts(fd, gen->consts);
|
||||
if (!result)
|
||||
{
|
||||
fzl_const_t c = gen->consts->array[i];
|
||||
fwrite(&c.type, sizeof(fzl_consttype_t), 1, fp);
|
||||
|
||||
if (c.type == FZL_CONSTTYPE_STRING)
|
||||
{
|
||||
size_t strlen = c.as.string.length;
|
||||
fwrite(&c.as.string.length, sizeof(size_t), 1, fp);
|
||||
fwrite(c.as.string.chars, sizeof(char), strlen, fp);
|
||||
}
|
||||
snprintf(wrlast_err, sizeof(wrlast_err), "failed to write constants table");
|
||||
fclose(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// TODO: write libs, types and functions
|
||||
result = wr_extern_libs(fd, gen->extern_libs, gen->extern_libs_count);
|
||||
if (!result)
|
||||
{
|
||||
snprintf(wrlast_err, sizeof(wrlast_err),
|
||||
"failed to write external lib names");
|
||||
fclose(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (gen->ops_count > 0)
|
||||
fwrite(gen->ops, sizeof(uint64_t), gen->ops_count, fp);
|
||||
result = wr_extern_types(fd, gen->extern_types, gen->extern_types_count);
|
||||
if (!result)
|
||||
{
|
||||
snprintf(wrlast_err, sizeof(wrlast_err),
|
||||
"failed to write extern lib types");
|
||||
fclose(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
result = wr_extern_fns(fd, gen->extern_fns, gen->extern_fns_count);
|
||||
if (!result)
|
||||
{
|
||||
snprintf(wrlast_err, sizeof(wrlast_err),
|
||||
"failed to write extern lib functions");
|
||||
fclose(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
result = wr_ops(fd, gen->ops, gen->ops_count);
|
||||
if (!result)
|
||||
{
|
||||
snprintf(wrlast_err, sizeof(wrlast_err), "failed to write opcodes");
|
||||
fclose(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fclose(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user