Expand description
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
}
Modules§
- c_api
- Provides functions for using the C API
- iter
- Provides iterators for various items
- target
- Provides wrappers for target-related operations
- types
- Provides functions that return LLVM types in the global context
Structs§
- Basic
Block - A wrapper around a
LLVMBasicBlockRef
- Builder
- A wrapper around a
LLVMBuilderRef
for a specific context - Module
- A wrapper around a
LLVMModuleRef
for a specific context - Type
- A wrapper around a
LLVMTypeRef
for a specific context - Value
- A wrapper around a
LLVMValueRef
for a specific context
Enums§
Functions§
- const_
string - A constant string with the given value
- const_
struct - A constant struct with the given elements
- create_
builder - Creates a new builder in this context
- create_
module - Creates a new module with the given name in this context