From 126cf18e82d65141b81c12f5ffb496d37e3b5ee7 Mon Sep 17 00:00:00 2001 From: Selfhosting Dev Date: Sat, 20 Sep 2025 01:30:15 +0900 Subject: [PATCH] macro(if normalize): switch to ControlFlowBuilder for If stmt construction; llvm: add PHI hygiene smoke for If cases --- .../examples/if_match_normalize_macro.nyash | 7 +-- .../test/smoke/llvm/ir_phi_hygiene_ifcases.sh | 51 +++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 tools/test/smoke/llvm/ir_phi_hygiene_ifcases.sh diff --git a/apps/macros/examples/if_match_normalize_macro.nyash b/apps/macros/examples/if_match_normalize_macro.nyash index 20d90786..666814dd 100644 --- a/apps/macros/examples/if_match_normalize_macro.nyash +++ b/apps/macros/examples/if_match_normalize_macro.nyash @@ -8,6 +8,7 @@ static box MacroBoxSpec { expand(json, ctx) { local JB = include "apps/lib/json_builder.nyash" + local CF = include "apps/lib/cf_builder.nyash" // --- helpers copied/adapted from loop_normalize --- function parse_value(s, i) { @@ -225,7 +226,7 @@ static box MacroBoxSpec { local then_s = [ JB.assignment(tgt, th_e) ] local else_s = null if el_e != null { else_s = [ JB.assignment(tgt, el_e) ] } - return JB.if_(cond, then_s, else_s) + return CF.if_stmt(cond, then_s, else_s) } } // Return with If value → If(Return ...) @@ -242,7 +243,7 @@ static box MacroBoxSpec { local then_s = [ JB.return_(th_e) ] local else_s = null if el_e != null { else_s = [ JB.return_(el_e) ] } - return JB.if_(cond, then_s, else_s) + return CF.if_stmt(cond, then_s, else_s) } } // Print with If expression → If(Print ...) @@ -259,7 +260,7 @@ static box MacroBoxSpec { local then_s = [ JB.print_(th_e) ] local else_s = null if el_e != null { else_s = [ JB.print_(el_e) ] } - return JB.if_(cond, then_s, else_s) + return CF.if_stmt(cond, then_s, else_s) } } // Recurse for If/Loop nodes diff --git a/tools/test/smoke/llvm/ir_phi_hygiene_ifcases.sh b/tools/test/smoke/llvm/ir_phi_hygiene_ifcases.sh new file mode 100644 index 00000000..3ded18b3 --- /dev/null +++ b/tools/test/smoke/llvm/ir_phi_hygiene_ifcases.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +set -euo pipefail + +root=$(cd "$(dirname "$0")"/../../../.. && pwd) +bin="$root/target/release/nyash" + +if [ ! -x "$bin" ]; then + echo "nyash binary not found at $bin; build first (cargo build --release --features llvm)" >&2 + exit 1 +fi + +export NYASH_MACRO_ENABLE=1 +export NYASH_MACRO_PATHS="apps/macros/examples/if_match_normalize_macro.nyash" +export NYASH_LLVM_USE_HARNESS=1 + +fails=0 + +check_case() { + local src="$1" + local irfile="$root/tmp/$(basename "$src" .nyash)_llvm.ll" + mkdir -p "$root/tmp" + NYASH_LLVM_DUMP_IR="$irfile" "$bin" --backend llvm "$src" >/dev/null 2>&1 || { + echo "[FAIL] LLVM run failed for $src" >&2 + fails=$((fails+1)) + return + } + if [ ! -s "$irfile" ]; then + echo "[FAIL] IR not dumped for $src" >&2 + fails=$((fails+1)) + return + } + local empty_cnt + empty_cnt=$(rg -n "\\bphi\\b" "$irfile" | rg -v "\\[" | wc -l | tr -d ' ') + if [ "${empty_cnt:-0}" != "0" ]; then + echo "[FAIL] Empty PHI detected in $irfile" >&2 + rg -n "\\bphi\\b" "$irfile" | rg -v "\\[" || true + fails=$((fails+1)) + return + fi + echo "[OK] PHI hygiene (no empty PHI): $(basename "$irfile")" +} + +check_case "apps/tests/macro_golden_if_assign.nyash" +check_case "apps/tests/macro_golden_if_print.nyash" + +if [ "$fails" -ne 0 ]; then + exit 2 +fi +echo "[OK] LLVM PHI hygiene for If-cases passed" +exit 0 +