#assembly #llvm #compiler #machine-code

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

#2137 in Development tools

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–5.5MB
~126K SLoC