diff --git a/crates/nyash-llvm-compiler/Cargo.toml b/crates/nyash-llvm-compiler/Cargo.toml index 20c152bf..4efe1197 100644 --- a/crates/nyash-llvm-compiler/Cargo.toml +++ b/crates/nyash-llvm-compiler/Cargo.toml @@ -9,3 +9,6 @@ anyhow = "1.0" clap = { version = "4.5", features = ["derive"] } serde_json = "1.0" +[[bin]] +name = "ny-llvmc" +path = "src/main.rs" diff --git a/src/runner/modes/mir.rs b/src/runner/modes/mir.rs index b60b36ec..0cf4b2f0 100644 --- a/src/runner/modes/mir.rs +++ b/src/runner/modes/mir.rs @@ -64,5 +64,16 @@ impl NyashRunner { println!("🚀 MIR Output for {}:", filename); println!("{}", printer.print_module(&compile_result.module)); } + + // Emit MIR JSON if requested and exit + if let Some(path) = self.config.emit_mir_json.as_ref() { + let p = std::path::Path::new(path); + if let Err(e) = crate::runner::mir_json_emit::emit_mir_json_for_harness(&compile_result.module, p) { + eprintln!("❌ MIR JSON emit error: {}", e); + std::process::exit(1); + } + println!("MIR JSON written: {}", p.display()); + std::process::exit(0); + } } } diff --git a/tools/build_llvm.sh b/tools/build_llvm.sh index b84bad41..8ee80878 100644 --- a/tools/build_llvm.sh +++ b/tools/build_llvm.sh @@ -64,12 +64,16 @@ if [[ "${NYASH_LLVM_SKIP_EMIT:-0}" != "1" ]]; then crate) # Use crates/nyash-llvm-compiler (ny-llvmc): requires pre-generated MIR JSON path in NYASH_LLVM_MIR_JSON if [[ -z "${NYASH_LLVM_MIR_JSON:-}" ]]; then - echo "[warn] NYASH_LLVM_COMPILER=crate but NYASH_LLVM_MIR_JSON is not set; falling back to harness" >&2 - COMPILER_MODE="harness" - else - echo " using ny-llvmc (crate) with JSON: $NYASH_LLVM_MIR_JSON" >&2 - cargo build --release -p nyash-llvm-compiler >/dev/null - # Optional schema validation if tool is available + # Auto‑emit MIR JSON via nyash CLI flag + mkdir -p tmp + NYASH_LLVM_MIR_JSON="tmp/nyash_crate_mir.json" + echo " emitting MIR JSON: $NYASH_LLVM_MIR_JSON" >&2 + ./target/release/nyash --emit-mir-json "$NYASH_LLVM_MIR_JSON" --backend mir "$INPUT" >/dev/null + fi + echo " using ny-llvmc (crate) with JSON: $NYASH_LLVM_MIR_JSON" >&2 + cargo build --release -p nyash-llvm-compiler >/dev/null + # Optional schema validation when explicitly requested + if [[ "${NYASH_LLVM_VALIDATE_JSON:-0}" == "1" ]]; then if [[ -f tools/validate_mir_json.py ]]; then if ! python3 -m jsonschema --version >/dev/null 2>&1; then echo "[warn] jsonschema not available; skipping schema validation" >&2 @@ -79,8 +83,8 @@ if [[ "${NYASH_LLVM_SKIP_EMIT:-0}" != "1" ]]; then echo "error: MIR JSON validation failed" >&2; exit 3; } fi fi - ./target/release/ny-llvmc --in "$NYASH_LLVM_MIR_JSON" --out "$OBJ" fi + ./target/release/ny-llvmc --in "$NYASH_LLVM_MIR_JSON" --out "$OBJ" ;; esac if [[ "$COMPILER_MODE" == "harness" ]]; then @@ -101,6 +105,11 @@ if [[ ! -f "$OBJ" ]]; then exit 3 fi +if [[ "${NYASH_LLVM_ONLY_OBJ:-0}" == "1" ]]; then + echo "[3/4] Skipping link: object generated at $OBJ (NYASH_LLVM_ONLY_OBJ=1)" + exit 0 +fi + echo "[3/4] Building NyRT static runtime ..." if [[ "${NYASH_LLVM_SKIP_NYRT_BUILD:-0}" == "1" ]]; then echo " Skipping NyRT build (NYASH_LLVM_SKIP_NYRT_BUILD=1)"