llama

Crates.iollama
lib.rsllama
version0.14.2
sourcesrc
created_at2019-07-24 00:12:29.403401
updated_at2022-05-21 17:27:28.63549
descriptionFriendly LLVM bindings
homepage
repositoryhttps://github.com/zshipko/llama
max_upload_size
id151184
size172,863
zach (zshipko)

documentation

https://zshipko.github.io/llama/llama

README

llama

A friendly LLVM library for Rust.

Goals:

  • Support the latest llvm-sys release (as of LLVM 14 and llama 0.14.0 the version numbers match)
  • Provide an improved interface, while still remaining as close as possible to the LLVM C API.

Due to the size of the LLVM API there is bound to be missing, broken or incomplete functionality in llama, please create an issue if something you need isn't implemented.

NOTE: llama will let you generate invalid IR, take a look at inkwell for LLVM bindings with a focus on type-safety

Documentation

Examples

Inkwell's example using llama:

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(())
}
Commit count: 159

cargo fmt