diff --git a/.gitignore b/.gitignore index 269f5a1..b6394d6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ /good /bad +*.ll +*.s +a.out # Added by cargo diff --git a/Cargo.toml b/Cargo.toml index 21023ee..ed1508a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[[bin]] +[lib] name = "code" path = "./code.rs" diff --git a/check.sh b/check.sh index 0198e7f..7e4b9e3 100755 --- a/check.sh +++ b/check.sh @@ -1,7 +1,7 @@ #/usr/bin/env bash -rustc code.rs --crate-name bad -Zmir-enable-passes=-ConstProp -Copt-level=3 -rustc code.rs --crate-name good -Zmir-enable-passes=+ConstProp -Copt-level=3 +rustc code.rs --crate-name bad -Zmir-enable-passes=-ConstProp -Copt-level=3 --cfg pure_rust +rustc code.rs --crate-name good -Zmir-enable-passes=+ConstProp -Copt-level=3 --cfg pure_rust bad=$(./bad) good=$(./good) diff --git a/checkllvm.sh b/checkllvm.sh new file mode 100755 index 0000000..38458f3 --- /dev/null +++ b/checkllvm.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +rustc code.rs --crate-name ll -Zmir-enable-passes=-ConstProp --emit llvm-ir -Cno-prepopulate-passes --crate-type=lib + +clang ll.ll helper.c + +./a.out diff --git a/code.rs b/code.rs index 560e26a..f67856a 100644 --- a/code.rs +++ b/code.rs @@ -1,9 +1,18 @@ use std::ptr; -#[inline(never)] -pub fn print_var(v: u8) { - println!("{v}"); + +extern "C" { + pub fn print_var(v: u8); } -pub unsafe fn fn12_rs() { +#[cfg(pure_rust)] +mod impl_ { + #[no_mangle] + pub extern "C" fn print_var(v: u8) { + println!("{v}"); + } +} + +#[no_mangle] +pub unsafe extern "C" fn fn12_rs() { let mut bool_storage: bool = false; let mut v9: usize = 0; @@ -12,7 +21,8 @@ pub unsafe fn fn12_rs() { let v20_ptr = ptr::addr_of_mut!(v20); let mut v12: *mut u8 = core::ptr::addr_of_mut!((*v20_ptr)[v9]); v9 = 2_usize; // unused but necessary write - loop { // only runs once, but necessary + loop { + // only runs once, but necessary match *v12 { 197 => { let mut match_condition: u64 = 0; @@ -60,8 +70,10 @@ pub unsafe fn fn12_rs() { } } } -pub fn main() { + +#[cfg(pure_rust)] +fn main() { unsafe { fn12_rs(); } -} +} \ No newline at end of file diff --git a/helper.c b/helper.c new file mode 100644 index 0000000..597b917 --- /dev/null +++ b/helper.c @@ -0,0 +1,12 @@ +#include +#include + +void print_var(uint8_t x) { + printf("%d", x); +} + +void fn12_rs(); + +int main() { + fn12_rs(); +} \ No newline at end of file