From 3116f11814cdecbcd0210805814753fed55f0e4a Mon Sep 17 00:00:00 2001 From: lyken Date: Fri, 12 Jul 2024 21:52:55 +0800 Subject: [PATCH] core: comment build.rs & move irrt to its own dir --- nac3core/build.rs | 36 +++++++++++++++++------- nac3core/{src/codegen => }/irrt/irrt.cpp | 0 2 files changed, 26 insertions(+), 10 deletions(-) rename nac3core/{src/codegen => }/irrt/irrt.cpp (100%) diff --git a/nac3core/build.rs b/nac3core/build.rs index 1f37121f..9d6292a2 100644 --- a/nac3core/build.rs +++ b/nac3core/build.rs @@ -8,7 +8,12 @@ use std::{ }; fn main() { - const FILE: &str = "src/codegen/irrt/irrt.cpp"; + // Define relevant directories + let out_dir = env::var("OUT_DIR").unwrap(); + let out_dir = Path::new(&out_dir); + let irrt_dir = Path::new("irrt"); + + let irrt_cpp_path = irrt_dir.join("irrt.cpp"); /* * HACK: Sadly, clang doesn't let us emit generic LLVM bitcode. @@ -16,7 +21,6 @@ fn main() { */ let flags: &[&str] = &[ "--target=wasm32", - FILE, "-x", "c++", "-fno-discard-value-names", @@ -34,12 +38,13 @@ fn main() { "-Werror=return-type", "-o", "-", + irrt_cpp_path.to_str().unwrap(), ]; - println!("cargo:rerun-if-changed={FILE}"); - let out_dir = env::var("OUT_DIR").unwrap(); - let out_path = Path::new(&out_dir); + // Tell Cargo to rerun if any file under `irrt_dir` (recursive) changes + println!("cargo:rerun-if-changed={}", irrt_dir.to_str().unwrap()); + // Compile IRRT and capture the LLVM IR output let output = Command::new("clang-irrt") .args(flags) .output() @@ -53,6 +58,11 @@ fn main() { let output = std::str::from_utf8(&output.stdout).unwrap().replace("\r\n", "\n"); let mut filtered_output = String::with_capacity(output.len()); + // Filter out irrelevant IR + // + // Regex: + // - `(?ms:^define.*?\}$)` captures LLVM `define` blocks + // - `(?m:^declare.*?$)` captures LLVM `declare` lines let regex_filter = Regex::new(r"(?ms:^define.*?\}$)|(?m:^declare.*?$)").unwrap(); for f in regex_filter.captures_iter(&output) { assert_eq!(f.len(), 1); @@ -64,18 +74,24 @@ fn main() { .unwrap() .replace_all(&filtered_output, ""); - println!("cargo:rerun-if-env-changed=DEBUG_DUMP_IRRT"); - if env::var("DEBUG_DUMP_IRRT").is_ok() { - let mut file = File::create(out_path.join("irrt.ll")).unwrap(); + // For debugging + // Doing `DEBUG_DUMP_IRRT=1 cargo build -p nac3core` dumps the LLVM IR generated + const DEBUG_DUMP_IRRT: &str = "DEBUG_DUMP_IRRT"; + println!("cargo:rerun-if-env-changed={DEBUG_DUMP_IRRT}"); + if env::var(DEBUG_DUMP_IRRT).is_ok() { + let mut file = File::create(out_dir.join("irrt.ll")).unwrap(); file.write_all(output.as_bytes()).unwrap(); - let mut file = File::create(out_path.join("irrt-filtered.ll")).unwrap(); + + let mut file = File::create(out_dir.join("irrt-filtered.ll")).unwrap(); file.write_all(filtered_output.as_bytes()).unwrap(); } + // Assemble the emitted and filtered IR to .bc + // That .bc will be integrated into nac3core's codegen let mut llvm_as = Command::new("llvm-as-irrt") .stdin(Stdio::piped()) .arg("-o") - .arg(out_path.join("irrt.bc")) + .arg(out_dir.join("irrt.bc")) .spawn() .unwrap(); llvm_as.stdin.as_mut().unwrap().write_all(filtered_output.as_bytes()).unwrap(); diff --git a/nac3core/src/codegen/irrt/irrt.cpp b/nac3core/irrt/irrt.cpp similarity index 100% rename from nac3core/src/codegen/irrt/irrt.cpp rename to nac3core/irrt/irrt.cpp