[][src]Crate llama

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;

// Context should be last to perform cleanup in the correct order
struct CodeGen<'ctx> {
    engine: ExecutionEngine<'ctx>,
    build: Builder<'ctx>,
    context: Context<'ctx>,
}

impl<'ctx> CodeGen<'ctx> {
    fn jit_compile_sum(&mut self) -> Result<SumFunc, Error> {
        let i64 = Type::i64(&self.context)?;
        let sum_t = FuncType::new(i64, [i64, i64, i64])?;
        self.engine
            .module()
            .declare_function(&self.build, "sum", sum_t, |f| {
                let params = f.params();
                let x = params[0];
                let y = params[1];
                let z = params[2];

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

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

fn main() -> Result<(), Error> {
   let context = Context::new()?;
   let module = Module::new(&context, "sum")?;
   let build = Builder::new(&context)?;
   let engine = ExecutionEngine::new_jit(module, 0)?;
   let mut codegen = CodeGen {
        context: context,
        build,
        engine,
    };

    let sum = codegen.jit_compile_sum()?;

    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

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 Definitions

Transform

An optimization pass

TypeKind

Enumerates all possible kinds of types

ValueKind

An enumeration of possible Value kinds