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

Macros

Add symbols to the global namespace

Structs

LLVM Attribute

BasicBlock wraps LLVMBasicBlock

Binary is used to store compiled binary objects

A Builder is used to create Instructions

Platform-specific machine code

Constant values

Context wraps LLVMContext

An execution engine can be used to execute JIT compiled code

Functions

PassManager for function optimizations

Function type

Instruction value

Alloca instruction

Call instruction

Fcmp instruction

GEP instruction

Icmp instruction

IndirectBr instruction

Phi instruction

Switch instruction

Jit bundles LLVMContext, LLVMBuilder and LLVMExecutionEngine

Memory buffer wraps LLVMMemoryBufferRef

Wraps LLVM messages, these are strings that should be freed using LLVMDisposeMessage

Metadata values

Wraps LLVMModule

PassManager for module optimizations

Struct type

LLVMTarget wrapper

LLVMTargetData wrapper

Information about the target machine

LLVMType wrapper

LLVM Value wrapper

Enums

Traits

Allows for llama types to be converted into LLVM pointers

PassManager trait is used to define common functionality between the two types of PassManagers

Functions

Add a symbol

Get the default target triple

Load a shared library

Type Definitions

An optimization pass

Enumerates all possible kinds of types

An enumeration of possible Value kinds