Crate llama

Source
Expand description

llama is a friendly LLVM wrapper

§Getting started

use llama::*;

// Convenience type alias for the `sum` function.
//
// Calling this is innately `unsafe` because there's no guarantee it doesn't
// do `unsafe` operations internally.
type SumFunc = unsafe extern "C" fn(u64, u64, u64) -> u64;

fn compile_sum(jit: &mut Jit) -> Result<SumFunc, Error> {
    let i64 = Type::i64(jit.context())?;
    let sum_t = FuncType::new(i64, [i64, i64, i64])?;
    jit.declare_function("sum", sum_t, |build, f| {
        let params = f.params();
        let x = params[0];
        let y = params[1];
        let z = params[2];

        let sum = build.add(x, y, "sum")?;
        let sum = build.add(sum, z, "sum")?;
        build.ret(sum)
    })?;

    unsafe { jit.engine().function("sum") }
}

fn main() -> Result<(), Error> {
    let mut jit = Jit::new("sum", None)?;

    let sum = compile_sum(&mut jit)?;

    let x = 1u64;
    let y = 2u64;
    let z = 3u64;

    unsafe {
        println!("{} + {} + {} = {}", x, y, z, sum(x, y, z));
        assert_eq!(sum(x, y, z), x + y + z);
    }

    Ok(())
}

Re-exports§

pub use llvm_sys as llvm;

Modules§

transforms

Macros§

symbol
Add symbols to the global namespace

Structs§

Attribute
LLVM Attribute
BasicBlock
BasicBlock wraps LLVMBasicBlock
Binary
Binary is used to store compiled binary objects
Builder
A Builder is used to create Instructions
Codegen
Platform-specific machine code
Const
Constant values
Context
Context wraps LLVMContext
ExecutionEngine
An execution engine can be used to execute JIT compiled code
Func
Functions
FuncPassManager
PassManager for function optimizations
FuncType
Function type
Instr
Instruction value
InstrAlloca
Alloca instruction
InstrCall
Call instruction
InstrFcmp
Fcmp instruction
InstrGep
GEP instruction
InstrIcmp
Icmp instruction
InstrIndirectBr
IndirectBr instruction
InstrPhi
Phi instruction
InstrSwitch
Switch instruction
Jit
Jit bundles LLVMContext, LLVMBuilder and LLVMExecutionEngine
MemoryBuffer
Memory buffer wraps LLVMMemoryBufferRef
Message
Wraps LLVM messages, these are strings that should be freed using LLVMDisposeMessage
Metadata
Metadata values
Module
Wraps LLVMModule
ModulePassManager
PassManager for module optimizations
StructType
Struct type
Target
LLVMTarget wrapper
TargetData
LLVMTargetData wrapper
TargetMachine
Information about the target machine
Type
LLVMType wrapper
Value
LLVM Value wrapper

Enums§

AtomicOrdering
AtomicRMWBinOp
AttributeIndex
Attribute placement index
BinaryType
ByteOrder
CallConv
CodeGenOptLevel
CodeModel
DiagnosticSeverity
Error
An enumeration of all possible errors
Fcmp
Icmp
InlineAsmDialect
Linkage
ModuleFlagBehavior
OpCode
RelocMode
ThreadLocalMode
UnnamedAddr
Visibility

Traits§

LLVM
Allows for llama types to be converted into LLVM pointers
PassManager
PassManager trait is used to define common functionality between the two types of PassManagers

Functions§

add_symbol
Add a symbol
default_target_triple
Get the default target triple
load_library
Load a shared library

Type Aliases§

Transform
An optimization pass
TypeKind
Enumerates all possible kinds of types
ValueKind
An enumeration of possible Value kinds