-
-
Save samestep/d45a6a00a4f3fbd9ac7487e555ee002c to your computer and use it in GitHub Desktop.
Cranelift JIT hello world
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
# This file is automatically @generated by Cargo. | |
# It is not intended for manual editing. | |
version = 3 | |
[[package]] | |
name = "ahash" | |
version = "0.8.11" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" | |
dependencies = [ | |
"cfg-if", | |
"once_cell", | |
"version_check", | |
"zerocopy", | |
] | |
[[package]] | |
name = "anyhow" | |
version = "1.0.86" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" | |
[[package]] | |
name = "arbitrary" | |
version = "1.3.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "7d5a26814d8dcb93b0e5a0ff3c6d80a8843bafb21b39e8e18a6f05471870e110" | |
[[package]] | |
name = "bitflags" | |
version = "1.3.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" | |
[[package]] | |
name = "bumpalo" | |
version = "3.16.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" | |
[[package]] | |
name = "cfg-if" | |
version = "1.0.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" | |
[[package]] | |
name = "cranelift" | |
version = "0.110.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "2ce7236eed5ce721638630f03e65a173fea6eecc477f652336358919cfe5bf9e" | |
dependencies = [ | |
"cranelift-codegen", | |
"cranelift-frontend", | |
"cranelift-module", | |
] | |
[[package]] | |
name = "cranelift-bforest" | |
version = "0.110.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "305d51c180ebdc46ef61bc60c54ae6512db3bc9a05842a1f1e762e45977019ab" | |
dependencies = [ | |
"cranelift-entity", | |
] | |
[[package]] | |
name = "cranelift-bitset" | |
version = "0.110.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e3247afacd9b13d620033f3190d9e49d1beefc1acb33d5604a249956c9c13709" | |
[[package]] | |
name = "cranelift-codegen" | |
version = "0.110.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "bd7ca95e831c18d1356da783765c344207cbdffea91e13e47fa9327dbb2e0719" | |
dependencies = [ | |
"bumpalo", | |
"cranelift-bforest", | |
"cranelift-bitset", | |
"cranelift-codegen-meta", | |
"cranelift-codegen-shared", | |
"cranelift-control", | |
"cranelift-entity", | |
"cranelift-isle", | |
"gimli", | |
"hashbrown 0.14.5", | |
"log", | |
"regalloc2", | |
"rustc-hash", | |
"smallvec", | |
"target-lexicon", | |
] | |
[[package]] | |
name = "cranelift-codegen-meta" | |
version = "0.110.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "450c105fa1e51bfba4e95a86e926504a867ad5639d63f31d43fe3b7ec1f1c9ef" | |
dependencies = [ | |
"cranelift-codegen-shared", | |
] | |
[[package]] | |
name = "cranelift-codegen-shared" | |
version = "0.110.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "5479117cd1266881479908d383086561cee37e49affbea9b1e6b594cc21cc220" | |
[[package]] | |
name = "cranelift-control" | |
version = "0.110.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "34378804f0abfdd22c068a741cfeed86938b92375b2a96fb0b42c878e0141bfb" | |
dependencies = [ | |
"arbitrary", | |
] | |
[[package]] | |
name = "cranelift-entity" | |
version = "0.110.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "a48cb0a194c9ba82fec35a1e492055388d89b2e3c03dee9dcf2488892be8004d" | |
dependencies = [ | |
"cranelift-bitset", | |
] | |
[[package]] | |
name = "cranelift-frontend" | |
version = "0.110.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "8327afc6c1c05f4be62fefce5b439fa83521c65363a322e86ea32c85e7ceaf64" | |
dependencies = [ | |
"cranelift-codegen", | |
"log", | |
"smallvec", | |
"target-lexicon", | |
] | |
[[package]] | |
name = "cranelift-isle" | |
version = "0.110.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "56b08621c00321efcfa3eee6a3179adc009e21ea8d24ca7adc3c326184bc3f48" | |
[[package]] | |
name = "cranelift-jit" | |
version = "0.110.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "a76d75887b8f762701bc77d42018023d6972bf1963cb5d2cb4a4463f9f3a6da3" | |
dependencies = [ | |
"anyhow", | |
"cranelift-codegen", | |
"cranelift-control", | |
"cranelift-entity", | |
"cranelift-module", | |
"cranelift-native", | |
"libc", | |
"log", | |
"region", | |
"target-lexicon", | |
"wasmtime-jit-icache-coherence", | |
"windows-sys", | |
] | |
[[package]] | |
name = "cranelift-jit-hello-world" | |
version = "0.0.0" | |
dependencies = [ | |
"cranelift", | |
"cranelift-jit", | |
"cranelift-module", | |
"cranelift-native", | |
] | |
[[package]] | |
name = "cranelift-module" | |
version = "0.110.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "2f498a1c666d013c191b42132cf330bd590a2cc5f3c71d016e070af2b50d5601" | |
dependencies = [ | |
"anyhow", | |
"cranelift-codegen", | |
"cranelift-control", | |
] | |
[[package]] | |
name = "cranelift-native" | |
version = "0.110.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "d51180b147c8557c1196c77b098f04140c91962e135ea152cd2fcabf40cf365c" | |
dependencies = [ | |
"cranelift-codegen", | |
"libc", | |
"target-lexicon", | |
] | |
[[package]] | |
name = "equivalent" | |
version = "1.0.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" | |
[[package]] | |
name = "fallible-iterator" | |
version = "0.3.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" | |
[[package]] | |
name = "gimli" | |
version = "0.28.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" | |
dependencies = [ | |
"fallible-iterator", | |
"indexmap", | |
"stable_deref_trait", | |
] | |
[[package]] | |
name = "hashbrown" | |
version = "0.13.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" | |
dependencies = [ | |
"ahash", | |
] | |
[[package]] | |
name = "hashbrown" | |
version = "0.14.5" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" | |
[[package]] | |
name = "indexmap" | |
version = "2.4.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "93ead53efc7ea8ed3cfb0c79fc8023fbb782a5432b52830b6518941cebe6505c" | |
dependencies = [ | |
"equivalent", | |
"hashbrown 0.14.5", | |
] | |
[[package]] | |
name = "libc" | |
version = "0.2.158" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" | |
[[package]] | |
name = "log" | |
version = "0.4.22" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" | |
[[package]] | |
name = "mach" | |
version = "0.3.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa" | |
dependencies = [ | |
"libc", | |
] | |
[[package]] | |
name = "once_cell" | |
version = "1.19.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" | |
[[package]] | |
name = "proc-macro2" | |
version = "1.0.86" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" | |
dependencies = [ | |
"unicode-ident", | |
] | |
[[package]] | |
name = "quote" | |
version = "1.0.36" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" | |
dependencies = [ | |
"proc-macro2", | |
] | |
[[package]] | |
name = "regalloc2" | |
version = "0.9.3" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "ad156d539c879b7a24a363a2016d77961786e71f48f2e2fc8302a92abd2429a6" | |
dependencies = [ | |
"hashbrown 0.13.2", | |
"log", | |
"rustc-hash", | |
"slice-group-by", | |
"smallvec", | |
] | |
[[package]] | |
name = "region" | |
version = "2.2.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "877e54ea2adcd70d80e9179344c97f93ef0dffd6b03e1f4529e6e83ab2fa9ae0" | |
dependencies = [ | |
"bitflags", | |
"libc", | |
"mach", | |
"winapi", | |
] | |
[[package]] | |
name = "rustc-hash" | |
version = "1.1.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" | |
[[package]] | |
name = "slice-group-by" | |
version = "0.3.1" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "826167069c09b99d56f31e9ae5c99049e932a98c9dc2dac47645b08dbbf76ba7" | |
[[package]] | |
name = "smallvec" | |
version = "1.13.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" | |
[[package]] | |
name = "stable_deref_trait" | |
version = "1.2.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" | |
[[package]] | |
name = "syn" | |
version = "2.0.75" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "f6af063034fc1935ede7be0122941bafa9bacb949334d090b77ca98b5817c7d9" | |
dependencies = [ | |
"proc-macro2", | |
"quote", | |
"unicode-ident", | |
] | |
[[package]] | |
name = "target-lexicon" | |
version = "0.12.16" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" | |
[[package]] | |
name = "unicode-ident" | |
version = "1.0.12" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" | |
[[package]] | |
name = "version_check" | |
version = "0.9.5" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" | |
[[package]] | |
name = "wasmtime-jit-icache-coherence" | |
version = "23.0.2" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "2cfee42dac5148fc2664ab1f5cb8d7fa77a28d1a2cf1d9483abc2c3d751a58b9" | |
dependencies = [ | |
"anyhow", | |
"cfg-if", | |
"libc", | |
"windows-sys", | |
] | |
[[package]] | |
name = "winapi" | |
version = "0.3.9" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" | |
dependencies = [ | |
"winapi-i686-pc-windows-gnu", | |
"winapi-x86_64-pc-windows-gnu", | |
] | |
[[package]] | |
name = "winapi-i686-pc-windows-gnu" | |
version = "0.4.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" | |
[[package]] | |
name = "winapi-x86_64-pc-windows-gnu" | |
version = "0.4.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" | |
[[package]] | |
name = "windows-sys" | |
version = "0.52.0" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" | |
dependencies = [ | |
"windows-targets", | |
] | |
[[package]] | |
name = "windows-targets" | |
version = "0.52.6" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" | |
dependencies = [ | |
"windows_aarch64_gnullvm", | |
"windows_aarch64_msvc", | |
"windows_i686_gnu", | |
"windows_i686_gnullvm", | |
"windows_i686_msvc", | |
"windows_x86_64_gnu", | |
"windows_x86_64_gnullvm", | |
"windows_x86_64_msvc", | |
] | |
[[package]] | |
name = "windows_aarch64_gnullvm" | |
version = "0.52.6" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" | |
[[package]] | |
name = "windows_aarch64_msvc" | |
version = "0.52.6" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" | |
[[package]] | |
name = "windows_i686_gnu" | |
version = "0.52.6" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" | |
[[package]] | |
name = "windows_i686_gnullvm" | |
version = "0.52.6" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" | |
[[package]] | |
name = "windows_i686_msvc" | |
version = "0.52.6" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" | |
[[package]] | |
name = "windows_x86_64_gnu" | |
version = "0.52.6" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" | |
[[package]] | |
name = "windows_x86_64_gnullvm" | |
version = "0.52.6" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" | |
[[package]] | |
name = "windows_x86_64_msvc" | |
version = "0.52.6" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" | |
[[package]] | |
name = "zerocopy" | |
version = "0.7.35" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" | |
dependencies = [ | |
"zerocopy-derive", | |
] | |
[[package]] | |
name = "zerocopy-derive" | |
version = "0.7.35" | |
source = "registry+https://github.com/rust-lang/crates.io-index" | |
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" | |
dependencies = [ | |
"proc-macro2", | |
"quote", | |
"syn", | |
] |
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
[package] | |
name = "cranelift-jit-hello-world" | |
version = "0.0.0" | |
publish = false | |
edition = "2021" | |
[dependencies] | |
cranelift = "0.110" | |
cranelift-jit = "0.110" | |
cranelift-module = "0.110" | |
cranelift-native = "0.110" |
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
use std::mem; | |
use cranelift::prelude::*; | |
use cranelift_jit::{JITBuilder, JITModule}; | |
use cranelift_module::{default_libcall_names, Linkage, Module}; | |
pub fn main() { | |
let mut jit_module = JITModule::new(JITBuilder::with_isa( | |
cranelift_native::builder() | |
.unwrap() | |
.finish(settings::Flags::new(settings::builder())) | |
.unwrap(), | |
default_libcall_names(), | |
)); | |
let mut jit_ctx = jit_module.make_context(); | |
let mut builder_ctx = FunctionBuilderContext::new(); | |
let mut func_builder = FunctionBuilder::new(&mut jit_ctx.func, &mut builder_ctx); | |
let block = func_builder.create_block(); | |
func_builder.switch_to_block(block); | |
func_builder.seal_block(block); | |
let mut putchar_sig = jit_module.make_signature(); | |
putchar_sig.params.push(AbiParam::new(types::I8)); | |
putchar_sig.returns.push(AbiParam::new(types::I32)); | |
let putchar_func = jit_module | |
.declare_function("putchar", Linkage::Import, &putchar_sig) | |
.unwrap(); | |
let local_putchar = jit_module.declare_func_in_func(putchar_func, func_builder.func); | |
for c in "Hello, world!\n".chars() { | |
let arg = func_builder.ins().iconst(types::I8, c as i64); | |
func_builder.ins().call(local_putchar, &[arg]); | |
} | |
func_builder.ins().return_(&[]); | |
func_builder.finalize(); | |
let func_id = jit_module | |
.declare_function("main", Linkage::Export, &jit_ctx.func.signature) | |
.unwrap(); | |
jit_module.define_function(func_id, &mut jit_ctx).unwrap(); | |
jit_module.clear_context(&mut jit_ctx); | |
jit_module.finalize_definitions().unwrap(); | |
let code_ptr = jit_module.get_finalized_function(func_id); | |
let code_fn = unsafe { mem::transmute::<*const u8, fn() -> ()>(code_ptr) }; | |
code_fn() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment