🔦 Rust library designed for the creation and manipulation of BEAM (Erlang VM) bytecode
MIT License
A Rust library designed for the creation and manipulation of BEAM (Erlang VM) bytecode
The beam_sys
project is a personal exploration and it try to provide an intuitive API to generate .beam
files, abstracting the intricacies of lower-level details. This project is inspired by the Tsoding bada project and incorporates ideas from the llvm-sys
and cranelift
crates.
beam_sys::context::Context
) for enhanced code generation.To create a .beam
file:
use beam_sys;
fn main() {
let module = beam_sys::context::Context::create("sys".to_string());
module.add_basic_bif();
let code = module.code();
let mut function_block = code.build_function_block("empty".to_string(), 0);
{
function_block.build_return();
}
let function_metadata = function_block.function_metadata();
module.export_table_mut().export_function(function_metadata);
std::fs::write("sys.beam", module.encode()).unwrap();
}
Verify with Erlang shell:
1> beam_disasm:file(sys).
{beam_file,sys,
[{'-',0,2}],
[],[],
[{function,empty,0,2,
[{label,1},
{line,1},
{func_info,{atom,sys},{atom,empty},0},
{label,2},
return]}]}
Demonstrating the shared context's reusability across different functions:
use beam_sys;
fn main() {
initialize_module();
add_function();
// ... more operations
}
fn initialize_module() {
let module = beam_sys::context::Context::create("sys".to_string());
module.add_basic_bif();
// ... additional initializations
}
fn add_function() {
let module = beam_sys::context::Context::get("sys".to_string());
let code = module.code();
let mut function_block = code.build_function_block("test_func".to_string(), 0);
{
// ... build the function
function_block.build_return();
}
let function_metadata = function_block.function_metadata();
module.export_table_mut().export_function(function_metadata);
}
In this example, the shared context is created in initialize_module
and reused in add_function
without the need to pass the context around, simplifying the API usage.
beam_sys
is in active development. The API is evolving and may undergo significant changes.
Contributions are welcome! Feel free to submit pull requests or open issues to propose features or discuss bugs.
This project is under the MIT License.