fixes + dependency test

This commit is contained in:
ProgramSnail 2025-01-12 20:17:24 +03:00
parent 19c99bc7e5
commit 72ec1923e3
7 changed files with 46 additions and 17 deletions

View file

@ -9,6 +9,7 @@
/* The unpacked representation of bytecode file */ /* The unpacked representation of bytecode file */
typedef struct { typedef struct {
uint main_offset; /* offset of the function 'main' */
char *string_ptr; /* A pointer to the beginning of the string table */ char *string_ptr; /* A pointer to the beginning of the string table */
int *imports_ptr; /* A pointer to the beginning of imports table */ int *imports_ptr; /* A pointer to the beginning of imports table */
int *public_ptr; /* A pointer to the beginning of publics table */ int *public_ptr; /* A pointer to the beginning of publics table */

2
byterun/regression/Dep.i Normal file
View file

@ -0,0 +1,2 @@
I,Std;
F,f;

View file

@ -0,0 +1 @@
public fun f(a, b) { a + b }

View file

@ -0,0 +1,3 @@
import Dep;
f(1, 2)

View file

@ -1,3 +1,4 @@
#include <iostream>
extern "C" { extern "C" {
#include "module_manager.h" #include "module_manager.h"
#include "utils.h" #include "utils.h"
@ -33,16 +34,23 @@ static ModuleManager manager;
uint32_t mod_add_impl(Bytefile *bf, bool do_verification, uint32_t mod_add_impl(Bytefile *bf, bool do_verification,
std::optional<const char *> name = std::nullopt) { std::optional<const char *> name = std::nullopt) {
#ifdef DEBUG_VERSION
std::cerr << "- add module (impl) '" << std::string{name ? *name : ""}
<< "'\n";
#endif
uint32_t id = manager.modules.size(); uint32_t id = manager.modules.size();
manager.modules.push_back({.name = name ? *name : "", .bf = bf}); manager.modules.push_back({.name = name ? *name : "", .bf = bf});
for (size_t i = 0; i < bf->public_symbols_number; ++i) { for (size_t i = 0; i < bf->public_symbols_number; ++i) {
if (!manager.public_symbols_mods const char *public_name = get_public_name_safe(bf, i);
.insert({ size_t public_offset = get_public_offset_safe(bf, i);
get_public_name_safe(bf, i), if (strcmp(public_name, "main") == 0) {
{.mod_id = id, .offset = get_public_offset_safe(bf, i)}, bf->main_offset = public_offset;
}) } else if (!manager.public_symbols_mods
.insert(
{public_name, {.mod_id = id, .offset = public_offset}})
.second) { .second) {
failure("public symbol loaded more then once\n"); failure("public symbol '%s' loaded more then once\n",
get_public_name_safe(bf, i));
} }
} }
if (name) { if (name) {
@ -56,8 +64,11 @@ uint32_t mod_add_impl(Bytefile *bf, bool do_verification,
uint32_t path_mod_load(const char *name, std::filesystem::path &&path, uint32_t path_mod_load(const char *name, std::filesystem::path &&path,
bool do_verification) { bool do_verification) {
#ifdef DEBUG_VERSION
std::cerr << "- module path load '" << name << "'\n";
#endif
Bytefile *module = read_file(path.c_str()); Bytefile *module = read_file(path.c_str());
return mod_add_impl(module, name); return mod_add_impl(module, do_verification, name);
} }
extern "C" { extern "C" {
@ -114,6 +125,9 @@ int32_t mod_load(const char *name, bool do_verification) {
} }
uint32_t mod_add(Bytefile *module, bool do_verification) { uint32_t mod_add(Bytefile *module, bool do_verification) {
#ifdef DEBUG_VERSION
std::cerr << "- add module, no name\n";
#endif
return mod_add_impl(module, do_verification); return mod_add_impl(module, do_verification);
} }

View file

@ -52,7 +52,15 @@ Bytefile *read_file(const char *fname) {
} }
long size = ftell(f); long size = ftell(f);
long additional_size = sizeof(void *) * 5 + sizeof(int);
// [uint] stringtab_size
// [uint] global_area_size
// [uint] imports_number
// [uint] public_symbols_number
// char[0] buffer
long file_header_size = 4 * sizeof(uint) + sizeof(char[0]);
long additional_size = sizeof(Bytefile) - file_header_size;
file = (Bytefile *)malloc(size + file = (Bytefile *)malloc(size +
additional_size); // file itself + additional data additional_size); // file itself + additional data

View file

@ -54,8 +54,8 @@ void init_mod_state(uint mod_id, struct State* s) {
s->fp = NULL; s->fp = NULL;
s->ip = s->bf->code_ptr; s->ip = s->bf->code_ptr + s->bf->main_offset;
s->instr_ip = s->bf->code_ptr; s->instr_ip = s->ip;
#ifdef DEBUG_VERSION #ifdef DEBUG_VERSION
print_stack(s); print_stack(s);