-
Notifications
You must be signed in to change notification settings - Fork 1
Description
The virtual machine loads and resolves external variables once the bytecode is fully loaded. It ensures that all symbols exists before executing the program and avoids a string lookup at runtime to determine the effective position of the value in memory.
Functions on the other hand are not resolved ahead of time, despite being known at load time.
Representing functions as dynamic symbols would be beneficial to know before executing if all the required functions to be present are found. It would also mean a direct access to the function code for the VM, without having to look for a string in a map.
Doing this change requires the compiler to also emits dynamic symbols for function calls. You can find this given code here. The VM msh::loader class will then need to be updated to account for those new symbols that may not be variable. The msh::pager class will also require an update to store those function declaration pointers behind each constant pool, and have a way to retrieve them from the interpreter.
Relevant files:
- Exported variables names are inserted in the dynsym list, as an example:
moshell/compiler/src/emit/identifier.rs
Line 67 in 4d5994a
Identifier::External(cp.insert_dynsym(&import.to_string(), name)) - Symbols are resolved by the
msh::loaderclass, that will need to account for functions:moshell/vm/src/definitions/loader.cpp
Line 84 in 4d5994a
void loader::resolve_all(pager &pager) { - Wrap all the different symbols types in a
std::variant:moshell/vm/src/definitions/pager.h
Line 13 in 4d5994a
using dynsym = std::variant<void *, function_definition *>; - Avoid using the string name to look for the function by using the dynamic symbol id:
moshell/vm/src/interpreter.cpp
Line 242 in 4d5994a
auto callee_def_it = state.loader.find_function(callee_identifier);