From 6096e5683bb597b58d1b3c5271222fbfd99058e9 Mon Sep 17 00:00:00 2001 From: Maxim Volgin Date: Fri, 5 May 2023 16:11:15 +0300 Subject: [PATCH] Initial commit --- .cargo/config.toml | 2 + .gitignore | 2 + Cargo.lock | 287 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 9 ++ src/codegen/code_gen.rs | 29 ++++ src/codegen/mod.rs | 5 + src/codegen/sum_func.rs | 27 ++++ src/main.rs | 28 ++++ 8 files changed, 389 insertions(+) create mode 100644 .cargo/config.toml create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/codegen/code_gen.rs create mode 100644 src/codegen/mod.rs create mode 100644 src/codegen/sum_func.rs create mode 100644 src/main.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..346fb98 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[env] +LLVM_SYS_150_PREFIX="/media/maxvog2020/main_files/LLVM/llvm-15.0.0" \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3df278e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +.vscode diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d22f106 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,287 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "either" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" + +[[package]] +name = "inkwell" +version = "0.1.0" +source = "git+https://github.com/TheDan64/inkwell?branch=master#468320973ec40c237ad34e266a680a875605aa3a" +dependencies = [ + "either", + "inkwell_internals", + "libc", + "llvm-sys", + "once_cell", + "parking_lot", +] + +[[package]] +name = "inkwell_internals" +version = "0.7.0" +source = "git+https://github.com/TheDan64/inkwell?branch=master#468320973ec40c237ad34e266a680a875605aa3a" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.139" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" + +[[package]] +name = "llvm-sys" +version = "150.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64be8a29d08e3165e4ed989b80cbc6b52104c543f16e272b83e2dedb749e81e6" +dependencies = [ + "cc", + "lazy_static", + "libc", + "regex", + "semver", +] + +[[package]] +name = "llvm_sys_test" +version = "0.1.0" +dependencies = [ + "inkwell", +] + +[[package]] +name = "lock_api" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "once_cell" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" + +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", +] + +[[package]] +name = "proc-macro2" +version = "1.0.51" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "semver" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58bc9567378fc7690d6b2addae4e60ac2eeea07becb2c64b9f218b53865cba2a" + +[[package]] +name = "smallvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" + +[[package]] +name = "syn" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..187c72c --- /dev/null +++ b/Cargo.toml @@ -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"] } \ No newline at end of file diff --git a/src/codegen/code_gen.rs b/src/codegen/code_gen.rs new file mode 100644 index 0000000..a7722e8 --- /dev/null +++ b/src/codegen/code_gen.rs @@ -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, + } + } +} + diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs new file mode 100644 index 0000000..fe2d9e6 --- /dev/null +++ b/src/codegen/mod.rs @@ -0,0 +1,5 @@ +mod code_gen; +mod sum_func; + +pub use code_gen::CodeGen; +pub use sum_func::SumFunc; \ No newline at end of file diff --git a/src/codegen/sum_func.rs b/src/codegen/sum_func.rs new file mode 100644 index 0000000..a8e316b --- /dev/null +++ b/src/codegen/sum_func.rs @@ -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> { + 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() } + } +} + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1d19fe6 --- /dev/null +++ b/src/main.rs @@ -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 = codegen.jit_compile_sum().unwrap(); + + let (x, y, z) = (1, 2, 3); + + unsafe { + assert_eq!(sum.call(x, y, z), x + y + z); + } +}