-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6096e56
Showing
8 changed files
with
389 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target | ||
.vscode |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |