#llvm #compiler #machinecode #assembly

hllvm

Idiomatic bindings to the LLVM compiler suite

4 releases

Uses old Rust 2015

0.1.3 Jul 2, 2017
0.1.2 Jun 25, 2017
0.1.1 Dec 21, 2016
0.1.0 Dec 20, 2016

#643 in Programming languages

Download history 1/week @ 2023-11-02 3/week @ 2023-11-09 6/week @ 2023-11-16 6/week @ 2023-11-23 19/week @ 2023-11-30 1/week @ 2023-12-07 4/week @ 2023-12-14 10/week @ 2023-12-21 2/week @ 2023-12-28 5/week @ 2024-01-04 1/week @ 2024-01-11 5/week @ 2024-01-18 5/week @ 2024-01-25 9/week @ 2024-02-01 5/week @ 2024-02-08 43/week @ 2024-02-15

64 downloads per month

MIT license

120KB
3K SLoC

LLVM bindings for Rust

Crates.io license

Documentation

A high-level and idiomatic wrapper over the C++ LLVM compiler suite.


lib.rs:

LLVM bindings for Rust

Generating an basic program

extern crate hllvm;

use hllvm::{ir, target, support};

fn build_module(context: &ir::Context) -> ir::Module {
    let mut module = ir::Module::new("mymodule", context);

    let int8 = ir::IntegerType::new(8, &context);
    let stru = ir::StructType::new(&[&int8.as_ref()], false, context);

    let func_ty = ir::FunctionType::new(&stru.as_ref(), &[], false);

    {
        let mut func = module.get_or_insert_function("my_func", &func_ty, &[]);

        let mut block = ir::Block::new(&context);
        block.append(ir::ReturnInst::new(None, context).as_ref().as_ref());

        func.append(&mut block);
    }

    module
}

fn main() {
    let context = ir::Context::new();
    let module = build_module(&context);

    module.dump();
    let stdout = support::FileOutputStream::stdout(false);

    let target = target::Registry::get().targets().find(|t| t.name() == "x86-64").expect("doesn't support X86-64");
    target::Compilation::new(&target)
        .compile(module, &stdout, target::FileType::Assembly);
}

Dependencies

~2.9–5MB
~120K SLoC