diff --git a/byterun/include/utils.h b/byterun/include/utils.h index bb951075d..bb9eb6cb1 100644 --- a/byterun/include/utils.h +++ b/byterun/include/utils.h @@ -9,12 +9,13 @@ /* 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 main_offset; /* offset of the function 'main' */ + 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 imports_number; /* The number of imports */ diff --git a/byterun/regression/Dep.i b/byterun/regression/Dep.i new file mode 100644 index 000000000..e0493b13d --- /dev/null +++ b/byterun/regression/Dep.i @@ -0,0 +1,2 @@ +I,Std; +F,f; diff --git a/byterun/regression/Dep.lama b/byterun/regression/Dep.lama new file mode 100644 index 000000000..22e955ccd --- /dev/null +++ b/byterun/regression/Dep.lama @@ -0,0 +1 @@ +public fun f(a, b) { a + b } diff --git a/byterun/regression/Test.lama b/byterun/regression/Test.lama new file mode 100644 index 000000000..3312c90f9 --- /dev/null +++ b/byterun/regression/Test.lama @@ -0,0 +1,3 @@ +import Dep; + +f(1, 2) diff --git a/byterun/src/module_manager.cpp b/byterun/src/module_manager.cpp index f3ee5a88a..2967ddc11 100644 --- a/byterun/src/module_manager.cpp +++ b/byterun/src/module_manager.cpp @@ -1,3 +1,4 @@ +#include extern "C" { #include "module_manager.h" #include "utils.h" @@ -33,16 +34,23 @@ static ModuleManager manager; uint32_t mod_add_impl(Bytefile *bf, bool do_verification, std::optional name = std::nullopt) { +#ifdef DEBUG_VERSION + std::cerr << "- add module (impl) '" << std::string{name ? *name : ""} + << "'\n"; +#endif uint32_t id = manager.modules.size(); manager.modules.push_back({.name = name ? *name : "", .bf = bf}); for (size_t i = 0; i < bf->public_symbols_number; ++i) { - if (!manager.public_symbols_mods - .insert({ - get_public_name_safe(bf, i), - {.mod_id = id, .offset = get_public_offset_safe(bf, i)}, - }) - .second) { - failure("public symbol loaded more then once\n"); + const char *public_name = get_public_name_safe(bf, i); + size_t public_offset = get_public_offset_safe(bf, i); + if (strcmp(public_name, "main") == 0) { + bf->main_offset = public_offset; + } else if (!manager.public_symbols_mods + .insert( + {public_name, {.mod_id = id, .offset = public_offset}}) + .second) { + failure("public symbol '%s' loaded more then once\n", + get_public_name_safe(bf, i)); } } 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, bool do_verification) { +#ifdef DEBUG_VERSION + std::cerr << "- module path load '" << name << "'\n"; +#endif Bytefile *module = read_file(path.c_str()); - return mod_add_impl(module, name); + return mod_add_impl(module, do_verification, name); } 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) { +#ifdef DEBUG_VERSION + std::cerr << "- add module, no name\n"; +#endif return mod_add_impl(module, do_verification); } diff --git a/byterun/src/parser.cpp b/byterun/src/parser.cpp index 67ebf13be..858a1b430 100644 --- a/byterun/src/parser.cpp +++ b/byterun/src/parser.cpp @@ -52,7 +52,15 @@ Bytefile *read_file(const char *fname) { } 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 + additional_size); // file itself + additional data diff --git a/byterun/src/types.c b/byterun/src/types.c index 16e379ef5..0337d6c07 100644 --- a/byterun/src/types.c +++ b/byterun/src/types.c @@ -54,8 +54,8 @@ void init_mod_state(uint mod_id, struct State* s) { s->fp = NULL; - s->ip = s->bf->code_ptr; - s->instr_ip = s->bf->code_ptr; + s->ip = s->bf->code_ptr + s->bf->main_offset; + s->instr_ip = s->ip; #ifdef DEBUG_VERSION print_stack(s);