bytecode compiler impirsts and external functions call support, byyterun imports and external functions calls parsing, initial impl of module manager

This commit is contained in:
ProgramSnail 2024-11-29 13:45:03 +03:00
parent 2cd7afe0c5
commit 0a26953318
11 changed files with 409 additions and 205 deletions

View file

@ -0,0 +1,19 @@
#pragma once
#include <stdint.h>
#include "utils.h"
struct ModSearchResult {
ssize_t symbol_offset; // < 0 => not found
uint32_t mod_id;
bytefile *mod_file;
};
void mod_add_search_path(const char *path);
bytefile *mod_get(uint32_t id);
int32_t mod_load(const char *name); // < 0 => not found
struct ModSearchResult mod_search_pub_symbol(const char *name);

View file

@ -19,8 +19,8 @@
DEF(CMD_BINOP_AND, &&) \
DEF(CMD_BINOP_OR, ||)
const char *read_cmd(char *ip);
const char *read_cmd(const char *ip);
bytefile *read_file(char *fname);
bytefile *read_file(const char *fname);
// void dump_file(FILE *f, bytefile *bf);
void dump_file(FILE *f, bytefile *bf);

View file

@ -146,6 +146,7 @@ enum CMD_CTRLS {
CMD_CTRL_ARRAY,
CMD_CTRL_FAIL,
CMD_CTRL_LINE,
CMD_CTRL_CALLF,
};
enum CMD_PATTS {

View file

@ -10,13 +10,15 @@
/* The unpacked representation of bytecode file */
typedef struct {
char *string_ptr; /* A pointer to the beginning of the string table */
int *imports_ptr; /* A pointer to the beginning of imports table */
int *public_ptr; /* A pointer to the beginning of publics table */
char *code_ptr; /* A pointer to the bytecode itself */
int *global_ptr; /* A pointer to the global area */
int code_size; /* The size (in bytes) of code */
uint stringtab_size; /* The size (in bytes) of the string table */
uint global_area_size; /* The size (in words) of global area */
uint public_symbols_number; /* The number of public symbols */
uint imports_number; /* The number of imports */
uint public_symbols_number; /* The number of public symbols */
char buffer[0];
} bytefile;
@ -29,15 +31,23 @@ static inline void exec_failure(const char *cmd, int line, aint offset,
/* Gets a string from a string table by an index */
static inline const char *get_string(const bytefile *f, size_t pos) {
if (pos >= f->stringtab_size) {
failure("strinpg pos is out of range: %zu >= %i\n", pos, f->stringtab_size);
failure("string pos is out of range: %zu >= %i\n", pos, f->stringtab_size);
}
return &f->string_ptr[pos];
}
/* Gets import */
static inline const char *get_import(const bytefile *f, size_t i) {
if (i >= f->imports_number) {
failure("import pos is out of range: %zu >= %i\n", i, f->imports_number);
}
return get_string(f, f->imports_ptr[i]);
}
/* Gets a name for a public symbol */
static inline const char *get_public_name(const bytefile *f, size_t i) {
if (i >= f->public_symbols_number) {
failure("public number is out of range: %zu >= %i\n", i * 2,
failure("public number is out of range: %zu >= %i\n", i,
f->public_symbols_number);
}
return get_string(f, f->public_ptr[i * 2]);
@ -45,8 +55,8 @@ static inline const char *get_public_name(const bytefile *f, size_t i) {
/* Gets an offset for a publie symbol */
static inline size_t get_public_offset(const bytefile *f, size_t i) {
if (i + 1 >= f->public_symbols_number) {
failure("public number is out of range: %zu >= %i\n", i * 2 + 1,
if (i >= f->public_symbols_number) {
failure("public number is out of range: %zu >= %i\n", i,
f->public_symbols_number);
}
return f->public_ptr[i * 2 + 1];