5 releases
Uses old Rust 2015
0.2.5 | Mar 21, 2018 |
---|---|
0.2.4 | Mar 21, 2018 |
0.2.3 | Mar 16, 2018 |
0.2.2 | Mar 13, 2018 |
0.2.1 | Mar 11, 2018 |
#76 in #wrap
68KB
1.5K
SLoC
A safer wrapper for the LLVM C API bindings in Rust, based on llvm-sys
Notes on Safety
Although there are no unsafe
blocks needed to use this library, it does ignore some of Rust's
constraints on memory management. Mainly, immutable references can still be modified in order
to make certain programs more simple.
To make things simple, LLVMBuilder
and LLVMModule
are disposed automatically when they leave
scope. Values and types are always created in the default global context provided by LLVM.
If necessary, it is possible to use the inner
function to access the wrapped value, or
into_inner
to destroy the wrapper without disposing the contained value. Most types can also
dereference into their C equivalents. This crate is still in development and many features are
not fully supported, so use this if you need to use unsupported LLVM functions. Use into_c
and from_c
to make strings easier to work with when dealing directly with the C API.
Setup
This crate relies heavily on llvm-sys
and requires the same setup. Before using this crate,
be sure to install a version of LLVM 5 and add LLVM_SYS_50_PREFIX=/path/to/llvm
to your PATH
environment variable.
LLVM Documentation | LLVM Language Reference | Rust Bindings
Example
// Create a module
let module = create_module("add");
// Create a builder
let builder = create_builder();
// Get an `i32 (i32, i32)` type
let ty = ty_i32().function(vec![ty_i32(); 2], false);
// Create the add function
let def = module.add_function("add", ty);
// Add an entry block
let entry = def.append_basic_block("entry");
// Move the builder to the end of the block
builder.position_at_end(entry);
// Add and name the two parameters
let result = builder.build_int_add(
def.param(0).name("a"),
def.param(1).name("b"),
);
// Return and name the result
builder.build_ret(result.name("tmp"));
// Dump the contents of the module
module.dump();
Output
; ModuleID = 'add'
source_filename = "add"
define i32 @add(i32 %a, i32 %b) {
entry:
%tmp = add i32 %a, %b
ret i32 %tmp
}
Dependencies
~0.3–1MB
~21K SLoC