Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maxvog2020 committed May 5, 2023
0 parents commit 6096e56
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[env]
LLVM_SYS_150_PREFIX="/media/maxvog2020/main_files/LLVM/llvm-15.0.0"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
.vscode
287 changes: 287 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "llvm_sys_test"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm15-0"] }
29 changes: 29 additions & 0 deletions src/codegen/code_gen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use inkwell::builder::Builder;
use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::execution_engine::ExecutionEngine;
use inkwell::OptimizationLevel;

pub struct CodeGen<'ctx> {
pub context: &'ctx Context,
pub module: Module<'ctx>,
pub builder: Builder<'ctx>,
pub execution_engine: ExecutionEngine<'ctx>,
}

impl<'ctx> CodeGen<'ctx> {
pub fn new(context: &'ctx Context, opt_level: OptimizationLevel) -> Self {
let module = context.create_module("sum");
let execution_engine = module
.create_jit_execution_engine(opt_level)
.unwrap();

CodeGen {
context,
module,
builder: context.create_builder(),
execution_engine,
}
}
}

5 changes: 5 additions & 0 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod code_gen;
mod sum_func;

pub use code_gen::CodeGen;
pub use sum_func::SumFunc;
27 changes: 27 additions & 0 deletions src/codegen/sum_func.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::codegen::code_gen::CodeGen;
use inkwell::execution_engine::JitFunction;

pub type SumFunc = unsafe extern "C" fn(u64, u64, u64) -> u64;

impl<'ctx> CodeGen<'ctx> {
pub fn jit_compile_sum(&self) -> Option<JitFunction<SumFunc>> {
let i64_type = self.context.i64_type();
let fn_type = i64_type.fn_type(&[i64_type.into(), i64_type.into(), i64_type.into()], false);
let function = self.module.add_function("sum", fn_type, None);
let basic_block = self.context.append_basic_block(function, "entry");

self.builder.position_at_end(basic_block);

let x = function.get_nth_param(0)?.into_int_value();
let y = function.get_nth_param(1)?.into_int_value();
let z = function.get_nth_param(2)?.into_int_value();

let sum = self.builder.build_int_add(x, y, "sum");
let sum = self.builder.build_int_add(sum, z, "sum");

self.builder.build_return(Some(&sum));

unsafe { self.execution_engine.get_function("sum").ok() }
}
}

28 changes: 28 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
mod codegen;
use codegen::CodeGen;
use codegen::SumFunc;

use inkwell::context::Context;
use inkwell::execution_engine::JitFunction;
use inkwell::OptimizationLevel;

fn main() {
let context = Context::create();
let codegen = CodeGen::new(&context, OptimizationLevel::Aggressive);
codegen.jit_compile_sum();
codegen.module.print_to_stderr();
}

#[test]
fn test_call() {
let context = Context::create();
let codegen = CodeGen::new(&context, OptimizationLevel::Aggressive);

let sum: JitFunction<SumFunc> = codegen.jit_compile_sum().unwrap();

let (x, y, z) = (1, 2, 3);

unsafe {
assert_eq!(sum.call(x, y, z), x + y + z);
}
}

0 comments on commit 6096e56

Please sign in to comment.