A rusty safe wrapper for llvm-sys with a focus on achieving higher safety and usability than inkwell.
[dependencies]
llvm = { git = "https://github.com/shqld/llvm.git" }
See examples for more details.
use llvm::{Context, ExecutionEngine};
let context = Context::new();
let module = context
.create_module("example")
.with_source_file_name("example.ts");
let function = module
.create_function("main", context.i32_ty(), &[], false)
.build(|builder| {
builder.build_ret(&context.const_i32(42));
Ok(())
})
.unwrap();
assert_eq!(
module.to_string().trim(),
r#"
; ModuleID = 'example'
source_filename = "example.ts"
define i32 @main() {
ret i32 42
}
"#.trim()
);
let engine = ExecutionEngine::new_mc_jit_compiler(module).unwrap();
let result = unsafe { engine.run_main(&[], &[]).unwrap() };
assert_eq!(result, 42);